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 ];
32 'modules' => 'startup',
36 $resourceLoader = $rl ?
: new ResourceLoader();
37 $request = new FauxRequest( [
38 'lang' => $options['lang'],
39 'modules' => $options['modules'],
40 'only' => $options['only'],
41 'safemode' => $options['safemode'],
42 'skin' => $options['skin'],
43 'target' => 'phpunit',
45 $ctx = $this->getMockBuilder( ResourceLoaderContext
::class )
46 ->setConstructorArgs( [ $resourceLoader, $request ] )
47 ->setMethods( [ 'getDirection' ] )
49 $ctx->method( 'getDirection' )->willReturn( $options['dir'] );
53 public static function getSettings() {
55 // For ResourceLoader::inDebugMode since it doesn't have context
56 'ResourceLoaderDebug' => true,
58 // Avoid influence from wgInvalidateCacheOnLocalSettingsChange
59 'CacheEpoch' => '20140101000000',
61 // For ResourceLoader::__construct()
62 'ResourceLoaderSources' => [],
66 'Script' => '/w/index.php',
67 'LoadScript' => '/w/load.php',
71 protected function setUp() {
74 ResourceLoader
::clearCache();
77 foreach ( self
::getSettings() as $key => $value ) {
78 $globals['wg' . $key] = $value;
80 $this->setMwGlobals( $globals );
86 class ResourceLoaderTestModule
extends ResourceLoaderModule
{
87 protected $messages = [];
88 protected $dependencies = [];
89 protected $group = null;
90 protected $source = 'local';
91 protected $script = '';
92 protected $styles = '';
93 protected $skipFunction = null;
94 protected $isRaw = false;
95 protected $isKnownEmpty = false;
96 protected $type = ResourceLoaderModule
::LOAD_GENERAL
;
97 protected $targets = [ 'phpunit' ];
98 protected $shouldEmbed = null;
100 public function __construct( $options = [] ) {
101 foreach ( $options as $key => $value ) {
102 $this->$key = $value;
106 public function getScript( ResourceLoaderContext
$context ) {
107 return $this->validateScriptFile( 'input', $this->script
);
110 public function getStyles( ResourceLoaderContext
$context ) {
111 return [ '' => $this->styles
];
114 public function getMessages() {
115 return $this->messages
;
118 public function getDependencies( ResourceLoaderContext
$context = null ) {
119 return $this->dependencies
;
122 public function getGroup() {
126 public function getSource() {
127 return $this->source
;
130 public function getType() {
134 public function getSkipFunction() {
135 return $this->skipFunction
;
138 public function isRaw() {
141 public function isKnownEmpty( ResourceLoaderContext
$context ) {
142 return $this->isKnownEmpty
;
145 public function shouldEmbedModule( ResourceLoaderContext
$context ) {
146 return $this->shouldEmbed ?? parent
::shouldEmbedModule( $context );
149 public function enableModuleContentVersion() {
154 class ResourceLoaderFileTestModule
extends ResourceLoaderFileModule
{
155 protected $lessVars = [];
157 public function __construct( $options = [], $test = [] ) {
158 parent
::__construct( $options );
160 foreach ( $test as $key => $value ) {
161 $this->$key = $value;
165 public function getLessVars( ResourceLoaderContext
$context ) {
166 return $this->lessVars
;
170 class ResourceLoaderFileModuleTestModule
extends ResourceLoaderFileModule
{
173 class EmptyResourceLoader
extends ResourceLoader
{
174 // TODO: This won't be needed once ResourceLoader is empty by default
175 // and default registrations are done from ServiceWiring instead.
176 public function __construct( Config
$config = null, LoggerInterface
$logger = null ) {
177 $this->setLogger( $logger ?
: new NullLogger() );
178 $this->config
= $config ?
: MediaWikiServices
::getInstance()->getMainConfig();
179 // Source "local" is required by StartupModule
180 $this->addSource( 'local', $this->config
->get( 'LoadScript' ) );
181 $this->setMessageBlobStore( new MessageBlobStore( $this, $this->getLogger() ) );
184 public function getErrors() {
185 return $this->errors
;