Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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 message-related steps definitions.
  19   *
  20   * @package    core_message
  21   * @category   test
  22   * @copyright  2013 David MonllaĆ³
  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  /**
  31   * Messaging system steps definitions.
  32   *
  33   * @package    core_message
  34   * @category   test
  35   * @copyright  2013 David MonllaĆ³
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class behat_message extends behat_base {
  39  
  40      /**
  41       * Return the list of partial named selectors.
  42       *
  43       * @return array
  44       */
  45      public static function get_partial_named_selectors(): array {
  46          return [
  47              new behat_component_named_selector('Message', [".//*[@data-conversation-id]//img[%altMatch%]/.."]),
  48              new behat_component_named_selector('Message conversation', [
  49                  <<<XPATH
  50      .//*[@data-region='message-drawer' and contains(., %locator%)]//div[@data-region='content-message-container']
  51  XPATH
  52              ], false),
  53              new behat_component_named_selector('Message header', [
  54                  <<<XPATH
  55      .//*[@data-region='message-drawer']//div[@data-region='header-content' and contains(., %locator%)]
  56  XPATH
  57              ]),
  58              new behat_component_named_selector('Message member', [
  59                  <<<XPATH
  60      .//*[@data-region='message-drawer']//div[@data-region='group-info-content-container']
  61      //div[@class='list-group' and not(contains(@class, 'hidden'))]//*[%core_message/textMatch%]
  62  XPATH
  63                  , <<<XPATH
  64      .//*[@data-region='message-drawer']//div[@data-region='group-info-content-container']
  65      //div[@data-region='empty-message-container' and not(contains(@class, 'hidden')) and contains(., %locator%)]
  66  XPATH
  67              ], false),
  68              new behat_component_named_selector('Message tab', [
  69                  <<<XPATH
  70      .//*[@data-region='message-drawer']//button[@data-toggle='collapse' and contains(string(), %locator%)]
  71  XPATH
  72              ], false),
  73              new behat_component_named_selector('Message list area', [
  74                  <<<XPATH
  75      .//*[@data-region='message-drawer']//*[contains(@data-region, concat('view-overview-', %locator%))]
  76  XPATH
  77              ], false),
  78              new behat_component_named_selector('Message content', [
  79                  <<<XPATH
  80      .//*[@data-region='message-drawer']//*[@data-region='message' and @data-message-id and contains(., %locator%)]
  81  XPATH
  82              ], false),
  83          ];
  84      }
  85  
  86      /**
  87       * Return a list of the Mink named replacements for the component.
  88       *
  89       * Named replacements allow you to define parts of an xpath that can be reused multiple times, or in multiple
  90       * xpaths.
  91       *
  92       * This method should return a list of {@link behat_component_named_replacement} and the docs on that class explain
  93       * how it works.
  94       *
  95       * @return behat_component_named_replacement[]
  96       */
  97      public static function get_named_replacements(): array {
  98          return [
  99              new behat_component_named_replacement('textMatch', 'text()[contains(., %locator%)]'),
 100          ];
 101      }
 102  
 103      /**
 104       * Open the messaging UI.
 105       *
 106       * @Given /^I open messaging$/
 107       */
 108      public function i_open_messaging() {
 109          // Visit home page and follow messages.
 110          $this->execute("behat_general::i_am_on_homepage");
 111          $this->execute("behat_general::i_click_on", [get_string('togglemessagemenu', 'core_message'), 'link']);
 112      }
 113  
 114      /**
 115       * Open the messaging conversation list.
 116       *
 117       * @Given /^I open the "(?P<tab_string>(?:[^"]|\\")*)" conversations list/
 118       * @param string $tab
 119       */
 120      public function i_open_the_conversations_list(string $tab) {
 121          $this->execute('behat_general::i_click_on', [
 122              $this->escape($tab),
 123              'core_message > Message tab'
 124          ]);
 125      }
 126  
 127      /**
 128       * Open the messaging UI.
 129       *
 130       * @Given /^I open messaging information$/
 131       */
 132      public function i_open_messaging_information() {
 133          $this->execute('behat_general::i_click_on', ["[data-action='view-group-info']", 'css_element']);
 134      }
 135  
 136      /**
 137       * View the contact information of a user in the messages ui.
 138       *
 139       * @Given /^I view the "(?P<user_full_name_string>(?:[^"]|\\")*)" contact in the message area$/
 140       * @param string $userfullname
 141       */
 142      public function i_view_contact_in_messages($userfullname) {
 143          // Visit home page and follow messages.
 144          $this->execute('behat_message::i_select_user_in_messaging', [$userfullname]);
 145  
 146          $this->execute('behat_general::i_click_on_in_the',
 147              array(
 148                  "//a[@data-action='view-contact']",
 149                  "xpath_element",
 150                  "//*[@data-region='message-drawer']//div[@data-region='header-container']",
 151                  "xpath_element",
 152              )
 153          );
 154          $this->execute('behat_general::i_click_on_in_the',
 155              array(
 156                  "//img[@title='Picture of ". $this->escape($userfullname) . "']",
 157                  "xpath_element",
 158                  "//*[@data-region='message-drawer']//*[@data-region='view-contact']",
 159                  "xpath_element",
 160              )
 161          );
 162  
 163          $this->execute('behat_general::wait_until_the_page_is_ready');
 164      }
 165  
 166      /**
 167       * Select a user in the messaging UI.
 168       *
 169       * @Given /^I select "(?P<user_full_name_string>(?:[^"]|\\")*)" user in messaging$/
 170       * @param string $userfullname
 171       */
 172      public function i_select_user_in_messaging($userfullname) {
 173          $this->execute('behat_message::i_open_messaging', []);
 174  
 175          $this->execute('behat_message::i_search_for_string_in_messaging', [$userfullname]);
 176  
 177          // Need to limit the click to the search results because the 'view-contact-profile' elements
 178          // can occur in two separate divs on the page.
 179          $this->execute('behat_general::i_click_on_in_the',
 180              [
 181                  $this->escape($userfullname),
 182                  'link',
 183                  "[data-region='message-drawer'] [data-region='search-results-container']",
 184                  "css_element",
 185              ]
 186          );
 187  
 188          $this->execute('behat_general::wait_until_the_page_is_ready');
 189      }
 190  
 191      /**
 192       * Search for a string using the messaging search.
 193       *
 194       * @Given /^I search for "(?P<string>(?:[^"]|\\")*)" in messaging$/
 195       * @param string $string the search string.
 196       */
 197      public function i_search_for_string_in_messaging($string) {
 198          $this->execute('behat_general::i_click_on', [get_string('search', 'core'), 'field']);
 199  
 200          $this->execute('behat_forms::i_set_the_field_with_xpath_to',
 201              [
 202                  "//*[@data-region='message-drawer']//input[@data-region='search-input']",
 203                  $this->escape($string)
 204              ]
 205          );
 206  
 207          $this->execute('behat_general::i_click_on', ['[data-action="search"]', 'css_element']);
 208  
 209          $this->execute('behat_general::wait_until_the_page_is_ready');
 210      }
 211  
 212      /**
 213       * Sends a message to the specified user from the logged user. The user full name should contain the first and last names.
 214       *
 215       * @Given /^I send "(?P<message_contents_string>(?:[^"]|\\")*)" message to "(?P<user_full_name_string>(?:[^"]|\\")*)" user$/
 216       * @param string $messagecontent
 217       * @param string $userfullname
 218       */
 219      public function i_send_message_to_user($messagecontent, $userfullname) {
 220          $this->execute('behat_message::i_select_user_in_messaging', [$userfullname]);
 221  
 222          $this->execute('behat_forms::i_set_the_field_with_xpath_to',
 223              array("//textarea[@data-region='send-message-txt']", $this->escape($messagecontent))
 224          );
 225  
 226          $this->execute('behat_general::i_click_on_in_the',
 227              [
 228                  '[data-action="send-message"]',
 229                  'css_element',
 230                  "[data-region='message-drawer'] [data-region='footer-container'] [data-region='view-conversation']",
 231                  "css_element",
 232              ]
 233          );
 234      }
 235  
 236      /**
 237       * Select messages from a user in the messaging ui.
 238       *
 239       * @Given /^I send "(?P<message_contents_string>(?:[^"]|\\")*)" message in the message area$/
 240       * @param string $messagecontent
 241       */
 242      public function i_send_message_in_the_message_area($messagecontent) {
 243          $this->execute('behat_general::wait_until_the_page_is_ready');
 244  
 245          $this->execute('behat_forms::i_set_the_field_with_xpath_to',
 246              array("//textarea[@data-region='send-message-txt']", $this->escape($messagecontent))
 247          );
 248  
 249          $this->execute("behat_forms::press_button", get_string('sendmessage', 'message'));
 250      }
 251  
 252      /**
 253       * Navigate back in the messages ui drawer.
 254       *
 255       * @Given /^I go back in "(?P<parent_element_string>(?:[^"]|\\")*)" message drawer$/
 256       * @param string $parentelement
 257       */
 258      public function i_go_back_in_message_drawer($parentelement) {
 259          $this->execute('behat_general::i_click_on_in_the',
 260              array(
 261                  'a[data-route-back]',
 262                  'css_element',
 263                  '[data-region="'.$this->escape($parentelement).'"]',
 264                  'css_element',
 265              )
 266          );
 267      }
 268  
 269      /**
 270       * Select a user in the messaging UI.
 271       *
 272       * @Given /^I select "(?P<conversation_name_string>(?:[^"]|\\")*)" conversation in messaging$/
 273       * @param string $conversationname
 274       */
 275      public function i_select_conversation_in_messaging($conversationname) {
 276          $this->execute('behat_general::i_click_on',
 277              array(
 278                  $this->escape($conversationname),
 279                  'core_message > Message',
 280              )
 281          );
 282      }
 283  
 284      /**
 285       * Open the contact menu.
 286       *
 287       * @Given /^I open contact menu$/
 288       */
 289      public function i_open_contact_menu() {
 290          $this->execute('behat_general::wait_until_the_page_is_ready');
 291          $this->execute('behat_general::i_click_on_in_the',
 292              array(
 293                  'button',
 294                  'css_element',
 295                  '[data-region="message-drawer"] [data-region="header-container"]',
 296                  'css_element',
 297              )
 298          );
 299      }
 300  
 301      /**
 302       * Select a user in a specific messaging UI conversations list.
 303       *
 304       * @Given /^I select "(?P<conv_name_string>(?:[^"]|\\")*)" conversation in the "(?P<list_name_string>(?:[^"]|\\")*)" conversations list$/
 305       * @param string $convname
 306       * @param string $listname
 307       */
 308      public function i_select_conversation_in_the_conversations_list(string $convname, string $listname) {
 309          $xpath = '//*[@data-region="message-drawer"]//div[@data-region="view-overview-'.
 310              $this->escape($listname).
 311              '"]//*[@data-conversation-id]//img[contains(@alt,"'.
 312              $this->escape($convname).'")]';
 313          $this->execute('behat_general::i_click_on', array($xpath, 'xpath_element'));
 314      }
 315  
 316      /**
 317       * Open the settings preferences.
 318       *
 319       * @Given /^I open messaging settings preferences$/
 320       */
 321      public function i_open_messaging_settings_preferences() {
 322          $this->execute('behat_general::wait_until_the_page_is_ready');
 323          $this->execute('behat_general::i_click_on',
 324              array(
 325                  '//*[@data-region="message-drawer"]//a[@data-route="view-settings"]',
 326                  'xpath_element',
 327                  '',
 328                  '',
 329              )
 330          );
 331      }
 332  }