From c7b0cc24100e4c8adf0f827a84e938c6818486c1 Mon Sep 17 00:00:00 2001 From: Daniel Thee Roperto Date: Tue, 4 Oct 2016 14:47:00 +1100 Subject: [PATCH] Issue #36 - Fixed missing or inconsistent PhpDocs. Version is now: 1.0.1 --- auth.php | 17 +++- classes/calendar/calendar.php | 12 ++- classes/dml/outagedb.php | 16 +++- classes/event/outage_created.php | 11 ++- classes/event/outage_deleted.php | 10 ++- classes/event/outage_updated.php | 11 ++- classes/form/baseform.php | 23 +++-- classes/form/outage/delete.php | 11 ++- classes/form/outage/edit.php | 14 ++- classes/form/outage/finish.php | 11 ++- classes/local/cli/cli_exception.php | 13 ++- classes/local/cli/clibase.php | 13 ++- classes/local/cli/create.php | 18 +++- classes/local/cli/finish.php | 11 ++- classes/local/cli/waitforit.php | 11 ++- classes/local/controllers/infopage.php | 14 ++- classes/local/outage.php | 18 +++- classes/local/outagelib.php | 14 ++- classes/output/manage/base_table.php | 14 ++- classes/output/manage/history_table.php | 11 ++- classes/output/manage/planned_table.php | 11 ++- classes/output/renderer.php | 15 ++-- classes/task/update_static_page.php | 12 ++- db/tasks.php | 1 + db/upgrade.php | 3 +- lib.php | 9 ++ local_outage/lang/en/local_outage.php | 2 +- local_outage/lib.php | 8 +- local_outage/version.php | 2 +- renderer.php | 14 +++ tests/behat/behat_auth_outage.php | 48 ++++++++++- tests/phpunit/base_testcase.php | 65 ++++++++++++++ tests/phpunit/calendar/calendar_test.php | 30 ++++++- tests/phpunit/dml/outagedb_test.php | 80 ++++++++++++----- tests/phpunit/event/events_test.php | 24 +++++- tests/phpunit/form/forms_test.php | 62 +++++++++++--- tests/phpunit/local/cli/cli_test.php | 38 ++++++--- tests/phpunit/local/cli/cli_testcase.php | 34 ++++++-- tests/phpunit/local/cli/create_test.php | 65 ++++++++++---- tests/phpunit/local/cli/finish_test.php | 45 +++++++--- tests/phpunit/local/cli/waitforit_test.php | 67 ++++++++++----- .../local/controllers/infopage_test.php | 85 ++++++++++++++++--- tests/phpunit/local/outage_test.php | 52 ++++++++++-- tests/phpunit/local/outagelib_test.php | 43 +++++++++- version.php | 4 +- 45 files changed, 915 insertions(+), 177 deletions(-) create mode 100644 tests/phpunit/base_testcase.php diff --git a/auth.php b/auth.php index be12d90..22cbd58 100644 --- a/auth.php +++ b/auth.php @@ -15,12 +15,16 @@ // along with Moodle. If not, see . /** + * auth_plugin_outage class. + * * This plugin allows for an outage window to be configured * and then optionally allows only a subset of IPs to connect, * it also shows an outage notification to users. * * @package auth_outage * @author Marcus Boon + * @author Daniel Thee Roperto + * @copyright 2016 Catalyst IT * @license http://www.gnu.org/copyleft/gpl.html GNU Public License */ @@ -31,7 +35,17 @@ defined('MOODLE_INTERNAL') || die(); require_once($CFG->libdir.'/authlib.php'); /** - * Class auth_plugin_outage + * auth_plugin_outage class. + * + * This plugin allows for an outage window to be configured + * and then optionally allows only a subset of IPs to connect, + * it also shows an outage notification to users. + * + * @package auth_outage + * @author Marcus Boon + * @author Daniel Thee Roperto + * @copyright 2016 Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU Public License */ class auth_plugin_outage extends auth_plugin_base { /** @@ -42,6 +56,7 @@ class auth_plugin_outage extends auth_plugin_base { } /** + * Always returns false (password wrong or user does not exist). * @param string $username Not used in this plugin. * @param string $password Not used in this plugin. * @return bool False diff --git a/classes/calendar/calendar.php b/classes/calendar/calendar.php index a97e339..2769c30 100644 --- a/classes/calendar/calendar.php +++ b/classes/calendar/calendar.php @@ -14,6 +14,15 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * calendar 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 + */ + namespace auth_outage\calendar; use auth_outage\local\outage; @@ -23,7 +32,7 @@ defined('MOODLE_INTERNAL') || die(); require_once($CFG->dirroot.'/calendar/lib.php'); /** - * Manages outages in the calendar. + * calendar class. * * @package auth_outage * @author Daniel Thee Roperto @@ -33,7 +42,6 @@ require_once($CFG->dirroot.'/calendar/lib.php'); class calendar { /** * Private constructor, use static methods instead. - * @codeCoverageIgnore */ private function __construct() { } diff --git a/classes/dml/outagedb.php b/classes/dml/outagedb.php index cc1716c..f785558 100644 --- a/classes/dml/outagedb.php +++ b/classes/dml/outagedb.php @@ -14,6 +14,18 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * outagedb class. + * + * The DB Context to manipulate Outages. + * It will also commit changes to the calendar as you change outages. + * + * @package auth_outage + * @author Daniel Thee Roperto + * @copyright 2016 Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + namespace auth_outage\dml; use auth_outage\calendar\calendar; @@ -29,6 +41,8 @@ defined('MOODLE_INTERNAL') || die(); require_once($CFG->dirroot.'/calendar/lib.php'); /** + * outagedb class. + * * The DB Context to manipulate Outages. * It will also commit changes to the calendar as you change outages. * @@ -40,7 +54,6 @@ require_once($CFG->dirroot.'/calendar/lib.php'); class outagedb { /** * Private constructor, use static methods instead. - * @codeCoverageIgnore */ private function __construct() { } @@ -63,6 +76,7 @@ class outagedb { } /** + * Gets an outage based on the given id. * @param int $id Outage id to get. * @return outage|null Returns the outage or null if not found. * @throws coding_exception diff --git a/classes/event/outage_created.php b/classes/event/outage_created.php index d83113c..922668d 100644 --- a/classes/event/outage_created.php +++ b/classes/event/outage_created.php @@ -14,6 +14,15 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * Toutage_created 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 + */ + namespace auth_outage\event; use core\event\base; @@ -22,7 +31,7 @@ use moodle_url; defined('MOODLE_INTERNAL') || die(); /** - * The auth_outage outage created class. + * outage_created class. * * @package auth_outage * @author Daniel Thee Roperto diff --git a/classes/event/outage_deleted.php b/classes/event/outage_deleted.php index 5d3f273..2234e62 100644 --- a/classes/event/outage_deleted.php +++ b/classes/event/outage_deleted.php @@ -14,6 +14,14 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * outage_deleted 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 + */ namespace auth_outage\event; use core\event\base; @@ -22,7 +30,7 @@ use moodle_url; defined('MOODLE_INTERNAL') || die(); /** - * The auth_outage outage deleted class. + * outage_deleted class. * * @package auth_outage * @author Daniel Thee Roperto diff --git a/classes/event/outage_updated.php b/classes/event/outage_updated.php index ed176fa..d0739bd 100644 --- a/classes/event/outage_updated.php +++ b/classes/event/outage_updated.php @@ -14,6 +14,15 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * outage_updated 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 + */ + namespace auth_outage\event; use core\event\base; @@ -22,7 +31,7 @@ use moodle_url; defined('MOODLE_INTERNAL') || die(); /** - * The auth_outage outage updated class. + * outage_updated class. * * @package auth_outage * @author Daniel Thee Roperto diff --git a/classes/form/baseform.php b/classes/form/baseform.php index 3021d78..0285e47 100644 --- a/classes/form/baseform.php +++ b/classes/form/baseform.php @@ -14,6 +14,15 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * baseform 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 + */ + namespace auth_outage\form; use moodleform; @@ -23,7 +32,7 @@ defined('MOODLE_INTERNAL') || die(); require_once($CFG->libdir.'/formslib.php'); /** - * Outage base for forms, extends Moodle form to fix an issue in the validation method. + * baseform class. * * @package auth_outage * @author Daniel Thee Roperto @@ -32,16 +41,7 @@ require_once($CFG->libdir.'/formslib.php'); */ 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, + * Validate the form. See MDL-56250. * * @param bool $validateonnosubmit optional, defaults to false. The default behaviour * is NOT to validate the form when a no submit button has been pressed. @@ -49,7 +49,6 @@ abstract class baseform extends moodleform { * * @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. diff --git a/classes/form/outage/delete.php b/classes/form/outage/delete.php index b8bfa22..d93793b 100644 --- a/classes/form/outage/delete.php +++ b/classes/form/outage/delete.php @@ -14,6 +14,15 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * delete 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 + */ + namespace auth_outage\form\outage; defined('MOODLE_INTERNAL') || die(); @@ -23,7 +32,7 @@ require_once($CFG->libdir.'/formslib.php'); defined('MOODLE_INTERNAL') || die(); /** - * Outage delete confirmation form. + * delete class. * * @package auth_outage * @author Daniel Thee Roperto diff --git a/classes/form/outage/edit.php b/classes/form/outage/edit.php index 66d7a29..ddd7658 100644 --- a/classes/form/outage/edit.php +++ b/classes/form/outage/edit.php @@ -14,6 +14,15 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * edit 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 + */ + namespace auth_outage\form\outage; use auth_outage\form\baseform; @@ -25,7 +34,7 @@ defined('MOODLE_INTERNAL') || die(); require_once($CFG->libdir.'/formslib.php'); /** - * Outage form. + * edit class. * * @package auth_outage * @author Daniel Thee Roperto @@ -33,6 +42,9 @@ require_once($CFG->libdir.'/formslib.php'); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class edit extends baseform { + /** + * @var int Maximum number of characters for a title. + */ const TITLE_MAX_CHARS = 100; /** diff --git a/classes/form/outage/finish.php b/classes/form/outage/finish.php index 76d2c4d..4711f45 100644 --- a/classes/form/outage/finish.php +++ b/classes/form/outage/finish.php @@ -14,6 +14,15 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * 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 + */ + namespace auth_outage\form\outage; use moodleform; @@ -23,7 +32,7 @@ defined('MOODLE_INTERNAL') || die(); require_once($CFG->libdir.'/formslib.php'); /** - * Outage finish confirmation form. + * finish class. * * @package auth_outage * @author Daniel Thee Roperto diff --git a/classes/local/cli/cli_exception.php b/classes/local/cli/cli_exception.php index 6a6e38e..c2f1b47 100644 --- a/classes/local/cli/cli_exception.php +++ b/classes/local/cli/cli_exception.php @@ -14,6 +14,15 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * cli_exception 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 + */ + namespace auth_outage\local\cli; use Exception; @@ -21,7 +30,7 @@ use Exception; defined('MOODLE_INTERNAL') || die(); /** - * Exception executing CLI. + * cli_exception class. * * @package auth_outage * @author Daniel Thee Roperto @@ -73,7 +82,7 @@ class cli_exception 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. + * @param Exception $previous Another exception as reference or null. */ public function __construct($message, $code = 1, Exception $previous = null) { parent::__construct('*ERROR* '.$message, $code, $previous = null); diff --git a/classes/local/cli/clibase.php b/classes/local/cli/clibase.php index 6c19ee5..cc4b853 100644 --- a/classes/local/cli/clibase.php +++ b/classes/local/cli/clibase.php @@ -14,6 +14,15 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * clibase 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 + */ + namespace auth_outage\local\cli; use coding_exception; @@ -22,7 +31,7 @@ use core\session\manager; defined('MOODLE_INTERNAL') || die(); /** - * Outage CLI base class. + * clibase class. * * @package auth_outage * @author Daniel Thee Roperto @@ -42,7 +51,7 @@ abstract class clibase { /** * clibase constructor. - * @param mixed[]|null $options The parameters to use or null to read from the command line. + * @param array $options The parameters to use. * @throws cli_exception */ public function __construct(array $options = null) { diff --git a/classes/local/cli/create.php b/classes/local/cli/create.php index bb66ad5..2be46bf 100644 --- a/classes/local/cli/create.php +++ b/classes/local/cli/create.php @@ -14,6 +14,15 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * 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 + */ + namespace auth_outage\local\cli; use auth_outage\dml\outagedb; @@ -23,7 +32,7 @@ use coding_exception; defined('MOODLE_INTERNAL') || die(); /** - * Outage CLI to create outage. + * create class. * * @package auth_outage * @author Daniel Thee Roperto @@ -182,6 +191,10 @@ class create extends clibase { return $id; } + /** + * Sets the defaults to the outage to clone. + * @throws cli_exception + */ private function clone_defaults() { $id = $this->options['clone']; if (!is_number($id) || ($id <= 0)) { @@ -201,7 +214,7 @@ class create extends clibase { /** * Check parameters converting their type as needed. - * @param mixed $options Input options. + * @param array $options Input options. * @return mixed Output options. * @throws cli_exception */ @@ -248,7 +261,6 @@ class create extends clibase { * @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]), diff --git a/classes/local/cli/finish.php b/classes/local/cli/finish.php index d3675d9..e00b2fb 100644 --- a/classes/local/cli/finish.php +++ b/classes/local/cli/finish.php @@ -14,6 +14,15 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * 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 + */ + namespace auth_outage\local\cli; use auth_outage\dml\outagedb; @@ -22,7 +31,7 @@ use auth_outage\local\outage; defined('MOODLE_INTERNAL') || die(); /** - * Outage CLI to finish an outage. + * finish class. * * @package auth_outage * @author Daniel Thee Roperto diff --git a/classes/local/cli/waitforit.php b/classes/local/cli/waitforit.php index 8f2cc2a..0270fc5 100644 --- a/classes/local/cli/waitforit.php +++ b/classes/local/cli/waitforit.php @@ -14,6 +14,15 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * 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 + */ + namespace auth_outage\local\cli; use auth_outage\dml\outagedb; @@ -22,7 +31,7 @@ use auth_outage\local\outage; defined('MOODLE_INTERNAL') || die(); /** - * Outage CLI to wait for an outage to start. + * waitforit class. * * @package auth_outage * @author Daniel Thee Roperto diff --git a/classes/local/controllers/infopage.php b/classes/local/controllers/infopage.php index bc91d97..d7c65cc 100644 --- a/classes/local/controllers/infopage.php +++ b/classes/local/controllers/infopage.php @@ -14,6 +14,15 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * infopage 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 + */ + namespace auth_outage\local\controllers; use auth_outage\dml\outagedb; @@ -29,7 +38,7 @@ use moodle_url; defined('MOODLE_INTERNAL') || die(); /** - * Controller for the info page. + * infopage class. * * @package auth_outage * @author Daniel Thee Roperto @@ -49,7 +58,7 @@ class infopage { /** * infopage_controller constructor. - * @param mixed[]|null $params Parameters to use or null to get from Moodle API (request). + * @param array $params Parameters to use or null to get from Moodle API (request). */ public function __construct(array $params = null) { if (is_null($params)) { @@ -142,6 +151,7 @@ class infopage { } /** + * Gets the default template file to use for static info page. * @return string The default template file to use for static info page. */ public static function get_defaulttemplatefile() { diff --git a/classes/local/outage.php b/classes/local/outage.php index 854ca37..252c36d 100644 --- a/classes/local/outage.php +++ b/classes/local/outage.php @@ -14,6 +14,15 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * 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 + */ + namespace auth_outage\local; use coding_exception; @@ -22,7 +31,7 @@ use stdClass; defined('MOODLE_INTERNAL') || die(); /** - * Outage class with all information about one specific outage. + * outage class. * * @package auth_outage * @author Daniel Thee Roperto @@ -112,7 +121,7 @@ class outage { /** * outage constructor. - * @param stdClass|mixed[]|null The data for the outage. + * @param stdClass|array|null $data The data for the outage. * @throws coding_exception */ public function __construct($data = null) { @@ -248,7 +257,7 @@ class outage { /** * Returns the input string with all placeholders replaced. - * @param $str string Input string. + * @param string $str Input string. * @return string Output string. */ private function replace_placeholders($str) { @@ -267,6 +276,9 @@ class outage { ); } + /** + * Converts the type of the fields as needed. + */ private function adjust_field_types() { // Adjust int fields. $fs = ['createdby', 'id', 'lastmodified', 'modifiedby', 'starttime', 'stoptime', 'warntime', 'finished']; diff --git a/classes/local/outagelib.php b/classes/local/outagelib.php index e372a8b..27670f9 100644 --- a/classes/local/outagelib.php +++ b/classes/local/outagelib.php @@ -14,6 +14,15 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * 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 + */ + namespace auth_outage\local; use auth_outage\dml\outagedb; @@ -26,7 +35,7 @@ use stdClass; defined('MOODLE_INTERNAL') || die(); /** - * Outage related functions. + * outagelib class. * * @package auth_outage * @author Daniel Thee Roperto @@ -34,6 +43,9 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class outagelib { + /** + * @var bool Flags in the page was already injected with the warning bar if needed. + */ private static $injected = false; /** diff --git a/classes/output/manage/base_table.php b/classes/output/manage/base_table.php index 84524c4..b2dee58 100644 --- a/classes/output/manage/base_table.php +++ b/classes/output/manage/base_table.php @@ -14,6 +14,15 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * base_table 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 + */ + namespace auth_outage\output\manage; use auth_outage\local\outage; @@ -26,7 +35,7 @@ defined('MOODLE_INTERNAL') || die(); require_once($CFG->libdir.'/tablelib.php'); /** - * Manage outages table base. + * base_table class. * * @package auth_outage * @author Daniel Thee Roperto @@ -34,6 +43,9 @@ require_once($CFG->libdir.'/tablelib.php'); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class base_table extends flexible_table { + /** + * @var int Autogenerated id. + */ private static $autoid = 0; /** diff --git a/classes/output/manage/history_table.php b/classes/output/manage/history_table.php index 8ec04fc..412139a 100644 --- a/classes/output/manage/history_table.php +++ b/classes/output/manage/history_table.php @@ -14,6 +14,15 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * history_table 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 + */ + namespace auth_outage\output\manage; use auth_outage\local\outage; @@ -23,7 +32,7 @@ defined('MOODLE_INTERNAL') || die(); require_once($CFG->libdir.'/tablelib.php'); /** - * Manage outages table. + * history_table class. * * @package auth_outage * @author Daniel Thee Roperto diff --git a/classes/output/manage/planned_table.php b/classes/output/manage/planned_table.php index 781e2c3..8275b07 100644 --- a/classes/output/manage/planned_table.php +++ b/classes/output/manage/planned_table.php @@ -14,6 +14,15 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * planned_table 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 + */ + namespace auth_outage\output\manage; use auth_outage\local\outage; @@ -25,7 +34,7 @@ defined('MOODLE_INTERNAL') || die(); require_once($CFG->libdir.'/tablelib.php'); /** - * Manage outages table. + * planned_table class. * * @package auth_outage * @author Daniel Thee Roperto diff --git a/classes/output/renderer.php b/classes/output/renderer.php index fb71a96..43cc826 100644 --- a/classes/output/renderer.php +++ b/classes/output/renderer.php @@ -14,11 +14,18 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * auth_outage_renderer 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 + */ + namespace auth_outage\output; use auth_outage\local\outage; -use auth_outage\output\manage\history_table; -use auth_outage\output\manage\planned_table; use coding_exception; use core_user; use html_writer; @@ -28,7 +35,7 @@ use plugin_renderer_base; defined('MOODLE_INTERNAL') || die(); /** - * auth_outage auth_outage_renderer + * auth_outage_renderer class. * * @package auth_outage * @author Daniel Thee Roperto @@ -117,10 +124,8 @@ class renderer extends plugin_renderer_base { * @param bool $preview If in preview mode the warning bar will not check if we are back online. * @return string HTML of the warning bar. * @throws coding_exception - * @SuppressWarnings("unused") because $viewbag is used inside require() */ public function render_warningbar(outage $outage, $time, $static, $preview) { - global $CFG; if (!is_int($time) || ($time <= 0)) { throw new coding_exception('$time is not an positive int or null.', $time); } diff --git a/classes/task/update_static_page.php b/classes/task/update_static_page.php index bcb5cc4..561cb6d 100644 --- a/classes/task/update_static_page.php +++ b/classes/task/update_static_page.php @@ -14,6 +14,15 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * update_static_page 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 + */ + namespace auth_outage\task; use auth_outage\local\controllers\infopage; @@ -22,7 +31,8 @@ use core\task\scheduled_task; defined('MOODLE_INTERNAL') || die(); /** - * Tasks information. + * update_static_page class. + * * @package auth_outage * @author Daniel Thee Roperto * @copyright 2016 Catalyst IT diff --git a/db/tasks.php b/db/tasks.php index dd78933..8f53d79 100644 --- a/db/tasks.php +++ b/db/tasks.php @@ -22,6 +22,7 @@ * @copyright 2016 Catalyst IT * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +defined('MOODLE_INTERNAL') || die(); $tasks = [ [ diff --git a/db/upgrade.php b/db/upgrade.php index c4aabe9..a7ec907 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -25,9 +25,10 @@ defined('MOODLE_INTERNAL') || die(); /** + * Outage plugin upgrade code + * * @param int $oldversion the version we are upgrading from * @return bool result - * @SuppressWarnings("unused") */ function xmldb_auth_outage_upgrade($oldversion) { global $DB; diff --git a/lib.php b/lib.php index d0932dc..21f5e46 100644 --- a/lib.php +++ b/lib.php @@ -27,14 +27,23 @@ use auth_outage\local\outagelib; defined('MOODLE_INTERNAL') || die; +/** + * Used in Moodle 30+ when a user is logged on. + */ function auth_outage_extend_navigation_user_settings() { outagelib::inject(); } +/** + * Used in Moodle 30+ on the frontpage. + */ function auth_outage_extend_navigation_frontpage() { outagelib::inject(); } +/** + * Used in Moodle 31+ when a user is logged on. + */ function auth_outage_extend_navigation_user() { outagelib::inject(); } diff --git a/local_outage/lang/en/local_outage.php b/local_outage/lang/en/local_outage.php index 28a0b0a..5d08538 100644 --- a/local_outage/lang/en/local_outage.php +++ b/local_outage/lang/en/local_outage.php @@ -17,7 +17,7 @@ /** * Strings for component 'local_outage', language 'en'. * - * @package local_outage + * @package auth_outage * @author Daniel Thee Roperto * @copyright 2016 Catalyst IT * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later diff --git a/local_outage/lib.php b/local_outage/lib.php index 9a85e58..7e38ca9 100644 --- a/local_outage/lib.php +++ b/local_outage/lib.php @@ -17,7 +17,7 @@ /** * local_outage plugin lib * - * @package local_outage + * @package auth_outage * @author Daniel Thee Roperto * @copyright 2016 Catalyst IT * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later @@ -27,10 +27,16 @@ use auth_outage\local\outagelib; defined('MOODLE_INTERNAL') || die(); +/** + * Used since Moodle 29. + */ function local_outage_extend_navigation() { outagelib::inject(); } +/** + * Used since Moodle 29. + */ function local_outage_extend_settings_navigation() { outagelib::inject(); } diff --git a/local_outage/version.php b/local_outage/version.php index 196a3ae..71060ee 100644 --- a/local_outage/version.php +++ b/local_outage/version.php @@ -17,7 +17,7 @@ /** * Version information. * - * @package local_outage + * @package auth_outage * @author Daniel Thee Roperto * @copyright 2016 Catalyst IT * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later diff --git a/renderer.php b/renderer.php index 20d7473..7adcad2 100644 --- a/renderer.php +++ b/renderer.php @@ -14,9 +14,23 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * auth_outage_renderer class. + * + * auth_outage auth_outage_renderer should just extend our renderer class in the classes directory. + * This is done to keep code organized and make easier to run tests and check coverage. + * + * @package auth_outage + * @author Daniel Thee Roperto + * @copyright 2016 Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + defined('MOODLE_INTERNAL') || die(); /** + * auth_outage_renderer class. + * * auth_outage auth_outage_renderer should just extend our renderer class in the classes directory. * This is done to keep code organized and make easier to run tests and check coverage. * diff --git a/tests/behat/behat_auth_outage.php b/tests/behat/behat_auth_outage.php index 855cd09..f2b3f57 100644 --- a/tests/behat/behat_auth_outage.php +++ b/tests/behat/behat_auth_outage.php @@ -14,6 +14,15 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * Steps definitions related to auth_outage. + * + * @package auth_outage + * @author Daniel Thee Roperto + * @copyright 2016 Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php. use auth_outage\dml\outagedb; @@ -39,7 +48,9 @@ class behat_auth_outage extends behat_base { private $outage = null; /** + * Checks if a authentication plugin is enabled * @Given the authentication plugin :name is enabled + * @param string $name Name of authentication plugin to check. */ public function the_authentication_plugin_is_enabled($name) { set_config('auth', $name); @@ -48,6 +59,7 @@ class behat_auth_outage extends behat_base { } /** + * Opens the Outage Management page. * @Given /^I am on Outage Management Page$/ */ public function i_am_on_outage_management_page() { @@ -55,6 +67,7 @@ class behat_auth_outage extends behat_base { } /** + * Logs into the system. * @Given /^I am an administrator$/ */ public function i_am_an_administrator() { @@ -70,6 +83,7 @@ class behat_auth_outage extends behat_base { } /** + * Opens the create outage page. * @Given /^I visit the Create Outage Page$/ */ public function i_visit_the_create_outage_page() { @@ -77,7 +91,9 @@ class behat_auth_outage extends behat_base { } /** + * Creates an outage of the given type. * @Given there is a :type outage + * @param string $type Type (stage) of outage to create. */ public function there_is_a_outage($type) { $data = [ @@ -120,11 +136,14 @@ class behat_auth_outage extends behat_base { } /** + * Check if there is an action button with the given name. * @Then I should see the action :action + * @param string $action Action to check. + * @throws ExpectationException */ public function i_should_see_the_action($action) { $expected = ($action == 'Edit') ? 2 : 1; // Edit is an action through the title or button. - $found = $this->can_i_see_action($action); + $found = $this->how_many_times_can_i_see_action($action); if ($found != $expected) { throw new ExpectationException('"'.$action.'" action not found, expected '.$expected. ' but found '.$found.'.', $this->getSession()); @@ -132,15 +151,23 @@ class behat_auth_outage extends behat_base { } /** + * Check if an specific action is not visible. * @Then I should not see the action :action + * @param string $action Action to check. + * @throws ExpectationException */ public function i_should_not_see_the_action($action) { - if ($this->can_i_see_action($action) != 0) { + if ($this->how_many_times_can_i_see_action($action) != 0) { throw new ExpectationException('"'.$action.'" action was found', $this->getSession()); } } - private function can_i_see_action($action) { + /** + * Counts how many times an specific action is visible. + * @param string $action Action to check. + * @return int Number of times it is shown. + */ + private function how_many_times_can_i_see_action($action) { $selector = 'css'; $locator = "div[role='main'] a[title='".$action."']"; $items = $this->getSession()->getPage()->findAll($selector, $locator); @@ -148,7 +175,9 @@ class behat_auth_outage extends behat_base { } /** + * Click the given action button. * @Then I click on the :action action button + * @param string $action Action button to click. */ public function i_click_on_the_action_button($action) { $node = $this->get_selected_node('css_element', "div[role='main'] table nobr a[title='".$action."']"); @@ -157,7 +186,9 @@ class behat_auth_outage extends behat_base { } /** + * Check if a new window was opened. * @Given I should be in a new window + * @throws ExpectationException */ public function i_should_be_in_a_new_window() { $count = count($this->getSession()->getWindowNames()); @@ -167,7 +198,10 @@ class behat_auth_outage extends behat_base { } /** + * Checks if the warning bar is visible. * @Then I should see :text in the warning bar + * @param string $text Text that should be in the warning bar. + * @throws ExpectationException */ public function i_should_see_in_the_warning_bar($text) { $element = '#auth_outage_warningbar_box'; @@ -197,7 +231,9 @@ class behat_auth_outage extends behat_base { } /** + * Checks if the warning bar is not visible. * @Then I should not see the warning bar + * @throws ExpectationException */ public function i_should_not_see_the_warning_bar() { $selector = 'css'; @@ -209,7 +245,10 @@ class behat_auth_outage extends behat_base { } /** + * Creates an outage for the given data. * @Given there is the following outage: + * @param TableNode $data Outage data. + * @throws Exception */ public function there_is_the_following_outage(TableNode $data) { $time = time(); @@ -249,7 +288,10 @@ class behat_auth_outage extends behat_base { } /** + * Waits (sleep) until outage reaches a stage. * @When /^I wait until the outage (?Pwarns|starts|stops)$/ + * @param string $what What to wait for. + * @throws Exception */ public function i_wait_until_outage($what) { switch ($what) { diff --git a/tests/phpunit/base_testcase.php b/tests/phpunit/base_testcase.php new file mode 100644 index 0000000..6934377 --- /dev/null +++ b/tests/phpunit/base_testcase.php @@ -0,0 +1,65 @@ +. + +/** + * Base testcase for auth outage tests. + * + * We added this testcase to handle exceptions. + * Moodle 29 uses PHPUnit 3.7.38 which does not implement the expectException(). + * Moodle 30 uses PHPUnit 4.8.21 which does not implement the expectException(). + * Moodle 31 uses PHPUnit 4.8.27 which does not implement the expectException(). + * Moodle 32 (as of now) uses PHPUnit 5.4.8 which deprecated setExpectedException(). + * In PHPUnit 6 the setExpectedException() will be removed. + * We are not not using the annotation expectException as it is not accepted by Moodle Checker. + * + * @package auth_outage + * @author Daniel Thee Roperto + * @copyright 2016 Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * auth_outage_base_testcase 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 + */ +abstract class auth_outage_base_testcase extends advanced_testcase { + /** + * Checks PHPUnit version and calls the functions accordingly. + * @param string $exception Expected exception class. + * @param string|null $message Expected exception message. + * @param int|null $code Expected exception code. + */ + public function set_expected_exception($exception, $message = null, $code = null) { + global $CFG; + if ($CFG->branch < 32) { + $this->setExpectedException($exception, $message, $code); + } else { + $this->expectException($exception); + if (!is_null($message) && ($message !== '')) { + $this->expectExceptionMessage($message); + } + if (!is_null($code)) { + $this->expectExceptionCode($code); + } + } + } +} diff --git a/tests/phpunit/calendar/calendar_test.php b/tests/phpunit/calendar/calendar_test.php index 1b6e207..efea563 100644 --- a/tests/phpunit/calendar/calendar_test.php +++ b/tests/phpunit/calendar/calendar_test.php @@ -14,13 +14,22 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * calendar_test test class. + * + * @package auth_outage + * @author Daniel Thee Roperto + * @copyright Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + use auth_outage\calendar\calendar; use auth_outage\local\outage; defined('MOODLE_INTERNAL') || die(); /** - * Tests performed on outage class. + * calendar_test test class. * * @package auth_outage * @author Daniel Thee Roperto @@ -28,6 +37,10 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class calendar_test extends advanced_testcase { + /** + * Creates an outage and checks if its in the calendar. + * @return outage Created outage. + */ public function test_create() { self::setAdminUser(); $this->resetAfterTest(false); @@ -49,7 +62,10 @@ class calendar_test extends advanced_testcase { } /** + * Updates an outage and checks the calendar. * @depends test_create + * @param outage $outage Outage to be updated. + * @return outage Updated outage. */ public function test_update(outage $outage) { self::setAdminUser(); @@ -63,7 +79,9 @@ class calendar_test extends advanced_testcase { } /** + * Deletes an outage and checks the calendar. * @depends test_update + * @param outage $outage Outage to delete. */ public function test_delete($outage) { self::setAdminUser(); @@ -73,6 +91,9 @@ class calendar_test extends advanced_testcase { self::assertNull(calendar::load($outage->id)); } + /** + * Try to update a non existing outage. + */ public function test_update_notfound() { self::setAdminUser(); $this->resetAfterTest(true); @@ -93,6 +114,9 @@ class calendar_test extends advanced_testcase { phpunit_util::reset_debugging(); } + /** + * Try to delete a non existing outage. + */ public function test_delete_notfound() { self::setAdminUser(); $this->resetAfterTest(true); @@ -101,6 +125,10 @@ class calendar_test extends advanced_testcase { phpunit_util::reset_debugging(); } + /** + * Check if there is a calendar entry for the given outage. + * @param outage $outage Outage to check. + */ private function check_calendar(outage $outage) { $calendar = calendar::load($outage->id); self::assertSame($outage->title, $calendar->name); diff --git a/tests/phpunit/dml/outagedb_test.php b/tests/phpunit/dml/outagedb_test.php index 00d84b9..77cc6f8 100644 --- a/tests/phpunit/dml/outagedb_test.php +++ b/tests/phpunit/dml/outagedb_test.php @@ -14,13 +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; - -defined('MOODLE_INTERNAL') || die(); - /** - * Tests performed on outage class. + * outagedb_test tests class. * * @package auth_outage * @author Daniel Thee Roperto @@ -28,7 +23,23 @@ defined('MOODLE_INTERNAL') || die(); * @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 { + +use auth_outage\dml\outagedb; +use auth_outage\local\outage; + +defined('MOODLE_INTERNAL') || die(); +require_once(__DIR__.'/../base_testcase.php'); + +/** + * outagedb_test tests 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 outagedb_test extends auth_outage_base_testcase { /** * Creates an array of ids in from the given outages array. * @param outage[] $outages An array of outages. @@ -87,6 +98,9 @@ class outagedb_test extends advanced_testcase { outagedb::save($outage); } + /** + * Create a few outages, fetch them and check if fields match. + */ public function test_saved_fields() { $this->resetAfterTest(true); for ($i = 0; $i < 4; $i++) { @@ -211,6 +225,9 @@ class outagedb_test extends advanced_testcase { } } + /** + * Tests the outagedb::get_active() method. + */ public function test_getactive() { $this->resetAfterTest(true); @@ -247,6 +264,9 @@ class outagedb_test extends advanced_testcase { self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.'); } + /** + * Tests the outagedb::get_all_unended() method. + */ public function test_getallunended() { $this->resetAfterTest(true); @@ -295,6 +315,9 @@ class outagedb_test extends advanced_testcase { self::createidarray(outagedb::get_all_unended($now)), 'Wrong future data.'); } + /** + * Tests the outagedb::get_all_ended() method. + */ public function test_getallended() { $this->resetAfterTest(true); @@ -331,63 +354,78 @@ class outagedb_test extends advanced_testcase { } /** - * @expectedException coding_exception + * Tests the outagedb::get_by_id() with an invalid parameter. */ public function test_getbyid_invalid() { $this->resetAfterTest(true); + $this->set_expected_exception(coding_exception::class); outagedb::get_by_id(-1); } /** - * @expectedException coding_exception + * Tests the outagedb::delete() with an invalid parameter. */ public function test_delete_invalid() { $this->resetAfterTest(true); + $this->set_expected_exception(coding_exception::class); outagedb::delete(-1); } /** - * @expectedException coding_exception + * Tests the outagedb::get_active() with an invalid parameter. */ public function test_getactive_invalid() { $this->resetAfterTest(true); + $this->set_expected_exception(coding_exception::class); outagedb::get_active(-1); } /** - * @expectedException coding_exception + * Tests the outagedb::get_all_unended() with an invalid parameter. */ public function test_getallunended_invalid() { $this->resetAfterTest(true); + $this->set_expected_exception(coding_exception::class); outagedb::get_all_unended(-1); } + /** + * Checks we can execute outagedb::get_all_unended() without parameters (now). + */ public function test_getallunended_now() { $this->resetAfterTest(true); - outagedb::get_all_unended(); + self::assertEmpty(outagedb::get_all_unended()); } /** - * @expectedException coding_exception + * Tests the outagedb::get_all_ended() with an invalid parameter. */ public function test_getallended_invalid() { $this->resetAfterTest(true); + $this->set_expected_exception(coding_exception::class); outagedb::get_all_ended(-1); } + /** + * Checks we can execute outagedb::test_getallended_now() without parameters (now). + */ public function test_getallended_now() { $this->resetAfterTest(true); - outagedb::get_all_ended(); + self::assertEmpty(outagedb::get_all_ended()); } /** - * @expectedException coding_exception + * Tests the outagedb::finish() with an invalid parameter. */ public function test_finish_invalid() { $this->resetAfterTest(true); + $this->set_expected_exception(coding_exception::class); outagedb::finish(1, -1); } + /** + * Tests the outagedb::finish() with a non existing outage. + */ public function test_finish_now_notfound() { $this->resetAfterTest(true); outagedb::finish(1); @@ -395,6 +433,9 @@ class outagedb_test extends advanced_testcase { phpunit_util::reset_debugging(); } + /** + * Try to finish not ongoing outages. + */ public function test_finish_notongoing() { $this->resetAfterTest(true); $time = time(); @@ -415,25 +456,26 @@ class outagedb_test extends advanced_testcase { } /** - * @expectedException coding_exception + * Tests the outagedb::get_next_starting() with an invalid parameter. */ public function test_getnextstartinginvalid() { $this->resetAfterTest(true); + $this->set_expected_exception(coding_exception::class); outagedb::get_next_starting(-1); } /** - * @expectedException coding_exception + * Tests the outagedb::get_next_autostarting() with an invalid parameter. */ public function test_getnextautostartinginvalid() { $this->resetAfterTest(true); + $this->set_expected_exception(coding_exception::class); outagedb::get_next_autostarting(-1); } /** * Helper function to create an outage for tests. - * - * @param $i int Used to populate the information. + * @param int $i Used to populate the information. * @return outage The created outage. */ private function createoutage($i) { diff --git a/tests/phpunit/event/events_test.php b/tests/phpunit/event/events_test.php index 278d81d..2591a75 100644 --- a/tests/phpunit/event/events_test.php +++ b/tests/phpunit/event/events_test.php @@ -14,13 +14,22 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * events_test tests class. + * + * @package auth_outage + * @author Daniel Thee Roperto + * @copyright Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + use auth_outage\dml\outagedb; use auth_outage\local\outage; defined('MOODLE_INTERNAL') || die(); /** - * Tests performed on outage class. + * events_test tests class. * * @package auth_outage * @author Daniel Thee Roperto @@ -28,6 +37,10 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class events_test extends advanced_testcase { + /** + * Saves an outage and check if the event was created. + * @return array With the outage id and the event id. + */ public function test_save() { global $DB; self::setAdminUser(); @@ -59,10 +72,12 @@ class events_test extends advanced_testcase { } /** - * @param int[] $ids + * Updates an outage and checks if the event was updated. + * @param int[] $ids Outage id and event id. + * @return int[] same as input parameter. * @depends test_save */ - public function test_update($ids) { + public function test_update(array $ids) { global $DB; self::setAdminUser(); @@ -88,7 +103,8 @@ class events_test extends advanced_testcase { } /** - * @param int[] $ids + * Deletes an outage and checks if the event was deleted. + * @param int[] $ids Outage id and event id. * @depends test_update */ public function test_delete($ids) { diff --git a/tests/phpunit/form/forms_test.php b/tests/phpunit/form/forms_test.php index f86e6e9..0f1b8dc 100644 --- a/tests/phpunit/form/forms_test.php +++ b/tests/phpunit/form/forms_test.php @@ -14,30 +14,49 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -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. + * forms_test test 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 forms_test extends advanced_testcase { + +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(); +require_once(__DIR__.'/../base_testcase.php'); + +/** + * forms_test test 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 forms_test extends auth_outage_base_testcase { + /** + * Create a delete form. + */ public function test_delete() { new delete(); } + /** + * Create a finish form. + */ public function test_finish() { new finish(); } + /** + * Mock some data and check values. + */ public function test_edit_valid() { $this->mock_edit_post(); $edit = new edit(); @@ -52,6 +71,9 @@ class forms_test extends advanced_testcase { self::assertSame('The description.', $outage->description); } + /** + * Check invalid warning duration. + */ public function test_edit_invalid_warning() { $this->mock_edit_post(); $_POST['warningduration'] = ['number' => '-1', 'timeunit' => '60']; @@ -60,6 +82,9 @@ class forms_test extends advanced_testcase { self::assertNull($outage); } + /** + * Check invalid outage duration. + */ public function test_edit_invalid_duration() { $this->mock_edit_post(); $_POST['outageduration'] = ['number' => '-2', 'timeunit' => '3600']; @@ -67,6 +92,9 @@ class forms_test extends advanced_testcase { self::assertNull($edit->get_data()); } + /** + * Check invalid title (empty). + */ public function test_edit_invalid_title() { $this->mock_edit_post(); $_POST['title'] = ''; @@ -74,6 +102,9 @@ class forms_test extends advanced_testcase { self::assertNull($edit->get_data()); } + /** + * Check invalid title (too long). + */ 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. '. @@ -83,6 +114,9 @@ class forms_test extends advanced_testcase { self::assertNull($edit->get_data()); } + /** + * Check invalid format for description. + */ public function test_edit_description_invalid_format() { $this->mock_edit_post(); $_POST['description'] = ['text' => 'The description.', 'format' => '2']; @@ -92,6 +126,9 @@ class forms_test extends advanced_testcase { phpunit_util::reset_debugging(); } + /** + * Check if set data works properly. + */ public function test_setdata() { $outage = new outage([ 'autostart' => false, @@ -106,15 +143,18 @@ class forms_test extends advanced_testcase { } /** - * @expectedException coding_exception + * Check edit::set_data() with invalid parameter. */ public function test_setdata_invalid() { $edit = new edit(); + $this->set_expected_exception(coding_exception::class); $edit->set_data(null); } + /** + * Mock a post, see MDL-56233. + */ private function mock_edit_post() { - // There is a bug in moodleform::mock_submit so we make our own version. $_POST = [ 'id' => '1', 'sesskey' => sesskey(), diff --git a/tests/phpunit/local/cli/cli_test.php b/tests/phpunit/local/cli/cli_test.php index 6795eb0..fd898b4 100644 --- a/tests/phpunit/local/cli/cli_test.php +++ b/tests/phpunit/local/cli/cli_test.php @@ -14,6 +14,15 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * cli_test test 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 + */ + use auth_outage\local\cli\cli_exception; use auth_outage\local\cli\create; @@ -21,7 +30,7 @@ defined('MOODLE_INTERNAL') || die(); require_once(__DIR__.'/cli_testcase.php'); /** - * Tests performed on CLI base and exception class. + * cli_test test class. * * @package auth_outage * @author Daniel Thee Roperto @@ -29,24 +38,27 @@ require_once(__DIR__.'/cli_testcase.php'); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @SuppressWarnings("public") */ -class cli_test extends cli_testcase { +class cli_test extends auth_outage_cli_testcase { /** - * @expectedException auth_outage\local\cli\cli_exception - * @expectedExceptionCode 2 + * Tests providing an unknown parameter. */ public function test_invalidargumentparam() { $this->set_parameters(['--aninvalidparameter']); + $this->set_expected_cli_exception(cli_exception::ERROR_PARAMETER_UNKNOWN); new create(); } /** - * @expectedException auth_outage\local\cli\cli_exception - * @expectedExceptionCode 2 + * Tests providing another unknow parameter (without --). */ public function test_invalidargumentgiven() { + $this->set_expected_cli_exception(cli_exception::ERROR_PARAMETER_UNKNOWN); new create(['anotherinvalidparameter']); } + /** + * Tests setting reference times. + */ public function test_setreferencetime() { $cli = new create(['start' => 0]); $cli->set_referencetime(1); @@ -54,14 +66,18 @@ class cli_test extends cli_testcase { } /** - * @expectedException coding_exception + * Tests setting an invalid reference time. */ public function test_setreferencetime_invalid() { $this->set_parameters(['--start=60']); $cli = new create(); + $this->set_expected_exception(coding_exception::class); $cli->set_referencetime(-1); } + /** + * Tests the help. + */ public function test_help() { $this->set_parameters(['-h']); $cli = new create(); @@ -71,16 +87,15 @@ class cli_test extends cli_testcase { } /** - * @expectedException auth_outage\local\cli\cli_exception - * @expectedExceptionCode 1 + * Tests the cli_exception. */ public function test_exception() { + $this->set_expected_cli_exception(cli_exception::ERROR_UNDEFINED); throw new cli_exception('An CLI exception.'); } /** - * @expectedException auth_outage\local\cli\cli_exception - * @expectedExceptionCode 8 + * Tests with the auth_outage plugin disabled. */ public function test_authdisabled() { // Disable all auth plugins. @@ -88,6 +103,7 @@ class cli_test extends cli_testcase { \core\session\manager::gc(); // Remove stale sessions. core_plugin_manager::reset_caches(); // Try to create an CLI object. + $this->set_expected_cli_exception(cli_exception::ERROR_PLUGIN_DISABLED); new create(); } } diff --git a/tests/phpunit/local/cli/cli_testcase.php b/tests/phpunit/local/cli/cli_testcase.php index fe2a095..5c71cd8 100644 --- a/tests/phpunit/local/cli/cli_testcase.php +++ b/tests/phpunit/local/cli/cli_testcase.php @@ -14,19 +14,33 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -use auth_outage\local\cli\clibase; - -defined('MOODLE_INTERNAL') || die(); - /** - * Tests performed on CLIs. + * auth_outage_cli_testcase 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 cli_testcase extends advanced_testcase { + +use auth_outage\local\cli\cli_exception; +use auth_outage\local\cli\clibase; + +defined('MOODLE_INTERNAL') || die(); +require_once(__DIR__.'/../../base_testcase.php'); + +/** + * auth_outage_cli_testcase 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 + */ +abstract class auth_outage_cli_testcase extends auth_outage_base_testcase { + /** + * Always enable the auth outage plugin, resets after test and set no parameters. + */ public function setUp() { // Enable auth plugins. set_config('auth', 'outage'); @@ -63,4 +77,12 @@ class cli_testcase extends advanced_testcase { ob_end_clean(); } } + + /** + * Sets the expected exception as cli_exception with the given error code. + * @param int $errorcode Error code. + */ + protected function set_expected_cli_exception($errorcode) { + $this->set_expected_exception(cli_exception::class, null, $errorcode); + } } diff --git a/tests/phpunit/local/cli/create_test.php b/tests/phpunit/local/cli/create_test.php index 6f5a185..b6f7ee5 100644 --- a/tests/phpunit/local/cli/create_test.php +++ b/tests/phpunit/local/cli/create_test.php @@ -14,6 +14,15 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * create_test test 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 + */ + use auth_outage\dml\outagedb; use auth_outage\local\cli\cli_exception; use auth_outage\local\cli\create; @@ -23,7 +32,7 @@ defined('MOODLE_INTERNAL') || die(); require_once(__DIR__.'/cli_testcase.php'); /** - * Tests performed on CLI create class. + * create_test test class. * * @package auth_outage * @author Daniel Thee Roperto @@ -31,19 +40,18 @@ require_once(__DIR__.'/cli_testcase.php'); * @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 { +class create_test extends auth_outage_cli_testcase { /** - * @expectedException auth_outage\local\cli\cli_exception - * @expectedExceptionCode 4 + * Tests without any arguments. */ public function test_noarguments() { $cli = new create(); + $this->set_expected_cli_exception(cli_exception::ERROR_PARAMETER_MISSING); $this->execute($cli); } /** - * @expectedException auth_outage\local\cli\cli_exception - * @expectedExceptionCode 3 + * Tests when the start time is not a valid number. */ public function test_invalidparam_notanumber() { $cli = new create(['start' => 'some day']); @@ -54,12 +62,12 @@ class create_test extends cli_testcase { 'title' => 'Default Title', 'description' => 'Default Description', ]); + $this->set_expected_cli_exception(cli_exception::ERROR_PARAMETER_INVALID); $this->execute($cli); } /** - * @expectedException auth_outage\local\cli\cli_exception - * @expectedExceptionCode 3 + * Tests when providing a negative start time. */ public function test_invalidparam_negative() { $cli = new create(['start' => -1]); @@ -70,12 +78,12 @@ class create_test extends cli_testcase { 'title' => 'Default Title', 'description' => 'Default Description', ]); + $this->set_expected_cli_exception(cli_exception::ERROR_PARAMETER_INVALID); $this->execute($cli); } /** - * @expectedException auth_outage\local\cli\cli_exception - * @expectedExceptionCode 3 + * Tests providing an empty title. */ public function test_invalidparam_emptystring() { $cli = new create(['start' => 0, 'title' => '']); @@ -86,12 +94,12 @@ class create_test extends cli_testcase { 'title' => 'Default Title', 'description' => 'Default Description', ]); + $this->set_expected_cli_exception(cli_exception::ERROR_PARAMETER_INVALID); $this->execute($cli); } /** - * @expectedException auth_outage\local\cli\cli_exception - * @expectedExceptionCode 3 + * Tests if not providing the title (it will be send as true). */ public function test_invalidparam_notastring() { $cli = new create(['start' => 0, 'title' => true]); @@ -102,9 +110,13 @@ class create_test extends cli_testcase { 'title' => 'Default Title', 'description' => 'Default Description', ]); + $this->set_expected_cli_exception(cli_exception::ERROR_PARAMETER_INVALID); $this->execute($cli); } + /** + * Tests the help. + */ public function test_help() { $this->set_parameters(['--help']); $cli = new create(); @@ -113,6 +125,9 @@ class create_test extends cli_testcase { self::assertContains('--help', $output); } + /** + * Tests the options and shortcuts. + */ public function test_options() { $cli = new create(); @@ -127,6 +142,9 @@ class create_test extends cli_testcase { } } + /** + * Tests creating with all given options. + */ public function test_create_withoptions() { $this->set_parameters([ '--autostart=true', @@ -153,6 +171,9 @@ class create_test extends cli_testcase { self::assertSame('A Description', $outage->description); } + /** + * Tests creating with the onlyid parameter. + */ public function test_create_onlyid() { $this->set_parameters([ '--onlyid', @@ -181,6 +202,9 @@ class create_test extends cli_testcase { self::assertSame('Description', $outage->description); } + /** + * Tests creating using some default values. + */ public function test_create_withdefaults() { $this->set_parameters([ '--warn=100', @@ -211,6 +235,9 @@ class create_test extends cli_testcase { self::assertSame('Default Description', $outage->description); } + /** + * Tests creating with clone. + */ public function test_create_withclone() { self::setAdminUser(); $now = time(); @@ -243,8 +270,7 @@ class create_test extends cli_testcase { } /** - * @expectedException auth_outage\local\cli\cli_exception - * @expectedExceptionCode 3 + * Tests creating with an invalid clone id. */ public function test_create_withclone_invalid() { $this->set_parameters([ @@ -252,9 +278,13 @@ class create_test extends cli_testcase { '--clone=-1', ]); $cli = new create(); + $this->set_expected_cli_exception(cli_exception::ERROR_PARAMETER_INVALID); $this->execute($cli); } + /** + * Tests creating with the block flag. + */ public function test_create_withblock() { // Not an extensive test in the blocking API, cliwaitforit tests should cover them deeper. $this->set_parameters([ @@ -275,16 +305,16 @@ class create_test extends cli_testcase { } /** - * @expectedException coding_exception + * Tests providing an invalid option as default. */ public function test_setdefaults_extra() { $cli = new create([]); + $this->set_expected_exception(coding_exception::class); $cli->set_defaults(['aninvalidparameter' => 'value']); } /** - * @expectedException auth_outage\local\cli\cli_exception - * @expectedExceptionCode 3 + * Tests with an invalud autostart bool value. */ public function test_invalid_bool() { $this->set_parameters([ @@ -296,6 +326,7 @@ class create_test extends cli_testcase { '--description=Description', ]); $cli = new create(); + $this->set_expected_cli_exception(cli_exception::ERROR_PARAMETER_INVALID); $cli->execute(); } } diff --git a/tests/phpunit/local/cli/finish_test.php b/tests/phpunit/local/cli/finish_test.php index b787947..ee97767 100644 --- a/tests/phpunit/local/cli/finish_test.php +++ b/tests/phpunit/local/cli/finish_test.php @@ -14,6 +14,15 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * finish_test test 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 + */ + use auth_outage\dml\outagedb; use auth_outage\local\cli\cli_exception; use auth_outage\local\cli\finish; @@ -23,19 +32,25 @@ defined('MOODLE_INTERNAL') || die(); require_once(__DIR__.'/cli_testcase.php'); /** - * Tests performed on CLI finish class. + * finish_test test 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 finish_test extends cli_testcase { +class finish_test extends auth_outage_cli_testcase { + /** + * Tests the constructor. + */ public function test_constructor() { $cli = new finish(); self::assertNotNull($cli); } + /** + * Tests the generated options and shortcuts. + */ public function test_options() { $cli = new finish(); @@ -50,6 +65,9 @@ class finish_test extends cli_testcase { } } + /** + * Tests the help. + */ public function test_help() { $this->set_parameters(['--help']); $cli = new finish(); @@ -59,17 +77,16 @@ class finish_test extends cli_testcase { } /** - * @expectedException auth_outage\local\cli\cli_exception - * @expectedExceptionCode 4 + * Tests if running without arguments. */ public function test_noarguments() { $cli = new finish(); + $this->set_expected_cli_exception(cli_exception::ERROR_PARAMETER_MISSING); $this->execute($cli); } /** - * @expectedException auth_outage\local\cli\cli_exception - * @expectedExceptionCode 5 + * Tests finishing an already ended outage. */ public function test_endedoutage() { self::setAdminUser(); @@ -85,9 +102,13 @@ class finish_test extends cli_testcase { $this->set_parameters(['-id='.$id]); $cli = new finish(); $cli->set_referencetime($now); + $this->set_expected_cli_exception(cli_exception::ERROR_OUTAGE_INVALID); $this->execute($cli); } + /** + * Tests finishing an outage. + */ public function test_finish() { self::setAdminUser(); $now = time(); @@ -106,35 +127,35 @@ class finish_test extends cli_testcase { } /** - * @expectedException auth_outage\local\cli\cli_exception - * @expectedExceptionCode 6 + * Tests finishing an active outage when it does not exists. */ public function test_activenotfound() { self::setAdminUser(); $this->set_parameters(['-a']); $cli = new finish(); + $this->set_expected_cli_exception(cli_exception::ERROR_OUTAGE_NOT_FOUND); $this->execute($cli); } /** - * @expectedException auth_outage\local\cli\cli_exception - * @expectedExceptionCode 3 + * Tests providing an invalid outage id. */ public function test_invalidid() { self::setAdminUser(); $this->set_parameters(['-id=theid']); $cli = new finish(); + $this->set_expected_cli_exception(cli_exception::ERROR_PARAMETER_INVALID); $this->execute($cli); } /** - * @expectedException auth_outage\local\cli\cli_exception - * @expectedExceptionCode 6 + * Tests when the outage is not found. */ public function test_idnotfound() { self::setAdminUser(); $this->set_parameters(['-id=99999']); $cli = new finish(); + $this->set_expected_cli_exception(cli_exception::ERROR_OUTAGE_NOT_FOUND); $this->execute($cli); } } diff --git a/tests/phpunit/local/cli/waitforit_test.php b/tests/phpunit/local/cli/waitforit_test.php index a483a35..acb3325 100644 --- a/tests/phpunit/local/cli/waitforit_test.php +++ b/tests/phpunit/local/cli/waitforit_test.php @@ -14,15 +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\cli\waitforit; -use auth_outage\local\outage; - -defined('MOODLE_INTERNAL') || die(); -require_once(__DIR__.'/cli_testcase.php'); - /** - * Tests performed on CLI waitforit class. + * waitforit_test test class. * * @package auth_outage * @author Daniel Thee Roperto @@ -30,12 +23,36 @@ require_once(__DIR__.'/cli_testcase.php'); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @SuppressWarnings("public") */ -class waitforit_test extends cli_testcase { + +use auth_outage\dml\outagedb; +use auth_outage\local\cli\cli_exception; +use auth_outage\local\cli\waitforit; +use auth_outage\local\outage; + +defined('MOODLE_INTERNAL') || die(); +require_once(__DIR__.'/cli_testcase.php'); + +/** + * waitforit_test test 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") + */ +class waitforit_test extends auth_outage_cli_testcase { + /** + * Tests the constructor. + */ public function test_constructor() { $cli = new waitforit(); self::assertNotNull($cli); } + /** + * Tests the generated options. + */ public function test_generateoptions() { $cli = new waitforit(); $options = $cli->generate_options(); @@ -44,6 +61,9 @@ class waitforit_test extends cli_testcase { } } + /** + * Tests the generated shortcut options. + */ public function test_generateshortcuts() { $cli = new waitforit(); $options = $cli->generate_options(); @@ -53,6 +73,9 @@ class waitforit_test extends cli_testcase { } } + /** + * Tests if help works. + */ public function test_help() { $this->set_parameters(['--help']); $cli = new waitforit(); @@ -62,38 +85,37 @@ class waitforit_test extends cli_testcase { } /** - * @expectedException auth_outage\local\cli\cli_exception - * @expectedExceptionCode 3 + * Checks if providing an outageid and active parameter. */ public function test_bothparams() { $this->set_parameters(['--outageid=1', '--active']); $cli = new waitforit(); + $this->set_expected_cli_exception(cli_exception::ERROR_PARAMETER_INVALID); $cli->execute(); } /** - * @expectedException auth_outage\local\cli\cli_exception - * @expectedExceptionCode 3 + * Tests with an invalid outage id */ public function test_invalidoutageid() { $this->set_parameters(['-id=-1']); $cli = new waitforit(); + $this->set_expected_cli_exception(cli_exception::ERROR_PARAMETER_INVALID); $this->execute($cli); } /** - * @expectedException auth_outage\local\cli\cli_exception - * @expectedExceptionCode 6 + * Tests with an active outage when it does not exists. */ public function test_outagenotfound() { $this->set_parameters(['-a']); $cli = new waitforit(); + $this->set_expected_cli_exception(cli_exception::ERROR_OUTAGE_NOT_FOUND); $this->execute($cli); } /** - * @expectedException auth_outage\local\cli\cli_exception - * @expectedExceptionCode 5 + * Tests with an outage that already ended. */ public function test_endedoutage() { self::setAdminUser(); @@ -109,9 +131,13 @@ class waitforit_test extends cli_testcase { $this->set_parameters(['-id='.$id]); $cli = new waitforit(); $cli->set_referencetime($now); + $this->set_expected_cli_exception(cli_exception::ERROR_OUTAGE_INVALID); $this->execute($cli); } + /** + * Tests waiting for an existing active outage, verbose mode. + */ public function test_activeverbose() { self::setAdminUser(); $now = time(); @@ -132,6 +158,9 @@ class waitforit_test extends cli_testcase { self::assertContains('started', $output); } + /** + * Tests the countdown. + */ public function test_countdown() { self::setAdminUser(); $now = time(); @@ -159,8 +188,7 @@ class waitforit_test extends cli_testcase { } /** - * @expectedException auth_outage\local\cli\cli_exception - * @expectedExceptionCode 7 + * Tests if the outage changed while waiting. */ public function test_outagechanged() { self::setAdminUser(); @@ -184,6 +212,7 @@ class waitforit_test extends cli_testcase { // Pretend it is time to start, but it should get an error instead. return $outage->starttime; }); + $this->set_expected_cli_exception(cli_exception::ERROR_OUTAGE_CHANGED); $this->execute($cli); } } diff --git a/tests/phpunit/local/controllers/infopage_test.php b/tests/phpunit/local/controllers/infopage_test.php index 5fd2ab2..fc3c591 100644 --- a/tests/phpunit/local/controllers/infopage_test.php +++ b/tests/phpunit/local/controllers/infopage_test.php @@ -14,12 +14,22 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * 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 + */ + use auth_outage\dml\outagedb; use auth_outage\local\controllers\infopage; use auth_outage\local\outage; use auth_outage\task\update_static_page; defined('MOODLE_INTERNAL') || die(); +require_once(__DIR__.'/../../base_testcase.php'); /** * Tests performed on infopage controller class and update_static_page task class. @@ -30,7 +40,10 @@ defined('MOODLE_INTERNAL') || die(); * @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 { +class infopagecontroller_test extends auth_outage_base_testcase { + /** + * Ensures the template file does not exist when starting a test. + */ public function setUp() { $file = infopage::get_defaulttemplatefile(); if (file_exists($file)) { @@ -42,17 +55,23 @@ class infopagecontroller_test extends advanced_testcase { } } + /** + * Tests the constructor. + */ public function test_constructor() { new infopage(); } + /** + * Tests the constructor with given parameters. + */ public function test_constructor_withparams() { $_GET = ['id' => 1, 'static' => 'true']; new infopage(); } /** - * @expectedException coding_exception + * Tests the constructor with different id and outage id. */ public function test_constructor_idmismatch() { $outage = new outage([ @@ -64,16 +83,21 @@ class infopagecontroller_test extends advanced_testcase { 'title' => 'Title', 'description' => 'Description', ]); + $this->set_expected_exception(coding_exception::class); new infopage(['id' => 2, 'outage' => $outage]); } /** - * @expectedException coding_exception + * Tests the constructor with an invalid outage. */ public function test_constructor_invalidoutage() { + $this->set_expected_exception(coding_exception::class); new infopage(['outage' => 'My outage']); } + /** + * Tests the static page contents. + */ public function test_staticpage_output() { global $PAGE; $this->resetAfterTest(true); @@ -104,6 +128,9 @@ class infopagecontroller_test extends advanced_testcase { self::assertSame($outage->id, infopage::find_outageid_from_infopage($html)); } + /** + * Tests the static page file contents. + */ public function test_staticpage_file() { $now = time(); $outage = new outage([ @@ -124,6 +151,9 @@ class infopagecontroller_test extends advanced_testcase { unlink($file); } + /** + * Checks if the default template file is a valid string with the name templage. + */ public function test_getdefaulttemplatefile() { $file = infopage::get_defaulttemplatefile(); self::assertTrue(is_string($file)); @@ -131,12 +161,16 @@ class infopagecontroller_test extends advanced_testcase { } /** - * @expectedException coding_exception + * Tests infopage::find_outageid_from_infopage() with an invalid parameter. */ public function test_findoutageid_notstring() { + $this->set_expected_exception(coding_exception::class); infopage::find_outageid_from_infopage(new stdClass()); } + /** + * Tests infopage::find_outageid_from_infopage() when the id is not found. + */ public function test_findoutageid_notfound() { self::assertNull( infopage::find_outageid_from_infopage( @@ -146,14 +180,15 @@ class infopagecontroller_test extends advanced_testcase { } /** - * @expectedException coding_exception + * Tests infopage::save_static_page() with an invalid parameter. */ public function test_savestaticpage_filenotstring() { + $this->set_expected_exception(coding_exception::class); infopage::save_static_page(new outage(), 1); } /** - * @expectedException file_exception + * Tests infopage::save_static_page() with an invalid filename. */ public function test_savestaticpage_fileinvalid() { global $CFG; @@ -165,30 +200,38 @@ class infopagecontroller_test extends advanced_testcase { 'title' => 'Title', 'description' => 'Description', ]); + + $this->set_expected_exception(file_exception::class); infopage::save_static_page($outage, $CFG->dataroot.'/someinvalidpath/someinvalidfile'); } /** - * @expectedException invalid_state_exception + * Tests infopage::test_sanity_notstring() with invalid contents: an int. */ public function test_sanity_notstring() { + $this->set_expected_exception(invalid_state_exception::class); infopage::save_static_page_sanitycheck(404); } /** - * @expectedException invalid_state_exception + * Tests infopage::test_sanity_notstring() with invalid contents: an empty string. */ public function test_sanity_empty() { + $this->set_expected_exception(invalid_state_exception::class); infopage::save_static_page_sanitycheck(' '); } /** - * @expectedException invalid_state_exception + * Tests infopage::test_sanity_notstring() with invalid contents: an empty HTML. */ public function test_sanity_clearhtml() { + $this->set_expected_exception(invalid_state_exception::class); infopage::save_static_page_sanitycheck(' '); } + /** + * Tests updating the static page. + */ public function test_updatestaticpage() { $this->resetAfterTest(true); self::setAdminUser(); @@ -210,10 +253,16 @@ class infopagecontroller_test extends advanced_testcase { unlink($file); } + /** + * Tests updating the static page when there is no outage. + */ public function test_updatestaticpage_nooutage() { infopage::update_static_page(); } + /** + * Tests updating the static page when there is no outage but the file existed before. + */ public function test_updatestaticpage_hasfile() { $file = infopage::get_defaulttemplatefile(); touch($file); @@ -223,12 +272,16 @@ class infopagecontroller_test extends advanced_testcase { } /** - * @expectedException coding_exception + * Tests updating the static page with an invalid filename. */ public function test_updatestaticpage_invalidfile() { + $this->set_expected_exception(coding_exception::class); infopage::update_static_page(123); } + /** + * Checks if infopage::has_admin_options() works as expected. + */ public function test_hasadminoptions() { $this->resetAfterTest(true); $static = new infopage(['static' => true]); @@ -245,22 +298,27 @@ class infopagecontroller_test extends advanced_testcase { } /** - * @expectedException coding_exception + * Tries to output a static page without a defined outage. */ public function test_output_static_nooutage() { $info = new infopage(['static' => true]); + + $this->set_expected_exception(coding_exception::class); $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]); + $this->set_expected_exception(moodle_exception::class); $info->output(); } + /** + * Checks the output of the info page. + */ public function test_output() { $now = time(); $outage = new outage([ @@ -277,6 +335,9 @@ class infopagecontroller_test extends advanced_testcase { self::assertContains('auth_outage_info', $output); } + /** + * Checks if we can create and execute a task to update outage pages. + */ public function test_tasks() { $task = new update_static_page(); self::assertNotEmpty($task->get_name()); diff --git a/tests/phpunit/local/outage_test.php b/tests/phpunit/local/outage_test.php index 8096ce3..94705e2 100644 --- a/tests/phpunit/local/outage_test.php +++ b/tests/phpunit/local/outage_test.php @@ -14,19 +14,32 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -use auth_outage\local\outage; - -defined('MOODLE_INTERNAL') || die(); - /** - * Tests performed on outage class. + * outage_test test 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 outage_test extends basic_testcase { + +use auth_outage\local\outage; + +defined('MOODLE_INTERNAL') || die(); +require_once(__DIR__.'/../base_testcase.php'); + +/** + * outage_test test 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 outage_test extends auth_outage_base_testcase { + /** + * Tests the constructor. + */ public function test_constructor() { $outage = new outage(); // Very important, this should never change. @@ -37,6 +50,9 @@ class outage_test extends basic_testcase { } } + /** + * Tests the constructor, giving data as an object. + */ public function test_constructor_object() { $obj = new stdClass(); $obj->id = 1; @@ -59,12 +75,16 @@ class outage_test extends basic_testcase { } /** - * @expectedException coding_exception + * Tests the constructor with invalid data. */ public function test_constructor_invalid() { + $this->set_expected_exception(coding_exception::class); new outage('My outage'); } + /** + * Tests getting the stage considering the current time (now). + */ public function test_getstage_now() { $now = time(); // Make sure it is in the past. @@ -79,13 +99,17 @@ class outage_test extends basic_testcase { } /** - * @expectedException coding_exception + * Tests getting the stage providing an invalid time reference. */ public function test_getstage_invalidtime() { $outage = new outage(); + $this->set_expected_exception(coding_exception::class); $outage->get_stage(-1); } + /** + * Tests is_ongoing() with different outage stages. + */ public function test_isongoing() { $now = time(); @@ -120,6 +144,9 @@ class outage_test extends basic_testcase { self::assertFalse($outage->is_ongoing($now)); } + /** + * Tests is_active() with different outage stages. + */ public function test_isactive() { $now = time(); @@ -164,6 +191,9 @@ class outage_test extends basic_testcase { self::assertFalse($outage->is_active($now)); } + /** + * Tests different outage stages. + */ public function test_stages() { $now = time(); @@ -236,6 +266,9 @@ class outage_test extends basic_testcase { self::assertTrue($outage->has_ended()); } + /** + * Tests if getting title and description replaces the placeholders. + */ public function test_gettitle_getdescription() { $now = time(); $outage = new outage([ @@ -254,6 +287,9 @@ class outage_test extends basic_testcase { self::assertNotContains('}', $description); } + /** + * Tests getting the durations. + */ public function test_getdurations() { $outage = new outage(['starttime' => 1000]); self::assertNull($outage->get_duration_actual()); diff --git a/tests/phpunit/local/outagelib_test.php b/tests/phpunit/local/outagelib_test.php index ce18f32..c6b0a37 100644 --- a/tests/phpunit/local/outagelib_test.php +++ b/tests/phpunit/local/outagelib_test.php @@ -14,6 +14,15 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +/** + * outagelib_test test 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 + */ + use auth_outage\dml\outagedb; use auth_outage\local\outage; use auth_outage\local\outagelib; @@ -21,7 +30,7 @@ use auth_outage\local\outagelib; defined('MOODLE_INTERNAL') || die(); /** - * Tests performed on outagelib class. + * outagelib_test test class. * * @package auth_outage * @author Daniel Thee Roperto @@ -29,6 +38,9 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class outagelib_test extends advanced_testcase { + /** + * Check if maintenance message is disabled as needed. + */ public function test_maintenancemessage() { $this->resetAfterTest(true); static::setAdminUser(); @@ -50,6 +62,9 @@ class outagelib_test extends advanced_testcase { phpunit_util::reset_debugging(); } + /** + * Check if maintenance later is removed if no outage set. + */ public function test_maintenancelater_nonext() { $this->resetAfterTest(true); set_config('maintenance_later', time() + (60 * 60 * 24 * 7)); // In 1 week. @@ -58,6 +73,9 @@ class outagelib_test extends advanced_testcase { self::assertEmpty(get_config('moodle', 'maintenance_later')); } + /** + * Check outagelib::inject() works as expected. + */ public function test_inject() { global $CFG; @@ -85,6 +103,9 @@ class outagelib_test extends advanced_testcase { self::assertSame($size, strlen($CFG->additionalhtmltopofbody)); } + /** + * Check outagelib::inject() will not break the page if something goes wrong. + */ public function test_inject_broken() { $_GET = ['auth_outage_break_code' => '1']; outagelib::reinject(); @@ -92,6 +113,9 @@ class outagelib_test extends advanced_testcase { phpunit_util::reset_debugging(); } + /** + * Check if injection works with preview. + */ public function test_inject_preview() { global $CFG; $this->resetAfterTest(true); @@ -114,6 +138,9 @@ class outagelib_test extends advanced_testcase { self::assertContains('