Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Registration;
4
5 use Exception;
6 use InvalidArgumentException;
7 use MediaWiki\Json\FormatJson;
8 use MediaWiki\Registration\ExtensionProcessor;
9 use MediaWiki\Registration\ExtensionRegistry;
10 use MediaWikiUnitTestCase;
11 use RuntimeException;
12 use UnexpectedValueException;
13 use Wikimedia\TestingAccessWrapper;
14
15 /**
16 * @covers \MediaWiki\Registration\ExtensionProcessor
17 */
18 class ExtensionProcessorTest extends MediaWikiUnitTestCase {
19
20 private string $extensionPath;
21 private string $dirname;
22
23 protected function setUp(): void {
24 parent::setUp();
25 $this->extensionPath = self::getExtensionPath();
26 $this->dirname = dirname( $this->extensionPath );
27 }
28
29 private static function getExtensionPath() {
30 return __DIR__ . '/fixtures/FooBar/extension.json';
31 }
32
33 /**
34 * 'name' is absolutely required
35 *
36 * @var array
37 */
38 public static $default = [
39 'name' => 'FooBar',
40 ];
41
42 /**
43 * 'name' is absolutely required, and sometimes we require two distinct ones...
44 * @var array
45 */
46 public static $default2 = [
47 'name' => 'FooBar2',
48 ];
49
50 public function testExtractInfo() {
51 // Test that attributes that begin with @ are ignored
52 $processor = new ExtensionProcessor();
53 $processor->extractInfo( $this->extensionPath, self::$default + [
54 '@metadata' => [ 'foobarbaz' ],
55 'AnAttribute' => [ 'omg' ],
56 'AutoloadClasses' => [ 'FooBar' => 'includes/FooBar.php' ],
57 'AutoloadNamespaces' => [ '\Foo\Bar\\' => 'includes/foo/bar/' ],
58 'ForeignResourcesDir' => 'lib',
59 'SpecialPages' => [ 'Foo' => 'SpecialFoo' ],
60 'callback' => 'FooBar::onRegistration',
61 ], 1 );
62
63 $extracted = $processor->getExtractedInfo();
64 $attributes = $extracted['attributes'];
65 $this->assertArrayHasKey( 'AnAttribute', $attributes );
66 $this->assertArrayNotHasKey( '@metadata', $attributes );
67 $this->assertArrayNotHasKey( 'AutoloadClasses', $attributes );
68 $this->assertArrayNotHasKey( 'AutoloadNamespaces', $attributes );
69 $this->assertArrayHasKey( 'autoloaderPaths', $extracted );
70 $this->assertArrayHasKey( 'autoloaderClasses', $extracted );
71 $this->assertArrayHasKey( 'autoloaderNS', $extracted );
72 $this->assertSame(
73 [ 'FooBar' => dirname( $this->extensionPath ) . '/lib' ],
74 $attributes['ForeignResourcesDir']
75 );
76 $this->assertSame(
77 [ 'FooBar' => 'FooBar::onRegistration' ],
78 $extracted['callbacks']
79 );
80 $this->assertSame(
81 [ 'Foo' => 'SpecialFoo' ],
82 $extracted['globals']['wgSpecialPages']
83 );
84 }
85
86 public function testExtractAutoload() {
87 // Test that attributes that begin with @ are ignored
88 $processor = new ExtensionProcessor();
89 $processor->extractInfo( $this->extensionPath, self::$default + [
90 'AutoloadClasses' => [ 'FooBar' => 'includes/FooBar.php' ],
91 'AutoloadNamespaces' => [ '\Foo\Bar\\' => 'includes/foo/bar/' ],
92 'TestAutoloadClasses' => [ 'FooBarTest' => 'tests/FooBarTest.php' ],
93 'TestAutoloadNamespaces' => [ '\Foo\Bar\Test\\' => 'tests/foo/bar/' ],
94 'load_composer_autoloader' => true,
95 ], 1 );
96
97 $extracted = $processor->getExtractedInfo();
98 $this->assertArrayHasKey( 'autoloaderPaths', $extracted );
99 $this->assertArrayHasKey( 'autoloaderClasses', $extracted );
100 $this->assertArrayHasKey( 'autoloaderNS', $extracted );
101 $this->assertSame(
102 [ 'FooBar' => $this->dirname . '/includes/FooBar.php' ],
103 $extracted['autoloaderClasses']
104 );
105 $this->assertSame(
106 [ '\Foo\Bar\\' => $this->dirname . '/includes/foo/bar/' ],
107 $extracted['autoloaderNS']
108 );
109 $this->assertSame(
110 [ $this->dirname . '/vendor/autoload.php' ],
111 $extracted['autoloaderPaths']
112 );
113
114 $extracted = $processor->getExtractedInfo( true );
115 $this->assertArrayHasKey( 'autoloaderPaths', $extracted );
116 $this->assertArrayHasKey( 'autoloaderClasses', $extracted );
117 $this->assertArrayHasKey( 'autoloaderNS', $extracted );
118 $this->assertSame(
119 [
120 'FooBar' => $this->dirname . '/includes/FooBar.php',
121 'FooBarTest' => $this->dirname . '/tests/FooBarTest.php'
122 ],
123 $extracted['autoloaderClasses']
124 );
125 $this->assertSame(
126 [
127 '\Foo\Bar\\' => $this->dirname . '/includes/foo/bar/',
128 '\Foo\Bar\Test\\' => $this->dirname . '/tests/foo/bar/'
129 ],
130 $extracted['autoloaderNS']
131 );
132 $this->assertSame(
133 [ $this->dirname . '/vendor/autoload.php' ],
134 $extracted['autoloaderPaths']
135 );
136 }
137
138 public function testExtractSkins() {
139 $processor = new ExtensionProcessor();
140 $processor->extractInfo( $this->extensionPath, self::$default + [
141 'ValidSkinNames' => [
142 'test-vector' => [
143 'class' => 'SkinTestVector',
144 ],
145 'test-vector-empty-args' => [
146 'class' => 'SkinTestVector',
147 'args' => []
148 ],
149 'test-vector-empty-options' => [
150 'class' => 'SkinTestVector',
151 'args' => [
152 []
153 ]
154 ],
155 'test-vector-skin-relative' => [
156 'class' => 'SkinTestVector',
157 'args' => [
158 [
159 'templateDirectory' => 'templates',
160 ]
161 ]
162 ],
163 'test-vector-string' => 'TestVector',
164 ]
165 ], 1 );
166 $extracted = $processor->getExtractedInfo();
167 $validSkins = $extracted['globals']['wgValidSkinNames'];
168
169 $this->assertArrayHasKey( 'test-vector', $validSkins );
170 $this->assertArrayHasKey( 'test-vector-empty-args', $validSkins );
171 $this->assertArrayHasKey( 'test-vector-skin-relative', $validSkins );
172 $this->assertSame(
173 $this->dirname . '/templates',
174 $validSkins['test-vector-empty-options']['args'][0]['templateDirectory'],
175 'A sensible default is provided.'
176 );
177 $this->assertSame(
178 $this->dirname . '/templates',
179 $validSkins['test-vector-skin-relative']['args'][0]['templateDirectory'],
180 'modified'
181 );
182 $this->assertSame( 'TestVector', $validSkins['test-vector-string'] );
183 }
184
185 public function testExtractNamespaces() {
186 // Test that namespace IDs defined in extension.json can be overwritten locally
187 if ( !defined( 'MW_EXTENSION_PROCESSOR_TEST_EXTRACT_INFO_X' ) ) {
188 define( 'MW_EXTENSION_PROCESSOR_TEST_EXTRACT_INFO_X', 123456 );
189 }
190
191 $processor = new ExtensionProcessor();
192 $processor->extractInfo( $this->extensionPath, self::$default + [
193 'namespaces' => [
194 [
195 'id' => 332200,
196 'constant' => 'MW_EXTENSION_PROCESSOR_TEST_EXTRACT_INFO_A',
197 'name' => 'Test_A',
198 'defaultcontentmodel' => 'TestModel',
199 'gender' => [
200 'male' => 'Male test',
201 'female' => 'Female test',
202 ],
203 'subpages' => true,
204 'content' => true,
205 'protection' => 'userright',
206 ],
207 [ // Test_X will use ID 123456 not 334400
208 'id' => 334400,
209 'constant' => 'MW_EXTENSION_PROCESSOR_TEST_EXTRACT_INFO_X',
210 'name' => 'Test_X',
211 'defaultcontentmodel' => 'TestModel'
212 ],
213 ]
214 ], 1 );
215
216 $extracted = $processor->getExtractedInfo();
217
218 $this->assertArrayHasKey(
219 'MW_EXTENSION_PROCESSOR_TEST_EXTRACT_INFO_A',
220 $extracted['defines']
221 );
222 $this->assertArrayHasKey(
223 'MW_EXTENSION_PROCESSOR_TEST_EXTRACT_INFO_X',
224 $extracted['defines']
225 );
226
227 $this->assertSame(
228 123456,
229 $extracted['defines']['MW_EXTENSION_PROCESSOR_TEST_EXTRACT_INFO_X']
230 );
231
232 $this->assertSame(
233 332200,
234 $extracted['defines']['MW_EXTENSION_PROCESSOR_TEST_EXTRACT_INFO_A']
235 );
236
237 $this->assertArrayHasKey( 'ExtensionNamespaces', $extracted['attributes'] );
238 $this->assertArrayHasKey( 123456, $extracted['attributes']['ExtensionNamespaces'] );
239 $this->assertArrayHasKey( 332200, $extracted['attributes']['ExtensionNamespaces'] );
240 $this->assertArrayNotHasKey( 334400, $extracted['attributes']['ExtensionNamespaces'] );
241
242 $this->assertSame( 'Test_X', $extracted['attributes']['ExtensionNamespaces'][123456] );
243 $this->assertSame( 'Test_A', $extracted['attributes']['ExtensionNamespaces'][332200] );
244 $this->assertSame(
245 [ 'male' => 'Male test', 'female' => 'Female test' ],
246 $extracted['globals']['wgExtraGenderNamespaces'][332200]
247 );
248 // A has subpages, X does not
249 $this->assertTrue( $extracted['globals']['wgNamespacesWithSubpages'][332200] );
250 $this->assertArrayNotHasKey( 123456, $extracted['globals']['wgNamespacesWithSubpages'] );
251 }
252
253 public function testRateLimits() {
254 $processor = new ExtensionProcessor();
255 $processor->extractInfo(
256 '',
257 [
258 'name' => 'Foo',
259 'RateLimits' => [
260 'test1' => [
261 "user" => [ 1, 7200 ]
262 ],
263 'test2' => [
264 "user" => [ 2, 7200 ]
265 ],
266 ],
267 'AvailableRights' => [
268 'test2',
269 ]
270 ] + self::$default,
271 2
272 );
273
274 $processor->extractInfo(
275 'Bar',
276 [
277 'name' => 'Bar',
278 'RateLimits' => [
279 'test3' => [
280 "user" => [ 1, 7200 ]
281 ],
282 ],
283 ] + self::$default,
284 2
285 );
286
287 $extracted = $processor->getExtractedInfo();
288
289 $this->assertArrayHasKey( 'wgRateLimits', $extracted['globals'] );
290 $this->assertArrayHasKey( 'wgImplicitRights', $extracted['globals'] );
291 $this->assertArrayHasKey( 'wgAvailableRights', $extracted['globals'] );
292
293 $this->assertArrayHasKey( 'test1', $extracted['globals']['wgRateLimits'] );
294 $this->assertArrayHasKey( 'test2', $extracted['globals']['wgRateLimits'] );
295 $this->assertArrayHasKey( 'test3', $extracted['globals']['wgRateLimits'] );
296
297 $this->assertContains( 'test2', $extracted['globals']['wgAvailableRights'] );
298 $this->assertNotContains( 'test1', $extracted['globals']['wgAvailableRights'] );
299
300 $this->assertContains( 'test1', $extracted['globals']['wgImplicitRights'] );
301 $this->assertContains( 'test3', $extracted['globals']['wgImplicitRights'] );
302 $this->assertNotContains( 'test2', $extracted['globals']['wgImplicitRights'] );
303 }
304
305 public function provideMixedStyleHooks() {
306 // Format:
307 // Content in extension.json
308 // Expected wgHooks
309 // Expected Hooks
310 return [
311 [
312 [
313 'Hooks' => [ 'FooBaz' => [
314 [ 'handler' => 'HandlerObjectCallback' ],
315 [ 'handler' => 'HandlerObjectCallback', 'deprecated' => true ],
316 'HandlerObjectCallback',
317 [ 'FooClass', 'FooMethod' ],
318 'GlobalLegacyFunction',
319 'FooClass',
320 "FooClass::staticMethod"
321 ] ],
322 'HookHandlers' => [
323 'HandlerObjectCallback' => [ 'class' => 'FooClass', 'services' => [] ]
324 ]
325 ] + self::$default,
326 [
327 'FooBaz' => [
328 [ 'FooClass', 'FooMethod' ],
329 'GlobalLegacyFunction',
330 'FooClass',
331 'FooClass::staticMethod'
332 ]
333 ] + [ ExtensionRegistry::MERGE_STRATEGY => 'array_merge_recursive' ],
334 [
335 'FooBaz' => [
336 [
337 'handler' => [
338 'class' => 'FooClass',
339 'services' => [],
340 'name' => 'FooBar-HandlerObjectCallback'
341 ],
342 'extensionPath' => $this->getExtensionPath()
343 ],
344 [
345 'handler' => [
346 'class' => 'FooClass',
347 'services' => [],
348 'name' => 'FooBar-HandlerObjectCallback'
349 ],
350 'deprecated' => true,
351 'extensionPath' => $this->getExtensionPath()
352 ],
353 [
354 'handler' => [
355 'class' => 'FooClass',
356 'services' => [],
357 'name' => 'FooBar-HandlerObjectCallback'
358 ],
359 'extensionPath' => $this->getExtensionPath()
360 ]
361 ]
362 ]
363 ]
364 ];
365 }
366
367 public function provideNonLegacyHooks() {
368 // Format:
369 // Current Hooks attribute
370 // Content in extension.json
371 // Expected Hooks attribute
372 return [
373 // Hook for "FooBaz": object with handler attribute
374 [
375 [ 'FooBaz' => [ 'PriorCallback' ] ],
376 [
377 'Hooks' => [ 'FooBaz' => [ 'handler' => 'HandlerObjectCallback', 'deprecated' => true ] ],
378 'HookHandlers' => [
379 'HandlerObjectCallback' => [
380 'class' => 'FooClass',
381 'services' => [],
382 'name' => 'FooBar-HandlerObjectCallback'
383 ]
384 ]
385 ] + self::$default,
386 [ 'FooBaz' =>
387 [
388 'PriorCallback',
389 [
390 'handler' => [
391 'class' => 'FooClass',
392 'services' => [],
393 'name' => 'FooBar-HandlerObjectCallback'
394 ],
395 'deprecated' => true,
396 'extensionPath' => $this->getExtensionPath()
397 ]
398 ]
399 ],
400 []
401 ],
402 // Hook for "FooBaz": string corresponding to a handler definition
403 [
404 [ 'FooBaz' => [ 'PriorCallback' ] ],
405 [
406 'Hooks' => [ 'FooBaz' => [ 'HandlerObjectCallback' ] ],
407 'HookHandlers' => [
408 'HandlerObjectCallback' => [ 'class' => 'FooClass', 'services' => [] ],
409 ]
410 ] + self::$default,
411 [ 'FooBaz' =>
412 [
413 'PriorCallback',
414 [
415 'handler' => [
416 'class' => 'FooClass',
417 'services' => [],
418 'name' => 'FooBar-HandlerObjectCallback'
419 ],
420 'extensionPath' => $this->getExtensionPath()
421 ],
422 ]
423 ],
424 []
425 ],
426 // Hook for "FooBaz", string corresponds to handler def. and object with handler attribute
427 [
428 [ 'FooBaz' => [ 'PriorCallback' ] ],
429 [
430 'Hooks' => [ 'FooBaz' => [
431 [ 'handler' => 'HandlerObjectCallback', 'deprecated' => true ],
432 'HandlerObjectCallback2'
433 ] ],
434 'HookHandlers' => [
435 'HandlerObjectCallback2' => [ 'class' => 'FooClass', 'services' => [] ],
436 'HandlerObjectCallback' => [ 'class' => 'FooClass', 'services' => [] ],
437 ]
438 ] + self::$default,
439 [ 'FooBaz' =>
440 [
441 'PriorCallback',
442 [
443 'handler' => [
444 'name' => 'FooBar-HandlerObjectCallback',
445 'class' => 'FooClass',
446 'services' => []
447 ],
448 'deprecated' => true,
449 'extensionPath' => $this->getExtensionPath()
450 ],
451 [
452 'handler' => [
453 'name' => 'FooBar-HandlerObjectCallback2',
454 'class' => 'FooClass',
455 'services' => [],
456 ],
457 'extensionPath' => $this->getExtensionPath()
458 ]
459 ]
460 ],
461 []
462 ],
463 // Hook for "FooBaz": string corresponding to a new-style handler definition
464 // and legacy style object and method array
465 [
466 [ 'FooBaz' => [ 'PriorCallback' ] ],
467 [
468 'Hooks' => [ 'FooBaz' => [
469 'HandlerObjectCallback',
470 [ 'FooClass', 'FooMethod ' ]
471 ] ],
472 'HookHandlers' => [
473 'HandlerObjectCallback' => [ 'class' => 'FooClass', 'services' => [] ],
474 ]
475 ] + self::$default,
476 [ 'FooBaz' =>
477 [
478 'PriorCallback',
479 [
480 'handler' => [
481 'name' => 'FooBar-HandlerObjectCallback',
482 'class' => 'FooClass',
483 'services' => []
484 ],
485 'extensionPath' => $this->getExtensionPath()
486 ],
487 ]
488 ],
489 [ 'FooClass', 'FooMethod' ]
490 ]
491 ];
492 }
493
494 public static function provideLegacyHooks() {
495 $merge = [ ExtensionRegistry::MERGE_STRATEGY => 'array_merge_recursive' ];
496 // Format:
497 // Current $wgHooks
498 // Content in extension.json
499 // Expected value of $wgHooks
500 return [
501 // No hooks
502 [
503 [],
504 self::$default,
505 $merge,
506 ],
507 // No current hooks, adding one for "FooBaz" in string format
508 [
509 [],
510 [ 'Hooks' => [ 'FooBaz' => 'FooBazCallback' ] ] + self::$default,
511 [ 'FooBaz' => [ 'FooBazCallback' ] ] + $merge,
512 ],
513 // Hook for "FooBaz", adding another one
514 [
515 [ 'FooBaz' => [ 'PriorCallback' ] ],
516 [ 'Hooks' => [ 'FooBaz' => 'FooBazCallback' ] ] + self::$default,
517 [ 'FooBaz' => [ 'PriorCallback', 'FooBazCallback' ] ] + $merge,
518 ],
519 // No current hooks, adding one for "FooBaz" in verbose array format
520 [
521 [],
522 [ 'Hooks' => [ 'FooBaz' => [ 'FooBazCallback' ] ] ] + self::$default,
523 [ 'FooBaz' => [ 'FooBazCallback' ] ] + $merge,
524 ],
525 // Hook for "BarBaz", adding one for "FooBaz"
526 [
527 [ 'BarBaz' => [ 'BarBazCallback' ] ],
528 [ 'Hooks' => [ 'FooBaz' => 'FooBazCallback' ] ] + self::$default,
529 [
530 'BarBaz' => [ 'BarBazCallback' ],
531 'FooBaz' => [ 'FooBazCallback' ],
532 ] + $merge,
533 ],
534 // Callbacks for FooBaz wrapped in an array
535 [
536 [],
537 [ 'Hooks' => [ 'FooBaz' => [ 'Callback1' ] ] ] + self::$default,
538 [
539 'FooBaz' => [ 'Callback1' ],
540 ] + $merge,
541 ],
542 // Multiple callbacks for FooBaz hook
543 [
544 [],
545 [ 'Hooks' => [ 'FooBaz' => [ 'Callback1', 'Callback2' ] ] ] + self::$default,
546 [
547 'FooBaz' => [ 'Callback1', 'Callback2' ],
548 ] + $merge,
549 ],
550 ];
551 }
552
553 /**
554 * @dataProvider provideNonLegacyHooks
555 */
556 public function testNonLegacyHooks( $pre, $info, $expected ) {
557 $processor = new MockExtensionProcessor( [ 'attributes' => [ 'Hooks' => $pre ] ] );
558 $processor->extractInfo( $this->extensionPath, $info, 1 );
559 $extracted = $processor->getExtractedInfo();
560 $this->assertEquals( $expected, $extracted['attributes']['Hooks'] );
561 }
562
563 /**
564 * @dataProvider provideMixedStyleHooks
565 */
566 public function testMixedStyleHooks( $info, $expectedWgHooks, $expectedNewHooks ) {
567 $processor = new MockExtensionProcessor();
568 $processor->extractInfo( $this->extensionPath, $info, 1 );
569 $extracted = $processor->getExtractedInfo();
570 $this->assertEquals( $expectedWgHooks, $extracted['globals']['wgHooks'] );
571 $this->assertEquals( $expectedNewHooks, $extracted['attributes']['Hooks'] );
572 }
573
574 /**
575 * @dataProvider provideLegacyHooks
576 */
577 public function testLegacyHooks( $pre, $info, $expected ) {
578 $preset = [ 'globals' => [ 'wgHooks' => $pre ] ];
579 $processor = new MockExtensionProcessor( $preset );
580 $processor->extractInfo( $this->extensionPath, $info, 1 );
581 $extracted = $processor->getExtractedInfo();
582 $this->assertEquals( $expected, $extracted['globals']['wgHooks'] );
583 }
584
585 public function testRegisterHandlerWithoutDefinition() {
586 $info = [
587 'Hooks' => [ 'FooBaz' => [ 'handler' => 'NoHandlerDefinition' ] ],
588 'HookHandlers' => []
589 ] + self::$default;
590 $processor = new MockExtensionProcessor();
591 $this->expectException( UnexpectedValueException::class );
592 $this->expectExceptionMessage(
593 'Missing handler definition for FooBaz in HookHandlers attribute'
594 );
595 $processor->extractInfo( $this->extensionPath, $info, 1 );
596 $processor->getExtractedInfo();
597 }
598
599 public function testExtractConfig1() {
600 $processor = new ExtensionProcessor();
601 $info = [
602 'config' => [
603 'Bar' => 'somevalue',
604 'Foo' => 10,
605 '@IGNORED' => 'yes',
606 ],
607 ] + self::$default;
608 $info2 = [
609 'config' => [
610 '_prefix' => 'eg',
611 'Bar' => 'somevalue'
612 ],
613 ] + self::$default2;
614 $processor->extractInfo( $this->extensionPath, $info, 1 );
615 $processor->extractInfo( $this->extensionPath, $info2, 1 );
616 $extracted = $processor->getExtractedInfo();
617 $this->assertEquals( 'somevalue', $extracted['globals']['wgBar'] );
618 $this->assertEquals( 10, $extracted['globals']['wgFoo'] );
619 $this->assertArrayNotHasKey( 'wg@IGNORED', $extracted['globals'] );
620 // Custom prefix:
621 $this->assertEquals( 'somevalue', $extracted['globals']['egBar'] );
622 }
623
624 public function testExtractConfig2() {
625 $processor = new ExtensionProcessor();
626 $info = [
627 'config' => [
628 'Bar' => [ 'value' => 'somevalue' ],
629 'Foo' => [ 'value' => 10 ],
630 'Path' => [ 'value' => 'foo.txt', 'path' => true ],
631 'PathArray' => [ 'value' => [ 'foo.bar', 'bar.foo', 'bar/foo.txt' ], 'path' => true ],
632 'Namespaces' => [
633 'value' => [
634 '10' => true,
635 '12' => false,
636 ],
637 'merge_strategy' => 'array_plus',
638 ],
639 ],
640 ] + self::$default;
641 $info2 = [
642 'config' => [
643 'Bar' => [ 'value' => 'somevalue' ],
644 ],
645 'config_prefix' => 'eg',
646 ] + self::$default2;
647 $processor->extractInfo( $this->extensionPath, $info, 2 );
648 $processor->extractInfo( $this->extensionPath, $info2, 2 );
649 $extracted = $processor->getExtractedInfo();
650 $this->assertEquals( 'somevalue', $extracted['globals']['wgBar'] );
651 $this->assertEquals( 10, $extracted['globals']['wgFoo'] );
652 $this->assertEquals( "{$this->dirname}/foo.txt", $extracted['globals']['wgPath'] );
653 $this->assertEquals(
654 [
655 "{$this->dirname}/foo.bar",
656 "{$this->dirname}/bar.foo",
657 "{$this->dirname}/bar/foo.txt"
658 ],
659 $extracted['globals']['wgPathArray']
660 );
661 // Custom prefix:
662 $this->assertEquals( 'somevalue', $extracted['globals']['egBar'] );
663 $this->assertSame(
664 [ 10 => true, 12 => false, ExtensionRegistry::MERGE_STRATEGY => 'array_plus' ],
665 $extracted['globals']['wgNamespaces']
666 );
667 }
668
669 public function testDuplicateConfigKey1() {
670 $processor = new ExtensionProcessor();
671 $info = [
672 'config' => [
673 'Bar' => '',
674 ]
675 ] + self::$default;
676 $info2 = [
677 'config' => [
678 'Bar' => 'g',
679 ],
680 ] + self::$default2;
681 $this->expectException( RuntimeException::class );
682 $processor->extractInfo( $this->extensionPath, $info, 1 );
683 $processor->extractInfo( $this->extensionPath, $info2, 1 );
684 }
685
686 public function testDuplicateConfigKey2() {
687 $processor = new ExtensionProcessor();
688 $info = [
689 'config' => [
690 'Bar' => [ 'value' => 'somevalue' ],
691 ]
692 ] + self::$default;
693 $info2 = [
694 'config' => [
695 'Bar' => [ 'value' => 'somevalue' ],
696 ],
697 ] + self::$default2;
698 $this->expectException( RuntimeException::class );
699 $processor->extractInfo( $this->extensionPath, $info, 2 );
700 $processor->extractInfo( $this->extensionPath, $info2, 2 );
701 }
702
703 public static function provideExtractExtensionMessagesFiles() {
704 $dir = dirname( self::getExtensionPath() );
705 return [
706 [
707 [ 'ExtensionMessagesFiles' => [ 'FooBarAlias' => 'FooBar.alias.php' ] ],
708 [ 'wgExtensionMessagesFiles' => [ 'FooBarAlias' => $dir . '/FooBar.alias.php' ] ]
709 ],
710 [
711 [
712 'ExtensionMessagesFiles' => [
713 'FooBarAlias' => 'FooBar.alias.php',
714 'FooBarMagic' => 'FooBar.magic.i18n.php',
715 ],
716 ],
717 [
718 'wgExtensionMessagesFiles' => [
719 'FooBarAlias' => $dir . '/FooBar.alias.php',
720 'FooBarMagic' => $dir . '/FooBar.magic.i18n.php',
721 ],
722 ],
723 ],
724 ];
725 }
726
727 public static function provideExtractRestModuleFiles() {
728 $dir = dirname( self::getExtensionPath() );
729 return [
730 [
731 [ 'RestModuleFiles' => [ 'FooBar.json' ] ],
732 [ 'wgRestAPIAdditionalRouteFiles' => [ $dir . '/FooBar.json' ] ]
733 ],
734 [
735 [
736 'RestModuleFiles' => [
737 'FooBar.json',
738 'Xyzzy.json',
739 ],
740 ],
741 [
742 'wgRestAPIAdditionalRouteFiles' => [
743 $dir . '/FooBar.json',
744 $dir . '/Xyzzy.json',
745 ],
746 ],
747 ],
748 ];
749 }
750
751 public static function provideExtractMessagesDirs() {
752 $dir = dirname( self::getExtensionPath() );
753 return [
754 [
755 [ 'MessagesDirs' => [ 'VisualEditor' => 'i18n' ] ],
756 [ 'wgMessagesDirs' => [ 'VisualEditor' => [ $dir . '/i18n' ] ] ]
757 ],
758 [
759 [ 'MessagesDirs' => [ 'VisualEditor' => [ 'i18n', 'foobar' ] ] ],
760 [ 'wgMessagesDirs' => [ 'VisualEditor' => [ $dir . '/i18n', $dir . '/foobar' ] ] ]
761 ],
762 ];
763 }
764
765 /**
766 * @dataProvider provideExtractExtensionMessagesFiles
767 * @dataProvider provideExtractRestModuleFiles
768 * @dataProvider provideExtractMessagesDirs
769 */
770 public function testExtractFilesAndDirs( $input, $expected ) {
771 $processor = new ExtensionProcessor();
772 $processor->extractInfo( $this->extensionPath, $input + self::$default, 1 );
773 $out = $processor->getExtractedInfo();
774 foreach ( $expected as $key => $value ) {
775 $this->assertEquals( $value, $out['globals'][$key] );
776 }
777 }
778
779 public function testExtractCredits() {
780 $processor = new ExtensionProcessor();
781 $processor->extractInfo( $this->extensionPath, self::$default, 1 );
782 $this->expectException( Exception::class );
783 $processor->extractInfo( $this->extensionPath, self::$default, 1 );
784 }
785
786 /**
787 * @dataProvider provideExtractResourceLoaderModules
788 */
789 public function testExtractResourceLoaderModules(
790 $input,
791 array $expectedGlobals,
792 array $expectedAttribs = []
793 ) {
794 $processor = new ExtensionProcessor();
795 $processor->extractInfo( $this->extensionPath, $input + self::$default, 1 );
796 $out = $processor->getExtractedInfo();
797 foreach ( $expectedGlobals as $key => $value ) {
798 $this->assertEquals( $value, $out['globals'][$key] );
799 }
800 foreach ( $expectedAttribs as $key => $value ) {
801 $this->assertEquals( $value, $out['attributes'][$key] );
802 }
803 }
804
805 public static function provideExtractResourceLoaderModules() {
806 $dir = dirname( self::getExtensionPath() );
807 return [
808 // Generic module with localBasePath/remoteExtPath specified
809 [
810 // Input
811 [
812 'ResourceModules' => [
813 'test.foo' => [
814 'styles' => 'foobar.js',
815 'localBasePath' => '',
816 'remoteExtPath' => 'FooBar',
817 ],
818 ],
819 ],
820 // Expected
821 [],
822 [
823 'ResourceModules' => [
824 'test.foo' => [
825 'styles' => 'foobar.js',
826 'localBasePath' => $dir,
827 'remoteExtPath' => 'FooBar',
828 ],
829 ],
830 ],
831 ],
832 // ResourceFileModulePaths specified:
833 [
834 // Input
835 [
836 'ResourceFileModulePaths' => [
837 'localBasePath' => 'modules',
838 'remoteExtPath' => 'FooBar/modules',
839 ],
840 'ResourceModules' => [
841 // No paths
842 'test.foo' => [
843 'styles' => 'foo.js',
844 ],
845 // Different paths set
846 'test.bar' => [
847 'styles' => 'bar.js',
848 'localBasePath' => 'subdir',
849 'remoteExtPath' => 'FooBar/subdir',
850 ],
851 // Custom class with no paths set
852 'test.class' => [
853 'class' => 'FooBarModule',
854 'extra' => 'argument',
855 ],
856 // Custom class with a localBasePath
857 'test.class.with.path' => [
858 'class' => 'FooBarPathModule',
859 'extra' => 'argument',
860 'localBasePath' => '',
861 ]
862 ],
863 ],
864 // Expected
865 [],
866 [
867 'ResourceModules' => [
868 'test.foo' => [
869 'styles' => 'foo.js',
870 'localBasePath' => "$dir/modules",
871 'remoteExtPath' => 'FooBar/modules',
872 ],
873 'test.bar' => [
874 'styles' => 'bar.js',
875 'localBasePath' => "$dir/subdir",
876 'remoteExtPath' => 'FooBar/subdir',
877 ],
878 'test.class' => [
879 'class' => 'FooBarModule',
880 'extra' => 'argument',
881 'localBasePath' => "$dir/modules",
882 'remoteExtPath' => 'FooBar/modules',
883 ],
884 'test.class.with.path' => [
885 'class' => 'FooBarPathModule',
886 'extra' => 'argument',
887 'localBasePath' => $dir,
888 'remoteExtPath' => 'FooBar/modules',
889 ]
890 ],
891 ],
892 ],
893 // ResourceModuleSkinStyles with file module paths
894 [
895 // Input
896 [
897 'ResourceFileModulePaths' => [
898 'localBasePath' => '',
899 'remoteSkinPath' => 'FooBar',
900 ],
901 'ResourceModuleSkinStyles' => [
902 'foobar' => [
903 'test.foo' => 'foo.css',
904 ]
905 ],
906 ],
907 // Expected
908 [],
909 [
910 'ResourceModuleSkinStyles' => [
911 'foobar' => [
912 'test.foo' => 'foo.css',
913 'localBasePath' => $dir,
914 'remoteSkinPath' => 'FooBar',
915 ],
916 ],
917 ],
918 ],
919 // ResourceModuleSkinStyles with file module paths and an override
920 [
921 // Input
922 [
923 'ResourceFileModulePaths' => [
924 'localBasePath' => '',
925 'remoteSkinPath' => 'FooBar',
926 ],
927 'ResourceModuleSkinStyles' => [
928 'foobar' => [
929 'test.foo' => 'foo.css',
930 'remoteSkinPath' => 'BarFoo'
931 ],
932 ],
933 ],
934 // Expected
935 [],
936 [
937 'ResourceModuleSkinStyles' => [
938 'foobar' => [
939 'test.foo' => 'foo.css',
940 'localBasePath' => $dir,
941 'remoteSkinPath' => 'BarFoo',
942 ],
943 ],
944 ],
945 ],
946 'QUnit test module' => [
947 // Input
948 [
949 'QUnitTestModule' => [
950 'localBasePath' => '',
951 'remoteExtPath' => 'Foo',
952 'scripts' => 'bar.js',
953 ],
954 ],
955 // Expected
956 [],
957 [
958 'QUnitTestModules' => [
959 'test.FooBar' => [
960 'localBasePath' => $dir,
961 'remoteExtPath' => 'Foo',
962 'scripts' => 'bar.js',
963 ],
964 ],
965 ],
966 ],
967 ];
968 }
969
970 public static function provideSetToGlobal() {
971 return [
972 [
973 [ 'wgAPIModules', 'wgAvailableRights' ],
974 [],
975 [
976 'APIModules' => [ 'foobar' => 'ApiFooBar' ],
977 'AvailableRights' => [ 'foobar', 'unfoobar' ],
978 ],
979 [
980 'wgAPIModules' => [ 'foobar' => 'ApiFooBar' ],
981 'wgAvailableRights' => [ 'foobar', 'unfoobar' ],
982 ],
983 ],
984 [
985 [ 'wgAPIModules', 'wgAvailableRights' ],
986 [
987 'wgAPIModules' => [ 'barbaz' => 'ApiBarBaz' ],
988 'wgAvailableRights' => [ 'barbaz' ]
989 ],
990 [
991 'APIModules' => [ 'foobar' => 'ApiFooBar' ],
992 'AvailableRights' => [ 'foobar', 'unfoobar' ],
993 ],
994 [
995 'wgAPIModules' => [ 'barbaz' => 'ApiBarBaz', 'foobar' => 'ApiFooBar' ],
996 'wgAvailableRights' => [ 'barbaz', 'foobar', 'unfoobar' ],
997 ],
998 ],
999 [
1000 [ 'wgGroupPermissions' ],
1001 [
1002 'wgGroupPermissions' => [
1003 'sysop' => [ 'delete' ]
1004 ],
1005 ],
1006 [
1007 'GroupPermissions' => [
1008 'sysop' => [ 'undelete' ],
1009 'user' => [ 'edit' ]
1010 ],
1011 ],
1012 [
1013 'wgGroupPermissions' => [
1014 'sysop' => [ 'delete', 'undelete' ],
1015 'user' => [ 'edit' ]
1016 ],
1017 ]
1018 ]
1019 ];
1020 }
1021
1022 /**
1023 * Attributes under manifest_version 2
1024 */
1025 public function testExtractAttributes() {
1026 $processor = new ExtensionProcessor();
1027 // Load FooBar extension
1028 $processor->extractInfo( $this->extensionPath, self::$default, 2 );
1029 $processor->extractInfo(
1030 $this->extensionPath,
1031 [
1032 'name' => 'Baz',
1033 'attributes' => [
1034 // Loaded
1035 'FooBar' => [
1036 'Plugins' => [
1037 'ext.baz.foobar',
1038 ],
1039 ],
1040 // Not loaded
1041 'FizzBuzz' => [
1042 'MorePlugins' => [
1043 'ext.baz.fizzbuzz',
1044 ],
1045 ],
1046 ],
1047 ],
1048 2
1049 );
1050
1051 $info = $processor->getExtractedInfo();
1052 $this->assertArrayHasKey( 'FooBarPlugins', $info['attributes'] );
1053 $this->assertSame( [ 'ext.baz.foobar' ], $info['attributes']['FooBarPlugins'] );
1054 $this->assertArrayNotHasKey( 'FizzBuzzMorePlugins', $info['attributes'] );
1055 }
1056
1057 /**
1058 * Attributes under manifest_version 1
1059 */
1060 public function testAttributes1() {
1061 $processor = new ExtensionProcessor();
1062 $processor->extractInfo(
1063 $this->extensionPath,
1064 [
1065 'FooBarPlugins' => [
1066 'ext.baz.foobar',
1067 ],
1068 'FizzBuzzMorePlugins' => [
1069 'ext.baz.fizzbuzz',
1070 ],
1071 ] + self::$default,
1072 1
1073 );
1074 $processor->extractInfo(
1075 $this->extensionPath,
1076 [
1077 'FizzBuzzMorePlugins' => [
1078 'ext.bar.fizzbuzz',
1079 ]
1080 ] + self::$default2,
1081 1
1082 );
1083
1084 $info = $processor->getExtractedInfo();
1085 $this->assertArrayHasKey( 'FooBarPlugins', $info['attributes'] );
1086 $this->assertSame( [ 'ext.baz.foobar' ], $info['attributes']['FooBarPlugins'] );
1087 $this->assertArrayHasKey( 'FizzBuzzMorePlugins', $info['attributes'] );
1088 $this->assertSame(
1089 [ 'ext.baz.fizzbuzz', 'ext.bar.fizzbuzz' ],
1090 $info['attributes']['FizzBuzzMorePlugins']
1091 );
1092 }
1093
1094 public function testAttributes1_notarray() {
1095 $processor = new ExtensionProcessor();
1096 $this->expectException( InvalidArgumentException::class );
1097 $this->expectExceptionMessage(
1098 "The value for 'FooBarPlugins' should be an array (from {$this->extensionPath})"
1099 );
1100 $processor->extractInfo(
1101 $this->extensionPath,
1102 [
1103 'FooBarPlugins' => 'ext.baz.foobar',
1104 ] + self::$default,
1105 1
1106 );
1107 }
1108
1109 public function testExtractPathBasedGlobal() {
1110 $processor = new ExtensionProcessor();
1111 $processor->extractInfo(
1112 $this->extensionPath,
1113 [
1114 'ServiceWiringFiles' => [
1115 'includes/ServiceWiring.php'
1116 ],
1117 ] + self::$default,
1118 1
1119 );
1120 $globals = $processor->getExtractedInfo()['globals'];
1121 $this->assertArrayHasKey( 'wgServiceWiringFiles', $globals );
1122 $this->assertSame( [
1123 "{$this->dirname}/includes/ServiceWiring.php"
1124 ], $globals['wgServiceWiringFiles'] );
1125 }
1126
1127 public function testGetRequirements() {
1128 $info = self::$default + [
1129 'requires' => [
1130 'MediaWiki' => '>= 1.25.0',
1131 'platform' => [
1132 'php' => '>= 5.5.9'
1133 ],
1134 'extensions' => [
1135 'Bar' => '*'
1136 ]
1137 ]
1138 ];
1139 $processor = new ExtensionProcessor();
1140 $this->assertSame(
1141 $info['requires'],
1142 $processor->getRequirements( $info, false )
1143 );
1144 $this->assertSame(
1145 [],
1146 $processor->getRequirements( [], false )
1147 );
1148 }
1149
1150 public function testGetDevRequirements() {
1151 $info = self::$default + [
1152 'dev-requires' => [
1153 'MediaWiki' => '>= 1.31.0',
1154 'platform' => [
1155 'ext-foo' => '*',
1156 ],
1157 'skins' => [
1158 'Baz' => '*',
1159 ],
1160 'extensions' => [
1161 'Biz' => '*',
1162 ],
1163 ],
1164 ];
1165 $processor = new ExtensionProcessor();
1166 $this->assertSame(
1167 $info['dev-requires'],
1168 $processor->getRequirements( $info, true )
1169 );
1170 // Set some standard requirements, so we can test merging
1171 $info['requires'] = [
1172 'MediaWiki' => '>= 1.25.0',
1173 'platform' => [
1174 'php' => '>= 5.5.9'
1175 ],
1176 'extensions' => [
1177 'Bar' => '*'
1178 ]
1179 ];
1180 $this->assertSame(
1181 [
1182 'MediaWiki' => '>= 1.25.0 >= 1.31.0',
1183 'platform' => [
1184 'php' => '>= 5.5.9',
1185 'ext-foo' => '*',
1186 ],
1187 'extensions' => [
1188 'Bar' => '*',
1189 'Biz' => '*',
1190 ],
1191 'skins' => [
1192 'Baz' => '*',
1193 ],
1194 ],
1195 $processor->getRequirements( $info, true )
1196 );
1197
1198 // If there's no dev-requires, it just returns requires
1199 unset( $info['dev-requires'] );
1200 $this->assertSame(
1201 $info['requires'],
1202 $processor->getRequirements( $info, true )
1203 );
1204 }
1205
1206 public function testGetExtractedAutoloadInfo() {
1207 $processor = new ExtensionProcessor();
1208
1209 $processor->extractInfo( $this->extensionPath, [
1210 'name' => 'Test',
1211 'AutoloadClasses' => [ 'FooBar' => 'includes/FooBar.php' ],
1212 'AutoloadNamespaces' => [ '\Foo\Bar\\' => 'includes/foo/bar/' ],
1213 'TestAutoloadClasses' => [ 'FooBarTest' => 'tests/FooBarTest.php' ],
1214 'TestAutoloadNamespaces' => [ '\Foo\Bar\Test\\' => 'tests/foo/bar/' ],
1215 'load_composer_autoloader' => true,
1216 ], 2 );
1217
1218 $autoload = $processor->getExtractedAutoloadInfo();
1219 $this->assertSame(
1220 [ 'FooBar' => $this->dirname . '/includes/FooBar.php' ],
1221 $autoload['classes']
1222 );
1223 $this->assertSame(
1224 [ '\Foo\Bar\\' => $this->dirname . '/includes/foo/bar/' ],
1225 $autoload['namespaces']
1226 );
1227 $this->assertSame(
1228 [ $this->dirname . '/vendor/autoload.php' ],
1229 $autoload['files']
1230 );
1231
1232 $autoload = $processor->getExtractedAutoloadInfo( true );
1233 $this->assertSame(
1234 [
1235 'FooBar' => $this->dirname . '/includes/FooBar.php',
1236 'FooBarTest' => $this->dirname . '/tests/FooBarTest.php'
1237 ],
1238 $autoload['classes']
1239 );
1240 $this->assertSame(
1241 [
1242 '\Foo\Bar\\' => $this->dirname . '/includes/foo/bar/',
1243 '\Foo\Bar\Test\\' => $this->dirname . '/tests/foo/bar/'
1244 ],
1245 $autoload['namespaces']
1246 );
1247 $this->assertSame(
1248 [ $this->dirname . '/vendor/autoload.php' ],
1249 $autoload['files']
1250 );
1251 }
1252
1253 /**
1254 * Verify that extension.schema.json is in sync with ExtensionProcessor
1255 *
1256 * @coversNothing
1257 */
1258 public function testGlobalSettingsDocumentedInSchema() {
1259 global $IP;
1260 $globalSettings = TestingAccessWrapper::newFromClass(
1261 ExtensionProcessor::class )->globalSettings;
1262
1263 $version = ExtensionRegistry::MANIFEST_VERSION;
1264 $schema = FormatJson::decode(
1265 file_get_contents( "$IP/docs/extension.schema.v$version.json" ),
1266 true
1267 );
1268 $missing = [];
1269 foreach ( $globalSettings as $global ) {
1270 if ( !isset( $schema['properties'][$global] ) ) {
1271 $missing[] = $global;
1272 }
1273 }
1274
1275 $this->assertEquals( [], $missing,
1276 "The following global settings are not documented in docs/extension.schema.json" );
1277 }
1278
1279 /**
1280 * Verify that extension.schema.v1.json is unchanged
1281 *
1282 * Frozen since MediaWiki 1.43; see T258668 for details.
1283 *
1284 * @coversNothing
1285 */
1286 public function testVersion1SchemaIsFrozen() {
1287 global $IP;
1288
1289 $schemaFileHash = md5_file( "$IP/docs/extension.schema.v1.json", false );
1290
1291 $this->assertTrue(
1292 $schemaFileHash === '197fc9db288765d17a76a826e879ac6b',
1293 "Manifest_version 1 is frozen and should not be changed or given new features" );
1294 }
1295
1296 public function testGetCoreAttribsMerging() {
1297 $processor = new ExtensionProcessor();
1298
1299 $info = self::$default + [
1300 'TrackingCategories' => [
1301 'Foo'
1302 ]
1303 ];
1304
1305 $info2 = self::$default2 + [
1306 'TrackingCategories' => [
1307 'Bar'
1308 ]
1309 ];
1310
1311 $processor->extractInfo( $this->extensionPath, $info, 2 );
1312 $processor->extractInfo( $this->extensionPath, $info2, 2 );
1313
1314 $attributes = $processor->getExtractedInfo()['attributes'];
1315
1316 $this->assertEquals(
1317 [ 'Foo', 'Bar' ],
1318 $attributes['TrackingCategories']
1319 );
1320 }
1321 }
1322
1323 /**
1324 * Allow overriding the default value of $this->globals and $this->attributes
1325 * so we can test merging and hook extraction
1326 */
1327 class MockExtensionProcessor extends ExtensionProcessor {
1328
1329 public function __construct( $preset = [] ) {
1330 if ( isset( $preset['globals'] ) ) {
1331 $this->globals = $preset['globals'] + $this->globals;
1332 }
1333 if ( isset( $preset['attributes'] ) ) {
1334 $this->attributes = $preset['attributes'] + $this->attributes;
1335 }
1336 }
1337 }