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
167 lines
4.4 KiB
PHP
167 lines
4.4 KiB
PHP
<?php
|
|
|
|
use MediaWiki\MediaWikiServices;
|
|
use Psr\Log\LoggerInterface;
|
|
use Psr\Log\NullLogger;
|
|
|
|
abstract class ResourceLoaderTestCase extends MediaWikiTestCase {
|
|
// Version hash for a blank file module.
|
|
// Result of ResourceLoader::makeHash(), ResourceLoaderTestModule
|
|
// and ResourceLoaderFileModule::getDefinitionSummary().
|
|
const BLANK_VERSION = '09p30q0';
|
|
|
|
/**
|
|
* @param array|string $options Language code or options array
|
|
* - string 'lang' Language code
|
|
* - string 'dir' Language direction (ltr or rtl)
|
|
* - string 'modules' Pipe-separated list of module names
|
|
* - string|null 'only' "scripts" (unwrapped script), "styles" (stylesheet), or null
|
|
* (mw.loader.implement).
|
|
* @return ResourceLoaderContext
|
|
*/
|
|
protected function getResourceLoaderContext( $options = [], ResourceLoader $rl = null ) {
|
|
if ( is_string( $options ) ) {
|
|
// Back-compat for extension tests
|
|
$options = [ 'lang' => $options ];
|
|
}
|
|
$options += [
|
|
'lang' => 'en',
|
|
'dir' => 'ltr',
|
|
'modules' => 'startup',
|
|
'only' => 'scripts',
|
|
];
|
|
$resourceLoader = $rl ?: new ResourceLoader();
|
|
$request = new FauxRequest( [
|
|
'lang' => $options['lang'],
|
|
'modules' => $options['modules'],
|
|
'only' => $options['only'],
|
|
'skin' => 'vector',
|
|
'target' => 'phpunit',
|
|
] );
|
|
$ctx = $this->getMockBuilder( 'ResourceLoaderContext' )
|
|
->setConstructorArgs( [ $resourceLoader, $request ] )
|
|
->setMethods( [ 'getDirection' ] )
|
|
->getMock();
|
|
$ctx->method( 'getDirection' )->willReturn( $options['dir'] );
|
|
return $ctx;
|
|
}
|
|
|
|
public static function getSettings() {
|
|
return [
|
|
// For ResourceLoader::inDebugMode since it doesn't have context
|
|
'ResourceLoaderDebug' => true,
|
|
|
|
// Avoid influence from wgInvalidateCacheOnLocalSettingsChange
|
|
'CacheEpoch' => '20140101000000',
|
|
|
|
// For ResourceLoader::__construct()
|
|
'ResourceLoaderSources' => [],
|
|
|
|
// For wfScript()
|
|
'ScriptPath' => '/w',
|
|
'ScriptExtension' => '.php',
|
|
'Script' => '/w/index.php',
|
|
'LoadScript' => '/w/load.php',
|
|
];
|
|
}
|
|
|
|
protected function setUp() {
|
|
parent::setUp();
|
|
|
|
ResourceLoader::clearCache();
|
|
|
|
$globals = [];
|
|
foreach ( self::getSettings() as $key => $value ) {
|
|
$globals['wg' . $key] = $value;
|
|
}
|
|
$this->setMwGlobals( $globals );
|
|
}
|
|
}
|
|
|
|
/* Stubs */
|
|
|
|
class ResourceLoaderTestModule extends ResourceLoaderModule {
|
|
protected $messages = [];
|
|
protected $dependencies = [];
|
|
protected $group = null;
|
|
protected $source = 'local';
|
|
protected $position = 'bottom';
|
|
protected $script = '';
|
|
protected $styles = '';
|
|
protected $skipFunction = null;
|
|
protected $isRaw = false;
|
|
protected $isKnownEmpty = false;
|
|
protected $type = ResourceLoaderModule::LOAD_GENERAL;
|
|
protected $targets = [ 'phpunit' ];
|
|
|
|
public function __construct( $options = [] ) {
|
|
foreach ( $options as $key => $value ) {
|
|
$this->$key = $value;
|
|
}
|
|
}
|
|
|
|
public function getScript( ResourceLoaderContext $context ) {
|
|
return $this->validateScriptFile( 'input', $this->script );
|
|
}
|
|
|
|
public function getStyles( ResourceLoaderContext $context ) {
|
|
return [ '' => $this->styles ];
|
|
}
|
|
|
|
public function getMessages() {
|
|
return $this->messages;
|
|
}
|
|
|
|
public function getDependencies( ResourceLoaderContext $context = null ) {
|
|
return $this->dependencies;
|
|
}
|
|
|
|
public function getGroup() {
|
|
return $this->group;
|
|
}
|
|
|
|
public function getSource() {
|
|
return $this->source;
|
|
}
|
|
public function getPosition() {
|
|
return $this->position;
|
|
}
|
|
|
|
public function getType() {
|
|
return $this->type;
|
|
}
|
|
|
|
public function getSkipFunction() {
|
|
return $this->skipFunction;
|
|
}
|
|
|
|
public function isRaw() {
|
|
return $this->isRaw;
|
|
}
|
|
public function isKnownEmpty( ResourceLoaderContext $context ) {
|
|
return $this->isKnownEmpty;
|
|
}
|
|
|
|
public function enableModuleContentVersion() {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
class ResourceLoaderFileModuleTestModule extends ResourceLoaderFileModule {
|
|
}
|
|
|
|
class EmptyResourceLoader extends ResourceLoader {
|
|
// TODO: This won't be needed once ResourceLoader is empty by default
|
|
// and default registrations are done from ServiceWiring instead.
|
|
public function __construct( Config $config = null, LoggerInterface $logger = null ) {
|
|
$this->setLogger( $logger ?: new NullLogger() );
|
|
$this->config = $config ?: MediaWikiServices::getInstance()->getMainConfig();
|
|
// Source "local" is required by StartupModule
|
|
$this->addSource( 'local', $this->config->get( 'LoadScript' ) );
|
|
$this->setMessageBlobStore( new MessageBlobStore( $this, $this->getLogger() ) );
|
|
}
|
|
|
|
public function getErrors() {
|
|
return $this->errors;
|
|
}
|
|
}
|