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 social activities
  19   *
  20   * @package    block_social_activities
  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 social activities
  36   *
  37   * @package    block_social_activities
  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_social_activities extends behat_base {
  43  
  44      /**
  45       * Returns the DOM node of the activity in the social activities 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_social_block_activity_node($activityname) {
  52          $activityname = behat_context_helper::escape($activityname);
  53          $xpath = "//*[contains(concat(' ',normalize-space(@class),' '),' block_social_activities ')]//li[contains(., $activityname)]";
  54  
  55          return $this->find('xpath', $xpath);
  56      }
  57  
  58      /**
  59       * Checks that the specified activity in the social activities block should have the specified editing icon.
  60       *
  61       * This includes items in the action menu for the item (does not require it to be open)
  62       *
  63       * You should be in the course page with editing mode turned on.
  64       *
  65       * @Then /^"(?P<activity_name_string>(?:[^"]|\\")*)" activity in social activities block should have "(?P<icon_name_string>(?:[^"]|\\")*)" editing icon$/
  66       * @param string $activityname
  67       * @param string $iconname
  68       */
  69      public function activity_in_social_activities_block_should_have_editing_icon($activityname, $iconname) {
  70          $activitynode = $this->get_social_block_activity_node($activityname);
  71  
  72          $notfoundexception = new ExpectationException('"' . $activityname . '" doesn\'t have a "' .
  73              $iconname . '" editing icon', $this->getSession());
  74          $this->find('named_partial', array('link', $iconname), $notfoundexception, $activitynode);
  75      }
  76  
  77      /**
  78       * Checks that the specified activity in the social activities block should not have the specified editing icon.
  79       *
  80       * This includes items in the action menu for the item (does not require it to be open)
  81       *
  82       * You should be in the course page with editing mode turned on.
  83       *
  84       * @Then /^"(?P<activity_name_string>(?:[^"]|\\")*)" activity in social activities block should not have "(?P<icon_name_string>(?:[^"]|\\")*)" editing icon$/
  85       * @param string $activityname
  86       * @param string $iconname
  87       */
  88      public function activity_in_social_activities_block_should_not_have_editing_icon($activityname, $iconname) {
  89          $activitynode = $this->get_social_block_activity_node($activityname);
  90  
  91          try {
  92              $this->find('named_partial', array('link', $iconname), false, $activitynode);
  93              throw new ExpectationException('"' . $activityname . '" has a "' . $iconname .
  94                  '" editing icon when it should not', $this->getSession());
  95          } catch (ElementNotFoundException $e) {
  96              // This is good, the menu item should not be there.
  97          }
  98      }
  99  
 100      /**
 101       * Clicks on the specified element of the activity. You should be in the course page with editing mode turned on.
 102       *
 103       * @Given /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" in the "(?P<activity_name_string>(?:[^"]|\\")*)" activity in social activities block$/
 104       * @param string $element
 105       * @param string $selectortype
 106       * @param string $activityname
 107       */
 108      public function i_click_on_in_the_activity_in_social_activities_block($element, $selectortype, $activityname) {
 109          $element = $this->get_social_block_activity_element($element, $selectortype, $activityname);
 110          $element->click();
 111      }
 112  
 113      /**
 114       * Finds the element containing a specific activity in the social activity block.
 115       *
 116       * @throws ElementNotFoundException
 117       * @param string $element
 118       * @param string $selectortype
 119       * @param string $activityname
 120       * @return NodeElement
 121       */
 122      protected function get_social_block_activity_element($element, $selectortype, $activityname) {
 123          $activitynode = $this->get_social_block_activity_node($activityname);
 124  
 125          $exception = new ElementNotFoundException($this->getSession(), "'{$element}' '{$selectortype}' in '$activityname}'");
 126          return $this->find($selectortype, $element, $exception, $activitynode);
 127      }
 128  
 129      /**
 130       * Checks that the specified activity is hidden in the social activities block.
 131       *
 132       * @Then /^"(?P<activity_name_string>(?:[^"]|\\")*)" activity in social activities block should be hidden$/
 133       * @param string $activityname
 134       */
 135      public function activity_in_social_activities_block_should_be_hidden($activityname) {
 136          $activitynode = $this->get_social_block_activity_node($activityname);
 137          $exception = new ExpectationException('"' . $activityname . '" is not hidden', $this->getSession());
 138          $this->find('named_partial', array('badge', get_string('hiddenfromstudents')), $exception, $activitynode);
 139      }
 140  
 141      /**
 142       * Checks that the specified activity is hidden in the social activities block.
 143       *
 144       * @Then /^"(?P<activity_name_string>(?:[^"]|\\")*)" activity in social activities block should be available but hidden from course page$/
 145       * @param string $activityname
 146       */
 147      public function activity_in_social_activities_block_should_be_available_but_hidden_from_course_page($activityname) {
 148          $activitynode = $this->get_social_block_activity_node($activityname);
 149          $exception = new ExpectationException('"' . $activityname . '" is not hidden but available', $this->getSession());
 150          $this->find('named_partial', array('badge', get_string('hiddenoncoursepage')), $exception, $activitynode);
 151      }
 152  
 153      /**
 154       * Opens an activity actions menu in the social activities block if it is not already opened.
 155       *
 156       * @Given /^I open "(?P<activity_name_string>(?:[^"]|\\")*)" actions menu in social activities block$/
 157       * @throws DriverException The step is not available when Javascript is disabled
 158       * @param string $activityname
 159       */
 160      public function i_open_actions_menu_in_social_activities_block($activityname) {
 161          $activityname = behat_context_helper::escape($activityname);
 162          $xpath = "//*[contains(concat(' ',normalize-space(@class),' '),' block_social_activities ')]//li[contains(., $activityname)]";
 163          $this->execute('behat_action_menu::i_open_the_action_menu_in', [$xpath, 'xpath_element']);
 164      }
 165  
 166      /**
 167       * Return the list of partial named selectors.
 168       *
 169       * @return array
 170       */
 171      public static function get_partial_named_selectors(): array {
 172          return [
 173              new behat_component_named_selector('Activity', [
 174                  "//*[contains(concat(' ',normalize-space(@class),' '),' block_social_activities ')]//li[contains(., %locator%)]",
 175              ]),
 176          ];
 177      }
 178  }