Merge "Title: Title::getSubpage should not lose the interwiki prefix"
[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 $context = new ResourceLoaderContext( $rl, new FauxRequest() );
234
235 TestResourceLoaderWikiModule::invalidateModuleCache(
236 Title::newFromText( 'MediaWiki:Common.css' ),
237 null,
238 null,
239 wfWikiID()
240 );
241 TestResourceLoaderWikiModule::preloadTitleInfo(
242 $context,
243 wfGetDB( DB_REPLICA ),
244 [ 'testmodule' ]
245 );
246
247 $module = TestingAccessWrapper::newFromObject( $module );
248 $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' );
249 }
250
251 /**
252 * @covers ResourceLoaderWikiModule::preloadTitleInfo
253 */
254 public function testGetPreloadedBadTitle() {
255 // Set up
256 TestResourceLoaderWikiModule::$returnFetchTitleInfo = [];
257 $rl = new EmptyResourceLoader();
258 $rl->getConfig()->set( 'UseSiteJs', true );
259 $rl->getConfig()->set( 'UseSiteCss', true );
260 $rl->register( 'testmodule', [
261 'class' => TestResourceLoaderWikiModule::class,
262 // Covers preloadTitleInfo branch for invalid page name
263 'styles' => [ '[x]' ],
264 ] );
265 $context = new ResourceLoaderContext( $rl, new FauxRequest() );
266
267 // Act
268 TestResourceLoaderWikiModule::preloadTitleInfo(
269 $context,
270 wfGetDB( DB_REPLICA ),
271 [ 'testmodule' ]
272 );
273
274 // Assert
275 $module = TestingAccessWrapper::newFromObject( $rl->getModule( 'testmodule' ) );
276 $this->assertSame( [], $module->getTitleInfo( $context ), 'Title info' );
277 }
278
279 /**
280 * @covers ResourceLoaderWikiModule::preloadTitleInfo
281 */
282 public function testGetPreloadedTitleInfoEmpty() {
283 $context = new ResourceLoaderContext( new EmptyResourceLoader(), new FauxRequest() );
284 // Covers early return
285 $this->assertSame(
286 null,
287 ResourceLoaderWikiModule::preloadTitleInfo(
288 $context,
289 wfGetDB( DB_REPLICA ),
290 []
291 )
292 );
293 }
294
295 public static function provideGetContent() {
296 return [
297 'Bad title' => [ null, '[x]' ],
298 'Dead redirect' => [ null, [
299 'text' => 'Dead redirect',
300 'title' => 'Dead_redirect',
301 'redirect' => 1,
302 ] ],
303 'Bad content model' => [ null, [
304 'text' => 'MediaWiki:Wikitext',
305 'ns' => NS_MEDIAWIKI,
306 'title' => 'Wikitext',
307 ] ],
308 'No JS content found' => [ null, [
309 'text' => 'MediaWiki:Script.js',
310 'ns' => NS_MEDIAWIKI,
311 'title' => 'Script.js',
312 ] ],
313 'No CSS content found' => [ null, [
314 'text' => 'MediaWiki:Styles.css',
315 'ns' => NS_MEDIAWIKI,
316 'title' => 'Script.css',
317 ] ],
318 ];
319 }
320
321 /**
322 * @covers ResourceLoaderWikiModule::getContent
323 * @dataProvider provideGetContent
324 */
325 public function testGetContent( $expected, $title ) {
326 $context = $this->getResourceLoaderContext( [], new EmptyResourceLoader );
327 $module = $this->getMockBuilder( ResourceLoaderWikiModule::class )
328 ->setMethods( [ 'getContentObj' ] )->getMock();
329 $module->expects( $this->any() )
330 ->method( 'getContentObj' )->willReturn( null );
331
332 if ( is_array( $title ) ) {
333 $title += [ 'ns' => NS_MAIN, 'id' => 1, 'len' => 1, 'redirect' => 0 ];
334 $titleText = $title['text'];
335 // Mock Title db access via LinkCache
336 MediaWikiServices::getInstance()->getLinkCache()->addGoodLinkObj(
337 $title['id'],
338 new TitleValue( $title['ns'], $title['title'] ),
339 $title['len'],
340 $title['redirect']
341 );
342 } else {
343 $titleText = $title;
344 }
345
346 $module = TestingAccessWrapper::newFromObject( $module );
347 $this->assertEquals(
348 $expected,
349 $module->getContent( $titleText, $context )
350 );
351 }
352
353 /**
354 * @covers ResourceLoaderWikiModule::getContent
355 * @covers ResourceLoaderWikiModule::getContentObj
356 * @covers ResourceLoaderWikiModule::shouldEmbedModule
357 */
358 public function testContentOverrides() {
359 $pages = [
360 'MediaWiki:Common.css' => [ 'type' => 'style' ],
361 ];
362
363 $module = $this->getMockBuilder( TestResourceLoaderWikiModule::class )
364 ->setMethods( [ 'getPages' ] )
365 ->getMock();
366 $module->method( 'getPages' )->willReturn( $pages );
367
368 $rl = new EmptyResourceLoader();
369 $context = new DerivativeResourceLoaderContext(
370 new ResourceLoaderContext( $rl, new FauxRequest() )
371 );
372 $context->setContentOverrideCallback( function ( Title $t ) {
373 if ( $t->getPrefixedText() === 'MediaWiki:Common.css' ) {
374 return new CssContent( '.override{}' );
375 }
376 return null;
377 } );
378
379 $this->assertTrue( $module->shouldEmbedModule( $context ) );
380 $this->assertEquals( [
381 'all' => [
382 "/*\nMediaWiki:Common.css\n*/\n.override{}"
383 ]
384 ], $module->getStyles( $context ) );
385
386 $context->setContentOverrideCallback( function ( Title $t ) {
387 if ( $t->getPrefixedText() === 'MediaWiki:Skin.css' ) {
388 return new CssContent( '.override{}' );
389 }
390 return null;
391 } );
392 $this->assertFalse( $module->shouldEmbedModule( $context ) );
393 }
394
395 /**
396 * @covers ResourceLoaderWikiModule::getContent
397 * @covers ResourceLoaderWikiModule::getContentObj
398 */
399 public function testGetContentForRedirects() {
400 // Set up context and module object
401 $context = new DerivativeResourceLoaderContext(
402 $this->getResourceLoaderContext( [], new EmptyResourceLoader )
403 );
404 $module = $this->getMockBuilder( ResourceLoaderWikiModule::class )
405 ->setMethods( [ 'getPages' ] )
406 ->getMock();
407 $module->expects( $this->any() )
408 ->method( 'getPages' )
409 ->will( $this->returnValue( [
410 'MediaWiki:Redirect.js' => [ 'type' => 'script' ]
411 ] ) );
412 $context->setContentOverrideCallback( function ( Title $title ) {
413 if ( $title->getPrefixedText() === 'MediaWiki:Redirect.js' ) {
414 $handler = new JavaScriptContentHandler();
415 return $handler->makeRedirectContent(
416 Title::makeTitle( NS_MEDIAWIKI, 'Target.js' )
417 );
418 } elseif ( $title->getPrefixedText() === 'MediaWiki:Target.js' ) {
419 return new JavaScriptContent( 'target;' );
420 } else {
421 return null;
422 }
423 } );
424
425 // Mock away Title's db queries with LinkCache
426 MediaWikiServices::getInstance()->getLinkCache()->addGoodLinkObj(
427 1, // id
428 new TitleValue( NS_MEDIAWIKI, 'Redirect.js' ),
429 1, // len
430 1 // redirect
431 );
432
433 $this->assertEquals(
434 "/*\nMediaWiki:Redirect.js\n*/\ntarget;\n",
435 $module->getScript( $context ),
436 'Redirect resolved by getContent'
437 );
438 }
439
440 function tearDown() {
441 Title::clearCaches();
442 parent::tearDown();
443 }
444 }
445
446 class TestResourceLoaderWikiModule extends ResourceLoaderWikiModule {
447 public static $returnFetchTitleInfo = null;
448
449 protected static function fetchTitleInfo( IDatabase $db, array $pages, $fname = null ) {
450 $ret = self::$returnFetchTitleInfo;
451 self::$returnFetchTitleInfo = null;
452 return $ret;
453 }
454 }