From dcd72611759f87f18e96eed4f82d7d0877ee2468 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 4d6f452aa47..8ea0cac991d 100644 --- a/editmode.php +++ b/editmode.php @@ -28,7 +28,12 @@ $setmode = optional_param('setmode', false, PARAM_BOOL); $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 8f4b805458c..446ed06032f 100644 --- a/lib/tests/behat/behat_navigation.php +++ b/lib/tests/behat/behat_navigation.php @@ -941,22 +941,20 @@ class behat_navigation extends behat_base { * @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]); } /**