Issue #46 - Added tests for every class, except outputs. Small bugs/invalid parameter detection fixed together.

This commit is contained in:
Daniel Thee Roperto
2016-09-26 19:34:27 +10:00
parent 619a5a663f
commit 8791727ba4
28 changed files with 1090 additions and 239 deletions

View File

@@ -0,0 +1,112 @@
<?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/>.
use auth_outage\calendar\calendar;
use auth_outage\local\outage;
defined('MOODLE_INTERNAL') || die();
/**
* Tests performed on outage class.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class calendar_test extends advanced_testcase {
public function test_create() {
self::setAdminUser();
$this->resetAfterTest(false);
$time = time();
$outage = new outage([
'id' => 1,
'autostart' => false,
'warntime' => $time - 100,
'starttime' => $time,
'stoptime' => $time + (2 * 60 * 60),
'title' => 'Title',
'description' => 'Description',
]);
calendar::create($outage);
$this->check_calendar($outage);
return $outage;
}
/**
* @depends test_create
*/
public function test_update(outage $outage) {
self::setAdminUser();
$this->resetAfterTest(false);
$outage->title = 'New Title';
calendar::update($outage);
$this->check_calendar($outage);
return $outage;
}
/**
* @depends test_update
*/
public function test_delete($outage) {
self::setAdminUser();
$this->resetAfterTest(true);
calendar::delete($outage->id);
self::assertNull(calendar::load($outage->id));
}
public function test_update_notfound() {
self::setAdminUser();
$this->resetAfterTest(true);
$time = time();
$outage = new outage([
'id' => 1,
'autostart' => false,
'warntime' => $time - 100,
'starttime' => $time,
'stoptime' => $time + (2 * 60 * 60),
'title' => 'Title',
'description' => 'Description',
]);
calendar::update($outage);
self::assertCount(1, phpunit_util::get_debugging_messages());
phpunit_util::reset_debugging();
}
public function test_delete_notfound() {
self::setAdminUser();
$this->resetAfterTest(true);
calendar::delete(1);
self::assertCount(1, phpunit_util::get_debugging_messages());
phpunit_util::reset_debugging();
}
private function check_calendar(outage $outage) {
$calendar = calendar::load($outage->id);
self::assertSame($outage->title, $calendar->name);
self::assertSame($outage->description, $calendar->description);
self::assertSame('auth_outage', $calendar->eventtype);
self::assertEquals($outage->starttime, $calendar->timestart);
self::assertEquals($outage->get_duration_planned(), $calendar->timeduration);
}
}

View File

