TraditionalImageGallery factor getCaptionHtml into own function
[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, $expected ) {
109 $module = $this->getMockBuilder( ResourceLoaderWikiModule::class )
110 ->setMethods( [ 'getTitleInfo', 'getGroup' ] )
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 $context = $this->getMockBuilder( ResourceLoaderContext::class )
119 ->disableOriginalConstructor()
120 ->getMock();
121 $this->assertEquals( $expected, $module->isKnownEmpty( $context ) );
122 }
123
124 public static function provideIsKnownEmpty() {
125 return [
126 // No valid pages
127 [ [], 'test1', true ],
128 // 'site' module with a non-empty page
129 [
130 [ 'MediaWiki:Common.js' => [ 'page_len' => 1234 ] ],
131 'site',
132 false,
133 ],
134 // 'site' module with an empty page
135 [
136 [ 'MediaWiki:Foo.js' => [ 'page_len' => 0 ] ],
137 'site',
138 false,
139 ],
140 // 'user' module with a non-empty page
141 [
142 [ 'User:Example/common.js' => [ 'page_len' => 25 ] ],
143 'user',
144 false,
145 ],
146 // 'user' module with an empty page
147 [
148 [ 'User:Example/foo.js' => [ 'page_len' => 0 ] ],
149 'user',
150 true,
151 ],
152 ];
153 }
154
155 /**
156 * @covers ResourceLoaderWikiModule::getTitleInfo
157 */
158 public function testGetTitleInfo() {
159 $pages = [
160 'MediaWiki:Common.css' => [ 'type' => 'styles' ],
161 'mediawiki: fallback.css' => [ 'type' => 'styles' ],
162 ];
163 $titleInfo = $this->prepareTitleInfo( [
164 'MediaWiki:Common.css' => [ 'page_len' => 1234 ],
165 'MediaWiki:Fallback.css' => [ 'page_len' => 0 ],
166 ] );
167 $expected = $titleInfo;
168
169 $module = $this->getMockBuilder( TestResourceLoaderWikiModule::class )
170 ->setMethods( [ 'getPages' ] )
171 ->getMock();
172 $module->method( 'getPages' )->willReturn( $pages );
173 // Can't mock static methods
174 $module::$returnFetchTitleInfo = $titleInfo;
175
176 $context = $this->getMockBuilder( ResourceLoaderContext::class )
177 ->disableOriginalConstructor()
178 ->getMock();
179
180 $module = TestingAccessWrapper::newFromObject( $module );
181 $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' );
182 }
183
184 /**
185 * @covers ResourceLoaderWikiModule::getTitleInfo
186 * @covers ResourceLoaderWikiModule::setTitleInfo
187 * @covers ResourceLoaderWikiModule::preloadTitleInfo
188 */
189 public function testGetPreloadedTitleInfo() {
190 $pages = [
191 'MediaWiki:Common.css' => [ 'type' => 'styles' ],
192 // Regression against T145673. It's impossible to statically declare page names in
193 // a canonical way since the canonical prefix is localised. As such, the preload
194 // cache computed the right cache key, but failed to find the results when
195 // doing an intersect on the canonical result, producing an empty array.
196 'mediawiki: fallback.css' => [ 'type' => 'styles' ],
197 ];
198 $titleInfo = $this->prepareTitleInfo( [
199 'MediaWiki:Common.css' => [ 'page_len' => 1234 ],
200 'MediaWiki:Fallback.css' => [ 'page_len' => 0 ],
201 ] );
202 $expected = $titleInfo;
203
204 $module = $this->getMockBuilder( TestResourceLoaderWikiModule::class )
205 ->setMethods( [ 'getPages' ] )
206 ->getMock();
207 $module->method( 'getPages' )->willReturn( $pages );
208 // Can't mock static methods
209 $module::$returnFetchTitleInfo = $titleInfo;
210
211 $rl = new EmptyResourceLoader();
212 $rl->register( 'testmodule', $module );
213 $context = new ResourceLoaderContext( $rl, new FauxRequest() );
214
215 TestResourceLoaderWikiModule::invalidateModuleCache(
216 Title::newFromText( 'MediaWiki:Common.css' ),
217 null,
218 null,
219 wfWikiID()
220 );
221 TestResourceLoaderWikiModule::preloadTitleInfo(
222 $context,
223 wfGetDB( DB_REPLICA ),
224 [ 'testmodule' ]
225 );
226
227 $module = TestingAccessWrapper::newFromObject( $module );
228 $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' );
229 }
230
231 /**
232 * @covers ResourceLoaderWikiModule::preloadTitleInfo
233 */
234 public function testGetPreloadedBadTitle() {
235 // Mock values
236 $pages = [
237 // Covers else branch for invalid page name
238 '[x]' => [ 'type' => 'styles' ],
239 ];
240 $titleInfo = [];
241
242 // Set up objects
243 $module = $this->getMockBuilder( TestResourceLoaderWikiModule::class )
244 ->setMethods( [ 'getPages' ] ) ->getMock();
245 $module->method( 'getPages' )->willReturn( $pages );
246 $module::$returnFetchTitleInfo = $titleInfo;
247 $rl = new EmptyResourceLoader();
248 $rl->register( 'testmodule', $module );
249 $context = new ResourceLoaderContext( $rl, new FauxRequest() );
250
251 // Act
252 TestResourceLoaderWikiModule::preloadTitleInfo(
253 $context,
254 wfGetDB( DB_REPLICA ),
255 [ 'testmodule' ]
256 );
257
258 // Assert
259 $module = TestingAccessWrapper::newFromObject( $module );
260 $this->assertEquals( $titleInfo, $module->getTitleInfo( $context ), 'Title info' );
261 }
262
263 /**
264 * @covers ResourceLoaderWikiModule::preloadTitleInfo
265 */
266 public function testGetPreloadedTitleInfoEmpty() {
267 $context = new ResourceLoaderContext( new EmptyResourceLoader(), new FauxRequest() );
268 // Covers early return
269 $this->assertSame(
270 null,
271 ResourceLoaderWikiModule::preloadTitleInfo(
272 $context,
273 wfGetDB( DB_REPLICA ),
274 []
275 )
276 );
277 }
278
279 public static function provideGetContent() {
280 return [
281 'Bad title' => [ null, '[x]' ],
282 'Dead redirect' => [ null, [
283 'text' => 'Dead redirect',
284 'title' => 'Dead_redirect',
285 'redirect' => 1,
286 ] ],
287 'Bad content model' => [ null, [
288 'text' => 'MediaWiki:Wikitext',
289 'ns' => NS_MEDIAWIKI,
290 'title' => 'Wikitext',
291 ] ],
292 'No JS content found' => [ null, [
293 'text' => 'MediaWiki:Script.js',
294 'ns' => NS_MEDIAWIKI,
295 'title' => 'Script.js',
296 ] ],
297 'No CSS content found' => [ null, [
298 'text' => 'MediaWiki:Styles.css',
299 'ns' => NS_MEDIAWIKI,
300 'title' => 'Script.css',
301 ] ],
302 ];
303 }
304
305 /**
306 * @covers ResourceLoaderWikiModule::getContent
307 * @dataProvider provideGetContent
308 */
309 public function testGetContent( $expected, $title ) {
310 $context = $this->getResourceLoaderContext( [], new EmptyResourceLoader );
311 $module = $this->getMockBuilder( ResourceLoaderWikiModule::class )
312 ->setMethods( [ 'getContentObj' ] ) ->getMock();
313 $module->expects( $this->any() )
314 ->method( 'getContentObj' )->willReturn( null );
315
316 if ( is_array( $title ) ) {
317 $title += [ 'ns' => NS_MAIN, 'id' => 1, 'len' => 1, 'redirect' => 0 ];
318 $titleText = $title['text'];
319 // Mock Title db access via LinkCache
320 MediaWikiServices::getInstance()->getLinkCache()->addGoodLinkObj(
321 $title['id'],
322 new TitleValue( $title['ns'], $title['title'] ),
323 $title['len'],
324 $title['redirect']
325 );
326 } else {
327 $titleText = $title;
328 }
329
330 $module = TestingAccessWrapper::newFromObject( $module );
331 $this->assertEquals(
332 $expected,
333 $module->getContent( $titleText )
334 );
335 }
336
337 /**
338 * @covers ResourceLoaderWikiModule::getContent
339 */
340 public function testGetContentForRedirects() {
341 // Set up context and module object
342 $context = $this->getResourceLoaderContext( [], new EmptyResourceLoader );
343 $module = $this->getMockBuilder( ResourceLoaderWikiModule::class )
344 ->setMethods( [ 'getPages', 'getContentObj' ] )
345 ->getMock();
346 $module->expects( $this->any() )
347 ->method( 'getPages' )
348 ->will( $this->returnValue( [
349 'MediaWiki:Redirect.js' => [ 'type' => 'script' ]
350 ] ) );
351 $module->expects( $this->any() )
352 ->method( 'getContentObj' )
353 ->will( $this->returnCallback( function ( Title $title ) {
354 if ( $title->getPrefixedText() === 'MediaWiki:Redirect.js' ) {
355 $handler = new JavaScriptContentHandler();
356 return $handler->makeRedirectContent(
357 Title::makeTitle( NS_MEDIAWIKI, 'Target.js' )
358 );
359 } elseif ( $title->getPrefixedText() === 'MediaWiki:Target.js' ) {
360 return new JavaScriptContent( 'target;' );
361 } else {
362 return null;
363 }
364 } ) );
365
366 // Mock away Title's db queries with LinkCache
367 MediaWikiServices::getInstance()->getLinkCache()->addGoodLinkObj(
368 1, // id
369 new TitleValue( NS_MEDIAWIKI, 'Redirect.js' ),
370 1, // len
371 1 // redirect
372 );
373
374 $this->assertEquals(
375 "/*\nMediaWiki:Redirect.js\n*/\ntarget;\n",
376 $module->getScript( $context ),
377 'Redirect resolved by getContent'
378 );
379 }
380 }
381
382 class TestResourceLoaderWikiModule extends ResourceLoaderWikiModule {
383 public static $returnFetchTitleInfo = null;
384 protected static function fetchTitleInfo( IDatabase $db, array $pages, $fname = null ) {
385 $ret = self::$returnFetchTitleInfo;
386 self::$returnFetchTitleInfo = null;
387 return $ret;
388 }
389 }