Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Enrolment steps definitions.
  19   *
  20   * @package    core_enrol
  21   * @category   test
  22   * @copyright  2013 David MonllaĆ³
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
  27  
  28  require_once (__DIR__ . '/../../../lib/behat/behat_base.php');
  29  
  30  use Behat\Gherkin\Node\TableNode as TableNode;
  31  
  32  /**
  33   * Steps definitions for general enrolment actions.
  34   *
  35   * @package    core_enrol
  36   * @category   test
  37   * @copyright  2013 David MonllaĆ³
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class behat_enrol extends behat_base {
  41  
  42      /**
  43       * Add the specified enrolment method to the specified course filling the form with the provided data.
  44       *
  45       * @Given /^I add "(?P<enrolment_method_name_string>(?:[^"]|\\")*)" enrolment method in "(?P<course_identifier_string>(?:[^"]|\\")*)" with:$/
  46       * @param string $enrolmethod The enrolment method being used
  47       * @param string $courseidentifier The courseidentifier such as short name
  48       * @param TableNode $table Enrolment details
  49       */
  50      public function i_add_enrolment_method_for_with(string $enrolmethod, string $courseidentifier, TableNode $table): void {
  51          $this->execute("behat_navigation::i_am_on_page_instance", [$courseidentifier, 'enrolment methods']);
  52  
  53          // Select enrolment method.
  54          $this->execute('behat_forms::i_select_from_the_singleselect',
  55              array($this->escape($enrolmethod), get_string('addinstance', 'enrol'))
  56          );
  57  
  58          // Wait again, for page to reloaded.
  59          $this->execute('behat_general::i_wait_to_be_redirected');
  60  
  61          // Set form fields.
  62          $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $table);
  63  
  64          // Ensure we get button in focus, before pressing button.
  65          if ($this->running_javascript()) {
  66              $this->execute('behat_general::i_press_named_key', ['', 'tab']);
  67          }
  68  
  69          // Save changes.
  70          $this->execute("behat_forms::press_button", get_string('addinstance', 'enrol'));
  71      }
  72  
  73      /**
  74       * Enrols the specified user in the current course without options.
  75       *
  76       * This is a simple step, to set enrolment options would be better to
  77       * create a separate step as a TableNode will be required.
  78       *
  79       * @Given /^I enrol "(?P<user_fullname_string>(?:[^"]|\\")*)" user as "(?P<rolename_string>(?:[^"]|\\")*)"$/
  80       * @param string $userfullname
  81       * @param string $rolename
  82       */
  83      public function i_enrol_user_as($userfullname, $rolename) {
  84  
  85          // Navigate to enrolment page.
  86          try {
  87              $parentnodes = get_string('users', 'admin');
  88              $this->execute("behat_navigation::i_navigate_to_in_current_page_administration",
  89                  array($parentnodes . ' > '. get_string('enrolledusers', 'enrol'))
  90              );
  91          } catch (Exception $e) {
  92              $this->execute("behat_general::i_click_on", [get_string('participants'), 'link']);
  93          }
  94  
  95          $this->execute("behat_forms::press_button", get_string('enrolusers', 'enrol'));
  96  
  97          if ($this->running_javascript()) {
  98              $this->execute('behat_forms::i_set_the_field_to', array(get_string('assignrole', 'enrol_manual'), $rolename));
  99  
 100              // We have a div here, not a tr.
 101              $this->execute('behat_forms::i_set_the_field_to', array(get_string('selectusers', 'enrol_manual'), $userfullname));
 102  
 103              $enrolusers = get_string('enrolusers', 'enrol_manual');
 104              $this->execute('behat_general::i_click_on_in_the', [$enrolusers, 'button', $enrolusers, 'dialogue']);
 105  
 106          } else {
 107              $this->execute('behat_forms::i_set_the_field_to', array(get_string('assignrole', 'role'), $rolename));
 108              $this->execute('behat_forms::i_set_the_field_to', array("addselect", $userfullname));
 109              $this->execute("behat_forms::press_button", "add");
 110          }
 111      }
 112  
 113  }