diff --git a/classes/local/controllers/maintenance_static_page_io.php b/classes/local/controllers/maintenance_static_page_io.php index 3dcc72f..3639c14 100644 --- a/classes/local/controllers/maintenance_static_page_io.php +++ b/classes/local/controllers/maintenance_static_page_io.php @@ -25,6 +25,7 @@ namespace auth_outage\local\controllers; +use auth_outage\local\outagelib; use coding_exception; use finfo; use invalid_parameter_exception; @@ -62,24 +63,18 @@ class maintenance_static_page_io { } if (self::is_url($file)) { - $curl = curl_init(); - curl_setopt($curl, CURLOPT_URL, $file); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); - curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); - curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); - $contents = curl_exec($curl); - $mime = curl_getinfo($curl, CURLINFO_CONTENT_TYPE); - curl_close($curl); + $result = outagelib::fetch_page($file); } else { - $contents = @file_get_contents($file); - $mime = (new finfo(FILEINFO_MIME_TYPE))->buffer($contents); // Not perfect, but try guessing it. + $result = ['contents' => @file_get_contents($file)]; + $result['mime'] = (new finfo(FILEINFO_MIME_TYPE)) + ->buffer($result['contents']); // Not perfect, but try guessing it. } - if ($contents === false) { + + if ($result['contents'] === false) { debugging('Cannot fetch: '.$file); - $contents = ''; - $mime = 'unknown'; + $result = ['contents' => '', 'mime' => 'unknown']; } - return ['contents' => $contents, 'mime' => $mime]; + return $result; } /** @var bool */ diff --git a/classes/local/outagelib.php b/classes/local/outagelib.php index 5dcf77d..dbea92f 100644 --- a/classes/local/outagelib.php +++ b/classes/local/outagelib.php @@ -52,6 +52,18 @@ class outagelib { */ private static $injectcalled = false; + public static function fetch_page($file) { + $curl = curl_init(); + curl_setopt($curl, CURLOPT_URL, $file); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); + $contents = curl_exec($curl); + $mime = curl_getinfo($curl, CURLINFO_CONTENT_TYPE); + curl_close($curl); + return compact('contents', 'mime'); + } + /** * Calls inject even if it was already called before. */ @@ -154,9 +166,14 @@ class outagelib { self::update_maintenance_later($outage); } + private static function check_wwwroot_accessible() { + global $CFG; + $result = self::fetch_page($CFG->wwwroot); + return (!empty($result['contents'])); + } + /** * Calls Moodle API - set_maintenance_later() to set when the next outage starts. - * * @param outage|null $outage Outage or null if no scheduled outage. */ private static function update_maintenance_later($outage) { @@ -316,6 +333,10 @@ EOT; $message[] = get_string('configurationdisabled', 'auth_outage'); } + if (!self::check_wwwroot_accessible()) { + $message[] = get_string('configurationinaccessiblewwwroot', 'auth_outage', ['wwwroot' => $CFG->wwwroot]); + } + if (count($message) == 0) { return ''; } diff --git a/lang/en/auth_outage.php b/lang/en/auth_outage.php index b6ca9da..7cd3d5e 100644 --- a/lang/en/auth_outage.php +++ b/lang/en/auth_outage.php @@ -61,6 +61,7 @@ $string['clioutagecreated'] = 'Outage created, id: {$a->id}'; $string['clone'] = 'Clone'; $string['configurationwarning'] = 'The outage plugin is not properly configured, please refer to README.md for more information.'; $string['configurationdisabled'] = 'The authentication plugin \'Outage\' is disabled. Please enable it in the site administration it and try again.'; +$string['configurationinaccessiblewwwroot'] = 'Could not access {$a->wwwroot} from the server, creation of outages may fail.'; $string['datetimeformat'] = '%a %d %h %Y at %I:%M%P %Z'; $string['defaultlayoutcss'] = 'Layout CSS'; $string['defaultlayoutcssdescription'] = 'This CSS code can be used to override the Outage Warning Bar CSS.'; diff --git a/tests/phpunit/form/forms_test.php b/tests/phpunit/form/forms_test.php index 8e33fb7..c7d2b2e 100644 --- a/tests/phpunit/form/forms_test.php +++ b/tests/phpunit/form/forms_test.php @@ -58,7 +58,7 @@ class forms_test extends auth_outage_base_testcase { * Mock some data and check values. */ public function test_edit_valid() { - if ($this->skip_because_moodle_is_below_30()) { + if ($this->skip_because_moodle_is_below_30('Moodle POST mocking was fixed in Moodle 30.')) { return; } @@ -79,7 +79,7 @@ class forms_test extends auth_outage_base_testcase { * Check invalid warning duration. */ public function test_edit_invalid_warning() { - if ($this->skip_because_moodle_is_below_30()) { + if ($this->skip_because_moodle_is_below_30('Moodle POST mocking was fixed in Moodle 30.')) { return; } @@ -94,7 +94,7 @@ class forms_test extends auth_outage_base_testcase { * Check invalid outage duration. */ public function test_edit_invalid_duration() { - if ($this->skip_because_moodle_is_below_30()) { + if ($this->skip_because_moodle_is_below_30('Moodle POST mocking was fixed in Moodle 30.')) { return; } @@ -108,7 +108,7 @@ class forms_test extends auth_outage_base_testcase { * Check invalid title (empty). */ public function test_edit_invalid_title() { - if ($this->skip_because_moodle_is_below_30()) { + if ($this->skip_because_moodle_is_below_30('Moodle POST mocking was fixed in Moodle 30.')) { return; } @@ -122,7 +122,7 @@ class forms_test extends auth_outage_base_testcase { * Check invalid title (too long). */ public function test_edit_invalid_title_toolong() { - if ($this->skip_because_moodle_is_below_30()) { + if ($this->skip_because_moodle_is_below_30('Moodle POST mocking was fixed in Moodle 30.')) { return; } @@ -138,7 +138,7 @@ class forms_test extends auth_outage_base_testcase { * Check invalid format for description. */ public function test_edit_description_invalid_format() { - if ($this->skip_because_moodle_is_below_30()) { + if ($this->skip_because_moodle_is_below_30('Moodle POST mocking was fixed in Moodle 30.')) { return; } @@ -198,13 +198,13 @@ class forms_test extends auth_outage_base_testcase { ]; } - private function skip_because_moodle_is_below_30() { + private function skip_because_moodle_is_below_30($reason = '') { global $CFG; // The bugfix MDL-56250 in only applies to Moodle 30+. // Before that the form validation test is meaningless (results are cached), so skip it. if ($CFG->branch < 30) { - $this->markTestSkipped(); + $this->markTestSkipped('Some tests can only run in Moodle 30+. '.$reason); return true; }