MDL-68353 form: filepicker label fix
[moodle.git] / lib / behat / classes / partial_named_selector.php
CommitLineData
520071f0
AN
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/**
27 * Moodle selectors manager.
28 *
29 * @package core
30 * @category test
31 * @copyright 2013 David MonllaĆ³
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34class behat_partial_named_selector extends \Behat\Mink\Selector\PartialNamedSelector {
35
61832fae
AN
36 // Use the named selector trait.
37 use behat_named_selector;
38
520071f0
AN
39 /**
40 * Creates selector instance.
41 */
42 public function __construct() {
43 foreach (self::$customselectors as $alias => $selectors) {
44 $this->registerNamedXpath($alias, implode(' | ', $selectors));
45 }
46
47 foreach (static::$moodleselectors as $name => $xpath) {
48 $this->registerNamedXpath($name, $xpath);
49 }
50
1e815600
AN
51 foreach (self::$customreplacements as $from => $tos) {
52 $this->registerReplacement($from, implode(' or ', $tos));
53 }
54
8857c715
DW
55 $this->registerReplacement('%iconMatch%', "(contains(concat(' ', @class, ' '), ' icon ') or name() = 'img')");
56 $this->registerReplacement('%imgAltMatch%', './/*[%iconMatch% and (%altMatch% or %titleMatch%)]');
520071f0
AN
57 parent::__construct();
58 }
59
60 /**
1e815600 61 * @var array Allowed types when using text selectors arguments.
520071f0
AN
62 */
63 protected static $allowedtextselectors = array(
64 'activity' => 'activity',
65 'block' => 'block',
66 'css_element' => 'css_element',
67 'dialogue' => 'dialogue',
68 'fieldset' => 'fieldset',
8857c715 69 'icon' => 'icon',
520071f0
AN
70 'list_item' => 'list_item',
71 'question' => 'question',
72 'region' => 'region',
73 'section' => 'section',
74 'table' => 'table',
75 'table_row' => 'table_row',
76 'xpath_element' => 'xpath_element',
77 'form_row' => 'form_row',
e688eab8 78 'group_message_header' => 'group_message_header',
f0e8f0a5 79 'group_message' => 'group_message',
989f3800 80 'autocomplete' => 'autocomplete',
0d97e610 81 'iframe' => 'iframe',
520071f0
AN
82 );
83
84 /**
1e815600 85 * @var array Allowed types when using selector arguments.
520071f0
AN
86 */
87 protected static $allowedselectors = array(
88 'activity' => 'activity',
89 'block' => 'block',
90 'button' => 'button',
91 'checkbox' => 'checkbox',
92 'css_element' => 'css_element',
93 'dialogue' => 'dialogue',
94 'field' => 'field',
95 'fieldset' => 'fieldset',
96 'file' => 'file',
97 'filemanager' => 'filemanager',
65f631c5 98 'group_message' => 'group_message',
d624c135 99 'group_message_conversation' => 'group_message_conversation',
65f631c5
AA
100 'group_message_header' => 'group_message_header',
101 'group_message_member' => 'group_message_member',
102 'group_message_tab' => 'group_message_tab',
e688eab8 103 'group_message_list_area' => 'group_message_list_area',
c961389b 104 'group_message_message_content' => 'group_message_message_content',
f0e8f0a5 105 'icon_container' => 'icon_container',
8857c715 106 'icon' => 'icon',
520071f0
AN
107 'link' => 'link',
108 'link_or_button' => 'link_or_button',
109 'list_item' => 'list_item',
110 'optgroup' => 'optgroup',
111 'option' => 'option',
112 'question' => 'question',
113 'radio' => 'radio',
114 'region' => 'region',
115 'section' => 'section',
116 'select' => 'select',
117 'table' => 'table',
118 'table_row' => 'table_row',
119 'text' => 'text',
120 'xpath_element' => 'xpath_element',
121 'form_row' => 'form_row',
ab2095fb 122 'autocomplete_selection' => 'autocomplete_selection',
1b63ff10 123 'autocomplete_suggestions' => 'autocomplete_suggestions',
989f3800 124 'autocomplete' => 'autocomplete',
0d97e610 125 'iframe' => 'iframe',
520071f0
AN
126 );
127
128 /**
129 * Behat by default comes with XPath, CSS and named selectors,
130 * named selectors are a mapping between names (like button) and
131 * xpaths that represents that names and includes a placeholder that
132 * will be replaced by the locator. These are Moodle's own xpaths.
133 *
1e815600 134 * @var array XPaths for moodle elements.
520071f0
AN
135 */
136 protected static $moodleselectors = array(
137 'activity' => <<<XPATH
138.//li[contains(concat(' ', normalize-space(@class), ' '), ' activity ')][normalize-space(.) = %locator% ]
139XPATH
140 , 'block' => <<<XPATH
141.//*[@data-block][contains(concat(' ', normalize-space(@class), ' '), concat(' ', %locator%, ' ')) or
3ec69c2e 142 descendant::*[self::h2|self::h3|self::h4|self::h5][normalize-space(.) = %locator%] or
520071f0
AN
143 @aria-label = %locator%]
144XPATH
145 , 'dialogue' => <<<XPATH
146.//div[contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue ') and
147 normalize-space(descendant::div[
148 contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue-hd ')
149 ]) = %locator%] |
150.//div[contains(concat(' ', normalize-space(@class), ' '), ' yui-dialog ') and
151 normalize-space(descendant::div[@class='hd']) = %locator%]
434d7869 152 |
dd4ac3d2
AN
153.//div[@data-region='modal' and descendant::*[@data-region='title'] = %locator%]
154 |
155.//div[
156 contains(concat(' ', normalize-space(@class), ' '), ' modal-content ')
157 and
3ec69c2e 158 normalize-space(descendant::*[self::h4 or self::h5][contains(concat(' ', normalize-space(@class), ' '), ' modal-title ')]) = %locator%
dd4ac3d2
AN
159 ]
160 |
161.//div[
162 contains(concat(' ', normalize-space(@class), ' '), ' modal ')
163 and
164 normalize-space(descendant::*[contains(concat(' ', normalize-space(@class), ' '), ' modal-header ')] = %locator%)
165 ]
65f631c5
AA
166XPATH
167 , 'group_message' => <<<XPATH
168 .//*[@data-conversation-id]//img[contains(@alt, %locator%)]/..
169XPATH
d624c135
AA
170 , 'group_message_conversation' => <<<XPATH
171 .//*[@data-region='message-drawer' and contains(., %locator%)]//div[@data-region='content-message-container']
172XPATH
173 , 'group_message_header' => <<<XPATH
f0e8f0a5 174 .//*[@data-region='message-drawer']//div[@data-region='header-content' and contains(., %locator%)]
65f631c5
AA
175XPATH
176 , 'group_message_member' => <<<XPATH
177 .//*[@data-region='message-drawer']//div[@data-region='group-info-content-container']
178 //div[@class='list-group' and not(contains(@class, 'hidden'))]//*[text()[contains(., %locator%)]] |
179 .//*[@data-region='message-drawer']//div[@data-region='group-info-content-container']
9171b588 180 //div[@data-region='empty-message-container' and not(contains(@class, 'hidden')) and contains(., %locator%)]
65f631c5
AA
181XPATH
182 , 'group_message_tab' => <<<XPATH
a58fbd48 183 .//*[@data-region='message-drawer']//button[@data-toggle='collapse' and contains(string(), %locator%)]
e688eab8
AA
184XPATH
185 , 'group_message_list_area' => <<<XPATH
186 .//*[@data-region='message-drawer']//*[contains(@data-region, concat('view-overview-', %locator%))]
c961389b
AA
187XPATH
188 , 'group_message_message_content' => <<<XPATH
189 .//*[@data-region='message-drawer']//*[@data-region='message' and @data-message-id and contains(., %locator%)]
f0e8f0a5
AA
190XPATH
191 , 'icon_container' => <<<XPATH
192 .//span[contains(@data-region, concat(%locator%,'-icon-container'))]
8857c715
DW
193XPATH
194 , 'icon' => <<<XPATH
195.//*[contains(concat(' ', normalize-space(@class), ' '), ' icon ') and ( contains(normalize-space(@title), %locator%))]
520071f0
AN
196XPATH
197 , 'list_item' => <<<XPATH
198.//li[contains(normalize-space(.), %locator%) and not(.//li[contains(normalize-space(.), %locator%)])]
199XPATH
200 , 'question' => <<<XPATH
201.//div[contains(concat(' ', normalize-space(@class), ' '), ' que ')]
202 [contains(div[@class='content']/div[contains(concat(' ', normalize-space(@class), ' '), ' formulation ')], %locator%)]
203XPATH
204 , 'region' => <<<XPATH
205.//*[self::div | self::section | self::aside | self::header | self::footer][./@id = %locator%]
206XPATH
207 , 'section' => <<<XPATH
208.//li[contains(concat(' ', normalize-space(@class), ' '), ' section ')][./descendant::*[self::h3]
209 [normalize-space(.) = %locator%][contains(concat(' ', normalize-space(@class), ' '), ' sectionname ') or
210 contains(concat(' ', normalize-space(@class), ' '), ' section-title ')]] |
211.//div[contains(concat(' ', normalize-space(@class), ' '), ' sitetopic ')]
212 [./descendant::*[self::h2][normalize-space(.) = %locator%] or %locator% = 'frontpage']
213XPATH
214 , 'table' => <<<XPATH
215.//table[(./@id = %locator% or contains(.//caption, %locator%) or contains(.//th, %locator%) or contains(concat(' ', normalize-space(@class), ' '), %locator% ))]
216XPATH
217 , 'table_row' => <<<XPATH
218.//tr[contains(normalize-space(.), %locator%) and not(.//tr[contains(normalize-space(.), %locator%)])]
219XPATH
220 , 'text' => <<<XPATH
221.//*[contains(., %locator%) and not(.//*[contains(., %locator%)])]
222XPATH
223 , 'form_row' => <<<XPATH
224.//*[self::label or self::div[contains(concat(' ', @class, ' '), ' fstaticlabel ')]][contains(., %locator%)]/ancestor::*[contains(concat(' ', @class, ' '), ' fitem ')]
ab2095fb
AG
225XPATH
226 , 'autocomplete_selection' => <<<XPATH
227.//div[contains(concat(' ', normalize-space(@class), ' '), concat(' ', 'form-autocomplete-selection', ' '))]/span[@role='listitem'][contains(normalize-space(.), %locator%)]
1b63ff10
SR
228XPATH
229 , 'autocomplete_suggestions' => <<<XPATH
230.//ul[contains(concat(' ', normalize-space(@class), ' '), concat(' ', 'form-autocomplete-suggestions', ' '))]/li[@role='option'][contains(normalize-space(.), %locator%)]
989f3800
AN
231XPATH
232 , 'autocomplete' => <<<XPATH
233.//descendant::input[@id = //label[contains(normalize-space(string(.)), %locator%)]/@for]/ancestor::*[@data-fieldtype = 'autocomplete']
0d97e610
AA
234XPATH
235 , 'iframe' => <<<XPATH
236.//iframe[contains(concat(' ', normalize-space(@class), ' '), %locator% )]
520071f0
AN
237XPATH
238 );
239
240 protected static $customselectors = [
241 'field' => [
242 'upstream' => <<<XPATH
243.//*
244[%fieldFilterWithPlaceholder%][%notFieldTypeFilter%][%fieldMatchWithPlaceholder%]
245|
246.//label[%tagTextMatch%]//.//*[%fieldFilterWithPlaceholder%][%notFieldTypeFilter%]
247|
248.//*
249[%fieldFilterWithoutPlaceholder%][%notFieldTypeFilter%][%fieldMatchWithoutPlaceholder%]
250|
251.//label[%tagTextMatch%]//.//*[%fieldFilterWithoutPlaceholder%][%notFieldTypeFilter%]
252XPATH
253 ,
254 'filemanager' => <<<XPATH
6ee51d01 255.//*[@data-fieldtype = 'filemanager' or @data-fieldtype = 'filepicker']
d9821755 256 /descendant::input[@id = //label[contains(normalize-space(string(.)), %locator%)]/@for]
ca25005c
AN
257XPATH
258 ,
259 'passwordunmask' => <<<XPATH
260.//*[@data-passwordunmask='wrapper']
737b54fe 261 /descendant::input[@id = %locator% or @id = //label[contains(normalize-space(string(.)), %locator%)]/@for]
520071f0
AN
262XPATH
263 ],
264 ];
265
1e815600
AN
266 /**
267 * Mink comes with a number of named replacements.
268 * Sometimes we want to add our own.
269 *
270 * @var array XPaths for moodle elements.
271 */
272 protected static $customreplacements = [
273 '%buttonMatch%' => [
274 'upstream' => '%idOrNameMatch% or %valueMatch% or %titleMatch%',
275 'aria' => '%ariaLabelMatch%',
276 ],
277 '%ariaLabelMatch%' => [
278 'moodle' => 'contains(./@aria-label, %locator%)',
279 ],
280 ];
281
61832fae
AN
282 /** @var List of deprecated selectors */
283 protected static $deprecatedselectors = [
5c783f14
AN
284 'group_message' => 'core_message > Message',
285 'group_message_member' => 'core_message > Message member',
286 'group_message_tab' => 'core_message > Message tab',
287 'group_message_list_area' => 'core_message > Message list area',
288 'group_message_message_content' => 'core_message > Message content',
61832fae
AN
289 ];
290
520071f0
AN
291 /**
292 * Allowed selectors getter.
293 *
294 * @return array
295 */
296 public static function get_allowed_selectors() {
297 return static::$allowedselectors;
298 }
299
300 /**
301 * Allowed text selectors getter.
302 *
303 * @return array
304 */
305 public static function get_allowed_text_selectors() {
306 return static::$allowedtextselectors;
307 }
308}