Issue #390: Code cleanup to fix code standards codesniffer errors.

This commit is contained in:
abhinavgandham
2026-01-12 15:08:32 +10:00
parent 42dbec5b4d
commit 7f968b2365
52 changed files with 481 additions and 363 deletions

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,18 @@ 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 +239,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 +255,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 +283,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 +321,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 +335,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);
}
}

View File

@@ -30,7 +30,7 @@ namespace auth_outage\dml;
use auth_outage\local\outage;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/../base_testcase.php');
require_once(__DIR__ . '/../base_testcase.php');
/**
* installation_test test class.
@@ -74,24 +74,33 @@ class installation_test extends \auth_outage\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');
}
}

View File

@@ -28,7 +28,7 @@ namespace auth_outage\dml;
use auth_outage\local\outage;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/../base_testcase.php');
require_once(__DIR__ . '/../base_testcase.php');
/**
* outagedb_test tests class.
@@ -112,7 +112,7 @@ class outagedb_test extends \auth_outage\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);
}
}
@@ -204,7 +204,7 @@ class outagedb_test extends \auth_outage\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);
@@ -212,11 +212,11 @@ class outagedb_test extends \auth_outage\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);
@@ -255,8 +255,14 @@ class outagedb_test extends \auth_outage\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);
@@ -268,8 +274,14 @@ class outagedb_test extends \auth_outage\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.');
}
@@ -292,36 +304,60 @@ class outagedb_test extends \auth_outage\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.'
);
}
/**
@@ -346,20 +382,32 @@ class outagedb_test extends \auth_outage\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.'
);
}
/**
@@ -493,7 +541,7 @@ class outagedb_test extends \auth_outage\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.',
]);
}

View File

@@ -28,7 +28,7 @@ namespace auth_outage\form\outage;
use auth_outage\local\outage;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/../../base_testcase.php');
require_once(__DIR__ . '/../../base_testcase.php');
/**
* forms_test test class.
@@ -127,8 +127,8 @@ class forms_test extends \auth_outage\base_testcase {
}
$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());
@@ -208,7 +208,7 @@ class forms_test extends \auth_outage\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;
}

View File

@@ -17,8 +17,8 @@
namespace auth_outage;
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
@@ -35,11 +35,11 @@ class lib_test extends base_testcase {
*/
public function test_auth_outage_get_climaintenance_resource_file_resolves_a_file() {
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.
@@ -59,13 +59,13 @@ class lib_test extends base_testcase {
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);
@@ -90,15 +90,15 @@ class lib_test extends base_testcase {
public function test_auth_outage_get_climaintenance_resource_file_prevent_path_traversal() {
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.

View File

@@ -26,7 +26,7 @@
namespace auth_outage\local\cli;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/cli_testcase.php');
require_once(__DIR__ . '/cli_testcase.php');
/**
* cli_test test class.

View File

@@ -26,7 +26,7 @@
namespace auth_outage\local\cli;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/../../base_testcase.php');
require_once(__DIR__ . '/../../base_testcase.php');
/**
* cli_testcase class.

View File

@@ -29,7 +29,7 @@ use auth_outage\dml\outagedb;
use auth_outage\local\outage;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/cli_testcase.php');
require_once(__DIR__ . '/cli_testcase.php');
/**
* create_test test class.
@@ -255,7 +255,7 @@ class create_test extends cli_testcase {
$this->set_parameters([
'--onlyid',
'--start=60',
'--clone='.$id,
'--clone=' . $id,
]);
$cli = new create();
$cli->set_referencetime($now);

View File

@@ -29,7 +29,7 @@ use auth_outage\dml\outagedb;
use auth_outage\local\outage;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/cli_testcase.php');
require_once(__DIR__ . '/cli_testcase.php');
/**
* finish_test test class.
@@ -100,7 +100,7 @@ class 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);
@@ -121,7 +121,7 @@ class 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);

View File

@@ -29,7 +29,7 @@ use auth_outage\dml\outagedb;
use auth_outage\local\outage;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/cli_testcase.php');
require_once(__DIR__ . '/cli_testcase.php');
/**
* waitforit_test test class.
@@ -127,7 +127,7 @@ class 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);

View File

@@ -29,7 +29,7 @@ use auth_outage\local\outage;
use context_system;
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.

View File

@@ -29,7 +29,7 @@ use auth_outage\task\update_static_page;
use DOMDocument;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/../../base_testcase.php');
require_once(__DIR__ . '/../../base_testcase.php');
/**
* maintenance_static_page_test class.
@@ -47,10 +47,12 @@ class maintenance_static_page_test extends \auth_outage\base_testcase {
public function test_templatefile() {
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()
);
}
/**
@@ -59,9 +61,9 @@ class maintenance_static_page_test extends \auth_outage\base_testcase {
public function test_resourcesfolder() {
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());
}
/**
@@ -76,7 +78,7 @@ class maintenance_static_page_test extends \auth_outage\base_testcase {
*/
public function test_createfromhtml() {
$html = "<!DOCTYPE html>\n<html><head><title>Title</title></head><body>Content</body></html>";
$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));
}
@@ -85,8 +87,8 @@ class maintenance_static_page_test extends \auth_outage\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>'.
$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();
@@ -100,9 +102,9 @@ class maintenance_static_page_test extends \auth_outage\base_testcase {
public function test_updatelinkstylesheet() {
$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);
@@ -115,17 +117,19 @@ class maintenance_static_page_test extends \auth_outage\base_testcase {
*/
public function test_updatelinkstylesheet_urls() {
$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().'/53365950336b070c0b26ca50e7d0dad962c364e6.dGV4dC9wbGFpbg');
self::assertFileExists($page->get_io()->get_resources_folder() .
'/53365950336b070c0b26ca50e7d0dad962c364e6.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');
}
/**
@@ -133,17 +137,19 @@ class maintenance_static_page_test extends \auth_outage\base_testcase {
*/
public function test_updatelinkstylesheet_urls_quoted() {
$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().'/e0b34925c1f939c247a4b50d6bf08c76088def39.dGV4dC9wbGFpbg');
self::assertFileExists($page->get_io()->get_resources_folder()
. '/e0b34925c1f939c247a4b50d6bf08c76088def39.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');
}
/**
@@ -151,17 +157,19 @@ class maintenance_static_page_test extends \auth_outage\base_testcase {
*/
public function test_updatelinkstylesheet_urls_subdir() {
$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');
}
/**
@@ -170,9 +178,9 @@ class maintenance_static_page_test extends \auth_outage\base_testcase {
public function test_updateimages() {
$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);
@@ -185,8 +193,8 @@ class maintenance_static_page_test extends \auth_outage\base_testcase {
*/
public function test_updatelinkfavicon() {
$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,9 +237,9 @@ class maintenance_static_page_test extends \auth_outage\base_testcase {
$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;
@@ -254,8 +262,8 @@ class maintenance_static_page_test extends \auth_outage\base_testcase {
*/
public function test_previewpath() {
$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);
@@ -295,7 +303,7 @@ class maintenance_static_page_test extends \auth_outage\base_testcase {
*/
public function test_updatestaticpage_hasfile() {
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();
@@ -314,19 +322,19 @@ class maintenance_static_page_test extends \auth_outage\base_testcase {
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);
}
@@ -338,7 +346,7 @@ class maintenance_static_page_test extends \auth_outage\base_testcase {
* @return string
*/
private function get_fixture_path_location($file) {
return (string)new \moodle_url('/auth/outage/tests/local/controllers/fixtures/'.$file);
return (string)new \moodle_url('/auth/outage/tests/local/controllers/fixtures/' . $file);
}
/**
@@ -398,7 +406,7 @@ class maintenance_static_page_test extends \auth_outage\base_testcase {
* Test file get_data.
*/
public function test_file_get_data() {
$file = __DIR__.'/fixtures/catalyst.png';
$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']);
@@ -408,7 +416,7 @@ class maintenance_static_page_test extends \auth_outage\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');
$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());
@@ -444,7 +452,9 @@ class maintenance_static_page_test extends \auth_outage\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.
@@ -459,8 +469,8 @@ class maintenance_static_page_test extends \auth_outage\base_testcase {
*/
public function test_remove_css_selector() {
$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);
@@ -474,8 +484,8 @@ class maintenance_static_page_test extends \auth_outage\base_testcase {
*/
public function test_remove_css_selector_id() {
$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);
@@ -489,11 +499,11 @@ class maintenance_static_page_test extends \auth_outage\base_testcase {
*/
public function test_remove_css_selector_with_multiline() {
$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);
@@ -508,11 +518,11 @@ class maintenance_static_page_test extends \auth_outage\base_testcase {
*/
public function test_remove_css_selector_needing_trim() {
$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);
@@ -527,11 +537,11 @@ class maintenance_static_page_test extends \auth_outage\base_testcase {
*/
public function test_remove_css_selector_with_empty_line() {
$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);
@@ -546,8 +556,8 @@ class maintenance_static_page_test extends \auth_outage\base_testcase {
*/
public function test_remove_css_selector_with_invalid_id() {
$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);
@@ -561,8 +571,8 @@ class maintenance_static_page_test extends \auth_outage\base_testcase {
*/
public function test_meta_refresh_5minutes() {
$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);
@@ -575,8 +585,8 @@ class maintenance_static_page_test extends \auth_outage\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);

View File

@@ -17,7 +17,7 @@
namespace auth_outage\local;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/../base_testcase.php');
require_once(__DIR__ . '/../base_testcase.php');
/**
* outage_test test class.

View File

@@ -20,8 +20,8 @@ use auth_outage\dml\outagedb;
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.
@@ -215,12 +215,12 @@ class outagelib_test extends \auth_outage\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');
@@ -461,7 +461,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);
@@ -490,7 +490,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');
@@ -509,7 +509,7 @@ EOT;
*/
public function test_createmaintenancephpcode_withoutoutage() {
global $CFG;
$file = $CFG->dataroot.'/climaintenance.php';
$file = $CFG->dataroot . '/climaintenance.php';
touch($file);
outagelib::update_climaintenance_code(null);
@@ -530,7 +530,7 @@ EOT;
$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);
}
@@ -548,7 +548,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);
}
@@ -566,7 +566,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);
}
@@ -596,9 +596,9 @@ 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');
}
}
@@ -744,8 +744,13 @@ EOT;
* see https://github.com/sebastianbergmann/phpunit/issues/720#issuecomment-10421092
* @runInSeparateProcess
*/
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
) {
global $CFG, $_SERVER, $_GET;
@@ -766,7 +771,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);
}