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 common functions for named selectors. 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 * Common functions for named selectors. 28 * 29 * This has to be a trait, because we need this in both the classes 30 * behat_exact_named_selector and behat_partial_named_selector, and 31 * those classes have to be subclasses of \Behat\Mink\Selector\ExactNamedSelector 32 * and \Behat\Mink\Selector\PartialNamedSelector. This trait is a way achieve 33 * that without duplciated code. 34 * 35 * @package core 36 * @category test 37 * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk> 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 39 */ 40 trait behat_named_selector { 41 42 /** 43 * Registers new XPath selector with specified name. 44 * 45 * @param string $component 46 * @param behat_component_named_selector $selector 47 */ 48 public function register_component_selector(string $component, behat_component_named_selector $selector) { 49 $alias = $selector->get_alias($component); 50 $name = $selector->get_name($component); 51 static::$allowedselectors[$alias] = $name; 52 53 if ($selector->is_text_selector()) { 54 static::$allowedtextselectors[$alias] = $name; 55 } 56 57 // We must use Reflection here. The replacements property is private and cannot be accessed otherwise. 58 // This is due to an API limitation in Mink. 59 $rc = new \ReflectionClass(\Behat\Mink\Selector\NamedSelector::class); 60 $r = $rc->getProperty('replacements'); 61 $r->setAccessible(true); 62 $replacements = $r->getValue($this); 63 64 $selectorxpath = strtr($selector->get_combined_xpath(), $replacements); 65 66 parent::registerNamedXpath($name, $selectorxpath); 67 } 68 69 /** 70 * Registers new XPath selector with specified name. 71 * 72 * @param string $component 73 * @param behat_component_named_replacement $replacement 74 */ 75 public function register_replacement(string $component, behat_component_named_replacement $replacement) { 76 // We must use Reflection here. The replacements property is private and cannot be accessed otherwise. 77 // This is due to an API limitation in Mink. 78 $rc = new \ReflectionClass(\Behat\Mink\Selector\NamedSelector::class); 79 $r = $rc->getProperty('replacements'); 80 $r->setAccessible(true); 81 $existing = $r->getValue($this); 82 83 $from = $replacement->get_from($component); 84 85 if (isset($existing[$from])) { 86 throw new \coding_exception("A named replacement already exists in the partial named selector for '{$from}'. " . 87 "Replacement names must be unique, and should be namespaced to the component"); 88 } 89 90 $translatedto = strtr($replacement->get_to(), $existing); 91 $this->registerReplacement($from, $translatedto); 92 } 93 94 /** 95 * Check whether the specified selector has been deprecated and marked for replacement. 96 * 97 * @param string $selector 98 * @return bool 99 */ 100 public static function is_deprecated_selector(string $selector): bool { 101 return array_key_exists($selector, static::$deprecatedselectors); 102 } 103 104 /** 105 * Fetch the replacement name of a deprecated selector. 106 * 107 * @param string $selector 108 * @return bool 109 */ 110 public static function get_deprecated_replacement(string $selector): ?string { 111 return static::$deprecatedselectors[$selector]; 112 } 113 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body