Issue #46 - Added tests for every class, except outputs. Small bugs/invalid parameter detection fixed together.

This commit is contained in:
Daniel Thee Roperto
2016-09-26 19:34:27 +10:00
parent 619a5a663f
commit 8791727ba4
28 changed files with 1090 additions and 239 deletions

View File

@@ -20,7 +20,6 @@ use auth_outage\local\outage;
use calendar_event;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/calendar/lib.php');
/**
@@ -34,6 +33,7 @@ require_once($CFG->dirroot.'/calendar/lib.php');
class calendar {
/**
* Private constructor, use static methods instead.
* @codeCoverageIgnore
*/
private function __construct() {
}
@@ -42,22 +42,23 @@ class calendar {
* Create an event on the calendar for this outage.
* @param outage $outage Outage to be added to the calendar.
*/
public static function calendar_create(outage $outage) {
calendar_event::create(self::calendar_data($outage));
public static function create(outage $outage) {
calendar_event::create(self::create_data($outage));
}
/**
* Updates an event on the calendar based on this outage.
* @param outage $outage Outage to be updated in the calendar.
* @SuppressWarnings("comment") Allow this test to have as many tests as necessary.
*/
public static function calendar_update(outage $outage) {
$event = self::calendar_load($outage->id);
public static function update(outage $outage) {
$event = self::load($outage->id);
if (is_null($event)) {
debugging('Cannot update calendar entry for outage #'.$outage->id.', event not found. Creating it...');
self::calendar_create($outage);
self::create($outage);
} else {
$event->update(self::calendar_data($outage));
$event->update(self::create_data($outage));
}
}
@@ -65,8 +66,8 @@ class calendar {
* Removes an event from the calendar related to this outage.
* @param int $outageid Id of outage to be deleted from the calendar.
*/
public static function calendar_delete($outageid) {
$event = self::calendar_load($outageid);
public static function delete($outageid) {
$event = self::load($outageid);
// If not found (was not created before) ignore it.
if (is_null($event)) {
@@ -81,7 +82,7 @@ class calendar {
* @param outage $outage Outage to use as reference for the calendar event.
* @return mixed[] Calendar event data.
*/
private static function calendar_data(outage $outage) {
private static function create_data(outage $outage) {
return [
'name' => $outage->get_title(),
'description' => $outage->get_description(),
@@ -102,7 +103,7 @@ class calendar {
* @param int $outageid The outage id to find in the calendar.
* @return calendar_event|null The calendar event or null if not found.
*/
private static function calendar_load($outageid) {
public static function load($outageid) {
global $DB;
$event = $DB->get_record_select(

View File

@@ -40,6 +40,7 @@ require_once($CFG->dirroot.'/calendar/lib.php');
class outagedb {
/**
* Private constructor, use static methods instead.
* @codeCoverageIgnore
*/
private function __construct() {
}
@@ -62,7 +63,7 @@ class outagedb {
}
/**
* @param $id int Outage id to get.
* @param int $id Outage id to get.
* @return outage|null Returns the outage or null if not found.
* @throws coding_exception
*/
@@ -106,7 +107,7 @@ class outagedb {
['objectid' => $outage->id, 'other' => (array)$outage]
)->trigger();
// Create calendar entry.
calendar::calendar_create($outage);
calendar::create($outage);
} else {
// Remove the createdby field so it does not get updated.
unset($outage->createdby);
@@ -116,7 +117,7 @@ class outagedb {
['objectid' => $outage->id, 'other' => (array)$outage]
)->trigger();
// Update calendar entry.
calendar::calendar_update($outage);
calendar::update($outage);
}
// Trigger outages modified events.
@@ -147,7 +148,7 @@ class outagedb {
// Delete it and remove from calendar.
$DB->delete_records('auth_outage', ['id' => $id]);
calendar::calendar_delete($id);
calendar::delete($id);
// Trigger events.
outagelib::outages_modified();

98
classes/form/baseform.php Normal file
View File

@@ -0,0 +1,98 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace auth_outage\form;
use moodleform;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/formslib.php');
/**
* Outage base for forms, extends Moodle form to fix a but in the validation method.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class baseform extends moodleform {
/**
* Validate the form.
*
* You almost always want to call {@link is_validated} instead of this
* because it calls {@link definition_after_data} first, before validating the form,
* which is what you want in 99% of cases.
*
* This is provided as a separate function for those special cases where
* you want the form validated before definition_after_data is called
* for example, to selectively add new elements depending on a no_submit_button press,
* but only when the form is valid when the no_submit_button is pressed,
*
* @param bool $validateonnosubmit optional, defaults to false. The default behaviour
* is NOT to validate the form when a no submit button has been pressed.
* pass true here to override this behaviour
*
* @return bool true if form data valid
* @SuppressWarnings(PHPMD) It is better to not refactor this method as it is linked to its parent functionality.
* @codeCoverageIgnore
*/
public function validate_defined_fields($validateonnosubmit = false) {
// One validation NOT is enough (if mocking). See parent method.
$mform =& $this->_form;
if ($this->no_submit_button_pressed() && empty($validateonnosubmit)) {
return false;
}
$internalval = $mform->validate();
$files = [];
$fileval = $this->_validate_files($files);
// Check draft files for validation and flag them if required files are not in draft area.
$draftfilevalue = $this->validate_draft_files();
if ($fileval !== true && $draftfilevalue !== true) {
$fileval = array_merge($fileval, $draftfilevalue);
} else if ($draftfilevalue !== true) {
$fileval = $draftfilevalue;
} //default is file_val, so no need to assign.
if ($fileval !== true) {
if (!empty($fileval)) {
foreach ($fileval as $element => $msg) {
$mform->setElementError($element, $msg);
}
}
$fileval = false;
}
$data = $mform->exportValues();
$moodleval = $this->validation($data, $files);
if ((is_array($moodleval) && count($moodleval) !== 0)) {
// Non-empty array means errors.
foreach ($moodleval as $element => $msg) {
$mform->setElementError($element, $msg);
}
$moodleval = false;
} else {
// Anything else means validation ok.
$moodleval = true;
}
$validated = ($internalval and $moodleval and $fileval);
return $validated;
}
}

View File

@@ -32,8 +32,7 @@ defined('MOODLE_INTERNAL') || die();
*/
class delete extends \moodleform {
/**
* {@inheritDoc}
* @see moodleform::definition()
* Defines the form elements.
*/
public function definition() {
$mform = $this->_form;

View File

@@ -16,9 +16,9 @@
namespace auth_outage\form\outage;
use auth_outage\form\baseform;
use auth_outage\local\outage;
use coding_exception;
use moodleform;
defined('MOODLE_INTERNAL') || die();
@@ -32,7 +32,7 @@ require_once($CFG->libdir.'/formslib.php');
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class edit extends moodleform {
class edit extends baseform {
const TITLE_MAX_CHARS = 100;
/**

View File

@@ -284,6 +284,7 @@ class create extends clibase {
}
}
throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param]));
throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param]),
cli_exception::ERROR_PARAMETER_INVALID);
}
}

View File

@@ -74,10 +74,11 @@ class infopage {
* Given the HTML code for the static page, find the outage id for that page.
* @param string $html Static info page HTML.
* @return int|null Outage id or null if not found.
* @throws coding_exception
*/
public static function find_outageid_from_infopage($html) {
if (!is_string($html)) {
throw new coding_exception('$html must be a string.', $html);
throw new coding_exception('$html must be a string.', gettype($html));
}
$output = [];
@@ -103,10 +104,7 @@ class infopage {
$info = new infopage(['outage' => $outage, 'static' => true]);
$html = $info->get_output();
// Sanity check before writing/overwriting old file.
if (!is_string($html) || ($html == '') || (html_to_text($html) == '')) {
throw new invalid_state_exception('Sanity check failed. Invalid contents on $html.');
}
self::save_static_page_sanitycheck($html);
$dir = dirname($file);
if (!file_exists($dir) || !is_dir($dir)) {
@@ -117,7 +115,9 @@ class infopage {
/**
* Updates the static info page by (re)creating or deleting it as needed.
* @param null $file
* @param string|null $file File to update. Null to use default.
* @throws coding_exception
* @throws file_exception
*/
public static function update_static_page($file = null) {
if (is_null($file)) {
@@ -149,19 +149,33 @@ class infopage {
return $CFG->dataroot.'/climaintenance.template.html';
}
/**
* Ensures the data to create the page is valid.
* It should never be invalid, but if it is we will get a blank page while the maintenance is ongoing and the
* system administrators may become frustrated to understand why it is not working, let's not provoke them!
* @param string $html The HTML to check.
* @throws invalid_state_exception
*/
public static function save_static_page_sanitycheck($html) {
if (!is_string($html) || (trim($html) == '') || (trim(html_to_text($html)) == '')) {
throw new invalid_state_exception('Sanity check failed. Invalid contents on $html.');
}
}
/**
* Generates and returns the HTML for the info page.
* @return string HTML for the info page.
*/
public function get_output() {
ob_start();
$output = null;
try {
// TODO what if redirection occurs here?
$this->output();
return ob_get_contents();
$output = ob_get_contents();
} finally {
ob_end_clean();
}
return $output;
}
/**
@@ -213,6 +227,7 @@ class infopage {
/**
* Adjusts the fields according to the given parameters.
* @param mixed[] $params
* @throws coding_exception
*/
private function set_parameters(array $params) {
if (!is_null($params['outage']) && !($params['outage'] instanceof outage)) {
@@ -220,7 +235,7 @@ class infopage {
}
if (!is_null($params['id']) && !is_null($params['outage']) && ($params['id'] !== $params['outage']->id)) {
throw new coding_exception('Provided id and outage->id do not match.', $params);
throw new coding_exception('Provided id and outage->id do not match.', $params['id'].'/'.$params['outage']->id);
}
if (is_null($params['id']) && is_null($params['outage'])) {

View File

@@ -20,7 +20,6 @@ use auth_outage\dml\outagedb;
use auth_outage\local\controllers\infopage;
use auth_outage\output\renderer;
use Exception;
use moodle_url;
use stdClass;
defined('MOODLE_INTERNAL') || die();
@@ -34,17 +33,14 @@ defined('MOODLE_INTERNAL') || die();
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class outagelib {
private static $initialized = false;
private static $injected = false;
/**
* Initializes admin pages for outage.
* @return renderer The outage renderer for the page.
* Calls inject even if it was already called before.
*/
public static function page_setup() {
global $PAGE;
admin_externalpage_setup('auth_outage_manage');
$PAGE->set_url(new moodle_url('/auth/outage/manage.php'));
return renderer::get();
public static function reinject() {
self::$injected = false;
self::inject();
}
/**
@@ -54,13 +50,18 @@ class outagelib {
global $CFG;
// Many hooks can call it, execute only once.
if (self::$initialized) {
if (self::$injected) {
return;
}
self::$initialized = true;
self::$injected = true;
// Ensure we do not kill the whole website in case of an error.
try {
// Ensure no exceptions break the code.
if (PHPUNIT_TEST && optional_param('auth_outage_break_code', false, PARAM_INT)) {
(new stdClass())->invalidfield;
}
// Check for a previewing outage, then for an active outage.
$previewid = optional_param('auth_outage_preview', null, PARAM_INT);
$time = time();
@@ -74,7 +75,8 @@ class outagelib {
return;
}
// Delta is in seconds, setting the time our warning bar will consider relative to the outage start time.
$time = $active->starttime + optional_param('auth_outage_delta', 0, PARAM_INT);
$delta = optional_param('auth_outage_delta', 0, PARAM_INT);
$time = $active->starttime + $delta;
if (!$active->is_active($time)) {
return;
}
@@ -134,10 +136,8 @@ class outagelib {
} else {
$message = get_config('moodle', 'maintenance_message');
if ($message) {
if (!defined('PHPUNIT_TEST') || !PHPUNIT_TEST) {
error_log('Disabling $CFG->maintenance_message to allow our template page to take place.');
error_log('Previous value: '.$message);
}
debugging('Disabling $CFG->maintenance_message to allow our template page to take place.');
debugging('Previous value: '.$message);
// We cannot do much if forced config, but the logs will show the error.
unset_config('maintenance_message');
}

View File

@@ -25,13 +25,14 @@
use auth_outage\dml\outagedb;
use auth_outage\form\outage\edit;
use auth_outage\local\outagelib;
use auth_outage\output\renderer;
require_once(__DIR__.'/../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/formslib.php');
$renderer = outagelib::page_setup();
admin_externalpage_setup('auth_outage_manage');
$PAGE->set_url(new moodle_url('/auth/outage/manage.php'));
$mform = new edit();
@@ -54,6 +55,6 @@ $mform->set_data($outage);
$PAGE->navbar->add($outage->get_title());
echo $OUTPUT->header();
echo $renderer->rendersubtitle('outageclone');
echo renderer::get()->rendersubtitle('outageclone');
$mform->display();
echo $OUTPUT->footer();

View File

@@ -25,13 +25,14 @@
use auth_outage\dml\outagedb;
use auth_outage\form\outage\delete;
use auth_outage\local\outagelib;
use auth_outage\output\renderer;
require_once(__DIR__.'/../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/formslib.php');
$renderer = outagelib::page_setup();
admin_externalpage_setup('auth_outage_manage');
$PAGE->set_url(new moodle_url('/auth/outage/manage.php'));
$mform = new delete();
if ($mform->is_cancelled()) {
@@ -53,7 +54,7 @@ $mform->set_data($dataid);
echo $OUTPUT->header();
echo $renderer->renderdeleteconfirmation($outage);
echo renderer::get()->renderdeleteconfirmation($outage);
$mform->display();

View File

@@ -25,13 +25,14 @@
use auth_outage\dml\outagedb;
use auth_outage\form\outage\edit;
use auth_outage\local\outagelib;
use auth_outage\output\renderer;
require_once(__DIR__.'/../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/formslib.php');
$renderer = outagelib::page_setup();
admin_externalpage_setup('auth_outage_manage');
$PAGE->set_url(new moodle_url('/auth/outage/manage.php'));
$mform = new edit();
@@ -51,6 +52,6 @@ $mform->set_data($outage);
$PAGE->navbar->add($outage->get_title());
echo $OUTPUT->header();
echo $renderer->rendersubtitle('outageedit');
echo renderer::get()->rendersubtitle('outageedit');
$mform->display();
echo $OUTPUT->footer();

View File

@@ -25,13 +25,14 @@
use auth_outage\dml\outagedb;
use auth_outage\form\outage\finish;
use auth_outage\local\outagelib;
use auth_outage\output\renderer;
require_once(__DIR__.'/../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/formslib.php');
$renderer = outagelib::page_setup();
admin_externalpage_setup('auth_outage_manage');
$PAGE->set_url(new moodle_url('/auth/outage/manage.php'));
$mform = new finish();
if ($mform->is_cancelled()) {
@@ -53,7 +54,7 @@ $mform->set_data($dataid);
echo $OUTPUT->header();
echo $renderer->renderfinishconfirmation($outage);
echo renderer::get()->renderfinishconfirmation($outage);
$mform->display();

View File

@@ -24,16 +24,17 @@
*/
use auth_outage\dml\outagedb;
use auth_outage\local\outagelib;
use auth_outage\output\renderer;
require_once(__DIR__.'/../../config.php');
require_once($CFG->libdir.'/adminlib.php');
$renderer = outagelib::page_setup();
admin_externalpage_setup('auth_outage_manage');
$PAGE->set_url(new moodle_url('/auth/outage/manage.php'));
echo $OUTPUT->header();
$renderer->output_view('manage.php', [
renderer::get()->output_view('manage.php', [
'unended' => outagedb::get_all_unended(),
'ended' => outagedb::get_all_ended(),
]);

View File

@@ -32,7 +32,8 @@ require_once(__DIR__.'/../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/formslib.php');
outagelib::page_setup();
admin_externalpage_setup('auth_outage_manage');
$PAGE->set_url(new moodle_url('/auth/outage/manage.php'));
$mform = new edit();
if ($mform->is_cancelled()) {

View File

@@ -0,0 +1,112 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
use auth_outage\calendar\calendar;
use auth_outage\local\outage;
defined('MOODLE_INTERNAL') || die();
/**
* Tests performed on outage class.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class calendar_test extends advanced_testcase {
public function test_create() {
self::setAdminUser();
$this->resetAfterTest(false);
$time = time();
$outage = new outage([
'id' => 1,
'autostart' => false,
'warntime' => $time - 100,
'starttime' => $time,
'stoptime' => $time + (2 * 60 * 60),
'title' => 'Title',
'description' => 'Description',
]);
calendar::create($outage);
$this->check_calendar($outage);
return $outage;
}
/**
* @depends test_create
*/
public function test_update(outage $outage) {
self::setAdminUser();
$this->resetAfterTest(false);
$outage->title = 'New Title';
calendar::update($outage);
$this->check_calendar($outage);
return $outage;
}
/**
* @depends test_update
*/
public function test_delete($outage) {
self::setAdminUser();
$this->resetAfterTest(true);
calendar::delete($outage->id);
self::assertNull(calendar::load($outage->id));
}
public function test_update_notfound() {
self::setAdminUser();
$this->resetAfterTest(true);
$time = time();
$outage = new outage([
'id' => 1,
'autostart' => false,
'warntime' => $time - 100,
'starttime' => $time,
'stoptime' => $time + (2 * 60 * 60),
'title' => 'Title',
'description' => 'Description',
]);
calendar::update($outage);
self::assertCount(1, phpunit_util::get_debugging_messages());
phpunit_util::reset_debugging();
}
public function test_delete_notfound() {
self::setAdminUser();
$this->resetAfterTest(true);
calendar::delete(1);
self::assertCount(1, phpunit_util::get_debugging_messages());
phpunit_util::reset_debugging();
}
private function check_calendar(outage $outage) {
$calendar = calendar::load($outage->id);
self::assertSame($outage->title, $calendar->name);
self::assertSame($outage->description, $calendar->description);
self::assertSame('auth_outage', $calendar->eventtype);
self::assertEquals($outage->starttime, $calendar->timestart);
self::assertEquals($outage->get_duration_planned(), $calendar->timeduration);
}
}

View File

@@ -22,10 +22,11 @@ defined('MOODLE_INTERNAL') || die();
/**
* Tests performed on outage class.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @SuppressWarnings("public") Allow this test to have as many tests as necessary.
*/
class outagedb_test extends advanced_testcase {
/**
@@ -329,6 +330,106 @@ class outagedb_test extends advanced_testcase {
self::createidarray(outagedb::get_all_ended($now)), 'Wrong past data.');
}
/**
* @expectedException coding_exception
*/
public function test_getbyid_invalid() {
$this->resetAfterTest(true);
outagedb::get_by_id(-1);
}
/**
* @expectedException coding_exception
*/
public function test_delete_invalid() {
$this->resetAfterTest(true);
outagedb::delete(-1);
}
/**
* @expectedException coding_exception
*/
public function test_getactive_invalid() {
$this->resetAfterTest(true);
outagedb::get_active(-1);
}
/**
* @expectedException coding_exception
*/
public function test_getallunended_invalid() {
$this->resetAfterTest(true);
outagedb::get_all_unended(-1);
}
public function test_getallunended_now() {
$this->resetAfterTest(true);
outagedb::get_all_unended();
}
/**
* @expectedException coding_exception
*/
public function test_getallended_invalid() {
$this->resetAfterTest(true);
outagedb::get_all_ended(-1);
}
public function test_getallended_now() {
$this->resetAfterTest(true);
outagedb::get_all_ended();
}
/**
* @expectedException coding_exception
*/
public function test_finish_invalid() {
$this->resetAfterTest(true);
outagedb::finish(1, -1);
}
public function test_finish_now_notfound() {
$this->resetAfterTest(true);
outagedb::finish(1);
self::assertCount(1, phpunit_util::get_debugging_messages());
phpunit_util::reset_debugging();
}
public function test_finish_notongoing() {
$this->resetAfterTest(true);
$time = time();
$outage = new outage([
'autostart' => false,
'warntime' => $time + (60 * 60 * 24 * 1),
'starttime' => $time + (60 * 60 * 24 * 2),
'stoptime' => $time + (60 * 60 * 24 * 3),
'title' => 'Title',
'description' => 'Description',
]);
$id = outagedb::save($outage);
self::assertTrue(!$outage->is_ongoing($time));
outagedb::finish($id, $time);
self::assertCount(1, phpunit_util::get_debugging_messages());
phpunit_util::reset_debugging();
}
/**
* @expectedException coding_exception
*/
public function test_getnextstartinginvalid() {
$this->resetAfterTest(true);
outagedb::get_next_starting(-1);
}
/**
* @expectedException coding_exception
*/
public function test_getnextautostartinginvalid() {
$this->resetAfterTest(true);
outagedb::get_next_autostarting(-1);
}
/**
* Helper function to create an outage for tests.
*

View File

@@ -26,9 +26,6 @@ defined('MOODLE_INTERNAL') || die();
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @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() {

View File

@@ -0,0 +1,130 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
use auth_outage\form\outage\delete;
use auth_outage\form\outage\edit;
use auth_outage\form\outage\finish;
use auth_outage\local\outage;
defined('MOODLE_INTERNAL') || die();
/**
* Tests performed on forms classes.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class forms_test extends advanced_testcase {
public function test_delete() {
new delete();
}
public function test_finish() {
new finish();
}
public function test_edit_valid() {
$this->mock_edit_post();
$edit = new edit();
self::assertFalse($edit->is_cancelled());
$outage = $edit->get_data();
self::assertInstanceOf(outage::class, $outage);
self::assertSame(false, $outage->autostart);
self::assertSame(60, $outage->get_warning_duration());
self::assertSame(mktime(14, 15, 0, 2, 1, 2013), $outage->starttime);
self::assertSame(2 * 60 * 60, $outage->get_duration_planned());
self::assertSame('The title.', $outage->title);
self::assertSame('The <b>description</b>.', $outage->description);
}
public function test_edit_invalid_warning() {
$this->mock_edit_post();
$_POST['warningduration'] = ['number' => '-1', 'timeunit' => '60'];
$edit = new edit();
$outage = $edit->get_data();
self::assertNull($outage);
}
public function test_edit_invalid_duration() {
$this->mock_edit_post();
$_POST['outageduration'] = ['number' => '-2', 'timeunit' => '3600'];
$edit = new edit();
self::assertNull($edit->get_data());
}
public function test_edit_invalid_title() {
$this->mock_edit_post();
$_POST['title'] = '';
$edit = new edit();
self::assertNull($edit->get_data());
}
public function test_edit_invalid_title_toolong() {
$this->mock_edit_post();
$_POST['title'] = 'This is a very long time, it is so long that at some point it should not be valid. '.
'With a very long title used in this place we should get a form validation error. '.
'Do you think this title is long enough?';
$edit = new edit();
self::assertNull($edit->get_data());
}
public function test_edit_description_invalid_format() {
$this->mock_edit_post();
$_POST['description'] = ['text' => 'The <b>description</b>.', 'format' => '2'];
$edit = new edit();
self::assertNull($edit->get_data());
self::assertCount(1, phpunit_util::get_debugging_messages());
phpunit_util::reset_debugging();
}
public function test_setdata() {
$outage = new outage([
'autostart' => false,
'warntime' => time() - 60,
'starttime' => time(),
'stoptime' => time() + 60,
'title' => 'Title',
'description' => 'Description',
]);
$edit = new edit();
$edit->set_data($outage);
}
/**
* @expectedException coding_exception
*/
public function test_setdata_invalid() {
$edit = new edit();
$edit->set_data(null);
}
private function mock_edit_post() {
// There is a bug in moodleform::mock_submit so we make our own version.
$_POST = [
'id' => '1',
'sesskey' => sesskey(),
'_qf__auth_outage_form_outage_edit' => '1',
'warningduration' => ['number' => '1', 'timeunit' => '60'],
'starttime' => ['day' => '1', 'month' => '2', 'year' => '2013', 'hour' => '14', 'minute' => '15'],
'outageduration' => ['number' => '2', 'timeunit' => '3600'],
'title' => 'The title.',
'description' => ['text' => 'The <b>description</b>.', 'format' => '1'],
'submitbutton' => 'Save changes',
];
}
}

View File

@@ -1,104 +0,0 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
use auth_outage\local\controllers\infopage;
use auth_outage\local\outage;
defined('MOODLE_INTERNAL') || die();
/**
* Tests performed on infopage_controller class.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\local\controllers\infopage
*/
class infopagecontroller_test extends advanced_testcase {
public function setUp() {
if (file_exists($this->get_file())) {
if (is_file($this->get_file())) {
unlink($this->get_file());
} else {
self::fail('Invalid temp file: '.$this->get_file());
}
}
}
/**
* Return a temporary file name to use for this test.
* @return string Default file.
*/
public function get_file() {
return sys_get_temp_dir().'/phpunit_authoutage.tmp';
}
public function test_staticpage_output() {
global $PAGE;
$this->resetAfterTest(true);
$PAGE->set_context(context_system::instance());
$now = time();
$outage = new outage([
'id' => 1,
'starttime' => $now + (60 * 60),
'warntime' => $now - (60 * 60),
'stoptime' => $now + (2 * 60 * 60),
'title' => 'Outage Title at {{start}}',
'description' => 'This is an <b>important</b> outage, starting at {{start}}.',
]);
$info = new infopage(['static' => true, 'outage' => $outage]);
$html = $info->get_output();
// Must find...
self::assertContains('<!DOCTYPE html>', $html);
self::assertContains('<meta http-equiv="refresh" ', $html);
self::assertContains('</html>', $html);
self::assertContains($outage->get_title(), $html);
self::assertContains($outage->get_description(), $html);
// Must not find...
self::assertNotContains('<link ', $html);
self::assertNotContains('<a ', $html);
self::assertNotContains('<script ', $html);
// Ensure it has the id encoded in it...
self::assertSame($outage->id, infopage::find_outageid_from_infopage($html));
}
public function test_staticpage_file() {
$now = time();
$outage = new outage([
'id' => 1,
'warntime' => $now - 100,
'starttime' => $now + 100,
'stoptime' => $now + 200,
'title' => 'Title',
'description' => 'Description',
]);
infopage::save_static_page($outage, $this->get_file());
self::assertFileExists($this->get_file());
$id = infopage::find_outageid_from_infopage(file_get_contents($this->get_file()));
self::assertSame($outage->id, $id);
unlink($this->get_file());
}
public function test_getdefaulttemplatefile() {
$file = infopage::get_defaulttemplatefile();
self::assertTrue(is_string($file));
self::assertContains('template', $file);
}
}

View File

@@ -27,8 +27,6 @@ require_once(__DIR__.'/cli_testcase.php');
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\local\cli\clibase
* @covers \auth_outage\local\cli\cli_exception
* @SuppressWarnings("public")
*/
class cli_test extends cli_testcase {

View File

@@ -25,10 +25,10 @@ require_once(__DIR__.'/cli_testcase.php');
/**
* Tests performed on CLI create class.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @SuppressWarnings("public") Allow this test to have as many tests as necessary.
*/
class create_test extends cli_testcase {
@@ -273,4 +273,29 @@ class create_test extends cli_testcase {
self::assertContains('created', $text);
self::assertContains('started', $text);
}
/**
* @expectedException coding_exception
*/
public function test_setdefaults_extra() {
$cli = new create([]);
$cli->set_defaults(['aninvalidparameter' => 'value']);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 3
*/
public function test_invalid_bool() {
$this->set_parameters([
'--autostart=maybe',
'--warn=60',
'--start=0',
'--duration=600',
'--title=Title',
'--description=Description',
]);
$cli = new create();
$cli->execute();
}
}

View File

@@ -25,11 +25,10 @@ require_once(__DIR__.'/cli_testcase.php');
/**
* Tests performed on CLI finish class.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\local\cli\finish
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class finish_test extends cli_testcase {
public function test_constructor() {

View File

@@ -24,11 +24,10 @@ require_once(__DIR__.'/cli_testcase.php');
/**
* Tests performed on CLI waitforit class.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\local\cli\waitforit
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @SuppressWarnings("public")
*/
class waitforit_test extends cli_testcase {

View File

@@ -0,0 +1,285 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
use auth_outage\dml\outagedb;
use auth_outage\local\controllers\infopage;
use auth_outage\local\outage;
defined('MOODLE_INTERNAL') || die();
/**
* Tests performed on infopage controller class and update_static_page task class.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @SuppressWarnings("public") Allow this test to have as many tests as necessary.
*/
class infopagecontroller_test extends advanced_testcase {
public function setUp() {
$file = infopage::get_defaulttemplatefile();
if (file_exists($file)) {
if (is_file($file)) {
unlink($file);
} else {
self::fail('Invalid temp file: '.$file);
}
}
}
public function test_constructor() {
new infopage();
}
public function test_constructor_withparams() {
$_GET = ['id' => 1, 'static' => 'true'];
new infopage();
}
/**
* @expectedException coding_exception
*/
public function test_constructor_idmismatch() {
$outage = new outage([
'id' => 1,
'autostart' => false,
'warntime' => time() - 60,
'starttime' => time(),
'stoptime' => time() + 60,
'title' => 'Title',
'description' => 'Description',
]);
new infopage(['id' => 2, 'outage' => $outage]);
}
/**
* @expectedException coding_exception
*/
public function test_constructor_invalidoutage() {
new infopage(['outage' => 'My outage']);
}
public function test_staticpage_output() {
global $PAGE;
$this->resetAfterTest(true);
$PAGE->set_context(context_system::instance());
$now = time();
$outage = new outage([
'id' => 1,
'starttime' => $now + (60 * 60),
'warntime' => $now - (60 * 60),
'stoptime' => $now + (2 * 60 * 60),
'title' => 'Outage Title at {{start}}',
'description' => 'This is an <b>important</b> outage, starting at {{start}}.',
]);
$info = new infopage(['static' => true, 'outage' => $outage]);
$html = $info->get_output();
// Must find...
self::assertContains('<!DOCTYPE html>', $html);
self::assertContains('<meta http-equiv="refresh" ', $html);
self::assertContains('</html>', $html);
self::assertContains($outage->get_title(), $html);
self::assertContains($outage->get_description(), $html);
// Must not find...
self::assertNotContains('<link ', $html);
self::assertNotContains('<a ', $html);
self::assertNotContains('<script ', $html);
// Ensure it has the id encoded in it...
self::assertSame($outage->id, infopage::find_outageid_from_infopage($html));
}
public function test_staticpage_file() {
$now = time();
$outage = new outage([
'id' => 1,
'warntime' => $now - 100,
'starttime' => $now + 100,
'stoptime' => $now + 200,
'title' => 'Title',
'description' => 'Description',
]);
$file = infopage::get_defaulttemplatefile();
infopage::save_static_page($outage, $file);
self::assertFileExists($file);
$id = infopage::find_outageid_from_infopage(file_get_contents($file));
self::assertSame($outage->id, $id);
unlink($file);
}
public function test_getdefaulttemplatefile() {
$file = infopage::get_defaulttemplatefile();
self::assertTrue(is_string($file));
self::assertContains('template', $file);
}
/**
* @expectedException coding_exception
*/
public function test_findoutageid_notstring() {
infopage::find_outageid_from_infopage(new stdClass());
}
public function test_findoutageid_notfound() {
self::assertNull(
infopage::find_outageid_from_infopage(
'<html><head><title>Hello world!</title></head><body>Done.</body></html>'
)
);
}
/**
* @expectedException coding_exception
*/
public function test_savestaticpage_filenotstring() {
infopage::save_static_page(new outage(), 1);
}
/**
* @expectedException file_exception
*/
public function test_savestaticpage_fileinvalid() {
global $CFG;
$outage = new outage([
'id' => 1,
'warntime' => time() - 100,
'starttime' => time() + 100,
'stoptime' => time() + 200,
'title' => 'Title',
'description' => 'Description',
]);
infopage::save_static_page($outage, $CFG->dataroot.'/someinvalidpath/someinvalidfile');
}
/**
* @expectedException invalid_state_exception
*/
public function test_sanity_notstring() {
infopage::save_static_page_sanitycheck(404);
}
/**
* @expectedException invalid_state_exception
*/
public function test_sanity_empty() {
infopage::save_static_page_sanitycheck(' ');
}
/**
* @expectedException invalid_state_exception
*/
public function test_sanity_clearhtml() {
infopage::save_static_page_sanitycheck('<html><head></head><body><b> <!-- Nothing --> </b></body></html>');
}
public function test_updatestaticpage() {
$this->resetAfterTest(true);
self::setAdminUser();
$file = infopage::get_defaulttemplatefile();
$now = time();
$outage = new outage([
'autostart' => false,
'warntime' => $now - 100,
'starttime' => $now + 100,
'stoptime' => $now + 200,
'title' => 'Title',
'description' => 'Description',
]);
$id = outagedb::save($outage);
// The method update_static_page should have been called by save().
self::assertFileExists($file);
$idfound = infopage::find_outageid_from_infopage(file_get_contents($file));
self::assertSame($id, $idfound);
unlink($file);
}
public function test_updatestaticpage_nooutage() {
infopage::update_static_page();
}
public function test_updatestaticpage_hasfile() {
$file = infopage::get_defaulttemplatefile();
touch($file);
self::assertFileExists($file);
infopage::update_static_page();
self::assertFileNotExists($file);
}
/**
* @expectedException coding_exception
*/
public function test_updatestaticpage_invalidfile() {
infopage::update_static_page(123);
}
public function test_hasadminoptions() {
$this->resetAfterTest(true);
$static = new infopage(['static' => true]);
$nonstatic = new infopage(['static' => false]);
// Now I am guest.
self::assertFalse(is_siteadmin());
self::assertFalse($static->has_admin_options());
self::assertFalse($nonstatic->has_admin_options());
// Now I am admin.
self::setAdminUser();
self::assertTrue(is_siteadmin());
self::assertFalse($static->has_admin_options());
self::assertTrue($nonstatic->has_admin_options());
}
/**
* @expectedException coding_exception
*/
public function test_output_static_nooutage() {
$info = new infopage(['static' => true]);
$info->output();
}
/**
* We should have an exception because CLI cannot redirect.
* @expectedException moodle_exception
*/
public function test_output_nonstatic_nooutage() {
$info = new infopage(['static' => false]);
$info->output();
}
public function test_output() {
$now = time();
$outage = new outage([
'id' => 1,
'autostart' => false,
'warntime' => $now - 100,
'starttime' => $now + 100,
'stoptime' => $now + 200,
'title' => 'Title',
'description' => 'Description',
]);
$info = new infopage(['outage' => $outage]);
self::hasExpectationOnOutput();
$output = $info->get_output();
self::assertContains('auth_outage_info', $output);
}
public function test_tasks() {
$task = new \auth_outage\task\update_static_page();
self::assertNotEmpty($task->get_name());
$task->execute();
}
}

View File

@@ -25,7 +25,6 @@ defined('MOODLE_INTERNAL') || die();
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\local\outage
*/
class outage_test extends basic_testcase {
public function test_constructor() {
@@ -38,6 +37,55 @@ class outage_test extends basic_testcase {
}
}
public function test_constructor_object() {
$obj = new stdClass();
$obj->id = 1;
$obj->autostart = true;
$obj->warntime = 2;
$obj->starttime = 3;
$obj->finished = 4;
$obj->stoptime = 5;
$obj->title = 'Title';
$obj->description = 'Description';
$outage = new outage($obj);
self::assertSame($obj->id, $outage->id);
self::assertSame($obj->autostart, $outage->autostart);
self::assertSame($obj->warntime, $outage->warntime);
self::assertSame($obj->starttime, $outage->starttime);
self::assertSame($obj->finished, $outage->finished);
self::assertSame($obj->stoptime, $outage->stoptime);
self::assertSame($obj->title, $outage->title);
self::assertSame($obj->description, $outage->description);
}
/**
* @expectedException coding_exception
*/
public function test_constructor_invalid() {
new outage('My outage');
}
public function test_getstage_now() {
$now = time();
// Make sure it is in the past.
$outage = new outage([
'starttime' => $now - (3 * 60 * 60),
'stoptime' => $now - (2 * 60 * 60),
'warntime' => $now - (2 * 60 * 60),
'title' => '',
'description' => '',
]);
self::assertSame(outage::STAGE_STOPPED, $outage->get_stage());
}
/**
* @expectedException coding_exception
*/
public function test_getstage_invalidtime() {
$outage = new outage();
$outage->get_stage(-1);
}
public function test_isongoing() {
$now = time();
@@ -128,6 +176,7 @@ class outage_test extends basic_testcase {
self::assertSame(outage::STAGE_WAITING, $outage->get_stage($now));
self::assertFalse($outage->is_active($now));
self::assertFalse($outage->is_ongoing($now));
self::assertFalse($outage->has_ended());
$outage = new outage([
'warntime' => $now - 10,
@@ -138,6 +187,7 @@ class outage_test extends basic_testcase {
self::assertSame(outage::STAGE_WARNING, $outage->get_stage($now));
self::assertTrue($outage->is_active($now));
self::assertFalse($outage->is_ongoing($now));
self::assertFalse($outage->has_ended());
$outage = new outage([
'warntime' => $now - 20,
@@ -148,6 +198,7 @@ class outage_test extends basic_testcase {
self::assertSame(outage::STAGE_ONGOING, $outage->get_stage($now));
self::assertTrue($outage->is_active($now));
self::assertTrue($outage->is_ongoing($now));
self::assertFalse($outage->has_ended());
$outage = new outage([
'warntime' => $now - 50,
@@ -158,6 +209,7 @@ class outage_test extends basic_testcase {
self::assertSame(outage::STAGE_STOPPED, $outage->get_stage($now));
self::assertFalse($outage->is_active($now));
self::assertFalse($outage->is_ongoing($now));
self::assertTrue($outage->has_ended());
$outage = new outage([
'warntime' => $now - 50,
@@ -169,6 +221,7 @@ class outage_test extends basic_testcase {
self::assertSame(outage::STAGE_FINISHED, $outage->get_stage($now));
self::assertFalse($outage->is_active($now));
self::assertFalse($outage->is_ongoing($now));
self::assertTrue($outage->has_ended());
$outage = new outage([
'warntime' => $now - 50,
@@ -180,5 +233,38 @@ class outage_test extends basic_testcase {
self::assertSame(outage::STAGE_FINISHED, $outage->get_stage($now));
self::assertFalse($outage->is_active($now));
self::assertFalse($outage->is_ongoing($now));
self::assertTrue($outage->has_ended());
}
public function test_gettitle_getdescription() {
$now = time();
$outage = new outage([
'warntime' => $now - 50,
'starttime' => $now - 40,
'stoptime' => $now - 30,
'finished' => $now - 20,
'title' => 'Title {{start}} {{stop}} {{duration}}',
'description' => 'Description {{start}} {{stop}} {{duration}}',
]);
$title = $outage->get_title();
self::assertNotContains('{', $title);
self::assertNotContains('}', $title);
$description = $outage->get_description();
self::assertNotContains('{', $description);
self::assertNotContains('}', $description);
}
public function test_getdurations() {
$outage = new outage(['starttime' => 1000]);
self::assertNull($outage->get_duration_actual());
$outage->finished = 3000;
self::assertSame(2000, $outage->get_duration_actual());
$outage->stoptime = 3050;
self::assertEquals(2050, $outage->get_duration_planned());
$outage->warntime = 600;
self::assertEquals(400, $outage->get_warning_duration());
}
}

View File

@@ -0,0 +1,152 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
use auth_outage\dml\outagedb;
use auth_outage\local\outage;
use auth_outage\local\outagelib;
defined('MOODLE_INTERNAL') || die();
/**
* Tests performed on outagelib class.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class outagelib_test extends advanced_testcase {
public function test_maintenancemessage() {
$this->resetAfterTest(true);
static::setAdminUser();
$now = time();
$outage = new outage([
'autostart' => true,
'warntime' => $now,
'starttime' => $now + 100,
'stoptime' => $now + 200,
'title' => 'Title',
'description' => 'Description',
]);
set_config('maintenance_message', 'A message.');
outagedb::save($outage);
self::assertFalse((bool)get_config('moodle', 'maintenance_message'));
self::assertCount(2, phpunit_util::get_debugging_messages());
phpunit_util::reset_debugging();
}
public function test_maintenancelater_nonext() {
$this->resetAfterTest(true);
set_config('maintenance_later', time() + (60 * 60 * 24 * 7)); // In 1 week.
self::assertNotEmpty(get_config('moodle', 'maintenance_later'));
outagelib::outages_modified();
self::assertEmpty(get_config('moodle', 'maintenance_later'));
}
public function test_inject() {
global $CFG;
$this->resetAfterTest(true);
self::setAdminUser();
$now = time();
$outage = new outage([
'autostart' => true,
'warntime' => $now,
'starttime' => $now + 100,
'stoptime' => $now + 200,
'title' => 'Title',
'description' => 'Description',
]);
$outage->id = outagedb::save($outage);
self::assertEmpty($CFG->additionalhtmltopofbody);
outagelib::reinject();
self::assertContains('<style>', $CFG->additionalhtmltopofbody);
self::assertContains('<script>', $CFG->additionalhtmltopofbody);
// Should not inject again.
$size = strlen($CFG->additionalhtmltopofbody);
outagelib::inject();
self::assertSame($size, strlen($CFG->additionalhtmltopofbody));
}
public function test_inject_broken() {
global $CFG;
$_GET = ['auth_outage_break_code' => '1'];
outagelib::reinject();
self::assertCount(2, phpunit_util::get_debugging_messages());
phpunit_util::reset_debugging();
}
public function test_inject_preview() {
global $CFG;
$this->resetAfterTest(true);
self::setAdminUser();
$now = time();
$outage = new outage([
'autostart' => true,
'warntime' => $now,
'starttime' => $now + 100,
'stoptime' => $now + 200,
'title' => 'Title',
'description' => 'Description',
]);
$outage->id = outagedb::save($outage);
self::assertEmpty($CFG->additionalhtmltopofbody);
$_GET = ['auth_outage_preview' => (string)$outage->id];
outagelib::reinject();
self::assertContains('<style>', $CFG->additionalhtmltopofbody);
self::assertContains('<script>', $CFG->additionalhtmltopofbody);
}
public function test_inject_preview_notfound() {
global $CFG;
self::assertEmpty($CFG->additionalhtmltopofbody);
$_GET = ['auth_outage_preview' => '1'];
// Should not throw exception or halt anything, silently ignore it.
outagelib::reinject();
self::assertEmpty($CFG->additionalhtmltopofbody);
}
public function test_inject_preview_withdelta() {
global $CFG;
$this->resetAfterTest(true);
self::setAdminUser();
$now = time();
$outage = new outage([
'autostart' => true,
'warntime' => $now,
'starttime' => $now + 100,
'stoptime' => $now + 200,
'title' => 'Title',
'description' => 'Description',
]);
$outage->id = outagedb::save($outage);
self::assertEmpty($CFG->additionalhtmltopofbody);
$_GET = ['auth_outage_preview' => (string)$outage->id, 'auth_outage_delta' => '500'];
outagelib::reinject();
// Still empty, delta is too high (outage ended).
self::assertEmpty($CFG->additionalhtmltopofbody);
}
public function test_inject_noactive() {
outagelib::reinject();
}
}

View File

@@ -1,50 +0,0 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
use auth_outage\dml\outagedb;
use auth_outage\local\outage;
defined('MOODLE_INTERNAL') || die();
/**
* Tests performed on outagelib class.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class outagelib_test extends advanced_testcase {
public function test_maintenancemessage() {
global $CFG;
$this->resetAfterTest(true);
static::setAdminUser();
$now = time();
$outage = new outage([
'autostart' => true,
'warntime' => $now,
'starttime' => $now + 100,
'stoptime' => $now + 200,
'title' => 'Title',
'description' => 'Description',
]);
set_config('maintenance_message', 'A message.');
outagedb::save($outage);
self::assertFalse((bool)get_config('moodle', 'maintenance_message'));
}
}