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