@@ -22,10 +22,11 @@ defined('MOODLE_INTERNAL') || die();
/**
* Tests performed on outage class.
*
* @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
* @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
* @SuppressWarnings("public") Allow this test to have as many tests as necessary.
*/
class outagedb_test extends advanced_testcase {
/**
@@ -329,6 +330,106 @@ class outagedb_test extends advanced_testcase {
self::createidarray(outagedb::get_all_ended($now)), 'Wrong past data.');
}
/**
* @expectedException coding_exception
*/
public function test_getbyid_invalid() {
$this->resetAfterTest(true);
outagedb::get_by_id(-1);
}
/**
* @expectedException coding_exception
*/
public function test_delete_invalid() {
$this->resetAfterTest(true);
outagedb::delete(-1);
}
/**
* @expectedException coding_exception
*/
public function test_getactive_invalid() {
$this->resetAfterTest(true);
outagedb::get_active(-1);
}
/**
* @expectedException coding_exception
*/
public function test_getallunended_invalid() {
$this->resetAfterTest(true);
outagedb::get_all_unended(-1);
}
public function test_getallunended_now() {
$this->resetAfterTest(true);
outagedb::get_all_unended();
}
/**
* @expectedException coding_exception
*/
public function test_getallended_invalid() {
$this->resetAfterTest(true);
outagedb::get_all_ended(-1);
}
public function test_getallended_now() {
$this->resetAfterTest(true);
outagedb::get_all_ended();
}
/**
* @expectedException coding_exception
*/
public function test_finish_invalid() {
$this->resetAfterTest(true);
outagedb::finish(1, -1);
}
public function test_finish_now_notfound() {
$this->resetAfterTest(true);
outagedb::finish(1);
self::assertCount(1, phpunit_util::get_debugging_messages());
phpunit_util::reset_debugging();
}
public function test_finish_notongoing() {
$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),
'title' => 'Title',
'description' => 'Description',
]);
$id = outagedb::save($outage);
self::assertTrue(!$outage->is_ongoing($time));
outagedb::finish($id, $time);
self::assertCount(1, phpunit_util::get_debugging_messages());
phpunit_util::reset_debugging();
}
/**
* @expectedException coding_exception
*/
public function test_getnextstartinginvalid() {
$this->resetAfterTest(true);
outagedb::get_next_starting(-1);
}
/**
* @expectedException coding_exception
*/
public function test_getnextautostartinginvalid() {
$this->resetAfterTest(true);
outagedb::get_next_autostarting(-1);
}
/**
* Helper function to create an outage for tests.
*

View File

@@ -26,9 +26,6 @@ defined('MOODLE_INTERNAL') || die();
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\event\outage_created
* @covers \auth_outage\event\outage_updated
* @covers \auth_outage\event\outage_deleted
*/
class events_test extends advanced_testcase {
public function test_save() {

View File

@@ -0,0 +1,130 @@
<?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/>.
use auth_outage\form\outage\delete;
use auth_outage\form\outage\edit;
use auth_outage\form\outage\finish;
use auth_outage\local\outage;
defined('MOODLE_INTERNAL') || die();
/**
* Tests performed on forms classes.
*
* @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 forms_test extends advanced_testcase {
public function test_delete() {
new delete();
}
public function test_finish() {
new finish();
}
public function test_edit_valid() {
$this->mock_edit_post();
$edit = new edit();
self::assertFalse($edit->is_cancelled());
$outage = $edit->get_data();
self::assertInstanceOf(outage::class, $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());
self::assertSame('The title.', $outage->title);
self::assertSame('The <b>description</b>.', $outage->description);
}
public function test_edit_invalid_warning() {
$this->mock_edit_post();
$_POST['warningduration'] = ['number' => '-1', 'timeunit' => '60'];
$edit = new edit();
$outage = $edit->get_data();
self::assertNull($outage);
}
public function test_edit_invalid_duration() {
$this->mock_edit_post();
$_POST['outageduration'] = ['number' => '-2', 'timeunit' => '3600'];
$edit = new edit();
self::assertNull($edit->get_data());
}
public function test_edit_invalid_title() {
$this->mock_edit_post();
$_POST['title'] = '';
$edit = new edit();
self::assertNull($edit->get_data());
}
public function test_edit_invalid_title_toolong() {
$this->mock_edit_post();
$_POST['title'] = 'This is a very long time, it is so long that at some point it should not be valid. '.
'With a very long title used in this place we should get a form validation error. '.
'Do you think this title is long enough?';
$edit = new edit();
self::assertNull($edit->get_data());
}
public function test_edit_description_invalid_format() {
$this->mock_edit_post();
$_POST['description'] = ['text' => 'The <b>description</b>.', 'format' => '2'];
$edit = new edit();
self::assertNull($edit->get_data());
self::assertCount(1, phpunit_util::get_debugging_messages());
phpunit_util::reset_debugging();
}
public function test_setdata() {
$outage = new outage([
'autostart' => false,
'warntime' => time() - 60,
'starttime' => time(),
'stoptime' => time() + 60,
'title' => 'Title',
'description' => 'Description',
]);
$edit = new edit();
$edit->set_data($outage);
}
/**
* @expectedException coding_exception
*/
public function test_setdata_invalid() {
$edit = new edit();
$edit->set_data(null);
}
private function mock_edit_post() {
// There is a bug in moodleform::mock_submit so we make our own version.
$_POST = [
'id' => '1',
'sesskey' => sesskey(),
'_qf__auth_outage_form_outage_edit' => '1',
'warningduration' => ['number' => '1', 'timeunit' => '60'],
'starttime' => ['day' => '1', 'month' => '2', 'year' => '2013', 'hour' => '14', 'minute' => '15'],
'outageduration' => ['number' => '2', 'timeunit' => '3600'],
'title' => 'The title.',
'description' => ['text' => 'The <b>description</b>.', 'format' => '1'],
'submitbutton' => 'Save changes',
];
}
}

View File

@@ -1,104 +0,0 @@
<?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/>.
use auth_outage\local\controllers\infopage;
use auth_outage\local\outage;
defined('MOODLE_INTERNAL') || die();
/**
* Tests performed on infopage_controller class.
*
* @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
* @covers \auth_outage\local\controllers\infopage
*/
class infopagecontroller_test extends advanced_testcase {
public function setUp() {
if (file_exists($this->get_file())) {
if (is_file($this->get_file())) {
unlink($this->get_file());
} else {
self::fail('Invalid temp file: '.$this->get_file());
}
}
}
/**
* Return a temporary file name to use for this test.
* @return string Default file.
*/
public function get_file() {
return sys_get_temp_dir().'/phpunit_authoutage.tmp';
}
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" ', $html);
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));
}
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',
]);
infopage::save_static_page($outage, $this->get_file());
self::assertFileExists($this->get_file());
$id = infopage::find_outageid_from_infopage(file_get_contents($this->get_file()));
self::assertSame($outage->id, $id);
unlink($this->get_file());
}
public function test_getdefaulttemplatefile() {
$file = infopage::get_defaulttemplatefile();
self::assertTrue(is_string($file));
self::assertContains('template', $file);
}
}

