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::getContent
224 public function testGetContentForRedirects() {
225 // Set up context and module object
226 $context = $this->getResourceLoaderContext( [], new EmptyResourceLoader
);
227 $module = $this->getMockBuilder( 'ResourceLoaderWikiModule' )
228 ->setMethods( [ 'getPages', 'getContentObj' ] )
230 $module->expects( $this->any() )
231 ->method( 'getPages' )
232 ->will( $this->returnValue( [
233 'MediaWiki:Redirect.js' => [ 'type' => 'script' ]
235 $module->expects( $this->any() )
236 ->method( 'getContentObj' )
237 ->will( $this->returnCallback( function ( Title
$title ) {
238 if ( $title->getPrefixedText() === 'MediaWiki:Redirect.js' ) {
239 $handler = new JavaScriptContentHandler();
240 return $handler->makeRedirectContent(
241 Title
::makeTitle( NS_MEDIAWIKI
, 'Target.js' )
243 } elseif ( $title->getPrefixedText() === 'MediaWiki:Target.js' ) {
244 return new JavaScriptContent( 'target;' );
250 // Mock away Title's db queries with LinkCache
251 MediaWikiServices
::getInstance()->getLinkCache()->addGoodLinkObj(
253 new TitleValue( NS_MEDIAWIKI
, 'Redirect.js' ),
259 "/*\nMediaWiki:Redirect.js\n*/\ntarget;\n",
260 $module->getScript( $context ),
261 'Redirect resolved by getContent'
266 class TestResourceLoaderWikiModule
extends ResourceLoaderWikiModule
{
267 public static $returnFetchTitleInfo = null;
268 protected static function fetchTitleInfo( IDatabase
$db, array $pages, $fname = null ) {
269 $ret = self
::$returnFetchTitleInfo;
270 self
::$returnFetchTitleInfo = null;