3 use MediaWiki\MediaWikiServices
;
4 use Wikimedia\Rdbms\IDatabase
;
6 class ResourceLoaderWikiModuleTest
extends ResourceLoaderTestCase
{
9 * @covers ResourceLoaderWikiModule::__construct
10 * @dataProvider provideConstructor
12 public function testConstructor( $params ) {
13 $module = new ResourceLoaderWikiModule( $params );
14 $this->assertInstanceOf( 'ResourceLoaderWikiModule', $module );
17 public static function provideConstructor() {
22 // Unrecognized settings
23 [ [ 'foo' => 'baz' ] ],
25 [ [ 'scripts' => [ 'MediaWiki:Common.js' ] ] ],
30 * @dataProvider provideGetPages
31 * @covers ResourceLoaderWikiModule::getPages
33 public function testGetPages( $params, Config
$config, $expected ) {
34 $module = new ResourceLoaderWikiModule( $params );
35 $module->setConfig( $config );
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 );
44 public static function provideGetPages() {
45 $settings = self
::getSettings() +
[
51 'styles' => [ 'MediaWiki:Common.css' ],
52 'scripts' => [ 'MediaWiki:Common.js' ],
56 [ [], new HashConfig( $settings ), [] ],
57 [ $params, new HashConfig( $settings ), [
58 'MediaWiki:Common.js' => [ 'type' => 'script' ],
59 'MediaWiki:Common.css' => [ 'type' => 'style' ]
61 [ $params, new HashConfig( [ 'UseSiteCss' => false ] +
$settings ), [
62 'MediaWiki:Common.js' => [ 'type' => 'script' ],
64 [ $params, new HashConfig( [ 'UseSiteJs' => false ] +
$settings ), [
65 'MediaWiki:Common.css' => [ 'type' => 'style' ],
69 [ 'UseSiteJs' => false, 'UseSiteCss' => false ]
77 * @covers ResourceLoaderWikiModule::getGroup
78 * @dataProvider provideGetGroup
80 public function testGetGroup( $params, $expected ) {
81 $module = new ResourceLoaderWikiModule( $params );
82 $this->assertEquals( $expected, $module->getGroup() );
85 public static function provideGetGroup() {
90 [ [ 'group' => 'foobar' ], 'foobar' ],
95 * @covers ResourceLoaderWikiModule::isKnownEmpty
96 * @dataProvider provideIsKnownEmpty
98 public function testIsKnownEmpty( $titleInfo, $group, $expected ) {
99 $module = $this->getMockBuilder( 'ResourceLoaderWikiModule' )
100 ->setMethods( [ 'getTitleInfo', 'getGroup' ] )
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()
111 $this->assertEquals( $expected, $module->isKnownEmpty( $context ) );
114 public static function provideIsKnownEmpty() {
117 [ [], 'test1', true ],
118 // 'site' module with a non-empty page
120 [ 'MediaWiki:Common.js' => [ 'page_len' => 1234 ] ],
124 // 'site' module with an empty page
126 [ 'MediaWiki:Foo.js' => [ 'page_len' => 0 ] ],
130 // 'user' module with a non-empty page
132 [ 'User:Example/common.js' => [ 'page_len' => 25 ] ],
136 // 'user' module with an empty page
138 [ 'User:Example/foo.js' => [ 'page_len' => 0 ] ],
146 * @covers ResourceLoaderWikiModule::getTitleInfo
148 public function testGetTitleInfo() {
150 'MediaWiki:Common.css' => [ 'type' => 'styles' ],
151 'mediawiki: fallback.css' => [ 'type' => 'styles' ],
154 'MediaWiki:Common.css' => [ 'page_len' => 1234 ],
155 'MediaWiki:Fallback.css' => [ 'page_len' => 0 ],
157 $expected = $titleInfo;
159 $module = $this->getMockBuilder( 'TestResourceLoaderWikiModule' )
160 ->setMethods( [ 'getPages' ] )
162 $module->method( 'getPages' )->willReturn( $pages );
163 // Can't mock static methods
164 $module::$returnFetchTitleInfo = $titleInfo;
166 $context = $this->getMockBuilder( 'ResourceLoaderContext' )
167 ->disableOriginalConstructor()
170 $module = TestingAccessWrapper
::newFromObject( $module );
171 $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' );
175 * @covers ResourceLoaderWikiModule::getTitleInfo
176 * @covers ResourceLoaderWikiModule::setTitleInfo
177 * @covers ResourceLoaderWikiModule::preloadTitleInfo
179 public function testGetPreloadedTitleInfo() {
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' ],
189 'MediaWiki:Common.css' => [ 'page_len' => 1234 ],
190 'MediaWiki:Fallback.css' => [ 'page_len' => 0 ],
192 $expected = $titleInfo;
194 $module = $this->getMockBuilder( 'TestResourceLoaderWikiModule' )
195 ->setMethods( [ 'getPages' ] )
197 $module->method( 'getPages' )->willReturn( $pages );
198 // Can't mock static methods
199 $module::$returnFetchTitleInfo = $titleInfo;
201 $rl = new EmptyResourceLoader();
202 $rl->register( 'testmodule', $module );
203 $context = new ResourceLoaderContext( $rl, new FauxRequest() );
205 TestResourceLoaderWikiModule
::invalidateModuleCache(
206 Title
::newFromText( 'MediaWiki:Common.css' ),
211 TestResourceLoaderWikiModule
::preloadTitleInfo(
213 wfGetDB( DB_REPLICA
),
217 $module = TestingAccessWrapper
::newFromObject( $module );
218 $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' );
222 * @covers ResourceLoaderWikiModule::preloadTitleInfo
224 public function testGetPreloadedBadTitle() {
227 // Covers else branch for invalid page name
228 '[x]' => [ 'type' => 'styles' ],
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() );
242 TestResourceLoaderWikiModule
::preloadTitleInfo(
244 wfGetDB( DB_REPLICA
),
249 $module = TestingAccessWrapper
::newFromObject( $module );
250 $this->assertEquals( $titleInfo, $module->getTitleInfo( $context ), 'Title info' );
254 * @covers ResourceLoaderWikiModule::preloadTitleInfo
256 public function testGetPreloadedTitleInfoEmpty() {
257 $context = new ResourceLoaderContext( new EmptyResourceLoader(), new FauxRequest() );
258 // Covers early return
261 ResourceLoaderWikiModule
::preloadTitleInfo(
263 wfGetDB( DB_REPLICA
),
269 public static function provideGetContent() {
271 'Bad title' => [ null, '[x]' ],
272 'Dead redirect' => [ null, [
273 'text' => 'Dead redirect',
274 'title' => 'Dead_redirect',
277 'Bad content model' => [ null, [
278 'text' => 'MediaWiki:Wikitext',
279 'ns' => NS_MEDIAWIKI
,
280 'title' => 'Wikitext',
282 'No JS content found' => [ null, [
283 'text' => 'MediaWiki:Script.js',
284 'ns' => NS_MEDIAWIKI
,
285 'title' => 'Script.js',
287 'No CSS content found' => [ null, [
288 'text' => 'MediaWiki:Styles.css',
289 'ns' => NS_MEDIAWIKI
,
290 'title' => 'Script.css',
296 * @covers ResourceLoaderWikiModule::getContent
297 * @dataProvider provideGetContent
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 );
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(
312 new TitleValue( $title['ns'], $title['title'] ),
320 $module = TestingAccessWrapper
::newFromObject( $module );
323 $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;