Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 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       * @Given /^I choose "(?P<link_string>(?:[^"]|\\")*)" in the open action menu$/
  73       * @param string $linkstring
  74       * @return void
  75       */
  76      public function i_choose_in_the_open_action_menu($menuitemstring) {
  77          if (!$this->running_javascript()) {
  78              throw new DriverException('Action menu steps are not available with Javascript disabled');
  79          }
  80          // Gets the node based on the requested selector type and locator.
  81          $menuselector = ".moodle-actionmenu .dropdown.show .dropdown-menu";
  82          $node = $this->get_node_in_container("link", $menuitemstring, "css_element", $menuselector);
  83          $this->ensure_node_is_visible($node);
  84          $node->click();
  85      }
  86  
  87      /**
  88       * Select a specific item in an action menu.
  89       *
  90       * @When /^I choose the "(?P<item_string>(?:[^"]|\\")*)" item in the "(?P<actionmenu_string>(?:[^"]|\\")*)" action menu$/
  91       * @param string $item The item to choose
  92       * @param string $actionmenu The text used in the description of the action menu
  93       */
  94      public function i_choose_in_the_named_menu(string $item, string $actionmenu): void {
  95          $menu = $this->find('actionmenu', $actionmenu);
  96          $this->select_item_in_action_menu($item, $menu);
  97      }
  98  
  99      /**
 100       * Select a specific item in an action menu within a container.
 101       *
 102       * @When /^I choose the "(?P<item_string>(?:[^"]|\\")*)" item in the "(?P<actionmenu_string>(?:[^"]|\\")*)" action menu of the "(?P<locator_string>(?:[^"]|\\")*)" "(?P<type_string>(?:[^"]|\\")*)"$/
 103       * @param string $item The item to choose
 104       * @param string $actionmenu The text used in the description of the action menu
 105       * @param string|NodeElement $locator The identifer used for the container
 106       * @param string $selector The type of container to locate
 107       */
 108      public function i_choose_in_the_named_menu_in_container(string $item, string $actionmenu, $locator, $selector): void {
 109          $container = $this->find($selector, $locator);
 110          $menu = $this->find('actionmenu', $actionmenu, false, $container);
 111          $this->select_item_in_action_menu($item, $menu);
 112      }
 113  
 114      /**
 115       * Select an item in the specified menu.
 116       *
 117       * Note: This step does work both with, and without, JavaScript.
 118       *
 119       * @param string $item Item string value
 120       * @param NodeElement $menu The menu NodeElement to select from
 121       */
 122      protected function select_item_in_action_menu(string $item, NodeElement $menu): void {
 123          if ($this->running_javascript()) {
 124              // Open the menu by clicking on the trigger.
 125              $this->execute(
 126                  'behat_general::i_click_on',
 127                  [$menu, "NodeElement"]
 128              );
 129          }
 130  
 131          // Select the menu item.
 132          $this->execute(
 133              'behat_general::i_click_on_in_the',
 134              [$item, "link", $menu, "NodeElement"]
 135          );
 136      }
 137  
 138      /**
 139       * The action menu item should not exist.
 140       *
 141       * @Then /^the "(?P<item_string>(?:[^"]|\\")*)" item should not exist in the "(?P<actionmenu_string>(?:[^"]|\\")*)" action menu$/
 142       * @param string $item The item to check
 143       * @param string $actionmenu The text used in the description of the action menu
 144       */
 145      public function item_should_not_exist(string $item, string $actionmenu): void {
 146          $menu = $this->find('actionmenu', $actionmenu);
 147          $this->execute('behat_general::should_not_exist_in_the', [
 148              $item, 'link',
 149              $menu, 'NodeElement'
 150          ]);
 151      }
 152  
 153      /**
 154       * The action menu item should not exist within a container.
 155       *
 156       * @Then /^the "(?P<item_string>(?:[^"]|\\")*)" item should not exist in the "(?P<actionmenu_string>(?:[^"]|\\")*)" action menu of the "(?P<locator_string>(?:[^"]|\\")*)" "(?P<type_string>(?:[^"]|\\")*)"$/
 157       * @param string $item The item to check
 158       * @param string $actionmenu The text used in the description of the action menu
 159       * @param string|NodeElement $locator The identifer used for the container
 160       * @param string $selector The type of container to locate
 161       */
 162      public function item_should_not_exist_in_the(string $item, string $actionmenu, $locator, $selector): void {
 163          $container = $this->find($selector, $locator);
 164          $menu = $this->find('actionmenu', $actionmenu, false, $container);
 165          $this->execute('behat_general::should_not_exist_in_the', [
 166              $item, 'link',
 167              $menu, 'NodeElement'
 168          ]);
 169      }
 170  
 171  
 172      /**
 173       * The action menu item should exist.
 174       *
 175       * @Then /^the "(?P<item_string>(?:[^"]|\\")*)" item should exist in the "(?P<actionmenu_string>(?:[^"]|\\")*)" action menu$/
 176       * @param string $item The item to check
 177       * @param string $actionmenu The text used in the description of the action menu
 178       */
 179      public function item_should_exist(string $item, string $actionmenu): void {
 180          $menu = $this->find('actionmenu', $actionmenu);
 181          $this->execute('behat_general::should_exist_in_the', [
 182              $item, 'link',
 183              $menu, 'NodeElement'
 184          ]);
 185      }
 186  
 187      /**
 188       * The action menu item should exist within a container.
 189       *
 190       * @Then /^the "(?P<item_string>(?:[^"]|\\")*)" item should exist in the "(?P<actionmenu_string>(?:[^"]|\\")*)" action menu of the "(?P<locator_string>(?:[^"]|\\")*)" "(?P<type_string>(?:[^"]|\\")*)"$/
 191       * @param string $item The item to check
 192       * @param string $actionmenu The text used in the description of the action menu
 193       * @param string|NodeElement $locator The identifer used for the container
 194       * @param string $selector The type of container to locate
 195       */
 196      public function item_should_exist_in_the(string $item, string $actionmenu, $locator, $selector): void {
 197          $container = $this->find($selector, $locator);
 198          $menu = $this->find('actionmenu', $actionmenu, false, $container);
 199          $this->execute('behat_general::should_exist_in_the', [
 200              $item, 'link',
 201              $menu, 'NodeElement'
 202          ]);
 203      }
 204  }