Merge "build: Don't install symfony/polyfill-* that we require"
[lhc/web/wiklou.git] / tests / phpunit / includes / TitleMethodsTest.php
1 <?php
2
3 use MediaWiki\Interwiki\InterwikiLookup;
4 use MediaWiki\MediaWikiServices;
5
6 /**
7 * @group ContentHandler
8 * @group Database
9 *
10 * @note We don't make assumptions about the main namespace.
11 * But we do expect the Help namespace to contain Wikitext.
12 */
13 class TitleMethodsTest extends MediaWikiLangTestCase {
14
15 protected function setUp() {
16 parent::setUp();
17
18 $this->mergeMwGlobalArrayValue(
19 'wgExtraNamespaces',
20 [
21 12302 => 'TEST-JS',
22 12303 => 'TEST-JS_TALK',
23 ]
24 );
25
26 $this->mergeMwGlobalArrayValue(
27 'wgNamespaceContentModels',
28 [
29 12302 => CONTENT_MODEL_JAVASCRIPT,
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
46 /**
47 * @dataProvider provideEquals
48 * @covers Title::equals
49 */
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
71 /**
72 * @dataProvider provideInNamespace
73 * @covers Title::inNamespace
74 */
75 public function testInNamespace( $title, $ns, $expectedBool ) {
76 $title = Title::newFromText( $title );
77 $this->assertEquals( $expectedBool, $title->inNamespace( $ns ) );
78 }
79
80 /**
81 * @covers Title::inNamespaces
82 */
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
104 /**
105 * @dataProvider provideHasSubjectNamespace
106 * @covers Title::hasSubjectNamespace
107 */
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
138 /**
139 * @dataProvider dataGetContentModel
140 * @covers Title::getContentModel
141 */
142 public function testGetContentModel( $title, $expectedModelId ) {
143 $title = Title::newFromText( $title );
144 $this->assertEquals( $expectedModelId, $title->getContentModel() );
145 }
146
147 /**
148 * @dataProvider dataGetContentModel
149 * @covers Title::hasContentModel
150 */
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
185 /**
186 * @dataProvider provideIsSiteConfigPage
187 * @covers Title::isSiteConfigPage
188 */
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
222 /**
223 * @dataProvider provideIsUserConfigPage
224 * @covers Title::isUserConfigPage
225 */
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
245 /**
246 * @dataProvider provideIsUserCssConfigPage
247 * @covers Title::isUserCssConfigPage
248 */
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
268 /**
269 * @dataProvider provideIsUserJsConfigPage
270 * @covers Title::isUserJsConfigPage
271 */
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
305 /**
306 * @dataProvider provideIsWikitextPage
307 * @covers Title::isWikitextPage
308 */
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
325 /**
326 * @dataProvider provideGetOtherpage
327 * @covers Title::getOtherPage
328 *
329 * @param string $text
330 * @param string|null $expected
331 */
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
341 /**
342 * @covers Title::clearCaches
343 */
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
408 /**
409 * @dataProvider provideGetLinkURL
410 *
411 * @covers Title::getLinkURL
412 * @covers Title::getFullURL
413 * @covers Title::getLocalURL
414 * @covers Title::getFragmentForURL
415 */
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 }