3 use MediaWiki\MediaWikiServices
;
4 use Psr\Log\LoggerInterface
;
6 abstract class ResourceLoaderTestCase
extends MediaWikiTestCase
{
7 // Version hash for a blank file module.
8 // Result of ResourceLoader::makeHash(), ResourceLoaderTestModule
9 // and ResourceLoaderFileModule::getDefinitionSummary().
10 const BLANK_VERSION
= '09p30q0';
13 * @param array|string $options Language code or options array
14 * - string 'lang' Language code
15 * - string 'dir' Language direction (ltr or rtl)
16 * - string 'modules' Pipe-separated list of module names
17 * - string|null 'only' "scripts" (unwrapped script), "styles" (stylesheet), or null
18 * (mw.loader.implement).
19 * @param ResourceLoader|null $rl
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 ];
32 'modules' => 'startup',
36 $resourceLoader = $rl ?
: new ResourceLoader( MediaWikiServices
::getInstance()->getMainConfig() );
37 $request = new FauxRequest( [
38 'debug' => $options['debug'],
39 'lang' => $options['lang'],
40 'modules' => $options['modules'],
41 'only' => $options['only'],
42 'safemode' => $options['safemode'],
43 'skin' => $options['skin'],
44 'target' => 'phpunit',
46 $ctx = $this->getMockBuilder( ResourceLoaderContext
::class )
47 ->setConstructorArgs( [ $resourceLoader, $request ] )
48 ->setMethods( [ 'getDirection' ] )
50 $ctx->method( 'getDirection' )->willReturn( $options['dir'] );
54 public static function getSettings() {
56 // For ResourceLoader::inDebugMode since it doesn't have context
57 'ResourceLoaderDebug' => true,
59 // For ResourceLoaderStartUpModule and ResourceLoader::__construct()
61 'Script' => '/w/index.php',
62 'LoadScript' => '/w/load.php',
64 // For ResourceLoader::register() - TODO: Inject somehow T32956
65 'ResourceModuleSkinStyles' => [],
67 // For ResourceLoader::respond() - TODO: Inject somehow T32956
68 'UseFileCache' => false,
72 public static function getMinimalConfig() {
73 return new HashConfig( self
::getSettings() );
76 protected function setUp() {
79 ResourceLoader
::clearCache();
82 foreach ( self
::getSettings() as $key => $value ) {
83 $globals['wg' . $key] = $value;
85 $this->setMwGlobals( $globals );
91 class ResourceLoaderTestModule
extends ResourceLoaderModule
{
92 protected $messages = [];
93 protected $dependencies = [];
94 protected $group = null;
95 protected $source = 'local';
96 protected $script = '';
97 protected $styles = '';
98 protected $skipFunction = null;
99 protected $isRaw = false;
100 protected $isKnownEmpty = false;
101 protected $type = ResourceLoaderModule
::LOAD_GENERAL
;
102 protected $targets = [ 'phpunit' ];
103 protected $shouldEmbed = null;
105 public function __construct( $options = [] ) {
106 foreach ( $options as $key => $value ) {
107 $this->$key = $value;
111 public function getScript( ResourceLoaderContext
$context ) {
112 return $this->validateScriptFile( 'input', $this->script
);
115 public function getStyles( ResourceLoaderContext
$context ) {
116 return [ '' => $this->styles
];
119 public function getMessages() {
120 return $this->messages
;
123 public function getDependencies( ResourceLoaderContext
$context = null ) {
124 return $this->dependencies
;
127 public function getGroup() {
131 public function getSource() {
132 return $this->source
;
135 public function getType() {
139 public function getSkipFunction() {
140 return $this->skipFunction
;
143 public function isRaw() {
147 public function isKnownEmpty( ResourceLoaderContext
$context ) {
148 return $this->isKnownEmpty
;
151 public function shouldEmbedModule( ResourceLoaderContext
$context ) {
152 return $this->shouldEmbed ?? parent
::shouldEmbedModule( $context );
155 public function enableModuleContentVersion() {
160 class ResourceLoaderFileTestModule
extends ResourceLoaderFileModule
{
161 protected $lessVars = [];
163 public function __construct( $options = [] ) {
164 if ( isset( $options['lessVars'] ) ) {
165 $this->lessVars
= $options['lessVars'];
166 unset( $options['lessVars'] );
169 parent
::__construct( $options );
172 public function getLessVars( ResourceLoaderContext
$context ) {
173 return $this->lessVars
;
177 class ResourceLoaderFileModuleTestModule
extends ResourceLoaderFileModule
{
180 class EmptyResourceLoader
extends ResourceLoader
{
181 public function __construct( Config
$config = null, LoggerInterface
$logger = null ) {
182 parent
::__construct( $config ?
: ResourceLoaderTestCase
::getMinimalConfig(), $logger );
185 public function getErrors() {
186 return $this->errors
;