From 22f85930ce0b0bdf367c6c8d9253b1d23dcef407 Mon Sep 17 00:00:00 2001 From: Daniel Thee Roperto Date: Mon, 12 Sep 2016 12:10:22 +1000 Subject: [PATCH] Issue #11 - TDD to get future and past outages (outagedb). --- classes/outagedb.php | 68 ++++++++++++++++++++++++++++++++++++++--- tests/outagedb_test.php | 62 +++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 4 deletions(-) diff --git a/classes/outagedb.php b/classes/outagedb.php index b2d7221..e211796 100644 --- a/classes/outagedb.php +++ b/classes/outagedb.php @@ -169,10 +169,7 @@ class outagedb { } /** - * Gets all active outages, sorted by importance as: - * - Ongoing outages more important than outages in warning period. - * - Outages that start earlier are more important. - * - Outages that stop later are more important. + * Gets all active outages (including in warning period). * @param int|null $time Timestamp considered to check for outages, null for current date/time. * @return array An array of outages or an empty array if no active outage found. */ @@ -201,4 +198,67 @@ class outagedb { return $outages; } + + /** + * Gets all future outages not in warning period. + * @param int|null $time Timestamp considered to check for outages, null for current date/time. + * @return array An array of outages or an empty array if no future outage found. + */ + public static function get_all_future($time = null) { + global $DB; + + if ($time === null) { + $time = time(); + } + if (!is_int($time)) { + throw new \InvalidArgumentException('$time must be null or an int.'); + } + + $outages = []; + + $rs = $DB->get_recordset_select( + 'auth_outage', + 'warntime > :datetime', + ['datetime' => $time], + 'starttime ASC, stoptime DESC, title ASC', + '*'); + foreach ($rs as $r) { + $outages[] = new outage($r); + } + $rs->close(); + + return $outages; + } + + + /** + * Gets all past outages. + * @param int|null $time Timestamp considered to check for outages, null for current date/time. + * @return array An array of outages or an empty array if no past outage found. + */ + public static function get_all_past($time = null) { + global $DB; + + if ($time === null) { + $time = time(); + } + if (!is_int($time)) { + throw new \InvalidArgumentException('$time must be null or an int.'); + } + + $outages = []; + + $rs = $DB->get_recordset_select( + 'auth_outage', + 'stoptime < :datetime', + ['datetime' => $time], + 'stoptime DESC, starttime DESC, title ASC', + '*'); + foreach ($rs as $r) { + $outages[] = new outage($r); + } + $rs->close(); + + return $outages; + } } diff --git a/tests/outagedb_test.php b/tests/outagedb_test.php index 770ba1d..976ae11 100644 --- a/tests/outagedb_test.php +++ b/tests/outagedb_test.php @@ -218,6 +218,68 @@ class outagedb_test extends advanced_testcase { self::createidarray(outagedb::get_all_active($now)), 'Wrong actives data.'); } + public function test_getallfuture() { + $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::saveoutage($now, -3, -2, -1, 'A past outage.'); + self::assertEquals([], outagedb::get_all_future($now), 'No future outages yet.'); + + self::saveoutage($now, -2, 1, 2, 'An outage in warning period.'); + self::assertEquals([], outagedb::get_all_future($now), 'No future outages yet.'); + + self::saveoutage($now, -3, -2, 2, 'An ongoing outage.'); + self::assertEquals([], outagedb::get_all_future($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.'); + + $id2 = self::saveoutage($now, 1, 4, 5, 'Another future outage.'); + self::assertEquals([$id1, $id2], + self::createidarray(outagedb::get_all_future($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.'); + } + + public function test_getallpast() { + $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::saveoutage($now, -2, 1, 2, 'An outage in warning period.'); + self::assertEquals([], outagedb::get_all_past($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::saveoutage($now, 2, 3, 4, 'A future outage.'); + self::assertEquals([], outagedb::get_all_past($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.'); + + $id2 = self::saveoutage($now, -8, -7, -5, 'Another past outage.'); + self::assertEquals([$id1, $id2], + self::createidarray(outagedb::get_all_past($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.'); + } + /** * Helper function to create an outage then save it to the database. *