From 965d556b6bd3363b0c7e4c1e5c6c00eed66345a4 Mon Sep 17 00:00:00 2001 From: Ferran Recio Date: Fri, 21 Jul 2023 10:17:11 +0200 Subject: [PATCH] MDL-78782 behat: optimize with editing mode step The current I am on course homepage with editing mode loads twice the course page. This patch prevents this from happening by going directly to editmode.php. --- editmode.php | 7 ++++++- lib/tests/behat/behat_navigation.php | 28 +++++++++++++--------------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/editmode.php b/editmode.php index 4d6f452aa4786..8ea0cac991db5 100644 --- a/editmode.php +++ b/editmode.php @@ -28,7 +28,12 @@ $contextid = required_param('context', PARAM_INT); $pageurl = required_param('pageurl', PARAM_LOCALURL); -require_sesskey(); +// Behat requires JS to get the session. To allow goutte +// to enable edit mode sesskey validation is skipped for behat scripts. +if (!defined('BEHAT_SITE_RUNNING')) { + require_sesskey(); +} + require_login(); $context = \context_helper::instance_by_id($contextid); diff --git a/lib/tests/behat/behat_navigation.php b/lib/tests/behat/behat_navigation.php index 17067d9d30083..0d08295267115 100644 --- a/lib/tests/behat/behat_navigation.php +++ b/lib/tests/behat/behat_navigation.php @@ -970,22 +970,20 @@ public function i_am_on_course_homepage_with_editing_mode_on($coursefullname) { * @param string $onoroff Whehter to switch editing on, or off. */ public function i_am_on_course_homepage_with_editing_mode_set_to(string $coursefullname, string $onoroff): void { - $courseid = $this->get_course_id($coursefullname); - $url = new moodle_url('/course/view.php', ['id' => $courseid]); - - // Visit the course page. - $this->execute('behat_general::i_visit', [$url]); - - switch ($onoroff) { - case 'on': - $this->execute('behat_navigation::i_turn_editing_mode_on'); - break; - case 'off': - $this->execute('behat_navigation::i_turn_editing_mode_off'); - break; - default: - throw new \coding_exception("Unknown editing mode '{$onoroff}'. Accepted values are 'on' and 'off'"); + if ($onoroff !== 'on' && $onoroff !== 'off') { + throw new coding_exception("Unknown editing mode '{$onoroff}'. Accepted values are 'on' and 'off'"); } + + $courseid = $this->get_course_id($coursefullname); + $context = context_course::instance($courseid); + $courseurl = new moodle_url('/course/view.php', ['id' => $courseid]); + + $editmodeurl = new moodle_url('/editmode.php', [ + 'context' => $context->id, + 'pageurl' => $courseurl->out(true), + 'setmode' => ($onoroff === 'on' ? 1 : 0), + ]); + $this->execute('behat_general::i_visit', [$editmodeurl]); } /**