Issue #22 - Self-refresh maintenance page every 5 minutes or the outage duration, whatever is lower.

This commit is contained in:
Daniel Thee Roperto
2016-11-14 15:43:57 +11:00
parent 3360d357d4
commit 5961e82b85
4 changed files with 89 additions and 3 deletions

View File

@@ -42,7 +42,9 @@ defined('MOODLE_INTERNAL') || die();
class maintenance_static_page {
/**
* Creates a page based on the given outage.
*
* @param outage|null $outage
*
* @return maintenance_static_page
* @throws coding_exception
*/
@@ -63,12 +65,18 @@ class maintenance_static_page {
$html = $data['contents'];
}
return self::create_from_html($html);
$page = self::create_from_html($html);
if (!is_null($outage)) {
$page->set_max_refresh_time($outage->get_duration_planned());
}
return $page;
}
/**
* Creates a page based on the given HTML.
*
* @param string|null $html
*
* @return maintenance_static_page
* @throws coding_exception
*/
@@ -96,7 +104,9 @@ class maintenance_static_page {
/**
* maintenance_static_page constructor.
*
* @param DOMDocument|null $dom
*
* @throws coding_exception
*/
protected function __construct($dom) {
@@ -117,4 +127,15 @@ class maintenance_static_page {
public function get_io() {
return $this->generator->get_io();
}
/**
* Sets the maximum amount of seconds to auto refresh the static page.
* @param int $maxsecs
*/
public function set_max_refresh_time($maxsecs) {
$current = $this->generator->get_refresh_time();
if ($maxsecs < $current) {
$this->generator->set_refresh_time($maxsecs);
}
}
}

View File

@@ -48,6 +48,9 @@ class maintenance_static_page_generator {
/** @var maintenance_static_page_io */
protected $io;
/** @var int */
protected $refreshtime = 300;
/**
* maintenance_static_page_generator constructor.
*
@@ -74,6 +77,7 @@ class maintenance_static_page_generator {
$this->io->create_resources_path();
$this->remove_script_tags();
$this->add_meta_refresh();
$this->update_link_stylesheet();
$this->update_link_favicon();
$this->update_images();
@@ -265,4 +269,29 @@ class maintenance_static_page_generator {
}
return $matches;
}
private function add_meta_refresh() {
$meta = $this->dom->createElement('meta');
$meta->setAttribute('http-equiv', 'refresh');
$meta->setAttribute('content', $this->refreshtime);
$head = $this->dom->getElementsByTagName('head')[0];
if ($head) {
$head->appendChild($meta);
}
}
/**
* @return int
*/
public function get_refresh_time() {
return $this->refreshtime;
}
/**
* @param int $refreshtime
*/
public function set_refresh_time($refreshtime) {
$this->refreshtime = $refreshtime;
}
}

View File

@@ -23,7 +23,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use auth_outage\local\cli\cli_exception;
use auth_outage\local\cli\clibase;
defined('MOODLE_INTERNAL') || die();
@@ -43,6 +42,11 @@ abstract class auth_outage_cli_testcase extends auth_outage_base_testcase {
* Always enable the auth outage plugin, resets after test and set no parameters.
*/
public function setUp() {
global $CFG;
// PHPUnit does not load config.php
$CFG->auth_outage_bootstrap_loaded = true;
// Enable auth plugins.
set_config('auth', 'outage');
\core\session\manager::gc(); // Remove stale sessions.
@@ -55,6 +59,7 @@ abstract class auth_outage_cli_testcase extends auth_outage_base_testcase {
/**
* Mocks the command line parameters.
*
* @param string[] $options Options to use as parameters.
*/
protected function set_parameters(array $options) {
@@ -65,7 +70,9 @@ abstract class auth_outage_cli_testcase extends auth_outage_base_testcase {
/**
* Executes the CLI.
*
* @param clibase $cli CLI to execute.
*
* @return string The output text.
*/
protected function execute(clibase $cli) {
@@ -81,6 +88,7 @@ abstract class auth_outage_cli_testcase extends auth_outage_base_testcase {
/**
* Sets the expected exception as cli_exception with the given error code.
*
* @param int $errorcode Error code.
*/
protected function set_expected_cli_exception($errorcode) {

View File

@@ -63,7 +63,9 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
public function test_createfromhtml() {
$html = "<!DOCTYPE html>\n<html><head><title>Title</title></head><body>Content</body></html>";
self::assertSame($html, $this->generated_page_html($html));
$expected = "<!DOCTYPE html>\n<html><head><title>Title</title><meta http-equiv=\"refresh\" content=\"300\">".
"</head><body>Content</body></html>";
self::assertSame($expected, $this->generated_page_html($html));
}
public function test_removescripttags() {
@@ -366,4 +368,30 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
self::assertContains('removeme', $generated);
self::assertContains('Goodbye cruel world', $generated);
}
public function test_meta_refresh_5minutes() {
$this->resetAfterTest(true);
$html = "<!DOCTYPE html>\n".
'<html><head><title>Title</title></head>'.
'<body>Content<b id="removeme">Goodbye cruel world.</b></body></html>';
set_config('remove_selectors', '#invalidid', 'auth_outage');
$generated = $this->generated_page_html($html);
self::assertContains('<meta http-equiv="refresh" content="300">', $generated);
}
public function test_meta_refresh_maximum_5seconds() {
$this->resetAfterTest(true);
$html = "<!DOCTYPE html>\n".
'<html><head><title>Title</title></head>'.
'<body>Content<b id="removeme">Goodbye cruel world.</b></body></html>';
set_config('remove_selectors', '#invalidid', 'auth_outage');
$page = maintenance_static_page::create_from_html($html);
$page->set_max_refresh_time(5);
$page->generate();
$generated = trim(file_get_contents($page->get_io()->get_template_file()));
return $generated;
self::assertContains('<meta http-equiv="refresh" content="5">', $generated);
}
}