3 use MediaWiki\Interwiki\InterwikiLookup
;
4 use MediaWiki\Linker\LinkTarget
;
5 use MediaWiki\MediaWikiServices
;
6 use Wikimedia\TestingAccessWrapper
;
12 class TitleTest
extends MediaWikiTestCase
{
13 protected function setUp() {
16 $this->setMwGlobals( [
17 'wgAllowUserJs' => false,
18 'wgDefaultLanguageVariant' => false,
19 'wgMetaNamespace' => 'Project',
21 $this->setUserLang( 'en' );
22 $this->setContentLang( 'en' );
25 protected function tearDown() {
26 // For testNewMainPage
27 MessageCache
::destroyInstance();
32 * @covers Title::legalChars
34 public function testLegalChars() {
35 $titlechars = Title
::legalChars();
37 foreach ( range( 1, 255 ) as $num ) {
39 if ( strpos( "#[]{}<>|", $chr ) !== false ||
preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
41 (bool)preg_match( "/[$titlechars]/", $chr ),
42 "chr($num) = $chr is not a valid titlechar"
46 (bool)preg_match( "/[$titlechars]/", $chr ),
47 "chr($num) = $chr is a valid titlechar"
53 public static function provideValidSecureAndSplit() {
64 [ 'Talk:Foo:Sandbox' ],
65 [ 'File:Example.svg' ],
66 [ 'File_talk:Example.svg' ],
67 [ 'Foo/.../Sandbox' ],
71 // Length is 256 total, but only title part matters
72 [ 'Category:' . str_repeat( 'x', 248 ) ],
73 [ str_repeat( 'x', 252 ) ],
75 [ 'localtestiw: #anchor' ],
77 [ 'localtestiw:foo' ],
78 [ 'localtestiw: foo # anchor' ],
79 [ 'localtestiw: Talk: Sandbox # anchor' ],
81 [ 'remotetestiw: Talk: # anchor' ],
82 [ 'remotetestiw: #bar' ],
83 [ 'remotetestiw: Talk:' ],
84 [ 'remotetestiw: Talk: Foo' ],
85 [ 'localtestiw:remotetestiw:' ],
86 [ 'localtestiw:remotetestiw:foo' ]
90 public static function provideInvalidSecureAndSplit() {
92 [ '', 'title-invalid-empty' ],
93 [ ':', 'title-invalid-empty' ],
94 [ '__ __', 'title-invalid-empty' ],
95 [ ' __ ', 'title-invalid-empty' ],
96 // Bad characters forbidden regardless of wgLegalTitleChars
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 | B', 'title-invalid-characters' ],
104 [ "A \t B", 'title-invalid-characters' ],
105 [ "A \n B", 'title-invalid-characters' ],
107 [ 'A%20B', 'title-invalid-characters' ],
108 [ 'A%23B', 'title-invalid-characters' ],
109 [ 'A%2523B', 'title-invalid-characters' ],
110 // XML/HTML character entity references
111 // Note: Commented out because they are not marked invalid by the PHP test as
112 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
116 // Subject of NS_TALK does not roundtrip to NS_MAIN
117 [ 'Talk:File:Example.svg', 'title-invalid-talk-namespace' ],
118 // Directory navigation
119 [ '.', 'title-invalid-relative' ],
120 [ '..', 'title-invalid-relative' ],
121 [ './Sandbox', 'title-invalid-relative' ],
122 [ '../Sandbox', 'title-invalid-relative' ],
123 [ 'Foo/./Sandbox', 'title-invalid-relative' ],
124 [ 'Foo/../Sandbox', 'title-invalid-relative' ],
125 [ 'Sandbox/.', 'title-invalid-relative' ],
126 [ 'Sandbox/..', 'title-invalid-relative' ],
128 [ 'A ~~~ Name', 'title-invalid-magic-tilde' ],
129 [ 'A ~~~~ Signature', 'title-invalid-magic-tilde' ],
130 [ 'A ~~~~~ Timestamp', 'title-invalid-magic-tilde' ],
132 [ str_repeat( 'x', 256 ), 'title-invalid-too-long' ],
133 // Namespace prefix without actual title
134 [ 'Talk:', 'title-invalid-empty' ],
135 [ 'Talk:#', 'title-invalid-empty' ],
136 [ 'Category: ', 'title-invalid-empty' ],
137 [ 'Category: #bar', 'title-invalid-empty' ],
139 [ 'localtestiw: Talk: # anchor', 'title-invalid-empty' ],
140 [ 'localtestiw: Talk:', 'title-invalid-empty' ]
144 private function secureAndSplitGlobals() {
145 $this->setMwGlobals( [
146 'wgLocalInterwikis' => [ 'localtestiw' ],
148 'InterwikiLoadPrefix' => [
149 function ( $prefix, &$data ) {
150 if ( $prefix === 'localtestiw' ) {
151 $data = [ 'iw_url' => 'localtestiw' ];
152 } elseif ( $prefix === 'remotetestiw' ) {
153 $data = [ 'iw_url' => 'remotetestiw' ];
161 // Reset services since we modified $wgLocalInterwikis
162 $this->overrideMwServices();
166 * See also mediawiki.Title.test.js
167 * @covers Title::secureAndSplit
168 * @dataProvider provideValidSecureAndSplit
169 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
171 public function testSecureAndSplitValid( $text ) {
172 $this->secureAndSplitGlobals();
173 $this->assertInstanceOf( Title
::class, Title
::newFromText( $text ), "Valid: $text" );
177 * See also mediawiki.Title.test.js
178 * @covers Title::secureAndSplit
179 * @dataProvider provideInvalidSecureAndSplit
180 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
182 public function testSecureAndSplitInvalid( $text, $expectedErrorMessage ) {
183 $this->secureAndSplitGlobals();
185 Title
::newFromTextThrow( $text ); // should throw
186 $this->assertTrue( false, "Invalid: $text" );
187 } catch ( MalformedTitleException
$ex ) {
188 $this->assertEquals( $expectedErrorMessage, $ex->getErrorMessage(), "Invalid: $text" );
192 public static function provideConvertByteClassToUnicodeClass() {
195 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
196 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
200 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
203 'QWERTY\\x66-\\xFD+',
204 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
212 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
215 'QWERTY\\x66-\\x80+\\x23',
216 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
219 'QWERTY\\x66-\\x80+\\xD3',
220 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
224 '\\\\\\u0080-\\uFFFF',
228 '\\-\\u0080-\\uFFFF',
232 'QWERTY\\-\\u0080-\\uFFFF',
240 'A-\\x7F\\u0080-\\uFFFF',
243 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
244 'f-wQWERTYFXZ\\u0080-\\uFFFF',
247 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
248 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
254 * @dataProvider provideConvertByteClassToUnicodeClass
255 * @covers Title::convertByteClassToUnicodeClass
257 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
258 $this->assertEquals( $unicodeClass, Title
::convertByteClassToUnicodeClass( $byteClass ) );
262 * @dataProvider provideSpecialNamesWithAndWithoutParameter
263 * @covers Title::fixSpecialName
265 public function testFixSpecialNameRetainsParameter( $text, $expectedParam ) {
266 $title = Title
::newFromText( $text );
267 $fixed = $title->fixSpecialName();
268 $stuff = explode( '/', $fixed->getDBkey(), 2 );
269 if ( count( $stuff ) == 2 ) {
277 "T33100 regression check: Title->fixSpecialName() should preserve parameter"
281 public static function provideSpecialNamesWithAndWithoutParameter() {
283 [ 'Special:Version', null ],
284 [ 'Special:Version/', '' ],
285 [ 'Special:Version/param', 'param' ],
290 * Auth-less test of Title::isValidMoveOperation
292 * @param string $source
293 * @param string $target
294 * @param array|string|bool $expected Required error
295 * @dataProvider provideTestIsValidMoveOperation
296 * @covers Title::isValidMoveOperation
298 public function testIsValidMoveOperation( $source, $target, $expected ) {
299 global $wgMultiContentRevisionSchemaMigrationStage;
301 $this->hideDeprecated( 'Title::isValidMoveOperation' );
303 if ( $wgMultiContentRevisionSchemaMigrationStage === SCHEMA_COMPAT_OLD
) {
304 // We can only set this to false with the old schema
305 $this->setMwGlobals( 'wgContentHandlerUseDB', false );
308 $title = Title
::newFromText( $source );
309 $nt = Title
::newFromText( $target );
310 $errors = $title->isValidMoveOperation( $nt, false );
311 if ( $expected === true ) {
312 $this->assertTrue( $errors );
314 $errors = $this->flattenErrorsArray( $errors );
315 foreach ( (array)$expected as $error ) {
316 $this->assertContains( $error, $errors );
321 public static function provideTestIsValidMoveOperation() {
322 global $wgMultiContentRevisionSchemaMigrationStage;
324 // for Title::isValidMoveOperation
325 [ 'Some page', '', 'badtitletext' ],
326 [ 'Test', 'Test', 'selfmove' ],
327 [ 'Special:FooBar', 'Test', 'immobile-source-namespace' ],
328 [ 'Test', 'Special:FooBar', 'immobile-target-namespace' ],
329 [ 'Page', 'File:Test.jpg', 'nonfile-cannot-move-to-file' ],
330 [ 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ],
332 if ( $wgMultiContentRevisionSchemaMigrationStage === SCHEMA_COMPAT_OLD
) {
333 // The error can only occur if $wgContentHandlerUseDB is false, which doesn't work with
334 // the new schema, so omit the test in that case
336 [ 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ]
343 * Auth-less test of Title::userCan
345 * @param array $whitelistRegexp
346 * @param string $source
347 * @param string $action
348 * @param array|string|bool $expected Required error
350 * @covers \Mediawiki\Permissions\PermissionManager::checkReadPermissions
351 * @dataProvider dataWgWhitelistReadRegexp
353 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
354 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
355 // usually have only one regex, it is more concise to write the lonely regex
356 // as a string. Thus we cast to a [] to honor $wgWhitelistReadRegexp
358 if ( is_string( $whitelistRegexp ) ) {
359 $whitelistRegexp = [ $whitelistRegexp ];
362 $this->setMwGlobals( [
363 // So User::isEveryoneAllowed( 'read' ) === false
364 'wgGroupPermissions' => [ '*' => [ 'read' => false ] ],
365 'wgWhitelistRead' => [ 'some random non sense title' ],
366 'wgWhitelistReadRegexp' => $whitelistRegexp,
369 $title = Title
::newFromDBkey( $source );
371 // New anonymous user with no rights
373 $this->overrideUserPermissions( $user, [] );
374 $errors = $title->userCan( $action, $user );
376 if ( is_bool( $expected ) ) {
377 # Forge the assertion message depending on the assertion expectation
378 $allowableness = $expected
379 ?
" should be allowed"
380 : " should NOT be allowed";
384 "User action '$action' on [[$source]] $allowableness."
387 $errors = $this->flattenErrorsArray( $errors );
388 foreach ( (array)$expected as $error ) {
389 $this->assertContains( $error, $errors );
395 * Provides test parameter values for testWgWhitelistReadRegexp()
397 public function dataWgWhitelistReadRegexp() {
402 // Everything, if this doesn't work, we're really in trouble
403 [ '/.*/', 'Main_Page', 'read', $ALLOWED ],
404 [ '/.*/', 'Main_Page', 'edit', $DISALLOWED ],
406 // We validate against the title name, not the db key
407 [ '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ],
409 [ '/^Main/', 'Main_Page', 'read', $ALLOWED ],
410 [ '/^Main.*/', 'Main_Page', 'read', $ALLOWED ],
412 [ '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ],
414 // ...without unicode modifier
415 [ '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ],
416 // ...with unicode modifier
417 [ '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ],
419 [ '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ],
420 [ '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ],
422 // From DefaultSettings.php:
423 [ "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ],
424 [ "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ],
427 [ '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ],
428 [ null, 'Special:Newpages', 'read', $DISALLOWED ],
433 public function flattenErrorsArray( $errors ) {
435 foreach ( $errors as $error ) {
436 $result[] = $error[0];
443 * @dataProvider provideGetPageViewLanguage
444 * @covers Title::getPageViewLanguage
446 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
447 $lang, $variant, $msg = ''
449 // Setup environnement for this test
450 $this->setMwGlobals( [
451 'wgDefaultLanguageVariant' => $variant,
452 'wgAllowUserJs' => true,
454 $this->setUserLang( $lang );
455 $this->setContentLang( $contLang );
457 $title = Title
::newFromText( $titleText );
458 $this->assertInstanceOf( Title
::class, $title,
459 "Test must be passed a valid title text, you gave '$titleText'"
461 $this->assertEquals( $expected,
462 $title->getPageViewLanguage()->getCode(),
467 public static function provideGetPageViewLanguage() {
471 # - content language (expected in most cases)
472 # - wgLang (on some specific pages)
473 # - wgDefaultLanguageVariant
476 [ 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ],
477 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ],
478 [ 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ],
480 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ],
481 [ 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ],
482 [ 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ],
483 [ 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ],
484 [ 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ],
485 [ 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ],
486 [ 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ],
487 [ 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ],
489 [ 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ],
490 [ 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ],
491 [ 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ],
492 [ 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ],
493 [ 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ],
494 [ 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ],
495 [ 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ],
496 [ 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ],
497 [ 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ],
498 [ 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ],
500 [ 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ],
501 [ 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ],
507 * @dataProvider provideBaseTitleCases
508 * @covers Title::getBaseText
510 public function testGetBaseText( $title, $expected, $msg = '' ) {
511 $title = Title
::newFromText( $title );
512 $this->assertSame( $expected,
513 $title->getBaseText(),
519 * @dataProvider provideBaseTitleCases
520 * @covers Title::getBaseTitle
522 public function testGetBaseTitle( $title, $expected, $msg = '' ) {
523 $title = Title
::newFromText( $title );
524 $base = $title->getBaseTitle();
525 $this->assertTrue( $base->isValid(), $msg );
527 $base->equals( Title
::makeTitleSafe( $title->getNamespace(), $expected ) ),
532 public static function provideBaseTitleCases() {
534 # Title, expected base, optional message
535 [ 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ],
536 [ 'User:Foo / Bar / Baz', 'Foo / Bar ' ],
541 * @dataProvider provideRootTitleCases
542 * @covers Title::getRootText
544 public function testGetRootText( $title, $expected, $msg = '' ) {
545 $title = Title
::newFromText( $title );
546 $this->assertEquals( $expected,
547 $title->getRootText(),
553 * @dataProvider provideRootTitleCases
554 * @covers Title::getRootTitle
556 public function testGetRootTitle( $title, $expected, $msg = '' ) {
557 $title = Title
::newFromText( $title );
558 $root = $title->getRootTitle();
559 $this->assertTrue( $root->isValid(), $msg );
561 $root->equals( Title
::makeTitleSafe( $title->getNamespace(), $expected ) ),
566 public static function provideRootTitleCases() {
568 # Title, expected base, optional message
569 [ 'User:John_Doe/subOne/subTwo', 'John Doe' ],
570 [ 'User:Foo / Bar / Baz', 'Foo ' ],
571 [ 'Talk:////', '////' ],
572 [ 'Template:////', '////' ],
573 [ 'Template:Foo////', 'Foo' ],
574 [ 'Template:Foo////Bar', 'Foo' ],
579 * @todo Handle $wgNamespacesWithSubpages cases
580 * @dataProvider provideSubpageTitleCases
581 * @covers Title::getSubpageText
583 public function testGetSubpageText( $title, $expected, $msg = '' ) {
584 $title = Title
::newFromText( $title );
585 $this->assertEquals( $expected,
586 $title->getSubpageText(),
591 public static function provideSubpageTitleCases() {
593 # Title, expected base, optional message
594 [ 'User:John_Doe/subOne/subTwo', 'subTwo' ],
595 [ 'User:John_Doe/subOne', 'subOne' ],
599 public function provideSubpage() {
600 // NOTE: avoid constructing Title objects in the provider, since it may access the database.
602 [ 'Foo', 'x', new TitleValue( NS_MAIN
, 'Foo/x' ) ],
603 [ 'Foo#bar', 'x', new TitleValue( NS_MAIN
, 'Foo/x' ) ],
604 [ 'User:Foo', 'x', new TitleValue( NS_USER
, 'Foo/x' ) ],
605 [ 'wiki:User:Foo', 'x', new TitleValue( NS_MAIN
, 'User:Foo/x', '', 'wiki' ) ],
610 * @dataProvider provideSubpage
611 * @covers Title::getSubpage
613 public function testSubpage( $title, $sub, LinkTarget
$expected ) {
614 $interwikiLookup = $this->getMock( InterwikiLookup
::class );
615 $interwikiLookup->expects( $this->any() )
616 ->method( 'isValidInterwiki' )
617 ->willReturnCallback(
618 function ( $prefix ) {
619 return $prefix == 'wiki';
623 $this->setService( 'InterwikiLookup', $interwikiLookup );
625 $title = Title
::newFromText( $title );
626 $expected = Title
::newFromLinkTarget( $expected );
627 $actual = $title->getSubpage( $sub );
629 // NOTE: convert to string for comparison
630 $this->assertSame( $expected->getPrefixedText(), $actual->getPrefixedText(), 'text form' );
631 $this->assertTrue( $expected->equals( $actual ), 'Title equality' );
634 public static function provideNewFromTitleValue() {
636 [ new TitleValue( NS_MAIN
, 'Foo' ) ],
637 [ new TitleValue( NS_MAIN
, 'Foo', 'bar' ) ],
638 [ new TitleValue( NS_USER
, 'Hansi_Maier' ) ],
643 * @covers Title::newFromTitleValue
644 * @dataProvider provideNewFromTitleValue
646 public function testNewFromTitleValue( TitleValue
$value ) {
647 $title = Title
::newFromTitleValue( $value );
649 $dbkey = str_replace( ' ', '_', $value->getText() );
650 $this->assertEquals( $dbkey, $title->getDBkey() );
651 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
652 $this->assertEquals( $value->getFragment(), $title->getFragment() );
656 * @covers Title::newFromLinkTarget
657 * @dataProvider provideNewFromTitleValue
659 public function testNewFromLinkTarget( LinkTarget
$value ) {
660 $title = Title
::newFromLinkTarget( $value );
662 $dbkey = str_replace( ' ', '_', $value->getText() );
663 $this->assertEquals( $dbkey, $title->getDBkey() );
664 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
665 $this->assertEquals( $value->getFragment(), $title->getFragment() );
669 * @covers Title::newFromLinkTarget
671 public function testNewFromLinkTarget_clone() {
672 $title = Title
::newFromText( __METHOD__
);
673 $this->assertSame( $title, Title
::newFromLinkTarget( $title ) );
675 // The Title::NEW_CLONE flag should ensure that a fresh instance is returned.
676 $clone = Title
::newFromLinkTarget( $title, Title
::NEW_CLONE
);
677 $this->assertNotSame( $title, $clone );
678 $this->assertTrue( $clone->equals( $title ) );
681 public function provideCastFromLinkTarget() {
682 return array_merge( [ [ null ] ], self
::provideNewFromTitleValue() );
686 * @covers Title::castFromLinkTarget
687 * @dataProvider provideCastFromLinkTarget
689 public function testCastFromLinkTarget( $value ) {
690 $title = Title
::castFromLinkTarget( $value );
692 if ( $value === null ) {
693 $this->assertNull( $title );
695 $dbkey = str_replace( ' ', '_', $value->getText() );
696 $this->assertSame( $dbkey, $title->getDBkey() );
697 $this->assertSame( $value->getNamespace(), $title->getNamespace() );
698 $this->assertSame( $value->getFragment(), $title->getFragment() );
702 public static function provideGetTitleValue() {
706 [ 'User:Hansi_Maier' ],
711 * @covers Title::getTitleValue
712 * @dataProvider provideGetTitleValue
714 public function testGetTitleValue( $text ) {
715 $title = Title
::newFromText( $text );
716 $value = $title->getTitleValue();
718 $dbkey = str_replace( ' ', '_', $value->getText() );
719 $this->assertEquals( $title->getDBkey(), $dbkey );
720 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
721 $this->assertEquals( $title->getFragment(), $value->getFragment() );
724 public static function provideGetFragment() {
727 [ 'Foo#bar', 'bar' ],
728 [ 'Foo#bär', 'bär' ],
730 // Inner whitespace is normalized
731 [ 'Foo#bar_bar', 'bar bar' ],
732 [ 'Foo#bar bar', 'bar bar' ],
733 [ 'Foo#bar bar', 'bar bar' ],
735 // Leading whitespace is kept, trailing whitespace is trimmed.
736 // XXX: Is this really want we want?
737 [ 'Foo#_bar_bar_', ' bar bar' ],
738 [ 'Foo# bar bar ', ' bar bar' ],
743 * @covers Title::getFragment
744 * @dataProvider provideGetFragment
746 * @param string $full
747 * @param string $fragment
749 public function testGetFragment( $full, $fragment ) {
750 $title = Title
::newFromText( $full );
751 $this->assertEquals( $fragment, $title->getFragment() );
755 * @covers Title::isAlwaysKnown
756 * @dataProvider provideIsAlwaysKnown
757 * @param string $page
758 * @param bool $isKnown
760 public function testIsAlwaysKnown( $page, $isKnown ) {
761 $title = Title
::newFromText( $page );
762 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
765 public static function provideIsAlwaysKnown() {
767 [ 'Some nonexistent page', false ],
770 [ 'Special:BlankPage', true ],
771 [ 'Special:SomeNonexistentSpecialPage', false ],
772 [ 'MediaWiki:Parentheses', true ],
773 [ 'MediaWiki:Some nonexistent message', false ],
778 * @covers Title::isValid
779 * @dataProvider provideIsValid
780 * @param Title $title
781 * @param bool $isValid
783 public function testIsValid( Title
$title, $isValid ) {
784 $this->assertEquals( $isValid, $title->isValid(), $title->getPrefixedText() );
787 public static function provideIsValid() {
789 [ Title
::makeTitle( NS_MAIN
, '' ), false ],
790 [ Title
::makeTitle( NS_MAIN
, '<>' ), false ],
791 [ Title
::makeTitle( NS_MAIN
, '|' ), false ],
792 [ Title
::makeTitle( NS_MAIN
, '#' ), false ],
793 [ Title
::makeTitle( NS_MAIN
, 'Test' ), true ],
794 [ Title
::makeTitle( NS_MAIN
, ' Test' ), false ],
795 [ Title
::makeTitle( NS_MAIN
, '_Test' ), false ],
796 [ Title
::makeTitle( NS_MAIN
, 'Test ' ), false ],
797 [ Title
::makeTitle( NS_MAIN
, 'Test_' ), false ],
798 [ Title
::makeTitle( NS_MAIN
, "Test\nthis" ), false ],
799 [ Title
::makeTitle( NS_MAIN
, "Test\tthis" ), false ],
800 [ Title
::makeTitle( -33, 'Test' ), false ],
801 [ Title
::makeTitle( 77663399, 'Test' ), false ],
806 * @covers Title::isAlwaysKnown
808 public function testIsAlwaysKnownOnInterwiki() {
809 $title = Title
::makeTitle( NS_MAIN
, 'Interwiki link', '', 'externalwiki' );
810 $this->assertTrue( $title->isAlwaysKnown() );
814 * @covers Title::exists
816 public function testExists() {
817 $title = Title
::makeTitle( NS_PROJECT
, 'New page' );
818 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
820 $article = new Article( $title );
821 $page = $article->getPage();
822 $page->doEditContent( new WikitextContent( 'Some [[link]]' ), 'summary' );
824 // Tell Title it doesn't know whether it exists
825 $title->mArticleID
= -1;
827 // Tell the link cache it doesn't exist when it really does
828 $linkCache->clearLink( $title );
829 $linkCache->addBadLinkObj( $title );
834 'exists() should rely on link cache unless GAID_FOR_UPDATE is used'
838 $title->exists( Title
::GAID_FOR_UPDATE
),
839 'exists() should re-query database when GAID_FOR_UPDATE is used'
843 public function provideCanHaveTalkPage() {
845 'User page has talk page' => [
846 Title
::makeTitle( NS_USER
, 'Jane' ), true
848 'Talke page has talk page' => [
849 Title
::makeTitle( NS_TALK
, 'Foo' ), true
851 'Special page cannot have talk page' => [
852 Title
::makeTitle( NS_SPECIAL
, 'Thing' ), false
854 'Virtual namespace cannot have talk page' => [
855 Title
::makeTitle( NS_MEDIA
, 'Kitten.jpg' ), false
857 'Relative link has no talk page' => [
858 Title
::makeTitle( NS_MAIN
, '', 'Kittens' ), false
860 'Interwiki link has no talk page' => [
861 Title
::makeTitle( NS_MAIN
, 'Kittens', '', 'acme' ), false
866 public function provideIsWatchable() {
868 'User page is watchable' => [
869 Title
::makeTitle( NS_USER
, 'Jane' ), true
871 'Talke page is watchable' => [
872 Title
::makeTitle( NS_TALK
, 'Foo' ), true
874 'Special page is not watchable' => [
875 Title
::makeTitle( NS_SPECIAL
, 'Thing' ), false
877 'Virtual namespace is not watchable' => [
878 Title
::makeTitle( NS_MEDIA
, 'Kitten.jpg' ), false
880 'Relative link is not watchable' => [
881 Title
::makeTitle( NS_MAIN
, '', 'Kittens' ), false
883 'Interwiki link is not watchable' => [
884 Title
::makeTitle( NS_MAIN
, 'Kittens', '', 'acme' ), false
889 public static function provideGetTalkPage_good() {
891 [ Title
::makeTitle( NS_MAIN
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
892 [ Title
::makeTitle( NS_TALK
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
896 public static function provideGetTalkPage_bad() {
898 [ Title
::makeTitle( NS_SPECIAL
, 'Test' ) ],
899 [ Title
::makeTitle( NS_MEDIA
, 'Test' ) ],
900 [ Title
::makeTitle( NS_MAIN
, '', 'Kittens' ) ],
901 [ Title
::makeTitle( NS_MAIN
, 'Kittens', '', 'acme' ) ],
905 public static function provideGetSubjectPage_good() {
907 [ Title
::makeTitle( NS_TALK
, 'Test' ), Title
::makeTitle( NS_MAIN
, 'Test' ) ],
908 [ Title
::makeTitle( NS_MAIN
, 'Test' ), Title
::makeTitle( NS_MAIN
, 'Test' ) ],
912 public static function provideGetOtherPage_good() {
914 [ Title
::makeTitle( NS_MAIN
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
915 [ Title
::makeTitle( NS_TALK
, 'Test' ), Title
::makeTitle( NS_MAIN
, 'Test' ) ],
920 * @dataProvider provideCanHaveTalkPage
921 * @covers Title::canHaveTalkPage
923 * @param Title $title
924 * @param bool $expected
926 public function testCanHaveTalkPage( Title
$title, $expected ) {
927 $actual = $title->canHaveTalkPage();
928 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
932 * @dataProvider provideIsWatchable
933 * @covers Title::isWatchable
935 * @param Title $title
936 * @param bool $expected
938 public function testIsWatchable( Title
$title, $expected ) {
939 $actual = $title->canHaveTalkPage();
940 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
944 * @dataProvider provideGetTalkPage_good
945 * @covers Title::getTalkPageIfDefined
947 public function testGetTalkPage_good( Title
$title, Title
$expected ) {
948 $actual = $title->getTalkPage();
949 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
953 * @dataProvider provideGetTalkPage_bad
954 * @covers Title::getTalkPageIfDefined
956 public function testGetTalkPage_bad( Title
$title ) {
957 $this->setExpectedException( MWException
::class );
958 $title->getTalkPage();
962 * @dataProvider provideGetTalkPage_good
963 * @covers Title::getTalkPageIfDefined
965 public function testGetTalkPageIfDefined_good( Title
$title, Title
$expected ) {
966 $actual = $title->getTalkPageIfDefined();
967 $this->assertNotNull( $actual, $title->getPrefixedDBkey() );
968 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
972 * @dataProvider provideGetTalkPage_bad
973 * @covers Title::getTalkPageIfDefined
975 public function testGetTalkPageIfDefined_bad( Title
$title ) {
976 $talk = $title->getTalkPageIfDefined();
979 $title->getPrefixedDBkey()
984 * @dataProvider provideGetSubjectPage_good
985 * @covers Title::getSubjectPage
987 public function testGetSubjectPage_good( Title
$title, Title
$expected ) {
988 $actual = $title->getSubjectPage();
989 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
993 * @dataProvider provideGetOtherPage_good
994 * @covers Title::getOtherPage
996 public function testGetOtherPage_good( Title
$title, Title
$expected ) {
997 $actual = $title->getOtherPage();
998 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
1002 * @dataProvider provideGetTalkPage_bad
1003 * @covers Title::getOtherPage
1005 public function testGetOtherPage_bad( Title
$title ) {
1006 $this->setExpectedException( MWException
::class );
1007 $title->getOtherPage();
1010 public function provideCreateFragmentTitle() {
1012 [ Title
::makeTitle( NS_MAIN
, 'Test' ), 'foo' ],
1013 [ Title
::makeTitle( NS_TALK
, 'Test', 'foo' ), '' ],
1014 [ Title
::makeTitle( NS_CATEGORY
, 'Test', 'foo' ), 'bar' ],
1015 [ Title
::makeTitle( NS_MAIN
, 'Test1', '', 'interwiki' ), 'baz' ]
1020 * @covers Title::createFragmentTarget
1021 * @dataProvider provideCreateFragmentTitle
1023 public function testCreateFragmentTitle( Title
$title, $fragment ) {
1024 $this->mergeMwGlobalArrayValue( 'wgHooks', [
1025 'InterwikiLoadPrefix' => [
1026 function ( $prefix, &$iwdata ) {
1027 if ( $prefix === 'interwiki' ) {
1029 'iw_url' => 'http://example.com/',
1039 $fragmentTitle = $title->createFragmentTarget( $fragment );
1041 $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
1042 $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
1043 $this->assertEquals( $title->getInterwiki(), $fragmentTitle->getInterwiki() );
1044 $this->assertEquals( $fragment, $fragmentTitle->getFragment() );
1047 public function provideGetPrefixedText() {
1051 Title
::makeTitle( NS_MAIN
, 'Foo bar' ),
1056 Title
::makeTitle( NS_USER
, 'Foo bar' ),
1061 Title
::makeTitle( NS_USER_TALK
, 'Foo bar' ),
1064 // fragment not included
1066 Title
::makeTitle( NS_MAIN
, 'Foo bar', 'fragment' ),
1071 Title
::makeTitle( NS_MEDIA
, 'Foo bar' ),
1074 // non-existent namespace
1076 Title
::makeTitle( 100777, 'Foo bar' ),
1077 'Special:Badtitle/NS100777:Foo bar'
1083 * @covers Title::getPrefixedText
1084 * @dataProvider provideGetPrefixedText
1086 public function testGetPrefixedText( Title
$title, $expected ) {
1087 $this->assertEquals( $expected, $title->getPrefixedText() );
1090 public function provideGetPrefixedDBKey() {
1094 Title
::makeTitle( NS_MAIN
, 'Foo_bar' ),
1099 Title
::makeTitle( NS_USER
, 'Foo_bar' ),
1104 Title
::makeTitle( NS_USER_TALK
, 'Foo_bar' ),
1107 // fragment not included
1109 Title
::makeTitle( NS_MAIN
, 'Foo_bar', 'fragment' ),
1114 Title
::makeTitle( NS_MEDIA
, 'Foo_bar' ),
1117 // non-existent namespace
1119 Title
::makeTitle( 100777, 'Foo_bar' ),
1120 'Special:Badtitle/NS100777:Foo_bar'
1126 * @covers Title::getPrefixedDBKey
1127 * @dataProvider provideGetPrefixedDBKey
1129 public function testGetPrefixedDBKey( Title
$title, $expected ) {
1130 $this->assertEquals( $expected, $title->getPrefixedDBkey() );
1134 * @covers Title::getFragmentForURL
1135 * @dataProvider provideGetFragmentForURL
1137 * @param string $titleStr
1138 * @param string $expected
1140 public function testGetFragmentForURL( $titleStr, $expected ) {
1141 $this->setMwGlobals( [
1142 'wgFragmentMode' => [ 'html5' ],
1143 'wgExternalInterwikiFragmentMode' => 'legacy',
1145 $dbw = wfGetDB( DB_MASTER
);
1146 $dbw->insert( 'interwiki',
1149 'iw_prefix' => 'de',
1150 'iw_url' => 'http://de.wikipedia.org/wiki/',
1151 'iw_api' => 'http://de.wikipedia.org/w/api.php',
1152 'iw_wikiid' => 'dewiki',
1157 'iw_prefix' => 'zz',
1158 'iw_url' => 'http://zzwiki.org/wiki/',
1159 'iw_api' => 'http://zzwiki.org/w/api.php',
1160 'iw_wikiid' => 'zzwiki',
1169 $title = Title
::newFromText( $titleStr );
1170 self
::assertEquals( $expected, $title->getFragmentForURL() );
1172 $dbw->delete( 'interwiki', '*', __METHOD__
);
1175 public function provideGetFragmentForURL() {
1178 [ 'Foo#ümlåût', '#ümlåût' ],
1179 [ 'de:Foo#Bå®', '#Bå®' ],
1180 [ 'zz:Foo#тест', '#.D1.82.D0.B5.D1.81.D1.82' ],
1185 * @covers Title::isRawHtmlMessage
1186 * @dataProvider provideIsRawHtmlMessage
1188 public function testIsRawHtmlMessage( $textForm, $expected ) {
1189 $this->setMwGlobals( 'wgRawHtmlMessages', [
1195 $title = Title
::newFromText( $textForm );
1196 $this->assertSame( $expected, $title->isRawHtmlMessage() );
1199 public function provideIsRawHtmlMessage() {
1201 [ 'MediaWiki:Foobar', true ],
1202 [ 'MediaWiki:Foo bar', true ],
1203 [ 'MediaWiki:Foo-bar', true ],
1204 [ 'MediaWiki:foo bar', true ],
1205 [ 'MediaWiki:foo-bar', true ],
1206 [ 'MediaWiki:foobar', true ],
1207 [ 'MediaWiki:some-other-message', false ],
1208 [ 'Main Page', false ],
1212 public function provideEquals() {
1214 Title
::newFromText( 'Main Page' ),
1215 Title
::newFromText( 'Main Page' ),
1219 Title
::newFromText( 'Main Page' ),
1220 Title
::newFromText( 'Not The Main Page' ),
1224 Title
::newFromText( 'Main Page' ),
1225 Title
::newFromText( 'Project:Main Page' ),
1229 Title
::newFromText( 'File:Example.png' ),
1230 Title
::newFromText( 'Image:Example.png' ),
1234 Title
::newFromText( 'Special:Version' ),
1235 Title
::newFromText( 'Special:Version' ),
1239 Title
::newFromText( 'Special:Version' ),
1240 Title
::newFromText( 'Special:Recentchanges' ),
1244 Title
::newFromText( 'Special:Version' ),
1245 Title
::newFromText( 'Main Page' ),
1249 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1250 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1254 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1255 Title
::makeTitle( NS_MAIN
, 'Bar', '', '' ),
1259 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1260 Title
::makeTitle( NS_TALK
, 'Foo', '', '' ),
1264 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1265 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1269 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1270 Title
::makeTitle( NS_MAIN
, 'Foo', 'Baz', '' ),
1274 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1275 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1279 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1280 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1284 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1285 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1291 * @covers Title::equals
1292 * @dataProvider provideEquals
1294 public function testEquals( Title
$firstValue, /* LinkTarget */ $secondValue, $expectedSame ) {
1297 $firstValue->equals( $secondValue )
1302 * @covers Title::newMainPage
1304 public function testNewMainPage() {
1305 $msgCache = TestingAccessWrapper
::newFromClass( MessageCache
::class );
1306 $msgCache->instance
= $this->createMock( MessageCache
::class );
1307 $msgCache->instance
->method( 'get' )->willReturn( 'Foresheet' );
1308 $msgCache->instance
->method( 'transform' )->willReturn( 'Foresheet' );
1312 Title
::newMainPage()->getText()
1317 * @covers Title::newMainPage
1319 public function testNewMainPageWithLocal() {
1320 $local = $this->createMock( MessageLocalizer
::class );
1321 $local->method( 'msg' )->willReturn( new RawMessage( 'Prime Article' ) );
1325 Title
::newMainPage( $local )->getText()