3 use MediaWiki\MediaWikiServices
;
4 use Psr\Log\LoggerInterface
;
5 use Psr\Log\NullLogger
;
7 abstract class ResourceLoaderTestCase
extends MediaWikiTestCase
{
8 // Version hash for a blank file module.
9 // Result of ResourceLoader::makeHash(), ResourceLoaderTestModule
10 // and ResourceLoaderFileModule::getDefinitionSummary().
11 const BLANK_VERSION
= '09p30q0';
14 * @param array|string $options Language code or options array
15 * - string 'lang' Language code
16 * - string 'dir' Language direction (ltr or rtl)
17 * - string 'modules' Pipe-separated list of module names
18 * - string|null 'only' "scripts" (unwrapped script), "styles" (stylesheet), or null
19 * (mw.loader.implement).
20 * @return ResourceLoaderContext
22 protected function getResourceLoaderContext( $options = [], ResourceLoader
$rl = null ) {
23 if ( is_string( $options ) ) {
24 // Back-compat for extension tests
25 $options = [ 'lang' => $options ];
31 'modules' => 'startup',
34 $resourceLoader = $rl ?
: new ResourceLoader();
35 $request = new FauxRequest( [
36 'lang' => $options['lang'],
37 'modules' => $options['modules'],
38 'only' => $options['only'],
39 'skin' => $options['skin'],
40 'target' => 'phpunit',
42 $ctx = $this->getMockBuilder( 'ResourceLoaderContext' )
43 ->setConstructorArgs( [ $resourceLoader, $request ] )
44 ->setMethods( [ 'getDirection' ] )
46 $ctx->method( 'getDirection' )->willReturn( $options['dir'] );
50 public static function getSettings() {
52 // For ResourceLoader::inDebugMode since it doesn't have context
53 'ResourceLoaderDebug' => true,
55 // Avoid influence from wgInvalidateCacheOnLocalSettingsChange
56 'CacheEpoch' => '20140101000000',
58 // For ResourceLoader::__construct()
59 'ResourceLoaderSources' => [],
63 'ScriptExtension' => '.php',
64 'Script' => '/w/index.php',
65 'LoadScript' => '/w/load.php',
69 protected function setUp() {
72 ResourceLoader
::clearCache();
75 foreach ( self
::getSettings() as $key => $value ) {
76 $globals['wg' . $key] = $value;
78 $this->setMwGlobals( $globals );
84 class ResourceLoaderTestModule
extends ResourceLoaderModule
{
85 protected $messages = [];
86 protected $dependencies = [];
87 protected $group = null;
88 protected $source = 'local';
89 protected $position = 'bottom';
90 protected $script = '';
91 protected $styles = '';
92 protected $skipFunction = null;
93 protected $isRaw = false;
94 protected $isKnownEmpty = false;
95 protected $type = ResourceLoaderModule
::LOAD_GENERAL
;
96 protected $targets = [ 'phpunit' ];
98 public function __construct( $options = [] ) {
99 foreach ( $options as $key => $value ) {
100 $this->$key = $value;
104 public function getScript( ResourceLoaderContext
$context ) {
105 return $this->validateScriptFile( 'input', $this->script
);
108 public function getStyles( ResourceLoaderContext
$context ) {
109 return [ '' => $this->styles
];
112 public function getMessages() {
113 return $this->messages
;
116 public function getDependencies( ResourceLoaderContext
$context = null ) {
117 return $this->dependencies
;
120 public function getGroup() {
124 public function getSource() {
125 return $this->source
;
127 public function getPosition() {
128 return $this->position
;
131 public function getType() {
135 public function getSkipFunction() {
136 return $this->skipFunction
;
139 public function isRaw() {
142 public function isKnownEmpty( ResourceLoaderContext
$context ) {
143 return $this->isKnownEmpty
;
146 public function enableModuleContentVersion() {
151 class ResourceLoaderFileModuleTestModule
extends ResourceLoaderFileModule
{
154 class EmptyResourceLoader
extends ResourceLoader
{
155 // TODO: This won't be needed once ResourceLoader is empty by default
156 // and default registrations are done from ServiceWiring instead.
157 public function __construct( Config
$config = null, LoggerInterface
$logger = null ) {
158 $this->setLogger( $logger ?
: new NullLogger() );
159 $this->config
= $config ?
: MediaWikiServices
::getInstance()->getMainConfig();
160 // Source "local" is required by StartupModule
161 $this->addSource( 'local', $this->config
->get( 'LoadScript' ) );
162 $this->setMessageBlobStore( new MessageBlobStore( $this, $this->getLogger() ) );
165 public function getErrors() {
166 return $this->errors
;