From e13bc05f5edc471fd60cb889e9816e2d2c04fa4a Mon Sep 17 00:00:00 2001 From: Daniel Thee Roperto Date: Mon, 12 Sep 2016 11:49:11 +1000 Subject: [PATCH] Issue #11 - Tests for outagedb get_all_active. --- tests/outagedb_test.php | 48 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/outagedb_test.php b/tests/outagedb_test.php index c694d56..770ba1d 100644 --- a/tests/outagedb_test.php +++ b/tests/outagedb_test.php @@ -30,6 +30,18 @@ defined('MOODLE_INTERNAL') || die(); class outagedb_test extends advanced_testcase { + /** + * Creates an array of ids in from the given outages array. + * @param $outages + */ + private static function createidarray(array $outages) { + $ids = []; + foreach ($outages as $outage) { + $ids[] = $outage->id; + } + return $ids; + } + /** * Make sure we can save and update. */ @@ -170,6 +182,42 @@ class outagedb_test extends advanced_testcase { self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.'); } + public function test_getallactive() { + $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_active($now), 'There should be no active outages at this point.'); + + self::saveoutage($now, 1, 2, 3, 'An outage that starts in the future and is not in warning period.'); + self::assertEquals([], outagedb::get_all_active($now), 'No active outages yet.'); + + self::saveoutage($now, -3, -2, -1, 'An outage that is already in the past.'); + self::assertEquals([], outagedb::get_all_active($now), 'No active outages yet.'); + + $id1 = self::saveoutage($now, -2, 1, 2, 'An outage in warning period.'); + self::assertEquals([$id1], + self::createidarray(outagedb::get_all_active($now)), 'Wrong actives data.'); + + $id2 = self::saveoutage($now, -1, 2, 3, 'Another outage in warning period.'); + self::assertEquals([$id1, $id2], + self::createidarray(outagedb::get_all_active($now)), 'Wrong actives data.'); + + $id3 = self::saveoutage($now, -3, -2, 2, 'An ongoing outage.'); + self::assertEquals([$id3, $id1, $id2], + self::createidarray(outagedb::get_all_active($now)), 'Wrong actives data.'); + + $id4 = self::saveoutage($now, -3, -1, 1, 'Another ongoing outage.'); + self::assertEquals([$id3, $id4, $id1, $id2], + self::createidarray(outagedb::get_all_active($now)), 'Wrong actives data.'); + + $id5 = self::saveoutage($now, -3, -2, 1, 'Yet another ongoing outage.'); + self::assertEquals([$id3, $id5, $id4, $id1, $id2], + self::createidarray(outagedb::get_all_active($now)), 'Wrong actives data.'); + } + /** * Helper function to create an outage then save it to the database. *