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