Debian Codegraph @

tests/phpunit/includes/registration/ExtensionRegistrationTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Registration;
4
5 use AutoLoader;
6 use Generator;
7 use MediaWiki\Registration\ExtensionRegistry;
8 use MediaWiki\Settings\Config\ArrayConfigBuilder;
9 use MediaWiki\Settings\Config\PhpIniSink;
10 use MediaWiki\Settings\SettingsBuilder;
11 use MediaWikiIntegrationTestCase;
12 use Wikimedia\ObjectCache\HashBagOStuff;
13 use Wikimedia\TestingAccessWrapper;
14
15 /**
16 * @covers \MediaWiki\Registration\ExtensionRegistry
17 */
18 class ExtensionRegistrationTest extends MediaWikiIntegrationTestCase {
19
20 /** @var array */
21 private $autoloaderState;
22
23 /** @var ?ExtensionRegistry */
24 private $originalExtensionRegistry = null;
25
26 protected function setUp(): void {
27 // phpcs:disable MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgHooks
28 global $wgHooks;
29
30 parent::setUp();
31
32 $this->autoloaderState = AutoLoader::getState();
33
34 // Make sure to restore globals
35 $this->stashMwGlobals( [
36 'wgHooks',
37 'wgAutoloadClasses',
38 'wgNamespaceProtection',
39 'wgNamespaceModels',
40 'wgAvailableRights',
41 'wgAuthManagerAutoConfig',
42 'wgGroupPermissions',
43 ] );
44
45 // For the purpose of this test, make $wgHooks behave like a real global config array.
46 $wgHooks = [];
47 }
48
49 protected function tearDown(): void {
50 AutoLoader::restoreState( $this->autoloaderState );
51
52 if ( $this->originalExtensionRegistry ) {
53 $this->setExtensionRegistry( $this->originalExtensionRegistry );
54 }
55
56 parent::tearDown();
57 }
58
59 public function testExportNamespaces() {
60 $manifest = [
61 'namespaces' => [
62 [
63 'id' => 1300,
64 'name' => 'ExtensionRegistrationTest',
65 'constant' => 'NS_EXTENSION_REGISTRATION_TEST',
66 'defaultcontentmodel' => 'Foo',
67 'protection' => [ 'sysop' ],
68 ]
69 ]
70 ];
71
72 $file = $this->makeManifestFile( $manifest );
73
74 $registry = new ExtensionRegistry();
75 $registry->queue( $file );
76 $registry->loadFromQueue();
77
78 $this->assertTrue( defined( 'NS_EXTENSION_REGISTRATION_TEST' ) );
79 $this->assertSame( 1300, constant( 'NS_EXTENSION_REGISTRATION_TEST' ) );
80
81 $expectedNamespaceNames = [ 1300 => 'ExtensionRegistrationTest' ];
82 $this->assertSame( $expectedNamespaceNames, $registry->getAttribute( 'ExtensionNamespaces' ) );
83
84 $this->assertArrayHasKey( 1300, $GLOBALS['wgNamespaceProtection'] );
85 $this->assertArrayHasKey( 1300, $GLOBALS['wgNamespaceContentModels'] );
86 }
87
88 private function setExtensionRegistry( ExtensionRegistry $registry ) {
89 $staticExtReg = TestingAccessWrapper::newFromClass( ExtensionRegistry::class );
90
91 if ( !$this->originalExtensionRegistry ) {
92 $this->originalExtensionRegistry = $staticExtReg->instance;
93 }
94
95 $staticExtReg->instance = $registry;
96 }
97
98 public static function onAnEvent() {
99 // no-op
100 }
101
102 public static function onBooEvent() {
103 // no-op
104 }
105
106 public function testExportHooks() {
107 $manifest = [
108 'Hooks' => [
109 'AnEvent' => self::class . '::onAnEvent',
110 'BooEvent' => 'main',
111 ],
112 'HookHandlers' => [
113 'main' => [ 'class' => self::class ]
114 ],
115 ];
116
117 $file = $this->makeManifestFile( $manifest );
118
119 $registry = new ExtensionRegistry();
120 $this->setExtensionRegistry( $registry );
121
122 $registry->queue( $file );
123 $registry->loadFromQueue();
124
125 $this->resetServices();
126 $hookContainer = $this->getServiceContainer()->getHookContainer();
127 $this->assertTrue( $hookContainer->isRegistered( 'AnEvent' ), 'AnEvent' );
128 $this->assertTrue( $hookContainer->isRegistered( 'BooEvent' ), 'BooEvent' );
129 }
130
131 public function testExportAutoload() {
132 global $wgAutoloadClasses;
133 $oldAutoloadClasses = $wgAutoloadClasses;
134
135 $manifest = [
136 'AutoloadClasses' => [
137 'TestAutoloaderClass' =>
138 __DIR__ . '/../../data/autoloader/TestAutoloadedClass.php',
139 ],
140 'AutoloadNamespaces' => [
141 'Dummy\Test\Namespace\\' =>
142 __DIR__ . '/../../data/autoloader/psr4/',
143 ],
144 'HookHandler' => [
145 'main' => [ 'class' => 'Whatever' ]
146 ],
147 ];
148
149 $file = $this->makeManifestFile( $manifest );
150
151 $registry = new ExtensionRegistry();
152 $registry->setCache( new HashBagOStuff() );
153
154 $registry->queue( $file );
155 $registry->loadFromQueue();
156
157 $this->assertArrayHasKey( 'TestAutoloaderClass', AutoLoader::getClassFiles() );
158 $this->assertArrayHasKey( 'Dummy\Test\Namespace\\', AutoLoader::getNamespaceDirectories() );
159
160 // Now, reset and do it again, but with the cached extension info.
161 // This is needed because autoloader registration is currently handled
162 // differently when loading from the cache (T240535).
163 AutoLoader::restoreState( $this->autoloaderState );
164 $wgAutoloadClasses = $oldAutoloadClasses;
165
166 $registry->queue( $file );
167 $registry->loadFromQueue();
168
169 $this->assertArrayHasKey( 'TestAutoloaderClass', AutoLoader::getClassFiles() );
170 $this->assertArrayHasKey( 'Dummy\Test\Namespace\\', AutoLoader::getNamespaceDirectories() );
171 }
172
173 /**
174 * @dataProvider provideExportConfigToGlobals
175 * @dataProvider provideExportAttributesToGlobals
176 */
177 public function testExportGlobals( $desc, $before, $manifest, $expected ) {
178 $this->setMwGlobals( $before );
179
180 $file = $this->makeManifestFile( $manifest );
181
182 $registry = new ExtensionRegistry();
183 $registry->queue( $file );
184 $registry->loadFromQueue();
185
186 foreach ( $expected as $name => $expectedValue ) {
187 $this->assertArrayHasKey( $name, $GLOBALS, $desc );
188 $this->assertEquals( $expectedValue, $GLOBALS[$name], $desc );
189 }
190 }
191
192 private function newSettingsBuilder(): SettingsBuilder {
193 $settings = new SettingsBuilder(
194 __DIR__,
195 $this->createMock( ExtensionRegistry::class ),
196 new ArrayConfigBuilder(),
197 $this->createMock( PhpIniSink::class ),
198 null
199 );
200
201 return $settings;
202 }
203
204 public static function callbackForTest( array $ext, SettingsBuilder $settings ) {
205 $settings->overrideConfigValue( 'RunCallbacksTest', 'foo' );
206 self::assertSame( 'CallbackTest', $ext['name'] );
207 }
208
209 public function testRunCallbacks() {
210 $manifest = [
211 'name' => 'CallbackTest',
212 'callback' => [ __CLASS__, 'callbackForTest' ],
213 ];
214
215 $file = $this->makeManifestFile( $manifest );
216
217 $settings = $this->newSettingsBuilder();
218
219 $registry = new ExtensionRegistry();
220 $registry->setSettingsBuilder( $settings );
221
222 $settings->enterRegistrationStage();
223 $registry->queue( $file );
224 $registry->loadFromQueue();
225
226 $this->assertSame( 'foo', $settings->getConfig()->get( 'RunCallbacksTest' ) );
227 }
228
229 /**
230 * Provides defaults coming from extension, global values from custom settings.
231 * The global value should be merged on top of the default from the extension (backwards merge).
232 *
233 * @return Generator
234 */
235 public static function provideExportConfigToGlobals() {
236 yield [
237 'Simple non-array values',
238 [
239 'mwtestFooBarConfig' => true,
240 'mwtestFooBarConfig2' => 'string',
241 ],
242 [
243 'config_prefix' => 'mwtest',
244 'config' => [
245 'FooBarDefault' => [ 'value' => 1234 ],
246 'FooBarConfig' => [ 'value' => false ],
247 ]
248 ],
249 [
250 'mwtestFooBarConfig' => true,
251 'mwtestFooBarConfig2' => 'string',
252 'mwtestFooBarDefault' => 1234,
253 ],
254 ];
255
256 yield [
257 'No global already set, simple assoc array',
258 [],
259 [
260 'config_prefix' => 'mwtest',
261 'config' => [
262 'DefaultOptions' => [
263 'value' => [
264 'foobar' => true,
265 ]
266 ]
267 ]
268 ],
269 [
270 'mwtestDefaultOptions' => [
271 'foobar' => true,
272 ]
273 ],
274 ];
275
276 yield [
277 'No global already set, simple assoc array, manifest version 1',
278 [],
279 [
280 'manifest_version' => 1,
281 'config' => [
282 '_prefix' => 'mwtest',
283 'SomeMap' => [
284 'foobar' => true,
285 ]
286 ]
287 ],
288 [
289 'mwtestSomeMap' => [
290 'foobar' => true,
291 ]
292 ],
293 ];
294
295 yield [
296 'Global already set, simple assoc array, manifest version 1',
297 [
298 'mwtestSomeMap' => [
299 'foobar' => true,
300 'foo' => 'string'
301 ],
302 ],
303 [
304 'manifest_version' => 1,
305 'config' => [
306 '_prefix' => 'mwtest',
307 'SomeMap' => [
308 'barbaz' => 12345,
309 'foobar' => false,
310 ]
311 ]
312 ],
313 [
314 'mwtestSomeMap' => [
315 'barbaz' => 12345,
316 'foo' => 'string',
317 'foobar' => true,
318 ],
319 ]
320 ];
321
322 yield [
323 'Global already set, simple list array',
324 [
325 'mwtestList' => [ 'x', 'y', 'z' ],
326 ],
327 [
328 'manifest_version' => 1,
329 'config' => [
330 '_prefix' => 'mwtest',
331 'List' => [ 'a', 'b' ]
332 ]
333 ],
334 [
335 'mwtestList' => [ 'a', 'b', 'x', 'y', 'z' ],
336 ]
337 ];
338
339 yield [
340 'New variable, explicit merge strategy',
341 [
342 'wgNamespacesFoo' => [
343 100 => true,
344 102 => false
345 ],
346 ],
347 [
348 'config' => [
349 'NamespacesFoo' => [
350 'value' => [
351 100 => false,
352 500 => true,
353 ],
354 'merge_strategy' => 'array_plus',
355 ],
356 ]
357 ],
358 [
359 'wgNamespacesFoo' => [
360 100 => true,
361 102 => false,
362 500 => true,
363 ],
364 ]
365 ];
366
367 yield [
368 'New variable, explicit merge strategy, manifest version 1',
369 [
370 'wgNamespacesFoo' => [
371 100 => true,
372 102 => false
373 ],
374 ],
375 [
376 'manifest_version' => 1,
377 'config' => [
378 'NamespacesFoo' => [
379 100 => false,
380 500 => true,
381 ExtensionRegistry::MERGE_STRATEGY => 'array_plus',
382 ],
383 ]
384 ],
385 [
386 'wgNamespacesFoo' => [
387 100 => true,
388 102 => false,
389 500 => true,
390 ],
391 ]
392 ];
393
394 yield [
395 'False local setting should not be overridden by default (T100767)',
396 [
397 'wgT100767' => false,
398 ],
399 [
400 'config' => [
401 'T100767' => [ 'value' => true ],
402 ]
403 ],
404 [
405 'wgT100767' => false,
406 ],
407 ];
408
409 yield [
410 'test array_replace_recursive',
411 [
412 'mwtestJsonConfigs' => [
413 'JsonZeroConfig' => [
414 'namespace' => 480,
415 'nsName' => 'Zero',
416 'isLocal' => false,
417 'remote' => [
418 'username' => 'foo',
419 ],
420 ],
421 ],
422 ],
423 [
424 'config_prefix' => 'mwtest',
425 'config' => [
426 'JsonConfigs' => [
427 'value' => [
428 'JsonZeroConfig' => [
429 'isLocal' => true,
430 ],
431 ],
432 'merge_strategy' => 'array_replace_recursive',
433 ],
434 ]
435 ],
436 [
437 'mwtestJsonConfigs' => [
438 'JsonZeroConfig' => [
439 'namespace' => 480,
440 'nsName' => 'Zero',
441 'isLocal' => false,
442 'remote' => [
443 'username' => 'foo',
444 ],
445 ],
446 ],
447 ],
448 ];
449
450 yield [
451 'Default doesn\'t override null',
452 [
453 'wgNullGlobal' => null,
454 ],
455 [
456 'config' => [
457 'NullGlobal' => [ 'value' => 'not-null' ]
458 ]
459 ],
460 [
461 'wgNullGlobal' => null
462 ],
463 ];
464
465 yield [
466 'provide_default passive case',
467 [
468 'wgFlatArray' => [],
469 ],
470 [
471 'config' => [
472 'FlatArray' => [
473 'value' => [ 1 ],
474 'merge_strategy' => 'provide_default'
475 ],
476 ]
477 ],
478 [
479 'wgFlatArray' => []
480 ],
481 ];
482
483 yield [
484 'provide_default active case',
485 [],
486 [
487 'config' => [
488 'FlatArray' => [
489 'value' => [ 1 ],
490 'merge_strategy' => 'provide_default'
491 ],
492 ]
493 ],
494 [
495 'wgFlatArray' => [ 1 ]
496 ],
497 ];
498 }
499
500 /**
501 * Provide global values as default coming from core, new value from extension attribute.
502 * The value coming from the extension should be merged on top of the global.
503 *
504 * @return Generator
505 */
506 public static function provideExportAttributesToGlobals() {
507 yield [
508 'AvailableRights appends to default value, per config schema',
509 [
510 'wgAvailableRights' => [
511 'aaa',
512 'bbb'
513 ],
514 ],
515 [ 'AvailableRights' => [ 'ccc', ] ],
516 [
517 // NOTE: This is backwards! Fortunately, the order in AvailableRights
518 // is not significant.
519 'wgAvailableRights' => [
520 'ccc',
521 'aaa',
522 'bbb',
523 ],
524 ]
525 ];
526
527 yield [
528 'AuthManagerAutoConfig appends to default value, per top level key',
529 [
530 'wgAuthManagerAutoConfig' => [
531 'preauth' => [ 'default' => 'DefaultPreAuth' ],
532 'primaryauth' => [ 'default' => 'DefaultPrimaryAuth' ],
533 'secondaryauth' => [ 'default' => 'DefaultSecondaryAuth' ],
534 ],
535 ],
536 [
537 'AuthManagerAutoConfig' => [
538 'primaryauth' => [ 'my' => 'MyPrimaryAuth' ],
539 ],
540 ],
541 [
542 'wgAuthManagerAutoConfig' => [
543 'preauth' => [ 'default' => 'DefaultPreAuth' ],
544 'primaryauth' => [ 'default' => 'DefaultPrimaryAuth', 'my' => 'MyPrimaryAuth' ],
545 'secondaryauth' => [ 'default' => 'DefaultSecondaryAuth' ],
546 ],
547 ]
548 ];
549
550 yield [
551 'Global already set, $wgGroupPermissions',
552 [
553 'wgGroupPermissions' => [
554 'sysop' => [
555 'something' => true,
556 ],
557 'user' => [
558 'somethingtwo' => true,
559 ]
560 ],
561 ],
562 [
563 'GroupPermissions' => [
564 'customgroup' => [
565 'right' => true,
566 ],
567 'user' => [
568 'right' => true,
569 'somethingtwo' => false,
570 'nonduplicated' => true,
571 ],
572 ],
573 ],
574 [
575 'wgGroupPermissions' => [
576 'customgroup' => [
577 'right' => true,
578 ],
579 'sysop' => [
580 'something' => true,
581 ],
582 'user' => [
583 // NOTE: somethingtwo should be false here, since the value from
584 // the extension should override the core default!
585 // See e.g. https://www.mediawiki.org/wiki/Topic:W2ttbedo3apzno4w
586 // and https://phabricator.wikimedia.org/T98347#2589540.
587 'somethingtwo' => true,
588 'right' => true,
589 'nonduplicated' => true,
590 ]
591 ],
592 ],
593 ];
594 }
595
596 /**
597 * @param array $manifest
598 *
599 * @return string
600 */
601 private function makeManifestFile( array $manifest ): string {
602 $manifest += [
603 'name' => 'Test',
604 'manifest_version' => 2,
605 'config' => [],
606 'callbacks' => [],
607 'defines' => [],
608 'credits' => [],
609 'attributes' => [],
610 'autoloaderPaths' => []
611 ];
612
613 $file = $this->getNewTempFile();
614 file_put_contents( $file, json_encode( $manifest ) );
615 return $file;
616 }
617
618 public function testExportAutoloaderWithPsr4Namespaces() {
619 $dir = __DIR__ . '/../../data/registration';
620 $registry = new ExtensionRegistry();
621 $data = $registry->readFromQueue( [
622 "{$dir}/autoload_namespaces.json" => 1
623 ] );
624
625 $access = TestingAccessWrapper::newFromObject( $registry );
626 $access->exportExtractedData( $data );
627
628 $this->assertTrue(
629 class_exists( 'Test\\MediaWiki\\AutoLoader\\TestFooBar' ),
630 "Registry initializes Autoloader from AutoloadNamespaces"
631 );
632 }
633
634 }