Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Basic authentication steps definitions.
  20   *
  21   * @package    core_auth
  22   * @category   test
  23   * @copyright  2012 David MonllaĆ³
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
  28  
  29  require_once (__DIR__ . '/../../../lib/behat/behat_base.php');
  30  
  31  /**
  32   * Log in log out steps definitions.
  33   *
  34   * @package    core_auth
  35   * @category   test
  36   * @copyright  2012 David MonllaĆ³
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class behat_auth extends behat_base {
  40  
  41      /**
  42       * Logs in the user. There should exist a user with the same value as username and password.
  43       *
  44       * @Given /^I log in as "(?P<username_string>(?:[^"]|\\")*)"$/
  45       * @Given I am logged in as :username
  46       * @param string $username the user to log in as.
  47       * @param moodle_url|null $wantsurl optional, URL to go to after logging in.
  48       */
  49      public function i_log_in_as(string $username, moodle_url $wantsurl = null) {
  50          // In the mobile app the required tasks are different (does not support $wantsurl).
  51          if ($this->is_in_app()) {
  52              $this->execute('behat_app::login', [$username]);
  53              return;
  54          }
  55  
  56          $loginurl = new moodle_url('/auth/tests/behat/login.php', [
  57              'username' => $username,
  58          ]);
  59          if ($wantsurl !== null) {
  60              $loginurl->param('wantsurl', $wantsurl->out_as_local_url());
  61          }
  62  
  63          // Visit login page.
  64          $this->execute('behat_general::i_visit', [$loginurl]);
  65      }
  66  
  67      /**
  68       * Logs out of the system.
  69       *
  70       * @Given /^I log out$/
  71       * @Given I am not logged in
  72       */
  73      public function i_log_out() {
  74          $this->execute('behat_general::i_visit', [new moodle_url('/auth/tests/behat/logout.php')]);
  75      }
  76  }