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 401 and 402] [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   * Behat steps definitions for block site main menu
  19   *
  20   * @package    block_site_main_menu
  21   * @category   test
  22   * @copyright  2016 Marina Glancy
  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\Mink\Exception\ExpectationException as ExpectationException,
  31      Behat\Mink\Exception\DriverException as DriverException,
  32      Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
  33  
  34  /**
  35   * Behat steps definitions for block site main menu
  36   *
  37   * @package    block_site_main_menu
  38   * @category   test
  39   * @copyright  2016 Marina Glancy
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class behat_block_site_main_menu extends behat_base {
  43  
  44      /**
  45       * Returns the DOM node of the activity in the site menu block
  46       *
  47       * @throws ElementNotFoundException Thrown by behat_base::find
  48       * @param string $activityname The activity name
  49       * @return NodeElement
  50       */
  51      protected function get_site_menu_activity_node($activityname) {
  52          $activityname = behat_context_helper::escape($activityname);
  53          $xpath = "//*[contains(concat(' ',normalize-space(@class),' '),' block_site_main_menu ')]//li[contains(., $activityname)]";
  54  
  55          return $this->find('xpath', $xpath);
  56      }
  57  
  58      /**
  59       * Checks that the specified activity's action menu contains an item.
  60       *
  61       * @Then /^"(?P<activity_name_string>(?:[^"]|\\")*)" activity in site main menu block should have "(?P<icon_name_string>(?:[^"]|\\")*)" editing icon$/
  62       * @param string $activityname
  63       * @param string $iconname
  64       */
  65      public function activity_in_site_main_menu_block_should_have_editing_icon($activityname, $iconname) {
  66          $activitynode = $this->get_site_menu_activity_node($activityname);
  67  
  68          $notfoundexception = new ExpectationException('"' . $activityname . '" doesn\'t have a "' .
  69              $iconname . '" editing icon', $this->getSession());
  70          $this->find('named_partial', array('link', $iconname), $notfoundexception, $activitynode);
  71      }
  72  
  73      /**
  74       * Checks that the specified activity's action menu contains an item.
  75       *
  76       * @Then /^"(?P<activity_name_string>(?:[^"]|\\")*)" activity in site main menu block should not have "(?P<icon_name_string>(?:[^"]|\\")*)" editing icon$/
  77       * @param string $activityname
  78       * @param string $iconname
  79       */
  80      public function activity_in_site_main_menu_block_should_not_have_editing_icon($activityname, $iconname) {
  81          $activitynode = $this->get_site_menu_activity_node($activityname);
  82  
  83          try {
  84              $this->find('named_partial', array('link', $iconname), false, $activitynode);
  85              throw new ExpectationException('"' . $activityname . '" has a "' . $iconname .
  86                  '" editing icon when it should not', $this->getSession());
  87          } catch (ElementNotFoundException $e) {
  88              // This is good, the menu item should not be there.
  89          }
  90      }
  91  
  92      /**
  93       * Clicks on the specified element of the activity. You should be in the course page with editing mode turned on.
  94       *
  95       * @Given /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" in the "(?P<activity_name_string>(?:[^"]|\\")*)" activity in site main menu block$/
  96       * @param string $element
  97       * @param string $selectortype
  98       * @param string $activityname
  99       */
 100      public function i_click_on_in_the_activity_in_site_main_menu_block($element, $selectortype, $activityname) {
 101          $element = $this->get_site_menu_activity_element($element, $selectortype, $activityname);
 102          $element->click();
 103      }
 104  
 105      /**
 106       * Clicks on the specified element inside the activity container.
 107       *
 108       * @throws ElementNotFoundException
 109       * @param string $element
 110       * @param string $selectortype
 111       * @param string $activityname
 112       * @return NodeElement
 113       */
 114      protected function get_site_menu_activity_element($element, $selectortype, $activityname) {
 115          $activitynode = $this->get_site_menu_activity_node($activityname);
 116  
 117          $exception = new ElementNotFoundException($this->getSession(), "'{$element}' '{$selectortype}' in '$activityname}'");
 118          return $this->find($selectortype, $element, $exception, $activitynode);
 119      }
 120  
 121      /**
 122       * Checks that the specified activity is hidden.
 123       *
 124       * @Then /^"(?P<activity_name_string>(?:[^"]|\\")*)" activity in site main menu block should be hidden$/
 125       * @param string $activityname
 126       */
 127      public function activity_in_site_main_menu_block_should_be_hidden($activityname) {
 128          $activitynode = $this->get_site_menu_activity_node($activityname);
 129          $exception = new ExpectationException('"' . $activityname . '" is not hidden', $this->getSession());
 130          $this->find('named_partial', array('badge', get_string('hiddenfromstudents')), $exception, $activitynode);
 131      }
 132  
 133      /**
 134       * Checks that the specified activity is hidden.
 135       *
 136       * @Then /^"(?P<activity_name_string>(?:[^"]|\\")*)" activity in site main menu block should be available but hidden from course page$/
 137       * @param string $activityname
 138       */
 139      public function activity_in_site_main_menu_block_should_be_available_but_hidden_from_course_page($activityname) {
 140          $activitynode = $this->get_site_menu_activity_node($activityname);
 141          $exception = new ExpectationException('"' . $activityname . '" is not hidden but available', $this->getSession());
 142          $this->find('named_partial', array('badge', get_string('hiddenoncoursepage')), $exception, $activitynode);
 143      }
 144  
 145      /**
 146       * Opens an activity actions menu if it is not already opened.
 147       *
 148       * @Given /^I open "(?P<activity_name_string>(?:[^"]|\\")*)" actions menu in site main menu block$/
 149       * @throws DriverException The step is not available when Javascript is disabled
 150       * @param string $activityname
 151       */
 152      public function i_open_actions_menu_in_site_main_menu_block($activityname) {
 153          $activityname = behat_context_helper::escape($activityname);
 154          $xpath = "//*[contains(concat(' ',normalize-space(@class),' '),' block_site_main_menu ')]//li[contains(., $activityname)]";
 155          $this->execute('behat_action_menu::i_open_the_action_menu_in', [$xpath, 'xpath_element']);
 156      }
 157  
 158      /**
 159       * Return the list of partial named selectors.
 160       *
 161       * @return array
 162       */
 163      public static function get_partial_named_selectors(): array {
 164          return [
 165              new behat_component_named_selector('Activity', [
 166                  "//*[contains(concat(' ',normalize-space(@class),' '),' block_site_main_menu ')]//li[contains(., %locator%)]"
 167              ]),
 168          ];
 169      }
 170  }