Remove "Squiz.WhiteSpace.FunctionSpacing" from phpcs exclusions
[lhc/web/wiklou.git] / tests / phpunit / includes / resourceloader / ResourceLoaderWikiModuleTest.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4 use Wikimedia\Rdbms\IDatabase;
5 use Wikimedia\TestingAccessWrapper;
6
7 class ResourceLoaderWikiModuleTest extends ResourceLoaderTestCase {
8
9 /**
10 * @covers ResourceLoaderWikiModule::__construct
11 * @dataProvider provideConstructor
12 */
13 public function testConstructor( $params ) {
14 $module = new ResourceLoaderWikiModule( $params );
15 $this->assertInstanceOf( ResourceLoaderWikiModule::class, $module );
16 }
17
18 private function prepareTitleInfo( array $mockInfo ) {
19 $module = TestingAccessWrapper::newFromClass( ResourceLoaderWikiModule::class );
20 $info = [];
21 foreach ( $mockInfo as $key => $val ) {
22 $info[ $module->makeTitleKey( Title::newFromText( $key ) ) ] = $val;
23 }
24 return $info;
25 }
26
27 public static function provideConstructor() {
28 return [
29 // Nothing
30 [ null ],
31 [ [] ],
32 // Unrecognized settings
33 [ [ 'foo' => 'baz' ] ],
34 // Real settings
35 [ [ 'scripts' => [ 'MediaWiki:Common.js' ] ] ],
36 ];
37 }
38
39 /**
40 * @dataProvider provideGetPages
41 * @covers ResourceLoaderWikiModule::getPages
42 */
43 public function testGetPages( $params, Config $config, $expected ) {
44 $module = new ResourceLoaderWikiModule( $params );
45 $module->setConfig( $config );
46
47 // Because getPages is protected..
48 $getPages = new ReflectionMethod( $module, 'getPages' );
49 $getPages->setAccessible( true );
50 $out = $getPages->invoke( $module, ResourceLoaderContext::newDummyContext() );
51 $this->assertEquals( $expected, $out );
52 }
53
54 public static function provideGetPages() {
55 $settings = self::getSettings() + [
56 'UseSiteJs' => true,
57 'UseSiteCss' => true,
58 ];
59
60 $params = [
61 'styles' => [ 'MediaWiki:Common.css' ],
62 'scripts' => [ 'MediaWiki:Common.js' ],
63 ];
64
65 return [
66 [ [], new HashConfig( $settings ), [] ],
67 [ $params, new HashConfig( $settings ), [
68 'MediaWiki:Common.js' => [ 'type' => 'script' ],
69 'MediaWiki:Common.css' => [ 'type' => 'style' ]
70 ] ],
71 [ $params, new HashConfig( [ 'UseSiteCss' => false ] + $settings ), [
72 'MediaWiki:Common.js' => [ 'type' => 'script' ],
73 ] ],
74 [ $params, new HashConfig( [ 'UseSiteJs' => false ] + $settings ), [
75 'MediaWiki:Common.css' => [ 'type' => 'style' ],
76 ] ],
77 [ $params,
78 new HashConfig(
79 [ 'UseSiteJs' => false, 'UseSiteCss' => false ]
80 ),
81 []
82 ],
83 ];
84 }
85
86 /**
87 * @covers ResourceLoaderWikiModule::getGroup
88 * @dataProvider provideGetGroup
89 */
90 public function testGetGroup( $params, $expected ) {
91 $module = new ResourceLoaderWikiModule( $params );
92 $this->assertEquals( $expected, $module->getGroup() );
93 }
94
95 public static function provideGetGroup() {
96 return [
97 // No group specified
98 [ [], null ],
99 // A random group
100 [ [ 'group' => 'foobar' ], 'foobar' ],
101 ];
102 }
103
104 /**
105 * @covers ResourceLoaderWikiModule::isKnownEmpty
106 * @dataProvider provideIsKnownEmpty
107 */
108 public function testIsKnownEmpty( $titleInfo, $group, $dependencies, $expected ) {
109 $module = $this->getMockBuilder( ResourceLoaderWikiModule::class )
110 ->setMethods( [ 'getTitleInfo', 'getGroup', 'getDependencies' ] )
111 ->getMock();
112 $module->expects( $this->any() )
113 ->method( 'getTitleInfo' )
114 ->will( $this->returnValue( $this->prepareTitleInfo( $titleInfo ) ) );
115 $module->expects( $this->any() )
116 ->method( 'getGroup' )
117 ->will( $this->returnValue( $group ) );
118 $module->expects( $this->any() )
119 ->method( 'getDependencies' )
120 ->will( $this->returnValue( $dependencies ) );
121 $context = $this->getMockBuilder( ResourceLoaderContext::class )
122 ->disableOriginalConstructor()
123 ->getMock();
124 $this->assertEquals( $expected, $module->isKnownEmpty( $context ) );
125 }
126
127 public static function provideIsKnownEmpty() {
128 return [
129 // No valid pages
130 [ [], 'test1', [], true ],
131 // 'site' module with a non-empty page
132 [
133 [ 'MediaWiki:Common.js' => [ 'page_len' => 1234 ] ],
134 'site',
135 [],
136 false,
137 ],
138 // 'site' module without existing pages but dependencies
139 [
140 [],
141 'site',
142 [ 'mobile.css' ],
143 false,
144 ],
145 // 'site' module which is empty but has dependencies
146 [
147 [ 'MediaWiki:Common.js' => [ 'page_len' => 0 ] ],
148 'site',
149 [ 'mobile.css' ],
150 false,
151 ],
152 // 'site' module with an empty page
153 [
154 [ 'MediaWiki:Foo.js' => [ 'page_len' => 0 ] ],
155 'site',
156 [],
157 false,
158 ],
159 // 'user' module with a non-empty page
160 [
161 [ 'User:Example/common.js' => [ 'page_len' => 25 ] ],
162 'user',
163 [],
164 false,
165 ],
166 // 'user' module with an empty page
167 [
168 [ 'User:Example/foo.js' => [ 'page_len' => 0 ] ],
169 'user',
170 [],
171 true,
172 ],
173 ];
174 }
175
176 /**
177 * @covers ResourceLoaderWikiModule::getTitleInfo
178 */
179 public function testGetTitleInfo() {
180 $pages = [
181 'MediaWiki:Common.css' => [ 'type' => 'styles' ],
182 'mediawiki: fallback.css' => [ 'type' => 'styles' ],
183 ];
184 $titleInfo = $this->prepareTitleInfo( [
185 'MediaWiki:Common.css' => [ 'page_len' => 1234 ],
186 'MediaWiki:Fallback.css' => [ 'page_len' => 0 ],
187 ] );
188 $expected = $titleInfo;
189
190 $module = $this->getMockBuilder( TestResourceLoaderWikiModule::class )
191 ->setMethods( [ 'getPages' ] )
192 ->getMock();
193 $module->method( 'getPages' )->willReturn( $pages );
194 // Can't mock static methods
195 $module::$returnFetchTitleInfo = $titleInfo;
196
197 $context = $this->getMockBuilder( ResourceLoaderContext::class )
198 ->disableOriginalConstructor()
199 ->getMock();
200
201 $module = TestingAccessWrapper::newFromObject( $module );
202 $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' );
203 }
204
205 /**
206 * @covers ResourceLoaderWikiModule::getTitleInfo
207 * @covers ResourceLoaderWikiModule::setTitleInfo
208 * @covers ResourceLoaderWikiModule::preloadTitleInfo
209 */
210 public function testGetPreloadedTitleInfo() {
211 $pages = [
212 'MediaWiki:Common.css' => [ 'type' => 'styles' ],
213 // Regression against T145673. It's impossible to statically declare page names in
214 // a canonical way since the canonical prefix is localised. As such, the preload
215 // cache computed the right cache key, but failed to find the results when
216 // doing an intersect on the canonical result, producing an empty array.
217 'mediawiki: fallback.css' => [ 'type' => 'styles' ],
218 ];
219 $titleInfo = $this->prepareTitleInfo( [
220 'MediaWiki:Common.css' => [ 'page_len' => 1234 ],
221 'MediaWiki:Fallback.css' => [ 'page_len' => 0 ],
222 ] );
223 $expected = $titleInfo;
224
225 $module = $this->getMockBuilder( TestResourceLoaderWikiModule::class )
226 ->setMethods( [ 'getPages' ] )
227 ->getMock();
228 $module->method( 'getPages' )->willReturn( $pages );
229 // Can't mock static methods
230 $module::$returnFetchTitleInfo = $titleInfo;
231
232 $rl = new EmptyResourceLoader();
233 $rl->register( 'testmodule', $module );
234 $context = new ResourceLoaderContext( $rl, new FauxRequest() );
235
236 TestResourceLoaderWikiModule::invalidateModuleCache(
237 Title::newFromText( 'MediaWiki:Common.css' ),
238 null,
239 null,
240 wfWikiID()
241 );
242 TestResourceLoaderWikiModule::preloadTitleInfo(
243 $context,
244 wfGetDB( DB_REPLICA ),
245 [ 'testmodule' ]
246 );
247
248 $module = TestingAccessWrapper::newFromObject( $module );
249 $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' );
250 }
251
252 /**
253 * @covers ResourceLoaderWikiModule::preloadTitleInfo
254 */
255 public function testGetPreloadedBadTitle() {
256 // Mock values
257 $pages = [
258 // Covers else branch for invalid page name
259 '[x]' => [ 'type' => 'styles' ],
260 ];
261 $titleInfo = [];
262
263 // Set up objects
264 $module = $this->getMockBuilder( TestResourceLoaderWikiModule::class )
265 ->setMethods( [ 'getPages' ] )->getMock();
266 $module->method( 'getPages' )->willReturn( $pages );
267 $module::$returnFetchTitleInfo = $titleInfo;
268 $rl = new EmptyResourceLoader();
269 $rl->register( 'testmodule', $module );
270 $context = new ResourceLoaderContext( $rl, new FauxRequest() );
271
272 // Act
273 TestResourceLoaderWikiModule::preloadTitleInfo(
274 $context,
275 wfGetDB( DB_REPLICA ),
276 [ 'testmodule' ]
277 );
278
279 // Assert
280 $module = TestingAccessWrapper::newFromObject( $module );
281 $this->assertEquals( $titleInfo, $module->getTitleInfo( $context ), 'Title info' );
282 }
283
284 /**
285 * @covers ResourceLoaderWikiModule::preloadTitleInfo
286 */
287 public function testGetPreloadedTitleInfoEmpty() {
288 $context = new ResourceLoaderContext( new EmptyResourceLoader(), new FauxRequest() );
289 // Covers early return
290 $this->assertSame(
291 null,
292 ResourceLoaderWikiModule::preloadTitleInfo(
293 $context,
294 wfGetDB( DB_REPLICA ),
295 []
296 )
297 );
298 }
299
300 public static function provideGetContent() {
301 return [
302 'Bad title' => [ null, '[x]' ],
303 'Dead redirect' => [ null, [
304 'text' => 'Dead redirect',
305 'title' => 'Dead_redirect',
306 'redirect' => 1,
307 ] ],
308 'Bad content model' => [ null, [
309 'text' => 'MediaWiki:Wikitext',
310 'ns' => NS_MEDIAWIKI,
311 'title' => 'Wikitext',
312 ] ],
313 'No JS content found' => [ null, [
314 'text' => 'MediaWiki:Script.js',
315 'ns' => NS_MEDIAWIKI,
316 'title' => 'Script.js',
317 ] ],
318 'No CSS content found' => [ null, [
319 'text' => 'MediaWiki:Styles.css',
320 'ns' => NS_MEDIAWIKI,
321 'title' => 'Script.css',
322 ] ],
323 ];
324 }
325
326 /**
327 * @covers ResourceLoaderWikiModule::getContent
328 * @dataProvider provideGetContent
329 */
330 public function testGetContent( $expected, $title ) {
331 $context = $this->getResourceLoaderContext( [], new EmptyResourceLoader );
332 $module = $this->getMockBuilder( ResourceLoaderWikiModule::class )
333 ->setMethods( [ 'getContentObj' ] )->getMock();
334 $module->expects( $this->any() )
335 ->method( 'getContentObj' )->willReturn( null );
336
337 if ( is_array( $title ) ) {
338 $title += [ 'ns' => NS_MAIN, 'id' => 1, 'len' => 1, 'redirect' => 0 ];
339 $titleText = $title['text'];
340 // Mock Title db access via LinkCache
341 MediaWikiServices::getInstance()->getLinkCache()->addGoodLinkObj(
342 $title['id'],
343 new TitleValue( $title['ns'], $title['title'] ),
344 $title['len'],
345 $title['redirect']
346 );
347 } else {
348 $titleText = $title;
349 }
350
351 $module = TestingAccessWrapper::newFromObject( $module );
352 $this->assertEquals(
353 $expected,
354 $module->getContent( $titleText, $context )
355 );
356 }
357
358 /**
359 * @covers ResourceLoaderWikiModule::getContent
360 * @covers ResourceLoaderWikiModule::getContentObj
361 * @covers ResourceLoaderWikiModule::shouldEmbedModule
362 */
363 public function testContentOverrides() {
364 $pages = [
365 'MediaWiki:Common.css' => [ 'type' => 'style' ],
366 ];
367
368 $module = $this->getMockBuilder( TestResourceLoaderWikiModule::class )
369 ->setMethods( [ 'getPages' ] )
370 ->getMock();
371 $module->method( 'getPages' )->willReturn( $pages );
372
373 $rl = new EmptyResourceLoader();
374 $rl->register( 'testmodule', $module );
375 $context = new DerivativeResourceLoaderContext(
376 new ResourceLoaderContext( $rl, new FauxRequest() )
377 );
378 $context->setContentOverrideCallback( function ( Title $t ) {
379 if ( $t->getPrefixedText() === 'MediaWiki:Common.css' ) {
380 return new CssContent( '.override{}' );
381 }
382 return null;
383 } );
384
385 $this->assertTrue( $module->shouldEmbedModule( $context ) );
386 $this->assertEquals( [
387 'all' => [
388 "/*\nMediaWiki:Common.css\n*/\n.override{}"
389 ]
390 ], $module->getStyles( $context ) );
391
392 $context->setContentOverrideCallback( function ( Title $t ) {
393 if ( $t->getPrefixedText() === 'MediaWiki:Skin.css' ) {
394 return new CssContent( '.override{}' );
395 }
396 return null;
397 } );
398 $this->assertFalse( $module->shouldEmbedModule( $context ) );
399 }
400
401 /**
402 * @covers ResourceLoaderWikiModule::getContent
403 * @covers ResourceLoaderWikiModule::getContentObj
404 */
405 public function testGetContentForRedirects() {
406 // Set up context and module object
407 $context = new DerivativeResourceLoaderContext(
408 $this->getResourceLoaderContext( [], new EmptyResourceLoader )
409 );
410 $module = $this->getMockBuilder( ResourceLoaderWikiModule::class )
411 ->setMethods( [ 'getPages' ] )
412 ->getMock();
413 $module->expects( $this->any() )
414 ->method( 'getPages' )
415 ->will( $this->returnValue( [
416 'MediaWiki:Redirect.js' => [ 'type' => 'script' ]
417 ] ) );
418 $context->setContentOverrideCallback( function ( Title $title ) {
419 if ( $title->getPrefixedText() === 'MediaWiki:Redirect.js' ) {
420 $handler = new JavaScriptContentHandler();
421 return $handler->makeRedirectContent(
422 Title::makeTitle( NS_MEDIAWIKI, 'Target.js' )
423 );
424 } elseif ( $title->getPrefixedText() === 'MediaWiki:Target.js' ) {
425 return new JavaScriptContent( 'target;' );
426 } else {
427 return null;
428 }
429 } );
430
431 // Mock away Title's db queries with LinkCache
432 MediaWikiServices::getInstance()->getLinkCache()->addGoodLinkObj(
433 1, // id
434 new TitleValue( NS_MEDIAWIKI, 'Redirect.js' ),
435 1, // len
436 1 // redirect
437 );
438
439 $this->assertEquals(
440 "/*\nMediaWiki:Redirect.js\n*/\ntarget;\n",
441 $module->getScript( $context ),
442 'Redirect resolved by getContent'
443 );
444 }
445
446 function tearDown() {
447 Title::clearCaches();
448 parent::tearDown();
449 }
450 }
451
452 class TestResourceLoaderWikiModule extends ResourceLoaderWikiModule {
453 public static $returnFetchTitleInfo = null;
454
455 protected static function fetchTitleInfo( IDatabase $db, array $pages, $fname = null ) {
456 $ret = self::$returnFetchTitleInfo;
457 self::$returnFetchTitleInfo = null;
458 return $ret;
459 }
460 }