Remove all usage of $CFG->additionalhtmltopofbody #211

This commit is contained in:
Jamie Stamp
2020-11-03 16:26:45 +00:00
parent 01bdacc90b
commit e4dc17aae4
4 changed files with 48 additions and 88 deletions

View File

@@ -66,11 +66,4 @@ class auth_plugin_outage extends auth_plugin_base {
public function user_login($username, $password) {
return false;
}
/**
* Login page hook.
*/
public function loginpage_hook() {
outagelib::inject();
}
}

View File

@@ -72,11 +72,10 @@ class outagelib {
}
/**
* Calls inject even if it was already called before.
* Resets inject called to allow the code to be regenerated.
*/
public static function reinject() {
public static function reset_injectcalled() {
self::$injectcalled = false;
self::inject();
}
/**
@@ -100,11 +99,11 @@ class outagelib {
/**
* Will check for ongoing or warning outages and will attach the message bar as required.
* Will check for ongoing or warning outages and will return the message bar as required.
*
* @return string|void CSS and HTML for the warning bar if it should be displayed
*/
public static function inject() {
global $CFG;
public static function get_inject_code() {
// Ensure we do not kill the whole website in case of an error.
try {
// Check if we should inject the code.
@@ -112,8 +111,6 @@ class outagelib {
return;
}
self::clean_outages();
// Check for a previewing outage, then for an active outage.
$previewid = optional_param('auth_outage_preview', null, PARAM_INT);
$time = time();
@@ -136,8 +133,7 @@ class outagelib {
}
// There is a previewing or active outage.
$CFG->additionalhtmltopofbody = renderer::get()->render_warningbar($active, $time, false, $preview).
$CFG->additionalhtmltopofbody;
return renderer::get()->render_warningbar($active, $time, false, $preview);
} catch (Exception $e) {
debugging('Exception occured while injecting our code: '.$e->getMessage());
debugging($e->getTraceAsString(), DEBUG_DEVELOPER);
@@ -397,26 +393,4 @@ EOT;
return $message;
}
/**
* Checks $CFG->additionalhtmltopofbody for saved outages and removes them.
* We should only be temporarily injecting into that variable and not saving them to the database.
*
* @return string the cleaned content
*/
public static function clean_outages() {
global $CFG;
// Replace the content to clean up pages that do not have the injection.
$re = '/' . self::OUTAGE_START . '[\s\S]*' . self::OUTAGE_END . '/m';
$replaced = preg_replace($re, '', $CFG->additionalhtmltopofbody);
// We have removed the outages and any duplicates as it should be injected and not saved to $CFG.
if ($CFG->additionalhtmltopofbody != $replaced) {
set_config('additionalhtmltopofbody', $replaced);
return $replaced;
}
return '';
}
}

31
lib.php
View File

@@ -27,27 +27,6 @@ use auth_outage\local\outagelib;
defined('MOODLE_INTERNAL') || die;
/**
* Used in Moodle 30+ when a user is logged on.
*/
function auth_outage_extend_navigation_user_settings() {
outagelib::inject();
}
/**
* Used in Moodle 30+ on the frontpage.
*/
function auth_outage_extend_navigation_frontpage() {
outagelib::inject();
}
/**
* Used in Moodle 31+ when a user is logged on.
*/
function auth_outage_extend_navigation_user() {
outagelib::inject();
}
/**
* Used for adminlib::set_updatedcallback which requires a string that resolves to a function.
*
@@ -98,3 +77,13 @@ function auth_outage_get_fontawesome_icon_map() {
'core:i/auth_outageevent' => 'fa-power-off',
];
}
/**
* Inject the warning bar into the page if there is currently an outage.
*
* @return string|void
*/
function auth_outage_before_standard_top_of_body_html() {
// Get code to inject.
return outagelib::get_inject_code();
}

View File

@@ -82,7 +82,7 @@ class outagelib_test extends auth_outage_base_testcase {
* Check outagelib::inject() works as expected.
*/
public function test_inject() {
global $CFG;
global $OUTPUT;
$this->resetAfterTest(true);
self::setAdminUser();
@@ -96,16 +96,15 @@ class outagelib_test extends auth_outage_base_testcase {
'description' => 'Description',
]);
$outage->id = outagedb::save($outage);
self::assertEmpty($CFG->additionalhtmltopofbody);
outagelib::reinject();
self::assertContains('<style>', $CFG->additionalhtmltopofbody);
self::assertContains('<script>', $CFG->additionalhtmltopofbody);
outagelib::reset_injectcalled();
$header1 = outagelib::get_inject_code();
self::assertContains('<style>', $header1);
self::assertContains('<script>', $header1);
// Should not inject more than once with the inject() function.
$size = strlen($CFG->additionalhtmltopofbody);
outagelib::inject();
self::assertSame($size, strlen($CFG->additionalhtmltopofbody));
// Should not inject more than once.
$size = strlen($OUTPUT->standard_top_of_body_html());
self::assertSame($size, strlen($OUTPUT->standard_top_of_body_html()));
}
/**
@@ -113,7 +112,8 @@ class outagelib_test extends auth_outage_base_testcase {
*/
public function test_inject_broken() {
$_GET = ['auth_outage_break_code' => '1'];
outagelib::reinject();
outagelib::reset_injectcalled();
$header = outagelib::get_inject_code();
self::assertCount(2, phpunit_util::get_debugging_messages());
phpunit_util::reset_debugging();
}
@@ -135,12 +135,13 @@ class outagelib_test extends auth_outage_base_testcase {
'description' => 'Description',
]);
$outage->id = outagedb::save($outage);
self::assertEmpty($CFG->additionalhtmltopofbody);
$_GET = ['auth_outage_preview' => (string)$outage->id];
outagelib::reinject();
self::assertContains('<style>', $CFG->additionalhtmltopofbody);
self::assertContains('<script>', $CFG->additionalhtmltopofbody);
outagelib::reset_injectcalled();
$header = outagelib::get_inject_code();
self::assertContains('<style>', $header);
self::assertContains('<script>', $header);
}
/**
@@ -148,11 +149,12 @@ class outagelib_test extends auth_outage_base_testcase {
*/
public function test_inject_preview_notfound() {
global $CFG;
self::assertEmpty($CFG->additionalhtmltopofbody);
$_GET = ['auth_outage_preview' => '1'];
// Should not throw exception or halt anything, silently ignore it.
outagelib::reinject();
self::assertEmpty($CFG->additionalhtmltopofbody);
outagelib::reset_injectcalled();
$header = outagelib::get_inject_code();
self::assertEmpty($header);
}
/**
@@ -172,18 +174,20 @@ class outagelib_test extends auth_outage_base_testcase {
'description' => 'Description',
]);
$outage->id = outagedb::save($outage);
self::assertEmpty($CFG->additionalhtmltopofbody);
$_GET = ['auth_outage_preview' => (string)$outage->id, 'auth_outage_delta' => '500'];
outagelib::reinject();
outagelib::reset_injectcalled();
$header = outagelib::get_inject_code();
// Still empty, delta is too high (outage ended).
self::assertEmpty($CFG->additionalhtmltopofbody);
self::assertEmpty($header);
}
/**
* Test injection without active outage.
*/
public function test_inject_noactive() {
outagelib::reinject();
outagelib::reset_injectcalled();
outagelib::get_inject_code();
}
/**
@@ -273,15 +277,15 @@ class outagelib_test extends auth_outage_base_testcase {
'description' => 'Description',
]);
$outage->id = outagedb::save($outage);
self::assertEmpty($CFG->additionalhtmltopofbody);
// Pretend we are there...
$_SERVER['SCRIPT_FILENAME'] = '/var/www/alternativepath/admin/settings.php'; // Issue #88 regression test.
$_SERVER['SCRIPT_NAME'] = '/admin/settings.php';
$_GET['section'] = 'additionalhtml';
outagelib::reinject();
outagelib::reset_injectcalled();
$header = outagelib::get_inject_code();
self::assertEmpty($CFG->additionalhtmltopofbody);
self::assertEmpty($header);
}
public function test_createmaintenancephpcode() {
@@ -499,15 +503,15 @@ EOT;
'description' => 'Description',
]);
$outage->id = outagedb::save($outage);
self::assertEmpty($CFG->additionalhtmltopofbody);
// Pretend we are there...
$_SERVER['SCRIPT_FILENAME'] = '/var/www/alternativepath/admin/settings.php'; // Issue #88 regression test.
$_SERVER['SCRIPT_NAME'] = '/admin/settings.php';
$_GET['section'] = 'notadditionalhtml';
outagelib::reinject();
outagelib::reset_injectcalled();
self::assertNotEmpty($CFG->additionalhtmltopofbody);
$header = outagelib::get_inject_code();
self::assertNotEmpty($header);
}
private function create_outage() {