MediaWiki REL1_32
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}
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
ResourceLoader module based on local JavaScript/CSS files.
testValidateScriptFile()
ResourceLoaderModule::validateScriptFile.
testGetVersionHash_parentDefinition()
ResourceLoaderModule::getVersionHash.
testGetVersionHash()
ResourceLoaderModule::getVersionHash.
testGetHeaders()
ResourceLoaderModule::getHeaders ResourceLoaderModule::getPreloadLinks.
testPlaceholderize()
ResourceLoaderModule::getRelativePaths ResourceLoaderModule::expandRelativePaths.
testBuildContentScripts( $raw, $build, $message=null)
provideBuildContentScripts ResourceLoaderModule::buildContent
getResourceLoaderContext( $options=[], ResourceLoader $rl=null)
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:2885