MediaWiki REL1_32
TitleMethodsTest.php
Go to the documentation of this file.
1<?php
2
5
14
15 protected function setUp() {
16 parent::setUp();
17
19 'wgExtraNamespaces',
20 [
21 12302 => 'TEST-JS',
22 12303 => 'TEST-JS_TALK',
23 ]
24 );
25
27 'wgNamespaceContentModels',
28 [
30 ]
31 );
32 }
33
34 public static function provideEquals() {
35 return [
36 [ 'Main Page', 'Main Page', true ],
37 [ 'Main Page', 'Not The Main Page', false ],
38 [ 'Main Page', 'Project:Main Page', false ],
39 [ 'File:Example.png', 'Image:Example.png', true ],
40 [ 'Special:Version', 'Special:Version', true ],
41 [ 'Special:Version', 'Special:Recentchanges', false ],
42 [ 'Special:Version', 'Main Page', false ],
43 ];
44 }
45
50 public function testEquals( $titleA, $titleB, $expectedBool ) {
51 $titleA = Title::newFromText( $titleA );
52 $titleB = Title::newFromText( $titleB );
53
54 $this->assertEquals( $expectedBool, $titleA->equals( $titleB ) );
55 $this->assertEquals( $expectedBool, $titleB->equals( $titleA ) );
56 }
57
58 public static function provideInNamespace() {
59 return [
60 [ 'Main Page', NS_MAIN, true ],
61 [ 'Main Page', NS_TALK, false ],
62 [ 'Main Page', NS_USER, false ],
63 [ 'User:Foo', NS_USER, true ],
64 [ 'User:Foo', NS_USER_TALK, false ],
65 [ 'User:Foo', NS_TEMPLATE, false ],
66 [ 'User_talk:Foo', NS_USER_TALK, true ],
67 [ 'User_talk:Foo', NS_USER, false ],
68 ];
69 }
70
75 public function testInNamespace( $title, $ns, $expectedBool ) {
76 $title = Title::newFromText( $title );
77 $this->assertEquals( $expectedBool, $title->inNamespace( $ns ) );
78 }
79
83 public function testInNamespaces() {
84 $mainpage = Title::newFromText( 'Main Page' );
85 $this->assertTrue( $mainpage->inNamespaces( NS_MAIN, NS_USER ) );
86 $this->assertTrue( $mainpage->inNamespaces( [ NS_MAIN, NS_USER ] ) );
87 $this->assertTrue( $mainpage->inNamespaces( [ NS_USER, NS_MAIN ] ) );
88 $this->assertFalse( $mainpage->inNamespaces( [ NS_PROJECT, NS_TEMPLATE ] ) );
89 }
90
91 public static function provideHasSubjectNamespace() {
92 return [
93 [ 'Main Page', NS_MAIN, true ],
94 [ 'Main Page', NS_TALK, true ],
95 [ 'Main Page', NS_USER, false ],
96 [ 'User:Foo', NS_USER, true ],
97 [ 'User:Foo', NS_USER_TALK, true ],
98 [ 'User:Foo', NS_TEMPLATE, false ],
99 [ 'User_talk:Foo', NS_USER_TALK, true ],
100 [ 'User_talk:Foo', NS_USER, true ],
101 ];
102 }
103
108 public function testHasSubjectNamespace( $title, $ns, $expectedBool ) {
109 $title = Title::newFromText( $title );
110 $this->assertEquals( $expectedBool, $title->hasSubjectNamespace( $ns ) );
111 }
112
113 public function dataGetContentModel() {
114 return [
115 [ 'Help:Foo', CONTENT_MODEL_WIKITEXT ],
116 [ 'Help:Foo.js', CONTENT_MODEL_WIKITEXT ],
117 [ 'Help:Foo/bar.js', CONTENT_MODEL_WIKITEXT ],
118 [ 'User:Foo', CONTENT_MODEL_WIKITEXT ],
119 [ 'User:Foo.js', CONTENT_MODEL_WIKITEXT ],
120 [ 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT ],
121 [ 'User:Foo/bar.css', CONTENT_MODEL_CSS ],
122 [ 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT ],
123 [ 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT ],
124 [ 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT ],
125 [ 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT ],
126 [ 'MediaWiki:Foo.css', CONTENT_MODEL_CSS ],
127 [ 'MediaWiki:Foo/bar.css', CONTENT_MODEL_CSS ],
128 [ 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT ],
129 [ 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT ],
130 [ 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT ],
131 [ 'TEST-JS:Foo', CONTENT_MODEL_JAVASCRIPT ],
132 [ 'TEST-JS:Foo.js', CONTENT_MODEL_JAVASCRIPT ],
133 [ 'TEST-JS:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT ],
134 [ 'TEST-JS_TALK:Foo.js', CONTENT_MODEL_WIKITEXT ],
135 ];
136 }
137
142 public function testGetContentModel( $title, $expectedModelId ) {
143 $title = Title::newFromText( $title );
144 $this->assertEquals( $expectedModelId, $title->getContentModel() );
145 }
146
151 public function testHasContentModel( $title, $expectedModelId ) {
152 $title = Title::newFromText( $title );
153 $this->assertTrue( $title->hasContentModel( $expectedModelId ) );
154 }
155
156 public static function provideIsSiteConfigPage() {
157 return [
158 [ 'Help:Foo', false ],
159 [ 'Help:Foo.js', false ],
160 [ 'Help:Foo/bar.js', false ],
161 [ 'User:Foo', false ],
162 [ 'User:Foo.js', false ],
163 [ 'User:Foo/bar.js', false ],
164 [ 'User:Foo/bar.json', false ],
165 [ 'User:Foo/bar.css', false ],
166 [ 'User:Foo/bar.JS', false ],
167 [ 'User:Foo/bar.JSON', false ],
168 [ 'User:Foo/bar.CSS', false ],
169 [ 'User talk:Foo/bar.css', false ],
170 [ 'User:Foo/bar.js.xxx', false ],
171 [ 'User:Foo/bar.xxx', false ],
172 [ 'MediaWiki:Foo.js', true ],
173 [ 'MediaWiki:Foo.json', true ],
174 [ 'MediaWiki:Foo.css', true ],
175 [ 'MediaWiki:Foo.JS', false ],
176 [ 'MediaWiki:Foo.JSON', false ],
177 [ 'MediaWiki:Foo.CSS', false ],
178 [ 'MediaWiki:Foo/bar.css', true ],
179 [ 'MediaWiki:Foo.css.xxx', false ],
180 [ 'TEST-JS:Foo', false ],
181 [ 'TEST-JS:Foo.js', false ],
182 ];
183 }
184
189 public function testSiteConfigPage( $title, $expectedBool ) {
190 $title = Title::newFromText( $title );
191 $this->assertEquals( $expectedBool, $title->isSiteConfigPage() );
192 }
193
194 public static function provideIsUserConfigPage() {
195 return [
196 [ 'Help:Foo', false ],
197 [ 'Help:Foo.js', false ],
198 [ 'Help:Foo/bar.js', false ],
199 [ 'User:Foo', false ],
200 [ 'User:Foo.js', false ],
201 [ 'User:Foo/bar.js', true ],
202 [ 'User:Foo/bar.JS', false ],
203 [ 'User:Foo/bar.json', true ],
204 [ 'User:Foo/bar.JSON', false ],
205 [ 'User:Foo/bar.css', true ],
206 [ 'User:Foo/bar.CSS', false ],
207 [ 'User talk:Foo/bar.css', false ],
208 [ 'User:Foo/bar.js.xxx', false ],
209 [ 'User:Foo/bar.xxx', false ],
210 [ 'MediaWiki:Foo.js', false ],
211 [ 'MediaWiki:Foo.json', false ],
212 [ 'MediaWiki:Foo.css', false ],
213 [ 'MediaWiki:Foo.JS', false ],
214 [ 'MediaWiki:Foo.JSON', false ],
215 [ 'MediaWiki:Foo.CSS', false ],
216 [ 'MediaWiki:Foo.css.xxx', false ],
217 [ 'TEST-JS:Foo', false ],
218 [ 'TEST-JS:Foo.js', false ],
219 ];
220 }
221
226 public function testIsUserConfigPage( $title, $expectedBool ) {
227 $title = Title::newFromText( $title );
228 $this->assertEquals( $expectedBool, $title->isUserConfigPage() );
229 }
230
231 public static function provideIsUserCssConfigPage() {
232 return [
233 [ 'Help:Foo', false ],
234 [ 'Help:Foo.css', false ],
235 [ 'User:Foo', false ],
236 [ 'User:Foo.js', false ],
237 [ 'User:Foo.json', false ],
238 [ 'User:Foo.css', false ],
239 [ 'User:Foo/bar.js', false ],
240 [ 'User:Foo/bar.json', false ],
241 [ 'User:Foo/bar.css', true ],
242 ];
243 }
244
249 public function testIsUserCssConfigPage( $title, $expectedBool ) {
250 $title = Title::newFromText( $title );
251 $this->assertEquals( $expectedBool, $title->isUserCssConfigPage() );
252 }
253
254 public static function provideIsUserJsConfigPage() {
255 return [
256 [ 'Help:Foo', false ],
257 [ 'Help:Foo.css', false ],
258 [ 'User:Foo', false ],
259 [ 'User:Foo.js', false ],
260 [ 'User:Foo.json', false ],
261 [ 'User:Foo.css', false ],
262 [ 'User:Foo/bar.js', true ],
263 [ 'User:Foo/bar.json', false ],
264 [ 'User:Foo/bar.css', false ],
265 ];
266 }
267
272 public function testIsUserJsConfigPage( $title, $expectedBool ) {
273 $title = Title::newFromText( $title );
274 $this->assertEquals( $expectedBool, $title->isUserJsConfigPage() );
275 }
276
277 public static function provideIsWikitextPage() {
278 return [
279 [ 'Help:Foo', true ],
280 [ 'Help:Foo.js', true ],
281 [ 'Help:Foo/bar.js', true ],
282 [ 'User:Foo', true ],
283 [ 'User:Foo.js', true ],
284 [ 'User:Foo/bar.js', false ],
285 [ 'User:Foo/bar.json', false ],
286 [ 'User:Foo/bar.css', false ],
287 [ 'User talk:Foo/bar.css', true ],
288 [ 'User:Foo/bar.js.xxx', true ],
289 [ 'User:Foo/bar.xxx', true ],
290 [ 'MediaWiki:Foo.js', false ],
291 [ 'User:Foo/bar.JS', true ],
292 [ 'User:Foo/bar.JSON', true ],
293 [ 'User:Foo/bar.CSS', true ],
294 [ 'MediaWiki:Foo.json', false ],
295 [ 'MediaWiki:Foo.css', false ],
296 [ 'MediaWiki:Foo.JS', true ],
297 [ 'MediaWiki:Foo.JSON', true ],
298 [ 'MediaWiki:Foo.CSS', true ],
299 [ 'MediaWiki:Foo.css.xxx', true ],
300 [ 'TEST-JS:Foo', false ],
301 [ 'TEST-JS:Foo.js', false ],
302 ];
303 }
304
309 public function testIsWikitextPage( $title, $expectedBool ) {
310 $title = Title::newFromText( $title );
311 $this->assertEquals( $expectedBool, $title->isWikitextPage() );
312 }
313
314 public static function provideGetOtherPage() {
315 return [
316 [ 'Main Page', 'Talk:Main Page' ],
317 [ 'Talk:Main Page', 'Main Page' ],
318 [ 'Help:Main Page', 'Help talk:Main Page' ],
319 [ 'Help talk:Main Page', 'Help:Main Page' ],
320 [ 'Special:FooBar', null ],
321 [ 'Media:File.jpg', null ],
322 ];
323 }
324
332 public function testGetOtherPage( $text, $expected ) {
333 if ( $expected === null ) {
334 $this->setExpectedException( MWException::class );
335 }
336
337 $title = Title::newFromText( $text );
338 $this->assertEquals( $expected, $title->getOtherPage()->getPrefixedText() );
339 }
340
344 public function testClearCaches() {
345 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
346
347 $title1 = Title::newFromText( 'Foo' );
348 $linkCache->addGoodLinkObj( 23, $title1 );
349
350 Title::clearCaches();
351
352 $title2 = Title::newFromText( 'Foo' );
353 $this->assertNotSame( $title1, $title2, 'title cache should be empty' );
354 $this->assertEquals( 0, $linkCache->getGoodLinkID( 'Foo' ), 'link cache should be empty' );
355 }
356
357 public function provideGetLinkURL() {
358 yield 'Simple' => [
359 '/wiki/Goats',
360 NS_MAIN,
361 'Goats'
362 ];
363
364 yield 'Fragment' => [
365 '/wiki/Goats#Goatificatiön',
366 NS_MAIN,
367 'Goats',
368 'Goatificatiön'
369 ];
370
371 yield 'Unknown interwiki with fragment' => [
372 'https://xx.wiki.test/wiki/xyzzy:Goats#Goatificatiön',
373 NS_MAIN,
374 'Goats',
375 'Goatificatiön',
376 'xyzzy'
377 ];
378
379 yield 'Interwiki with fragment' => [
380 'https://acme.test/Goats#Goatificati.C3.B6n',
381 NS_MAIN,
382 'Goats',
383 'Goatificatiön',
384 'acme'
385 ];
386
387 yield 'Interwiki with query' => [
388 'https://acme.test/Goats?a=1&b=blank+blank#Goatificati.C3.B6n',
389 NS_MAIN,
390 'Goats',
391 'Goatificatiön',
392 'acme',
393 [
394 'a' => 1,
395 'b' => 'blank blank'
396 ]
397 ];
398
399 yield 'Local interwiki with fragment' => [
400 'https://yy.wiki.test/wiki/Goats#Goatificatiön',
401 NS_MAIN,
402 'Goats',
403 'Goatificatiön',
404 'yy'
405 ];
406 }
407
416 public function testGetLinkURL(
417 $expected,
418 $ns,
419 $title,
420 $fragment = '',
421 $interwiki = '',
422 $query = '',
423 $query2 = false,
424 $proto = false
425 ) {
426 $this->setMwGlobals( [
427 'wgServer' => 'https://xx.wiki.test',
428 'wgArticlePath' => '/wiki/$1',
429 'wgExternalInterwikiFragmentMode' => 'legacy',
430 'wgFragmentMode' => [ 'html5', 'legacy' ]
431 ] );
432
433 $interwikiLookup = $this->getMock( InterwikiLookup::class );
434
435 $interwikiLookup->method( 'fetch' )
436 ->willReturnCallback( function ( $interwiki ) {
437 switch ( $interwiki ) {
438 case '':
439 return null;
440 case 'acme':
441 return new Interwiki(
442 'acme',
443 'https://acme.test/$1'
444 );
445 case 'yy':
446 return new Interwiki(
447 'yy',
448 'https://yy.wiki.test/wiki/$1',
449 '/w/api.php',
450 'yywiki',
451 true
452 );
453 default:
454 return false;
455 }
456 } );
457
458 $this->setService( 'InterwikiLookup', $interwikiLookup );
459
460 $title = Title::makeTitle( $ns, $title, $fragment, $interwiki );
461 $this->assertSame( $expected, $title->getLinkURL( $query, $query2, $proto ) );
462 }
463
464 function tearDown() {
465 Title::clearCaches();
466 parent::tearDown();
467 }
468}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Value object for representing interwiki records.
Definition Interwiki.php:27
Base class that store and restore the Language objects.
mergeMwGlobalArrayValue( $name, $values)
Merges the given values into a MW global array variable.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
setService( $name, $object)
Sets a service, maintaining a stashed version of the previous service to be restored in tearDown.
MediaWikiServices is the service locator for the application scope of MediaWiki.
ContentHandler Database.
testEquals( $titleA, $titleB, $expectedBool)
provideEquals Title::equals
testGetContentModel( $title, $expectedModelId)
dataGetContentModel Title::getContentModel
static provideIsUserJsConfigPage()
static provideHasSubjectNamespace()
testIsUserJsConfigPage( $title, $expectedBool)
provideIsUserJsConfigPage Title::isUserJsConfigPage
testGetLinkURL( $expected, $ns, $title, $fragment='', $interwiki='', $query='', $query2=false, $proto=false)
provideGetLinkURL
testHasContentModel( $title, $expectedModelId)
dataGetContentModel Title::hasContentModel
static provideIsSiteConfigPage()
testIsWikitextPage( $title, $expectedBool)
provideIsWikitextPage Title::isWikitextPage
testSiteConfigPage( $title, $expectedBool)
provideIsSiteConfigPage Title::isSiteConfigPage
testClearCaches()
Title::clearCaches.
static provideIsUserCssConfigPage()
testIsUserConfigPage( $title, $expectedBool)
provideIsUserConfigPage Title::isUserConfigPage
testIsUserCssConfigPage( $title, $expectedBool)
provideIsUserCssConfigPage Title::isUserCssConfigPage
testInNamespaces()
Title::inNamespaces.
static provideIsUserConfigPage()
testHasSubjectNamespace( $title, $ns, $expectedBool)
provideHasSubjectNamespace Title::hasSubjectNamespace
testGetOtherPage( $text, $expected)
provideGetOtherpage Title::getOtherPage
testInNamespace( $title, $ns, $expectedBool)
provideInNamespace Title::inNamespace
const NS_USER
Definition Defines.php:66
const CONTENT_MODEL_CSS
Definition Defines.php:237
const NS_MAIN
Definition Defines.php:64
const NS_TEMPLATE
Definition Defines.php:74
const CONTENT_MODEL_WIKITEXT
Definition Defines.php:235
const NS_TALK
Definition Defines.php:65
const NS_USER_TALK
Definition Defines.php:67
const NS_PROJECT
Definition Defines.php:68
const CONTENT_MODEL_JAVASCRIPT
Definition Defines.php:236
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:994
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:2055
null for the local wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition hooks.txt:1656
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
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
Service interface for looking up Interwiki records.