MediaWiki  1.30.0
ResourceLoaderWikiModuleTest.php
Go to the documentation of this file.
1 <?php
2 
5 use Wikimedia\TestingAccessWrapper;
6 
8 
13  public function testConstructor( $params ) {
14  $module = new ResourceLoaderWikiModule( $params );
15  $this->assertInstanceOf( 'ResourceLoaderWikiModule', $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' )
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' )
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' )
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' )
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' )
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,
214  wfGetDB( DB_REPLICA ),
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' )
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,
245  wfGetDB( DB_REPLICA ),
246  [ 'testmodule' ]
247  );
248 
249  // Assert
250  $module = TestingAccessWrapper::newFromObject( $module );
251  $this->assertEquals( $titleInfo, $module->getTitleInfo( $context ), 'Title info' );
252  }
253 
257  public function testGetPreloadedTitleInfoEmpty() {
259  // Covers early return
260  $this->assertSame(
261  null,
263  $context,
264  wfGetDB( DB_REPLICA ),
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' )
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' )
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' ) {
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 }
ResourceLoaderContext
Object passed around to modules which contains information about the state of a specific loader reque...
Definition: ResourceLoaderContext.php:32
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
ResourceLoaderWikiModuleTest\testIsKnownEmpty
testIsKnownEmpty( $titleInfo, $group, $expected)
ResourceLoaderWikiModule::isKnownEmpty provideIsKnownEmpty.
Definition: ResourceLoaderWikiModuleTest.php:99
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:268
ResourceLoaderContext\newDummyContext
static newDummyContext()
Return a dummy ResourceLoaderContext object suitable for passing into things that don't "really" need...
Definition: ResourceLoaderContext.php:139
$context
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:2581
HashConfig
A Config instance which stores all settings as a member variable.
Definition: HashConfig.php:28
ResourceLoaderWikiModuleTest\provideGetContent
static provideGetContent()
Definition: ResourceLoaderWikiModuleTest.php:270
ResourceLoaderWikiModuleTest
Definition: ResourceLoaderWikiModuleTest.php:7
ResourceLoaderWikiModuleTest\testGetGroup
testGetGroup( $params, $expected)
ResourceLoaderWikiModule::getGroup provideGetGroup.
Definition: ResourceLoaderWikiModuleTest.php:81
ResourceLoaderWikiModule
Abstraction for ResourceLoader modules which pull from wiki pages.
Definition: ResourceLoaderWikiModule.php:48
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
ResourceLoaderWikiModuleTest\testGetPreloadedTitleInfo
testGetPreloadedTitleInfo()
ResourceLoaderWikiModule::getTitleInfo ResourceLoaderWikiModule::setTitleInfo ResourceLoaderWikiModul...
Definition: ResourceLoaderWikiModuleTest.php:180
$fname
if(!defined( 'MEDIAWIKI')) $fname
This file is not a valid entry point, perform no further processing unless MEDIAWIKI is defined.
Definition: Setup.php:36
$params
$params
Definition: styleTest.css.php:40
TestResourceLoaderWikiModule
Definition: ResourceLoaderWikiModuleTest.php:373
ResourceLoaderTestCase\getSettings
static getSettings()
Definition: ResourceLoaderTestCase.php:51
php
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:35
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:40
NS_MAIN
const NS_MAIN
Definition: Defines.php:65
ResourceLoaderWikiModuleTest\testGetTitleInfo
testGetTitleInfo()
ResourceLoaderWikiModule::getTitleInfo.
Definition: ResourceLoaderWikiModuleTest.php:149
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:932
TestResourceLoaderWikiModule\fetchTitleInfo
static fetchTitleInfo(IDatabase $db, array $pages, $fname=null)
Definition: ResourceLoaderWikiModuleTest.php:375
ResourceLoaderWikiModuleTest\provideGetPages
static provideGetPages()
Definition: ResourceLoaderWikiModuleTest.php:45
TestResourceLoaderWikiModule\$returnFetchTitleInfo
static $returnFetchTitleInfo
Definition: ResourceLoaderWikiModuleTest.php:374
ResourceLoaderWikiModule\preloadTitleInfo
static preloadTitleInfo(ResourceLoaderContext $context, IDatabase $db, array $moduleNames)
Definition: ResourceLoaderWikiModule.php:362
ResourceLoaderWikiModuleTest\testGetContent
testGetContent( $expected, $title)
ResourceLoaderWikiModule::getContent provideGetContent.
Definition: ResourceLoaderWikiModuleTest.php:300
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2856
ResourceLoaderWikiModuleTest\provideConstructor
static provideConstructor()
Definition: ResourceLoaderWikiModuleTest.php:18
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:529
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
ResourceLoaderWikiModuleTest\testGetPreloadedBadTitle
testGetPreloadedBadTitle()
ResourceLoaderWikiModule::preloadTitleInfo.
Definition: ResourceLoaderWikiModuleTest.php:225
JavaScriptContent
Content for JavaScript pages.
Definition: JavaScriptContent.php:33
ResourceLoaderWikiModuleTest\testGetContentForRedirects
testGetContentForRedirects()
ResourceLoaderWikiModule::getContent.
Definition: ResourceLoaderWikiModuleTest.php:331
any
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
Definition: COPYING.txt:326
wfWikiID
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
Definition: GlobalFunctions.php:2807
$ret
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:1965
ResourceLoaderWikiModuleTest\provideGetGroup
static provideGetGroup()
Definition: ResourceLoaderWikiModuleTest.php:86
JavaScriptContentHandler
Content handler for JavaScript pages.
Definition: JavaScriptContentHandler.php:29
$handler
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 modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check set to true or false to override the $wgMaxImageArea check result gives extension the possibility to transform it themselves $handler
Definition: hooks.txt:781
ResourceLoaderWikiModuleTest\testGetPreloadedTitleInfoEmpty
testGetPreloadedTitleInfoEmpty()
ResourceLoaderWikiModule::preloadTitleInfo.
Definition: ResourceLoaderWikiModuleTest.php:257
Title
Represents a title within MediaWiki.
Definition: Title.php:39
ResourceLoaderWikiModuleTest\testConstructor
testConstructor( $params)
ResourceLoaderWikiModule::__construct provideConstructor.
Definition: ResourceLoaderWikiModuleTest.php:13
EmptyResourceLoader
Definition: ResourceLoaderTestCase.php:176
ResourceLoaderTestCase\getResourceLoaderContext
getResourceLoaderContext( $options=[], ResourceLoader $rl=null)
Definition: ResourceLoaderTestCase.php:23
true
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:1965
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition: Defines.php:73
MediaWikiServices
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 MediaWikiServices
Definition: injection.txt:23
ResourceLoaderWikiModule\invalidateModuleCache
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...
Definition: ResourceLoaderWikiModule.php:445
ResourceLoaderWikiModuleTest\provideIsKnownEmpty
static provideIsKnownEmpty()
Definition: ResourceLoaderWikiModuleTest.php:115
ResourceLoaderWikiModuleTest\testGetPages
testGetPages( $params, Config $config, $expected)
provideGetPages ResourceLoaderWikiModule::getPages
Definition: ResourceLoaderWikiModuleTest.php:34
array
the array() calling protocol came about after MediaWiki 1.4rc1.
TitleValue
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:36
ResourceLoaderTestCase
Definition: ResourceLoaderTestCase.php:7
$out
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:781