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 * Step definitions related to administration overrides for the Classic theme.
19 *
20 * @package    theme_classic
21 * @category   test
22 * @copyright  2019 Michael Hawkins
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// For that reason, we can't even rely on $CFG->admin being available here.
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 for the Classic theme.
36 *
37 * @package    theme_classic
38 * @category   test
39 * @copyright  2019 Michael Hawkins
40 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
42class behat_theme_classic_behat_admin extends behat_admin {
43
44    /**
45     * Sets the specified site settings. A table with | Setting label | value | is expected.
46     *
47     * @param TableNode $table
48     */
49    public function i_set_the_following_administration_settings_values(TableNode $table) {
50
51        if (!$data = $table->getRowsHash()) {
52            return;
53        }
54
55        foreach ($data as $label => $value) {
56
57            // We expect admin block to be visible, otherwise go to homepage.
58            if (!$this->getSession()->getPage()->find('css', '.block_settings')) {
59                $this->execute('behat_forms::i_am_on_homepage');
60            }
61
62            // Search by label.
63            $this->execute('behat_forms::i_set_the_field_to', [get_string('searchinsettings', 'admin'), $label]);
64            $this->execute("behat_general::i_click_on_in_the", [get_string('search', 'admin'), 'button', '.block_settings', 'css_element']);
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 {
75                $fieldxpath = "//*[self::input | self::textarea | self::select]" .
76                        "[not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')]" .
77                        "[@id=//label[contains(normalize-space(.), $label)]/@for or " .
78                        "@id=//span[contains(normalize-space(.), $label)]/preceding-sibling::label[1]/@for]";
79                $fieldnode = $this->find('xpath', $fieldxpath, $exception);
80            } catch (ElementNotFoundException $e) {
81
82                // Multi element settings, interacting only the first one.
83                $fieldxpath = "//*[label[contains(., $label)]|span[contains(., $label)]]" .
84                        "/ancestor::div[contains(concat(' ', normalize-space(@class), ' '), ' form-item ')]" .
85                        "/descendant::div[contains(concat(' ', @class, ' '), ' form-group ')]" .
86                        "/descendant::*[self::input | self::textarea | self::select]" .
87                        "[not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')]";
88                $fieldnode = $this->find('xpath', $fieldxpath);
89            }
90
91            $this->execute('behat_forms::i_set_the_field_with_xpath_to', [$fieldxpath, $value]);
92
93            $this->execute("behat_general::i_click_on", [get_string('savechanges'), 'button']);
94        }
95    }
96}
97