MDL-58428 theme: Shift templates ready for Bootstrapbase removal
[moodle.git] / theme / bootstrapbase / tests / behat / behat_theme_bootstrapbase_behat_admin.php
CommitLineData
63e4df60
DW
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 administration overrides.
19 *
e3652936
MM
20 * @package theme_bootstrapbase
21 * @category test
63e4df60
DW
22 * @copyright 2016 Damyon Wiese
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.
abcd5344 27// For that reason, we can't even rely on $CFG->admin being available here.
63e4df60
DW
28
29require_once(__DIR__ . '/../../../../admin/tests/behat/behat_admin.php');
30
31use Behat\Gherkin\Node\TableNode as TableNode,
32 Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
33
34/**
35 * Site administration level steps definitions overrides.
36 *
e3652936
MM
37 * @package theme_bootstrapbase
38 * @category test
39 * @copyright 2016 Damyon Wiese
63e4df60
DW
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
e3652936 42class behat_theme_bootstrapbase_behat_admin extends behat_admin {
63e4df60
DW
43
44 public function i_set_the_following_administration_settings_values(TableNode $table) {
45
46 if (!$data = $table->getRowsHash()) {
47 return;
48 }
49
50 foreach ($data as $label => $value) {
51
e3652936
MM
52 // We expect admin block to be visible, otherwise go to homepage.
53 if (!$this->getSession()->getPage()->find('css', '.block_settings')) {
54 $this->getSession()->visit($this->locate_path('/'));
55 $this->wait(self::TIMEOUT * 1000, self::PAGE_READY_JS);
56 }
63e4df60
DW
57
58 // Search by label.
e3652936 59 $searchbox = $this->find_field(get_string('searchinsettings', 'admin'));
63e4df60 60 $searchbox->setValue($label);
e3652936 61 $submitsearch = $this->find('css', 'form.adminsearchform input[type=submit]');
63e4df60
DW
62 $submitsearch->press();
63
64 $this->wait(self::TIMEOUT * 1000, self::PAGE_READY_JS);
65
66 // Admin settings does not use the same DOM structure than other moodle forms
67 // but we also need to use lib/behat/form_field/* to deal with the different moodle form elements.
68 $exception = new ElementNotFoundException($this->getSession(), '"' . $label . '" administration setting ');
69
70 // The argument should be converted to an xpath literal.
71 $label = behat_context_helper::escape($label);
72
73 // Single element settings.
74 try {
e3652936
MM
75 $fieldxpath = "//*[self::input | self::textarea | self::select][not(./@type = 'submit' or ./@type = 'image' or " .
76 "./@type = 'hidden')]" . "[@id=//label[contains(normalize-space(.), $label)]/@for or " .
77 "@id=//span[contains(normalize-space(.), $label)]/preceding-sibling::label[1]/@for]";
63e4df60
DW
78 $fieldnode = $this->find('xpath', $fieldxpath, $exception);
79
e3652936
MM
80 $formfieldtypenode = $this->find('xpath', $fieldxpath . "/ancestor::div[@class='form-setting']" .
81 "/child::div[contains(concat(' ', @class, ' '), ' form-')]/child::*/parent::div");
63e4df60
DW
82
83 } catch (ElementNotFoundException $e) {
84
85 // Multi element settings, interacting only the first one.
e3652936
MM
86 $fieldxpath = "//*[label[normalize-space(.)= $label]|span[normalize-space(.)= $label]]/" .
87 "ancestor::div[contains(concat(' ', normalize-space(@class), ' '), ' form-item ')]" .
88 "/descendant::div[@class='form-group']/descendant::*[self::input | self::textarea | self::select]" .
89 "[not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')]";
63e4df60
DW
90 $fieldnode = $this->find('xpath', $fieldxpath);
91
92 // It is the same one that contains the type.
93 $formfieldtypenode = $fieldnode;
94 }
95
96 // Getting the class which contains the field type.
97 $classes = explode(' ', $formfieldtypenode->getAttribute('class'));
63e4df60
DW
98 foreach ($classes as $class) {
99 if (substr($class, 0, 5) == 'form-') {
100 $type = substr($class, 5);
101 }
102 }
103
104 // Instantiating the appropiate field type.
105 $field = behat_field_manager::get_field_instance($type, $fieldnode, $this->getSession());
106 $field->set_value($value);
107
108 $this->find_button(get_string('savechanges'))->press();
109 }
110 }
111
112}