WR489688: ci fixes

This commit is contained in:
Sarah Cotton
2026-06-25 15:30:40 +01:00
parent 7b2106555b
commit ff63eb4233
55 changed files with 677 additions and 583 deletions
+22 -20
View File
@@ -30,7 +30,7 @@ use auth_outage\local\outage;
use Behat\Gherkin\Node\TableNode;
use Behat\Mink\Exception\ExpectationException;
require_once(__DIR__.'/../../../../lib/behat/behat_base.php');
require_once(__DIR__ . '/../../../../lib/behat/behat_base.php');
/**
* Steps definitions related to auth_outage.
@@ -90,8 +90,8 @@ class behat_auth_outage extends behat_base {
$data = [
'autostart' => false,
'finished' => null,
'title' => 'Example of '.$type.' outage',
'description' => 'An outage: '.$type,
'title' => 'Example of ' . $type . ' outage',
'description' => 'An outage: ' . $type,
];
switch ($type) {
case 'waiting':
@@ -121,7 +121,7 @@ class behat_auth_outage extends behat_base {
$data['stoptime'] = time() - (60 * 60 * 2); // Stopped 1 hour ago.
break;
default:
throw new InvalidArgumentException('$type='.$type.' is not valid.');
throw new InvalidArgumentException('$type=' . $type . ' is not valid.');
}
outagedb::save(new outage($data));
}
@@ -136,8 +136,8 @@ class behat_auth_outage extends behat_base {
$expected = ($action == 'Edit') ? 2 : 1; // Edit is an action through the title or button.
$found = $this->how_many_times_can_i_see_action($action);
if ($found != $expected) {
throw new ExpectationException('"'.$action.'" action not found, expected '.$expected.
' but found '.$found.'.', $this->getSession());
throw new ExpectationException('"' . $action . '" action not found, expected ' . $expected .
' but found ' . $found . '.', $this->getSession());
}
}
@@ -149,7 +149,7 @@ class behat_auth_outage extends behat_base {
*/
public function i_should_not_see_the_action($action) {
if ($this->how_many_times_can_i_see_action($action) != 0) {
throw new ExpectationException('"'.$action.'" action was found', $this->getSession());
throw new ExpectationException('"' . $action . '" action was found', $this->getSession());
}
}
@@ -159,7 +159,7 @@ class behat_auth_outage extends behat_base {
* @param string $name
*/
public function i_should_see_an_empty_settings_text_area($name) {
$this->assertSession()->fieldValueEquals('s_auth_outage_'.$name, '');
$this->assertSession()->fieldValueEquals('s_auth_outage_' . $name, '');
}
/**
@@ -177,7 +177,7 @@ class behat_auth_outage extends behat_base {
*/
private function how_many_times_can_i_see_action($action) {
$selector = 'css';
$locator = "div[role='main'] a[title='".$action."']";
$locator = "div[role='main'] a[title='" . $action . "']";
$items = $this->getSession()->getPage()->findAll($selector, $locator);
return count($items);
}
@@ -188,7 +188,7 @@ class behat_auth_outage extends behat_base {
* @param string $action Action button to click.
*/
public function i_click_on_the_action_button($action) {
$node = $this->get_selected_node('css_element', "div[role='main'] table nobr a[title='".$action."']");
$node = $this->get_selected_node('css_element', "div[role='main'] table nobr a[title='" . $action . "']");
$this->ensure_node_is_visible($node);
$node->click();
}
@@ -205,7 +205,7 @@ class behat_auth_outage extends behat_base {
$count = count($this->getSession()->getWindowNames());
if ($count != 2) {
throw new ExpectationException('Number of windows: '.$count, $this->getSession());
throw new ExpectationException('Number of windows: ' . $count, $this->getSession());
}
}
@@ -220,17 +220,17 @@ class behat_auth_outage extends behat_base {
$container = $this->getSession()->getPage()->findAll('css', $element);
if (count($container) == 0) {
throw new ExpectationException('"'.$element.'" element not found', $this->getSession());
throw new ExpectationException('"' . $element . '" element not found', $this->getSession());
}
$container = $container[0];
$xpathliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($text);
$xpath = "/descendant-or-self::*[contains(., $xpathliteral)]".
$xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
"[count(descendant::*[contains(., $xpathliteral)]) = 0]";
$found = $this->find_all('xpath', $xpath, false, $container);
if (count($found) == 0) {
throw new ExpectationException('"'.$text.'" text was not found in the "'.$element.'" element', $this->getSession());
throw new ExpectationException('"' . $text . '" text was not found in the "' . $element . '" element', $this->getSession());
}
foreach ($found as $node) {
@@ -238,8 +238,10 @@ class behat_auth_outage extends behat_base {
return;
}
}
throw new ExpectationException('"'.$text.'" text was found in the "'.$element.'" element but was not visible',
$this->getSession());
throw new ExpectationException(
'"' . $text . '" text was found in the "' . $element . '" element but was not visible',
$this->getSession()
);
}
/**
@@ -252,7 +254,7 @@ class behat_auth_outage extends behat_base {
$locator = "#auth_outage_warningbar_box";
$items = $this->getSession()->getPage()->findAll($selector, $locator);
if (count($items) > 0) {
throw new ExpectationException($locator.' found, not expected.', $this->getSession());
throw new ExpectationException($locator . ' found, not expected.', $this->getSession());
}
}
@@ -280,7 +282,7 @@ class behat_auth_outage extends behat_base {
$row
);
if (($row['autostart'] != 'yes') && ($row['autostart'] != 'no')) {
throw new Exception('autostart must be yes or no, found: '.$row['autostart']);
throw new Exception('autostart must be yes or no, found: ' . $row['autostart']);
}
if ($row['finished'] == '') {
$row['finished'] = null;
@@ -318,7 +320,7 @@ class behat_auth_outage extends behat_base {
$seconds += 5; // Give it some extra time to pool the server.
break;
default:
throw new Exception('Invalid $what='.$what);
throw new Exception('Invalid $what=' . $what);
}
if ($seconds >= 0) {
$seconds++; // Give one extra second for things to happen.
@@ -332,7 +334,7 @@ class behat_auth_outage extends behat_base {
*/
private function is_behat_3() {
global $version;
list($behat) = explode('.', $version);
[$behat] = explode('.', $version);
return ($behat >= 3);
}
}
+6 -6
View File
@@ -38,7 +38,7 @@ use auth_outage\calendar\calendar;
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\calendar\calendar
*/
class calendar_test extends \core_phpunit\testcase {
final class calendar_test extends \core_phpunit\testcase {
/**
* @var outage|null The calendar entry owner.
*/
@@ -47,7 +47,7 @@ class calendar_test extends \core_phpunit\testcase {
/**
* Creates an outage and checks if its in the calendar.
*/
public function test_create() {
public function test_create(): void {
$this->resetAfterTest(true);
self::setAdminUser();
@@ -68,7 +68,7 @@ class calendar_test extends \core_phpunit\testcase {
/**
* Updates an outage and checks the calendar.
*/
public function test_update() {
public function test_update(): void {
$this->resetAfterTest(true);
self::setAdminUser();
@@ -92,7 +92,7 @@ class calendar_test extends \core_phpunit\testcase {
/**
* Deletes an outage and checks the calendar.
*/
public function test_delete() {
public function test_delete(): void {
$this->resetAfterTest(true);
self::setAdminUser();
@@ -117,7 +117,7 @@ class calendar_test extends \core_phpunit\testcase {
/**
* Try to update a non existing outage.
*/
public function test_update_notfound() {
public function test_update_notfound(): void {
$this->resetAfterTest(true);
self::setAdminUser();
@@ -140,7 +140,7 @@ class calendar_test extends \core_phpunit\testcase {
/**
* Try to delete a non existing outage.
*/
public function test_delete_notfound() {
public function test_delete_notfound(): void {
$this->resetAfterTest(true);
self::setAdminUser();
+18 -19
View File
@@ -29,7 +29,7 @@ use auth_outage\local\cli\create;
use auth_outage\local\cli\cli_exception;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/cli_testcase.php');
require_once(__DIR__ . '/cli_testcase.php');
/**
* create_test test class.
@@ -40,8 +40,7 @@ require_once(__DIR__.'/cli_testcase.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\local\cli\create
*/
class cli_create_test extends cli_testcase {
final class cli_create_test extends cli_testcase {
public function tearDown(): void {
parent::tearDown();
}
@@ -49,7 +48,7 @@ class cli_create_test extends cli_testcase {
/**
* Tests without any arguments.
*/
public function test_noarguments() {
public function test_noarguments(): void {
$cli = new create();
$this->set_expected_cli_exception(cli_exception::ERROR_PARAMETER_MISSING);
$this->execute($cli);
@@ -58,7 +57,7 @@ class cli_create_test extends cli_testcase {
/**
* Tests when the start time is not a valid number.
*/
public function test_invalidparam_notanumber() {
public function test_invalidparam_notanumber(): void {
$cli = new create(['start' => 'some day']);
$cli->set_defaults([
'warn' => 50,
@@ -74,7 +73,7 @@ class cli_create_test extends cli_testcase {
/**
* Tests when providing a negative start time.
*/
public function test_invalidparam_negative() {
public function test_invalidparam_negative(): void {
$cli = new create(['start' => -1]);
$cli->set_defaults([
'warn' => 50,
@@ -90,7 +89,7 @@ class cli_create_test extends cli_testcase {
/**
* Tests providing an empty title.
*/
public function test_invalidparam_emptystring() {
public function test_invalidparam_emptystring(): void {
$cli = new create(['start' => 0, 'title' => '']);
$cli->set_defaults([
'warn' => 50,
@@ -106,7 +105,7 @@ class cli_create_test extends cli_testcase {
/**
* Tests if not providing the title (it will be send as true).
*/
public function test_invalidparam_notastring() {
public function test_invalidparam_notastring(): void {
$cli = new create(['start' => 0, 'title' => true]);
$cli->set_defaults([
'warn' => 50,
@@ -122,7 +121,7 @@ class cli_create_test extends cli_testcase {
/**
* Tests the help.
*/
public function test_help() {
public function test_help(): void {
$this->set_parameters(['--help']);
$cli = new create();
$output = $this->execute($cli);
@@ -133,7 +132,7 @@ class cli_create_test extends cli_testcase {
/**
* Tests the options and shortcuts.
*/
public function test_options() {
public function test_options(): void {
$cli = new create();
$options = $cli->generate_options();
@@ -150,7 +149,7 @@ class cli_create_test extends cli_testcase {
/**
* Tests creating with all given options.
*/
public function test_create_withoptions() {
public function test_create_withoptions(): void {
$this->set_parameters([
'--autostart=true',
'--warn=10',
@@ -179,7 +178,7 @@ class cli_create_test extends cli_testcase {
/**
* Tests creating with the onlyid parameter.
*/
public function test_create_onlyid() {
public function test_create_onlyid(): void {
$this->set_parameters([
'--onlyid',
'--autostart=N',
@@ -210,7 +209,7 @@ class cli_create_test extends cli_testcase {
/**
* Tests creating using some default values.
*/
public function test_create_withdefaults() {
public function test_create_withdefaults(): void {
$this->set_parameters([
'--warn=100',
'--start=50',
@@ -243,7 +242,7 @@ class cli_create_test extends cli_testcase {
/**
* Tests creating with clone.
*/
public function test_create_withclone() {
public function test_create_withclone(): void {
self::setAdminUser();
$now = time();
// Create the outage to clone.
@@ -260,7 +259,7 @@ class cli_create_test extends cli_testcase {
$this->set_parameters([
'--onlyid',
'--start=60',
'--clone='.$id,
'--clone=' . $id,
]);
$cli = new create();
$cli->set_referencetime($now);
@@ -277,7 +276,7 @@ class cli_create_test extends cli_testcase {
/**
* Tests creating with an invalid clone id.
*/
public function test_create_withclone_invalid() {
public function test_create_withclone_invalid(): void {
$this->set_parameters([
'--start=60',
'--clone=-1',
@@ -290,7 +289,7 @@ class cli_create_test extends cli_testcase {
/**
* Tests creating with the block flag.
*/
public function test_create_withblock() {
public function test_create_withblock(): void {
// Not an extensive test in the blocking API, cliwaitforit tests should cover them deeper.
$this->set_parameters([
'--autostart=N',
@@ -312,7 +311,7 @@ class cli_create_test extends cli_testcase {
/**
* Tests providing an invalid option as default.
*/
public function test_setdefaults_extra() {
public function test_setdefaults_extra(): void {
$cli = new create([]);
$this->set_expected_exception('coding_exception');
$cli->set_defaults(['aninvalidparameter' => 'value']);
@@ -321,7 +320,7 @@ class cli_create_test extends cli_testcase {
/**
* Tests with an invalud autostart bool value.
*/
public function test_invalid_bool() {
public function test_invalid_bool(): void {
$this->set_parameters([
'--autostart=maybe',
'--warn=60',
+13 -14
View File
@@ -29,7 +29,7 @@ use auth_outage\local\cli\finish;
use auth_outage\local\cli\cli_exception;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/cli_testcase.php');
require_once(__DIR__ . '/cli_testcase.php');
/**
* finish_test test class.
@@ -40,8 +40,7 @@ require_once(__DIR__.'/cli_testcase.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\local\cli\finish
*/
class cli_finish_test extends cli_testcase {
final class cli_finish_test extends cli_testcase {
public function tearDown(): void {
parent::tearDown();
}
@@ -49,7 +48,7 @@ class cli_finish_test extends cli_testcase {
/**
* Tests the constructor.
*/
public function test_constructor() {
public function test_constructor(): void {
$cli = new finish();
self::assertNotNull($cli);
}
@@ -57,7 +56,7 @@ class cli_finish_test extends cli_testcase {
/**
* Tests the generated options and shortcuts.
*/
public function test_options() {
public function test_options(): void {
$cli = new finish();
$options = $cli->generate_options();
@@ -74,7 +73,7 @@ class cli_finish_test extends cli_testcase {
/**
* Tests the help.
*/
public function test_help() {
public function test_help(): void {
$this->set_parameters(['--help']);
$cli = new finish();
$text = $this->execute($cli);
@@ -85,7 +84,7 @@ class cli_finish_test extends cli_testcase {
/**
* Tests if running without arguments.
*/
public function test_noarguments() {
public function test_noarguments(): void {
$cli = new finish();
$this->set_expected_cli_exception(cli_exception::ERROR_PARAMETER_MISSING);
$this->execute($cli);
@@ -94,7 +93,7 @@ class cli_finish_test extends cli_testcase {
/**
* Tests finishing an already ended outage.
*/
public function test_endedoutage() {
public function test_endedoutage(): void {
self::setAdminUser();
$now = time();
$id = outagedb::save(new outage([
@@ -105,7 +104,7 @@ class cli_finish_test extends cli_testcase {
'title' => 'Title',
'description' => 'Description',
]));
$this->set_parameters(['-id='.$id]);
$this->set_parameters(['-id=' . $id]);
$cli = new finish();
$cli->set_referencetime($now);
$this->set_expected_cli_exception(cli_exception::ERROR_OUTAGE_INVALID);
@@ -115,7 +114,7 @@ class cli_finish_test extends cli_testcase {
/**
* Tests finishing an outage.
*/
public function test_finish() {
public function test_finish(): void {
self::setAdminUser();
$now = time();
$id = outagedb::save(new outage([
@@ -126,7 +125,7 @@ class cli_finish_test extends cli_testcase {
'title' => 'Title',
'description' => 'Description',
]));
$this->set_parameters(['-id='.$id]);
$this->set_parameters(['-id=' . $id]);
$cli = new finish();
$cli->set_referencetime($now);
$this->execute($cli);
@@ -135,7 +134,7 @@ class cli_finish_test extends cli_testcase {
/**
* Tests finishing an active outage when it does not exists.
*/
public function test_activenotfound() {
public function test_activenotfound(): void {
self::setAdminUser();
$this->set_parameters(['-a']);
$cli = new finish();
@@ -146,7 +145,7 @@ class cli_finish_test extends cli_testcase {
/**
* Tests providing an invalid outage id.
*/
public function test_invalidid() {
public function test_invalidid(): void {
self::setAdminUser();
$this->set_parameters(['-id=theid']);
$cli = new finish();
@@ -157,7 +156,7 @@ class cli_finish_test extends cli_testcase {
/**
* Tests when the outage is not found.
*/
public function test_idnotfound() {
public function test_idnotfound(): void {
self::setAdminUser();
$this->set_parameters(['-id=99999']);
$cli = new finish();
+9 -10
View File
@@ -27,7 +27,7 @@ use auth_outage\local\cli\cli_exception;
use auth_outage\local\cli\create;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/cli_testcase.php');
require_once(__DIR__ . '/cli_testcase.php');
/**
* cli_test test class.
@@ -38,8 +38,7 @@ require_once(__DIR__.'/cli_testcase.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\local\cli\create
*/
class cli_test extends cli_testcase {
final class cli_test extends cli_testcase {
public function tearDown(): void {
parent::tearDown();
}
@@ -47,7 +46,7 @@ class cli_test extends cli_testcase {
/**
* Tests providing an unknown parameter.
*/
public function test_invalidargumentparam() {
public function test_invalidargumentparam(): void {
$this->set_parameters(['--aninvalidparameter']);
$this->set_expected_cli_exception(cli_exception::ERROR_PARAMETER_UNKNOWN);
new create();
@@ -56,7 +55,7 @@ class cli_test extends cli_testcase {
/**
* Tests providing another unknow parameter (without --).
*/
public function test_invalidargumentgiven() {
public function test_invalidargumentgiven(): void {
$this->set_expected_cli_exception(cli_exception::ERROR_PARAMETER_UNKNOWN);
new create(['anotherinvalidparameter']);
}
@@ -64,7 +63,7 @@ class cli_test extends cli_testcase {
/**
* Tests setting reference times.
*/
public function test_setreferencetime() {
public function test_setreferencetime(): void {
$cli = new create(['start' => 0]);
$cli->set_referencetime(1);
$cli->set_referencetime(60 * 60 * 24 * 7);
@@ -73,7 +72,7 @@ class cli_test extends cli_testcase {
/**
* Tests setting an invalid reference time.
*/
public function test_setreferencetime_invalid() {
public function test_setreferencetime_invalid(): void {
$this->set_parameters(['--start=60']);
$cli = new create();
$this->set_expected_exception('coding_exception');
@@ -83,7 +82,7 @@ class cli_test extends cli_testcase {
/**
* Tests the help.
*/
public function test_help() {
public function test_help(): void {
$this->set_parameters(['-h']);
$cli = new create();
$output = $this->execute($cli);
@@ -94,7 +93,7 @@ class cli_test extends cli_testcase {
/**
* Tests the cli_exception.
*/
public function test_exception() {
public function test_exception(): void {
$this->set_expected_cli_exception(cli_exception::ERROR_UNDEFINED);
throw new cli_exception('An CLI exception.');
}
@@ -102,7 +101,7 @@ class cli_test extends cli_testcase {
/**
* Tests with the auth_outage plugin disabled.
*/
public function test_authdisabled() {
public function test_authdisabled(): void {
// Disable all auth plugins.
set_config('auth', '');
\core\session\manager::gc(); // Remove stale sessions.
+1 -2
View File
@@ -24,7 +24,7 @@
*/
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/base_testcase.php');
require_once(__DIR__ . '/base_testcase.php');
/**
* cli_testcase class.
@@ -35,7 +35,6 @@ require_once(__DIR__.'/base_testcase.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class cli_testcase extends base_testcase {
public function tearDown(): void {
parent::tearDown();
}
+12 -13
View File
@@ -29,7 +29,7 @@ use auth_outage\local\cli\waitforit;
use auth_outage\local\cli\cli_exception;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/cli_testcase.php');
require_once(__DIR__ . '/cli_testcase.php');
/**
* waitforit_test test class.
@@ -40,8 +40,7 @@ require_once(__DIR__.'/cli_testcase.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\local\cli\waitforit
*/
class cli_waitforit_test extends cli_testcase {
final class cli_waitforit_test extends cli_testcase {
public function tearDown(): void {
parent::tearDown();
}
@@ -49,7 +48,7 @@ class cli_waitforit_test extends cli_testcase {
/**
* Tests the constructor.
*/
public function test_constructor() {
public function test_constructor(): void {
$cli = new waitforit();
self::assertNotNull($cli);
}
@@ -57,7 +56,7 @@ class cli_waitforit_test extends cli_testcase {
/**
* Tests the generated options.
*/
public function test_generateoptions() {
public function test_generateoptions(): void {
$cli = new waitforit();
$options = $cli->generate_options();
foreach (array_keys($options) as $k) {
@@ -68,7 +67,7 @@ class cli_waitforit_test extends cli_testcase {
/**
* Tests the generated shortcut options.
*/
public function test_generateshortcuts() {
public function test_generateshortcuts(): void {
$cli = new waitforit();
$options = $cli->generate_options();
$shorts = $cli->generate_shortcuts();
@@ -80,7 +79,7 @@ class cli_waitforit_test extends cli_testcase {
/**
* Tests if help works.
*/
public function test_help() {
public function test_help(): void {
$this->set_parameters(['--help']);
$cli = new waitforit();
$text = $this->execute($cli);
@@ -91,7 +90,7 @@ class cli_waitforit_test extends cli_testcase {
/**
* Checks if providing an outageid and active parameter.
*/
public function test_bothparams() {
public function test_bothparams(): void {
$this->set_parameters(['--outageid=1', '--active']);
$cli = new waitforit();
$this->set_expected_cli_exception(cli_exception::ERROR_PARAMETER_INVALID);
@@ -101,7 +100,7 @@ class cli_waitforit_test extends cli_testcase {
/**
* Tests with an invalid outage id
*/
public function test_invalidoutageid() {
public function test_invalidoutageid(): void {
$this->set_parameters(['-id=-1']);
$cli = new waitforit();
$this->set_expected_cli_exception(cli_exception::ERROR_PARAMETER_INVALID);
@@ -111,7 +110,7 @@ class cli_waitforit_test extends cli_testcase {
/**
* Tests with an active outage when it does not exists.
*/
public function test_outagenotfound() {
public function test_outagenotfound(): void {
$this->set_parameters(['-a']);
$cli = new waitforit();
$this->set_expected_cli_exception(cli_exception::ERROR_OUTAGE_NOT_FOUND);
@@ -121,7 +120,7 @@ class cli_waitforit_test extends cli_testcase {
/**
* Tests with an outage that already ended.
*/
public function test_endedoutage() {
public function test_endedoutage(): void {
self::setAdminUser();
$now = time();
$id = outagedb::save(new outage([
@@ -132,7 +131,7 @@ class cli_waitforit_test extends cli_testcase {
'title' => 'Title',
'description' => 'Description',
]));
$this->set_parameters(['-id='.$id]);
$this->set_parameters(['-id=' . $id]);
$cli = new waitforit();
$cli->set_referencetime($now);
$this->set_expected_cli_exception(cli_exception::ERROR_OUTAGE_INVALID);
@@ -142,7 +141,7 @@ class cli_waitforit_test extends cli_testcase {
/**
* Tests waiting for an existing active outage, verbose mode.
*/
public function test_activeverbose() {
public function test_activeverbose(): void {
self::setAdminUser();
$now = time();
outagedb::save(new outage([
+12 -13
View File
@@ -27,7 +27,7 @@ use auth_outage\local\outage;
use auth_outage\local\controllers\infopage;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/base_testcase.php');
require_once(__DIR__ . '/base_testcase.php');
/**
* Tests performed on infopage controller class and update_static_page task class.
@@ -38,8 +38,7 @@ require_once(__DIR__.'/base_testcase.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\local\controllers\infopage
*/
class controllers_infopage_test extends base_testcase {
final class controllers_infopage_test extends base_testcase {
public function tearDown(): void {
parent::tearDown();
}
@@ -47,7 +46,7 @@ class controllers_infopage_test extends base_testcase {
/**
* Tests the constructor.
*/
public function test_constructor() {
public function test_constructor(): void {
$this->assertTrue(has_capability('auth/outage:viewinfo', context_system::instance()));
new infopage();
@@ -56,7 +55,7 @@ class controllers_infopage_test extends base_testcase {
/**
* Tests the constructor with given parameters.
*/
public function test_constructor_withparams() {
public function test_constructor_withparams(): void {
$this->assertTrue(has_capability('auth/outage:viewinfo', context_system::instance()));
$_GET = ['id' => 1, 'static' => 'true'];
@@ -66,7 +65,7 @@ class controllers_infopage_test extends base_testcase {
/**
* Tests the constructor with different id and outage id.
*/
public function test_constructor_idmismatch() {
public function test_constructor_idmismatch(): void {
$this->assertTrue(has_capability('auth/outage:viewinfo', context_system::instance()));
$outage = $this->get_dummy_outage();
@@ -77,7 +76,7 @@ class controllers_infopage_test extends base_testcase {
/**
* Tests the constructor with an invalid outage.
*/
public function test_constructor_invalidoutage() {
public function test_constructor_invalidoutage(): void {
$this->assertTrue(has_capability('auth/outage:viewinfo', context_system::instance()));
$this->set_expected_exception('coding_exception', 'Provided outage is not a valid outage object. (My outage)');
@@ -87,7 +86,7 @@ class controllers_infopage_test extends base_testcase {
/**
* Checks the output of the info page.
*/
public function test_output() {
public function test_output(): void {
$this->assertTrue(has_capability('auth/outage:viewinfo', context_system::instance()));
$outage = $this->get_dummy_outage();
@@ -100,7 +99,7 @@ class controllers_infopage_test extends base_testcase {
/**
* Checks the output of the info page.
*/
public function test_output_without_permission() {
public function test_output_without_permission(): void {
$this->revoke_info_page_permissions();
$this->assertFalse(has_capability('auth/outage:viewinfo', context_system::instance()));
@@ -114,7 +113,7 @@ class controllers_infopage_test extends base_testcase {
/**
* Checks the output of the info page.
*/
public function test_output_without_permission_but_static() {
public function test_output_without_permission_but_static(): void {
$this->revoke_info_page_permissions();
$this->assertFalse(has_capability('auth/outage:viewinfo', context_system::instance()));
@@ -128,7 +127,7 @@ class controllers_infopage_test extends base_testcase {
/**
* Checks the output of the info page.
*/
public function test_output_with_forcelogin() {
public function test_output_with_forcelogin(): void {
$this->assertTrue(has_capability('auth/outage:viewinfo', context_system::instance()));
set_config('forcelogin', true);
@@ -143,7 +142,7 @@ class controllers_infopage_test extends base_testcase {
/**
* Checks the output of the info page.
*/
public function test_output_with_forcelogin_if_static() {
public function test_output_with_forcelogin_if_static(): void {
$this->assertTrue(has_capability('auth/outage:viewinfo', context_system::instance()));
set_config('forcelogin', true);
@@ -159,7 +158,7 @@ class controllers_infopage_test extends base_testcase {
/**
* Tests the constructor enables SVG support.
*/
public function test_svgicons_is_true() {
public function test_svgicons_is_true(): void {
global $CFG;
$this->assertTrue(has_capability('auth/outage:viewinfo', context_system::instance()));
+106 -103
View File
@@ -29,7 +29,7 @@ use auth_outage\local\controllers\maintenance_static_page_io;
use auth_outage\local\controllers\maintenance_static_page_generator;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/base_testcase.php');
require_once(__DIR__ . '/base_testcase.php');
/**
* maintenance_static_page_test class.
@@ -40,8 +40,7 @@ require_once(__DIR__.'/base_testcase.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\local\controllers\maintenance_static_page_generator
*/
class controllers_maintenance_static_page_test extends base_testcase {
final class controllers_maintenance_static_page_test extends base_testcase {
public function tearDown(): void {
parent::tearDown();
}
@@ -49,39 +48,41 @@ class controllers_maintenance_static_page_test extends base_testcase {
/**
* Test template file.
*/
public function test_templatefile() {
public function test_templatefile(): void {
global $CFG;
$page = maintenance_static_page::create_from_html('<html></html>');
self::assertSame($CFG->dataroot.'/climaintenance.template.html', $page->get_io()->get_template_file());
self::assertSame($CFG->dataroot . '/climaintenance.template.html', $page->get_io()->get_template_file());
$page->get_io()->set_preview(true);
self::assertSame($CFG->dataroot.'/auth_outage/climaintenance/preview/climaintenance.html',
$page->get_io()->get_template_file());
self::assertSame(
$CFG->dataroot . '/auth_outage/climaintenance/preview/climaintenance.html',
$page->get_io()->get_template_file()
);
}
/**
* Test resources folder.
*/
public function test_resourcesfolder() {
public function test_resourcesfolder(): void {
global $CFG;
$page = maintenance_static_page::create_from_html('<html></html>');
self::assertSame($CFG->dataroot.'/auth_outage/climaintenance', $page->get_io()->get_resources_folder());
self::assertSame($CFG->dataroot . '/auth_outage/climaintenance', $page->get_io()->get_resources_folder());
$page->get_io()->set_preview(true);
self::assertSame($CFG->dataroot.'/auth_outage/climaintenance/preview', $page->get_io()->get_resources_folder());
self::assertSame($CFG->dataroot . '/auth_outage/climaintenance/preview', $page->get_io()->get_resources_folder());
}
/**
* Test create from outage.
*/
public function test_createfromoutage() {
public function test_createfromoutage(): void {
// How to fetch a page from PHPUnit environment?
}
/**
* Test create from HTML.
*/
public function test_createfromhtml() {
public function test_createfromhtml(): void {
$html = "<!DOCTYPE html>\n<html><head><title>Title</title></head><body>Content</body></html>";
$expected = "<!DOCTYPE html>\n<html><head><title>Title</title><meta http-equiv=\"refresh\" content=\"300\">".
$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));
}
@@ -89,9 +90,9 @@ class controllers_maintenance_static_page_test extends base_testcase {
/**
* Test remove script tags.
*/
public function test_removescripttags() {
$html = "<!DOCTYPE html>\n".
'<html><head><script type="text/javascript" src="http://xyz"></script><title>Title</title></head>'.
public function test_removescripttags(): void {
$html = "<!DOCTYPE html>\n" .
'<html><head><script type="text/javascript" src="http://xyz"></script><title>Title</title></head>' .
'<body>Content<script> a < 5; x > 3</script></body></html>';
maintenance_static_page::create_from_html($html)->generate();
@@ -102,12 +103,12 @@ class controllers_maintenance_static_page_test extends base_testcase {
/**
* Test remove script tags.
*/
public function test_updatelinkstylesheet() {
public function test_updatelinkstylesheet(): void {
$localcsslink = $this->get_fixture_path_location('simple.css');
$externalcsslink = 'http://google.com/coolstuff.css';
$html = "<!DOCTYPE html>\n".
'<html><head><link href="'.$localcsslink.'" rel="stylesheet" /><title>Title</title></head>'.
'<body>Content<link rel="stylesheet" href="'.$externalcsslink.'"></body></html>';
$html = "<!DOCTYPE html>\n" .
'<html><head><link href="' . $localcsslink . '" rel="stylesheet" /><title>Title</title></head>' .
'<body>Content<link rel="stylesheet" href="' . $externalcsslink . '"></body></html>';
$generated = $this->generated_page_html($html);
self::assertStringContainsString('www.example.com/moodle/auth/outage/file.php?file=', $generated);
@@ -118,66 +119,66 @@ class controllers_maintenance_static_page_test extends base_testcase {
/**
* Test update link style sheet urls.
*/
public function test_updatelinkstylesheet_urls() {
public function test_updatelinkstylesheet_urls(): void {
$localcsslink = $this->get_fixture_path_location('withurls.css');
$html = "<!DOCTYPE html>\n".
'<html><head><link href="'.$localcsslink.'" rel="stylesheet" /><title>Title</title></head>'.
$html = "<!DOCTYPE html>\n" .
'<html><head><link href="' . $localcsslink . '" rel="stylesheet" /><title>Title</title></head>' .
'<body>Content</body></html>';
$page = maintenance_static_page::create_from_html($html);
$page->generate();
// Check for css file.
self::assertFileExists($page->get_io()->get_resources_folder().'/b09bd4b66cc3964d5fc5978752fc554f5666daa3.dGV4dC9wbGFpbg');
self::assertFileExists($page->get_io()->get_resources_folder() . '/b09bd4b66cc3964d5fc5978752fc554f5666daa3.dGV4dC9wbGFpbg');
// Check for catalyst.png file referenced in url(..) of css.
self::assertFileExists($page->get_io()->get_resources_folder().'/ff7f7f87a26a908fc72930eaefb6b57306361d16.aW1hZ2UvcG5n');
self::assertFileExists($page->get_io()->get_resources_folder() . '/ff7f7f87a26a908fc72930eaefb6b57306361d16.aW1hZ2UvcG5n');
}
/**
* Test update link style sheet urls quoted.
*/
public function test_updatelinkstylesheet_urls_quoted() {
public function test_updatelinkstylesheet_urls_quoted(): void {
$localcsslink = $this->get_fixture_path_location('withurls-quoted.css');
$html = "<!DOCTYPE html>\n".
'<html><head><link href="'.$localcsslink.'" rel="stylesheet" /><title>Title</title></head>'.
$html = "<!DOCTYPE html>\n" .
'<html><head><link href="' . $localcsslink . '" rel="stylesheet" /><title>Title</title></head>' .
'<body>Content</body></html>';
$page = maintenance_static_page::create_from_html($html);
$page->generate();
// Check for css file.
self::assertFileExists($page->get_io()->get_resources_folder().'/2ec04228cc8bb37782f511aaeb01ee553cc884a4.dGV4dC9wbGFpbg');
self::assertFileExists($page->get_io()->get_resources_folder() . '/2ec04228cc8bb37782f511aaeb01ee553cc884a4.dGV4dC9wbGFpbg');
// Check for catalyst.png file referenced in url(..) of css.
self::assertFileExists($page->get_io()->get_resources_folder().'/ff7f7f87a26a908fc72930eaefb6b57306361d16.aW1hZ2UvcG5n');
self::assertFileExists($page->get_io()->get_resources_folder() . '/ff7f7f87a26a908fc72930eaefb6b57306361d16.aW1hZ2UvcG5n');
}
/**
* Test update link style sheet urls with sub dir.
*/
public function test_updatelinkstylesheet_urls_subdir() {
public function test_updatelinkstylesheet_urls_subdir(): void {
$localcsslink = $this->get_fixture_path_location('subdir/withurls-subdir.css');
$html = "<!DOCTYPE html>\n".
'<html><head><link href="'.$localcsslink.'" rel="stylesheet" /><title>Title</title></head>'.
$html = "<!DOCTYPE html>\n" .
'<html><head><link href="' . $localcsslink . '" rel="stylesheet" /><title>Title</title></head>' .
'<body>Content</body></html>';
$page = maintenance_static_page::create_from_html($html);
$page->generate();
// Check for css file.
self::assertFileExists($page->get_io()->get_resources_folder().'/beb44281e23b9d872056bf0230cea34535e8cdea.dGV4dC9wbGFpbg');
self::assertFileExists($page->get_io()->get_resources_folder() . '/beb44281e23b9d872056bf0230cea34535e8cdea.dGV4dC9wbGFpbg');
// Check for file referenced in url(..) of css.
self::assertFileExists($page->get_io()->get_resources_folder().'/a02a8a442fa82d5205ffb24722d9df7f35161f56.dGV4dC9wbGFpbg');
self::assertFileExists($page->get_io()->get_resources_folder() . '/a02a8a442fa82d5205ffb24722d9df7f35161f56.dGV4dC9wbGFpbg');
}
/**
* Test update images to file.php style link.
*/
public function test_updateimages() {
public function test_updateimages(): void {
$localimglink = $this->get_fixture_path_location('catalyst.png');
$externalimglink = 'http://google.com/coolstyle.css';
$html = "<!DOCTYPE html>\n".
'<html><head><title>Title</title></head>'.
'<body><img src="'.$localimglink.'">Content<img src="'.$externalimglink.'" /></body></html>';
$html = "<!DOCTYPE html>\n" .
'<html><head><title>Title</title></head>' .
'<body><img src="' . $localimglink . '">Content<img src="' . $externalimglink . '" /></body></html>';
$generated = $this->generated_page_html($html);
self::assertStringContainsString('www.example.com/moodle/auth/outage/file.php?file=', $generated);
@@ -188,10 +189,10 @@ class controllers_maintenance_static_page_test extends base_testcase {
/**
* Test update favicon to file.php style link.
*/
public function test_updatelinkfavicon() {
public function test_updatelinkfavicon(): void {
$link = $this->get_fixture_path_location('catalyst.png');
$html = "<!DOCTYPE html>\n".
'<html><head><title>Title</title><link rel="shortcut icon" href="'.$link.'""></head>'.
$html = "<!DOCTYPE html>\n" .
'<html><head><title>Title</title><link rel="shortcut icon" href="' . $link . '""></head>' .
'<body>Content</body></html>';
$generated = $this->generated_page_html($html);
@@ -229,14 +230,14 @@ class controllers_maintenance_static_page_test extends base_testcase {
* @param bool $rewrite Flag if URL should be rewritten
* @throws coding_exception
*/
public function test_update_inline_background_images($stylecontent, $rewrite) {
public function test_update_inline_background_images($stylecontent, $rewrite): void {
global $CFG;
$this->resetAfterTest(true);
$generator = new maintenance_static_page_generator(new DOMDocument(), new maintenance_static_page_io());
$html = '<!DOCTYPE html>\n'.
'<html><head><title>Title</title></head>'.
'<body><div style="'.$stylecontent.'">Content</div></body></html>';
$html = '<!DOCTYPE html>\n' .
'<html><head><title>Title</title></head>' .
'<body><div style="' . $stylecontent . '">Content</div></body></html>';
// Temporarily disable debugging to prevent errors because file does not exist.
$debuglevel = $CFG->debug;
@@ -257,10 +258,10 @@ class controllers_maintenance_static_page_test extends base_testcase {
/**
* Test update preview path to file.php style link.
*/
public function test_previewpath() {
public function test_previewpath(): void {
$link = $this->get_fixture_path_location('catalyst.png');
$html = "<!DOCTYPE html>\n".
'<html><head><title>Title</title><link rel="shortcut icon" href="'.$link.'""></head>'.
$html = "<!DOCTYPE html>\n" .
'<html><head><title>Title</title><link rel="shortcut icon" href="' . $link . '""></head>' .
'<body>Content</body></html>';
$page = maintenance_static_page::create_from_html($html);
$page->get_io()->set_preview(true);
@@ -288,7 +289,7 @@ class controllers_maintenance_static_page_test extends base_testcase {
/**
* Checks if we can create and execute a task to update outage pages.
*/
public function test_tasks() {
public function test_tasks(): void {
$this->resetAfterTest(true);
$task = new update_static_page();
self::assertNotEmpty($task->get_name());
@@ -298,9 +299,9 @@ class controllers_maintenance_static_page_test extends base_testcase {
/**
* Tests updating the static page when there is no outage but the file existed before.
*/
public function test_updatestaticpage_hasfile() {
public function test_updatestaticpage_hasfile(): void {
global $CFG;
$file = $CFG->dataroot.'/climaintenance.template.html';
$file = $CFG->dataroot . '/climaintenance.template.html';
touch($file);
self::assertFileExists($file);
maintenance_static_page::create_from_outage(null)->generate();
@@ -315,23 +316,23 @@ class controllers_maintenance_static_page_test extends base_testcase {
/**
* Tests created file.
*/
public function test_createdfile() {
public function test_createdfile(): void {
global $CFG;
$link = $this->get_fixture_path_location('catalyst.png');
$html = "<!DOCTYPE html>\n".
'<html><head><title>Title</title></head>'.
'<body>Content<img src="'.$link.'" /></body></html>';
$html = "<!DOCTYPE html>\n" .
'<html><head><title>Title</title></head>' .
'<body>Content<img src="' . $link . '" /></body></html>';
$page = maintenance_static_page::create_from_html($html);
$page->generate();
// This checks if content is correct and mime type is correct from the encoded name.
$file = $page->get_io()->get_resources_folder().'/ff7f7f87a26a908fc72930eaefb6b57306361d16.aW1hZ2UvcG5n';
$file = $page->get_io()->get_resources_folder() . '/ff7f7f87a26a908fc72930eaefb6b57306361d16.aW1hZ2UvcG5n';
self::assertFileExists($file);
// We can still assert the contents really match, not just the hash.
$found = file_get_contents($file);
$expected = file_get_contents(__DIR__.'/fixtures/catalyst.png');
$expected = file_get_contents(__DIR__ . '/fixtures/catalyst.png');
self::assertSame($found, $expected);
}
@@ -343,13 +344,13 @@ class controllers_maintenance_static_page_test extends base_testcase {
* @return string
*/
private function get_fixture_path_location($file) {
return (string)new \moodle_url('/auth/outage/tests/fixtures/'.$file);
return (string)new \moodle_url('/auth/outage/tests/fixtures/' . $file);
}
/**
* Test saving empty string for template file.
*/
public function test_invalid_string_saving_template_empty() {
public function test_invalid_string_saving_template_empty(): void {
$io = new maintenance_static_page_io();
$this->set_expected_exception('coding_exception');
$io->save_template_file('');
@@ -358,7 +359,7 @@ class controllers_maintenance_static_page_test extends base_testcase {
/**
* Test saving non string for template file.
*/
public function test_invalid_string_saving_template_nostring() {
public function test_invalid_string_saving_template_nostring(): void {
$io = new maintenance_static_page_io();
$this->set_expected_exception('coding_exception');
$io->save_template_file(50);
@@ -367,7 +368,7 @@ class controllers_maintenance_static_page_test extends base_testcase {
/**
* Test get url for file.
*/
public function test_get_url_for_file() {
public function test_get_url_for_file(): void {
$io = new maintenance_static_page_io();
self::assertStringContainsString(
'www.example.com/moodle/auth/outage/file.php?file=img.png',
@@ -395,15 +396,15 @@ class controllers_maintenance_static_page_test extends base_testcase {
* @param string $result expected result
* @param string $url url to be checked
*/
public function test_is_url($result, $url) {
public function test_is_url($result, $url): void {
self::assertEquals($result, maintenance_static_page_io::is_url($url));
}
/**
* Test file get_data.
*/
public function test_file_get_data() {
$file = __DIR__.'/fixtures/catalyst.png';
public function test_file_get_data(): void {
$file = __DIR__ . '/fixtures/catalyst.png';
$found = maintenance_static_page_io::file_get_data($file);
self::assertSame(file_get_contents($file), $found['contents']);
self::assertSame('image/png', $found['mime']);
@@ -412,8 +413,8 @@ class controllers_maintenance_static_page_test extends base_testcase {
/**
* Test invalid file get_data.
*/
public function test_file_get_data_invalidfile() {
$found = maintenance_static_page_io::file_get_data(__DIR__.'/fixtures/invalidfile');
public function test_file_get_data_invalidfile(): void {
$found = maintenance_static_page_io::file_get_data(__DIR__ . '/fixtures/invalidfile');
self::assertSame('', $found['contents']);
self::assertSame('unknown', $found['mime']);
self::assertCount(1, $this->getDebuggingMessages());
@@ -423,7 +424,7 @@ class controllers_maintenance_static_page_test extends base_testcase {
/**
* Test invalid file get_data.
*/
public function test_file_get_data_invalidfilename() {
public function test_file_get_data_invalidfilename(): void {
$this->set_expected_exception('coding_exception');
maintenance_static_page_io::file_get_data(200);
}
@@ -433,7 +434,7 @@ class controllers_maintenance_static_page_test extends base_testcase {
* We will use an external URL to test passing ignoresecurity inside of file_get_data works,
* ideally in real code we should only be calling file_get_data with internal URLs.
*/
public function test_file_get_data_curlsecurityblockedhosts() {
public function test_file_get_data_curlsecurityblockedhosts(): void {
global $CFG, $USER;
$testhtml = $this->getExternalTestFileUrl('/test.html');
@@ -449,7 +450,9 @@ class controllers_maintenance_static_page_test extends base_testcase {
self::assertSame(0, $curl->get_errno());
if ($CFG->branch >= 403) {
self::assertDebuggingCalled(
"Blocked $testhtml: The URL is blocked. [user {$USER->id}]", DEBUG_NONE);
"Blocked $testhtml: The URL is blocked. [user {$USER->id}]",
DEBUG_NONE
);
}
// Test file_get_data does return the page and isn't blocked by security.
@@ -462,10 +465,10 @@ class controllers_maintenance_static_page_test extends base_testcase {
/**
* Test remove css selector.
*/
public function test_remove_css_selector() {
public function test_remove_css_selector(): void {
$this->resetAfterTest(true);
$html = "<!DOCTYPE html>\n".
'<html><head><title>Title</title></head>'.
$html = "<!DOCTYPE html>\n" .
'<html><head><title>Title</title></head>' .
'<body>Content<b class="removeme">Goodbye cruel world.</b></body></html>';
set_config('remove_selectors', '.removeme', 'auth_outage');
$generated = $this->generated_page_html($html);
@@ -477,10 +480,10 @@ class controllers_maintenance_static_page_test extends base_testcase {
/**
* Test remove css selector id.
*/
public function test_remove_css_selector_id() {
public function test_remove_css_selector_id(): void {
$this->resetAfterTest(true);
$html = "<!DOCTYPE html>\n".
'<html><head><title>Title</title></head>'.
$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', '#removeme', 'auth_outage');
$generated = $this->generated_page_html($html);
@@ -492,13 +495,13 @@ class controllers_maintenance_static_page_test extends base_testcase {
/**
* Test remove css selector with multi lines.
*/
public function test_remove_css_selector_with_multiline() {
public function test_remove_css_selector_with_multiline(): void {
$this->resetAfterTest(true);
$html = "<!DOCTYPE html>\n".
'<html><head><title>Title</title></head>'.
'<body>'.
'<b class="deleteme">Goodbye cruel world.</b>'.
'<b class="removeme">Goodbye cruel world.</b>'.
$html = "<!DOCTYPE html>\n" .
'<html><head><title>Title</title></head>' .
'<body>' .
'<b class="deleteme">Goodbye cruel world.</b>' .
'<b class="removeme">Goodbye cruel world.</b>' .
'</body></html>';
set_config('remove_selectors', ".removeme\n.deleteme", 'auth_outage');
$generated = $this->generated_page_html($html);
@@ -511,13 +514,13 @@ class controllers_maintenance_static_page_test extends base_testcase {
/**
* Test remove css selector needs trim.
*/
public function test_remove_css_selector_needing_trim() {
public function test_remove_css_selector_needing_trim(): void {
$this->resetAfterTest(true);
$html = "<!DOCTYPE html>\n".
'<html><head><title>Title</title></head>'.
'<body>'.
'<b class="deleteme">Goodbye cruel world.</b>'.
'<b class="removeme">Goodbye cruel world.</b>'.
$html = "<!DOCTYPE html>\n" .
'<html><head><title>Title</title></head>' .
'<body>' .
'<b class="deleteme">Goodbye cruel world.</b>' .
'<b class="removeme">Goodbye cruel world.</b>' .
'</body></html>';
set_config('remove_selectors', " .removeme \n .deleteme ", 'auth_outage');
$generated = $this->generated_page_html($html);
@@ -530,13 +533,13 @@ class controllers_maintenance_static_page_test extends base_testcase {
/**
* Test remove css selector with empty line.
*/
public function test_remove_css_selector_with_empty_line() {
public function test_remove_css_selector_with_empty_line(): void {
$this->resetAfterTest(true);
$html = "<!DOCTYPE html>\n".
'<html><head><title>Title</title></head>'.
'<body>'.
'<b class="deleteme">Goodbye cruel world.</b>'.
'<b class="removeme">Goodbye cruel world.</b>'.
$html = "<!DOCTYPE html>\n" .
'<html><head><title>Title</title></head>' .
'<body>' .
'<b class="deleteme">Goodbye cruel world.</b>' .
'<b class="removeme">Goodbye cruel world.</b>' .
'</body></html>';
set_config('remove_selectors', "\n\n.removeme\n\n\n\n.deleteme\n\n", 'auth_outage');
$generated = $this->generated_page_html($html);
@@ -549,10 +552,10 @@ class controllers_maintenance_static_page_test extends base_testcase {
/**
* Test remove css selector with invalid id.
*/
public function test_remove_css_selector_with_invalid_id() {
public function test_remove_css_selector_with_invalid_id(): void {
$this->resetAfterTest(true);
$html = "<!DOCTYPE html>\n".
'<html><head><title>Title</title></head>'.
$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);
@@ -564,10 +567,10 @@ class controllers_maintenance_static_page_test extends base_testcase {
/**
* Test meta refresh 5 minutes.
*/
public function test_meta_refresh_5minutes() {
public function test_meta_refresh_5minutes(): void {
$this->resetAfterTest(true);
$html = "<!DOCTYPE html>\n".
'<html><head><title>Title</title></head>'.
$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);
@@ -580,8 +583,8 @@ class controllers_maintenance_static_page_test extends base_testcase {
*/
public function test_meta_refresh_maximum_5seconds() {
$this->resetAfterTest(true);
$html = "<!DOCTYPE html>\n".
'<html><head><title>Title</title></head>'.
$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);
@@ -624,7 +627,7 @@ class controllers_maintenance_static_page_test extends base_testcase {
* @param int $count Expected quantity of found URLs
* @throws coding_exception
*/
public function test_get_urls_from_stylesheet($filecontent, $count) {
public function test_get_urls_from_stylesheet($filecontent, $count): void {
$this->resetAfterTest(true);
$generator = new maintenance_static_page_generator(new DOMDocument(), new maintenance_static_page_io());
$matches = $generator->get_urls_from_stylesheet($filecontent);
+4 -4
View File
@@ -38,7 +38,7 @@ use auth_outage\dml\outagedb;
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\dml\outagedb
*/
class dml_events_test extends \core_phpunit\testcase {
final class dml_events_test extends \core_phpunit\testcase {
/**
* @var outage|null Outage used in the tests.
*/
@@ -53,7 +53,7 @@ class dml_events_test extends \core_phpunit\testcase {
* Saves an outage and check if the event was created.
* @return array With the outage id and the event id.
*/
public function test_save() {
public function test_save(): void {
global $DB;
self::setAdminUser();
$this->resetAfterTest(true);
@@ -85,7 +85,7 @@ class dml_events_test extends \core_phpunit\testcase {
/**
* Updates an outage and checks if the event was updated.
*/
public function test_update() {
public function test_update(): void {
global $DB;
self::setAdminUser();
@@ -123,7 +123,7 @@ class dml_events_test extends \core_phpunit\testcase {
/**
* Deletes an outage and checks if the event was deleted.
*/
public function test_delete() {
public function test_delete(): void {
global $DB;
self::setAdminUser();
+23 -15
View File
@@ -29,7 +29,7 @@ use auth_outage\local\outage;
use auth_outage\dml\outagedb;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/base_testcase.php');
require_once(__DIR__ . '/base_testcase.php');
/**
* installation_test test class.
@@ -42,8 +42,7 @@ require_once(__DIR__.'/base_testcase.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\dml\outagedb
*/
class dml_installation_test extends base_testcase {
final class dml_installation_test extends base_testcase {
public function tearDown(): void {
parent::tearDown();
}
@@ -53,7 +52,7 @@ class dml_installation_test extends base_testcase {
*
* See Issue #57.
*/
public function test_uninstall() {
public function test_uninstall(): void {
global $CFG, $DB;
$this->resetAfterTest();
@@ -78,24 +77,33 @@ class dml_installation_test extends base_testcase {
self::assertSame(1, $DB->count_records_select('event', "eventtype = 'auth_outage'", null));
// Uninstall plugin.
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir . '/adminlib.php');
$progress = new \progress_trace_buffer(new \text_progress_trace(), false);
\core_plugin_manager::instance()->uninstall_plugin('auth_outage', $progress);
$progress->finished();
self::assertStringContainsString('++ Success ++', $progress->get_buffer());
// Check ...
self::assertSame(0, $DB->count_records_select('event', "eventtype = 'auth_outage'", null),
'The outage events were not removed.');
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.
self::assertFalse($dbman->table_exists('auth_outage'),
'Table "auth_outage" was not dropped.');
self::assertSame(
0,
$DB->count_records_select('event', "eventtype = 'auth_outage'", null),
'The outage events were not removed.'
);
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.
self::assertFalse(
$dbman->table_exists('auth_outage'),
'Table "auth_outage" was not dropped.'
);
// Create tables back so tests do not fail with MySQL ...
require_once($CFG->libdir.'/upgradelib.php');
$DB->get_manager()->install_from_xmldb_file($CFG->dirroot.'/auth/outage/db/install.xml');
require_once($CFG->libdir . '/upgradelib.php');
$DB->get_manager()->install_from_xmldb_file($CFG->dirroot . '/auth/outage/db/install.xml');
}
}
+105 -58
View File
@@ -27,7 +27,7 @@ use auth_outage\local\outage;
use auth_outage\dml\outagedb;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/base_testcase.php');
require_once(__DIR__ . '/base_testcase.php');
/**
* outagedb_test tests class.
@@ -38,8 +38,7 @@ require_once(__DIR__.'/base_testcase.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\dml\outagedb
*/
class dml_outagedb_test extends base_testcase {
final class dml_outagedb_test extends base_testcase {
public function tearDown(): void {
parent::tearDown();
}
@@ -92,7 +91,7 @@ class dml_outagedb_test extends base_testcase {
/**
* Make sure we can save and update.
*/
public function test_save() {
public function test_save(): void {
$this->resetAfterTest(true);
// Save new outage.
$id = outagedb::save($this->createoutage(1));
@@ -105,7 +104,7 @@ class dml_outagedb_test extends base_testcase {
/**
* Create a few outages, fetch them and check if fields match.
*/
public function test_saved_fields() {
public function test_saved_fields(): void {
$this->resetAfterTest(true);
for ($i = 0; $i < 4; $i++) {
$expected = $this->createoutage($i);
@@ -116,14 +115,14 @@ class dml_outagedb_test extends base_testcase {
$expected->createdby = $actual->createdby;
$expected->modifiedby = $actual->modifiedby;
// Check if fields are the same.
self::assertEquals($expected, $actual, 'Failed for $i='.$i);
self::assertEquals($expected, $actual, 'Failed for $i=' . $i);
}
}
/**
* Make sure we can get existing entries and null if not found.
*/
public function test_getbyid() {
public function test_getbyid(): void {
$this->resetAfterTest(true);
// Create something.
$id = outagedb::save($this->createoutage(1));
@@ -140,7 +139,7 @@ class dml_outagedb_test extends base_testcase {
/**
* Make sure we can delete stuff.
*/
public function test_delete() {
public function test_delete(): void {
$this->resetAfterTest(true);
// Create something.
$id = outagedb::save($this->createoutage(1));
@@ -153,7 +152,7 @@ class dml_outagedb_test extends base_testcase {
/**
* Make sure we can finish outages.
*/
public function test_finish() {
public function test_finish(): void {
$now = time();
$this->resetAfterTest(true);
// Create it.
@@ -173,7 +172,7 @@ class dml_outagedb_test extends base_testcase {
/**
* Make sure getall brings all entries.
*/
public function test_getall() {
public function test_getall(): void {
$this->resetAfterTest(true);
$amount = 10;
// Should start empty.
@@ -190,7 +189,7 @@ class dml_outagedb_test extends base_testcase {
/**
* Perform some tests on the data itself, checking values after inserted and updated.
*/
public function test_basiccrud() {
public function test_basiccrud(): void {
$this->resetAfterTest(true);
// Create some outages.
@@ -208,7 +207,7 @@ class dml_outagedb_test extends base_testcase {
self::assertNotNull($inserted);
// Check its data.
foreach (['starttime', 'stoptime', 'warntime', 'title', 'description'] as $field) {
self::assertSame($outage->$field, $inserted->$field, 'Field '.$field.' does not match.');
self::assertSame($outage->$field, $inserted->$field, 'Field ' . $field . ' does not match.');
}
// Check generated data.
self::assertGreaterThan(0, $inserted->id);
@@ -216,11 +215,11 @@ class dml_outagedb_test extends base_testcase {
self::assertNotNull($inserted->createdby);
self::assertNotNull($inserted->modifiedby);
// Change it.
$inserted->title = 'Title ID'.$id;
$inserted->title = 'Title ID' . $id;
outagedb::save($inserted);
// Get it again and check data.
$updated = outagedb::get_by_id($id);
self::assertSame('Title ID'.$id, $updated->title);
self::assertSame('Title ID' . $id, $updated->title);
self::assertSame($inserted->description, $updated->description);
// Delete it.
outagedb::delete($id);
@@ -232,7 +231,7 @@ class dml_outagedb_test extends base_testcase {
/**
* Tests the outagedb::get_active() method.
*/
public function test_getactive() {
public function test_getactive(): void {
$this->resetAfterTest(true);
// Have a consistent time for now (no seconds variation), helps debugging.
@@ -259,8 +258,14 @@ class dml_outagedb_test extends base_testcase {
self::saveoutage(false, $now, -2, 0, 0, 'Invalid outage.');
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
self::saveoutage(false, $now, -1, 2, 3,
'Another outage in warning period, but ignored as it starts after the previous one.');
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(false, $now, -3, -2, 2, 'An finished outage.', -1);
@@ -272,15 +277,21 @@ class dml_outagedb_test extends base_testcase {
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(false, $now, -3, -2, 1,
'Another ongoing outage starting at the same time, but ignored as it stops before the previous one.');
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.');
}
/**
* Tests the outagedb::get_all_unended() method.
*/
public function test_getallunended() {
public function test_getallunended(): void {
$this->resetAfterTest(true);
// Have a consistent time for now (no seconds variation), helps debugging.
@@ -296,42 +307,66 @@ class dml_outagedb_test extends base_testcase {
self::assertEquals([], outagedb::get_all_unended($now), 'No future outages yet.');
$id1 = self::saveoutage(false, $now, 2, 3, 4, 'A future outage.');
self::assertEquals([$id1],
self::createidarray(outagedb::get_all_unended($now)), 'Wrong future data.');
self::assertEquals(
[$id1],
self::createidarray(outagedb::get_all_unended($now)),
'Wrong future data.'
);
$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.');
self::assertEquals(
[$id1, $id2],
self::createidarray(outagedb::get_all_unended($now)),
'Wrong future data.'
);
$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.');
self::assertEquals(
[$id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)),
'Wrong future data.'
);
$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.');
self::assertEquals(
[$id4, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)),
'Wrong future data.'
);
$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.');
self::assertEquals(
[$id4, $id5, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)),
'Wrong future data.'
);
$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.');
self::assertEquals(
[$id6, $id4, $id5, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)),
'Wrong future data.'
);
$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.');
self::assertEquals(
[$id6, $id7, $id4, $id5, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)),
'Wrong future data.'
);
$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.');
self::assertEquals(
[$id6, $id8, $id7, $id4, $id5, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)),
'Wrong future data.'
);
}
/**
* Tests the outagedb::get_all_ended() method.
*/
public function test_getallended() {
public function test_getallended(): void {
$this->resetAfterTest(true);
// Have a consistent time for now (no seconds variation), helps debugging.
@@ -350,26 +385,38 @@ class dml_outagedb_test extends base_testcase {
self::assertEquals([], outagedb::get_all_ended($now), 'No past outages yet.');
$id1 = self::saveoutage(false, $now, -8, -6, -4, 'A past outage.');
self::assertEquals([$id1],
self::createidarray(outagedb::get_all_ended($now)), 'Wrong past data.');
self::assertEquals(
[$id1],
self::createidarray(outagedb::get_all_ended($now)),
'Wrong past data.'
);
$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.');
self::assertEquals(
[$id1, $id2],
self::createidarray(outagedb::get_all_ended($now)),
'Wrong past data.'
);
$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.');
self::assertEquals(
[$id3, $id1, $id2],
self::createidarray(outagedb::get_all_ended($now)),
'Wrong past data.'
);
$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.');
self::assertEquals(
[$id4, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_ended($now)),
'Wrong past data.'
);
}
/**
* Tests the outagedb::get_by_id() with an invalid parameter.
*/
public function test_getbyid_invalid() {
public function test_getbyid_invalid(): void {
$this->resetAfterTest(true);
$this->set_expected_exception('coding_exception');
outagedb::get_by_id(-1);
@@ -378,7 +425,7 @@ class dml_outagedb_test extends base_testcase {
/**
* Tests the outagedb::delete() with an invalid parameter.
*/
public function test_delete_invalid() {
public function test_delete_invalid(): void {
$this->resetAfterTest(true);
$this->set_expected_exception('coding_exception');
outagedb::delete(-1);
@@ -387,7 +434,7 @@ class dml_outagedb_test extends base_testcase {
/**
* Tests the outagedb::get_active() with an invalid parameter.
*/
public function test_getactive_invalid() {
public function test_getactive_invalid(): void {
$this->resetAfterTest(true);
$this->set_expected_exception('coding_exception');
outagedb::get_active(-1);
@@ -396,7 +443,7 @@ class dml_outagedb_test extends base_testcase {
/**
* Tests the outagedb::get_all_unended() with an invalid parameter.
*/
public function test_getallunended_invalid() {
public function test_getallunended_invalid(): void {
$this->resetAfterTest(true);
$this->set_expected_exception('coding_exception');
outagedb::get_all_unended(-1);
@@ -405,7 +452,7 @@ class dml_outagedb_test extends base_testcase {
/**
* Checks we can execute outagedb::get_all_unended() without parameters (now).
*/
public function test_getallunended_now() {
public function test_getallunended_now(): void {
$this->resetAfterTest(true);
self::assertEmpty(outagedb::get_all_unended());
}
@@ -413,7 +460,7 @@ class dml_outagedb_test extends base_testcase {
/**
* Tests the outagedb::get_all_ended() with an invalid parameter.
*/
public function test_getallended_invalid() {
public function test_getallended_invalid(): void {
$this->resetAfterTest(true);
$this->set_expected_exception('coding_exception');
outagedb::get_all_ended(-1);
@@ -422,7 +469,7 @@ class dml_outagedb_test extends base_testcase {
/**
* Checks we can execute outagedb::test_getallended_now() without parameters (now).
*/
public function test_getallended_now() {
public function test_getallended_now(): void {
$this->resetAfterTest(true);
self::assertEmpty(outagedb::get_all_ended());
}
@@ -430,7 +477,7 @@ class dml_outagedb_test extends base_testcase {
/**
* Tests the outagedb::finish() with an invalid parameter.
*/
public function test_finish_invalid() {
public function test_finish_invalid(): void {
$this->resetAfterTest(true);
$this->set_expected_exception('coding_exception');
outagedb::finish(1, -1);
@@ -439,7 +486,7 @@ class dml_outagedb_test extends base_testcase {
/**
* Tests the outagedb::finish() with a non existing outage.
*/
public function test_finish_now_notfound() {
public function test_finish_now_notfound(): void {
$this->resetAfterTest(true);
outagedb::finish(1);
self::assertCount(1, $this->getDebuggingMessages());
@@ -449,7 +496,7 @@ class dml_outagedb_test extends base_testcase {
/**
* Try to finish not ongoing outages.
*/
public function test_finish_notongoing() {
public function test_finish_notongoing(): void {
$this->resetAfterTest(true);
$time = time();
$outage = new outage([
@@ -471,7 +518,7 @@ class dml_outagedb_test extends base_testcase {
/**
* Tests the outagedb::get_next_starting() with an invalid parameter.
*/
public function test_getnextstartinginvalid() {
public function test_getnextstartinginvalid(): void {
$this->resetAfterTest(true);
$this->set_expected_exception('coding_exception');
outagedb::get_next_starting(-1);
@@ -480,7 +527,7 @@ class dml_outagedb_test extends base_testcase {
/**
* Tests the outagedb::get_next_autostarting() with an invalid parameter.
*/
public function test_getnextautostartinginvalid() {
public function test_getnextautostartinginvalid(): void {
$this->resetAfterTest(true);
$this->set_expected_exception('coding_exception');
outagedb::get_next_autostarting(-1);
@@ -497,7 +544,7 @@ class dml_outagedb_test extends base_testcase {
'starttime' => $i * 100,
'stoptime' => $i * 100 + 50,
'warntime' => $i * 60,
'title' => 'The Title '.$i,
'title' => 'The Title ' . $i,
'description' => 'A <b>description</b> in HTML.',
]);
}
+15 -16
View File
@@ -29,7 +29,7 @@ use auth_outage\form\outage\finish;
use auth_outage\form\outage\edit;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/base_testcase.php');
require_once(__DIR__ . '/base_testcase.php');
/**
* forms_test test class.
@@ -40,8 +40,7 @@ require_once(__DIR__.'/base_testcase.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\form\outage\edit
*/
class forms_test extends base_testcase {
final class forms_test extends base_testcase {
public function tearDown(): void {
parent::tearDown();
}
@@ -49,21 +48,21 @@ class forms_test extends base_testcase {
/**
* Create a delete form.
*/
public function test_delete() {
public function test_delete(): void {
new delete();
}
/**
* Create a finish form.
*/
public function test_finish() {
public function test_finish(): void {
new finish();
}
/**
* Mock some data and check values.
*/
public function test_edit_valid() {
public function test_edit_valid(): void {
if ($this->skip_because_moodle_is_below_30('Moodle POST mocking was fixed in Moodle 30.')) {
return;
}
@@ -84,7 +83,7 @@ class forms_test extends base_testcase {
/**
* Check invalid warning duration.
*/
public function test_edit_invalid_warning() {
public function test_edit_invalid_warning(): void {
if ($this->skip_because_moodle_is_below_30('Moodle POST mocking was fixed in Moodle 30.')) {
return;
}
@@ -99,7 +98,7 @@ class forms_test extends base_testcase {
/**
* Check invalid outage duration.
*/
public function test_edit_invalid_duration() {
public function test_edit_invalid_duration(): void {
if ($this->skip_because_moodle_is_below_30('Moodle POST mocking was fixed in Moodle 30.')) {
return;
}
@@ -113,7 +112,7 @@ class forms_test extends base_testcase {
/**
* Check invalid title (empty).
*/
public function test_edit_invalid_title() {
public function test_edit_invalid_title(): void {
if ($this->skip_because_moodle_is_below_30('Moodle POST mocking was fixed in Moodle 30.')) {
return;
}
@@ -127,14 +126,14 @@ class forms_test extends base_testcase {
/**
* Check invalid title (too long).
*/
public function test_edit_invalid_title_toolong() {
public function test_edit_invalid_title_toolong(): void {
if ($this->skip_because_moodle_is_below_30('Moodle POST mocking was fixed in Moodle 30.')) {
return;
}
$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. '.
$_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());
@@ -143,7 +142,7 @@ class forms_test extends base_testcase {
/**
* Check invalid format for description.
*/
public function test_edit_description_invalid_format() {
public function test_edit_description_invalid_format(): void {
if ($this->skip_because_moodle_is_below_30('Moodle POST mocking was fixed in Moodle 30.')) {
return;
}
@@ -159,7 +158,7 @@ class forms_test extends base_testcase {
/**
* Check if set data works properly.
*/
public function test_setdata() {
public function test_setdata(): void {
$outage = new outage([
'autostart' => false,
'warntime' => time() - 60,
@@ -175,7 +174,7 @@ class forms_test extends base_testcase {
/**
* Check edit::set_data() with invalid parameter.
*/
public function test_setdata_invalid() {
public function test_setdata_invalid(): void {
$edit = new edit();
$this->set_expected_exception('coding_exception');
$edit->set_data(null);
@@ -214,7 +213,7 @@ class forms_test extends base_testcase {
// The bugfix MDL-56250 in only applies to Moodle 30+.
// Before that the form validation test is meaningless (results are cached), so skip it.
if ($CFG->branch < 30) {
$this->markTestSkipped('Some tests can only run in Moodle 30+. '.$reason);
$this->markTestSkipped('Some tests can only run in Moodle 30+. ' . $reason);
return true;
}
+14 -14
View File
@@ -15,8 +15,8 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/base_testcase.php');
require_once(__DIR__.'/../lib.php');
require_once(__DIR__ . '/base_testcase.php');
require_once(__DIR__ . '/../lib.php');
/**
* tests for lib.php
@@ -27,17 +27,17 @@ require_once(__DIR__.'/../lib.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers ::auth_outage_get_climaintenance_resource_file
*/
class lib_test extends \core_phpunit\testcase {
final class lib_test extends \core_phpunit\testcase {
/**
* Test this plugin gets climaintenance resource file.
*/
public function test_auth_outage_get_climaintenance_resource_file_resolves_a_file() {
public function test_auth_outage_get_climaintenance_resource_file_resolves_a_file(): void {
global $CFG;
$dir = $CFG->dataroot.'/auth_outage/climaintenance';
$dir = $CFG->dataroot . '/auth_outage/climaintenance';
mkdir($dir, 0777, true);
// Create a file.
$expected = $dir.'/example.txt';
$expected = $dir . '/example.txt';
file_put_contents($expected, 'Outage Unit Test Message');
// Get that file.
@@ -53,17 +53,17 @@ class lib_test extends \core_phpunit\testcase {
/**
* Regression test for issue #104.
*/
public function test_auth_outage_get_climaintenance_resource_file_resolves_a_file_with_symlink() {
public function test_auth_outage_get_climaintenance_resource_file_resolves_a_file_with_symlink(): void {
global $CFG;
// Create a file.
$realdir = $CFG->dataroot.'/auth_outage/climaintenance_real';
$realdir = $CFG->dataroot . '/auth_outage/climaintenance_real';
mkdir($realdir, 0777, true);
$realfile = $realdir.'/example.txt';
$realfile = $realdir . '/example.txt';
file_put_contents($realfile, 'Outage Unit Test Message');
// Create a symlink.
$symdir = $CFG->dataroot.'/auth_outage/climaintenance';
$symdir = $CFG->dataroot . '/auth_outage/climaintenance';
if (!symlink($realdir, $symdir)) {
unlink($realfile);
rmdir($realdir);
@@ -85,18 +85,18 @@ class lib_test extends \core_phpunit\testcase {
/**
* Test this plugin gets climaintenance resource file and prevents path traversal attack.
*/
public function test_auth_outage_get_climaintenance_resource_file_prevent_path_traversal() {
public function test_auth_outage_get_climaintenance_resource_file_prevent_path_traversal(): void {
global $CFG;
$dir = $CFG->dataroot.'/auth_outage/climaintenance';
$dir = $CFG->dataroot . '/auth_outage/climaintenance';
mkdir($dir, 0777, true);
// Create a file.
$expected = $dir.'/example.txt';
$expected = $dir . '/example.txt';
file_put_contents($expected, 'Outage Unit Test Message');
// Create a sensitive file.
$sensitivefile = $CFG->dataroot.'/auth_outage/nuclear_silo_passwords.txt';
$sensitivefile = $CFG->dataroot . '/auth_outage/nuclear_silo_passwords.txt';
file_put_contents($sensitivefile, 'The password to launch the ICBM: 123456');
// Path Traversal Attack.
+12 -13
View File
@@ -17,7 +17,7 @@
use auth_outage\local\outage;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/base_testcase.php');
require_once(__DIR__ . '/base_testcase.php');
/**
* outage_test test class.
@@ -28,8 +28,7 @@ require_once(__DIR__.'/base_testcase.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\local\outage
*/
class outage_test extends base_testcase {
final class outage_test extends base_testcase {
public function tearDown(): void {
parent::tearDown();
}
@@ -37,7 +36,7 @@ class outage_test extends base_testcase {
/**
* Tests the constructor.
*/
public function test_constructor() {
public function test_constructor(): void {
$outage = new outage();
// Very important, this should never change.
self::assertNull($outage->id, 'New empty outage can never have an id set.');
@@ -50,7 +49,7 @@ class outage_test extends base_testcase {
/**
* Tests the constructor, giving data as an object.
*/
public function test_constructor_object() {
public function test_constructor_object(): void {
$obj = new \stdClass();
$obj->id = 1;
$obj->autostart = true;
@@ -74,7 +73,7 @@ class outage_test extends base_testcase {
/**
* Tests the constructor with invalid data.
*/
public function test_constructor_invalid() {
public function test_constructor_invalid(): void {
$this->set_expected_exception('coding_exception');
new outage('My outage');
}
@@ -82,7 +81,7 @@ class outage_test extends base_testcase {
/**
* Tests getting the stage considering the current time (now).
*/
public function test_getstage_now() {
public function test_getstage_now(): void {
$now = time();
// Make sure it is in the past.
$outage = new outage([
@@ -98,7 +97,7 @@ class outage_test extends base_testcase {
/**
* Tests getting the stage providing an invalid time reference.
*/
public function test_getstage_invalidtime() {
public function test_getstage_invalidtime(): void {
$outage = new outage();
$this->set_expected_exception('coding_exception');
$outage->get_stage(-1);
@@ -107,7 +106,7 @@ class outage_test extends base_testcase {
/**
* Tests is_ongoing() with different outage stages.
*/
public function test_isongoing() {
public function test_isongoing(): void {
$now = time();
// In the past.
@@ -144,7 +143,7 @@ class outage_test extends base_testcase {
/**
* Tests is_active() with different outage stages.
*/
public function test_isactive() {
public function test_isactive(): void {
$now = time();
// In the past.
@@ -191,7 +190,7 @@ class outage_test extends base_testcase {
/**
* Tests different outage stages.
*/
public function test_stages() {
public function test_stages(): void {
$now = time();
$outage = new outage([
@@ -266,7 +265,7 @@ class outage_test extends base_testcase {
/**
* Tests if getting title and description replaces the placeholders.
*/
public function test_gettitle_getdescription() {
public function test_gettitle_getdescription(): void {
$now = time();
$outage = new outage([
'warntime' => $now - 50,
@@ -287,7 +286,7 @@ class outage_test extends base_testcase {
/**
* Tests getting the durations.
*/
public function test_getdurations() {
public function test_getdurations(): void {
$outage = new outage(['starttime' => 1000]);
self::assertNull($outage->get_duration_actual());
+42 -38
View File
@@ -20,8 +20,8 @@ use auth_outage\local\outagelib;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir.'/adminlib.php');
require_once(__DIR__.'/base_testcase.php');
require_once($CFG->libdir . '/adminlib.php');
require_once(__DIR__ . '/base_testcase.php');
/**
* outagelib_test test class.
@@ -32,8 +32,7 @@ require_once(__DIR__.'/base_testcase.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\local\outagelib
*/
class outagelib_test extends base_testcase {
final class outagelib_test extends base_testcase {
public function tearDown(): void {
parent::tearDown();
}
@@ -41,7 +40,7 @@ class outagelib_test extends base_testcase {
/**
* Check if maintenance message is disabled as needed.
*/
public function test_maintenancemessage() {
public function test_maintenancemessage(): void {
$this->resetAfterTest(true);
static::setAdminUser();
@@ -69,7 +68,7 @@ class outagelib_test extends base_testcase {
/**
* Check if maintenance later is removed if no outage set.
*/
public function test_maintenancelater_nonext() {
public function test_maintenancelater_nonext(): void {
$this->resetAfterTest(true);
set_config('maintenance_later', time() + (60 * 60 * 24 * 7)); // In 1 week.
self::assertNotEmpty(get_config('moodle', 'maintenance_later'));
@@ -80,7 +79,7 @@ class outagelib_test extends base_testcase {
/**
* Check outagelib::inject() works as expected.
*/
public function test_inject() {
public function test_inject(): void {
global $OUTPUT;
$this->resetAfterTest(true);
@@ -116,7 +115,7 @@ class outagelib_test extends base_testcase {
/**
* Check outagelib::inject() will not break the page if something goes wrong.
*/
public function test_inject_broken() {
public function test_inject_broken(): void {
$_GET = ['auth_outage_break_code' => '1'];
outagelib::reset_injectcalled();
$header = outagelib::get_inject_code();
@@ -127,7 +126,7 @@ class outagelib_test extends base_testcase {
/**
* Check if injection works with preview.
*/
public function test_inject_preview() {
public function test_inject_preview(): void {
global $CFG;
$this->resetAfterTest(true);
self::setAdminUser();
@@ -157,7 +156,7 @@ class outagelib_test extends base_testcase {
/**
* Check if injection works with invalid preview without stopping the page.
*/
public function test_inject_preview_notfound() {
public function test_inject_preview_notfound(): void {
global $CFG;
$_GET = ['auth_outage_preview' => '1'];
@@ -170,7 +169,7 @@ class outagelib_test extends base_testcase {
/**
* Test injection with preview and delta.
*/
public function test_inject_preview_withdelta() {
public function test_inject_preview_withdelta(): void {
global $CFG;
$this->resetAfterTest(true);
self::setAdminUser();
@@ -198,7 +197,7 @@ class outagelib_test extends base_testcase {
/**
* Test injection without active outage.
*/
public function test_inject_noactive() {
public function test_inject_noactive(): void {
outagelib::reset_injectcalled();
outagelib::get_inject_code();
}
@@ -206,7 +205,7 @@ class outagelib_test extends base_testcase {
/**
* Check if get config works without getting defaults.
*/
public function test_get_config() {
public function test_get_config(): void {
$this->resetAfterTest(true);
$keys = [
'css',
@@ -220,12 +219,12 @@ class outagelib_test extends base_testcase {
];
// Set config with values.
foreach ($keys as $k) {
set_config($k, $k.'_value', 'auth_outage');
set_config($k, $k . '_value', 'auth_outage');
}
// Ensure it is not using any defaults.
$config = outagelib::get_config();
foreach ($keys as $k) {
self::assertSame($config->$k, $k.'_value', 'auth_outage');
self::assertSame($config->$k, $k . '_value', 'auth_outage');
}
set_config('allowedips_forced', 'allowedips_forced_value', 'auth_outage');
@@ -236,7 +235,7 @@ class outagelib_test extends base_testcase {
/**
* Check that config has key.
*/
public function test_config_keys() {
public function test_config_keys(): void {
$this->resetAfterTest(true);
$keys = [
'allowedips',
@@ -257,7 +256,7 @@ class outagelib_test extends base_testcase {
/**
* Check if get config works getting defaults when needed.
*/
public function test_get_config_invalid() {
public function test_get_config_invalid(): void {
$this->resetAfterTest(true);
// Set config with invalid values.
set_config('allowedips', " \n", 'auth_outage');
@@ -279,7 +278,7 @@ class outagelib_test extends base_testcase {
/**
* Check if outagelib::inject() does not inject on admin/settings.php?section=additionalhtml
*/
public function test_inject_settings() {
public function test_inject_settings(): void {
global $CFG;
$this->resetAfterTest(true);
@@ -312,7 +311,7 @@ class outagelib_test extends base_testcase {
/**
* Test create maintenance php code
*/
public function test_createmaintenancephpcode() {
public function test_createmaintenancephpcode(): void {
global $CFG;
$CFG->cookiehttponly = false;
@@ -392,7 +391,7 @@ EOT;
* @param string $configkey The key of the config.
* @dataProvider createmaintenancephpcode_withoutage_provider
*/
public function test_createmaintenancephpcode_withoutage($configkey) {
public function test_createmaintenancephpcode_withoutage($configkey): void {
global $CFG;
$this->resetAfterTest(true);
$CFG->cookiehttponly = false;
@@ -466,7 +465,7 @@ EOT;
'stoptime' => 456,
'accesskey' => '5678',
]);
$file = $CFG->dataroot.'/climaintenance.php';
$file = $CFG->dataroot . '/climaintenance.php';
set_config($configkey, '127.0.0.1', 'auth_outage');
outagelib::update_climaintenance_code($outage);
@@ -486,7 +485,7 @@ EOT;
/**
* Test create maintenance php code without IPs or accesskey
*/
public function test_createmaintenancephpcode_withoutips_or_accesskey() {
public function test_createmaintenancephpcode_withoutips_or_accesskey(): void {
global $CFG;
$this->resetAfterTest(true);
@@ -495,7 +494,7 @@ EOT;
'stoptime' => 456,
'accesskey' => null,
]);
$file = $CFG->dataroot.'/climaintenance.php';
$file = $CFG->dataroot . '/climaintenance.php';
set_config('allowedips', '', 'auth_outage');
set_config('allowedips_forced', '', 'auth_outage');
@@ -512,9 +511,9 @@ EOT;
/**
* Test create maintenance php code without outage
*/
public function test_createmaintenancephpcode_withoutoutage() {
public function test_createmaintenancephpcode_withoutoutage(): void {
global $CFG;
$file = $CFG->dataroot.'/climaintenance.php';
$file = $CFG->dataroot . '/climaintenance.php';
touch($file);
outagelib::update_climaintenance_code(null);
@@ -529,13 +528,13 @@ EOT;
/**
* Related to Issue #70: Creating ongoing outage does not trigger maintenance file creation.
*/
public function test_preparenextoutage_notautostart() {
public function test_preparenextoutage_notautostart(): void {
global $CFG;
$this->create_outage();
// The method outagelib::prepare_next_outage() should have been called by save().
foreach ([$CFG->dataroot.'/climaintenance.template.html', $CFG->dataroot.'/climaintenance.php'] as $file) {
foreach ([$CFG->dataroot . '/climaintenance.template.html', $CFG->dataroot . '/climaintenance.php'] as $file) {
self::assertFileExists($file);
unlink($file);
}
@@ -544,7 +543,7 @@ EOT;
/**
* Regression Test - Issue #82: When changing the IP address list it should recreate the maintenance files.
*/
public function test_when_we_change_allowed_ips_in_settings_it_updates_the_templates() {
public function test_when_we_change_allowed_ips_in_settings_it_updates_the_templates(): void {
global $CFG;
$this->create_outage();
@@ -553,7 +552,7 @@ EOT;
set_config('s_auth_outage_allowedips', '127', 'auth_outage');
// The method outagelib::prepare_next_outage() should have been called from admin_write_settings().
foreach ([$CFG->dataroot.'/climaintenance.template.html', $CFG->dataroot.'/climaintenance.php'] as $file) {
foreach ([$CFG->dataroot . '/climaintenance.template.html', $CFG->dataroot . '/climaintenance.php'] as $file) {
self::assertFileExists($file);
unlink($file);
}
@@ -562,7 +561,7 @@ EOT;
/**
* Problem detected while solving Issue #82.
*/
public function test_when_we_change_remove_selectors_in_settings_it_updates_the_templates() {
public function test_when_we_change_remove_selectors_in_settings_it_updates_the_templates(): void {
global $CFG;
$this->create_outage();
@@ -571,7 +570,7 @@ EOT;
set_config('s_auth_outage_remove_selectors', '.something', 'auth_outage');
// The method outagelib::prepare_next_outage() should have been called from admin_write_settings().
foreach ([$CFG->dataroot.'/climaintenance.template.html', $CFG->dataroot.'/climaintenance.php'] as $file) {
foreach ([$CFG->dataroot . '/climaintenance.template.html', $CFG->dataroot . '/climaintenance.php'] as $file) {
self::assertFileExists($file);
unlink($file);
}
@@ -580,7 +579,7 @@ EOT;
/**
* Related to Issue #72: IP Block still triggers cli maintenance mode even without autostart.
*/
public function test_preparenextoutage_noautostarttrigger() {
public function test_preparenextoutage_noautostarttrigger(): void {
global $CFG;
$this->resetAfterTest(true);
@@ -601,16 +600,16 @@ EOT;
// This file should not exist even if the statement above fails as Moodle does not create it immediately but test anyway.
// Backwards compatibility with older PHPUnit - use old assertFile method.
if (method_exists($this, 'assertFileDoesNotExist')) {
self::assertFileDoesNotExist($CFG->dataroot.'/climaintenance.html');
self::assertFileDoesNotExist($CFG->dataroot . '/climaintenance.html');
} else {
self::assertFileNotExists($CFG->dataroot.'/climaintenance.html');
self::assertFileNotExists($CFG->dataroot . '/climaintenance.html');
}
}
/**
* Regression test for issue #85.
*/
public function test_it_can_inject_in_settings_if_not_additional_html() {
public function test_it_can_inject_in_settings_if_not_additional_html(): void {
global $CFG;
$this->resetAfterTest(true);
@@ -749,8 +748,13 @@ EOT;
* see https://github.com/sebastianbergmann/phpunit/issues/720#issuecomment-10421092
* @runClassInSeparateProcess
*/
public function test_evaluation_maintenancepage(?string $allowedips, ?string $iptouse, ?string $accesskey,
?string $accesskeytouse, array $expectedoutputs) {
public function test_evaluation_maintenancepage(
?string $allowedips,
?string $iptouse,
?string $accesskey,
?string $accesskeytouse,
array $expectedoutputs
): void {
global $CFG, $_SERVER, $_GET;
@@ -771,7 +775,7 @@ EOT;
set_config('allowedips', $allowedips, 'auth_outage');
}
// Ensure if the file exists we clean it (e.g. from a previous test run).
$file = $CFG->dataroot.'/climaintenance.php';
$file = $CFG->dataroot . '/climaintenance.php';
if (file_exists($file)) {
unlink($file);
}