MDL-39348 behat: Chaining strings instead of splitting strings in lines
[moodle.git] / admin / tests / behat / behat_admin.php
blob0cd23064ad4d1e65ba76cb8da72a9011a4a53abe
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 * Steps definitions related with administration.
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 require_once(__DIR__ . '/../../../lib/behat/behat_base.php');
29 require_once(__DIR__ . '/../../../lib/behat/behat_field_manager.php');
31 use Behat\Gherkin\Node\TableNode as TableNode,
32 Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
34 /**
35 * Site administration level steps definitions.
37 * @package core
38 * @category test
39 * @copyright 2013 David MonllaĆ³
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class behat_admin extends behat_base {
44 /**
45 * Sets the specified site settings. A table with | Setting label | value | is expected.
47 * @Given /^I set the following administration settings values:$/
48 * @param TableNode $table
50 public function i_set_the_following_administration_settings_values(TableNode $table) {
52 if (!$data = $table->getRowsHash()) {
53 return;
56 foreach ($data as $label => $value) {
58 // We expect admin block to be visible, otherwise go to homepage.
59 if (!$this->getSession()->getPage()->find('css', '.block_settings')) {
60 $this->getSession()->visit($this->locate_path('/'));
61 $this->wait(self::TIMEOUT, '(document.readyState === "complete")');
64 // Search by label.
65 $searchbox = $this->find_field('Search in settings');
66 $searchbox->setValue($label);
67 $submitsearch = $this->find('css', 'form.adminsearchform input[type=submit]');
68 $submitsearch->press();
70 $this->wait(self::TIMEOUT, '(document.readyState === "complete")');
72 // Admin settings does not use the same DOM structure than other moodle forms
73 // but we also need to use lib/behat/form_field/* to deal with the different moodle form elements.
74 $exception = new ElementNotFoundException($this->getSession(), '"' . $label . '" administration setting ');
75 $fieldxpath = "//*[self::input | self::textarea | self::select][not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')]" .
76 "[@id=//label[contains(normalize-space(string(.)), '" . $label . "')]/@for]";
77 $fieldnode = $this->find('xpath', $fieldxpath, $exception);
78 $formfieldtypenode = $this->find('xpath', $fieldxpath . "/ancestor::div[@class='form-setting']" .
79 "/child::div[contains(concat(' ', @class, ' '), ' form-')]/child::*/parent::div");
81 // Getting the class which contains the field type.
82 $classes = explode(' ', $formfieldtypenode->getAttribute('class'));
83 foreach ($classes as $class) {
84 if (substr($class, 0, 5) == 'form-') {
85 $type = substr($class, 5);
89 // Instantiating the appropiate field type.
90 $field = behat_field_manager::get_field_instance($type, $fieldnode, $this->getSession());
91 $field->set_value($value);
93 $this->find_button('Save changes')->press();
97 /**
98 * Waits with the provided params if we are running a JS session.
100 * @param int $timeout
101 * @param string $javascript
102 * @return void
104 protected function wait($timeout, $javascript = false) {
105 if ($this->running_javascript()) {
106 $this->getSession()->wait($timeout, $javascript);