Merge branch 'wip-mdl-55258-m' of https://github.com/rajeshtaneja/moodle
[moodle.git] / lib / tests / behat / behat_forms.php
CommitLineData
23ebc481
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 * Steps definitions related with forms.
19 *
20 * @package core
21 * @category test
22 * @copyright 2012 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
28require_once(__DIR__ . '/../../../lib/behat/behat_base.php');
a4534dce 29require_once(__DIR__ . '/../../../lib/behat/behat_field_manager.php');
23ebc481 30
42ad096f 31use Behat\Gherkin\Node\TableNode as TableNode,
9776f3dc 32 Behat\Gherkin\Node\PyStringNode as PyStringNode,
23ebc481
DM
33 Behat\Mink\Exception\ExpectationException as ExpectationException,
34 Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
35
36/**
37 * Forms-related steps definitions.
38 *
39 * @package core
40 * @category test
41 * @copyright 2012 David MonllaĆ³
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 */
44class behat_forms extends behat_base {
45
46 /**
47 * Presses button with specified id|name|title|alt|value.
48 *
23ebc481 49 * @When /^I press "(?P<button_string>(?:[^"]|\\")*)"$/
1f9ffbdb 50 * @throws ElementNotFoundException Thrown by behat_base::find
46ac40cd 51 * @param string $button
23ebc481
DM
52 */
53 public function press_button($button) {
1f9ffbdb
DM
54
55 // Ensures the button is present.
56 $buttonnode = $this->find_button($button);
ddf36bf5
RT
57 // Focus on button to ensure it is in viewport, before pressing it.
58 if ($this->running_javascript()) {
59 $buttonnode->focus();
60 }
1f9ffbdb 61 $buttonnode->press();
23ebc481
DM
62 }
63
1ef6a5e3
RT
64 /**
65 * Press button with specified id|name|title|alt|value and switch to main window.
66 *
67 * @When /^I press "(?P<button_string>(?:[^"]|\\")*)" and switch to main window$/
68 * @throws ElementNotFoundException Thrown by behat_base::find
69 * @param string $button
70 */
71 public function press_button_and_switch_to_main_window($button) {
72 // Ensures the button is present, before pressing.
73 $buttonnode = $this->find_button($button);
74 $buttonnode->press();
75
76 // Switch to main window.
77 $this->getSession()->switchToWindow(behat_general::MAIN_WINDOW_NAME);
78 }
79
23ebc481 80 /**
a5254141 81 * Fills a form with field/value data. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps.
23ebc481 82 *
a5254141 83 * @Given /^I set the following fields to these values:$/
1f9ffbdb 84 * @throws ElementNotFoundException Thrown by behat_base::find
23ebc481
DM
85 * @param TableNode $data
86 */
a5254141 87 public function i_set_the_following_fields_to_these_values(TableNode $data) {
23ebc481 88
f8f1bdc3
DM
89 // Expand all fields in case we have.
90 $this->expand_all_fields();
91
23ebc481
DM
92 $datahash = $data->getRowsHash();
93
94 // The action depends on the field type.
95 foreach ($datahash as $locator => $value) {
a5254141 96 $this->set_field_value($locator, $value);
23ebc481
DM
97 }
98 }
99
f8f1bdc3
DM
100 /**
101 * Expands all moodleform's fields, including collapsed fieldsets and advanced fields if they are present.
102 * @Given /^I expand all fieldsets$/
103 */
104 public function i_expand_all_fieldsets() {
105 $this->expand_all_fields();
106 }
107
108 /**
109 * Expands all moodle form fieldsets if they exists.
110 *
111 * Externalized from i_expand_all_fields to call it from
112 * other form-related steps without having to use steps-group calls.
113 *
114 * @throws ElementNotFoundException Thrown by behat_base::find_all
115 * @return void
116 */
117 protected function expand_all_fields() {
20153f63
RT
118 // Expand only if JS mode, else not needed.
119 if (!$this->running_javascript()) {
120 return;
121 }
f8f1bdc3 122
74c78e74
DM
123 // We already know that we waited for the DOM and the JS to be loaded, even the editor
124 // so, we will use the reduced timeout as it is a common task and we should save time.
f8f1bdc3
DM
125 try {
126
8cda2e6f 127 // Expand fieldsets link.
74c78e74 128 $xpath = "//div[@class='collapsible-actions']" .
8cda2e6f 129 "/descendant::a[contains(concat(' ', @class, ' '), ' collapseexpand ')]" .
74c78e74
DM
130 "[not(contains(concat(' ', @class, ' '), ' collapse-all '))]";
131 $collapseexpandlink = $this->find('xpath', $xpath, false, false, self::REDUCED_TIMEOUT);
8cda2e6f 132 $collapseexpandlink->click();
f8f1bdc3
DM
133
134 } catch (ElementNotFoundException $e) {
74c78e74
DM
135 // The behat_base::find() method throws an exception if there are no elements,
136 // we should not fail a test because of this. We continue if there are not expandable fields.
f8f1bdc3
DM
137 }
138
139 // Different try & catch as we can have expanded fieldsets with advanced fields on them.
140 try {
141
8cda2e6f
DM
142 // Expand all fields xpath.
143 $showmorexpath = "//a[normalize-space(.)='" . get_string('showmore', 'form') . "']" .
144 "[contains(concat(' ', normalize-space(@class), ' '), ' moreless-toggler')]";
f8f1bdc3 145
8cda2e6f 146 // We don't wait here as we already waited when getting the expand fieldsets links.
74c78e74
DM
147 if (!$showmores = $this->getSession()->getPage()->findAll('xpath', $showmorexpath)) {
148 return;
149 }
f8f1bdc3 150
8cda2e6f
DM
151 // Funny thing about this, with findAll() we specify a pattern and each element matching the pattern is added to the array
152 // with of xpaths with a [0], [1]... sufix, but when we click on an element it does not matches the specified xpath
153 // anymore (now is a "Show less..." link) so [1] becomes [0], that's why we always click on the first XPath match,
154 // will be always the next one.
f8f1bdc3
DM
155 $iterations = count($showmores);
156 for ($i = 0; $i < $iterations; $i++) {
157 $showmores[0]->click();
158 }
159
160 } catch (ElementNotFoundException $e) {
161 // We continue with the test.
162 }
163
164 }
165
01e8bfd7
JO
166 /**
167 * Sets the field to wwwroot plus the given path. Include the first slash.
168 *
169 * @Given /^I set the field "(?P<field_string>(?:[^"]|\\")*)" to local url "(?P<field_path_string>(?:[^"]|\\")*)"$/
170 * @throws ElementNotFoundException Thrown by behat_base::find
171 * @param string $field
172 * @param string $path
173 * @return void
174 */
175 public function i_set_the_field_to_local_url($field, $path) {
176 global $CFG;
177 $this->set_field_value($field, $CFG->wwwroot . $path);
178 }
179
23ebc481 180 /**
a5254141 181 * Sets the specified value to the field.
23ebc481 182 *
decf1e14 183 * @Given /^I set the field "(?P<field_string>(?:[^"]|\\")*)" to "(?P<field_value_string>(?:[^"]|\\")*)"$/
1f9ffbdb 184 * @throws ElementNotFoundException Thrown by behat_base::find
46ac40cd
DM
185 * @param string $field
186 * @param string $value
a5254141 187 * @return void
23ebc481 188 */
a5254141 189 public function i_set_the_field_to($field, $value) {
c3f1e953 190 $this->set_field_value($field, $value);
23ebc481
DM
191 }
192
0cf72524
MG
193 /**
194 * Press the key in the field to trigger the javascript keypress event
195 *
196 * Note that the character key will not actually be typed in the input field
197 *
198 * @Given /^I press key "(?P<key_string>(?:[^"]|\\")*)" in the field "(?P<field_string>(?:[^"]|\\")*)"$/
199 * @throws ElementNotFoundException Thrown by behat_base::find
200 * @param string $key either char-code or character itself,
201 * may optionally be prefixed with ctrl-, alt-, shift- or meta-
202 * @param string $field
203 * @return void
204 */
205 public function i_press_key_in_the_field($key, $field) {
206 if (!$this->running_javascript()) {
8a6900ff 207 throw new DriverException('Key press step is not available with Javascript disabled');
0cf72524
MG
208 }
209 $fld = behat_field_manager::get_form_field_from_label($field, $this);
210 $modifier = null;
211 $char = $key;
212 if (preg_match('/-/', $key)) {
213 list($modifier, $char) = preg_split('/-/', $key, 2);
214 }
215 if (is_numeric($char)) {
216 $char = (int)$char;
217 }
218 $fld->key_press($char, $modifier);
219 }
220
9776f3dc
JO
221 /**
222 * Sets the specified value to the field.
223 *
224 * @Given /^I set the field "(?P<field_string>(?:[^"]|\\")*)" to multiline$/
225 * @throws ElementNotFoundException Thrown by behat_base::find
226 * @param string $field
227 * @param PyStringNode $value
228 * @return void
229 */
230 public function i_set_the_field_to_multiline($field, PyStringNode $value) {
231 $this->set_field_value($field, (string)$value);
232 }
233
6c396b6b
RT
234 /**
235 * Sets the specified value to the field with xpath.
236 *
237 * @Given /^I set the field with xpath "(?P<fieldxpath_string>(?:[^"]|\\")*)" to "(?P<field_value_string>(?:[^"]|\\")*)"$/
238 * @throws ElementNotFoundException Thrown by behat_base::find
239 * @param string $field
240 * @param string $value
241 * @return void
242 */
243 public function i_set_the_field_with_xpath_to($fieldxpath, $value) {
f0edd9d8 244 $fieldNode = $this->find('xpath', $fieldxpath);
6c396b6b
RT
245 $field = behat_field_manager::get_form_field($fieldNode, $this->getSession());
246 $field->set_value($value);
247 }
248
23ebc481 249 /**
a5254141 250 * Checks, the field matches the value. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps.
23ebc481 251 *
decf1e14 252 * @Then /^the field "(?P<field_string>(?:[^"]|\\")*)" matches value "(?P<field_value_string>(?:[^"]|\\")*)"$/
1f9ffbdb 253 * @throws ElementNotFoundException Thrown by behat_base::find
a5254141 254 * @param string $field
46ac40cd 255 * @param string $value
a5254141 256 * @return void
23ebc481 257 */
a5254141 258 public function the_field_matches_value($field, $value) {
23ebc481 259
49d91129 260 // Get the field.
af4830a2 261 $formfield = behat_field_manager::get_form_field_from_label($field, $this);
23ebc481
DM
262
263 // Checks if the provided value matches the current field value.
a5254141
DM
264 if (!$formfield->matches($value)) {
265 $fieldvalue = $formfield->get_value();
23ebc481 266 throw new ExpectationException(
a5254141 267 'The \'' . $field . '\' value is \'' . $fieldvalue . '\', \'' . $value . '\' expected' ,
d9fddb0b
AH
268 $this->getSession()
269 );
270 }
271 }
272
273 /**
267033a9
T
274 * Checks, the field does not match the value. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps.
275 *
276 * @Then /^the field "(?P<field_string>(?:[^"]|\\")*)" does not match value "(?P<field_value_string>(?:[^"]|\\")*)"$/
277 * @throws ExpectationException
278 * @throws ElementNotFoundException Thrown by behat_base::find
279 * @param string $field
280 * @param string $value
281 * @return void
282 */
283 public function the_field_does_not_match_value($field, $value) {
284
285 // Get the field.
286 $formfield = behat_field_manager::get_form_field_from_label($field, $this);
287
288 // Checks if the provided value matches the current field value.
289 if ($formfield->matches($value)) {
290 throw new ExpectationException(
291 'The \'' . $field . '\' value matches \'' . $value . '\' and it should not match it' ,
292 $this->getSession()
293 );
294 }
295 }
296
297 /**
298 * Checks, the field matches the value.
d9fddb0b 299 *
267033a9
T
300 * @Then /^the field with xpath "(?P<xpath_string>(?:[^"]|\\")*)" matches value "(?P<field_value_string>(?:[^"]|\\")*)"$/
301 * @throws ExpectationException
d9fddb0b
AH
302 * @throws ElementNotFoundException Thrown by behat_base::find
303 * @param string $fieldxpath
304 * @param string $value
305 * @return void
306 */
307 public function the_field_with_xpath_matches_value($fieldxpath, $value) {
308
309 // Get the field.
310 $fieldnode = $this->find('xpath', $fieldxpath);
311 $formfield = behat_field_manager::get_form_field($fieldnode, $this->getSession());
312
313 // Checks if the provided value matches the current field value.
314 if (!$formfield->matches($value)) {
315 $fieldvalue = $formfield->get_value();
316 throw new ExpectationException(
317 'The \'' . $fieldxpath . '\' value is \'' . $fieldvalue . '\', \'' . $value . '\' expected' ,
23ebc481
DM
318 $this->getSession()
319 );
320 }
321 }
322
c3f1e953 323 /**
267033a9 324 * Checks, the field does not match the value.
c3f1e953 325 *
267033a9 326 * @Then /^the field with xpath "(?P<xpath_string>(?:[^"]|\\")*)" does not match value "(?P<field_value_string>(?:[^"]|\\")*)"$/
c3f1e953
DM
327 * @throws ExpectationException
328 * @throws ElementNotFoundException Thrown by behat_base::find
267033a9 329 * @param string $fieldxpath
c3f1e953
DM
330 * @param string $value
331 * @return void
332 */
267033a9 333 public function the_field_with_xpath_does_not_match_value($fieldxpath, $value) {
c3f1e953 334
c3f1e953 335 // Get the field.
267033a9
T
336 $fieldnode = $this->find('xpath', $fieldxpath);
337 $formfield = behat_field_manager::get_form_field($fieldnode, $this->getSession());
c3f1e953
DM
338
339 // Checks if the provided value matches the current field value.
a5254141 340 if ($formfield->matches($value)) {
c3f1e953 341 throw new ExpectationException(
267033a9 342 'The \'' . $fieldxpath . '\' value matches \'' . $value . '\' and it should not match it' ,
c3f1e953
DM
343 $this->getSession()
344 );
345 }
346 }
347
348 /**
a5254141 349 * Checks, the provided field/value matches. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps.
c3f1e953
DM
350 *
351 * @Then /^the following fields match these values:$/
352 * @throws ExpectationException
a5254141 353 * @param TableNode $data Pairs of | field | value |
c3f1e953
DM
354 */
355 public function the_following_fields_match_these_values(TableNode $data) {
356
357 // Expand all fields in case we have.
358 $this->expand_all_fields();
359
360 $datahash = $data->getRowsHash();
361
362 // The action depends on the field type.
363 foreach ($datahash as $locator => $value) {
a5254141 364 $this->the_field_matches_value($locator, $value);
c3f1e953
DM
365 }
366 }
367
368 /**
decf1e14 369 * Checks that the provided field/value pairs don't match. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps.
c3f1e953
DM
370 *
371 * @Then /^the following fields do not match these values:$/
372 * @throws ExpectationException
a5254141 373 * @param TableNode $data Pairs of | field | value |
c3f1e953
DM
374 */
375 public function the_following_fields_do_not_match_these_values(TableNode $data) {
376
377 // Expand all fields in case we have.
378 $this->expand_all_fields();
379
380 $datahash = $data->getRowsHash();
381
382 // The action depends on the field type.
383 foreach ($datahash as $locator => $value) {
384 $this->the_field_does_not_match_value($locator, $value);
385 }
386 }
387
23ebc481
DM
388 /**
389 * Checks, that given select box contains the specified option.
390 *
23ebc481 391 * @Then /^the "(?P<select_string>(?:[^"]|\\")*)" select box should contain "(?P<option_string>(?:[^"]|\\")*)"$/
1f9ffbdb
DM
392 * @throws ExpectationException
393 * @throws ElementNotFoundException Thrown by behat_base::find
23ebc481 394 * @param string $select The select element name
5f66d46e
EL
395 * @param string $option The option text/value. Plain value or comma separated
396 * values if multiple. Commas in multiple values escaped with backslash.
23ebc481
DM
397 */
398 public function the_select_box_should_contain($select, $option) {
399
1f9ffbdb 400 $selectnode = $this->find_field($select);
5f66d46e
EL
401 $multiple = $selectnode->hasAttribute('multiple');
402 $optionsarr = array(); // Array of passed value/text options to test.
23ebc481 403
5f66d46e
EL
404 if ($multiple) {
405 // Can pass multiple comma separated, with valuable commas escaped with backslash.
406 foreach (preg_replace('/\\\,/', ',', preg_split('/(?<!\\\),/', $option)) as $opt) {
407 $optionsarr[] = trim($opt);
408 }
409 } else {
410 // Only one option has been passed.
411 $optionsarr[] = trim($option);
412 }
413
414 // Now get all the values and texts in the select.
415 $options = $selectnode->findAll('xpath', '//option');
416 $values = array();
417 foreach ($options as $opt) {
418 $values[trim($opt->getValue())] = trim($opt->getText());
23ebc481
DM
419 }
420
5f66d46e
EL
421 foreach ($optionsarr as $opt) {
422 // Verify every option is a valid text or value.
423 if (!in_array($opt, $values) && !array_key_exists($opt, $values)) {
424 throw new ExpectationException(
425 'The select box "' . $select . '" does not contain the option "' . $opt . '"',
426 $this->getSession()
427 );
428 }
429 }
23ebc481
DM
430 }
431
432 /**
433 * Checks, that given select box does not contain the specified option.
434 *
23ebc481 435 * @Then /^the "(?P<select_string>(?:[^"]|\\")*)" select box should not contain "(?P<option_string>(?:[^"]|\\")*)"$/
1f9ffbdb
DM
436 * @throws ExpectationException
437 * @throws ElementNotFoundException Thrown by behat_base::find
23ebc481 438 * @param string $select The select element name
5f66d46e
EL
439 * @param string $option The option text/value. Plain value or comma separated
440 * values if multiple. Commas in multiple values escaped with backslash.
23ebc481
DM
441 */
442 public function the_select_box_should_not_contain($select, $option) {
443
1f9ffbdb 444 $selectnode = $this->find_field($select);
5f66d46e
EL
445 $multiple = $selectnode->hasAttribute('multiple');
446 $optionsarr = array(); // Array of passed value/text options to test.
23ebc481 447
5f66d46e
EL
448 if ($multiple) {
449 // Can pass multiple comma separated, with valuable commas escaped with backslash.
450 foreach (preg_replace('/\\\,/', ',', preg_split('/(?<!\\\),/', $option)) as $opt) {
451 $optionsarr[] = trim($opt);
452 }
453 } else {
454 // Only one option has been passed.
455 $optionsarr[] = trim($option);
456 }
457
458 // Now get all the values and texts in the select.
459 $options = $selectnode->findAll('xpath', '//option');
460 $values = array();
461 foreach ($options as $opt) {
462 $values[trim($opt->getValue())] = trim($opt->getText());
463 }
464
465 foreach ($optionsarr as $opt) {
466 // Verify every option is not a valid text or value.
467 if (in_array($opt, $values) || array_key_exists($opt, $values)) {
468 throw new ExpectationException(
469 'The select box "' . $select . '" contains the option "' . $opt . '"',
470 $this->getSession()
471 );
472 }
473 }
474 }
475
476 /**
c3f1e953 477 * Generic field setter.
5f66d46e 478 *
c3f1e953
DM
479 * Internal API method, a generic *I set "VALUE" to "FIELD" field*
480 * could be created based on it.
5f66d46e 481 *
c3f1e953
DM
482 * @param string $fieldlocator The pointer to the field, it will depend on the field type.
483 * @param string $value
484 * @return void
5f66d46e 485 */
c3f1e953 486 protected function set_field_value($fieldlocator, $value) {
5f66d46e 487
c3f1e953
DM
488 // We delegate to behat_form_field class, it will
489 // guess the type properly as it is a select tag.
af4830a2 490 $field = behat_field_manager::get_form_field_from_label($fieldlocator, $this);
c3f1e953 491 $field->set_value($value);
23ebc481
DM
492 }
493
2b964e1d
AN
494 /**
495 * Select a value from single select and redirect.
496 *
497 * @Given /^I select "(?P<singleselect_option_string>(?:[^"]|\\")*)" from the "(?P<singleselect_name_string>(?:[^"]|\\")*)" singleselect$/
498 */
499 public function i_select_from_the_singleselect($option, $singleselect) {
eb9ca848
RT
500
501 $this->execute('behat_forms::i_set_the_field_to', array($this->escape($singleselect), $this->escape($option)));
2b964e1d
AN
502
503 if (!$this->running_javascript()) {
bf4c3ee0 504 // Press button in the specified select container.
bc7a5953
RT
505 $containerxpath = "//div[" .
506 "(contains(concat(' ', normalize-space(@class), ' '), ' singleselect ') " .
507 "or contains(concat(' ', normalize-space(@class), ' '), ' urlselect ')".
508 ") and (
509 .//label[contains(normalize-space(string(.)), '" . $singleselect . "')] " .
510 "or .//select[(./@name='" . $singleselect . "' or ./@id='". $singleselect . "')]" .
511 ")]";
bf4c3ee0 512
eb9ca848
RT
513 $this->execute('behat_general::i_click_on_in_the',
514 array(get_string('go'), "button", $containerxpath, "xpath_element")
515 );
2b964e1d 516 }
2b964e1d
AN
517 }
518
23ebc481 519}