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 $context = new ResourceLoaderContext( $rl, new FauxRequest() );
235 TestResourceLoaderWikiModule
::invalidateModuleCache(
236 Title
::newFromText( 'MediaWiki:Common.css' ),
241 TestResourceLoaderWikiModule
::preloadTitleInfo(
243 wfGetDB( DB_REPLICA
),
247 $module = TestingAccessWrapper
::newFromObject( $module );
248 $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' );
252 * @covers ResourceLoaderWikiModule::preloadTitleInfo
254 public function testGetPreloadedBadTitle() {
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]' ],
265 $context = new ResourceLoaderContext( $rl, new FauxRequest() );
268 TestResourceLoaderWikiModule
::preloadTitleInfo(
270 wfGetDB( DB_REPLICA
),
275 $module = TestingAccessWrapper
::newFromObject( $rl->getModule( 'testmodule' ) );
276 $this->assertSame( [], $module->getTitleInfo( $context ), 'Title info' );
280 * @covers ResourceLoaderWikiModule::preloadTitleInfo
282 public function testGetPreloadedTitleInfoEmpty() {
283 $context = new ResourceLoaderContext( new EmptyResourceLoader(), new FauxRequest() );
284 // Covers early return
287 ResourceLoaderWikiModule
::preloadTitleInfo(
289 wfGetDB( DB_REPLICA
),
295 public static function provideGetContent() {
297 'Bad title' => [ null, '[x]' ],
298 'Dead redirect' => [ null, [
299 'text' => 'Dead redirect',
300 'title' => 'Dead_redirect',
303 'Bad content model' => [ null, [
304 'text' => 'MediaWiki:Wikitext',
305 'ns' => NS_MEDIAWIKI
,
306 'title' => 'Wikitext',
308 'No JS content found' => [ null, [
309 'text' => 'MediaWiki:Script.js',
310 'ns' => NS_MEDIAWIKI
,
311 'title' => 'Script.js',
313 'No CSS content found' => [ null, [
314 'text' => 'MediaWiki:Styles.css',
315 'ns' => NS_MEDIAWIKI
,
316 'title' => 'Script.css',
322 * @covers ResourceLoaderWikiModule::getContent
323 * @dataProvider provideGetContent
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 );
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(
338 new TitleValue( $title['ns'], $title['title'] ),
346 $module = TestingAccessWrapper
::newFromObject( $module );
349 $module->getContent( $titleText, $context )
354 * @covers ResourceLoaderWikiModule::getContent
355 * @covers ResourceLoaderWikiModule::getContentObj
356 * @covers ResourceLoaderWikiModule::shouldEmbedModule
358 public function testContentOverrides() {
360 'MediaWiki:Common.css' => [ 'type' => 'style' ],
363 $module = $this->getMockBuilder( TestResourceLoaderWikiModule
::class )
364 ->setMethods( [ 'getPages' ] )
366 $module->method( 'getPages' )->willReturn( $pages );
368 $rl = new EmptyResourceLoader();
369 $context = new DerivativeResourceLoaderContext(
370 new ResourceLoaderContext( $rl, new FauxRequest() )
372 $context->setContentOverrideCallback( function ( Title
$t ) {
373 if ( $t->getPrefixedText() === 'MediaWiki:Common.css' ) {
374 return new CssContent( '.override{}' );
379 $this->assertTrue( $module->shouldEmbedModule( $context ) );
380 $this->assertEquals( [
382 "/*\nMediaWiki:Common.css\n*/\n.override{}"
384 ], $module->getStyles( $context ) );
386 $context->setContentOverrideCallback( function ( Title
$t ) {
387 if ( $t->getPrefixedText() === 'MediaWiki:Skin.css' ) {
388 return new CssContent( '.override{}' );
392 $this->assertFalse( $module->shouldEmbedModule( $context ) );
396 * @covers ResourceLoaderWikiModule::getContent
397 * @covers ResourceLoaderWikiModule::getContentObj
399 public function testGetContentForRedirects() {
400 // Set up context and module object
401 $context = new DerivativeResourceLoaderContext(
402 $this->getResourceLoaderContext( [], new EmptyResourceLoader
)
404 $module = $this->getMockBuilder( ResourceLoaderWikiModule
::class )
405 ->setMethods( [ 'getPages' ] )
407 $module->expects( $this->any() )
408 ->method( 'getPages' )
409 ->will( $this->returnValue( [
410 'MediaWiki:Redirect.js' => [ 'type' => 'script' ]
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' )
418 } elseif ( $title->getPrefixedText() === 'MediaWiki:Target.js' ) {
419 return new JavaScriptContent( 'target;' );
425 // Mock away Title's db queries with LinkCache
426 MediaWikiServices
::getInstance()->getLinkCache()->addGoodLinkObj(
428 new TitleValue( NS_MEDIAWIKI
, 'Redirect.js' ),
434 "/*\nMediaWiki:Redirect.js\n*/\ntarget;\n",
435 $module->getScript( $context ),
436 'Redirect resolved by getContent'
440 function tearDown() {
441 Title
::clearCaches();
446 class TestResourceLoaderWikiModule
extends ResourceLoaderWikiModule
{
447 public static $returnFetchTitleInfo = null;
449 protected static function fetchTitleInfo( IDatabase
$db, array $pages, $fname = null ) {
450 $ret = self
::$returnFetchTitleInfo;
451 self
::$returnFetchTitleInfo = null;