Issue #82 - Recreate templates when settings (allowed ips or blocks removed) is updated.

This commit is contained in:
Daniel Thee Roperto
2016-12-05 11:15:58 +11:00
parent 67600472ec
commit 32f48a2133
4 changed files with 107 additions and 4 deletions

View File

@@ -36,6 +36,8 @@ use stdClass;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/../../lib.php');
/**
* outagelib class.
*

View File

@@ -48,3 +48,11 @@ function auth_outage_extend_navigation_user() {
outagelib::inject();
}
/**
* Used for adminlib::set_updatedcallback which requires a string that resolves to a function.
*
* Related to: MDL-57264 and MDL-32984
*/
function auth_outage_outagelib_prepare_next_outage() {
outagelib::prepare_next_outage();
}

View File

@@ -103,20 +103,24 @@ if ($hassiteconfig && is_enabled_auth('outage')) {
$description .= $OUTPUT->notification(get_string($message, 'auth_outage', ['ip' => getremoteaddr()]), $type);
$description .= '<p>'.get_string('ipblockersyntax', 'admin').'</p>';
$settings->add(new admin_setting_configiplist(
$iplist = new admin_setting_configiplist(
'auth_outage/allowedips',
get_string('allowediplist', 'admin'),
$description,
$defaults['allowedips']
));
);
$iplist->set_updatedcallback('auth_outage_outagelib_prepare_next_outage');
$settings->add($iplist);
// Create 'Static Page - Elements to Remove' settings.
$settings->add(new admin_setting_configtextarea(
$toremove = new admin_setting_configtextarea(
'auth_outage/remove_selectors',
get_string('removeselectors', 'auth_outage'),
get_string('removeselectorsdescription', 'auth_outage'),
$defaults['remove_selectors']
));
);
$toremove->set_updatedcallback('auth_outage_outagelib_prepare_next_outage');
$settings->add($toremove);
// Create category for Outage.
$ADMIN->add('authsettings', new admin_category('auth_outage', get_string('pluginname', 'auth_outage')));

View File

@@ -30,6 +30,9 @@ use auth_outage\local\outagelib;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir.'/adminlib.php');
/**
* outagelib_test test class.
*
@@ -411,6 +414,92 @@ 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() {
global $CFG;
$this->resetAfterTest(true);
self::setAdminUser();
$now = time();
$outage = new outage([
'autostart' => false,
'warntime' => $now - 200,
'starttime' => $now - 100,
'stoptime' => $now + 200,
'title' => 'Title',
'description' => 'Description',
]);
set_config('allowedips', '127.0.0.1', 'auth_outage');
outagedb::save($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) {
self::assertFileExists($file);
unlink($file);
}
// Enable outage plugin so settings can be changed.
set_config('auth', 'outage');
\core\session\manager::gc(); // Remove stale sessions.
core_plugin_manager::reset_caches();
// Change settings.
admin_write_settings((object)[
's_auth_outage_allowedips' => '127',
]);
// 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) {
self::assertFileExists($file);
unlink($file);
}
}
/**
* Problem detected while solving Issue #82.
*/
public function test_when_we_change_remove_selectors_in_settings_it_updates_the_templates() {
global $CFG;
$this->resetAfterTest(true);
self::setAdminUser();
$now = time();
$outage = new outage([
'autostart' => false,
'warntime' => $now - 200,
'starttime' => $now - 100,
'stoptime' => $now + 200,
'title' => 'Title',
'description' => 'Description',
]);
set_config('allowedips', '127.0.0.1', 'auth_outage');
outagedb::save($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) {
self::assertFileExists($file);
unlink($file);
}
// Enable outage plugin so settings can be changed.
set_config('auth', 'outage');
\core\session\manager::gc(); // Remove stale sessions.
core_plugin_manager::reset_caches();
// Change settings.
admin_write_settings((object)[
's_auth_outage_remove_selectors' => '.something',
]);
// 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) {
self::assertFileExists($file);
unlink($file);
}
}
/**
* Related to Issue #72: IP Block still triggers cli maintenance mode even without autostart.
*/