From 5df01b342e29a72fa21601eb4e0b815ed5e4316c Mon Sep 17 00:00:00 2001 From: Daniel Thee Roperto Date: Thu, 22 Sep 2016 16:47:07 +1000 Subject: [PATCH] Fixed insepections, refactored to reduce complexity (phpmd warning). --- classes/calendar/calendar.php | 118 +++++++++++++++++++++++++ classes/{local => dml}/outagedb.php | 90 ++----------------- classes/local/cli/cli_exception.php | 35 ++++++++ classes/local/cli/clibase.php | 5 +- classes/local/cli/create.php | 113 +++++++++++++++-------- classes/local/cli/finish.php | 12 +-- classes/local/cli/waitforit.php | 14 +-- classes/local/controllers/infopage.php | 2 +- classes/local/outage.php | 21 +++-- classes/local/outagelib.php | 1 + classes/task/update_static_page.php | 7 ++ clone.php | 2 +- delete.php | 2 +- edit.php | 2 +- finish.php | 2 +- lib.php | 2 - manage.php | 2 +- new.php | 2 +- tests/cli/cli_test.php | 24 +++-- tests/cli/create_test.php | 32 +++++-- tests/cli/finish_test.php | 27 ++++-- tests/cli/waitforit_test.php | 28 ++++-- tests/events_test.php | 2 +- tests/outagedb_test.php | 2 +- 24 files changed, 366 insertions(+), 181 deletions(-) create mode 100644 classes/calendar/calendar.php rename classes/{local => dml}/outagedb.php (79%) diff --git a/classes/calendar/calendar.php b/classes/calendar/calendar.php new file mode 100644 index 0000000..37eac35 --- /dev/null +++ b/classes/calendar/calendar.php @@ -0,0 +1,118 @@ +. + +namespace auth_outage\calendar; + +use auth_outage\local\outage; +use calendar_event; + +defined('MOODLE_INTERNAL') || die(); + +require_once($CFG->dirroot.'/calendar/lib.php'); + +/** + * Manages outages in the calendar. + * + * @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 calendar { + /** + * Private constructor, use static methods instead. + */ + private function __construct() { + } + + /** + * 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)); + } + + /** + * Updates an event on the calendar based on this outage. + * @param outage $outage Outage to be updated in the calendar. + */ + public static function calendar_update(outage $outage) { + $event = self::calendar_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); + } else { + $event->update(self::calendar_data($outage)); + } + } + + /** + * 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); + + // If not found (was not created before) ignore it. + if (is_null($event)) { + debugging('Cannot delete calendar entry for outage #'.$outageid.', event not found. Ignoring it...'); + } else { + $event->delete(); + } + } + + /** + * Generates an array with the calendar event data based on an outage object. + * @param outage $outage Outage to use as reference for the calendar event. + * @return mixed[] Calendar event data. + */ + private static function calendar_data(outage $outage) { + return [ + 'name' => $outage->get_title(), + 'description' => $outage->get_description(), + 'courseid' => 1, + 'groupid' => 0, + 'userid' => 0, + 'modulename' => '', + 'instance' => $outage->id, + 'eventtype' => 'auth_outage', + 'timestart' => $outage->starttime, + 'visible' => true, + 'timeduration' => $outage->get_duration_planned(), + ]; + } + + /** + * Finds the calendar event for an specific outage. + * @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) { + global $DB; + + $event = $DB->get_record_select( + 'event', + "(eventtype = 'auth_outage' AND instance = :outageid)", + ['outageid' => $outageid], + 'id', + IGNORE_MISSING + ); + + return ($event === false) ? null : calendar_event::load($event->id); + } +} diff --git a/classes/local/outagedb.php b/classes/dml/outagedb.php similarity index 79% rename from classes/local/outagedb.php rename to classes/dml/outagedb.php index 7045ae6..85b754d 100644 --- a/classes/local/outagedb.php +++ b/classes/dml/outagedb.php @@ -14,12 +14,14 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -namespace auth_outage\local; +namespace auth_outage\dml; +use auth_outage\calendar\calendar; use auth_outage\event\outage_created; use auth_outage\event\outage_deleted; use auth_outage\event\outage_updated; -use calendar_event; +use auth_outage\local\outage; +use auth_outage\local\outagelib; use coding_exception; defined('MOODLE_INTERNAL') || die(); @@ -104,7 +106,7 @@ class outagedb { ['objectid' => $outage->id, 'other' => (array)$outage] )->trigger(); // Create calendar entry. - self::calendar_create($outage); + calendar::calendar_create($outage); } else { // Remove the createdby field so it does not get updated. unset($outage->createdby); @@ -114,7 +116,7 @@ class outagedb { ['objectid' => $outage->id, 'other' => (array)$outage] )->trigger(); // Update calendar entry. - self::calendar_update($outage); + calendar::calendar_update($outage); } // Trigger outages modified events. @@ -145,7 +147,7 @@ class outagedb { // Delete it and remove from calendar. $DB->delete_records('auth_outage', ['id' => $id]); - self::calendar_delete($id); + calendar::calendar_delete($id); // Trigger events. outagelib::outages_modified(); @@ -341,82 +343,4 @@ class outagedb { // Allowing multiple records still raises an internal error. return (count($data) == 0) ? null : new outage(array_shift($data)); } - - /** - * Create an event on the calendar for this outage. - * @param outage $outage Outage to be added to the calendar. - */ - private static function calendar_create(outage $outage) { - calendar_event::create(self::calendar_data($outage)); - } - - /** - * Updates an event on the calendar based on this outage. - * @param outage $outage Outage to be updated in the calendar. - */ - private static function calendar_update(outage $outage) { - $event = self::calendar_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); - } else { - $event->update(self::calendar_data($outage)); - } - } - - /** - * Removes an event from the calendar related to this outage. - * @param int $outageid Id of outage to be deleted from the calendar. - */ - private static function calendar_delete($outageid) { - $event = self::calendar_load($outageid); - - // If not found (was not created before) ignore it. - if (is_null($event)) { - debugging('Cannot delete calendar entry for outage #'.$outageid.', event not found. Ignoring it...'); - } else { - $event->delete(); - } - } - - /** - * Generates an array with the calendar event data based on an outage object. - * @param outage $outage Outage to use as reference for the calendar event. - * @return mixed[] Calendar event data. - */ - private static function calendar_data(outage $outage) { - return [ - 'name' => $outage->get_title(), - 'description' => $outage->get_description(), - 'courseid' => 1, - 'groupid' => 0, - 'userid' => 0, - 'modulename' => '', - 'instance' => $outage->id, - 'eventtype' => 'auth_outage', - 'timestart' => $outage->starttime, - 'visible' => true, - 'timeduration' => $outage->get_duration_planned(), - ]; - } - - /** - * Finds the calendar event for an specific outage. - * @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) { - global $DB; - - $event = $DB->get_record_select( - 'event', - "(eventtype = 'auth_outage' AND instance = :outageid)", - ['outageid' => $outageid], - 'id', - IGNORE_MISSING - ); - - return ($event === false) ? null : calendar_event::load($event->id); - } } diff --git a/classes/local/cli/cli_exception.php b/classes/local/cli/cli_exception.php index 4f4f2b7..750e98a 100644 --- a/classes/local/cli/cli_exception.php +++ b/classes/local/cli/cli_exception.php @@ -29,6 +29,41 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class cli_exception extends Exception { + /** + * Undefined error. + */ + const ERROR_UNDEFINED = 1; + + /** + * Unknow parameter. + */ + const ERROR_PARAMETER_UNKNOWN = 2; + + /** + * Invalid parameter usage. + */ + const ERROR_PARAMETER_INVALID = 3; + + /** + * Missing required parameter. + */ + const ERROR_PARAMETER_MISSING = 4; + + /** + * The informed outage cannot be used for that purpose. + */ + const ERROR_OUTAGE_INVALID = 5; + + /** + * The informed outage was not found. + */ + const ERROR_OUTAGE_NOT_FOUND = 6; + + /** + * The outage has changed before the completion of the command. + */ + const ERROR_OUTAGE_CHANGED = 7; + /** * cliexception constructor. * @param string $message An explanation of the exception. diff --git a/classes/local/cli/clibase.php b/classes/local/cli/clibase.php index 24af488..6d3cae5 100644 --- a/classes/local/cli/clibase.php +++ b/classes/local/cli/clibase.php @@ -56,14 +56,15 @@ abstract class clibase { list($options, $unrecognized) = cli_get_params($this->generate_options(), $this->generate_shortcuts()); if ($unrecognized) { $unrecognized = implode("\n ", $unrecognized); - throw new cli_exception(get_string('cliunknowoption', 'admin', $unrecognized)); + throw new cli_exception(get_string('cliunknowoption', 'admin', $unrecognized), + cli_exception::ERROR_PARAMETER_UNKNOWN); } } else { // If not using Moodle CLI API to read parameters, ensure all keys exist. $default = $this->generate_options(); foreach ($options as $k => $v) { if (!array_key_exists($k, $default)) { - throw new cli_exception(get_string('cliunknowoption', 'admin', $k)); + throw new cli_exception(get_string('cliunknowoption', 'admin', $k), cli_exception::ERROR_PARAMETER_UNKNOWN); } $default[$k] = $v; } diff --git a/classes/local/cli/create.php b/classes/local/cli/create.php index 0405258..075dc18 100644 --- a/classes/local/cli/create.php +++ b/classes/local/cli/create.php @@ -16,8 +16,8 @@ namespace auth_outage\local\cli; +use auth_outage\dml\outagedb; use auth_outage\local\outage; -use auth_outage\local\outagedb; use coding_exception; defined('MOODLE_INTERNAL') || die(); @@ -112,7 +112,8 @@ class create extends clibase { // If not help mode, 'start' is required and cannot use default. if (is_null($this->options['start'])) { - throw new cli_exception(get_string('clierrormissingparamaters', 'auth_outage')); + throw new cli_exception(get_string('clierrormissingparamaters', 'auth_outage'), + cli_exception::ERROR_PARAMETER_MISSING); } // If cloning, set defaults to outage being cloned. @@ -184,7 +185,8 @@ class create extends clibase { private function clone_defaults() { $id = $this->options['clone']; if (!is_number($id) || ($id <= 0)) { - throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => 'clone'])); + throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => 'clone']), + cli_exception::ERROR_PARAMETER_INVALID); } $outage = outagedb::get_by_id((int)$id); @@ -204,51 +206,84 @@ class create extends clibase { * @throws cli_exception */ private function merge_options_check_parameters(array $options) { - // Check parameters that must be a non-negative int while converting their type to int. foreach (['start', 'warn', 'duration'] as $param) { - if (!is_number($options[$param])) { - throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param])); - } - $options[$param] = (int)$options[$param]; - if ($options[$param] < 0) { - throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param])); - } + $options[$param] = $this->merge_options_check_parameters_int_nonnegative($options[$param], $param); } - // Check parameters that must be a non empty string. foreach (['title', 'description'] as $param) { - if (!is_string($options[$param])) { - throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param])); - } - $options[$param] = trim($options[$param]); - if (strlen($options[$param]) == 0) { - throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param])); - } + $options[$param] = $this->merge_options_check_parameters_string_nonempty($options[$param], $param); } - // Check parameters that must be a specified bool. foreach (['autostart'] as $param) { - if (is_string($options[$param])) { - switch (strtoupper($options[$param])) { - case '0': - case 'FALSE': - case 'NO': - case 'N': - $options[$param] = false; - break; - case '1': - case 'TRUE': - case 'YES': - case 'Y': - $options[$param] = true; - break; - } - } - if (!is_bool($options[$param])) { - throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param])); - } + $options[$param] = $this->merge_options_check_parameters_bool($options[$param], $param); } return $options; } + + /** + * Ensures the given option is or can be converted to a non-negative int. + * @param mixed $option The parameter to check. + * @param string $param Name of that parameter. + * @return int The converted parameter. + * @throws cli_exception + */ + private function merge_options_check_parameters_int_nonnegative($option, $param) { + if (!is_number($option)) { + throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param]), + cli_exception::ERROR_PARAMETER_INVALID); + } + $option = (int)$option; + if ($option < 0) { + throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param]), + cli_exception::ERROR_PARAMETER_INVALID); + } + return $option; + } + + /** + * Ensures the given option is or can be converted to a non-empty string. + * @param mixed $option The parameter to check. + * @param string $param Name of that parameter. + * @return string The converted parameter. + * @throws cli_exception + */ + + private function merge_options_check_parameters_string_nonempty($option, $param) { + if (!is_string($option)) { + throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param]), + cli_exception::ERROR_PARAMETER_INVALID); + } + $option = trim($option); + if (strlen($option) == 0) { + throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param]), + cli_exception::ERROR_PARAMETER_INVALID); + } + return $option; + } + + /** + * Ensures the given option is or can be converted to a bool. + * @param mixed $option The parameter to check. + * @param string $param Name of that parameter. + * @return bool The converted parameter. + * @throws cli_exception + */ + private function merge_options_check_parameters_bool($option, $param) { + if (is_bool($option)) { + return $option; + } + + if (is_string($option)) { + $option = strtoupper($option); + if (in_array($option, ['0', 'FALSE', 'NO', 'N'])) { + return false; + } + if (in_array($option, ['1', 'TRUE', 'YES', 'Y'])) { + return true; + } + } + + throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param])); + } } diff --git a/classes/local/cli/finish.php b/classes/local/cli/finish.php index 70573a0..d3675d9 100644 --- a/classes/local/cli/finish.php +++ b/classes/local/cli/finish.php @@ -16,8 +16,8 @@ namespace auth_outage\local\cli; +use auth_outage\dml\outagedb; use auth_outage\local\outage; -use auth_outage\local\outagedb; defined('MOODLE_INTERNAL') || die(); @@ -70,12 +70,13 @@ class finish extends clibase { $byid = !is_null($this->options['outageid']); $byactive = $this->options['active']; if ($byid == $byactive) { - throw new cli_exception(get_string('cliwaitforiterroridxoractive', 'auth_outage')); + throw new cli_exception(get_string('cliwaitforiterroridxoractive', 'auth_outage'), + cli_exception::ERROR_PARAMETER_MISSING); } $outage = $this->get_outage(); if (!$outage->is_ongoing()) { - throw new cli_exception(get_string('clifinishnotongoing', 'auth_outage')); + throw new cli_exception(get_string('clifinishnotongoing', 'auth_outage'), cli_exception::ERROR_OUTAGE_INVALID); } outagedb::finish($outage->id, $this->time); @@ -92,13 +93,14 @@ class finish extends clibase { } else { $id = $this->options['outageid']; if (!is_number($id) || ($id <= 0)) { - throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => 'outageid'])); + throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => 'outageid']), + cli_exception::ERROR_PARAMETER_INVALID); } $outage = outagedb::get_by_id((int)$id); } if (is_null($outage)) { - throw new cli_exception(get_string('clierroroutagenotfound', 'auth_outage')); + throw new cli_exception(get_string('clierroroutagenotfound', 'auth_outage'), cli_exception::ERROR_OUTAGE_NOT_FOUND); } return $outage; diff --git a/classes/local/cli/waitforit.php b/classes/local/cli/waitforit.php index 9d6dbff..8f2cc2a 100644 --- a/classes/local/cli/waitforit.php +++ b/classes/local/cli/waitforit.php @@ -16,8 +16,8 @@ namespace auth_outage\local\cli; +use auth_outage\dml\outagedb; use auth_outage\local\outage; -use auth_outage\local\outagedb; defined('MOODLE_INTERNAL') || die(); @@ -92,7 +92,8 @@ class waitforit extends clibase { $byid = !is_null($this->options['outageid']); $byactive = $this->options['active']; if ($byid == $byactive) { - throw new cli_exception(get_string('cliwaitforiterroridxoractive', 'auth_outage')); + throw new cli_exception(get_string('cliwaitforiterroridxoractive', 'auth_outage'), + cli_exception::ERROR_PARAMETER_INVALID); } $this->verbose('Verbose mode activated.'); @@ -137,14 +138,15 @@ class waitforit extends clibase { } else { $id = $this->options['outageid']; if (!is_number($id) || ($id <= 0)) { - throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => 'outageid'])); + throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => 'outageid']), + cli_exception::ERROR_PARAMETER_INVALID); } $this->verbose('Querying database for outage #'.$id.'...'); $outage = outagedb::get_by_id((int)$id); } if (is_null($outage)) { - throw new cli_exception(get_string('clierroroutagenotfound', 'auth_outage')); + throw new cli_exception(get_string('clierroroutagenotfound', 'auth_outage'), cli_exception::ERROR_OUTAGE_NOT_FOUND); } $this->verbose('Found outage #'.$outage->id.': '.$outage->get_title()); @@ -161,11 +163,11 @@ class waitforit extends clibase { $this->verbose('Checking outage status...'); // Outage should not change while waiting to start. if (outagedb::get_by_id($outage->id) != $outage) { - throw new cli_exception(get_string('clierroroutagechanged', 'auth_outage')); + throw new cli_exception(get_string('clierroroutagechanged', 'auth_outage'), cli_exception::ERROR_OUTAGE_CHANGED); } // Outage cannot have already ended. if ($outage->has_ended($this->time)) { - throw new cli_exception(get_string('clierroroutageended', 'auth_outage')); + throw new cli_exception(get_string('clierroroutageended', 'auth_outage'), cli_exception::ERROR_OUTAGE_INVALID); } // If outage has started, do not wait. if ($outage->is_ongoing($this->time)) { diff --git a/classes/local/controllers/infopage.php b/classes/local/controllers/infopage.php index 689a636..965a97f 100644 --- a/classes/local/controllers/infopage.php +++ b/classes/local/controllers/infopage.php @@ -16,8 +16,8 @@ namespace auth_outage\local\controllers; +use auth_outage\dml\outagedb; use auth_outage\local\outage; -use auth_outage\local\outagedb; use auth_outage\local\outagelib; use coding_exception; use context_system; diff --git a/classes/local/outage.php b/classes/local/outage.php index 2f7c9be..854ca37 100644 --- a/classes/local/outage.php +++ b/classes/local/outage.php @@ -132,15 +132,7 @@ class outage { $this->$k = $v; } } - - // Adjust int fields. - $fs = ['createdby', 'id', 'lastmodified', 'modifiedby', 'starttime', 'stoptime', 'warntime', 'finished']; - foreach ($fs as $f) { - $this->$f = ($this->$f === null) ? null : (int)$this->$f; - } - - // Adjust bool fields. - $this->autostart = ($this->autostart === null) ? null : (bool)$this->autostart; + $this->adjust_field_types(); } /** @@ -274,4 +266,15 @@ class outage { $str ); } + + private function adjust_field_types() { + // Adjust int fields. + $fs = ['createdby', 'id', 'lastmodified', 'modifiedby', 'starttime', 'stoptime', 'warntime', 'finished']; + foreach ($fs as $f) { + $this->$f = ($this->$f === null) ? null : (int)$this->$f; + } + + // Adjust bool fields. + $this->autostart = ($this->autostart === null) ? null : (bool)$this->autostart; + } } diff --git a/classes/local/outagelib.php b/classes/local/outagelib.php index 9b44103..9686bfe 100644 --- a/classes/local/outagelib.php +++ b/classes/local/outagelib.php @@ -16,6 +16,7 @@ namespace auth_outage\local; +use auth_outage\dml\outagedb; use auth_outage\local\controllers\infopage; use auth_outage_renderer; use Exception; diff --git a/classes/task/update_static_page.php b/classes/task/update_static_page.php index 080793a..bcb5cc4 100644 --- a/classes/task/update_static_page.php +++ b/classes/task/update_static_page.php @@ -29,10 +29,17 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class update_static_page extends scheduled_task { + /** + * Gets the name of this event. + * @return string Name of this event. + */ public function get_name() { return get_string('taskupdatestaticpage', 'auth_outage'); } + /** + * Executes the event. + */ public function execute() { infopage::update_static_page(); } diff --git a/clone.php b/clone.php index 0495888..7552ded 100644 --- a/clone.php +++ b/clone.php @@ -23,8 +23,8 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +use auth_outage\dml\outagedb; use auth_outage\form\outage\edit; -use auth_outage\local\outagedb; use auth_outage\local\outagelib; require_once(__DIR__.'/../../config.php'); diff --git a/delete.php b/delete.php index eb1cb05..93a8aa9 100644 --- a/delete.php +++ b/delete.php @@ -23,8 +23,8 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +use auth_outage\dml\outagedb; use auth_outage\form\outage\delete; -use auth_outage\local\outagedb; use auth_outage\local\outagelib; require_once(__DIR__.'/../../config.php'); diff --git a/edit.php b/edit.php index b675d89..b5a50e5 100644 --- a/edit.php +++ b/edit.php @@ -23,8 +23,8 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +use auth_outage\dml\outagedb; use auth_outage\form\outage\edit; -use auth_outage\local\outagedb; use auth_outage\local\outagelib; require_once(__DIR__.'/../../config.php'); diff --git a/finish.php b/finish.php index fdb61d1..84b1a3e 100644 --- a/finish.php +++ b/finish.php @@ -23,8 +23,8 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +use auth_outage\dml\outagedb; use auth_outage\form\outage\finish; -use auth_outage\local\outagedb; use auth_outage\local\outagelib; require_once(__DIR__.'/../../config.php'); diff --git a/lib.php b/lib.php index 17ebffe..91effb7 100644 --- a/lib.php +++ b/lib.php @@ -24,8 +24,6 @@ */ defined('MOODLE_INTERNAL') || die; -// FIXME hook not installing in courses/index.php page as guest. - function auth_outage_extend_navigation_user() { \auth_outage\local\outagelib::inject(); } diff --git a/manage.php b/manage.php index 623243d..507ee08 100644 --- a/manage.php +++ b/manage.php @@ -23,7 +23,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -use auth_outage\local\outagedb; +use auth_outage\dml\outagedb; use auth_outage\local\outagelib; require_once(__DIR__.'/../../config.php'); diff --git a/new.php b/new.php index 9c01e2b..4cb1d1a 100644 --- a/new.php +++ b/new.php @@ -23,9 +23,9 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +use auth_outage\dml\outagedb; use auth_outage\form\outage\edit; use auth_outage\local\outage; -use auth_outage\local\outagedb; use auth_outage\local\outagelib; require_once(__DIR__.'/../../config.php'); diff --git a/tests/cli/cli_test.php b/tests/cli/cli_test.php index 984e0b9..400acc5 100644 --- a/tests/cli/cli_test.php +++ b/tests/cli/cli_test.php @@ -32,14 +32,20 @@ require_once(__DIR__.'/cli_testcase.php'); * @SuppressWarnings("public") */ class cli_test extends cli_testcase { + /** + * @expectedException auth_outage\local\cli\cli_exception + * @expectedExceptionCode 2 + */ public function test_invalidargumentparam() { $this->set_parameters(['--aninvalidparameter']); - $this->setExpectedException(cli_exception::class); new create(); } + /** + * @expectedException auth_outage\local\cli\cli_exception + * @expectedExceptionCode 2 + */ public function test_invalidargumentgiven() { - $this->setExpectedException(cli_exception::class); new create(['anotherinvalidparameter']); } @@ -49,9 +55,12 @@ class cli_test extends cli_testcase { $cli->set_referencetime(60 * 60 * 24 * 7); } + /** + * @expectedException coding_exception + */ public function test_setreferencetime_invalid() { - $cli = new create(['start' => 0]); - $this->setExpectedException(coding_exception::class); + $this->set_parameters(['--start=60']); + $cli = new create(); $cli->set_referencetime(-1); } @@ -63,8 +72,11 @@ class cli_test extends cli_testcase { self::assertContains('--help', $output); } + /** + * @expectedException auth_outage\local\cli\cli_exception + * @expectedExceptionCode 1 + */ public function test_exception() { - self::setExpectedException(cli_exception::class, '*ERROR* An CLI exception.', 5); - throw new cli_exception('An CLI exception.', 5); + throw new cli_exception('An CLI exception.'); } } diff --git a/tests/cli/create_test.php b/tests/cli/create_test.php index 56e861b..a02abfe 100644 --- a/tests/cli/create_test.php +++ b/tests/cli/create_test.php @@ -14,10 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +use auth_outage\dml\outagedb; use auth_outage\local\cli\cli_exception; use auth_outage\local\cli\create; use auth_outage\local\outage; -use auth_outage\local\outagedb; defined('MOODLE_INTERNAL') || die(); require_once(__DIR__.'/cli_testcase.php'); @@ -32,12 +32,19 @@ require_once(__DIR__.'/cli_testcase.php'); * @SuppressWarnings("public") Allow this test to have as many tests as necessary. */ class create_test extends cli_testcase { + /** + * @expectedException auth_outage\local\cli\cli_exception + * @expectedExceptionCode 4 + */ public function test_noarguments() { $cli = new create(); - $this->setExpectedException(cli_exception::class); $this->execute($cli); } + /** + * @expectedException auth_outage\local\cli\cli_exception + * @expectedExceptionCode 3 + */ public function test_invalidparam_notanumber() { $cli = new create(['start' => 'some day']); $cli->set_defaults([ @@ -47,10 +54,13 @@ class create_test extends cli_testcase { 'title' => 'Default Title', 'description' => 'Default Description', ]); - $this->setExpectedException(cli_exception::class); $this->execute($cli); } + /** + * @expectedException auth_outage\local\cli\cli_exception + * @expectedExceptionCode 3 + */ public function test_invalidparam_negative() { $cli = new create(['start' => -1]); $cli->set_defaults([ @@ -60,10 +70,13 @@ class create_test extends cli_testcase { 'title' => 'Default Title', 'description' => 'Default Description', ]); - $this->setExpectedException(cli_exception::class); $this->execute($cli); } + /** + * @expectedException auth_outage\local\cli\cli_exception + * @expectedExceptionCode 3 + */ public function test_invalidparam_emptystring() { $cli = new create(['start' => 0, 'title' => '']); $cli->set_defaults([ @@ -73,10 +86,13 @@ class create_test extends cli_testcase { 'title' => 'Default Title', 'description' => 'Default Description', ]); - $this->setExpectedException(cli_exception::class); $this->execute($cli); } + /** + * @expectedException auth_outage\local\cli\cli_exception + * @expectedExceptionCode 3 + */ public function test_invalidparam_notastring() { $cli = new create(['start' => 0, 'title' => true]); $cli->set_defaults([ @@ -86,7 +102,6 @@ class create_test extends cli_testcase { 'title' => 'Default Title', 'description' => 'Default Description', ]); - $this->setExpectedException(cli_exception::class); $this->execute($cli); } @@ -227,8 +242,11 @@ class create_test extends cli_testcase { self::assertSame($original->description, $cloned->description); } + /** + * @expectedException auth_outage\local\cli\cli_exception + * @expectedExceptionCode 3 + */ public function test_create_withclone_invalid() { - $this->setExpectedException(cli_exception::class); $this->set_parameters([ '--start=60', '--clone=-1', diff --git a/tests/cli/finish_test.php b/tests/cli/finish_test.php index 10ee192..6a67e06 100644 --- a/tests/cli/finish_test.php +++ b/tests/cli/finish_test.php @@ -14,10 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +use auth_outage\dml\outagedb; use auth_outage\local\cli\cli_exception; use auth_outage\local\cli\finish; use auth_outage\local\outage; -use auth_outage\local\outagedb; defined('MOODLE_INTERNAL') || die(); require_once(__DIR__.'/cli_testcase.php'); @@ -59,12 +59,19 @@ class finish_test extends cli_testcase { self::assertContains('--help', $text); } + /** + * @expectedException auth_outage\local\cli\cli_exception + * @expectedExceptionCode 4 + */ public function test_noarguments() { $cli = new finish(); - $this->setExpectedException(cli_exception::class); $this->execute($cli); } + /** + * @expectedException auth_outage\local\cli\cli_exception + * @expectedExceptionCode 5 + */ public function test_endedoutage() { self::setAdminUser(); $now = time(); @@ -79,7 +86,6 @@ class finish_test extends cli_testcase { $this->set_parameters(['-id='.$id]); $cli = new finish(); $cli->set_referencetime($now); - $this->setExpectedException(cli_exception::class); $this->execute($cli); } @@ -100,27 +106,36 @@ class finish_test extends cli_testcase { $this->execute($cli); } + /** + * @expectedException auth_outage\local\cli\cli_exception + * @expectedExceptionCode 6 + */ public function test_activenotfound() { self::setAdminUser(); $this->set_parameters(['-a']); $cli = new finish(); - $this->setExpectedException(cli_exception::class); $this->execute($cli); } + /** + * @expectedException auth_outage\local\cli\cli_exception + * @expectedExceptionCode 3 + */ public function test_invalidid() { self::setAdminUser(); $this->set_parameters(['-id=theid']); $cli = new finish(); - $this->setExpectedException(cli_exception::class); $this->execute($cli); } + /** + * @expectedException auth_outage\local\cli\cli_exception + * @expectedExceptionCode 6 + */ public function test_idnotfound() { self::setAdminUser(); $this->set_parameters(['-id=99999']); $cli = new finish(); - $this->setExpectedException(cli_exception::class); $this->execute($cli); } } diff --git a/tests/cli/waitforit_test.php b/tests/cli/waitforit_test.php index e613740..8f59940 100644 --- a/tests/cli/waitforit_test.php +++ b/tests/cli/waitforit_test.php @@ -14,10 +14,9 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -use auth_outage\local\cli\cli_exception; +use auth_outage\dml\outagedb; use auth_outage\local\cli\waitforit; use auth_outage\local\outage; -use auth_outage\local\outagedb; defined('MOODLE_INTERNAL') || die(); require_once(__DIR__.'/cli_testcase.php'); @@ -63,27 +62,40 @@ class waitforit_test extends cli_testcase { self::assertContains('--help', $text); } + /** + * @expectedException auth_outage\local\cli\cli_exception + * @expectedExceptionCode 3 + */ public function test_bothparams() { $this->set_parameters(['--outageid=1', '--active']); $cli = new waitforit(); - $this->setExpectedException(cli_exception::class); $cli->execute(); } + /** + * @expectedException auth_outage\local\cli\cli_exception + * @expectedExceptionCode 3 + */ public function test_invalidoutageid() { $this->set_parameters(['-id=-1']); $cli = new waitforit(); - $this->setExpectedException(cli_exception::class); $this->execute($cli); } + /** + * @expectedException auth_outage\local\cli\cli_exception + * @expectedExceptionCode 6 + */ public function test_outagenotfound() { $this->set_parameters(['-a']); $cli = new waitforit(); - $this->setExpectedException(cli_exception::class); $this->execute($cli); } + /** + * @expectedException auth_outage\local\cli\cli_exception + * @expectedExceptionCode 5 + */ public function test_endedoutage() { self::setAdminUser(); $now = time(); @@ -98,7 +110,6 @@ class waitforit_test extends cli_testcase { $this->set_parameters(['-id='.$id]); $cli = new waitforit(); $cli->set_referencetime($now); - $this->setExpectedException(cli_exception::class); $this->execute($cli); } @@ -148,6 +159,10 @@ class waitforit_test extends cli_testcase { self::assertContains("started!", $output); } + /** + * @expectedException auth_outage\local\cli\cli_exception + * @expectedExceptionCode 7 + */ public function test_outagechanged() { self::setAdminUser(); $now = time(); @@ -170,7 +185,6 @@ class waitforit_test extends cli_testcase { // Pretend it is time to start, but it should get an error instead. return $outage->starttime; }); - $this->setExpectedException(cli_exception::class); $this->execute($cli); } } diff --git a/tests/events_test.php b/tests/events_test.php index e1aaaeb..54bef26 100644 --- a/tests/events_test.php +++ b/tests/events_test.php @@ -14,8 +14,8 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +use auth_outage\dml\outagedb; use auth_outage\local\outage; -use auth_outage\local\outagedb; defined('MOODLE_INTERNAL') || die(); diff --git a/tests/outagedb_test.php b/tests/outagedb_test.php index 4b91a4d..27f53b3 100644 --- a/tests/outagedb_test.php +++ b/tests/outagedb_test.php @@ -14,8 +14,8 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +use auth_outage\dml\outagedb; use auth_outage\local\outage; -use auth_outage\local\outagedb; defined('MOODLE_INTERNAL') || die();