Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [Versions 402 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   * Behat grade related steps definitions.
  19   *
  20   * @package    core_grades
  21   * @category   test
  22   * @copyright  2014 Mark Nelson <markn@moodle.com>
  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  class behat_grade extends behat_base {
  33  
  34      /**
  35       * Enters a grade via the gradebook for a specific grade item and user when viewing the 'Grader report' with editing mode turned on.
  36       *
  37       * @Given /^I give the grade "(?P<grade_number>(?:[^"]|\\")*)" to the user "(?P<username_string>(?:[^"]|\\")*)" for the grade item "(?P<grade_activity_string>(?:[^"]|\\")*)"$/
  38       * @param int $grade
  39       * @param string $userfullname the user's fullname as returned by fullname()
  40       * @param string $itemname
  41       */
  42      public function i_give_the_grade($grade, $userfullname, $itemname) {
  43          $gradelabel = $userfullname . ' ' . $itemname;
  44          $fieldstr = get_string('useractivitygrade', 'gradereport_grader', $gradelabel);
  45  
  46          $this->execute('behat_forms::i_set_the_field_to', array($this->escape($fieldstr), $grade));
  47      }
  48  
  49      /**
  50       * Changes the settings of a grade item or category or the course.
  51       *
  52       * Teacher must be either on the grade setup page or on the Grader report page with editing mode turned on.
  53       *
  54       * @Given /^I set the following settings for grade item "(?P<grade_item_string>(?:[^"]|\\")*)" of type "([^"]*)" on "([^"]*)" page:$/
  55       * @param string $gradeitem
  56       * @param string $type
  57       * @param string $page
  58       * @param TableNode $data
  59       */
  60      public function i_set_the_following_settings_for_grade_item(string $gradeitem, string $type, string $page, TableNode $data) {
  61  
  62          if ($this->running_javascript()) {
  63              $this->execute("behat_grades::i_click_on_grade_item_menu", [$gradeitem, $type, $page]);
  64          }
  65  
  66          if ($type == 'gradeitem') {
  67              $linktext = get_string('itemsedit', 'grades');
  68          } else if ($type == 'category') {
  69              $linktext = get_string('categoryedit', 'grades');
  70          } else {
  71              $linktext = get_string('categoryedit', 'grades');
  72          }
  73          $this->execute("behat_action_menu::i_choose_in_the_open_action_menu", $linktext);
  74  
  75          $savechanges = get_string('savechanges', 'grades');
  76          $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $data);
  77          $this->execute('behat_forms::press_button', $this->escape($savechanges));
  78      }
  79  
  80      /**
  81       * Hids a grade item or category on gradebook setup or grader page.
  82       *
  83       * Teacher must be on the grade setup page.
  84       *
  85       * @Given /^I hide the grade item "(?P<grade_item_string>(?:[^"]|\\")*)" of type "([^"]*)" on "([^"]*)" page$/
  86       * @param string $gradeitem
  87       * @param string $type
  88       * @param string $page
  89       */
  90      public function i_hide_the_grade_item(string $gradeitem, string $type, string $page) {
  91  
  92          $linktext = get_string('hide');
  93  
  94          if ($this->running_javascript()) {
  95              $this->execute("behat_grades::i_click_on_grade_item_menu", [$gradeitem, $type, $page]);
  96          }
  97  
  98          $this->execute("behat_action_menu::i_choose_in_the_open_action_menu", $linktext);
  99      }
 100  
 101      /**
 102       * Duplicates a grade item or category.
 103       *
 104       * Teacher must be on the grade setup page.
 105       *
 106       * @Given /^I duplicate the grade item "(?P<grade_item_string>(?:[^"]|\\")*)"$/
 107       * @param string $gradeitem
 108       */
 109      public function i_duplicate_the_grade_item(string $gradeitem) {
 110          $linktext = get_string('duplicate');
 111  
 112          if ($this->running_javascript()) {
 113              $this->execute("behat_grades::i_click_on_grade_item_menu", [$gradeitem, 'gradeitem', 'setup']);
 114          }
 115  
 116          $this->execute("behat_action_menu::i_choose_in_the_open_action_menu", $linktext);
 117      }
 118  
 119      /**
 120       * Sets a calculated manual grade item. Needs a table with item name - idnumber relation.
 121       * The step requires you to be in the 'Gradebook setup' page.
 122       *
 123       * @Given /^I set "(?P<calculation_string>(?:[^"]|\\")*)" calculation for grade item "(?P<grade_item_string>(?:[^"]|\\")*)" with idnumbers:$/
 124       * @param string $calculation The calculation.
 125       * @param string $gradeitem The grade item name.
 126       * @param TableNode $TableNode The grade item name - idnumbers relation.
 127       */
 128      public function i_set_calculation_for_grade_item_with_idnumbers($calculation, $gradeitem, TableNode $data) {
 129  
 130          $edit = get_string('editcalculation', 'grades');
 131  
 132          if ($this->running_javascript()) {
 133              $this->execute("behat_grades::i_click_on_grade_item_menu", [$gradeitem, 'gradeitem', 'setup']);
 134          }
 135          $this->execute("behat_action_menu::i_choose_in_the_open_action_menu", $edit);
 136  
 137          $savechanges = get_string('savechanges', 'grades');
 138  
 139          // Mapping names to idnumbers.
 140          $datahash = $data->getRowsHash();
 141          foreach ($datahash as $gradeitem => $idnumber) {
 142              // This xpath looks for course, categories and items with the provided name.
 143              // Grrr, we can't equal in categoryitem and courseitem because there is a line jump...
 144              $inputxpath = "//input[@class='idnumber'][" .
 145                      "parent::li[@class='item'][text()='" . $gradeitem . "']" .
 146                      " or " .
 147                      "parent::li[@class='categoryitem' or @class='courseitem']" .
 148                      "/parent::ul/parent::li[starts-with(text(),'" . $gradeitem . "')]" .
 149                      "]";
 150              $this->execute('behat_forms::i_set_the_field_with_xpath_to', [$inputxpath, $idnumber]);
 151          }
 152  
 153          $this->execute('behat_forms::press_button', get_string('addidnumbers', 'grades'));
 154          $this->execute('behat_forms::i_set_the_field_to', [get_string('calculation', 'grades'), $calculation]);
 155          $this->execute('behat_forms::press_button', $savechanges);
 156  
 157      }
 158  
 159      /**
 160       * Sets a calculated manual grade category total. Needs a table with item name - idnumber relation.
 161       * The step requires you to be in the 'Gradebook setup' page.
 162       *
 163       * @Given /^I set "(?P<calculation_string>(?:[^"]|\\")*)" calculation for grade category "(?P<grade_item_string>(?:[^"]|\\")*)" with idnumbers:$/
 164       * @param string $calculation The calculation.
 165       * @param string $gradeitem The grade item name.
 166       * @param TableNode $data The grade item name - idnumbers relation.
 167       */
 168      public function i_set_calculation_for_grade_category_with_idnumbers(string $calculation, string $gradeitem, TableNode $data) {
 169  
 170          $edit = get_string('editcalculation', 'grades');
 171  
 172          if ($this->running_javascript()) {
 173              $this->execute("behat_grades::i_click_on_grade_item_menu", [$gradeitem, 'gradeitem', 'setup']);
 174          }
 175          $this->execute("behat_action_menu::i_choose_in_the_open_action_menu", $edit);
 176  
 177          $savechanges = get_string('savechanges', 'grades');
 178  
 179          // Mapping names to idnumbers.
 180          $datahash = $data->getRowsHash();
 181          foreach ($datahash as $gradeitem => $idnumber) {
 182              // This xpath looks for course, categories and items with the provided name.
 183              // Grrr, we can't equal in categoryitem and courseitem because there is a line jump...
 184              $inputxpath = "//input[@class='idnumber'][" .
 185                      "parent::li[@class='item'][text()='" . $gradeitem . "']" .
 186                      " | " .
 187                      "parent::li[@class='categoryitem' or @class='courseitem']" .
 188                      "/parent::ul/parent::li[starts-with(text(),'" . $gradeitem . "')]" .
 189                      "]";
 190              $this->execute('behat_forms::i_set_the_field_with_xpath_to', array($inputxpath, $idnumber));
 191          }
 192  
 193          $this->execute('behat_forms::press_button', get_string('addidnumbers', 'grades'));
 194  
 195          $this->execute('behat_forms::i_set_the_field_to', [get_string('calculation', 'grades'), $calculation]);
 196          $this->execute('behat_forms::press_button', $savechanges);
 197      }
 198  
 199      /**
 200       * Resets the weights for the grade category
 201       *
 202       * Teacher must be on the grade setup page.
 203       *
 204       * @Given /^I reset weights for grade category "(?P<grade_item_string>(?:[^"]|\\")*)"$/
 205       * @param string $gradeitem
 206       */
 207      public function i_reset_weights_for_grade_category(string $gradeitem) {
 208  
 209          if ($this->running_javascript()) {
 210              $this->execute("behat_grades::i_click_on_grade_item_menu", [$gradeitem, 'category', 'setup']);
 211          }
 212          $linktext = get_string('resetweightsshort', 'grades');
 213          $this->execute("behat_action_menu::i_choose_in_the_open_action_menu", [$this->escape($linktext), "link"]);
 214      }
 215  
 216      /**
 217       * Step allowing to test before-the-fix behaviour of the gradebook
 218       *
 219       * @Given /^gradebook calculations for the course "(?P<coursename_string>(?:[^"]|\\")*)" are frozen at version "(?P<version_string>(?:[^"]|\\")*)"$/
 220       * @param string $coursename
 221       * @param string $version
 222       */
 223      public function gradebook_calculations_for_the_course_are_frozen_at_version($coursename, $version) {
 224          global $DB;
 225          $courseid = $DB->get_field('course', 'id', array('shortname' => $coursename), MUST_EXIST);
 226          set_config('gradebook_calculations_freeze_' . $courseid, $version);
 227      }
 228  
 229      /**
 230       * Select the tab in the gradebook. We must be on one of the gradebook pages already.
 231       *
 232       * @deprecated since 4.0 - use behat_forms::i_set_the_field_to() instead.
 233       * @param string $gradepath examples: "View > User report", "Letters > View", "Scales"
 234       */
 235      protected function select_in_gradebook_tabs($gradepath) {
 236          debugging('The function select_in_gradebook_tabs() is deprecated, please use ' .
 237              'behat_forms::i_set_the_field_to() instead.', DEBUG_DEVELOPER);
 238  
 239          $gradepath = preg_split('/\s*>\s*/', trim($gradepath));
 240          if (count($gradepath) > 2) {
 241              throw new coding_exception('Grade path is too long (must have no more than two items separated with ">")');
 242          }
 243  
 244          $xpath = '//div[contains(@class,\'grade-navigation\')]';
 245  
 246          // If the first row of the grade-navigation tabs does not have $gradepath[0] as active tab, click on it.
 247          $link = '\'' . $this->escape($gradepath[0]) . '\'';
 248          $xpathrow1 = $xpath . '//ul[1]//*[contains(@class,\'active\') and contains(normalize-space(.), ' . $link . ')]';
 249          if (!$this->getSession()->getPage()->findAll('xpath', $xpathrow1)) {
 250              $this->find('xpath', $xpath . '//ul[1]/li/a[text()=' . $link . ']')->click();
 251              $this->wait_for_pending_js();
 252          }
 253  
 254          if (isset($gradepath[1])) {
 255              // If the second row of the grade-navigation tabs does not have $gradepath[1] as active tab, click on it.
 256              $link = '\'' . $this->escape($gradepath[1]) . '\'';
 257              $xpathrow2 = $xpath . '//ul[2]//*[contains(@class,\'active\') and contains(normalize-space(.), ' . $link . ')]';
 258              if (!$this->getSession()->getPage()->findAll('xpath', $xpathrow2)) {
 259                  $this->find('xpath', $xpath . '//ul[2]/li/a[text()=' . $link . ']')->click();
 260                  $this->wait_for_pending_js();
 261              }
 262          }
 263      }
 264  
 265      /**
 266       * Navigates to the course gradebook and selects the specified item from the general grade navigation selector.
 267       *
 268       * Examples:
 269       * - I navigate to "Setup > Gradebook setup" in the course gradebook
 270       * - I navigate to "Scales" in the course gradebook
 271       * - I navigate to "More > Grade letters" in the course gradebook
 272       *
 273       * @Given /^I navigate to "(?P<gradepath_string>(?:[^"]|\\")*)" in the course gradebook$/
 274       * @param string $gradepath The path string. If the path has two items (ex. "More > Grade letters"), the first item
 275       *                          ("More") will be used to identify an option group in the navigation selector, while the
 276       *                          second ("Grade letters") will be used to identify an option within that option group.
 277       *                          Otherwise, a single item in a path (ex. "Scales") will be used to identify an option in
 278       *                          the navigation selector regardless of the option group.
 279       */
 280      public function i_navigate_to_in_the_course_gradebook($gradepath) {
 281          // If we are not on one of the gradebook pages already, follow "Grades" link in the navigation drawer.
 282          $xpath = '//div[contains(@class,\'grade-navigation\')]';
 283          if (!$this->getSession()->getPage()->findAll('xpath', $xpath)) {
 284              $this->execute('behat_navigation::i_select_from_secondary_navigation', get_string('grades'));
 285          }
 286  
 287          $this->execute('behat_forms::i_set_the_field_to', [get_string('gradebooknavigationmenu', 'grades'), $gradepath]);
 288      }
 289  
 290      /**
 291       * Navigates to the imports page in the course gradebook and selects the specified import type from the grade
 292       * imports navigation selector.
 293       *
 294       * Examples:
 295       * - I navigate to "CSV file" import page in the course gradebook
 296       *
 297       * @Given /^I navigate to "(?P<importoption_string>(?:[^"]|\\")*)" import page in the course gradebook$/
 298       * @param string $gradeimportoption The name of an existing grade import option.
 299       */
 300      public function i_navigate_to_import_page_in_the_course_gradebook($gradeimportoption) {
 301          $this->i_navigate_to_in_the_course_gradebook("More > Import");
 302          $this->execute('behat_forms::i_set_the_field_to', [get_string('importas', 'grades'), $gradeimportoption]);
 303      }
 304  
 305      /**
 306       * Navigates to the exports page in the course gradebook and selects the specified export type from the grade
 307       * exports navigation selector.
 308       *
 309       * Examples:
 310       * - I navigate to "XML file" export page in the course gradebook
 311       *
 312       * @Given /^I navigate to "(?P<exportoption_string>(?:[^"]|\\")*)" export page in the course gradebook$/
 313       * @param string $gradeexportoption The name of an existing grade export option.
 314       */
 315      public function i_navigate_to_export_page_in_the_course_gradebook($gradeexportoption) {
 316          $this->i_navigate_to_in_the_course_gradebook("More > Export");
 317          $this->execute('behat_forms::i_set_the_field_to', [get_string('exportas', 'grades'), $gradeexportoption]);
 318      }
 319  
 320      /**
 321       * Select a given option from a navigation URL selector in the gradebook. We must be on one of the gradebook pages
 322       * already.
 323       *
 324       * @deprecated since 4.1 - use behat_forms::i_set_the_field_to() instead.
 325       * @param string $path The string path that is used to identify an item within the navigation selector. If the path
 326       *                     has two items (ex. "More > Grade letters"), the first item ("More") will be used to identify
 327       *                     an option group in the navigation selector, while the second ("Grade letters") will be used to
 328       *                     identify an option within that option group. Otherwise, a single item in a path (ex. "Scales")
 329       *                     will be used to identify an option in the navigation selector regardless of the option group.
 330       * @param string $formid The ID of the form element which contains the navigation URL selector element.
 331       */
 332      protected function select_in_gradebook_navigation_selector(string $path, string $formid) {
 333          debugging('The function select_in_gradebook_navigation_selector() is deprecated, please use ' .
 334              'behat_forms::i_set_the_field_to() instead.', DEBUG_DEVELOPER);
 335  
 336          // Split the path string by ">".
 337          $path = preg_split('/\s*>\s*/', trim($path));
 338  
 339          // Make sure that the path does not have more than two items separated with ">".
 340          if (count($path) > 2) {
 341              throw new coding_exception('The path is too long (must have no more than two items separated with ">")');
 342          }
 343  
 344          // Get the select element.
 345          $selectxpath = "//form[contains(@id,'{$formid}')]//select";
 346          $select = $this->find('xpath', $selectxpath);
 347  
 348          // Define the xpath to the option element depending on the provided path.
 349          // If two items are provided in the path, the first item will be considered as an identifier of an existing
 350          // option group in the select select element, while the second item will identify an existing option within
 351          // that option group.
 352          // If one item is provided in the path, this item will identify any existing option in the select element
 353          // regardless of the option group. Also, this is useful when option elements are not a part of an option group
 354          // which is possible.
 355          if (count($path) === 2) {
 356              $optionxpath = $selectxpath . '/optgroup[@label="' . $this->escape($path[0]) . '"]' .
 357                  '/option[contains(.,"' . $this->escape($path[1]) . '")]';
 358          } else {
 359              $optionxpath = $selectxpath . '//option[contains(.,"' . $this->escape($path[0]) . '")]';
 360          }
 361  
 362          // Get the option element that we are looking to select.
 363          $option = $this->find('xpath', $optionxpath);
 364  
 365          // Select the given option in the select element.
 366          $field = behat_field_manager::get_field_instance('select', $select, $this->getSession());
 367          $field->set_value($this->escape($option->getValue()));
 368  
 369          if (!$this->running_javascript()) {
 370              $this->execute('behat_general::i_click_on_in_the', [get_string('go'), 'button',
 371                  "#{$formid}", 'css_element']);
 372          }
 373      }
 374  
 375      /**
 376       * Confirm if a value is within the search widget within the gradebook.
 377       *
 378       * Examples:
 379       * - I confirm "User" in "user" search within the gradebook widget exists
 380       * - I confirm "Group" in "group" search within the gradebook widget exists
 381       * - I confirm "Grade item" in "grade" search within the gradebook widget exists
 382       *
 383       * @Given /^I confirm "(?P<needle>(?:[^"]|\\")*)" in "(?P<haystack>(?:[^"]|\\")*)" search within the gradebook widget exists$/
 384       * @param string $needle The value to search for.
 385       * @param string $haystack The type of the search widget.
 386       */
 387      public function i_confirm_in_search_within_the_gradebook_widget_exists($needle, $haystack) {
 388          $triggercssselector = ".search-widget[data-searchtype='{$haystack}']";
 389  
 390          // Make sure that the dropdown menu is visible.
 391          $node = $this->find("css_element", "{$triggercssselector} .dropdown-menu");
 392          if (!$node->isVisible()) {
 393              $this->execute("behat_general::i_click_on", [$triggercssselector, "css_element"]);
 394          }
 395  
 396          $this->execute("behat_general::wait_until_the_page_is_ready");
 397          $this->execute("behat_general::assert_element_contains_text",
 398              [$needle, "{$triggercssselector} .dropdown-menu", "css_element"]);
 399      }
 400  
 401      /**
 402       * Confirm if a value is not within the search widget within the gradebook.
 403       *
 404       * Examples:
 405       * - I confirm "User" in "user" search within the gradebook widget does not exist
 406       * - I confirm "Group" in "group" search within the gradebook widget does not exist
 407       * - I confirm "Grade item" in "grade" search within the gradebook widget does not exist
 408       *
 409       * @Given /^I confirm "(?P<needle>(?:[^"]|\\")*)" in "(?P<haystack>(?:[^"]|\\")*)" search within the gradebook widget does not exist$/
 410       * @param string $needle The value to search for.
 411       * @param string $haystack The type of the search widget.
 412       */
 413      public function i_confirm_in_search_within_the_gradebook_widget_does_not_exist($needle, $haystack) {
 414          $triggercssselector = ".search-widget[data-searchtype='{$haystack}']";
 415  
 416          // Make sure that the dropdown menu is visible.
 417          $node = $this->find("css_element", "{$triggercssselector} .dropdown-menu");
 418          if (!$node->isVisible()) {
 419              $this->execute("behat_general::i_click_on", [$triggercssselector, "css_element"]);
 420          }
 421  
 422          $this->execute("behat_general::wait_until_the_page_is_ready");
 423          $this->execute("behat_general::assert_element_not_contains_text",
 424              [$needle, "{$triggercssselector} .dropdown-menu", "css_element"]);
 425      }
 426  
 427      /**
 428       * Clicks on an option from the specified search widget in the current gradebook page.
 429       *
 430       * Examples:
 431       * - I click on "Student" in the "user" search widget
 432       * - I click on "Group" in the "group" search widget
 433       * - I click on "Grade item" in the "grade" search widget
 434       *
 435       * @Given /^I click on "(?P<needle>(?:[^"]|\\")*)" in the "(?P<haystack>(?:[^"]|\\")*)" search widget$/
 436       * @param string $needle The value to search for.
 437       * @param string $haystack The type of the search widget.
 438       */
 439      public function i_click_on_in_search_widget(string $needle, string $haystack) {
 440          $this->execute("behat_general::wait_until_the_page_is_ready");
 441  
 442          $triggercssselector = ".search-widget[data-searchtype='{$haystack}']";
 443  
 444          $this->execute("behat_general::i_click_on", [$triggercssselector, "css_element"]);
 445          $this->execute("behat_general::wait_until_the_page_is_ready");
 446          $this->execute('behat_general::i_click_on_in_the', [
 447              "//li[@role='option'][contains(., '" . $needle . "')] | //a[contains(., '" . $needle . "')]",
 448              "xpath_element",
 449              "{$triggercssselector} .dropdown-menu",
 450              "css_element"
 451          ]);
 452      }
 453  }