View File

@@ -27,8 +27,6 @@ require_once(__DIR__.'/cli_testcase.php');
* @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
* @covers \auth_outage\local\cli\clibase
* @covers \auth_outage\local\cli\cli_exception
* @SuppressWarnings("public")
*/
class cli_test extends cli_testcase {

View File

@@ -25,10 +25,10 @@ require_once(__DIR__.'/cli_testcase.php');
/**
* Tests performed on CLI create class.
*
* @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
* @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
* @SuppressWarnings("public") Allow this test to have as many tests as necessary.
*/
class create_test extends cli_testcase {
@@ -273,4 +273,29 @@ class create_test extends cli_testcase {
self::assertContains('created', $text);
self::assertContains('started', $text);
}
/**
* @expectedException coding_exception
*/
public function test_setdefaults_extra() {
$cli = new create([]);
$cli->set_defaults(['aninvalidparameter' => 'value']);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 3
*/
public function test_invalid_bool() {
$this->set_parameters([
'--autostart=maybe',
'--warn=60',
'--start=0',
'--duration=600',
'--title=Title',
'--description=Description',
]);
$cli = new create();
$cli->execute();
}
}

View File

@@ -25,11 +25,10 @@ require_once(__DIR__.'/cli_testcase.php');
/**
* Tests performed on CLI finish class.
*
* @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
* @covers \auth_outage\local\cli\finish
* @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 finish_test extends cli_testcase {
public function test_constructor() {

View File

@@ -24,11 +24,10 @@ require_once(__DIR__.'/cli_testcase.php');
/**
* Tests performed on CLI waitforit class.
*
* @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
* @covers \auth_outage\local\cli\waitforit
* @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
* @SuppressWarnings("public")
*/
class waitforit_test extends cli_testcase {

View File

@@ -0,0 +1,285 @@
<?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/>.
use auth_outage\dml\outagedb;
use auth_outage\local\controllers\infopage;
use auth_outage\local\outage;
defined('MOODLE_INTERNAL') || die();
/**
* Tests performed on infopage controller class and update_static_page task class.
*
* @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
* @SuppressWarnings("public") Allow this test to have as many tests as necessary.
*/
class infopagecontroller_test extends advanced_testcase {
public function setUp() {
$file = infopage::get_defaulttemplatefile();
if (file_exists($file)) {
if (is_file($file)) {
unlink($file);
} else {
self::fail('Invalid temp file: '.$file);
}
}
}
public function test_constructor() {
new infopage();
}
public function test_constructor_withparams() {
$_GET = ['id' => 1, 'static' => 'true'];
new infopage();
}
/**
* @expectedException coding_exception
*/
public function test_constructor_idmismatch() {
$outage = new outage([
'id' => 1,
'autostart' => false,
'warntime' => time() - 60,
'starttime' => time(),
'stoptime' => time() + 60,
'title' => 'Title',
'description' => 'Description',
]);
new infopage(['id' => 2, 'outage' => $outage]);
}
/**
* @expectedException coding_exception
*/
public function test_constructor_invalidoutage() {
new infopage(['outage' => 'My outage']);
}
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" ', $html);
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));
}
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);
}
public function test_getdefaulttemplatefile() {
$file = infopage::get_defaulttemplatefile();
self::assertTrue(is_string($file));
self::assertContains('template', $file);
}
/**
* @expectedException coding_exception
*/
public function test_findoutageid_notstring() {
infopage::find_outageid_from_infopage(new stdClass());
}
public function test_findoutageid_notfound() {
self::assertNull(
infopage::find_outageid_from_infopage(
'<html><head><title>Hello world!</title></head><body>Done.</body></html>'
)
);
}
/**
* @expectedException coding_exception
*/
public function test_savestaticpage_filenotstring() {
infopage::save_static_page(new outage(), 1);
}
/**
* @expectedException file_exception
*/
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',
]);
infopage::save_static_page($outage, $CFG->dataroot.'/someinvalidpath/someinvalidfile');
}
/**
* @expectedException invalid_state_exception
*/
public function test_sanity_notstring() {
infopage::save_static_page_sanitycheck(404);
}
/**
* @expectedException invalid_state_exception
*/
public function test_sanity_empty() {
infopage::save_static_page_sanitycheck(' ');
}
/**
* @expectedException invalid_state_exception
*/
public function test_sanity_clearhtml() {
infopage::save_static_page_sanitycheck('<html><head></head><body><b> <!-- Nothing --> </b></body></html>');
}
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);
}
public function test_updatestaticpage_nooutage() {
infopage::update_static_page();
}
public function test_updatestaticpage_hasfile() {
$file = infopage::get_defaulttemplatefile();
touch($file);
self::assertFileExists($file);
infopage::update_static_page();
self::assertFileNotExists($file);
}
/**
* @expectedException coding_exception
*/
public function test_updatestaticpage_invalidfile() {
infopage::update_static_page(123);
}
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());
}
/**
* @expectedException coding_exception
*/
public function test_output_static_nooutage() {
$info = new infopage(['static' => true]);
$info->output();
}
/**
* We should have an exception because CLI cannot redirect.
* @expectedException moodle_exception
*/
public function test_output_nonstatic_nooutage() {
$info = new infopage(['static' => false]);
$info->output();
}
public function test_output() {
$now = time();
$outage = new outage([
'id' => 1,
'autostart' => false,
'warntime' => $now - 100,
'starttime' => $now + 100,
'stoptime' => $now + 200,
'title' => 'Title',
'description' => 'Description',
]);
$info = new infopage(['outage' => $outage]);
self::hasExpectationOnOutput();
$output = $info->get_output();
self::assertContains('auth_outage_info', $output);
}
public function test_tasks() {
$task = new \auth_outage\task\update_static_page();
self::assertNotEmpty($task->get_name());
$task->execute();
}
}

