Add sslCAFile option to DatabaseMysqli
[mediawiki.git] / tests / phpunit / includes / resourceloader / ResourceLoaderModuleTest.php
blob7c7f1cf5c403f8f85312b28ecb8314b2440c196d
1 <?php
3 class ResourceLoaderModuleTest extends ResourceLoaderTestCase {
5 /**
6 * @covers ResourceLoaderModule::getVersionHash
7 */
8 public function testGetVersionHash() {
9 $context = $this->getResourceLoaderContext();
11 $baseParams = [
12 'scripts' => [ 'foo.js', 'bar.js' ],
13 'dependencies' => [ 'jquery', 'mediawiki' ],
14 'messages' => [ 'hello', 'world' ],
17 $module = new ResourceLoaderFileModule( $baseParams );
18 $version = json_encode( $module->getVersionHash( $context ) );
20 // Exactly the same
21 $module = new ResourceLoaderFileModule( $baseParams );
22 $this->assertEquals(
23 $version,
24 json_encode( $module->getVersionHash( $context ) ),
25 'Instance is insignificant'
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'
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'
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'
58 // Subclass
59 $module = new ResourceLoaderFileModuleTestModule( $baseParams );
60 $this->assertNotEquals(
61 $version,
62 json_encode( $module->getVersionHash( $context ) ),
63 'Class is significant'
67 /**
68 * @covers ResourceLoaderModule::validateScriptFile
70 public function testValidateScriptFile() {
71 $this->setMwGlobals( 'wgResourceLoaderValidateJS', true );
73 $context = $this->getResourceLoaderContext();
75 $module = new ResourceLoaderTestModule( [
76 'script' => "var a = 'this is';\n {\ninvalid"
77 ] );
78 $this->assertEquals(
79 'mw.log.error(' .
80 '"JavaScript parse error: Parse error: Unexpected token; ' .
81 'token } expected in file \'input\' on line 3"' .
82 ');',
83 $module->getScript( $context ),
84 'Replace invalid syntax with error logging'
87 $module = new ResourceLoaderTestModule( [
88 'script' => "\n'valid';"
89 ] );
90 $this->assertEquals(
91 "\n'valid';",
92 $module->getScript( $context ),
93 'Leave valid scripts as-is'
97 public static function provideBuildContentScripts() {
98 return [
100 "mw.foo()",
101 "mw.foo()\n",
104 "mw.foo();",
105 "mw.foo();\n",
108 "mw.foo();\n",
109 "mw.foo();\n",
112 "mw.foo()\n",
113 "mw.foo()\n",
116 "mw.foo()\n// mw.bar();",
117 "mw.foo()\n// mw.bar();\n",
120 "mw.foo()\n// mw.bar()",
121 "mw.foo()\n// mw.bar()\n",
124 "mw.foo()// mw.bar();",
125 "mw.foo()// mw.bar();\n",
131 * @dataProvider provideBuildContentScripts
132 * @covers ResourceLoaderModule::buildContent
134 public function testBuildContentScripts( $raw, $build, $message = null ) {
135 $context = $this->getResourceLoaderContext();
136 $module = new ResourceLoaderTestModule( [
137 'script' => $raw
138 ] );
139 $this->assertEquals( $raw, $module->getScript( $context ), 'Raw script' );
140 $this->assertEquals(
141 [ 'scripts' => $build ],
142 $module->getModuleContent( $context ),
143 $message
148 * @covers ResourceLoaderModule::getRelativePaths
149 * @covers ResourceLoaderModule::expandRelativePaths
151 public function testPlaceholderize() {
152 $getRelativePaths = new ReflectionMethod( 'ResourceLoaderModule', 'getRelativePaths' );
153 $getRelativePaths->setAccessible( true );
154 $expandRelativePaths = new ReflectionMethod( 'ResourceLoaderModule', 'expandRelativePaths' );
155 $expandRelativePaths->setAccessible( true );
157 $this->setMwGlobals( [
158 'IP' => '/srv/example/mediawiki/core',
159 ] );
160 $raw = [
161 '/srv/example/mediawiki/core/resources/foo.js',
162 '/srv/example/mediawiki/core/extensions/Example/modules/bar.js',
163 '/srv/example/mediawiki/skins/Example/baz.css',
164 '/srv/example/mediawiki/skins/Example/images/quux.png',
166 $canonical = [
167 'resources/foo.js',
168 'extensions/Example/modules/bar.js',
169 '../skins/Example/baz.css',
170 '../skins/Example/images/quux.png',
172 $this->assertEquals(
173 $canonical,
174 $getRelativePaths->invoke( null, $raw ),
175 'Insert placeholders'
177 $this->assertEquals(
178 $raw,
179 $expandRelativePaths->invoke( null, $canonical ),
180 'Substitute placeholders'
185 * @covers ResourceLoaderModule::getHeaders
186 * @covers ResourceLoaderModule::getPreloadLinks
188 public function testGetHeaders() {
189 $context = $this->getResourceLoaderContext();
191 $module = new ResourceLoaderTestModule();
192 $this->assertSame( [], $module->getHeaders( $context ), 'Default' );
194 $module = $this->getMockBuilder( ResourceLoaderTestModule::class )
195 ->setMethods( [ 'getPreloadLinks' ] )->getMock();
196 $module->method( 'getPreloadLinks' )->willReturn( [
197 'https://example.org/script.js' => [ 'as' => 'script' ],
198 ] );
199 $this->assertSame(
201 'Link: <https://example.org/script.js>;rel=preload;as=script'
203 $module->getHeaders( $context ),
204 'Preload one resource'
207 $module = $this->getMockBuilder( ResourceLoaderTestModule::class )
208 ->setMethods( [ 'getPreloadLinks' ] )->getMock();
209 $module->method( 'getPreloadLinks' )->willReturn( [
210 'https://example.org/script.js' => [ 'as' => 'script' ],
211 '/example.png' => [ 'as' => 'image' ],
212 ] );
213 $this->assertSame(
215 'Link: <https://example.org/script.js>;rel=preload;as=script,' .
216 '</example.png>;rel=preload;as=image'
218 $module->getHeaders( $context ),
219 'Preload two resources'