diff --git a/classes/cli/clibase.php b/classes/cli/clibase.php index d0eb62c..2cf2b52 100644 --- a/classes/cli/clibase.php +++ b/classes/cli/clibase.php @@ -19,10 +19,6 @@ namespace auth_outage\cli; use core\session\manager; use InvalidArgumentException; -defined('MOODLE_INTERNAL') || die(); - -require_once($CFG->libdir . '/clilib.php'); - /** * Outage CLI base class. * @@ -48,6 +44,9 @@ abstract class clibase { * @throws cliexception */ public function __construct(array $options = null) { + global $CFG; + require_once($CFG->libdir . '/clilib.php'); + $this->becomeadmin(); if (is_null($options)) { diff --git a/classes/cli/cliexception.php b/classes/cli/cliexception.php index 2ae4b1f..79ae8f4 100644 --- a/classes/cli/cliexception.php +++ b/classes/cli/cliexception.php @@ -27,4 +27,13 @@ use Exception; * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class cliexception extends Exception { + /** + * cliexception constructor. + * @param string $message An explanation of the exception. + * @param int $code Exit code to be used. + * @param Exception|null $previous Another exception as reference. + */ + public function __construct($message, $code = 1, Exception $previous = null) { + parent::__construct('*ERROR* ' . $message, $code, $previous = null); + } } diff --git a/classes/cli/create.php b/classes/cli/create.php index 3c245aa..35f7850 100644 --- a/classes/cli/create.php +++ b/classes/cli/create.php @@ -19,8 +19,6 @@ namespace auth_outage\cli; use auth_outage\models\outage; use auth_outage\outagedb; -defined('MOODLE_INTERNAL') || die(); - /** * Outage CLI to create outage. * diff --git a/classes/cli/finish.php b/classes/cli/finish.php index a1d56e3..df498b5 100644 --- a/classes/cli/finish.php +++ b/classes/cli/finish.php @@ -19,8 +19,6 @@ namespace auth_outage\cli; use auth_outage\models\outage; use auth_outage\outagedb; -defined('MOODLE_INTERNAL') || die(); - /** * Outage CLI to finish an outage. * diff --git a/classes/cli/waitforit.php b/classes/cli/waitforit.php index 8ec4ed7..9b0d060 100644 --- a/classes/cli/waitforit.php +++ b/classes/cli/waitforit.php @@ -19,8 +19,6 @@ namespace auth_outage\cli; use auth_outage\models\outage; use auth_outage\outagedb; -defined('MOODLE_INTERNAL') || die(); - /** * Outage CLI to wait for an outage to start. * diff --git a/classes/event/outage_created.php b/classes/event/outage_created.php index 0339818..7cc3cce 100644 --- a/classes/event/outage_created.php +++ b/classes/event/outage_created.php @@ -17,8 +17,7 @@ namespace auth_outage\event; use core\event\base; - -defined('MOODLE_INTERNAL') || die(); +use moodle_url; /** * The auth_outage outage created class. @@ -51,9 +50,9 @@ class outage_created extends base { /** * Returns relevant URL, override in subclasses. - * @return \moodle_url + * @return moodle_url */ public function get_url() { - return new \moodle_url('/auth/outage/list.php#auth_outage_id_' . $this->other['id']); + return new moodle_url('/auth/outage/list.php#auth_outage_id_' . $this->other['id']); } } diff --git a/classes/event/outage_deleted.php b/classes/event/outage_deleted.php index e1844a7..4dc3291 100644 --- a/classes/event/outage_deleted.php +++ b/classes/event/outage_deleted.php @@ -18,8 +18,6 @@ namespace auth_outage\event; use core\event\base; -defined('MOODLE_INTERNAL') || die(); - /** * The auth_outage outage deleted class. * diff --git a/classes/event/outage_updated.php b/classes/event/outage_updated.php index 797b680..0710bfe 100644 --- a/classes/event/outage_updated.php +++ b/classes/event/outage_updated.php @@ -18,8 +18,6 @@ namespace auth_outage\event; use core\event\base; -defined('MOODLE_INTERNAL') || die(); - /** * The auth_outage outage updated class. * diff --git a/classes/outagedb.php b/classes/outagedb.php index 4fa6d85..7d43f90 100644 --- a/classes/outagedb.php +++ b/classes/outagedb.php @@ -16,9 +16,7 @@ namespace auth_outage; -if (!defined('MOODLE_INTERNAL')) { - die('Direct access to this script is forbidden.'); // It must be included from a Moodle page. -} +defined('MOODLE_INTERNAL') || die(); require_once($CFG->dirroot . '/calendar/lib.php'); @@ -259,7 +257,7 @@ class outagedb { if (is_null($time)) { $time = time(); } - if (!is_int($time)) { + if (!is_int($time) && ($time <= 0)) { throw new InvalidArgumentException('$time must be an int or null.'); } diff --git a/tests/cli/cli_test.php b/tests/cli/cli_test.php new file mode 100644 index 0000000..bc6f485 --- /dev/null +++ b/tests/cli/cli_test.php @@ -0,0 +1,70 @@ +. + +use auth_outage\cli\cliexception; +use auth_outage\cli\create; + +defined('MOODLE_INTERNAL') || die(); +require_once('cli_testcase.php'); + +/** + * Tests performed on CLI base and exception class. + * + * @package auth_outage + * @author Daniel Thee Roperto + * @copyright Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @covers \auth_outage\cli\clibase + * @covers \auth_outage\cli\cliexception + * @SuppressWarnings("public") + */ +class cli_test extends cli_testcase { + public function test_invalidargumentparam() { + $this->set_parameters(['--aninvalidparameter']); + $this->setExpectedException(cliexception::class); + new create(); + } + + public function test_invalidargumentgiven() { + $this->setExpectedException(cliexception::class); + new create(['anotherinvalidparameter']); + } + + public function test_setreferencetime() { + $cli = new create(['start' => 0]); + $cli->set_referencetime(1); + $cli->set_referencetime(60 * 60 * 24 * 7); + } + + public function test_setreferencetime_invalid() { + $cli = new create(['start' => 0]); + $this->setExpectedException(InvalidArgumentException::class); + $cli->set_referencetime(-1); + } + + public function test_help() { + $this->set_parameters(['-h']); + $cli = new create(); + $output = $this->execute($cli); + self::assertContains('-h', $output); + self::assertContains('--help', $output); + } + + public function test_exception() { + self::setExpectedException(cliexception::class, '*ERROR* An CLI exception.', 5); + throw new cliexception('An CLI exception.', 5); + } +} diff --git a/tests/cli/create_test.php b/tests/cli/create_test.php index 4fb0abc..e764c1d 100644 --- a/tests/cli/create_test.php +++ b/tests/cli/create_test.php @@ -29,7 +29,7 @@ require_once('cli_testcase.php'); * @author Daniel Thee Roperto * @copyright Catalyst IT * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @SuppressWarnings("public") + * @SuppressWarnings("public") Allow this test to have as many tests as necessary. */ class create_test extends cli_testcase { public function test_noarguments() { @@ -38,17 +38,6 @@ class create_test extends cli_testcase { $this->execute($cli); } - public function test_invalidargumentparam() { - $this->set_parameters(['--aninvalidparameter']); - $this->setExpectedException(cliexception::class); - new create(); - } - - public function test_invalidargumentgiven() { - $this->setExpectedException(cliexception::class); - new create(['anotherinvalidparameter']); - } - public function test_invalidparam_notanumber() { $cli = new create(['start' => 'some day']); $cli->set_defaults([ @@ -101,12 +90,6 @@ class create_test extends cli_testcase { $this->execute($cli); } - public function test_setreferencetime_invalid() { - $cli = new create(['start' => 0]); - $this->setExpectedException(InvalidArgumentException::class); - $cli->set_referencetime(-1); - } - public function test_help() { $this->set_parameters(['--help']); $cli = new create(); diff --git a/tests/cli/finish_test.php b/tests/cli/finish_test.php index d2f9095..53a9d5e 100644 --- a/tests/cli/finish_test.php +++ b/tests/cli/finish_test.php @@ -16,6 +16,8 @@ use auth_outage\cli\cliexception; use auth_outage\cli\finish; +use auth_outage\models\outage; +use auth_outage\outagedb; defined('MOODLE_INTERNAL') || die(); require_once('cli_testcase.php'); @@ -27,6 +29,7 @@ require_once('cli_testcase.php'); * @author Daniel Thee Roperto * @copyright Catalyst IT * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @covers \auth_outage\cli\finish */ class finish_test extends cli_testcase { public function test_constructor() { @@ -61,4 +64,61 @@ class finish_test extends cli_testcase { $this->setExpectedException(cliexception::class); $this->execute($cli); } + + public function test_endedoutage() { + $this->setAdminUser(); + $now = time(); + $id = outagedb::save(new outage([ + 'warntime' => $now - 200, + 'starttime' => $now - 100, + 'stoptime' => $now - 50, + 'title' => 'Title', + 'description' => 'Description', + ])); + $this->set_parameters(['-id=' . $id]); + $cli = new finish(); + $cli->set_referencetime($now); + $this->setExpectedException(cliexception::class); + $this->execute($cli); + } + + public function test_finish() { + $this->setAdminUser(); + $now = time(); + $id = outagedb::save(new outage([ + 'warntime' => $now - 200, + 'starttime' => $now - 100, + 'stoptime' => $now + 100, + 'title' => 'Title', + 'description' => 'Description', + ])); + $this->set_parameters(['-id=' . $id]); + $cli = new finish(); + $cli->set_referencetime($now); + $this->execute($cli); + } + + public function test_activenotfound() { + $this->setAdminUser(); + $this->set_parameters(['-a']); + $cli = new finish(); + $this->setExpectedException(cliexception::class); + $this->execute($cli); + } + + public function test_invalidid() { + $this->setAdminUser(); + $this->set_parameters(['-id=theid']); + $cli = new finish(); + $this->setExpectedException(cliexception::class); + $this->execute($cli); + } + + public function test_idnotfound() { + $this->setAdminUser(); + $this->set_parameters(['-id=99999']); + $cli = new finish(); + $this->setExpectedException(cliexception::class); + $this->execute($cli); + } } diff --git a/tests/cli/waitforit_test.php b/tests/cli/waitforit_test.php index 5be7881..cb7f7b3 100644 --- a/tests/cli/waitforit_test.php +++ b/tests/cli/waitforit_test.php @@ -29,6 +29,7 @@ require_once('cli_testcase.php'); * @author Daniel Thee Roperto * @copyright Catalyst IT * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @covers \auth_outage\cli\waitforit * @SuppressWarnings("public") */ class waitforit_test extends cli_testcase { diff --git a/tests/events_test.php b/tests/events_test.php new file mode 100644 index 0000000..6563d06 --- /dev/null +++ b/tests/events_test.php @@ -0,0 +1,115 @@ +. + +use auth_outage\models\outage; +use auth_outage\outagedb; + +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 + * @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() { + global $DB; + $this->setAdminUser(); + $this->resetAfterTest(false); + + // Save new outage. + $now = time(); + $id = outagedb::save(new outage([ + 'warntime' => $now - 60, + 'starttime' => 60, + 'stoptime' => 120, + 'title' => 'Title', + 'description' => 'Description', + ])); + + // Check existance. + $event = $DB->get_record_select( + 'event', + "(eventtype = 'auth_outage' AND instance = :outageid)", + ['outageid' => $id], + 'id', + IGNORE_MISSING + ); + self::assertNotFalse($event); + + // Another test will use it. + return [$id, $event->id]; + } + + /** + * @param array $ids + * @depends test_save + */ + public function test_update($ids) { + global $DB; + + $this->setAdminUser(); + $this->resetAfterTest(false); + + list($idoutage, $idevent) = $ids; + $outage = outagedb::get_by_id($idoutage); + $outage->starttime += 10; + outagedb::save($outage); + + // Should still exist. + $event = $DB->get_record_select( + 'event', + "(eventtype = 'auth_outage' AND instance = :idoutage)", + ['idoutage' => $idoutage], + 'id', + IGNORE_MISSING + ); + self::assertNotFalse($event); + self::assertSame($idevent, $event->id); + + return $ids; + } + + /** + * @param array $ids + * @depends test_update + */ + public function test_delete($ids) { + global $DB; + + $this->setAdminUser(); + $this->resetAfterTest(true); + list($idoutage, $idevent) = $ids; + + outagedb::delete($idoutage); + + // Should not exist. + $event = $DB->get_record_select( + 'event', + "(eventtype = 'auth_outage' AND instance = :idoutage) OR (id = :idevent)", + ['idoutage' => $idoutage, 'idevent' => $idevent], + 'id', + IGNORE_MISSING + ); + self::assertFalse($event); + } +} diff --git a/tests/outage_test.php b/tests/outage_test.php index d0c88d5..aeaec26 100644 --- a/tests/outage_test.php +++ b/tests/outage_test.php @@ -25,6 +25,7 @@ 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\models\outage */ class outage_test extends basic_testcase { public function test_constructor() {