1 |
efrain |
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 |
$replacements = $r->getValue($this);
|
|
|
62 |
|
|
|
63 |
$selectorxpath = strtr($selector->get_combined_xpath(), $replacements);
|
|
|
64 |
|
|
|
65 |
parent::registerNamedXpath($name, $selectorxpath);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* Registers new XPath selector with specified name.
|
|
|
70 |
*
|
|
|
71 |
* @param string $component
|
|
|
72 |
* @param behat_component_named_replacement $replacement
|
|
|
73 |
*/
|
|
|
74 |
public function register_replacement(string $component, behat_component_named_replacement $replacement) {
|
|
|
75 |
// We must use Reflection here. The replacements property is private and cannot be accessed otherwise.
|
|
|
76 |
// This is due to an API limitation in Mink.
|
|
|
77 |
$rc = new \ReflectionClass(\Behat\Mink\Selector\NamedSelector::class);
|
|
|
78 |
$r = $rc->getProperty('replacements');
|
|
|
79 |
$existing = $r->getValue($this);
|
|
|
80 |
|
|
|
81 |
$from = $replacement->get_from($component);
|
|
|
82 |
|
|
|
83 |
if (isset($existing[$from])) {
|
|
|
84 |
throw new \coding_exception("A named replacement already exists in the partial named selector for '{$from}'. " .
|
|
|
85 |
"Replacement names must be unique, and should be namespaced to the component");
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
$translatedto = strtr($replacement->get_to(), $existing);
|
|
|
89 |
$this->registerReplacement($from, $translatedto);
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* Check whether the specified selector has been deprecated and marked for replacement.
|
|
|
94 |
*
|
|
|
95 |
* @param string $selector
|
|
|
96 |
* @return bool
|
|
|
97 |
*/
|
|
|
98 |
public static function is_deprecated_selector(string $selector): bool {
|
|
|
99 |
return array_key_exists($selector, static::$deprecatedselectors);
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* Fetch the replacement name of a deprecated selector.
|
|
|
104 |
*
|
|
|
105 |
* @param string $selector
|
|
|
106 |
* @return bool
|
|
|
107 |
*/
|
|
|
108 |
public static function get_deprecated_replacement(string $selector): ?string {
|
|
|
109 |
return static::$deprecatedselectors[$selector];
|
|
|
110 |
}
|
|
|
111 |
}
|