diff --git a/classes/local/outagelib.php b/classes/local/outagelib.php index 408743c..26737bb 100644 --- a/classes/local/outagelib.php +++ b/classes/local/outagelib.php @@ -36,6 +36,8 @@ use stdClass; defined('MOODLE_INTERNAL') || die(); +require_once(__DIR__.'/../../lib.php'); + /** * outagelib class. * diff --git a/lib.php b/lib.php index 21f5e46..142befc 100644 --- a/lib.php +++ b/lib.php @@ -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(); +} diff --git a/settings.php b/settings.php index 2261fbd..b15e775 100644 --- a/settings.php +++ b/settings.php @@ -103,20 +103,24 @@ if ($hassiteconfig && is_enabled_auth('outage')) { $description .= $OUTPUT->notification(get_string($message, 'auth_outage', ['ip' => getremoteaddr()]), $type); $description .= '
'.get_string('ipblockersyntax', 'admin').'
'; - $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'))); diff --git a/tests/phpunit/local/outagelib_test.php b/tests/phpunit/local/outagelib_test.php index 544599a..ab6101c 100644 --- a/tests/phpunit/local/outagelib_test.php +++ b/tests/phpunit/local/outagelib_test.php @@ -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. */