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