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.
   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   * A class for recording the definition of Mink replacements.
  19   *
  20   * @package    core
  21   * @category   test
  22   * @copyright  2019 Andrew Nicols <andrew@nicols.co.uk>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  /**
  27   * A class for recording the definition of Mink replacements for use in Mink selectors.
  28   *
  29   * These are comprised of a source string, and a replacement.
  30   *
  31   * During use the source string is converted from the string to be in the format:
  32   *
  33   *      %[component]/[string]%
  34   *
  35   * For example:
  36   *
  37   *      %mod_forum/title%
  38   *
  39   * Mink replacements are used in xpath translation to translate regularly used items such as title.
  40   * Here is an example from the upstream Mink project:
  41   *
  42   * '%tagTextMatch%' => 'contains(normalize-space(string(.)), %locator%)'
  43   *
  44   * And can be used in an xpath:
  45   *
  46   *      .//label[%tagTextMatch%]
  47   *
  48   * This would be expanded to:
  49   *
  50   *      .//label[contains(normalize-space(string(.)), %locator%)]
  51   *
  52   * Replacements can also be used in other replacements, as long as that replacement is defined later.
  53   *
  54   *      '%linkMatch%' => '(%idMatch% or %tagTextMatch% or %titleMatch% or %relMatch%)'
  55   *
  56   * @package    core
  57   * @category   test
  58   * @copyright  2019 Andrew Nicols <andrew@nicols.co.uk>
  59   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  60   */
  61  class behat_component_named_replacement {
  62      /** @var string */
  63      protected $from;
  64  
  65      /** @var string */
  66      protected $to;
  67  
  68      /**
  69       * Create the replacement.
  70       *
  71       * @param string $from this is the old selector that should no longer be used.
  72       *      For example 'group_message'.
  73       * @param string $to this is the new equivalent that should be used instead.
  74       *      For example 'core_message > Message'.
  75       */
  76      public function __construct(string $from, string $to) {
  77          $this->from = $from;
  78          $this->to = $to;
  79      }
  80  
  81      /**
  82       * Get the 'from' part of the replacement, formatted for the component.
  83       *
  84       * @param string $component
  85       * @return string
  86       */
  87      public function get_from(string $component): string {
  88          return "%{$component}/{$this->from}%";
  89      }
  90  
  91      /**
  92       * Get the 'to' part of the replacement.
  93       *
  94       * @return string Target xpath
  95       */
  96      public function get_to(): string {
  97          return $this->to;
  98      }
  99  }