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', $module );
18 public static function provideConstructor() {
23 // Unrecognized settings
24 [ [ 'foo' => 'baz' ] ],
26 [ [ 'scripts' => [ 'MediaWiki:Common.js' ] ] ],
31 * @dataProvider provideGetPages
32 * @covers ResourceLoaderWikiModule::getPages
34 public function testGetPages( $params, Config
$config, $expected ) {
35 $module = new ResourceLoaderWikiModule( $params );
36 $module->setConfig( $config );
38 // Because getPages is protected..
39 $getPages = new ReflectionMethod( $module, 'getPages' );
40 $getPages->setAccessible( true );
41 $out = $getPages->invoke( $module, ResourceLoaderContext
::newDummyContext() );
42 $this->assertEquals( $expected, $out );
45 public static function provideGetPages() {
46 $settings = self
::getSettings() +
[
52 'styles' => [ 'MediaWiki:Common.css' ],
53 'scripts' => [ 'MediaWiki:Common.js' ],
57 [ [], new HashConfig( $settings ), [] ],
58 [ $params, new HashConfig( $settings ), [
59 'MediaWiki:Common.js' => [ 'type' => 'script' ],
60 'MediaWiki:Common.css' => [ 'type' => 'style' ]
62 [ $params, new HashConfig( [ 'UseSiteCss' => false ] +
$settings ), [
63 'MediaWiki:Common.js' => [ 'type' => 'script' ],
65 [ $params, new HashConfig( [ 'UseSiteJs' => false ] +
$settings ), [
66 'MediaWiki:Common.css' => [ 'type' => 'style' ],
70 [ 'UseSiteJs' => false, 'UseSiteCss' => false ]
78 * @covers ResourceLoaderWikiModule::getGroup
79 * @dataProvider provideGetGroup
81 public function testGetGroup( $params, $expected ) {
82 $module = new ResourceLoaderWikiModule( $params );
83 $this->assertEquals( $expected, $module->getGroup() );
86 public static function provideGetGroup() {
91 [ [ 'group' => 'foobar' ], 'foobar' ],
96 * @covers ResourceLoaderWikiModule::isKnownEmpty
97 * @dataProvider provideIsKnownEmpty
99 public function testIsKnownEmpty( $titleInfo, $group, $expected ) {
100 $module = $this->getMockBuilder( 'ResourceLoaderWikiModule' )
101 ->setMethods( [ 'getTitleInfo', 'getGroup' ] )
103 $module->expects( $this->any() )
104 ->method( 'getTitleInfo' )
105 ->will( $this->returnValue( $titleInfo ) );
106 $module->expects( $this->any() )
107 ->method( 'getGroup' )
108 ->will( $this->returnValue( $group ) );
109 $context = $this->getMockBuilder( 'ResourceLoaderContext' )
110 ->disableOriginalConstructor()
112 $this->assertEquals( $expected, $module->isKnownEmpty( $context ) );
115 public static function provideIsKnownEmpty() {
118 [ [], 'test1', true ],
119 // 'site' module with a non-empty page
121 [ 'MediaWiki:Common.js' => [ 'page_len' => 1234 ] ],
125 // 'site' module with an empty page
127 [ 'MediaWiki:Foo.js' => [ 'page_len' => 0 ] ],
131 // 'user' module with a non-empty page
133 [ 'User:Example/common.js' => [ 'page_len' => 25 ] ],
137 // 'user' module with an empty page
139 [ 'User:Example/foo.js' => [ 'page_len' => 0 ] ],
147 * @covers ResourceLoaderWikiModule::getTitleInfo
149 public function testGetTitleInfo() {
151 'MediaWiki:Common.css' => [ 'type' => 'styles' ],
152 'mediawiki: fallback.css' => [ 'type' => 'styles' ],
155 'MediaWiki:Common.css' => [ 'page_len' => 1234 ],
156 'MediaWiki:Fallback.css' => [ 'page_len' => 0 ],
158 $expected = $titleInfo;
160 $module = $this->getMockBuilder( 'TestResourceLoaderWikiModule' )
161 ->setMethods( [ 'getPages' ] )
163 $module->method( 'getPages' )->willReturn( $pages );
164 // Can't mock static methods
165 $module::$returnFetchTitleInfo = $titleInfo;
167 $context = $this->getMockBuilder( 'ResourceLoaderContext' )
168 ->disableOriginalConstructor()
171 $module = TestingAccessWrapper
::newFromObject( $module );
172 $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' );
176 * @covers ResourceLoaderWikiModule::getTitleInfo
177 * @covers ResourceLoaderWikiModule::setTitleInfo
178 * @covers ResourceLoaderWikiModule::preloadTitleInfo
180 public function testGetPreloadedTitleInfo() {
182 'MediaWiki:Common.css' => [ 'type' => 'styles' ],
183 // Regression against T145673. It's impossible to statically declare page names in
184 // a canonical way since the canonical prefix is localised. As such, the preload
185 // cache computed the right cache key, but failed to find the results when
186 // doing an intersect on the canonical result, producing an empty array.
187 'mediawiki: fallback.css' => [ 'type' => 'styles' ],
190 'MediaWiki:Common.css' => [ 'page_len' => 1234 ],
191 'MediaWiki:Fallback.css' => [ 'page_len' => 0 ],
193 $expected = $titleInfo;
195 $module = $this->getMockBuilder( 'TestResourceLoaderWikiModule' )
196 ->setMethods( [ 'getPages' ] )
198 $module->method( 'getPages' )->willReturn( $pages );
199 // Can't mock static methods
200 $module::$returnFetchTitleInfo = $titleInfo;
202 $rl = new EmptyResourceLoader();
203 $rl->register( 'testmodule', $module );
204 $context = new ResourceLoaderContext( $rl, new FauxRequest() );
206 TestResourceLoaderWikiModule
::invalidateModuleCache(
207 Title
::newFromText( 'MediaWiki:Common.css' ),
212 TestResourceLoaderWikiModule
::preloadTitleInfo(
214 wfGetDB( DB_REPLICA
),
218 $module = TestingAccessWrapper
::newFromObject( $module );
219 $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' );
223 * @covers ResourceLoaderWikiModule::preloadTitleInfo
225 public function testGetPreloadedBadTitle() {
228 // Covers else branch for invalid page name
229 '[x]' => [ 'type' => 'styles' ],
234 $module = $this->getMockBuilder( 'TestResourceLoaderWikiModule' )
235 ->setMethods( [ 'getPages' ] ) ->getMock();
236 $module->method( 'getPages' )->willReturn( $pages );
237 $module::$returnFetchTitleInfo = $titleInfo;
238 $rl = new EmptyResourceLoader();
239 $rl->register( 'testmodule', $module );
240 $context = new ResourceLoaderContext( $rl, new FauxRequest() );
243 TestResourceLoaderWikiModule
::preloadTitleInfo(
245 wfGetDB( DB_REPLICA
),
250 $module = TestingAccessWrapper
::newFromObject( $module );
251 $this->assertEquals( $titleInfo, $module->getTitleInfo( $context ), 'Title info' );
255 * @covers ResourceLoaderWikiModule::preloadTitleInfo
257 public function testGetPreloadedTitleInfoEmpty() {
258 $context = new ResourceLoaderContext( new EmptyResourceLoader(), new FauxRequest() );
259 // Covers early return
262 ResourceLoaderWikiModule
::preloadTitleInfo(
264 wfGetDB( DB_REPLICA
),
270 public static function provideGetContent() {
272 'Bad title' => [ null, '[x]' ],
273 'Dead redirect' => [ null, [
274 'text' => 'Dead redirect',
275 'title' => 'Dead_redirect',
278 'Bad content model' => [ null, [
279 'text' => 'MediaWiki:Wikitext',
280 'ns' => NS_MEDIAWIKI
,
281 'title' => 'Wikitext',
283 'No JS content found' => [ null, [
284 'text' => 'MediaWiki:Script.js',
285 'ns' => NS_MEDIAWIKI
,
286 'title' => 'Script.js',
288 'No CSS content found' => [ null, [
289 'text' => 'MediaWiki:Styles.css',
290 'ns' => NS_MEDIAWIKI
,
291 'title' => 'Script.css',
297 * @covers ResourceLoaderWikiModule::getContent
298 * @dataProvider provideGetContent
300 public function testGetContent( $expected, $title ) {
301 $context = $this->getResourceLoaderContext( [], new EmptyResourceLoader
);
302 $module = $this->getMockBuilder( 'ResourceLoaderWikiModule' )
303 ->setMethods( [ 'getContentObj' ] ) ->getMock();
304 $module->expects( $this->any() )
305 ->method( 'getContentObj' )->willReturn( null );
307 if ( is_array( $title ) ) {
308 $title +
= [ 'ns' => NS_MAIN
, 'id' => 1, 'len' => 1, 'redirect' => 0 ];
309 $titleText = $title['text'];
310 // Mock Title db access via LinkCache
311 MediaWikiServices
::getInstance()->getLinkCache()->addGoodLinkObj(
313 new TitleValue( $title['ns'], $title['title'] ),
321 $module = TestingAccessWrapper
::newFromObject( $module );
324 $module->getContent( $titleText )
329 * @covers ResourceLoaderWikiModule::getContent
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' ] )
337 $module->expects( $this->any() )
338 ->method( 'getPages' )
339 ->will( $this->returnValue( [
340 'MediaWiki:Redirect.js' => [ 'type' => 'script' ]
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' )
350 } elseif ( $title->getPrefixedText() === 'MediaWiki:Target.js' ) {
351 return new JavaScriptContent( 'target;' );
357 // Mock away Title's db queries with LinkCache
358 MediaWikiServices
::getInstance()->getLinkCache()->addGoodLinkObj(
360 new TitleValue( NS_MEDIAWIKI
, 'Redirect.js' ),
366 "/*\nMediaWiki:Redirect.js\n*/\ntarget;\n",
367 $module->getScript( $context ),
368 'Redirect resolved by getContent'
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;