MediaWiki  1.28.0
ResourceLoaderModuleTest.php
Go to the documentation of this file.
1 <?php
2 
4 
8  public function testGetVersionHash() {
10 
11  $baseParams = [
12  'scripts' => [ 'foo.js', 'bar.js' ],
13  'dependencies' => [ 'jquery', 'mediawiki' ],
14  'messages' => [ 'hello', 'world' ],
15  ];
16 
17  $module = new ResourceLoaderFileModule( $baseParams );
18  $version = json_encode( $module->getVersionHash( $context ) );
19 
20  // Exactly the same
21  $module = new ResourceLoaderFileModule( $baseParams );
22  $this->assertEquals(
23  $version,
24  json_encode( $module->getVersionHash( $context ) ),
25  'Instance is insignificant'
26  );
27 
28  // Re-order dependencies
29  $module = new ResourceLoaderFileModule( [
30  'dependencies' => [ 'mediawiki', 'jquery' ],
31  ] + $baseParams );
32  $this->assertEquals(
33  $version,
34  json_encode( $module->getVersionHash( $context ) ),
35  'Order of dependencies is insignificant'
36  );
37 
38  // Re-order messages
39  $module = new ResourceLoaderFileModule( [
40  'messages' => [ 'world', 'hello' ],
41  ] + $baseParams );
42  $this->assertEquals(
43  $version,
44  json_encode( $module->getVersionHash( $context ) ),
45  'Order of messages is insignificant'
46  );
47 
48  // Re-order scripts
49  $module = new ResourceLoaderFileModule( [
50  'scripts' => [ 'bar.js', 'foo.js' ],
51  ] + $baseParams );
52  $this->assertNotEquals(
53  $version,
54  json_encode( $module->getVersionHash( $context ) ),
55  'Order of scripts is significant'
56  );
57 
58  // Subclass
59  $module = new ResourceLoaderFileModuleTestModule( $baseParams );
60  $this->assertNotEquals(
61  $version,
62  json_encode( $module->getVersionHash( $context ) ),
63  'Class is significant'
64  );
65  }
66 
70  public function testValidateScriptFile() {
71  $this->setMwGlobals( 'wgResourceLoaderValidateJS', true );
72 
74 
75  $module = new ResourceLoaderTestModule( [
76  'script' => "var a = 'this is';\n {\ninvalid"
77  ] );
78  $this->assertEquals(
79  $module->getScript( $context ),
80  'mw.log.error(' .
81  '"JavaScript parse error: Parse error: Unexpected token; ' .
82  'token } expected in file \'input\' on line 3"' .
83  ');',
84  'Replace invalid syntax with error logging'
85  );
86 
87  $module = new ResourceLoaderTestModule( [
88  'script' => "\n'valid';"
89  ] );
90  $this->assertEquals(
91  $module->getScript( $context ),
92  "\n'valid';",
93  'Leave valid scripts as-is'
94  );
95  }
96 
101  public function testPlaceholderize() {
102  $getRelativePaths = new ReflectionMethod( 'ResourceLoaderModule', 'getRelativePaths' );
103  $getRelativePaths->setAccessible( true );
104  $expandRelativePaths = new ReflectionMethod( 'ResourceLoaderModule', 'expandRelativePaths' );
105  $expandRelativePaths->setAccessible( true );
106 
107  $this->setMwGlobals( [
108  'IP' => '/srv/example/mediawiki/core',
109  ] );
110  $raw = [
111  '/srv/example/mediawiki/core/resources/foo.js',
112  '/srv/example/mediawiki/core/extensions/Example/modules/bar.js',
113  '/srv/example/mediawiki/skins/Example/baz.css',
114  '/srv/example/mediawiki/skins/Example/images/quux.png',
115  ];
116  $canonical = [
117  'resources/foo.js',
118  'extensions/Example/modules/bar.js',
119  '../skins/Example/baz.css',
120  '../skins/Example/images/quux.png',
121  ];
122  $this->assertEquals(
123  $getRelativePaths->invoke( null, $raw ),
124  $canonical,
125  'Insert placeholders'
126  );
127  $this->assertEquals(
128  $expandRelativePaths->invoke( null, $canonical ),
129  $raw,
130  'Substitute placeholders'
131  );
132  }
133 }
$context
Definition: load.php:50
getResourceLoaderContext($lang= 'en', $dir= 'ltr')
ResourceLoader module based on local JavaScript/CSS files.
testPlaceholderize()
ResourceLoaderModule::getRelativePaths ResourceLoaderModule::expandRelativePaths. ...
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
testGetVersionHash()
ResourceLoaderModule::getVersionHash.
testValidateScriptFile()
ResourceLoaderModule::validateScriptFile.
setMwGlobals($pairs, $value=null)