mirror of
https://github.com/catalyst/moodle-auth_outage.git
synced 2026-05-17 05:48:43 +02:00
Issue #30 - Add finished flag to mark the real time that an outage was finished.
This commit is contained in:
@@ -114,4 +114,70 @@ class outage_test extends basic_testcase {
|
||||
]);
|
||||
self::assertFalse($outage->is_active($now));
|
||||
}
|
||||
|
||||
public function test_stages() {
|
||||
$now = time();
|
||||
|
||||
$outage = new outage([
|
||||
'warntime' => $now + 10,
|
||||
'starttime' => $now + 20,
|
||||
'stoptime' => $now + 30,
|
||||
'title' => 'Outage Waiting',
|
||||
]);
|
||||
self::assertSame(outage::STAGE_WAITING, $outage->get_stage($now));
|
||||
self::assertFalse($outage->is_active($now));
|
||||
self::assertFalse($outage->is_ongoing($now));
|
||||
|
||||
$outage = new outage([
|
||||
'warntime' => $now - 10,
|
||||
'starttime' => $now + 20,
|
||||
'stoptime' => $now + 30,
|
||||
'title' => 'Outage Warning',
|
||||
]);
|
||||
self::assertSame(outage::STAGE_WARNING, $outage->get_stage($now));
|
||||
self::assertTrue($outage->is_active($now));
|
||||
self::assertFalse($outage->is_ongoing($now));
|
||||
|
||||
$outage = new outage([
|
||||
'warntime' => $now - 20,
|
||||
'starttime' => $now - 10,
|
||||
'stoptime' => $now + 30,
|
||||
'title' => 'Outage Ongoing',
|
||||
]);
|
||||
self::assertSame(outage::STAGE_ONGOING, $outage->get_stage($now));
|
||||
self::assertTrue($outage->is_active($now));
|
||||
self::assertTrue($outage->is_ongoing($now));
|
||||
|
||||
$outage = new outage([
|
||||
'warntime' => $now - 50,
|
||||
'starttime' => $now - 40,
|
||||
'stoptime' => $now - 30,
|
||||
'title' => 'Outage Stopped',
|
||||
]);
|
||||
self::assertSame(outage::STAGE_STOPPED, $outage->get_stage($now));
|
||||
self::assertFalse($outage->is_active($now));
|
||||
self::assertFalse($outage->is_ongoing($now));
|
||||
|
||||
$outage = new outage([
|
||||
'warntime' => $now - 50,
|
||||
'starttime' => $now - 40,
|
||||
'finishtime' => $now - 30,
|
||||
'stoptime' => $now - 20,
|
||||
'title' => 'Outage Finished before Stop',
|
||||
]);
|
||||
self::assertSame(outage::STAGE_FINISHED, $outage->get_stage($now));
|
||||
self::assertFalse($outage->is_active($now));
|
||||
self::assertFalse($outage->is_ongoing($now));
|
||||
|
||||
$outage = new outage([
|
||||
'warntime' => $now - 50,
|
||||
'starttime' => $now - 40,
|
||||
'stoptime' => $now - 30,
|
||||
'finishtime' => $now - 20,
|
||||
'title' => 'Outage Finished after Stop',
|
||||
]);
|
||||
self::assertSame(outage::STAGE_FINISHED, $outage->get_stage($now));
|
||||
self::assertFalse($outage->is_active($now));
|
||||
self::assertFalse($outage->is_ongoing($now));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,6 +92,26 @@ class outagedb_test extends advanced_testcase {
|
||||
self::assertNull(outagedb::get_by_id($id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure we can finish outages.
|
||||
*/
|
||||
public function test_finish() {
|
||||
$now = time();
|
||||
$this->resetAfterTest(true);
|
||||
// Create it.
|
||||
$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));
|
||||
self::assertSame(null, $outage->finished);
|
||||
// Finish it.
|
||||
outagedb::finish($id, $now);
|
||||
$outage = outagedb::get_by_id($id);
|
||||
self::assertSame($now, $outage->finished);
|
||||
self::assertFalse($outage->is_active($now));
|
||||
self::assertFalse($outage->is_ongoing($now));
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure getall brings all entries.
|
||||
*/
|
||||
@@ -176,6 +196,9 @@ class outagedb_test extends advanced_testcase {
|
||||
'Another outage in warning period, but ignored as it starts after the previous one.');
|
||||
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
|
||||
|
||||
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($now, -3, -2, 2,
|
||||
'An ongoing outage.');
|
||||
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
|
||||
@@ -189,97 +212,106 @@ class outagedb_test extends advanced_testcase {
|
||||
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
|
||||
}
|
||||
|
||||
public function test_getallfuture() {
|
||||
public function test_getallunended() {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
// Have a consistent time for now (no seconds variation), helps debugging.
|
||||
$now = time();
|
||||
|
||||
self::assertEquals([], outagedb::get_all(), 'Ensure there are no other outages that can affect the test.');
|
||||
self::assertEquals([], outagedb::get_all_future($now), 'There should be no future outages at this point.');
|
||||
self::assertEquals([], outagedb::get_all_unended($now), 'There should be no future outages at this point.');
|
||||
|
||||
self::saveoutage($now, -3, -2, -1, 'A past outage.');
|
||||
self::assertEquals([], outagedb::get_all_future($now), 'No future outages yet.');
|
||||
self::assertEquals([], outagedb::get_all_unended($now), 'No future outages yet.');
|
||||
|
||||
self::saveoutage($now, -3, -2, 2, 'A finished outage.', -1);
|
||||
self::assertEquals([], outagedb::get_all_unended($now), 'No future outages yet.');
|
||||
|
||||
$id1 = self::saveoutage($now, 2, 3, 4, 'A future outage.');
|
||||
self::assertEquals([$id1],
|
||||
self::createidarray(outagedb::get_all_future($now)), 'Wrong future data.');
|
||||
self::createidarray(outagedb::get_all_unended($now)), 'Wrong future data.');
|
||||
|
||||
$id2 = self::saveoutage($now, 1, 4, 5, 'Another future outage.');
|
||||
self::assertEquals([$id1, $id2],
|
||||
self::createidarray(outagedb::get_all_future($now)), 'Wrong future data.');
|
||||
self::createidarray(outagedb::get_all_unended($now)), 'Wrong future data.');
|
||||
|
||||
$id3 = self::saveoutage($now, 1, 3, 5, 'Yet another future outage.');
|
||||
self::assertEquals([$id3, $id1, $id2],
|
||||
self::createidarray(outagedb::get_all_future($now)), 'Wrong future data.');
|
||||
self::createidarray(outagedb::get_all_unended($now)), 'Wrong future data.');
|
||||
|
||||
$id4 = self::saveoutage($now, -2, 1, 2, 'An outage in warning period.');
|
||||
self::assertEquals([$id4, $id3, $id1, $id2],
|
||||
self::createidarray(outagedb::get_all_future($now)), 'Wrong future data.');
|
||||
self::createidarray(outagedb::get_all_unended($now)), 'Wrong future data.');
|
||||
|
||||
$id5 = self::saveoutage($now, -1, 2, 3, 'Another outage in warning period.');
|
||||
self::assertEquals([$id4, $id5, $id3, $id1, $id2],
|
||||
self::createidarray(outagedb::get_all_future($now)), 'Wrong future data.');
|
||||
self::createidarray(outagedb::get_all_unended($now)), 'Wrong future data.');
|
||||
|
||||
$id6 = self::saveoutage($now, -3, -2, 2, 'An ongoing outage.');
|
||||
self::assertEquals([$id6, $id4, $id5, $id3, $id1, $id2],
|
||||
self::createidarray(outagedb::get_all_future($now)), 'Wrong future data.');
|
||||
self::createidarray(outagedb::get_all_unended($now)), 'Wrong future data.');
|
||||
|
||||
$id7 = self::saveoutage($now, -3, -1, 1, 'Another ongoing outage.');
|
||||
self::assertEquals([$id6, $id7, $id4, $id5, $id3, $id1, $id2],
|
||||
self::createidarray(outagedb::get_all_future($now)), 'Wrong future data.');
|
||||
self::createidarray(outagedb::get_all_unended($now)), 'Wrong future data.');
|
||||
|
||||
$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_future($now)), 'Wrong future data.');
|
||||
self::createidarray(outagedb::get_all_unended($now)), 'Wrong future data.');
|
||||
}
|
||||
|
||||
public function test_getallpast() {
|
||||
public function test_getallended() {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
// Have a consistent time for now (no seconds variation), helps debugging.
|
||||
$now = time();
|
||||
|
||||
self::assertEquals([], outagedb::get_all(), 'Ensure there are no other outages that can affect the test.');
|
||||
self::assertEquals([], outagedb::get_all_past($now), 'There should be no future outages at this point.');
|
||||
self::assertEquals([], outagedb::get_all_ended($now), 'There should be no future outages at this point.');
|
||||
|
||||
self::saveoutage($now, -2, 1, 2, 'An outage in warning period.');
|
||||
self::assertEquals([], outagedb::get_all_past($now), 'No past outages yet.');
|
||||
self::assertEquals([], outagedb::get_all_ended($now), 'No past outages yet.');
|
||||
|
||||
self::saveoutage($now, -3, -2, 2, 'An ongoing outage.');
|
||||
self::assertEquals([], outagedb::get_all_past($now), 'No past outages yet.');
|
||||
self::assertEquals([], outagedb::get_all_ended($now), 'No past outages yet.');
|
||||
|
||||
self::saveoutage($now, 2, 3, 4, 'A future outage.');
|
||||
self::assertEquals([], outagedb::get_all_past($now), 'No past outages yet.');
|
||||
self::assertEquals([], outagedb::get_all_ended($now), 'No past outages yet.');
|
||||
|
||||
$id1 = self::saveoutage($now, -8, -6, -4, 'A past outage.');
|
||||
self::assertEquals([$id1],
|
||||
self::createidarray(outagedb::get_all_past($now)), 'Wrong past data.');
|
||||
self::createidarray(outagedb::get_all_ended($now)), 'Wrong past data.');
|
||||
|
||||
$id2 = self::saveoutage($now, -8, -7, -5, 'Another past outage.');
|
||||
self::assertEquals([$id1, $id2],
|
||||
self::createidarray(outagedb::get_all_past($now)), 'Wrong past data.');
|
||||
self::createidarray(outagedb::get_all_ended($now)), 'Wrong past data.');
|
||||
|
||||
$id3 = self::saveoutage($now, -8, -5, -3, 'Yet another past outage.');
|
||||
self::assertEquals([$id3, $id1, $id2],
|
||||
self::createidarray(outagedb::get_all_past($now)), 'Wrong past data.');
|
||||
self::createidarray(outagedb::get_all_ended($now)), 'Wrong past data.');
|
||||
|
||||
$id4 = self::saveoutage($now, -3, -2, 2, 'A finished outage.', -1);
|
||||
self::assertEquals([$id4, $id3, $id1, $id2],
|
||||
self::createidarray(outagedb::get_all_ended($now)), 'Wrong past data.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to create an outage then save it to the database.
|
||||
*
|
||||
* @param $now int Timestamp for now, such as time().
|
||||
* @param $warning int In how many hours the warning starts. Can be negative.
|
||||
* @param $start int In how many hours this outage starts. Can be negative.
|
||||
* @param $stop int In how many hours this outage finishes. Can be negative.
|
||||
* @param $title string Title for the outage.
|
||||
* @param int $now Timestamp for now, such as time().
|
||||
* @param int $warning In how many hours the warning starts. Can be negative.
|
||||
* @param int $start In how many hours this outage starts. Can be negative.
|
||||
* @param int $stop In how many hours this outage finishes. Can be negative.
|
||||
* @param string $title Title for the outage.
|
||||
* @param int|null $finished In how many hours this outage is marked as finished. Can be negative or null.
|
||||
* @return int Id the of created outage.
|
||||
*/
|
||||
private static function saveoutage($now, $warning, $start, $stop, $title) {
|
||||
private static function saveoutage($now, $warning, $start, $stop, $title, $finished = null) {
|
||||
return outagedb::save(new outage([
|
||||
'warntime' => $now + ($warning * 60 * 60),
|
||||
'starttime' => $now + ($start * 60 * 60),
|
||||
'stoptime' => $now + ($stop * 60 * 60),
|
||||
'warntime' => $now + ($warning * 60 * 60),
|
||||
'finished' => is_null($finished) ? null : ($now + ($finished * 60 * 60)),
|
||||
'title' => $title,
|
||||
'description' => 'Test Outage Description.'
|
||||
]));
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use auth_outage\outagelib;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Tests performed on outageutils class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class outagelib_test extends basic_testcase
|
||||
{
|
||||
public function test_data2object() {
|
||||
// Using object data, no new fields.
|
||||
$obj = new stdClass();
|
||||
$obj->foo = 'bar';
|
||||
$obj->number = 42;
|
||||
$data = new stdClass();
|
||||
$data->foo = 'not bar';
|
||||
outagelib::data2object($data, $obj);
|
||||
self::assertEquals(get_object_vars($obj), ['foo' => 'not bar', 'number' => 42], 'Invalid result.');
|
||||
self::assertEquals(get_object_vars($data), ['foo' => 'not bar'], 'Data should not change.');
|
||||
|
||||
// Using array data, with new fields.
|
||||
$obj = new stdClass();
|
||||
$obj->foo = 'bar';
|
||||
$obj->number = 42;
|
||||
$data = ['foo' => 'foobar', 'flag' => false];
|
||||
outagelib::data2object($data, $obj);
|
||||
self::assertEquals(get_object_vars($obj), ['foo' => 'foobar', 'number' => 42], 'Invalid result.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user