Skip to content

Commit

Permalink
Merge pull request #311 from QihuiChan/logging-maint-mode
Browse files Browse the repository at this point in the history
Add logging for maint mode and fix failed CI tests
  • Loading branch information
brendanheywood authored Dec 21, 2022
2 parents 42d4744 + 89e2621 commit 1627bce
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 9 deletions.
2 changes: 1 addition & 1 deletion classes/form/outage/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function set_data($outage) {

// If the default_autostart is configured in config, then force autostart to be the default value.
if (array_key_exists('auth_outage', $CFG->forced_plugin_settings)
&& array_key_exists('default_autostart', $CFG->forced_plugin_settings['auth_outage'])){
&& array_key_exists('default_autostart', $CFG->forced_plugin_settings['auth_outage'])) {
$this->_form->setDefaults([
'autostart' => $CFG->forced_plugin_settings['auth_outage']['default_autostart']
]);
Expand Down
15 changes: 15 additions & 0 deletions classes/local/outagelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ private static function update_maintenance_later($outage) {
unset_config('maintenance_message');
}
set_config('maintenance_later', $outage->starttime);
self::maintenance_config_log($outage);
}
}

Expand Down Expand Up @@ -414,4 +415,18 @@ public static function generate_plugin_configuration_warning() {

return $message;
}

/**
* Logging for maintenance mode configuration.
*
* @param outage|null $outage Outage or null if no scheduled outage.
*/
private static function maintenance_config_log(outage $outage) {
mtrace(get_string('logformaintmodeconfig', 'auth_outage'));
$timezone = ' (Timezone ' . \core_date::get_server_timezone_object()->getName() . ')';
mtrace('... updated at ' . date('H:i:s'));
$time = date("Y-m-d H:i:s", $outage->starttime);
mtrace("... enable maintenance mode at $time $timezone");
mtrace(get_string('logformaintmodeconfigcomplete', 'auth_outage'));
}
}
2 changes: 2 additions & 0 deletions lang/en/auth_outage.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@
$string['allowedipshasmyip'] = 'Your IP (<i>{$a->ip}</i>) is in the list and you will not be blocked out during an Outage.';
$string['allowedipshasntmyip'] = 'Your IP (<i>{$a->ip}</i>) is not in the list and you will be blocked out during an outage.';
$string['allowedipsnoconfig'] = 'Your config.php does not have the extra setup to allow blocking via IP.<br />Please refer to our <a href="https://github.com/catalyst/moodle-auth_outage#installation" target="_blank">README.md</a> file for more information.';
$string['logformaintmodeconfig'] = 'Update maintenance mode configuration.';
$string['logformaintmodeconfigcomplete'] = 'Updating maintenance mode configuration complete.';
$string['menusettings'] = 'Settings';
$string['menumanage'] = 'Manage outages';
$string['messageoutagebackonline'] = 'We are back online!';
Expand Down
4 changes: 4 additions & 0 deletions tests/phpunit/installation_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ public function test_uninstall() {
'title' => 'Title',
'description' => 'Description',
]);
ob_start();
\auth_outage\dml\outagedb::save($outage);
$text = trim(ob_get_contents());
ob_end_clean();
self::assertStringContainsString('Update maintenance mode configuration', $text);
self::assertSame(1, $DB->count_records_select('event', "eventtype = 'auth_outage'", null));

