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.
   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   * Moodle-specific selectors.
  19   *
  20   * @package    core
  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  require_once (__DIR__ . '/named_selector.php');
  27  require_once (__DIR__ . '/exact_named_selector.php');
  28  require_once (__DIR__ . '/partial_named_selector.php');
  29  
  30  use Behat\Mink\Exception\ExpectationException as ExpectationException;
  31  use Behat\Mink\Element\Element;
  32  
  33  /**
  34   * Moodle selectors manager.
  35   *
  36   * @package    core
  37   * @category   test
  38   * @copyright  2013 David MonllaĆ³
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class behat_selectors {
  42  
  43      /**
  44       * Returns the behat selector and locator for a given moodle selector and locator
  45       *
  46       * @param string $selectortype The moodle selector type, which includes moodle selectors
  47       * @param string $element The locator we look for in that kind of selector
  48       * @param Session $session The Mink opened session
  49       * @return array Contains the selector and the locator expected by Mink.
  50       */
  51      public static function get_behat_selector($selectortype, $element, Behat\Mink\Session $session) {
  52          // Note: This function is not deprecated, but not the recommended way of doing things.
  53          [
  54              'selector' => $selector,
  55              'locator' => $locator,
  56          ] = $session->normalise_selector($selectortype, $element, $session->getPage());
  57  
  58          // CSS and XPath selectors locator is one single argument.
  59          return [$selector, $locator];
  60      }
  61  
  62      /**
  63       * Allowed selectors getter.
  64       *
  65       * @return array
  66       */
  67      public static function get_allowed_selectors() {
  68          return array_merge(
  69              behat_partial_named_selector::get_allowed_selectors(),
  70              behat_exact_named_selector::get_allowed_selectors()
  71          );
  72      }
  73  
  74      /**
  75       * Allowed text selectors getter.
  76       *
  77       * @return array
  78       */
  79      public static function get_allowed_text_selectors() {
  80          return array_merge(
  81              behat_partial_named_selector::get_allowed_text_selectors(),
  82              behat_exact_named_selector::get_allowed_text_selectors()
  83          );
  84      }
  85  
  86      /**
  87       * Normalise the selector and locator for a named partial.
  88       *
  89       * @param string $selector The selector name
  90       * @param string $locator The value to normalise
  91       * @return array
  92       */
  93      public static function normalise_named_selector(string $selector, string $locator): array {
  94          return [
  95              $selector,
  96              behat_context_helper::escape($locator),
  97          ];
  98      }
  99  
 100      /**
 101       * Transform the selector for a field.
 102       *
 103       * @param string $label The label to find
 104       * @param Element $container The container to look within
 105       * @return array The selector, locator, and container to search within
 106       */
 107      public static function transform_find_for_field(behat_base $context, string $label, Element $container): array {
 108          $hasfieldset = strpos($label, '>');
 109          if (false !== $hasfieldset) {
 110              [$containerlabel, $label] = explode(">", $label, 2);
 111              $container = $context->find_fieldset(trim($containerlabel), $container);
 112              $label = trim($label);
 113          }
 114  
 115          return [
 116              'selector' => 'named_partial',
 117              'locator' => self::normalise_named_selector('field', $label),
 118              'container' => $container,
 119          ];
 120      }
 121  }