diff --git a/ajax/toggleZoneDownload.php b/ajax/toggleZoneDownload.php index feba5601..a3f8fe0a 100644 --- a/ajax/toggleZoneDownload.php +++ b/ajax/toggleZoneDownload.php @@ -30,6 +30,7 @@ * ------------------------------------------------------------------------- */ +use Config as GlpiConfig; use GlpiPlugin\Carbon\Source; use GlpiPlugin\Carbon\Source_Zone; @@ -37,27 +38,32 @@ // Check if plugin is activated... if (!Plugin::isPluginActive('carbon')) { + echo __('Not found.', 'carbon'); http_response_code(404); die(); } -if (!Source::canView()) { +if (!Source::canView() || ! GlpiConfig::canUpdate()) { // Will die + echo __('Access denied.', 'carbon'); http_response_code(403); die(); } if (!isset($_GET['id'])) { + echo __('Bad request.', 'carbon'); http_response_code(400); die(); } $source_zone = new Source_Zone(); if (!$source_zone->getFromDB($_GET['id'])) { + echo __('Item not found.', 'carbon'); http_response_code(403); die(); } if (!$source_zone->toggleZone()) { + echo __('Update failed.', 'carbon'); http_response_code(500); die(); } diff --git a/src/Source_Zone.php b/src/Source_Zone.php index 3d544e41..c485486f 100644 --- a/src/Source_Zone.php +++ b/src/Source_Zone.php @@ -199,6 +199,10 @@ public static function showForSource(CommonDBTM $item) fetch(CFG_GLPI["root_doc"] + "/plugins/carbon/ajax/toggleZoneDownload.php?id=" + id).then(response => { if (response.status === 200) { reloadTab(); + } else { + response.text().then(function (text) { + glpi_toast_error(text) + }); } }); }; @@ -294,6 +298,10 @@ public static function showForZone(CommonDBTM $item) fetch(CFG_GLPI["root_doc"] + "/plugins/carbon/ajax/toggleZoneDownload.php?id=" + id).then(response => { if (response.status === 200) { reloadTab(); + } else { + response.text().then(function (text) { + glpi_toast_error(text) + }); } }); }; diff --git a/tests/units/Source_ZoneTest.php b/tests/units/Source_ZoneTest.php index 9bd17eb9..5c80d634 100644 --- a/tests/units/Source_ZoneTest.php +++ b/tests/units/Source_ZoneTest.php @@ -43,9 +43,6 @@ #[CoversClass(Source_Zone::class)] class Source_ZoneTest extends DbTestCase { - /** - * #CoversMethod GlpiPlugin\Carbon\Source_Zone::showForSource - */ public function testShowForSource() { $source = $this->createItem(Source::class, [ @@ -63,30 +60,25 @@ public function testShowForSource() $this->logout(); ob_start(); - $result = $instance->showForSource($source); + $instance->showForSource($source); $output = ob_get_clean(); $this->assertEquals('', $output); $this->login('glpi', 'glpi'); ob_start(); - $result = $instance->showForSource($source); + $instance->showForSource($source); $output = ob_get_clean(); $this->assertNotEmpty($output); } - /** - * #CoversMethod GlpiPlugin\Carbon\Source_Zone::showForZone - */ - public function testShowForZone() + public function test_showForZone_shows_nothing_when_user_cannot_view_the_related_zone() { $source = $this->createItem(Source::class, [ 'name' => 'foo', ]); - $zone = $this->createItem(Zone::class, [ 'name' => 'bar', ]); - $instance = $this->createItem(Source_Zone::class, [ $source::getForeignKeyField() => $source->getID(), $zone::getForeignKeyField() => $zone->getID(), @@ -94,13 +86,27 @@ public function testShowForZone() $this->logout(); ob_start(); - $result = $instance->showForZone($zone); + $instance->showForZone($zone); $output = ob_get_clean(); $this->assertEquals('', $output); + } + + public function test_showForZone_shows_nothing_when_user_can_view_the_related_zone() + { + $source = $this->createItem(Source::class, [ + 'name' => 'foo', + ]); + $zone = $this->createItem(Zone::class, [ + 'name' => 'bar', + ]); + $instance = $this->createItem(Source_Zone::class, [ + $source::getForeignKeyField() => $source->getID(), + $zone::getForeignKeyField() => $zone->getID(), + ]); $this->login('glpi', 'glpi'); ob_start(); - $result = $instance->showForZone($zone); + $instance->showForZone($zone); $output = ob_get_clean(); $this->assertNotEmpty($output); } @@ -279,9 +285,9 @@ public function testGetOrCreate() $source_fk => $source->getID(), $zone_fk => $zone->getID(), ]; - $this->count(0, $instance->find($where)); + $this->assertEquals(0, count($instance->find($where))); $instance->getOrCreate([], $where); - $this->count(1, $instance->find($where)); + $this->assertEquals(1, count($instance->find($where))); // Test we find an existing instance $instance_2 = new Source_Zone(); @@ -293,4 +299,63 @@ public function testGetOrCreate() $instance_3->getOrCreate(['code' => 'FOO'], $where); $this->assertEquals('FOO', $instance_3->fields['code']); } + + public function test_toggleZone_flips_the_download_flag() + { + $zone = $this->createItem(Zone::class); + $source = $this->createItem(Source::class); + $source_zone = $this->createItem(Source_Zone::class, [ + $zone::getForeignKeyField() => $zone->getID(), + $source::getForeignKeyField() => $source->getID(), + ]); + + $this->assertEquals(0, $source_zone->fields['is_download_enabled']); + $success = $source_zone->toggleZone(); + $this->assertTrue($success); + $this->assertEquals(1, $source_zone->fields['is_download_enabled']); + } + + public function test_toggleZone_disables_the_download_flag() + { + $zone = $this->createItem(Zone::class); + $source = $this->createItem(Source::class); + $source_zone = $this->createItem(Source_Zone::class, [ + $zone::getForeignKeyField() => $zone->getID(), + $source::getForeignKeyField() => $source->getID(), + 'is_download_enabled' => 1 + ]); + + // If the flas is set, it should reset + $this->assertEquals(1, $source_zone->fields['is_download_enabled']); + $success = $source_zone->toggleZone(false); + $this->assertTrue($success); + $this->assertEquals(0, $source_zone->fields['is_download_enabled']); + + // If the flag is reset it should stay reset + $success = $source_zone->toggleZone(false); + $this->assertTrue($success); + $this->assertEquals(0, $source_zone->fields['is_download_enabled']); + } + + public function test_toggleZone_enables_the_download_flag() + { + $zone = $this->createItem(Zone::class); + $source = $this->createItem(Source::class); + $source_zone = $this->createItem(Source_Zone::class, [ + $zone::getForeignKeyField() => $zone->getID(), + $source::getForeignKeyField() => $source->getID(), + 'is_download_enabled' => 0 + ]); + + // If the flas is reset, it should set + $this->assertEquals(0, $source_zone->fields['is_download_enabled']); + $success = $source_zone->toggleZone(true); + $this->assertTrue($success); + $this->assertEquals(1, $source_zone->fields['is_download_enabled']); + + // If the flas is set, it should stay set + $success = $source_zone->toggleZone(true); + $this->assertTrue($success); + $this->assertEquals(1, $source_zone->fields['is_download_enabled']); + } }