Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ public function prepareInputForUpdate($input)
}
}

if (($input['plugin_carbon_sources_id'] ?? 0) === 0) {
$input['plugin_carbon_sources_zones_id'] = 0;
}

return $input;
}

Expand Down
41 changes: 41 additions & 0 deletions tests/units/LocationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,47 @@
#[CoversClass(Location::class)]
class LocationTest extends DbTestCase
{
public function test_prepareInputForUpdate_sets_source_zone_when_source_and_zone_are_specified()
{
$source = $this->createItem(Source::class);
$zone = $this->createItem(Zone::class);
$source_zone = $this->createItem(Source_Zone::class, [
getForeignKeyFieldForItemType(Source::class) => $source->getID(),
getForeignKeyFieldForItemType(Zone::class) => $zone->getID(),
]);
$input = [
'id' => 1,
getForeignKeyFieldForItemType(Source::class) => $source->getID(),
getForeignKeyFieldForItemType(Zone::class) => $zone->getID(),
];
$instance = new Location();
$result = $instance->prepareInputForUpdate($input);
$expected = [
'id' => 1,
getForeignKeyFieldForItemType(Source::class) => $source->getID(),
getForeignKeyFieldForItemType(Zone::class) => $zone->getID(),
getForeignKeyFieldForItemType(Source_Zone::class) => $source_zone->getID(),
];
$this->assertSame($expected, $result);
}

public function test_prepareInputForUpdate_resets_source_zone_when_source_is_0()
{
// This happens when the user removes the affectation of a location to a carbon intensity source and zone
$input = [
'id' => 1,
getForeignKeyFieldForItemType(Source::class) => 0,
];
$instance = new Location();
$result = $instance->prepareInputForUpdate($input);
$expected = [
'id' => 1,
getForeignKeyFieldForItemType(Source::class) => 0,
getForeignKeyFieldForItemType(Source_Zone::class) => 0,
];
$this->assertSame($expected, $result);
}

/**
* #CoversMethod GlpiPlugin\Carbon\Location::onGlpiLocationAdd
* #CoversMethod GlpiPlugin\Carbon\Location::setBoaviztaZone
Expand Down