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 * @param ResourceLoader|null $rl
21 * @return ResourceLoaderContext
23 protected function getResourceLoaderContext( $options = [], ResourceLoader
$rl = null ) {
24 if ( is_string( $options ) ) {
25 // Back-compat for extension tests
26 $options = [ 'lang' => $options ];
33 'modules' => 'startup',
37 $resourceLoader = $rl ?
: new ResourceLoader();
38 $request = new FauxRequest( [
39 'debug' => $options['debug'],
40 'lang' => $options['lang'],
41 'modules' => $options['modules'],
42 'only' => $options['only'],
43 'safemode' => $options['safemode'],
44 'skin' => $options['skin'],
45 'target' => 'phpunit',
47 $ctx = $this->getMockBuilder( ResourceLoaderContext
::class )
48 ->setConstructorArgs( [ $resourceLoader, $request ] )
49 ->setMethods( [ 'getDirection' ] )
51 $ctx->method( 'getDirection' )->willReturn( $options['dir'] );
55 public static function getSettings() {
57 // For ResourceLoader::inDebugMode since it doesn't have context
58 'ResourceLoaderDebug' => true,
60 // Avoid influence from wgInvalidateCacheOnLocalSettingsChange
61 'CacheEpoch' => '20140101000000',
65 'Script' => '/w/index.php',
66 'LoadScript' => '/w/load.php',
70 protected function setUp() {
73 ResourceLoader
::clearCache();
76 foreach ( self
::getSettings() as $key => $value ) {
77 $globals['wg' . $key] = $value;
79 $this->setMwGlobals( $globals );
85 class ResourceLoaderTestModule
extends ResourceLoaderModule
{
86 protected $messages = [];
87 protected $dependencies = [];
88 protected $group = null;
89 protected $source = 'local';
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' ];
97 protected $shouldEmbed = null;
99 public function __construct( $options = [] ) {
100 foreach ( $options as $key => $value ) {
101 $this->$key = $value;
105 public function getScript( ResourceLoaderContext
$context ) {
106 return $this->validateScriptFile( 'input', $this->script
);
109 public function getStyles( ResourceLoaderContext
$context ) {
110 return [ '' => $this->styles
];
113 public function getMessages() {
114 return $this->messages
;
117 public function getDependencies( ResourceLoaderContext
$context = null ) {
118 return $this->dependencies
;
121 public function getGroup() {
125 public function getSource() {
126 return $this->source
;
129 public function getType() {
133 public function getSkipFunction() {
134 return $this->skipFunction
;
137 public function isRaw() {
140 public function isKnownEmpty( ResourceLoaderContext
$context ) {
141 return $this->isKnownEmpty
;
144 public function shouldEmbedModule( ResourceLoaderContext
$context ) {
145 return $this->shouldEmbed ?? parent
::shouldEmbedModule( $context );
148 public function enableModuleContentVersion() {
153 class ResourceLoaderFileTestModule
extends ResourceLoaderFileModule
{
154 protected $lessVars = [];
156 public function __construct( $options = [], $test = [] ) {
157 parent
::__construct( $options );
159 foreach ( $test as $key => $value ) {
160 $this->$key = $value;
164 public function getLessVars( ResourceLoaderContext
$context ) {
165 return $this->lessVars
;
169 class ResourceLoaderFileModuleTestModule
extends ResourceLoaderFileModule
{
172 class EmptyResourceLoader
extends ResourceLoader
{
173 // TODO: This won't be needed once ResourceLoader is empty by default
174 // and default registrations are done from ServiceWiring instead.
175 public function __construct( Config
$config = null, LoggerInterface
$logger = null ) {
176 $this->setLogger( $logger ?
: new NullLogger() );
177 $this->config
= $config ?
: MediaWikiServices
::getInstance()->getMainConfig();
178 // Source "local" is required by StartupModule
179 $this->addSource( 'local', $this->config
->get( 'LoadScript' ) );
180 $this->setMessageBlobStore( new MessageBlobStore( $this, $this->getLogger() ) );
183 public function getErrors() {
184 return $this->errors
;