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' )
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 'ScriptExtension' => '.php',
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 $position = 'bottom';
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
;
129 public function getPosition() {
130 return $this->position
;
133 public function getType() {
137 public function getSkipFunction() {
138 return $this->skipFunction
;
141 public function isRaw() {
144 public function isKnownEmpty( ResourceLoaderContext
$context ) {
145 return $this->isKnownEmpty
;
148 public function shouldEmbedModule( ResourceLoaderContext
$context ) {
149 return $this->shouldEmbed
!== null ?
$this->shouldEmbed
: parent
::shouldEmbedModule( $context );
152 public function enableModuleContentVersion() {
157 class ResourceLoaderFileTestModule
extends ResourceLoaderFileModule
{
158 protected $lessVars = [];
160 public function __construct( $options = [], $test = [] ) {
161 parent
::__construct( $options );
163 foreach ( $test as $key => $value ) {
164 $this->$key = $value;
168 public function getLessVars( ResourceLoaderContext
$context ) {
169 return $this->lessVars
;
173 class ResourceLoaderFileModuleTestModule
extends ResourceLoaderFileModule
{
176 class EmptyResourceLoader
extends ResourceLoader
{
177 // TODO: This won't be needed once ResourceLoader is empty by default
178 // and default registrations are done from ServiceWiring instead.
179 public function __construct( Config
$config = null, LoggerInterface
$logger = null ) {
180 $this->setLogger( $logger ?
: new NullLogger() );
181 $this->config
= $config ?
: MediaWikiServices
::getInstance()->getMainConfig();
182 // Source "local" is required by StartupModule
183 $this->addSource( 'local', $this->config
->get( 'LoadScript' ) );
184 $this->setMessageBlobStore( new MessageBlobStore( $this, $this->getLogger() ) );
187 public function getErrors() {
188 return $this->errors
;