Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 *
20
 * @package   core_admin
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
 
28
require_once(__DIR__ . '/../../../lib/behat/behat_base.php');
29
require_once(__DIR__ . '/../../../lib/behat/behat_field_manager.php');
30
 
31
use Behat\Gherkin\Node\TableNode as TableNode,
32
    Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
33
 
34
/**
35
 * Site administration level steps definitions.
36
 *
37
 * @package    core_admin
38
 * @category   test
39
 * @copyright  2013 David Monllaó
40
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41
 */
42
class 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
        if (!$data = $table->getRowsHash()) {
52
            return;
53
        }
54
 
55
        foreach ($data as $label => $value) {
56
            // Navigate straight to the search results fo rthis label.
57
            $this->execute('behat_general::i_visit', ["/admin/search.php?query=" . urlencode($label)]);
58
 
59
            // Admin settings does not use the same DOM structure than other moodle forms
60
            // but we also need to use lib/behat/form_field/* to deal with the different moodle form elements.
61
            $exception = new ElementNotFoundException($this->getSession(), '"' . $label . '" administration setting ');
62
 
63
            // The argument should be converted to an xpath literal.
64
            $label = behat_context_helper::escape($label);
65
 
66
            // Single element settings.
67
            try {
68
                $fieldxpath = "//*[self::input | self::textarea | self::select]" .
69
                        "[not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')]" .
70
                        "[@id=//label[contains(normalize-space(.), $label)]/@for or " .
71
                        "@id=//span[contains(normalize-space(.), $label)]/preceding-sibling::label[1]/@for]";
72
                $fieldnode = $this->find('xpath', $fieldxpath, $exception);
73
 
74
            } catch (ElementNotFoundException $e) {
75
                // Multi element settings, interacting only the first one.
76
                $fieldxpath = "//*[label[contains(., $label)]|span[contains(., $label)]]" .
77
                        "/ancestor::div[contains(concat(' ', normalize-space(@class), ' '), ' form-item ')]" .
78
                        "/descendant::*[self::input | self::textarea | self::select]" .
79
                        "[not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')]";
80
            }
81
 
82
            $this->execute('behat_forms::i_set_the_field_with_xpath_to', [$fieldxpath, $value]);
83
            $this->execute("behat_general::i_click_on", [get_string('savechanges'), 'button']);
84
        }
85
    }
86
 
87
    /**
88
     * Sets the specified site settings. A table with | config | value | (optional)plugin | (optional)encrypted | is expected.
89
     *
90
     * @Given /^the following config values are set as admin:$/
91
     * @param TableNode $table
92
     */
93
    public function the_following_config_values_are_set_as_admin(TableNode $table) {
94
 
95
        if (!$data = $table->getRowsHash()) {
96
            return;
97
        }
98
 
99
        foreach ($data as $config => $value) {
100
            // Default plugin value is null.
101
            $plugin = null;
102
            $encrypted = false;
103
 
104
            if (is_array($value)) {
105
                $plugin = $value[1];
106
                if (array_key_exists(2, $value)) {
107
                    $encrypted = $value[2] === 'encrypted';
108
                }
109
                $value = $value[0];
110
            }
111
 
112
            if ($encrypted) {
113
                $value = \core\encryption::encrypt($value);
114
            }
115
 
116
            set_config($config, $value, $plugin);
117
        }
118
    }
119
 
120
    /**
121
     * Waits with the provided params if we are running a JS session.
122
     *
123
     * @param int $timeout
124
     * @param string $javascript
125
     * @return void
126
     */
127
    protected function wait($timeout, $javascript = false) {
128
        if ($this->running_javascript()) {
129
            $this->getSession()->wait($timeout, $javascript);
130
        }
131
    }
132
}