Issue #22 - Infopage now is not responsible for generating a static page anymore, hooked into maintenance_static_page.

This commit is contained in:
Daniel Thee Roperto
2016-11-09 14:35:56 +11:00
parent 146ad61ede
commit afd17994fe
9 changed files with 105 additions and 485 deletions

View File

@@ -51,7 +51,7 @@ class installation_test extends auth_outage_base_testcase {
global $CFG, $DB;
$this->resetAfterTest();
$this->setAdminUser();
static::setAdminUser();
$dbman = $DB->get_manager();
// Create a future outage with autostart.
@@ -77,7 +77,7 @@ class installation_test extends auth_outage_base_testcase {
// Check ...
self::assertSame(0, $DB->count_records_select('event', "eventtype = 'auth_outage'", null),
'The outage events were not removed.');
self::assertFalse(file_exists(auth_outage\local\controllers\infopage::get_defaulttemplatefile()),
self::assertFalse(file_exists($CFG->dataroot.'/climaintenance.php'),
'The maintenance template file was not deleted.');
self::assertFalse(get_config('moodle', 'maintenance_later'),
'Maintenance later must not be set.'); // Issue #57.

View File

@@ -23,10 +23,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use auth_outage\dml\outagedb;
use auth_outage\local\controllers\infopage;
use auth_outage\local\outage;
use auth_outage\task\update_static_page;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/../../base_testcase.php');
@@ -41,20 +39,6 @@ require_once(__DIR__.'/../../base_testcase.php');
* @SuppressWarnings(public) Allow as many methods as needed.
*/
class infopagecontroller_test extends auth_outage_base_testcase {
/**
* Ensures the template file does not exist when starting a test.
*/
public function setUp() {
$file = infopage::get_defaulttemplatefile();
if (file_exists($file)) {
if (is_file($file)) {
unlink($file);
} else {
self::fail('Invalid temp file: '.$file);
}
}
}
/**
* Tests the constructor.
*/
@@ -95,226 +79,6 @@ class infopagecontroller_test extends auth_outage_base_testcase {
new infopage(['outage' => 'My outage']);
}
/**
* Tests the static page contents.
*/
public function test_staticpage_output() {
global $PAGE;
$this->resetAfterTest(true);
$PAGE->set_context(context_system::instance());
$now = time();
$outage = new outage([
'id' => 1,
'starttime' => $now + (60 * 60),
'warntime' => $now - (60 * 60),
'stoptime' => $now + (2 * 60 * 60),
'title' => 'Outage Title at {{start}}',
'description' => 'This is an <b>important</b> outage, starting at {{start}}.',
]);
$info = new infopage(['static' => true, 'outage' => $outage]);
$html = $info->get_output();
// Must find...
self::assertContains('<!DOCTYPE html>', $html);
self::assertContains('<meta http-equiv="refresh" content="'.(60 * 60).'">', $html); // Issue #53.
self::assertContains('</html>', $html);
self::assertContains($outage->get_title(), $html);
self::assertContains($outage->get_description(), $html);
// Must not find...
self::assertNotContains('<link ', $html);
self::assertNotContains('<a ', $html);
self::assertNotContains('<script ', $html);
// Ensure it has the id encoded in it...
self::assertSame($outage->id, infopage::find_outageid_from_infopage($html));
}
/**
* Tests the static page file contents.
*/
public function test_staticpage_file() {
$now = time();
$outage = new outage([
'id' => 1,
'warntime' => $now - 100,
'starttime' => $now + 100,
'stoptime' => $now + 200,
'title' => 'Title',
'description' => 'Description',
]);
$file = infopage::get_defaulttemplatefile();
infopage::save_static_page($outage, $file);
self::assertFileExists($file);
$id = infopage::find_outageid_from_infopage(file_get_contents($file));
self::assertSame($outage->id, $id);
unlink($file);
}
/**
* Checks if the default template file is a valid string with the name templage.
*/
public function test_getdefaulttemplatefile() {
$file = infopage::get_defaulttemplatefile();
self::assertTrue(is_string($file));
self::assertContains('template', $file);
}
/**
* Tests infopage::find_outageid_from_infopage() with an invalid parameter.
*/
public function test_findoutageid_notstring() {
$this->set_expected_exception('coding_exception');
infopage::find_outageid_from_infopage(new stdClass());
}
/**
* Tests infopage::find_outageid_from_infopage() when the id is not found.
*/
public function test_findoutageid_notfound() {
self::assertNull(
infopage::find_outageid_from_infopage(
'<html><head><title>Hello world!</title></head><body>Done.</body></html>'
)
);
}
/**
* Tests infopage::save_static_page() with an invalid parameter.
*/
public function test_savestaticpage_filenotstring() {
$this->set_expected_exception('coding_exception');
infopage::save_static_page(new outage(), 1);
}
/**
* Tests infopage::save_static_page() with an invalid filename.
*/
public function test_savestaticpage_fileinvalid() {
global $CFG;
$outage = new outage([
'id' => 1,
'warntime' => time() - 100,
'starttime' => time() + 100,
'stoptime' => time() + 200,
'title' => 'Title',
'description' => 'Description',
]);
$this->set_expected_exception('file_exception');
infopage::save_static_page($outage, $CFG->dataroot.'/someinvalidpath/someinvalidfile');
}
/**
* Tests infopage::test_sanity_notstring() with invalid contents: an int.
*/
public function test_sanity_notstring() {
$this->set_expected_exception('invalid_state_exception');
infopage::save_static_page_sanitycheck(404);
}
/**
* Tests infopage::test_sanity_notstring() with invalid contents: an empty string.
*/
public function test_sanity_empty() {
$this->set_expected_exception('invalid_state_exception');
infopage::save_static_page_sanitycheck(' ');
}
/**
* Tests infopage::test_sanity_notstring() with invalid contents: an empty HTML.
*/
public function test_sanity_clearhtml() {
$this->set_expected_exception('invalid_state_exception');
infopage::save_static_page_sanitycheck('<html><head></head><body><b> <!-- Nothing --> </b></body></html>');
}
/**
* Tests updating the static page.
*/
public function test_updatestaticpage() {
$this->resetAfterTest(true);
self::setAdminUser();
$file = infopage::get_defaulttemplatefile();
$now = time();
$outage = new outage([
'autostart' => false,
'warntime' => $now - 100,
'starttime' => $now + 100,
'stoptime' => $now + 200,
'title' => 'Title',
'description' => 'Description',
]);
$id = outagedb::save($outage);
// The method update_static_page should have been called by save().
self::assertFileExists($file);
$idfound = infopage::find_outageid_from_infopage(file_get_contents($file));
self::assertSame($id, $idfound);
unlink($file);
}
/**
* Tests updating the static page when there is no outage.
*/
public function test_updatestaticpage_nooutage() {
infopage::update_static_page(null);
}
/**
* Tests updating the static page when there is no outage but the file existed before.
*/
public function test_updatestaticpage_hasfile() {
$file = infopage::get_defaulttemplatefile();
touch($file);
self::assertFileExists($file);
infopage::update_static_page(null);
self::assertFileNotExists($file);
}
/**
* Tests updating the static page with an invalid filename.
*/
public function test_updatestaticpage_invalidfile() {
$this->set_expected_exception('coding_exception');
infopage::update_static_page(null, 123);
}
/**
* Tests updating the static page with an invalid outage.
*/
public function test_updatestaticpage_invalidoutage() {
$this->set_expected_exception('coding_exception');
infopage::update_static_page("foobar");
}
/**
* Checks if infopage::has_admin_options() works as expected.
*/
public function test_hasadminoptions() {
$this->resetAfterTest(true);
$static = new infopage(['static' => true]);
$nonstatic = new infopage(['static' => false]);
// Now I am guest.
self::assertFalse(is_siteadmin());
self::assertFalse($static->has_admin_options());
self::assertFalse($nonstatic->has_admin_options());
// Now I am admin.
self::setAdminUser();
self::assertTrue(is_siteadmin());
self::assertFalse($static->has_admin_options());
self::assertTrue($nonstatic->has_admin_options());
}
/**
* Tries to output a static page without a defined outage.
*/
public function test_output_static_nooutage() {
$info = new infopage(['static' => true]);
$this->set_expected_exception('coding_exception');
$info->output();
}
/**
* We should have an exception because CLI cannot redirect.
*/
@@ -342,14 +106,4 @@ class infopagecontroller_test extends auth_outage_base_testcase {
$output = $info->get_output();
self::assertContains('auth_outage_info', $output);
}
/**
* Checks if we can create and execute a task to update outage pages.
*/
public function test_tasks() {
$this->resetAfterTest(true);
$task = new update_static_page();
self::assertNotEmpty($task->get_name());
$task->execute();
}
}

View File

@@ -24,6 +24,7 @@
*/
use auth_outage\local\controllers\maintenance_static_page;
use auth_outage\task\update_static_page;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/../../base_testcase.php');
@@ -139,4 +140,26 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
$generated = trim(file_get_contents($page->get_template_file()));
return $generated;
}
/**
* Checks if we can create and execute a task to update outage pages.
*/
public function test_tasks() {
$this->resetAfterTest(true);
$task = new update_static_page();
self::assertNotEmpty($task->get_name());
$task->execute();
}
/**
* Tests updating the static page when there is no outage but the file existed before.
*/
public function test_updatestaticpage_hasfile() {
global $CFG;
$file = $CFG->dataroot.'/climaintenance.template.html';
touch($file);
self::assertFileExists($file);
maintenance_static_page::create_from_outage(null)->generate();
self::assertFileNotExists($file);
}
}

View File

@@ -409,7 +409,7 @@ EOT;
outagedb::save($outage);
// The method outagelib::prepare_next_outage() should have been called by save().
foreach ([infopage::get_defaulttemplatefile(), $CFG->dataroot.'/climaintenance.php'] as $file) {
foreach ([$CFG->dataroot.'/climaintenance.template.html', $CFG->dataroot.'/climaintenance.php'] as $file) {
self::assertFileExists($file);
unlink($file);
}