[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 28 require_once (__DIR__ . '/../../../lib/behat/behat_base.php'); 29 require_once (__DIR__ . '/../../../lib/behat/behat_field_manager.php'); 30 31 use 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 */ 47 class behat_forms extends behat_base { 48 49 /** 50 * Presses button with specified id|name|title|alt|value. 51 * 52 * @When /^I press "(?P<button_string>(?:[^"]|\\")*)"$/ 53 * @throws ElementNotFoundException Thrown by behat_base::find 54 * @param string $button 55 */ 56 public function press_button($button) { 57 58 // Ensures the button is present. 59 $buttonnode = $this->find_button($button); 60 $buttonnode->press(); 61 } 62 63 /** 64 * Fills a form with field/value data. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps. 65 * 66 * @Given /^I set the following fields to these values:$/ 67 * @throws ElementNotFoundException Thrown by behat_base::find 68 * @param TableNode $data 69 */ 70 public function i_set_the_following_fields_to_these_values(TableNode $data) { 71 72 // Expand all fields in case we have. 73 $this->expand_all_fields(); 74 75 $datahash = $data->getRowsHash(); 76 77 // The action depends on the field type. 78 foreach ($datahash as $locator => $value) { 79 $this->set_field_value($locator, $value); 80 } 81 } 82 83 /** 84 * Expands all moodleform's fields, including collapsed fieldsets and advanced fields if they are present. 85 * @Given /^I expand all fieldsets$/ 86 */ 87 public function i_expand_all_fieldsets() { 88 $this->expand_all_fields(); 89 } 90 91 /** 92 * Expands all moodle form fieldsets if they exists. 93 * 94 * Externalized from i_expand_all_fields to call it from 95 * other form-related steps without having to use steps-group calls. 96 * 97 * @throws ElementNotFoundException Thrown by behat_base::find_all 98 * @return void 99 */ 100 protected function expand_all_fields() { 101 102 // We already know that we waited for the DOM and the JS to be loaded, even the editor 103 // so, we will use the reduced timeout as it is a common task and we should save time. 104 try { 105 106 // Expand fieldsets link. 107 $xpath = "//div[@class='collapsible-actions']" . 108 "/descendant::a[contains(concat(' ', @class, ' '), ' collapseexpand ')]" . 109 "[not(contains(concat(' ', @class, ' '), ' collapse-all '))]"; 110 $collapseexpandlink = $this->find('xpath', $xpath, false, false, self::REDUCED_TIMEOUT); 111 $collapseexpandlink->click(); 112 113 } catch (ElementNotFoundException $e) { 114 // The behat_base::find() method throws an exception if there are no elements, 115 // we should not fail a test because of this. We continue if there are not expandable fields. 116 } 117 118 // Different try & catch as we can have expanded fieldsets with advanced fields on them. 119 try { 120 121 // Expand all fields xpath. 122 $showmorexpath = "//a[normalize-space(.)='" . get_string('showmore', 'form') . "']" . 123 "[contains(concat(' ', normalize-space(@class), ' '), ' moreless-toggler')]"; 124 125 // We don't wait here as we already waited when getting the expand fieldsets links. 126 if (!$showmores = $this->getSession()->getPage()->findAll('xpath', $showmorexpath)) { 127 return; 128 } 129 130 // Funny thing about this, with findAll() we specify a pattern and each element matching the pattern is added to the array 131 // with of xpaths with a [0], [1]... sufix, but when we click on an element it does not matches the specified xpath 132 // anymore (now is a "Show less..." link) so [1] becomes [0], that's why we always click on the first XPath match, 133 // will be always the next one. 134 $iterations = count($showmores); 135 for ($i = 0; $i < $iterations; $i++) { 136 $showmores[0]->click(); 137 } 138 139 } catch (ElementNotFoundException $e) { 140 // We continue with the test. 141 } 142 143 } 144 145 /** 146 * Sets the specified value to the field. 147 * 148 * @Given /^I set the field "(?P<field_string>(?:[^"]|\\")*)" to "(?P<field_value_string>(?:[^"]|\\")*)"$/ 149 * @throws ElementNotFoundException Thrown by behat_base::find 150 * @param string $field 151 * @param string $value 152 * @return void 153 */ 154 public function i_set_the_field_to($field, $value) { 155 $this->set_field_value($field, $value); 156 } 157 158 /** 159 * Sets the specified value to the field with xpath. 160 * 161 * @Given /^I set the field with xpath "(?P<fieldxpath_string>(?:[^"]|\\")*)" to "(?P<field_value_string>(?:[^"]|\\")*)"$/ 162 * @throws ElementNotFoundException Thrown by behat_base::find 163 * @param string $field 164 * @param string $value 165 * @return void 166 */ 167 public function i_set_the_field_with_xpath_to($fieldxpath, $value) { 168 try { 169 $fieldNode = $this->find('xpath', $fieldxpath); 170 } catch (\Behat\Mink\Exception\ElementNotFoundException $e) { 171 throw new ElementNotFoundException('Field with xpath ' . $fieldxpath . 'not found, so can\'t be set'); 172 } 173 $field = behat_field_manager::get_form_field($fieldNode, $this->getSession()); 174 $field->set_value($value); 175 } 176 177 /** 178 * Checks, the field matches the value. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps. 179 * 180 * @Then /^the field "(?P<field_string>(?:[^"]|\\")*)" matches value "(?P<field_value_string>(?:[^"]|\\")*)"$/ 181 * @throws ElementNotFoundException Thrown by behat_base::find 182 * @param string $field 183 * @param string $value 184 * @return void 185 */ 186 public function the_field_matches_value($field, $value) { 187 188 // Get the field. 189 $formfield = behat_field_manager::get_form_field_from_label($field, $this); 190 191 // Checks if the provided value matches the current field value. 192 if (!$formfield->matches($value)) { 193 $fieldvalue = $formfield->get_value(); 194 throw new ExpectationException( 195 'The \'' . $field . '\' value is \'' . $fieldvalue . '\', \'' . $value . '\' expected' , 196 $this->getSession() 197 ); 198 } 199 } 200 201 /** 202 * Checks, the field does not match the value. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps. 203 * 204 * @Then /^the field "(?P<field_string>(?:[^"]|\\")*)" does not match value "(?P<field_value_string>(?:[^"]|\\")*)"$/ 205 * @throws ExpectationException 206 * @throws ElementNotFoundException Thrown by behat_base::find 207 * @param string $field 208 * @param string $value 209 * @return void 210 */ 211 public function the_field_does_not_match_value($field, $value) { 212 213 // Get the field. 214 $formfield = behat_field_manager::get_form_field_from_label($field, $this); 215 216 // Checks if the provided value matches the current field value. 217 if ($formfield->matches($value)) { 218 $fieldvalue = $formfield->get_value(); 219 throw new ExpectationException( 220 'The \'' . $field . '\' value matches \'' . $value . '\' and it should not match it' , 221 $this->getSession() 222 ); 223 } 224 } 225 226 /** 227 * Checks, the provided field/value matches. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps. 228 * 229 * @Then /^the following fields match these values:$/ 230 * @throws ExpectationException 231 * @param TableNode $data Pairs of | field | value | 232 */ 233 public function the_following_fields_match_these_values(TableNode $data) { 234 235 // Expand all fields in case we have. 236 $this->expand_all_fields(); 237 238 $datahash = $data->getRowsHash(); 239 240 // The action depends on the field type. 241 foreach ($datahash as $locator => $value) { 242 $this->the_field_matches_value($locator, $value); 243 } 244 } 245 246 /** 247 * Checks that the provided field/value pairs don't match. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps. 248 * 249 * @Then /^the following fields do not match these values:$/ 250 * @throws ExpectationException 251 * @param TableNode $data Pairs of | field | value | 252 */ 253 public function the_following_fields_do_not_match_these_values(TableNode $data) { 254 255 // Expand all fields in case we have. 256 $this->expand_all_fields(); 257 258 $datahash = $data->getRowsHash(); 259 260 // The action depends on the field type. 261 foreach ($datahash as $locator => $value) { 262 $this->the_field_does_not_match_value($locator, $value); 263 } 264 } 265 266 /** 267 * Checks, that given select box contains the specified option. 268 * 269 * @Then /^the "(?P<select_string>(?:[^"]|\\")*)" select box should contain "(?P<option_string>(?:[^"]|\\")*)"$/ 270 * @throws ExpectationException 271 * @throws ElementNotFoundException Thrown by behat_base::find 272 * @param string $select The select element name 273 * @param string $option The option text/value. Plain value or comma separated 274 * values if multiple. Commas in multiple values escaped with backslash. 275 */ 276 public function the_select_box_should_contain($select, $option) { 277 278 $selectnode = $this->find_field($select); 279 $multiple = $selectnode->hasAttribute('multiple'); 280 $optionsarr = array(); // Array of passed value/text options to test. 281 282 if ($multiple) { 283 // Can pass multiple comma separated, with valuable commas escaped with backslash. 284 foreach (preg_replace('/\\\,/', ',', preg_split('/(?<!\\\),/', $option)) as $opt) { 285 $optionsarr[] = trim($opt); 286 } 287 } else { 288 // Only one option has been passed. 289 $optionsarr[] = trim($option); 290 } 291 292 // Now get all the values and texts in the select. 293 $options = $selectnode->findAll('xpath', '//option'); 294 $values = array(); 295 foreach ($options as $opt) { 296 $values[trim($opt->getValue())] = trim($opt->getText()); 297 } 298 299 foreach ($optionsarr as $opt) { 300 // Verify every option is a valid text or value. 301 if (!in_array($opt, $values) && !array_key_exists($opt, $values)) { 302 throw new ExpectationException( 303 'The select box "' . $select . '" does not contain the option "' . $opt . '"', 304 $this->getSession() 305 ); 306 } 307 } 308 } 309 310 /** 311 * Checks, that given select box does not contain the specified option. 312 * 313 * @Then /^the "(?P<select_string>(?:[^"]|\\")*)" select box should not contain "(?P<option_string>(?:[^"]|\\")*)"$/ 314 * @throws ExpectationException 315 * @throws ElementNotFoundException Thrown by behat_base::find 316 * @param string $select The select element name 317 * @param string $option The option text/value. Plain value or comma separated 318 * values if multiple. Commas in multiple values escaped with backslash. 319 */ 320 public function the_select_box_should_not_contain($select, $option) { 321 322 $selectnode = $this->find_field($select); 323 $multiple = $selectnode->hasAttribute('multiple'); 324 $optionsarr = array(); // Array of passed value/text options to test. 325 326 if ($multiple) { 327 // Can pass multiple comma separated, with valuable commas escaped with backslash. 328 foreach (preg_replace('/\\\,/', ',', preg_split('/(?<!\\\),/', $option)) as $opt) { 329 $optionsarr[] = trim($opt); 330 } 331 } else { 332 // Only one option has been passed. 333 $optionsarr[] = trim($option); 334 } 335 336 // Now get all the values and texts in the select. 337 $options = $selectnode->findAll('xpath', '//option'); 338 $values = array(); 339 foreach ($options as $opt) { 340 $values[trim($opt->getValue())] = trim($opt->getText()); 341 } 342 343 foreach ($optionsarr as $opt) { 344 // Verify every option is not a valid text or value. 345 if (in_array($opt, $values) || array_key_exists($opt, $values)) { 346 throw new ExpectationException( 347 'The select box "' . $select . '" contains the option "' . $opt . '"', 348 $this->getSession() 349 ); 350 } 351 } 352 } 353 354 /** 355 * Generic field setter. 356 * 357 * Internal API method, a generic *I set "VALUE" to "FIELD" field* 358 * could be created based on it. 359 * 360 * @param string $fieldlocator The pointer to the field, it will depend on the field type. 361 * @param string $value 362 * @return void 363 */ 364 protected function set_field_value($fieldlocator, $value) { 365 366 // We delegate to behat_form_field class, it will 367 // guess the type properly as it is a select tag. 368 $field = behat_field_manager::get_form_field_from_label($fieldlocator, $this); 369 $field->set_value($value); 370 } 371 372 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |