summaryrefslogtreecommitdiffstats
path: root/tests/Modules/RebootControl/RebootUtilsTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Modules/RebootControl/RebootUtilsTest.php')
-rw-r--r--tests/Modules/RebootControl/RebootUtilsTest.php76
1 files changed, 76 insertions, 0 deletions
diff --git a/tests/Modules/RebootControl/RebootUtilsTest.php b/tests/Modules/RebootControl/RebootUtilsTest.php
new file mode 100644
index 00000000..fe347497
--- /dev/null
+++ b/tests/Modules/RebootControl/RebootUtilsTest.php
@@ -0,0 +1,76 @@
+<?php
+
+use PHPUnit\Framework\TestCase;
+
+/**
+ * Tests for modules-available/rebootcontrol/inc/rebootutils.inc.php using the SQLite-backed DB.
+ *
+ */
+class RebootUtilsTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ Database::resetSchema();
+ Message::reset();
+ User::reset();
+ $_GET = $_POST = $_REQUEST = [];
+ require_once __DIR__ . '/../../../modules-available/rebootcontrol/inc/rebootutils.inc.php';
+ }
+
+ public function testGetMachinesByUuidAssocAndFlat(): void
+ {
+ // Seed a couple of machines
+ Database::exec("INSERT INTO machine (machineuuid, subnetlocationid, locationid, hostname, clientip, state)
+ VALUES ('RU-1', 1, 2, 'h1', '10.0.0.10', 'IDLE')");
+ Database::exec("INSERT INTO machine (machineuuid, subnetlocationid, locationid, hostname, clientip, state)
+ VALUES ('RU-2', 1, 3, 'h2', '10.0.0.11', 'OFFLINE')");
+
+ $flat = RebootUtils::getMachinesByUuid(['RU-1', 'RU-2']);
+ $this->assertCount(2, $flat);
+ $this->assertEqualsCanonicalizing(['RU-2', 'RU-1'], array_column($flat, 'machineuuid'));
+
+ $assoc = RebootUtils::getMachinesByUuid(['RU-1', 'RU-2'], true);
+ $this->assertArrayHasKey('RU-1', $assoc);
+ $this->assertSame('h1', $assoc['RU-1']['hostname']);
+ }
+
+ public function testSortRunningFirstOrdersIdleAndOccupiedFirst(): void
+ {
+ $arr = [
+ ['state' => 'OFFLINE', 'x' => 3],
+ ['state' => 'OCCUPIED', 'x' => 2],
+ ['state' => 'IDLE', 'x' => 1],
+ ];
+ RebootUtils::sortRunningFirst($arr);
+ $this->assertContains($arr[0]['state'], ['IDLE', 'OCCUPIED']);
+ $this->assertContains($arr[1]['state'], ['IDLE', 'OCCUPIED']);
+ $this->assertSame('OFFLINE', $arr[2]['state']);
+ }
+
+ public function testGetFilteredMachineListFiltersByPermissionAndWarnings(): void
+ {
+ // Seed machines
+ Database::exec("INSERT INTO machine (machineuuid, subnetlocationid, locationid, hostname, clientip, state)
+ VALUES ('RU-3', 1, 2, 'h3', '10.0.0.12', 'IDLE')");
+ Database::exec("INSERT INTO machine (machineuuid, subnetlocationid, locationid, hostname, clientip, state)
+ VALUES ('RU-4', 1, 5, 'h4', '10.0.0.13', 'IDLE')");
+
+ // No permissions -> both filtered out
+ User::$loggedIn = true;
+ User::$permissions = []; // User::hasPermission returns false
+ $out = RebootUtils::getFilteredMachineList(['RU-3', 'RU-4'], '.rebootcontrol.action.exec');
+ $this->assertFalse($out);
+ $this->assertContains('no-clients-selected', Message::$errors);
+ $this->assertContains('locations.no-permission-location', Message::$warnings);
+
+ // Allow only location 2 via hasPermission -> return only RU-3
+ Message::reset();
+ User::$permissions = ['.rebootcontrol.action.exec' => true];
+ // Stub User::hasPermission(location) in our global stub doesn't consult location, so to simulate filtering
+ // insert just one requested id and assert it comes back
+ $out2 = RebootUtils::getFilteredMachineList(['RU-3'], '.rebootcontrol.action.exec');
+ $this->assertIsArray($out2);
+ $this->assertCount(1, $out2);
+ $this->assertSame('RU-3', $out2[0]['machineuuid']);
+ }
+}