2010-12-14 16:26:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
2017-04-19 19:37:35 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
|
|
2014-03-07 17:18:31 +00:00
|
|
|
class ResourceLoaderTest extends ResourceLoaderTestCase {
|
2010-12-14 16:26:35 +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
|
|
|
protected function setUp() {
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( [
|
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.
|
|
|
|
|
*
|
2010-12-14 16:26:35 +00:00
|
|
|
* @covers ResourceLoader::__construct
|
|
|
|
|
*/
|
2016-08-25 01:50:30 +00:00
|
|
|
public function testConstructRegistrationHook() {
|
2014-10-15 15:05:04 +00:00
|
|
|
$resourceLoaderRegisterModulesHook = false;
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( 'wgHooks', [
|
|
|
|
|
'ResourceLoaderRegisterModules' => [
|
2014-10-15 15:05:04 +00:00
|
|
|
function ( &$resourceLoader ) use ( &$resourceLoaderRegisterModulesHook ) {
|
|
|
|
|
$resourceLoaderRegisterModulesHook = true;
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
]
|
|
|
|
|
] );
|
2014-10-15 15:05:04 +00:00
|
|
|
|
2016-08-25 01:50:30 +00:00
|
|
|
$unused = new ResourceLoader();
|
2014-10-15 15:05:04 +00:00
|
|
|
$this->assertTrue(
|
|
|
|
|
$resourceLoaderRegisterModulesHook,
|
|
|
|
|
'Hook ResourceLoaderRegisterModules called'
|
|
|
|
|
);
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::register
|
|
|
|
|
* @covers ResourceLoader::getModule
|
|
|
|
|
*/
|
2017-04-06 01:19:48 +00:00
|
|
|
public function testRegisterValidObject() {
|
2016-08-25 01:50:30 +00:00
|
|
|
$module = new ResourceLoaderTestModule();
|
|
|
|
|
$resourceLoader = new EmptyResourceLoader();
|
|
|
|
|
$resourceLoader->register( 'test', $module );
|
|
|
|
|
$this->assertEquals( $module, $resourceLoader->getModule( 'test' ) );
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
2011-06-07 17:56:40 +00:00
|
|
|
|
2017-04-06 01:19:48 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::register
|
|
|
|
|
* @covers ResourceLoader::getModule
|
|
|
|
|
*/
|
|
|
|
|
public function testRegisterValidArray() {
|
|
|
|
|
$module = new ResourceLoaderTestModule();
|
|
|
|
|
$resourceLoader = new EmptyResourceLoader();
|
|
|
|
|
// Covers case of register() setting $rl->moduleInfos,
|
|
|
|
|
// but $rl->modules lazy-populated by getModule()
|
|
|
|
|
$resourceLoader->register( 'test', [ 'object' => $module ] );
|
|
|
|
|
$this->assertEquals( $module, $resourceLoader->getModule( 'test' ) );
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-17 01:15:04 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::register
|
|
|
|
|
*/
|
|
|
|
|
public function testRegisterEmptyString() {
|
|
|
|
|
$module = new ResourceLoaderTestModule();
|
|
|
|
|
$resourceLoader = new EmptyResourceLoader();
|
|
|
|
|
$resourceLoader->register( '', $module );
|
|
|
|
|
$this->assertEquals( $module, $resourceLoader->getModule( '' ) );
|
|
|
|
|
}
|
|
|
|
|
|
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
|
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();
|
2018-01-13 00:02:09 +00:00
|
|
|
$this->setExpectedException( MWException::class, "name 'test!invalid' is invalid" );
|
2016-08-25 01:50:30 +00:00
|
|
|
$resourceLoader->register( 'test!invalid', new ResourceLoaderTestModule() );
|
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();
|
2018-01-13 00:02:09 +00:00
|
|
|
$this->setExpectedException( MWException::class, 'ResourceLoader module info type error' );
|
2016-08-25 01:50:30 +00:00
|
|
|
$resourceLoader->register( 'test', new stdClass() );
|
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 );
|
|
|
|
|
|
|
|
|
|
$module1 = new ResourceLoaderTestModule();
|
|
|
|
|
$module2 = new ResourceLoaderTestModule();
|
|
|
|
|
$resourceLoader->register( 'test', $module1 );
|
|
|
|
|
$resourceLoader->register( 'test', $module2 );
|
|
|
|
|
$this->assertSame( $module2, $resourceLoader->getModule( 'test' ) );
|
|
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
$resourceLoader->register( 'test.foo', new ResourceLoaderTestModule() );
|
|
|
|
|
$resourceLoader->register( 'test.bar', new ResourceLoaderTestModule() );
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
[ 'test.foo', 'test.bar' ],
|
|
|
|
|
$resourceLoader->getModuleNames()
|
|
|
|
|
);
|
2011-06-07 17:56:40 +00:00
|
|
|
}
|
|
|
|
|
|
2017-06-21 19:37:44 +00:00
|
|
|
public function provideTestIsFileModule() {
|
|
|
|
|
$fileModuleObj = $this->getMockBuilder( ResourceLoaderFileModule::class )
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
|
|
|
|
return [
|
|
|
|
|
'object' => [ false,
|
|
|
|
|
new ResourceLoaderTestModule()
|
|
|
|
|
],
|
|
|
|
|
'FileModule object' => [ false,
|
|
|
|
|
$fileModuleObj
|
|
|
|
|
],
|
|
|
|
|
'simple empty' => [ true,
|
|
|
|
|
[]
|
|
|
|
|
],
|
|
|
|
|
'simple scripts' => [ true,
|
|
|
|
|
[ 'scripts' => 'example.js' ]
|
|
|
|
|
],
|
|
|
|
|
'simple scripts, raw and targets' => [ true, [
|
|
|
|
|
'scripts' => [ 'a.js', 'b.js' ],
|
|
|
|
|
'raw' => true,
|
|
|
|
|
'targets' => [ 'desktop', 'mobile' ],
|
|
|
|
|
] ],
|
|
|
|
|
'FileModule' => [ true,
|
|
|
|
|
[ 'class' => ResourceLoaderFileModule::class, 'scripts' => 'example.js' ]
|
|
|
|
|
],
|
|
|
|
|
'TestModule' => [ false,
|
|
|
|
|
[ 'class' => ResourceLoaderTestModule::class, 'scripts' => 'example.js' ]
|
|
|
|
|
],
|
|
|
|
|
'SkinModule (FileModule subclass)' => [ true,
|
|
|
|
|
[ 'class' => ResourceLoaderSkinModule::class, 'scripts' => 'example.js' ]
|
|
|
|
|
],
|
|
|
|
|
'JqueryMsgModule (FileModule subclass)' => [ true, [
|
|
|
|
|
'class' => ResourceLoaderJqueryMsgModule::class,
|
|
|
|
|
'scripts' => 'example.js',
|
|
|
|
|
] ],
|
|
|
|
|
'WikiModule' => [ false, [
|
|
|
|
|
'class' => ResourceLoaderWikiModule::class,
|
|
|
|
|
'scripts' => [ 'MediaWiki:Example.js' ],
|
|
|
|
|
] ],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideTestIsFileModule
|
|
|
|
|
* @covers ResourceLoader::isFileModule
|
|
|
|
|
*/
|
|
|
|
|
public function testIsFileModule( $expected, $module ) {
|
|
|
|
|
$rl = TestingAccessWrapper::newFromObject( new EmptyResourceLoader() );
|
|
|
|
|
$rl->register( 'test', $module );
|
|
|
|
|
$this->assertSame( $expected, $rl->isFileModule( 'test' ) );
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-28 02:10:03 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::isFileModule
|
|
|
|
|
*/
|
|
|
|
|
public function testIsFileModuleUnknown() {
|
|
|
|
|
$rl = TestingAccessWrapper::newFromObject( new EmptyResourceLoader() );
|
|
|
|
|
$this->assertSame( false, $rl->isFileModule( 'unknown' ) );
|
|
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
$rl->register( 'test', new ResourceLoaderTestModule() );
|
|
|
|
|
$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'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
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( [
|
|
|
|
|
"$basePath/common" => '',
|
|
|
|
|
] );
|
|
|
|
|
$css = $lc->parseFile( "$basePath/module/use-import-dir.less" )->getCss();
|
|
|
|
|
$this->assertStringEqualsFile( "$basePath/module/styles.css", $css );
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
* @covers ResourceLoaderContext::expandModuleNames
|
|
|
|
|
*/
|
2016-08-29 23:35:55 +00:00
|
|
|
public function testExpandModuleNames( $desc, $modules, $packed, $unpacked = null ) {
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$unpacked ?: $modules,
|
|
|
|
|
ResourceLoaderContext::expandModuleNames( $packed ),
|
|
|
|
|
$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 ) {
|
|
|
|
|
$rl = new ResourceLoader;
|
|
|
|
|
$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() {
|
|
|
|
|
$rl = new ResourceLoader;
|
2018-01-13 00:02:09 +00:00
|
|
|
$this->setExpectedException(
|
|
|
|
|
MWException::class, 'ResourceLoader duplicate source addition error'
|
|
|
|
|
);
|
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() {
|
|
|
|
|
$rl = new ResourceLoader;
|
2018-01-13 00:02:09 +00:00
|
|
|
$this->setExpectedException( MWException::class, 'with no "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();
|
|
|
|
|
} );',
|
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 );" );',
|
|
|
|
|
] ],
|
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,
|
|
|
|
|
'styles' => [], 'templates' => [], 'messages' => new XmlJsCode( '{}' )
|
2016-08-25 01:50:30 +00:00
|
|
|
];
|
|
|
|
|
ResourceLoader::clearCache();
|
2016-09-15 04:01:09 +00:00
|
|
|
$this->setMwGlobals( 'wgResourceLoaderDebug', true );
|
2016-08-25 01:50:30 +00:00
|
|
|
|
2018-01-13 00:02:09 +00:00
|
|
|
$rl = TestingAccessWrapper::newFromClass( ResourceLoader::class );
|
2014-12-09 01:17:53 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
$case['expected'],
|
2016-09-15 04:01:09 +00:00
|
|
|
$rl->makeLoaderImplementScript(
|
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'],
|
|
|
|
|
$case['templates']
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-25 01:50:30 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::makeLoaderImplementScript
|
|
|
|
|
*/
|
|
|
|
|
public function testMakeLoaderImplementScriptInvalid() {
|
2018-01-13 00:02:09 +00:00
|
|
|
$this->setExpectedException( MWException::class, 'Invalid scripts error' );
|
|
|
|
|
$rl = TestingAccessWrapper::newFromClass( ResourceLoader::class );
|
2016-09-15 04:01:09 +00:00
|
|
|
$rl->makeLoaderImplementScript(
|
2016-08-25 01:50:30 +00:00
|
|
|
'test', // name
|
|
|
|
|
123, // scripts
|
|
|
|
|
null, // styles
|
|
|
|
|
null, // messages
|
|
|
|
|
null // templates
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-06 01:19:48 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::makeLoaderRegisterScript
|
|
|
|
|
*/
|
|
|
|
|
public function testMakeLoaderRegisterScript() {
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'mw.loader.register( [
|
|
|
|
|
[
|
|
|
|
|
"test.name",
|
|
|
|
|
"1234567"
|
|
|
|
|
]
|
|
|
|
|
] );',
|
|
|
|
|
ResourceLoader::makeLoaderRegisterScript( [
|
|
|
|
|
[ 'test.name', '1234567' ],
|
|
|
|
|
] ),
|
|
|
|
|
'Nested array parameter'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2018-09-15 21:31:18 +00:00
|
|
|
'mw.loader.register( [
|
|
|
|
|
[
|
|
|
|
|
"test.foo",
|
|
|
|
|
"100"
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
"test.bar",
|
|
|
|
|
"200",
|
|
|
|
|
[
|
|
|
|
|
"test.unknown"
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
"test.baz",
|
|
|
|
|
"300",
|
|
|
|
|
[
|
|
|
|
|
3,
|
|
|
|
|
0
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
"test.quux",
|
|
|
|
|
"400",
|
|
|
|
|
[],
|
|
|
|
|
null,
|
|
|
|
|
null,
|
|
|
|
|
"return true;"
|
|
|
|
|
]
|
|
|
|
|
] );',
|
|
|
|
|
ResourceLoader::makeLoaderRegisterScript( [
|
|
|
|
|
[ '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() {
|
|
|
|
|
$this->assertEquals(
|
2018-09-03 23:17:11 +00:00
|
|
|
'mw.loader.addSource( {
|
|
|
|
|
"local": "/w/load.php"
|
|
|
|
|
} );',
|
2016-08-25 01:50:30 +00:00
|
|
|
ResourceLoader::makeLoaderSourcesScript( 'local', '/w/load.php' )
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'mw.loader.addSource( {
|
|
|
|
|
"local": "/w/load.php"
|
|
|
|
|
} );',
|
|
|
|
|
ResourceLoader::makeLoaderSourcesScript( [ 'local' => '/w/load.php' ] )
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'mw.loader.addSource( {
|
|
|
|
|
"local": "/w/load.php",
|
|
|
|
|
"example": "https://example.org/w/load.php"
|
|
|
|
|
} );',
|
|
|
|
|
ResourceLoader::makeLoaderSourcesScript( [
|
|
|
|
|
'local' => '/w/load.php',
|
|
|
|
|
'example' => 'https://example.org/w/load.php'
|
|
|
|
|
] )
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'mw.loader.addSource( [] );',
|
|
|
|
|
ResourceLoader::makeLoaderSourcesScript( [] )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( 'wgResourceLoaderSources', [] );
|
2014-06-28 02:57:40 +00:00
|
|
|
$rl = new ResourceLoader();
|
|
|
|
|
$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'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$rl->getLoadScript( 'thiswasneverreigstered' );
|
|
|
|
|
$this->assertTrue( false, 'ResourceLoader::getLoadScript should have thrown an exception' );
|
|
|
|
|
} catch ( MWException $e ) {
|
|
|
|
|
$this->assertTrue( true );
|
|
|
|
|
}
|
|
|
|
|
}
|
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 )
|
2018-05-28 00:39:58 +00:00
|
|
|
->setMethods( [ $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 )
|
|
|
|
|
->setMethods( [ 'getScript' ] )
|
|
|
|
|
->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 )
|
|
|
|
|
->setMethods( [ 'getStyles' ] )
|
|
|
|
|
->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
|
|
|
|
|
->setMethods( [ '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( [
|
|
|
|
|
'foo' => self::getSimpleModuleMock(),
|
|
|
|
|
'ferry' => self::getFailFerryMock(),
|
|
|
|
|
'bar' => self::getSimpleModuleMock(),
|
|
|
|
|
] );
|
|
|
|
|
$context = $this->getResourceLoaderContext( [], $rl );
|
|
|
|
|
|
2017-06-28 02:10:03 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
'',
|
|
|
|
|
$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(
|
|
|
|
|
ResourceLoader::makeHash( self::BLANK_VERSION ),
|
|
|
|
|
$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()',
|
|
|
|
|
],
|
2017-06-28 02:51:03 +00:00
|
|
|
'expected' => "foo()\n" . 'mw.loader.state( {
|
2017-06-28 02:10:03 +00:00
|
|
|
"foo": "ready"
|
|
|
|
|
} );',
|
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()',
|
|
|
|
|
],
|
2017-06-28 02:51:03 +00:00
|
|
|
'expected' => "foo()\nbar()\n" . 'mw.loader.state( {
|
2017-06-28 02:10:03 +00:00
|
|
|
"foo": "ready",
|
|
|
|
|
"bar": "ready"
|
|
|
|
|
} );',
|
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();"
|
|
|
|
|
],
|
2017-06-28 02:51:03 +00:00
|
|
|
'expected' => "foo()\n// bar();\n" . 'mw.loader.state( {
|
2017-06-28 02:10:03 +00:00
|
|
|
"foo": "ready"
|
|
|
|
|
} );',
|
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 ) {
|
|
|
|
|
return self::getSimpleModuleMock( $script );
|
|
|
|
|
}, $scripts );
|
|
|
|
|
$rl->register( $modules );
|
|
|
|
|
|
|
|
|
|
$this->setMwGlobals( 'wgResourceLoaderDebug', $debug );
|
|
|
|
|
$context = $this->getResourceLoaderContext(
|
|
|
|
|
[
|
|
|
|
|
'modules' => implode( '|', array_keys( $modules ) ),
|
|
|
|
|
'only' => 'scripts',
|
|
|
|
|
],
|
|
|
|
|
$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 = [
|
|
|
|
|
'foo' => self::getSimpleModuleMock( 'foo();' ),
|
|
|
|
|
'ferry' => self::getFailFerryMock(),
|
|
|
|
|
'bar' => self::getSimpleModuleMock( 'bar();' ),
|
|
|
|
|
];
|
|
|
|
|
$rl = new EmptyResourceLoader();
|
|
|
|
|
$rl->register( $modules );
|
|
|
|
|
$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(
|
2017-06-28 02:51:03 +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"
|
|
|
|
|
} );',
|
|
|
|
|
$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{}' ),
|
|
|
|
|
'ferry' => self::getFailFerryMock( 'getStyles' ),
|
|
|
|
|
'bar' => self::getSimpleStyleModuleMock( '.bar{}' ),
|
|
|
|
|
];
|
|
|
|
|
$rl = new EmptyResourceLoader();
|
|
|
|
|
$rl->register( $modules );
|
|
|
|
|
$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() {
|
|
|
|
|
$rl = new EmptyResourceLoader();
|
|
|
|
|
$rl->register( [
|
|
|
|
|
'foo' => self::getSimpleModuleMock( 'foo();' ),
|
|
|
|
|
'ferry' => self::getFailFerryMock(),
|
|
|
|
|
'bar' => self::getSimpleModuleMock( 'bar();' ),
|
2018-01-13 00:02:09 +00:00
|
|
|
'startup' => [ 'class' => ResourceLoaderStartUpModule::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
|
|
|
] );
|
|
|
|
|
$context = $this->getResourceLoaderContext(
|
|
|
|
|
[
|
|
|
|
|
'modules' => 'startup',
|
|
|
|
|
'only' => 'scripts',
|
|
|
|
|
],
|
|
|
|
|
$rl
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
[ 'foo', 'ferry', 'bar', 'startup' ],
|
|
|
|
|
$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();
|
|
|
|
|
|
|
|
|
|
$this->assertRegExp( '/Ferry not found/', $errors[0] );
|
|
|
|
|
$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 )
|
|
|
|
|
->setMethods( [ 'getPreloadLinks' ] )->getMock();
|
|
|
|
|
$module->method( 'getPreloadLinks' )->willReturn( [
|
|
|
|
|
'https://example.org/script.js' => [ 'as' => 'script' ],
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$rl = new EmptyResourceLoader();
|
|
|
|
|
$rl->register( [
|
|
|
|
|
'foo' => $module,
|
|
|
|
|
] );
|
|
|
|
|
$context = $this->getResourceLoaderContext(
|
|
|
|
|
[ 'modules' => 'foo', 'only' => 'scripts' ],
|
|
|
|
|
$rl
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$modules = [ 'foo' => $rl->getModule( 'foo' ) ];
|
|
|
|
|
$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 )
|
|
|
|
|
->setMethods( [ 'getPreloadLinks' ] )->getMock();
|
|
|
|
|
$foo->method( 'getPreloadLinks' )->willReturn( [
|
|
|
|
|
'https://example.org/script.js' => [ 'as' => 'script' ],
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$bar = $this->getMockBuilder( ResourceLoaderTestModule::class )
|
|
|
|
|
->setMethods( [ 'getPreloadLinks' ] )->getMock();
|
|
|
|
|
$bar->method( 'getPreloadLinks' )->willReturn( [
|
|
|
|
|
'/example.png' => [ 'as' => 'image' ],
|
|
|
|
|
'/example.jpg' => [ 'as' => 'image' ],
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$rl = new EmptyResourceLoader();
|
|
|
|
|
$rl->register( [ 'foo' => $foo, 'bar' => $bar ] );
|
|
|
|
|
$context = $this->getResourceLoaderContext(
|
|
|
|
|
[ 'modules' => 'foo|bar', 'only' => 'scripts' ],
|
|
|
|
|
$rl
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$modules = [ 'foo' => $rl->getModule( 'foo' ), 'bar' => $rl->getModule( 'bar' ) ];
|
|
|
|
|
$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 )
|
|
|
|
|
->setMethods( [
|
|
|
|
|
'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 )
|
|
|
|
|
->setMethods( [
|
|
|
|
|
'measureResponseTime',
|
|
|
|
|
'tryRespondNotModified',
|
|
|
|
|
'sendResponseHeaders',
|
|
|
|
|
'makeModuleResponse',
|
|
|
|
|
] )
|
|
|
|
|
->getMock();
|
|
|
|
|
$rl->register( 'test', $module );
|
|
|
|
|
$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 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ResourceLoader::respond
|
|
|
|
|
*/
|
|
|
|
|
public function testRespondInternalFailures() {
|
|
|
|
|
$module = new ResourceLoaderTestModule( [ 'script' => 'foo();' ] );
|
|
|
|
|
$rl = $this->getMockBuilder( EmptyResourceLoader::class )
|
|
|
|
|
->setMethods( [
|
|
|
|
|
'measureResponseTime',
|
|
|
|
|
'preloadModuleInfo',
|
|
|
|
|
'getCombinedVersion',
|
|
|
|
|
'tryRespondNotModified',
|
|
|
|
|
'makeModuleResponse',
|
|
|
|
|
'sendResponseHeaders',
|
|
|
|
|
] )
|
|
|
|
|
->getMock();
|
|
|
|
|
$rl->register( 'test', $module );
|
|
|
|
|
$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 )
|
|
|
|
|
->setMethods( [ 'timing' ] )->getMock();
|
|
|
|
|
$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
|
|
|
}
|