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