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',
35 $resourceLoader = $rl ?
: new ResourceLoader();
36 $request = new FauxRequest( [
37 'lang' => $options['lang'],
38 'modules' => $options['modules'],
39 'only' => $options['only'],
40 'skin' => $options['skin'],
41 'target' => 'phpunit',
43 $ctx = $this->getMockBuilder( ResourceLoaderContext
::class )
44 ->setConstructorArgs( [ $resourceLoader, $request ] )
45 ->setMethods( [ 'getDirection' ] )
47 $ctx->method( 'getDirection' )->willReturn( $options['dir'] );
51 public static function getSettings() {
53 // For ResourceLoader::inDebugMode since it doesn't have context
54 'ResourceLoaderDebug' => true,
56 // Avoid influence from wgInvalidateCacheOnLocalSettingsChange
57 'CacheEpoch' => '20140101000000',
59 // For ResourceLoader::__construct()
60 'ResourceLoaderSources' => [],
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 $script = '';
90 protected $styles = '';
91 protected $skipFunction = null;
92 protected $isRaw = false;
93 protected $isKnownEmpty = false;
94 protected $type = ResourceLoaderModule
::LOAD_GENERAL
;
95 protected $targets = [ 'phpunit' ];
96 protected $shouldEmbed = null;
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
;
128 public function getType() {
132 public function getSkipFunction() {
133 return $this->skipFunction
;
136 public function isRaw() {
139 public function isKnownEmpty( ResourceLoaderContext
$context ) {
140 return $this->isKnownEmpty
;
143 public function shouldEmbedModule( ResourceLoaderContext
$context ) {
144 return $this->shouldEmbed
!== null ?
$this->shouldEmbed
: parent
::shouldEmbedModule( $context );
147 public function enableModuleContentVersion() {
152 class ResourceLoaderFileTestModule
extends ResourceLoaderFileModule
{
153 protected $lessVars = [];
155 public function __construct( $options = [], $test = [] ) {
156 parent
::__construct( $options );
158 foreach ( $test as $key => $value ) {
159 $this->$key = $value;
163 public function getLessVars( ResourceLoaderContext
$context ) {
164 return $this->lessVars
;
168 class ResourceLoaderFileModuleTestModule
extends ResourceLoaderFileModule
{
171 class EmptyResourceLoader
extends ResourceLoader
{
172 // TODO: This won't be needed once ResourceLoader is empty by default
173 // and default registrations are done from ServiceWiring instead.
174 public function __construct( Config
$config = null, LoggerInterface
$logger = null ) {
175 $this->setLogger( $logger ?
: new NullLogger() );
176 $this->config
= $config ?
: MediaWikiServices
::getInstance()->getMainConfig();
177 // Source "local" is required by StartupModule
178 $this->addSource( 'local', $this->config
->get( 'LoadScript' ) );
179 $this->setMessageBlobStore( new MessageBlobStore( $this, $this->getLogger() ) );
182 public function getErrors() {
183 return $this->errors
;