From 8791727ba4cb30aaa4773436de579bdb968d42a8 Mon Sep 17 00:00:00 2001 From: Daniel Thee Roperto Date: Mon, 26 Sep 2016 19:34:27 +1000 Subject: [PATCH] Issue #46 - Added tests for every class, except outputs. Small bugs/invalid parameter detection fixed together. --- classes/calendar/calendar.php | 23 +- classes/dml/outagedb.php | 9 +- classes/form/baseform.php | 98 ++++++ classes/form/outage/delete.php | 3 +- classes/form/outage/edit.php | 4 +- classes/local/cli/create.php | 3 +- classes/local/controllers/infopage.php | 33 +- classes/local/outagelib.php | 32 +- clone.php | 7 +- delete.php | 7 +- edit.php | 7 +- finish.php | 7 +- manage.php | 7 +- new.php | 3 +- tests/phpunit/calendar/calendar_test.php | 112 +++++++ tests/phpunit/{ => dml}/outagedb_test.php | 109 ++++++- tests/phpunit/{ => event}/events_test.php | 3 - tests/phpunit/form/forms_test.php | 130 ++++++++ tests/phpunit/infopagecontroller_test.php | 104 ------- tests/phpunit/{ => local}/cli/cli_test.php | 2 - .../phpunit/{ => local}/cli/cli_testcase.php | 0 tests/phpunit/{ => local}/cli/create_test.php | 33 +- tests/phpunit/{ => local}/cli/finish_test.php | 9 +- .../{ => local}/cli/waitforit_test.php | 9 +- .../local/controllers/infopage_test.php | 285 ++++++++++++++++++ tests/phpunit/{ => local}/outage_test.php | 88 +++++- tests/phpunit/local/outagelib_test.php | 152 ++++++++++ tests/phpunit/outagelib_test.php | 50 --- 28 files changed, 1090 insertions(+), 239 deletions(-) create mode 100644 classes/form/baseform.php create mode 100644 tests/phpunit/calendar/calendar_test.php rename tests/phpunit/{ => dml}/outagedb_test.php (82%) rename tests/phpunit/{ => event}/events_test.php (95%) create mode 100644 tests/phpunit/form/forms_test.php delete mode 100644 tests/phpunit/infopagecontroller_test.php rename tests/phpunit/{ => local}/cli/cli_test.php (96%) rename tests/phpunit/{ => local}/cli/cli_testcase.php (100%) rename tests/phpunit/{ => local}/cli/create_test.php (91%) rename tests/phpunit/{ => local}/cli/finish_test.php (94%) rename tests/phpunit/{ => local}/cli/waitforit_test.php (96%) create mode 100644 tests/phpunit/local/controllers/infopage_test.php rename tests/phpunit/{ => local}/outage_test.php (67%) create mode 100644 tests/phpunit/local/outagelib_test.php delete mode 100644 tests/phpunit/outagelib_test.php diff --git a/classes/calendar/calendar.php b/classes/calendar/calendar.php index 37eac35..a97e339 100644 --- a/classes/calendar/calendar.php +++ b/classes/calendar/calendar.php @@ -20,7 +20,6 @@ use auth_outage\local\outage; use calendar_event; defined('MOODLE_INTERNAL') || die(); - require_once($CFG->dirroot.'/calendar/lib.php'); /** @@ -34,6 +33,7 @@ require_once($CFG->dirroot.'/calendar/lib.php'); class calendar { /** * Private constructor, use static methods instead. + * @codeCoverageIgnore */ private function __construct() { } @@ -42,22 +42,23 @@ class calendar { * Create an event on the calendar for this outage. * @param outage $outage Outage to be added to the calendar. */ - public static function calendar_create(outage $outage) { - calendar_event::create(self::calendar_data($outage)); + public static function create(outage $outage) { + calendar_event::create(self::create_data($outage)); } /** * Updates an event on the calendar based on this outage. * @param outage $outage Outage to be updated in the calendar. + * @SuppressWarnings("comment") Allow this test to have as many tests as necessary. */ - public static function calendar_update(outage $outage) { - $event = self::calendar_load($outage->id); + public static function update(outage $outage) { + $event = self::load($outage->id); if (is_null($event)) { debugging('Cannot update calendar entry for outage #'.$outage->id.', event not found. Creating it...'); - self::calendar_create($outage); + self::create($outage); } else { - $event->update(self::calendar_data($outage)); + $event->update(self::create_data($outage)); } } @@ -65,8 +66,8 @@ class calendar { * Removes an event from the calendar related to this outage. * @param int $outageid Id of outage to be deleted from the calendar. */ - public static function calendar_delete($outageid) { - $event = self::calendar_load($outageid); + public static function delete($outageid) { + $event = self::load($outageid); // If not found (was not created before) ignore it. if (is_null($event)) { @@ -81,7 +82,7 @@ class calendar { * @param outage $outage Outage to use as reference for the calendar event. * @return mixed[] Calendar event data. */ - private static function calendar_data(outage $outage) { + private static function create_data(outage $outage) { return [ 'name' => $outage->get_title(), 'description' => $outage->get_description(), @@ -102,7 +103,7 @@ class calendar { * @param int $outageid The outage id to find in the calendar. * @return calendar_event|null The calendar event or null if not found. */ - private static function calendar_load($outageid) { + public static function load($outageid) { global $DB; $event = $DB->get_record_select( diff --git a/classes/dml/outagedb.php b/classes/dml/outagedb.php index 85b754d..cc1716c 100644 --- a/classes/dml/outagedb.php +++ b/classes/dml/outagedb.php @@ -40,6 +40,7 @@ require_once($CFG->dirroot.'/calendar/lib.php'); class outagedb { /** * Private constructor, use static methods instead. + * @codeCoverageIgnore */ private function __construct() { } @@ -62,7 +63,7 @@ class outagedb { } /** - * @param $id int Outage id to get. + * @param int $id Outage id to get. * @return outage|null Returns the outage or null if not found. * @throws coding_exception */ @@ -106,7 +107,7 @@ class outagedb { ['objectid' => $outage->id, 'other' => (array)$outage] )->trigger(); // Create calendar entry. - calendar::calendar_create($outage); + calendar::create($outage); } else { // Remove the createdby field so it does not get updated. unset($outage->createdby); @@ -116,7 +117,7 @@ class outagedb { ['objectid' => $outage->id, 'other' => (array)$outage] )->trigger(); // Update calendar entry. - calendar::calendar_update($outage); + calendar::update($outage); } // Trigger outages modified events. @@ -147,7 +148,7 @@ class outagedb { // Delete it and remove from calendar. $DB->delete_records('auth_outage', ['id' => $id]); - calendar::calendar_delete($id); + calendar::delete($id); // Trigger events. outagelib::outages_modified(); diff --git a/classes/form/baseform.php b/classes/form/baseform.php new file mode 100644 index 0000000..68f0ee5 --- /dev/null +++ b/classes/form/baseform.php @@ -0,0 +1,98 @@ +. + +namespace auth_outage\form; + +use moodleform; + +defined('MOODLE_INTERNAL') || die(); + +require_once($CFG->libdir.'/formslib.php'); + +/** + * Outage base for forms, extends Moodle form to fix a but in the validation method. + * + * @package auth_outage + * @author Daniel Thee Roperto + * @copyright 2016 Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +abstract class baseform extends moodleform { + /** + * Validate the form. + * + * You almost always want to call {@link is_validated} instead of this + * because it calls {@link definition_after_data} first, before validating the form, + * which is what you want in 99% of cases. + * + * This is provided as a separate function for those special cases where + * you want the form validated before definition_after_data is called + * for example, to selectively add new elements depending on a no_submit_button press, + * but only when the form is valid when the no_submit_button is pressed, + * + * @param bool $validateonnosubmit optional, defaults to false. The default behaviour + * is NOT to validate the form when a no submit button has been pressed. + * pass true here to override this behaviour + * + * @return bool true if form data valid + * @SuppressWarnings(PHPMD) It is better to not refactor this method as it is linked to its parent functionality. + * @codeCoverageIgnore + */ + public function validate_defined_fields($validateonnosubmit = false) { + // One validation NOT is enough (if mocking). See parent method. + $mform =& $this->_form; + if ($this->no_submit_button_pressed() && empty($validateonnosubmit)) { + return false; + } + $internalval = $mform->validate(); + + $files = []; + $fileval = $this->_validate_files($files); + // Check draft files for validation and flag them if required files are not in draft area. + $draftfilevalue = $this->validate_draft_files(); + + if ($fileval !== true && $draftfilevalue !== true) { + $fileval = array_merge($fileval, $draftfilevalue); + } else if ($draftfilevalue !== true) { + $fileval = $draftfilevalue; + } //default is file_val, so no need to assign. + + if ($fileval !== true) { + if (!empty($fileval)) { + foreach ($fileval as $element => $msg) { + $mform->setElementError($element, $msg); + } + } + $fileval = false; + } + + $data = $mform->exportValues(); + $moodleval = $this->validation($data, $files); + if ((is_array($moodleval) && count($moodleval) !== 0)) { + // Non-empty array means errors. + foreach ($moodleval as $element => $msg) { + $mform->setElementError($element, $msg); + } + $moodleval = false; + } else { + // Anything else means validation ok. + $moodleval = true; + } + + $validated = ($internalval and $moodleval and $fileval); + return $validated; + } +} diff --git a/classes/form/outage/delete.php b/classes/form/outage/delete.php index b7aea50..b8bfa22 100644 --- a/classes/form/outage/delete.php +++ b/classes/form/outage/delete.php @@ -32,8 +32,7 @@ defined('MOODLE_INTERNAL') || die(); */ class delete extends \moodleform { /** - * {@inheritDoc} - * @see moodleform::definition() + * Defines the form elements. */ public function definition() { $mform = $this->_form; diff --git a/classes/form/outage/edit.php b/classes/form/outage/edit.php index e5b6440..66d7a29 100644 --- a/classes/form/outage/edit.php +++ b/classes/form/outage/edit.php @@ -16,9 +16,9 @@ namespace auth_outage\form\outage; +use auth_outage\form\baseform; use auth_outage\local\outage; use coding_exception; -use moodleform; defined('MOODLE_INTERNAL') || die(); @@ -32,7 +32,7 @@ require_once($CFG->libdir.'/formslib.php'); * @copyright 2016 Catalyst IT * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class edit extends moodleform { +class edit extends baseform { const TITLE_MAX_CHARS = 100; /** diff --git a/classes/local/cli/create.php b/classes/local/cli/create.php index 075dc18..bb66ad5 100644 --- a/classes/local/cli/create.php +++ b/classes/local/cli/create.php @@ -284,6 +284,7 @@ class create extends clibase { } } - throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param])); + throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param]), + cli_exception::ERROR_PARAMETER_INVALID); } } diff --git a/classes/local/controllers/infopage.php b/classes/local/controllers/infopage.php index aed01bd..68366f1 100644 --- a/classes/local/controllers/infopage.php +++ b/classes/local/controllers/infopage.php @@ -74,10 +74,11 @@ class infopage { * Given the HTML code for the static page, find the outage id for that page. * @param string $html Static info page HTML. * @return int|null Outage id or null if not found. + * @throws coding_exception */ public static function find_outageid_from_infopage($html) { if (!is_string($html)) { - throw new coding_exception('$html must be a string.', $html); + throw new coding_exception('$html must be a string.', gettype($html)); } $output = []; @@ -103,10 +104,7 @@ class infopage { $info = new infopage(['outage' => $outage, 'static' => true]); $html = $info->get_output(); - // Sanity check before writing/overwriting old file. - if (!is_string($html) || ($html == '') || (html_to_text($html) == '')) { - throw new invalid_state_exception('Sanity check failed. Invalid contents on $html.'); - } + self::save_static_page_sanitycheck($html); $dir = dirname($file); if (!file_exists($dir) || !is_dir($dir)) { @@ -117,7 +115,9 @@ class infopage { /** * Updates the static info page by (re)creating or deleting it as needed. - * @param null $file + * @param string|null $file File to update. Null to use default. + * @throws coding_exception + * @throws file_exception */ public static function update_static_page($file = null) { if (is_null($file)) { @@ -149,19 +149,33 @@ class infopage { return $CFG->dataroot.'/climaintenance.template.html'; } + /** + * Ensures the data to create the page is valid. + * It should never be invalid, but if it is we will get a blank page while the maintenance is ongoing and the + * system administrators may become frustrated to understand why it is not working, let's not provoke them! + * @param string $html The HTML to check. + * @throws invalid_state_exception + */ + public static function save_static_page_sanitycheck($html) { + if (!is_string($html) || (trim($html) == '') || (trim(html_to_text($html)) == '')) { + throw new invalid_state_exception('Sanity check failed. Invalid contents on $html.'); + } + } + /** * Generates and returns the HTML for the info page. * @return string HTML for the info page. */ public function get_output() { ob_start(); + $output = null; try { - // TODO what if redirection occurs here? $this->output(); - return ob_get_contents(); + $output = ob_get_contents(); } finally { ob_end_clean(); } + return $output; } /** @@ -213,6 +227,7 @@ class infopage { /** * Adjusts the fields according to the given parameters. * @param mixed[] $params + * @throws coding_exception */ private function set_parameters(array $params) { if (!is_null($params['outage']) && !($params['outage'] instanceof outage)) { @@ -220,7 +235,7 @@ class infopage { } if (!is_null($params['id']) && !is_null($params['outage']) && ($params['id'] !== $params['outage']->id)) { - throw new coding_exception('Provided id and outage->id do not match.', $params); + throw new coding_exception('Provided id and outage->id do not match.', $params['id'].'/'.$params['outage']->id); } if (is_null($params['id']) && is_null($params['outage'])) { diff --git a/classes/local/outagelib.php b/classes/local/outagelib.php index 5a18c96..e34903e 100644 --- a/classes/local/outagelib.php +++ b/classes/local/outagelib.php @@ -20,7 +20,6 @@ use auth_outage\dml\outagedb; use auth_outage\local\controllers\infopage; use auth_outage\output\renderer; use Exception; -use moodle_url; use stdClass; defined('MOODLE_INTERNAL') || die(); @@ -34,17 +33,14 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class outagelib { - private static $initialized = false; + private static $injected = false; /** - * Initializes admin pages for outage. - * @return renderer The outage renderer for the page. + * Calls inject even if it was already called before. */ - public static function page_setup() { - global $PAGE; - admin_externalpage_setup('auth_outage_manage'); - $PAGE->set_url(new moodle_url('/auth/outage/manage.php')); - return renderer::get(); + public static function reinject() { + self::$injected = false; + self::inject(); } /** @@ -54,13 +50,18 @@ class outagelib { global $CFG; // Many hooks can call it, execute only once. - if (self::$initialized) { + if (self::$injected) { return; } - self::$initialized = true; + self::$injected = true; // Ensure we do not kill the whole website in case of an error. try { + // Ensure no exceptions break the code. + if (PHPUNIT_TEST && optional_param('auth_outage_break_code', false, PARAM_INT)) { + (new stdClass())->invalidfield; + } + // Check for a previewing outage, then for an active outage. $previewid = optional_param('auth_outage_preview', null, PARAM_INT); $time = time(); @@ -74,7 +75,8 @@ class outagelib { return; } // Delta is in seconds, setting the time our warning bar will consider relative to the outage start time. - $time = $active->starttime + optional_param('auth_outage_delta', 0, PARAM_INT); + $delta = optional_param('auth_outage_delta', 0, PARAM_INT); + $time = $active->starttime + $delta; if (!$active->is_active($time)) { return; } @@ -134,10 +136,8 @@ class outagelib { } else { $message = get_config('moodle', 'maintenance_message'); if ($message) { - if (!defined('PHPUNIT_TEST') || !PHPUNIT_TEST) { - error_log('Disabling $CFG->maintenance_message to allow our template page to take place.'); - error_log('Previous value: '.$message); - } + debugging('Disabling $CFG->maintenance_message to allow our template page to take place.'); + debugging('Previous value: '.$message); // We cannot do much if forced config, but the logs will show the error. unset_config('maintenance_message'); } diff --git a/clone.php b/clone.php index 7552ded..6dd00e8 100644 --- a/clone.php +++ b/clone.php @@ -25,13 +25,14 @@ use auth_outage\dml\outagedb; use auth_outage\form\outage\edit; -use auth_outage\local\outagelib; +use auth_outage\output\renderer; require_once(__DIR__.'/../../config.php'); require_once($CFG->libdir.'/adminlib.php'); require_once($CFG->libdir.'/formslib.php'); -$renderer = outagelib::page_setup(); +admin_externalpage_setup('auth_outage_manage'); +$PAGE->set_url(new moodle_url('/auth/outage/manage.php')); $mform = new edit(); @@ -54,6 +55,6 @@ $mform->set_data($outage); $PAGE->navbar->add($outage->get_title()); echo $OUTPUT->header(); -echo $renderer->rendersubtitle('outageclone'); +echo renderer::get()->rendersubtitle('outageclone'); $mform->display(); echo $OUTPUT->footer(); diff --git a/delete.php b/delete.php index 93a8aa9..21dd274 100644 --- a/delete.php +++ b/delete.php @@ -25,13 +25,14 @@ use auth_outage\dml\outagedb; use auth_outage\form\outage\delete; -use auth_outage\local\outagelib; +use auth_outage\output\renderer; require_once(__DIR__.'/../../config.php'); require_once($CFG->libdir.'/adminlib.php'); require_once($CFG->libdir.'/formslib.php'); -$renderer = outagelib::page_setup(); +admin_externalpage_setup('auth_outage_manage'); +$PAGE->set_url(new moodle_url('/auth/outage/manage.php')); $mform = new delete(); if ($mform->is_cancelled()) { @@ -53,7 +54,7 @@ $mform->set_data($dataid); echo $OUTPUT->header(); -echo $renderer->renderdeleteconfirmation($outage); +echo renderer::get()->renderdeleteconfirmation($outage); $mform->display(); diff --git a/edit.php b/edit.php index b5a50e5..074d103 100644 --- a/edit.php +++ b/edit.php @@ -25,13 +25,14 @@ use auth_outage\dml\outagedb; use auth_outage\form\outage\edit; -use auth_outage\local\outagelib; +use auth_outage\output\renderer; require_once(__DIR__.'/../../config.php'); require_once($CFG->libdir.'/adminlib.php'); require_once($CFG->libdir.'/formslib.php'); -$renderer = outagelib::page_setup(); +admin_externalpage_setup('auth_outage_manage'); +$PAGE->set_url(new moodle_url('/auth/outage/manage.php')); $mform = new edit(); @@ -51,6 +52,6 @@ $mform->set_data($outage); $PAGE->navbar->add($outage->get_title()); echo $OUTPUT->header(); -echo $renderer->rendersubtitle('outageedit'); +echo renderer::get()->rendersubtitle('outageedit'); $mform->display(); echo $OUTPUT->footer(); diff --git a/finish.php b/finish.php index 84b1a3e..4a42054 100644 --- a/finish.php +++ b/finish.php @@ -25,13 +25,14 @@ use auth_outage\dml\outagedb; use auth_outage\form\outage\finish; -use auth_outage\local\outagelib; +use auth_outage\output\renderer; require_once(__DIR__.'/../../config.php'); require_once($CFG->libdir.'/adminlib.php'); require_once($CFG->libdir.'/formslib.php'); -$renderer = outagelib::page_setup(); +admin_externalpage_setup('auth_outage_manage'); +$PAGE->set_url(new moodle_url('/auth/outage/manage.php')); $mform = new finish(); if ($mform->is_cancelled()) { @@ -53,7 +54,7 @@ $mform->set_data($dataid); echo $OUTPUT->header(); -echo $renderer->renderfinishconfirmation($outage); +echo renderer::get()->renderfinishconfirmation($outage); $mform->display(); diff --git a/manage.php b/manage.php index 400e59c..775d47b 100644 --- a/manage.php +++ b/manage.php @@ -24,16 +24,17 @@ */ use auth_outage\dml\outagedb; -use auth_outage\local\outagelib; +use auth_outage\output\renderer; require_once(__DIR__.'/../../config.php'); require_once($CFG->libdir.'/adminlib.php'); -$renderer = outagelib::page_setup(); +admin_externalpage_setup('auth_outage_manage'); +$PAGE->set_url(new moodle_url('/auth/outage/manage.php')); echo $OUTPUT->header(); -$renderer->output_view('manage.php', [ +renderer::get()->output_view('manage.php', [ 'unended' => outagedb::get_all_unended(), 'ended' => outagedb::get_all_ended(), ]); diff --git a/new.php b/new.php index fa3c5f3..9d85a92 100644 --- a/new.php +++ b/new.php @@ -32,7 +32,8 @@ require_once(__DIR__.'/../../config.php'); require_once($CFG->libdir.'/adminlib.php'); require_once($CFG->libdir.'/formslib.php'); -outagelib::page_setup(); +admin_externalpage_setup('auth_outage_manage'); +$PAGE->set_url(new moodle_url('/auth/outage/manage.php')); $mform = new edit(); if ($mform->is_cancelled()) { diff --git a/tests/phpunit/calendar/calendar_test.php b/tests/phpunit/calendar/calendar_test.php new file mode 100644 index 0000000..1b6e207 --- /dev/null +++ b/tests/phpunit/calendar/calendar_test.php @@ -0,0 +1,112 @@ +. + +use auth_outage\calendar\calendar; +use auth_outage\local\outage; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Tests performed on outage class. + * + * @package auth_outage + * @author Daniel Thee Roperto + * @copyright Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class calendar_test extends advanced_testcase { + public function test_create() { + self::setAdminUser(); + $this->resetAfterTest(false); + + $time = time(); + $outage = new outage([ + 'id' => 1, + 'autostart' => false, + 'warntime' => $time - 100, + 'starttime' => $time, + 'stoptime' => $time + (2 * 60 * 60), + 'title' => 'Title', + 'description' => 'Description', + ]); + calendar::create($outage); + $this->check_calendar($outage); + + return $outage; + } + + /** + * @depends test_create + */ + public function test_update(outage $outage) { + self::setAdminUser(); + $this->resetAfterTest(false); + + $outage->title = 'New Title'; + calendar::update($outage); + $this->check_calendar($outage); + + return $outage; + } + + /** + * @depends test_update + */ + public function test_delete($outage) { + self::setAdminUser(); + $this->resetAfterTest(true); + + calendar::delete($outage->id); + self::assertNull(calendar::load($outage->id)); + } + + public function test_update_notfound() { + self::setAdminUser(); + $this->resetAfterTest(true); + + $time = time(); + $outage = new outage([ + 'id' => 1, + 'autostart' => false, + 'warntime' => $time - 100, + 'starttime' => $time, + 'stoptime' => $time + (2 * 60 * 60), + 'title' => 'Title', + 'description' => 'Description', + ]); + + calendar::update($outage); + self::assertCount(1, phpunit_util::get_debugging_messages()); + phpunit_util::reset_debugging(); + } + + public function test_delete_notfound() { + self::setAdminUser(); + $this->resetAfterTest(true); + calendar::delete(1); + self::assertCount(1, phpunit_util::get_debugging_messages()); + phpunit_util::reset_debugging(); + } + + private function check_calendar(outage $outage) { + $calendar = calendar::load($outage->id); + self::assertSame($outage->title, $calendar->name); + self::assertSame($outage->description, $calendar->description); + self::assertSame('auth_outage', $calendar->eventtype); + self::assertEquals($outage->starttime, $calendar->timestart); + self::assertEquals($outage->get_duration_planned(), $calendar->timeduration); + } +} diff --git a/tests/phpunit/outagedb_test.php b/tests/phpunit/dml/outagedb_test.php similarity index 82% rename from tests/phpunit/outagedb_test.php rename to tests/phpunit/dml/outagedb_test.php index 27f53b3..00d84b9 100644 --- a/tests/phpunit/outagedb_test.php +++ b/tests/phpunit/dml/outagedb_test.php @@ -22,10 +22,11 @@ defined('MOODLE_INTERNAL') || die(); /** * Tests performed on outage class. * - * @package auth_outage - * @author Daniel Thee Roperto - * @copyright 2016 Catalyst IT - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package auth_outage + * @author Daniel Thee Roperto + * @copyright 2016 Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @SuppressWarnings("public") Allow this test to have as many tests as necessary. */ class outagedb_test extends advanced_testcase { /** @@ -329,6 +330,106 @@ class outagedb_test extends advanced_testcase { self::createidarray(outagedb::get_all_ended($now)), 'Wrong past data.'); } + /** + * @expectedException coding_exception + */ + public function test_getbyid_invalid() { + $this->resetAfterTest(true); + outagedb::get_by_id(-1); + } + + /** + * @expectedException coding_exception + */ + public function test_delete_invalid() { + $this->resetAfterTest(true); + outagedb::delete(-1); + } + + /** + * @expectedException coding_exception + */ + public function test_getactive_invalid() { + $this->resetAfterTest(true); + outagedb::get_active(-1); + } + + /** + * @expectedException coding_exception + */ + public function test_getallunended_invalid() { + $this->resetAfterTest(true); + outagedb::get_all_unended(-1); + } + + public function test_getallunended_now() { + $this->resetAfterTest(true); + outagedb::get_all_unended(); + } + + /** + * @expectedException coding_exception + */ + public function test_getallended_invalid() { + $this->resetAfterTest(true); + outagedb::get_all_ended(-1); + } + + public function test_getallended_now() { + $this->resetAfterTest(true); + outagedb::get_all_ended(); + } + + /** + * @expectedException coding_exception + */ + public function test_finish_invalid() { + $this->resetAfterTest(true); + outagedb::finish(1, -1); + } + + public function test_finish_now_notfound() { + $this->resetAfterTest(true); + outagedb::finish(1); + self::assertCount(1, phpunit_util::get_debugging_messages()); + phpunit_util::reset_debugging(); + } + + public function test_finish_notongoing() { + $this->resetAfterTest(true); + $time = time(); + $outage = new outage([ + 'autostart' => false, + 'warntime' => $time + (60 * 60 * 24 * 1), + 'starttime' => $time + (60 * 60 * 24 * 2), + 'stoptime' => $time + (60 * 60 * 24 * 3), + 'title' => 'Title', + 'description' => 'Description', + ]); + $id = outagedb::save($outage); + self::assertTrue(!$outage->is_ongoing($time)); + + outagedb::finish($id, $time); + self::assertCount(1, phpunit_util::get_debugging_messages()); + phpunit_util::reset_debugging(); + } + + /** + * @expectedException coding_exception + */ + public function test_getnextstartinginvalid() { + $this->resetAfterTest(true); + outagedb::get_next_starting(-1); + } + + /** + * @expectedException coding_exception + */ + public function test_getnextautostartinginvalid() { + $this->resetAfterTest(true); + outagedb::get_next_autostarting(-1); + } + /** * Helper function to create an outage for tests. * diff --git a/tests/phpunit/events_test.php b/tests/phpunit/event/events_test.php similarity index 95% rename from tests/phpunit/events_test.php rename to tests/phpunit/event/events_test.php index 54bef26..278d81d 100644 --- a/tests/phpunit/events_test.php +++ b/tests/phpunit/event/events_test.php @@ -26,9 +26,6 @@ defined('MOODLE_INTERNAL') || die(); * @author Daniel Thee Roperto * @copyright Catalyst IT * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @covers \auth_outage\event\outage_created - * @covers \auth_outage\event\outage_updated - * @covers \auth_outage\event\outage_deleted */ class events_test extends advanced_testcase { public function test_save() { diff --git a/tests/phpunit/form/forms_test.php b/tests/phpunit/form/forms_test.php new file mode 100644 index 0000000..f86e6e9 --- /dev/null +++ b/tests/phpunit/form/forms_test.php @@ -0,0 +1,130 @@ +. + +use auth_outage\form\outage\delete; +use auth_outage\form\outage\edit; +use auth_outage\form\outage\finish; +use auth_outage\local\outage; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Tests performed on forms classes. + * + * @package auth_outage + * @author Daniel Thee Roperto + * @copyright 2016 Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class forms_test extends advanced_testcase { + public function test_delete() { + new delete(); + } + + public function test_finish() { + new finish(); + } + + public function test_edit_valid() { + $this->mock_edit_post(); + $edit = new edit(); + self::assertFalse($edit->is_cancelled()); + $outage = $edit->get_data(); + self::assertInstanceOf(outage::class, $outage); + self::assertSame(false, $outage->autostart); + self::assertSame(60, $outage->get_warning_duration()); + self::assertSame(mktime(14, 15, 0, 2, 1, 2013), $outage->starttime); + self::assertSame(2 * 60 * 60, $outage->get_duration_planned()); + self::assertSame('The title.', $outage->title); + self::assertSame('The description.', $outage->description); + } + + public function test_edit_invalid_warning() { + $this->mock_edit_post(); + $_POST['warningduration'] = ['number' => '-1', 'timeunit' => '60']; + $edit = new edit(); + $outage = $edit->get_data(); + self::assertNull($outage); + } + + public function test_edit_invalid_duration() { + $this->mock_edit_post(); + $_POST['outageduration'] = ['number' => '-2', 'timeunit' => '3600']; + $edit = new edit(); + self::assertNull($edit->get_data()); + } + + public function test_edit_invalid_title() { + $this->mock_edit_post(); + $_POST['title'] = ''; + $edit = new edit(); + self::assertNull($edit->get_data()); + } + + public function test_edit_invalid_title_toolong() { + $this->mock_edit_post(); + $_POST['title'] = 'This is a very long time, it is so long that at some point it should not be valid. '. + 'With a very long title used in this place we should get a form validation error. '. + 'Do you think this title is long enough?'; + $edit = new edit(); + self::assertNull($edit->get_data()); + } + + public function test_edit_description_invalid_format() { + $this->mock_edit_post(); + $_POST['description'] = ['text' => 'The description.', 'format' => '2']; + $edit = new edit(); + self::assertNull($edit->get_data()); + self::assertCount(1, phpunit_util::get_debugging_messages()); + phpunit_util::reset_debugging(); + } + + public function test_setdata() { + $outage = new outage([ + 'autostart' => false, + 'warntime' => time() - 60, + 'starttime' => time(), + 'stoptime' => time() + 60, + 'title' => 'Title', + 'description' => 'Description', + ]); + $edit = new edit(); + $edit->set_data($outage); + } + + /** + * @expectedException coding_exception + */ + public function test_setdata_invalid() { + $edit = new edit(); + $edit->set_data(null); + } + + private function mock_edit_post() { + // There is a bug in moodleform::mock_submit so we make our own version. + $_POST = [ + 'id' => '1', + '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'], + 'outageduration' => ['number' => '2', 'timeunit' => '3600'], + 'title' => 'The title.', + 'description' => ['text' => 'The description.', 'format' => '1'], + 'submitbutton' => 'Save changes', + ]; + } +} diff --git a/tests/phpunit/infopagecontroller_test.php b/tests/phpunit/infopagecontroller_test.php deleted file mode 100644 index 44244b8..0000000 --- a/tests/phpunit/infopagecontroller_test.php +++ /dev/null @@ -1,104 +0,0 @@ -. - -use auth_outage\local\controllers\infopage; -use auth_outage\local\outage; - -defined('MOODLE_INTERNAL') || die(); - -/** - * Tests performed on infopage_controller class. - * - * @package auth_outage - * @author Daniel Thee Roperto - * @copyright 2016 Catalyst IT - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @covers \auth_outage\local\controllers\infopage - */ -class infopagecontroller_test extends advanced_testcase { - public function setUp() { - if (file_exists($this->get_file())) { - if (is_file($this->get_file())) { - unlink($this->get_file()); - } else { - self::fail('Invalid temp file: '.$this->get_file()); - } - } - } - - /** - * Return a temporary file name to use for this test. - * @return string Default file. - */ - public function get_file() { - return sys_get_temp_dir().'/phpunit_authoutage.tmp'; - } - - public function test_staticpage_output() { - global $PAGE; - $this->resetAfterTest(true); - - $PAGE->set_context(context_system::instance()); - $now = time(); - $outage = new outage([ - 'id' => 1, - 'starttime' => $now + (60 * 60), - 'warntime' => $now - (60 * 60), - 'stoptime' => $now + (2 * 60 * 60), - 'title' => 'Outage Title at {{start}}', - 'description' => 'This is an important outage, starting at {{start}}.', - ]); - $info = new infopage(['static' => true, 'outage' => $outage]); - $html = $info->get_output(); - // Must find... - self::assertContains('', $html); - self::assertContains('', $html); - self::assertContains($outage->get_title(), $html); - self::assertContains($outage->get_description(), $html); - // Must not find... - self::assertNotContains('id, infopage::find_outageid_from_infopage($html)); - } - - public function test_staticpage_file() { - $now = time(); - $outage = new outage([ - 'id' => 1, - 'warntime' => $now - 100, - 'starttime' => $now + 100, - 'stoptime' => $now + 200, - 'title' => 'Title', - 'description' => 'Description', - ]); - infopage::save_static_page($outage, $this->get_file()); - self::assertFileExists($this->get_file()); - - $id = infopage::find_outageid_from_infopage(file_get_contents($this->get_file())); - self::assertSame($outage->id, $id); - - unlink($this->get_file()); - } - - public function test_getdefaulttemplatefile() { - $file = infopage::get_defaulttemplatefile(); - self::assertTrue(is_string($file)); - self::assertContains('template', $file); - } -} diff --git a/tests/phpunit/cli/cli_test.php b/tests/phpunit/local/cli/cli_test.php similarity index 96% rename from tests/phpunit/cli/cli_test.php rename to tests/phpunit/local/cli/cli_test.php index 2b6cdb8..6795eb0 100644 --- a/tests/phpunit/cli/cli_test.php +++ b/tests/phpunit/local/cli/cli_test.php @@ -27,8 +27,6 @@ require_once(__DIR__.'/cli_testcase.php'); * @author Daniel Thee Roperto * @copyright 2016 Catalyst IT * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @covers \auth_outage\local\cli\clibase - * @covers \auth_outage\local\cli\cli_exception * @SuppressWarnings("public") */ class cli_test extends cli_testcase { diff --git a/tests/phpunit/cli/cli_testcase.php b/tests/phpunit/local/cli/cli_testcase.php similarity index 100% rename from tests/phpunit/cli/cli_testcase.php rename to tests/phpunit/local/cli/cli_testcase.php diff --git a/tests/phpunit/cli/create_test.php b/tests/phpunit/local/cli/create_test.php similarity index 91% rename from tests/phpunit/cli/create_test.php rename to tests/phpunit/local/cli/create_test.php index a02abfe..6f5a185 100644 --- a/tests/phpunit/cli/create_test.php +++ b/tests/phpunit/local/cli/create_test.php @@ -25,10 +25,10 @@ require_once(__DIR__.'/cli_testcase.php'); /** * Tests performed on CLI create class. * - * @package auth_outage - * @author Daniel Thee Roperto - * @copyright 2016 Catalyst IT - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package auth_outage + * @author Daniel Thee Roperto + * @copyright 2016 Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @SuppressWarnings("public") Allow this test to have as many tests as necessary. */ class create_test extends cli_testcase { @@ -273,4 +273,29 @@ class create_test extends cli_testcase { self::assertContains('created', $text); self::assertContains('started', $text); } + + /** + * @expectedException coding_exception + */ + public function test_setdefaults_extra() { + $cli = new create([]); + $cli->set_defaults(['aninvalidparameter' => 'value']); + } + + /** + * @expectedException auth_outage\local\cli\cli_exception + * @expectedExceptionCode 3 + */ + public function test_invalid_bool() { + $this->set_parameters([ + '--autostart=maybe', + '--warn=60', + '--start=0', + '--duration=600', + '--title=Title', + '--description=Description', + ]); + $cli = new create(); + $cli->execute(); + } } diff --git a/tests/phpunit/cli/finish_test.php b/tests/phpunit/local/cli/finish_test.php similarity index 94% rename from tests/phpunit/cli/finish_test.php rename to tests/phpunit/local/cli/finish_test.php index 6a67e06..b787947 100644 --- a/tests/phpunit/cli/finish_test.php +++ b/tests/phpunit/local/cli/finish_test.php @@ -25,11 +25,10 @@ require_once(__DIR__.'/cli_testcase.php'); /** * Tests performed on CLI finish class. * - * @package auth_outage - * @author Daniel Thee Roperto - * @copyright 2016 Catalyst IT - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @covers \auth_outage\local\cli\finish + * @package auth_outage + * @author Daniel Thee Roperto + * @copyright 2016 Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class finish_test extends cli_testcase { public function test_constructor() { diff --git a/tests/phpunit/cli/waitforit_test.php b/tests/phpunit/local/cli/waitforit_test.php similarity index 96% rename from tests/phpunit/cli/waitforit_test.php rename to tests/phpunit/local/cli/waitforit_test.php index 8f59940..a483a35 100644 --- a/tests/phpunit/cli/waitforit_test.php +++ b/tests/phpunit/local/cli/waitforit_test.php @@ -24,11 +24,10 @@ require_once(__DIR__.'/cli_testcase.php'); /** * Tests performed on CLI waitforit class. * - * @package auth_outage - * @author Daniel Thee Roperto - * @copyright 2016 Catalyst IT - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @covers \auth_outage\local\cli\waitforit + * @package auth_outage + * @author Daniel Thee Roperto + * @copyright 2016 Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @SuppressWarnings("public") */ class waitforit_test extends cli_testcase { diff --git a/tests/phpunit/local/controllers/infopage_test.php b/tests/phpunit/local/controllers/infopage_test.php new file mode 100644 index 0000000..5082289 --- /dev/null +++ b/tests/phpunit/local/controllers/infopage_test.php @@ -0,0 +1,285 @@ +. + +use auth_outage\dml\outagedb; +use auth_outage\local\controllers\infopage; +use auth_outage\local\outage; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Tests performed on infopage controller class and update_static_page task class. + * + * @package auth_outage + * @author Daniel Thee Roperto + * @copyright 2016 Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @SuppressWarnings("public") Allow this test to have as many tests as necessary. + */ +class infopagecontroller_test extends advanced_testcase { + public function setUp() { + $file = infopage::get_defaulttemplatefile(); + if (file_exists($file)) { + if (is_file($file)) { + unlink($file); + } else { + self::fail('Invalid temp file: '.$file); + } + } + } + + public function test_constructor() { + new infopage(); + } + + public function test_constructor_withparams() { + $_GET = ['id' => 1, 'static' => 'true']; + new infopage(); + } + + /** + * @expectedException coding_exception + */ + public function test_constructor_idmismatch() { + $outage = new outage([ + 'id' => 1, + 'autostart' => false, + 'warntime' => time() - 60, + 'starttime' => time(), + 'stoptime' => time() + 60, + 'title' => 'Title', + 'description' => 'Description', + ]); + new infopage(['id' => 2, 'outage' => $outage]); + } + + /** + * @expectedException coding_exception + */ + public function test_constructor_invalidoutage() { + new infopage(['outage' => 'My outage']); + } + + public function test_staticpage_output() { + global $PAGE; + $this->resetAfterTest(true); + + $PAGE->set_context(context_system::instance()); + $now = time(); + $outage = new outage([ + 'id' => 1, + 'starttime' => $now + (60 * 60), + 'warntime' => $now - (60 * 60), + 'stoptime' => $now + (2 * 60 * 60), + 'title' => 'Outage Title at {{start}}', + 'description' => 'This is an important outage, starting at {{start}}.', + ]); + $info = new infopage(['static' => true, 'outage' => $outage]); + $html = $info->get_output(); + // Must find... + self::assertContains('', $html); + self::assertContains('', $html); + self::assertContains($outage->get_title(), $html); + self::assertContains($outage->get_description(), $html); + // Must not find... + self::assertNotContains('id, infopage::find_outageid_from_infopage($html)); + } + + public function test_staticpage_file() { + $now = time(); + $outage = new outage([ + 'id' => 1, + 'warntime' => $now - 100, + 'starttime' => $now + 100, + 'stoptime' => $now + 200, + 'title' => 'Title', + 'description' => 'Description', + ]); + $file = infopage::get_defaulttemplatefile(); + infopage::save_static_page($outage, $file); + self::assertFileExists($file); + + $id = infopage::find_outageid_from_infopage(file_get_contents($file)); + self::assertSame($outage->id, $id); + + unlink($file); + } + + public function test_getdefaulttemplatefile() { + $file = infopage::get_defaulttemplatefile(); + self::assertTrue(is_string($file)); + self::assertContains('template', $file); + } + + /** + * @expectedException coding_exception + */ + public function test_findoutageid_notstring() { + infopage::find_outageid_from_infopage(new stdClass()); + } + + public function test_findoutageid_notfound() { + self::assertNull( + infopage::find_outageid_from_infopage( + 'Hello world!Done.' + ) + ); + } + + /** + * @expectedException coding_exception + */ + public function test_savestaticpage_filenotstring() { + infopage::save_static_page(new outage(), 1); + } + + /** + * @expectedException file_exception + */ + public function test_savestaticpage_fileinvalid() { + global $CFG; + $outage = new outage([ + 'id' => 1, + 'warntime' => time() - 100, + 'starttime' => time() + 100, + 'stoptime' => time() + 200, + 'title' => 'Title', + 'description' => 'Description', + ]); + infopage::save_static_page($outage, $CFG->dataroot.'/someinvalidpath/someinvalidfile'); + } + + /** + * @expectedException invalid_state_exception + */ + public function test_sanity_notstring() { + infopage::save_static_page_sanitycheck(404); + } + + /** + * @expectedException invalid_state_exception + */ + public function test_sanity_empty() { + infopage::save_static_page_sanitycheck(' '); + } + + /** + * @expectedException invalid_state_exception + */ + public function test_sanity_clearhtml() { + infopage::save_static_page_sanitycheck(' '); + } + + public function test_updatestaticpage() { + $this->resetAfterTest(true); + self::setAdminUser(); + $file = infopage::get_defaulttemplatefile(); + $now = time(); + $outage = new outage([ + 'autostart' => false, + 'warntime' => $now - 100, + 'starttime' => $now + 100, + 'stoptime' => $now + 200, + 'title' => 'Title', + 'description' => 'Description', + ]); + $id = outagedb::save($outage); + // The method update_static_page should have been called by save(). + self::assertFileExists($file); + $idfound = infopage::find_outageid_from_infopage(file_get_contents($file)); + self::assertSame($id, $idfound); + unlink($file); + } + + public function test_updatestaticpage_nooutage() { + infopage::update_static_page(); + } + + public function test_updatestaticpage_hasfile() { + $file = infopage::get_defaulttemplatefile(); + touch($file); + self::assertFileExists($file); + infopage::update_static_page(); + self::assertFileNotExists($file); + } + + /** + * @expectedException coding_exception + */ + public function test_updatestaticpage_invalidfile() { + infopage::update_static_page(123); + } + + public function test_hasadminoptions() { + $this->resetAfterTest(true); + $static = new infopage(['static' => true]); + $nonstatic = new infopage(['static' => false]); + // Now I am guest. + self::assertFalse(is_siteadmin()); + self::assertFalse($static->has_admin_options()); + self::assertFalse($nonstatic->has_admin_options()); + // Now I am admin. + self::setAdminUser(); + self::assertTrue(is_siteadmin()); + self::assertFalse($static->has_admin_options()); + self::assertTrue($nonstatic->has_admin_options()); + } + + /** + * @expectedException coding_exception + */ + public function test_output_static_nooutage() { + $info = new infopage(['static' => true]); + $info->output(); + } + + /** + * We should have an exception because CLI cannot redirect. + * @expectedException moodle_exception + */ + public function test_output_nonstatic_nooutage() { + $info = new infopage(['static' => false]); + $info->output(); + } + + public function test_output() { + $now = time(); + $outage = new outage([ + 'id' => 1, + 'autostart' => false, + 'warntime' => $now - 100, + 'starttime' => $now + 100, + 'stoptime' => $now + 200, + 'title' => 'Title', + 'description' => 'Description', + ]); + $info = new infopage(['outage' => $outage]); + self::hasExpectationOnOutput(); + $output = $info->get_output(); + self::assertContains('auth_outage_info', $output); + } + + public function test_tasks() { + $task = new \auth_outage\task\update_static_page(); + self::assertNotEmpty($task->get_name()); + $task->execute(); + } +} diff --git a/tests/phpunit/outage_test.php b/tests/phpunit/local/outage_test.php similarity index 67% rename from tests/phpunit/outage_test.php rename to tests/phpunit/local/outage_test.php index a364fa6..8096ce3 100644 --- a/tests/phpunit/outage_test.php +++ b/tests/phpunit/local/outage_test.php @@ -25,7 +25,6 @@ defined('MOODLE_INTERNAL') || die(); * @author Daniel Thee Roperto * @copyright 2016 Catalyst IT * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @covers \auth_outage\local\outage */ class outage_test extends basic_testcase { public function test_constructor() { @@ -38,6 +37,55 @@ class outage_test extends basic_testcase { } } + public function test_constructor_object() { + $obj = new stdClass(); + $obj->id = 1; + $obj->autostart = true; + $obj->warntime = 2; + $obj->starttime = 3; + $obj->finished = 4; + $obj->stoptime = 5; + $obj->title = 'Title'; + $obj->description = 'Description'; + $outage = new outage($obj); + self::assertSame($obj->id, $outage->id); + self::assertSame($obj->autostart, $outage->autostart); + self::assertSame($obj->warntime, $outage->warntime); + self::assertSame($obj->starttime, $outage->starttime); + self::assertSame($obj->finished, $outage->finished); + self::assertSame($obj->stoptime, $outage->stoptime); + self::assertSame($obj->title, $outage->title); + self::assertSame($obj->description, $outage->description); + } + + /** + * @expectedException coding_exception + */ + public function test_constructor_invalid() { + new outage('My outage'); + } + + public function test_getstage_now() { + $now = time(); + // Make sure it is in the past. + $outage = new outage([ + 'starttime' => $now - (3 * 60 * 60), + 'stoptime' => $now - (2 * 60 * 60), + 'warntime' => $now - (2 * 60 * 60), + 'title' => '', + 'description' => '', + ]); + self::assertSame(outage::STAGE_STOPPED, $outage->get_stage()); + } + + /** + * @expectedException coding_exception + */ + public function test_getstage_invalidtime() { + $outage = new outage(); + $outage->get_stage(-1); + } + public function test_isongoing() { $now = time(); @@ -128,6 +176,7 @@ class outage_test extends basic_testcase { self::assertSame(outage::STAGE_WAITING, $outage->get_stage($now)); self::assertFalse($outage->is_active($now)); self::assertFalse($outage->is_ongoing($now)); + self::assertFalse($outage->has_ended()); $outage = new outage([ 'warntime' => $now - 10, @@ -138,6 +187,7 @@ class outage_test extends basic_testcase { self::assertSame(outage::STAGE_WARNING, $outage->get_stage($now)); self::assertTrue($outage->is_active($now)); self::assertFalse($outage->is_ongoing($now)); + self::assertFalse($outage->has_ended()); $outage = new outage([ 'warntime' => $now - 20, @@ -148,6 +198,7 @@ class outage_test extends basic_testcase { self::assertSame(outage::STAGE_ONGOING, $outage->get_stage($now)); self::assertTrue($outage->is_active($now)); self::assertTrue($outage->is_ongoing($now)); + self::assertFalse($outage->has_ended()); $outage = new outage([ 'warntime' => $now - 50, @@ -158,6 +209,7 @@ class outage_test extends basic_testcase { self::assertSame(outage::STAGE_STOPPED, $outage->get_stage($now)); self::assertFalse($outage->is_active($now)); self::assertFalse($outage->is_ongoing($now)); + self::assertTrue($outage->has_ended()); $outage = new outage([ 'warntime' => $now - 50, @@ -169,6 +221,7 @@ class outage_test extends basic_testcase { self::assertSame(outage::STAGE_FINISHED, $outage->get_stage($now)); self::assertFalse($outage->is_active($now)); self::assertFalse($outage->is_ongoing($now)); + self::assertTrue($outage->has_ended()); $outage = new outage([ 'warntime' => $now - 50, @@ -180,5 +233,38 @@ class outage_test extends basic_testcase { self::assertSame(outage::STAGE_FINISHED, $outage->get_stage($now)); self::assertFalse($outage->is_active($now)); self::assertFalse($outage->is_ongoing($now)); + self::assertTrue($outage->has_ended()); + } + + public function test_gettitle_getdescription() { + $now = time(); + $outage = new outage([ + 'warntime' => $now - 50, + 'starttime' => $now - 40, + 'stoptime' => $now - 30, + 'finished' => $now - 20, + 'title' => 'Title {{start}} {{stop}} {{duration}}', + 'description' => 'Description {{start}} {{stop}} {{duration}}', + ]); + $title = $outage->get_title(); + self::assertNotContains('{', $title); + self::assertNotContains('}', $title); + $description = $outage->get_description(); + self::assertNotContains('{', $description); + self::assertNotContains('}', $description); + } + + public function test_getdurations() { + $outage = new outage(['starttime' => 1000]); + self::assertNull($outage->get_duration_actual()); + + $outage->finished = 3000; + self::assertSame(2000, $outage->get_duration_actual()); + + $outage->stoptime = 3050; + self::assertEquals(2050, $outage->get_duration_planned()); + + $outage->warntime = 600; + self::assertEquals(400, $outage->get_warning_duration()); } } diff --git a/tests/phpunit/local/outagelib_test.php b/tests/phpunit/local/outagelib_test.php new file mode 100644 index 0000000..84dfd85 --- /dev/null +++ b/tests/phpunit/local/outagelib_test.php @@ -0,0 +1,152 @@ +. + +use auth_outage\dml\outagedb; +use auth_outage\local\outage; +use auth_outage\local\outagelib; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Tests performed on outagelib class. + * + * @package auth_outage + * @author Daniel Thee Roperto + * @copyright 2016 Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class outagelib_test extends advanced_testcase { + public function test_maintenancemessage() { + $this->resetAfterTest(true); + static::setAdminUser(); + + $now = time(); + $outage = new outage([ + 'autostart' => true, + 'warntime' => $now, + 'starttime' => $now + 100, + 'stoptime' => $now + 200, + 'title' => 'Title', + 'description' => 'Description', + ]); + + set_config('maintenance_message', 'A message.'); + outagedb::save($outage); + self::assertFalse((bool)get_config('moodle', 'maintenance_message')); + self::assertCount(2, phpunit_util::get_debugging_messages()); + phpunit_util::reset_debugging(); + } + + public function test_maintenancelater_nonext() { + $this->resetAfterTest(true); + set_config('maintenance_later', time() + (60 * 60 * 24 * 7)); // In 1 week. + self::assertNotEmpty(get_config('moodle', 'maintenance_later')); + outagelib::outages_modified(); + self::assertEmpty(get_config('moodle', 'maintenance_later')); + } + + public function test_inject() { + global $CFG; + + $this->resetAfterTest(true); + self::setAdminUser(); + $now = time(); + $outage = new outage([ + 'autostart' => true, + 'warntime' => $now, + 'starttime' => $now + 100, + 'stoptime' => $now + 200, + 'title' => 'Title', + 'description' => 'Description', + ]); + $outage->id = outagedb::save($outage); + self::assertEmpty($CFG->additionalhtmltopofbody); + + outagelib::reinject(); + self::assertContains('