-
Improvement
-
Resolution: Fixed
-
Minor
-
3.1.4, 3.2.1
-
MOODLE_31_STABLE, MOODLE_32_STABLE
-
MOODLE_31_STABLE, MOODLE_32_STABLE
-
MDL-58052_behat_get_session_user -
Recently I wrote a custom step for sending a test message
public function i_send_message_to_user($messagecontent, $userfullname) {
|
$fromuser = $this->get_behat_user();
|
What I really didn't want to have to do is put the username of the 'from' person in the custom step arguments as this is an 'I send message' step so the person it's from is inferred to be the user in the current behat session.
The solution was to create a function for recovering the user from the current behat session:
/**
|
* Get the actual behat user (note $USER does not correspond to the behat sessions user).
|
* @return mixed
|
* @throws coding_exception
|
*/
|
protected function get_behat_user() {
|
global $DB;
|
|
$sid = $this->getSession()->getCookie('MoodleSession');
|
if (empty($sid)) {
|
throw new coding_exception('failed to get moodle session');
|
}
|
$userid = $DB->get_field('sessions', 'userid', ['sid' => $sid]);
|
if (empty($userid)) {
|
throw new coding_exception('failed to get user from seession id '.$sid);
|
}
|
return $DB->get_record('user', ['id' => $userid]);
|
}
|
Does this sound like something that could be useful to core?