Skip to content
Closed
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
8 changes: 7 additions & 1 deletion ajax/toggleZoneDownload.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,40 @@
* -------------------------------------------------------------------------
*/

use Config as GlpiConfig;
use GlpiPlugin\Carbon\Source;
use GlpiPlugin\Carbon\Source_Zone;

include(__DIR__ . '/../../../inc/includes.php');

// 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();
}
8 changes: 8 additions & 0 deletions src/Source_Zone.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});
}
});
};
Expand Down Expand Up @@ -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)
});
}
});
};
Expand Down
95 changes: 80 additions & 15 deletions tests/units/Source_ZoneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, [
Expand All @@ -63,44 +60,53 @@ 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(),
]);

$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);
}
Expand Down Expand Up @@ -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();
Expand All @@ -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']);
}
}
Loading