MediaWiki REL1_33
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 private function prepareTitleInfo( array $mockInfo ) {
19 $module = TestingAccessWrapper::newFromClass( ResourceLoaderWikiModule::class );
20 $info = [];
21 foreach ( $mockInfo as $key => $val ) {
22 $info[ $module->makeTitleKey( Title::newFromText( $key ) ) ] = $val;
23 }
24 return $info;
25 }
26
27 public static function provideConstructor() {
28 return [
29 // Nothing
30 [ null ],
31 [ [] ],
32 // Unrecognized settings
33 [ [ 'foo' => 'baz' ] ],
34 // Real settings
35 [ [ 'scripts' => [ 'MediaWiki:Common.js' ] ] ],
36 ];
37 }
38
43 public function testGetPages( $params, Config $config, $expected ) {
44 $module = new ResourceLoaderWikiModule( $params );
45 $module->setConfig( $config );
46
47 // Because getPages is protected..
48 $getPages = new ReflectionMethod( $module, 'getPages' );
49 $getPages->setAccessible( true );
50 $out = $getPages->invoke( $module, ResourceLoaderContext::newDummyContext() );
51 $this->assertEquals( $expected, $out );
52 }
53
54 public static function provideGetPages() {
55 $settings = self::getSettings() + [
56 'UseSiteJs' => true,
57 'UseSiteCss' => true,
58 ];
59
60 $params = [
61 'styles' => [ 'MediaWiki:Common.css' ],
62 'scripts' => [ 'MediaWiki:Common.js' ],
63 ];
64
65 return [
66 [ [], new HashConfig( $settings ), [] ],
67 [ $params, new HashConfig( $settings ), [
68 'MediaWiki:Common.js' => [ 'type' => 'script' ],
69 'MediaWiki:Common.css' => [ 'type' => 'style' ]
70 ] ],
71 [ $params, new HashConfig( [ 'UseSiteCss' => false ] + $settings ), [
72 'MediaWiki:Common.js' => [ 'type' => 'script' ],
73 ] ],
74 [ $params, new HashConfig( [ 'UseSiteJs' => false ] + $settings ), [
75 'MediaWiki:Common.css' => [ 'type' => 'style' ],
76 ] ],
77 [ $params,
78 new HashConfig(
79 [ 'UseSiteJs' => false, 'UseSiteCss' => false ]
80 ),
81 []
82 ],
83 ];
84 }
85
90 public function testGetGroup( $params, $expected ) {
91 $module = new ResourceLoaderWikiModule( $params );
92 $this->assertEquals( $expected, $module->getGroup() );
93 }
94
95 public static function provideGetGroup() {
96 return [
97 // No group specified
98 [ [], null ],
99 // A random group
100 [ [ 'group' => 'foobar' ], 'foobar' ],
101 ];
102 }
103
108 public function testIsKnownEmpty( $titleInfo, $group, $dependencies, $expected ) {
109 $module = $this->getMockBuilder( ResourceLoaderWikiModule::class )
110 ->setMethods( [ 'getTitleInfo', 'getGroup', 'getDependencies' ] )
111 ->getMock();
112 $module->expects( $this->any() )
113 ->method( 'getTitleInfo' )
114 ->will( $this->returnValue( $this->prepareTitleInfo( $titleInfo ) ) );
115 $module->expects( $this->any() )
116 ->method( 'getGroup' )
117 ->will( $this->returnValue( $group ) );
118 $module->expects( $this->any() )
119 ->method( 'getDependencies' )
120 ->will( $this->returnValue( $dependencies ) );
121 $context = $this->getMockBuilder( ResourceLoaderContext::class )
122 ->disableOriginalConstructor()
123 ->getMock();
124 $this->assertEquals( $expected, $module->isKnownEmpty( $context ) );
125 }
126
127 public static function provideIsKnownEmpty() {
128 return [
129 // No valid pages
130 [ [], 'test1', [], true ],
131 // 'site' module with a non-empty page
132 [
133 [ 'MediaWiki:Common.js' => [ 'page_len' => 1234 ] ],
134 'site',
135 [],
136 false,
137 ],
138 // 'site' module without existing pages but dependencies
139 [
140 [],
141 'site',
142 [ 'mobile.css' ],
143 false,
144 ],
145 // 'site' module which is empty but has dependencies
146 [
147 [ 'MediaWiki:Common.js' => [ 'page_len' => 0 ] ],
148 'site',
149 [ 'mobile.css' ],
150 false,
151 ],
152 // 'site' module with an empty page
153 [
154 [ 'MediaWiki:Foo.js' => [ 'page_len' => 0 ] ],
155 'site',
156 [],
157 false,
158 ],
159 // 'user' module with a non-empty page
160 [
161 [ 'User:Example/common.js' => [ 'page_len' => 25 ] ],
162 'user',
163 [],
164 false,
165 ],
166 // 'user' module with an empty page
167 [
168 [ 'User:Example/foo.js' => [ 'page_len' => 0 ] ],
169 'user',
170 [],
171 true,
172 ],
173 ];
174 }
175
179 public function testGetTitleInfo() {
180 $pages = [
181 'MediaWiki:Common.css' => [ 'type' => 'styles' ],
182 'mediawiki: fallback.css' => [ 'type' => 'styles' ],
183 ];
184 $titleInfo = $this->prepareTitleInfo( [
185 'MediaWiki:Common.css' => [ 'page_len' => 1234 ],
186 'MediaWiki:Fallback.css' => [ 'page_len' => 0 ],
187 ] );
188 $expected = $titleInfo;
189
190 $module = $this->getMockBuilder( TestResourceLoaderWikiModule::class )
191 ->setMethods( [ 'getPages' ] )
192 ->getMock();
193 $module->method( 'getPages' )->willReturn( $pages );
194 // Can't mock static methods
195 $module::$returnFetchTitleInfo = $titleInfo;
196
197 $context = $this->getMockBuilder( ResourceLoaderContext::class )
198 ->disableOriginalConstructor()
199 ->getMock();
200
201 $module = TestingAccessWrapper::newFromObject( $module );
202 $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' );
203 }
204
210 public function testGetPreloadedTitleInfo() {
211 $pages = [
212 'MediaWiki:Common.css' => [ 'type' => 'styles' ],
213 // Regression against T145673. It's impossible to statically declare page names in
214 // a canonical way since the canonical prefix is localised. As such, the preload
215 // cache computed the right cache key, but failed to find the results when
216 // doing an intersect on the canonical result, producing an empty array.
217 'mediawiki: fallback.css' => [ 'type' => 'styles' ],
218 ];
219 $titleInfo = $this->prepareTitleInfo( [
220 'MediaWiki:Common.css' => [ 'page_len' => 1234 ],
221 'MediaWiki:Fallback.css' => [ 'page_len' => 0 ],
222 ] );
223 $expected = $titleInfo;
224
225 $module = $this->getMockBuilder( TestResourceLoaderWikiModule::class )
226 ->setMethods( [ 'getPages' ] )
227 ->getMock();
228 $module->method( 'getPages' )->willReturn( $pages );
229 // Can't mock static methods
230 $module::$returnFetchTitleInfo = $titleInfo;
231
232 $rl = new EmptyResourceLoader();
233 $rl->register( 'testmodule', $module );
234 $context = new ResourceLoaderContext( $rl, new FauxRequest() );
235
237 Title::newFromText( 'MediaWiki:Common.css' ),
238 null,
239 null,
240 wfWikiID()
241 );
243 $context,
245 [ 'testmodule' ]
246 );
247
248 $module = TestingAccessWrapper::newFromObject( $module );
249 $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' );
250 }
251
255 public function testGetPreloadedBadTitle() {
256 // Mock values
257 $pages = [
258 // Covers else branch for invalid page name
259 '[x]' => [ 'type' => 'styles' ],
260 ];
261 $titleInfo = [];
262
263 // Set up objects
264 $module = $this->getMockBuilder( TestResourceLoaderWikiModule::class )
265 ->setMethods( [ 'getPages' ] )->getMock();
266 $module->method( 'getPages' )->willReturn( $pages );
267 $module::$returnFetchTitleInfo = $titleInfo;
268 $rl = new EmptyResourceLoader();
269 $rl->register( 'testmodule', $module );
270 $context = new ResourceLoaderContext( $rl, new FauxRequest() );
271
272 // Act
274 $context,
276 [ 'testmodule' ]
277 );
278
279 // Assert
280 $module = TestingAccessWrapper::newFromObject( $module );
281 $this->assertEquals( $titleInfo, $module->getTitleInfo( $context ), 'Title info' );
282 }
283
289 // Covers early return
290 $this->assertSame(
291 null,
292 ResourceLoaderWikiModule::preloadTitleInfo(
293 $context,
295 []
296 )
297 );
298 }
299
300 public static function provideGetContent() {
301 return [
302 'Bad title' => [ null, '[x]' ],
303 'Dead redirect' => [ null, [
304 'text' => 'Dead redirect',
305 'title' => 'Dead_redirect',
306 'redirect' => 1,
307 ] ],
308 'Bad content model' => [ null, [
309 'text' => 'MediaWiki:Wikitext',
310 'ns' => NS_MEDIAWIKI,
311 'title' => 'Wikitext',
312 ] ],
313 'No JS content found' => [ null, [
314 'text' => 'MediaWiki:Script.js',
315 'ns' => NS_MEDIAWIKI,
316 'title' => 'Script.js',
317 ] ],
318 'No CSS content found' => [ null, [
319 'text' => 'MediaWiki:Styles.css',
320 'ns' => NS_MEDIAWIKI,
321 'title' => 'Script.css',
322 ] ],
323 ];
324 }
325
330 public function testGetContent( $expected, $title ) {
332 $module = $this->getMockBuilder( ResourceLoaderWikiModule::class )
333 ->setMethods( [ 'getContentObj' ] )->getMock();
334 $module->expects( $this->any() )
335 ->method( 'getContentObj' )->willReturn( null );
336
337 if ( is_array( $title ) ) {
338 $title += [ 'ns' => NS_MAIN, 'id' => 1, 'len' => 1, 'redirect' => 0 ];
339 $titleText = $title['text'];
340 // Mock Title db access via LinkCache
341 MediaWikiServices::getInstance()->getLinkCache()->addGoodLinkObj(
342 $title['id'],
343 new TitleValue( $title['ns'], $title['title'] ),
344 $title['len'],
345 $title['redirect']
346 );
347 } else {
348 $titleText = $title;
349 }
350
351 $module = TestingAccessWrapper::newFromObject( $module );
352 $this->assertEquals(
353 $expected,
354 $module->getContent( $titleText, $context )
355 );
356 }
357
363 public function testContentOverrides() {
364 $pages = [
365 'MediaWiki:Common.css' => [ 'type' => 'style' ],
366 ];
367
368 $module = $this->getMockBuilder( TestResourceLoaderWikiModule::class )
369 ->setMethods( [ 'getPages' ] )
370 ->getMock();
371 $module->method( 'getPages' )->willReturn( $pages );
372
373 $rl = new EmptyResourceLoader();
374 $rl->register( 'testmodule', $module );
376 new ResourceLoaderContext( $rl, new FauxRequest() )
377 );
378 $context->setContentOverrideCallback( function ( Title $t ) {
379 if ( $t->getPrefixedText() === 'MediaWiki:Common.css' ) {
380 return new CssContent( '.override{}' );
381 }
382 return null;
383 } );
384
385 $this->assertTrue( $module->shouldEmbedModule( $context ) );
386 $this->assertEquals( [
387 'all' => [
388 "/*\nMediaWiki:Common.css\n*/\n.override{}"
389 ]
390 ], $module->getStyles( $context ) );
391
392 $context->setContentOverrideCallback( function ( Title $t ) {
393 if ( $t->getPrefixedText() === 'MediaWiki:Skin.css' ) {
394 return new CssContent( '.override{}' );
395 }
396 return null;
397 } );
398 $this->assertFalse( $module->shouldEmbedModule( $context ) );
399 }
400
405 public function testGetContentForRedirects() {
406 // Set up context and module object
409 );
410 $module = $this->getMockBuilder( ResourceLoaderWikiModule::class )
411 ->setMethods( [ 'getPages' ] )
412 ->getMock();
413 $module->expects( $this->any() )
414 ->method( 'getPages' )
415 ->will( $this->returnValue( [
416 'MediaWiki:Redirect.js' => [ 'type' => 'script' ]
417 ] ) );
418 $context->setContentOverrideCallback( function ( Title $title ) {
419 if ( $title->getPrefixedText() === 'MediaWiki:Redirect.js' ) {
420 $handler = new JavaScriptContentHandler();
421 return $handler->makeRedirectContent(
422 Title::makeTitle( NS_MEDIAWIKI, 'Target.js' )
423 );
424 } elseif ( $title->getPrefixedText() === 'MediaWiki:Target.js' ) {
425 return new JavaScriptContent( 'target;' );
426 } else {
427 return null;
428 }
429 } );
430
431 // Mock away Title's db queries with LinkCache
432 MediaWikiServices::getInstance()->getLinkCache()->addGoodLinkObj(
433 1, // id
434 new TitleValue( NS_MEDIAWIKI, 'Redirect.js' ),
435 1, // len
436 1 // redirect
437 );
438
439 $this->assertEquals(
440 "/*\nMediaWiki:Redirect.js\n*/\ntarget;\n",
441 $module->getScript( $context ),
442 'Redirect resolved by getContent'
443 );
444 }
445
446 function tearDown() {
447 Title::clearCaches();
448 parent::tearDown();
449 }
450}
451
453 public static $returnFetchTitleInfo = null;
454 protected static function fetchTitleInfo( IDatabase $db, array $pages, $fname = null ) {
456 self::$returnFetchTitleInfo = null;
457 return $ret;
458 }
459}
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:123
Allows changing specific properties of a context object, without changing the main one.
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)
testContentOverrides()
ResourceLoaderWikiModule::getContent ResourceLoaderWikiModule::getContentObj ResourceLoaderWikiModule...
testGetContent( $expected, $title)
ResourceLoaderWikiModule::getContent provideGetContent.
testGetTitleInfo()
ResourceLoaderWikiModule::getTitleInfo.
testIsKnownEmpty( $titleInfo, $group, $dependencies, $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 ResourceLoaderWikiModule::getContentObj.
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, $domain)
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:40
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
const NS_MAIN
Definition Defines.php:73
const NS_MEDIAWIKI
Definition Defines.php:81
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password 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:855
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:2848
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:955
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition hooks.txt:783
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:2004
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:2003
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
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
const DB_REPLICA
Definition defines.php:25
$params