MediaWiki  1.28.3
ExtensionProcessorTest.php
Go to the documentation of this file.
1 <?php
2 
4 
5  private $dir;
6 
7  public function setUp() {
8  parent::setUp();
9  $this->dir = __DIR__ . '/FooBar/extension.json';
10  }
11 
17  public static $default = [
18  'name' => 'FooBar',
19  ];
20 
24  public function testExtractInfo() {
25  // Test that attributes that begin with @ are ignored
26  $processor = new ExtensionProcessor();
27  $processor->extractInfo( $this->dir, self::$default + [
28  '@metadata' => [ 'foobarbaz' ],
29  'AnAttribute' => [ 'omg' ],
30  'AutoloadClasses' => [ 'FooBar' => 'includes/FooBar.php' ],
31  ], 1 );
32 
33  $extracted = $processor->getExtractedInfo();
34  $attributes = $extracted['attributes'];
35  $this->assertArrayHasKey( 'AnAttribute', $attributes );
36  $this->assertArrayNotHasKey( '@metadata', $attributes );
37  $this->assertArrayNotHasKey( 'AutoloadClasses', $attributes );
38  }
39 
43  public function testExtractInfo_namespaces() {
44  // Test that namespace IDs can be overwritten
45  if ( !defined( 'MW_EXTENSION_PROCESSOR_TEST_EXTRACT_INFO_X' ) ) {
46  define( 'MW_EXTENSION_PROCESSOR_TEST_EXTRACT_INFO_X', 123456 );
47  }
48 
49  $processor = new ExtensionProcessor();
50  $processor->extractInfo( $this->dir, self::$default + [
51  'namespaces' => [
52  [
53  'id' => 332200,
54  'constant' => 'MW_EXTENSION_PROCESSOR_TEST_EXTRACT_INFO_A',
55  'name' => 'Test_A',
56  'content' => 'TestModel'
57  ],
58  [ // Test_X will use ID 123456 not 334400
59  'id' => 334400,
60  'constant' => 'MW_EXTENSION_PROCESSOR_TEST_EXTRACT_INFO_X',
61  'name' => 'Test_X',
62  'content' => 'TestModel'
63  ],
64  ]
65  ], 1 );
66 
67  $extracted = $processor->getExtractedInfo();
68 
69  $this->assertArrayHasKey(
70  'MW_EXTENSION_PROCESSOR_TEST_EXTRACT_INFO_A',
71  $extracted['defines']
72  );
73  $this->assertArrayNotHasKey(
74  'MW_EXTENSION_PROCESSOR_TEST_EXTRACT_INFO_X',
75  $extracted['defines']
76  );
77 
78  $this->assertSame(
79  $extracted['defines']['MW_EXTENSION_PROCESSOR_TEST_EXTRACT_INFO_A'],
80  332200
81  );
82 
83  $this->assertArrayHasKey( 'ExtensionNamespaces', $extracted['attributes'] );
84  $this->assertArrayHasKey( 123456, $extracted['attributes']['ExtensionNamespaces'] );
85  $this->assertArrayHasKey( 332200, $extracted['attributes']['ExtensionNamespaces'] );
86  $this->assertArrayNotHasKey( 334400, $extracted['attributes']['ExtensionNamespaces'] );
87 
88  $this->assertSame( 'Test_X', $extracted['attributes']['ExtensionNamespaces'][123456] );
89  $this->assertSame( 'Test_A', $extracted['attributes']['ExtensionNamespaces'][332200] );
90  }
91 
92  public static function provideRegisterHooks() {
93  $merge = [ ExtensionRegistry::MERGE_STRATEGY => 'array_merge_recursive' ];
94  // Format:
95  // Current $wgHooks
96  // Content in extension.json
97  // Expected value of $wgHooks
98  return [
99  // No hooks
100  [
101  [],
102  self::$default,
103  $merge,
104  ],
105  // No current hooks, adding one for "FooBaz" in string format
106  [
107  [],
108  [ 'Hooks' => [ 'FooBaz' => 'FooBazCallback' ] ] + self::$default,
109  [ 'FooBaz' => [ 'FooBazCallback' ] ] + $merge,
110  ],
111  // Hook for "FooBaz", adding another one
112  [
113  [ 'FooBaz' => [ 'PriorCallback' ] ],
114  [ 'Hooks' => [ 'FooBaz' => 'FooBazCallback' ] ] + self::$default,
115  [ 'FooBaz' => [ 'PriorCallback', 'FooBazCallback' ] ] + $merge,
116  ],
117  // No current hooks, adding one for "FooBaz" in verbose array format
118  [
119  [],
120  [ 'Hooks' => [ 'FooBaz' => [ 'FooBazCallback' ] ] ] + self::$default,
121  [ 'FooBaz' => [ 'FooBazCallback' ] ] + $merge,
122  ],
123  // Hook for "BarBaz", adding one for "FooBaz"
124  [
125  [ 'BarBaz' => [ 'BarBazCallback' ] ],
126  [ 'Hooks' => [ 'FooBaz' => 'FooBazCallback' ] ] + self::$default,
127  [
128  'BarBaz' => [ 'BarBazCallback' ],
129  'FooBaz' => [ 'FooBazCallback' ],
130  ] + $merge,
131  ],
132  // Callbacks for FooBaz wrapped in an array
133  [
134  [],
135  [ 'Hooks' => [ 'FooBaz' => [ 'Callback1' ] ] ] + self::$default,
136  [
137  'FooBaz' => [ 'Callback1' ],
138  ] + $merge,
139  ],
140  // Multiple callbacks for FooBaz hook
141  [
142  [],
143  [ 'Hooks' => [ 'FooBaz' => [ 'Callback1', 'Callback2' ] ] ] + self::$default,
144  [
145  'FooBaz' => [ 'Callback1', 'Callback2' ],
146  ] + $merge,
147  ],
148  ];
149  }
150 
155  public function testRegisterHooks( $pre, $info, $expected ) {
156  $processor = new MockExtensionProcessor( [ 'wgHooks' => $pre ] );
157  $processor->extractInfo( $this->dir, $info, 1 );
158  $extracted = $processor->getExtractedInfo();
159  $this->assertEquals( $expected, $extracted['globals']['wgHooks'] );
160  }
161 
165  public function testExtractConfig() {
166  $processor = new ExtensionProcessor;
167  $info = [
168  'config' => [
169  'Bar' => 'somevalue',
170  'Foo' => 10,
171  '@IGNORED' => 'yes',
172  ],
173  ] + self::$default;
174  $info2 = [
175  'config' => [
176  '_prefix' => 'eg',
177  'Bar' => 'somevalue'
178  ],
179  'name' => 'FooBar2',
180  ];
181  $processor->extractInfo( $this->dir, $info, 1 );
182  $processor->extractInfo( $this->dir, $info2, 1 );
183  $extracted = $processor->getExtractedInfo();
184  $this->assertEquals( 'somevalue', $extracted['globals']['wgBar'] );
185  $this->assertEquals( 10, $extracted['globals']['wgFoo'] );
186  $this->assertArrayNotHasKey( 'wg@IGNORED', $extracted['globals'] );
187  // Custom prefix:
188  $this->assertEquals( 'somevalue', $extracted['globals']['egBar'] );
189  }
190 
191  public static function provideExtractExtensionMessagesFiles() {
192  $dir = __DIR__ . '/FooBar/';
193  return [
194  [
195  [ 'ExtensionMessagesFiles' => [ 'FooBarAlias' => 'FooBar.alias.php' ] ],
196  [ 'wgExtensionMessagesFiles' => [ 'FooBarAlias' => $dir . 'FooBar.alias.php' ] ]
197  ],
198  [
199  [
200  'ExtensionMessagesFiles' => [
201  'FooBarAlias' => 'FooBar.alias.php',
202  'FooBarMagic' => 'FooBar.magic.i18n.php',
203  ],
204  ],
205  [
206  'wgExtensionMessagesFiles' => [
207  'FooBarAlias' => $dir . 'FooBar.alias.php',
208  'FooBarMagic' => $dir . 'FooBar.magic.i18n.php',
209  ],
210  ],
211  ],
212  ];
213  }
214 
219  public function testExtractExtensionMessagesFiles( $input, $expected ) {
220  $processor = new ExtensionProcessor();
221  $processor->extractInfo( $this->dir, $input + self::$default, 1 );
222  $out = $processor->getExtractedInfo();
223  foreach ( $expected as $key => $value ) {
224  $this->assertEquals( $value, $out['globals'][$key] );
225  }
226  }
227 
228  public static function provideExtractMessagesDirs() {
229  $dir = __DIR__ . '/FooBar/';
230  return [
231  [
232  [ 'MessagesDirs' => [ 'VisualEditor' => 'i18n' ] ],
233  [ 'wgMessagesDirs' => [ 'VisualEditor' => [ $dir . 'i18n' ] ] ]
234  ],
235  [
236  [ 'MessagesDirs' => [ 'VisualEditor' => [ 'i18n', 'foobar' ] ] ],
237  [ 'wgMessagesDirs' => [ 'VisualEditor' => [ $dir . 'i18n', $dir . 'foobar' ] ] ]
238  ],
239  ];
240  }
241 
246  public function testExtractMessagesDirs( $input, $expected ) {
247  $processor = new ExtensionProcessor();
248  $processor->extractInfo( $this->dir, $input + self::$default, 1 );
249  $out = $processor->getExtractedInfo();
250  foreach ( $expected as $key => $value ) {
251  $this->assertEquals( $value, $out['globals'][$key] );
252  }
253  }
254 
258  public function testExtractCredits() {
259  $processor = new ExtensionProcessor();
260  $processor->extractInfo( $this->dir, self::$default, 1 );
261  $this->setExpectedException( 'Exception' );
262  $processor->extractInfo( $this->dir, self::$default, 1 );
263  }
264 
269  public function testExtractResourceLoaderModules( $input, $expected ) {
270  $processor = new ExtensionProcessor();
271  $processor->extractInfo( $this->dir, $input + self::$default, 1 );
272  $out = $processor->getExtractedInfo();
273  foreach ( $expected as $key => $value ) {
274  $this->assertEquals( $value, $out['globals'][$key] );
275  }
276  }
277 
278  public static function provideExtractResourceLoaderModules() {
279  $dir = __DIR__ . '/FooBar';
280  return [
281  // Generic module with localBasePath/remoteExtPath specified
282  [
283  // Input
284  [
285  'ResourceModules' => [
286  'test.foo' => [
287  'styles' => 'foobar.js',
288  'localBasePath' => '',
289  'remoteExtPath' => 'FooBar',
290  ],
291  ],
292  ],
293  // Expected
294  [
295  'wgResourceModules' => [
296  'test.foo' => [
297  'styles' => 'foobar.js',
298  'localBasePath' => $dir,
299  'remoteExtPath' => 'FooBar',
300  ],
301  ],
302  ],
303  ],
304  // ResourceFileModulePaths specified:
305  [
306  // Input
307  [
308  'ResourceFileModulePaths' => [
309  'localBasePath' => '',
310  'remoteExtPath' => 'FooBar',
311  ],
312  'ResourceModules' => [
313  // No paths
314  'test.foo' => [
315  'styles' => 'foo.js',
316  ],
317  // Different paths set
318  'test.bar' => [
319  'styles' => 'bar.js',
320  'localBasePath' => 'subdir',
321  'remoteExtPath' => 'FooBar/subdir',
322  ],
323  // Custom class with no paths set
324  'test.class' => [
325  'class' => 'FooBarModule',
326  'extra' => 'argument',
327  ],
328  // Custom class with a localBasePath
329  'test.class.with.path' => [
330  'class' => 'FooBarPathModule',
331  'extra' => 'argument',
332  'localBasePath' => '',
333  ]
334  ],
335  ],
336  // Expected
337  [
338  'wgResourceModules' => [
339  'test.foo' => [
340  'styles' => 'foo.js',
341  'localBasePath' => $dir,
342  'remoteExtPath' => 'FooBar',
343  ],
344  'test.bar' => [
345  'styles' => 'bar.js',
346  'localBasePath' => "$dir/subdir",
347  'remoteExtPath' => 'FooBar/subdir',
348  ],
349  'test.class' => [
350  'class' => 'FooBarModule',
351  'extra' => 'argument',
352  'localBasePath' => $dir,
353  'remoteExtPath' => 'FooBar',
354  ],
355  'test.class.with.path' => [
356  'class' => 'FooBarPathModule',
357  'extra' => 'argument',
358  'localBasePath' => $dir,
359  'remoteExtPath' => 'FooBar',
360  ]
361  ],
362  ],
363  ],
364  // ResourceModuleSkinStyles with file module paths
365  [
366  // Input
367  [
368  'ResourceFileModulePaths' => [
369  'localBasePath' => '',
370  'remoteSkinPath' => 'FooBar',
371  ],
372  'ResourceModuleSkinStyles' => [
373  'foobar' => [
374  'test.foo' => 'foo.css',
375  ]
376  ],
377  ],
378  // Expected
379  [
380  'wgResourceModuleSkinStyles' => [
381  'foobar' => [
382  'test.foo' => 'foo.css',
383  'localBasePath' => $dir,
384  'remoteSkinPath' => 'FooBar',
385  ],
386  ],
387  ],
388  ],
389  // ResourceModuleSkinStyles with file module paths and an override
390  [
391  // Input
392  [
393  'ResourceFileModulePaths' => [
394  'localBasePath' => '',
395  'remoteSkinPath' => 'FooBar',
396  ],
397  'ResourceModuleSkinStyles' => [
398  'foobar' => [
399  'test.foo' => 'foo.css',
400  'remoteSkinPath' => 'BarFoo'
401  ],
402  ],
403  ],
404  // Expected
405  [
406  'wgResourceModuleSkinStyles' => [
407  'foobar' => [
408  'test.foo' => 'foo.css',
409  'localBasePath' => $dir,
410  'remoteSkinPath' => 'BarFoo',
411  ],
412  ],
413  ],
414  ],
415  ];
416  }
417 
418  public static function provideSetToGlobal() {
419  return [
420  [
421  [ 'wgAPIModules', 'wgAvailableRights' ],
422  [],
423  [
424  'APIModules' => [ 'foobar' => 'ApiFooBar' ],
425  'AvailableRights' => [ 'foobar', 'unfoobar' ],
426  ],
427  [
428  'wgAPIModules' => [ 'foobar' => 'ApiFooBar' ],
429  'wgAvailableRights' => [ 'foobar', 'unfoobar' ],
430  ],
431  ],
432  [
433  [ 'wgAPIModules', 'wgAvailableRights' ],
434  [
435  'wgAPIModules' => [ 'barbaz' => 'ApiBarBaz' ],
436  'wgAvailableRights' => [ 'barbaz' ]
437  ],
438  [
439  'APIModules' => [ 'foobar' => 'ApiFooBar' ],
440  'AvailableRights' => [ 'foobar', 'unfoobar' ],
441  ],
442  [
443  'wgAPIModules' => [ 'barbaz' => 'ApiBarBaz', 'foobar' => 'ApiFooBar' ],
444  'wgAvailableRights' => [ 'barbaz', 'foobar', 'unfoobar' ],
445  ],
446  ],
447  [
448  [ 'wgGroupPermissions' ],
449  [
450  'wgGroupPermissions' => [
451  'sysop' => [ 'delete' ]
452  ],
453  ],
454  [
455  'GroupPermissions' => [
456  'sysop' => [ 'undelete' ],
457  'user' => [ 'edit' ]
458  ],
459  ],
460  [
461  'wgGroupPermissions' => [
462  'sysop' => [ 'delete', 'undelete' ],
463  'user' => [ 'edit' ]
464  ],
465  ]
466  ]
467  ];
468  }
469 
471  global $IP;
472  $globalSettings = TestingAccessWrapper::newFromClass(
473  ExtensionProcessor::class )->globalSettings;
474 
475  $schema = FormatJson::decode(
476  file_get_contents( "$IP/docs/extension.schema.json" ),
477  true
478  );
479  $missing = [];
480  foreach ( $globalSettings as $global ) {
481  if ( !isset( $schema['properties'][$global] ) ) {
482  $missing[] = $global;
483  }
484  }
485 
486  $this->assertEquals( [], $missing,
487  "The following global settings are not documented in docs/extension.schema.json" );
488  }
489 }
490 
496  public function __construct( $globals = [] ) {
497  $this->globals = $globals + $this->globals;
498  }
499 }
array $globals
Stuff that is going to be set to $GLOBALS.
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:806
static array testExtractInfo()
ExtensionProcessor::extractInfo.
$IP
Definition: WebStart.php:58
static array $default
'name' is absolutely required
$value
testExtractCredits()
ExtensionProcessor::extractCredits.
static newFromClass($className)
Allow access to non-public static methods and properties of the class.
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
const MERGE_STRATEGY
Special key that defines the merge strategy.
testExtractResourceLoaderModules($input, $expected)
ExtensionProcessor::extractResourceLoaderModules provideExtractResourceLoaderModules.
testExtractConfig()
ExtensionProcessor::extractConfig1.
extractInfo($path, array $info, $version)
testExtractInfo_namespaces()
ExtensionProcessor::extractInfo.
Allow overriding the default value of $this->globals so we can test merging.
testExtractExtensionMessagesFiles($input, $expected)
ExtensionProcessor::extractExtensionMessagesFiles provideExtractExtensionMessagesFiles.
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
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
testRegisterHooks($pre, $info, $expected)
ExtensionProcessor::extractHooks provideRegisterHooks.
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
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
testExtractMessagesDirs($input, $expected)
ExtensionProcessor::extractMessagesDirs provideExtractMessagesDirs.
static decode($value, $assoc=false)
Decodes a JSON string.
Definition: FormatJson.php:187
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:1446