From e1b1ef2a61e00d383952536747f613c48609219c Mon Sep 17 00:00:00 2001 From: ferishili Date: Fri, 6 Dec 2024 10:44:42 +0100 Subject: [PATCH] correction and adjustments --- classes/local/maintenance_class.php | 25 ++++++++++++++++---- tests/behat/tool_opencast_maintenace.feature | 1 + 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/classes/local/maintenance_class.php b/classes/local/maintenance_class.php index 7d2ee2a..a670b25 100644 --- a/classes/local/maintenance_class.php +++ b/classes/local/maintenance_class.php @@ -321,14 +321,15 @@ public function can_access(string $method) { public function decide_access_bounce() { global $CFG, $COURSE, $SITE; $wwwroot = $CFG->wwwroot; - if (defined('BEHAT_SITE_RUNNING') && BEHAT_SITE_RUNNING) { - $wwwroot = $CFG->behat_wwwroot; - } $wwwrootparsed = parse_url($wwwroot); $iswebrequest = isset($_SERVER['REMOTE_ADDR']); // It is a web call when the REMOTE_ADDR is set in $_SERVER! + $isbahat = defined('BEHAT_SITE_RUNNING') && BEHAT_SITE_RUNNING; + $iscli = defined('CLI_SCRIPT') && CLI_SCRIPT; + $isphpunit = defined('PHPUNIT_TEST') && PHPUNIT_TEST; + // If it is a web request. - if ($iswebrequest) { + if ($iswebrequest && !$iscli && !$isphpunit) { // We have to carefully decide whether to redirect back to the referer or the course page. // The reason for this check is to avoid redirecting loops! $referer = get_local_referer(false); @@ -339,10 +340,24 @@ public function decide_access_bounce() { $requesttarget = parse_url($initiator); $tagetpath = $requesttarget['path'] ?? ''; + // We handle behat test process right here before going further. + if ($isbahat) { + $behatredirectbackurl = $referer; + if (empty($behatredirectbackurl) && $COURSE && !empty($COURSE->id)) { + $behatredirectbackurl = new \moodle_url('/course/view.php', ['id' => $COURSE->id]); + } + if (!empty($behatredirectbackurl)) { + redirect($behatredirectbackurl); + } else { + throw new \moodle_exception('maintenance_exception_message', self::PLUGINNAME); + } + } + $whitelist = []; $whitelist[] = $wwwrootparsed['path']; $whitelist[] = $wwwrootparsed['path'] . '/course/view.php'; $whitelist[] = $wwwrootparsed['path'] . '/my'; + $whitelist[] = $wwwrootparsed['path'] . '/course'; $blacklist = []; $blacklist['block_opencast'] = $wwwrootparsed['path'] . '/blocks/opencast'; // Match for block_opencast plugin. @@ -385,7 +400,7 @@ public function decide_access_bounce() { close_window(0, true); } - // If it is not yet returned, it has to throw an error. + // If it is not yet returned, it has to throw an error, this should also cover requests coming from unit tests. throw new \moodle_exception('maintenance_exception_message', self::PLUGINNAME); } diff --git a/tests/behat/tool_opencast_maintenace.feature b/tests/behat/tool_opencast_maintenace.feature index 7ab4baa..9e1cf3f 100644 --- a/tests/behat/tool_opencast_maintenace.feature +++ b/tests/behat/tool_opencast_maintenace.feature @@ -99,6 +99,7 @@ Feature: Configure and check maintenance | maintenancemode_startdate_1 | {"enabled":false} | tool_opencast | | maintenancemode_enddate_1 | {"enabled":false} | tool_opencast | When I reload the page + And I wait "2" seconds And I click on "Add video" "button" Then I should see "Opencast Maintenance Notification" in the "#user-notifications" "css_element" And I should not see "Videos available in this course" in the "#region-main" "css_element"