MDL-69107 form_autocomplete: Rewrite item selection
[moodle.git] / lib / behat / behat_field_manager.php
CommitLineData
a4534dce
DM
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 * Form fields helper.
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// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
27
28use Behat\Mink\Session as Session,
af4830a2
DM
29 Behat\Mink\Element\NodeElement as NodeElement,
30 Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException,
31 Behat\MinkExtension\Context\RawMinkContext as RawMinkContext;
a4534dce
DM
32
33/**
34 * Helper to interact with form fields.
35 *
36 * @package core
37 * @category test
38 * @copyright 2013 David MonllaĆ³
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
41class behat_field_manager {
42
af4830a2
DM
43 /**
44 * Gets an instance of the form field from it's label
45 *
46 * @param string $label
47 * @param RawMinkContext $context
48 * @return behat_form_field
49 */
50 public static function get_form_field_from_label($label, RawMinkContext $context) {
af4830a2
DM
51 // There are moodle form elements that are not directly related with
52 // a basic HTML form field, we should also take care of them.
520071f0
AN
53 // The DOM node.
54 $fieldnode = $context->find_field($label);
af4830a2
DM
55
56 // The behat field manager.
57 return self::get_form_field($fieldnode, $context->getSession());
58 }
59
a4534dce
DM
60 /**
61 * Gets an instance of the form field.
62 *
63 * Not all the fields are part of a moodle form, in this
64 * cases it fallsback to the generic form field. Also note
65 * that this generic field type is using a generic setValue()
66 * method from the Behat API, which is not always good to set
67 * the value of form elements.
68 *
69 * @param NodeElement $fieldnode
a4534dce
DM
70 * @param Session $session The behat browser session
71 * @return behat_form_field
72 */
051e9663 73 public static function get_form_field(NodeElement $fieldnode, Session $session) {
a4534dce
DM
74
75 // Get the field type if is part of a moodleform.
76 if (self::is_moodleform_field($fieldnode)) {
e24188c8
RW
77 // This might go out of scope, finding element beyond the dom and fail. So fallback to guessing type.
78 try {
79 $type = self::get_field_node_type($fieldnode, $session);
80 } catch (WebDriver\Exception\InvalidSelector $e) {
81 $type = 'field';
82 }
a4534dce
DM
83 }
84
85 // If is not a moodleforms field use the base field type.
86 if (empty($type)) {
87 $type = 'field';
88 }
89
3a4d76ac
DM
90 return self::get_field_instance($type, $fieldnode, $session);
91 }
92
93 /**
94 * Returns the appropiate behat_form_field according to the provided type.
95 *
96 * It defaults to behat_form_field.
97 *
98 * @param string $type The field type (checkbox, date_selector, text...)
99 * @param NodeElement $fieldnode
100 * @param Session $session The behat session
101 * @return behat_form_field
102 */
103 public static function get_field_instance($type, NodeElement $fieldnode, Session $session) {
3a4d76ac
DM
104 global $CFG;
105
8aff0eec
DM
106 // If the field is not part of a moodleform, we should still try to find out
107 // which field type are we dealing with.
108 if ($type == 'field' &&
109 $guessedtype = self::guess_field_type($fieldnode, $session)) {
110 $type = $guessedtype;
111 }
112
a4534dce
DM
113 $classname = 'behat_form_' . $type;
114
8aff0eec 115 // Fallsback on the type guesser if nothing specific exists.
a4534dce
DM
116 $classpath = $CFG->libdir . '/behat/form_field/' . $classname . '.php';
117 if (!file_exists($classpath)) {
118 $classname = 'behat_form_field';
119 $classpath = $CFG->libdir . '/behat/form_field/' . $classname . '.php';
120 }
121
122 // Returns the instance.
123 require_once($classpath);
124 return new $classname($session, $fieldnode);
125 }
126
8aff0eec
DM
127 /**
128 * Guesses a basic field type and returns it.
129 *
130 * This method is intended to detect HTML form fields when no
131 * moodleform-specific elements have been detected.
132 *
133 * @param NodeElement $fieldnode
134 * @param Session $session
135 * @return string|bool The field type or false.
136 */
137 public static function guess_field_type(NodeElement $fieldnode, Session $session) {
138
139 // Textareas are considered text based elements.
140 $tagname = strtolower($fieldnode->getTagName());
141 if ($tagname == 'textarea') {
142
143 // If there is an iframe with $id + _ifr there a TinyMCE editor loaded.
786ced99 144 $xpath = '//div[@id="' . $fieldnode->getAttribute('id') . 'editable"]';
8aff0eec
DM
145 if ($session->getPage()->find('xpath', $xpath)) {
146 return 'editor';
147 }
148 return 'textarea';
149
150 } else if ($tagname == 'input') {
151 $type = $fieldnode->getAttribute('type');
152 switch ($type) {
153 case 'text':
f07d3b71
AN
154 if ($fieldtype = $fieldnode->getAttribute('data-fieldtype')) {
155 return self::normalise_fieldtype($fieldtype);
156 }
157 return 'text';
8aff0eec
DM
158 case 'password':
159 case 'email':
160 case 'file':
161 return 'text';
162 case 'checkbox':
163 return 'checkbox';
164 break;
165 case 'radio':
166 return 'radio';
167 break;
168 default:
169 // Here we return false because all text-based
170 // fields should be included in the first switch case.
171 return false;
172 }
173
174 } else if ($tagname == 'select') {
175 // Select tag.
176 return 'select';
fd71823e
AN
177 } else if ($tagname == 'span') {
178 if ($fieldnode->hasAttribute('data-inplaceeditable') && $fieldnode->getAttribute('data-inplaceeditable')) {
179 return 'inplaceeditable';
180 }
8aff0eec
DM
181 }
182
183 // We can not provide a closer field type.
184 return false;
185 }
186
a4534dce
DM
187 /**
188 * Detects when the field is a moodleform field type.
189 *
190 * Note that there are fields inside moodleforms that are not
191 * moodleform element; this method can not detect this, this will
051e9663 192 * be managed by get_field_node_type, after failing to find the form
a4534dce
DM
193 * element element type.
194 *
195 * @param NodeElement $fieldnode
196 * @return bool
197 */
198 protected static function is_moodleform_field(NodeElement $fieldnode) {
199
200 // We already waited when getting the NodeElement and we don't want an exception if it's not part of a moodleform.
8aff0eec 201 $parentformfound = $fieldnode->find('xpath',
8aff0eec
DM
202 "/ancestor::form[contains(concat(' ', normalize-space(@class), ' '), ' mform ')]"
203 );
a4534dce
DM
204
205 return ($parentformfound != false);
206 }
207
208 /**
209 * Recursive method to find the field type.
210 *
051e9663 211 * Depending on the field the felement class node is in a level or in another. We
a4534dce
DM
212 * look recursively for a parent node with a 'felement' class to find the field type.
213 *
214 * @param NodeElement $fieldnode The current node.
a4534dce
DM
215 * @param Session $session The behat browser session
216 * @return mixed A NodeElement if we continue looking for the element type and String or false when we are done.
217 */
051e9663 218 protected static function get_field_node_type(NodeElement $fieldnode, Session $session) {
a4534dce 219
002ccdcf 220 // Special handling for availability field which requires custom JavaScript.
221 if ($fieldnode->getAttribute('name') === 'availabilityconditionsjson') {
222 return 'availability';
223 }
224
63e4df60
DW
225 if ($fieldnode->getTagName() == 'html') {
226 return false;
9b9afffe
RT
227 }
228
63e4df60 229 // If the type is explictly set on the element pointed to by the label - use it.
06dc3a6c
PD
230 $fieldtype = $fieldnode->getAttribute('data-fieldtype');
231 if ($fieldtype) {
0e09bc93 232 return self::normalise_fieldtype($fieldtype);
63e4df60
DW
233 }
234
ca25005c
AN
235 if (!empty($fieldnode->find('xpath', '/ancestor::*[@data-passwordunmaskid]'))) {
236 return 'passwordunmask';
237 }
238
06dc3a6c
PD
239 // Fetch the parentnode only once.
240 $parentnode = $fieldnode->getParent();
a4534dce 241
0e09bc93
AN
242 // Check the parent fieldtype before we check classes.
243 $fieldtype = $parentnode->getAttribute('data-fieldtype');
244 if ($fieldtype) {
245 return self::normalise_fieldtype($fieldtype);
246 }
247
06dc3a6c
PD
248 // We look for a parent node with 'felement' class.
249 if ($class = $parentnode->getAttribute('class')) {
a4534dce
DM
250 if (strstr($class, 'felement') != false) {
251 // Remove 'felement f' from class value.
252 return substr($class, 10);
253 }
254
255 // Stop propagation through the DOM, if it does not have a felement is not part of a moodle form.
256 if (strstr($class, 'fcontainer') != false) {
257 return false;
258 }
259 }
260
06dc3a6c 261 return self::get_field_node_type($parentnode, $session);
051e9663
DM
262 }
263
0e09bc93
AN
264 /**
265 * Normalise the field type.
266 *
267 * @param string $fieldtype
268 * @return string
269 */
270 protected static function normalise_fieldtype(string $fieldtype): string {
271 if ($fieldtype === 'tags') {
272 return 'autocomplete';
273 }
274
275 return $fieldtype;
276 }
277
051e9663
DM
278 /**
279 * Gets an instance of the form field.
280 *
281 * Not all the fields are part of a moodle form, in this
282 * cases it fallsback to the generic form field. Also note
283 * that this generic field type is using a generic setValue()
284 * method from the Behat API, which is not always good to set
285 * the value of form elements.
286 *
287 * @deprecated since Moodle 2.6 MDL-39634 - please do not use this function any more.
288 * @todo MDL-XXXXX This will be deleted in Moodle 2.8
289 * @see behat_field_manager::get_form_field()
290 * @param NodeElement $fieldnode
af4830a2 291 * @param string $locator
051e9663
DM
292 * @param Session $session The behat browser session
293 * @return behat_form_field
294 */
295 public static function get_field(NodeElement $fieldnode, $locator, Session $session) {
296 debugging('Function behat_field_manager::get_field() is deprecated, ' .
297 'please use function behat_field_manager::get_form_field() instead', DEBUG_DEVELOPER);
298
299 return self::get_form_field($fieldnode, $session);
300 }
301
302 /**
303 * Recursive method to find the field type.
304 *
305 * Depending on the field the felement class node is in a level or in another. We
306 * look recursively for a parent node with a 'felement' class to find the field type.
307 *
308 * @deprecated since Moodle 2.6 MDL-39634 - please do not use this function any more.
309 * @todo MDL-XXXXX This will be deleted in Moodle 2.8
310 * @see behat_field_manager::get_field_node_type()
311 * @param NodeElement $fieldnode The current node.
af4830a2 312 * @param string $locator
051e9663
DM
313 * @param Session $session The behat browser session
314 * @return mixed A NodeElement if we continue looking for the element type and String or false when we are done.
315 */
316 protected static function get_node_type(NodeElement $fieldnode, $locator, Session $session) {
317 debugging('Function behat_field_manager::get_node_type() is deprecated, ' .
318 'please use function behat_field_manager::get_field_node_type() instead', DEBUG_DEVELOPER);
319
320 return self::get_field_node_type($fieldnode, $session);
a4534dce 321 }
a4534dce 322}