Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-58052

Behat - get the session user within a custom step

XMLWordPrintable

    • MOODLE_31_STABLE, MOODLE_32_STABLE
    • MOODLE_31_STABLE, MOODLE_32_STABLE
    • MDL-58052_behat_get_session_user
    • Hide

      Make sure the following behat tests pass:

      admin/tool/behat/tests/behat/manipulate_forms.feature
      admin/tool/behat/tests/behat/nasty_strings.feature
      badges/tests/behat/award_badge.feature
      course/tests/behat/navigate_course_list.feature
      lib/editor/atto/plugins/accessibilitychecker/tests/behat/accessibilitychecker.feature
      lib/editor/atto/plugins/accessibilityhelper/tests/behat/accessibilityhelper.feature
      lib/editor/atto/plugins/align/tests/behat/align.feature
      lib/editor/atto/plugins/bold/tests/behat/bold.feature
      lib/editor/atto/plugins/charmap/tests/behat/charmap.feature
      lib/editor/atto/plugins/clear/tests/behat/clear.feature
      lib/editor/atto/plugins/collapse/tests/behat/collapse.feature
      lib/editor/atto/plugins/equation/tests/behat/equation.feature
      lib/editor/atto/plugins/html/tests/behat/html.feature
      lib/editor/atto/plugins/image/tests/behat/image.feature
      lib/editor/atto/plugins/indent/tests/behat/indent.feature
      lib/editor/atto/plugins/italic/tests/behat/italic.feature
      lib/editor/atto/plugins/link/tests/behat/link.feature
      lib/editor/atto/plugins/orderedlist/tests/behat/orderedlist.feature
      lib/editor/atto/plugins/strike/tests/behat/strike.feature
      lib/editor/atto/plugins/subscript/tests/behat/subscript.feature
      lib/editor/atto/plugins/superscript/tests/behat/superscript.feature
      lib/editor/atto/plugins/title/tests/behat/title.feature
      lib/editor/atto/plugins/underline/tests/behat/underline.feature
      lib/editor/atto/plugins/unorderedlist/tests/behat/unorderedlist.feature
      tag/tests/behat/collections.feature
      tag/tests/behat/standard_tags.feature
      user/tests/behat/name_fields.feature

      Show
      Make sure the following behat tests pass: admin/tool/behat/tests/behat/manipulate_forms.feature admin/tool/behat/tests/behat/nasty_strings.feature badges/tests/behat/award_badge.feature course/tests/behat/navigate_course_list.feature lib/editor/atto/plugins/accessibilitychecker/tests/behat/accessibilitychecker.feature lib/editor/atto/plugins/accessibilityhelper/tests/behat/accessibilityhelper.feature lib/editor/atto/plugins/align/tests/behat/align.feature lib/editor/atto/plugins/bold/tests/behat/bold.feature lib/editor/atto/plugins/charmap/tests/behat/charmap.feature lib/editor/atto/plugins/clear/tests/behat/clear.feature lib/editor/atto/plugins/collapse/tests/behat/collapse.feature lib/editor/atto/plugins/equation/tests/behat/equation.feature lib/editor/atto/plugins/html/tests/behat/html.feature lib/editor/atto/plugins/image/tests/behat/image.feature lib/editor/atto/plugins/indent/tests/behat/indent.feature lib/editor/atto/plugins/italic/tests/behat/italic.feature lib/editor/atto/plugins/link/tests/behat/link.feature lib/editor/atto/plugins/orderedlist/tests/behat/orderedlist.feature lib/editor/atto/plugins/strike/tests/behat/strike.feature lib/editor/atto/plugins/subscript/tests/behat/subscript.feature lib/editor/atto/plugins/superscript/tests/behat/superscript.feature lib/editor/atto/plugins/title/tests/behat/title.feature lib/editor/atto/plugins/underline/tests/behat/underline.feature lib/editor/atto/plugins/unorderedlist/tests/behat/unorderedlist.feature tag/tests/behat/collections.feature tag/tests/behat/standard_tags.feature user/tests/behat/name_fields.feature

      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?

            brudinie guy thomas
            brudinie guy thomas
            Dan Poltawski Dan Poltawski
            CiBoT CiBoT
            Votes:
            6 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:

                Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.