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