diff --git a/classes/local/controllers/maintenance_static_page.php b/classes/local/controllers/maintenance_static_page.php index 7c2257b..0935257 100644 --- a/classes/local/controllers/maintenance_static_page.php +++ b/classes/local/controllers/maintenance_static_page.php @@ -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); + } + } } diff --git a/classes/local/controllers/maintenance_static_page_generator.php b/classes/local/controllers/maintenance_static_page_generator.php index bb6bde4..82620ac 100644 --- a/classes/local/controllers/maintenance_static_page_generator.php +++ b/classes/local/controllers/maintenance_static_page_generator.php @@ -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; + } } diff --git a/tests/phpunit/local/cli/cli_testcase.php b/tests/phpunit/local/cli/cli_testcase.php index dc620df..32fec0e 100644 --- a/tests/phpunit/local/cli/cli_testcase.php +++ b/tests/phpunit/local/cli/cli_testcase.php @@ -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) { diff --git a/tests/phpunit/local/controllers/maintenance_static_page_test.php b/tests/phpunit/local/controllers/maintenance_static_page_test.php index 89f74ce..f1babc2 100644 --- a/tests/phpunit/local/controllers/maintenance_static_page_test.php +++ b/tests/phpunit/local/controllers/maintenance_static_page_test.php @@ -63,7 +63,9 @@ class maintenance_static_page_test extends auth_outage_base_testcase { public function test_createfromhtml() { $html = "\nTitleContent"; - self::assertSame($html, $this->generated_page_html($html)); + $expected = "\nTitle". + "Content"; + 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 = "\n". + 'Title'. + 'ContentGoodbye cruel world.'; + set_config('remove_selectors', '#invalidid', 'auth_outage'); + $generated = $this->generated_page_html($html); + + self::assertContains('', $generated); + } + + public function test_meta_refresh_maximum_5seconds() { + $this->resetAfterTest(true); + $html = "\n". + 'Title'. + 'ContentGoodbye cruel world.'; + 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('', $generated); + } }