Issue #21 - Added flag to outages, if set it will automatically start the maintenance mode once the outage starts.

This commit is contained in:
Daniel Thee Roperto
2016-09-22 14:26:52 +10:00
parent 708622bbcd
commit b302d9dfa5
19 changed files with 341 additions and 110 deletions

View File

@@ -18,6 +18,7 @@ namespace auth_outage\local\cli;
use auth_outage\local\outage;
use auth_outage\local\outagedb;
use coding_exception;
defined('MOODLE_INTERNAL') || die();
@@ -44,6 +45,7 @@ class create extends clibase {
return [
'help' => false,
'clone' => null,
'autostart' => null,
'warn' => null,
'start' => null,
'duration' => null,
@@ -60,6 +62,7 @@ class create extends clibase {
*/
public function generate_shortcuts() {
return [
'a' => 'autostart',
'b' => 'block',
'c' => 'clone',
'd' => 'duration',
@@ -74,8 +77,26 @@ class create extends clibase {
/**
* Sets the default values for options.
* @param mixed[] $defaults Defaults.
* @throws coding_exception
*/
public function set_defaults(array $defaults) {
$missing = $this->generate_options();
// Check if any extra parameter was given.
foreach (array_keys($defaults) as $key) {
if (!array_key_exists($key, $missing)) {
throw new coding_exception('$default['.$key.'] is not valid.');
}
unset($missing[$key]);
}
// Check if any required parameter is missing.
foreach (array_keys($missing) as $k => $v) {
if (is_null($v)) {
throw new coding_exception('$default[] missing: '.$k);
}
}
$this->defaults = $defaults;
}
@@ -110,12 +131,13 @@ class create extends clibase {
}
/**
* Merges provided options with defaults, checking and converting types as needed.
* Merges provided options with defaults.
* @return mixed[] Parameters to use.
* @throws cli_exception
*/
private function merge_options() {
$options = $this->options;
// Merge with defaults.
if (!is_null($this->defaults)) {
foreach ($options as $k => $v) {
@@ -140,6 +162,7 @@ class create extends clibase {
// Create the outage.
$start = $this->time + $options['start'];
$outage = new outage([
'autostart' => $options['autostart'],
'warntime' => $start - $options['warn'],
'starttime' => $start,
'stoptime' => $start + $options['duration'],
@@ -166,6 +189,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,
@@ -202,6 +226,29 @@ class create extends clibase {
}
}
// Check parameters that must be a specified bool.
foreach (['autostart'] as $param) {
if (is_string($options[$param])) {
switch (strtoupper($options[$param])) {
case '0':
case 'FALSE':
case 'NO':
case 'N':
$options[$param] = false;
break;
case '1':
case 'TRUE':
case 'YES':
case 'Y':
$options[$param] = true;
break;
}
}
if (!is_bool($options[$param])) {
throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param]));
}
}
return $options;
}
}

View File

@@ -90,6 +90,9 @@ class infopage {
* Saves a static info page for the given outage.
* @param outage $outage Outage to generate the info page.
* @param string $file File to save the static info page.
* @throws coding_exception
* @throws file_exception
* @throws invalid_state_exception
*/
public static function save_static_page(outage $outage, $file) {
if (!is_string($file)) {

View File

@@ -56,22 +56,27 @@ class outage {
const STAGE_STOPPED = 'stopped';
/**
* @var int Outage ID (auto generated by the DB).
* @var int|null Outage ID (auto generated by the DB).
*/
public $id = null;
/**
* @var int Start Time timestamp.
* @var bool|null Maintenance mode auto start flag.
*/
public $autostart = null;
/**
* @var int|null Start Time timestamp.
*/
public $starttime = null;
/**
* @var int Stop Time timestamp.
* @var int|null Stop Time timestamp.
*/
public $stoptime = null;
/**
* @var int Warning start timestamp.
* @var int|null Warning start timestamp.
*/
public $warntime = null;
@@ -81,27 +86,27 @@ class outage {
public $finished = null;
/**
* @var string Short description of the outage (no HTML).
* @var string|null Short description of the outage (no HTML).
*/
public $title = null;
/**
* @var string Description of the outage (some HTML allowed).
* @var string|null Description of the outage (some HTML allowed).
*/
public $description = null;
/**
* @var int Moodle User Id that created this outage.
* @var int|null Moodle User Id that created this outage.
*/
public $createdby = null;
/**
* @var int Moodle User Id that last modified this outage.
* @var int|null Moodle User Id that last modified this outage.
*/
public $modifiedby = null;
/**
* @var int Timestamp of when this outage was last modified.
* @var int|null Timestamp of when this outage was last modified.
*/
public $lastmodified = null;
@@ -133,6 +138,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;
}
/**

View File

@@ -19,7 +19,6 @@ namespace auth_outage\local;
use auth_outage\event\outage_created;
use auth_outage\event\outage_deleted;
use auth_outage\event\outage_updated;
use auth_outage\local\controllers\infopage;
use calendar_event;
use coding_exception;
@@ -118,8 +117,8 @@ class outagedb {
self::calendar_update($outage);
}
// Trigger static page update.
infopage::update_static_page();
// Trigger outages modified events.
outagelib::outages_modified();
// All done, return the id.
return $outage->id;
@@ -148,8 +147,8 @@ class outagedb {
$DB->delete_records('auth_outage', ['id' => $id]);
self::calendar_delete($id);
// Trigger static page update.
infopage::update_static_page();
// Trigger events.
outagelib::outages_modified();
}
/**
@@ -297,10 +296,40 @@ class outagedb {
throw new coding_exception('$time must be null or a positive int.', $time);
}
$select = ':datetime <= starttime'; // End condition.
$data = $DB->get_records_select(
'auth_outage',
$select,
':datetime <= starttime',
['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 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',
'*',

View File

@@ -16,6 +16,7 @@
namespace auth_outage\local;
use auth_outage\local\controllers\infopage;
use auth_outage_renderer;
use Exception;
use moodle_url;
@@ -112,11 +113,32 @@ class outagelib {
global $CFG;
return [
'default_autostart' => false,
'default_duration' => 60,
'warning_duration' => 60,
'warning_title' => get_string('defaultwarningtitlevalue', 'auth_outage'),
'warning_description' => get_string('defaultwarningdescriptionvalue', 'auth_outage'),
'default_warning_duration' => 60,
'default_warning_title' => get_string('defaultwarningtitlevalue', 'auth_outage'),
'default_warning_description' => get_string('defaultwarningdescriptionvalue', 'auth_outage'),
'css' => file_get_contents($CFG->dirroot.'/auth/outage/views/warningbar.css'),
];
}
/**
* Executed when outages are modified (created, updated or deleted).
*/
public static function outages_modified() {
infopage::update_static_page();
self::update_maintenance_later();
}
/**
* Calls Moodle API - set_maintenance_later() to set when the next outage starts.
*/
private static function update_maintenance_later() {
$next = outagedb::get_next_autostarting();
if (is_null($next)) {
unset_config('maintenance_later');
} else {
set_config('maintenance_later', $next->starttime);
}
}
}

View File

@@ -0,0 +1,39 @@
<?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\task;
use auth_outage\local\controllers\infopage;
use core\task\scheduled_task;
defined('MOODLE_INTERNAL') || die();
/**
* Tasks information.
* @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 update_static_page extends scheduled_task {
public function get_name() {
return get_string('taskupdatestaticpage', 'auth_outage');
}
public function execute() {
infopage::update_static_page();
}
}

View File

@@ -35,11 +35,11 @@ $cli = new create();
$config = outagelib::get_config();
$cli->set_defaults([
'help' => false,
'warn' => (int)($config->warning_duration),
'warn' => (int)($config->default_warning_duration),
'start' => null,
'duration' => (int)($config->default_duration),
'title' => $config->warning_title,
'description' => $config->warning_description,
'title' => $config->default_warning_title,
'description' => $config->default_warning_description,
]);
try {

View File

@@ -1,35 +1,29 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" PATH="auth/outage/db" VERSION="20160915"
COMMENT="XMLDB file for Moodle auth/outage"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
<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"
>
<TABLES>
<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="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."/>
<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="title" TYPE="char" LENGTH="100" NOTNULL="true" SEQUENCE="false"
COMMENT="Title for the outage (short description)."/>
<FIELD NAME="description" TYPE="text" NOTNULL="true" SEQUENCE="false"
COMMENT="More information about the outage."/>
<FIELD NAME="createdby" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Who created this entry."/>
<FIELD NAME="modifiedby" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"
COMMENT="Who last modified this entry."/>
<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."/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
<INDEXES>
<INDEX NAME="start_stop_title" UNIQUE="false" FIELDS="starttime, stoptime, title"/>
</INDEXES>
</TABLE>
</TABLES>
<TABLES>
<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."/>
<FIELD NAME="title" TYPE="char" LENGTH="100" NOTNULL="true" SEQUENCE="false" COMMENT="Title for the outage (short description)."/>
<FIELD NAME="description" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="More information about the outage."/>
<FIELD NAME="createdby" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Who created this entry."/>
<FIELD NAME="modifiedby" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Who last modified this entry."/>
<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."/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
<INDEXES>
<INDEX NAME="start_stop_title" UNIQUE="false" FIELDS="starttime, stoptime, title"/>
</INDEXES>
</TABLE>
</TABLES>
</XMLDB>

36
db/tasks.php Normal file
View File

@@ -0,0 +1,36 @@
<?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/>.
/**
* Tasks information.
*
* @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
*/
$tasks = [
[
'classname' => 'auth_outage\task\update_static_page',
'blocking' => 0,
'minute' => '23',
'hour' => '*',
'day' => '*',
'dayofweek' => '*',
'month' => '*',
],
];

View File

@@ -30,5 +30,22 @@ defined('MOODLE_INTERNAL') || die();
* @SuppressWarnings("unused")
*/
function xmldb_auth_outage_upgrade($oldversion) {
global $DB;
$dbman = $DB->get_manager();
if ($oldversion < 2016092200) {
// Define field autostart to be added to auth_outage.
$table = new xmldb_table('auth_outage');
$field = new xmldb_field('autostart', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'finished');
// Conditionally launch add field autostart.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Outage savepoint reached.
upgrade_plugin_savepoint(true, 2016092200, 'auth', 'outage');
}
return true;
}

View File

@@ -25,6 +25,7 @@
$string['auth_outagedescription'] = 'Auxiliary plugin that warns users about a future outage and prevents them from logging in once the outage starts.';
$string['clicreatehelp'] = 'Creates a new outage.';
$string['clicreateparamautostart'] = 'must be Y or N, sets if the outage automatically triggers maintenance mode.';
$string['clicreateparamblock'] = 'blocks until outage starts.';
$string['clicreateparamclone'] = 'clone another outage except for the start time.';
$string['clicreateparamdescription'] = 'the description of the outage.';
@@ -58,6 +59,8 @@ $string['clone'] = 'Clone';
$string['datetimeformat'] = '%a %d %h %Y at %I:%M%P %Z';
$string['defaultlayoutcss'] = 'Layout CSS';
$string['defaultlayoutcssdescription'] = 'This CSS code will be used to display the Outage Warning Bar.';
$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';
$string['defaultoutagedurationdescription'] = 'Default duration (in minutes) of an outage.';
$string['defaultwarningduration'] = 'Warning Duration';
@@ -105,6 +108,7 @@ $string['tableheaderdurationactual'] = 'Actual Duration';
$string['tableheaderstarttime'] = 'Starts on';
$string['tableheaderwarnbefore'] = 'Warns before';
$string['tableheadertitle'] = 'Title';
$string['taskupdatestaticpage'] = 'Update static outage page';
$string['textplaceholdershint'] = 'You can use {{start}}, {{stop}} and {{duration}} as placeholders on the title and description.';
$string['titleerrorinvalid'] = 'Title cannot be left blank.';
$string['titleerrortoolong'] = 'Title cannot have more than {$a} characters.';

View File

@@ -46,9 +46,9 @@ $config = outagelib::get_config();
$defaults = new outage([
'starttime' => time(),
'stoptime' => time() + ($config->default_duration * 60),
'warntime' => time() - ($config->warning_duration * 60),
'title' => $config->warning_title,
'description' => $config->warning_description,
'warntime' => time() - ($config->default_warning_duration * 60),
'title' => $config->default_warning_title,
'description' => $config->default_warning_description,
]);
$mform->set_data($defaults);

View File

@@ -30,6 +30,13 @@ if ($hassiteconfig && is_enabled_auth('outage')) {
$defaults = outagelib::get_config_defaults();
// Configure default settings page.
$settings->visiblename = get_string('menudefaults', 'auth_outage');
$settings->add(new admin_setting_configcheckbox(
'auth_outage/default_autostart',
get_string('defaultoutageautostart', 'auth_outage'),
get_string('defaultoutageautostartdescription', 'auth_outage'),
$defaults['default_autostart'],
PARAM_BOOL
));
$settings->add(new admin_setting_configtext(
'auth_outage/default_duration',
get_string('defaultoutageduration', 'auth_outage'),
@@ -38,24 +45,24 @@ if ($hassiteconfig && is_enabled_auth('outage')) {
PARAM_INT
));
$settings->add(new admin_setting_configtext(
'auth_outage/warning_duration',
'auth_outage/default_warning_duration',
get_string('defaultwarningduration', 'auth_outage'),
get_string('defaultwarningdurationdescription', 'auth_outage'),
$defaults['warning_duration'],
$defaults['default_warning_duration'],
PARAM_INT
));
$settings->add(new admin_setting_configtext(
'auth_outage/warning_title',
'auth_outage/default_warning_title',
get_string('defaultwarningtitle', 'auth_outage'),
get_string('defaultwarningtitledescription', 'auth_outage'),
$defaults['warning_title'],
$defaults['default_warning_title'],
PARAM_TEXT
));
$settings->add(new admin_setting_configtextarea(
'auth_outage/warning_description',
'auth_outage/default_warning_description',
get_string('defaultwarningdescription', 'auth_outage'),
get_string('defaultwarningdescriptiondescription', 'auth_outage'),
$defaults['warning_description'],
$defaults['default_warning_description'],
PARAM_TEXT
));
$settings->add(new admin_setting_configtextarea(

View File

@@ -114,6 +114,7 @@ class create_test extends cli_testcase {
public function test_create_withoptions() {
$this->set_parameters([
'--autostart=true',
'--warn=10',
'--start=0',
'--duration=30',
@@ -140,6 +141,7 @@ class create_test extends cli_testcase {
public function test_create_onlyid() {
$this->set_parameters([
'--onlyid',
'--autostart=N',
'--warn=10',
'--start=0',
'--duration=30',
@@ -173,6 +175,7 @@ class create_test extends cli_testcase {
$cli = new create();
$cli->set_referencetime($now);
$cli->set_defaults([
'autostart' => false,
'warn' => 50,
'start' => 200,
'duration' => 300,
@@ -194,10 +197,11 @@ class create_test extends cli_testcase {
}
public function test_create_withclone() {
$this->setAdminUser();
self::setAdminUser();
$now = time();
// Create the outage to clone.
$original = new outage([
'autostart' => false,
'warntime' => $now - 120,
'starttime' => $now,
'stoptime' => $now + 120,
@@ -236,6 +240,7 @@ class create_test extends cli_testcase {
public function test_create_withblock() {
// Not an extensive test in the blocking API, cliwaitforit tests should cover them deeper.
$this->set_parameters([
'--autostart=N',
'--block',
'--warn=60',
'--start=0',

View File

@@ -66,9 +66,10 @@ class finish_test extends cli_testcase {
}
public function test_endedoutage() {
$this->setAdminUser();
self::setAdminUser();
$now = time();
$id = outagedb::save(new outage([
'autostart' => false,
'warntime' => $now - 200,
'starttime' => $now - 100,
'stoptime' => $now - 50,
@@ -83,9 +84,10 @@ class finish_test extends cli_testcase {
}
public function test_finish() {
$this->setAdminUser();
self::setAdminUser();
$now = time();
$id = outagedb::save(new outage([
'autostart' => false,
'warntime' => $now - 200,
'starttime' => $now - 100,
'stoptime' => $now + 100,
@@ -99,7 +101,7 @@ class finish_test extends cli_testcase {
}
public function test_activenotfound() {
$this->setAdminUser();
self::setAdminUser();
$this->set_parameters(['-a']);
$cli = new finish();
$this->setExpectedException(cli_exception::class);
@@ -107,7 +109,7 @@ class finish_test extends cli_testcase {
}
public function test_invalidid() {
$this->setAdminUser();
self::setAdminUser();
$this->set_parameters(['-id=theid']);
$cli = new finish();
$this->setExpectedException(cli_exception::class);
@@ -115,7 +117,7 @@ class finish_test extends cli_testcase {
}
public function test_idnotfound() {
$this->setAdminUser();
self::setAdminUser();
$this->set_parameters(['-id=99999']);
$cli = new finish();
$this->setExpectedException(cli_exception::class);

View File

@@ -85,9 +85,10 @@ class waitforit_test extends cli_testcase {
}
public function test_endedoutage() {
$this->setAdminUser();
self::setAdminUser();
$now = time();
$id = outagedb::save(new outage([
'autostart' => false,
'warntime' => $now - 200,
'starttime' => $now - 100,
'stoptime' => $now - 50,
@@ -102,9 +103,10 @@ class waitforit_test extends cli_testcase {
}
public function test_activeverbose() {
$this->setAdminUser();
self::setAdminUser();
$now = time();
outagedb::save(new outage([
'autostart' => false,
'warntime' => $now - 10,
'starttime' => $now + 1,
'stoptime' => $now + 10,
@@ -121,9 +123,10 @@ class waitforit_test extends cli_testcase {
}
public function test_countdown() {
$this->setAdminUser();
self::setAdminUser();
$now = time();
outagedb::save(new outage([
'autostart' => false,
'warntime' => $now,
'starttime' => $now + 45,
'stoptime' => $now + (60 * 60),
@@ -146,9 +149,10 @@ class waitforit_test extends cli_testcase {
}
public function test_outagechanged() {
$this->setAdminUser();
self::setAdminUser();
$now = time();
$id = outagedb::save(new outage([
'autostart' => false,
'warntime' => $now,
'starttime' => $now + (2 * 60 * 60),
'stoptime' => $now + (60 * 60),

View File

@@ -39,6 +39,7 @@ class events_test extends advanced_testcase {
// Save new outage.
$now = time();
$id = outagedb::save(new outage([
'autostart' => false,
'warntime' => $now - 60,
'starttime' => 60,
'stoptime' => 120,

View File

@@ -44,6 +44,7 @@ class outagedb_test extends advanced_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.
@@ -52,8 +53,9 @@ class outagedb_test extends advanced_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),
@@ -84,6 +86,21 @@ class outagedb_test extends advanced_testcase {
outagedb::save($outage);
}
public function test_saved_fields() {
$this->resetAfterTest(true);
for ($i = 0; $i < 4; $i++) {
$expected = $this->createoutage($i);
$expected->id = outagedb::save($expected);
$actual = outagedb::get_by_id($expected->id);
// Ignore the following fields.
$expected->lastmodified = $actual->lastmodified;
$expected->createdby = $actual->createdby;
$expected->modifiedby = $actual->modifiedby;
// Check if fields are the same.
self::assertEquals($expected, $actual, 'Failed for $i='.$i);
}
}
/**
* Make sure we can get existing entries and null if not found.
*/
@@ -121,7 +138,7 @@ class outagedb_test extends advanced_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));
@@ -202,34 +219,29 @@ class outagedb_test extends advanced_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.');
self::saveoutage($now, -1, 2, 3,
self::saveoutage(false, $now, -1, 2, 3,
'Another outage in warning period, but ignored as it starts after the previous one.');
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($now, -3, -2, 1,
self::saveoutage(false, $now, -3, -2, 1,
'Another ongoing outage starting at the same time, but ignored as it stops before the previous one.');
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
}
@@ -243,41 +255,41 @@ class outagedb_test extends advanced_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)), 'Wrong future data.');
}
@@ -291,28 +303,28 @@ class outagedb_test extends advanced_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)), 'Wrong past data.');
}
@@ -325,6 +337,7 @@ class outagedb_test extends advanced_testcase {
*/
private function createoutage($i) {
return new outage([
'autostart' => ($i % 2 == 0),
'starttime' => $i * 100,
'stoptime' => $i * 100 + 50,
'warntime' => $i * 60,

View File

@@ -27,8 +27,8 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2016091500; // The current plugin version (Date: YYYYMMDDXX).
$plugin->release = $plugin->version; // Same as version
$plugin->requires = 2014051200; // Requires Moodle 2.7 or later.
$plugin->component = "auth_outage";
$plugin->maturity = MATURITY_ALPHA; // Not suitable for PRODUCTION environments yet!
$plugin->version = 2016092207; // The current plugin version (Date: YYYYMMDDXX).
$plugin->release = 'Build '.$plugin->version; // Human-readable release information.
$plugin->requires = 2014051200; // Requires Moodle 2.7 or later.
$plugin->maturity = MATURITY_ALPHA; // Not suitable for PRODUCTION environments yet!