MDL-58428 theme: Shift templates ready for Bootstrapbase removal
[moodle.git] / admin / tests / behat / behat_admin.php
CommitLineData
e7a0600b
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 administration.
19 *
abe572e3 20 * @package core_admin
e7a0600b
DM
21 * @category test
22 * @copyright 2013 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');
29require_once(__DIR__ . '/../../../lib/behat/behat_field_manager.php');
30
42ad096f 31use Behat\Gherkin\Node\TableNode as TableNode,
e7a0600b
DM
32 Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
33
34/**
35 * Site administration level steps definitions.
36 *
abe572e3 37 * @package core_admin
e7a0600b
DM
38 * @category test
39 * @copyright 2013 David MonllaĆ³
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
42class behat_admin extends behat_base {
43
44 /**
45 * Sets the specified site settings. A table with | Setting label | value | is expected.
46 *
47 * @Given /^I set the following administration settings values:$/
48 * @param TableNode $table
49 */
50 public function i_set_the_following_administration_settings_values(TableNode $table) {
51
52 if (!$data = $table->getRowsHash()) {
53 return;
54 }
55
56 foreach ($data as $label => $value) {
57
e3652936 58 $this->execute('behat_navigation::i_select_from_flat_navigation_drawer', [get_string('administrationsite')]);
e7a0600b
DM
59
60 // Search by label.
e3652936 61 $searchbox = $this->find_field(get_string('query', 'admin'));
e7a0600b 62 $searchbox->setValue($label);
e3652936 63 $submitsearch = $this->find('css', 'form input[type=submit][name=search]');
e7a0600b
DM
64 $submitsearch->press();
65
d1e55a47 66 $this->wait(self::TIMEOUT * 1000, self::PAGE_READY_JS);
e7a0600b
DM
67
68 // Admin settings does not use the same DOM structure than other moodle forms
69 // but we also need to use lib/behat/form_field/* to deal with the different moodle form elements.
70 $exception = new ElementNotFoundException($this->getSession(), '"' . $label . '" administration setting ');
38976081
DM
71
72 // The argument should be converted to an xpath literal.
921faad9 73 $label = behat_context_helper::escape($label);
38976081 74
32fcea74
DM
75 // Single element settings.
76 try {
e3652936
MM
77 $fieldxpath = "//*[self::input | self::textarea | self::select]" .
78 "[not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')]" .
79 "[@id=//label[contains(normalize-space(.), $label)]/@for or " .
80 "@id=//span[contains(normalize-space(.), $label)]/preceding-sibling::label[1]/@for]";
32fcea74
DM
81 $fieldnode = $this->find('xpath', $fieldxpath, $exception);
82
e3652936
MM
83 $formfieldtypenode = $this->find('xpath', $fieldxpath .
84 "/ancestor::div[contains(concat(' ', @class, ' '), ' form-setting ')]" .
85 "/child::div[contains(concat(' ', @class, ' '), ' form-')]/child::*/parent::div");
32fcea74
DM
86
87 } catch (ElementNotFoundException $e) {
88
89 // Multi element settings, interacting only the first one.
e3652936
MM
90 $fieldxpath = "//*[label[contains(., $label)]|span[contains(., $label)]]" .
91 "/ancestor::div[contains(concat(' ', normalize-space(@class), ' '), ' form-item ')]" .
92 "/descendant::div[contains(concat(' ', @class, ' '), ' form-group ')]" .
93 "/descendant::*[self::input | self::textarea | self::select]" .
94 "[not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')]";
32fcea74
DM
95 $fieldnode = $this->find('xpath', $fieldxpath);
96
97 // It is the same one that contains the type.
98 $formfieldtypenode = $fieldnode;
99 }
e7a0600b
DM
100
101 // Getting the class which contains the field type.
102 $classes = explode(' ', $formfieldtypenode->getAttribute('class'));
e3652936 103 $type = false;
e7a0600b
DM
104 foreach ($classes as $class) {
105 if (substr($class, 0, 5) == 'form-') {
106 $type = substr($class, 5);
107 }
108 }
109
110 // Instantiating the appropiate field type.
111 $field = behat_field_manager::get_field_instance($type, $fieldnode, $this->getSession());
112 $field->set_value($value);
113
dedb9738 114 $this->find_button(get_string('savechanges'))->press();
e7a0600b
DM
115 }
116 }
117
bed97c39
RT
118 /**
119 * Sets the specified site settings. A table with | config | value | (optional)plugin | is expected.
120 *
121 * @Given /^the following config values are set as admin:$/
122 * @param TableNode $table
123 */
124 public function the_following_config_values_are_set_as_admin(TableNode $table) {
125
126 if (!$data = $table->getRowsHash()) {
127 return;
128 }
129
130 foreach ($data as $config => $value) {
131 // Default plugin value is null.
132 $plugin = null;
133
134 if (is_array($value)) {
135 $plugin = $value[1];
136 $value = $value[0];
137 }
138 set_config($config, $value, $plugin);
139 }
140 }
141
6b9c8667
DM
142 /**
143 * Waits with the provided params if we are running a JS session.
144 *
145 * @param int $timeout
146 * @param string $javascript
147 * @return void
148 */
149 protected function wait($timeout, $javascript = false) {
150 if ($this->running_javascript()) {
151 $this->getSession()->wait($timeout, $javascript);
152 }
153 }
e7a0600b 154}