Compare commits

..
Author SHA1 Message Date
Sarah Cotton b5f02de0c5 Merge pull request #414 from sarahjcotton/WR489688-security-fix-t19
WR489688: General fix #30
2026-07-14 14:09:03 +01:00
Sarah Cotton ff63eb4233 WR489688: ci fixes 2026-06-25 15:30:40 +01:00
Sarah Cotton 7b2106555b WR489688: Security fix #30 2026-06-25 15:12:58 +01:00
Dan Marsden f45918a277 Merge pull request #371 from SashaAnastasi/TOTARA_19-fixes
Totara 19 fixes
2025-03-04 16:17:49 +13:00
Sasha Anastasi 478d0fc5c1 Run class in separate process ensuring tearDown method gets called 2025-03-04 08:18:47 +13:00
Sasha Anastasi ca4a202752 Remove redundant use statements with non-compound name 2025-03-04 08:17:04 +13:00
Sasha Anastasi 25b9e553a5 update test fixture path 2025-02-27 15:33:10 +13:00
Sasha Anastasi 5cc215d819 add tearDown method to base runtest 2025-02-27 15:32:25 +13:00
Dan Marsden e8c00be2dd Merge pull request #370 from SashaAnastasi/TOTARA_19-t19_fixes
[T19] Fixes for totara19
2025-02-27 14:27:17 +13:00
Sasha Anastasi ca213e4c04 flatten test directory structure 2025-02-27 10:17:25 +13:00
Sasha Anastasi eac1575f51 use imports instead of namespaces 2025-02-27 09:09:06 +13:00
Sasha Anastasi cff0f3fbe3 update test classes 2025-02-27 08:59:31 +13:00
Sasha Anastasi ffb9e27456 add tearDown method to runtests 2025-02-26 16:40:41 +13:00
Dan Marsden f842e2baa4 Merge pull request #366 from SashaAnastasi/TOTARA_19-phpunit10
outagelib: adjust exception trigger for phpunit10
2025-02-20 14:46:57 +13:00
Sasha Anastasi f3d91cb044 remove superfluous conditional 2025-02-20 14:36:26 +13:00
Sasha Anastasi a63ffed1bc outagelib: adjust exception trigger for phpunit10 2025-02-20 12:33:01 +13:00
Dan Marsden c99d8f12a0 Merge pull request #364 from catalyst/fix-t19-tests
[T19] Replace deprecated testcase classes with core_phpunit
2025-02-19 10:55:09 +13:00
Sasha Anastasi d3b5ed73aa Replace deprecated testcase classes with core_phpunit 2025-02-18 15:12:09 +13:00
49 changed files with 481 additions and 379 deletions
+2 -1
View File
@@ -98,8 +98,9 @@ Execute the commands from your Moodle instalation directory and leave the browse
1) Create an outage
This command will create an outage starting in 30 seconds, with a warning period of 15 seconds.
It will automatically start (trigger maintenance mode).
`php auth/outage/cli/create.php -w=20 -s=30`
`php auth/outage/cli/create.php -w=20 -s=30 --autostart=Y`
Refresh the page but you will not see anything yet.
+8 -12
View File
@@ -40,19 +40,14 @@ If you have an older version of Moodle you can still make it work but you will
need to manually add one extra plugin, please check:
* https://github.com/catalyst/moodle-local_outage
Moodle supported branches
Branches
--------
| Version | Branch | PHP |
|-------------|-------------------|------|
| Moodle 3.9+ | MOODLE_39_STABLE | 7.2+ |
Totara supported branches
--------
| Version | Branch | PHP |
| --------------- | ---------------- | ---- |
| Totara 19 | TOTARA_19 | 7.2+ |
| Totara 13-18 | MOODLE_39_STABLE | 7.2+ |
| Moodle version | Totara | Branch | PHP |
| ------------------ | --------------- | ---------------- | ---- |
| Moodle 3.9+ | Totara 13+ | MOODLE_39_STABLE | 7.2+ |
| Moodle 3.3 to 3.8 | Totara 11 to 12 | MOODLE_38_STABLE | 7.1+ |
| Moodle 2.7 to 3.2 | | MOODLE_32_STABLE | 5.5+ |
| | Totara up to 10 | TOTARA_10 | 5.5+ |
Screenshots
-----------
@@ -167,6 +162,7 @@ Creates a new outage.
-h, --help shows parameters help.
-c, --clone clone another outage except for the start time.
-a, --autostart must be Y or N, sets if the outage automatically triggers maintenance mode.
-w, --warn how many seconds before it starts to display a warning.
-s, --start in how many seconds should this outage start or unix time to start outage. Required.
-d, --duration how many seconds should the outage last.
+2 -3
View File
@@ -29,10 +29,9 @@
*
* @var stdClass $CFG
*/
define('MOODLE_INTERNAL', true);
defined('MOODLE_INTERNAL') || die();
defined('MOODLE_INTERNAL') || die(); // Make sniffer happy.
//
// We need the CFG->dataroot, if not set yet this script is called too early in config.php file.
if (!isset($CFG->dataroot)) {
+31
View File
@@ -342,6 +342,37 @@ class outagedb {
return (count($data) == 0) ? null : new outage(array_shift($data));
}
/**
* Gets the next outage which has not started yet and has the autostart flag set to true.
* @param null $time Timestamp reference for current time.
* @return outage|null The outage or null if not found.
* @throws coding_exception
*/
public static function get_next_autostarting($time = null) {
global $DB;
if ($time === null) {
$time = time();
}
if (!is_int($time) || ($time <= 0)) {
throw new coding_exception('$time must be null or a positive int.', $time);
}
$data = $DB->get_records_select(
'auth_outage',
'(:datetime <= starttime) AND (autostart = 1)',
['datetime' => $time],
'starttime ASC',
'*',
0,
1
);
// Not using $DB->get_record_select instead because there is no 'limit' parameter.
// Allowing multiple records still raises an internal error.
return (count($data) == 0) ? null : new outage(array_shift($data));
}
/**
* Gets an ongoing outage (between start and stop time but not finished).
* @param int|null $time Timestamp considered to check for outages, null for current date/time.
+5 -6
View File
@@ -48,6 +48,9 @@ class edit extends moodleform {
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$mform->addElement('checkbox', 'autostart', get_string('autostart', 'auth_outage'));
$mform->addHelpButton('autostart', 'autostart', 'auth_outage');
$mform->addElement('duration', 'warningduration', get_string('warningduration', 'auth_outage'));
$mform->addHelpButton('warningduration', 'warningduration', 'auth_outage');
@@ -85,10 +88,6 @@ class edit extends moodleform {
$mform->disabledIf('accesskey', 'useaccesskey');
$mform->addHelpButton('accesskey', 'accesskey', 'auth_outage');
$mform->addElement('text', 'metadata', get_string('metadata', 'auth_outage'));
$mform->setType('metadata', PARAM_TEXT);
$mform->addHelpButton('metadata', 'metadata', 'auth_outage');
$this->add_action_buttons();
}
@@ -136,13 +135,13 @@ class edit extends moodleform {
}
$outagedata = [
'id' => ($data->id === 0) ? null : $data->id,
'autostart' => (isset($data->autostart) && ($data->autostart == 1)),
'starttime' => $data->starttime,
'stoptime' => $data->starttime + $data->outageduration,
'warntime' => $data->starttime - $data->warningduration,
'title' => $data->title,
'description' => $data->description['text'],
'accesskey' => $data->useaccesskey ? $data->accesskey : null,
'metadata' => $data->metadata ?? null,
];
return new outage($outagedata);
}
@@ -160,6 +159,7 @@ class edit extends moodleform {
if ($outage instanceof outage) {
$this->_form->setDefaults([
'id' => $outage->id,
'autostart' => $outage->autostart,
'starttime' => $outage->starttime,
'outageduration' => $outage->get_duration_planned(),
'warningduration' => $outage->get_warning_duration(),
@@ -167,7 +167,6 @@ class edit extends moodleform {
'description' => ['text' => $outage->description, 'format' => '1'],
'accesskey' => $outage->accesskey,
'useaccesskey' => !empty($outage->accesskey),
'metadata' => $outage->metadata,
]);
// If the default_autostart is configured in config, then force autostart to be the default value.
-7
View File
@@ -34,13 +34,6 @@ class hook_callbacks {
* @param before_standard_top_of_body_html_generation $hook
*/
public static function before_standard_top_of_body_html_generation(before_standard_top_of_body_html_generation $hook): void {
global $CFG;
if (during_initial_install() || isset($CFG->upgraderunning)) {
// Do nothing during installation or upgrade.
return;
}
// Get code to inject.
$hook->add_html(outagelib::get_inject_code());
}
+10 -3
View File
@@ -28,8 +28,7 @@ use coding_exception;
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class create extends clibase
{
class create extends clibase {
/**
* @var mixed[] Defaults to use if given option is null.
*/
@@ -44,6 +43,7 @@ class create extends clibase
return [
'help' => false,
'clone' => null,
'autostart' => null,
'warn' => null,
'start' => null,
'duration' => null,
@@ -60,6 +60,7 @@ class create extends clibase
*/
public function generate_shortcuts() {
return [
'a' => 'autostart',
'b' => 'block',
'c' => 'clone',
'd' => 'duration',
@@ -124,7 +125,7 @@ class create extends clibase
$options = $this->merge_options();
$id = $this->create_outage($options);
if ($options['block'] && !$options['onlyid']) {
if ($options['block']) {
$block = new waitforit(['outageid' => $id]);
$block->execute();
}
@@ -164,6 +165,7 @@ class create extends clibase
// an outage 47 years in advance.
$start = $options['start'] > 1500000000 ? $options['start'] : $this->time + $options['start'];
$outage = new outage([
'autostart' => $options['autostart'],
'warntime' => $start - $options['warn'],
'starttime' => $start,
'stoptime' => $start + $options['duration'],
@@ -197,6 +199,7 @@ class create extends clibase
$outage = outagedb::get_by_id((int)$id);
$this->set_defaults([
'autostart' => $outage->autostart,
'warn' => $outage->get_warning_duration(),
'duration' => $outage->get_duration_planned(),
'title' => $outage->title,
@@ -219,6 +222,10 @@ class create extends clibase
$options[$param] = $this->merge_options_check_parameters_string_nonempty($options[$param], $param);
}
foreach (['autostart'] as $param) {
$options[$param] = $this->merge_options_check_parameters_bool($options[$param], $param);
}
return $options;
}
+2 -30
View File
@@ -53,14 +53,10 @@ class infopage {
$CFG->svgicons = true;
if (is_null($params)) {
$id = optional_param('id', null, PARAM_INT);
$params = [
'id' => $id,
'id' => optional_param('id', null, PARAM_INT),
'outage' => null,
'static' => !is_null($id) && hash_equals(
self::statickey($id),
optional_param('statickey', '', PARAM_ALPHANUM)
),
'static' => optional_param('static', false, PARAM_BOOL),
];
} else {
$defaults = [
@@ -113,13 +109,6 @@ class infopage {
// No hooks injecting into this page, do it manually.
echo outagelib::get_inject_code();
// Inject metadata into the header before output.
if (!empty($this->outage->metadata)) {
header('X-Outage-Metadata: ' . $this->outage->metadata);
header('X-Outage-StartTime: ' . $this->outage->starttime);
header('X-Outage-EndTime: ' . $this->outage->stoptime);
}
echo $OUTPUT->header();
$viewbag = [
'admin' => is_siteadmin(),
@@ -156,21 +145,4 @@ class infopage {
$this->outage = $params['outage'];
$this->static = $params['static'];
}
/**
* Computes the secret token that proves a request to view an outage's static
* rendering came from this plugin's own static-page generator, not an external
* client forging the request. Used to gate the 'static' flag (see constructor).
*
* @param int $outageid
* @return string
*/
public static function statickey($outageid) {
$secret = get_config('auth_outage', 'staticsecret');
if (empty($secret)) {
$secret = random_string(64);
set_config('staticsecret', $secret, 'auth_outage');
}
return hash_hmac('sha256', (string)$outageid, $secret);
}
}
@@ -49,15 +49,8 @@ class maintenance_static_page {
} else if (PHPUNIT_TEST || defined('BEHAT_SITE_RUNNING')) {
$html = '<html></html>';
} else {
// Inject metadata into the header before output.
if (!empty($outage->metadata)) {
header('X-Outage-Metadata: ' . $outage->metadata);
header('X-Outage-StartTime: ' . $outage->starttime);
header('X-Outage-EndTime: ' . $outage->stoptime);
}
$data = maintenance_static_page_io::file_get_data(
$CFG->wwwroot . '/auth/outage/info.php?auth_outage_hide_warning=1&id=' . $outage->id
. '&statickey=' . infopage::statickey($outage->id)
$CFG->wwwroot . '/auth/outage/info.php?auth_outage_hide_warning=1&static=1&id=' . $outage->id
);
$html = $data['contents'];
}
+8 -5
View File
@@ -58,6 +58,11 @@ class outage {
*/
public $id = null;
/**
* @var bool|null Maintenance mode auto start flag.
*/
public $autostart = null;
/**
* @var int|null Start Time timestamp.
*/
@@ -108,11 +113,6 @@ class outage {
*/
public $accesskey = null;
/**
* @var string|null metadata string, or null if not enabled.
*/
public $metadata = null;
/**
* outage constructor.
* @param stdClass|array|null $data The data for the outage.
@@ -289,6 +289,9 @@ class outage {
foreach ($fs as $f) {
$this->$f = ($this->$f === null) ? null : (int)$this->$f;
}
// Adjust bool fields.
$this->autostart = ($this->autostart === null) ? null : (bool)$this->autostart;
}
/**
+33 -15
View File
@@ -56,7 +56,7 @@ class outagelib {
global $CFG;
require_once($CFG->libdir . '/filelib.php');
$curl = new curl();
$curl = new curl(['ignoresecurity' => true]);
$contents = $curl->get($file);
$info = $curl->get_info();
if (!empty($info['content_type'])) {
@@ -173,6 +173,7 @@ class outagelib {
'allowedips' => '',
'css' => '',
'default_time' => '',
'default_autostart' => '0',
'default_duration' => (string)(60 * 60),
'default_warning_duration' => (string)(60 * 60),
'default_title' => get_string('defaulttitlevalue', 'auth_outage'),
@@ -193,6 +194,9 @@ class outagelib {
$outage = outagedb::get_ongoing();
if (is_null($outage)) {
$outage = outagedb::get_next_starting();
$ongoingoutage = false;
} else {
$ongoingoutage = true;
}
// Set json formatted outage string to cache.
@@ -200,6 +204,9 @@ class outagelib {
maintenance_static_page::create_from_outage($outage)->generate();
self::update_climaintenance_code($outage);
if (!$ongoingoutage || $reenablemaint || is_null($outage)) {
self::update_maintenance_later($outage);
}
}
/**
@@ -211,6 +218,25 @@ class outagelib {
return (!empty($result['contents']));
}
/**
* Calls Moodle API - set_maintenance_later() to set when the next outage starts.
* @param outage|null $outage Outage or null if no scheduled outage.
*/
private static function update_maintenance_later($outage) {
if (is_null($outage) || !$outage->autostart) {
unset_config('maintenance_later');
} else {
$message = get_config('moodle', 'maintenance_message');
if ($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');
}
set_config('maintenance_later', $outage->starttime);
self::maintenance_config_log($outage);
}
}
/**
* Checks if we should try to inject an warning bar.
@@ -253,12 +279,11 @@ class outagelib {
* @param int $stoptime Outage stop time.
* @param string $allowedips List of IPs allowed.
* @param string|null $accesskey access key, or null if no access key set.
* @param string|null $metadata metadata to be added to the outage headers, or null if none.
*
* @return string
* @throws invalid_parameter_exception
*/
public static function create_climaintenancephp_code($starttime, $stoptime, $allowedips, $accesskey = null, $metadata = null) {
public static function create_climaintenancephp_code($starttime, $stoptime, $allowedips, $accesskey = null) {
global $CFG;
if (!is_int($starttime) || !is_int($stoptime)) {
throw new invalid_parameter_exception('Make sure $startime and $stoptime are integers.');
@@ -316,11 +341,6 @@ if ((time() >= {{STARTTIME}}) && (time() < {{STOPTIME}})) {
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Accept-Ranges: none');
header('X-Moodle-Maintenance: manager');
if (!empty({{METADATA}})) {
header('X-Outage-Metadata: ' . {{METADATA}});
}
header('X-Outage-StartTime: ' . '{{STARTTIME}}');
header('X-Outage-EndTime: ' . '{{STOPTIME}}');
}
if (!$isphpunit && ((defined('AJAX_SCRIPT') && AJAX_SCRIPT) || (defined('WS_SERVER') && WS_SERVER))) {
@@ -328,11 +348,11 @@ if ((time() >= {{STARTTIME}}) && (time() < {{STOPTIME}})) {
}
if ({{USEALLOWEDIPS}} && $ipblocked) {
echo '<!-- auth_outage blocked your ip: '.getremoteaddr('n/a').' -->';
echo '<!-- Blocked by ip, your ip: '.getremoteaddr('n/a').' -->';
}
if ({{USEACCESSKEY}} && $accesskeyblocked) {
echo '<!-- auth_outage blocked by missing or incorrect access key, access key given: '. $useraccesskey .' -->';
echo '<!-- Blocked by missing or incorrect access key, access key given: '. $useraccesskey .' -->';
}
if (!$isphpunit) {
@@ -347,11 +367,10 @@ if ((time() >= {{STARTTIME}}) && (time() < {{STOPTIME}})) {
}
EOT;
$search = ['{{STARTTIME}}', '{{STOPTIME}}', '{{USEALLOWEDIPS}}', '{{ALLOWEDIPS}}', '{{USEACCESSKEY}}', '{{ACCESSKEY}}',
'{{YOURIP}}', '{{COOKIESECURE}}', '{{COOKIEHTTPONLY}}', '{{METADATA}}'];
'{{YOURIP}}', '{{COOKIESECURE}}', '{{COOKIEHTTPONLY}}'];
// Note that var_export is required because (string) false == '', not 'false'.
$replace = [$starttime, $stoptime, var_export(!empty($allowedips), true), $allowedips, var_export(!empty($accesskey), true),
$accesskey, getremoteaddr('n/a'), var_export($cookiesecure, true),
var_export($cookiehttponly, true), var_export($metadata, true)];
$accesskey, getremoteaddr('n/a'), var_export($cookiesecure, true), var_export($cookiehttponly, true)];
return str_replace($search, $replace, $code);
}
@@ -374,7 +393,6 @@ EOT;
$config = self::get_config();
$allowedips = trim($config->allowedips);
$accesskey = $outage->accesskey ?? null;
$metadata = $outage->metadata ?? null;
// If no outage, or allowed ips is null and access key is null (i.e. no blocking required).
if (is_null($outage) || ($allowedips == '' && empty($accesskey))) {
@@ -382,7 +400,7 @@ EOT;
unlink($file);
}
} else {
$code = self::create_climaintenancephp_code($outage->starttime, $outage->stoptime, $allowedips, $accesskey, $metadata);
$code = self::create_climaintenancephp_code($outage->starttime, $outage->stoptime, $allowedips, $accesskey);
$dir = dirname($file);
if (!file_exists($dir) || !is_dir($dir)) {
-35
View File
@@ -64,41 +64,6 @@ class base_table extends flexible_table {
$this->set_attribute('class', 'generaltable admintable');
}
/**
* Displays a user by their fullname with a link to a profile.
* @param int $userid
* @return string HTML link to user profile
*/
private function format_user(int $userid): string {
if ($userid == 0 || !$user = \core_user::get_user($userid)) {
return get_string('na', 'auth_outage');
}
$url = new moodle_url('/user/profile.php', ['id' => $userid]);
return html_writer::link($url, fullname($user));
}
/**
* Formats created by column.
* @param outage $outage
* @return string The user who created the outage.
*/
protected function format_created(outage $outage): string {
return $this->format_user($outage->createdby);
}
/**
* Formats modified by column.
* @param outage $outage
* @return string The user who last modiifed the outage and the last modified time.
*/
protected function format_modified(outage $outage): string {
$timestamp = html_writer::div(
userdate($outage->lastmodified, get_string('datetimeformat', 'auth_outage')),
'small text-muted'
);
return $this->format_user($outage->modifiedby) . $timestamp;
}
/**
* Create the action buttons HTML code for a specific outage.
* @param outage $outage The outage to generate the buttons.
+1 -5
View File
@@ -36,7 +36,7 @@ class history_table extends base_table {
public function __construct() {
parent::__construct();
$this->define_columns(['warning', 'starts', 'duration', 'durationactual', 'title', 'created', 'modified', 'actions']);
$this->define_columns(['warning', 'starts', 'durationplanned', 'durationactual', 'title', 'actions']);
$this->define_headers([
get_string('tableheaderwarnbefore', 'auth_outage'),
@@ -44,8 +44,6 @@ class history_table extends base_table {
get_string('tableheaderdurationplanned', 'auth_outage'),
get_string('tableheaderdurationactual', 'auth_outage'),
get_string('tableheadertitle', 'auth_outage'),
get_string('tableheadercreatedby', 'auth_outage'),
get_string('tableheadermodifiedby', 'auth_outage'),
get_string('actions'),
]);
@@ -66,8 +64,6 @@ class history_table extends base_table {
format_time($outage->get_duration_planned()),
$finished,
$outage->get_title(),
$this->format_created($outage),
$this->format_modified($outage),
$this->create_data_buttons($outage, false),
]);
}
+1 -5
View File
@@ -38,15 +38,13 @@ class planned_table extends base_table {
public function __construct() {
parent::__construct();
$this->define_columns(['warning', 'starts', 'duration', 'title', 'created', 'modified', 'actions']);
$this->define_columns(['warning', 'starts', 'duration', 'title', 'actions']);
$this->define_headers([
get_string('tableheaderwarnbefore', 'auth_outage'),
get_string('tableheaderstarttime', 'auth_outage'),
get_string('tableheaderduration', 'auth_outage'),
get_string('tableheadertitle', 'auth_outage'),
get_string('tableheadercreatedby', 'auth_outage'),
get_string('tableheadermodifiedby', 'auth_outage'),
get_string('actions'),
]);
@@ -70,8 +68,6 @@ class planned_table extends base_table {
self::create_starttime_string($outage->starttime),
format_time($outage->get_duration_planned()),
$title,
$this->format_created($outage),
$this->format_modified($outage),
$this->create_data_buttons($outage, true),
]);
}
+2 -2
View File
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="auth/outage/db" VERSION="20260120" COMMENT="XMLDB file for Moodle auth/outage"
<XMLDB PATH="auth/outage/db" VERSION="20160922" COMMENT="XMLDB file for Moodle auth/outage"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
@@ -7,6 +7,7 @@
<TABLE NAME="auth_outage" COMMENT="Table used for auth/outage plugin. Holds information about all past, current and future outages.">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="autostart" TYPE="int" LENGTH="1" NOTNULL="true" SEQUENCE="false" COMMENT="If the maintenance mode should be automatically triggered once this outage startes."/>
<FIELD NAME="warntime" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="When the outage will start showing a warning for that outage."/>
<FIELD NAME="starttime" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="When outage starts."/>
<FIELD NAME="stoptime" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="When outage ends."/>
@@ -17,7 +18,6 @@
<FIELD NAME="lastmodified" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="When was this entry last modified."/>
<FIELD NAME="finished" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="Timestamp of when the outage really finished."/>
<FIELD NAME="accesskey" TYPE="char" LENGTH="16" NOTNULL="false" SEQUENCE="false" COMMENT="Unique key used to access during outage"/>
<FIELD NAME="metadata" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="metadata to be exposed in outage page headers"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
-31
View File
@@ -61,36 +61,5 @@ function xmldb_auth_outage_upgrade($oldversion) {
upgrade_plugin_savepoint(true, 2024081900, 'auth', 'outage');
}
if ($oldversion < 2024081901) {
// Define field metadata to be added to auth_outage.
$table = new xmldb_table('auth_outage');
$field = new xmldb_field('metadata', XMLDB_TYPE_TEXT, null, null, null, null, null, 'accesskey');
// Conditionally launch add field metadata.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Outage savepoint reached.
upgrade_plugin_savepoint(true, 2024081901, 'auth', 'outage');
}
if ($oldversion < 2024081902) {
// Getting the table auth_outage and target field to remove from the table.
$table = new xmldb_table('auth_outage');
$field = new xmldb_field('autostart');
// Conditionally launch drop field autostart.
if ($dbman->field_exists($table, $field)) {
$dbman->drop_field($table, $field);
}
// Removing the default_autostart config as it is no longer used.
unset_config('default_autostart', 'auth_outage');
// Outage savepoint reached.
upgrade_plugin_savepoint(true, 2024081902, 'auth', 'outage');
}
return true;
}
+3 -20
View File
@@ -27,7 +27,7 @@ use auth_outage\dml\outagedb;
use auth_outage\form\outage\edit;
use auth_outage\local\outage;
use auth_outage\local\outagelib;
use auth_outage\output\renderer;
require_once(__DIR__ . '/../../config.php');
require_once($CFG->libdir . '/adminlib.php');
@@ -48,24 +48,7 @@ if ($mform->is_cancelled()) {
$clone = optional_param('clone', 0, PARAM_INT);
$edit = optional_param('edit', 0, PARAM_INT);
if (array_key_exists('starttime', $_POST) && is_array($_POST['starttime'])) {
$start = optional_param_array('starttime', [], PARAM_INT);
} else {
$start = optional_param('starttime', 0, PARAM_INT);
}
if (!empty($start['year']) && !empty($start['month']) && !empty($start['day'])) {
$hour = $start['hour'] ?? 0;
$minute = $start['minute'] ?? 0;
$time = make_timestamp(
(int) $start['year'],
(int) $start['month'],
(int) $start['day'],
(int) $hour,
(int) $minute
);
} else {
$time = 0;
}
$time = optional_param('starttime', 0, PARAM_INT);
if ($clone && $edit) {
throw new invalid_parameter_exception('Cannot provide both clone and edit ids.');
}
@@ -84,12 +67,12 @@ if ($clone) {
}
$outage = new outage([
'autostart' => $config->default_autostart,
'starttime' => $time,
'stoptime' => $time + $config->default_duration,
'warntime' => $time - $config->default_warning_duration,
'title' => $config->default_title,
'description' => $config->default_description,
'metadata' => $config->default_metadata,
]);
$action = 'outagecreate';
}
-5
View File
@@ -69,11 +69,6 @@ function auth_outage_bootstrap_callback() {
http_response_code(404);
die('File not found.');
}
// Ensure there is nothing in the output buffer.
// otherwise the file will not be read correctly.
ob_clean();
readfile($file);
exit(0);
}
-6
View File
@@ -85,8 +85,6 @@ $string['defaultdescriptiondescription'] = 'Default warning message for outages.
$string['defaultdescriptionvalue'] = 'There is maintenance scheduled from {{start}} to {{stop}} and our system will not be available during that time.';
$string['defaultlayoutcss'] = 'Layout CSS';
$string['defaultlayoutcssdescription'] = 'This CSS code can be used to override the Outage Warning Bar CSS.';
$string['defaultmetadata'] = 'Default metadata';
$string['defaultmetadatadescription'] = 'The default metadata to include in the outage settings, to be used as a template for new outages. E.g. JIRA-{ticketno}.';
$string['defaultoutageautostart'] = 'Outage auto start';
$string['defaultoutageautostartdescription'] = 'If the outage should automatically trigger maintenance mode once it starts, locking down the whole site.';
$string['defaultoutageduration'] = 'Outage duration';
@@ -119,8 +117,6 @@ $string['messageoutagebackonline'] = 'We are back online!';
$string['messageoutagebackonlinedescription'] = 'You may resume browsing safely.';
$string['messageoutageongoing'] = 'Back online at {$a->stop}.';
$string['messageoutagewarning'] = 'Shutting down in {{countdown}}';
$string['metadata'] = 'Outage metadata';
$string['metadata_help'] = 'Data here will be output in the outage page as a meta tag in the header of the outage page.';
$string['na'] = 'n/a';
$string['notfound'] = 'No outages found.';
$string['outage:updatenotify'] = '';
@@ -149,11 +145,9 @@ $string['settingssectionplugin'] = 'Plugin Configuration';
$string['settingssectionplugindescription'] = 'General outage management plugin settings.';
$string['starttime'] = 'Start date and time';
$string['starttime_help'] = 'At which date and time the outage starts, preventing general access to the system.';
$string['tableheadercreatedby'] = 'Created by';
$string['tableheaderduration'] = 'Duration';
$string['tableheaderdurationactual'] = 'Actual duration';
$string['tableheaderdurationplanned'] = 'Planned duration';
$string['tableheadermodifiedby'] = 'Last modified by';
$string['tableheaderstartedtime'] = 'Started on';
$string['tableheaderstarttime'] = 'Starts on';
$string['tableheadertitle'] = 'Title';
-4
View File
@@ -30,11 +30,7 @@ use auth_outage\local\controllers\maintenance_static_page;
// @codingStandardsIgnoreStart
require_once(__DIR__.'/../../config.php');
require_once($CFG->libdir . '/adminlib.php');
// @codingStandardsIgnoreEnd
admin_externalpage_setup('auth_outage_manage');
$id = optional_param('id', null, PARAM_INT);
$outage = is_null($id) ? outagedb::get_next_starting() : outagedb::get_by_id($id);
if (is_null($outage)) {
+7 -10
View File
@@ -43,6 +43,13 @@ if ($hassiteconfig) {
get_string('settingssectiondefaultsdescription', 'auth_outage') . $description
));
$settings->add(new admin_setting_configcheckbox(
'auth_outage/default_autostart',
get_string('defaultoutageautostart', 'auth_outage'),
get_string('defaultoutageautostartdescription', 'auth_outage'),
$defaults['default_autostart']
));
$settings->add(new admin_setting_configduration(
'auth_outage/default_warning_duration',
get_string('defaultwarningduration', 'auth_outage'),
@@ -71,16 +78,6 @@ if ($hassiteconfig) {
$defaults['default_title'],
PARAM_TEXT
));
$settings->add(
new admin_setting_configtext(
'auth_outage/default_metadata',
get_string('defaultmetadata', 'auth_outage'),
get_string('defaultmetadatadescription', 'auth_outage'),
'',
PARAM_TEXT
)
);
$settings->add(new admin_setting_configtextarea(
'auth_outage/default_description',
get_string('defaultdescription', 'auth_outage'),
+3 -3
View File
@@ -14,8 +14,6 @@
// 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;
use auth_outage\dml\outagedb;
use auth_outage\local\outage;
@@ -35,7 +33,7 @@ use auth_outage\local\outage;
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class base_testcase extends \advanced_testcase {
abstract class base_testcase extends \core_phpunit\testcase {
/**
* Checks PHPUnit version and calls the functions accordingly.
* @param string $exception Expected exception class.
@@ -79,6 +77,7 @@ abstract class base_testcase extends \advanced_testcase {
return new outage([
'id' => 1,
'autostart' => false,
'warntime' => $now - 100,
'starttime' => $now + 100,
'stoptime' => $now + 200,
@@ -106,5 +105,6 @@ abstract class base_testcase extends \advanced_testcase {
foreach (outagedb::get_all() as $i => $outage) {
$DB->delete_records('auth_outage', ['id' => $outage->id]);
}
parent::tearDown();
}
}
+7 -2
View File
@@ -88,6 +88,7 @@ class behat_auth_outage extends behat_base {
*/
public function there_is_a_outage($type) {
$data = [
'autostart' => false,
'finished' => null,
'title' => 'Example of ' . $type . ' outage',
'description' => 'An outage: ' . $type,
@@ -229,8 +230,7 @@ class behat_auth_outage extends behat_base {
$found = $this->find_all('xpath', $xpath, false, $container);
if (count($found) == 0) {
throw new ExpectationException('"' . $text . '" text was not found in the "' . $element .
'" element', $this->getSession());
throw new ExpectationException('"' . $text . '" text was not found in the "' . $element . '" element', $this->getSession());
}
foreach ($found as $node) {
@@ -271,6 +271,7 @@ class behat_auth_outage extends behat_base {
// Set defaults.
$row = array_merge(
[
'autostart' => 'no',
'warnbefore' => 60,
'startsin' => 0,
'stopsafter' => 60,
@@ -280,12 +281,16 @@ class behat_auth_outage extends behat_base {
],
$row
);
if (($row['autostart'] != 'yes') && ($row['autostart'] != 'no')) {
throw new Exception('autostart must be yes or no, found: ' . $row['autostart']);
}
if ($row['finished'] == '') {
$row['finished'] = null;
}
$starttime = $time + $row['startsin'];
$this->outage = new outage([
'autostart' => ($row['autostart'] == 'yes'),
'warntime' => $starttime - $row['warnbefore'],
'starttime' => $starttime,
'stoptime' => $starttime + $row['stopsafter'],
+5 -3
View File
@@ -14,6 +14,7 @@ Feature: Change the default settings
Scenario Outline: Check if I can save the default settings.
When I navigate to "Plugins > Authentication > Outage manager > Settings" in site administration
And I set the following fields to these values:
| s_auth_outage_default_autostart | <autostart> |
| s_auth_outage_default_warning_duration[v] | <warning> |
| s_auth_outage_default_warning_duration[u] | 60 |
| s_auth_outage_default_duration[v] | <duration> |
@@ -25,6 +26,7 @@ Feature: Change the default settings
Then I should see "Changes saved"
When I visit the Create Outage Page
Then the following fields match these values:
| autostart | <autostart> |
| warningduration[number] | <warning> |
| warningduration[timeunit] | 60 |
| outageduration[number] | <duration> |
@@ -33,6 +35,6 @@ Feature: Change the default settings
| description[text] | <description> |
Examples:
| warning | duration | title | description | css |
| 15 | 30 | An Outage | My outage until {stop}. | /* Some CSS. */ |
| 30 | 45 | My Behat Outage {start} | My outage with <b>HTML</b>. | /* More CSS. */ |
| autostart | warning | duration | title | description | css |
| 1 | 15 | 30 | An Outage | My outage until {stop}. | /* Some CSS. */ |
| 0 | 30 | 45 | My Behat Outage {start} | My outage with <b>HTML</b>. | /* More CSS. */ |
@@ -23,9 +23,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace auth_outage\calendar;
use auth_outage\local\outage;
use auth_outage\calendar\calendar;
/**
* calendar_test test class.
@@ -39,7 +38,7 @@ use auth_outage\local\outage;
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\calendar\calendar
*/
final class calendar_test extends \advanced_testcase {
final class calendar_test extends \core_phpunit\testcase {
/**
* @var outage|null The calendar entry owner.
*/
@@ -55,6 +54,7 @@ final class calendar_test extends \advanced_testcase {
$time = time();
self::$outage = new outage([
'id' => 1,
'autostart' => false,
'warntime' => $time - 100,
'starttime' => $time,
'stoptime' => $time + (2 * 60 * 60),
@@ -75,6 +75,7 @@ final class calendar_test extends \advanced_testcase {
$time = time();
self::$outage = new outage([
'id' => 1,
'autostart' => false,
'warntime' => $time - 100,
'starttime' => $time,
'stoptime' => $time + (2 * 60 * 60),
@@ -98,6 +99,7 @@ final class calendar_test extends \advanced_testcase {
$time = time();
self::$outage = new outage([
'id' => 1,
'autostart' => false,
'warntime' => $time - 100,
'starttime' => $time,
'stoptime' => $time + (2 * 60 * 60),
@@ -122,6 +124,7 @@ final class calendar_test extends \advanced_testcase {
$time = time();
$outage = new outage([
'id' => 1,
'autostart' => false,
'warntime' => $time - 100,
'starttime' => $time,
'stoptime' => $time + (2 * 60 * 60),
@@ -23,10 +23,10 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace auth_outage\local\cli;
use auth_outage\dml\outagedb;
use auth_outage\local\outage;
use auth_outage\local\cli\create;
use auth_outage\local\cli\cli_exception;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/cli_testcase.php');
@@ -40,7 +40,11 @@ require_once(__DIR__ . '/cli_testcase.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\local\cli\create
*/
final class create_test extends cli_testcase {
final class cli_create_test extends cli_testcase {
public function tearDown(): void {
parent::tearDown();
}
/**
* Tests without any arguments.
*/
@@ -147,6 +151,7 @@ final class create_test extends cli_testcase {
*/
public function test_create_withoptions(): void {
$this->set_parameters([
'--autostart=true',
'--warn=10',
'--start=0',
'--duration=30',
@@ -176,6 +181,7 @@ final class create_test extends cli_testcase {
public function test_create_onlyid(): void {
$this->set_parameters([
'--onlyid',
'--autostart=N',
'--warn=10',
'--start=0',
'--duration=30',
@@ -186,10 +192,8 @@ final class create_test extends cli_testcase {
$cli = new create();
$cli->set_referencetime($now);
$id = $this->execute($cli);
// Extracting only the id digits from the output.
preg_match('/(\d+)\s*$/', $id, $matches);
// Passing the proper id into the id variable.
$id = $matches ? $matches[1] : null;
// Check if the id contains is only a number (parameter onlyid).
$id = trim($id);
self::assertTrue(is_number($id));
$id = (int)$id;
// Check creted outage.
@@ -214,6 +218,7 @@ final class create_test extends cli_testcase {
$cli = new create();
$cli->set_referencetime($now);
$cli->set_defaults([
'autostart' => false,
'warn' => 50,
'start' => 200,
'duration' => 300,
@@ -242,6 +247,7 @@ final class create_test extends cli_testcase {
$now = time();
// Create the outage to clone.
$original = new outage([
'autostart' => false,
'warntime' => $now - 120,
'starttime' => $now,
'stoptime' => $now + 120,
@@ -257,13 +263,10 @@ final class create_test extends cli_testcase {
]);
$cli = new create();
$cli->set_referencetime($now);
// Extracting only the id digits from the output.
preg_match('/(\d+)\s*$/', $id, $matches);
// Passing the proper id into the id variable.
$id = $matches ? (int)$matches[1] : null;
$id = trim($this->execute($cli));
// Check cloned data.
$cloned = outagedb::get_by_id((int)$id);
self::assertSame($now, $cloned->starttime);
self::assertSame($now + 60, $cloned->starttime);
self::assertSame($original->get_warning_duration(), $cloned->get_warning_duration());
self::assertSame($original->get_duration_planned(), $cloned->get_duration_planned());
self::assertSame($original->title, $cloned->title);
@@ -289,6 +292,7 @@ final class create_test extends cli_testcase {
public function test_create_withblock(): void {
// Not an extensive test in the blocking API, cliwaitforit tests should cover them deeper.
$this->set_parameters([
'--autostart=N',
'--block',
'--warn=60',
'--start=0',
@@ -312,4 +316,21 @@ final class create_test extends cli_testcase {
$this->set_expected_exception('coding_exception');
$cli->set_defaults(['aninvalidparameter' => 'value']);
}
/**
* Tests with an invalud autostart bool value.
*/
public function test_invalid_bool(): void {
$this->set_parameters([
'--autostart=maybe',
'--warn=60',
'--start=0',
'--duration=600',
'--title=Title',
'--description=Description',
]);
$cli = new create();
$this->set_expected_cli_exception(cli_exception::ERROR_PARAMETER_INVALID);
$cli->execute();
}
}
@@ -23,10 +23,10 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace auth_outage\local\cli;
use auth_outage\dml\outagedb;
use auth_outage\local\outage;
use auth_outage\local\cli\finish;
use auth_outage\local\cli\cli_exception;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/cli_testcase.php');
@@ -40,7 +40,11 @@ require_once(__DIR__ . '/cli_testcase.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\local\cli\finish
*/
final class finish_test extends cli_testcase {
final class cli_finish_test extends cli_testcase {
public function tearDown(): void {
parent::tearDown();
}
/**
* Tests the constructor.
*/
@@ -93,6 +97,7 @@ final class finish_test extends cli_testcase {
self::setAdminUser();
$now = time();
$id = outagedb::save(new outage([
'autostart' => false,
'warntime' => $now - 200,
'starttime' => $now - 100,
'stoptime' => $now - 50,
@@ -113,6 +118,7 @@ final class finish_test extends cli_testcase {
self::setAdminUser();
$now = time();
$id = outagedb::save(new outage([
'autostart' => false,
'warntime' => $now - 200,
'starttime' => $now - 100,
'stoptime' => $now + 100,
@@ -23,7 +23,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace auth_outage\local\cli;
use auth_outage\local\cli\cli_exception;
use auth_outage\local\cli\create;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/cli_testcase.php');
@@ -38,6 +39,10 @@ require_once(__DIR__ . '/cli_testcase.php');
* @covers \auth_outage\local\cli\create
*/
final class cli_test extends cli_testcase {
public function tearDown(): void {
parent::tearDown();
}
/**
* Tests providing an unknown parameter.
*/
@@ -23,10 +23,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace auth_outage\local\cli;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/../../base_testcase.php');
require_once(__DIR__ . '/base_testcase.php');
/**
* cli_testcase class.
@@ -36,7 +34,11 @@ require_once(__DIR__ . '/../../base_testcase.php');
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class cli_testcase extends \auth_outage\base_testcase {
abstract class cli_testcase extends base_testcase {
public function tearDown(): void {
parent::tearDown();
}
/**
* Always enable the auth outage plugin, resets after test and set no parameters.
*/
@@ -73,7 +75,7 @@ abstract class cli_testcase extends \auth_outage\base_testcase {
*
* @return string The output text.
*/
protected function execute(clibase $cli) {
protected function execute(mixed $cli) {
ob_start();
try {
$cli->execute();
@@ -23,10 +23,10 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace auth_outage\local\cli;
use auth_outage\dml\outagedb;
use auth_outage\local\outage;
use auth_outage\local\cli\waitforit;
use auth_outage\local\cli\cli_exception;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/cli_testcase.php');
@@ -40,7 +40,11 @@ require_once(__DIR__ . '/cli_testcase.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\local\cli\waitforit
*/
final class waitforit_test extends cli_testcase {
final class cli_waitforit_test extends cli_testcase {
public function tearDown(): void {
parent::tearDown();
}
/**
* Tests the constructor.
*/
@@ -120,6 +124,7 @@ final class waitforit_test extends cli_testcase {
self::setAdminUser();
$now = time();
$id = outagedb::save(new outage([
'autostart' => false,
'warntime' => $now - 200,
'starttime' => $now - 100,
'stoptime' => $now - 50,
@@ -140,6 +145,7 @@ final class waitforit_test extends cli_testcase {
self::setAdminUser();
$now = time();
outagedb::save(new outage([
'autostart' => false,
'warntime' => $now - 10,
'starttime' => $now + 1,
'stoptime' => $now + 10,
@@ -162,6 +168,7 @@ final class waitforit_test extends cli_testcase {
self::setAdminUser();
$now = time();
outagedb::save(new outage([
'autostart' => false,
'warntime' => $now,
'starttime' => $now + 45,
'stoptime' => $now + (60 * 60),
@@ -190,6 +197,7 @@ final class waitforit_test extends cli_testcase {
self::setAdminUser();
$now = time();
$id = outagedb::save(new outage([
'autostart' => false,
'warntime' => $now,
'starttime' => $now + (2 * 60 * 60),
'stoptime' => $now + (60 * 60),
@@ -23,13 +23,11 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace auth_outage\local\controllers;
use auth_outage\local\outage;
use context_system;
use auth_outage\local\controllers\infopage;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/../../base_testcase.php');
require_once(__DIR__ . '/base_testcase.php');
/**
* Tests performed on infopage controller class and update_static_page task class.
@@ -40,7 +38,11 @@ require_once(__DIR__ . '/../../base_testcase.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\local\controllers\infopage
*/
final class infopage_test extends \auth_outage\base_testcase {
final class controllers_infopage_test extends base_testcase {
public function tearDown(): void {
parent::tearDown();
}
/**
* Tests the constructor.
*/
@@ -23,13 +23,13 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace auth_outage\local\controllers;
use auth_outage\task\update_static_page;
use DOMDocument;
use auth_outage\local\controllers\maintenance_static_page;
use auth_outage\local\controllers\maintenance_static_page_io;
use auth_outage\local\controllers\maintenance_static_page_generator;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/../../base_testcase.php');
require_once(__DIR__ . '/base_testcase.php');
/**
* maintenance_static_page_test class.
@@ -40,7 +40,11 @@ require_once(__DIR__ . '/../../base_testcase.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\local\controllers\maintenance_static_page_generator
*/
final class maintenance_static_page_test extends \auth_outage\base_testcase {
final class controllers_maintenance_static_page_test extends base_testcase {
public function tearDown(): void {
parent::tearDown();
}
/**
* Test template file.
*/
@@ -124,12 +128,10 @@ final class maintenance_static_page_test extends \auth_outage\base_testcase {
$page->generate();
// Check for css file.
self::assertFileExists($page->get_io()->get_resources_folder() .
'/53365950336b070c0b26ca50e7d0dad962c364e6.dGV4dC9wbGFpbg');
self::assertFileExists($page->get_io()->get_resources_folder() . '/b09bd4b66cc3964d5fc5978752fc554f5666daa3.dGV4dC9wbGFpbg');
// Check for catalyst.png file referenced in url(..) of css.
self::assertFileExists($page->get_io()->get_resources_folder() .
'/ff7f7f87a26a908fc72930eaefb6b57306361d16.aW1hZ2UvcG5n');
self::assertFileExists($page->get_io()->get_resources_folder() . '/ff7f7f87a26a908fc72930eaefb6b57306361d16.aW1hZ2UvcG5n');
}
/**
@@ -144,12 +146,10 @@ final class maintenance_static_page_test extends \auth_outage\base_testcase {
$page->generate();
// Check for css file.
self::assertFileExists($page->get_io()->get_resources_folder()
. '/e0b34925c1f939c247a4b50d6bf08c76088def39.dGV4dC9wbGFpbg');
self::assertFileExists($page->get_io()->get_resources_folder() . '/2ec04228cc8bb37782f511aaeb01ee553cc884a4.dGV4dC9wbGFpbg');
// Check for catalyst.png file referenced in url(..) of css.
self::assertFileExists($page->get_io()->get_resources_folder()
. '/ff7f7f87a26a908fc72930eaefb6b57306361d16.aW1hZ2UvcG5n');
self::assertFileExists($page->get_io()->get_resources_folder() . '/ff7f7f87a26a908fc72930eaefb6b57306361d16.aW1hZ2UvcG5n');
}
/**
@@ -164,12 +164,10 @@ final class maintenance_static_page_test extends \auth_outage\base_testcase {
$page->generate();
// Check for css file.
self::assertFileExists($page->get_io()->get_resources_folder() .
'/beb44281e23b9d872056bf0230cea34535e8cdea.dGV4dC9wbGFpbg');
self::assertFileExists($page->get_io()->get_resources_folder() . '/beb44281e23b9d872056bf0230cea34535e8cdea.dGV4dC9wbGFpbg');
// Check for file referenced in url(..) of css.
self::assertFileExists($page->get_io()->get_resources_folder() .
'/a02a8a442fa82d5205ffb24722d9df7f35161f56.dGV4dC9wbGFpbg');
self::assertFileExists($page->get_io()->get_resources_folder() . '/a02a8a442fa82d5205ffb24722d9df7f35161f56.dGV4dC9wbGFpbg');
}
/**
@@ -346,7 +344,7 @@ final class maintenance_static_page_test extends \auth_outage\base_testcase {
* @return string
*/
private function get_fixture_path_location($file) {
return (string)new \moodle_url('/auth/outage/tests/local/controllers/fixtures/' . $file);
return (string)new \moodle_url('/auth/outage/tests/fixtures/' . $file);
}
/**
@@ -431,6 +429,39 @@ final class maintenance_static_page_test extends \auth_outage\base_testcase {
maintenance_static_page_io::file_get_data(200);
}
/**
* Test file_get_data with curlsecurityblockedhosts.
* We will use an external URL to test passing ignoresecurity inside of file_get_data works,
* ideally in real code we should only be calling file_get_data with internal URLs.
*/
public function test_file_get_data_curlsecurityblockedhosts(): void {
global $CFG, $USER;
$testhtml = $this->getExternalTestFileUrl('/test.html');
$url = new \moodle_url($testhtml);
$host = $url->get_host();
set_config('curlsecurityblockedhosts', $host); // Blocks $host.
// Test a regular curl with the default security enabled does in fact get blocked.
$curl = new \curl();
$contents = $curl->get($testhtml);
$expected = $curl->get_security()->get_blocked_url_string();
self::assertSame($expected, $contents);
self::assertSame(0, $curl->get_errno());
if ($CFG->branch >= 403) {
self::assertDebuggingCalled(
"Blocked $testhtml: The URL is blocked. [user {$USER->id}]",
DEBUG_NONE
);
}
// Test file_get_data does return the page and isn't blocked by security.
$found = maintenance_static_page_io::file_get_data($url->out());
$expected = '47250a973d1b88d9445f94db4ef2c97a';
self::assertSame($expected, md5($found['contents']));
self::assertSame('text/html', $found['mime']);
}
/**
* Test remove css selector.
*/
@@ -23,9 +23,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace auth_outage\dml;
use auth_outage\local\outage;
use auth_outage\dml\outagedb;
/**
* events_test tests class.
@@ -39,7 +38,7 @@ use auth_outage\local\outage;
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\dml\outagedb
*/
final class events_test extends \advanced_testcase {
final class dml_events_test extends \core_phpunit\testcase {
/**
* @var outage|null Outage used in the tests.
*/
@@ -62,6 +61,7 @@ final class events_test extends \advanced_testcase {
// Save new outage.
$now = time();
$outage = new outage([
'autostart' => false,
'warntime' => $now - 60,
'starttime' => 60,
'stoptime' => 120,
@@ -94,6 +94,7 @@ final class events_test extends \advanced_testcase {
// Save new outage.
$now = time();
$outage = new outage([
'autostart' => false,
'warntime' => $now - 60,
'starttime' => 60,
'stoptime' => 120,
@@ -131,6 +132,7 @@ final class events_test extends \advanced_testcase {
// Save new outage.
$now = time();
$outage = new outage([
'autostart' => false,
'warntime' => $now - 60,
'starttime' => 60,
'stoptime' => 120,
@@ -25,12 +25,11 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace auth_outage\dml;
use auth_outage\local\outage;
use auth_outage\dml\outagedb;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/../base_testcase.php');
require_once(__DIR__ . '/base_testcase.php');
/**
* installation_test test class.
@@ -43,7 +42,11 @@ require_once(__DIR__ . '/../base_testcase.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\dml\outagedb
*/
final class installation_test extends \auth_outage\base_testcase {
final class dml_installation_test extends base_testcase {
public function tearDown(): void {
parent::tearDown();
}
/**
* Checks if plugin cleans up data after uninstall.
*
@@ -56,9 +59,10 @@ final class installation_test extends \auth_outage\base_testcase {
static::setAdminUser();
$dbman = $DB->get_manager();
// Create a future outage.
// Create a future outage with autostart.
$now = time();
$outage = new outage([
'autostart' => true,
'starttime' => $now + (1 * 60 * 60),
'stoptime' => $now + (2 * 60 * 60),
'warntime' => $now - (2 * 60 * 60),
@@ -69,6 +73,7 @@ final class installation_test extends \auth_outage\base_testcase {
outagedb::save($outage);
$text = trim(ob_get_contents());
ob_end_clean();
self::assertStringContainsString('Update maintenance mode configuration', $text);
self::assertSame(1, $DB->count_records_select('event', "eventtype = 'auth_outage'", null));
// Uninstall plugin.
@@ -23,12 +23,11 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace auth_outage\dml;
use auth_outage\local\outage;
use auth_outage\dml\outagedb;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/../base_testcase.php');
require_once(__DIR__ . '/base_testcase.php');
/**
* outagedb_test tests class.
@@ -39,7 +38,11 @@ require_once(__DIR__ . '/../base_testcase.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\dml\outagedb
*/
final class outagedb_test extends \auth_outage\base_testcase {
final class dml_outagedb_test extends base_testcase {
public function tearDown(): void {
parent::tearDown();
}
/**
* Creates an array of ids in from the given outages array.
* @param outage[] $outages An array of outages.
@@ -56,6 +59,7 @@ final class outagedb_test extends \auth_outage\base_testcase {
/**
* Helper function to create an outage then save it to the database.
*
* @param bool $autostart If outage should automatically start.
* @param int $now Timestamp for now, such as time().
* @param int $warning In how many hours the warning starts. Can be negative.
* @param int $start In how many hours this outage starts. Can be negative.
@@ -64,8 +68,9 @@ final class outagedb_test extends \auth_outage\base_testcase {
* @param int|null $finished In how many hours this outage is marked as finished. Can be negative or null.
* @return int Id the of created outage.
*/
private static function saveoutage($now, $warning, $start, $stop, $title, $finished = null) {
private static function saveoutage($autostart, $now, $warning, $start, $stop, $title, $finished = null) {
return outagedb::save(new outage([
'autostart' => $autostart,
'warntime' => $now + ($warning * 60 * 60),
'starttime' => $now + ($start * 60 * 60),
'stoptime' => $now + ($stop * 60 * 60),
@@ -151,7 +156,7 @@ final class outagedb_test extends \auth_outage\base_testcase {
$now = time();
$this->resetAfterTest(true);
// Create it.
$id = self::saveoutage($now, -3, -2, 2, 'An ongoing outage.');
$id = self::saveoutage(false, $now, -3, -2, 2, 'An ongoing outage.');
$outage = outagedb::get_by_id($id);
self::assertTrue($outage->is_active($now));
self::assertTrue($outage->is_ongoing($now));
@@ -235,25 +240,26 @@ final class outagedb_test extends \auth_outage\base_testcase {
self::assertEquals([], outagedb::get_all(), 'Ensure there are no other outages that can affect the test.');
self::assertNull(outagedb::get_active($now), 'There should be no active outage at this point.');
self::saveoutage($now, 1, 2, 3, 'An outage that starts in the future and is not in warning period.');
self::saveoutage(false, $now, 1, 2, 3, 'An outage that starts in the future and is not in warning period.');
self::assertNull(outagedb::get_active($now), 'No active outages yet.');
self::saveoutage($now, -3, -2, -1, 'An outage that is already in the past.');
self::saveoutage(false, $now, -3, -2, -1, 'An outage that is already in the past.');
self::assertNull(outagedb::get_active($now), 'No active outages yet.');
$activeid = self::saveoutage($now, -2, 1, 2, 'An outage in warning period.');
$activeid = self::saveoutage(false, $now, -2, 1, 2, 'An outage in warning period.');
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
$activeid = self::saveoutage($now, -2, 0, 2, 'An outage starts now.');
$activeid = self::saveoutage(false, $now, -2, 0, 2, 'An outage starts now.');
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
self::saveoutage($now, -2, 0, -1, 'Invalid outage.');
self::saveoutage(false, $now, -2, 0, -1, 'Invalid outage.');
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
self::saveoutage($now, -2, 0, 0, 'Invalid outage.');
self::saveoutage(false, $now, -2, 0, 0, 'Invalid outage.');
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
self::saveoutage(
false,
$now,
-1,
2,
@@ -262,16 +268,17 @@ final class outagedb_test extends \auth_outage\base_testcase {
);
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
self::saveoutage($now, -3, -2, 2, 'An finished outage.', -1);
self::saveoutage(false, $now, -3, -2, 2, 'An finished outage.', -1);
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
$activeid = self::saveoutage($now, -3, -2, 2, 'An ongoing outage.');
$activeid = self::saveoutage(false, $now, -3, -2, 2, 'An ongoing outage.');
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
self::saveoutage($now, -3, -1, 1, 'Another ongoing outage but ignored because it started after the previous one.');
self::saveoutage(false, $now, -3, -1, 1, 'Another ongoing outage but ignored because it started after the previous one.');
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
self::saveoutage(
false,
$now,
-3,
-2,
@@ -293,62 +300,62 @@ final class outagedb_test extends \auth_outage\base_testcase {
self::assertEquals([], outagedb::get_all(), 'Ensure there are no other outages that can affect the test.');
self::assertEquals([], outagedb::get_all_unended($now), 'There should be no future outages at this point.');
self::saveoutage($now, -3, -2, -1, 'A past outage.');
self::saveoutage(false, $now, -3, -2, -1, 'A past outage.');
self::assertEquals([], outagedb::get_all_unended($now), 'No future outages yet.');
self::saveoutage($now, -3, -2, 2, 'A finished outage.', -1);
self::saveoutage(false, $now, -3, -2, 2, 'A finished outage.', -1);
self::assertEquals([], outagedb::get_all_unended($now), 'No future outages yet.');
$id1 = self::saveoutage($now, 2, 3, 4, 'A future outage.');
$id1 = self::saveoutage(false, $now, 2, 3, 4, 'A future outage.');
self::assertEquals(
[$id1],
self::createidarray(outagedb::get_all_unended($now)),
'Wrong future data.'
);
$id2 = self::saveoutage($now, 1, 4, 5, 'Another future outage.');
$id2 = self::saveoutage(false, $now, 1, 4, 5, 'Another future outage.');
self::assertEquals(
[$id1, $id2],
self::createidarray(outagedb::get_all_unended($now)),
'Wrong future data.'
);
$id3 = self::saveoutage($now, 1, 3, 5, 'Yet another future outage.');
$id3 = self::saveoutage(false, $now, 1, 3, 5, 'Yet another future outage.');
self::assertEquals(
[$id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)),
'Wrong future data.'
);
$id4 = self::saveoutage($now, -2, 1, 2, 'An outage in warning period.');
$id4 = self::saveoutage(false, $now, -2, 1, 2, 'An outage in warning period.');
self::assertEquals(
[$id4, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)),
'Wrong future data.'
);
$id5 = self::saveoutage($now, -1, 2, 3, 'Another outage in warning period.');
$id5 = self::saveoutage(false, $now, -1, 2, 3, 'Another outage in warning period.');
self::assertEquals(
[$id4, $id5, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)),
'Wrong future data.'
);
$id6 = self::saveoutage($now, -3, -2, 2, 'An ongoing outage.');
$id6 = self::saveoutage(false, $now, -3, -2, 2, 'An ongoing outage.');
self::assertEquals(
[$id6, $id4, $id5, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)),
'Wrong future data.'
);
$id7 = self::saveoutage($now, -3, -1, 1, 'Another ongoing outage.');
$id7 = self::saveoutage(false, $now, -3, -1, 1, 'Another ongoing outage.');
self::assertEquals(
[$id6, $id7, $id4, $id5, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)),
'Wrong future data.'
);
$id8 = self::saveoutage($now, -3, -2, 1, 'Yet another ongoing outage.');
$id8 = self::saveoutage(false, $now, -3, -2, 1, 'Yet another ongoing outage.');
self::assertEquals(
[$id6, $id8, $id7, $id4, $id5, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)),
@@ -368,37 +375,37 @@ final class outagedb_test extends \auth_outage\base_testcase {
self::assertEquals([], outagedb::get_all(), 'Ensure there are no other outages that can affect the test.');
self::assertEquals([], outagedb::get_all_ended($now), 'There should be no future outages at this point.');
self::saveoutage($now, -2, 1, 2, 'An outage in warning period.');
self::saveoutage(false, $now, -2, 1, 2, 'An outage in warning period.');
self::assertEquals([], outagedb::get_all_ended($now), 'No past outages yet.');
self::saveoutage($now, -3, -2, 2, 'An ongoing outage.');
self::saveoutage(false, $now, -3, -2, 2, 'An ongoing outage.');
self::assertEquals([], outagedb::get_all_ended($now), 'No past outages yet.');
self::saveoutage($now, 2, 3, 4, 'A future outage.');
self::saveoutage(false, $now, 2, 3, 4, 'A future outage.');
self::assertEquals([], outagedb::get_all_ended($now), 'No past outages yet.');
$id1 = self::saveoutage($now, -8, -6, -4, 'A past outage.');
$id1 = self::saveoutage(false, $now, -8, -6, -4, 'A past outage.');
self::assertEquals(
[$id1],
self::createidarray(outagedb::get_all_ended($now)),
'Wrong past data.'
);
$id2 = self::saveoutage($now, -8, -7, -5, 'Another past outage.');
$id2 = self::saveoutage(false, $now, -8, -7, -5, 'Another past outage.');
self::assertEquals(
[$id1, $id2],
self::createidarray(outagedb::get_all_ended($now)),
'Wrong past data.'
);
$id3 = self::saveoutage($now, -8, -5, -3, 'Yet another past outage.');
$id3 = self::saveoutage(false, $now, -8, -5, -3, 'Yet another past outage.');
self::assertEquals(
[$id3, $id1, $id2],
self::createidarray(outagedb::get_all_ended($now)),
'Wrong past data.'
);
$id4 = self::saveoutage($now, -3, -2, 2, 'A finished outage.', -1);
$id4 = self::saveoutage(false, $now, -3, -2, 2, 'A finished outage.', -1);
self::assertEquals(
[$id4, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_ended($now)),
@@ -493,6 +500,7 @@ final class outagedb_test extends \auth_outage\base_testcase {
$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),
@@ -516,6 +524,15 @@ final class outagedb_test extends \auth_outage\base_testcase {
outagedb::get_next_starting(-1);
}
/**
* Tests the outagedb::get_next_autostarting() with an invalid parameter.
*/
public function test_getnextautostartinginvalid(): void {
$this->resetAfterTest(true);
$this->set_expected_exception('coding_exception');
outagedb::get_next_autostarting(-1);
}
/**
* Helper function to create an outage for tests.
* @param int $i Used to populate the information.
@@ -523,6 +540,7 @@ final class outagedb_test extends \auth_outage\base_testcase {
*/
private function createoutage($i) {
return new outage([
'autostart' => ($i % 2 == 0),
'starttime' => $i * 100,
'stoptime' => $i * 100 + 50,
'warntime' => $i * 60,

Before

Width:  |  Height:  |  Size: 2.8 KiB

After

Width:  |  Height:  |  Size: 2.8 KiB

+7
View File
@@ -0,0 +1,7 @@
a {
font-size: 200%;
}
div {
background-image: url('/moodle/auth/outage/tests/fixtures/catalyst.png');
}
+7
View File
@@ -0,0 +1,7 @@
a {
font-size: 200%;
}
div {
background-image: url(/moodle/auth/outage/tests/fixtures/catalyst.png);
}
@@ -23,12 +23,13 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace auth_outage\form\outage;
use auth_outage\local\outage;
use auth_outage\form\outage\delete;
use auth_outage\form\outage\finish;
use auth_outage\form\outage\edit;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/../../base_testcase.php');
require_once(__DIR__ . '/base_testcase.php');
/**
* forms_test test class.
@@ -39,7 +40,11 @@ require_once(__DIR__ . '/../../base_testcase.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\form\outage\edit
*/
final class forms_test extends \auth_outage\base_testcase {
final class forms_test extends base_testcase {
public function tearDown(): void {
parent::tearDown();
}
/**
* Create a delete form.
*/
@@ -67,6 +72,7 @@ final class forms_test extends \auth_outage\base_testcase {
self::assertFalse($edit->is_cancelled());
$outage = $edit->get_data();
self::assertInstanceOf('\\auth_outage\\local\\outage', $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());
@@ -154,6 +160,7 @@ final class forms_test extends \auth_outage\base_testcase {
*/
public function test_setdata(): void {
$outage = new outage([
'autostart' => false,
'warntime' => time() - 60,
'starttime' => time(),
'stoptime' => time() + 60,
+1 -3
View File
@@ -14,8 +14,6 @@
// 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;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/base_testcase.php');
require_once(__DIR__ . '/../lib.php');
@@ -29,7 +27,7 @@ require_once(__DIR__ . '/../lib.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers ::auth_outage_get_climaintenance_resource_file
*/
final class lib_test extends base_testcase {
final class lib_test extends \core_phpunit\testcase {
/**
* Test this plugin gets climaintenance resource file.
*/
@@ -1,7 +0,0 @@
a {
font-size: 200%;
}
div {
background-image: url('/moodle/auth/outage/tests/local/controllers/fixtures/catalyst.png');
}
@@ -1,7 +0,0 @@
a {
font-size: 200%;
}
div {
background-image: url(/moodle/auth/outage/tests/local/controllers/fixtures/catalyst.png);
}
@@ -14,10 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace auth_outage\local;
use auth_outage\local\outage;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/../base_testcase.php');
require_once(__DIR__ . '/base_testcase.php');
/**
* outage_test test class.
@@ -28,7 +28,11 @@ require_once(__DIR__ . '/../base_testcase.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\local\outage
*/
final class outage_test extends \auth_outage\base_testcase {
final class outage_test extends base_testcase {
public function tearDown(): void {
parent::tearDown();
}
/**
* Tests the constructor.
*/
@@ -48,6 +52,7 @@ final class outage_test extends \auth_outage\base_testcase {
public function test_constructor_object(): void {
$obj = new \stdClass();
$obj->id = 1;
$obj->autostart = true;
$obj->warntime = 2;
$obj->starttime = 3;
$obj->finished = 4;
@@ -56,6 +61,7 @@ final class outage_test extends \auth_outage\base_testcase {
$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);
@@ -14,14 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace auth_outage\local;
use auth_outage\dml\outagedb;
use auth_outage\local\outage;
use auth_outage\local\outagelib;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . '/adminlib.php');
require_once(__DIR__ . '/../base_testcase.php');
require_once(__DIR__ . '/base_testcase.php');
/**
* outagelib_test test class.
@@ -32,7 +32,50 @@ require_once(__DIR__ . '/../base_testcase.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\local\outagelib
*/
final class outagelib_test extends \auth_outage\base_testcase {
final class outagelib_test extends base_testcase {
public function tearDown(): void {
parent::tearDown();
}
/**
* Check if maintenance message is disabled as needed.
*/
public function test_maintenancemessage(): void {
$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.');
ob_start();
outagedb::save($outage);
$text = trim(ob_get_contents());
ob_end_clean();
self::assertStringContainsString('Update maintenance mode configuration', $text);
self::assertFalse((bool)get_config('moodle', 'maintenance_message'));
self::assertCount(2, $this->getDebuggingMessages());
$this->resetDebugging();
}
/**
* Check if maintenance later is removed if no outage set.
*/
public function test_maintenancelater_nonext(): void {
$this->resetAfterTest(true);
set_config('maintenance_later', time() + (60 * 60 * 24 * 7)); // In 1 week.
self::assertNotEmpty(get_config('moodle', 'maintenance_later'));
outagelib::prepare_next_outage();
self::assertEmpty(get_config('moodle', 'maintenance_later'));
}
/**
* Check outagelib::inject() works as expected.
*/
@@ -43,6 +86,7 @@ final class outagelib_test extends \auth_outage\base_testcase {
self::setAdminUser();
$now = time();
$outage = new outage([
'autostart' => true,
'warntime' => $now,
'starttime' => $now + 100,
'stoptime' => $now + 200,
@@ -53,6 +97,7 @@ final class outagelib_test extends \auth_outage\base_testcase {
$outage->id = outagedb::save($outage);
$text = trim(ob_get_contents());
ob_end_clean();
self::assertStringContainsString('Update maintenance mode configuration', $text);
outagelib::reset_injectcalled();
// Get full header to avoid interactions with other single inject plugins.
@@ -87,6 +132,7 @@ final class outagelib_test extends \auth_outage\base_testcase {
self::setAdminUser();
$now = time();
$outage = new outage([
'autostart' => true,
'warntime' => $now,
'starttime' => $now + 100,
'stoptime' => $now + 200,
@@ -97,6 +143,7 @@ final class outagelib_test extends \auth_outage\base_testcase {
$outage->id = outagedb::save($outage);
$text = trim(ob_get_contents());
ob_end_clean();
self::assertStringContainsString('Update maintenance mode configuration', $text);
$_GET = ['auth_outage_preview' => (string)$outage->id];
@@ -128,6 +175,7 @@ final class outagelib_test extends \auth_outage\base_testcase {
self::setAdminUser();
$now = time();
$outage = new outage([
'autostart' => true,
'warntime' => $now,
'starttime' => $now + 100,
'stoptime' => $now + 200,
@@ -138,6 +186,7 @@ final class outagelib_test extends \auth_outage\base_testcase {
$outage->id = outagedb::save($outage);
$text = trim(ob_get_contents());
ob_end_clean();
self::assertStringContainsString('Update maintenance mode configuration', $text);
$_GET = ['auth_outage_preview' => (string)$outage->id, 'auth_outage_delta' => '500'];
outagelib::reset_injectcalled();
$header = outagelib::get_inject_code();
@@ -160,6 +209,7 @@ final class outagelib_test extends \auth_outage\base_testcase {
$this->resetAfterTest(true);
$keys = [
'css',
'default_autostart',
'default_description',
'default_duration',
'default_title',
@@ -190,6 +240,7 @@ final class outagelib_test extends \auth_outage\base_testcase {
$keys = [
'allowedips',
'css',
'default_autostart',
'default_description',
'default_duration',
'default_title',
@@ -210,6 +261,7 @@ final class outagelib_test extends \auth_outage\base_testcase {
// Set config with invalid values.
set_config('allowedips', " \n", 'auth_outage');
set_config('css', " \n", 'auth_outage');
set_config('default_autostart', " \n", 'auth_outage');
set_config('default_description', " \n", 'auth_outage');
set_config('default_duration', " \n", 'auth_outage');
set_config('default_title', " \n", 'auth_outage');
@@ -233,6 +285,7 @@ final class outagelib_test extends \auth_outage\base_testcase {
self::setAdminUser();
$now = time();
$outage = new outage([
'autostart' => true,
'warntime' => $now,
'starttime' => $now + 100,
'stoptime' => $now + 200,
@@ -243,6 +296,7 @@ final class outagelib_test extends \auth_outage\base_testcase {
$outage->id = outagedb::save($outage);
$text = trim(ob_get_contents());
ob_end_clean();
self::assertStringContainsString('Update maintenance mode configuration', $text);
// Pretend we are there...
$_SERVER['SCRIPT_FILENAME'] = '/var/www/alternativepath/admin/settings.php'; // Issue #88 regression test.
@@ -302,11 +356,6 @@ e.e.e.e/20');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Accept-Ranges: none');
header('X-Moodle-Maintenance: manager');
if (!empty('testmeta')) {
header('X-Outage-Metadata: ' . 'testmeta');
}
header('X-Outage-StartTime: ' . '123');
header('X-Outage-EndTime: ' . '456');
}
if (!$isphpunit && ((defined('AJAX_SCRIPT') && AJAX_SCRIPT) || (defined('WS_SERVER') && WS_SERVER))) {
@@ -314,11 +363,11 @@ e.e.e.e/20');
}
if (true && $ipblocked) {
echo '<!-- auth_outage blocked your ip: '.getremoteaddr('n/a').' -->';
echo '<!-- Blocked by ip, your ip: '.getremoteaddr('n/a').' -->';
}
if (true && $accesskeyblocked) {
echo '<!-- auth_outage blocked by missing or incorrect access key, access key given: '. $useraccesskey .' -->';
echo '<!-- Blocked by missing or incorrect access key, access key given: '. $useraccesskey .' -->';
}
if (!$isphpunit) {
@@ -332,7 +381,7 @@ e.e.e.e/20');
}
}
EOT;
$found = outagelib::create_climaintenancephp_code(123, 456, "hey'\"you\na.b.c.d\ne.e.e.e/20", '12345', 'testmeta');
$found = outagelib::create_climaintenancephp_code(123, 456, "hey'\"you\na.b.c.d\ne.e.e.e/20", '12345');
self::assertSame($expected, $found);
}
@@ -386,11 +435,6 @@ if ((time() >= 123) && (time() < 456)) {
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Accept-Ranges: none');
header('X-Moodle-Maintenance: manager');
if (!empty(NULL)) {
header('X-Outage-Metadata: ' . NULL);
}
header('X-Outage-StartTime: ' . '123');
header('X-Outage-EndTime: ' . '456');
}
if (!$isphpunit && ((defined('AJAX_SCRIPT') && AJAX_SCRIPT) || (defined('WS_SERVER') && WS_SERVER))) {
@@ -398,11 +442,11 @@ if ((time() >= 123) && (time() < 456)) {
}
if (true && $ipblocked) {
echo '<!-- auth_outage blocked your ip: '.getremoteaddr('n/a').' -->';
echo '<!-- Blocked by ip, your ip: '.getremoteaddr('n/a').' -->';
}
if (true && $accesskeyblocked) {
echo '<!-- auth_outage blocked by missing or incorrect access key, access key given: '. $useraccesskey .' -->';
echo '<!-- Blocked by missing or incorrect access key, access key given: '. $useraccesskey .' -->';
}
if (!$isphpunit) {
@@ -481,6 +525,21 @@ EOT;
}
}
/**
* Related to Issue #70: Creating ongoing outage does not trigger maintenance file creation.
*/
public function test_preparenextoutage_notautostart(): void {
global $CFG;
$this->create_outage();
// The method outagelib::prepare_next_outage() should have been called by save().
foreach ([$CFG->dataroot . '/climaintenance.template.html', $CFG->dataroot . '/climaintenance.php'] as $file) {
self::assertFileExists($file);
unlink($file);
}
}
/**
* Regression Test - Issue #82: When changing the IP address list it should recreate the maintenance files.
*/
@@ -517,6 +576,36 @@ EOT;
}
}
/**
* Related to Issue #72: IP Block still triggers cli maintenance mode even without autostart.
*/
public function test_preparenextoutage_noautostarttrigger(): void {
global $CFG;
$this->resetAfterTest(true);
self::setAdminUser();
$now = time();
$outage = new outage([
'autostart' => false,
'warntime' => $now - 200,
'starttime' => $now - 100,
'stoptime' => $now + 200,
'title' => 'Title',
'description' => 'Description',
]);
outagedb::save($outage);
// The method outagelib::prepare_next_outage() should have been called by save().
self::assertFalse(get_config('moodle', 'maintenance_later'));
// This file should not exist even if the statement above fails as Moodle does not create it immediately but test anyway.
// Backwards compatibility with older PHPUnit - use old assertFile method.
if (method_exists($this, 'assertFileDoesNotExist')) {
self::assertFileDoesNotExist($CFG->dataroot . '/climaintenance.html');
} else {
self::assertFileNotExists($CFG->dataroot . '/climaintenance.html');
}
}
/**
* Regression test for issue #85.
*/
@@ -527,6 +616,7 @@ EOT;
self::setAdminUser();
$now = time();
$outage = new outage([
'autostart' => true,
'warntime' => $now,
'starttime' => $now + 100,
'stoptime' => $now + 200,
@@ -537,6 +627,7 @@ EOT;
$outage->id = outagedb::save($outage);
$text = trim(ob_get_contents());
ob_end_clean();
self::assertStringContainsString('Update maintenance mode configuration', $text);
// Pretend we are there...
$_SERVER['SCRIPT_FILENAME'] = '/var/www/alternativepath/admin/settings.php'; // Issue #88 regression test.
$_SERVER['SCRIPT_NAME'] = '/admin/settings.php';
@@ -555,6 +646,7 @@ EOT;
self::setAdminUser();
$now = time();
$outage = new outage([
'autostart' => false,
'warntime' => $now - 200,
'starttime' => $now - 100,
'stoptime' => $now + 200,
@@ -575,8 +667,8 @@ EOT;
* @return array
*/
public static function evaluation_maintenancepage_provider(): array {
$blockedipout = '<!-- auth_outage blocked your ip:';
$blockedaccesskeyout = '<!-- auth_outage blocked by missing or incorrect access key, access key given:';
$blockedipout = '<!-- Blocked by ip, your ip:';
$blockedaccesskeyout = '<!-- Blocked by missing or incorrect access key, access key given:';
return [
// IP set up, access key not set up.
@@ -654,7 +746,7 @@ EOT;
*
* We need this because we modify the request headers,
* see https://github.com/sebastianbergmann/phpunit/issues/720#issuecomment-10421092
* @runInSeparateProcess
* @runClassInSeparateProcess
*/
public function test_evaluation_maintenancepage(
?string $allowedips,
@@ -670,6 +762,7 @@ EOT;
self::setAdminUser();
$now = time();
$outage = new outage([
'autostart' => false,
'warntime' => $now - 200,
'starttime' => $now - 100,
'stoptime' => $now + 200,
+2 -3
View File
@@ -28,9 +28,8 @@
defined('MOODLE_INTERNAL') || die();
$plugin->component = "auth_outage";
$plugin->version = 2024081907; // The current plugin version (Date: YYYYMMDDXX).
$plugin->release = 2024081907; // Human-readable release information.
$plugin->version = 2024081901; // The current plugin version (Date: YYYYMMDDXX).
$plugin->release = 2024081901; // Human-readable release information.
$plugin->requires = 2017111309; // 2017111309 = T13, but this really requires 3.9 and higher.
$plugin->maturity = MATURITY_STABLE; // Suitable for PRODUCTION environments!
$plugin->supported = [39, 405]; // A range of branch numbers of supported moodle versions.
$plugin->incompatible = 501;
+1 -9
View File
@@ -39,15 +39,7 @@ defined('MOODLE_INTERNAL') || die();
<b><?php echo get_string('infountil', 'auth_outage'); ?></b>
<?php echo userdate($viewbag['outage']->stoptime, get_string('datetimeformat', 'auth_outage')); ?>
</div>
<div class="auth_outage_info_description">
<?php
echo format_text(
$viewbag['outage']->get_description(),
FORMAT_HTML,
['context' => context_system::instance()]
);
?>
</div>
<div class="auth_outage_info_description"><?php echo $viewbag['outage']->get_description(); ?></div>
<?php if ($viewbag['admin']) : ?>
<?php
+4 -10
View File
@@ -49,17 +49,11 @@ if (!$viewbag['static']) {
if (is_siteadmin()) {
$link = html_writer::link(
new moodle_url('/auth/outage/finish.php', ['id' => $viewbag['outage']->id]),
$OUTPUT->pix_icon(
't/check',
get_string('finish', 'auth_outage'),
'moodle',
['class' => 'iconsmall']
)
. get_string('finish', 'auth_outage'),
$OUTPUT->pix_icon('t/check', get_string('finish', 'auth_outage'), 'moodle', ['class' => 'iconsmall']) . get_string('finish', 'auth_outage'),
[
'title' => get_string('finish', 'auth_outage'),
'class' => 'auth_outage_warningbar_box_finish',
]
'title' => get_string('finish', 'auth_outage'),
'class' => 'auth_outage_warningbar_box_finish',
]
);
$title .= ' ' . html_writer::span($link, '', ['id' => 'auth_outage_warningbar_button']);
}