View File

@@ -25,7 +25,6 @@ defined('MOODLE_INTERNAL') || die();
* @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
* @covers \auth_outage\local\outage
*/
class outage_test extends basic_testcase {
public function test_constructor() {
@@ -38,6 +37,55 @@ class outage_test extends basic_testcase {
}
}
public function test_constructor_object() {
$obj = new stdClass();
$obj->id = 1;
$obj->autostart = true;
$obj->warntime = 2;
$obj->starttime = 3;
$obj->finished = 4;
$obj->stoptime = 5;
$obj->title = 'Title';
$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);
self::assertSame($obj->stoptime, $outage->stoptime);
self::assertSame($obj->title, $outage->title);
self::assertSame($obj->description, $outage->description);
}
/**
* @expectedException coding_exception
*/
public function test_constructor_invalid() {
new outage('My outage');
}
public function test_getstage_now() {
$now = time();
// Make sure it is in the past.
$outage = new outage([
'starttime' => $now - (3 * 60 * 60),
'stoptime' => $now - (2 * 60 * 60),
'warntime' => $now - (2 * 60 * 60),
'title' => '',
'description' => '',
]);
self::assertSame(outage::STAGE_STOPPED, $outage->get_stage());
}
/**
* @expectedException coding_exception
*/
public function test_getstage_invalidtime() {
$outage = new outage();
$outage->get_stage(-1);
}
public function test_isongoing() {
$now = time();
@@ -128,6 +176,7 @@ class outage_test extends basic_testcase {
self::assertSame(outage::STAGE_WAITING, $outage->get_stage($now));
self::assertFalse($outage->is_active($now));
self::assertFalse($outage->is_ongoing($now));
self::assertFalse($outage->has_ended());
$outage = new outage([
'warntime' => $now - 10,
@@ -138,6 +187,7 @@ class outage_test extends basic_testcase {
self::assertSame(outage::STAGE_WARNING, $outage->get_stage($now));
self::assertTrue($outage->is_active($now));
self::assertFalse($outage->is_ongoing($now));
self::assertFalse($outage->has_ended());
$outage = new outage([
'warntime' => $now - 20,
@@ -148,6 +198,7 @@ class outage_test extends basic_testcase {
self::assertSame(outage::STAGE_ONGOING, $outage->get_stage($now));
self::assertTrue($outage->is_active($now));
self::assertTrue($outage->is_ongoing($now));
self::assertFalse($outage->has_ended());
$outage = new outage([
'warntime' => $now - 50,
@@ -158,6 +209,7 @@ class outage_test extends basic_testcase {
self::assertSame(outage::STAGE_STOPPED, $outage->get_stage($now));
self::assertFalse($outage->is_active($now));
self::assertFalse($outage->is_ongoing($now));
self::assertTrue($outage->has_ended());
$outage = new outage([
'warntime' => $now - 50,
@@ -169,6 +221,7 @@ class outage_test extends basic_testcase {
self::assertSame(outage::STAGE_FINISHED, $outage->get_stage($now));
self::assertFalse($outage->is_active($now));
self::assertFalse($outage->is_ongoing($now));
self::assertTrue($outage->has_ended());
$outage = new outage([
'warntime' => $now - 50,
@@ -180,5 +233,38 @@ class outage_test extends basic_testcase {
self::assertSame(outage::STAGE_FINISHED, $outage->get_stage($now));
self::assertFalse($outage->is_active($now));
self::assertFalse($outage->is_ongoing($now));
self::assertTrue($outage->has_ended());
}
public function test_gettitle_getdescription() {
$now = time();
$outage = new outage([
'warntime' => $now - 50,
'starttime' => $now - 40,
'stoptime' => $now - 30,
'finished' => $now - 20,
'title' => 'Title {{start}} {{stop}} {{duration}}',
'description' => 'Description {{start}} {{stop}} {{duration}}',
]);
$title = $outage->get_title();
self::assertNotContains('{', $title);
self::assertNotContains('}', $title);
$description = $outage->get_description();
self::assertNotContains('{', $description);
self::assertNotContains('}', $description);
}
public function test_getdurations() {
$outage = new outage(['starttime' => 1000]);
self::assertNull($outage->get_duration_actual());
$outage->finished = 3000;
self::assertSame(2000, $outage->get_duration_actual());
$outage->stoptime = 3050;
self::assertEquals(2050, $outage->get_duration_planned());
$outage->warntime = 600;
self::assertEquals(400, $outage->get_warning_duration());
}
}

View File

@@ -0,0 +1,152 @@
<?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/>.
use auth_outage\dml\outagedb;
use auth_outage\local\outage;
use auth_outage\local\outagelib;
defined('MOODLE_INTERNAL') || die();
/**
* Tests performed on outagelib class.
*
* @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 outagelib_test extends advanced_testcase {
public function test_maintenancemessage() {
$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.');
outagedb::save($outage);
self::assertFalse((bool)get_config('moodle', 'maintenance_message'));
self::assertCount(2, phpunit_util::get_debugging_messages());
phpunit_util::reset_debugging();
}
public function test_maintenancelater_nonext() {
$this->resetAfterTest(true);
set_config('maintenance_later', time() + (60 * 60 * 24 * 7)); // In 1 week.
self::assertNotEmpty(get_config('moodle', 'maintenance_later'));
outagelib::outages_modified();
self::assertEmpty(get_config('moodle', 'maintenance_later'));
}
public function test_inject() {
global $CFG;
$this->resetAfterTest(true);
self::setAdminUser();
$now = time();
$outage = new outage([
'autostart' => true,
'warntime' => $now,
'starttime' => $now + 100,
'stoptime' => $now + 200,
'title' => 'Title',
'description' => 'Description',
]);
$outage->id = outagedb::save($outage);
self::assertEmpty($CFG->additionalhtmltopofbody);
outagelib::reinject();
self::assertContains('<style>', $CFG->additionalhtmltopofbody);
self::assertContains('<script>', $CFG->additionalhtmltopofbody);
// Should not inject again.
$size = strlen($CFG->additionalhtmltopofbody);
outagelib::inject();
self::assertSame($size, strlen($CFG->additionalhtmltopofbody));
}
public function test_inject_broken() {
global $CFG;
$_GET = ['auth_outage_break_code' => '1'];
outagelib::reinject();
self::assertCount(2, phpunit_util::get_debugging_messages());
phpunit_util::reset_debugging();
}
public function test_inject_preview() {
global $CFG;
$this->resetAfterTest(true);
self::setAdminUser();
$now = time();
$outage = new outage([
'autostart' => true,
'warntime' => $now,
'starttime' => $now + 100,
'stoptime' => $now + 200,
'title' => 'Title',
'description' => 'Description',
]);
$outage->id = outagedb::save($outage);
self::assertEmpty($CFG->additionalhtmltopofbody);
$_GET = ['auth_outage_preview' => (string)$outage->id];
outagelib::reinject();
self::assertContains('<style>', $CFG->additionalhtmltopofbody);
self::assertContains('<script>', $CFG->additionalhtmltopofbody);
}
public function test_inject_preview_notfound() {
global $CFG;
self::assertEmpty($CFG->additionalhtmltopofbody);
$_GET = ['auth_outage_preview' => '1'];
// Should not throw exception or halt anything, silently ignore it.
outagelib::reinject();
self::assertEmpty($CFG->additionalhtmltopofbody);
}
public function test_inject_preview_withdelta() {
global $CFG;
$this->resetAfterTest(true);
self::setAdminUser();
$now = time();
$outage = new outage([
'autostart' => true,
'warntime' => $now,
'starttime' => $now + 100,
'stoptime' => $now + 200,
'title' => 'Title',
'description' => 'Description',
]);
$outage->id = outagedb::save($outage);
self::assertEmpty($CFG->additionalhtmltopofbody);
$_GET = ['auth_outage_preview' => (string)$outage->id, 'auth_outage_delta' => '500'];
outagelib::reinject();
// Still empty, delta is too high (outage ended).
self::assertEmpty($CFG->additionalhtmltopofbody);
}
public function test_inject_noactive() {
outagelib::reinject();
}
}

View File

@@ -1,50 +0,0 @@
<?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/>.
use auth_outage\dml\outagedb;
use auth_outage\local\outage;
defined('MOODLE_INTERNAL') || die();
/**
* Tests performed on outagelib class.
*
* @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 outagelib_test extends advanced_testcase {
public function test_maintenancemessage() {
global $CFG;
$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.');
outagedb::save($outage);
self::assertFalse((bool)get_config('moodle', 'maintenance_message'));
}
}