Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
global $CFG;
22
require_once($CFG->libdir.'/adminlib.php');
23
 
24
/**
25
 * Unit tests for parts of adminlib.php.
26
 *
27
 * @package    core
28
 * @copyright  2020 Brendan Heywood <brendan@catalyst-au.net>
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
class adminlib_test extends \advanced_testcase {
32
 
33
    /**
34
     * Data provider of serialized string.
35
     *
36
     * @return array
37
     */
38
    public function db_should_replace_dataprovider() {
39
        return [
40
            // Skipped tables.
41
            ['block_instances', '', false],
42
            ['config',          '', false],
43
            ['config_plugins',  '', false],
44
            ['config_log',      '', false],
45
            ['events_queue',    '', false],
46
            ['filter_config',   '', false],
47
            ['log',             '', false],
48
            ['repository_instance_config', '', false],
49
            ['sessions',        '', false],
50
            ['upgrade_log',     '', false],
51
 
52
            // Unknown skipped tables.
53
            ['foobar_log',      '', false],
54
            ['foobar_logs',     '', false],
55
 
56
            // Unknown ok tables.
57
            ['foobar_logical',  '', true],
58
 
59
            // Normal tables.
60
            ['assign',          '', true],
61
 
62
            // Normal tables with excluded columns.
63
            ['message_conversations', 'convhash',   false],
64
            ['user_password_history', 'hash',       false],
65
            ['foo',                   'barhash',    false],
66
        ];
67
    }
68
 
69
    /**
70
     * Test which tables and column should be replaced.
71
     *
72
     * @dataProvider db_should_replace_dataprovider
73
     * @covers ::db_should_replace
74
     * @param string $table name
75
     * @param string $column name
76
     * @param bool $expected whether it should be replaced
77
     */
11 efrain 78
    public function test_db_should_replace(string $table, string $column, bool $expected): void {
1 efrain 79
        $actual = db_should_replace($table, $column);
80
        $this->assertSame($actual, $expected);
81
    }
82
 
83
    /**
84
     * Data provider for additional skip tables.
85
     *
86
     * @covers ::db_should_replace
87
     * @return array
88
     */
89
    public function db_should_replace_additional_skip_tables_dataprovider() {
90
        return [
91
            // Skipped tables.
92
            ['block_instances', '', false],
93
            ['config',          '', false],
94
            ['config_plugins',  '', false],
95
            ['config_log',      '', false],
96
            ['events_queue',    '', false],
97
            ['filter_config',   '', false],
98
            ['log',             '', false],
99
            ['repository_instance_config', '', false],
100
            ['sessions',        '', false],
101
            ['upgrade_log',     '', false],
102
 
103
            // Additional skipped tables.
104
            ['context',      '', false],
105
            ['quiz_attempts',     '', false],
106
            ['role_assignments',     '', false],
107
 
108
            // Normal tables.
109
            ['assign',          '', true],
110
            ['book',          '', true],
111
        ];
112
    }
113
 
114
    /**
115
     * Test additional skip tables.
116
     *
117
     * @dataProvider db_should_replace_additional_skip_tables_dataprovider
118
     * @covers ::db_should_replace
119
     * @param string $table name
120
     * @param string $column name
121
     * @param bool $expected whether it should be replaced
122
     */
11 efrain 123
    public function test_db_should_replace_additional_skip_tables(string $table, string $column, bool $expected): void {
1 efrain 124
        $this->resetAfterTest();
125
        $additionalskiptables = 'context, quiz_attempts, role_assignments ';
126
        $actual = db_should_replace($table, $column, $additionalskiptables);
127
        $this->assertSame($actual, $expected);
128
    }
129
 
130
    /**
131
     * Test admin_output_new_settings_by_page method.
132
     *
133
     * @covers ::admin_output_new_settings_by_page
134
     */
11 efrain 135
    public function test_admin_output_new_settings_by_page(): void {
1 efrain 136
        $this->resetAfterTest();
137
        $this->setAdminUser();
138
 
139
        $root = admin_get_root(true, true);
140
        // The initial list of html pages with no default settings.
141
        $initialsettings = admin_output_new_settings_by_page($root);
142
        $this->assertArrayHasKey('supportcontact', $initialsettings);
143
        $this->assertArrayHasKey('frontpagesettings', $initialsettings);
144
        // Existing default setting.
145
        $this->assertArrayNotHasKey('modsettingbook', $initialsettings);
146
 
147
        // Add settings not set during PHPUnit init.
148
        set_config('supportemail', 'support@example.com');
149
        $frontpage = new \admin_setting_special_frontpagedesc();
150
        $frontpage->write_setting('test test');
151
        // Remove a default setting.
152
        unset_config('numbering', 'book');
153
 
154
        $root = admin_get_root(true, true);
155
        $new = admin_output_new_settings_by_page($root);
156
        $this->assertArrayNotHasKey('supportcontact', $new);
157
        $this->assertArrayNotHasKey('frontpagesettings', $new);
158
        $this->assertArrayHasKey('modsettingbook', $new);
159
    }
160
 
161
    /**
162
     * Test repeated recursive application of default settings.
163
     *
164
     * @covers ::admin_apply_default_settings
165
     */
11 efrain 166
    public function test_admin_apply_default_settings(): void {
1 efrain 167
        global $DB;
168
 
169
        $this->resetAfterTest();
170
        $this->setAdminUser();
171
 
172
        // There should not be any pending new defaults.
173
        $saved = admin_apply_default_settings(null, false);
174
        $this->assertSame([], $saved);
175
 
176
        // Emulation of upgrades from CLI.
177
        unset_config('logocompact', 'core_admin');
178
        unset_config('grade_aggregationposition');
179
        unset_config('numbering', 'book');
180
        unset_config('enabled', 'core_competency');
181
        unset_config('pushcourseratingstouserplans', 'core_competency');
182
        $saved = admin_apply_default_settings(null, false);
183
        $expected = [
184
            'core_competency/enabled' => '1',
185
            'grade_aggregationposition' => '1',
186
            'book/numbering' => '1',
187
            'core_admin/logocompact' => '',
188
            'core_competency/pushcourseratingstouserplans' => '1',
189
        ];
190
        $this->assertEquals($expected, $saved);
191
 
192
        // Repeated application of defaults - not done usually.
193
        $saved = admin_apply_default_settings(null, true);
194
        $this->assertGreaterThan(500, count($saved));
195
        $saved = admin_apply_default_settings();
196
        $this->assertGreaterThan(500, count($saved));
197
 
198
        // Emulate initial application of defaults.
199
        $DB->delete_records('config', []);
200
        $DB->delete_records('config_plugins', []);
201
        purge_all_caches();
202
        $saved = admin_apply_default_settings(null, true);
203
        $this->assertGreaterThan(500, count($saved));
204
 
205
        // Make sure there were enough repetitions.
206
        $saved = admin_apply_default_settings(null, false);
207
        $this->assertSame([], $saved);
208
    }
209
}