3 use MediaWiki\MediaWikiServices
;
5 class ResourceLoaderWikiModuleTest
extends ResourceLoaderTestCase
{
8 * @covers ResourceLoaderWikiModule::__construct
9 * @dataProvider provideConstructor
11 public function testConstructor( $params ) {
12 $module = new ResourceLoaderWikiModule( $params );
13 $this->assertInstanceOf( 'ResourceLoaderWikiModule', $module );
16 public static function provideConstructor() {
21 // Unrecognized settings
22 [ [ 'foo' => 'baz' ] ],
24 [ [ 'scripts' => [ 'MediaWiki:Common.js' ] ] ],
29 * @dataProvider provideGetPages
30 * @covers ResourceLoaderWikiModule::getPages
32 public function testGetPages( $params, Config
$config, $expected ) {
33 $module = new ResourceLoaderWikiModule( $params );
34 $module->setConfig( $config );
36 // Because getPages is protected..
37 $getPages = new ReflectionMethod( $module, 'getPages' );
38 $getPages->setAccessible( true );
39 $out = $getPages->invoke( $module, ResourceLoaderContext
::newDummyContext() );
40 $this->assertEquals( $expected, $out );
43 public static function provideGetPages() {
44 $settings = self
::getSettings() +
[
50 'styles' => [ 'MediaWiki:Common.css' ],
51 'scripts' => [ 'MediaWiki:Common.js' ],
55 [ [], new HashConfig( $settings ), [] ],
56 [ $params, new HashConfig( $settings ), [
57 'MediaWiki:Common.js' => [ 'type' => 'script' ],
58 'MediaWiki:Common.css' => [ 'type' => 'style' ]
60 [ $params, new HashConfig( [ 'UseSiteCss' => false ] +
$settings ), [
61 'MediaWiki:Common.js' => [ 'type' => 'script' ],
63 [ $params, new HashConfig( [ 'UseSiteJs' => false ] +
$settings ), [
64 'MediaWiki:Common.css' => [ 'type' => 'style' ],
68 [ 'UseSiteJs' => false, 'UseSiteCss' => false ]
76 * @covers ResourceLoaderWikiModule::getGroup
77 * @dataProvider provideGetGroup
79 public function testGetGroup( $params, $expected ) {
80 $module = new ResourceLoaderWikiModule( $params );
81 $this->assertEquals( $expected, $module->getGroup() );
84 public static function provideGetGroup() {
89 [ [ 'group' => 'foobar' ], 'foobar' ],
94 * @covers ResourceLoaderWikiModule::isKnownEmpty
95 * @dataProvider provideIsKnownEmpty
97 public function testIsKnownEmpty( $titleInfo, $group, $expected ) {
98 $module = $this->getMockBuilder( 'ResourceLoaderWikiModule' )
99 ->setMethods( [ 'getTitleInfo', 'getGroup' ] )
101 $module->expects( $this->any() )
102 ->method( 'getTitleInfo' )
103 ->will( $this->returnValue( $titleInfo ) );
104 $module->expects( $this->any() )
105 ->method( 'getGroup' )
106 ->will( $this->returnValue( $group ) );
107 $context = $this->getMockBuilder( 'ResourceLoaderContext' )
108 ->disableOriginalConstructor()
110 $this->assertEquals( $expected, $module->isKnownEmpty( $context ) );
113 public static function provideIsKnownEmpty() {
116 [ [], 'test1', true ],
117 // 'site' module with a non-empty page
119 [ 'MediaWiki:Common.js' => [ 'page_len' => 1234 ] ],
123 // 'site' module with an empty page
125 [ 'MediaWiki:Foo.js' => [ 'page_len' => 0 ] ],
129 // 'user' module with a non-empty page
131 [ 'User:Example/common.js' => [ 'page_len' => 25 ] ],
135 // 'user' module with an empty page
137 [ 'User:Example/foo.js' => [ 'page_len' => 0 ] ],
145 * @covers ResourceLoaderWikiModule::getTitleInfo
147 public function testGetTitleInfo() {
149 'MediaWiki:Common.css' => [ 'type' => 'styles' ],
150 'mediawiki: fallback.css' => [ 'type' => 'styles' ],
153 'MediaWiki:Common.css' => [ 'page_len' => 1234 ],
154 'MediaWiki:Fallback.css' => [ 'page_len' => 0 ],
156 $expected = $titleInfo;
158 $module = $this->getMockBuilder( 'TestResourceLoaderWikiModule' )
159 ->setMethods( [ 'getPages' ] )
161 $module->method( 'getPages' )->willReturn( $pages );
162 // Can't mock static methods
163 $module::$returnFetchTitleInfo = $titleInfo;
165 $context = $this->getMockBuilder( 'ResourceLoaderContext' )
166 ->disableOriginalConstructor()
169 $module = TestingAccessWrapper
::newFromObject( $module );
170 $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' );
174 * @covers ResourceLoaderWikiModule::getTitleInfo
175 * @covers ResourceLoaderWikiModule::setTitleInfo
176 * @covers ResourceLoaderWikiModule::preloadTitleInfo
178 public function testGetPreloadedTitleInfo() {
180 'MediaWiki:Common.css' => [ 'type' => 'styles' ],
181 // Regression against T145673. It's impossible to statically declare page names in
182 // a canonical way since the canonical prefix is localised. As such, the preload
183 // cache computed the right cache key, but failed to find the results when
184 // doing an intersect on the canonical result, producing an empty array.
185 'mediawiki: fallback.css' => [ 'type' => 'styles' ],
188 'MediaWiki:Common.css' => [ 'page_len' => 1234 ],
189 'MediaWiki:Fallback.css' => [ 'page_len' => 0 ],
191 $expected = $titleInfo;
193 $module = $this->getMockBuilder( 'TestResourceLoaderWikiModule' )
194 ->setMethods( [ 'getPages' ] )
196 $module->method( 'getPages' )->willReturn( $pages );
197 // Can't mock static methods
198 $module::$returnFetchTitleInfo = $titleInfo;
200 $rl = new EmptyResourceLoader();
201 $rl->register( 'testmodule', $module );
202 $context = new ResourceLoaderContext( $rl, new FauxRequest() );
204 TestResourceLoaderWikiModule
::invalidateModuleCache(
205 Title
::newFromText( 'MediaWiki:Common.css' ),
210 TestResourceLoaderWikiModule
::preloadTitleInfo(
212 wfGetDB( DB_REPLICA
),
216 $module = TestingAccessWrapper
::newFromObject( $module );
217 $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' );
221 * @covers ResourceLoaderWikiModule::getContent
223 public function testGetContentForRedirects() {
224 // Set up context and module object
225 $context = $this->getResourceLoaderContext( [], new EmptyResourceLoader
);
226 $module = $this->getMockBuilder( 'ResourceLoaderWikiModule' )
227 ->setMethods( [ 'getPages', 'getContentObj' ] )
229 $module->expects( $this->any() )
230 ->method( 'getPages' )
231 ->will( $this->returnValue( [
232 'MediaWiki:Redirect.js' => [ 'type' => 'script' ]
234 $module->expects( $this->any() )
235 ->method( 'getContentObj' )
236 ->will( $this->returnCallback( function ( Title
$title ) {
237 if ( $title->getPrefixedText() === 'MediaWiki:Redirect.js' ) {
238 $handler = new JavaScriptContentHandler();
239 return $handler->makeRedirectContent(
240 Title
::makeTitle( NS_MEDIAWIKI
, 'Target.js' )
242 } elseif ( $title->getPrefixedText() === 'MediaWiki:Target.js' ) {
243 return new JavaScriptContent( 'target;' );
249 // Mock away Title's db queries with LinkCache
250 MediaWikiServices
::getInstance()->getLinkCache()->addGoodLinkObj(
252 new TitleValue( NS_MEDIAWIKI
, 'Redirect.js' ),
258 "/*\nMediaWiki:Redirect.js\n*/\ntarget;\n",
259 $module->getScript( $context ),
260 'Redirect resolved by getContent'
265 class TestResourceLoaderWikiModule
extends ResourceLoaderWikiModule
{
266 public static $returnFetchTitleInfo = null;
267 protected static function fetchTitleInfo( IDatabase
$db, array $pages, $fname = null ) {
268 $ret = self
::$returnFetchTitleInfo;
269 self
::$returnFetchTitleInfo = null;