Follow-up Ifa346c8a92: LanguageNameUtils: CONSTRUCTOR_OTPIONS, not constructorOptions
[lhc/web/wiklou.git] / tests / phpunit / ResourceLoaderTestCase.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4 use Psr\Log\LoggerInterface;
5
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 = '9p30q';
11 // Result of ResoureLoader::makeVersionQuery() for a blank file module.
12 // In other words, result of ResourceLoader::makeHash( BLANK_VERSION );
13 const BLANK_COMBI = 'rbml8';
14
15 /**
16 * @param array|string $options Language code or options array
17 * - string 'lang' Language code
18 * - string 'dir' Language direction (ltr or rtl)
19 * - string 'modules' Pipe-separated list of module names
20 * - string|null 'only' "scripts" (unwrapped script), "styles" (stylesheet), or null
21 * (mw.loader.implement).
22 * @param ResourceLoader|null $rl
23 * @return ResourceLoaderContext
24 */
25 protected function getResourceLoaderContext( $options = [], ResourceLoader $rl = null ) {
26 if ( is_string( $options ) ) {
27 // Back-compat for extension tests
28 $options = [ 'lang' => $options ];
29 }
30 $options += [
31 'debug' => 'true',
32 'lang' => 'en',
33 'dir' => 'ltr',
34 'skin' => 'fallback',
35 'modules' => 'startup',
36 'only' => 'scripts',
37 'safemode' => null,
38 ];
39 $resourceLoader = $rl ?: new ResourceLoader( MediaWikiServices::getInstance()->getMainConfig() );
40 $request = new FauxRequest( [
41 'debug' => $options['debug'],
42 'lang' => $options['lang'],
43 'modules' => $options['modules'],
44 'only' => $options['only'],
45 'safemode' => $options['safemode'],
46 'skin' => $options['skin'],
47 'target' => 'phpunit',
48 ] );
49 $ctx = $this->getMockBuilder( ResourceLoaderContext::class )
50 ->setConstructorArgs( [ $resourceLoader, $request ] )
51 ->setMethods( [ 'getDirection' ] )
52 ->getMock();
53 $ctx->method( 'getDirection' )->willReturn( $options['dir'] );
54 return $ctx;
55 }
56
57 public static function getSettings() {
58 return [
59 // For ResourceLoader::inDebugMode since it doesn't have context
60 'ResourceLoaderDebug' => true,
61
62 // For ResourceLoaderStartUpModule and ResourceLoader::__construct()
63 'ScriptPath' => '/w',
64 'Script' => '/w/index.php',
65 'LoadScript' => '/w/load.php',
66
67 // For ResourceLoader::register() - TODO: Inject somehow T32956
68 'ResourceModuleSkinStyles' => [],
69
70 // For ResourceLoader::respond() - TODO: Inject somehow T32956
71 'UseFileCache' => false,
72 ];
73 }
74
75 public static function getMinimalConfig() {
76 return new HashConfig( self::getSettings() );
77 }
78
79 protected function setUp() {
80 parent::setUp();
81
82 ResourceLoader::clearCache();
83
84 $globals = [];
85 foreach ( self::getSettings() as $key => $value ) {
86 $globals['wg' . $key] = $value;
87 }
88 $this->setMwGlobals( $globals );
89 }
90 }
91
92 /* Stubs */
93
94 class ResourceLoaderTestModule extends ResourceLoaderModule {
95 protected $messages = [];
96 protected $dependencies = [];
97 protected $group = null;
98 protected $source = 'local';
99 protected $script = '';
100 protected $styles = '';
101 protected $skipFunction = null;
102 protected $isRaw = false;
103 protected $isKnownEmpty = false;
104 protected $type = ResourceLoaderModule::LOAD_GENERAL;
105 protected $targets = [ 'phpunit' ];
106 protected $shouldEmbed = null;
107 protected $mayValidateScript = false;
108
109 public function __construct( $options = [] ) {
110 foreach ( $options as $key => $value ) {
111 $this->$key = $value;
112 }
113 }
114
115 public function getScript( ResourceLoaderContext $context ) {
116 if ( $this->mayValidateScript ) {
117 // This enables the validation check that replaces invalid
118 // scripts with a warning message.
119 // Based on $wgResourceLoaderValidateJS
120 return $this->validateScriptFile( 'input', $this->script );
121 } else {
122 return $this->script;
123 }
124 }
125
126 public function getStyles( ResourceLoaderContext $context ) {
127 return [ '' => $this->styles ];
128 }
129
130 public function getMessages() {
131 return $this->messages;
132 }
133
134 public function getDependencies( ResourceLoaderContext $context = null ) {
135 return $this->dependencies;
136 }
137
138 public function getGroup() {
139 return $this->group;
140 }
141
142 public function getSource() {
143 return $this->source;
144 }
145
146 public function getType() {
147 return $this->type;
148 }
149
150 public function getSkipFunction() {
151 return $this->skipFunction;
152 }
153
154 public function isRaw() {
155 return $this->isRaw;
156 }
157
158 public function isKnownEmpty( ResourceLoaderContext $context ) {
159 return $this->isKnownEmpty;
160 }
161
162 public function shouldEmbedModule( ResourceLoaderContext $context ) {
163 return $this->shouldEmbed ?? parent::shouldEmbedModule( $context );
164 }
165
166 public function enableModuleContentVersion() {
167 return true;
168 }
169 }
170
171 /**
172 * A more constrained and testable variant of ResourceLoaderFileModule.
173 *
174 * - Implements getLessVars() support.
175 * - Disables database persistance of discovered file dependencies.
176 */
177 class ResourceLoaderFileTestModule extends ResourceLoaderFileModule {
178 protected $lessVars = [];
179
180 public function __construct( $options = [] ) {
181 if ( isset( $options['lessVars'] ) ) {
182 $this->lessVars = $options['lessVars'];
183 unset( $options['lessVars'] );
184 }
185
186 parent::__construct( $options );
187 }
188
189 public function getLessVars( ResourceLoaderContext $context ) {
190 return $this->lessVars;
191 }
192
193 /** @return array */
194 protected function getFileDependencies( ResourceLoaderContext $context ) {
195 // No-op
196 return [];
197 }
198
199 protected function saveFileDependencies( ResourceLoaderContext $context, $refs ) {
200 // No-op
201 }
202 }
203
204 class ResourceLoaderFileModuleTestingSubclass extends ResourceLoaderFileModule {
205 }
206
207 class EmptyResourceLoader extends ResourceLoader {
208 public function __construct( Config $config = null, LoggerInterface $logger = null ) {
209 parent::__construct( $config ?: ResourceLoaderTestCase::getMinimalConfig(), $logger );
210 }
211
212 public function getErrors() {
213 return $this->errors;
214 }
215 }