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.

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