Merge "Allow filtering by username on Special:NewFiles"
[lhc/web/wiklou.git] / tests / phpunit / includes / resourceloader / ResourceLoaderWikiModuleTest.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4 use Wikimedia\Rdbms\IDatabase;
5
6 class ResourceLoaderWikiModuleTest extends ResourceLoaderTestCase {
7
8 /**
9 * @covers ResourceLoaderWikiModule::__construct
10 * @dataProvider provideConstructor
11 */
12 public function testConstructor( $params ) {
13 $module = new ResourceLoaderWikiModule( $params );
14 $this->assertInstanceOf( 'ResourceLoaderWikiModule', $module );
15 }
16
17 public static function provideConstructor() {
18 return [
19 // Nothing
20 [ null ],
21 [ [] ],
22 // Unrecognized settings
23 [ [ 'foo' => 'baz' ] ],
24 // Real settings
25 [ [ 'scripts' => [ 'MediaWiki:Common.js' ] ] ],
26 ];
27 }
28
29 /**
30 * @dataProvider provideGetPages
31 * @covers ResourceLoaderWikiModule::getPages
32 */
33 public function testGetPages( $params, Config $config, $expected ) {
34 $module = new ResourceLoaderWikiModule( $params );
35 $module->setConfig( $config );
36
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 );
42 }
43
44 public static function provideGetPages() {
45 $settings = self::getSettings() + [
46 'UseSiteJs' => true,
47 'UseSiteCss' => true,
48 ];
49
50 $params = [
51 'styles' => [ 'MediaWiki:Common.css' ],
52 'scripts' => [ 'MediaWiki:Common.js' ],
53 ];
54
55 return [
56 [ [], new HashConfig( $settings ), [] ],
57 [ $params, new HashConfig( $settings ), [
58 'MediaWiki:Common.js' => [ 'type' => 'script' ],
59 'MediaWiki:Common.css' => [ 'type' => 'style' ]
60 ] ],
61 [ $params, new HashConfig( [ 'UseSiteCss' => false ] + $settings ), [
62 'MediaWiki:Common.js' => [ 'type' => 'script' ],
63 ] ],
64 [ $params, new HashConfig( [ 'UseSiteJs' => false ] + $settings ), [
65 'MediaWiki:Common.css' => [ 'type' => 'style' ],
66 ] ],
67 [ $params,
68 new HashConfig(
69 [ 'UseSiteJs' => false, 'UseSiteCss' => false ]
70 ),
71 []
72 ],
73 ];
74 }
75
76 /**
77 * @covers ResourceLoaderWikiModule::getGroup
78 * @dataProvider provideGetGroup
79 */
80 public function testGetGroup( $params, $expected ) {
81 $module = new ResourceLoaderWikiModule( $params );
82 $this->assertEquals( $expected, $module->getGroup() );
83 }
84
85 public static function provideGetGroup() {
86 return [
87 // No group specified
88 [ [], null ],
89 // A random group
90 [ [ 'group' => 'foobar' ], 'foobar' ],
91 ];
92 }
93
94 /**
95 * @covers ResourceLoaderWikiModule::isKnownEmpty
96 * @dataProvider provideIsKnownEmpty
97 */
98 public function testIsKnownEmpty( $titleInfo, $group, $expected ) {
99 $module = $this->getMockBuilder( 'ResourceLoaderWikiModule' )
100 ->setMethods( [ 'getTitleInfo', 'getGroup' ] )
101 ->getMock();
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()
110 ->getMock();
111 $this->assertEquals( $expected, $module->isKnownEmpty( $context ) );
112 }
113
114 public static function provideIsKnownEmpty() {
115 return [
116 // No valid pages
117 [ [], 'test1', true ],
118 // 'site' module with a non-empty page
119 [
120 [ 'MediaWiki:Common.js' => [ 'page_len' => 1234 ] ],
121 'site',
122 false,
123 ],
124 // 'site' module with an empty page
125 [
126 [ 'MediaWiki:Foo.js' => [ 'page_len' => 0 ] ],
127 'site',
128 false,
129 ],
130 // 'user' module with a non-empty page
131 [
132 [ 'User:Example/common.js' => [ 'page_len' => 25 ] ],
133 'user',
134 false,
135 ],
136 // 'user' module with an empty page
137 [
138 [ 'User:Example/foo.js' => [ 'page_len' => 0 ] ],
139 'user',
140 true,
141 ],
142 ];
143 }
144
145 /**
146 * @covers ResourceLoaderWikiModule::getTitleInfo
147 */
148 public function testGetTitleInfo() {
149 $pages = [
150 'MediaWiki:Common.css' => [ 'type' => 'styles' ],
151 'mediawiki: fallback.css' => [ 'type' => 'styles' ],
152 ];
153 $titleInfo = [
154 'MediaWiki:Common.css' => [ 'page_len' => 1234 ],
155 'MediaWiki:Fallback.css' => [ 'page_len' => 0 ],
156 ];
157 $expected = $titleInfo;
158
159 $module = $this->getMockBuilder( 'TestResourceLoaderWikiModule' )
160 ->setMethods( [ 'getPages' ] )
161 ->getMock();
162 $module->method( 'getPages' )->willReturn( $pages );
163 // Can't mock static methods
164 $module::$returnFetchTitleInfo = $titleInfo;
165
166 $context = $this->getMockBuilder( 'ResourceLoaderContext' )
167 ->disableOriginalConstructor()
168 ->getMock();
169
170 $module = TestingAccessWrapper::newFromObject( $module );
171 $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' );
172 }
173
174 /**
175 * @covers ResourceLoaderWikiModule::getTitleInfo
176 * @covers ResourceLoaderWikiModule::setTitleInfo
177 * @covers ResourceLoaderWikiModule::preloadTitleInfo
178 */
179 public function testGetPreloadedTitleInfo() {
180 $pages = [
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' ],
187 ];
188 $titleInfo = [
189 'MediaWiki:Common.css' => [ 'page_len' => 1234 ],
190 'MediaWiki:Fallback.css' => [ 'page_len' => 0 ],
191 ];
192 $expected = $titleInfo;
193
194 $module = $this->getMockBuilder( 'TestResourceLoaderWikiModule' )
195 ->setMethods( [ 'getPages' ] )
196 ->getMock();
197 $module->method( 'getPages' )->willReturn( $pages );
198 // Can't mock static methods
199 $module::$returnFetchTitleInfo = $titleInfo;
200
201 $rl = new EmptyResourceLoader();
202 $rl->register( 'testmodule', $module );
203 $context = new ResourceLoaderContext( $rl, new FauxRequest() );
204
205 TestResourceLoaderWikiModule::invalidateModuleCache(
206 Title::newFromText( 'MediaWiki:Common.css' ),
207 null,
208 null,
209 wfWikiID()
210 );
211 TestResourceLoaderWikiModule::preloadTitleInfo(
212 $context,
213 wfGetDB( DB_REPLICA ),
214 [ 'testmodule' ]
215 );
216
217 $module = TestingAccessWrapper::newFromObject( $module );
218 $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' );
219 }
220
221 /**
222 * @covers ResourceLoaderWikiModule::getContent
223 */
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' ] )
229 ->getMock();
230 $module->expects( $this->any() )
231 ->method( 'getPages' )
232 ->will( $this->returnValue( [
233 'MediaWiki:Redirect.js' => [ 'type' => 'script' ]
234 ] ) );
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' )
242 );
243 } elseif ( $title->getPrefixedText() === 'MediaWiki:Target.js' ) {
244 return new JavaScriptContent( 'target;' );
245 } else {
246 return null;
247 }
248 } ) );
249
250 // Mock away Title's db queries with LinkCache
251 MediaWikiServices::getInstance()->getLinkCache()->addGoodLinkObj(
252 1, // id
253 new TitleValue( NS_MEDIAWIKI, 'Redirect.js' ),
254 1, // len
255 1 // redirect
256 );
257
258 $this->assertEquals(
259 "/*\nMediaWiki:Redirect.js\n*/\ntarget;\n",
260 $module->getScript( $context ),
261 'Redirect resolved by getContent'
262 );
263 }
264 }
265
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;
271 return $ret;
272 }
273 }