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] [Versions 400 and 403] [Versions 401 and 403] [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   * Steps definitions to open and close action menus.
  19   *
  20   * @package    core
  21   * @category   test
  22   * @copyright  2016 Damyon Wiese
  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__ . '/../../behat/behat_base.php');
  29  
  30  use Behat\Mink\Element\NodeElement;
  31  use Behat\Mink\Exception\DriverException;
  32  
  33  /**
  34   * Steps definitions to open and close action menus.
  35   *
  36   * @package    core
  37   * @category   test
  38   * @copyright  2016 Damyon Wiese
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class behat_action_menu extends behat_base {
  42  
  43      /**
  44       * Open the action menu in
  45       *
  46       * @Given /^I open the action menu in "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
  47       * @param string $element
  48       * @param string $selector
  49       * @return void
  50       */
  51      public function i_open_the_action_menu_in($element, $selectortype) {
  52          // Gets the node based on the requested selector type and locator.
  53          $node = $this->get_node_in_container(
  54              "css_element",
  55              "[role=button][aria-haspopup=true],button[aria-haspopup=true],[role=menuitem][aria-haspopup=true]",
  56              $selectortype,
  57              $element
  58          );
  59  
  60          // Check if it is not already opened.
  61          if ($node->getAttribute('aria-expanded') === 'true') {
  62              return;
  63          }
  64  
  65          $this->ensure_node_is_visible($node);
  66          $node->click();
  67      }
  68  
  69      /**
  70       * When an action menu is open, follow one of the items in it.
  71       *
  72       * The > is used to indicate a sub-menu. For example "Group mode > Visible groups"
  73       * will do two clicks, one on the Group mode menu item, and one on the Visible groups link
  74       * in the sub-menu.
  75       *
  76       * @Given /^I choose "(?P<link_string>(?:[^"]|\\")*)" in the open action menu$/
  77       * @param string $linkstring
  78       * @return void
  79       */
  80      public function i_choose_in_the_open_action_menu($menuitemstring) {
  81          if (!$this->running_javascript()) {
  82              throw new DriverException('Action menu steps are not available with Javascript disabled');
  83          }
  84          // Check for sub-menus.
  85          $menuitems = explode('>', $menuitemstring);
  86          foreach ($menuitems as $menuitem) {
  87              // Gets the node based on the requested selector type and locator.
  88              $menuselector = ".moodle-actionmenu .dropdown.show .dropdown-menu";
  89              $node = $this->get_node_in_container("link", trim($menuitem), "css_element", $menuselector);
  90              $this->ensure_node_is_visible($node);
  91              $node->click();
  92          }
  93  
  94      }
  95  
  96      /**
  97       * Select a specific item in an action menu.
  98       *
  99       * @When /^I choose the "(?P<item_string>(?:[^"]|\\")*)" item in the "(?P<actionmenu_string>(?:[^"]|\\")*)" action menu$/
 100       * @param string $item The item to choose
 101       * @param string $actionmenu The text used in the description of the action menu
 102       */
 103      public function i_choose_in_the_named_menu(string $item, string $actionmenu): void {
 104          $menu = $this->find('actionmenu', $actionmenu);
 105          $this->select_item_in_action_menu($item, $menu);
 106      }
 107  
 108      /**
 109       * Select a specific item in an action menu within a container.
 110       *
 111       * @When /^I choose the "(?P<item_string>(?:[^"]|\\")*)" item in the "(?P<actionmenu_string>(?:[^"]|\\")*)" action menu of the "(?P<locator_string>(?:[^"]|\\")*)" "(?P<type_string>(?:[^"]|\\")*)"$/
 112       * @param string $item The item to choose
 113       * @param string $actionmenu The text used in the description of the action menu
 114       * @param string|NodeElement $locator The identifer used for the container
 115       * @param string $selector The type of container to locate
 116       */
 117      public function i_choose_in_the_named_menu_in_container(string $item, string $actionmenu, $locator, $selector): void {
 118          $container = $this->find($selector, $locator);
 119          $menu = $this->find('actionmenu', $actionmenu, false, $container);
 120          $this->select_item_in_action_menu($item, $menu);
 121      }
 122  
 123      /**
 124       * Select an item in the specified menu.
 125       *
 126       * Note: This step does work both with, and without, JavaScript.
 127       *
 128       * @param string $item Item string value
 129       * @param NodeElement $menu The menu NodeElement to select from
 130       */
 131      protected function select_item_in_action_menu(string $item, NodeElement $menu): void {
 132          if ($this->running_javascript()) {
 133              // Open the menu by clicking on the trigger.
 134              $this->execute(
 135                  'behat_general::i_click_on',
 136                  [$menu, "NodeElement"]
 137              );
 138          }
 139  
 140          // Select the menu item.
 141          $this->execute(
 142              'behat_general::i_click_on_in_the',
 143              [$item, "link", $menu, "NodeElement"]
 144          );
 145      }
 146  
 147      /**
 148       * The action menu item should not exist.
 149       *
 150       * @Then /^the "(?P<item_string>(?:[^"]|\\")*)" item should not exist in the "(?P<actionmenu_string>(?:[^"]|\\")*)" action menu$/
 151       * @param string $item The item to check
 152       * @param string $actionmenu The text used in the description of the action menu
 153       */
 154      public function item_should_not_exist(string $item, string $actionmenu): void {
 155          $menu = $this->find('actionmenu', $actionmenu);
 156          $this->execute('behat_general::should_not_exist_in_the', [
 157              $item, 'link',
 158              $menu, 'NodeElement'
 159          ]);
 160      }
 161  
 162      /**
 163       * The action menu item should not exist within a container.
 164       *
 165       * @Then /^the "(?P<item_string>(?:[^"]|\\")*)" item should not exist in the "(?P<actionmenu_string>(?:[^"]|\\")*)" action menu of the "(?P<locator_string>(?:[^"]|\\")*)" "(?P<type_string>(?:[^"]|\\")*)"$/
 166       * @param string $item The item to check
 167       * @param string $actionmenu The text used in the description of the action menu
 168       * @param string|NodeElement $locator The identifer used for the container
 169       * @param string $selector The type of container to locate
 170       */
 171      public function item_should_not_exist_in_the(string $item, string $actionmenu, $locator, $selector): void {
 172          $container = $this->find($selector, $locator);
 173          $menu = $this->find('actionmenu', $actionmenu, false, $container);
 174          $this->execute('behat_general::should_not_exist_in_the', [
 175              $item, 'link',
 176              $menu, 'NodeElement'
 177          ]);
 178      }
 179  
 180  
 181      /**
 182       * The action menu item should exist.
 183       *
 184       * @Then /^the "(?P<item_string>(?:[^"]|\\")*)" item should exist in the "(?P<actionmenu_string>(?:[^"]|\\")*)" action menu$/
 185       * @param string $item The item to check
 186       * @param string $actionmenu The text used in the description of the action menu
 187       */
 188      public function item_should_exist(string $item, string $actionmenu): void {
 189          $menu = $this->find('actionmenu', $actionmenu);
 190          $this->execute('behat_general::should_exist_in_the', [
 191              $item, 'link',
 192              $menu, 'NodeElement'
 193          ]);
 194      }
 195  
 196      /**
 197       * The action menu item should exist within a container.
 198       *
 199       * @Then /^the "(?P<item_string>(?:[^"]|\\")*)" item should exist in the "(?P<actionmenu_string>(?:[^"]|\\")*)" action menu of the "(?P<locator_string>(?:[^"]|\\")*)" "(?P<type_string>(?:[^"]|\\")*)"$/
 200       * @param string $item The item to check
 201       * @param string $actionmenu The text used in the description of the action menu
 202       * @param string|NodeElement $locator The identifer used for the container
 203       * @param string $selector The type of container to locate
 204       */
 205      public function item_should_exist_in_the(string $item, string $actionmenu, $locator, $selector): void {
 206          $container = $this->find($selector, $locator);
 207          $menu = $this->find('actionmenu', $actionmenu, false, $container);
 208          $this->execute('behat_general::should_exist_in_the', [
 209              $item, 'link',
 210              $menu, 'NodeElement'
 211          ]);
 212      }
 213  }