MDL-65336 core_message: Behat tests for mute/unmute conversations
[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
AN
67 'list_item' => 'list_item',
68 'question' => 'question',
69 'region' => 'region',
70 'section' => 'section',
71 'table' => 'table',
72 'table_row' => 'table_row',
73 'xpath_element' => 'xpath_element',
74 'form_row' => 'form_row',
e688eab8 75 'group_message_header' => 'group_message_header',
520071f0
AN
76 );
77
78 /**
1e815600 79 * @var array Allowed types when using selector arguments.
520071f0
AN
80 */
81 protected static $allowedselectors = array(
82 'activity' => 'activity',
83 'block' => 'block',
84 'button' => 'button',
85 'checkbox' => 'checkbox',
86 'css_element' => 'css_element',
87 'dialogue' => 'dialogue',
88 'field' => 'field',
89 'fieldset' => 'fieldset',
90 'file' => 'file',
91 'filemanager' => 'filemanager',
65f631c5 92 'group_message' => 'group_message',
d624c135 93 'group_message_conversation' => 'group_message_conversation',
65f631c5
AA
94 'group_message_header' => 'group_message_header',
95 'group_message_member' => 'group_message_member',
96 'group_message_tab' => 'group_message_tab',
e688eab8 97 'group_message_list_area' => 'group_message_list_area',
c961389b 98 'group_message_message_content' => 'group_message_message_content',
8857c715 99 'icon' => 'icon',
520071f0
AN
100 'link' => 'link',
101 'link_or_button' => 'link_or_button',
102 'list_item' => 'list_item',
103 'optgroup' => 'optgroup',
104 'option' => 'option',
105 'question' => 'question',
106 'radio' => 'radio',
107 'region' => 'region',
108 'section' => 'section',
109 'select' => 'select',
110 'table' => 'table',
111 'table_row' => 'table_row',
112 'text' => 'text',
113 'xpath_element' => 'xpath_element',
114 'form_row' => 'form_row',
ab2095fb 115 'autocomplete_selection' => 'autocomplete_selection',
1b63ff10 116 'autocomplete_suggestions' => 'autocomplete_suggestions',
520071f0
AN
117 );
118
119 /**
120 * Behat by default comes with XPath, CSS and named selectors,
121 * named selectors are a mapping between names (like button) and
122 * xpaths that represents that names and includes a placeholder that
123 * will be replaced by the locator. These are Moodle's own xpaths.
124 *
1e815600 125 * @var array XPaths for moodle elements.
520071f0
AN
126 */
127 protected static $moodleselectors = array(
128 'activity' => <<<XPATH
129.//li[contains(concat(' ', normalize-space(@class), ' '), ' activity ')][normalize-space(.) = %locator% ]
130XPATH
131 , 'block' => <<<XPATH
132.//*[@data-block][contains(concat(' ', normalize-space(@class), ' '), concat(' ', %locator%, ' ')) or
3ec69c2e 133 descendant::*[self::h2|self::h3|self::h4|self::h5][normalize-space(.) = %locator%] or
520071f0
AN
134 @aria-label = %locator%]
135XPATH
136 , 'dialogue' => <<<XPATH
137.//div[contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue ') and
138 normalize-space(descendant::div[
139 contains(concat(' ', normalize-space(@class), ' '), ' moodle-dialogue-hd ')
140 ]) = %locator%] |
141.//div[contains(concat(' ', normalize-space(@class), ' '), ' yui-dialog ') and
142 normalize-space(descendant::div[@class='hd']) = %locator%]
434d7869 143 |
dd4ac3d2
AN
144.//div[@data-region='modal' and descendant::*[@data-region='title'] = %locator%]
145 |
146.//div[
147 contains(concat(' ', normalize-space(@class), ' '), ' modal-content ')
148 and
3ec69c2e 149 normalize-space(descendant::*[self::h4 or self::h5][contains(concat(' ', normalize-space(@class), ' '), ' modal-title ')]) = %locator%
dd4ac3d2
AN
150 ]
151 |
152.//div[
153 contains(concat(' ', normalize-space(@class), ' '), ' modal ')
154 and
155 normalize-space(descendant::*[contains(concat(' ', normalize-space(@class), ' '), ' modal-header ')] = %locator%)
156 ]
65f631c5
AA
157XPATH
158 , 'group_message' => <<<XPATH
159 .//*[@data-conversation-id]//img[contains(@alt, %locator%)]/..
160XPATH
d624c135
AA
161 , 'group_message_conversation' => <<<XPATH
162 .//*[@data-region='message-drawer' and contains(., %locator%)]//div[@data-region='content-message-container']
163XPATH
164 , 'group_message_header' => <<<XPATH
e688eab8 165 .//*[@data-region='message-drawer']//div[@data-region='header-container' and contains(., %locator%)]
65f631c5
AA
166XPATH
167 , 'group_message_member' => <<<XPATH
168 .//*[@data-region='message-drawer']//div[@data-region='group-info-content-container']
169 //div[@class='list-group' and not(contains(@class, 'hidden'))]//*[text()[contains(., %locator%)]] |
170 .//*[@data-region='message-drawer']//div[@data-region='group-info-content-container']
9171b588 171 //div[@data-region='empty-message-container' and not(contains(@class, 'hidden')) and contains(., %locator%)]
65f631c5
AA
172XPATH
173 , 'group_message_tab' => <<<XPATH
a58fbd48 174 .//*[@data-region='message-drawer']//button[@data-toggle='collapse' and contains(string(), %locator%)]
e688eab8
AA
175XPATH
176 , 'group_message_list_area' => <<<XPATH
177 .//*[@data-region='message-drawer']//*[contains(@data-region, concat('view-overview-', %locator%))]
c961389b
AA
178XPATH
179 , 'group_message_message_content' => <<<XPATH
180 .//*[@data-region='message-drawer']//*[@data-region='message' and @data-message-id and contains(., %locator%)]
8857c715
DW
181XPATH
182 , 'icon' => <<<XPATH
183.//*[contains(concat(' ', normalize-space(@class), ' '), ' icon ') and ( contains(normalize-space(@title), %locator%))]
520071f0
AN
184XPATH
185 , 'list_item' => <<<XPATH
186.//li[contains(normalize-space(.), %locator%) and not(.//li[contains(normalize-space(.), %locator%)])]
187XPATH
188 , 'question' => <<<XPATH
189.//div[contains(concat(' ', normalize-space(@class), ' '), ' que ')]
190 [contains(div[@class='content']/div[contains(concat(' ', normalize-space(@class), ' '), ' formulation ')], %locator%)]
191XPATH
192 , 'region' => <<<XPATH
193.//*[self::div | self::section | self::aside | self::header | self::footer][./@id = %locator%]
194XPATH
195 , 'section' => <<<XPATH
196.//li[contains(concat(' ', normalize-space(@class), ' '), ' section ')][./descendant::*[self::h3]
197 [normalize-space(.) = %locator%][contains(concat(' ', normalize-space(@class), ' '), ' sectionname ') or
198 contains(concat(' ', normalize-space(@class), ' '), ' section-title ')]] |
199.//div[contains(concat(' ', normalize-space(@class), ' '), ' sitetopic ')]
200 [./descendant::*[self::h2][normalize-space(.) = %locator%] or %locator% = 'frontpage']
201XPATH
202 , 'table' => <<<XPATH
203.//table[(./@id = %locator% or contains(.//caption, %locator%) or contains(.//th, %locator%) or contains(concat(' ', normalize-space(@class), ' '), %locator% ))]
204XPATH
205 , 'table_row' => <<<XPATH
206.//tr[contains(normalize-space(.), %locator%) and not(.//tr[contains(normalize-space(.), %locator%)])]
207XPATH
208 , 'text' => <<<XPATH
209.//*[contains(., %locator%) and not(.//*[contains(., %locator%)])]
210XPATH
211 , 'form_row' => <<<XPATH
212.//*[self::label or self::div[contains(concat(' ', @class, ' '), ' fstaticlabel ')]][contains(., %locator%)]/ancestor::*[contains(concat(' ', @class, ' '), ' fitem ')]
ab2095fb
AG
213XPATH
214 , 'autocomplete_selection' => <<<XPATH
215.//div[contains(concat(' ', normalize-space(@class), ' '), concat(' ', 'form-autocomplete-selection', ' '))]/span[@role='listitem'][contains(normalize-space(.), %locator%)]
1b63ff10
SR
216XPATH
217 , 'autocomplete_suggestions' => <<<XPATH
218.//ul[contains(concat(' ', normalize-space(@class), ' '), concat(' ', 'form-autocomplete-suggestions', ' '))]/li[@role='option'][contains(normalize-space(.), %locator%)]
520071f0
AN
219XPATH
220 );
221
222 protected static $customselectors = [
223 'field' => [
224 'upstream' => <<<XPATH
225.//*
226[%fieldFilterWithPlaceholder%][%notFieldTypeFilter%][%fieldMatchWithPlaceholder%]
227|
228.//label[%tagTextMatch%]//.//*[%fieldFilterWithPlaceholder%][%notFieldTypeFilter%]
229|
230.//*
231[%fieldFilterWithoutPlaceholder%][%notFieldTypeFilter%][%fieldMatchWithoutPlaceholder%]
232|
233.//label[%tagTextMatch%]//.//*[%fieldFilterWithoutPlaceholder%][%notFieldTypeFilter%]
234XPATH
235 ,
236 'filemanager' => <<<XPATH
6ee51d01 237.//*[@data-fieldtype = 'filemanager' or @data-fieldtype = 'filepicker']
d9821755 238 /descendant::input[@id = //label[contains(normalize-space(string(.)), %locator%)]/@for]
ca25005c
AN
239XPATH
240 ,
241 'passwordunmask' => <<<XPATH
242.//*[@data-passwordunmask='wrapper']
737b54fe 243 /descendant::input[@id = %locator% or @id = //label[contains(normalize-space(string(.)), %locator%)]/@for]
520071f0
AN
244XPATH
245 ],
246 ];
247
1e815600
AN
248 /**
249 * Mink comes with a number of named replacements.
250 * Sometimes we want to add our own.
251 *
252 * @var array XPaths for moodle elements.
253 */
254 protected static $customreplacements = [
255 '%buttonMatch%' => [
256 'upstream' => '%idOrNameMatch% or %valueMatch% or %titleMatch%',
257 'aria' => '%ariaLabelMatch%',
258 ],
259 '%ariaLabelMatch%' => [
260 'moodle' => 'contains(./@aria-label, %locator%)',
261 ],
262 ];
263
520071f0
AN
264 /**
265 * Allowed selectors getter.
266 *
267 * @return array
268 */
269 public static function get_allowed_selectors() {
270 return static::$allowedselectors;
271 }
272
273 /**
274 * Allowed text selectors getter.
275 *
276 * @return array
277 */
278 public static function get_allowed_text_selectors() {
279 return static::$allowedtextselectors;
280 }
281}