Merge branch 'MDL-61949-master-privacy-upgrade-txt' of git://github.com/mudrd8mz...
[moodle.git] / lib / behat / behat_field_manager.php
blobb98f843799bf42cdf3074670c12ec36d5af57a5e
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.
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/>.
17 /**
18 * Form fields helper.
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
26 // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
28 use Behat\Mink\Session as Session,
29 Behat\Mink\Element\NodeElement as NodeElement,
30 Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException,
31 Behat\MinkExtension\Context\RawMinkContext as RawMinkContext;
33 /**
34 * Helper to interact with form fields.
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
41 class behat_field_manager {
43 /**
44 * Gets an instance of the form field from it's label
46 * @param string $label
47 * @param RawMinkContext $context
48 * @return behat_form_field
50 public static function get_form_field_from_label($label, RawMinkContext $context) {
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 // The DOM node.
55 $fieldnode = $context->find_field($label);
57 // The behat field manager.
58 return self::get_form_field($fieldnode, $context->getSession());
61 /**
62 * Gets an instance of the form field.
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.
70 * @param NodeElement $fieldnode
71 * @param Session $session The behat browser session
72 * @return behat_form_field
74 public static function get_form_field(NodeElement $fieldnode, Session $session) {
76 // Get the field type if is part of a moodleform.
77 if (self::is_moodleform_field($fieldnode)) {
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';
86 // If is not a moodleforms field use the base field type.
87 if (empty($type)) {
88 $type = 'field';
91 return self::get_field_instance($type, $fieldnode, $session);
94 /**
95 * Returns the appropiate behat_form_field according to the provided type.
97 * It defaults to behat_form_field.
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
104 public static function get_field_instance($type, NodeElement $fieldnode, Session $session) {
106 global $CFG;
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;
115 $classname = 'behat_form_' . $type;
117 // Fallsback on the type guesser if nothing specific exists.
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';
124 // Returns the instance.
125 require_once($classpath);
126 return new $classname($session, $fieldnode);
130 * Guesses a basic field type and returns it.
132 * This method is intended to detect HTML form fields when no
133 * moodleform-specific elements have been detected.
135 * @param NodeElement $fieldnode
136 * @param Session $session
137 * @return string|bool The field type or false.
139 public static function guess_field_type(NodeElement $fieldnode, Session $session) {
141 // Textareas are considered text based elements.
142 $tagname = strtolower($fieldnode->getTagName());
143 if ($tagname == 'textarea') {
145 // If there is an iframe with $id + _ifr there a TinyMCE editor loaded.
146 $xpath = '//div[@id="' . $fieldnode->getAttribute('id') . 'editable"]';
147 if ($session->getPage()->find('xpath', $xpath)) {
148 return 'editor';
150 return 'textarea';
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;
172 } else if ($tagname == 'select') {
173 // Select tag.
174 return 'select';
177 // We can not provide a closer field type.
178 return false;
182 * Detects when the field is a moodleform field type.
184 * Note that there are fields inside moodleforms that are not
185 * moodleform element; this method can not detect this, this will
186 * be managed by get_field_node_type, after failing to find the form
187 * element element type.
189 * @param NodeElement $fieldnode
190 * @return bool
192 protected static function is_moodleform_field(NodeElement $fieldnode) {
194 // We already waited when getting the NodeElement and we don't want an exception if it's not part of a moodleform.
195 $parentformfound = $fieldnode->find('xpath',
196 "/ancestor::form[contains(concat(' ', normalize-space(@class), ' '), ' mform ')]"
199 return ($parentformfound != false);
203 * Recursive method to find the field type.
205 * Depending on the field the felement class node is in a level or in another. We
206 * look recursively for a parent node with a 'felement' class to find the field type.
208 * @param NodeElement $fieldnode The current node.
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.
212 protected static function get_field_node_type(NodeElement $fieldnode, Session $session) {
214 // Special handling for availability field which requires custom JavaScript.
215 if ($fieldnode->getAttribute('name') === 'availabilityconditionsjson') {
216 return 'availability';
219 if ($fieldnode->getTagName() == 'html') {
220 return false;
223 // If the type is explictly set on the element pointed to by the label - use it.
224 if ($type = $fieldnode->getParent()->getAttribute('data-fieldtype')) {
225 if ($type == 'tags') {
226 return 'autocomplete';
228 return $type;
231 if (!empty($fieldnode->find('xpath', '/ancestor::*[@data-passwordunmaskid]'))) {
232 return 'passwordunmask';
235 // We look for a parent node with 'felement' class.
236 if ($class = $fieldnode->getParent()->getAttribute('class')) {
238 if (strstr($class, 'felement') != false) {
239 // Remove 'felement f' from class value.
240 return substr($class, 10);
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;
249 return self::get_field_node_type($fieldnode->getParent(), $session);
253 * Gets an instance of the form field.
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.
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
265 * @param string $locator
266 * @param Session $session The behat browser session
267 * @return behat_form_field
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);
273 return self::get_form_field($fieldnode, $session);
277 * Recursive method to find the field type.
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.
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.
286 * @param string $locator
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.
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);
294 return self::get_field_node_type($fieldnode, $session);