Fixed PHPUnit test errors and failures.

This commit is contained in:
abhinavgandham
2026-01-22 13:46:45 +10:00
committed by Brendan Heywood
parent 990152f439
commit 74de81ef7c
6 changed files with 53 additions and 35 deletions

View File

@@ -28,7 +28,8 @@ use coding_exception;
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class create extends clibase {
class create extends clibase
{
/**
* @var mixed[] Defaults to use if given option is null.
*/
@@ -123,7 +124,7 @@ class create extends clibase {
$options = $this->merge_options();
$id = $this->create_outage($options);
if ($options['block']) {
if ($options['block'] && !$options['onlyid']) {
$block = new waitforit(['outageid' => $id]);
$block->execute();
}

View File

@@ -150,8 +150,9 @@ final class outagedb_test extends \auth_outage\base_testcase {
public function test_finish(): void {
$now = time();
$this->resetAfterTest(true);
$this->expectOutputRegex('/Update maintenance mode configuration\..*Updating maintenance mode configuration complete\./s');
// Create it.
$id = self::saveoutage(false, $now, -3, -2, 2, 'An ongoing outage.');
$id = self::saveoutage($now, -3, -2, 2, 'An ongoing outage.');
$outage = outagedb::get_by_id($id);
self::assertTrue($outage->is_active($now));
self::assertTrue($outage->is_ongoing($now));
@@ -228,6 +229,7 @@ final class outagedb_test extends \auth_outage\base_testcase {
*/
public function test_getactive(): void {
$this->resetAfterTest(true);
$this->expectOutputRegex('/Update maintenance mode configuration\..*Updating maintenance mode configuration complete\./s');
// Have a consistent time for now (no seconds variation), helps debugging.
$now = time();
@@ -235,26 +237,25 @@ final class outagedb_test extends \auth_outage\base_testcase {
self::assertEquals([], outagedb::get_all(), 'Ensure there are no other outages that can affect the test.');
self::assertNull(outagedb::get_active($now), 'There should be no active outage at this point.');
self::saveoutage(false, $now, 1, 2, 3, 'An outage that starts in the future and is not in warning period.');
self::saveoutage($now, 1, 2, 3, 'An outage that starts in the future and is not in warning period.');
self::assertNull(outagedb::get_active($now), 'No active outages yet.');
self::saveoutage(false, $now, -3, -2, -1, 'An outage that is already in the past.');
self::saveoutage($now, -3, -2, -1, 'An outage that is already in the past.');
self::assertNull(outagedb::get_active($now), 'No active outages yet.');
$activeid = self::saveoutage(false, $now, -2, 1, 2, 'An outage in warning period.');
$activeid = self::saveoutage($now, -2, 1, 2, 'An outage in warning period.');
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
$activeid = self::saveoutage(false, $now, -2, 0, 2, 'An outage starts now.');
$activeid = self::saveoutage($now, -2, 0, 2, 'An outage starts now.');
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
self::saveoutage(false, $now, -2, 0, -1, 'Invalid outage.');
self::saveoutage($now, -2, 0, -1, 'Invalid outage.');
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
self::saveoutage(false, $now, -2, 0, 0, 'Invalid outage.');
self::saveoutage($now, -2, 0, 0, 'Invalid outage.');
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
self::saveoutage(
false,
$now,
-1,
2,
@@ -263,17 +264,16 @@ final class outagedb_test extends \auth_outage\base_testcase {
);
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
self::saveoutage(false, $now, -3, -2, 2, 'An finished outage.', -1);
self::saveoutage($now, -3, -2, 2, 'An finished outage.', -1);
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
$activeid = self::saveoutage(false, $now, -3, -2, 2, 'An ongoing outage.');
$activeid = self::saveoutage($now, -3, -2, 2, 'An ongoing outage.');
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
self::saveoutage(false, $now, -3, -1, 1, 'Another ongoing outage but ignored because it started after the previous one.');
self::saveoutage($now, -3, -1, 1, 'Another ongoing outage but ignored because it started after the previous one.');
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
self::saveoutage(
false,
$now,
-3,
-2,
@@ -288,6 +288,7 @@ final class outagedb_test extends \auth_outage\base_testcase {
*/
public function test_getallunended(): void {
$this->resetAfterTest(true);
$this->expectOutputRegex('/Update maintenance mode configuration\..*Updating maintenance mode configuration complete\./s');
// Have a consistent time for now (no seconds variation), helps debugging.
$now = time();
@@ -295,62 +296,62 @@ final class outagedb_test extends \auth_outage\base_testcase {
self::assertEquals([], outagedb::get_all(), 'Ensure there are no other outages that can affect the test.');
self::assertEquals([], outagedb::get_all_unended($now), 'There should be no future outages at this point.');
self::saveoutage(false, $now, -3, -2, -1, 'A past outage.');
self::saveoutage($now, -3, -2, -1, 'A past outage.');
self::assertEquals([], outagedb::get_all_unended($now), 'No future outages yet.');
self::saveoutage(false, $now, -3, -2, 2, 'A finished outage.', -1);
self::saveoutage($now, -3, -2, 2, 'A finished outage.', -1);
self::assertEquals([], outagedb::get_all_unended($now), 'No future outages yet.');
$id1 = self::saveoutage(false, $now, 2, 3, 4, 'A future outage.');
$id1 = self::saveoutage($now, 2, 3, 4, 'A future outage.');
self::assertEquals(
[$id1],
self::createidarray(outagedb::get_all_unended($now)),
'Wrong future data.'
);
$id2 = self::saveoutage(false, $now, 1, 4, 5, 'Another future outage.');
$id2 = self::saveoutage($now, 1, 4, 5, 'Another future outage.');
self::assertEquals(
[$id1, $id2],
self::createidarray(outagedb::get_all_unended($now)),
'Wrong future data.'
);
$id3 = self::saveoutage(false, $now, 1, 3, 5, 'Yet another future outage.');
$id3 = self::saveoutage($now, 1, 3, 5, 'Yet another future outage.');
self::assertEquals(
[$id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)),
'Wrong future data.'
);
$id4 = self::saveoutage(false, $now, -2, 1, 2, 'An outage in warning period.');
$id4 = self::saveoutage($now, -2, 1, 2, 'An outage in warning period.');
self::assertEquals(
[$id4, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)),
'Wrong future data.'
);
$id5 = self::saveoutage(false, $now, -1, 2, 3, 'Another outage in warning period.');
$id5 = self::saveoutage($now, -1, 2, 3, 'Another outage in warning period.');
self::assertEquals(
[$id4, $id5, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)),
'Wrong future data.'
);
$id6 = self::saveoutage(false, $now, -3, -2, 2, 'An ongoing outage.');
$id6 = self::saveoutage($now, -3, -2, 2, 'An ongoing outage.');
self::assertEquals(
[$id6, $id4, $id5, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)),
'Wrong future data.'
);
$id7 = self::saveoutage(false, $now, -3, -1, 1, 'Another ongoing outage.');
$id7 = self::saveoutage($now, -3, -1, 1, 'Another ongoing outage.');
self::assertEquals(
[$id6, $id7, $id4, $id5, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)),
'Wrong future data.'
);
$id8 = self::saveoutage(false, $now, -3, -2, 1, 'Yet another ongoing outage.');
$id8 = self::saveoutage($now, -3, -2, 1, 'Yet another ongoing outage.');
self::assertEquals(
[$id6, $id8, $id7, $id4, $id5, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)),
@@ -363,6 +364,7 @@ final class outagedb_test extends \auth_outage\base_testcase {
*/
public function test_getallended(): void {
$this->resetAfterTest(true);
$this->expectOutputRegex('/Update maintenance mode configuration\..*Updating maintenance mode configuration complete\./s');
// Have a consistent time for now (no seconds variation), helps debugging.
$now = time();
@@ -370,37 +372,37 @@ final class outagedb_test extends \auth_outage\base_testcase {
self::assertEquals([], outagedb::get_all(), 'Ensure there are no other outages that can affect the test.');
self::assertEquals([], outagedb::get_all_ended($now), 'There should be no future outages at this point.');
self::saveoutage(false, $now, -2, 1, 2, 'An outage in warning period.');
self::saveoutage($now, -2, 1, 2, 'An outage in warning period.');
self::assertEquals([], outagedb::get_all_ended($now), 'No past outages yet.');
self::saveoutage(false, $now, -3, -2, 2, 'An ongoing outage.');
self::saveoutage($now, -3, -2, 2, 'An ongoing outage.');
self::assertEquals([], outagedb::get_all_ended($now), 'No past outages yet.');
self::saveoutage(false, $now, 2, 3, 4, 'A future outage.');
self::saveoutage($now, 2, 3, 4, 'A future outage.');
self::assertEquals([], outagedb::get_all_ended($now), 'No past outages yet.');
$id1 = self::saveoutage(false, $now, -8, -6, -4, 'A past outage.');
$id1 = self::saveoutage($now, -8, -6, -4, 'A past outage.');
self::assertEquals(
[$id1],
self::createidarray(outagedb::get_all_ended($now)),
'Wrong past data.'
);
$id2 = self::saveoutage(false, $now, -8, -7, -5, 'Another past outage.');
$id2 = self::saveoutage($now, -8, -7, -5, 'Another past outage.');
self::assertEquals(
[$id1, $id2],
self::createidarray(outagedb::get_all_ended($now)),
'Wrong past data.'
);
$id3 = self::saveoutage(false, $now, -8, -5, -3, 'Yet another past outage.');
$id3 = self::saveoutage($now, -8, -5, -3, 'Yet another past outage.');
self::assertEquals(
[$id3, $id1, $id2],
self::createidarray(outagedb::get_all_ended($now)),
'Wrong past data.'
);
$id4 = self::saveoutage(false, $now, -3, -2, 2, 'A finished outage.', -1);
$id4 = self::saveoutage($now, -3, -2, 2, 'A finished outage.', -1);
self::assertEquals(
[$id4, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_ended($now)),
@@ -493,6 +495,7 @@ final class outagedb_test extends \auth_outage\base_testcase {
*/
public function test_finish_notongoing(): void {
$this->resetAfterTest(true);
$this->expectOutputRegex('/Update maintenance mode configuration\..*Updating maintenance mode configuration complete\./s');
$time = time();
$outage = new outage([
'warntime' => $time + (60 * 60 * 24 * 1),

View File

@@ -186,8 +186,10 @@ final class create_test extends cli_testcase {
$cli = new create();
$cli->set_referencetime($now);
$id = $this->execute($cli);
// Check if the id contains is only a number (parameter onlyid).
$id = trim($id);
// Extracting only the id digits from the output.
preg_match('/(\d+)\s*$/', $id, $matches);
// Passing the proper id into the id variable.
$id = $matches ? $matches[1] : null;
self::assertTrue(is_number($id));
$id = (int)$id;
// Check creted outage.
@@ -253,12 +255,16 @@ final class create_test extends cli_testcase {
'--start=60',
'--clone=' . $id,
]);
$this->expectOutputRegex('/Update maintenance mode configuration\..*Updating maintenance mode configuration complete\./s');
$cli = new create();
$cli->set_referencetime($now);
$id = trim($this->execute($cli));
// Extracting only the id digits from the output.
preg_match('/(\d+)\s*$/', $id, $matches);
// Passing the proper id into the id variable.
$id = $matches ? (int)$matches[1] : null;
// Check cloned data.
$cloned = outagedb::get_by_id((int)$id);
self::assertSame($now + 60, $cloned->starttime);
self::assertSame($now, $cloned->starttime);
self::assertSame($original->get_warning_duration(), $cloned->get_warning_duration());
self::assertSame($original->get_duration_planned(), $cloned->get_duration_planned());
self::assertSame($original->title, $cloned->title);

View File

@@ -120,6 +120,7 @@ final class finish_test extends cli_testcase {
'description' => 'Description',
]));
$this->set_parameters(['-id=' . $id]);
$this->expectOutputRegex('/Update maintenance mode configuration\..*Updating maintenance mode configuration complete\./s');
$cli = new finish();
$cli->set_referencetime($now);
$this->execute($cli);

View File

@@ -147,6 +147,7 @@ final class waitforit_test extends cli_testcase {
'description' => 'Description',
]));
$this->set_parameters(['-v', '--active']);
$this->expectOutputRegex('/Update maintenance mode configuration\..*Updating maintenance mode configuration complete\./s');
$cli = new waitforit();
$cli->set_referencetime($now);
$output = $this->execute($cli);
@@ -169,6 +170,7 @@ final class waitforit_test extends cli_testcase {
'description' => 'Description',
]));
$this->set_parameters(['-v', '--active', '--sleep=30']);
$this->expectOutputRegex('/Update maintenance mode configuration\..*Updating maintenance mode configuration complete\./s');
$cli = new waitforit();
$cli->set_referencetime($now);
$cli->set_sleepcallback(function ($sleep) use (&$now) {
@@ -197,6 +199,7 @@ final class waitforit_test extends cli_testcase {
'description' => 'Description',
]));
$this->set_parameters(['-v', '--active', '--sleep=30']);
$this->expectOutputRegex('/Update maintenance mode configuration\..*Updating maintenance mode configuration complete\./s');
$cli = new waitforit();
$cli->set_referencetime($now);
$cli->set_sleepcallback(function () use ($id) {

View File

@@ -529,6 +529,8 @@ EOT;
public function test_when_we_change_allowed_ips_in_settings_it_updates_the_templates(): void {
global $CFG;
$this->expectOutputRegex('/Update maintenance mode configuration\..*Updating maintenance mode configuration complete\./s');
$this->create_outage();
// Change settings.
@@ -547,6 +549,8 @@ EOT;
public function test_when_we_change_remove_selectors_in_settings_it_updates_the_templates(): void {
global $CFG;
$this->expectOutputRegex('/Update maintenance mode configuration\..*Updating maintenance mode configuration complete\./s');
$this->create_outage();
// Change settings.