// Uninstall plugin.
Expand Down
8 changes: 4 additions & 4 deletions tests/phpunit/local/cli/create_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ public function test_create_withoptions() {
$text = $this->execute($cli);
self::assertStringContainsString('created', $text);
// Check creted outage.
list(, $id) = explode(':', $text);
$id = (int)$id;
$clioutput = explode(':', $text);
$id = (int)end($clioutput);
$outage = outagedb::get_by_id($id);
self::assertSame($now, $outage->starttime);
self::assertSame(10, $outage->get_warning_duration());
Expand Down Expand Up @@ -223,8 +223,8 @@ public function test_create_withdefaults() {
$text = $this->execute($cli);
self::assertStringContainsString('created', $text);
// Check creted outage.
list(, $id) = explode(':', $text);
$id = (int)$id;
$clioutput = explode(':', $text);
$id = (int)end($clioutput);
$outage = outagedb::get_by_id($id);
self::assertSame($now + 50, $outage->starttime, 'Wrong starttime.');
self::assertSame($outage->starttime - 100, $outage->warntime, 'Wrong warntime.');
Expand Down
27 changes: 25 additions & 2 deletions tests/phpunit/local/outagelib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ public function test_maintenancemessage() {
]);

set_config('maintenance_message', 'A message.');
ob_start();
outagedb::save($outage);
$text = trim(ob_get_contents());
ob_end_clean();
self::assertStringContainsString('Update maintenance mode configuration', $text);
self::assertFalse((bool)get_config('moodle', 'maintenance_message'));
self::assertCount(2, $this->getDebuggingMessages());
$this->resetDebugging();
Expand Down Expand Up @@ -94,7 +98,11 @@ public function test_inject() {
'title' => 'Title',
'description' => 'Description',
]);
ob_start();
$outage->id = outagedb::save($outage);
$text = trim(ob_get_contents());
ob_end_clean();
self::assertStringContainsString('Update maintenance mode configuration', $text);

outagelib::reset_injectcalled();
// Get full header to avoid interactions with other single inject plugins.
Expand Down Expand Up @@ -136,7 +144,11 @@ public function test_inject_preview() {
'title' => 'Title',
'description' => 'Description',
]);
ob_start();
$outage->id = outagedb::save($outage);
$text = trim(ob_get_contents());
ob_end_clean();
self::assertStringContainsString('Update maintenance mode configuration', $text);

$_GET = ['auth_outage_preview' => (string)$outage->id];

Expand Down Expand Up @@ -175,8 +187,11 @@ public function test_inject_preview_withdelta() {
'title' => 'Title',
'description' => 'Description',
]);
ob_start();
$outage->id = outagedb::save($outage);

$text = trim(ob_get_contents());
ob_end_clean();
self::assertStringContainsString('Update maintenance mode configuration', $text);
$_GET = ['auth_outage_preview' => (string)$outage->id, 'auth_outage_delta' => '500'];
outagelib::reset_injectcalled();
$header = outagelib::get_inject_code();
Expand Down Expand Up @@ -282,7 +297,11 @@ public function test_inject_settings() {
'title' => 'Title',
'description' => 'Description',
]);
ob_start();
$outage->id = outagedb::save($outage);
$text = trim(ob_get_contents());
ob_end_clean();
self::assertStringContainsString('Update maintenance mode configuration', $text);

// Pretend we are there...
$_SERVER['SCRIPT_FILENAME'] = '/var/www/alternativepath/admin/settings.php'; // Issue #88 regression test.
Expand Down Expand Up @@ -341,6 +360,7 @@ public function test_createmaintenancephpcode() {
/**
* Test create maintenance php code without age
*
* @param string $configkey The key of the config.
* @dataProvider test_createmaintenancephpcode_withoutage_provider
*/
public function test_createmaintenancephpcode_withoutage($configkey) {
Expand Down Expand Up @@ -538,8 +558,11 @@ public function test_it_can_inject_in_settings_if_not_additional_html() {
'title' => 'Title',
'description' => 'Description',
]);
ob_start();
$outage->id = outagedb::save($outage);

$text = trim(ob_get_contents());
ob_end_clean();
self::assertStringContainsString('Update maintenance mode configuration', $text);
// Pretend we are there...
$_SERVER['SCRIPT_FILENAME'] = '/var/www/alternativepath/admin/settings.php'; // Issue #88 regression test.
$_SERVER['SCRIPT_NAME'] = '/admin/settings.php';
Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = "auth_outage";
$plugin->version = 2022051200; // The current plugin version (Date: YYYYMMDDXX).
$plugin->release = 2022051200; // Human-readable release information.
$plugin->version = 2022121900; // The current plugin version (Date: YYYYMMDDXX).
$plugin->release = 2022121900; // Human-readable release information.
$plugin->requires = 2017111309; // 2017111309 = T13, but this really requires 3.9 and higher.
$plugin->maturity = MATURITY_STABLE; // Suitable for PRODUCTION environments!
$plugin->supported = [39, 311]; // A range of branch numbers of supported moodle versions.

0 comments on commit 1627bce

Please sign in to comment.