2010-12-14 16:26:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
2019-04-17 14:17:15 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2020-01-10 00:00:51 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
2017-04-19 19:37:35 +00:00
|
|
|
|
2014-03-07 17:18:31 +00:00
|
|
|
class ResourceLoaderTest extends ResourceLoaderTestCase {
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2021-07-22 03:11:47 +00:00
|
|
|
protected function setUp(): void {
|
Support LESS stylesheets in ResourceLoader
This patch adds support for the LESS stylesheet language to ResourceLoader.
LESS is a stylesheet language that compiles into CSS. The patch includes
lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is
explained in a MediaWiki RFC which accompanies this patch, available at
<https://www.mediawiki.org/wiki/Requests_for_comment/LESS>.
LESS support is provided for ResourceLoader file modules. It is triggered by
the presence of the '.less' extension in stylesheet filenames. LESS files are
compiled by lessc, and the resultant CSS is subjected to the standard set of
transformations (CSSJanus & CSSMin). The immediate result of LESS compilation
is encoded as an array, which includes the list of LESS files that were
compiled and their mtimes. This array is cached. Cache invalidation is
performed by comparing the cached mtimes with the mtimes of the files on disk.
If the compiler itself throws an exception, ResourceLoader constructs a
compilation result which consists of the error message encoded as a CSS
comment. Failed compilation results are cached too, but with an expiration time
of five minutes. The expiration time is required because the full list of
referenced files is not known.
Three configuration variables configure the global environment for LESS
modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and
$wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names
to CSS values, specified as strings. Variables declared in this array are
available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except
it maps custom function names to PHP callables. These functions can be called
from within LESS to transform values. Read more about custom functions at
<http://leafo.net/lessphp/docs/#custom_functions>. Finally,
$wgResourceLoaderLESSImportPaths specifies file system paths in addition to the
current module's path where the LESS compiler should look up files referenced
in @import statements.
The issue of handling of /* @embed */ and /* @noflip */ annotations is left
unresolved. Earlier versions of this patch included an @embed analog
implemented as a LESS custom function, but there was enough ambiguity about
whether the strategy it took was optimal to merit discussing it in a separate,
follow-up patch.
Bug: 40964
Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( [
|
2018-05-20 18:32:57 +00:00
|
|
|
'wgSkinLessVariablesImportPaths' => [],
|
2017-02-22 02:51:45 +00:00
|
|
|
'wgShowExceptionDetails' => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
Support LESS stylesheets in ResourceLoader
This patch adds support for the LESS stylesheet language to ResourceLoader.
LESS is a stylesheet language that compiles into CSS. The patch includes
lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is
explained in a MediaWiki RFC which accompanies this patch, available at
<https://www.mediawiki.org/wiki/Requests_for_comment/LESS>.
LESS support is provided for ResourceLoader file modules. It is triggered by
the presence of the '.less' extension in stylesheet filenames. LESS files are
compiled by lessc, and the resultant CSS is subjected to the standard set of
transformations (CSSJanus & CSSMin). The immediate result of LESS compilation
is encoded as an array, which includes the list of LESS files that were
compiled and their mtimes. This array is cached. Cache invalidation is
performed by comparing the cached mtimes with the mtimes of the files on disk.
If the compiler itself throws an exception, ResourceLoader constructs a
compilation result which consists of the error message encoded as a CSS
comment. Failed compilation results are cached too, but with an expiration time
of five minutes. The expiration time is required because the full list of
referenced files is not known.
Three configuration variables configure the global environment for LESS
modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and
$wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names
to CSS values, specified as strings. Variables declared in this array are
available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except
it maps custom function names to PHP callables. These functions can be called
from within LESS to transform values. Read more about custom functions at
<http://leafo.net/lessphp/docs/#custom_functions>. Finally,
$wgResourceLoaderLESSImportPaths specifies file system paths in addition to the
current module's path where the LESS compiler should look up files referenced
in @import statements.
The issue of handling of /* @embed */ and /* @noflip */ annotations is left
unresolved. Earlier versions of this patch included an @embed analog
implemented as a LESS custom function, but there was enough ambiguity about
whether the strategy it took was optimal to merit discussing it in a separate,
follow-up patch.
Bug: 40964
Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
|
|
|
}
|
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
/**
|
2016-08-25 01:50:30 +00:00
|
|
|
* Ensure the ResourceLoaderRegisterModules hook is called.
|
2019-04-17 14:17:15 +00:00
|
|
|
* @coversNothing
|
2010-12-14 16:26:35 +00:00
|
|
|
*/
|
2019-04-17 14:17:15 +00:00
|
|
|
public function testServiceWiring() {
|
|
|
|
|
$ranHook = 0;
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( 'wgHooks', [
|
|
|
|
|
'ResourceLoaderRegisterModules' => [
|
2021-02-07 13:10:36 +00:00
|
|
|
static function ( &$resourceLoader ) use ( &$ranHook ) {
|
2019-04-17 14:17:15 +00:00
|
|
|
$ranHook++;
|
2014-10-15 15:05:04 +00:00
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
]
|
|
|
|
|
] );
|
2014-10-15 15:05:04 +00:00
|
|
|
|
2019-04-17 14:17:15 +00:00
|
|
|
MediaWikiServices::getInstance()->getResourceLoader();
|
|
|
|
|
|
|
|
|
|
$this->assertSame( 1, $ranHook, 'Hook was called' );
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
2019-06-01 22:30:46 +00:00
|
|
|
public static function provideInvalidModuleName() {
|
|
|
|
|
return [
|
|
|
|
|
'name with 300 chars' => [ str_repeat( 'x', 300 ) ],
|
|
|
|
|
'name with bang' => [ 'this!that' ],
|
|
|
|
|
'name with comma' => [ 'this,that' ],
|
|
|
|
|
'name with pipe' => [ 'this|that' ],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideValidModuleName() {
|
|
|
|
|
return [
|
|
|
|
|
'empty string' => [ '' ],
|
|
|
|
|
'simple name' => [ 'this.and-that2' ],
|
|
|
|
|
'name with 100 chars' => [ str_repeat( 'x', 100 ) ],
|
|
|
|
|
'name with hash' => [ 'this#that' ],
|
|
|
|
|
'name with slash' => [ 'this/that' ],
|
|
|
|
|
'name with at' => [ 'this@that' ],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideInvalidModuleName
|
|
|
|
|
* @covers ResourceLoader
|
|
|
|
|
*/
|
|
|
|
|
public function testIsValidModuleName_invalid( $name ) {
|
|
|
|
|
$this->assertFalse( ResourceLoader::isValidModuleName( $name ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideValidModuleName
|
|
|
|
|
* @covers ResourceLoader
|
|
|
|
|
*/
|
|
|
|
|
public function testIsValidModuleName_valid( $name ) {
|
|
|
|
|
$this->assertTrue( ResourceLoader::isValidModuleName( $name ) );
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-06 01:19:48 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::register
|
|
|
|
|
* @covers ResourceLoader::getModule
|
|
|
|
|
*/
|
|
|
|
|
public function testRegisterValidArray() {
|
|
|
|
|
$resourceLoader = new EmptyResourceLoader();
|
|
|
|
|
// Covers case of register() setting $rl->moduleInfos,
|
|
|
|
|
// but $rl->modules lazy-populated by getModule()
|
2019-07-11 19:48:57 +00:00
|
|
|
$resourceLoader->register( 'test', [ 'class' => ResourceLoaderTestModule::class ] );
|
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
|
ResourceLoaderTestModule::class,
|
|
|
|
|
$resourceLoader->getModule( 'test' )
|
|
|
|
|
);
|
2017-04-06 01:19:48 +00:00
|
|
|
}
|
|
|
|
|
|
2016-11-17 01:15:04 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::register
|
2019-06-01 22:30:46 +00:00
|
|
|
* @group medium
|
2016-11-17 01:15:04 +00:00
|
|
|
*/
|
|
|
|
|
public function testRegisterEmptyString() {
|
|
|
|
|
$resourceLoader = new EmptyResourceLoader();
|
2019-07-11 19:48:57 +00:00
|
|
|
$resourceLoader->register( '', [ 'class' => ResourceLoaderTestModule::class ] );
|
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
|
ResourceLoaderTestModule::class,
|
|
|
|
|
$resourceLoader->getModule( '' )
|
|
|
|
|
);
|
2016-11-17 01:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
Support LESS stylesheets in ResourceLoader
This patch adds support for the LESS stylesheet language to ResourceLoader.
LESS is a stylesheet language that compiles into CSS. The patch includes
lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is
explained in a MediaWiki RFC which accompanies this patch, available at
<https://www.mediawiki.org/wiki/Requests_for_comment/LESS>.
LESS support is provided for ResourceLoader file modules. It is triggered by
the presence of the '.less' extension in stylesheet filenames. LESS files are
compiled by lessc, and the resultant CSS is subjected to the standard set of
transformations (CSSJanus & CSSMin). The immediate result of LESS compilation
is encoded as an array, which includes the list of LESS files that were
compiled and their mtimes. This array is cached. Cache invalidation is
performed by comparing the cached mtimes with the mtimes of the files on disk.
If the compiler itself throws an exception, ResourceLoader constructs a
compilation result which consists of the error message encoded as a CSS
comment. Failed compilation results are cached too, but with an expiration time
of five minutes. The expiration time is required because the full list of
referenced files is not known.
Three configuration variables configure the global environment for LESS
modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and
$wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names
to CSS values, specified as strings. Variables declared in this array are
available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except
it maps custom function names to PHP callables. These functions can be called
from within LESS to transform values. Read more about custom functions at
<http://leafo.net/lessphp/docs/#custom_functions>. Finally,
$wgResourceLoaderLESSImportPaths specifies file system paths in addition to the
current module's path where the LESS compiler should look up files referenced
in @import statements.
The issue of handling of /* @embed */ and /* @noflip */ annotations is left
unresolved. Earlier versions of this patch included an @embed analog
implemented as a LESS custom function, but there was enough ambiguity about
whether the strategy it took was optimal to merit discussing it in a separate,
follow-up patch.
Bug: 40964
Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
|
|
|
/**
|
2016-08-25 01:50:30 +00:00
|
|
|
* @covers ResourceLoader::register
|
2019-06-01 22:30:46 +00:00
|
|
|
* @group medium
|
Support LESS stylesheets in ResourceLoader
This patch adds support for the LESS stylesheet language to ResourceLoader.
LESS is a stylesheet language that compiles into CSS. The patch includes
lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is
explained in a MediaWiki RFC which accompanies this patch, available at
<https://www.mediawiki.org/wiki/Requests_for_comment/LESS>.
LESS support is provided for ResourceLoader file modules. It is triggered by
the presence of the '.less' extension in stylesheet filenames. LESS files are
compiled by lessc, and the resultant CSS is subjected to the standard set of
transformations (CSSJanus & CSSMin). The immediate result of LESS compilation
is encoded as an array, which includes the list of LESS files that were
compiled and their mtimes. This array is cached. Cache invalidation is
performed by comparing the cached mtimes with the mtimes of the files on disk.
If the compiler itself throws an exception, ResourceLoader constructs a
compilation result which consists of the error message encoded as a CSS
comment. Failed compilation results are cached too, but with an expiration time
of five minutes. The expiration time is required because the full list of
referenced files is not known.
Three configuration variables configure the global environment for LESS
modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and
$wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names
to CSS values, specified as strings. Variables declared in this array are
available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except
it maps custom function names to PHP callables. These functions can be called
from within LESS to transform values. Read more about custom functions at
<http://leafo.net/lessphp/docs/#custom_functions>. Finally,
$wgResourceLoaderLESSImportPaths specifies file system paths in addition to the
current module's path where the LESS compiler should look up files referenced
in @import statements.
The issue of handling of /* @embed */ and /* @noflip */ annotations is left
unresolved. Earlier versions of this patch included an @embed analog
implemented as a LESS custom function, but there was enough ambiguity about
whether the strategy it took was optimal to merit discussing it in a separate,
follow-up patch.
Bug: 40964
Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
|
|
|
*/
|
2016-08-25 01:50:30 +00:00
|
|
|
public function testRegisterInvalidName() {
|
|
|
|
|
$resourceLoader = new EmptyResourceLoader();
|
2020-02-21 00:26:07 +00:00
|
|
|
$this->expectException( InvalidArgumentException::class );
|
2019-10-05 15:42:53 +00:00
|
|
|
$this->expectExceptionMessage( "name 'test!invalid' is invalid" );
|
2019-07-11 19:48:57 +00:00
|
|
|
$resourceLoader->register( 'test!invalid', [] );
|
Support LESS stylesheets in ResourceLoader
This patch adds support for the LESS stylesheet language to ResourceLoader.
LESS is a stylesheet language that compiles into CSS. The patch includes
lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is
explained in a MediaWiki RFC which accompanies this patch, available at
<https://www.mediawiki.org/wiki/Requests_for_comment/LESS>.
LESS support is provided for ResourceLoader file modules. It is triggered by
the presence of the '.less' extension in stylesheet filenames. LESS files are
compiled by lessc, and the resultant CSS is subjected to the standard set of
transformations (CSSJanus & CSSMin). The immediate result of LESS compilation
is encoded as an array, which includes the list of LESS files that were
compiled and their mtimes. This array is cached. Cache invalidation is
performed by comparing the cached mtimes with the mtimes of the files on disk.
If the compiler itself throws an exception, ResourceLoader constructs a
compilation result which consists of the error message encoded as a CSS
comment. Failed compilation results are cached too, but with an expiration time
of five minutes. The expiration time is required because the full list of
referenced files is not known.
Three configuration variables configure the global environment for LESS
modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and
$wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names
to CSS values, specified as strings. Variables declared in this array are
available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except
it maps custom function names to PHP callables. These functions can be called
from within LESS to transform values. Read more about custom functions at
<http://leafo.net/lessphp/docs/#custom_functions>. Finally,
$wgResourceLoaderLESSImportPaths specifies file system paths in addition to the
current module's path where the LESS compiler should look up files referenced
in @import statements.
The issue of handling of /* @embed */ and /* @noflip */ annotations is left
unresolved. Earlier versions of this patch included an @embed analog
implemented as a LESS custom function, but there was enough ambiguity about
whether the strategy it took was optimal to merit discussing it in a separate,
follow-up patch.
Bug: 40964
Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
|
|
|
}
|
|
|
|
|
|
2014-09-20 21:26:13 +00:00
|
|
|
/**
|
2016-08-25 01:50:30 +00:00
|
|
|
* @covers ResourceLoader::register
|
2014-09-20 21:26:13 +00:00
|
|
|
*/
|
2016-08-25 01:50:30 +00:00
|
|
|
public function testRegisterInvalidType() {
|
|
|
|
|
$resourceLoader = new EmptyResourceLoader();
|
2019-10-05 15:42:53 +00:00
|
|
|
$this->expectException( InvalidArgumentException::class );
|
|
|
|
|
$this->expectExceptionMessage( 'Invalid module info' );
|
2020-02-28 15:13:53 +00:00
|
|
|
$resourceLoader->register( [ 'test' => (object)[] ] );
|
2014-09-20 21:26:13 +00:00
|
|
|
}
|
|
|
|
|
|
2018-05-04 01:15:24 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::register
|
|
|
|
|
*/
|
|
|
|
|
public function testRegisterDuplicate() {
|
|
|
|
|
$logger = $this->getMockBuilder( Psr\Log\LoggerInterface::class )->getMock();
|
|
|
|
|
$logger->expects( $this->once() )
|
|
|
|
|
->method( 'warning' );
|
|
|
|
|
$resourceLoader = new EmptyResourceLoader( null, $logger );
|
|
|
|
|
|
2019-07-11 19:48:57 +00:00
|
|
|
$resourceLoader->register( 'test', [ 'class' => ResourceLoaderSkinModule::class ] );
|
|
|
|
|
$resourceLoader->register( 'test', [ 'class' => ResourceLoaderStartUpModule::class ] );
|
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
|
ResourceLoaderStartUpModule::class,
|
|
|
|
|
$resourceLoader->getModule( 'test' ),
|
|
|
|
|
'last one wins'
|
|
|
|
|
);
|
2018-05-04 01:15:24 +00:00
|
|
|
}
|
|
|
|
|
|
2011-06-07 17:56:40 +00:00
|
|
|
/**
|
2016-08-25 01:50:30 +00:00
|
|
|
* @covers ResourceLoader::getModuleNames
|
2011-06-07 17:56:40 +00:00
|
|
|
*/
|
2016-08-25 01:50:30 +00:00
|
|
|
public function testGetModuleNames() {
|
|
|
|
|
// Use an empty one so that core and extension modules don't get in.
|
|
|
|
|
$resourceLoader = new EmptyResourceLoader();
|
2019-07-11 19:48:57 +00:00
|
|
|
$resourceLoader->register( 'test.foo', [] );
|
|
|
|
|
$resourceLoader->register( 'test.bar', [] );
|
2016-08-25 01:50:30 +00:00
|
|
|
$this->assertEquals(
|
2019-06-10 15:00:16 +00:00
|
|
|
[ 'startup', 'test.foo', 'test.bar' ],
|
2016-08-25 01:50:30 +00:00
|
|
|
$resourceLoader->getModuleNames()
|
|
|
|
|
);
|
2011-06-07 17:56:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-08-25 01:50:30 +00:00
|
|
|
* @covers ResourceLoader::isModuleRegistered
|
2011-06-07 17:56:40 +00:00
|
|
|
*/
|
2016-08-25 01:50:30 +00:00
|
|
|
public function testIsModuleRegistered() {
|
|
|
|
|
$rl = new EmptyResourceLoader();
|
2019-07-11 19:48:57 +00:00
|
|
|
$rl->register( 'test', [] );
|
2016-08-25 01:50:30 +00:00
|
|
|
$this->assertTrue( $rl->isModuleRegistered( 'test' ) );
|
|
|
|
|
$this->assertFalse( $rl->isModuleRegistered( 'test.unknown' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::getModule
|
|
|
|
|
*/
|
|
|
|
|
public function testGetModuleUnknown() {
|
|
|
|
|
$rl = new EmptyResourceLoader();
|
|
|
|
|
$this->assertSame( null, $rl->getModule( 'test' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::getModule
|
|
|
|
|
*/
|
|
|
|
|
public function testGetModuleClass() {
|
|
|
|
|
$rl = new EmptyResourceLoader();
|
|
|
|
|
$rl->register( 'test', [ 'class' => ResourceLoaderTestModule::class ] );
|
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
|
ResourceLoaderTestModule::class,
|
|
|
|
|
$rl->getModule( 'test' )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-04 16:10:28 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::getModule
|
|
|
|
|
*/
|
|
|
|
|
public function testGetModuleFactory() {
|
2017-06-26 16:35:31 +00:00
|
|
|
$factory = function ( array $info ) {
|
2017-05-04 16:10:28 +00:00
|
|
|
$this->assertArrayHasKey( 'kitten', $info );
|
|
|
|
|
return new ResourceLoaderTestModule( $info );
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$rl = new EmptyResourceLoader();
|
|
|
|
|
$rl->register( 'test', [ 'factory' => $factory, 'kitten' => 'little ball of fur' ] );
|
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
|
ResourceLoaderTestModule::class,
|
|
|
|
|
$rl->getModule( 'test' )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-25 01:50:30 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::getModule
|
|
|
|
|
*/
|
|
|
|
|
public function testGetModuleClassDefault() {
|
|
|
|
|
$rl = new EmptyResourceLoader();
|
|
|
|
|
$rl->register( 'test', [] );
|
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
|
ResourceLoaderFileModule::class,
|
|
|
|
|
$rl->getModule( 'test' ),
|
|
|
|
|
'Array-style module registrations default to FileModule'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-28 01:50:11 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::makeHash
|
|
|
|
|
*/
|
|
|
|
|
public function testGetVersionHash_length() {
|
|
|
|
|
$hash = ResourceLoader::makeHash(
|
|
|
|
|
'Anything you do could have serious repercussions on future events.'
|
|
|
|
|
);
|
|
|
|
|
$this->assertSame( 'xhh1x', $hash, 'Hash' );
|
|
|
|
|
$this->assertSame( ResourceLoader::HASH_LENGTH, strlen( $hash ), 'Hash length' );
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-19 18:37:04 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::getLessCompiler
|
|
|
|
|
*/
|
|
|
|
|
public function testLessImportDirs() {
|
|
|
|
|
$rl = new EmptyResourceLoader();
|
2018-08-09 15:09:56 +00:00
|
|
|
$lc = $rl->getLessCompiler( [ 'foo' => '2px', 'Foo' => '#eeeeee' ] );
|
2018-05-19 18:37:04 +00:00
|
|
|
$basePath = dirname( dirname( __DIR__ ) ) . '/data/less';
|
|
|
|
|
$lc->SetImportDirs( [
|
2021-09-03 22:52:31 +00:00
|
|
|
"$basePath/common" => '',
|
2018-05-19 18:37:04 +00:00
|
|
|
] );
|
|
|
|
|
$css = $lc->parseFile( "$basePath/module/use-import-dir.less" )->getCss();
|
|
|
|
|
$this->assertStringEqualsFile( "$basePath/module/styles.css", $css );
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-20 18:32:57 +00:00
|
|
|
public static function provideMediaWikiVariablesCases() {
|
|
|
|
|
$basePath = __DIR__ . '/../../data/less';
|
|
|
|
|
return [
|
|
|
|
|
[
|
|
|
|
|
'config' => [],
|
|
|
|
|
'importPaths' => [],
|
|
|
|
|
'skin' => 'fallback',
|
|
|
|
|
'expected' => "$basePath/use-variables-default.css",
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'config' => [
|
|
|
|
|
'wgValidSkinNames' => [
|
|
|
|
|
// Required to make ResourceLoaderContext::getSkin work
|
|
|
|
|
'example' => 'Example',
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
'importPaths' => [
|
|
|
|
|
'example' => "$basePath/testvariables/",
|
|
|
|
|
],
|
|
|
|
|
'skin' => 'example',
|
|
|
|
|
'expected' => "$basePath/use-variables-test.css",
|
|
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideMediaWikiVariablesCases
|
|
|
|
|
* @covers ResourceLoader::getLessCompiler
|
|
|
|
|
* @covers ResourceLoaderFileModule::compileLessFile
|
|
|
|
|
*/
|
|
|
|
|
public function testMediawikiVariablesDefault( array $config, array $importPaths, $skin, $expectedFile ) {
|
|
|
|
|
$this->setMwGlobals( $config );
|
|
|
|
|
$reset = ExtensionRegistry::getInstance()->setAttributeForTest( 'SkinLessImportPaths', $importPaths );
|
|
|
|
|
// Reset Skin::getSkinNames for ResourceLoaderContext
|
|
|
|
|
MediaWiki\MediaWikiServices::getInstance()->resetServiceForTesting( 'SkinFactory' );
|
|
|
|
|
|
|
|
|
|
$context = $this->getResourceLoaderContext( [ 'skin' => $skin ] );
|
|
|
|
|
$module = new ResourceLoaderFileModule( [
|
|
|
|
|
'localBasePath' => __DIR__ . '/../../data/less',
|
|
|
|
|
'styles' => [ 'use-variables.less' ],
|
|
|
|
|
] );
|
|
|
|
|
$module->setName( 'test.less' );
|
|
|
|
|
$styles = $module->getStyles( $context );
|
|
|
|
|
$this->assertStringEqualsFile( $expectedFile, $styles['all'] );
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
public static function providePackedModules() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[
|
2011-06-07 17:56:40 +00:00
|
|
|
'Example from makePackedModulesString doc comment',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' ],
|
2011-06-07 17:56:40 +00:00
|
|
|
'foo.bar,baz|bar.baz,quux',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2011-06-07 17:56:40 +00:00
|
|
|
'Example from expandModuleNames doc comment',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'jquery.foo', 'jquery.bar', 'jquery.ui.baz', 'jquery.ui.quux' ],
|
2011-06-07 17:56:40 +00:00
|
|
|
'jquery.foo,bar|jquery.ui.baz,quux',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2018-03-01 01:45:45 +00:00
|
|
|
'Regression fixed in r87497 (7fee86c38e) with dotless names',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'foo', 'bar', 'baz' ],
|
2011-06-07 17:56:40 +00:00
|
|
|
'foo,bar,baz',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2013-05-27 15:13:59 +00:00
|
|
|
'Prefixless modules after a prefixed module',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'single.module', 'foobar', 'foobaz' ],
|
2013-05-27 15:13:59 +00:00
|
|
|
'single.module|foobar,foobaz',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2016-08-29 23:35:55 +00:00
|
|
|
[
|
|
|
|
|
'Ordering',
|
|
|
|
|
[ 'foo', 'foo.baz', 'baz.quux', 'foo.bar' ],
|
|
|
|
|
'foo|foo.baz,bar|baz.quux',
|
|
|
|
|
[ 'foo', 'foo.baz', 'foo.bar', 'baz.quux' ],
|
|
|
|
|
]
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2011-06-07 17:56:40 +00:00
|
|
|
}
|
2014-06-28 02:57:40 +00:00
|
|
|
|
2016-08-25 01:50:30 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider providePackedModules
|
|
|
|
|
* @covers ResourceLoader::makePackedModulesString
|
|
|
|
|
*/
|
|
|
|
|
public function testMakePackedModulesString( $desc, $modules, $packed ) {
|
|
|
|
|
$this->assertEquals( $packed, ResourceLoader::makePackedModulesString( $modules ), $desc );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider providePackedModules
|
2019-04-11 20:28:53 +00:00
|
|
|
* @covers ResourceLoader::expandModuleNames
|
2016-08-25 01:50:30 +00:00
|
|
|
*/
|
2016-08-29 23:35:55 +00:00
|
|
|
public function testExpandModuleNames( $desc, $modules, $packed, $unpacked = null ) {
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$unpacked ?: $modules,
|
2019-04-11 20:28:53 +00:00
|
|
|
ResourceLoader::expandModuleNames( $packed ),
|
2016-08-29 23:35:55 +00:00
|
|
|
$desc
|
|
|
|
|
);
|
2016-08-25 01:50:30 +00:00
|
|
|
}
|
|
|
|
|
|
2014-08-25 08:02:48 +00:00
|
|
|
public static function provideAddSource() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2016-08-25 01:50:30 +00:00
|
|
|
[ 'foowiki', 'https://example.org/w/load.php', 'foowiki' ],
|
|
|
|
|
[ 'foowiki', [ 'loadScript' => 'https://example.org/w/load.php' ], 'foowiki' ],
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2016-08-25 01:50:30 +00:00
|
|
|
[
|
|
|
|
|
'foowiki' => 'https://example.org/w/load.php',
|
|
|
|
|
'bazwiki' => 'https://example.com/w/load.php',
|
|
|
|
|
],
|
2014-08-25 08:02:48 +00:00
|
|
|
null,
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'foowiki', 'bazwiki' ]
|
2016-08-25 01:50:30 +00:00
|
|
|
]
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2014-08-25 08:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideAddSource
|
|
|
|
|
* @covers ResourceLoader::addSource
|
2015-03-23 03:57:45 +00:00
|
|
|
* @covers ResourceLoader::getSources
|
2014-08-25 08:02:48 +00:00
|
|
|
*/
|
|
|
|
|
public function testAddSource( $name, $info, $expected ) {
|
2019-06-10 15:00:16 +00:00
|
|
|
$rl = new EmptyResourceLoader;
|
2014-08-25 08:02:48 +00:00
|
|
|
$rl->addSource( $name, $info );
|
|
|
|
|
if ( is_array( $expected ) ) {
|
|
|
|
|
foreach ( $expected as $source ) {
|
|
|
|
|
$this->assertArrayHasKey( $source, $rl->getSources() );
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$this->assertArrayHasKey( $expected, $rl->getSources() );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-25 01:50:30 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::addSource
|
|
|
|
|
*/
|
|
|
|
|
public function testAddSourceDupe() {
|
2019-06-10 15:00:16 +00:00
|
|
|
$rl = new EmptyResourceLoader;
|
2020-02-21 00:26:07 +00:00
|
|
|
$this->expectException( RuntimeException::class );
|
|
|
|
|
$this->expectExceptionMessage( 'Cannot register source' );
|
2016-08-25 01:50:30 +00:00
|
|
|
$rl->addSource( 'foo', 'https://example.org/w/load.php' );
|
|
|
|
|
$rl->addSource( 'foo', 'https://example.com/w/load.php' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::addSource
|
|
|
|
|
*/
|
|
|
|
|
public function testAddSourceInvalid() {
|
2019-06-10 15:00:16 +00:00
|
|
|
$rl = new EmptyResourceLoader;
|
2020-02-21 00:26:07 +00:00
|
|
|
$this->expectException( InvalidArgumentException::class );
|
|
|
|
|
$this->expectExceptionMessage( 'must have a "loadScript" key' );
|
2016-08-25 01:50:30 +00:00
|
|
|
$rl->addSource( 'foo', [ 'x' => 'https://example.org/w/load.php' ] );
|
2014-06-28 02:57:40 +00:00
|
|
|
}
|
|
|
|
|
|
2014-12-09 01:17:53 +00:00
|
|
|
public static function provideLoaderImplement() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ [
|
2014-12-09 01:17:53 +00:00
|
|
|
'title' => 'Implement scripts, styles and messages',
|
|
|
|
|
|
|
|
|
|
'name' => 'test.example',
|
|
|
|
|
'scripts' => 'mw.example();',
|
2016-02-17 09:09:32 +00:00
|
|
|
'styles' => [ 'css' => [ '.mw-example {}' ] ],
|
|
|
|
|
'messages' => [ 'example' => '' ],
|
|
|
|
|
'templates' => [],
|
2014-12-09 01:17:53 +00:00
|
|
|
|
2016-01-22 19:29:28 +00:00
|
|
|
'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
|
2014-12-09 01:17:53 +00:00
|
|
|
mw.example();
|
|
|
|
|
}, {
|
|
|
|
|
"css": [
|
|
|
|
|
".mw-example {}"
|
|
|
|
|
]
|
|
|
|
|
}, {
|
|
|
|
|
"example": ""
|
|
|
|
|
} );',
|
2016-02-17 09:09:32 +00:00
|
|
|
] ],
|
|
|
|
|
[ [
|
2014-12-09 01:17:53 +00:00
|
|
|
'title' => 'Implement scripts',
|
|
|
|
|
|
|
|
|
|
'name' => 'test.example',
|
|
|
|
|
'scripts' => 'mw.example();',
|
2016-02-17 09:09:32 +00:00
|
|
|
'styles' => [],
|
2014-12-09 01:17:53 +00:00
|
|
|
|
2016-01-22 19:29:28 +00:00
|
|
|
'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
|
2014-12-09 01:17:53 +00:00
|
|
|
mw.example();
|
2019-07-27 09:26:58 +00:00
|
|
|
} );',
|
|
|
|
|
] ],
|
|
|
|
|
[ [
|
|
|
|
|
'title' => 'Implement scripts with newline at end',
|
|
|
|
|
|
|
|
|
|
'name' => 'test.example',
|
|
|
|
|
'scripts' => "mw.example();\n",
|
|
|
|
|
'styles' => [],
|
|
|
|
|
|
|
|
|
|
'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
|
|
|
|
|
mw.example();
|
|
|
|
|
|
|
|
|
|
} );',
|
|
|
|
|
] ],
|
|
|
|
|
[ [
|
|
|
|
|
'title' => 'Implement scripts with comment at end',
|
|
|
|
|
|
|
|
|
|
'name' => 'test.example',
|
|
|
|
|
'scripts' => "mw.example();//Foo",
|
|
|
|
|
'styles' => [],
|
|
|
|
|
|
|
|
|
|
'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
|
|
|
|
|
mw.example();//Foo
|
2014-12-09 01:17:53 +00:00
|
|
|
} );',
|
2016-02-17 09:09:32 +00:00
|
|
|
] ],
|
|
|
|
|
[ [
|
2014-12-09 01:17:53 +00:00
|
|
|
'title' => 'Implement styles',
|
|
|
|
|
|
|
|
|
|
'name' => 'test.example',
|
2016-02-17 09:09:32 +00:00
|
|
|
'scripts' => [],
|
|
|
|
|
'styles' => [ 'css' => [ '.mw-example {}' ] ],
|
2014-12-09 01:17:53 +00:00
|
|
|
|
|
|
|
|
'expected' => 'mw.loader.implement( "test.example", [], {
|
|
|
|
|
"css": [
|
|
|
|
|
".mw-example {}"
|
|
|
|
|
]
|
|
|
|
|
} );',
|
2016-02-17 09:09:32 +00:00
|
|
|
] ],
|
|
|
|
|
[ [
|
2014-12-09 01:17:53 +00:00
|
|
|
'title' => 'Implement scripts and messages',
|
|
|
|
|
|
|
|
|
|
'name' => 'test.example',
|
|
|
|
|
'scripts' => 'mw.example();',
|
2016-02-17 09:09:32 +00:00
|
|
|
'messages' => [ 'example' => '' ],
|
2014-12-09 01:17:53 +00:00
|
|
|
|
2016-01-22 19:29:28 +00:00
|
|
|
'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
|
2014-12-09 01:17:53 +00:00
|
|
|
mw.example();
|
|
|
|
|
}, {}, {
|
|
|
|
|
"example": ""
|
|
|
|
|
} );',
|
2016-02-17 09:09:32 +00:00
|
|
|
] ],
|
|
|
|
|
[ [
|
2014-12-09 01:17:53 +00:00
|
|
|
'title' => 'Implement scripts and templates',
|
|
|
|
|
|
|
|
|
|
'name' => 'test.example',
|
|
|
|
|
'scripts' => 'mw.example();',
|
2016-02-17 09:09:32 +00:00
|
|
|
'templates' => [ 'example.html' => '' ],
|
2014-12-09 01:17:53 +00:00
|
|
|
|
2016-01-22 19:29:28 +00:00
|
|
|
'expected' => 'mw.loader.implement( "test.example", function ( $, jQuery, require, module ) {
|
2014-12-09 01:17:53 +00:00
|
|
|
mw.example();
|
|
|
|
|
}, {}, {}, {
|
|
|
|
|
"example.html": ""
|
|
|
|
|
} );',
|
2016-02-17 09:09:32 +00:00
|
|
|
] ],
|
2016-08-25 01:50:30 +00:00
|
|
|
[ [
|
|
|
|
|
'title' => 'Implement unwrapped user script',
|
|
|
|
|
|
|
|
|
|
'name' => 'user',
|
|
|
|
|
'scripts' => 'mw.example( 1 );',
|
2016-09-15 04:01:09 +00:00
|
|
|
'wrap' => false,
|
2016-08-25 01:50:30 +00:00
|
|
|
|
|
|
|
|
'expected' => 'mw.loader.implement( "user", "mw.example( 1 );" );',
|
|
|
|
|
] ],
|
ResourceLoader: Add support for packageFiles
Package files are files that are part of a module, but are not
immediately executed when the module executes. Instead, they are
lazy-excecuted when require() is called on them. Package files can be
scripts (JS) or data (JSON), and can be real files on the file system,
or virtual files generated by a callback.
Using virtual data files, server-side data and config variables can be
bundled with a module. Support for file-based require() allows us to
import npm modules into ResourceLoader more easily.
The require function passed to each script execution context, which was
previously a reference to the global mw.loader.require() function, is
changed to one that is scoped to the module and the file being executed.
This is needed to support relative paths: require( '../foo.js' ) can
mean a different file depending on the path of the calling file.
The results of require()ing each file (i.e. the value of module.exports
after executing it) are stored, and calling require() on the same file a
second time won't execute it again, but will return the stored value.
Miscellaneous changes:
- Add XmlJsCode::encodeObject(), which combines an associative array of
XmlJsCode objects into one larger XmlJsCode object. This is needed for
encoding the packageFiles parameter in mw.loader.implement() calls.
Bug: T133462
Change-Id: I78cc86e626de0720397718cd2bed8ed279579112
2018-11-03 00:53:17 +00:00
|
|
|
[ [
|
|
|
|
|
'title' => 'Implement multi-file script',
|
|
|
|
|
|
|
|
|
|
'name' => 'test.multifile',
|
|
|
|
|
'scripts' => [
|
|
|
|
|
'files' => [
|
|
|
|
|
'one.js' => [
|
|
|
|
|
'type' => 'script',
|
|
|
|
|
'content' => 'mw.example( 1 );',
|
|
|
|
|
],
|
|
|
|
|
'two.json' => [
|
|
|
|
|
'type' => 'data',
|
|
|
|
|
'content' => [ 'n' => 2 ],
|
|
|
|
|
],
|
|
|
|
|
'three.js' => [
|
|
|
|
|
'type' => 'script',
|
2019-07-27 09:26:58 +00:00
|
|
|
'content' => 'mw.example( 3 ); // Comment'
|
|
|
|
|
],
|
|
|
|
|
'four.js' => [
|
|
|
|
|
'type' => 'script',
|
|
|
|
|
'content' => "mw.example( 4 );\n"
|
|
|
|
|
],
|
|
|
|
|
'five.js' => [
|
|
|
|
|
'type' => 'script',
|
|
|
|
|
'content' => 'mw.example( 5 );'
|
ResourceLoader: Add support for packageFiles
Package files are files that are part of a module, but are not
immediately executed when the module executes. Instead, they are
lazy-excecuted when require() is called on them. Package files can be
scripts (JS) or data (JSON), and can be real files on the file system,
or virtual files generated by a callback.
Using virtual data files, server-side data and config variables can be
bundled with a module. Support for file-based require() allows us to
import npm modules into ResourceLoader more easily.
The require function passed to each script execution context, which was
previously a reference to the global mw.loader.require() function, is
changed to one that is scoped to the module and the file being executed.
This is needed to support relative paths: require( '../foo.js' ) can
mean a different file depending on the path of the calling file.
The results of require()ing each file (i.e. the value of module.exports
after executing it) are stored, and calling require() on the same file a
second time won't execute it again, but will return the stored value.
Miscellaneous changes:
- Add XmlJsCode::encodeObject(), which combines an associative array of
XmlJsCode objects into one larger XmlJsCode object. This is needed for
encoding the packageFiles parameter in mw.loader.implement() calls.
Bug: T133462
Change-Id: I78cc86e626de0720397718cd2bed8ed279579112
2018-11-03 00:53:17 +00:00
|
|
|
],
|
|
|
|
|
],
|
2019-07-27 09:26:58 +00:00
|
|
|
'main' => 'five.js',
|
ResourceLoader: Add support for packageFiles
Package files are files that are part of a module, but are not
immediately executed when the module executes. Instead, they are
lazy-excecuted when require() is called on them. Package files can be
scripts (JS) or data (JSON), and can be real files on the file system,
or virtual files generated by a callback.
Using virtual data files, server-side data and config variables can be
bundled with a module. Support for file-based require() allows us to
import npm modules into ResourceLoader more easily.
The require function passed to each script execution context, which was
previously a reference to the global mw.loader.require() function, is
changed to one that is scoped to the module and the file being executed.
This is needed to support relative paths: require( '../foo.js' ) can
mean a different file depending on the path of the calling file.
The results of require()ing each file (i.e. the value of module.exports
after executing it) are stored, and calling require() on the same file a
second time won't execute it again, but will return the stored value.
Miscellaneous changes:
- Add XmlJsCode::encodeObject(), which combines an associative array of
XmlJsCode objects into one larger XmlJsCode object. This is needed for
encoding the packageFiles parameter in mw.loader.implement() calls.
Bug: T133462
Change-Id: I78cc86e626de0720397718cd2bed8ed279579112
2018-11-03 00:53:17 +00:00
|
|
|
],
|
|
|
|
|
|
|
|
|
|
'expected' => <<<END
|
|
|
|
|
mw.loader.implement( "test.multifile", {
|
2019-07-27 09:26:58 +00:00
|
|
|
"main": "five.js",
|
ResourceLoader: Add support for packageFiles
Package files are files that are part of a module, but are not
immediately executed when the module executes. Instead, they are
lazy-excecuted when require() is called on them. Package files can be
scripts (JS) or data (JSON), and can be real files on the file system,
or virtual files generated by a callback.
Using virtual data files, server-side data and config variables can be
bundled with a module. Support for file-based require() allows us to
import npm modules into ResourceLoader more easily.
The require function passed to each script execution context, which was
previously a reference to the global mw.loader.require() function, is
changed to one that is scoped to the module and the file being executed.
This is needed to support relative paths: require( '../foo.js' ) can
mean a different file depending on the path of the calling file.
The results of require()ing each file (i.e. the value of module.exports
after executing it) are stored, and calling require() on the same file a
second time won't execute it again, but will return the stored value.
Miscellaneous changes:
- Add XmlJsCode::encodeObject(), which combines an associative array of
XmlJsCode objects into one larger XmlJsCode object. This is needed for
encoding the packageFiles parameter in mw.loader.implement() calls.
Bug: T133462
Change-Id: I78cc86e626de0720397718cd2bed8ed279579112
2018-11-03 00:53:17 +00:00
|
|
|
"files": {
|
|
|
|
|
"one.js": function ( require, module ) {
|
|
|
|
|
mw.example( 1 );
|
|
|
|
|
},
|
|
|
|
|
"two.json": {
|
|
|
|
|
"n": 2
|
|
|
|
|
},
|
|
|
|
|
"three.js": function ( require, module ) {
|
2019-07-27 09:26:58 +00:00
|
|
|
mw.example( 3 ); // Comment
|
|
|
|
|
},
|
|
|
|
|
"four.js": function ( require, module ) {
|
|
|
|
|
mw.example( 4 );
|
|
|
|
|
},
|
|
|
|
|
"five.js": function ( require, module ) {
|
|
|
|
|
mw.example( 5 );
|
ResourceLoader: Add support for packageFiles
Package files are files that are part of a module, but are not
immediately executed when the module executes. Instead, they are
lazy-excecuted when require() is called on them. Package files can be
scripts (JS) or data (JSON), and can be real files on the file system,
or virtual files generated by a callback.
Using virtual data files, server-side data and config variables can be
bundled with a module. Support for file-based require() allows us to
import npm modules into ResourceLoader more easily.
The require function passed to each script execution context, which was
previously a reference to the global mw.loader.require() function, is
changed to one that is scoped to the module and the file being executed.
This is needed to support relative paths: require( '../foo.js' ) can
mean a different file depending on the path of the calling file.
The results of require()ing each file (i.e. the value of module.exports
after executing it) are stored, and calling require() on the same file a
second time won't execute it again, but will return the stored value.
Miscellaneous changes:
- Add XmlJsCode::encodeObject(), which combines an associative array of
XmlJsCode objects into one larger XmlJsCode object. This is needed for
encoding the packageFiles parameter in mw.loader.implement() calls.
Bug: T133462
Change-Id: I78cc86e626de0720397718cd2bed8ed279579112
2018-11-03 00:53:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} );
|
|
|
|
|
END
|
|
|
|
|
] ],
|
2021-07-20 10:10:21 +00:00
|
|
|
[ [
|
|
|
|
|
'title' => 'Implement multi-file script, non-debug mode',
|
|
|
|
|
|
|
|
|
|
'name' => 'test.multifile',
|
|
|
|
|
'debug' => 'false',
|
|
|
|
|
'scripts' => [
|
|
|
|
|
'files' => [
|
|
|
|
|
'one.js' => [
|
|
|
|
|
'type' => 'script',
|
|
|
|
|
'content' => 'mw.example( 1 );',
|
|
|
|
|
],
|
|
|
|
|
'two.json' => [
|
|
|
|
|
'type' => 'data',
|
|
|
|
|
'content' => [ 'n' => 2 ],
|
|
|
|
|
],
|
|
|
|
|
'three.js' => [
|
|
|
|
|
'type' => 'script',
|
|
|
|
|
'content' => 'mw.example( 3 );//'
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
'main' => 'three.js',
|
|
|
|
|
],
|
|
|
|
|
|
|
|
|
|
'expected' => implode( '', [
|
|
|
|
|
'mw.loader.implement("test.multifile",',
|
|
|
|
|
'{"main":"three.js","files":{',
|
|
|
|
|
'"one.js":function(require,module){mw.example( 1 );' . "\n" . '},',
|
|
|
|
|
'"two.json":{"n":2},',
|
|
|
|
|
'"three.js":function(require,module){mw.example( 3 );//' . "\n" . '}',
|
|
|
|
|
'}});',
|
|
|
|
|
] ),
|
|
|
|
|
] ],
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2014-12-09 01:17:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideLoaderImplement
|
|
|
|
|
* @covers ResourceLoader::makeLoaderImplementScript
|
2016-08-25 01:50:30 +00:00
|
|
|
* @covers ResourceLoader::trimArray
|
2014-12-09 01:17:53 +00:00
|
|
|
*/
|
|
|
|
|
public function testMakeLoaderImplementScript( $case ) {
|
2016-08-25 01:50:30 +00:00
|
|
|
$case += [
|
2016-09-15 04:01:09 +00:00
|
|
|
'wrap' => true,
|
2021-07-20 10:10:21 +00:00
|
|
|
'styles' => [],
|
|
|
|
|
'templates' => [],
|
|
|
|
|
'messages' => new XmlJsCode( '{}' ),
|
|
|
|
|
'packageFiles' => [],
|
|
|
|
|
'debug' => 'true',
|
2016-08-25 01:50:30 +00:00
|
|
|
];
|
2018-01-13 00:02:09 +00:00
|
|
|
$rl = TestingAccessWrapper::newFromClass( ResourceLoader::class );
|
2019-09-09 15:50:13 +00:00
|
|
|
$context = new ResourceLoaderContext( new EmptyResourceLoader(), new FauxRequest( [
|
2021-07-20 10:10:21 +00:00
|
|
|
'debug' => $case['debug'],
|
2019-09-09 15:50:13 +00:00
|
|
|
] ) );
|
2014-12-09 01:17:53 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
$case['expected'],
|
2016-09-15 04:01:09 +00:00
|
|
|
$rl->makeLoaderImplementScript(
|
2019-09-09 15:50:13 +00:00
|
|
|
$context,
|
2014-12-09 01:17:53 +00:00
|
|
|
$case['name'],
|
2016-09-15 04:01:09 +00:00
|
|
|
( $case['wrap'] && is_string( $case['scripts'] ) )
|
|
|
|
|
? new XmlJsCode( $case['scripts'] )
|
|
|
|
|
: $case['scripts'],
|
2014-12-09 01:17:53 +00:00
|
|
|
$case['styles'],
|
|
|
|
|
$case['messages'],
|
ResourceLoader: Add support for packageFiles
Package files are files that are part of a module, but are not
immediately executed when the module executes. Instead, they are
lazy-excecuted when require() is called on them. Package files can be
scripts (JS) or data (JSON), and can be real files on the file system,
or virtual files generated by a callback.
Using virtual data files, server-side data and config variables can be
bundled with a module. Support for file-based require() allows us to
import npm modules into ResourceLoader more easily.
The require function passed to each script execution context, which was
previously a reference to the global mw.loader.require() function, is
changed to one that is scoped to the module and the file being executed.
This is needed to support relative paths: require( '../foo.js' ) can
mean a different file depending on the path of the calling file.
The results of require()ing each file (i.e. the value of module.exports
after executing it) are stored, and calling require() on the same file a
second time won't execute it again, but will return the stored value.
Miscellaneous changes:
- Add XmlJsCode::encodeObject(), which combines an associative array of
XmlJsCode objects into one larger XmlJsCode object. This is needed for
encoding the packageFiles parameter in mw.loader.implement() calls.
Bug: T133462
Change-Id: I78cc86e626de0720397718cd2bed8ed279579112
2018-11-03 00:53:17 +00:00
|
|
|
$case['templates'],
|
|
|
|
|
$case['packageFiles']
|
2014-12-09 01:17:53 +00:00
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-25 01:50:30 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::makeLoaderImplementScript
|
|
|
|
|
*/
|
|
|
|
|
public function testMakeLoaderImplementScriptInvalid() {
|
2020-02-21 00:26:07 +00:00
|
|
|
$this->expectException( InvalidArgumentException::class );
|
|
|
|
|
$this->expectExceptionMessage( 'Script must be a' );
|
2018-01-13 00:02:09 +00:00
|
|
|
$rl = TestingAccessWrapper::newFromClass( ResourceLoader::class );
|
2019-09-09 15:50:13 +00:00
|
|
|
$context = new ResourceLoaderContext( new EmptyResourceLoader(), new FauxRequest() );
|
2016-09-15 04:01:09 +00:00
|
|
|
$rl->makeLoaderImplementScript(
|
2019-09-09 15:50:13 +00:00
|
|
|
$context,
|
2016-08-25 01:50:30 +00:00
|
|
|
'test', // name
|
|
|
|
|
123, // scripts
|
|
|
|
|
null, // styles
|
|
|
|
|
null, // messages
|
ResourceLoader: Add support for packageFiles
Package files are files that are part of a module, but are not
immediately executed when the module executes. Instead, they are
lazy-excecuted when require() is called on them. Package files can be
scripts (JS) or data (JSON), and can be real files on the file system,
or virtual files generated by a callback.
Using virtual data files, server-side data and config variables can be
bundled with a module. Support for file-based require() allows us to
import npm modules into ResourceLoader more easily.
The require function passed to each script execution context, which was
previously a reference to the global mw.loader.require() function, is
changed to one that is scoped to the module and the file being executed.
This is needed to support relative paths: require( '../foo.js' ) can
mean a different file depending on the path of the calling file.
The results of require()ing each file (i.e. the value of module.exports
after executing it) are stored, and calling require() on the same file a
second time won't execute it again, but will return the stored value.
Miscellaneous changes:
- Add XmlJsCode::encodeObject(), which combines an associative array of
XmlJsCode objects into one larger XmlJsCode object. This is needed for
encoding the packageFiles parameter in mw.loader.implement() calls.
Bug: T133462
Change-Id: I78cc86e626de0720397718cd2bed8ed279579112
2018-11-03 00:53:17 +00:00
|
|
|
null, // templates
|
|
|
|
|
null // package files
|
2016-08-25 01:50:30 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-06 01:19:48 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::makeLoaderRegisterScript
|
|
|
|
|
*/
|
|
|
|
|
public function testMakeLoaderRegisterScript() {
|
2019-09-09 15:50:13 +00:00
|
|
|
$context = new ResourceLoaderContext( new EmptyResourceLoader(), new FauxRequest( [
|
|
|
|
|
'debug' => 'true',
|
|
|
|
|
] ) );
|
2017-04-06 01:19:48 +00:00
|
|
|
$this->assertEquals(
|
2019-07-16 00:15:32 +00:00
|
|
|
'mw.loader.register([
|
2017-04-06 01:19:48 +00:00
|
|
|
[
|
|
|
|
|
"test.name",
|
|
|
|
|
"1234567"
|
|
|
|
|
]
|
2019-07-16 00:15:32 +00:00
|
|
|
]);',
|
2019-09-09 15:50:13 +00:00
|
|
|
ResourceLoader::makeLoaderRegisterScript( $context, [
|
2017-04-06 01:19:48 +00:00
|
|
|
[ 'test.name', '1234567' ],
|
|
|
|
|
] ),
|
|
|
|
|
'Nested array parameter'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2019-07-16 00:15:32 +00:00
|
|
|
'mw.loader.register([
|
2018-09-15 21:31:18 +00:00
|
|
|
[
|
|
|
|
|
"test.foo",
|
|
|
|
|
"100"
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
"test.bar",
|
|
|
|
|
"200",
|
|
|
|
|
[
|
|
|
|
|
"test.unknown"
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
"test.baz",
|
|
|
|
|
"300",
|
|
|
|
|
[
|
|
|
|
|
3,
|
|
|
|
|
0
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
"test.quux",
|
|
|
|
|
"400",
|
|
|
|
|
[],
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
"return true;"
|
|
|
|
|
]
|
2019-07-16 00:15:32 +00:00
|
|
|
]);',
|
2019-09-09 15:50:13 +00:00
|
|
|
ResourceLoader::makeLoaderRegisterScript( $context, [
|
2018-09-15 21:31:18 +00:00
|
|
|
[ 'test.foo', '100' , [], null, null ],
|
|
|
|
|
[ 'test.bar', '200', [ 'test.unknown' ], null ],
|
|
|
|
|
[ 'test.baz', '300', [ 'test.quux', 'test.foo' ], null ],
|
|
|
|
|
[ 'test.quux', '400', [], null, null, 'return true;' ],
|
|
|
|
|
] ),
|
|
|
|
|
'Compact dependency indexes'
|
2017-04-06 01:19:48 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-25 01:50:30 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::makeLoaderSourcesScript
|
|
|
|
|
*/
|
|
|
|
|
public function testMakeLoaderSourcesScript() {
|
2019-09-09 15:50:13 +00:00
|
|
|
$context = new ResourceLoaderContext( new EmptyResourceLoader(), new FauxRequest( [
|
|
|
|
|
'debug' => 'true',
|
|
|
|
|
] ) );
|
2016-08-25 01:50:30 +00:00
|
|
|
$this->assertEquals(
|
2019-07-16 00:15:32 +00:00
|
|
|
'mw.loader.addSource({
|
2018-09-03 23:17:11 +00:00
|
|
|
"local": "/w/load.php"
|
2019-07-16 00:15:32 +00:00
|
|
|
});',
|
2019-09-09 15:50:13 +00:00
|
|
|
ResourceLoader::makeLoaderSourcesScript( $context, [ 'local' => '/w/load.php' ] )
|
2016-08-25 01:50:30 +00:00
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
2019-07-16 00:15:32 +00:00
|
|
|
'mw.loader.addSource({
|
2016-08-25 01:50:30 +00:00
|
|
|
"local": "/w/load.php",
|
|
|
|
|
"example": "https://example.org/w/load.php"
|
2019-07-16 00:15:32 +00:00
|
|
|
});',
|
2019-09-09 15:50:13 +00:00
|
|
|
ResourceLoader::makeLoaderSourcesScript( $context, [
|
2016-08-25 01:50:30 +00:00
|
|
|
'local' => '/w/load.php',
|
|
|
|
|
'example' => 'https://example.org/w/load.php'
|
|
|
|
|
] )
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
2019-07-16 00:15:32 +00:00
|
|
|
'mw.loader.addSource([]);',
|
2019-09-09 15:50:13 +00:00
|
|
|
ResourceLoader::makeLoaderSourcesScript( $context, [] )
|
2016-08-25 01:50:30 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function fakeSources() {
|
|
|
|
|
return [
|
|
|
|
|
'examplewiki' => [
|
|
|
|
|
'loadScript' => '//example.org/w/load.php',
|
|
|
|
|
'apiScript' => '//example.org/w/api.php',
|
|
|
|
|
],
|
|
|
|
|
'example2wiki' => [
|
|
|
|
|
'loadScript' => '//example.com/w/load.php',
|
|
|
|
|
'apiScript' => '//example.com/w/api.php',
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-28 02:57:40 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::getLoadScript
|
|
|
|
|
*/
|
|
|
|
|
public function testGetLoadScript() {
|
2019-06-10 15:00:16 +00:00
|
|
|
$rl = new EmptyResourceLoader();
|
2014-06-28 02:57:40 +00:00
|
|
|
$sources = self::fakeSources();
|
|
|
|
|
$rl->addSource( $sources );
|
2016-02-17 09:09:32 +00:00
|
|
|
foreach ( [ 'examplewiki', 'example2wiki' ] as $name ) {
|
2014-06-28 02:57:40 +00:00
|
|
|
$this->assertEquals( $rl->getLoadScript( $name ), $sources[$name]['loadScript'] );
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-21 00:26:07 +00:00
|
|
|
$this->expectException( UnexpectedValueException::class );
|
|
|
|
|
$rl->getLoadScript( 'thiswasneverregistered' );
|
2014-06-28 02:57:40 +00:00
|
|
|
}
|
resourceloader: Don't let module exception break startup
When getScript (or some other method used in a module response)
throws an error, only that module fails (by outputting mw.loader.state
instead of mw.loader.implement). Other modules will work.
This has always been the case and is working fine. For example,
"load.php?modules=foo|bar", where 'foo' throws, will return:
```js
/* exception message: .. */
mw.loader.implement('bar', ..)
mw.loader.state('foo', 'error')
```
The problem, however, is that during the generation of the startup
module, we iterate over all other modules. In 2011, the
getVersionHash method (then: getModifiedTime) was fairly simple
and unlikely to throw errors.
Nowadays, some modules use enableModuleContentVersion which will
involve the same code path as for regular module responses.
The try/catch in ResourceLoader::makeModuleResponse() suffices
for the case of loading modules other than startup. But when
loading the startup module, and an exception happens in getVersionHash,
then the entire startup response is replaced with an exception comment.
Example case:
* A file not existing for a FileModule subclass that uses
enableModuleContentVersion.
* A database error from a data module, like CiteDataModule or
CNChoiceData.
Changes:
* Ensure E-Tag is still useful while an error happens in production
because we respond with 200 OK and one error isn't the same as
another.
Fixed by try/catch in getCombinedVersion.
* Ensure start manifest isn't disrupted by one broken module.
Fixed by try/catch in StartupModule::getModuleRegistrations().
Tests:
* testMakeModuleResponseError: The case that already worked fined.
* testMakeModuleResponseStartupError: The case fixed in this commit.
* testGetCombinedVersion: The case fixed in this commit for E-Tag.
Bug: T152266
Change-Id: Ice4ede5ea594bf3fa591134bc9382bd9c24e2f39
2016-12-03 00:48:14 +00:00
|
|
|
|
2018-05-28 00:39:58 +00:00
|
|
|
protected function getFailFerryMock( $getter = 'getScript' ) {
|
resourceloader: Don't let module exception break startup
When getScript (or some other method used in a module response)
throws an error, only that module fails (by outputting mw.loader.state
instead of mw.loader.implement). Other modules will work.
This has always been the case and is working fine. For example,
"load.php?modules=foo|bar", where 'foo' throws, will return:
```js
/* exception message: .. */
mw.loader.implement('bar', ..)
mw.loader.state('foo', 'error')
```
The problem, however, is that during the generation of the startup
module, we iterate over all other modules. In 2011, the
getVersionHash method (then: getModifiedTime) was fairly simple
and unlikely to throw errors.
Nowadays, some modules use enableModuleContentVersion which will
involve the same code path as for regular module responses.
The try/catch in ResourceLoader::makeModuleResponse() suffices
for the case of loading modules other than startup. But when
loading the startup module, and an exception happens in getVersionHash,
then the entire startup response is replaced with an exception comment.
Example case:
* A file not existing for a FileModule subclass that uses
enableModuleContentVersion.
* A database error from a data module, like CiteDataModule or
CNChoiceData.
Changes:
* Ensure E-Tag is still useful while an error happens in production
because we respond with 200 OK and one error isn't the same as
another.
Fixed by try/catch in getCombinedVersion.
* Ensure start manifest isn't disrupted by one broken module.
Fixed by try/catch in StartupModule::getModuleRegistrations().
Tests:
* testMakeModuleResponseError: The case that already worked fined.
* testMakeModuleResponseStartupError: The case fixed in this commit.
* testGetCombinedVersion: The case fixed in this commit for E-Tag.
Bug: T152266
Change-Id: Ice4ede5ea594bf3fa591134bc9382bd9c24e2f39
2016-12-03 00:48:14 +00:00
|
|
|
$mock = $this->getMockBuilder( ResourceLoaderTestModule::class )
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [ $getter ] )
|
resourceloader: Don't let module exception break startup
When getScript (or some other method used in a module response)
throws an error, only that module fails (by outputting mw.loader.state
instead of mw.loader.implement). Other modules will work.
This has always been the case and is working fine. For example,
"load.php?modules=foo|bar", where 'foo' throws, will return:
```js
/* exception message: .. */
mw.loader.implement('bar', ..)
mw.loader.state('foo', 'error')
```
The problem, however, is that during the generation of the startup
module, we iterate over all other modules. In 2011, the
getVersionHash method (then: getModifiedTime) was fairly simple
and unlikely to throw errors.
Nowadays, some modules use enableModuleContentVersion which will
involve the same code path as for regular module responses.
The try/catch in ResourceLoader::makeModuleResponse() suffices
for the case of loading modules other than startup. But when
loading the startup module, and an exception happens in getVersionHash,
then the entire startup response is replaced with an exception comment.
Example case:
* A file not existing for a FileModule subclass that uses
enableModuleContentVersion.
* A database error from a data module, like CiteDataModule or
CNChoiceData.
Changes:
* Ensure E-Tag is still useful while an error happens in production
because we respond with 200 OK and one error isn't the same as
another.
Fixed by try/catch in getCombinedVersion.
* Ensure start manifest isn't disrupted by one broken module.
Fixed by try/catch in StartupModule::getModuleRegistrations().
Tests:
* testMakeModuleResponseError: The case that already worked fined.
* testMakeModuleResponseStartupError: The case fixed in this commit.
* testGetCombinedVersion: The case fixed in this commit for E-Tag.
Bug: T152266
Change-Id: Ice4ede5ea594bf3fa591134bc9382bd9c24e2f39
2016-12-03 00:48:14 +00:00
|
|
|
->getMock();
|
2018-05-28 00:39:58 +00:00
|
|
|
$mock->method( $getter )->will( $this->throwException(
|
resourceloader: Don't let module exception break startup
When getScript (or some other method used in a module response)
throws an error, only that module fails (by outputting mw.loader.state
instead of mw.loader.implement). Other modules will work.
This has always been the case and is working fine. For example,
"load.php?modules=foo|bar", where 'foo' throws, will return:
```js
/* exception message: .. */
mw.loader.implement('bar', ..)
mw.loader.state('foo', 'error')
```
The problem, however, is that during the generation of the startup
module, we iterate over all other modules. In 2011, the
getVersionHash method (then: getModifiedTime) was fairly simple
and unlikely to throw errors.
Nowadays, some modules use enableModuleContentVersion which will
involve the same code path as for regular module responses.
The try/catch in ResourceLoader::makeModuleResponse() suffices
for the case of loading modules other than startup. But when
loading the startup module, and an exception happens in getVersionHash,
then the entire startup response is replaced with an exception comment.
Example case:
* A file not existing for a FileModule subclass that uses
enableModuleContentVersion.
* A database error from a data module, like CiteDataModule or
CNChoiceData.
Changes:
* Ensure E-Tag is still useful while an error happens in production
because we respond with 200 OK and one error isn't the same as
another.
Fixed by try/catch in getCombinedVersion.
* Ensure start manifest isn't disrupted by one broken module.
Fixed by try/catch in StartupModule::getModuleRegistrations().
Tests:
* testMakeModuleResponseError: The case that already worked fined.
* testMakeModuleResponseStartupError: The case fixed in this commit.
* testGetCombinedVersion: The case fixed in this commit for E-Tag.
Bug: T152266
Change-Id: Ice4ede5ea594bf3fa591134bc9382bd9c24e2f39
2016-12-03 00:48:14 +00:00
|
|
|
new Exception( 'Ferry not found' )
|
|
|
|
|
) );
|
|
|
|
|
return $mock;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getSimpleModuleMock( $script = '' ) {
|
|
|
|
|
$mock = $this->getMockBuilder( ResourceLoaderTestModule::class )
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [ 'getScript' ] )
|
resourceloader: Don't let module exception break startup
When getScript (or some other method used in a module response)
throws an error, only that module fails (by outputting mw.loader.state
instead of mw.loader.implement). Other modules will work.
This has always been the case and is working fine. For example,
"load.php?modules=foo|bar", where 'foo' throws, will return:
```js
/* exception message: .. */
mw.loader.implement('bar', ..)
mw.loader.state('foo', 'error')
```
The problem, however, is that during the generation of the startup
module, we iterate over all other modules. In 2011, the
getVersionHash method (then: getModifiedTime) was fairly simple
and unlikely to throw errors.
Nowadays, some modules use enableModuleContentVersion which will
involve the same code path as for regular module responses.
The try/catch in ResourceLoader::makeModuleResponse() suffices
for the case of loading modules other than startup. But when
loading the startup module, and an exception happens in getVersionHash,
then the entire startup response is replaced with an exception comment.
Example case:
* A file not existing for a FileModule subclass that uses
enableModuleContentVersion.
* A database error from a data module, like CiteDataModule or
CNChoiceData.
Changes:
* Ensure E-Tag is still useful while an error happens in production
because we respond with 200 OK and one error isn't the same as
another.
Fixed by try/catch in getCombinedVersion.
* Ensure start manifest isn't disrupted by one broken module.
Fixed by try/catch in StartupModule::getModuleRegistrations().
Tests:
* testMakeModuleResponseError: The case that already worked fined.
* testMakeModuleResponseStartupError: The case fixed in this commit.
* testGetCombinedVersion: The case fixed in this commit for E-Tag.
Bug: T152266
Change-Id: Ice4ede5ea594bf3fa591134bc9382bd9c24e2f39
2016-12-03 00:48:14 +00:00
|
|
|
->getMock();
|
|
|
|
|
$mock->method( 'getScript' )->willReturn( $script );
|
|
|
|
|
return $mock;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-28 00:39:58 +00:00
|
|
|
protected function getSimpleStyleModuleMock( $styles = '' ) {
|
|
|
|
|
$mock = $this->getMockBuilder( ResourceLoaderTestModule::class )
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [ 'getStyles' ] )
|
2018-05-28 00:39:58 +00:00
|
|
|
->getMock();
|
|
|
|
|
$mock->method( 'getStyles' )->willReturn( [ '' => $styles ] );
|
|
|
|
|
return $mock;
|
|
|
|
|
}
|
|
|
|
|
|
resourceloader: Don't let module exception break startup
When getScript (or some other method used in a module response)
throws an error, only that module fails (by outputting mw.loader.state
instead of mw.loader.implement). Other modules will work.
This has always been the case and is working fine. For example,
"load.php?modules=foo|bar", where 'foo' throws, will return:
```js
/* exception message: .. */
mw.loader.implement('bar', ..)
mw.loader.state('foo', 'error')
```
The problem, however, is that during the generation of the startup
module, we iterate over all other modules. In 2011, the
getVersionHash method (then: getModifiedTime) was fairly simple
and unlikely to throw errors.
Nowadays, some modules use enableModuleContentVersion which will
involve the same code path as for regular module responses.
The try/catch in ResourceLoader::makeModuleResponse() suffices
for the case of loading modules other than startup. But when
loading the startup module, and an exception happens in getVersionHash,
then the entire startup response is replaced with an exception comment.
Example case:
* A file not existing for a FileModule subclass that uses
enableModuleContentVersion.
* A database error from a data module, like CiteDataModule or
CNChoiceData.
Changes:
* Ensure E-Tag is still useful while an error happens in production
because we respond with 200 OK and one error isn't the same as
another.
Fixed by try/catch in getCombinedVersion.
* Ensure start manifest isn't disrupted by one broken module.
Fixed by try/catch in StartupModule::getModuleRegistrations().
Tests:
* testMakeModuleResponseError: The case that already worked fined.
* testMakeModuleResponseStartupError: The case fixed in this commit.
* testGetCombinedVersion: The case fixed in this commit for E-Tag.
Bug: T152266
Change-Id: Ice4ede5ea594bf3fa591134bc9382bd9c24e2f39
2016-12-03 00:48:14 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::getCombinedVersion
|
|
|
|
|
*/
|
|
|
|
|
public function testGetCombinedVersion() {
|
2017-07-13 05:16:53 +00:00
|
|
|
$rl = $this->getMockBuilder( EmptyResourceLoader::class )
|
|
|
|
|
// Disable log from outputErrorAndLog
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [ 'outputErrorAndLog' ] )->getMock();
|
resourceloader: Don't let module exception break startup
When getScript (or some other method used in a module response)
throws an error, only that module fails (by outputting mw.loader.state
instead of mw.loader.implement). Other modules will work.
This has always been the case and is working fine. For example,
"load.php?modules=foo|bar", where 'foo' throws, will return:
```js
/* exception message: .. */
mw.loader.implement('bar', ..)
mw.loader.state('foo', 'error')
```
The problem, however, is that during the generation of the startup
module, we iterate over all other modules. In 2011, the
getVersionHash method (then: getModifiedTime) was fairly simple
and unlikely to throw errors.
Nowadays, some modules use enableModuleContentVersion which will
involve the same code path as for regular module responses.
The try/catch in ResourceLoader::makeModuleResponse() suffices
for the case of loading modules other than startup. But when
loading the startup module, and an exception happens in getVersionHash,
then the entire startup response is replaced with an exception comment.
Example case:
* A file not existing for a FileModule subclass that uses
enableModuleContentVersion.
* A database error from a data module, like CiteDataModule or
CNChoiceData.
Changes:
* Ensure E-Tag is still useful while an error happens in production
because we respond with 200 OK and one error isn't the same as
another.
Fixed by try/catch in getCombinedVersion.
* Ensure start manifest isn't disrupted by one broken module.
Fixed by try/catch in StartupModule::getModuleRegistrations().
Tests:
* testMakeModuleResponseError: The case that already worked fined.
* testMakeModuleResponseStartupError: The case fixed in this commit.
* testGetCombinedVersion: The case fixed in this commit for E-Tag.
Bug: T152266
Change-Id: Ice4ede5ea594bf3fa591134bc9382bd9c24e2f39
2016-12-03 00:48:14 +00:00
|
|
|
$rl->register( [
|
2019-07-11 19:48:57 +00:00
|
|
|
'foo' => [ 'class' => ResourceLoaderTestModule::class ],
|
|
|
|
|
'ferry' => [
|
|
|
|
|
'factory' => function () {
|
2021-02-07 13:39:00 +00:00
|
|
|
return $this->getFailFerryMock();
|
2019-07-11 19:48:57 +00:00
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
'bar' => [ 'class' => ResourceLoaderTestModule::class ],
|
resourceloader: Don't let module exception break startup
When getScript (or some other method used in a module response)
throws an error, only that module fails (by outputting mw.loader.state
instead of mw.loader.implement). Other modules will work.
This has always been the case and is working fine. For example,
"load.php?modules=foo|bar", where 'foo' throws, will return:
```js
/* exception message: .. */
mw.loader.implement('bar', ..)
mw.loader.state('foo', 'error')
```
The problem, however, is that during the generation of the startup
module, we iterate over all other modules. In 2011, the
getVersionHash method (then: getModifiedTime) was fairly simple
and unlikely to throw errors.
Nowadays, some modules use enableModuleContentVersion which will
involve the same code path as for regular module responses.
The try/catch in ResourceLoader::makeModuleResponse() suffices
for the case of loading modules other than startup. But when
loading the startup module, and an exception happens in getVersionHash,
then the entire startup response is replaced with an exception comment.
Example case:
* A file not existing for a FileModule subclass that uses
enableModuleContentVersion.
* A database error from a data module, like CiteDataModule or
CNChoiceData.
Changes:
* Ensure E-Tag is still useful while an error happens in production
because we respond with 200 OK and one error isn't the same as
another.
Fixed by try/catch in getCombinedVersion.
* Ensure start manifest isn't disrupted by one broken module.
Fixed by try/catch in StartupModule::getModuleRegistrations().
Tests:
* testMakeModuleResponseError: The case that already worked fined.
* testMakeModuleResponseStartupError: The case fixed in this commit.
* testGetCombinedVersion: The case fixed in this commit for E-Tag.
Bug: T152266
Change-Id: Ice4ede5ea594bf3fa591134bc9382bd9c24e2f39
2016-12-03 00:48:14 +00:00
|
|
|
] );
|
resourceloader: Skip version hash calculation in debug mode
=== Why
* More speed
In debug mode, the server should regenerate the startup manifest
on each page view to ensure immediate effect of changes. But,
this also means more version recomputation work on the server.
For most modules, this was already quite fast on repeat views
because of OS-level file caches, and our file-hash caches and
LESS compile caches in php-apcu from ResourceLoader.
But, this makes it even faster.
* Better integration with browser devtools.
Breakpoints stay more consistently across browsers when the
URL stays the same even after you have changed the file and
reloaded the page. For static files, I believe most browsers ignore
query parameters. But for package files that come from load.php,
this was harder for browsers to guess correctly which old script URL
is logically replaced by a different one on the next page view.
=== How
Change Module::getVersionHash to return empty strings in debug mode.
I considered approaching this from StartupModule::getModuleRegistrations
instead to make the change apply only to the client-side manifest.
I decided against this because we have other calls to getVersionHash
on the server-side (such as for E-Tag calculation, and formatting
cross-wiki URLs) which would then not match the version queries that
mw.loader formats in debug mode.
Also, those calls would still be incurring some the avoidable costs.
=== Notes
* The two test cases for verifying the graceful fallback in production
if version hash computations throw an exception, were moved to a
non-debug test case as no longer happen now during the debug
(unminified) test cases.
* Avoid "PHP Notice: Undefined offset 0" in testMakeModuleResponseStartupError
by adding a fallback to empty string so that if the test fails,
it fails in a more useful way instead of aborting with this error
before the assertion happens. (Since PHPUnit generally stops on the
first error.)
* In practice, there are still "version" query parameters and E-Tag
headers in debug mode. These are not module versions, but URL
"combined versions" crafted by getCombinedVersion() in JS and PHP.
These return the constant "ztntf" in debug mode, which is the hash
of an empty string. We could alter these methods to special-case
when all inputs are and join to a still-empty string, or maybe we
just leave them be. I've done the latter for now.
Bug: T235672
Bug: T85805
Change-Id: I0e63eef4f85b13089a0aa3806a5b6f821d527a92
2021-08-28 02:53:36 +00:00
|
|
|
$context = $this->getResourceLoaderContext( [ 'debug' => 'false' ], $rl );
|
resourceloader: Don't let module exception break startup
When getScript (or some other method used in a module response)
throws an error, only that module fails (by outputting mw.loader.state
instead of mw.loader.implement). Other modules will work.
This has always been the case and is working fine. For example,
"load.php?modules=foo|bar", where 'foo' throws, will return:
```js
/* exception message: .. */
mw.loader.implement('bar', ..)
mw.loader.state('foo', 'error')
```
The problem, however, is that during the generation of the startup
module, we iterate over all other modules. In 2011, the
getVersionHash method (then: getModifiedTime) was fairly simple
and unlikely to throw errors.
Nowadays, some modules use enableModuleContentVersion which will
involve the same code path as for regular module responses.
The try/catch in ResourceLoader::makeModuleResponse() suffices
for the case of loading modules other than startup. But when
loading the startup module, and an exception happens in getVersionHash,
then the entire startup response is replaced with an exception comment.
Example case:
* A file not existing for a FileModule subclass that uses
enableModuleContentVersion.
* A database error from a data module, like CiteDataModule or
CNChoiceData.
Changes:
* Ensure E-Tag is still useful while an error happens in production
because we respond with 200 OK and one error isn't the same as
another.
Fixed by try/catch in getCombinedVersion.
* Ensure start manifest isn't disrupted by one broken module.
Fixed by try/catch in StartupModule::getModuleRegistrations().
Tests:
* testMakeModuleResponseError: The case that already worked fined.
* testMakeModuleResponseStartupError: The case fixed in this commit.
* testGetCombinedVersion: The case fixed in this commit for E-Tag.
Bug: T152266
Change-Id: Ice4ede5ea594bf3fa591134bc9382bd9c24e2f39
2016-12-03 00:48:14 +00:00
|
|
|
|
2019-09-17 14:28:35 +00:00
|
|
|
$this->assertSame(
|
2017-06-28 02:10:03 +00:00
|
|
|
'',
|
|
|
|
|
$rl->getCombinedVersion( $context, [] ),
|
|
|
|
|
'empty list'
|
|
|
|
|
);
|
|
|
|
|
|
resourceloader: Don't let module exception break startup
When getScript (or some other method used in a module response)
throws an error, only that module fails (by outputting mw.loader.state
instead of mw.loader.implement). Other modules will work.
This has always been the case and is working fine. For example,
"load.php?modules=foo|bar", where 'foo' throws, will return:
```js
/* exception message: .. */
mw.loader.implement('bar', ..)
mw.loader.state('foo', 'error')
```
The problem, however, is that during the generation of the startup
module, we iterate over all other modules. In 2011, the
getVersionHash method (then: getModifiedTime) was fairly simple
and unlikely to throw errors.
Nowadays, some modules use enableModuleContentVersion which will
involve the same code path as for regular module responses.
The try/catch in ResourceLoader::makeModuleResponse() suffices
for the case of loading modules other than startup. But when
loading the startup module, and an exception happens in getVersionHash,
then the entire startup response is replaced with an exception comment.
Example case:
* A file not existing for a FileModule subclass that uses
enableModuleContentVersion.
* A database error from a data module, like CiteDataModule or
CNChoiceData.
Changes:
* Ensure E-Tag is still useful while an error happens in production
because we respond with 200 OK and one error isn't the same as
another.
Fixed by try/catch in getCombinedVersion.
* Ensure start manifest isn't disrupted by one broken module.
Fixed by try/catch in StartupModule::getModuleRegistrations().
Tests:
* testMakeModuleResponseError: The case that already worked fined.
* testMakeModuleResponseStartupError: The case fixed in this commit.
* testGetCombinedVersion: The case fixed in this commit for E-Tag.
Bug: T152266
Change-Id: Ice4ede5ea594bf3fa591134bc9382bd9c24e2f39
2016-12-03 00:48:14 +00:00
|
|
|
$this->assertEquals(
|
2019-07-29 16:15:23 +00:00
|
|
|
self::BLANK_COMBI,
|
resourceloader: Don't let module exception break startup
When getScript (or some other method used in a module response)
throws an error, only that module fails (by outputting mw.loader.state
instead of mw.loader.implement). Other modules will work.
This has always been the case and is working fine. For example,
"load.php?modules=foo|bar", where 'foo' throws, will return:
```js
/* exception message: .. */
mw.loader.implement('bar', ..)
mw.loader.state('foo', 'error')
```
The problem, however, is that during the generation of the startup
module, we iterate over all other modules. In 2011, the
getVersionHash method (then: getModifiedTime) was fairly simple
and unlikely to throw errors.
Nowadays, some modules use enableModuleContentVersion which will
involve the same code path as for regular module responses.
The try/catch in ResourceLoader::makeModuleResponse() suffices
for the case of loading modules other than startup. But when
loading the startup module, and an exception happens in getVersionHash,
then the entire startup response is replaced with an exception comment.
Example case:
* A file not existing for a FileModule subclass that uses
enableModuleContentVersion.
* A database error from a data module, like CiteDataModule or
CNChoiceData.
Changes:
* Ensure E-Tag is still useful while an error happens in production
because we respond with 200 OK and one error isn't the same as
another.
Fixed by try/catch in getCombinedVersion.
* Ensure start manifest isn't disrupted by one broken module.
Fixed by try/catch in StartupModule::getModuleRegistrations().
Tests:
* testMakeModuleResponseError: The case that already worked fined.
* testMakeModuleResponseStartupError: The case fixed in this commit.
* testGetCombinedVersion: The case fixed in this commit for E-Tag.
Bug: T152266
Change-Id: Ice4ede5ea594bf3fa591134bc9382bd9c24e2f39
2016-12-03 00:48:14 +00:00
|
|
|
$rl->getCombinedVersion( $context, [ 'foo' ] ),
|
|
|
|
|
'compute foo'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Verify that getCombinedVersion() does not throw when ferry fails.
|
|
|
|
|
// Instead it gracefully continues to combine the remaining modules.
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
ResourceLoader::makeHash( self::BLANK_VERSION . self::BLANK_VERSION ),
|
|
|
|
|
$rl->getCombinedVersion( $context, [ 'foo', 'ferry', 'bar' ] ),
|
|
|
|
|
'compute foo+ferry+bar (T152266)'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-28 02:10:03 +00:00
|
|
|
public static function provideMakeModuleResponseConcat() {
|
|
|
|
|
$testcases = [
|
|
|
|
|
[
|
|
|
|
|
'modules' => [
|
|
|
|
|
'foo' => 'foo()',
|
|
|
|
|
],
|
2019-07-16 00:15:32 +00:00
|
|
|
'expected' => "foo()\n" . 'mw.loader.state({
|
2017-06-28 02:10:03 +00:00
|
|
|
"foo": "ready"
|
2019-07-16 00:15:32 +00:00
|
|
|
});',
|
2017-06-28 02:51:03 +00:00
|
|
|
'minified' => "foo()\n" . 'mw.loader.state({"foo":"ready"});',
|
2017-06-28 02:10:03 +00:00
|
|
|
'message' => 'Script without semi-colon',
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'modules' => [
|
|
|
|
|
'foo' => 'foo()',
|
|
|
|
|
'bar' => 'bar()',
|
|
|
|
|
],
|
2019-07-16 00:15:32 +00:00
|
|
|
'expected' => "foo()\nbar()\n" . 'mw.loader.state({
|
2017-06-28 02:10:03 +00:00
|
|
|
"foo": "ready",
|
|
|
|
|
"bar": "ready"
|
2019-07-16 00:15:32 +00:00
|
|
|
});',
|
2017-06-28 02:51:03 +00:00
|
|
|
'minified' => "foo()\nbar()\n" . 'mw.loader.state({"foo":"ready","bar":"ready"});',
|
2017-06-28 02:10:03 +00:00
|
|
|
'message' => 'Two scripts without semi-colon',
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'modules' => [
|
|
|
|
|
'foo' => "foo()\n// bar();"
|
|
|
|
|
],
|
2019-07-16 00:15:32 +00:00
|
|
|
'expected' => "foo()\n// bar();\n" . 'mw.loader.state({
|
2017-06-28 02:10:03 +00:00
|
|
|
"foo": "ready"
|
2019-07-16 00:15:32 +00:00
|
|
|
});',
|
2017-06-28 02:51:03 +00:00
|
|
|
'minified' => "foo()\n" . 'mw.loader.state({"foo":"ready"});',
|
|
|
|
|
'message' => 'Script with semi-colon in comment (T162719)',
|
2017-06-28 02:10:03 +00:00
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
$ret = [];
|
|
|
|
|
foreach ( $testcases as $i => $case ) {
|
|
|
|
|
$ret["#$i"] = [
|
|
|
|
|
$case['modules'],
|
|
|
|
|
$case['expected'],
|
|
|
|
|
true, // debug
|
|
|
|
|
$case['message'],
|
|
|
|
|
];
|
|
|
|
|
$ret["#$i (minified)"] = [
|
|
|
|
|
$case['modules'],
|
|
|
|
|
$case['minified'],
|
|
|
|
|
false, // debug
|
|
|
|
|
$case['message'],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Verify how multiple scripts and mw.loader.state() calls are concatenated.
|
|
|
|
|
*
|
|
|
|
|
* @dataProvider provideMakeModuleResponseConcat
|
|
|
|
|
* @covers ResourceLoader::makeModuleResponse
|
|
|
|
|
*/
|
|
|
|
|
public function testMakeModuleResponseConcat( $scripts, $expected, $debug, $message = null ) {
|
|
|
|
|
$rl = new EmptyResourceLoader();
|
|
|
|
|
$modules = array_map( function ( $script ) {
|
2021-02-07 13:39:00 +00:00
|
|
|
return $this->getSimpleModuleMock( $script );
|
2017-06-28 02:10:03 +00:00
|
|
|
}, $scripts );
|
|
|
|
|
|
|
|
|
|
$context = $this->getResourceLoaderContext(
|
|
|
|
|
[
|
|
|
|
|
'modules' => implode( '|', array_keys( $modules ) ),
|
|
|
|
|
'only' => 'scripts',
|
resourceloader: Replace ResourceLoaderDebug config use with context
Reduce our reliance on static state and configuration, and
propagate more state in explicit ways, through context, and
request parameters.
OutputPage creates ResourceLoaderContext and ResourceLoaderClientHtml
based on the configuration (via ResourceLoader::inDebugMode).
Everything within those classes should not need to check it
again.
* ResourceLoaderClientHtml:
Already doesn't check MW config, but it's test was still
mocking it. Removed now, and confirmed that it passes both
with true and false. The individual test cases set
debug=true/false as needed already.
It's sets were previously relying on the accidental behaviour
that within a unit test, we don't serialise over HTTP, which
meant that a pure PHP boolean would survive. With the new
raw `=== 'true'` check, this no longer works. Set it as a
string explicitly instead, which is the only thing we support
outside unit tests as well.
* ResourceLoaderContext:
Remove fallback to MW config when 'debug' is unset.
This is never unset in practice given that all load.php
urls have it set by OutputPage based on ResourceLoader::inDebugMode.
This change means that manually constructed ad-hoc load.php
urls that are missing 'debug=' parameter, will now always be
read as debug=false. This was the default already, but could
previously be changed through wgResourceLoaderDebug.
When changing wgResourceLoaderDebug, everything will still have
debug=true as before. The only change is when constructing load.php
urls manually and explicitly not set it.
Bug: T32956
Change-Id: Ie3424be46e2b8311968f3068ca08ba6a1139224a
2019-03-08 20:33:04 +00:00
|
|
|
'debug' => $debug ? 'true' : 'false',
|
2017-06-28 02:10:03 +00:00
|
|
|
],
|
|
|
|
|
$rl
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$response = $rl->makeModuleResponse( $context, $modules );
|
2017-06-28 02:51:03 +00:00
|
|
|
$this->assertSame( [], $rl->getErrors(), 'Errors' );
|
2017-06-28 02:10:03 +00:00
|
|
|
$this->assertEquals( $expected, $response, $message ?: 'Response' );
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-29 02:03:31 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::makeModuleResponse
|
|
|
|
|
*/
|
|
|
|
|
public function testMakeModuleResponseEmpty() {
|
|
|
|
|
$rl = new EmptyResourceLoader();
|
|
|
|
|
$context = $this->getResourceLoaderContext(
|
|
|
|
|
[ 'modules' => '', 'only' => 'scripts' ],
|
|
|
|
|
$rl
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$response = $rl->makeModuleResponse( $context, [] );
|
|
|
|
|
$this->assertSame( [], $rl->getErrors(), 'Errors' );
|
|
|
|
|
$this->assertRegExp( '/^\/\*.+no modules were requested.+\*\/$/ms', $response );
|
|
|
|
|
}
|
|
|
|
|
|
resourceloader: Don't let module exception break startup
When getScript (or some other method used in a module response)
throws an error, only that module fails (by outputting mw.loader.state
instead of mw.loader.implement). Other modules will work.
This has always been the case and is working fine. For example,
"load.php?modules=foo|bar", where 'foo' throws, will return:
```js
/* exception message: .. */
mw.loader.implement('bar', ..)
mw.loader.state('foo', 'error')
```
The problem, however, is that during the generation of the startup
module, we iterate over all other modules. In 2011, the
getVersionHash method (then: getModifiedTime) was fairly simple
and unlikely to throw errors.
Nowadays, some modules use enableModuleContentVersion which will
involve the same code path as for regular module responses.
The try/catch in ResourceLoader::makeModuleResponse() suffices
for the case of loading modules other than startup. But when
loading the startup module, and an exception happens in getVersionHash,
then the entire startup response is replaced with an exception comment.
Example case:
* A file not existing for a FileModule subclass that uses
enableModuleContentVersion.
* A database error from a data module, like CiteDataModule or
CNChoiceData.
Changes:
* Ensure E-Tag is still useful while an error happens in production
because we respond with 200 OK and one error isn't the same as
another.
Fixed by try/catch in getCombinedVersion.
* Ensure start manifest isn't disrupted by one broken module.
Fixed by try/catch in StartupModule::getModuleRegistrations().
Tests:
* testMakeModuleResponseError: The case that already worked fined.
* testMakeModuleResponseStartupError: The case fixed in this commit.
* testGetCombinedVersion: The case fixed in this commit for E-Tag.
Bug: T152266
Change-Id: Ice4ede5ea594bf3fa591134bc9382bd9c24e2f39
2016-12-03 00:48:14 +00:00
|
|
|
/**
|
|
|
|
|
* Verify that when building module content in a load.php response,
|
|
|
|
|
* an exception from one module will not break script output from
|
|
|
|
|
* other modules.
|
2017-06-27 04:44:11 +00:00
|
|
|
*
|
|
|
|
|
* @covers ResourceLoader::makeModuleResponse
|
resourceloader: Don't let module exception break startup
When getScript (or some other method used in a module response)
throws an error, only that module fails (by outputting mw.loader.state
instead of mw.loader.implement). Other modules will work.
This has always been the case and is working fine. For example,
"load.php?modules=foo|bar", where 'foo' throws, will return:
```js
/* exception message: .. */
mw.loader.implement('bar', ..)
mw.loader.state('foo', 'error')
```
The problem, however, is that during the generation of the startup
module, we iterate over all other modules. In 2011, the
getVersionHash method (then: getModifiedTime) was fairly simple
and unlikely to throw errors.
Nowadays, some modules use enableModuleContentVersion which will
involve the same code path as for regular module responses.
The try/catch in ResourceLoader::makeModuleResponse() suffices
for the case of loading modules other than startup. But when
loading the startup module, and an exception happens in getVersionHash,
then the entire startup response is replaced with an exception comment.
Example case:
* A file not existing for a FileModule subclass that uses
enableModuleContentVersion.
* A database error from a data module, like CiteDataModule or
CNChoiceData.
Changes:
* Ensure E-Tag is still useful while an error happens in production
because we respond with 200 OK and one error isn't the same as
another.
Fixed by try/catch in getCombinedVersion.
* Ensure start manifest isn't disrupted by one broken module.
Fixed by try/catch in StartupModule::getModuleRegistrations().
Tests:
* testMakeModuleResponseError: The case that already worked fined.
* testMakeModuleResponseStartupError: The case fixed in this commit.
* testGetCombinedVersion: The case fixed in this commit for E-Tag.
Bug: T152266
Change-Id: Ice4ede5ea594bf3fa591134bc9382bd9c24e2f39
2016-12-03 00:48:14 +00:00
|
|
|
*/
|
|
|
|
|
public function testMakeModuleResponseError() {
|
|
|
|
|
$modules = [
|
2021-02-07 13:39:00 +00:00
|
|
|
'foo' => $this->getSimpleModuleMock( 'foo();' ),
|
|
|
|
|
'ferry' => $this->getFailFerryMock(),
|
|
|
|
|
'bar' => $this->getSimpleModuleMock( 'bar();' ),
|
resourceloader: Don't let module exception break startup
When getScript (or some other method used in a module response)
throws an error, only that module fails (by outputting mw.loader.state
instead of mw.loader.implement). Other modules will work.
This has always been the case and is working fine. For example,
"load.php?modules=foo|bar", where 'foo' throws, will return:
```js
/* exception message: .. */
mw.loader.implement('bar', ..)
mw.loader.state('foo', 'error')
```
The problem, however, is that during the generation of the startup
module, we iterate over all other modules. In 2011, the
getVersionHash method (then: getModifiedTime) was fairly simple
and unlikely to throw errors.
Nowadays, some modules use enableModuleContentVersion which will
involve the same code path as for regular module responses.
The try/catch in ResourceLoader::makeModuleResponse() suffices
for the case of loading modules other than startup. But when
loading the startup module, and an exception happens in getVersionHash,
then the entire startup response is replaced with an exception comment.
Example case:
* A file not existing for a FileModule subclass that uses
enableModuleContentVersion.
* A database error from a data module, like CiteDataModule or
CNChoiceData.
Changes:
* Ensure E-Tag is still useful while an error happens in production
because we respond with 200 OK and one error isn't the same as
another.
Fixed by try/catch in getCombinedVersion.
* Ensure start manifest isn't disrupted by one broken module.
Fixed by try/catch in StartupModule::getModuleRegistrations().
Tests:
* testMakeModuleResponseError: The case that already worked fined.
* testMakeModuleResponseStartupError: The case fixed in this commit.
* testGetCombinedVersion: The case fixed in this commit for E-Tag.
Bug: T152266
Change-Id: Ice4ede5ea594bf3fa591134bc9382bd9c24e2f39
2016-12-03 00:48:14 +00:00
|
|
|
];
|
|
|
|
|
$rl = new EmptyResourceLoader();
|
|
|
|
|
$context = $this->getResourceLoaderContext(
|
|
|
|
|
[
|
|
|
|
|
'modules' => 'foo|ferry|bar',
|
|
|
|
|
'only' => 'scripts',
|
|
|
|
|
],
|
|
|
|
|
$rl
|
|
|
|
|
);
|
|
|
|
|
|
2017-07-13 05:16:53 +00:00
|
|
|
// Disable log from makeModuleResponse via outputErrorAndLog
|
|
|
|
|
$this->setLogger( 'exception', new Psr\Log\NullLogger() );
|
|
|
|
|
|
resourceloader: Don't let module exception break startup
When getScript (or some other method used in a module response)
throws an error, only that module fails (by outputting mw.loader.state
instead of mw.loader.implement). Other modules will work.
This has always been the case and is working fine. For example,
"load.php?modules=foo|bar", where 'foo' throws, will return:
```js
/* exception message: .. */
mw.loader.implement('bar', ..)
mw.loader.state('foo', 'error')
```
The problem, however, is that during the generation of the startup
module, we iterate over all other modules. In 2011, the
getVersionHash method (then: getModifiedTime) was fairly simple
and unlikely to throw errors.
Nowadays, some modules use enableModuleContentVersion which will
involve the same code path as for regular module responses.
The try/catch in ResourceLoader::makeModuleResponse() suffices
for the case of loading modules other than startup. But when
loading the startup module, and an exception happens in getVersionHash,
then the entire startup response is replaced with an exception comment.
Example case:
* A file not existing for a FileModule subclass that uses
enableModuleContentVersion.
* A database error from a data module, like CiteDataModule or
CNChoiceData.
Changes:
* Ensure E-Tag is still useful while an error happens in production
because we respond with 200 OK and one error isn't the same as
another.
Fixed by try/catch in getCombinedVersion.
* Ensure start manifest isn't disrupted by one broken module.
Fixed by try/catch in StartupModule::getModuleRegistrations().
Tests:
* testMakeModuleResponseError: The case that already worked fined.
* testMakeModuleResponseStartupError: The case fixed in this commit.
* testGetCombinedVersion: The case fixed in this commit for E-Tag.
Bug: T152266
Change-Id: Ice4ede5ea594bf3fa591134bc9382bd9c24e2f39
2016-12-03 00:48:14 +00:00
|
|
|
$response = $rl->makeModuleResponse( $context, $modules );
|
|
|
|
|
$errors = $rl->getErrors();
|
|
|
|
|
|
|
|
|
|
$this->assertCount( 1, $errors );
|
|
|
|
|
$this->assertRegExp( '/Ferry not found/', $errors[0] );
|
|
|
|
|
$this->assertEquals(
|
2019-07-16 00:15:32 +00:00
|
|
|
"foo();\nbar();\n" . 'mw.loader.state({
|
resourceloader: Don't let module exception break startup
When getScript (or some other method used in a module response)
throws an error, only that module fails (by outputting mw.loader.state
instead of mw.loader.implement). Other modules will work.
This has always been the case and is working fine. For example,
"load.php?modules=foo|bar", where 'foo' throws, will return:
```js
/* exception message: .. */
mw.loader.implement('bar', ..)
mw.loader.state('foo', 'error')
```
The problem, however, is that during the generation of the startup
module, we iterate over all other modules. In 2011, the
getVersionHash method (then: getModifiedTime) was fairly simple
and unlikely to throw errors.
Nowadays, some modules use enableModuleContentVersion which will
involve the same code path as for regular module responses.
The try/catch in ResourceLoader::makeModuleResponse() suffices
for the case of loading modules other than startup. But when
loading the startup module, and an exception happens in getVersionHash,
then the entire startup response is replaced with an exception comment.
Example case:
* A file not existing for a FileModule subclass that uses
enableModuleContentVersion.
* A database error from a data module, like CiteDataModule or
CNChoiceData.
Changes:
* Ensure E-Tag is still useful while an error happens in production
because we respond with 200 OK and one error isn't the same as
another.
Fixed by try/catch in getCombinedVersion.
* Ensure start manifest isn't disrupted by one broken module.
Fixed by try/catch in StartupModule::getModuleRegistrations().
Tests:
* testMakeModuleResponseError: The case that already worked fined.
* testMakeModuleResponseStartupError: The case fixed in this commit.
* testGetCombinedVersion: The case fixed in this commit for E-Tag.
Bug: T152266
Change-Id: Ice4ede5ea594bf3fa591134bc9382bd9c24e2f39
2016-12-03 00:48:14 +00:00
|
|
|
"ferry": "error",
|
|
|
|
|
"foo": "ready",
|
|
|
|
|
"bar": "ready"
|
2019-07-16 00:15:32 +00:00
|
|
|
});',
|
resourceloader: Don't let module exception break startup
When getScript (or some other method used in a module response)
throws an error, only that module fails (by outputting mw.loader.state
instead of mw.loader.implement). Other modules will work.
This has always been the case and is working fine. For example,
"load.php?modules=foo|bar", where 'foo' throws, will return:
```js
/* exception message: .. */
mw.loader.implement('bar', ..)
mw.loader.state('foo', 'error')
```
The problem, however, is that during the generation of the startup
module, we iterate over all other modules. In 2011, the
getVersionHash method (then: getModifiedTime) was fairly simple
and unlikely to throw errors.
Nowadays, some modules use enableModuleContentVersion which will
involve the same code path as for regular module responses.
The try/catch in ResourceLoader::makeModuleResponse() suffices
for the case of loading modules other than startup. But when
loading the startup module, and an exception happens in getVersionHash,
then the entire startup response is replaced with an exception comment.
Example case:
* A file not existing for a FileModule subclass that uses
enableModuleContentVersion.
* A database error from a data module, like CiteDataModule or
CNChoiceData.
Changes:
* Ensure E-Tag is still useful while an error happens in production
because we respond with 200 OK and one error isn't the same as
another.
Fixed by try/catch in getCombinedVersion.
* Ensure start manifest isn't disrupted by one broken module.
Fixed by try/catch in StartupModule::getModuleRegistrations().
Tests:
* testMakeModuleResponseError: The case that already worked fined.
* testMakeModuleResponseStartupError: The case fixed in this commit.
* testGetCombinedVersion: The case fixed in this commit for E-Tag.
Bug: T152266
Change-Id: Ice4ede5ea594bf3fa591134bc9382bd9c24e2f39
2016-12-03 00:48:14 +00:00
|
|
|
$response
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-28 00:39:58 +00:00
|
|
|
/**
|
|
|
|
|
* Verify that exceptions in PHP for one module will not break others
|
|
|
|
|
* (stylesheet response).
|
|
|
|
|
*
|
|
|
|
|
* @covers ResourceLoader::makeModuleResponse
|
|
|
|
|
*/
|
|
|
|
|
public function testMakeModuleResponseErrorCSS() {
|
|
|
|
|
$modules = [
|
|
|
|
|
'foo' => self::getSimpleStyleModuleMock( '.foo{}' ),
|
2021-02-07 13:39:00 +00:00
|
|
|
'ferry' => $this->getFailFerryMock( 'getStyles' ),
|
2018-05-28 00:39:58 +00:00
|
|
|
'bar' => self::getSimpleStyleModuleMock( '.bar{}' ),
|
|
|
|
|
];
|
|
|
|
|
$rl = new EmptyResourceLoader();
|
|
|
|
|
$context = $this->getResourceLoaderContext(
|
|
|
|
|
[
|
|
|
|
|
'modules' => 'foo|ferry|bar',
|
|
|
|
|
'only' => 'styles',
|
|
|
|
|
'debug' => 'false',
|
|
|
|
|
],
|
|
|
|
|
$rl
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Disable log from makeModuleResponse via outputErrorAndLog
|
|
|
|
|
$this->setLogger( 'exception', new Psr\Log\NullLogger() );
|
|
|
|
|
|
|
|
|
|
$response = $rl->makeModuleResponse( $context, $modules );
|
|
|
|
|
$errors = $rl->getErrors();
|
|
|
|
|
|
|
|
|
|
$this->assertCount( 2, $errors );
|
|
|
|
|
$this->assertRegExp( '/Ferry not found/', $errors[0] );
|
2018-05-29 02:03:31 +00:00
|
|
|
$this->assertRegExp( '/Problem.+"ferry":\s*"error"/ms', $errors[1] );
|
2018-05-28 00:39:58 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
'.foo{}.bar{}',
|
|
|
|
|
$response
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-01-15 15:04:58 +00:00
|
|
|
|
resourceloader: Don't let module exception break startup
When getScript (or some other method used in a module response)
throws an error, only that module fails (by outputting mw.loader.state
instead of mw.loader.implement). Other modules will work.
This has always been the case and is working fine. For example,
"load.php?modules=foo|bar", where 'foo' throws, will return:
```js
/* exception message: .. */
mw.loader.implement('bar', ..)
mw.loader.state('foo', 'error')
```
The problem, however, is that during the generation of the startup
module, we iterate over all other modules. In 2011, the
getVersionHash method (then: getModifiedTime) was fairly simple
and unlikely to throw errors.
Nowadays, some modules use enableModuleContentVersion which will
involve the same code path as for regular module responses.
The try/catch in ResourceLoader::makeModuleResponse() suffices
for the case of loading modules other than startup. But when
loading the startup module, and an exception happens in getVersionHash,
then the entire startup response is replaced with an exception comment.
Example case:
* A file not existing for a FileModule subclass that uses
enableModuleContentVersion.
* A database error from a data module, like CiteDataModule or
CNChoiceData.
Changes:
* Ensure E-Tag is still useful while an error happens in production
because we respond with 200 OK and one error isn't the same as
another.
Fixed by try/catch in getCombinedVersion.
* Ensure start manifest isn't disrupted by one broken module.
Fixed by try/catch in StartupModule::getModuleRegistrations().
Tests:
* testMakeModuleResponseError: The case that already worked fined.
* testMakeModuleResponseStartupError: The case fixed in this commit.
* testGetCombinedVersion: The case fixed in this commit for E-Tag.
Bug: T152266
Change-Id: Ice4ede5ea594bf3fa591134bc9382bd9c24e2f39
2016-12-03 00:48:14 +00:00
|
|
|
/**
|
|
|
|
|
* Verify that when building the startup module response,
|
|
|
|
|
* an exception from one module class will not break the entire
|
|
|
|
|
* startup module response. See T152266.
|
2017-06-27 04:44:11 +00:00
|
|
|
*
|
|
|
|
|
* @covers ResourceLoader::makeModuleResponse
|
resourceloader: Don't let module exception break startup
When getScript (or some other method used in a module response)
throws an error, only that module fails (by outputting mw.loader.state
instead of mw.loader.implement). Other modules will work.
This has always been the case and is working fine. For example,
"load.php?modules=foo|bar", where 'foo' throws, will return:
```js
/* exception message: .. */
mw.loader.implement('bar', ..)
mw.loader.state('foo', 'error')
```
The problem, however, is that during the generation of the startup
module, we iterate over all other modules. In 2011, the
getVersionHash method (then: getModifiedTime) was fairly simple
and unlikely to throw errors.
Nowadays, some modules use enableModuleContentVersion which will
involve the same code path as for regular module responses.
The try/catch in ResourceLoader::makeModuleResponse() suffices
for the case of loading modules other than startup. But when
loading the startup module, and an exception happens in getVersionHash,
then the entire startup response is replaced with an exception comment.
Example case:
* A file not existing for a FileModule subclass that uses
enableModuleContentVersion.
* A database error from a data module, like CiteDataModule or
CNChoiceData.
Changes:
* Ensure E-Tag is still useful while an error happens in production
because we respond with 200 OK and one error isn't the same as
another.
Fixed by try/catch in getCombinedVersion.
* Ensure start manifest isn't disrupted by one broken module.
Fixed by try/catch in StartupModule::getModuleRegistrations().
Tests:
* testMakeModuleResponseError: The case that already worked fined.
* testMakeModuleResponseStartupError: The case fixed in this commit.
* testGetCombinedVersion: The case fixed in this commit for E-Tag.
Bug: T152266
Change-Id: Ice4ede5ea594bf3fa591134bc9382bd9c24e2f39
2016-12-03 00:48:14 +00:00
|
|
|
*/
|
|
|
|
|
public function testMakeModuleResponseStartupError() {
|
2019-06-10 15:00:16 +00:00
|
|
|
// This is an integration test that uses a lot of MediaWiki state,
|
|
|
|
|
// provide the full Config object here.
|
|
|
|
|
$rl = new EmptyResourceLoader( MediaWikiServices::getInstance()->getMainConfig() );
|
resourceloader: Don't let module exception break startup
When getScript (or some other method used in a module response)
throws an error, only that module fails (by outputting mw.loader.state
instead of mw.loader.implement). Other modules will work.
This has always been the case and is working fine. For example,
"load.php?modules=foo|bar", where 'foo' throws, will return:
```js
/* exception message: .. */
mw.loader.implement('bar', ..)
mw.loader.state('foo', 'error')
```
The problem, however, is that during the generation of the startup
module, we iterate over all other modules. In 2011, the
getVersionHash method (then: getModifiedTime) was fairly simple
and unlikely to throw errors.
Nowadays, some modules use enableModuleContentVersion which will
involve the same code path as for regular module responses.
The try/catch in ResourceLoader::makeModuleResponse() suffices
for the case of loading modules other than startup. But when
loading the startup module, and an exception happens in getVersionHash,
then the entire startup response is replaced with an exception comment.
Example case:
* A file not existing for a FileModule subclass that uses
enableModuleContentVersion.
* A database error from a data module, like CiteDataModule or
CNChoiceData.
Changes:
* Ensure E-Tag is still useful while an error happens in production
because we respond with 200 OK and one error isn't the same as
another.
Fixed by try/catch in getCombinedVersion.
* Ensure start manifest isn't disrupted by one broken module.
Fixed by try/catch in StartupModule::getModuleRegistrations().
Tests:
* testMakeModuleResponseError: The case that already worked fined.
* testMakeModuleResponseStartupError: The case fixed in this commit.
* testGetCombinedVersion: The case fixed in this commit for E-Tag.
Bug: T152266
Change-Id: Ice4ede5ea594bf3fa591134bc9382bd9c24e2f39
2016-12-03 00:48:14 +00:00
|
|
|
$rl->register( [
|
2019-07-11 19:48:57 +00:00
|
|
|
'foo' => [ 'factory' => function () {
|
2021-02-07 13:39:00 +00:00
|
|
|
return $this->getSimpleModuleMock( 'foo();' );
|
2019-07-11 19:48:57 +00:00
|
|
|
} ],
|
|
|
|
|
'ferry' => [ 'factory' => function () {
|
2021-02-07 13:39:00 +00:00
|
|
|
return $this->getFailFerryMock();
|
2019-07-11 19:48:57 +00:00
|
|
|
} ],
|
|
|
|
|
'bar' => [ 'factory' => function () {
|
2021-02-07 13:39:00 +00:00
|
|
|
return $this->getSimpleModuleMock( 'bar();' );
|
2019-07-11 19:48:57 +00:00
|
|
|
} ],
|
resourceloader: Don't let module exception break startup
When getScript (or some other method used in a module response)
throws an error, only that module fails (by outputting mw.loader.state
instead of mw.loader.implement). Other modules will work.
This has always been the case and is working fine. For example,
"load.php?modules=foo|bar", where 'foo' throws, will return:
```js
/* exception message: .. */
mw.loader.implement('bar', ..)
mw.loader.state('foo', 'error')
```
The problem, however, is that during the generation of the startup
module, we iterate over all other modules. In 2011, the
getVersionHash method (then: getModifiedTime) was fairly simple
and unlikely to throw errors.
Nowadays, some modules use enableModuleContentVersion which will
involve the same code path as for regular module responses.
The try/catch in ResourceLoader::makeModuleResponse() suffices
for the case of loading modules other than startup. But when
loading the startup module, and an exception happens in getVersionHash,
then the entire startup response is replaced with an exception comment.
Example case:
* A file not existing for a FileModule subclass that uses
enableModuleContentVersion.
* A database error from a data module, like CiteDataModule or
CNChoiceData.
Changes:
* Ensure E-Tag is still useful while an error happens in production
because we respond with 200 OK and one error isn't the same as
another.
Fixed by try/catch in getCombinedVersion.
* Ensure start manifest isn't disrupted by one broken module.
Fixed by try/catch in StartupModule::getModuleRegistrations().
Tests:
* testMakeModuleResponseError: The case that already worked fined.
* testMakeModuleResponseStartupError: The case fixed in this commit.
* testGetCombinedVersion: The case fixed in this commit for E-Tag.
Bug: T152266
Change-Id: Ice4ede5ea594bf3fa591134bc9382bd9c24e2f39
2016-12-03 00:48:14 +00:00
|
|
|
] );
|
|
|
|
|
$context = $this->getResourceLoaderContext(
|
|
|
|
|
[
|
|
|
|
|
'modules' => 'startup',
|
|
|
|
|
'only' => 'scripts',
|
resourceloader: Skip version hash calculation in debug mode
=== Why
* More speed
In debug mode, the server should regenerate the startup manifest
on each page view to ensure immediate effect of changes. But,
this also means more version recomputation work on the server.
For most modules, this was already quite fast on repeat views
because of OS-level file caches, and our file-hash caches and
LESS compile caches in php-apcu from ResourceLoader.
But, this makes it even faster.
* Better integration with browser devtools.
Breakpoints stay more consistently across browsers when the
URL stays the same even after you have changed the file and
reloaded the page. For static files, I believe most browsers ignore
query parameters. But for package files that come from load.php,
this was harder for browsers to guess correctly which old script URL
is logically replaced by a different one on the next page view.
=== How
Change Module::getVersionHash to return empty strings in debug mode.
I considered approaching this from StartupModule::getModuleRegistrations
instead to make the change apply only to the client-side manifest.
I decided against this because we have other calls to getVersionHash
on the server-side (such as for E-Tag calculation, and formatting
cross-wiki URLs) which would then not match the version queries that
mw.loader formats in debug mode.
Also, those calls would still be incurring some the avoidable costs.
=== Notes
* The two test cases for verifying the graceful fallback in production
if version hash computations throw an exception, were moved to a
non-debug test case as no longer happen now during the debug
(unminified) test cases.
* Avoid "PHP Notice: Undefined offset 0" in testMakeModuleResponseStartupError
by adding a fallback to empty string so that if the test fails,
it fails in a more useful way instead of aborting with this error
before the assertion happens. (Since PHPUnit generally stops on the
first error.)
* In practice, there are still "version" query parameters and E-Tag
headers in debug mode. These are not module versions, but URL
"combined versions" crafted by getCombinedVersion() in JS and PHP.
These return the constant "ztntf" in debug mode, which is the hash
of an empty string. We could alter these methods to special-case
when all inputs are and join to a still-empty string, or maybe we
just leave them be. I've done the latter for now.
Bug: T235672
Bug: T85805
Change-Id: I0e63eef4f85b13089a0aa3806a5b6f821d527a92
2021-08-28 02:53:36 +00:00
|
|
|
// No module build for version hash in debug mode
|
|
|
|
|
'debug' => 'false',
|
resourceloader: Don't let module exception break startup
When getScript (or some other method used in a module response)
throws an error, only that module fails (by outputting mw.loader.state
instead of mw.loader.implement). Other modules will work.
This has always been the case and is working fine. For example,
"load.php?modules=foo|bar", where 'foo' throws, will return:
```js
/* exception message: .. */
mw.loader.implement('bar', ..)
mw.loader.state('foo', 'error')
```
The problem, however, is that during the generation of the startup
module, we iterate over all other modules. In 2011, the
getVersionHash method (then: getModifiedTime) was fairly simple
and unlikely to throw errors.
Nowadays, some modules use enableModuleContentVersion which will
involve the same code path as for regular module responses.
The try/catch in ResourceLoader::makeModuleResponse() suffices
for the case of loading modules other than startup. But when
loading the startup module, and an exception happens in getVersionHash,
then the entire startup response is replaced with an exception comment.
Example case:
* A file not existing for a FileModule subclass that uses
enableModuleContentVersion.
* A database error from a data module, like CiteDataModule or
CNChoiceData.
Changes:
* Ensure E-Tag is still useful while an error happens in production
because we respond with 200 OK and one error isn't the same as
another.
Fixed by try/catch in getCombinedVersion.
* Ensure start manifest isn't disrupted by one broken module.
Fixed by try/catch in StartupModule::getModuleRegistrations().
Tests:
* testMakeModuleResponseError: The case that already worked fined.
* testMakeModuleResponseStartupError: The case fixed in this commit.
* testGetCombinedVersion: The case fixed in this commit for E-Tag.
Bug: T152266
Change-Id: Ice4ede5ea594bf3fa591134bc9382bd9c24e2f39
2016-12-03 00:48:14 +00:00
|
|
|
],
|
|
|
|
|
$rl
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2019-06-10 15:00:16 +00:00
|
|
|
[ 'startup', 'foo', 'ferry', 'bar' ],
|
resourceloader: Don't let module exception break startup
When getScript (or some other method used in a module response)
throws an error, only that module fails (by outputting mw.loader.state
instead of mw.loader.implement). Other modules will work.
This has always been the case and is working fine. For example,
"load.php?modules=foo|bar", where 'foo' throws, will return:
```js
/* exception message: .. */
mw.loader.implement('bar', ..)
mw.loader.state('foo', 'error')
```
The problem, however, is that during the generation of the startup
module, we iterate over all other modules. In 2011, the
getVersionHash method (then: getModifiedTime) was fairly simple
and unlikely to throw errors.
Nowadays, some modules use enableModuleContentVersion which will
involve the same code path as for regular module responses.
The try/catch in ResourceLoader::makeModuleResponse() suffices
for the case of loading modules other than startup. But when
loading the startup module, and an exception happens in getVersionHash,
then the entire startup response is replaced with an exception comment.
Example case:
* A file not existing for a FileModule subclass that uses
enableModuleContentVersion.
* A database error from a data module, like CiteDataModule or
CNChoiceData.
Changes:
* Ensure E-Tag is still useful while an error happens in production
because we respond with 200 OK and one error isn't the same as
another.
Fixed by try/catch in getCombinedVersion.
* Ensure start manifest isn't disrupted by one broken module.
Fixed by try/catch in StartupModule::getModuleRegistrations().
Tests:
* testMakeModuleResponseError: The case that already worked fined.
* testMakeModuleResponseStartupError: The case fixed in this commit.
* testGetCombinedVersion: The case fixed in this commit for E-Tag.
Bug: T152266
Change-Id: Ice4ede5ea594bf3fa591134bc9382bd9c24e2f39
2016-12-03 00:48:14 +00:00
|
|
|
$rl->getModuleNames(),
|
|
|
|
|
'getModuleNames'
|
|
|
|
|
);
|
|
|
|
|
|
2017-07-13 05:16:53 +00:00
|
|
|
// Disable log from makeModuleResponse via outputErrorAndLog
|
|
|
|
|
$this->setLogger( 'exception', new Psr\Log\NullLogger() );
|
|
|
|
|
|
resourceloader: Don't let module exception break startup
When getScript (or some other method used in a module response)
throws an error, only that module fails (by outputting mw.loader.state
instead of mw.loader.implement). Other modules will work.
This has always been the case and is working fine. For example,
"load.php?modules=foo|bar", where 'foo' throws, will return:
```js
/* exception message: .. */
mw.loader.implement('bar', ..)
mw.loader.state('foo', 'error')
```
The problem, however, is that during the generation of the startup
module, we iterate over all other modules. In 2011, the
getVersionHash method (then: getModifiedTime) was fairly simple
and unlikely to throw errors.
Nowadays, some modules use enableModuleContentVersion which will
involve the same code path as for regular module responses.
The try/catch in ResourceLoader::makeModuleResponse() suffices
for the case of loading modules other than startup. But when
loading the startup module, and an exception happens in getVersionHash,
then the entire startup response is replaced with an exception comment.
Example case:
* A file not existing for a FileModule subclass that uses
enableModuleContentVersion.
* A database error from a data module, like CiteDataModule or
CNChoiceData.
Changes:
* Ensure E-Tag is still useful while an error happens in production
because we respond with 200 OK and one error isn't the same as
another.
Fixed by try/catch in getCombinedVersion.
* Ensure start manifest isn't disrupted by one broken module.
Fixed by try/catch in StartupModule::getModuleRegistrations().
Tests:
* testMakeModuleResponseError: The case that already worked fined.
* testMakeModuleResponseStartupError: The case fixed in this commit.
* testGetCombinedVersion: The case fixed in this commit for E-Tag.
Bug: T152266
Change-Id: Ice4ede5ea594bf3fa591134bc9382bd9c24e2f39
2016-12-03 00:48:14 +00:00
|
|
|
$modules = [ 'startup' => $rl->getModule( 'startup' ) ];
|
|
|
|
|
$response = $rl->makeModuleResponse( $context, $modules );
|
|
|
|
|
$errors = $rl->getErrors();
|
|
|
|
|
|
resourceloader: Skip version hash calculation in debug mode
=== Why
* More speed
In debug mode, the server should regenerate the startup manifest
on each page view to ensure immediate effect of changes. But,
this also means more version recomputation work on the server.
For most modules, this was already quite fast on repeat views
because of OS-level file caches, and our file-hash caches and
LESS compile caches in php-apcu from ResourceLoader.
But, this makes it even faster.
* Better integration with browser devtools.
Breakpoints stay more consistently across browsers when the
URL stays the same even after you have changed the file and
reloaded the page. For static files, I believe most browsers ignore
query parameters. But for package files that come from load.php,
this was harder for browsers to guess correctly which old script URL
is logically replaced by a different one on the next page view.
=== How
Change Module::getVersionHash to return empty strings in debug mode.
I considered approaching this from StartupModule::getModuleRegistrations
instead to make the change apply only to the client-side manifest.
I decided against this because we have other calls to getVersionHash
on the server-side (such as for E-Tag calculation, and formatting
cross-wiki URLs) which would then not match the version queries that
mw.loader formats in debug mode.
Also, those calls would still be incurring some the avoidable costs.
=== Notes
* The two test cases for verifying the graceful fallback in production
if version hash computations throw an exception, were moved to a
non-debug test case as no longer happen now during the debug
(unminified) test cases.
* Avoid "PHP Notice: Undefined offset 0" in testMakeModuleResponseStartupError
by adding a fallback to empty string so that if the test fails,
it fails in a more useful way instead of aborting with this error
before the assertion happens. (Since PHPUnit generally stops on the
first error.)
* In practice, there are still "version" query parameters and E-Tag
headers in debug mode. These are not module versions, but URL
"combined versions" crafted by getCombinedVersion() in JS and PHP.
These return the constant "ztntf" in debug mode, which is the hash
of an empty string. We could alter these methods to special-case
when all inputs are and join to a still-empty string, or maybe we
just leave them be. I've done the latter for now.
Bug: T235672
Bug: T85805
Change-Id: I0e63eef4f85b13089a0aa3806a5b6f821d527a92
2021-08-28 02:53:36 +00:00
|
|
|
$this->assertRegExp( '/Ferry not found/', $errors[0] ?? '' );
|
resourceloader: Don't let module exception break startup
When getScript (or some other method used in a module response)
throws an error, only that module fails (by outputting mw.loader.state
instead of mw.loader.implement). Other modules will work.
This has always been the case and is working fine. For example,
"load.php?modules=foo|bar", where 'foo' throws, will return:
```js
/* exception message: .. */
mw.loader.implement('bar', ..)
mw.loader.state('foo', 'error')
```
The problem, however, is that during the generation of the startup
module, we iterate over all other modules. In 2011, the
getVersionHash method (then: getModifiedTime) was fairly simple
and unlikely to throw errors.
Nowadays, some modules use enableModuleContentVersion which will
involve the same code path as for regular module responses.
The try/catch in ResourceLoader::makeModuleResponse() suffices
for the case of loading modules other than startup. But when
loading the startup module, and an exception happens in getVersionHash,
then the entire startup response is replaced with an exception comment.
Example case:
* A file not existing for a FileModule subclass that uses
enableModuleContentVersion.
* A database error from a data module, like CiteDataModule or
CNChoiceData.
Changes:
* Ensure E-Tag is still useful while an error happens in production
because we respond with 200 OK and one error isn't the same as
another.
Fixed by try/catch in getCombinedVersion.
* Ensure start manifest isn't disrupted by one broken module.
Fixed by try/catch in StartupModule::getModuleRegistrations().
Tests:
* testMakeModuleResponseError: The case that already worked fined.
* testMakeModuleResponseStartupError: The case fixed in this commit.
* testGetCombinedVersion: The case fixed in this commit for E-Tag.
Bug: T152266
Change-Id: Ice4ede5ea594bf3fa591134bc9382bd9c24e2f39
2016-12-03 00:48:14 +00:00
|
|
|
$this->assertCount( 1, $errors );
|
|
|
|
|
$this->assertRegExp(
|
2018-09-08 18:01:45 +00:00
|
|
|
'/isCompatible.*window\.RLQ/s',
|
resourceloader: Don't let module exception break startup
When getScript (or some other method used in a module response)
throws an error, only that module fails (by outputting mw.loader.state
instead of mw.loader.implement). Other modules will work.
This has always been the case and is working fine. For example,
"load.php?modules=foo|bar", where 'foo' throws, will return:
```js
/* exception message: .. */
mw.loader.implement('bar', ..)
mw.loader.state('foo', 'error')
```
The problem, however, is that during the generation of the startup
module, we iterate over all other modules. In 2011, the
getVersionHash method (then: getModifiedTime) was fairly simple
and unlikely to throw errors.
Nowadays, some modules use enableModuleContentVersion which will
involve the same code path as for regular module responses.
The try/catch in ResourceLoader::makeModuleResponse() suffices
for the case of loading modules other than startup. But when
loading the startup module, and an exception happens in getVersionHash,
then the entire startup response is replaced with an exception comment.
Example case:
* A file not existing for a FileModule subclass that uses
enableModuleContentVersion.
* A database error from a data module, like CiteDataModule or
CNChoiceData.
Changes:
* Ensure E-Tag is still useful while an error happens in production
because we respond with 200 OK and one error isn't the same as
another.
Fixed by try/catch in getCombinedVersion.
* Ensure start manifest isn't disrupted by one broken module.
Fixed by try/catch in StartupModule::getModuleRegistrations().
Tests:
* testMakeModuleResponseError: The case that already worked fined.
* testMakeModuleResponseStartupError: The case fixed in this commit.
* testGetCombinedVersion: The case fixed in this commit for E-Tag.
Bug: T152266
Change-Id: Ice4ede5ea594bf3fa591134bc9382bd9c24e2f39
2016-12-03 00:48:14 +00:00
|
|
|
$response,
|
|
|
|
|
'startup response undisrupted (T152266)'
|
|
|
|
|
);
|
|
|
|
|
$this->assertRegExp(
|
|
|
|
|
'/register\([^)]+"ferry",\s*""/s',
|
|
|
|
|
$response,
|
|
|
|
|
'startup response registers broken module'
|
|
|
|
|
);
|
|
|
|
|
$this->assertRegExp(
|
|
|
|
|
'/state\([^)]+"ferry":\s*"error"/s',
|
|
|
|
|
$response,
|
|
|
|
|
'startup response sets state to error'
|
|
|
|
|
);
|
|
|
|
|
}
|
resourceloader: Add support for modules sending preload headers
ResourceLoaderModule objects gain a new method getPreloadLinks() which
returns an array with the meta data required to build a Link rel=preload
header according to the current draft for W3C Preload.
<https://w3c.github.io/preload/>
Another implementation of this is already in use in OutputPage for
preloading the logo image.
This array is formatted by the ResourceLoaderModule::getHeaders method,
which is implemented as "final" at this time, thus restricting use to
the Link rel=preload header.
Headers are exposed and process-cached, like all other content
(scripts, styles, etc.), through ResourceLoaderModule::getModuleContent,
and aggregated by ResoureLoader::makeModuleResponse.
I had hoped for the getPreloadLinks to be stateless (not vary on $context).
Whether something should be preloaded and what, should not vary on the
skin or language. However, while that conceptually holds true, the exact
url for any given resource may still vary. Even the main use case for this
feature (T164299, preloading base modules request) require $context to pass
down skin and lang to the load.php url.
Add full test coverage and example documentation.
Bug: T164299
Change-Id: I2bfe0796ceaa0c82579c501f5b10e931f2175681
2017-07-18 02:36:01 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Integration test for modules sending extra HTTP response headers.
|
|
|
|
|
*
|
|
|
|
|
* @covers ResourceLoaderModule::getHeaders
|
|
|
|
|
* @covers ResourceLoaderModule::buildContent
|
|
|
|
|
* @covers ResourceLoader::makeModuleResponse
|
|
|
|
|
*/
|
|
|
|
|
public function testMakeModuleResponseExtraHeaders() {
|
|
|
|
|
$module = $this->getMockBuilder( ResourceLoaderTestModule::class )
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [ 'getPreloadLinks' ] )->getMock();
|
resourceloader: Add support for modules sending preload headers
ResourceLoaderModule objects gain a new method getPreloadLinks() which
returns an array with the meta data required to build a Link rel=preload
header according to the current draft for W3C Preload.
<https://w3c.github.io/preload/>
Another implementation of this is already in use in OutputPage for
preloading the logo image.
This array is formatted by the ResourceLoaderModule::getHeaders method,
which is implemented as "final" at this time, thus restricting use to
the Link rel=preload header.
Headers are exposed and process-cached, like all other content
(scripts, styles, etc.), through ResourceLoaderModule::getModuleContent,
and aggregated by ResoureLoader::makeModuleResponse.
I had hoped for the getPreloadLinks to be stateless (not vary on $context).
Whether something should be preloaded and what, should not vary on the
skin or language. However, while that conceptually holds true, the exact
url for any given resource may still vary. Even the main use case for this
feature (T164299, preloading base modules request) require $context to pass
down skin and lang to the load.php url.
Add full test coverage and example documentation.
Bug: T164299
Change-Id: I2bfe0796ceaa0c82579c501f5b10e931f2175681
2017-07-18 02:36:01 +00:00
|
|
|
$module->method( 'getPreloadLinks' )->willReturn( [
|
2021-09-03 22:52:31 +00:00
|
|
|
'https://example.org/script.js' => [ 'as' => 'script' ],
|
resourceloader: Add support for modules sending preload headers
ResourceLoaderModule objects gain a new method getPreloadLinks() which
returns an array with the meta data required to build a Link rel=preload
header according to the current draft for W3C Preload.
<https://w3c.github.io/preload/>
Another implementation of this is already in use in OutputPage for
preloading the logo image.
This array is formatted by the ResourceLoaderModule::getHeaders method,
which is implemented as "final" at this time, thus restricting use to
the Link rel=preload header.
Headers are exposed and process-cached, like all other content
(scripts, styles, etc.), through ResourceLoaderModule::getModuleContent,
and aggregated by ResoureLoader::makeModuleResponse.
I had hoped for the getPreloadLinks to be stateless (not vary on $context).
Whether something should be preloaded and what, should not vary on the
skin or language. However, while that conceptually holds true, the exact
url for any given resource may still vary. Even the main use case for this
feature (T164299, preloading base modules request) require $context to pass
down skin and lang to the load.php url.
Add full test coverage and example documentation.
Bug: T164299
Change-Id: I2bfe0796ceaa0c82579c501f5b10e931f2175681
2017-07-18 02:36:01 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$rl = new EmptyResourceLoader();
|
|
|
|
|
$context = $this->getResourceLoaderContext(
|
|
|
|
|
[ 'modules' => 'foo', 'only' => 'scripts' ],
|
|
|
|
|
$rl
|
|
|
|
|
);
|
|
|
|
|
|
2019-07-11 19:48:57 +00:00
|
|
|
$modules = [ 'foo' => $module ];
|
resourceloader: Add support for modules sending preload headers
ResourceLoaderModule objects gain a new method getPreloadLinks() which
returns an array with the meta data required to build a Link rel=preload
header according to the current draft for W3C Preload.
<https://w3c.github.io/preload/>
Another implementation of this is already in use in OutputPage for
preloading the logo image.
This array is formatted by the ResourceLoaderModule::getHeaders method,
which is implemented as "final" at this time, thus restricting use to
the Link rel=preload header.
Headers are exposed and process-cached, like all other content
(scripts, styles, etc.), through ResourceLoaderModule::getModuleContent,
and aggregated by ResoureLoader::makeModuleResponse.
I had hoped for the getPreloadLinks to be stateless (not vary on $context).
Whether something should be preloaded and what, should not vary on the
skin or language. However, while that conceptually holds true, the exact
url for any given resource may still vary. Even the main use case for this
feature (T164299, preloading base modules request) require $context to pass
down skin and lang to the load.php url.
Add full test coverage and example documentation.
Bug: T164299
Change-Id: I2bfe0796ceaa0c82579c501f5b10e931f2175681
2017-07-18 02:36:01 +00:00
|
|
|
$response = $rl->makeModuleResponse( $context, $modules );
|
|
|
|
|
$extraHeaders = TestingAccessWrapper::newFromObject( $rl )->extraHeaders;
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
[
|
|
|
|
|
'Link: <https://example.org/script.js>;rel=preload;as=script'
|
|
|
|
|
],
|
|
|
|
|
$extraHeaders,
|
|
|
|
|
'Extra headers'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoaderModule::getHeaders
|
|
|
|
|
* @covers ResourceLoaderModule::buildContent
|
|
|
|
|
* @covers ResourceLoader::makeModuleResponse
|
|
|
|
|
*/
|
|
|
|
|
public function testMakeModuleResponseExtraHeadersMulti() {
|
|
|
|
|
$foo = $this->getMockBuilder( ResourceLoaderTestModule::class )
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [ 'getPreloadLinks' ] )->getMock();
|
resourceloader: Add support for modules sending preload headers
ResourceLoaderModule objects gain a new method getPreloadLinks() which
returns an array with the meta data required to build a Link rel=preload
header according to the current draft for W3C Preload.
<https://w3c.github.io/preload/>
Another implementation of this is already in use in OutputPage for
preloading the logo image.
This array is formatted by the ResourceLoaderModule::getHeaders method,
which is implemented as "final" at this time, thus restricting use to
the Link rel=preload header.
Headers are exposed and process-cached, like all other content
(scripts, styles, etc.), through ResourceLoaderModule::getModuleContent,
and aggregated by ResoureLoader::makeModuleResponse.
I had hoped for the getPreloadLinks to be stateless (not vary on $context).
Whether something should be preloaded and what, should not vary on the
skin or language. However, while that conceptually holds true, the exact
url for any given resource may still vary. Even the main use case for this
feature (T164299, preloading base modules request) require $context to pass
down skin and lang to the load.php url.
Add full test coverage and example documentation.
Bug: T164299
Change-Id: I2bfe0796ceaa0c82579c501f5b10e931f2175681
2017-07-18 02:36:01 +00:00
|
|
|
$foo->method( 'getPreloadLinks' )->willReturn( [
|
2021-09-03 22:52:31 +00:00
|
|
|
'https://example.org/script.js' => [ 'as' => 'script' ],
|
resourceloader: Add support for modules sending preload headers
ResourceLoaderModule objects gain a new method getPreloadLinks() which
returns an array with the meta data required to build a Link rel=preload
header according to the current draft for W3C Preload.
<https://w3c.github.io/preload/>
Another implementation of this is already in use in OutputPage for
preloading the logo image.
This array is formatted by the ResourceLoaderModule::getHeaders method,
which is implemented as "final" at this time, thus restricting use to
the Link rel=preload header.
Headers are exposed and process-cached, like all other content
(scripts, styles, etc.), through ResourceLoaderModule::getModuleContent,
and aggregated by ResoureLoader::makeModuleResponse.
I had hoped for the getPreloadLinks to be stateless (not vary on $context).
Whether something should be preloaded and what, should not vary on the
skin or language. However, while that conceptually holds true, the exact
url for any given resource may still vary. Even the main use case for this
feature (T164299, preloading base modules request) require $context to pass
down skin and lang to the load.php url.
Add full test coverage and example documentation.
Bug: T164299
Change-Id: I2bfe0796ceaa0c82579c501f5b10e931f2175681
2017-07-18 02:36:01 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$bar = $this->getMockBuilder( ResourceLoaderTestModule::class )
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [ 'getPreloadLinks' ] )->getMock();
|
resourceloader: Add support for modules sending preload headers
ResourceLoaderModule objects gain a new method getPreloadLinks() which
returns an array with the meta data required to build a Link rel=preload
header according to the current draft for W3C Preload.
<https://w3c.github.io/preload/>
Another implementation of this is already in use in OutputPage for
preloading the logo image.
This array is formatted by the ResourceLoaderModule::getHeaders method,
which is implemented as "final" at this time, thus restricting use to
the Link rel=preload header.
Headers are exposed and process-cached, like all other content
(scripts, styles, etc.), through ResourceLoaderModule::getModuleContent,
and aggregated by ResoureLoader::makeModuleResponse.
I had hoped for the getPreloadLinks to be stateless (not vary on $context).
Whether something should be preloaded and what, should not vary on the
skin or language. However, while that conceptually holds true, the exact
url for any given resource may still vary. Even the main use case for this
feature (T164299, preloading base modules request) require $context to pass
down skin and lang to the load.php url.
Add full test coverage and example documentation.
Bug: T164299
Change-Id: I2bfe0796ceaa0c82579c501f5b10e931f2175681
2017-07-18 02:36:01 +00:00
|
|
|
$bar->method( 'getPreloadLinks' )->willReturn( [
|
2021-09-03 22:52:31 +00:00
|
|
|
'/example.png' => [ 'as' => 'image' ],
|
|
|
|
|
'/example.jpg' => [ 'as' => 'image' ],
|
resourceloader: Add support for modules sending preload headers
ResourceLoaderModule objects gain a new method getPreloadLinks() which
returns an array with the meta data required to build a Link rel=preload
header according to the current draft for W3C Preload.
<https://w3c.github.io/preload/>
Another implementation of this is already in use in OutputPage for
preloading the logo image.
This array is formatted by the ResourceLoaderModule::getHeaders method,
which is implemented as "final" at this time, thus restricting use to
the Link rel=preload header.
Headers are exposed and process-cached, like all other content
(scripts, styles, etc.), through ResourceLoaderModule::getModuleContent,
and aggregated by ResoureLoader::makeModuleResponse.
I had hoped for the getPreloadLinks to be stateless (not vary on $context).
Whether something should be preloaded and what, should not vary on the
skin or language. However, while that conceptually holds true, the exact
url for any given resource may still vary. Even the main use case for this
feature (T164299, preloading base modules request) require $context to pass
down skin and lang to the load.php url.
Add full test coverage and example documentation.
Bug: T164299
Change-Id: I2bfe0796ceaa0c82579c501f5b10e931f2175681
2017-07-18 02:36:01 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$rl = new EmptyResourceLoader();
|
|
|
|
|
$context = $this->getResourceLoaderContext(
|
|
|
|
|
[ 'modules' => 'foo|bar', 'only' => 'scripts' ],
|
|
|
|
|
$rl
|
|
|
|
|
);
|
|
|
|
|
|
2019-07-11 19:48:57 +00:00
|
|
|
$modules = [ 'foo' => $foo, 'bar' => $bar ];
|
resourceloader: Add support for modules sending preload headers
ResourceLoaderModule objects gain a new method getPreloadLinks() which
returns an array with the meta data required to build a Link rel=preload
header according to the current draft for W3C Preload.
<https://w3c.github.io/preload/>
Another implementation of this is already in use in OutputPage for
preloading the logo image.
This array is formatted by the ResourceLoaderModule::getHeaders method,
which is implemented as "final" at this time, thus restricting use to
the Link rel=preload header.
Headers are exposed and process-cached, like all other content
(scripts, styles, etc.), through ResourceLoaderModule::getModuleContent,
and aggregated by ResoureLoader::makeModuleResponse.
I had hoped for the getPreloadLinks to be stateless (not vary on $context).
Whether something should be preloaded and what, should not vary on the
skin or language. However, while that conceptually holds true, the exact
url for any given resource may still vary. Even the main use case for this
feature (T164299, preloading base modules request) require $context to pass
down skin and lang to the load.php url.
Add full test coverage and example documentation.
Bug: T164299
Change-Id: I2bfe0796ceaa0c82579c501f5b10e931f2175681
2017-07-18 02:36:01 +00:00
|
|
|
$response = $rl->makeModuleResponse( $context, $modules );
|
|
|
|
|
$extraHeaders = TestingAccessWrapper::newFromObject( $rl )->extraHeaders;
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
[
|
|
|
|
|
'Link: <https://example.org/script.js>;rel=preload;as=script',
|
|
|
|
|
'Link: </example.png>;rel=preload;as=image,</example.jpg>;rel=preload;as=image'
|
|
|
|
|
],
|
|
|
|
|
$extraHeaders,
|
|
|
|
|
'Extra headers'
|
|
|
|
|
);
|
|
|
|
|
}
|
2017-10-17 01:48:54 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::respond
|
|
|
|
|
*/
|
2018-05-30 17:21:31 +00:00
|
|
|
public function testRespondEmpty() {
|
2017-10-17 01:48:54 +00:00
|
|
|
$rl = $this->getMockBuilder( EmptyResourceLoader::class )
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [
|
2017-10-17 01:48:54 +00:00
|
|
|
'tryRespondNotModified',
|
|
|
|
|
'sendResponseHeaders',
|
|
|
|
|
'measureResponseTime',
|
|
|
|
|
] )
|
|
|
|
|
->getMock();
|
|
|
|
|
$context = $this->getResourceLoaderContext( [ 'modules' => '' ], $rl );
|
|
|
|
|
|
|
|
|
|
$rl->expects( $this->once() )->method( 'measureResponseTime' );
|
|
|
|
|
$this->expectOutputRegex( '/no modules were requested/' );
|
|
|
|
|
|
|
|
|
|
$rl->respond( $context );
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-30 17:21:31 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::respond
|
|
|
|
|
*/
|
|
|
|
|
public function testRespondSimple() {
|
|
|
|
|
$module = new ResourceLoaderTestModule( [ 'script' => 'foo();' ] );
|
|
|
|
|
$rl = $this->getMockBuilder( EmptyResourceLoader::class )
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [
|
2018-05-30 17:21:31 +00:00
|
|
|
'measureResponseTime',
|
|
|
|
|
'tryRespondNotModified',
|
|
|
|
|
'sendResponseHeaders',
|
|
|
|
|
'makeModuleResponse',
|
|
|
|
|
] )
|
|
|
|
|
->getMock();
|
2019-07-11 19:48:57 +00:00
|
|
|
$rl->register( 'test', [
|
2021-02-07 13:10:36 +00:00
|
|
|
'factory' => static function () use ( $module ) {
|
2019-07-11 19:48:57 +00:00
|
|
|
return $module;
|
|
|
|
|
}
|
|
|
|
|
] );
|
2018-05-30 17:21:31 +00:00
|
|
|
$context = $this->getResourceLoaderContext(
|
|
|
|
|
[ 'modules' => 'test', 'only' => null ],
|
|
|
|
|
$rl
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$rl->expects( $this->once() )->method( 'makeModuleResponse' )
|
|
|
|
|
->with( $context, [ 'test' => $module ] )
|
|
|
|
|
->willReturn( 'implement_foo;' );
|
|
|
|
|
$this->expectOutputRegex( '/^implement_foo;/' );
|
|
|
|
|
|
|
|
|
|
$rl->respond( $context );
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-24 18:45:27 +00:00
|
|
|
/**
|
|
|
|
|
* Refuse requests for private modules.
|
|
|
|
|
*
|
|
|
|
|
* @covers ResourceLoader::respond
|
|
|
|
|
*/
|
|
|
|
|
public function testRespondErrorPrivate() {
|
|
|
|
|
$rl = $this->getMockBuilder( EmptyResourceLoader::class )
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [
|
2019-08-24 18:45:27 +00:00
|
|
|
'measureResponseTime',
|
|
|
|
|
'tryRespondNotModified',
|
|
|
|
|
'sendResponseHeaders',
|
|
|
|
|
] )
|
|
|
|
|
->getMock();
|
|
|
|
|
$rl->register( [
|
|
|
|
|
'foo' => [ 'class' => ResourceLoaderTestModule::class ],
|
|
|
|
|
'bar' => [ 'class' => ResourceLoaderTestModule::class, 'group' => 'private' ],
|
|
|
|
|
] );
|
|
|
|
|
$context = $this->getResourceLoaderContext(
|
|
|
|
|
[ 'modules' => 'foo|bar', 'only' => null ],
|
|
|
|
|
$rl
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->expectOutputRegex( '/^\/\*.+Cannot build private module/s' );
|
|
|
|
|
$rl->respond( $context );
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-30 17:21:31 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::respond
|
|
|
|
|
*/
|
|
|
|
|
public function testRespondInternalFailures() {
|
|
|
|
|
$module = new ResourceLoaderTestModule( [ 'script' => 'foo();' ] );
|
|
|
|
|
$rl = $this->getMockBuilder( EmptyResourceLoader::class )
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [
|
2018-05-30 17:21:31 +00:00
|
|
|
'measureResponseTime',
|
|
|
|
|
'preloadModuleInfo',
|
|
|
|
|
'getCombinedVersion',
|
|
|
|
|
'tryRespondNotModified',
|
|
|
|
|
'makeModuleResponse',
|
|
|
|
|
'sendResponseHeaders',
|
|
|
|
|
] )
|
|
|
|
|
->getMock();
|
2019-07-11 19:48:57 +00:00
|
|
|
$rl->register( 'test', [
|
2021-02-07 13:10:36 +00:00
|
|
|
'factory' => static function () use ( $module ) {
|
2019-07-11 19:48:57 +00:00
|
|
|
return $module;
|
|
|
|
|
}
|
|
|
|
|
] );
|
2018-05-30 17:21:31 +00:00
|
|
|
$context = $this->getResourceLoaderContext( [ 'modules' => 'test' ], $rl );
|
|
|
|
|
// Disable logging from outputErrorAndLog
|
|
|
|
|
$this->setLogger( 'exception', new Psr\Log\NullLogger() );
|
|
|
|
|
|
|
|
|
|
$rl->expects( $this->once() )->method( 'preloadModuleInfo' )
|
|
|
|
|
->willThrowException( new Exception( 'Preload error' ) );
|
|
|
|
|
$rl->expects( $this->once() )->method( 'getCombinedVersion' )
|
|
|
|
|
->willThrowException( new Exception( 'Version error' ) );
|
|
|
|
|
$rl->expects( $this->once() )->method( 'makeModuleResponse' )
|
|
|
|
|
->with( $context, [ 'test' => $module ] )
|
|
|
|
|
->willReturn( 'foo;' );
|
|
|
|
|
// Internal errors should be caught and logged without affecting module output
|
|
|
|
|
$this->expectOutputRegex( '/^\/\*.+Preload error.+Version error.+\*\/.*foo;/ms' );
|
|
|
|
|
|
|
|
|
|
$rl->respond( $context );
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-17 01:48:54 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::measureResponseTime
|
|
|
|
|
*/
|
|
|
|
|
public function testMeasureResponseTime() {
|
|
|
|
|
$stats = $this->getMockBuilder( NullStatsdDataFactory::class )
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [ 'timing' ] )->getMock();
|
2017-10-17 01:48:54 +00:00
|
|
|
$this->setService( 'StatsdDataFactory', $stats );
|
|
|
|
|
|
|
|
|
|
$stats->expects( $this->once() )->method( 'timing' )
|
|
|
|
|
->with( 'resourceloader.responseTime', $this->anything() );
|
|
|
|
|
|
|
|
|
|
$timing = new Timing();
|
|
|
|
|
$timing->mark( 'requestShutdown' );
|
|
|
|
|
$rl = TestingAccessWrapper::newFromObject( new EmptyResourceLoader );
|
|
|
|
|
$rl->measureResponseTime( $timing );
|
|
|
|
|
DeferredUpdates::doUpdates();
|
|
|
|
|
}
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|