From 6efbb0c2c74f37ec0861f7cd8ab76649adbfc172 Mon Sep 17 00:00:00 2001 From: Daniel Thee Roperto Date: Fri, 16 Dec 2016 10:23:52 +1100 Subject: [PATCH] Refactored tests. --- tests/phpunit/form/forms_test.php | 8 ++- tests/phpunit/local/outagelib_test.php | 85 ++++++++------------------ 2 files changed, 31 insertions(+), 62 deletions(-) diff --git a/tests/phpunit/form/forms_test.php b/tests/phpunit/form/forms_test.php index b1e3959..8e33fb7 100644 --- a/tests/phpunit/form/forms_test.php +++ b/tests/phpunit/form/forms_test.php @@ -184,7 +184,13 @@ class forms_test extends auth_outage_base_testcase { 'sesskey' => sesskey(), '_qf__auth_outage_form_outage_edit' => '1', 'warningduration' => ['number' => '1', 'timeunit' => '60'], - 'starttime' => ['day' => '1', 'month' => '2', 'year' => '2013', 'hour' => '14', 'minute' => '15'], + 'starttime' => [ + 'day' => '1', + 'month' => '2', + 'year' => '2013', + 'hour' => '14', + 'minute' => '15', + ], 'outageduration' => ['number' => '2', 'timeunit' => '3600'], 'title' => 'The title.', 'description' => ['text' => 'The description.', 'format' => '1'], diff --git a/tests/phpunit/local/outagelib_test.php b/tests/phpunit/local/outagelib_test.php index 4b1f808..949219f 100644 --- a/tests/phpunit/local/outagelib_test.php +++ b/tests/phpunit/local/outagelib_test.php @@ -393,19 +393,7 @@ EOT; public function test_preparenextoutage_notautostart() { global $CFG; - $this->resetAfterTest(true); - self::setAdminUser(); - $now = time(); - $outage = new outage([ - 'autostart' => false, - 'warntime' => $now - 200, - 'starttime' => $now - 100, - 'stoptime' => $now + 200, - 'title' => 'Title', - 'description' => 'Description', - ]); - set_config('allowedips', '127.0.0.1', 'auth_outage'); - outagedb::save($outage); + $this->create_outage(); // The method outagelib::prepare_next_outage() should have been called by save(). foreach ([$CFG->dataroot.'/climaintenance.template.html', $CFG->dataroot.'/climaintenance.php'] as $file) { @@ -420,30 +408,7 @@ EOT; public function test_when_we_change_allowed_ips_in_settings_it_updates_the_templates() { global $CFG; - $this->resetAfterTest(true); - self::setAdminUser(); - $now = time(); - $outage = new outage([ - 'autostart' => false, - 'warntime' => $now - 200, - 'starttime' => $now - 100, - 'stoptime' => $now + 200, - 'title' => 'Title', - 'description' => 'Description', - ]); - set_config('allowedips', '127.0.0.1', 'auth_outage'); - outagedb::save($outage); - - // The method outagelib::prepare_next_outage() should have been called by save(). - foreach ([$CFG->dataroot.'/climaintenance.template.html', $CFG->dataroot.'/climaintenance.php'] as $file) { - self::assertFileExists($file); - unlink($file); - } - - // Enable outage plugin so settings can be changed. - set_config('auth', 'outage'); - \core\session\manager::gc(); // Remove stale sessions. - core_plugin_manager::reset_caches(); + $this->create_outage(); // Change settings. admin_write_settings((object)[ @@ -463,30 +428,7 @@ EOT; public function test_when_we_change_remove_selectors_in_settings_it_updates_the_templates() { global $CFG; - $this->resetAfterTest(true); - self::setAdminUser(); - $now = time(); - $outage = new outage([ - 'autostart' => false, - 'warntime' => $now - 200, - 'starttime' => $now - 100, - 'stoptime' => $now + 200, - 'title' => 'Title', - 'description' => 'Description', - ]); - set_config('allowedips', '127.0.0.1', 'auth_outage'); - outagedb::save($outage); - - // The method outagelib::prepare_next_outage() should have been called by save(). - foreach ([$CFG->dataroot.'/climaintenance.template.html', $CFG->dataroot.'/climaintenance.php'] as $file) { - self::assertFileExists($file); - unlink($file); - } - - // Enable outage plugin so settings can be changed. - set_config('auth', 'outage'); - \core\session\manager::gc(); // Remove stale sessions. - core_plugin_manager::reset_caches(); + $this->create_outage(); // Change settings. admin_write_settings((object)[ @@ -553,4 +495,25 @@ EOT; self::assertNotEmpty($CFG->additionalhtmltopofbody); } + + private function create_outage() { + $this->resetAfterTest(true); + self::setAdminUser(); + $now = time(); + $outage = new outage([ + 'autostart' => false, + 'warntime' => $now - 200, + 'starttime' => $now - 100, + 'stoptime' => $now + 200, + 'title' => 'Title', + 'description' => 'Description', + ]); + set_config('allowedips', '127.0.0.1', 'auth_outage'); + outagedb::save($outage); + + // Enable outage plugin so settings can be changed. + set_config('auth', 'outage'); + \core\session\manager::gc(); // Remove stale sessions. + core_plugin_manager::reset_caches(); + } }