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
namespace core_adminpresets\local\setting;
18
 
19
/**
20
 * Tests for the adminpresets_setting class.
21
 *
22
 * @package    core_adminpresets
23
 * @category   test
24
 * @copyright  2021 Sara Arjona (sara@moodle.com)
25
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 * @coversDefaultClass \core_adminpresets\local\setting\adminpresets_setting
27
 */
28
class adminpresets_setting_test extends \advanced_testcase {
29
 
30
    /**
31
     * Test the behaviour of save_value() method.
32
     *
33
     * @covers ::save_value
34
     * @dataProvider save_value_provider
35
     *
36
     * @param string $category Admin tree where the setting belongs.
37
     * @param string $settingplugin Plugin where the setting belongs.
38
     * @param string $settingname Setting name.
39
     * @param string $settingvalue Setting value to be saved.
40
     * @param bool $expectedsaved Whether the setting will be saved or not.
41
     */
42
    public function test_save_value(string $category, string $settingplugin, string $settingname, string $settingvalue,
43
            bool $expectedsaved): void {
44
        global $DB;
45
 
46
        $this->resetAfterTest();
47
 
48
        // Login as admin, to access all the settings.
49
        $this->setAdminUser();
50
 
51
        // Set the config values (to confirm they change after applying the preset).
52
        set_config('enablebadges', 1);
53
        set_config('mediawidth', '640', 'mod_lesson');
54
 
55
        // The expected setting name in the admin tree is $plugin.$name when plugin is not core.
56
        if ($settingplugin !== 'core') {
57
            $name = $settingplugin . $settingname;
58
        } else {
59
            $name = $settingname;
60
        }
61
        // Get the setting and save the value.
62
        $generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
63
        $setting = $generator->get_admin_preset_setting($category, $name);
64
        $result = $setting->save_value(false, $settingvalue);
65
 
66
        // Check the result is the expected (saved when it has a different value and ignored when the value is the same).
67
        if ($expectedsaved) {
68
            $this->assertCount(1, $DB->get_records('config_log', ['id' => $result]));
69
        } else {
70
            $this->assertFalse($result);
71
        }
72
        $this->assertEquals($settingvalue, get_config($settingplugin, $settingname));
73
    }
74
 
75
    /**
76
     * Data provider for test_save_value().
77
     *
78
     * @return array
79
     */
80
    public function save_value_provider(): array {
81
        return [
82
            'Core setting with the same value is not saved' => [
83
                'category' => 'optionalsubsystems',
84
                'settingplugin' => 'core',
85
                'settingname' => 'enablebadges',
86
                'setttingvalue' => '1',
87
                'expectedsaved' => false,
88
            ],
89
            'Core setting with a different value is saved' => [
90
                'category' => 'optionalsubsystems',
91
                'settingplugin' => 'core',
92
                'settingname' => 'enablebadges',
93
                'setttingvalue' => '0',
94
                'expectedsaved' => true,
95
            ],
96
            'Plugin setting with the same value is not saved' => [
97
                'category' => 'modsettinglesson',
98
                'settingplugin' => 'mod_lesson',
99
                'settingname' => 'mediawidth',
100
                'setttingvalue' => '640',
101
                'expectedsaved' => false,
102
            ],
103
            'Plugin setting with different value is saved' => [
104
                'category' => 'modsettinglesson',
105
                'settingplugin' => 'mod_lesson',
106
                'settingname' => 'mediawidth',
107
                'setttingvalue' => '900',
108
                'expectedsaved' => true,
109
            ],
110
        ];
111
    }
112
 
113
    /**
114
     * Test the behaviour of save_attributes_values() method.
115
     *
116
     * @covers ::save_attributes_values
117
     * @dataProvider save_attributes_values_provider
118
     *
119
     * @param string $category Admin tree where the setting belongs.
120
     * @param string $settingplugin Plugin where the setting belongs.
121
     * @param string $settingname Setting name.
122
     * @param string|null $advsettingname Advanced setting name.
123
     * @param string $advsettingvalue Advanced setting value to be saved.
124
     * @param bool $expectedsaved Whether the setting will be saved or not.
125
     */
126
    public function test_save_attributes_values(string $category, string $settingplugin, string $settingname,
127
            ?string $advsettingname, string $advsettingvalue, bool $expectedsaved): void {
128
        global $DB;
129
 
130
        $this->resetAfterTest();
131
 
132
        // Login as admin, to access all the settings.
133
        $this->setAdminUser();
134
 
135
        // Set the config values (to confirm they change after applying the preset).
136
        set_config('maxanswers_adv', '1', 'mod_lesson');
137
 
138
        // The expected setting name in the admin tree is $plugin.$name when plugin is not core.
139
        if ($settingplugin !== 'core') {
140
            $name = $settingplugin . $settingname;
141
        } else {
142
            $name = $settingname;
143
        }
144
        // Get the setting and save the value.
145
        $generator = $this->getDataGenerator()->get_plugin_generator('core_adminpresets');
146
        $setting = $generator->get_admin_preset_setting($category, $name);
147
        if ($advsettingname) {
148
            $setting->set_attribute_value($advsettingname, $advsettingvalue);
149
        }
150
        $result = $setting->save_attributes_values();
151
 
152
        // Check the result is the expected (saved when it has a different value and ignored when the value is the same).
153
        if ($expectedsaved) {
154
            $this->assertCount(1, $result);
155
            $configlog = reset($result);
156
            $this->assertCount(1, $DB->get_records('config_log', ['id' => $configlog]));
157
        } else {
158
            $this->assertFalse($result);
159
        }
160
        if ($advsettingname) {
161
            $this->assertEquals($advsettingvalue, get_config($settingplugin, $advsettingname));
162
        }
163
    }
164
 
165
    /**
166
     * Data provider for test_save_attributes_values().
167
     *
168
     * @return array
169
     */
170
    public function save_attributes_values_provider(): array {
171
        return [
172
            'Plugin setting with the same value is not saved' => [
173
                'category' => 'modsettinglesson',
174
                'settingplugin' => 'mod_lesson',
175
                'settingname' => 'maxanswers',
176
                'advsettingname' => 'maxanswers_adv',
177
                'advsetttingvalue' => '1',
178
                'expectedsaved' => false,
179
            ],
180
            'Plugin setting with different value is saved' => [
181
                'category' => 'modsettinglesson',
182
                'settingplugin' => 'mod_lesson',
183
                'settingname' => 'maxanswers',
184
                'advsettingname' => 'maxanswers_adv',
185
                'advsetttingvalue' => '0',
186
                'expectedsaved' => true,
187
            ],
188
            'Plugin setting without advanced attributes are not saved' => [
189
                'category' => 'modsettinglesson',
190
                'settingplugin' => 'mod_lesson',
191
                'settingname' => 'maxanswers',
192
                'advsettingname' => null,
193
                'advsetttingvalue' => '0',
194
                'expectedsaved' => false,
195
            ],
196
        ];
197
    }
198
}