Fixed insepections, refactored to reduce complexity (phpmd warning).

This commit is contained in:
Daniel Thee Roperto
2016-09-22 16:47:07 +10:00
parent b302d9dfa5
commit 5df01b342e
24 changed files with 366 additions and 181 deletions

View File

@@ -0,0 +1,118 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
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 <daniel.roperto@catalyst-au.net>
* @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);
}
}

View File

@@ -14,12 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
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);
}
}

View File

@@ -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.

View File

@@ -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;
}

View File

@@ -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]));
}
}

View File

@@ -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;

View File

@@ -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)) {

View File

@@ -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;

View File

@@ -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;
}
}

View File

@@ -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;

View File

@@ -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();
}

View File

@@ -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');

View File

@@ -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');

View File

@@ -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');

View File

@@ -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');

View File

@@ -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();
}

View File

@@ -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');

View File

@@ -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');

View File

@@ -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.');
}
}

View File

@@ -14,10 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
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',

View File

@@ -14,10 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
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);
}
}

View File

@@ -14,10 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
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);
}
}

View File

@@ -14,8 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
use auth_outage\dml\outagedb;
use auth_outage\local\outage;
use auth_outage\local\outagedb;
defined('MOODLE_INTERNAL') || die();

View File

@@ -14,8 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
use auth_outage\dml\outagedb;
use auth_outage\local\outage;
use auth_outage\local\outagedb;
defined('MOODLE_INTERNAL') || die();