Merge "Update MediaWikiTitleCodec to use NamespaceInfo"
[lhc/web/wiklou.git] / tests / phpunit / includes / TitleTest.php
1 <?php
2
3 use MediaWiki\Linker\LinkTarget;
4 use MediaWiki\MediaWikiServices;
5 use Wikimedia\TestingAccessWrapper;
6
7 /**
8 * @group Database
9 * @group Title
10 */
11 class TitleTest extends MediaWikiTestCase {
12 protected function setUp() {
13 parent::setUp();
14
15 $this->setMwGlobals( [
16 'wgAllowUserJs' => false,
17 'wgDefaultLanguageVariant' => false,
18 'wgMetaNamespace' => 'Project',
19 ] );
20 $this->setUserLang( 'en' );
21 $this->setContentLang( 'en' );
22 }
23
24 protected function tearDown() {
25 // For testNewMainPage
26 MessageCache::destroyInstance();
27 parent::tearDown();
28 }
29
30 /**
31 * @covers Title::legalChars
32 */
33 public function testLegalChars() {
34 $titlechars = Title::legalChars();
35
36 foreach ( range( 1, 255 ) as $num ) {
37 $chr = chr( $num );
38 if ( strpos( "#[]{}<>|", $chr ) !== false || preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
39 $this->assertFalse(
40 (bool)preg_match( "/[$titlechars]/", $chr ),
41 "chr($num) = $chr is not a valid titlechar"
42 );
43 } else {
44 $this->assertTrue(
45 (bool)preg_match( "/[$titlechars]/", $chr ),
46 "chr($num) = $chr is a valid titlechar"
47 );
48 }
49 }
50 }
51
52 public static function provideValidSecureAndSplit() {
53 return [
54 [ 'Sandbox' ],
55 [ 'A "B"' ],
56 [ 'A \'B\'' ],
57 [ '.com' ],
58 [ '~' ],
59 [ '#' ],
60 [ '"' ],
61 [ '\'' ],
62 [ 'Talk:Sandbox' ],
63 [ 'Talk:Foo:Sandbox' ],
64 [ 'File:Example.svg' ],
65 [ 'File_talk:Example.svg' ],
66 [ 'Foo/.../Sandbox' ],
67 [ 'Sandbox/...' ],
68 [ 'A~~' ],
69 [ ':A' ],
70 // Length is 256 total, but only title part matters
71 [ 'Category:' . str_repeat( 'x', 248 ) ],
72 [ str_repeat( 'x', 252 ) ],
73 // interwiki prefix
74 [ 'localtestiw: #anchor' ],
75 [ 'localtestiw:' ],
76 [ 'localtestiw:foo' ],
77 [ 'localtestiw: foo # anchor' ],
78 [ 'localtestiw: Talk: Sandbox # anchor' ],
79 [ 'remotetestiw:' ],
80 [ 'remotetestiw: Talk: # anchor' ],
81 [ 'remotetestiw: #bar' ],
82 [ 'remotetestiw: Talk:' ],
83 [ 'remotetestiw: Talk: Foo' ],
84 [ 'localtestiw:remotetestiw:' ],
85 [ 'localtestiw:remotetestiw:foo' ]
86 ];
87 }
88
89 public static function provideInvalidSecureAndSplit() {
90 return [
91 [ '', 'title-invalid-empty' ],
92 [ ':', 'title-invalid-empty' ],
93 [ '__ __', 'title-invalid-empty' ],
94 [ ' __ ', 'title-invalid-empty' ],
95 // Bad characters forbidden regardless of wgLegalTitleChars
96 [ 'A [ B', 'title-invalid-characters' ],
97 [ 'A ] B', 'title-invalid-characters' ],
98 [ 'A { B', 'title-invalid-characters' ],
99 [ 'A } B', 'title-invalid-characters' ],
100 [ 'A < B', 'title-invalid-characters' ],
101 [ 'A > B', 'title-invalid-characters' ],
102 [ 'A | B', 'title-invalid-characters' ],
103 [ "A \t B", 'title-invalid-characters' ],
104 [ "A \n B", 'title-invalid-characters' ],
105 // URL encoding
106 [ 'A%20B', 'title-invalid-characters' ],
107 [ 'A%23B', 'title-invalid-characters' ],
108 [ 'A%2523B', 'title-invalid-characters' ],
109 // XML/HTML character entity references
110 // Note: Commented out because they are not marked invalid by the PHP test as
111 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
112 // 'A &eacute; B',
113 // 'A &#233; B',
114 // 'A &#x00E9; B',
115 // Subject of NS_TALK does not roundtrip to NS_MAIN
116 [ 'Talk:File:Example.svg', 'title-invalid-talk-namespace' ],
117 // Directory navigation
118 [ '.', 'title-invalid-relative' ],
119 [ '..', 'title-invalid-relative' ],
120 [ './Sandbox', 'title-invalid-relative' ],
121 [ '../Sandbox', 'title-invalid-relative' ],
122 [ 'Foo/./Sandbox', 'title-invalid-relative' ],
123 [ 'Foo/../Sandbox', 'title-invalid-relative' ],
124 [ 'Sandbox/.', 'title-invalid-relative' ],
125 [ 'Sandbox/..', 'title-invalid-relative' ],
126 // Tilde
127 [ 'A ~~~ Name', 'title-invalid-magic-tilde' ],
128 [ 'A ~~~~ Signature', 'title-invalid-magic-tilde' ],
129 [ 'A ~~~~~ Timestamp', 'title-invalid-magic-tilde' ],
130 // Length
131 [ str_repeat( 'x', 256 ), 'title-invalid-too-long' ],
132 // Namespace prefix without actual title
133 [ 'Talk:', 'title-invalid-empty' ],
134 [ 'Talk:#', 'title-invalid-empty' ],
135 [ 'Category: ', 'title-invalid-empty' ],
136 [ 'Category: #bar', 'title-invalid-empty' ],
137 // interwiki prefix
138 [ 'localtestiw: Talk: # anchor', 'title-invalid-empty' ],
139 [ 'localtestiw: Talk:', 'title-invalid-empty' ]
140 ];
141 }
142
143 private function secureAndSplitGlobals() {
144 $this->setMwGlobals( [
145 'wgLocalInterwikis' => [ 'localtestiw' ],
146 'wgHooks' => [
147 'InterwikiLoadPrefix' => [
148 function ( $prefix, &$data ) {
149 if ( $prefix === 'localtestiw' ) {
150 $data = [ 'iw_url' => 'localtestiw' ];
151 } elseif ( $prefix === 'remotetestiw' ) {
152 $data = [ 'iw_url' => 'remotetestiw' ];
153 }
154 return false;
155 }
156 ]
157 ]
158 ] );
159
160 $this->overrideMwServices();
161 }
162
163 /**
164 * See also mediawiki.Title.test.js
165 * @covers Title::secureAndSplit
166 * @dataProvider provideValidSecureAndSplit
167 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
168 */
169 public function testSecureAndSplitValid( $text ) {
170 $this->secureAndSplitGlobals();
171 $this->assertInstanceOf( Title::class, Title::newFromText( $text ), "Valid: $text" );
172 }
173
174 /**
175 * See also mediawiki.Title.test.js
176 * @covers Title::secureAndSplit
177 * @dataProvider provideInvalidSecureAndSplit
178 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
179 */
180 public function testSecureAndSplitInvalid( $text, $expectedErrorMessage ) {
181 $this->secureAndSplitGlobals();
182 try {
183 Title::newFromTextThrow( $text ); // should throw
184 $this->assertTrue( false, "Invalid: $text" );
185 } catch ( MalformedTitleException $ex ) {
186 $this->assertEquals( $expectedErrorMessage, $ex->getErrorMessage(), "Invalid: $text" );
187 }
188 }
189
190 public static function provideConvertByteClassToUnicodeClass() {
191 return [
192 [
193 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
194 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
195 ],
196 [
197 'QWERTYf-\\xFF+',
198 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
199 ],
200 [
201 'QWERTY\\x66-\\xFD+',
202 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
203 ],
204 [
205 'QWERTYf-y+',
206 'QWERTYf-y+',
207 ],
208 [
209 'QWERTYf-\\x80+',
210 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
211 ],
212 [
213 'QWERTY\\x66-\\x80+\\x23',
214 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
215 ],
216 [
217 'QWERTY\\x66-\\x80+\\xD3',
218 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
219 ],
220 [
221 '\\\\\\x99',
222 '\\\\\\u0080-\\uFFFF',
223 ],
224 [
225 '-\\x99',
226 '\\-\\u0080-\\uFFFF',
227 ],
228 [
229 'QWERTY\\-\\x99',
230 'QWERTY\\-\\u0080-\\uFFFF',
231 ],
232 [
233 '\\\\x99',
234 '\\\\x99',
235 ],
236 [
237 'A-\\x9F',
238 'A-\\x7F\\u0080-\\uFFFF',
239 ],
240 [
241 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
242 'f-wQWERTYFXZ\\u0080-\\uFFFF',
243 ],
244 [
245 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
246 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
247 ],
248 ];
249 }
250
251 /**
252 * @dataProvider provideConvertByteClassToUnicodeClass
253 * @covers Title::convertByteClassToUnicodeClass
254 */
255 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
256 $this->assertEquals( $unicodeClass, Title::convertByteClassToUnicodeClass( $byteClass ) );
257 }
258
259 /**
260 * @dataProvider provideSpecialNamesWithAndWithoutParameter
261 * @covers Title::fixSpecialName
262 */
263 public function testFixSpecialNameRetainsParameter( $text, $expectedParam ) {
264 $title = Title::newFromText( $text );
265 $fixed = $title->fixSpecialName();
266 $stuff = explode( '/', $fixed->getDBkey(), 2 );
267 if ( count( $stuff ) == 2 ) {
268 $par = $stuff[1];
269 } else {
270 $par = null;
271 }
272 $this->assertEquals(
273 $expectedParam,
274 $par,
275 "T33100 regression check: Title->fixSpecialName() should preserve parameter"
276 );
277 }
278
279 public static function provideSpecialNamesWithAndWithoutParameter() {
280 return [
281 [ 'Special:Version', null ],
282 [ 'Special:Version/', '' ],
283 [ 'Special:Version/param', 'param' ],
284 ];
285 }
286
287 /**
288 * Auth-less test of Title::isValidMoveOperation
289 *
290 * @param string $source
291 * @param string $target
292 * @param array|string|bool $expected Required error
293 * @dataProvider provideTestIsValidMoveOperation
294 * @covers Title::isValidMoveOperation
295 */
296 public function testIsValidMoveOperation( $source, $target, $expected ) {
297 $this->setMwGlobals( 'wgContentHandlerUseDB', false );
298 $title = Title::newFromText( $source );
299 $nt = Title::newFromText( $target );
300 $errors = $title->isValidMoveOperation( $nt, false );
301 if ( $expected === true ) {
302 $this->assertTrue( $errors );
303 } else {
304 $errors = $this->flattenErrorsArray( $errors );
305 foreach ( (array)$expected as $error ) {
306 $this->assertContains( $error, $errors );
307 }
308 }
309 }
310
311 public static function provideTestIsValidMoveOperation() {
312 return [
313 // for Title::isValidMoveOperation
314 [ 'Some page', '', 'badtitletext' ],
315 [ 'Test', 'Test', 'selfmove' ],
316 [ 'Special:FooBar', 'Test', 'immobile-source-namespace' ],
317 [ 'Test', 'Special:FooBar', 'immobile-target-namespace' ],
318 [ 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ],
319 [ 'Page', 'File:Test.jpg', 'nonfile-cannot-move-to-file' ],
320 [ 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ],
321 ];
322 }
323
324 /**
325 * Auth-less test of Title::userCan
326 *
327 * @param array $whitelistRegexp
328 * @param string $source
329 * @param string $action
330 * @param array|string|bool $expected Required error
331 *
332 * @covers \Mediawiki\Permissions\PermissionManager::checkReadPermissions
333 * @dataProvider dataWgWhitelistReadRegexp
334 */
335 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
336 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
337 // usually have only one regex, it is more concise to write the lonely regex
338 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
339 // type requisite.
340 if ( is_string( $whitelistRegexp ) ) {
341 $whitelistRegexp = [ $whitelistRegexp ];
342 }
343
344 $this->setMwGlobals( [
345 // So User::isEveryoneAllowed( 'read' ) === false
346 'wgGroupPermissions' => [ '*' => [ 'read' => false ] ],
347 'wgWhitelistRead' => [ 'some random non sense title' ],
348 'wgWhitelistReadRegexp' => $whitelistRegexp,
349 ] );
350
351 $title = Title::newFromDBkey( $source );
352
353 // New anonymous user with no rights
354 $user = new User;
355 $user->mRights = [];
356 $errors = $title->userCan( $action, $user );
357
358 if ( is_bool( $expected ) ) {
359 # Forge the assertion message depending on the assertion expectation
360 $allowableness = $expected
361 ? " should be allowed"
362 : " should NOT be allowed";
363 $this->assertEquals(
364 $expected,
365 $errors,
366 "User action '$action' on [[$source]] $allowableness."
367 );
368 } else {
369 $errors = $this->flattenErrorsArray( $errors );
370 foreach ( (array)$expected as $error ) {
371 $this->assertContains( $error, $errors );
372 }
373 }
374 }
375
376 /**
377 * Provides test parameter values for testWgWhitelistReadRegexp()
378 */
379 public function dataWgWhitelistReadRegexp() {
380 $ALLOWED = true;
381 $DISALLOWED = false;
382
383 return [
384 // Everything, if this doesn't work, we're really in trouble
385 [ '/.*/', 'Main_Page', 'read', $ALLOWED ],
386 [ '/.*/', 'Main_Page', 'edit', $DISALLOWED ],
387
388 // We validate against the title name, not the db key
389 [ '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ],
390 // Main page
391 [ '/^Main/', 'Main_Page', 'read', $ALLOWED ],
392 [ '/^Main.*/', 'Main_Page', 'read', $ALLOWED ],
393 // With spaces
394 [ '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ],
395 // Unicode multibyte
396 // ...without unicode modifier
397 [ '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ],
398 // ...with unicode modifier
399 [ '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ],
400 // Case insensitive
401 [ '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ],
402 [ '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ],
403
404 // From DefaultSettings.php:
405 [ "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ],
406 [ "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ],
407
408 // With namespaces:
409 [ '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ],
410 [ null, 'Special:Newpages', 'read', $DISALLOWED ],
411
412 ];
413 }
414
415 public function flattenErrorsArray( $errors ) {
416 $result = [];
417 foreach ( $errors as $error ) {
418 $result[] = $error[0];
419 }
420
421 return $result;
422 }
423
424 /**
425 * @dataProvider provideGetPageViewLanguage
426 * @covers Title::getPageViewLanguage
427 */
428 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
429 $lang, $variant, $msg = ''
430 ) {
431 // Setup environnement for this test
432 $this->setMwGlobals( [
433 'wgDefaultLanguageVariant' => $variant,
434 'wgAllowUserJs' => true,
435 ] );
436 $this->setUserLang( $lang );
437 $this->setContentLang( $contLang );
438
439 $title = Title::newFromText( $titleText );
440 $this->assertInstanceOf( Title::class, $title,
441 "Test must be passed a valid title text, you gave '$titleText'"
442 );
443 $this->assertEquals( $expected,
444 $title->getPageViewLanguage()->getCode(),
445 $msg
446 );
447 }
448
449 public static function provideGetPageViewLanguage() {
450 # Format:
451 # - expected
452 # - Title name
453 # - content language (expected in most cases)
454 # - wgLang (on some specific pages)
455 # - wgDefaultLanguageVariant
456 # - Optional message
457 return [
458 [ 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ],
459 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ],
460 [ 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ],
461
462 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ],
463 [ 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ],
464 [ 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ],
465 [ 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ],
466 [ 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ],
467 [ 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ],
468 [ 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ],
469 [ 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ],
470
471 [ 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ],
472 [ 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ],
473 [ 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ],
474 [ 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ],
475 [ 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ],
476 [ 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ],
477 [ 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ],
478 [ 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ],
479 [ 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ],
480 [ 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ],
481
482 [ 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ],
483 [ 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ],
484
485 ];
486 }
487
488 /**
489 * @dataProvider provideBaseTitleCases
490 * @covers Title::getBaseText
491 */
492 public function testGetBaseText( $title, $expected, $msg = '' ) {
493 $title = Title::newFromText( $title );
494 $this->assertEquals( $expected,
495 $title->getBaseText(),
496 $msg
497 );
498 }
499
500 public static function provideBaseTitleCases() {
501 return [
502 # Title, expected base, optional message
503 [ 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ],
504 [ 'User:Foo/Bar/Baz', 'Foo/Bar' ],
505 ];
506 }
507
508 /**
509 * @dataProvider provideRootTitleCases
510 * @covers Title::getRootText
511 */
512 public function testGetRootText( $title, $expected, $msg = '' ) {
513 $title = Title::newFromText( $title );
514 $this->assertEquals( $expected,
515 $title->getRootText(),
516 $msg
517 );
518 }
519
520 public static function provideRootTitleCases() {
521 return [
522 # Title, expected base, optional message
523 [ 'User:John_Doe/subOne/subTwo', 'John Doe' ],
524 [ 'User:Foo/Bar/Baz', 'Foo' ],
525 ];
526 }
527
528 /**
529 * @todo Handle $wgNamespacesWithSubpages cases
530 * @dataProvider provideSubpageTitleCases
531 * @covers Title::getSubpageText
532 */
533 public function testGetSubpageText( $title, $expected, $msg = '' ) {
534 $title = Title::newFromText( $title );
535 $this->assertEquals( $expected,
536 $title->getSubpageText(),
537 $msg
538 );
539 }
540
541 public static function provideSubpageTitleCases() {
542 return [
543 # Title, expected base, optional message
544 [ 'User:John_Doe/subOne/subTwo', 'subTwo' ],
545 [ 'User:John_Doe/subOne', 'subOne' ],
546 ];
547 }
548
549 public static function provideNewFromTitleValue() {
550 return [
551 [ new TitleValue( NS_MAIN, 'Foo' ) ],
552 [ new TitleValue( NS_MAIN, 'Foo', 'bar' ) ],
553 [ new TitleValue( NS_USER, 'Hansi_Maier' ) ],
554 ];
555 }
556
557 /**
558 * @covers Title::newFromTitleValue
559 * @dataProvider provideNewFromTitleValue
560 */
561 public function testNewFromTitleValue( TitleValue $value ) {
562 $title = Title::newFromTitleValue( $value );
563
564 $dbkey = str_replace( ' ', '_', $value->getText() );
565 $this->assertEquals( $dbkey, $title->getDBkey() );
566 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
567 $this->assertEquals( $value->getFragment(), $title->getFragment() );
568 }
569
570 /**
571 * @covers Title::newFromLinkTarget
572 * @dataProvider provideNewFromTitleValue
573 */
574 public function testNewFromLinkTarget( LinkTarget $value ) {
575 $title = Title::newFromLinkTarget( $value );
576
577 $dbkey = str_replace( ' ', '_', $value->getText() );
578 $this->assertEquals( $dbkey, $title->getDBkey() );
579 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
580 $this->assertEquals( $value->getFragment(), $title->getFragment() );
581 }
582
583 /**
584 * @covers Title::newFromLinkTarget
585 */
586 public function testNewFromLinkTarget_clone() {
587 $title = Title::newFromText( __METHOD__ );
588 $this->assertSame( $title, Title::newFromLinkTarget( $title ) );
589
590 // The Title::NEW_CLONE flag should ensure that a fresh instance is returned.
591 $clone = Title::newFromLinkTarget( $title, Title::NEW_CLONE );
592 $this->assertNotSame( $title, $clone );
593 $this->assertTrue( $clone->equals( $title ) );
594 }
595
596 public static function provideGetTitleValue() {
597 return [
598 [ 'Foo' ],
599 [ 'Foo#bar' ],
600 [ 'User:Hansi_Maier' ],
601 ];
602 }
603
604 /**
605 * @covers Title::getTitleValue
606 * @dataProvider provideGetTitleValue
607 */
608 public function testGetTitleValue( $text ) {
609 $title = Title::newFromText( $text );
610 $value = $title->getTitleValue();
611
612 $dbkey = str_replace( ' ', '_', $value->getText() );
613 $this->assertEquals( $title->getDBkey(), $dbkey );
614 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
615 $this->assertEquals( $title->getFragment(), $value->getFragment() );
616 }
617
618 public static function provideGetFragment() {
619 return [
620 [ 'Foo', '' ],
621 [ 'Foo#bar', 'bar' ],
622 [ 'Foo#bär', 'bär' ],
623
624 // Inner whitespace is normalized
625 [ 'Foo#bar_bar', 'bar bar' ],
626 [ 'Foo#bar bar', 'bar bar' ],
627 [ 'Foo#bar bar', 'bar bar' ],
628
629 // Leading whitespace is kept, trailing whitespace is trimmed.
630 // XXX: Is this really want we want?
631 [ 'Foo#_bar_bar_', ' bar bar' ],
632 [ 'Foo# bar bar ', ' bar bar' ],
633 ];
634 }
635
636 /**
637 * @covers Title::getFragment
638 * @dataProvider provideGetFragment
639 *
640 * @param string $full
641 * @param string $fragment
642 */
643 public function testGetFragment( $full, $fragment ) {
644 $title = Title::newFromText( $full );
645 $this->assertEquals( $fragment, $title->getFragment() );
646 }
647
648 /**
649 * @covers Title::isAlwaysKnown
650 * @dataProvider provideIsAlwaysKnown
651 * @param string $page
652 * @param bool $isKnown
653 */
654 public function testIsAlwaysKnown( $page, $isKnown ) {
655 $title = Title::newFromText( $page );
656 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
657 }
658
659 public static function provideIsAlwaysKnown() {
660 return [
661 [ 'Some nonexistent page', false ],
662 [ 'UTPage', false ],
663 [ '#test', true ],
664 [ 'Special:BlankPage', true ],
665 [ 'Special:SomeNonexistentSpecialPage', false ],
666 [ 'MediaWiki:Parentheses', true ],
667 [ 'MediaWiki:Some nonexistent message', false ],
668 ];
669 }
670
671 /**
672 * @covers Title::isValid
673 * @dataProvider provideIsValid
674 * @param Title $title
675 * @param bool $isValid
676 */
677 public function testIsValid( Title $title, $isValid ) {
678 $this->assertEquals( $isValid, $title->isValid(), $title->getPrefixedText() );
679 }
680
681 public static function provideIsValid() {
682 return [
683 [ Title::makeTitle( NS_MAIN, '' ), false ],
684 [ Title::makeTitle( NS_MAIN, '<>' ), false ],
685 [ Title::makeTitle( NS_MAIN, '|' ), false ],
686 [ Title::makeTitle( NS_MAIN, '#' ), false ],
687 [ Title::makeTitle( NS_MAIN, 'Test' ), true ],
688 [ Title::makeTitle( -33, 'Test' ), false ],
689 [ Title::makeTitle( 77663399, 'Test' ), false ],
690 ];
691 }
692
693 /**
694 * @covers Title::isAlwaysKnown
695 */
696 public function testIsAlwaysKnownOnInterwiki() {
697 $title = Title::makeTitle( NS_MAIN, 'Interwiki link', '', 'externalwiki' );
698 $this->assertTrue( $title->isAlwaysKnown() );
699 }
700
701 /**
702 * @covers Title::exists
703 */
704 public function testExists() {
705 $title = Title::makeTitle( NS_PROJECT, 'New page' );
706 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
707
708 $article = new Article( $title );
709 $page = $article->getPage();
710 $page->doEditContent( new WikitextContent( 'Some [[link]]' ), 'summary' );
711
712 // Tell Title it doesn't know whether it exists
713 $title->mArticleID = -1;
714
715 // Tell the link cache it doesn't exists when it really does
716 $linkCache->clearLink( $title );
717 $linkCache->addBadLinkObj( $title );
718
719 $this->assertEquals(
720 false,
721 $title->exists(),
722 'exists() should rely on link cache unless GAID_FOR_UPDATE is used'
723 );
724 $this->assertEquals(
725 true,
726 $title->exists( Title::GAID_FOR_UPDATE ),
727 'exists() should re-query database when GAID_FOR_UPDATE is used'
728 );
729 }
730
731 public function provideCanHaveTalkPage() {
732 return [
733 'User page has talk page' => [
734 Title::makeTitle( NS_USER, 'Jane' ), true
735 ],
736 'Talke page has talk page' => [
737 Title::makeTitle( NS_TALK, 'Foo' ), true
738 ],
739 'Special page cannot have talk page' => [
740 Title::makeTitle( NS_SPECIAL, 'Thing' ), false
741 ],
742 'Virtual namespace cannot have talk page' => [
743 Title::makeTitle( NS_MEDIA, 'Kitten.jpg' ), false
744 ],
745 ];
746 }
747
748 /**
749 * @dataProvider provideCanHaveTalkPage
750 * @covers Title::canHaveTalkPage
751 *
752 * @param Title $title
753 * @param bool $expected
754 */
755 public function testCanHaveTalkPage( Title $title, $expected ) {
756 $actual = $title->canHaveTalkPage();
757 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
758 }
759
760 public static function provideGetTalkPage_good() {
761 return [
762 [ Title::makeTitle( NS_MAIN, 'Test' ), Title::makeTitle( NS_TALK, 'Test' ) ],
763 [ Title::makeTitle( NS_TALK, 'Test' ), Title::makeTitle( NS_TALK, 'Test' ) ],
764 ];
765 }
766
767 /**
768 * @dataProvider provideGetTalkPage_good
769 * @covers Title::getTalkPage
770 */
771 public function testGetTalkPage_good( Title $title, Title $expected ) {
772 $talk = $title->getTalkPage();
773 $this->assertSame(
774 $expected->getPrefixedDBKey(),
775 $talk->getPrefixedDBKey(),
776 $title->getPrefixedDBKey()
777 );
778 }
779
780 /**
781 * @dataProvider provideGetTalkPage_good
782 * @covers Title::getTalkPageIfDefined
783 */
784 public function testGetTalkPageIfDefined_good( Title $title ) {
785 $talk = $title->getTalkPageIfDefined();
786 $this->assertInstanceOf(
787 Title::class,
788 $talk,
789 $title->getPrefixedDBKey()
790 );
791 }
792
793 public static function provideGetTalkPage_bad() {
794 return [
795 [ Title::makeTitle( NS_SPECIAL, 'Test' ) ],
796 [ Title::makeTitle( NS_MEDIA, 'Test' ) ],
797 ];
798 }
799
800 /**
801 * @dataProvider provideGetTalkPage_bad
802 * @covers Title::getTalkPageIfDefined
803 */
804 public function testGetTalkPageIfDefined_bad( Title $title ) {
805 $talk = $title->getTalkPageIfDefined();
806 $this->assertNull(
807 $talk,
808 $title->getPrefixedDBKey()
809 );
810 }
811
812 public function provideCreateFragmentTitle() {
813 return [
814 [ Title::makeTitle( NS_MAIN, 'Test' ), 'foo' ],
815 [ Title::makeTitle( NS_TALK, 'Test', 'foo' ), '' ],
816 [ Title::makeTitle( NS_CATEGORY, 'Test', 'foo' ), 'bar' ],
817 [ Title::makeTitle( NS_MAIN, 'Test1', '', 'interwiki' ), 'baz' ]
818 ];
819 }
820
821 /**
822 * @covers Title::createFragmentTarget
823 * @dataProvider provideCreateFragmentTitle
824 */
825 public function testCreateFragmentTitle( Title $title, $fragment ) {
826 $this->mergeMwGlobalArrayValue( 'wgHooks', [
827 'InterwikiLoadPrefix' => [
828 function ( $prefix, &$iwdata ) {
829 if ( $prefix === 'interwiki' ) {
830 $iwdata = [
831 'iw_url' => 'http://example.com/',
832 'iw_local' => 0,
833 'iw_trans' => 0,
834 ];
835 return false;
836 }
837 },
838 ],
839 ] );
840
841 $fragmentTitle = $title->createFragmentTarget( $fragment );
842
843 $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
844 $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
845 $this->assertEquals( $title->getInterwiki(), $fragmentTitle->getInterwiki() );
846 $this->assertEquals( $fragment, $fragmentTitle->getFragment() );
847 }
848
849 public function provideGetPrefixedText() {
850 return [
851 // ns = 0
852 [
853 Title::makeTitle( NS_MAIN, 'Foo bar' ),
854 'Foo bar'
855 ],
856 // ns = 2
857 [
858 Title::makeTitle( NS_USER, 'Foo bar' ),
859 'User:Foo bar'
860 ],
861 // ns = 3
862 [
863 Title::makeTitle( NS_USER_TALK, 'Foo bar' ),
864 'User talk:Foo bar'
865 ],
866 // fragment not included
867 [
868 Title::makeTitle( NS_MAIN, 'Foo bar', 'fragment' ),
869 'Foo bar'
870 ],
871 // ns = -2
872 [
873 Title::makeTitle( NS_MEDIA, 'Foo bar' ),
874 'Media:Foo bar'
875 ],
876 // non-existent namespace
877 [
878 Title::makeTitle( 100777, 'Foo bar' ),
879 'Special:Badtitle/NS100777:Foo bar'
880 ],
881 ];
882 }
883
884 /**
885 * @covers Title::getPrefixedText
886 * @dataProvider provideGetPrefixedText
887 */
888 public function testGetPrefixedText( Title $title, $expected ) {
889 $this->assertEquals( $expected, $title->getPrefixedText() );
890 }
891
892 public function provideGetPrefixedDBKey() {
893 return [
894 // ns = 0
895 [
896 Title::makeTitle( NS_MAIN, 'Foo_bar' ),
897 'Foo_bar'
898 ],
899 // ns = 2
900 [
901 Title::makeTitle( NS_USER, 'Foo_bar' ),
902 'User:Foo_bar'
903 ],
904 // ns = 3
905 [
906 Title::makeTitle( NS_USER_TALK, 'Foo_bar' ),
907 'User_talk:Foo_bar'
908 ],
909 // fragment not included
910 [
911 Title::makeTitle( NS_MAIN, 'Foo_bar', 'fragment' ),
912 'Foo_bar'
913 ],
914 // ns = -2
915 [
916 Title::makeTitle( NS_MEDIA, 'Foo_bar' ),
917 'Media:Foo_bar'
918 ],
919 // non-existent namespace
920 [
921 Title::makeTitle( 100777, 'Foo_bar' ),
922 'Special:Badtitle/NS100777:Foo_bar'
923 ],
924 ];
925 }
926
927 /**
928 * @covers Title::getPrefixedDBKey
929 * @dataProvider provideGetPrefixedDBKey
930 */
931 public function testGetPrefixedDBKey( Title $title, $expected ) {
932 $this->assertEquals( $expected, $title->getPrefixedDBkey() );
933 }
934
935 /**
936 * @covers Title::getFragmentForURL
937 * @dataProvider provideGetFragmentForURL
938 *
939 * @param string $titleStr
940 * @param string $expected
941 */
942 public function testGetFragmentForURL( $titleStr, $expected ) {
943 $this->setMwGlobals( [
944 'wgFragmentMode' => [ 'html5' ],
945 'wgExternalInterwikiFragmentMode' => 'legacy',
946 ] );
947 $dbw = wfGetDB( DB_MASTER );
948 $dbw->insert( 'interwiki',
949 [
950 [
951 'iw_prefix' => 'de',
952 'iw_url' => 'http://de.wikipedia.org/wiki/',
953 'iw_api' => 'http://de.wikipedia.org/w/api.php',
954 'iw_wikiid' => 'dewiki',
955 'iw_local' => 1,
956 'iw_trans' => 0,
957 ],
958 [
959 'iw_prefix' => 'zz',
960 'iw_url' => 'http://zzwiki.org/wiki/',
961 'iw_api' => 'http://zzwiki.org/w/api.php',
962 'iw_wikiid' => 'zzwiki',
963 'iw_local' => 0,
964 'iw_trans' => 0,
965 ],
966 ],
967 __METHOD__,
968 [ 'IGNORE' ]
969 );
970
971 $title = Title::newFromText( $titleStr );
972 self::assertEquals( $expected, $title->getFragmentForURL() );
973
974 $dbw->delete( 'interwiki', '*', __METHOD__ );
975 }
976
977 public function provideGetFragmentForURL() {
978 return [
979 [ 'Foo', '' ],
980 [ 'Foo#ümlåût', '#ümlåût' ],
981 [ 'de:Foo#Bå®', '#Bå®' ],
982 [ 'zz:Foo#тест', '#.D1.82.D0.B5.D1.81.D1.82' ],
983 ];
984 }
985
986 /**
987 * @covers Title::isRawHtmlMessage
988 * @dataProvider provideIsRawHtmlMessage
989 */
990 public function testIsRawHtmlMessage( $textForm, $expected ) {
991 $this->setMwGlobals( 'wgRawHtmlMessages', [
992 'foobar',
993 'foo_bar',
994 'foo-bar',
995 ] );
996
997 $title = Title::newFromText( $textForm );
998 $this->assertSame( $expected, $title->isRawHtmlMessage() );
999 }
1000
1001 public function provideIsRawHtmlMessage() {
1002 return [
1003 [ 'MediaWiki:Foobar', true ],
1004 [ 'MediaWiki:Foo bar', true ],
1005 [ 'MediaWiki:Foo-bar', true ],
1006 [ 'MediaWiki:foo bar', true ],
1007 [ 'MediaWiki:foo-bar', true ],
1008 [ 'MediaWiki:foobar', true ],
1009 [ 'MediaWiki:some-other-message', false ],
1010 [ 'Main Page', false ],
1011 ];
1012 }
1013
1014 public function provideEquals() {
1015 yield [
1016 Title::newFromText( 'Main Page' ),
1017 Title::newFromText( 'Main Page' ),
1018 true
1019 ];
1020 yield [
1021 Title::newFromText( 'Main Page' ),
1022 Title::newFromText( 'Not The Main Page' ),
1023 false
1024 ];
1025 yield [
1026 Title::newFromText( 'Main Page' ),
1027 Title::newFromText( 'Project:Main Page' ),
1028 false
1029 ];
1030 yield [
1031 Title::newFromText( 'File:Example.png' ),
1032 Title::newFromText( 'Image:Example.png' ),
1033 true
1034 ];
1035 yield [
1036 Title::newFromText( 'Special:Version' ),
1037 Title::newFromText( 'Special:Version' ),
1038 true
1039 ];
1040 yield [
1041 Title::newFromText( 'Special:Version' ),
1042 Title::newFromText( 'Special:Recentchanges' ),
1043 false
1044 ];
1045 yield [
1046 Title::newFromText( 'Special:Version' ),
1047 Title::newFromText( 'Main Page' ),
1048 false
1049 ];
1050 yield [
1051 Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
1052 Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
1053 true
1054 ];
1055 yield [
1056 Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
1057 Title::makeTitle( NS_MAIN, 'Bar', '', '' ),
1058 false
1059 ];
1060 yield [
1061 Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
1062 Title::makeTitle( NS_TALK, 'Foo', '', '' ),
1063 false
1064 ];
1065 yield [
1066 Title::makeTitle( NS_MAIN, 'Foo', 'Bar', '' ),
1067 Title::makeTitle( NS_MAIN, 'Foo', 'Bar', '' ),
1068 true
1069 ];
1070 yield [
1071 Title::makeTitle( NS_MAIN, 'Foo', 'Bar', '' ),
1072 Title::makeTitle( NS_MAIN, 'Foo', 'Baz', '' ),
1073 true
1074 ];
1075 yield [
1076 Title::makeTitle( NS_MAIN, 'Foo', 'Bar', '' ),
1077 Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
1078 true
1079 ];
1080 yield [
1081 Title::makeTitle( NS_MAIN, 'Foo', '', 'baz' ),
1082 Title::makeTitle( NS_MAIN, 'Foo', '', 'baz' ),
1083 true
1084 ];
1085 yield [
1086 Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
1087 Title::makeTitle( NS_MAIN, 'Foo', '', 'baz' ),
1088 false
1089 ];
1090 }
1091
1092 /**
1093 * @covers Title::equals
1094 * @dataProvider provideEquals
1095 */
1096 public function testEquals( Title $firstValue, /* LinkTarget */ $secondValue, $expectedSame ) {
1097 $this->assertSame(
1098 $expectedSame,
1099 $firstValue->equals( $secondValue )
1100 );
1101 }
1102
1103 /**
1104 * @covers Title::newMainPage
1105 */
1106 public function testNewMainPage() {
1107 $msgCache = TestingAccessWrapper::newFromClass( MessageCache::class );
1108 $msgCache->instance = $this->createMock( MessageCache::class );
1109 $msgCache->instance->method( 'get' )->willReturn( 'Foresheet' );
1110 $msgCache->instance->method( 'transform' )->willReturn( 'Foresheet' );
1111
1112 $this->assertSame(
1113 'Foresheet',
1114 Title::newMainPage()->getText()
1115 );
1116 }
1117
1118 /**
1119 * @covers Title::newMainPage
1120 */
1121 public function testNewMainPageWithLocal() {
1122 $local = $this->createMock( MessageLocalizer::class );
1123 $local->method( 'msg' )->willReturn( new RawMessage( 'Prime Article' ) );
1124
1125 $this->assertSame(
1126 'Prime Article',
1127 Title::newMainPage( $local )->getText()
1128 );
1129 }
1130 }