| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\ResourceLoader; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use InvalidArgumentException; |
| 7 |
use MediaWiki\Config\Config; |
| 8 |
use MediaWiki\Config\HashConfig; |
| 9 |
use MediaWiki\Html\HtmlJsCode; |
| 10 |
use MediaWiki\MainConfigNames; |
| 11 |
use MediaWiki\Registration\ExtensionRegistry; |
| 12 |
use MediaWiki\Request\FauxRequest; |
| 13 |
use MediaWiki\ResourceLoader\Context; |
| 14 |
use MediaWiki\ResourceLoader\FileModule; |
| 15 |
use MediaWiki\ResourceLoader\ResourceLoader; |
| 16 |
use MediaWiki\ResourceLoader\SkinModule; |
| 17 |
use MediaWiki\ResourceLoader\StartUpModule; |
| 18 |
use MediaWiki\User\Options\StaticUserOptionsLookup; |
| 19 |
use Psr\Log\LoggerInterface; |
| 20 |
use Psr\Log\NullLogger; |
| 21 |
use RuntimeException; |
| 22 |
use UnexpectedValueException; |
| 23 |
use Wikimedia\Minify\IdentityMinifierState; |
| 24 |
use Wikimedia\Stats\NullStatsdDataFactory; |
| 25 |
use Wikimedia\TestingAccessWrapper; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
class ResourceLoaderTest extends ResourceLoaderTestCase { |
| 32 |
|
| 33 |
protected function setUp(): void { |
| 34 |
parent::setUp(); |
| 35 |
|
| 36 |
$this->overrideConfigValue( MainConfigNames::ShowExceptionDetails, true ); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public function testServiceWiring() { |
| 44 |
$ranHook = 0; |
| 45 |
$this->setTemporaryHook( |
| 46 |
'ResourceLoaderRegisterModules', |
| 47 |
static function ( &$resourceLoader ) use ( &$ranHook ) { |
| 48 |
$ranHook++; |
| 49 |
} |
| 50 |
); |
| 51 |
|
| 52 |
$this->getServiceContainer()->getResourceLoader(); |
| 53 |
|
| 54 |
$this->assertSame( 1, $ranHook, 'Hook was called' ); |
| 55 |
} |
| 56 |
|
| 57 |
public static function provideInvalidModuleName() { |
| 58 |
return [ |
| 59 |
'name with 300 chars' => [ str_repeat( 'x', 300 ) ], |
| 60 |
'name with bang' => [ 'this!that' ], |
| 61 |
'name with comma' => [ 'this,that' ], |
| 62 |
'name with pipe' => [ 'this|that' ], |
| 63 |
]; |
| 64 |
} |
| 65 |
|
| 66 |
public static function provideValidModuleName() { |
| 67 |
return [ |
| 68 |
'empty string' => [ '' ], |
| 69 |
'simple name' => [ 'this.and-that2' ], |
| 70 |
'name with 100 chars' => [ str_repeat( 'x', 100 ) ], |
| 71 |
'name with hash' => [ 'this#that' ], |
| 72 |
'name with slash' => [ 'this/that' ], |
| 73 |
'name with at' => [ 'this@that' ], |
| 74 |
]; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public function testIsValidModuleName_invalid( $name ) { |
| 81 |
$this->assertFalse( ResourceLoader::isValidModuleName( $name ) ); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public function testIsValidModuleName_valid( $name ) { |
| 88 |
$this->assertTrue( ResourceLoader::isValidModuleName( $name ) ); |
| 89 |
} |
| 90 |
|
| 91 |
public function testRegisterValidArray() { |
| 92 |
$resourceLoader = new EmptyResourceLoader(); |
| 93 |
|
| 94 |
|
| 95 |
$resourceLoader->register( 'test', [ 'class' => ResourceLoaderTestModule::class ] ); |
| 96 |
$this->assertInstanceOf( |
| 97 |
ResourceLoaderTestModule::class, |
| 98 |
$resourceLoader->getModule( 'test' ) |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public function testRegisterEmptyString() { |
| 106 |
$resourceLoader = new EmptyResourceLoader(); |
| 107 |
$resourceLoader->register( '', [ 'class' => ResourceLoaderTestModule::class ] ); |
| 108 |
$this->assertInstanceOf( |
| 109 |
ResourceLoaderTestModule::class, |
| 110 |
$resourceLoader->getModule( '' ) |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public function testRegisterInvalidName() { |
| 118 |
$resourceLoader = new EmptyResourceLoader(); |
| 119 |
$this->expectException( InvalidArgumentException::class ); |
| 120 |
$this->expectExceptionMessage( "name 'test!invalid' is invalid" ); |
| 121 |
$resourceLoader->register( 'test!invalid', [] ); |
| 122 |
} |
| 123 |
|
| 124 |
public function testRegisterInvalidType() { |
| 125 |
$resourceLoader = new EmptyResourceLoader(); |
| 126 |
$this->expectException( InvalidArgumentException::class ); |
| 127 |
$this->expectExceptionMessage( 'Invalid module info' ); |
| 128 |
$resourceLoader->register( [ 'test' => (object)[] ] ); |
| 129 |
} |
| 130 |
|
| 131 |
public function testRegisterDuplicate() { |
| 132 |
$logger = $this->createMock( LoggerInterface::class ); |
| 133 |
$logger->expects( $this->once() ) |
| 134 |
->method( 'warning' ); |
| 135 |
$resourceLoader = new EmptyResourceLoader( null, $logger ); |
| 136 |
|
| 137 |
$resourceLoader->register( 'test', [ 'class' => SkinModule::class ] ); |
| 138 |
$resourceLoader->register( 'test', [ 'class' => StartUpModule::class ] ); |
| 139 |
$this->assertInstanceOf( |
| 140 |
StartUpModule::class, |
| 141 |
$resourceLoader->getModule( 'test' ), |
| 142 |
'last one wins' |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
public function testGetModuleNames() { |
| 147 |
|
| 148 |
$resourceLoader = new EmptyResourceLoader(); |
| 149 |
$resourceLoader->register( 'test.foo', [] ); |
| 150 |
$resourceLoader->register( 'test.bar', [] ); |
| 151 |
$this->assertEquals( |
| 152 |
[ 'startup', 'test.foo', 'test.bar' ], |
| 153 |
$resourceLoader->getModuleNames() |
| 154 |
); |
| 155 |
} |
| 156 |
|
| 157 |
public function testIsModuleRegistered() { |
| 158 |
$rl = new EmptyResourceLoader(); |
| 159 |
$rl->register( 'test', [] ); |
| 160 |
$this->assertTrue( $rl->isModuleRegistered( 'test' ) ); |
| 161 |
$this->assertFalse( $rl->isModuleRegistered( 'test.unknown' ) ); |
| 162 |
} |
| 163 |
|
| 164 |
public function testGetModuleUnknown() { |
| 165 |
$rl = new EmptyResourceLoader(); |
| 166 |
$this->assertSame( null, $rl->getModule( 'test' ) ); |
| 167 |
} |
| 168 |
|
| 169 |
public function testGetModuleClass() { |
| 170 |
$rl = new EmptyResourceLoader(); |
| 171 |
$rl->register( 'test', [ 'class' => ResourceLoaderTestModule::class ] ); |
| 172 |
$this->assertInstanceOf( |
| 173 |
ResourceLoaderTestModule::class, |
| 174 |
$rl->getModule( 'test' ) |
| 175 |
); |
| 176 |
} |
| 177 |
|
| 178 |
public function testGetModuleFactory() { |
| 179 |
$factory = function ( array $info ) { |
| 180 |
$this->assertArrayHasKey( 'kitten', $info ); |
| 181 |
unset( $info['kitten'] ); |
| 182 |
return new ResourceLoaderTestModule( $info ); |
| 183 |
}; |
| 184 |
|
| 185 |
$rl = new EmptyResourceLoader(); |
| 186 |
$rl->register( 'test', [ 'factory' => $factory, 'kitten' => 'little ball of fur' ] ); |
| 187 |
$this->assertInstanceOf( |
| 188 |
ResourceLoaderTestModule::class, |
| 189 |
$rl->getModule( 'test' ) |
| 190 |
); |
| 191 |
} |
| 192 |
|
| 193 |
public function testGetModuleClassDefault() { |
| 194 |
$rl = new EmptyResourceLoader(); |
| 195 |
$rl->register( 'test', [] ); |
| 196 |
$this->assertInstanceOf( |
| 197 |
FileModule::class, |
| 198 |
$rl->getModule( 'test' ), |
| 199 |
'Array-style module registrations default to FileModule' |
| 200 |
); |
| 201 |
} |
| 202 |
|
| 203 |
public function testGetVersionHash_length() { |
| 204 |
$hash = ResourceLoader::makeHash( |
| 205 |
'Anything you do could have serious repercussions on future events.' |
| 206 |
); |
| 207 |
$this->assertSame( 'xhh1x', $hash, 'Hash' ); |
| 208 |
$this->assertSame( ResourceLoader::HASH_LENGTH, strlen( $hash ), 'Hash length' ); |
| 209 |
} |
| 210 |
|
| 211 |
public function testLessImportDirs() { |
| 212 |
$rl = new EmptyResourceLoader(); |
| 213 |
$lc = $rl->getLessCompiler( [ 'foo' => '2px', 'Foo' => '#eeeeee' ] ); |
| 214 |
$basePath = dirname( dirname( __DIR__ ) ) . '/data/less'; |
| 215 |
$lc->SetImportDirs( [ |
| 216 |
"$basePath/common" => '', |
| 217 |
] ); |
| 218 |
$css = $lc->parseFile( "$basePath/module/use-import-dir.less" )->getCss(); |
| 219 |
$this->assertStringEqualsFile( "$basePath/module/styles.css", $css ); |
| 220 |
} |
| 221 |
|
| 222 |
public static function provideLessImportRemappingCases() { |
| 223 |
$basePath = dirname( dirname( __DIR__ ) ) . '/data/less'; |
| 224 |
return [ |
| 225 |
[ |
| 226 |
'input' => "$basePath/import-codex-icons.less", |
| 227 |
'expected' => "$basePath/import-codex-icons.css" |
| 228 |
], |
| 229 |
[ |
| 230 |
'input' => "$basePath/import-codex-icons.less", |
| 231 |
'expected' => "$basePath/import-codex-icons-devmode.css", |
| 232 |
'exception' => null, |
| 233 |
'devmode' => true |
| 234 |
], |
| 235 |
[ |
| 236 |
'input' => "$basePath/import-codex-tokens.less", |
| 237 |
'expected' => "$basePath/import-codex-tokens.css" |
| 238 |
], |
| 239 |
[ |
| 240 |
'input' => "$basePath/import-codex-tokens.less", |
| 241 |
'expected' => "$basePath/import-codex-tokens-devmode.css", |
| 242 |
'exception' => null, |
| 243 |
'devmode' => true |
| 244 |
], |
| 245 |
[ |
| 246 |
'input' => "$basePath/import-codex-tokens-npm.less", |
| 247 |
'expected' => null, |
| 248 |
'exception' => [ |
| 249 |
'class' => Exception::class, |
| 250 |
'message' => 'Importing from @wikimedia/codex-design-tokens is not supported. ' . |
| 251 |
"To use the Codex tokens, use `@import 'mediawiki.skin.variables.less';` instead." |
| 252 |
] |
| 253 |
] |
| 254 |
]; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
public function testLessImportRemapping( $input, $expected, $exception = null, $devmode = false ) { |
| 261 |
$configOverrides = []; |
| 262 |
if ( $devmode ) { |
| 263 |
$devDir = MW_INSTALL_PATH . '/tests/phpunit/data/resourceloader/codex-devmode'; |
| 264 |
$configOverrides += [ |
| 265 |
MainConfigNames::CodexDevelopmentDir => $devDir |
| 266 |
]; |
| 267 |
} |
| 268 |
|
| 269 |
$this->overrideConfigValues( $configOverrides ); |
| 270 |
|
| 271 |
|
| 272 |
$baseConfig = static::getSettings(); |
| 273 |
$rl = new EmptyResourceLoader( new HashConfig( $configOverrides + $baseConfig ) ); |
| 274 |
$lc = $rl->getLessCompiler(); |
| 275 |
|
| 276 |
if ( $exception !== null ) { |
| 277 |
if ( isset( $exception['class'] ) ) { |
| 278 |
$this->expectException( $exception['class'] ); |
| 279 |
} |
| 280 |
if ( isset( $exception['message'] ) ) { |
| 281 |
$this->expectExceptionMessage( $exception['message'] ); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
$css = $lc->parseFile( $input )->getCss(); |
| 286 |
if ( $expected !== null ) { |
| 287 |
$this->assertStringEqualsFile( $expected, $css ); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
public static function provideMediaWikiVariablesCases() { |
| 292 |
$basePath = __DIR__ . '/../../data/less'; |
| 293 |
return [ |
| 294 |
[ |
| 295 |
'config' => [], |
| 296 |
'importPaths' => [], |
| 297 |
'skin' => 'fallback', |
| 298 |
'expected' => "$basePath/use-variables-default.css", |
| 299 |
], |
| 300 |
[ |
| 301 |
'config' => [ |
| 302 |
MainConfigNames::ValidSkinNames => [ |
| 303 |
|
| 304 |
'example' => 'Example', |
| 305 |
], |
| 306 |
], |
| 307 |
'importPaths' => [ |
| 308 |
'example' => "$basePath/testvariables/", |
| 309 |
], |
| 310 |
'skin' => 'example', |
| 311 |
'expected' => "$basePath/use-variables-test.css", |
| 312 |
] |
| 313 |
]; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
public function testMediaWikiVariablesDefault( array $config, array $importPaths, $skin, $expectedFile ) { |
| 320 |
$this->overrideConfigValues( $config ); |
| 321 |
$reset = ExtensionRegistry::getInstance()->setAttributeForTest( 'SkinLessImportPaths', $importPaths ); |
| 322 |
|
| 323 |
$context = $this->getResourceLoaderContext( [ 'skin' => $skin ] ); |
| 324 |
$module = new FileModule( [ |
| 325 |
'localBasePath' => __DIR__ . '/../../data/less', |
| 326 |
'styles' => [ 'use-variables.less' ], |
| 327 |
] ); |
| 328 |
$module->setConfig( $context->getResourceLoader()->getConfig() ); |
| 329 |
$module->setName( 'test.less' ); |
| 330 |
$styles = $module->getStyles( $context ); |
| 331 |
$this->assertStringEqualsFile( $expectedFile, $styles['all'] ); |
| 332 |
} |
| 333 |
|
| 334 |
public static function providePackedModules() { |
| 335 |
return [ |
| 336 |
[ |
| 337 |
'Example from makePackedModulesString doc comment', |
| 338 |
[ 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' ], |
| 339 |
'foo.bar,baz|bar.baz,quux', |
| 340 |
], |
| 341 |
[ |
| 342 |
'Example from expandModuleNames doc comment', |
| 343 |
[ 'jquery.foo', 'jquery.bar', 'jquery.ui.baz', 'jquery.ui.quux' ], |
| 344 |
'jquery.foo,bar|jquery.ui.baz,quux', |
| 345 |
], |
| 346 |
[ |
| 347 |
'Regression fixed in r87497 (7fee86c38e) with dotless names', |
| 348 |
[ 'foo', 'bar', 'baz' ], |
| 349 |
'foo,bar,baz', |
| 350 |
], |
| 351 |
[ |
| 352 |
'Prefixless modules after a prefixed module', |
| 353 |
[ 'single.module', 'foobar', 'foobaz' ], |
| 354 |
'single.module|foobar,foobaz', |
| 355 |
], |
| 356 |
[ |
| 357 |
'Ordering', |
| 358 |
[ 'foo', 'foo.baz', 'baz.quux', 'foo.bar' ], |
| 359 |
'foo|foo.baz,bar|baz.quux', |
| 360 |
[ 'foo', 'foo.baz', 'foo.bar', 'baz.quux' ], |
| 361 |
] |
| 362 |
]; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
public function testMakePackedModulesString( $desc, $modules, $packed ) { |
| 369 |
$this->assertEquals( $packed, ResourceLoader::makePackedModulesString( $modules ), $desc ); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
public function testExpandModuleNames( $desc, $modules, $packed, $unpacked = null ) { |
| 376 |
$this->assertEquals( |
| 377 |
$unpacked ?: $modules, |
| 378 |
ResourceLoader::expandModuleNames( $packed ), |
| 379 |
$desc |
| 380 |
); |
| 381 |
} |
| 382 |
|
| 383 |
public static function provideAddSource() { |
| 384 |
return [ |
| 385 |
[ 'foowiki', 'https://example.org/w/load.php', 'foowiki' ], |
| 386 |
[ 'foowiki', [ 'loadScript' => 'https://example.org/w/load.php' ], 'foowiki' ], |
| 387 |
[ |
| 388 |
[ |
| 389 |
'foowiki' => 'https://example.org/w/load.php', |
| 390 |
'bazwiki' => 'https://example.com/w/load.php', |
| 391 |
], |
| 392 |
null, |
| 393 |
[ 'foowiki', 'bazwiki' ] |
| 394 |
] |
| 395 |
]; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
public function testAddSource( $name, $info, $expected ) { |
| 402 |
$rl = new EmptyResourceLoader; |
| 403 |
$rl->addSource( $name, $info ); |
| 404 |
if ( is_array( $expected ) ) { |
| 405 |
foreach ( $expected as $source ) { |
| 406 |
$this->assertArrayHasKey( $source, $rl->getSources() ); |
| 407 |
} |
| 408 |
} else { |
| 409 |
$this->assertArrayHasKey( $expected, $rl->getSources() ); |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
public function testAddSourceDupe() { |
| 414 |
$rl = new EmptyResourceLoader; |
| 415 |
$this->expectException( RuntimeException::class ); |
| 416 |
$this->expectExceptionMessage( 'Cannot register source' ); |
| 417 |
$rl->addSource( 'foo', 'https://example.org/w/load.php' ); |
| 418 |
$rl->addSource( 'foo', 'https://example.com/w/load.php' ); |
| 419 |
} |
| 420 |
|
| 421 |
public function testAddSourceInvalid() { |
| 422 |
$rl = new EmptyResourceLoader; |
| 423 |
$this->expectException( InvalidArgumentException::class ); |
| 424 |
$this->expectExceptionMessage( 'must have a "loadScript" key' ); |
| 425 |
$rl->addSource( 'foo', [ 'x' => 'https://example.org/w/load.php' ] ); |
| 426 |
} |
| 427 |
|
| 428 |
public static function provideLoaderImplement() { |
| 429 |
return [ |
| 430 |
'Implement scripts, styles and messages' => [ [ |
| 431 |
'name' => 'test.example', |
| 432 |
'scripts' => 'mw.example();', |
| 433 |
'styles' => [ 'css' => [ '.mw-example {}' ] ], |
| 434 |
'messages' => [ 'example' => '' ], |
| 435 |
'templates' => [], |
| 436 |
|
| 437 |
'expected' => 'mw.loader.impl(function(){return["test.example@1",function($,jQuery,require,module){mw.example(); |
| 438 |
},{"css":[".mw-example {}"]},{"example":""}];});', |
| 439 |
] ], |
| 440 |
'Implement scripts' => [ [ |
| 441 |
'name' => 'test.example', |
| 442 |
'scripts' => 'mw.example();', |
| 443 |
'styles' => [], |
| 444 |
|
| 445 |
'expected' => 'mw.loader.impl(function(){return["test.example@1",function($,jQuery,require,module){mw.example(); |
| 446 |
}];});', |
| 447 |
] ], |
| 448 |
'Implement scripts with newline at end' => [ [ |
| 449 |
'name' => 'test.example', |
| 450 |
'scripts' => "mw.example();\n", |
| 451 |
'styles' => [], |
| 452 |
|
| 453 |
'expected' => 'mw.loader.impl(function(){return["test.example@1",function($,jQuery,require,module){mw.example(); |
| 454 |
}];});', |
| 455 |
] ], |
| 456 |
'Implement scripts with comment at end' => [ [ |
| 457 |
'name' => 'test.example', |
| 458 |
'scripts' => "mw.example();//Foo", |
| 459 |
'styles' => [], |
| 460 |
|
| 461 |
'expected' => 'mw.loader.impl(function(){return["test.example@1",function($,jQuery,require,module){mw.example();//Foo |
| 462 |
}];});', |
| 463 |
] ], |
| 464 |
'Implement styles' => [ [ |
| 465 |
'name' => 'test.example', |
| 466 |
'scripts' => [], |
| 467 |
'styles' => [ 'css' => [ '.mw-example {}' ] ], |
| 468 |
|
| 469 |
'expected' => 'mw.loader.impl(function(){return["test.example@1",[],{"css":[".mw-example {}"]}];});', |
| 470 |
] ], |
| 471 |
'Implement scripts and messages' => [ [ |
| 472 |
'name' => 'test.example', |
| 473 |
'scripts' => 'mw.example();', |
| 474 |
'messages' => [ 'example' => '' ], |
| 475 |
|
| 476 |
'expected' => 'mw.loader.impl(function(){return["test.example@1",function($,jQuery,require,module){mw.example(); |
| 477 |
},{},{"example":""}];});', |
| 478 |
] ], |
| 479 |
'Implement scripts and templates' => [ [ |
| 480 |
'name' => 'test.example', |
| 481 |
'scripts' => 'mw.example();', |
| 482 |
'templates' => [ 'example.html' => '' ], |
| 483 |
|
| 484 |
'expected' => 'mw.loader.impl(function(){return["test.example@1",function($,jQuery,require,module){mw.example(); |
| 485 |
},{},{},{"example.html":""}];});', |
| 486 |
] ], |
| 487 |
'Implement unwrapped user script' => [ [ |
| 488 |
'name' => 'user', |
| 489 |
'scripts' => 'mw.example( 1 );', |
| 490 |
'wrap' => false, |
| 491 |
|
| 492 |
'expected' => 'mw.loader.impl(function(){return["user@1","mw.example( 1 );"];});', |
| 493 |
] ], |
| 494 |
'Implement multi-file script' => [ [ |
| 495 |
'name' => 'test.multifile', |
| 496 |
'scripts' => [ |
| 497 |
'files' => [ |
| 498 |
'one.js' => [ |
| 499 |
'type' => 'script', |
| 500 |
'content' => 'mw.example( 1 );', |
| 501 |
], |
| 502 |
'two.json' => [ |
| 503 |
'type' => 'data', |
| 504 |
'content' => [ 'n' => 2 ], |
| 505 |
], |
| 506 |
'three.js' => [ |
| 507 |
'type' => 'script', |
| 508 |
'content' => 'mw.example( 3 ); // Comment' |
| 509 |
], |
| 510 |
'four.js' => [ |
| 511 |
'type' => 'script', |
| 512 |
'content' => "mw.example( 4 );\n" |
| 513 |
], |
| 514 |
'five.js' => [ |
| 515 |
'type' => 'script', |
| 516 |
'content' => 'mw.example( 5 );' |
| 517 |
], |
| 518 |
], |
| 519 |
'main' => 'five.js', |
| 520 |
], |
| 521 |
|
| 522 |
'expected' => <<<END |
| 523 |
mw.loader.impl(function(){return["test.multifile@1",{"main":"five.js","files":{"one.js":function(require,module,exports){mw.example( 1 ); |
| 524 |
},"two.json":{ |
| 525 |
"n": 2 |
| 526 |
},"three.js":function(require,module,exports){mw.example( 3 ); // Comment |
| 527 |
},"four.js":function(require,module,exports){mw.example( 4 ); |
| 528 |
},"five.js":function(require,module,exports){mw.example( 5 ); |
| 529 |
}}}];}); |
| 530 |
END |
| 531 |
] ], |
| 532 |
]; |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
public function testAddImplementScript( $case ) { |
| 539 |
$case += [ |
| 540 |
'version' => '1', |
| 541 |
'wrap' => true, |
| 542 |
'styles' => [], |
| 543 |
'templates' => [], |
| 544 |
'messages' => new HtmlJsCode( '{}' ), |
| 545 |
'packageFiles' => [], |
| 546 |
]; |
| 547 |
$rl = TestingAccessWrapper::newFromObject( new EmptyResourceLoader ); |
| 548 |
$minifier = new IdentityMinifierState; |
| 549 |
$rl->addImplementScript( |
| 550 |
$minifier, |
| 551 |
$case['name'], |
| 552 |
$case['version'], |
| 553 |
( $case['wrap'] && is_string( $case['scripts'] ) ) |
| 554 |
? [ 'plainScripts' => [ [ 'content' => $case['scripts'] ] ] ] |
| 555 |
: $case['scripts'], |
| 556 |
$case['styles'], |
| 557 |
$case['messages'], |
| 558 |
$case['templates'], |
| 559 |
$case['packageFiles'] |
| 560 |
); |
| 561 |
$this->assertEquals( $case['expected'], $minifier->getMinifiedOutput() ); |
| 562 |
} |
| 563 |
|
| 564 |
public function testAddImplementScriptInvalid() { |
| 565 |
$this->expectException( InvalidArgumentException::class ); |
| 566 |
$this->expectExceptionMessage( 'Script must be a' ); |
| 567 |
$minifier = new IdentityMinifierState; |
| 568 |
$rl = TestingAccessWrapper::newFromObject( new EmptyResourceLoader ); |
| 569 |
$rl->addImplementScript( |
| 570 |
$minifier, |
| 571 |
'test', |
| 572 |
'1', |
| 573 |
123, |
| 574 |
null, |
| 575 |
null, |
| 576 |
null, |
| 577 |
null |
| 578 |
); |
| 579 |
} |
| 580 |
|
| 581 |
public function testMakeLoaderRegisterScript() { |
| 582 |
$context = new Context( new EmptyResourceLoader(), new FauxRequest( [ |
| 583 |
'debug' => 'true', |
| 584 |
] ) ); |
| 585 |
$this->assertEquals( |
| 586 |
'mw.loader.register([ |
| 587 |
[ |
| 588 |
"test.name", |
| 589 |
"1234567" |
| 590 |
] |
| 591 |
]);', |
| 592 |
ResourceLoader::makeLoaderRegisterScript( $context, [ |
| 593 |
[ 'test.name', '1234567' ], |
| 594 |
] ), |
| 595 |
'Nested array parameter' |
| 596 |
); |
| 597 |
|
| 598 |
$this->assertEquals( |
| 599 |
'mw.loader.register([ |
| 600 |
[ |
| 601 |
"test.foo", |
| 602 |
"100" |
| 603 |
], |
| 604 |
[ |
| 605 |
"test.bar", |
| 606 |
"200", |
| 607 |
[ |
| 608 |
"test.unknown" |
| 609 |
] |
| 610 |
], |
| 611 |
[ |
| 612 |
"test.baz", |
| 613 |
"300", |
| 614 |
[ |
| 615 |
3, |
| 616 |
0 |
| 617 |
] |
| 618 |
], |
| 619 |
[ |
| 620 |
"test.quux", |
| 621 |
"400", |
| 622 |
[], |
| 623 |
null, |
| 624 |
null, |
| 625 |
"return true;" |
| 626 |
] |
| 627 |
]);', |
| 628 |
ResourceLoader::makeLoaderRegisterScript( $context, [ |
| 629 |
[ 'test.foo', '100', [], null, null ], |
| 630 |
[ 'test.bar', '200', [ 'test.unknown' ], null ], |
| 631 |
[ 'test.baz', '300', [ 'test.quux', 'test.foo' ], null ], |
| 632 |
[ 'test.quux', '400', [], null, null, 'return true;' ], |
| 633 |
] ), |
| 634 |
'Compact dependency indexes' |
| 635 |
); |
| 636 |
} |
| 637 |
|
| 638 |
public function testMakeLoaderSourcesScript() { |
| 639 |
$context = new Context( new EmptyResourceLoader(), new FauxRequest( [ |
| 640 |
'debug' => 'true', |
| 641 |
] ) ); |
| 642 |
$this->assertEquals( |
| 643 |
'mw.loader.addSource({ |
| 644 |
"local": "/w/load.php" |
| 645 |
});', |
| 646 |
ResourceLoader::makeLoaderSourcesScript( $context, [ 'local' => '/w/load.php' ] ) |
| 647 |
); |
| 648 |
$this->assertEquals( |
| 649 |
'mw.loader.addSource({ |
| 650 |
"local": "/w/load.php", |
| 651 |
"example": "https://example.org/w/load.php" |
| 652 |
});', |
| 653 |
ResourceLoader::makeLoaderSourcesScript( $context, [ |
| 654 |
'local' => '/w/load.php', |
| 655 |
'example' => 'https://example.org/w/load.php' |
| 656 |
] ) |
| 657 |
); |
| 658 |
$this->assertEquals( |
| 659 |
'mw.loader.addSource([]);', |
| 660 |
ResourceLoader::makeLoaderSourcesScript( $context, [] ) |
| 661 |
); |
| 662 |
} |
| 663 |
|
| 664 |
private static function fakeSources() { |
| 665 |
return [ |
| 666 |
'examplewiki' => [ |
| 667 |
'loadScript' => '//example.org/w/load.php', |
| 668 |
'apiScript' => '//example.org/w/api.php', |
| 669 |
], |
| 670 |
'example2wiki' => [ |
| 671 |
'loadScript' => '//example.com/w/load.php', |
| 672 |
'apiScript' => '//example.com/w/api.php', |
| 673 |
], |
| 674 |
]; |
| 675 |
} |
| 676 |
|
| 677 |
public function testGetLoadScript() { |
| 678 |
$rl = new EmptyResourceLoader(); |
| 679 |
$sources = self::fakeSources(); |
| 680 |
$rl->addSource( $sources ); |
| 681 |
foreach ( [ 'examplewiki', 'example2wiki' ] as $name ) { |
| 682 |
$this->assertEquals( $rl->getLoadScript( $name ), $sources[$name]['loadScript'] ); |
| 683 |
} |
| 684 |
|
| 685 |
$this->expectException( UnexpectedValueException::class ); |
| 686 |
$rl->getLoadScript( 'thiswasneverregistered' ); |
| 687 |
} |
| 688 |
|
| 689 |
protected function getFailFerryMock( $getter = 'getScript' ) { |
| 690 |
$mock = $this->getMockBuilder( ResourceLoaderTestModule::class ) |
| 691 |
->onlyMethods( [ $getter, 'getName' ] ) |
| 692 |
->getMock(); |
| 693 |
$mock->method( $getter )->willThrowException( |
| 694 |
new Exception( 'Ferry not found' ) |
| 695 |
); |
| 696 |
$mock->method( 'getName' )->willReturn( __METHOD__ ); |
| 697 |
return $mock; |
| 698 |
} |
| 699 |
|
| 700 |
protected function getSimpleModuleMock( $script = '' ) { |
| 701 |
$mock = $this->getMockBuilder( ResourceLoaderTestModule::class ) |
| 702 |
->onlyMethods( [ 'getScript', 'getName' ] ) |
| 703 |
->getMock(); |
| 704 |
$mock->method( 'getScript' )->willReturn( $script ); |
| 705 |
$mock->method( 'getName' )->willReturn( __METHOD__ ); |
| 706 |
return $mock; |
| 707 |
} |
| 708 |
|
| 709 |
protected function getSimpleStyleModuleMock( $styles = '' ) { |
| 710 |
$mock = $this->getMockBuilder( ResourceLoaderTestModule::class ) |
| 711 |
->onlyMethods( [ 'getStyles', 'getName' ] ) |
| 712 |
->getMock(); |
| 713 |
$mock->method( 'getStyles' )->willReturn( [ '' => $styles ] ); |
| 714 |
$mock->method( 'getName' )->willReturn( __METHOD__ ); |
| 715 |
return $mock; |
| 716 |
} |
| 717 |
|
| 718 |
public function testGetCombinedVersion() { |
| 719 |
$rl = $this->getMockBuilder( EmptyResourceLoader::class ) |
| 720 |
|
| 721 |
->onlyMethods( [ 'outputErrorAndLog' ] )->getMock(); |
| 722 |
$rl->register( [ |
| 723 |
'foo' => [ 'class' => ResourceLoaderTestModule::class ], |
| 724 |
'ferry' => [ |
| 725 |
'factory' => function () { |
| 726 |
return $this->getFailFerryMock(); |
| 727 |
} |
| 728 |
], |
| 729 |
'bar' => [ 'class' => ResourceLoaderTestModule::class ], |
| 730 |
] ); |
| 731 |
$context = $this->getResourceLoaderContext( [ 'debug' => 'false' ], $rl ); |
| 732 |
|
| 733 |
$this->assertSame( |
| 734 |
'', |
| 735 |
$rl->getCombinedVersion( $context, [] ), |
| 736 |
'empty list' |
| 737 |
); |
| 738 |
|
| 739 |
$this->assertEquals( |
| 740 |
self::BLANK_COMBI, |
| 741 |
$rl->getCombinedVersion( $context, [ 'foo' ] ), |
| 742 |
'compute foo' |
| 743 |
); |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
$this->assertEquals( |
| 748 |
ResourceLoader::makeHash( self::BLANK_VERSION . self::BLANK_VERSION ), |
| 749 |
$rl->getCombinedVersion( $context, [ 'foo', 'ferry', 'bar' ] ), |
| 750 |
'compute foo+ferry+bar (T152266)' |
| 751 |
); |
| 752 |
} |
| 753 |
|
| 754 |
public static function provideMakeModuleResponseConcat() { |
| 755 |
$testcases = [ |
| 756 |
[ |
| 757 |
'modules' => [ |
| 758 |
'foo' => 'foo()', |
| 759 |
], |
| 760 |
'expected' => "foo()\n" . 'mw.loader.state({ |
| 761 |
"foo": "ready" |
| 762 |
});', |
| 763 |
'minified' => "foo()\n" . 'mw.loader.state({"foo":"ready"});', |
| 764 |
'message' => 'Script without semi-colon', |
| 765 |
], |
| 766 |
[ |
| 767 |
'modules' => [ |
| 768 |
'foo' => 'foo()', |
| 769 |
'bar' => 'bar()', |
| 770 |
], |
| 771 |
'expected' => "foo()\nbar()\n" . 'mw.loader.state({ |
| 772 |
"foo": "ready", |
| 773 |
"bar": "ready" |
| 774 |
});', |
| 775 |
'minified' => "foo()\nbar()\n" . 'mw.loader.state({"foo":"ready","bar":"ready"});', |
| 776 |
'message' => 'Two scripts without semi-colon', |
| 777 |
], |
| 778 |
[ |
| 779 |
'modules' => [ |
| 780 |
'foo' => "foo()\n// bar();" |
| 781 |
], |
| 782 |
'expected' => "foo()\n// bar();\n" . 'mw.loader.state({ |
| 783 |
"foo": "ready" |
| 784 |
});', |
| 785 |
'minified' => "foo()\n" . 'mw.loader.state({"foo":"ready"});', |
| 786 |
'message' => 'Script with semi-colon in comment (T162719)', |
| 787 |
], |
| 788 |
]; |
| 789 |
$ret = []; |
| 790 |
foreach ( $testcases as $i => $case ) { |
| 791 |
$ret["#$i"] = [ |
| 792 |
$case['modules'], |
| 793 |
$case['expected'], |
| 794 |
true, |
| 795 |
$case['message'], |
| 796 |
]; |
| 797 |
$ret["#$i (minified)"] = [ |
| 798 |
$case['modules'], |
| 799 |
$case['minified'], |
| 800 |
false, |
| 801 |
$case['message'], |
| 802 |
]; |
| 803 |
} |
| 804 |
return $ret; |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
public function testMakeModuleResponseConcat( $scripts, $expected, $debug, $message = null ) { |
| 813 |
$rl = new EmptyResourceLoader(); |
| 814 |
$modules = array_map( function ( $script ) { |
| 815 |
return $this->getSimpleModuleMock( $script ); |
| 816 |
}, $scripts ); |
| 817 |
|
| 818 |
$context = $this->getResourceLoaderContext( |
| 819 |
[ |
| 820 |
'modules' => implode( '|', array_keys( $modules ) ), |
| 821 |
'only' => 'scripts', |
| 822 |
'debug' => $debug ? 'true' : 'false', |
| 823 |
], |
| 824 |
$rl |
| 825 |
); |
| 826 |
|
| 827 |
$response = $rl->makeModuleResponse( $context, $modules ); |
| 828 |
$this->assertSame( [], $rl->getErrors(), 'Errors' ); |
| 829 |
$this->assertEquals( $expected, $response, $message ?: 'Response' ); |
| 830 |
} |
| 831 |
|
| 832 |
public function testMakeModuleResponseEmpty() { |
| 833 |
$rl = new EmptyResourceLoader(); |
| 834 |
$context = $this->getResourceLoaderContext( |
| 835 |
[ 'modules' => '', 'only' => 'scripts' ], |
| 836 |
$rl |
| 837 |
); |
| 838 |
|
| 839 |
$response = $rl->makeModuleResponse( $context, [] ); |
| 840 |
$this->assertSame( [], $rl->getErrors(), 'Errors' ); |
| 841 |
$this->assertMatchesRegularExpression( '/^\/\*.+no modules were requested.+\*\/$/ms', $response ); |
| 842 |
} |
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
public function testMakeModuleResponseError() { |
| 850 |
$modules = [ |
| 851 |
'foo' => $this->getSimpleModuleMock( 'foo();' ), |
| 852 |
'ferry' => $this->getFailFerryMock(), |
| 853 |
'bar' => $this->getSimpleModuleMock( 'bar();' ), |
| 854 |
]; |
| 855 |
$rl = new EmptyResourceLoader(); |
| 856 |
$context = $this->getResourceLoaderContext( |
| 857 |
[ |
| 858 |
'modules' => 'foo|ferry|bar', |
| 859 |
'only' => 'scripts', |
| 860 |
], |
| 861 |
$rl |
| 862 |
); |
| 863 |
|
| 864 |
|
| 865 |
$this->setLogger( 'exception', new NullLogger() ); |
| 866 |
|
| 867 |
$response = $rl->makeModuleResponse( $context, $modules ); |
| 868 |
$errors = $rl->getErrors(); |
| 869 |
|
| 870 |
$this->assertCount( 1, $errors ); |
| 871 |
$this->assertMatchesRegularExpression( '/Ferry not found/', $errors[0] ); |
| 872 |
$this->assertEquals( |
| 873 |
"foo();\nbar();\n" . 'mw.loader.state({ |
| 874 |
"ferry": "error", |
| 875 |
"foo": "ready", |
| 876 |
"bar": "ready" |
| 877 |
});', |
| 878 |
$response |
| 879 |
); |
| 880 |
} |
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
public function testMakeModuleResponseErrorCSS() { |
| 887 |
$modules = [ |
| 888 |
'foo' => self::getSimpleStyleModuleMock( '.foo{}' ), |
| 889 |
'ferry' => $this->getFailFerryMock( 'getStyles' ), |
| 890 |
'bar' => self::getSimpleStyleModuleMock( '.bar{}' ), |
| 891 |
]; |
| 892 |
$rl = new EmptyResourceLoader(); |
| 893 |
$context = $this->getResourceLoaderContext( |
| 894 |
[ |
| 895 |
'modules' => 'foo|ferry|bar', |
| 896 |
'only' => 'styles', |
| 897 |
'debug' => 'false', |
| 898 |
], |
| 899 |
$rl |
| 900 |
); |
| 901 |
|
| 902 |
|
| 903 |
$this->setLogger( 'exception', new NullLogger() ); |
| 904 |
|
| 905 |
$response = $rl->makeModuleResponse( $context, $modules ); |
| 906 |
$errors = $rl->getErrors(); |
| 907 |
|
| 908 |
$this->assertCount( 2, $errors ); |
| 909 |
$this->assertMatchesRegularExpression( '/Ferry not found/', $errors[0] ); |
| 910 |
$this->assertMatchesRegularExpression( '/Problem.+"ferry":\s*"error"/ms', $errors[1] ); |
| 911 |
$this->assertEquals( |
| 912 |
'.foo{}.bar{}', |
| 913 |
$response |
| 914 |
); |
| 915 |
} |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
public function testMakeModuleResponseStartupError() { |
| 923 |
|
| 924 |
|
| 925 |
$rl = new EmptyResourceLoader( $this->getServiceContainer()->getMainConfig() ); |
| 926 |
$rl->register( [ |
| 927 |
'foo' => [ 'factory' => function () { |
| 928 |
return $this->getSimpleModuleMock( 'foo();' ); |
| 929 |
} ], |
| 930 |
'ferry' => [ 'factory' => function () { |
| 931 |
return $this->getFailFerryMock(); |
| 932 |
} ], |
| 933 |
'bar' => [ 'factory' => function () { |
| 934 |
return $this->getSimpleModuleMock( 'bar();' ); |
| 935 |
} ], |
| 936 |
] ); |
| 937 |
$context = $this->getResourceLoaderContext( |
| 938 |
[ |
| 939 |
'modules' => 'startup', |
| 940 |
'only' => 'scripts', |
| 941 |
|
| 942 |
'debug' => 'false', |
| 943 |
], |
| 944 |
$rl |
| 945 |
); |
| 946 |
|
| 947 |
$this->assertEquals( |
| 948 |
[ 'startup', 'foo', 'ferry', 'bar' ], |
| 949 |
$rl->getModuleNames(), |
| 950 |
'getModuleNames' |
| 951 |
); |
| 952 |
|
| 953 |
|
| 954 |
$this->setLogger( 'exception', new NullLogger() ); |
| 955 |
|
| 956 |
$modules = [ 'startup' => $rl->getModule( 'startup' ) ]; |
| 957 |
$response = $rl->makeModuleResponse( $context, $modules ); |
| 958 |
$errors = $rl->getErrors(); |
| 959 |
|
| 960 |
$this->assertMatchesRegularExpression( '/Ferry not found/', $errors[0] ?? '' ); |
| 961 |
$this->assertCount( 1, $errors ); |
| 962 |
$this->assertMatchesRegularExpression( |
| 963 |
'/isCompatible.*window\.RLQ/s', |
| 964 |
$response, |
| 965 |
'startup response undisrupted (T152266)' |
| 966 |
); |
| 967 |
$this->assertMatchesRegularExpression( |
| 968 |
'/register\([^)]+"ferry",\s*""/', |
| 969 |
$response, |
| 970 |
'startup response registers broken module' |
| 971 |
); |
| 972 |
$this->assertMatchesRegularExpression( |
| 973 |
'/state\([^)]+"ferry":\s*"error"/', |
| 974 |
$response, |
| 975 |
'startup response sets state to error' |
| 976 |
); |
| 977 |
} |
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
public function testMakeModuleResponseExtraHeaders() { |
| 983 |
$module = $this->getMockBuilder( ResourceLoaderTestModule::class ) |
| 984 |
->onlyMethods( [ 'getPreloadLinks', 'getName' ] )->getMock(); |
| 985 |
$module->method( 'getPreloadLinks' )->willReturn( [ |
| 986 |
'https://example.org/script.js' => [ 'as' => 'script' ], |
| 987 |
] ); |
| 988 |
$module->method( 'getName' )->willReturn( __METHOD__ ); |
| 989 |
|
| 990 |
$rl = new EmptyResourceLoader(); |
| 991 |
$context = $this->getResourceLoaderContext( |
| 992 |
[ 'modules' => 'foo', 'only' => 'scripts' ], |
| 993 |
$rl |
| 994 |
); |
| 995 |
|
| 996 |
$modules = [ 'foo' => $module ]; |
| 997 |
$response = $rl->makeModuleResponse( $context, $modules ); |
| 998 |
$extraHeaders = TestingAccessWrapper::newFromObject( $rl )->extraHeaders; |
| 999 |
|
| 1000 |
$this->assertEquals( |
| 1001 |
[ |
| 1002 |
'Link: <https://example.org/script.js>;rel=preload;as=script' |
| 1003 |
], |
| 1004 |
$extraHeaders, |
| 1005 |
'Extra headers' |
| 1006 |
); |
| 1007 |
} |
| 1008 |
|
| 1009 |
public function testMakeModuleResponseExtraHeadersMulti() { |
| 1010 |
$foo = $this->getMockBuilder( ResourceLoaderTestModule::class ) |
| 1011 |
->onlyMethods( [ 'getPreloadLinks', 'getName' ] )->getMock(); |
| 1012 |
$foo->method( 'getPreloadLinks' )->willReturn( [ |
| 1013 |
'https://example.org/script.js' => [ 'as' => 'script' ], |
| 1014 |
] ); |
| 1015 |
$foo->method( 'getName' )->willReturn( __METHOD__ ); |
| 1016 |
|
| 1017 |
$bar = $this->getMockBuilder( ResourceLoaderTestModule::class ) |
| 1018 |
->onlyMethods( [ 'getPreloadLinks', 'getName' ] )->getMock(); |
| 1019 |
$bar->method( 'getPreloadLinks' )->willReturn( [ |
| 1020 |
'/example.png' => [ 'as' => 'image' ], |
| 1021 |
'/example.jpg' => [ 'as' => 'image' ], |
| 1022 |
] ); |
| 1023 |
$bar->method( 'getName' )->willReturn( __METHOD__ ); |
| 1024 |
|
| 1025 |
$rl = new EmptyResourceLoader(); |
| 1026 |
$context = $this->getResourceLoaderContext( |
| 1027 |
[ 'modules' => 'foo|bar', 'only' => 'scripts' ], |
| 1028 |
$rl |
| 1029 |
); |
| 1030 |
|
| 1031 |
$modules = [ 'foo' => $foo, 'bar' => $bar ]; |
| 1032 |
$response = $rl->makeModuleResponse( $context, $modules ); |
| 1033 |
$extraHeaders = TestingAccessWrapper::newFromObject( $rl )->extraHeaders; |
| 1034 |
$this->assertEquals( |
| 1035 |
[ |
| 1036 |
'Link: <https://example.org/script.js>;rel=preload;as=script', |
| 1037 |
'Link: </example.png>;rel=preload;as=image,</example.jpg>;rel=preload;as=image' |
| 1038 |
], |
| 1039 |
$extraHeaders, |
| 1040 |
'Extra headers' |
| 1041 |
); |
| 1042 |
} |
| 1043 |
|
| 1044 |
public function testRespondEmpty() { |
| 1045 |
$rl = $this->getMockBuilder( EmptyResourceLoader::class ) |
| 1046 |
->onlyMethods( [ |
| 1047 |
'tryRespondNotModified', |
| 1048 |
'sendResponseHeaders', |
| 1049 |
'measureResponseTime', |
| 1050 |
] ) |
| 1051 |
->getMock(); |
| 1052 |
$context = $this->getResourceLoaderContext( [ 'modules' => '' ], $rl ); |
| 1053 |
|
| 1054 |
$rl->expects( $this->once() )->method( 'measureResponseTime' ); |
| 1055 |
$this->expectOutputRegex( '/no modules were requested/' ); |
| 1056 |
|
| 1057 |
$rl->respond( $context ); |
| 1058 |
} |
| 1059 |
|
| 1060 |
public function testRespondSimple() { |
| 1061 |
$module = new ResourceLoaderTestModule( [ 'script' => 'foo();' ] ); |
| 1062 |
$rl = $this->getMockBuilder( EmptyResourceLoader::class ) |
| 1063 |
->onlyMethods( [ |
| 1064 |
'measureResponseTime', |
| 1065 |
'tryRespondNotModified', |
| 1066 |
'sendResponseHeaders', |
| 1067 |
'makeModuleResponse', |
| 1068 |
] ) |
| 1069 |
->getMock(); |
| 1070 |
$rl->register( 'test', [ |
| 1071 |
'factory' => static function () use ( $module ) { |
| 1072 |
return $module; |
| 1073 |
} |
| 1074 |
] ); |
| 1075 |
$context = $this->getResourceLoaderContext( |
| 1076 |
[ 'modules' => 'test', 'only' => null ], |
| 1077 |
$rl |
| 1078 |
); |
| 1079 |
|
| 1080 |
$rl->expects( $this->once() )->method( 'makeModuleResponse' ) |
| 1081 |
->with( $context, [ 'test' => $module ] ) |
| 1082 |
->willReturn( 'implement_foo;' ); |
| 1083 |
$this->expectOutputRegex( '/^implement_foo;/' ); |
| 1084 |
|
| 1085 |
$rl->respond( $context ); |
| 1086 |
} |
| 1087 |
|
| 1088 |
public function testRespondMissingModule() { |
| 1089 |
$rl = $this->getMockBuilder( EmptyResourceLoader::class ) |
| 1090 |
->onlyMethods( [ |
| 1091 |
'measureResponseTime', |
| 1092 |
'tryRespondNotModified', |
| 1093 |
'sendResponseHeaders', |
| 1094 |
] ) |
| 1095 |
->getMock(); |
| 1096 |
$context = $this->getResourceLoaderContext( |
| 1097 |
[ 'modules' => 'unknown', 'only' => null ], |
| 1098 |
$rl |
| 1099 |
); |
| 1100 |
|
| 1101 |
$this->expectOutputRegex( '/mw\.loader\.state.*"unknown": "missing"/s' ); |
| 1102 |
|
| 1103 |
$rl->respond( $context ); |
| 1104 |
} |
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
public function testRespondInvalidMissingModule() { |
| 1112 |
$rl = $this->getMockBuilder( EmptyResourceLoader::class ) |
| 1113 |
->onlyMethods( [ |
| 1114 |
'measureResponseTime', |
| 1115 |
'tryRespondNotModified', |
| 1116 |
'sendResponseHeaders', |
| 1117 |
] ) |
| 1118 |
->getMock(); |
| 1119 |
|
| 1120 |
|
| 1121 |
$context = $this->getResourceLoaderContext( |
| 1122 |
[ 'modules' => "foo|bar\x80\xf0bara|quux", 'only' => null ], |
| 1123 |
$rl |
| 1124 |
); |
| 1125 |
$this->expectOutputRegex( '/mw\.loader\.state.*"foo": "missing"/s' ); |
| 1126 |
$rl->respond( $context ); |
| 1127 |
|
| 1128 |
|
| 1129 |
$context = $this->getResourceLoaderContext( |
| 1130 |
[ 'modules' => "foo|bar\x80\xf0bara|quux", 'only' => 'styles' ], |
| 1131 |
$rl |
| 1132 |
); |
| 1133 |
$this->expectOutputRegex( '/Problematic modules.*"foo": "missing"/s' ); |
| 1134 |
$rl->respond( $context ); |
| 1135 |
} |
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
public function testRespondErrorPrivate() { |
| 1141 |
$rl = $this->getMockBuilder( EmptyResourceLoader::class ) |
| 1142 |
->onlyMethods( [ |
| 1143 |
'measureResponseTime', |
| 1144 |
'tryRespondNotModified', |
| 1145 |
'sendResponseHeaders', |
| 1146 |
] ) |
| 1147 |
->getMock(); |
| 1148 |
$rl->register( [ |
| 1149 |
'foo' => [ 'class' => ResourceLoaderTestModule::class ], |
| 1150 |
'bar' => [ 'class' => ResourceLoaderTestModule::class, 'group' => 'private' ], |
| 1151 |
] ); |
| 1152 |
$context = $this->getResourceLoaderContext( |
| 1153 |
[ 'modules' => 'foo|bar', 'only' => null ], |
| 1154 |
$rl |
| 1155 |
); |
| 1156 |
|
| 1157 |
$this->expectOutputRegex( '/\/\*.+Cannot build private module/s' ); |
| 1158 |
$rl->respond( $context ); |
| 1159 |
} |
| 1160 |
|
| 1161 |
public function testRespondInternalFailures() { |
| 1162 |
$module = $this->getMockBuilder( ResourceLoaderTestModule::class ) |
| 1163 |
->onlyMethods( [ 'getDefinitionSummary', 'enableModuleContentVersion' ] ) |
| 1164 |
->getMock(); |
| 1165 |
$module->method( 'enableModuleContentVersion' ) |
| 1166 |
->willReturn( false ); |
| 1167 |
$module->method( 'getDefinitionSummary' ) |
| 1168 |
->willThrowException( new Exception( 'Version error' ) ); |
| 1169 |
$rl = $this->getMockBuilder( EmptyResourceLoader::class ) |
| 1170 |
->onlyMethods( [ |
| 1171 |
'measureResponseTime', |
| 1172 |
'preloadModuleInfo', |
| 1173 |
'tryRespondNotModified', |
| 1174 |
'makeModuleResponse', |
| 1175 |
'sendResponseHeaders', |
| 1176 |
] ) |
| 1177 |
->getMock(); |
| 1178 |
$rl->register( 'test', [ |
| 1179 |
'factory' => static function () use ( $module ) { |
| 1180 |
return $module; |
| 1181 |
} |
| 1182 |
] ); |
| 1183 |
$context = $this->getResourceLoaderContext( |
| 1184 |
[ 'modules' => 'test', 'debug' => 'false' ], |
| 1185 |
$rl |
| 1186 |
); |
| 1187 |
|
| 1188 |
$this->setLogger( 'exception', new NullLogger() ); |
| 1189 |
|
| 1190 |
$rl->expects( $this->once() )->method( 'preloadModuleInfo' ) |
| 1191 |
->willThrowException( new Exception( 'Preload error' ) ); |
| 1192 |
$rl->expects( $this->once() )->method( 'makeModuleResponse' ) |
| 1193 |
->with( $context, [ 'test' => $module ] ) |
| 1194 |
->willReturn( 'foo;' ); |
| 1195 |
|
| 1196 |
$this->expectOutputRegex( '/foo;.*\/\*.+Preload error.+Version error.+\*\//ms' ); |
| 1197 |
|
| 1198 |
$rl->respond( $context ); |
| 1199 |
} |
| 1200 |
|
| 1201 |
private function getResourceLoaderWithTestModules( ?Config $config = null ) { |
| 1202 |
$localBasePath = __DIR__ . '/../../data/resourceloader'; |
| 1203 |
$remoteBasePath = '/w'; |
| 1204 |
$rl = new EmptyResourceLoader( $config ); |
| 1205 |
$rl->register( 'test1', [ |
| 1206 |
'localBasePath' => $localBasePath, |
| 1207 |
'remoteBasePath' => $remoteBasePath, |
| 1208 |
'scripts' => [ 'script-nosemi.js', 'script-comment.js' ], |
| 1209 |
] ); |
| 1210 |
$rl->register( 'test2', [ |
| 1211 |
'localBasePath' => $localBasePath, |
| 1212 |
'remoteBasePath' => $remoteBasePath, |
| 1213 |
'scripts' => [ 'script-nosemi-nonl.js' ], |
| 1214 |
] ); |
| 1215 |
return $rl; |
| 1216 |
} |
| 1217 |
|
| 1218 |
public function testRespondSourceMap() { |
| 1219 |
$rl = $this->getResourceLoaderWithTestModules(); |
| 1220 |
$context = $this->getResourceLoaderContext( |
| 1221 |
[ 'modules' => 'test1', 'sourcemap' => '1', 'debug' => '' ], |
| 1222 |
$rl |
| 1223 |
); |
| 1224 |
$this->expectOutputString( <<<JSON |
| 1225 |
{ |
| 1226 |
"version": 3, |
| 1227 |
"file": "/load.php?lang=en&modules=test1&only=scripts", |
| 1228 |
"sources": ["/w/script-nosemi.js","/w/script-comment.js"], |
| 1229 |
"sourcesContent": ["/* eslint-disable */\\nmw.foo()\\n","/* eslint-disable */\\nmw.foo()\\n// mw.bar();\\n"], |
| 1230 |
"names": [], |
| 1231 |
"mappings": "AACA,EAAE,CAAC,GAAG,CAAC;ACAP,EAAE,CAAC,GAAG,CAAC" |
| 1232 |
} |
| 1233 |
|
| 1234 |
JSON |
| 1235 |
); |
| 1236 |
$rl->respond( $context ); |
| 1237 |
} |
| 1238 |
|
| 1239 |
public function testRespondIndexMap() { |
| 1240 |
$rl = $this->getResourceLoaderWithTestModules(); |
| 1241 |
$context = $this->getResourceLoaderContext( |
| 1242 |
[ 'modules' => 'test1|test2', 'sourcemap' => '1', 'debug' => '' ], |
| 1243 |
$rl |
| 1244 |
); |
| 1245 |
$this->expectOutputString( <<<JSON |
| 1246 |
{ |
| 1247 |
"version": 3, |
| 1248 |
"sections": [ |
| 1249 |
{"offset":{"line":0,"column":0},"map":{ |
| 1250 |
"version": 3, |
| 1251 |
"file": "/load.php?lang=en&modules=test1%2Ctest2&only=scripts", |
| 1252 |
"sources": ["/w/script-nosemi.js","/w/script-comment.js"], |
| 1253 |
"sourcesContent": ["/* eslint-disable */\\nmw.foo()\\n","/* eslint-disable */\\nmw.foo()\\n// mw.bar();\\n"], |
| 1254 |
"names": [], |
| 1255 |
"mappings": "AACA,EAAE,CAAC,GAAG,CAAC;ACAP,EAAE,CAAC,GAAG,CAAC" |
| 1256 |
} |
| 1257 |
}, |
| 1258 |
{"offset":{"line":2,"column":0},"map":{ |
| 1259 |
"version": 3, |
| 1260 |
"file": "/load.php?lang=en&modules=test1%2Ctest2&only=scripts", |
| 1261 |
"sources": ["/w/script-nosemi-nonl.js"], |
| 1262 |
"sourcesContent": ["/* eslint-disable */\\nmw.foo()"], |
| 1263 |
"names": [], |
| 1264 |
"mappings": "AACA,EAAE,CAAC,GAAG,CAAC" |
| 1265 |
} |
| 1266 |
} |
| 1267 |
] |
| 1268 |
} |
| 1269 |
JSON |
| 1270 |
); |
| 1271 |
$rl->respond( $context ); |
| 1272 |
} |
| 1273 |
|
| 1274 |
public function testRespondSourceMapLink() { |
| 1275 |
$rl = $this->getResourceLoaderWithTestModules( new HashConfig( |
| 1276 |
[ |
| 1277 |
MainConfigNames::ResourceLoaderEnableSourceMapLinks => true, |
| 1278 |
] |
| 1279 |
) ); |
| 1280 |
$context = $this->getResourceLoaderContext( |
| 1281 |
[ 'modules' => 'test1|test2', 'debug' => '' ], |
| 1282 |
$rl |
| 1283 |
); |
| 1284 |
$this->expectOutputString( <<<JS |
| 1285 |
mw.foo() |
| 1286 |
mw.foo() |
| 1287 |
mw.foo() |
| 1288 |
mw.loader.state({"test1":"ready","test2":"ready"}); |
| 1289 |
JS |
| 1290 |
); |
| 1291 |
$rl->respond( $context ); |
| 1292 |
|
| 1293 |
$extraHeaders = TestingAccessWrapper::newFromObject( $rl )->extraHeaders; |
| 1294 |
$this->assertEquals( |
| 1295 |
[ |
| 1296 |
'SourceMap: /load.php?lang=en&modules=test1%2Ctest2&only=scripts&sourcemap=1&version=pq39u' |
| 1297 |
], |
| 1298 |
$extraHeaders, |
| 1299 |
'Extra headers' |
| 1300 |
); |
| 1301 |
} |
| 1302 |
|
| 1303 |
public function testMeasureResponseTime() { |
| 1304 |
$stats = $this->getMockBuilder( NullStatsdDataFactory::class ) |
| 1305 |
->onlyMethods( [ 'timing' ] )->getMock(); |
| 1306 |
$this->setService( 'StatsdDataFactory', $stats ); |
| 1307 |
|
| 1308 |
$stats->expects( $this->once() )->method( 'timing' ) |
| 1309 |
->with( 'resourceloader.responseTime', $this->anything() ); |
| 1310 |
|
| 1311 |
$rl = TestingAccessWrapper::newFromObject( new EmptyResourceLoader ); |
| 1312 |
$rl->measureResponseTime(); |
| 1313 |
} |
| 1314 |
|
| 1315 |
public function testGetUserDefaults() { |
| 1316 |
$this->setService( 'UserOptionsLookup', new StaticUserOptionsLookup( |
| 1317 |
[], |
| 1318 |
[ |
| 1319 |
'include' => 1, |
| 1320 |
'exclude' => 1, |
| 1321 |
] |
| 1322 |
) ); |
| 1323 |
$ctx = $this->createStub( Context::class ); |
| 1324 |
$this->setTemporaryHook( 'ResourceLoaderExcludeUserOptions', function ( |
| 1325 |
array &$keysToExclude, |
| 1326 |
Context $context |
| 1327 |
) use ( $ctx ): void { |
| 1328 |
$this->assertSame( $ctx, $context ); |
| 1329 |
$keysToExclude[] = 'exclude'; |
| 1330 |
}, true ); |
| 1331 |
|
| 1332 |
$defaults = ResourceLoader::getUserDefaults( |
| 1333 |
$ctx, |
| 1334 |
$this->getServiceContainer()->getHookContainer(), |
| 1335 |
$this->getServiceContainer()->getUserOptionsLookup() |
| 1336 |
); |
| 1337 |
$this->assertSame( [ 'include' => 1 ], $defaults ); |
| 1338 |
} |
| 1339 |
} |