MediaWiki  1.29.2
ExtensionProcessorTest.php
Go to the documentation of this file.
1 <?php
2 
3 use Wikimedia\TestingAccessWrapper;
4 
6 
7  private $dir, $dirname;
8 
9  public function setUp() {
10  parent::setUp();
11  $this->dir = __DIR__ . '/FooBar/extension.json';
12  $this->dirname = dirname( $this->dir );
13  }
14 
20  public static $default = [
21  'name' => 'FooBar',
22  ];
23 
27  public function testExtractInfo() {
28  // Test that attributes that begin with @ are ignored
29  $processor = new ExtensionProcessor();
30  $processor->extractInfo( $this->dir, self::$default + [
31  '@metadata' => [ 'foobarbaz' ],
32  'AnAttribute' => [ 'omg' ],
33  'AutoloadClasses' => [ 'FooBar' => 'includes/FooBar.php' ],
34  ], 1 );
35 
36  $extracted = $processor->getExtractedInfo();
37  $attributes = $extracted['attributes'];
38  $this->assertArrayHasKey( 'AnAttribute', $attributes );
39  $this->assertArrayNotHasKey( '@metadata', $attributes );
40  $this->assertArrayNotHasKey( 'AutoloadClasses', $attributes );
41  }
42 
43  public static function provideRegisterHooks() {
44  $merge = [ ExtensionRegistry::MERGE_STRATEGY => 'array_merge_recursive' ];
45  // Format:
46  // Current $wgHooks
47  // Content in extension.json
48  // Expected value of $wgHooks
49  return [
50  // No hooks
51  [
52  [],
54  $merge,
55  ],
56  // No current hooks, adding one for "FooBaz" in string format
57  [
58  [],
59  [ 'Hooks' => [ 'FooBaz' => 'FooBazCallback' ] ] + self::$default,
60  [ 'FooBaz' => [ 'FooBazCallback' ] ] + $merge,
61  ],
62  // Hook for "FooBaz", adding another one
63  [
64  [ 'FooBaz' => [ 'PriorCallback' ] ],
65  [ 'Hooks' => [ 'FooBaz' => 'FooBazCallback' ] ] + self::$default,
66  [ 'FooBaz' => [ 'PriorCallback', 'FooBazCallback' ] ] + $merge,
67  ],
68  // No current hooks, adding one for "FooBaz" in verbose array format
69  [
70  [],
71  [ 'Hooks' => [ 'FooBaz' => [ 'FooBazCallback' ] ] ] + self::$default,
72  [ 'FooBaz' => [ 'FooBazCallback' ] ] + $merge,
73  ],
74  // Hook for "BarBaz", adding one for "FooBaz"
75  [
76  [ 'BarBaz' => [ 'BarBazCallback' ] ],
77  [ 'Hooks' => [ 'FooBaz' => 'FooBazCallback' ] ] + self::$default,
78  [
79  'BarBaz' => [ 'BarBazCallback' ],
80  'FooBaz' => [ 'FooBazCallback' ],
81  ] + $merge,
82  ],
83  // Callbacks for FooBaz wrapped in an array
84  [
85  [],
86  [ 'Hooks' => [ 'FooBaz' => [ 'Callback1' ] ] ] + self::$default,
87  [
88  'FooBaz' => [ 'Callback1' ],
89  ] + $merge,
90  ],
91  // Multiple callbacks for FooBaz hook
92  [
93  [],
94  [ 'Hooks' => [ 'FooBaz' => [ 'Callback1', 'Callback2' ] ] ] + self::$default,
95  [
96  'FooBaz' => [ 'Callback1', 'Callback2' ],
97  ] + $merge,
98  ],
99  ];
100  }
101 
106  public function testRegisterHooks( $pre, $info, $expected ) {
107  $processor = new MockExtensionProcessor( [ 'wgHooks' => $pre ] );
108  $processor->extractInfo( $this->dir, $info, 1 );
109  $extracted = $processor->getExtractedInfo();
110  $this->assertEquals( $expected, $extracted['globals']['wgHooks'] );
111  }
112 
116  public function testExtractConfig1() {
117  $processor = new ExtensionProcessor;
118  $info = [
119  'config' => [
120  'Bar' => 'somevalue',
121  'Foo' => 10,
122  '@IGNORED' => 'yes',
123  ],
124  ] + self::$default;
125  $info2 = [
126  'config' => [
127  '_prefix' => 'eg',
128  'Bar' => 'somevalue'
129  ],
130  'name' => 'FooBar2',
131  ];
132  $processor->extractInfo( $this->dir, $info, 1 );
133  $processor->extractInfo( $this->dir, $info2, 1 );
134  $extracted = $processor->getExtractedInfo();
135  $this->assertEquals( 'somevalue', $extracted['globals']['wgBar'] );
136  $this->assertEquals( 10, $extracted['globals']['wgFoo'] );
137  $this->assertArrayNotHasKey( 'wg@IGNORED', $extracted['globals'] );
138  // Custom prefix:
139  $this->assertEquals( 'somevalue', $extracted['globals']['egBar'] );
140  }
141 
145  public function testExtractConfig2() {
146  $processor = new ExtensionProcessor;
147  $info = [
148  'config' => [
149  'Bar' => [ 'value' => 'somevalue' ],
150  'Foo' => [ 'value' => 10 ],
151  'Path' => [ 'value' => 'foo.txt', 'path' => true ],
152  ],
153  ] + self::$default;
154  $info2 = [
155  'config' => [
156  'Bar' => [ 'value' => 'somevalue' ],
157  ],
158  'config_prefix' => 'eg',
159  'name' => 'FooBar2',
160  ];
161  $processor->extractInfo( $this->dir, $info, 2 );
162  $processor->extractInfo( $this->dir, $info2, 2 );
163  $extracted = $processor->getExtractedInfo();
164  $this->assertEquals( 'somevalue', $extracted['globals']['wgBar'] );
165  $this->assertEquals( 10, $extracted['globals']['wgFoo'] );
166  $this->assertEquals( "{$this->dirname}/foo.txt", $extracted['globals']['wgPath'] );
167  // Custom prefix:
168  $this->assertEquals( 'somevalue', $extracted['globals']['egBar'] );
169  }
170 
171  public static function provideExtractExtensionMessagesFiles() {
172  $dir = __DIR__ . '/FooBar/';
173  return [
174  [
175  [ 'ExtensionMessagesFiles' => [ 'FooBarAlias' => 'FooBar.alias.php' ] ],
176  [ 'wgExtensionMessagesFiles' => [ 'FooBarAlias' => $dir . 'FooBar.alias.php' ] ]
177  ],
178  [
179  [
180  'ExtensionMessagesFiles' => [
181  'FooBarAlias' => 'FooBar.alias.php',
182  'FooBarMagic' => 'FooBar.magic.i18n.php',
183  ],
184  ],
185  [
186  'wgExtensionMessagesFiles' => [
187  'FooBarAlias' => $dir . 'FooBar.alias.php',
188  'FooBarMagic' => $dir . 'FooBar.magic.i18n.php',
189  ],
190  ],
191  ],
192  ];
193  }
194 
199  public function testExtractExtensionMessagesFiles( $input, $expected ) {
200  $processor = new ExtensionProcessor();
201  $processor->extractInfo( $this->dir, $input + self::$default, 1 );
202  $out = $processor->getExtractedInfo();
203  foreach ( $expected as $key => $value ) {
204  $this->assertEquals( $value, $out['globals'][$key] );
205  }
206  }
207 
208  public static function provideExtractMessagesDirs() {
209  $dir = __DIR__ . '/FooBar/';
210  return [
211  [
212  [ 'MessagesDirs' => [ 'VisualEditor' => 'i18n' ] ],
213  [ 'wgMessagesDirs' => [ 'VisualEditor' => [ $dir . 'i18n' ] ] ]
214  ],
215  [
216  [ 'MessagesDirs' => [ 'VisualEditor' => [ 'i18n', 'foobar' ] ] ],
217  [ 'wgMessagesDirs' => [ 'VisualEditor' => [ $dir . 'i18n', $dir . 'foobar' ] ] ]
218  ],
219  ];
220  }
221 
226  public function testExtractMessagesDirs( $input, $expected ) {
227  $processor = new ExtensionProcessor();
228  $processor->extractInfo( $this->dir, $input + self::$default, 1 );
229  $out = $processor->getExtractedInfo();
230  foreach ( $expected as $key => $value ) {
231  $this->assertEquals( $value, $out['globals'][$key] );
232  }
233  }
234 
238  public function testExtractCredits() {
239  $processor = new ExtensionProcessor();
240  $processor->extractInfo( $this->dir, self::$default, 1 );
241  $this->setExpectedException( 'Exception' );
242  $processor->extractInfo( $this->dir, self::$default, 1 );
243  }
244 
249  public function testExtractResourceLoaderModules( $input, $expected ) {
250  $processor = new ExtensionProcessor();
251  $processor->extractInfo( $this->dir, $input + self::$default, 1 );
252  $out = $processor->getExtractedInfo();
253  foreach ( $expected as $key => $value ) {
254  $this->assertEquals( $value, $out['globals'][$key] );
255  }
256  }
257 
258  public static function provideExtractResourceLoaderModules() {
259  $dir = __DIR__ . '/FooBar';
260  return [
261  // Generic module with localBasePath/remoteExtPath specified
262  [
263  // Input
264  [
265  'ResourceModules' => [
266  'test.foo' => [
267  'styles' => 'foobar.js',
268  'localBasePath' => '',
269  'remoteExtPath' => 'FooBar',
270  ],
271  ],
272  ],
273  // Expected
274  [
275  'wgResourceModules' => [
276  'test.foo' => [
277  'styles' => 'foobar.js',
278  'localBasePath' => $dir,
279  'remoteExtPath' => 'FooBar',
280  ],
281  ],
282  ],
283  ],
284  // ResourceFileModulePaths specified:
285  [
286  // Input
287  [
288  'ResourceFileModulePaths' => [
289  'localBasePath' => '',
290  'remoteExtPath' => 'FooBar',
291  ],
292  'ResourceModules' => [
293  // No paths
294  'test.foo' => [
295  'styles' => 'foo.js',
296  ],
297  // Different paths set
298  'test.bar' => [
299  'styles' => 'bar.js',
300  'localBasePath' => 'subdir',
301  'remoteExtPath' => 'FooBar/subdir',
302  ],
303  // Custom class with no paths set
304  'test.class' => [
305  'class' => 'FooBarModule',
306  'extra' => 'argument',
307  ],
308  // Custom class with a localBasePath
309  'test.class.with.path' => [
310  'class' => 'FooBarPathModule',
311  'extra' => 'argument',
312  'localBasePath' => '',
313  ]
314  ],
315  ],
316  // Expected
317  [
318  'wgResourceModules' => [
319  'test.foo' => [
320  'styles' => 'foo.js',
321  'localBasePath' => $dir,
322  'remoteExtPath' => 'FooBar',
323  ],
324  'test.bar' => [
325  'styles' => 'bar.js',
326  'localBasePath' => "$dir/subdir",
327  'remoteExtPath' => 'FooBar/subdir',
328  ],
329  'test.class' => [
330  'class' => 'FooBarModule',
331  'extra' => 'argument',
332  'localBasePath' => $dir,
333  'remoteExtPath' => 'FooBar',
334  ],
335  'test.class.with.path' => [
336  'class' => 'FooBarPathModule',
337  'extra' => 'argument',
338  'localBasePath' => $dir,
339  'remoteExtPath' => 'FooBar',
340  ]
341  ],
342  ],
343  ],
344  // ResourceModuleSkinStyles with file module paths
345  [
346  // Input
347  [
348  'ResourceFileModulePaths' => [
349  'localBasePath' => '',
350  'remoteSkinPath' => 'FooBar',
351  ],
352  'ResourceModuleSkinStyles' => [
353  'foobar' => [
354  'test.foo' => 'foo.css',
355  ]
356  ],
357  ],
358  // Expected
359  [
360  'wgResourceModuleSkinStyles' => [
361  'foobar' => [
362  'test.foo' => 'foo.css',
363  'localBasePath' => $dir,
364  'remoteSkinPath' => 'FooBar',
365  ],
366  ],
367  ],
368  ],
369  // ResourceModuleSkinStyles with file module paths and an override
370  [
371  // Input
372  [
373  'ResourceFileModulePaths' => [
374  'localBasePath' => '',
375  'remoteSkinPath' => 'FooBar',
376  ],
377  'ResourceModuleSkinStyles' => [
378  'foobar' => [
379  'test.foo' => 'foo.css',
380  'remoteSkinPath' => 'BarFoo'
381  ],
382  ],
383  ],
384  // Expected
385  [
386  'wgResourceModuleSkinStyles' => [
387  'foobar' => [
388  'test.foo' => 'foo.css',
389  'localBasePath' => $dir,
390  'remoteSkinPath' => 'BarFoo',
391  ],
392  ],
393  ],
394  ],
395  ];
396  }
397 
398  public static function provideSetToGlobal() {
399  return [
400  [
401  [ 'wgAPIModules', 'wgAvailableRights' ],
402  [],
403  [
404  'APIModules' => [ 'foobar' => 'ApiFooBar' ],
405  'AvailableRights' => [ 'foobar', 'unfoobar' ],
406  ],
407  [
408  'wgAPIModules' => [ 'foobar' => 'ApiFooBar' ],
409  'wgAvailableRights' => [ 'foobar', 'unfoobar' ],
410  ],
411  ],
412  [
413  [ 'wgAPIModules', 'wgAvailableRights' ],
414  [
415  'wgAPIModules' => [ 'barbaz' => 'ApiBarBaz' ],
416  'wgAvailableRights' => [ 'barbaz' ]
417  ],
418  [
419  'APIModules' => [ 'foobar' => 'ApiFooBar' ],
420  'AvailableRights' => [ 'foobar', 'unfoobar' ],
421  ],
422  [
423  'wgAPIModules' => [ 'barbaz' => 'ApiBarBaz', 'foobar' => 'ApiFooBar' ],
424  'wgAvailableRights' => [ 'barbaz', 'foobar', 'unfoobar' ],
425  ],
426  ],
427  [
428  [ 'wgGroupPermissions' ],
429  [
430  'wgGroupPermissions' => [
431  'sysop' => [ 'delete' ]
432  ],
433  ],
434  [
435  'GroupPermissions' => [
436  'sysop' => [ 'undelete' ],
437  'user' => [ 'edit' ]
438  ],
439  ],
440  [
441  'wgGroupPermissions' => [
442  'sysop' => [ 'delete', 'undelete' ],
443  'user' => [ 'edit' ]
444  ],
445  ]
446  ]
447  ];
448  }
449 
456  public function testExtractAttributes() {
457  $processor = new ExtensionProcessor();
458  // Load FooBar extension
459  $processor->extractInfo( $this->dir, [ 'name' => 'FooBar' ], 2 );
460  $processor->extractInfo(
461  $this->dir,
462  [
463  'name' => 'Baz',
464  'attributes' => [
465  // Loaded
466  'FooBar' => [
467  'Plugins' => [
468  'ext.baz.foobar',
469  ],
470  ],
471  // Not loaded
472  'FizzBuzz' => [
473  'MorePlugins' => [
474  'ext.baz.fizzbuzz',
475  ],
476  ],
477  ],
478  ],
479  2
480  );
481 
482  $info = $processor->getExtractedInfo();
483  $this->assertArrayHasKey( 'FooBarPlugins', $info['attributes'] );
484  $this->assertSame( [ 'ext.baz.foobar' ], $info['attributes']['FooBarPlugins'] );
485  $this->assertArrayNotHasKey( 'FizzBuzzMorePlugins', $info['attributes'] );
486  }
487 
493  public function testAttributes1() {
494  $processor = new ExtensionProcessor();
495  $processor->extractInfo(
496  $this->dir,
497  [
498  'name' => 'FooBar',
499  'FooBarPlugins' => [
500  'ext.baz.foobar',
501  ],
502  'FizzBuzzMorePlugins' => [
503  'ext.baz.fizzbuzz',
504  ],
505  ],
506  1
507  );
508 
509  $info = $processor->getExtractedInfo();
510  $this->assertArrayHasKey( 'FooBarPlugins', $info['attributes'] );
511  $this->assertSame( [ 'ext.baz.foobar' ], $info['attributes']['FooBarPlugins'] );
512  $this->assertArrayHasKey( 'FizzBuzzMorePlugins', $info['attributes'] );
513  $this->assertSame( [ 'ext.baz.fizzbuzz' ], $info['attributes']['FizzBuzzMorePlugins'] );
514  }
515 
517  global $IP;
518  $globalSettings = TestingAccessWrapper::newFromClass(
519  ExtensionProcessor::class )->globalSettings;
520 
522  $schema = FormatJson::decode(
523  file_get_contents( "$IP/docs/extension.schema.v$version.json" ),
524  true
525  );
526  $missing = [];
527  foreach ( $globalSettings as $global ) {
528  if ( !isset( $schema['properties'][$global] ) ) {
529  $missing[] = $global;
530  }
531  }
532 
533  $this->assertEquals( [], $missing,
534  "The following global settings are not documented in docs/extension.schema.json" );
535  }
536 }
537 
543  public function __construct( $globals = [] ) {
544  $this->globals = $globals + $this->globals;
545  }
546 }
ExtensionProcessorTest\provideExtractExtensionMessagesFiles
static provideExtractExtensionMessagesFiles()
Definition: ExtensionProcessorTest.php:171
ExtensionProcessorTest\$dirname
$dirname
Definition: ExtensionProcessorTest.php:7
ExtensionRegistry\MANIFEST_VERSION
const MANIFEST_VERSION
Version of the highest supported manifest version.
Definition: ExtensionRegistry.php:24
ExtensionProcessorTest\provideRegisterHooks
static provideRegisterHooks()
Definition: ExtensionProcessorTest.php:43
ExtensionProcessorTest\testExtractInfo
testExtractInfo()
ExtensionProcessor::extractInfo.
Definition: ExtensionProcessorTest.php:27
ExtensionProcessorTest\$default
static array $default
'name' is absolutely required
Definition: ExtensionProcessorTest.php:20
ExtensionProcessorTest\testExtractAttributes
testExtractAttributes()
Attributes under manifest_version 2.
Definition: ExtensionProcessorTest.php:456
MockExtensionProcessor\__construct
__construct( $globals=[])
Definition: ExtensionProcessorTest.php:543
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
ExtensionProcessorTest\$dir
$dir
Definition: ExtensionProcessorTest.php:7
ExtensionProcessorTest\provideExtractMessagesDirs
static provideExtractMessagesDirs()
Definition: ExtensionProcessorTest.php:208
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ExtensionRegistry\MERGE_STRATEGY
const MERGE_STRATEGY
Special key that defines the merge strategy.
Definition: ExtensionRegistry.php:41
ExtensionProcessorTest\testAttributes1
testAttributes1()
Attributes under manifest_version 1.
Definition: ExtensionProcessorTest.php:493
FormatJson\decode
static decode( $value, $assoc=false)
Decodes a JSON string.
Definition: FormatJson.php:187
ExtensionProcessor
Definition: ExtensionProcessor.php:3
$input
if(is_array( $mode)) switch( $mode) $input
Definition: postprocess-phan.php:141
MediaWikiTestCase
Definition: MediaWikiTestCase.php:13
ExtensionProcessorTest\provideExtractResourceLoaderModules
static provideExtractResourceLoaderModules()
Definition: ExtensionProcessorTest.php:258
$IP
$IP
Definition: update.php:3
MockExtensionProcessor
Allow overriding the default value of $this->globals so we can test merging.
Definition: ExtensionProcessorTest.php:542
ExtensionProcessorTest\testRegisterHooks
testRegisterHooks( $pre, $info, $expected)
ExtensionProcessor::extractHooks provideRegisterHooks.
Definition: ExtensionProcessorTest.php:106
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
ExtensionProcessor\$globals
array $globals
Stuff that is going to be set to $GLOBALS.
Definition: ExtensionProcessor.php:140
ExtensionProcessorTest\testExtractMessagesDirs
testExtractMessagesDirs( $input, $expected)
ExtensionProcessor::extractMessagesDirs provideExtractMessagesDirs.
Definition: ExtensionProcessorTest.php:226
ExtensionProcessorTest\setUp
setUp()
Definition: ExtensionProcessorTest.php:9
$value
$value
Definition: styleTest.css.php:45
ExtensionProcessorTest\testExtractExtensionMessagesFiles
testExtractExtensionMessagesFiles( $input, $expected)
ExtensionProcessor::extractExtensionMessagesFiles provideExtractExtensionMessagesFiles.
Definition: ExtensionProcessorTest.php:199
ExtensionProcessorTest\testExtractResourceLoaderModules
testExtractResourceLoaderModules( $input, $expected)
ExtensionProcessor::extractResourceLoaderModules provideExtractResourceLoaderModules.
Definition: ExtensionProcessorTest.php:249
globals
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining globals
Definition: globals.txt:25
ExtensionProcessorTest\testExtractConfig2
testExtractConfig2()
ExtensionProcessor::extractConfig2.
Definition: ExtensionProcessorTest.php:145
ExtensionProcessorTest
Definition: ExtensionProcessorTest.php:5
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ExtensionProcessorTest\provideSetToGlobal
static provideSetToGlobal()
Definition: ExtensionProcessorTest.php:398
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1956
ExtensionProcessorTest\testExtractConfig1
testExtractConfig1()
ExtensionProcessor::extractConfig1.
Definition: ExtensionProcessorTest.php:116
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
ExtensionProcessorTest\testGlobalSettingsDocumentedInSchema
testGlobalSettingsDocumentedInSchema()
Definition: ExtensionProcessorTest.php:516
$pre
return true to allow those checks to and false if checking is done remove or add to the links of a group of changes in EnhancedChangesList Hook subscribers can return false to omit this line from recentchanges use this to change the tables headers temp or archived zone change it to an object instance and return false override the list derivative used the name of the old file when set the default code will be skipped $pre
Definition: hooks.txt:1459
ExtensionProcessorTest\testExtractCredits
testExtractCredits()
ExtensionProcessor::extractCredits.
Definition: ExtensionProcessorTest.php:238
array
the array() calling protocol came about after MediaWiki 1.4rc1.
$out
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition: hooks.txt:783