Debian Codegraph @

tests/phpunit/unit/includes/registration/VersionCheckerTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Registration\VersionChecker;
4
5 /**
6 * @covers \MediaWiki\Registration\VersionChecker
7 */
8 class VersionCheckerTest extends MediaWikiUnitTestCase {
9
10 /**
11 * @dataProvider provideMediaWikiCheck
12 */
13 public function testMediaWikiCheck( $coreVersion, $constraint, $expected ) {
14 $checker = new VersionChecker( $coreVersion, '7.0.0', [] );
15 $extDependencies = [ 'FakeExtension' => [ 'MediaWiki' => $constraint ] ];
16 $this->assertSame( $expected, $checker->checkArray( $extDependencies ) === [] );
17 }
18
19 public static function provideMediaWikiCheck() {
20 return [
21 // [ MediaWiki version, constraint, expected ]
22 [ '1.25alpha', '>= 1.26', false ],
23 [ '1.25.0', '>= 1.26', false ],
24 [ '1.26alpha', '>= 1.26', true ],
25 [ '1.26alpha', '>= 1.26.0', true ],
26 [ '1.26alpha', '>= 1.26.0-stable', false ],
27 [ '1.26.0', '>= 1.26.0-stable', true ],
28 [ '1.26.1', '>= 1.26.0-stable', true ],
29 [ '1.27.1', '>= 1.26.0-stable', true ],
30 [ '1.26alpha', '>= 1.26.1', false ],
31 [ '1.26alpha', '>= 1.26alpha', true ],
32 [ '1.26alpha', '>= 1.25', true ],
33 [ '1.26.0-alpha.14', '>= 1.26.0-alpha.15', false ],
34 [ '1.26.0-alpha.14', '>= 1.26.0-alpha.10', true ],
35 [ '1.26.1', '>= 1.26.2, <=1.26.0', false ],
36 [ '1.26.1', '^1.26.2', false ],
37 // Accept anything for un-parsable version strings
38 [ '1.26mwf14', '== 1.25alpha', true ],
39 [ 'totallyinvalid', '== 1.0', true ],
40 ];
41 }
42
43 /**
44 * @dataProvider providePhpValidCheck
45 */
46 public function testPhpValidCheck( $phpVersion, $constraint, $expected ) {
47 $checker = new VersionChecker( '1.0.0', $phpVersion, [] );
48 $extDependencies = [ 'FakeExtension' => [ 'platform' => [ 'php' => $constraint ] ] ];
49 $this->assertSame( $expected, $checker->checkArray( $extDependencies ) === [] );
50 }
51
52 public static function providePhpValidCheck() {
53 return [
54 // [ phpVersion, constraint, expected ]
55 [ '7.0.23', '>= 7.0.0', true ],
56 [ '7.0.23', '^7.1.0', false ],
57 [ '7.0.23', '7.0.23', true ],
58 ];
59 }
60
61 public function testPhpInvalidConstraint() {
62 $checker = new VersionChecker( '1.0.0', '7.0.0', [] );
63 $this->expectException( UnexpectedValueException::class );
64 $checker->checkArray( [
65 'FakeExtension' => [
66 'platform' => [
67 'php' => 'totallyinvalid',
68 ],
69 ],
70 ] );
71 }
72
73 /**
74 * @dataProvider providePhpInvalidVersion
75 */
76 public function testPhpInvalidVersion( $phpVersion ) {
77 $this->expectException( UnexpectedValueException::class );
78 $checker = new VersionChecker( '1.0.0', $phpVersion, [] );
79 }
80
81 public static function providePhpInvalidVersion() {
82 return [
83 // [ phpVersion ]
84 [ '7.abc' ],
85 [ '5.a.x' ],
86 ];
87 }
88
89 /**
90 * @dataProvider provideType
91 */
92 public function testType( $given, $expected ) {
93 $checker = new VersionChecker(
94 '1.0.0',
95 '7.0.0',
96 [ 'phpLoadedExtension' ],
97 [
98 'presentAbility' => true,
99 'presentAbilityWithMessage' => true,
100 'missingAbility' => false,
101 'missingAbilityWithMessage' => false,
102 ],
103 [
104 'presentAbilityWithMessage' => 'Present.',
105 'missingAbilityWithMessage' => 'Missing.',
106 ]
107 );
108 $checker->setLoadedExtensionsAndSkins( [
109 'FakeDependency' => [
110 'version' => '1.0.0',
111 ],
112 'NoVersionGiven' => [],
113 ] );
114 $this->assertEquals( $expected, $checker->checkArray( [
115 'FakeExtension' => $given,
116 ] ) );
117 }
118
119 public static function provideType() {
120 return [
121 // valid type
122 [
123 [
124 'extensions' => [
125 'FakeDependency' => '1.0.0',
126 ],
127 ],
128 [],
129 ],
130 [
131 [
132 'MediaWiki' => '1.0.0',
133 ],
134 [],
135 ],
136 [
137 [
138 'extensions' => [
139 'NoVersionGiven' => '*',
140 ],
141 ],
142 [],
143 ],
144 [
145 [
146 'extensions' => [
147 'NoVersionGiven' => '1.0',
148 ],
149 ],
150 [
151 [
152 'incompatible' => 'FakeExtension',
153 'type' => 'incompatible-extensions',
154 'msg' => 'NoVersionGiven does not expose its version, but FakeExtension requires: 1.0.',
155 ],
156 ],
157 ],
158 [
159 [
160 'extensions' => [
161 'Missing' => '*',
162 ],
163 ],
164 [
165 [
166 'missing' => 'Missing',
167 'type' => 'missing-extensions',
168 'msg' => 'FakeExtension requires Missing to be installed.',
169 ],
170 ],
171 ],
172 [
173 [
174 'extensions' => [
175 'FakeDependency' => '2.0.0',
176 ],
177 ],
178 [
179 [
180 'incompatible' => 'FakeExtension',
181 'type' => 'incompatible-extensions',
182 'msg' => 'FakeExtension is not compatible with the current installed version of FakeDependency (1.0.0), it requires: 2.0.0.',
183 ],
184 ],
185 ],
186 [
187 [
188 'skins' => [
189 'FakeSkin' => '*',
190 ],
191 ],
192 [
193 [
194 'missing' => 'FakeSkin',
195 'type' => 'missing-skins',
196 'msg' => 'FakeExtension requires FakeSkin to be installed.',
197 ],
198 ],
199 ],
200 [
201 [
202 'platform' => [
203 'ext-phpLoadedExtension' => '*',
204 ],
205 ],
206 [],
207 ],
208 [
209 [
210 'platform' => [
211 'ext-phpMissingExtension' => '*',
212 ],
213 ],
214 [
215 [
216 'missing' => 'phpMissingExtension',
217 'type' => 'missing-phpExtension',
218 'msg' => 'FakeExtension requires phpMissingExtension PHP extension to be installed.',
219 ],
220 ],
221 ],
222 [
223 [
224 'platform' => [
225 'ability-presentAbility' => true,
226 ],
227 ],
228 [],
229 ],
230 [
231 [
232 'platform' => [
233 'ability-presentAbilityWithMessage' => true,
234 ],
235 ],
236 [],
237 ],
238 [
239 [
240 'platform' => [
241 'ability-presentAbility' => false,
242 ],
243 ],
244 [],
245 ],
246 [
247 [
248 'platform' => [
249 'ability-presentAbilityWithMessage' => false,
250 ],
251 ],
252 [],
253 ],
254 [
255 [
256 'platform' => [
257 'ability-missingAbility' => true,
258 ],
259 ],
260 [
261 [
262 'missing' => 'missingAbility',
263 'type' => 'missing-ability',
264 'msg' => 'FakeExtension requires "missingAbility" ability',
265 ],
266 ],
267 ],
268 [
269 [
270 'platform' => [
271 'ability-missingAbilityWithMessage' => true,
272 ],
273 ],
274 [
275 [
276 'missing' => 'missingAbilityWithMessage',
277 'type' => 'missing-ability',
278 'msg' => 'FakeExtension requires "missingAbilityWithMessage" ability: Missing.',
279 ],
280 ],
281 ],
282 [
283 [
284 'platform' => [
285 'ability-missingAbility' => false,
286 ],
287 ],
288 [],
289 ],
290 [
291 [
292 'platform' => [
293 'ability-missingAbilityWithMessage' => false,
294 ],
295 ],
296 [],
297 ],
298 ];
299 }
300
301 /**
302 * Check, if a non-parsable version constraint does not throw an exception or
303 * returns any error message.
304 */
305 public function testInvalidConstraint() {
306 $checker = new VersionChecker( '1.0.0', '7.0.0', [] );
307 $checker->setLoadedExtensionsAndSkins( [
308 'FakeDependency' => [
309 'version' => 'not really valid',
310 ],
311 ] );
312 $this->assertEquals( [
313 [
314 'type' => 'invalid-version',
315 'msg' => "FakeDependency does not have a valid version string.",
316 ],
317 ], $checker->checkArray( [
318 'FakeExtension' => [
319 'extensions' => [
320 'FakeDependency' => '1.24.3',
321 ],
322 ],
323 ] ) );
324
325 $checker = new VersionChecker( '1.0.0', '7.0.0', [] );
326 $checker->setLoadedExtensionsAndSkins( [
327 'FakeDependency' => [
328 'version' => '1.24.3',
329 ],
330 ] );
331
332 $this->expectException( UnexpectedValueException::class );
333 $checker->checkArray( [
334 'FakeExtension' => [
335 'FakeDependency' => 'not really valid',
336 ],
337 ] );
338 }
339
340 public static function provideInvalidDependency() {
341 return [
342 [
343 [
344 'FakeExtension' => [
345 'platform' => [
346 'undefinedPlatformDependency' => '*',
347 ],
348 ],
349 ],
350 'undefinedPlatformDependency',
351 ],
352 [
353 [
354 'FakeExtension' => [
355 'platform' => [
356 'phpLoadedExtension' => '*',
357 ],
358 ],
359 ],
360 'phpLoadedExtension',
361 ],
362 [
363 [
364 'FakeExtension' => [
365 'platform' => [
366 'ability-invalidAbility' => true,
367 ],
368 ],
369 ],
370 'ability-invalidAbility',
371 ],
372 [
373 [
374 'FakeExtension' => [
375 'platform' => [
376 'presentAbility' => true,
377 ],
378 ],
379 ],
380 'presentAbility',
381 ],
382 [
383 [
384 'FakeExtension' => [
385 'undefinedDependencyType' => '*',
386 ],
387 ],
388 'undefinedDependencyType',
389 ],
390 // T197478
391 [
392 [
393 'FakeExtension' => [
394 'skin' => [
395 'FakeSkin' => '*',
396 ],
397 ],
398 ],
399 'skin',
400 ],
401 ];
402 }
403
404 /**
405 * @dataProvider provideInvalidDependency
406 */
407 public function testInvalidDependency( $dependency, $type ) {
408 $checker = new VersionChecker(
409 '1.0.0',
410 '7.0.0',
411 [ 'phpLoadedExtension' ],
412 [
413 'presentAbility' => true,
414 'missingAbility' => false,
415 ]
416 );
417 $this->expectException( UnexpectedValueException::class );
418 $this->expectExceptionMessage( "Dependency type $type unknown in FakeExtension" );
419 $checker->checkArray( $dependency );
420 }
421
422 public function testInvalidPhpExtensionConstraint() {
423 $checker = new VersionChecker( '1.0.0', '7.0.0', [ 'phpLoadedExtension' ] );
424 $this->expectException( UnexpectedValueException::class );
425 $this->expectExceptionMessage(
426 'Version constraints for PHP extensions are not supported in FakeExtension' );
427 $checker->checkArray( [
428 'FakeExtension' => [
429 'platform' => [
430 'ext-phpLoadedExtension' => '1.0.0',
431 ],
432 ],
433 ] );
434 }
435
436 /**
437 * @dataProvider provideInvalidAbilityType
438 */
439 public function testInvalidAbilityType( $value ) {
440 $checker = new VersionChecker( '1.0.0', '7.0.0', [], [ 'presentAbility' => true ] );
441 $this->expectException( UnexpectedValueException::class );
442 $this->expectExceptionMessage(
443 'Only booleans are allowed to to indicate the presence of abilities in FakeExtension' );
444 $checker->checkArray( [
445 'FakeExtension' => [
446 'platform' => [
447 'ability-presentAbility' => $value,
448 ],
449 ],
450 ] );
451 }
452
453 public static function provideInvalidAbilityType() {
454 return [
455 [ null ],
456 [ 1 ],
457 [ '1' ],
458 ];
459 }
460
461 }