]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - tests/phpunit/ResourceLoaderTestCase.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / tests / phpunit / ResourceLoaderTestCase.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4 use Psr\Log\LoggerInterface;
5 use Psr\Log\NullLogger;
6
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';
12
13         /**
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
22          */
23         protected function getResourceLoaderContext( $options = [], ResourceLoader $rl = null ) {
24                 if ( is_string( $options ) ) {
25                         // Back-compat for extension tests
26                         $options = [ 'lang' => $options ];
27                 }
28                 $options += [
29                         'lang' => 'en',
30                         'dir' => 'ltr',
31                         'skin' => 'vector',
32                         'modules' => 'startup',
33                         'only' => 'scripts',
34                 ];
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',
42                 ] );
43                 $ctx = $this->getMockBuilder( 'ResourceLoaderContext' )
44                         ->setConstructorArgs( [ $resourceLoader, $request ] )
45                         ->setMethods( [ 'getDirection' ] )
46                         ->getMock();
47                 $ctx->method( 'getDirection' )->willReturn( $options['dir'] );
48                 return $ctx;
49         }
50
51         public static function getSettings() {
52                 return [
53                         // For ResourceLoader::inDebugMode since it doesn't have context
54                         'ResourceLoaderDebug' => true,
55
56                         // Avoid influence from wgInvalidateCacheOnLocalSettingsChange
57                         'CacheEpoch' => '20140101000000',
58
59                         // For ResourceLoader::__construct()
60                         'ResourceLoaderSources' => [],
61
62                         // For wfScript()
63                         'ScriptPath' => '/w',
64                         'ScriptExtension' => '.php',
65                         'Script' => '/w/index.php',
66                         'LoadScript' => '/w/load.php',
67                 ];
68         }
69
70         protected function setUp() {
71                 parent::setUp();
72
73                 ResourceLoader::clearCache();
74
75                 $globals = [];
76                 foreach ( self::getSettings() as $key => $value ) {
77                         $globals['wg' . $key] = $value;
78                 }
79                 $this->setMwGlobals( $globals );
80         }
81 }
82
83 /* Stubs */
84
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;
99
100         public function __construct( $options = [] ) {
101                 foreach ( $options as $key => $value ) {
102                         $this->$key = $value;
103                 }
104         }
105
106         public function getScript( ResourceLoaderContext $context ) {
107                 return $this->validateScriptFile( 'input', $this->script );
108         }
109
110         public function getStyles( ResourceLoaderContext $context ) {
111                 return [ '' => $this->styles ];
112         }
113
114         public function getMessages() {
115                 return $this->messages;
116         }
117
118         public function getDependencies( ResourceLoaderContext $context = null ) {
119                 return $this->dependencies;
120         }
121
122         public function getGroup() {
123                 return $this->group;
124         }
125
126         public function getSource() {
127                 return $this->source;
128         }
129         public function getPosition() {
130                 return $this->position;
131         }
132
133         public function getType() {
134                 return $this->type;
135         }
136
137         public function getSkipFunction() {
138                 return $this->skipFunction;
139         }
140
141         public function isRaw() {
142                 return $this->isRaw;
143         }
144         public function isKnownEmpty( ResourceLoaderContext $context ) {
145                 return $this->isKnownEmpty;
146         }
147
148         public function shouldEmbedModule( ResourceLoaderContext $context ) {
149                 return $this->shouldEmbed !== null ? $this->shouldEmbed : parent::shouldEmbedModule( $context );
150         }
151
152         public function enableModuleContentVersion() {
153                 return true;
154         }
155 }
156
157 class ResourceLoaderFileTestModule extends ResourceLoaderFileModule {
158         protected $lessVars = [];
159
160         public function __construct( $options = [], $test = [] ) {
161                 parent::__construct( $options );
162
163                 foreach ( $test as $key => $value ) {
164                         $this->$key = $value;
165                 }
166         }
167
168         public function getLessVars( ResourceLoaderContext $context ) {
169                 return $this->lessVars;
170         }
171 }
172
173 class ResourceLoaderFileModuleTestModule extends ResourceLoaderFileModule {
174 }
175
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() ) );
185         }
186
187         public function getErrors() {
188                 return $this->errors;
189         }
190 }