MDL-61234 tool_usertours: Rewrite feature to work with all themes
[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
36 /**
37 * Creates selector instance.
38 */
39 public function __construct() {
40 foreach (self::$customselectors as $alias => $selectors) {
41 $this->registerNamedXpath($alias, implode(' | ', $selectors));
42 }
43
44 foreach (static::$moodleselectors as $name => $xpath) {
45 $this->registerNamedXpath($name, $xpath);
46 }
47
1e815600
AN
48 foreach (self::$customreplacements as $from => $tos) {
49 $this->registerReplacement($from, implode(' or ', $tos));
50 }
51
8857c715
DW
52 $this->registerReplacement('%iconMatch%', "(contains(concat(' ', @class, ' '), ' icon ') or name() = 'img')");
53 $this->registerReplacement('%imgAltMatch%', './/*[%iconMatch% and (%altMatch% or %titleMatch%)]');
520071f0
AN
54 parent::__construct();
55 }
56
57 /**
1e815600 58 * @var array Allowed types when using text selectors arguments.
520071f0
AN
59 */
60 protected static $allowedtextselectors = array(
61 'activity' => 'activity',
62 'block' => 'block',
63 'css_element' => 'css_element',
64 'dialogue' => 'dialogue',
65 'fieldset' => 'fieldset',
8857c715 66 'icon' => 'icon',
520071f0 67 'list_item' => 'list_item',
f05c1013
RW
68 'message_area_region' => 'message_area_region',
69 'message_area_region_content' => 'message_area_region_content',
520071f0
AN
70 'question' => 'question',
71 'region' => 'region',
72 'section' => 'section',
73 'table' => 'table',
74 'table_row' => 'table_row',
75 'xpath_element' => 'xpath_element',
76 'form_row' => 'form_row',
77 );
78
79 /**
1e815600 80 * @var array Allowed types when using selector arguments.
520071f0
AN
81 */
82 protected static $allowedselectors = array(
83 'activity' => 'activity',
84 'block' => 'block',
85 'button' => 'button',
86 'checkbox' => 'checkbox',
87 'css_element' => 'css_element',
88 'dialogue' => 'dialogue',
89 'field' => 'field',
90 'fieldset' => 'fieldset',
91 'file' => 'file',
92 'filemanager' => 'filemanager',
8857c715 93 'icon' => 'icon',
520071f0
AN
94 'link' => 'link',
95 'link_or_button' => 'link_or_button',
96 'list_item' => 'list_item',
f05c1013
RW
97 'message_area_action' => 'message_area_action',
98 'message_area_region' => 'message_area_region',
99 'message_area_region_content' => 'message_area_region_content',
520071f0
AN
100 'optgroup' => 'optgroup',
101 'option' => 'option',
102 'question' => 'question',
103 'radio' => 'radio',
104 'region' => 'region',
105 'section' => 'section',
106 'select' => 'select',
107 'table' => 'table',
108 'table_row' => 'table_row',
109 'text' => 'text',
110 'xpath_element' => 'xpath_element',
111 'form_row' => 'form_row',
ab2095fb 112 'autocomplete_selection' => 'autocomplete_selection',
520071f0
AN
113 );
114
115 /**
116 * Behat by default comes with XPath, CSS and named selectors,
117 * named selectors are a mapping between names (like button) and
118 * xpaths that represents that names and includes a placeholder that
119 * will be replaced by the locator. These are Moodle's own xpaths.
120 *
1e815600 121 * @var array XPaths for moodle elements.
520071f0
AN
122 */
123 protected static $moodleselectors = array(
124 'activity' => <<<XPATH
125.//li[contains(concat(' ', normalize-space(@class), ' '), ' activity ')][normalize-space(.) = %locator% ]
126XPATH
127 , 'block' => <<<XPATH
128.//*[@data-block][contains(concat(' ', normalize-space(@class), ' '), concat(' ', %locator%, ' ')) or
129 descendant::*[self::h2|self::h3][normalize-space(.) = %locator%] or
130 @aria-label = %locator%]
131XPATH
132 , 'dialogue' => <<<XPATH
133.//div[contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue ') and
134 normalize-space(descendant::div[
135 contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue-hd ')
136 ]) = %locator%] |
137.//div[contains(concat(' ', normalize-space(@class), ' '), ' yui-dialog ') and
138 normalize-space(descendant::div[@class='hd']) = %locator%]
434d7869 139 |
26872a80
K
140.//div[@data-region='modal' and descendant::*[@data-region='title'] = %locator%] |
141.//div[contains(concat(' ', normalize-space(@class), ' '), ' modal-content ') and
142 normalize-space(descendant::h4[
143 contains(concat(' ', normalize-space(@class), ' '), ' modal-title ')
144 ]) = %locator%]
8857c715
DW
145XPATH
146 , 'icon' => <<<XPATH
147.//*[contains(concat(' ', normalize-space(@class), ' '), ' icon ') and ( contains(normalize-space(@title), %locator%))]
520071f0
AN
148XPATH
149 , 'list_item' => <<<XPATH
150.//li[contains(normalize-space(.), %locator%) and not(.//li[contains(normalize-space(.), %locator%)])]
151XPATH
152 , 'question' => <<<XPATH
153.//div[contains(concat(' ', normalize-space(@class), ' '), ' que ')]
154 [contains(div[@class='content']/div[contains(concat(' ', normalize-space(@class), ' '), ' formulation ')], %locator%)]
155XPATH
156 , 'region' => <<<XPATH
157.//*[self::div | self::section | self::aside | self::header | self::footer][./@id = %locator%]
158XPATH
159 , 'section' => <<<XPATH
160.//li[contains(concat(' ', normalize-space(@class), ' '), ' section ')][./descendant::*[self::h3]
161 [normalize-space(.) = %locator%][contains(concat(' ', normalize-space(@class), ' '), ' sectionname ') or
162 contains(concat(' ', normalize-space(@class), ' '), ' section-title ')]] |
163.//div[contains(concat(' ', normalize-space(@class), ' '), ' sitetopic ')]
164 [./descendant::*[self::h2][normalize-space(.) = %locator%] or %locator% = 'frontpage']
165XPATH
166 , 'table' => <<<XPATH
167.//table[(./@id = %locator% or contains(.//caption, %locator%) or contains(.//th, %locator%) or contains(concat(' ', normalize-space(@class), ' '), %locator% ))]
168XPATH
169 , 'table_row' => <<<XPATH
170.//tr[contains(normalize-space(.), %locator%) and not(.//tr[contains(normalize-space(.), %locator%)])]
171XPATH
172 , 'text' => <<<XPATH
173.//*[contains(., %locator%) and not(.//*[contains(., %locator%)])]
174XPATH
175 , 'form_row' => <<<XPATH
176.//*[self::label or self::div[contains(concat(' ', @class, ' '), ' fstaticlabel ')]][contains(., %locator%)]/ancestor::*[contains(concat(' ', @class, ' '), ' fitem ')]
f05c1013
RW
177XPATH
178 , 'message_area_region' => <<<XPATH
179.//div[@data-region='messaging-area']/descendant::*[@data-region = %locator%]
180XPATH
181 , 'message_area_region_content' => <<<XPATH
182.//div[@data-region='messaging-area']/descendant::*[@data-region-content = %locator%]
183XPATH
184 , 'message_area_action' => <<<XPATH
185.//div[@data-region='messaging-area']/descendant::*[@data-action = %locator%]
ab2095fb
AG
186XPATH
187 , 'autocomplete_selection' => <<<XPATH
188.//div[contains(concat(' ', normalize-space(@class), ' '), concat(' ', 'form-autocomplete-selection', ' '))]/span[@role='listitem'][contains(normalize-space(.), %locator%)]
520071f0
AN
189XPATH
190 );
191
192 protected static $customselectors = [
193 'field' => [
194 'upstream' => <<<XPATH
195.//*
196[%fieldFilterWithPlaceholder%][%notFieldTypeFilter%][%fieldMatchWithPlaceholder%]
197|
198.//label[%tagTextMatch%]//.//*[%fieldFilterWithPlaceholder%][%notFieldTypeFilter%]
199|
200.//*
201[%fieldFilterWithoutPlaceholder%][%notFieldTypeFilter%][%fieldMatchWithoutPlaceholder%]
202|
203.//label[%tagTextMatch%]//.//*[%fieldFilterWithoutPlaceholder%][%notFieldTypeFilter%]
204XPATH
205 ,
206 'filemanager' => <<<XPATH
6ee51d01 207.//*[@data-fieldtype = 'filemanager' or @data-fieldtype = 'filepicker']
d9821755 208 /descendant::input[@id = //label[contains(normalize-space(string(.)), %locator%)]/@for]
ca25005c
AN
209XPATH
210 ,
211 'passwordunmask' => <<<XPATH
212.//*[@data-passwordunmask='wrapper']
737b54fe 213 /descendant::input[@id = %locator% or @id = //label[contains(normalize-space(string(.)), %locator%)]/@for]
520071f0
AN
214XPATH
215 ],
216 ];
217
1e815600
AN
218 /**
219 * Mink comes with a number of named replacements.
220 * Sometimes we want to add our own.
221 *
222 * @var array XPaths for moodle elements.
223 */
224 protected static $customreplacements = [
225 '%buttonMatch%' => [
226 'upstream' => '%idOrNameMatch% or %valueMatch% or %titleMatch%',
227 'aria' => '%ariaLabelMatch%',
228 ],
229 '%ariaLabelMatch%' => [
230 'moodle' => 'contains(./@aria-label, %locator%)',
231 ],
232 ];
233
520071f0
AN
234 /**
235 * Allowed selectors getter.
236 *
237 * @return array
238 */
239 public static function get_allowed_selectors() {
240 return static::$allowedselectors;
241 }
242
243 /**
244 * Allowed text selectors getter.
245 *
246 * @return array
247 */
248 public static function get_allowed_text_selectors() {
249 return static::$allowedtextselectors;
250 }
251}