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
declare(strict_types=1);
18
 
19
namespace core\plugininfo;
20
 
21
use testable_core_plugin_manager;
22
use testable_plugininfo_base;
23
 
24
/**
25
 * Unit tests for plugin base class.
26
 *
27
 * @package   core
28
 * @copyright 2019 Andrew Nicols
29
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1441 ariadna 30
 * @covers \core\plugininfo\base
1 efrain 31
 */
1441 ariadna 32
final class base_test extends \advanced_testcase {
1 efrain 33
 
34
    /**
35
     * Setup to ensure that fixtures are loaded.
36
     */
37
    public static function setUpBeforeClass(): void {
38
        global $CFG;
39
 
40
        require_once($CFG->dirroot.'/lib/tests/fixtures/testable_plugin_manager.php');
41
        require_once($CFG->dirroot.'/lib/tests/fixtures/testable_plugininfo_base.php');
1441 ariadna 42
        parent::setUpBeforeClass();
1 efrain 43
    }
44
 
45
    /**
46
     * Tear down the testable plugin manager singleton between tests.
47
     */
48
    public function tearDown(): void {
49
        // The caches of the testable singleton must be reset explicitly. It is
50
        // safer to kill the whole testable singleton at the end of every test.
51
        testable_core_plugin_manager::reset_caches();
1441 ariadna 52
        parent::tearDown();
1 efrain 53
    }
54
 
55
    /**
56
     * Test the load_disk_version function to check that it handles a variety of invalid supported fields.
57
     *
58
     * @dataProvider load_disk_version_invalid_supported_version_provider
59
     * @param array|null $supported Supported versions to inject
60
     * @param string|int|null $incompatible Incompatible version to inject.
61
     * @param int $version Version to test
62
     */
63
    public function test_load_disk_version_invalid_supported_version($supported, $incompatible, $version): void {
64
        $pluginman = testable_core_plugin_manager::instance();
65
 
66
        // Prepare a fake plugininfo instance.
67
        $plugininfo = new testable_plugininfo_base();
68
        $plugininfo->type = 'fake';
69
        $plugininfo->typerootdir = '/dev/null';
70
        $plugininfo->name = 'example';
71
        $plugininfo->rootdir = '/dev/null/fake';
72
        $plugininfo->pluginman = $pluginman;
73
        $plugininfo->versiondisk = 2015060600;
74
        $plugininfo->supported = $supported;
75
        $plugininfo->incompatible = $incompatible;
76
 
77
        $pluginman->add_fake_plugin_info($plugininfo);
78
 
79
        $this->expectException(\coding_exception::class);
80
        $this->expectExceptionMessage('Incorrect syntax in plugin supported declaration in example');
81
        $plugininfo->load_disk_version();
82
    }
83
 
84
    /**
85
     * Data provider for the load_disk_version tests for testing with invalid supported fields.
86
     *
87
     * @return array
88
     */
1441 ariadna 89
    public static function load_disk_version_invalid_supported_version_provider(): array {
1 efrain 90
        return [
91
            'Invalid supported range.' => [
92
                'supported' => [31, 29],
93
                'incompatible' => null,
94
                'version' => 32,
95
            ],
96
            'Explicit list, low' => [
97
                'supported' => [29, 30, 31, 32],
98
                'incompatible' => null,
99
                'version' => 28,
100
            ],
101
            'Explicit list, high' => [
102
                'supported' => [29, 30, 31, 32],
103
                'incompatible' => null,
104
                'version' => 33,
105
            ],
106
            'Explicit list, in list' => [
107
                'supported' => [29, 30, 31, 32, 33],
108
                'incompatible' => null,
109
                'version' => 31,
110
            ],
111
            'Explicit list, missing value, unsupported' => [
112
                'supported' => [29, 30, 32],
113
                'incompatible' => null,
114
                'version' => 31,
115
            ],
116
            'Explicit list, missing value, supported' => [
117
                'supported' => [29, 30, 32],
118
                'incompatible' => null,
119
                'version' => 30,
120
            ],
121
        ];
122
    }
123
 
124
    /**
125
     * Test the load_disk_version function to check that it handles a variety of invalid incompatible fields.
126
     *
127
     * @dataProvider load_disk_version_invalid_incompatible_version_provider
128
     * @param mixed $incompatible
129
     */
130
    public function test_load_disk_version_invalid_incompatible_version($incompatible): void {
131
        $pluginman = testable_core_plugin_manager::instance();
132
 
133
        // Prepare a fake plugininfo instance.
134
        $plugininfo = new testable_plugininfo_base();
135
        $plugininfo->type = 'fake';
136
        $plugininfo->typerootdir = '/dev/null';
137
        $plugininfo->name = 'example';
138
        $plugininfo->rootdir = '/dev/null/fake';
139
        $plugininfo->pluginman = $pluginman;
140
        $plugininfo->versiondisk = 2015060600;
141
        $plugininfo->incompatible = $incompatible;
142
 
143
        $pluginman->add_fake_plugin_info($plugininfo);
144
 
145
        $this->expectException(\coding_exception::class);
146
        $this->expectExceptionMessage('Incorrect syntax in plugin incompatible declaration in example');
147
        $plugininfo->load_disk_version();
148
    }
149
 
150
    /**
151
     * Data provider for the load_disk_version tests for testing with invalid incompatible fields.
152
     *
153
     * @return array
154
     */
1441 ariadna 155
    public static function load_disk_version_invalid_incompatible_version_provider(): array {
1 efrain 156
        return [
157
            [[38]],
158
            [['38']],
159
            [3.8],
160
            ['3.8'],
161
            [''],
162
            ['somestring'],
163
        ];
164
 
165
    }
166
 
167
    /**
168
     * Test the load_disk_version function to check that it handles a range of correct supported and incompatible field
169
     * definitions.
170
     *
171
     * @dataProvider load_disk_version_branch_supports_provider
172
     * @param array|null $supported Supported versions to inject
173
     * @param string|int|null $incompatible Incompatible version to inject.
174
     * @param int $version Version to test
175
     */
176
    public function test_load_disk_version_branch_supports($supported, $incompatible, $version): void {
177
        $pluginman = testable_core_plugin_manager::instance();
178
 
179
        // Prepare a fake plugininfo instance.
180
        $plugininfo = new testable_plugininfo_base();
181
        $plugininfo->type = 'fake';
182
        $plugininfo->typerootdir = '/dev/null';
183
        $plugininfo->name = 'example';
184
        $plugininfo->rootdir = '/dev/null/fake';
185
        $plugininfo->pluginman = $pluginman;
186
        $plugininfo->versiondisk = 2015060600;
187
        $plugininfo->supported = $supported;
188
        $plugininfo->incompatible = $incompatible;
189
 
190
        $pluginman->add_fake_plugin_info($plugininfo);
191
 
192
        $plugininfo->load_disk_version();
193
 
194
        $this->assertEquals($supported, $plugininfo->supported);
195
        $this->assertEquals($incompatible, $plugininfo->incompatible);
196
    }
197
 
198
    /**
199
     * Test cases for tests of load_disk_version for testing the supported/incompatible fields.
200
     *
201
     * @return array
202
     */
1441 ariadna 203
    public static function load_disk_version_branch_supports_provider(): array {
1 efrain 204
        return [
205
            'Range, branch in support, lowest' => [
206
                'supported' => [29, 31],
207
                'incompatible' => null,
208
                'version' => 29,
209
            ],
210
            'Range, branch in support, mid' => [
211
                'supported' => [29, 31],
212
                'incompatible' => null,
213
                'version' => 30,
214
            ],
215
            'Range, branch in support, highest' => [
216
                'supported' => [29, 31],
217
                'incompatible' => null,
218
                'version' => 31,
219
            ],
220
 
221
            'Range, branch not in support, high' => [
222
                'supported' => [29, 31],
223
                'incompatible' => null,
224
                'version' => 32,
225
            ],
226
            'Range, branch not in support, low' => [
227
                'supported' => [29, 31],
228
                'incompatible' => null,
229
                'version' => 28,
230
            ],
231
            'Range, incompatible, high.' => [
232
                'supported' => [29, 31],
233
                'incompatible' => 32,
234
                'version' => 33,
235
            ],
236
            'Range, incompatible, low.' => [
237
                'supported' => [29, 31],
238
                'incompatible' => 32,
239
                'version' => 31,
240
            ],
241
            'Range, incompatible, equal.' => [
242
                'supported' => [29, 31],
243
                'incompatible' => 32,
244
                'version' => 32,
245
            ],
246
            'No supports' => [
247
                'supported' => null,
248
                'incompatible' => null,
249
                'version' => 32,
250
            ],
251
            'No supports, but incompatible, older' => [
252
                'supported' => null,
253
                'incompatible' => 30,
254
                'version' => 32,
255
            ],
256
            'No supports, but incompatible, equal' => [
257
                'supported' => null,
258
                'incompatible' => 32,
259
                'version' => 32,
260
            ],
261
            'No supports, but incompatible, newer' => [
262
                'supported' => null,
263
                'incompatible' => 34,
264
                'version' => 32,
265
            ],
266
            'String incompatible' => [
267
                'supported' => null,
268
                'incompatible' => '34',
269
                'version' => 32,
270
            ],
271
            'Empty incompatible' => [
272
                'supported' => null,
273
                'incompatible' => null,
274
                'version' => 32,
275
            ],
276
        ];
277
    }
278
 
279
    /**
280
     * Ensure that plugintype_supports_ordering() returns true.
281
     */
282
    public function test_plugintype_supports_ordering(): void {
283
        $this->assertFalse(base::plugintype_supports_ordering());
284
    }
285
 
286
    /**
287
     * Ensure that the base implementation is used for plugins not supporting ordering.
288
     *
289
     * @dataProvider plugins_not_supporting_ordering
290
     * @param string $plugin
291
     * @coversNothing
292
     *
293
     * Note: This test cannot declare coverage because it covers the various plugin implementations.
294
     */
295
    public function test_get_sorted_plugins(
296
        string $plugin,
297
    ): void {
298
        [$plugintype, $pluginname] = explode('_', $plugin, 2);
299
        $classname = \core_plugin_manager::resolve_plugininfo_class($plugintype);
300
 
301
        $this->assertFalse($classname::plugintype_supports_ordering());
302
 
303
        $this->assertNull($classname::get_sorted_plugins());
304
        $this->assertNull($classname::get_sorted_plugins(true));
305
        $this->assertNull($classname::get_sorted_plugins(false));
306
 
307
        $this->assertFalse($classname::change_plugin_order($pluginname, base::MOVE_UP));
308
        $this->assertFalse($classname::change_plugin_order($pluginname, base::MOVE_DOWN));
309
    }
310
 
311
    /**
312
     * Data provider for plugins_not_supporting_ordering.
313
     *
314
     * @return string[]
315
     */
1441 ariadna 316
    public static function plugins_not_supporting_ordering(): array {
1 efrain 317
        return [
318
            ['mod_assign'],
319
            ['block_login'],
320
        ];
321
    }
322
}