3 use MediaWiki\MediaWikiServices
;
4 use Wikimedia\Rdbms\IDatabase
;
5 use Wikimedia\TestingAccessWrapper
;
7 class ResourceLoaderWikiModuleTest
extends ResourceLoaderTestCase
{
10 * @covers ResourceLoaderWikiModule::__construct
11 * @dataProvider provideConstructor
13 public function testConstructor( $params ) {
14 $module = new ResourceLoaderWikiModule( $params );
15 $this->assertInstanceOf( ResourceLoaderWikiModule
::class, $module );
18 private function prepareTitleInfo( array $mockInfo ) {
19 $module = TestingAccessWrapper
::newFromClass( ResourceLoaderWikiModule
::class );
21 foreach ( $mockInfo as $key => $val ) {
22 $info[ $module->makeTitleKey( Title
::newFromText( $key ) ) ] = $val;
27 public static function provideConstructor() {
32 // Unrecognized settings
33 [ [ 'foo' => 'baz' ] ],
35 [ [ 'scripts' => [ 'MediaWiki:Common.js' ] ] ],
40 * @dataProvider provideGetPages
41 * @covers ResourceLoaderWikiModule::getPages
43 public function testGetPages( $params, Config
$config, $expected ) {
44 $module = new ResourceLoaderWikiModule( $params );
45 $module->setConfig( $config );
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 );
54 public static function provideGetPages() {
55 $settings = self
::getSettings() +
[
61 'styles' => [ 'MediaWiki:Common.css' ],
62 'scripts' => [ 'MediaWiki:Common.js' ],
66 [ [], new HashConfig( $settings ), [] ],
67 [ $params, new HashConfig( $settings ), [
68 'MediaWiki:Common.js' => [ 'type' => 'script' ],
69 'MediaWiki:Common.css' => [ 'type' => 'style' ]
71 [ $params, new HashConfig( [ 'UseSiteCss' => false ] +
$settings ), [
72 'MediaWiki:Common.js' => [ 'type' => 'script' ],
74 [ $params, new HashConfig( [ 'UseSiteJs' => false ] +
$settings ), [
75 'MediaWiki:Common.css' => [ 'type' => 'style' ],
79 [ 'UseSiteJs' => false, 'UseSiteCss' => false ]
87 * @covers ResourceLoaderWikiModule::getGroup
88 * @dataProvider provideGetGroup
90 public function testGetGroup( $params, $expected ) {
91 $module = new ResourceLoaderWikiModule( $params );
92 $this->assertEquals( $expected, $module->getGroup() );
95 public static function provideGetGroup() {
100 [ [ 'group' => 'foobar' ], 'foobar' ],
105 * @covers ResourceLoaderWikiModule::isKnownEmpty
106 * @dataProvider provideIsKnownEmpty
108 public function testIsKnownEmpty( $titleInfo, $group, $dependencies, $expected ) {
109 $module = $this->getMockBuilder( ResourceLoaderWikiModule
::class )
110 ->setMethods( [ 'getTitleInfo', 'getGroup', 'getDependencies' ] )
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()
124 $this->assertEquals( $expected, $module->isKnownEmpty( $context ) );
127 public static function provideIsKnownEmpty() {
130 [ [], 'test1', [], true ],
131 // 'site' module with a non-empty page
133 [ 'MediaWiki:Common.js' => [ 'page_len' => 1234 ] ],
138 // 'site' module without existing pages but dependencies
145 // 'site' module which is empty but has dependencies
147 [ 'MediaWiki:Common.js' => [ 'page_len' => 0 ] ],
152 // 'site' module with an empty page
154 [ 'MediaWiki:Foo.js' => [ 'page_len' => 0 ] ],
159 // 'user' module with a non-empty page
161 [ 'User:Example/common.js' => [ 'page_len' => 25 ] ],
166 // 'user' module with an empty page
168 [ 'User:Example/foo.js' => [ 'page_len' => 0 ] ],
177 * @covers ResourceLoaderWikiModule::getTitleInfo
179 public function testGetTitleInfo() {
181 'MediaWiki:Common.css' => [ 'type' => 'styles' ],
182 'mediawiki: fallback.css' => [ 'type' => 'styles' ],
184 $titleInfo = $this->prepareTitleInfo( [
185 'MediaWiki:Common.css' => [ 'page_len' => 1234 ],
186 'MediaWiki:Fallback.css' => [ 'page_len' => 0 ],
188 $expected = $titleInfo;
190 $module = $this->getMockBuilder( TestResourceLoaderWikiModule
::class )
191 ->setMethods( [ 'getPages' ] )
193 $module->method( 'getPages' )->willReturn( $pages );
194 // Can't mock static methods
195 $module::$returnFetchTitleInfo = $titleInfo;
197 $context = $this->getMockBuilder( ResourceLoaderContext
::class )
198 ->disableOriginalConstructor()
201 $module = TestingAccessWrapper
::newFromObject( $module );
202 $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' );
206 * @covers ResourceLoaderWikiModule::getTitleInfo
207 * @covers ResourceLoaderWikiModule::setTitleInfo
208 * @covers ResourceLoaderWikiModule::preloadTitleInfo
210 public function testGetPreloadedTitleInfo() {
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' ],
219 $titleInfo = $this->prepareTitleInfo( [
220 'MediaWiki:Common.css' => [ 'page_len' => 1234 ],
221 'MediaWiki:Fallback.css' => [ 'page_len' => 0 ],
223 $expected = $titleInfo;
225 $module = $this->getMockBuilder( TestResourceLoaderWikiModule
::class )
226 ->setMethods( [ 'getPages' ] )
228 $module->method( 'getPages' )->willReturn( $pages );
229 // Can't mock static methods
230 $module::$returnFetchTitleInfo = $titleInfo;
232 $rl = new EmptyResourceLoader();
233 $rl->register( 'testmodule', $module );
234 $context = new ResourceLoaderContext( $rl, new FauxRequest() );
236 TestResourceLoaderWikiModule
::invalidateModuleCache(
237 Title
::newFromText( 'MediaWiki:Common.css' ),
242 TestResourceLoaderWikiModule
::preloadTitleInfo(
244 wfGetDB( DB_REPLICA
),
248 $module = TestingAccessWrapper
::newFromObject( $module );
249 $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' );
253 * @covers ResourceLoaderWikiModule::preloadTitleInfo
255 public function testGetPreloadedBadTitle() {
258 // Covers else branch for invalid page name
259 '[x]' => [ 'type' => 'styles' ],
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() );
273 TestResourceLoaderWikiModule
::preloadTitleInfo(
275 wfGetDB( DB_REPLICA
),
280 $module = TestingAccessWrapper
::newFromObject( $module );
281 $this->assertEquals( $titleInfo, $module->getTitleInfo( $context ), 'Title info' );
285 * @covers ResourceLoaderWikiModule::preloadTitleInfo
287 public function testGetPreloadedTitleInfoEmpty() {
288 $context = new ResourceLoaderContext( new EmptyResourceLoader(), new FauxRequest() );
289 // Covers early return
292 ResourceLoaderWikiModule
::preloadTitleInfo(
294 wfGetDB( DB_REPLICA
),
300 public static function provideGetContent() {
302 'Bad title' => [ null, '[x]' ],
303 'Dead redirect' => [ null, [
304 'text' => 'Dead redirect',
305 'title' => 'Dead_redirect',
308 'Bad content model' => [ null, [
309 'text' => 'MediaWiki:Wikitext',
310 'ns' => NS_MEDIAWIKI
,
311 'title' => 'Wikitext',
313 'No JS content found' => [ null, [
314 'text' => 'MediaWiki:Script.js',
315 'ns' => NS_MEDIAWIKI
,
316 'title' => 'Script.js',
318 'No CSS content found' => [ null, [
319 'text' => 'MediaWiki:Styles.css',
320 'ns' => NS_MEDIAWIKI
,
321 'title' => 'Script.css',
327 * @covers ResourceLoaderWikiModule::getContent
328 * @dataProvider provideGetContent
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 );
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(
343 new TitleValue( $title['ns'], $title['title'] ),
351 $module = TestingAccessWrapper
::newFromObject( $module );
354 $module->getContent( $titleText, $context )
359 * @covers ResourceLoaderWikiModule::getContent
360 * @covers ResourceLoaderWikiModule::getContentObj
361 * @covers ResourceLoaderWikiModule::shouldEmbedModule
363 public function testContentOverrides() {
365 'MediaWiki:Common.css' => [ 'type' => 'style' ],
368 $module = $this->getMockBuilder( TestResourceLoaderWikiModule
::class )
369 ->setMethods( [ 'getPages' ] )
371 $module->method( 'getPages' )->willReturn( $pages );
373 $rl = new EmptyResourceLoader();
374 $rl->register( 'testmodule', $module );
375 $context = new DerivativeResourceLoaderContext(
376 new ResourceLoaderContext( $rl, new FauxRequest() )
378 $context->setContentOverrideCallback( function ( Title
$t ) {
379 if ( $t->getPrefixedText() === 'MediaWiki:Common.css' ) {
380 return new CssContent( '.override{}' );
385 $this->assertTrue( $module->shouldEmbedModule( $context ) );
386 $this->assertEquals( [
388 "/*\nMediaWiki:Common.css\n*/\n.override{}"
390 ], $module->getStyles( $context ) );
392 $context->setContentOverrideCallback( function ( Title
$t ) {
393 if ( $t->getPrefixedText() === 'MediaWiki:Skin.css' ) {
394 return new CssContent( '.override{}' );
398 $this->assertFalse( $module->shouldEmbedModule( $context ) );
402 * @covers ResourceLoaderWikiModule::getContent
403 * @covers ResourceLoaderWikiModule::getContentObj
405 public function testGetContentForRedirects() {
406 // Set up context and module object
407 $context = new DerivativeResourceLoaderContext(
408 $this->getResourceLoaderContext( [], new EmptyResourceLoader
)
410 $module = $this->getMockBuilder( ResourceLoaderWikiModule
::class )
411 ->setMethods( [ 'getPages' ] )
413 $module->expects( $this->any() )
414 ->method( 'getPages' )
415 ->will( $this->returnValue( [
416 'MediaWiki:Redirect.js' => [ 'type' => 'script' ]
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' )
424 } elseif ( $title->getPrefixedText() === 'MediaWiki:Target.js' ) {
425 return new JavaScriptContent( 'target;' );
431 // Mock away Title's db queries with LinkCache
432 MediaWikiServices
::getInstance()->getLinkCache()->addGoodLinkObj(
434 new TitleValue( NS_MEDIAWIKI
, 'Redirect.js' ),
440 "/*\nMediaWiki:Redirect.js\n*/\ntarget;\n",
441 $module->getScript( $context ),
442 'Redirect resolved by getContent'
446 function tearDown() {
447 Title
::clearCaches();
452 class TestResourceLoaderWikiModule
extends ResourceLoaderWikiModule
{
453 public static $returnFetchTitleInfo = null;
454 protected static function fetchTitleInfo( IDatabase
$db, array $pages, $fname = null ) {
455 $ret = self
::$returnFetchTitleInfo;
456 self
::$returnFetchTitleInfo = null;