<?php
+use MediaWiki\MediaWikiServices;
+use Wikimedia\Rdbms\IDatabase;
+
class ResourceLoaderWikiModuleTest extends ResourceLoaderTestCase {
/**
],
];
}
+
+ /**
+ * @covers ResourceLoaderWikiModule::getTitleInfo
+ */
+ public function testGetTitleInfo() {
+ $pages = [
+ 'MediaWiki:Common.css' => [ 'type' => 'styles' ],
+ 'mediawiki: fallback.css' => [ 'type' => 'styles' ],
+ ];
+ $titleInfo = [
+ 'MediaWiki:Common.css' => [ 'page_len' => 1234 ],
+ 'MediaWiki:Fallback.css' => [ 'page_len' => 0 ],
+ ];
+ $expected = $titleInfo;
+
+ $module = $this->getMockBuilder( 'TestResourceLoaderWikiModule' )
+ ->setMethods( [ 'getPages' ] )
+ ->getMock();
+ $module->method( 'getPages' )->willReturn( $pages );
+ // Can't mock static methods
+ $module::$returnFetchTitleInfo = $titleInfo;
+
+ $context = $this->getMockBuilder( 'ResourceLoaderContext' )
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $module = TestingAccessWrapper::newFromObject( $module );
+ $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' );
+ }
+
+ /**
+ * @covers ResourceLoaderWikiModule::getTitleInfo
+ * @covers ResourceLoaderWikiModule::setTitleInfo
+ * @covers ResourceLoaderWikiModule::preloadTitleInfo
+ */
+ public function testGetPreloadedTitleInfo() {
+ $pages = [
+ 'MediaWiki:Common.css' => [ 'type' => 'styles' ],
+ // Regression against T145673. It's impossible to statically declare page names in
+ // a canonical way since the canonical prefix is localised. As such, the preload
+ // cache computed the right cache key, but failed to find the results when
+ // doing an intersect on the canonical result, producing an empty array.
+ 'mediawiki: fallback.css' => [ 'type' => 'styles' ],
+ ];
+ $titleInfo = [
+ 'MediaWiki:Common.css' => [ 'page_len' => 1234 ],
+ 'MediaWiki:Fallback.css' => [ 'page_len' => 0 ],
+ ];
+ $expected = $titleInfo;
+
+ $module = $this->getMockBuilder( 'TestResourceLoaderWikiModule' )
+ ->setMethods( [ 'getPages' ] )
+ ->getMock();
+ $module->method( 'getPages' )->willReturn( $pages );
+ // Can't mock static methods
+ $module::$returnFetchTitleInfo = $titleInfo;
+
+ $rl = new EmptyResourceLoader();
+ $rl->register( 'testmodule', $module );
+ $context = new ResourceLoaderContext( $rl, new FauxRequest() );
+
+ TestResourceLoaderWikiModule::invalidateModuleCache(
+ Title::newFromText( 'MediaWiki:Common.css' ),
+ null,
+ null,
+ wfWikiID()
+ );
+ TestResourceLoaderWikiModule::preloadTitleInfo(
+ $context,
+ wfGetDB( DB_REPLICA ),
+ [ 'testmodule' ]
+ );
+
+ $module = TestingAccessWrapper::newFromObject( $module );
+ $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' );
+ }
+
+ /**
+ * @covers ResourceLoaderWikiModule::getContent
+ */
+ public function testGetContentForRedirects() {
+ // Set up context and module object
+ $context = $this->getResourceLoaderContext( [], new EmptyResourceLoader );
+ $module = $this->getMockBuilder( 'ResourceLoaderWikiModule' )
+ ->setMethods( [ 'getPages', 'getContentObj' ] )
+ ->getMock();
+ $module->expects( $this->any() )
+ ->method( 'getPages' )
+ ->will( $this->returnValue( [
+ 'MediaWiki:Redirect.js' => [ 'type' => 'script' ]
+ ] ) );
+ $module->expects( $this->any() )
+ ->method( 'getContentObj' )
+ ->will( $this->returnCallback( function ( Title $title ) {
+ if ( $title->getPrefixedText() === 'MediaWiki:Redirect.js' ) {
+ $handler = new JavaScriptContentHandler();
+ return $handler->makeRedirectContent(
+ Title::makeTitle( NS_MEDIAWIKI, 'Target.js' )
+ );
+ } elseif ( $title->getPrefixedText() === 'MediaWiki:Target.js' ) {
+ return new JavaScriptContent( 'target;' );
+ } else {
+ return null;
+ }
+ } ) );
+
+ // Mock away Title's db queries with LinkCache
+ MediaWikiServices::getInstance()->getLinkCache()->addGoodLinkObj(
+ 1, // id
+ new TitleValue( NS_MEDIAWIKI, 'Redirect.js' ),
+ 1, // len
+ 1 // redirect
+ );
+
+ $this->assertEquals(
+ "/*\nMediaWiki:Redirect.js\n*/\ntarget;\n",
+ $module->getScript( $context ),
+ 'Redirect resolved by getContent'
+ );
+ }
+}
+
+class TestResourceLoaderWikiModule extends ResourceLoaderWikiModule {
+ public static $returnFetchTitleInfo = null;
+ protected static function fetchTitleInfo( IDatabase $db, array $pages, $fname = null ) {
+ $ret = self::$returnFetchTitleInfo;
+ self::$returnFetchTitleInfo = null;
+ return $ret;
+ }
}