MediaWiki  1.32.5
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 
72  $module = $this->getMockBuilder( ResourceLoaderModule::class )
73  ->setMethods( [ 'getDefinitionSummary' ] )->getMock();
74  $module->method( 'getDefinitionSummary' )->willReturn( [ 'a' => 'summary' ] );
75 
76  $this->setExpectedException( LogicException::class, 'must call parent' );
77  $module->getVersionHash( $context );
78  }
79 
83  public function testValidateScriptFile() {
84  $this->setMwGlobals( 'wgResourceLoaderValidateJS', true );
85 
87 
88  $module = new ResourceLoaderTestModule( [
89  'script' => "var a = 'this is';\n {\ninvalid"
90  ] );
91  $this->assertEquals(
92  'mw.log.error(' .
93  '"JavaScript parse error: Parse error: Unexpected token; ' .
94  'token } expected in file \'input\' on line 3"' .
95  ');',
96  $module->getScript( $context ),
97  'Replace invalid syntax with error logging'
98  );
99 
100  $module = new ResourceLoaderTestModule( [
101  'script' => "\n'valid';"
102  ] );
103  $this->assertEquals(
104  "\n'valid';",
105  $module->getScript( $context ),
106  'Leave valid scripts as-is'
107  );
108  }
109 
110  public static function provideBuildContentScripts() {
111  return [
112  [
113  "mw.foo()",
114  "mw.foo()\n",
115  ],
116  [
117  "mw.foo();",
118  "mw.foo();\n",
119  ],
120  [
121  "mw.foo();\n",
122  "mw.foo();\n",
123  ],
124  [
125  "mw.foo()\n",
126  "mw.foo()\n",
127  ],
128  [
129  "mw.foo()\n// mw.bar();",
130  "mw.foo()\n// mw.bar();\n",
131  ],
132  [
133  "mw.foo()\n// mw.bar()",
134  "mw.foo()\n// mw.bar()\n",
135  ],
136  [
137  "mw.foo()// mw.bar();",
138  "mw.foo()// mw.bar();\n",
139  ],
140  ];
141  }
142 
147  public function testBuildContentScripts( $raw, $build, $message = null ) {
149  $module = new ResourceLoaderTestModule( [
150  'script' => $raw
151  ] );
152  $this->assertEquals( $raw, $module->getScript( $context ), 'Raw script' );
153  $this->assertEquals(
154  $build,
155  $module->getModuleContent( $context )[ 'scripts' ],
156  $message
157  );
158  }
159 
164  public function testPlaceholderize() {
165  $getRelativePaths = new ReflectionMethod( ResourceLoaderModule::class, 'getRelativePaths' );
166  $getRelativePaths->setAccessible( true );
167  $expandRelativePaths = new ReflectionMethod( ResourceLoaderModule::class, 'expandRelativePaths' );
168  $expandRelativePaths->setAccessible( true );
169 
170  $this->setMwGlobals( [
171  'IP' => '/srv/example/mediawiki/core',
172  ] );
173  $raw = [
174  '/srv/example/mediawiki/core/resources/foo.js',
175  '/srv/example/mediawiki/core/extensions/Example/modules/bar.js',
176  '/srv/example/mediawiki/skins/Example/baz.css',
177  '/srv/example/mediawiki/skins/Example/images/quux.png',
178  ];
179  $canonical = [
180  'resources/foo.js',
181  'extensions/Example/modules/bar.js',
182  '../skins/Example/baz.css',
183  '../skins/Example/images/quux.png',
184  ];
185  $this->assertEquals(
186  $canonical,
187  $getRelativePaths->invoke( null, $raw ),
188  'Insert placeholders'
189  );
190  $this->assertEquals(
191  $raw,
192  $expandRelativePaths->invoke( null, $canonical ),
193  'Substitute placeholders'
194  );
195  }
196 
201  public function testGetHeaders() {
203 
204  $module = new ResourceLoaderTestModule();
205  $this->assertSame( [], $module->getHeaders( $context ), 'Default' );
206 
207  $module = $this->getMockBuilder( ResourceLoaderTestModule::class )
208  ->setMethods( [ 'getPreloadLinks' ] )->getMock();
209  $module->method( 'getPreloadLinks' )->willReturn( [
210  'https://example.org/script.js' => [ 'as' => 'script' ],
211  ] );
212  $this->assertSame(
213  [
214  'Link: <https://example.org/script.js>;rel=preload;as=script'
215  ],
216  $module->getHeaders( $context ),
217  'Preload one resource'
218  );
219 
220  $module = $this->getMockBuilder( ResourceLoaderTestModule::class )
221  ->setMethods( [ 'getPreloadLinks' ] )->getMock();
222  $module->method( 'getPreloadLinks' )->willReturn( [
223  'https://example.org/script.js' => [ 'as' => 'script' ],
224  '/example.png' => [ 'as' => 'image' ],
225  ] );
226  $this->assertSame(
227  [
228  'Link: <https://example.org/script.js>;rel=preload;as=script,' .
229  '</example.png>;rel=preload;as=image'
230  ],
231  $module->getHeaders( $context ),
232  'Preload two resources'
233  );
234  }
235 }
$context
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition: hooks.txt:2683
ResourceLoaderFileModuleTestModule
Definition: ResourceLoaderTestCase.php:170
ResourceLoaderModuleTest\provideBuildContentScripts
static provideBuildContentScripts()
Definition: ResourceLoaderModuleTest.php:110
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
ResourceLoaderModuleTest\testGetVersionHash_parentDefinition
testGetVersionHash_parentDefinition()
ResourceLoaderModule::getVersionHash.
Definition: ResourceLoaderModuleTest.php:70
ResourceLoaderModuleTest
Definition: ResourceLoaderModuleTest.php:3
ResourceLoaderFileModule
ResourceLoader module based on local JavaScript/CSS files.
Definition: ResourceLoaderFileModule.php:28
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:706
ResourceLoaderModuleTest\testGetVersionHash
testGetVersionHash()
ResourceLoaderModule::getVersionHash.
Definition: ResourceLoaderModuleTest.php:8
ResourceLoaderModuleTest\testValidateScriptFile
testValidateScriptFile()
ResourceLoaderModule::validateScriptFile.
Definition: ResourceLoaderModuleTest.php:83
ResourceLoaderTestModule
Definition: ResourceLoaderTestCase.php:86
ResourceLoaderModuleTest\testGetHeaders
testGetHeaders()
ResourceLoaderModule::getHeaders ResourceLoaderModule::getPreloadLinks.
Definition: ResourceLoaderModuleTest.php:201
ResourceLoaderModuleTest\testPlaceholderize
testPlaceholderize()
ResourceLoaderModule::getRelativePaths ResourceLoaderModule::expandRelativePaths.
Definition: ResourceLoaderModuleTest.php:164
ResourceLoaderModuleTest\testBuildContentScripts
testBuildContentScripts( $raw, $build, $message=null)
provideBuildContentScripts ResourceLoaderModule::buildContent
Definition: ResourceLoaderModuleTest.php:147
ResourceLoaderTestCase\getResourceLoaderContext
getResourceLoaderContext( $options=[], ResourceLoader $rl=null)
Definition: ResourceLoaderTestCase.php:23
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
ResourceLoaderTestCase
Definition: ResourceLoaderTestCase.php:7