Skip to content

Commit

Permalink
MDL-67657 behat: Open course with editing using URL
Browse files Browse the repository at this point in the history
This is a huge performance improvement for behat.

The current set of steps loads the page
It then looks for the Classic/Clean version of the settings menu
If it fails to find it looks for the Boost Cog
Then it clicks the "Turn editing on" button

This can take a substantial period.

We do not actually need to run these steps as we are able to jump
straight to the URL. We already have access to the sesskey value
required to do this.

There is not loss in testing functionality because the actual testing of
the Turn editing functionality is tested in other places sufficiently.
  • Loading branch information
andrewnicols committed Jul 16, 2020
1 parent 00df049 commit a89aaec
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
18 changes: 18 additions & 0 deletions lib/behat/behat_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -1291,4 +1291,22 @@ public static function execute_script_in_session(Session $session, string $scrip

$session->executeScript($script);
}

/**
* Get the session key for the current session via Javascript.
*
* @return string
*/
public function get_sesskey(): string {
$script = <<<EOF
return (function() {
if (M && M.cfg && M.cfg.sesskey) {
return M.cfg.sesskey;
}
return '';
})()
EOF;

return $this->evaluate_script($script);
}
}
15 changes: 14 additions & 1 deletion lib/tests/behat/behat_navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ public function i_am_on_course_homepage($coursefullname) {
}

/**
* Opens the course homepage with editing mode on.
* Open the course homepage with editing mode enabled.
*
* @Given /^I am on "(?P<coursefullname_string>(?:[^"]|\\")*)" course homepage with editing mode on$/
* @throws coding_exception
Expand All @@ -766,9 +766,22 @@ public function i_am_on_course_homepage($coursefullname) {
*/
public function i_am_on_course_homepage_with_editing_mode_on($coursefullname) {
global $DB;

$course = $DB->get_record("course", array("fullname" => $coursefullname), 'id', MUST_EXIST);
$url = new moodle_url('/course/view.php', ['id' => $course->id]);

if ($this->running_javascript() && $sesskey = $this->get_sesskey()) {
// Javascript is running so it is possible to grab the session ket and jump straight to editing mode.
$url->param('edit', 1);
$url->param('sesskey', $sesskey);
$this->getSession()->visit($this->locate_path($url->out_as_local_url(false)));

return;
}

// Visit the course page.
$this->getSession()->visit($this->locate_path($url->out_as_local_url(false)));

try {
$this->execute("behat_forms::press_button", get_string('turneditingon'));
} catch (Exception $e) {
Expand Down

0 comments on commit a89aaec

Please sign in to comment.