2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Steps definitions related with administration overrides.
20 * @copyright 2016 Damyon Wiese
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
25 // For that reason, we can't even rely on $CFG->admin being available here.
27 require_once(__DIR__ . '/../../../../admin/tests/behat/behat_admin.php');
29 use Behat\Gherkin\Node\TableNode as TableNode,
30 Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
33 * Site administration level steps definitions overrides.
35 * @copyright 2016 Damyon Wiese
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class behat_theme_boost_behat_admin extends behat_admin {
40 public function i_set_the_following_administration_settings_values(TableNode $table) {
42 if (!$data = $table->getRowsHash()) {
46 foreach ($data as $label => $value) {
48 $this->execute('behat_navigation::i_select_from_flat_navigation_drawer', [get_string('administrationsite')]);
51 $searchbox = $this->find_field(get_string('query', 'admin'));
52 $searchbox->setValue($label);
53 $submitsearch = $this->find('css', 'form input[type=submit][name=search]');
54 $submitsearch->press();
56 $this->wait(self::TIMEOUT * 1000, self::PAGE_READY_JS);
58 // Admin settings does not use the same DOM structure than other moodle forms
59 // but we also need to use lib/behat/form_field/* to deal with the different moodle form elements.
60 $exception = new ElementNotFoundException($this->getSession(), '"' . $label . '" administration setting ');
62 // The argument should be converted to an xpath literal.
63 $label = behat_context_helper::escape($label);
65 // Single element settings.
67 $fieldxpath = "//*[self::input | self::textarea | self::select]" .
68 "[not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')]" .
69 "[@id=//label[contains(normalize-space(.), $label)]/@for or " .
70 "@id=//span[contains(normalize-space(.), $label)]/preceding-sibling::label[1]/@for]";
71 $fieldnode = $this->find('xpath', $fieldxpath, $exception);
73 $formfieldtypenode = $this->find('xpath', $fieldxpath .
74 "/ancestor::div[contains(concat(' ', @class, ' '), ' form-setting ')]" .
75 "/child::div[contains(concat(' ', @class, ' '), ' form-')]/child::*/parent::div");
77 } catch (ElementNotFoundException $e) {
79 // Multi element settings, interacting only the first one.
80 $fieldxpath = "//*[label[contains(., $label)]|span[contains(., $label)]]" .
81 "/ancestor::div[contains(concat(' ', normalize-space(@class), ' '), ' form-item ')]" .
82 "/descendant::div[contains(concat(' ', @class, ' '), ' form-group ')]" .
83 "/descendant::*[self::input | self::textarea | self::select]" .
84 "[not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')]";
85 $fieldnode = $this->find('xpath', $fieldxpath);
87 // It is the same one that contains the type.
88 $formfieldtypenode = $fieldnode;
91 // Getting the class which contains the field type.
92 $classes = explode(' ', $formfieldtypenode->getAttribute('class'));
94 foreach ($classes as $class) {
95 if (substr($class, 0, 5) == 'form-') {
96 $type = substr($class, 5);
100 // Instantiating the appropiate field type.
101 $field = behat_field_manager::get_field_instance($type, $fieldnode, $this->getSession());
102 $field->set_value($value);
104 $this->find_button(get_string('savechanges'))->press();