wiki.techinc.nl/tests/phpunit/ResourceLoaderTestCase.php
Timo Tijhof dbe592df6d resourceloader: Fix WikiModule preload to support localised titles
Follows-up 6f8dc27ca2 and dbd11e04aa. You'd expect a bug in preloading
to fallback to fetching it on-demand but due to a title format mismatch
the preload method did use the same title format for the cache key, but
not the same title format for discovering the results. As such, it set
the right cache key to an empty array.

* Make relevant methods testable and mockable.
* Add regression test.
* Change call to array_interect_key to use the same format as
  fetchTitleInfo(): Normalised title keys from Title::getPrefixedText.
  Previously, the intersect used the declared titles from getPages() which
  are not localised - causing an empty array to be returned from the
  intersect on wikis where the namespace name is localised.

Bug: T145673
Change-Id: Ibe788157724d73c727b9e2127b6828db32ca9420
2016-09-14 14:54:01 -07:00

141 lines
3.5 KiB
PHP

<?php
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
abstract class ResourceLoaderTestCase extends MediaWikiTestCase {
/**
* @param string $lang
* @param string $dir
* @return ResourceLoaderContext
*/
protected function getResourceLoaderContext( $lang = 'en', $dir = 'ltr' ) {
$resourceLoader = new ResourceLoader();
$request = new FauxRequest( [
'lang' => $lang,
'modules' => 'startup',
'only' => 'scripts',
'skin' => 'vector',
'target' => 'phpunit',
] );
$ctx = $this->getMockBuilder( 'ResourceLoaderContext' )
->setConstructorArgs( [ $resourceLoader, $request ] )
->setMethods( [ 'getDirection' ] )
->getMock();
$ctx->method( 'getDirection' )->willReturn( $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 ?: ConfigFactory::getDefaultInstance()->makeConfig( 'main' );
$this->setMessageBlobStore( new MessageBlobStore( $this, $this->getLogger() ) );
}
}