Merge branch 'MDL-38474-master' of git://github.com/FMCorz/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
DM
30
31use Behat\Behat\Context\Step\Given as Given,
32 Behat\Behat\Context\Step\When as When,
33 Behat\Behat\Context\Step\Then as Then,
34 Behat\Gherkin\Node\TableNode as TableNode,
35 Behat\Mink\Element\NodeElement as NodeElement,
36 Behat\Mink\Exception\ExpectationException as ExpectationException,
37 Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
38
39/**
40 * Forms-related steps definitions.
41 *
42 * @package core
43 * @category test
44 * @copyright 2012 David MonllaĆ³
45 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
46 */
47class behat_forms extends behat_base {
48
49 /**
50 * Presses button with specified id|name|title|alt|value.
51 *
23ebc481 52 * @When /^I press "(?P<button_string>(?:[^"]|\\")*)"$/
1f9ffbdb 53 * @throws ElementNotFoundException Thrown by behat_base::find
46ac40cd 54 * @param string $button
23ebc481
DM
55 */
56 public function press_button($button) {
1f9ffbdb
DM
57
58 // Ensures the button is present.
59 $buttonnode = $this->find_button($button);
60 $buttonnode->press();
23ebc481
DM
61 }
62
63 /**
64 * Fills a moodle form with field/value data.
65 *
23ebc481 66 * @Given /^I fill the moodle form with:$/
1f9ffbdb 67 * @throws ElementNotFoundException Thrown by behat_base::find
23ebc481
DM
68 * @param TableNode $data
69 */
70 public function i_fill_the_moodle_form_with(TableNode $data) {
71
f8f1bdc3
DM
72 // Expand all fields in case we have.
73 $this->expand_all_fields();
74
23ebc481
DM
75 $datahash = $data->getRowsHash();
76
77 // The action depends on the field type.
78 foreach ($datahash as $locator => $value) {
79
a4534dce 80 // Getting the node element pointed by the label.
1f9ffbdb 81 $fieldnode = $this->find_field($locator);
23ebc481
DM
82
83 // Gets the field type from a parent node.
a4534dce 84 $field = behat_field_manager::get_field($fieldnode, $locator, $this->getSession());
23ebc481
DM
85
86 // Delegates to the field class.
23ebc481
DM
87 $field->set_value($value);
88 }
89 }
90
f8f1bdc3
DM
91 /**
92 * Expands all moodleform's fields, including collapsed fieldsets and advanced fields if they are present.
93 * @Given /^I expand all fieldsets$/
94 */
95 public function i_expand_all_fieldsets() {
96 $this->expand_all_fields();
97 }
98
99 /**
100 * Expands all moodle form fieldsets if they exists.
101 *
102 * Externalized from i_expand_all_fields to call it from
103 * other form-related steps without having to use steps-group calls.
104 *
105 * @throws ElementNotFoundException Thrown by behat_base::find_all
106 * @return void
107 */
108 protected function expand_all_fields() {
109
110 // behat_base::find() throws an exception if there are no elements, we should not fail a test because of this.
111 try {
112
113 // Expand fieldsets.
114 $fieldsets = $this->find_all('css', 'fieldset.collapsed.jsprocessed a.fheader');
115
116 // We are supposed to have fieldsets here, otherwise exception.
117
118 // Funny thing about this, with find_all() we specify a pattern and each element matching the pattern is added to the array
119 // with of xpaths with a [0], [1]... sufix, but when we click on an element it does not matches the specified xpath
120 // anymore (is not collapsed) so [1] becomes [0], that's why we always click on the first XPath match, will be always the next one.
121 $iterations = count($fieldsets);
122 for ($i = 0; $i < $iterations; $i++) {
123 $fieldsets[0]->click();
124 }
125
126 } catch (ElementNotFoundException $e) {
127 // We continue if there are not expanded fields.
128 }
129
130 // Different try & catch as we can have expanded fieldsets with advanced fields on them.
131 try {
132
133 // Show all fields.
134 $showmorestr = get_string('showmore', 'form');
135 $showmores = $this->find_all('xpath', "//a[contains(concat(' ', normalize-space(.), ' '), '" . $showmorestr . "')][contains(concat(' ', normalize-space(@class), ' '), ' morelesstoggler')]");
136
137 // We are supposed to have 'show more's here, otherwise exception.
138
139 // Same funny case, after clicking on the element the [1] showmore link becomes the [0].
140 $iterations = count($showmores);
141 for ($i = 0; $i < $iterations; $i++) {
142 $showmores[0]->click();
143 }
144
145 } catch (ElementNotFoundException $e) {
146 // We continue with the test.
147 }
148
149 }
150
23ebc481
DM
151 /**
152 * Fills in form field with specified id|name|label|value.
153 *
23ebc481 154 * @When /^I fill in "(?P<field_string>(?:[^"]|\\")*)" with "(?P<value_string>(?:[^"]|\\")*)"$/
1f9ffbdb 155 * @throws ElementNotFoundException Thrown by behat_base::find
46ac40cd
DM
156 * @param string $field
157 * @param string $value
23ebc481
DM
158 */
159 public function fill_field($field, $value) {
1f9ffbdb
DM
160
161 $fieldnode = $this->find_field($field);
162 $fieldnode->setValue($value);
23ebc481
DM
163 }
164
165 /**
166 * Selects option in select field with specified id|name|label|value.
167 *
23ebc481 168 * @When /^I select "(?P<option_string>(?:[^"]|\\")*)" from "(?P<select_string>(?:[^"]|\\")*)"$/
1f9ffbdb 169 * @throws ElementNotFoundException Thrown by behat_base::find
46ac40cd
DM
170 * @param string $option
171 * @param string $select
23ebc481
DM
172 */
173 public function select_option($option, $select) {
23ebc481 174
1f9ffbdb 175 $selectnode = $this->find_field($select);
23ebc481 176 $selectnode->selectOption($option);
1f9ffbdb
DM
177
178 // Adding a click as Selenium requires it to fire some JS events.
23ebc481
DM
179 $selectnode->click();
180 }
181
182 /**
183 * Checks checkbox with specified id|name|label|value.
184 *
23ebc481 185 * @When /^I check "(?P<option_string>(?:[^"]|\\")*)"$/
1f9ffbdb 186 * @throws ElementNotFoundException Thrown by behat_base::find
46ac40cd 187 * @param string $option
23ebc481
DM
188 */
189 public function check_option($option) {
1f9ffbdb
DM
190
191 $checkboxnode = $this->find_field($option);
192 $checkboxnode->check();
23ebc481
DM
193 }
194
195 /**
196 * Unchecks checkbox with specified id|name|label|value.
197 *
23ebc481 198 * @When /^I uncheck "(?P<option_string>(?:[^"]|\\")*)"$/
1f9ffbdb 199 * @throws ElementNotFoundException Thrown by behat_base::find
46ac40cd 200 * @param string $option
23ebc481
DM
201 */
202 public function uncheck_option($option) {
1f9ffbdb
DM
203
204 $checkboxnode = $this->find_field($option);
205 $checkboxnode->uncheck();
23ebc481
DM
206 }
207
208 /**
209 * Checks that the form element field have the specified value.
210 *
23ebc481 211 * @Then /^the "(?P<field_string>(?:[^"]|\\")*)" field should match "(?P<value_string>(?:[^"]|\\")*)" value$/
1f9ffbdb
DM
212 * @throws ExpectationException
213 * @throws ElementNotFoundException Thrown by behat_base::find
46ac40cd
DM
214 * @param string $locator
215 * @param string $value
23ebc481
DM
216 */
217 public function the_field_should_match_value($locator, $value) {
218
1f9ffbdb 219 $fieldnode = $this->find_field($locator);
23ebc481 220
49d91129 221 // Get the field.
a4534dce 222 $field = behat_field_manager::get_field($fieldnode, $locator, $this->getSession());
49d91129 223 $fieldvalue = $field->get_value();
23ebc481
DM
224
225 // Checks if the provided value matches the current field value.
15c7fb4a 226 if (trim($value) != trim($fieldvalue)) {
23ebc481 227 throw new ExpectationException(
49d91129 228 'The \'' . $locator . '\' value is \'' . $fieldvalue . '\', \'' . $value . '\' expected' ,
23ebc481
DM
229 $this->getSession()
230 );
231 }
232 }
233
234 /**
235 * Checks, that checkbox with specified in|name|label|value is checked.
236 *
23ebc481 237 * @Then /^the "(?P<checkbox_string>(?:[^"]|\\")*)" checkbox should be checked$/
1f9ffbdb 238 * @see Behat\MinkExtension\Context\MinkContext
46ac40cd 239 * @param string $checkbox
23ebc481
DM
240 */
241 public function assert_checkbox_checked($checkbox) {
23ebc481
DM
242 $this->assertSession()->checkboxChecked($checkbox);
243 }
244
245 /**
246 * Checks, that checkbox with specified in|name|label|value is unchecked.
247 *
23ebc481 248 * @Then /^the "(?P<checkbox_string>(?:[^"]|\\")*)" checkbox should not be checked$/
1f9ffbdb 249 * @see Behat\MinkExtension\Context\MinkContext
46ac40cd 250 * @param string $checkbox
23ebc481
DM
251 */
252 public function assert_checkbox_not_checked($checkbox) {
23ebc481
DM
253 $this->assertSession()->checkboxNotChecked($checkbox);
254 }
255
256 /**
257 * Checks, that given select box contains the specified option.
258 *
23ebc481 259 * @Then /^the "(?P<select_string>(?:[^"]|\\")*)" select box should contain "(?P<option_string>(?:[^"]|\\")*)"$/
1f9ffbdb
DM
260 * @throws ExpectationException
261 * @throws ElementNotFoundException Thrown by behat_base::find
23ebc481
DM
262 * @param string $select The select element name
263 * @param string $option The option text/value
264 */
265 public function the_select_box_should_contain($select, $option) {
266
1f9ffbdb 267 $selectnode = $this->find_field($select);
23ebc481
DM
268
269 $regex = '/' . preg_quote($option, '/') . '/ui';
270 if (!preg_match($regex, $selectnode->getText())) {
271 throw new ExpectationException(
272 'The select box "' . $select . '" does not contains the option "' . $option . '"',
273 $this->getSession()
274 );
275 }
276
277 }
278
279 /**
280 * Checks, that given select box does not contain the specified option.
281 *
23ebc481 282 * @Then /^the "(?P<select_string>(?:[^"]|\\")*)" select box should not contain "(?P<option_string>(?:[^"]|\\")*)"$/
1f9ffbdb
DM
283 * @throws ExpectationException
284 * @throws ElementNotFoundException Thrown by behat_base::find
23ebc481
DM
285 * @param string $select The select element name
286 * @param string $option The option text/value
287 */
288 public function the_select_box_should_not_contain($select, $option) {
289
1f9ffbdb 290 $selectnode = $this->find_field($select);
23ebc481
DM
291
292 $regex = '/' . preg_quote($option, '/') . '/ui';
293 if (preg_match($regex, $selectnode->getText())) {
294 throw new ExpectationException(
295 'The select box "' . $select . '" contains the option "' . $option . '"',
296 $this->getSession()
297 );
298 }
299 }
300
23ebc481 301}