| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\ResourceLoader; |
| 4 |
|
| 5 |
use LogicException; |
| 6 |
use MediaWiki\MainConfigNames; |
| 7 |
use MediaWiki\ResourceLoader\FileModule; |
| 8 |
use MediaWiki\ResourceLoader\MessageBlobStore; |
| 9 |
use MediaWiki\ResourceLoader\Module; |
| 10 |
use MediaWiki\ResourceLoader\ResourceLoader; |
| 11 |
use ReflectionMethod; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class ModuleTest extends ResourceLoaderTestCase { |
| 18 |
|
| 19 |
public function testGetVersionHash() { |
| 20 |
$context = $this->getResourceLoaderContext( [ 'debug' => 'false' ] ); |
| 21 |
$msgBlobStore = $this->createMock( MessageBlobStore::class ); |
| 22 |
$msgBlobStore->method( 'getBlob' )->willReturn( '{}' ); |
| 23 |
$context->getResourceLoader()->setMessageBlobStore( $msgBlobStore ); |
| 24 |
|
| 25 |
$baseParams = [ |
| 26 |
'scripts' => [ 'foo.js', 'bar.js' ], |
| 27 |
'dependencies' => [ 'jquery', 'mediawiki' ], |
| 28 |
'messages' => [ 'hello', 'world' ], |
| 29 |
]; |
| 30 |
|
| 31 |
$module = new FileModule( $baseParams ); |
| 32 |
$module->setName( 'test' ); |
| 33 |
$version = json_encode( $module->getVersionHash( $context ) ); |
| 34 |
|
| 35 |
|
| 36 |
$module = new FileModule( $baseParams ); |
| 37 |
$module->setName( 'test' ); |
| 38 |
$this->assertEquals( |
| 39 |
$version, |
| 40 |
json_encode( $module->getVersionHash( $context ) ), |
| 41 |
'Instance is insignificant' |
| 42 |
); |
| 43 |
|
| 44 |
|
| 45 |
$module = new FileModule( [ |
| 46 |
'dependencies' => [ 'mediawiki', 'jquery' ], |
| 47 |
] + $baseParams ); |
| 48 |
$module->setName( 'test' ); |
| 49 |
$this->assertEquals( |
| 50 |
$version, |
| 51 |
json_encode( $module->getVersionHash( $context ) ), |
| 52 |
'Order of dependencies is insignificant' |
| 53 |
); |
| 54 |
|
| 55 |
|
| 56 |
$module = new FileModule( [ |
| 57 |
'messages' => [ 'world', 'hello' ], |
| 58 |
] + $baseParams ); |
| 59 |
$module->setName( 'test' ); |
| 60 |
$this->assertEquals( |
| 61 |
$version, |
| 62 |
json_encode( $module->getVersionHash( $context ) ), |
| 63 |
'Order of messages is insignificant' |
| 64 |
); |
| 65 |
|
| 66 |
|
| 67 |
$module = new FileModule( [ |
| 68 |
'scripts' => [ 'bar.js', 'foo.js' ], |
| 69 |
] + $baseParams ); |
| 70 |
$module->setName( 'test' ); |
| 71 |
$this->assertNotEquals( |
| 72 |
$version, |
| 73 |
json_encode( $module->getVersionHash( $context ) ), |
| 74 |
'Order of scripts is significant' |
| 75 |
); |
| 76 |
|
| 77 |
|
| 78 |
$module = new ResourceLoaderFileModuleTestingSubclass( $baseParams ); |
| 79 |
$module->setName( 'test' ); |
| 80 |
$this->assertNotEquals( |
| 81 |
$version, |
| 82 |
json_encode( $module->getVersionHash( $context ) ), |
| 83 |
'Class is significant' |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
public function testGetVersionHash_debug() { |
| 88 |
$module = new ResourceLoaderTestModule( [ 'script' => 'foo();' ] ); |
| 89 |
$module->setName( 'test' ); |
| 90 |
$context = $this->getResourceLoaderContext( [ 'debug' => 'true' ] ); |
| 91 |
$this->assertSame( '', $module->getVersionHash( $context ) ); |
| 92 |
} |
| 93 |
|
| 94 |
public function testGetVersionHash_length() { |
| 95 |
$context = $this->getResourceLoaderContext( [ 'debug' => 'false' ] ); |
| 96 |
$module = new ResourceLoaderTestModule( [ |
| 97 |
'script' => 'foo();' |
| 98 |
] ); |
| 99 |
$module->setName( 'test' ); |
| 100 |
$version = $module->getVersionHash( $context ); |
| 101 |
$this->assertSame( ResourceLoader::HASH_LENGTH, strlen( $version ), 'Hash length' ); |
| 102 |
} |
| 103 |
|
| 104 |
public function testGetVersionHash_parentDefinition() { |
| 105 |
$context = $this->getResourceLoaderContext( [ 'debug' => 'false' ] ); |
| 106 |
$module = $this->getMockBuilder( Module::class ) |
| 107 |
->onlyMethods( [ 'getDefinitionSummary' ] )->getMock(); |
| 108 |
$module->method( 'getDefinitionSummary' )->willReturn( [ 'a' => 'summary' ] ); |
| 109 |
$module->setName( 'test' ); |
| 110 |
|
| 111 |
$this->expectException( LogicException::class ); |
| 112 |
$this->expectExceptionMessage( 'must call parent' ); |
| 113 |
$module->getVersionHash( $context ); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public function testGetURLsForDebug() { |
| 121 |
$module = new ResourceLoaderTestModule( [ |
| 122 |
'script' => 'foo();', |
| 123 |
'styles' => '.foo { color: blue; }', |
| 124 |
] ); |
| 125 |
$context = $this->getResourceLoaderContext( [ 'debug' => 'true' ] ); |
| 126 |
$module->setConfig( $context->getResourceLoader()->getConfig() ); |
| 127 |
$module->setName( 'test' ); |
| 128 |
|
| 129 |
$this->assertEquals( |
| 130 |
[ |
| 131 |
'https://example.org/w/load.php?debug=1&lang=en&modules=test&only=scripts' |
| 132 |
], |
| 133 |
$module->getScriptURLsForDebug( $context ), |
| 134 |
'script urls debug=true' |
| 135 |
); |
| 136 |
$this->assertEquals( |
| 137 |
[ 'all' => [ |
| 138 |
'/w/load.php?debug=1&lang=en&modules=test&only=styles' |
| 139 |
] ], |
| 140 |
$module->getStyleURLsForDebug( $context ), |
| 141 |
'style urls debug=true' |
| 142 |
); |
| 143 |
|
| 144 |
$context = $this->getResourceLoaderContext( [ 'debug' => '2' ] ); |
| 145 |
$this->assertEquals( |
| 146 |
[ |
| 147 |
'https://example.org/w/load.php?debug=2&lang=en&modules=test&only=scripts' |
| 148 |
], |
| 149 |
$module->getScriptURLsForDebug( $context ), |
| 150 |
'script urls debug=2' |
| 151 |
); |
| 152 |
$this->assertEquals( |
| 153 |
[ 'all' => [ |
| 154 |
'/w/load.php?debug=2&lang=en&modules=test&only=styles' |
| 155 |
] ], |
| 156 |
$module->getStyleURLsForDebug( $context ), |
| 157 |
'style urls debug=2' |
| 158 |
); |
| 159 |
} |
| 160 |
|
| 161 |
public static function provideValidateScripts() { |
| 162 |
yield 'valid ES5' => [ "\n'valid';" ]; |
| 163 |
|
| 164 |
yield 'valid ES6/ES2015 for-of' => [ |
| 165 |
"var x = ['a', 'b']; for (var key of x) { console.log(key); }" |
| 166 |
]; |
| 167 |
|
| 168 |
yield 'valid ES2016 exponentiation' => [ |
| 169 |
"var x = 2; var y = 3; console.log(x ** y);" |
| 170 |
]; |
| 171 |
|
| 172 |
yield 'valid ES2017 async-await' => [ |
| 173 |
"var foo = async function(x) { return await x.fetch(); }", |
| 174 |
'Parse error: Unexpected: function on line 1' |
| 175 |
]; |
| 176 |
|
| 177 |
yield 'valid ES2018 spread in object literal' => [ |
| 178 |
"var x = {b: 2, c: 3}; var y = {a: 1, ...x};", |
| 179 |
'Parse error: Unexpected: ... on line 1' |
| 180 |
]; |
| 181 |
|
| 182 |
yield 'SyntaxError' => [ |
| 183 |
"var a = 'this is';\n {\ninvalid", |
| 184 |
'Parse error: Unclosed { on line 3' |
| 185 |
]; |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
$input = '"' . str_repeat( 'x', 10000 ) . '";'; |
| 190 |
yield 'double quote string 10K' => [ $input, ]; |
| 191 |
$input = '\'' . str_repeat( 'x', 10000 ) . '\';'; |
| 192 |
yield 'single quote string 10K' => [ $input ]; |
| 193 |
$input = '"' . str_repeat( '\u0021', 100 ) . '";'; |
| 194 |
yield 'escaping string 100' => [ $input ]; |
| 195 |
$input = '"' . str_repeat( '\u0021', 10000 ) . '";'; |
| 196 |
yield 'escaping string 10K' => [ $input ]; |
| 197 |
$input = '/' . str_repeat( 'x', 1000 ) . '/;'; |
| 198 |
yield 'regex 1K' => [ $input ]; |
| 199 |
$input = '/' . str_repeat( 'x', 10000 ) . '/;'; |
| 200 |
yield 'regex 10K' => [ $input ]; |
| 201 |
$input = '/' . str_repeat( '\u0021', 100 ) . '/;'; |
| 202 |
yield 'escaping regex 100' => [ $input ]; |
| 203 |
$input = '/' . str_repeat( '\u0021', 10000 ) . '/;'; |
| 204 |
yield 'escaping regex 10K' => [ $input ]; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public function testValidateScriptFile( $input, $error = null ) { |
| 211 |
$this->overrideConfigValue( MainConfigNames::ResourceLoaderValidateJS, true ); |
| 212 |
|
| 213 |
$context = $this->getResourceLoaderContext(); |
| 214 |
|
| 215 |
$module = new ResourceLoaderTestModule( [ |
| 216 |
'mayValidateScript' => true, |
| 217 |
'script' => $input |
| 218 |
] ); |
| 219 |
$module->setConfig( $context->getResourceLoader()->getConfig() ); |
| 220 |
|
| 221 |
$result = $module->getScript( $context ); |
| 222 |
if ( $error ) { |
| 223 |
$this->assertStringContainsString( 'mw.log.error(', $result, 'log error' ); |
| 224 |
$this->assertStringContainsString( $error, $result, 'error message' ); |
| 225 |
} else { |
| 226 |
$this->assertEquals( |
| 227 |
$input, |
| 228 |
$module->getScript( $context ), |
| 229 |
'Leave valid scripts as-is' |
| 230 |
); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
public static function provideBuildContentScripts() { |
| 235 |
return [ |
| 236 |
[ |
| 237 |
"mw.foo()", |
| 238 |
], |
| 239 |
[ |
| 240 |
"mw.foo();", |
| 241 |
], |
| 242 |
[ |
| 243 |
"mw.foo();\n", |
| 244 |
], |
| 245 |
[ |
| 246 |
"mw.foo()\n", |
| 247 |
], |
| 248 |
[ |
| 249 |
"mw.foo()\n// mw.bar();", |
| 250 |
], |
| 251 |
[ |
| 252 |
"mw.foo()\n// mw.bar()", |
| 253 |
], |
| 254 |
[ |
| 255 |
"mw.foo()// mw.bar();", |
| 256 |
], |
| 257 |
]; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
public function testBuildContentScripts( $raw, $message = '' ) { |
| 264 |
$context = $this->getResourceLoaderContext(); |
| 265 |
$module = new ResourceLoaderTestModule( [ |
| 266 |
'script' => $raw |
| 267 |
] ); |
| 268 |
$module->setName( 'test' ); |
| 269 |
$this->assertEquals( $raw, $module->getScript( $context ), 'Raw script' ); |
| 270 |
$this->assertEquals( |
| 271 |
[ 'plainScripts' => [ [ 'content' => $raw ] ] ], |
| 272 |
$module->getModuleContent( $context )[ 'scripts' ], |
| 273 |
$message |
| 274 |
); |
| 275 |
} |
| 276 |
|
| 277 |
public function testPlaceholderize() { |
| 278 |
$getRelativePaths = new ReflectionMethod( Module::class, 'getRelativePaths' ); |
| 279 |
$expandRelativePaths = new ReflectionMethod( Module::class, 'expandRelativePaths' ); |
| 280 |
|
| 281 |
$this->setMwGlobals( [ |
| 282 |
'IP' => '/srv/example/mediawiki/core', |
| 283 |
] ); |
| 284 |
$raw = [ |
| 285 |
'/srv/example/mediawiki/core/resources/foo.js', |
| 286 |
'/srv/example/mediawiki/core/extensions/Example/modules/bar.js', |
| 287 |
'/srv/example/mediawiki/skins/Example/baz.css', |
| 288 |
'/srv/example/mediawiki/skins/Example/images/quux.png', |
| 289 |
]; |
| 290 |
$canonical = [ |
| 291 |
'resources/foo.js', |
| 292 |
'extensions/Example/modules/bar.js', |
| 293 |
'../skins/Example/baz.css', |
| 294 |
'../skins/Example/images/quux.png', |
| 295 |
]; |
| 296 |
$this->assertEquals( |
| 297 |
$canonical, |
| 298 |
$getRelativePaths->invoke( null, $raw ), |
| 299 |
'Insert placeholders' |
| 300 |
); |
| 301 |
$this->assertEquals( |
| 302 |
$raw, |
| 303 |
$expandRelativePaths->invoke( null, $canonical ), |
| 304 |
'Substitute placeholders' |
| 305 |
); |
| 306 |
} |
| 307 |
|
| 308 |
public function testGetHeaders() { |
| 309 |
$context = $this->getResourceLoaderContext(); |
| 310 |
|
| 311 |
$module = new ResourceLoaderTestModule(); |
| 312 |
$module->setName( 'test' ); |
| 313 |
$this->assertSame( [], $module->getHeaders( $context ), 'Default' ); |
| 314 |
|
| 315 |
$module = $this->getMockBuilder( ResourceLoaderTestModule::class ) |
| 316 |
->onlyMethods( [ 'getPreloadLinks' ] )->getMock(); |
| 317 |
$module->method( 'getPreloadLinks' )->willReturn( [ |
| 318 |
'https://example.org/script.js' => [ 'as' => 'script' ], |
| 319 |
] ); |
| 320 |
$this->assertSame( |
| 321 |
[ |
| 322 |
'Link: <https://example.org/script.js>;rel=preload;as=script' |
| 323 |
], |
| 324 |
$module->getHeaders( $context ), |
| 325 |
'Preload one resource' |
| 326 |
); |
| 327 |
|
| 328 |
$module = $this->getMockBuilder( ResourceLoaderTestModule::class ) |
| 329 |
->onlyMethods( [ 'getPreloadLinks' ] )->getMock(); |
| 330 |
$module->method( 'getPreloadLinks' )->willReturn( [ |
| 331 |
'https://example.org/script.js' => [ 'as' => 'script' ], |
| 332 |
'/example.png' => [ 'as' => 'image' ], |
| 333 |
] ); |
| 334 |
$module->setName( 'test' ); |
| 335 |
$this->assertSame( |
| 336 |
[ |
| 337 |
'Link: <https://example.org/script.js>;rel=preload;as=script,' . |
| 338 |
'</example.png>;rel=preload;as=image' |
| 339 |
], |
| 340 |
$module->getHeaders( $context ), |
| 341 |
'Preload two resources' |
| 342 |
); |
| 343 |
} |
| 344 |
|
| 345 |
public static function provideGetDeprecationWarning() { |
| 346 |
return [ |
| 347 |
[ |
| 348 |
null, |
| 349 |
'normalModule', |
| 350 |
null, |
| 351 |
], |
| 352 |
[ |
| 353 |
true, |
| 354 |
'deprecatedModule', |
| 355 |
'This page is using the deprecated ResourceLoader module "deprecatedModule".', |
| 356 |
], |
| 357 |
[ |
| 358 |
'Will be removed tomorrow.', |
| 359 |
'deprecatedTomorrow', |
| 360 |
"This page is using the deprecated ResourceLoader module \"deprecatedTomorrow\".\n" . |
| 361 |
"Will be removed tomorrow.", |
| 362 |
], |
| 363 |
]; |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
public function testGetDeprecationWarning( $deprecated, $name, $expected ) { |
| 374 |
$module = new ResourceLoaderTestModule( [ 'deprecated' => $deprecated ] ); |
| 375 |
$module->setName( $name ); |
| 376 |
$this->assertSame( $expected, $module->getDeprecationWarning() ); |
| 377 |
|
| 378 |
$this->hideDeprecated( 'MediaWiki\ResourceLoader\Module::getDeprecationInformation' ); |
| 379 |
$info = $module->getDeprecationInformation( $this->getResourceLoaderContext() ); |
| 380 |
if ( !$expected ) { |
| 381 |
$this->assertSame( '', $info ); |
| 382 |
} else { |
| 383 |
$this->assertSame( 'mw.log.warn(' . json_encode( $expected ) . ');', $info ); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
} |