summaryrefslogtreecommitdiffstats
path: root/tests/Modules/RebootControl/SchedulerTest.php
diff options
context:
space:
mode:
authorSimon Rettberg2025-11-26 10:46:51 +0100
committerSimon Rettberg2025-12-12 15:16:59 +0100
commit7c173411785f959d250d3dfbd7d4cfcb0e20f0e0 (patch)
tree242157791a76afb7af23ec2cd3d22b599e54ce9d /tests/Modules/RebootControl/SchedulerTest.php
parent[exams] Fix incorrect count() clause (diff)
downloadslx-admin-7c173411785f959d250d3dfbd7d4cfcb0e20f0e0.tar.gz
slx-admin-7c173411785f959d250d3dfbd7d4cfcb0e20f0e0.tar.xz
slx-admin-7c173411785f959d250d3dfbd7d4cfcb0e20f0e0.zip
Add tests using PHPUnit
Tests generated by Junie AI. Might not have the best possible quality but at least we got something, and if it turns out to be complete rubbish, we can just throw it out again without any issues, as this is independent of the actual code base.
Diffstat (limited to 'tests/Modules/RebootControl/SchedulerTest.php')
-rw-r--r--tests/Modules/RebootControl/SchedulerTest.php71
1 files changed, 71 insertions, 0 deletions
diff --git a/tests/Modules/RebootControl/SchedulerTest.php b/tests/Modules/RebootControl/SchedulerTest.php
new file mode 100644
index 00000000..af9cadf3
--- /dev/null
+++ b/tests/Modules/RebootControl/SchedulerTest.php
@@ -0,0 +1,71 @@
+<?php
+
+use PHPUnit\Framework\TestCase;
+
+/**
+ * Tests for modules-available/rebootcontrol/inc/scheduler.inc.php
+ * Uses SQLite-backed DB and real Location + OpeningTimes.
+ *
+ */
+class SchedulerTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ Database::resetSchema();
+ Render::reset();
+ Message::reset();
+ User::reset();
+ $_GET = $_POST = $_REQUEST = [];
+ // Use real Location and OpeningTimes
+ $GLOBALS['__TEST_USE_REAL_CLASSES'] = ['Location'];
+ require_once __DIR__ . '/../../../modules-available/locations/inc/location.inc.php';
+ require_once __DIR__ . '/../../../modules-available/locations/inc/openingtimes.inc.php';
+ require_once __DIR__ . '/../../../modules-available/rebootcontrol/inc/scheduler.inc.php';
+ // Ensure campus (1) has opening times already from seed; adjust to predictable short window for test stability
+ $today = date('l');
+ $now = time();
+ $open = date('H:i', $now + 3600); // opens in 1h
+ $close = date('H:i', $now + 7200); // closes in 2h
+ $json = json_encode([[ 'days' => [$today], 'openingtime' => $open, 'closingtime' => $close ]]);
+ Database::exec('UPDATE location SET openingtime = :ot WHERE locationid = 1', ['ot' => $json]);
+ }
+
+ public function testSetLocationOptionsCreatesOrDeletesSchedule(): void
+ {
+ // Initially, no schedule rows
+ $row0 = Database::queryFirst('SELECT COUNT(*) AS c FROM reboot_scheduler');
+ $this->assertSame(0, (int)$row0['c']);
+
+ // Enable WOL with small offset -> creates a schedule with next execution in future
+ Scheduler::setLocationOptions(1, ['wol' => true, 'sd' => false, 'wol-offset' => 5, 'sd-offset' => 0, 'ra-mode' => Scheduler::RA_ALWAYS]);
+ $row = Database::queryFirst('SELECT * FROM reboot_scheduler WHERE locationid = 1');
+ $this->assertNotFalse($row);
+ $this->assertSame('WOL', $row['action']);
+ $this->assertGreaterThan(time(), (int)$row['nextexecution']);
+ $opts = json_decode($row['options'], true);
+ $this->assertIsArray($opts);
+ $this->assertTrue($opts['wol']);
+
+ // Disable all (and RA_ALWAYS) -> deletes schedule
+ Scheduler::setLocationOptions(1, ['wol' => false, 'sd' => false, 'wol-offset' => 0, 'sd-offset' => 0, 'ra-mode' => Scheduler::RA_ALWAYS]);
+ $row2 = Database::queryFirst('SELECT * FROM reboot_scheduler WHERE locationid = 1');
+ $this->assertFalse($row2);
+ }
+
+ public function testUpdateScheduleRecursiveUpdatesChildrenWithoutOwnTimes(): void
+ {
+ // Give child (2) no opening times and write options
+ Database::exec('UPDATE location SET openingtime = NULL WHERE locationid = 2');
+ // Assign options to child 2
+ Scheduler::setLocationOptions(2, ['wol' => true, 'sd' => false, 'wol-offset' => 0, 'sd-offset' => 0, 'ra-mode' => Scheduler::RA_ALWAYS]);
+ // Now modify parent (1) opening time again -> should update child's schedule
+ $today = date('l');
+ $json = json_encode([[ 'days' => [$today], 'openingtime' => '23:59', 'closingtime' => '23:59' ]]);
+ Database::exec('UPDATE location SET openingtime = :ot WHERE locationid = 1', ['ot' => $json]);
+ // Trigger recalculation by setting location options on parent (even if unchanged)
+ Scheduler::setLocationOptions(1, ['wol' => true, 'sd' => false, 'wol-offset' => 0, 'sd-offset' => 0, 'ra-mode' => Scheduler::RA_ALWAYS]);
+ $child = Database::queryFirst('SELECT * FROM reboot_scheduler WHERE locationid = 2');
+ $this->assertNotFalse($child);
+ $this->assertGreaterThanOrEqual(0, (int)$child['nextexecution']);
+ }
+}