MediaWiki REL1_31
ResourceLoaderWikiModuleTest.php
Go to the documentation of this file.
1<?php
2
5use Wikimedia\TestingAccessWrapper;
6
8
13 public function testConstructor( $params ) {
14 $module = new ResourceLoaderWikiModule( $params );
15 $this->assertInstanceOf( ResourceLoaderWikiModule::class, $module );
16 }
17
18 public static function provideConstructor() {
19 return [
20 // Nothing
21 [ null ],
22 [ [] ],
23 // Unrecognized settings
24 [ [ 'foo' => 'baz' ] ],
25 // Real settings
26 [ [ 'scripts' => [ 'MediaWiki:Common.js' ] ] ],
27 ];
28 }
29
34 public function testGetPages( $params, Config $config, $expected ) {
35 $module = new ResourceLoaderWikiModule( $params );
36 $module->setConfig( $config );
37
38 // Because getPages is protected..
39 $getPages = new ReflectionMethod( $module, 'getPages' );
40 $getPages->setAccessible( true );
41 $out = $getPages->invoke( $module, ResourceLoaderContext::newDummyContext() );
42 $this->assertEquals( $expected, $out );
43 }
44
45 public static function provideGetPages() {
46 $settings = self::getSettings() + [
47 'UseSiteJs' => true,
48 'UseSiteCss' => true,
49 ];
50
51 $params = [
52 'styles' => [ 'MediaWiki:Common.css' ],
53 'scripts' => [ 'MediaWiki:Common.js' ],
54 ];
55
56 return [
57 [ [], new HashConfig( $settings ), [] ],
58 [ $params, new HashConfig( $settings ), [
59 'MediaWiki:Common.js' => [ 'type' => 'script' ],
60 'MediaWiki:Common.css' => [ 'type' => 'style' ]
61 ] ],
62 [ $params, new HashConfig( [ 'UseSiteCss' => false ] + $settings ), [
63 'MediaWiki:Common.js' => [ 'type' => 'script' ],
64 ] ],
65 [ $params, new HashConfig( [ 'UseSiteJs' => false ] + $settings ), [
66 'MediaWiki:Common.css' => [ 'type' => 'style' ],
67 ] ],
68 [ $params,
69 new HashConfig(
70 [ 'UseSiteJs' => false, 'UseSiteCss' => false ]
71 ),
72 []
73 ],
74 ];
75 }
76
81 public function testGetGroup( $params, $expected ) {
82 $module = new ResourceLoaderWikiModule( $params );
83 $this->assertEquals( $expected, $module->getGroup() );
84 }
85
86 public static function provideGetGroup() {
87 return [
88 // No group specified
89 [ [], null ],
90 // A random group
91 [ [ 'group' => 'foobar' ], 'foobar' ],
92 ];
93 }
94
99 public function testIsKnownEmpty( $titleInfo, $group, $expected ) {
100 $module = $this->getMockBuilder( ResourceLoaderWikiModule::class )
101 ->setMethods( [ 'getTitleInfo', 'getGroup' ] )
102 ->getMock();
103 $module->expects( $this->any() )
104 ->method( 'getTitleInfo' )
105 ->will( $this->returnValue( $titleInfo ) );
106 $module->expects( $this->any() )
107 ->method( 'getGroup' )
108 ->will( $this->returnValue( $group ) );
109 $context = $this->getMockBuilder( ResourceLoaderContext::class )
110 ->disableOriginalConstructor()
111 ->getMock();
112 $this->assertEquals( $expected, $module->isKnownEmpty( $context ) );
113 }
114
115 public static function provideIsKnownEmpty() {
116 return [
117 // No valid pages
118 [ [], 'test1', true ],
119 // 'site' module with a non-empty page
120 [
121 [ 'MediaWiki:Common.js' => [ 'page_len' => 1234 ] ],
122 'site',
123 false,
124 ],
125 // 'site' module with an empty page
126 [
127 [ 'MediaWiki:Foo.js' => [ 'page_len' => 0 ] ],
128 'site',
129 false,
130 ],
131 // 'user' module with a non-empty page
132 [
133 [ 'User:Example/common.js' => [ 'page_len' => 25 ] ],
134 'user',
135 false,
136 ],
137 // 'user' module with an empty page
138 [
139 [ 'User:Example/foo.js' => [ 'page_len' => 0 ] ],
140 'user',
141 true,
142 ],
143 ];
144 }
145
149 public function testGetTitleInfo() {
150 $pages = [
151 'MediaWiki:Common.css' => [ 'type' => 'styles' ],
152 'mediawiki: fallback.css' => [ 'type' => 'styles' ],
153 ];
154 $titleInfo = [
155 'MediaWiki:Common.css' => [ 'page_len' => 1234 ],
156 'MediaWiki:Fallback.css' => [ 'page_len' => 0 ],
157 ];
158 $expected = $titleInfo;
159
160 $module = $this->getMockBuilder( TestResourceLoaderWikiModule::class )
161 ->setMethods( [ 'getPages' ] )
162 ->getMock();
163 $module->method( 'getPages' )->willReturn( $pages );
164 // Can't mock static methods
165 $module::$returnFetchTitleInfo = $titleInfo;
166
167 $context = $this->getMockBuilder( ResourceLoaderContext::class )
168 ->disableOriginalConstructor()
169 ->getMock();
170
171 $module = TestingAccessWrapper::newFromObject( $module );
172 $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' );
173 }
174
180 public function testGetPreloadedTitleInfo() {
181 $pages = [
182 'MediaWiki:Common.css' => [ 'type' => 'styles' ],
183 // Regression against T145673. It's impossible to statically declare page names in
184 // a canonical way since the canonical prefix is localised. As such, the preload
185 // cache computed the right cache key, but failed to find the results when
186 // doing an intersect on the canonical result, producing an empty array.
187 'mediawiki: fallback.css' => [ 'type' => 'styles' ],
188 ];
189 $titleInfo = [
190 'MediaWiki:Common.css' => [ 'page_len' => 1234 ],
191 'MediaWiki:Fallback.css' => [ 'page_len' => 0 ],
192 ];
193 $expected = $titleInfo;
194
195 $module = $this->getMockBuilder( TestResourceLoaderWikiModule::class )
196 ->setMethods( [ 'getPages' ] )
197 ->getMock();
198 $module->method( 'getPages' )->willReturn( $pages );
199 // Can't mock static methods
200 $module::$returnFetchTitleInfo = $titleInfo;
201
202 $rl = new EmptyResourceLoader();
203 $rl->register( 'testmodule', $module );
204 $context = new ResourceLoaderContext( $rl, new FauxRequest() );
205
207 Title::newFromText( 'MediaWiki:Common.css' ),
208 null,
209 null,
210 wfWikiID()
211 );
213 $context,
215 [ 'testmodule' ]
216 );
217
218 $module = TestingAccessWrapper::newFromObject( $module );
219 $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' );
220 }
221
225 public function testGetPreloadedBadTitle() {
226 // Mock values
227 $pages = [
228 // Covers else branch for invalid page name
229 '[x]' => [ 'type' => 'styles' ],
230 ];
231 $titleInfo = [];
232
233 // Set up objects
234 $module = $this->getMockBuilder( TestResourceLoaderWikiModule::class )
235 ->setMethods( [ 'getPages' ] ) ->getMock();
236 $module->method( 'getPages' )->willReturn( $pages );
237 $module::$returnFetchTitleInfo = $titleInfo;
238 $rl = new EmptyResourceLoader();
239 $rl->register( 'testmodule', $module );
240 $context = new ResourceLoaderContext( $rl, new FauxRequest() );
241
242 // Act
244 $context,
246 [ 'testmodule' ]
247 );
248
249 // Assert
250 $module = TestingAccessWrapper::newFromObject( $module );
251 $this->assertEquals( $titleInfo, $module->getTitleInfo( $context ), 'Title info' );
252 }
253
259 // Covers early return
260 $this->assertSame(
261 null,
263 $context,
265 []
266 )
267 );
268 }
269
270 public static function provideGetContent() {
271 return [
272 'Bad title' => [ null, '[x]' ],
273 'Dead redirect' => [ null, [
274 'text' => 'Dead redirect',
275 'title' => 'Dead_redirect',
276 'redirect' => 1,
277 ] ],
278 'Bad content model' => [ null, [
279 'text' => 'MediaWiki:Wikitext',
280 'ns' => NS_MEDIAWIKI,
281 'title' => 'Wikitext',
282 ] ],
283 'No JS content found' => [ null, [
284 'text' => 'MediaWiki:Script.js',
285 'ns' => NS_MEDIAWIKI,
286 'title' => 'Script.js',
287 ] ],
288 'No CSS content found' => [ null, [
289 'text' => 'MediaWiki:Styles.css',
290 'ns' => NS_MEDIAWIKI,
291 'title' => 'Script.css',
292 ] ],
293 ];
294 }
295
300 public function testGetContent( $expected, $title ) {
302 $module = $this->getMockBuilder( ResourceLoaderWikiModule::class )
303 ->setMethods( [ 'getContentObj' ] ) ->getMock();
304 $module->expects( $this->any() )
305 ->method( 'getContentObj' )->willReturn( null );
306
307 if ( is_array( $title ) ) {
308 $title += [ 'ns' => NS_MAIN, 'id' => 1, 'len' => 1, 'redirect' => 0 ];
309 $titleText = $title['text'];
310 // Mock Title db access via LinkCache
311 MediaWikiServices::getInstance()->getLinkCache()->addGoodLinkObj(
312 $title['id'],
313 new TitleValue( $title['ns'], $title['title'] ),
314 $title['len'],
315 $title['redirect']
316 );
317 } else {
318 $titleText = $title;
319 }
320
321 $module = TestingAccessWrapper::newFromObject( $module );
322 $this->assertEquals(
323 $expected,
324 $module->getContent( $titleText )
325 );
326 }
327
331 public function testGetContentForRedirects() {
332 // Set up context and module object
334 $module = $this->getMockBuilder( ResourceLoaderWikiModule::class )
335 ->setMethods( [ 'getPages', 'getContentObj' ] )
336 ->getMock();
337 $module->expects( $this->any() )
338 ->method( 'getPages' )
339 ->will( $this->returnValue( [
340 'MediaWiki:Redirect.js' => [ 'type' => 'script' ]
341 ] ) );
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' )
349 );
350 } elseif ( $title->getPrefixedText() === 'MediaWiki:Target.js' ) {
351 return new JavaScriptContent( 'target;' );
352 } else {
353 return null;
354 }
355 } ) );
356
357 // Mock away Title's db queries with LinkCache
358 MediaWikiServices::getInstance()->getLinkCache()->addGoodLinkObj(
359 1, // id
360 new TitleValue( NS_MEDIAWIKI, 'Redirect.js' ),
361 1, // len
362 1 // redirect
363 );
364
365 $this->assertEquals(
366 "/*\nMediaWiki:Redirect.js\n*/\ntarget;\n",
367 $module->getScript( $context ),
368 'Redirect resolved by getContent'
369 );
370 }
371}
372
374 public static $returnFetchTitleInfo = null;
375 protected static function fetchTitleInfo( IDatabase $db, array $pages, $fname = null ) {
377 self::$returnFetchTitleInfo = null;
378 return $ret;
379 }
380}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Definition Setup.php:112
WebRequest clone which takes values from a provided array.
A Config instance which stores all settings as a member variable.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Object passed around to modules which contains information about the state of a specific loader reque...
static newDummyContext()
Return a dummy ResourceLoaderContext object suitable for passing into things that don't "really" need...
getResourceLoaderContext( $options=[], ResourceLoader $rl=null)
testGetContent( $expected, $title)
ResourceLoaderWikiModule::getContent provideGetContent.
testGetTitleInfo()
ResourceLoaderWikiModule::getTitleInfo.
testIsKnownEmpty( $titleInfo, $group, $expected)
ResourceLoaderWikiModule::isKnownEmpty provideIsKnownEmpty.
testGetPreloadedTitleInfoEmpty()
ResourceLoaderWikiModule::preloadTitleInfo.
testConstructor( $params)
ResourceLoaderWikiModule::__construct provideConstructor.
testGetGroup( $params, $expected)
ResourceLoaderWikiModule::getGroup provideGetGroup.
testGetPages( $params, Config $config, $expected)
provideGetPages ResourceLoaderWikiModule::getPages
testGetPreloadedTitleInfo()
ResourceLoaderWikiModule::getTitleInfo ResourceLoaderWikiModule::setTitleInfo ResourceLoaderWikiModul...
testGetContentForRedirects()
ResourceLoaderWikiModule::getContent.
testGetPreloadedBadTitle()
ResourceLoaderWikiModule::preloadTitleInfo.
Abstraction for ResourceLoader modules which pull from wiki pages.
static preloadTitleInfo(ResourceLoaderContext $context, IDatabase $db, array $moduleNames)
static invalidateModuleCache(Title $title, Revision $old=null, Revision $new=null, $wikiId)
Clear the preloadTitleInfo() cache for all wiki modules on this wiki on page change if it was a JS or...
static fetchTitleInfo(IDatabase $db, array $pages, $fname=null)
Represents a page (or page fragment) title within MediaWiki.
Represents a title within MediaWiki.
Definition Title.php:39
const NS_MAIN
Definition Defines.php:74
const NS_MEDIAWIKI
Definition Defines.php:82
the array() calling protocol came about after MediaWiki 1.4rc1.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition hooks.txt:2811
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:964
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:2006
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:2005
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition hooks.txt:864
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
Interface for configuration instances.
Definition Config.php:28
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
const DB_REPLICA
Definition defines.php:25
$params