From b9cbf015acf316bb80b112cb89aef4c8e0411c9c Mon Sep 17 00:00:00 2001 From: Qihui Chan Date: Tue, 20 Dec 2022 09:48:36 +1000 Subject: [PATCH 1/3] Add logging for maint mode config --- classes/local/outagelib.php | 18 ++++++++++++++++++ lang/en/auth_outage.php | 2 ++ version.php | 4 ++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/classes/local/outagelib.php b/classes/local/outagelib.php index 7d5b340..42be2f5 100644 --- a/classes/local/outagelib.php +++ b/classes/local/outagelib.php @@ -246,6 +246,7 @@ class outagelib { unset_config('maintenance_message'); } set_config('maintenance_later', $outage->starttime); + self::maintenance_config_log($outage); } } @@ -414,4 +415,21 @@ EOT; 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) { + // Not output logging info in php unit test. + if (PHPUNIT_TEST) return; + + 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')); + } } diff --git a/lang/en/auth_outage.php b/lang/en/auth_outage.php index 0324a19..80ca4a3 100644 --- a/lang/en/auth_outage.php +++ b/lang/en/auth_outage.php @@ -107,6 +107,8 @@ $string['allowedipsempty'] = 'When the allowed IPs list is empty we will not blo $string['allowedipshasmyip'] = 'Your IP ({$a->ip}) is in the list and you will not be blocked out during an Outage.'; $string['allowedipshasntmyip'] = 'Your IP ({$a->ip}) 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.
Please refer to our README.md 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!'; diff --git a/version.php b/version.php index 3482760..dd71866 100644 --- a/version.php +++ b/version.php @@ -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. From 9473db8c6e00ea5c1a6e2f75d4aeb62ef22a45ac Mon Sep 17 00:00:00 2001 From: Qihui Chan Date: Tue, 20 Dec 2022 10:26:11 +1000 Subject: [PATCH 2/3] Modify unit tests to declare expected output --- classes/local/outagelib.php | 3 --- tests/phpunit/installation_test.php | 4 ++++ tests/phpunit/local/cli/create_test.php | 8 ++++---- tests/phpunit/local/outagelib_test.php | 26 +++++++++++++++++++++++-- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/classes/local/outagelib.php b/classes/local/outagelib.php index 42be2f5..9b532b2 100644 --- a/classes/local/outagelib.php +++ b/classes/local/outagelib.php @@ -422,9 +422,6 @@ EOT; * @param outage|null $outage Outage or null if no scheduled outage. */ private static function maintenance_config_log(outage $outage) { - // Not output logging info in php unit test. - if (PHPUNIT_TEST) return; - mtrace(get_string('logformaintmodeconfig', 'auth_outage')); $timezone = ' (Timezone ' . \core_date::get_server_timezone_object()->getName() . ')'; mtrace('... updated at ' . date('H:i:s')); diff --git a/tests/phpunit/installation_test.php b/tests/phpunit/installation_test.php index 1c5936b..bd54723 100644 --- a/tests/phpunit/installation_test.php +++ b/tests/phpunit/installation_test.php @@ -63,7 +63,11 @@ class auth_outage_installation_test extends auth_outage_base_testcase { '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. diff --git a/tests/phpunit/local/cli/create_test.php b/tests/phpunit/local/cli/create_test.php index b293c97..61e9163 100644 --- a/tests/phpunit/local/cli/create_test.php +++ b/tests/phpunit/local/cli/create_test.php @@ -159,8 +159,8 @@ class auth_outage_create_test extends auth_outage_cli_testcase { $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()); @@ -223,8 +223,8 @@ class auth_outage_create_test extends auth_outage_cli_testcase { $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.'); diff --git a/tests/phpunit/local/outagelib_test.php b/tests/phpunit/local/outagelib_test.php index 942fa1b..9fae62d 100644 --- a/tests/phpunit/local/outagelib_test.php +++ b/tests/phpunit/local/outagelib_test.php @@ -60,7 +60,11 @@ class auth_outage_outagelib_test extends auth_outage_base_testcase { ]); 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(); @@ -94,7 +98,11 @@ class auth_outage_outagelib_test extends auth_outage_base_testcase { '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. @@ -136,7 +144,11 @@ class auth_outage_outagelib_test extends auth_outage_base_testcase { '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]; @@ -175,8 +187,11 @@ class auth_outage_outagelib_test extends auth_outage_base_testcase { '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(); @@ -282,7 +297,11 @@ class auth_outage_outagelib_test extends auth_outage_base_testcase { '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. @@ -538,8 +557,11 @@ EOT; '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'; From 89e2621bf6b75ebe80ff6712256987069f476fad Mon Sep 17 00:00:00 2001 From: Qihui Chan Date: Tue, 20 Dec 2022 16:35:56 +1000 Subject: [PATCH 3/3] Fix failed Github CI tests --- classes/form/outage/edit.php | 2 +- tests/phpunit/local/outagelib_test.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/classes/form/outage/edit.php b/classes/form/outage/edit.php index 10b44e2..78f7108 100644 --- a/classes/form/outage/edit.php +++ b/classes/form/outage/edit.php @@ -164,7 +164,7 @@ class edit extends moodleform { // 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'] ]); diff --git a/tests/phpunit/local/outagelib_test.php b/tests/phpunit/local/outagelib_test.php index 9fae62d..9845386 100644 --- a/tests/phpunit/local/outagelib_test.php +++ b/tests/phpunit/local/outagelib_test.php @@ -360,6 +360,7 @@ EOT; /** * 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) {