3 use MediaWiki\Interwiki\InterwikiLookup
;
4 use MediaWiki\Linker\LinkTarget
;
5 use MediaWiki\MediaWikiServices
;
11 class TitleTest
extends MediaWikiTestCase
{
12 protected function setUp() {
15 $this->setMwGlobals( [
16 'wgAllowUserJs' => false,
17 'wgDefaultLanguageVariant' => false,
18 'wgMetaNamespace' => 'Project',
20 $this->setUserLang( 'en' );
21 $this->setContentLang( 'en' );
25 * @covers Title::legalChars
27 public function testLegalChars() {
28 $titlechars = Title
::legalChars();
30 foreach ( range( 1, 255 ) as $num ) {
32 if ( strpos( "#[]{}<>|", $chr ) !== false ||
preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
34 (bool)preg_match( "/[$titlechars]/", $chr ),
35 "chr($num) = $chr is not a valid titlechar"
39 (bool)preg_match( "/[$titlechars]/", $chr ),
40 "chr($num) = $chr is a valid titlechar"
46 public static function provideValidSecureAndSplit() {
57 [ 'Talk:Foo:Sandbox' ],
58 [ 'File:Example.svg' ],
59 [ 'File_talk:Example.svg' ],
60 [ 'Foo/.../Sandbox' ],
64 // Length is 256 total, but only title part matters
65 [ 'Category:' . str_repeat( 'x', 248 ) ],
66 [ str_repeat( 'x', 252 ) ],
68 [ 'localtestiw: #anchor' ],
70 [ 'localtestiw:foo' ],
71 [ 'localtestiw: foo # anchor' ],
72 [ 'localtestiw: Talk: Sandbox # anchor' ],
74 [ 'remotetestiw: Talk: # anchor' ],
75 [ 'remotetestiw: #bar' ],
76 [ 'remotetestiw: Talk:' ],
77 [ 'remotetestiw: Talk: Foo' ],
78 [ 'localtestiw:remotetestiw:' ],
79 [ 'localtestiw:remotetestiw:foo' ]
83 public static function provideInvalidSecureAndSplit() {
85 [ '', 'title-invalid-empty' ],
86 [ ':', 'title-invalid-empty' ],
87 [ '__ __', 'title-invalid-empty' ],
88 [ ' __ ', 'title-invalid-empty' ],
89 // Bad characters forbidden regardless of wgLegalTitleChars
90 [ 'A [ B', 'title-invalid-characters' ],
91 [ 'A ] B', 'title-invalid-characters' ],
92 [ 'A { B', 'title-invalid-characters' ],
93 [ 'A } B', 'title-invalid-characters' ],
94 [ 'A < B', 'title-invalid-characters' ],
95 [ 'A > B', 'title-invalid-characters' ],
96 [ 'A | B', 'title-invalid-characters' ],
97 [ "A \t B", 'title-invalid-characters' ],
98 [ "A \n B", 'title-invalid-characters' ],
100 [ 'A%20B', 'title-invalid-characters' ],
101 [ 'A%23B', 'title-invalid-characters' ],
102 [ 'A%2523B', 'title-invalid-characters' ],
103 // XML/HTML character entity references
104 // Note: Commented out because they are not marked invalid by the PHP test as
105 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
109 // Subject of NS_TALK does not roundtrip to NS_MAIN
110 [ 'Talk:File:Example.svg', 'title-invalid-talk-namespace' ],
111 // Directory navigation
112 [ '.', 'title-invalid-relative' ],
113 [ '..', 'title-invalid-relative' ],
114 [ './Sandbox', 'title-invalid-relative' ],
115 [ '../Sandbox', 'title-invalid-relative' ],
116 [ 'Foo/./Sandbox', 'title-invalid-relative' ],
117 [ 'Foo/../Sandbox', 'title-invalid-relative' ],
118 [ 'Sandbox/.', 'title-invalid-relative' ],
119 [ 'Sandbox/..', 'title-invalid-relative' ],
121 [ 'A ~~~ Name', 'title-invalid-magic-tilde' ],
122 [ 'A ~~~~ Signature', 'title-invalid-magic-tilde' ],
123 [ 'A ~~~~~ Timestamp', 'title-invalid-magic-tilde' ],
125 [ str_repeat( 'x', 256 ), 'title-invalid-too-long' ],
126 // Namespace prefix without actual title
127 [ 'Talk:', 'title-invalid-empty' ],
128 [ 'Talk:#', 'title-invalid-empty' ],
129 [ 'Category: ', 'title-invalid-empty' ],
130 [ 'Category: #bar', 'title-invalid-empty' ],
132 [ 'localtestiw: Talk: # anchor', 'title-invalid-empty' ],
133 [ 'localtestiw: Talk:', 'title-invalid-empty' ]
137 private function secureAndSplitGlobals() {
138 $this->setMwGlobals( [
139 'wgLocalInterwikis' => [ 'localtestiw' ],
141 'InterwikiLoadPrefix' => [
142 function ( $prefix, &$data ) {
143 if ( $prefix === 'localtestiw' ) {
144 $data = [ 'iw_url' => 'localtestiw' ];
145 } elseif ( $prefix === 'remotetestiw' ) {
146 $data = [ 'iw_url' => 'remotetestiw' ];
154 // Reset services since we modified $wgLocalInterwikis
155 $this->overrideMwServices();
159 * See also mediawiki.Title.test.js
160 * @covers Title::secureAndSplit
161 * @dataProvider provideValidSecureAndSplit
162 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
164 public function testSecureAndSplitValid( $text ) {
165 $this->secureAndSplitGlobals();
166 $this->assertInstanceOf( Title
::class, Title
::newFromText( $text ), "Valid: $text" );
170 * See also mediawiki.Title.test.js
171 * @covers Title::secureAndSplit
172 * @dataProvider provideInvalidSecureAndSplit
173 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
175 public function testSecureAndSplitInvalid( $text, $expectedErrorMessage ) {
176 $this->secureAndSplitGlobals();
178 Title
::newFromTextThrow( $text ); // should throw
179 $this->assertTrue( false, "Invalid: $text" );
180 } catch ( MalformedTitleException
$ex ) {
181 $this->assertEquals( $expectedErrorMessage, $ex->getErrorMessage(), "Invalid: $text" );
185 public static function provideConvertByteClassToUnicodeClass() {
188 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
189 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
193 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
196 'QWERTY\\x66-\\xFD+',
197 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
205 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
208 'QWERTY\\x66-\\x80+\\x23',
209 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
212 'QWERTY\\x66-\\x80+\\xD3',
213 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
217 '\\\\\\u0080-\\uFFFF',
221 '\\-\\u0080-\\uFFFF',
225 'QWERTY\\-\\u0080-\\uFFFF',
233 'A-\\x7F\\u0080-\\uFFFF',
236 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
237 'f-wQWERTYFXZ\\u0080-\\uFFFF',
240 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
241 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
247 * @dataProvider provideConvertByteClassToUnicodeClass
248 * @covers Title::convertByteClassToUnicodeClass
250 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
251 $this->assertEquals( $unicodeClass, Title
::convertByteClassToUnicodeClass( $byteClass ) );
255 * @dataProvider provideSpecialNamesWithAndWithoutParameter
256 * @covers Title::fixSpecialName
258 public function testFixSpecialNameRetainsParameter( $text, $expectedParam ) {
259 $title = Title
::newFromText( $text );
260 $fixed = $title->fixSpecialName();
261 $stuff = explode( '/', $fixed->getDBkey(), 2 );
262 if ( count( $stuff ) == 2 ) {
270 "T33100 regression check: Title->fixSpecialName() should preserve parameter"
274 public static function provideSpecialNamesWithAndWithoutParameter() {
276 [ 'Special:Version', null ],
277 [ 'Special:Version/', '' ],
278 [ 'Special:Version/param', 'param' ],
283 * Auth-less test of Title::isValidMoveOperation
285 * @param string $source
286 * @param string $target
287 * @param array|string|bool $expected Required error
288 * @dataProvider provideTestIsValidMoveOperation
289 * @covers Title::isValidMoveOperation
291 public function testIsValidMoveOperation( $source, $target, $expected ) {
292 global $wgMultiContentRevisionSchemaMigrationStage;
294 $this->hideDeprecated( 'Title::isValidMoveOperation' );
296 if ( $wgMultiContentRevisionSchemaMigrationStage === SCHEMA_COMPAT_OLD
) {
297 // We can only set this to false with the old schema
298 $this->setMwGlobals( 'wgContentHandlerUseDB', false );
301 $title = Title
::newFromText( $source );
302 $nt = Title
::newFromText( $target );
303 $errors = $title->isValidMoveOperation( $nt, false );
304 if ( $expected === true ) {
305 $this->assertTrue( $errors );
307 $errors = $this->flattenErrorsArray( $errors );
308 foreach ( (array)$expected as $error ) {
309 $this->assertContains( $error, $errors );
314 public static function provideTestIsValidMoveOperation() {
315 global $wgMultiContentRevisionSchemaMigrationStage;
317 // for Title::isValidMoveOperation
318 [ 'Some page', '', 'badtitletext' ],
319 [ 'Test', 'Test', 'selfmove' ],
320 [ 'Special:FooBar', 'Test', 'immobile-source-namespace' ],
321 [ 'Test', 'Special:FooBar', 'immobile-target-namespace' ],
322 [ 'Page', 'File:Test.jpg', 'nonfile-cannot-move-to-file' ],
323 [ 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ],
325 if ( $wgMultiContentRevisionSchemaMigrationStage === SCHEMA_COMPAT_OLD
) {
326 // The error can only occur if $wgContentHandlerUseDB is false, which doesn't work with
327 // the new schema, so omit the test in that case
329 [ 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ]
336 * Auth-less test of Title::userCan
338 * @param array $whitelistRegexp
339 * @param string $source
340 * @param string $action
341 * @param array|string|bool $expected Required error
343 * @covers \Mediawiki\Permissions\PermissionManager::checkReadPermissions
344 * @dataProvider dataWgWhitelistReadRegexp
346 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
347 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
348 // usually have only one regex, it is more concise to write the lonely regex
349 // as a string. Thus we cast to a [] to honor $wgWhitelistReadRegexp
351 if ( is_string( $whitelistRegexp ) ) {
352 $whitelistRegexp = [ $whitelistRegexp ];
355 $this->setMwGlobals( [
356 // So User::isEveryoneAllowed( 'read' ) === false
357 'wgGroupPermissions' => [ '*' => [ 'read' => false ] ],
358 'wgWhitelistRead' => [ 'some random non sense title' ],
359 'wgWhitelistReadRegexp' => $whitelistRegexp,
362 $title = Title
::newFromDBkey( $source );
364 // New anonymous user with no rights
366 $this->overrideUserPermissions( $user, [] );
367 $errors = $title->userCan( $action, $user );
369 if ( is_bool( $expected ) ) {
370 # Forge the assertion message depending on the assertion expectation
371 $allowableness = $expected
372 ?
" should be allowed"
373 : " should NOT be allowed";
377 "User action '$action' on [[$source]] $allowableness."
380 $errors = $this->flattenErrorsArray( $errors );
381 foreach ( (array)$expected as $error ) {
382 $this->assertContains( $error, $errors );
388 * Provides test parameter values for testWgWhitelistReadRegexp()
390 public function dataWgWhitelistReadRegexp() {
395 // Everything, if this doesn't work, we're really in trouble
396 [ '/.*/', 'Main_Page', 'read', $ALLOWED ],
397 [ '/.*/', 'Main_Page', 'edit', $DISALLOWED ],
399 // We validate against the title name, not the db key
400 [ '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ],
402 [ '/^Main/', 'Main_Page', 'read', $ALLOWED ],
403 [ '/^Main.*/', 'Main_Page', 'read', $ALLOWED ],
405 [ '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ],
407 // ...without unicode modifier
408 [ '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ],
409 // ...with unicode modifier
410 [ '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ],
412 [ '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ],
413 [ '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ],
415 // From DefaultSettings.php:
416 [ "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ],
417 [ "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ],
420 [ '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ],
421 [ null, 'Special:Newpages', 'read', $DISALLOWED ],
426 public function flattenErrorsArray( $errors ) {
428 foreach ( $errors as $error ) {
429 $result[] = $error[0];
436 * @dataProvider provideGetPageViewLanguage
437 * @covers Title::getPageViewLanguage
439 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
440 $lang, $variant, $msg = ''
442 // Setup environnement for this test
443 $this->setMwGlobals( [
444 'wgDefaultLanguageVariant' => $variant,
445 'wgAllowUserJs' => true,
447 $this->setUserLang( $lang );
448 $this->setContentLang( $contLang );
450 $title = Title
::newFromText( $titleText );
451 $this->assertInstanceOf( Title
::class, $title,
452 "Test must be passed a valid title text, you gave '$titleText'"
454 $this->assertEquals( $expected,
455 $title->getPageViewLanguage()->getCode(),
460 public static function provideGetPageViewLanguage() {
464 # - content language (expected in most cases)
465 # - wgLang (on some specific pages)
466 # - wgDefaultLanguageVariant
469 [ 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ],
470 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ],
471 [ 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ],
473 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ],
474 [ 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ],
475 [ 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ],
476 [ 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ],
477 [ 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ],
478 [ 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ],
479 [ 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ],
480 [ 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ],
482 [ 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ],
483 [ 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ],
484 [ 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ],
485 [ 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ],
486 [ 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ],
487 [ 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ],
488 [ 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ],
489 [ 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ],
490 [ 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ],
491 [ 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ],
493 [ 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ],
494 [ 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ],
500 * @dataProvider provideBaseTitleCases
501 * @covers Title::getBaseText
503 public function testGetBaseText( $title, $expected, $msg = '' ) {
504 $title = Title
::newFromText( $title );
505 $this->assertSame( $expected,
506 $title->getBaseText(),
512 * @dataProvider provideBaseTitleCases
513 * @covers Title::getBaseTitle
515 public function testGetBaseTitle( $title, $expected, $msg = '' ) {
516 $title = Title
::newFromText( $title );
517 $base = $title->getBaseTitle();
518 $this->assertTrue( $base->isValid(), $msg );
520 $base->equals( Title
::makeTitleSafe( $title->getNamespace(), $expected ) ),
525 public static function provideBaseTitleCases() {
527 # Title, expected base, optional message
528 [ 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ],
529 [ 'User:Foo / Bar / Baz', 'Foo / Bar ' ],
534 * @dataProvider provideRootTitleCases
535 * @covers Title::getRootText
537 public function testGetRootText( $title, $expected, $msg = '' ) {
538 $title = Title
::newFromText( $title );
539 $this->assertEquals( $expected,
540 $title->getRootText(),
546 * @dataProvider provideRootTitleCases
547 * @covers Title::getRootTitle
549 public function testGetRootTitle( $title, $expected, $msg = '' ) {
550 $title = Title
::newFromText( $title );
551 $root = $title->getRootTitle();
552 $this->assertTrue( $root->isValid(), $msg );
554 $root->equals( Title
::makeTitleSafe( $title->getNamespace(), $expected ) ),
559 public static function provideRootTitleCases() {
561 # Title, expected base, optional message
562 [ 'User:John_Doe/subOne/subTwo', 'John Doe' ],
563 [ 'User:Foo / Bar / Baz', 'Foo ' ],
564 [ 'Talk:////', '////' ],
565 [ 'Template:////', '////' ],
566 [ 'Template:Foo////', 'Foo' ],
567 [ 'Template:Foo////Bar', 'Foo' ],
572 * @todo Handle $wgNamespacesWithSubpages cases
573 * @dataProvider provideSubpageTitleCases
574 * @covers Title::getSubpageText
576 public function testGetSubpageText( $title, $expected, $msg = '' ) {
577 $title = Title
::newFromText( $title );
578 $this->assertEquals( $expected,
579 $title->getSubpageText(),
584 public static function provideSubpageTitleCases() {
586 # Title, expected base, optional message
587 [ 'User:John_Doe/subOne/subTwo', 'subTwo' ],
588 [ 'User:John_Doe/subOne', 'subOne' ],
592 public function provideSubpage() {
593 // NOTE: avoid constructing Title objects in the provider, since it may access the database.
595 [ 'Foo', 'x', new TitleValue( NS_MAIN
, 'Foo/x' ) ],
596 [ 'Foo#bar', 'x', new TitleValue( NS_MAIN
, 'Foo/x' ) ],
597 [ 'User:Foo', 'x', new TitleValue( NS_USER
, 'Foo/x' ) ],
598 [ 'wiki:User:Foo', 'x', new TitleValue( NS_MAIN
, 'User:Foo/x', '', 'wiki' ) ],
603 * @dataProvider provideSubpage
604 * @covers Title::getSubpage
606 public function testSubpage( $title, $sub, LinkTarget
$expected ) {
607 $interwikiLookup = $this->getMock( InterwikiLookup
::class );
608 $interwikiLookup->expects( $this->any() )
609 ->method( 'isValidInterwiki' )
610 ->willReturnCallback(
611 function ( $prefix ) {
612 return $prefix == 'wiki';
616 $this->setService( 'InterwikiLookup', $interwikiLookup );
618 $title = Title
::newFromText( $title );
619 $expected = Title
::newFromLinkTarget( $expected );
620 $actual = $title->getSubpage( $sub );
622 // NOTE: convert to string for comparison
623 $this->assertSame( $expected->getPrefixedText(), $actual->getPrefixedText(), 'text form' );
624 $this->assertTrue( $expected->equals( $actual ), 'Title equality' );
627 public static function provideNewFromTitleValue() {
629 [ new TitleValue( NS_MAIN
, 'Foo' ) ],
630 [ new TitleValue( NS_MAIN
, 'Foo', 'bar' ) ],
631 [ new TitleValue( NS_USER
, 'Hansi_Maier' ) ],
636 * @covers Title::newFromTitleValue
637 * @dataProvider provideNewFromTitleValue
639 public function testNewFromTitleValue( TitleValue
$value ) {
640 $title = Title
::newFromTitleValue( $value );
642 $dbkey = str_replace( ' ', '_', $value->getText() );
643 $this->assertEquals( $dbkey, $title->getDBkey() );
644 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
645 $this->assertEquals( $value->getFragment(), $title->getFragment() );
649 * @covers Title::newFromLinkTarget
650 * @dataProvider provideNewFromTitleValue
652 public function testNewFromLinkTarget( LinkTarget
$value ) {
653 $title = Title
::newFromLinkTarget( $value );
655 $dbkey = str_replace( ' ', '_', $value->getText() );
656 $this->assertEquals( $dbkey, $title->getDBkey() );
657 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
658 $this->assertEquals( $value->getFragment(), $title->getFragment() );
662 * @covers Title::newFromLinkTarget
664 public function testNewFromLinkTarget_clone() {
665 $title = Title
::newFromText( __METHOD__
);
666 $this->assertSame( $title, Title
::newFromLinkTarget( $title ) );
668 // The Title::NEW_CLONE flag should ensure that a fresh instance is returned.
669 $clone = Title
::newFromLinkTarget( $title, Title
::NEW_CLONE
);
670 $this->assertNotSame( $title, $clone );
671 $this->assertTrue( $clone->equals( $title ) );
674 public function provideCastFromLinkTarget() {
675 return array_merge( [ [ null ] ], self
::provideNewFromTitleValue() );
679 * @covers Title::castFromLinkTarget
680 * @dataProvider provideCastFromLinkTarget
682 public function testCastFromLinkTarget( $value ) {
683 $title = Title
::castFromLinkTarget( $value );
685 if ( $value === null ) {
686 $this->assertNull( $title );
688 $dbkey = str_replace( ' ', '_', $value->getText() );
689 $this->assertSame( $dbkey, $title->getDBkey() );
690 $this->assertSame( $value->getNamespace(), $title->getNamespace() );
691 $this->assertSame( $value->getFragment(), $title->getFragment() );
695 public static function provideGetTitleValue() {
699 [ 'User:Hansi_Maier' ],
704 * @covers Title::getTitleValue
705 * @dataProvider provideGetTitleValue
707 public function testGetTitleValue( $text ) {
708 $title = Title
::newFromText( $text );
709 $value = $title->getTitleValue();
711 $dbkey = str_replace( ' ', '_', $value->getText() );
712 $this->assertEquals( $title->getDBkey(), $dbkey );
713 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
714 $this->assertEquals( $title->getFragment(), $value->getFragment() );
717 public static function provideGetFragment() {
720 [ 'Foo#bar', 'bar' ],
721 [ 'Foo#bär', 'bär' ],
723 // Inner whitespace is normalized
724 [ 'Foo#bar_bar', 'bar bar' ],
725 [ 'Foo#bar bar', 'bar bar' ],
726 [ 'Foo#bar bar', 'bar bar' ],
728 // Leading whitespace is kept, trailing whitespace is trimmed.
729 // XXX: Is this really want we want?
730 [ 'Foo#_bar_bar_', ' bar bar' ],
731 [ 'Foo# bar bar ', ' bar bar' ],
736 * @covers Title::getFragment
737 * @dataProvider provideGetFragment
739 * @param string $full
740 * @param string $fragment
742 public function testGetFragment( $full, $fragment ) {
743 $title = Title
::newFromText( $full );
744 $this->assertEquals( $fragment, $title->getFragment() );
748 * @covers Title::isAlwaysKnown
749 * @dataProvider provideIsAlwaysKnown
750 * @param string $page
751 * @param bool $isKnown
753 public function testIsAlwaysKnown( $page, $isKnown ) {
754 $title = Title
::newFromText( $page );
755 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
758 public static function provideIsAlwaysKnown() {
760 [ 'Some nonexistent page', false ],
763 [ 'Special:BlankPage', true ],
764 [ 'Special:SomeNonexistentSpecialPage', false ],
765 [ 'MediaWiki:Parentheses', true ],
766 [ 'MediaWiki:Some nonexistent message', false ],
771 * @covers Title::isValid
772 * @dataProvider provideIsValid
773 * @param Title $title
774 * @param bool $isValid
776 public function testIsValid( Title
$title, $isValid ) {
777 $this->assertEquals( $isValid, $title->isValid(), $title->getPrefixedText() );
780 public static function provideIsValid() {
782 [ Title
::makeTitle( NS_MAIN
, '' ), false ],
783 [ Title
::makeTitle( NS_MAIN
, '<>' ), false ],
784 [ Title
::makeTitle( NS_MAIN
, '|' ), false ],
785 [ Title
::makeTitle( NS_MAIN
, '#' ), false ],
786 [ Title
::makeTitle( NS_MAIN
, 'Test' ), true ],
787 [ Title
::makeTitle( NS_MAIN
, ' Test' ), false ],
788 [ Title
::makeTitle( NS_MAIN
, '_Test' ), false ],
789 [ Title
::makeTitle( NS_MAIN
, 'Test ' ), false ],
790 [ Title
::makeTitle( NS_MAIN
, 'Test_' ), false ],
791 [ Title
::makeTitle( NS_MAIN
, "Test\nthis" ), false ],
792 [ Title
::makeTitle( NS_MAIN
, "Test\tthis" ), false ],
793 [ Title
::makeTitle( -33, 'Test' ), false ],
794 [ Title
::makeTitle( 77663399, 'Test' ), false ],
799 * @covers Title::isAlwaysKnown
801 public function testIsAlwaysKnownOnInterwiki() {
802 $title = Title
::makeTitle( NS_MAIN
, 'Interwiki link', '', 'externalwiki' );
803 $this->assertTrue( $title->isAlwaysKnown() );
807 * @covers Title::exists
809 public function testExists() {
810 $title = Title
::makeTitle( NS_PROJECT
, 'New page' );
811 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
813 $article = new Article( $title );
814 $page = $article->getPage();
815 $page->doEditContent( new WikitextContent( 'Some [[link]]' ), 'summary' );
817 // Tell Title it doesn't know whether it exists
818 $title->mArticleID
= -1;
820 // Tell the link cache it doesn't exist when it really does
821 $linkCache->clearLink( $title );
822 $linkCache->addBadLinkObj( $title );
827 'exists() should rely on link cache unless GAID_FOR_UPDATE is used'
831 $title->exists( Title
::GAID_FOR_UPDATE
),
832 'exists() should re-query database when GAID_FOR_UPDATE is used'
836 public function provideCanHaveTalkPage() {
838 'User page has talk page' => [
839 Title
::makeTitle( NS_USER
, 'Jane' ), true
841 'Talke page has talk page' => [
842 Title
::makeTitle( NS_TALK
, 'Foo' ), true
844 'Special page cannot have talk page' => [
845 Title
::makeTitle( NS_SPECIAL
, 'Thing' ), false
847 'Virtual namespace cannot have talk page' => [
848 Title
::makeTitle( NS_MEDIA
, 'Kitten.jpg' ), false
850 'Relative link has no talk page' => [
851 Title
::makeTitle( NS_MAIN
, '', 'Kittens' ), false
853 'Interwiki link has no talk page' => [
854 Title
::makeTitle( NS_MAIN
, 'Kittens', '', 'acme' ), false
859 public function provideIsWatchable() {
861 'User page is watchable' => [
862 Title
::makeTitle( NS_USER
, 'Jane' ), true
864 'Talke page is watchable' => [
865 Title
::makeTitle( NS_TALK
, 'Foo' ), true
867 'Special page is not watchable' => [
868 Title
::makeTitle( NS_SPECIAL
, 'Thing' ), false
870 'Virtual namespace is not watchable' => [
871 Title
::makeTitle( NS_MEDIA
, 'Kitten.jpg' ), false
873 'Relative link is not watchable' => [
874 Title
::makeTitle( NS_MAIN
, '', 'Kittens' ), false
876 'Interwiki link is not watchable' => [
877 Title
::makeTitle( NS_MAIN
, 'Kittens', '', 'acme' ), false
882 public static function provideGetTalkPage_good() {
884 [ Title
::makeTitle( NS_MAIN
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
885 [ Title
::makeTitle( NS_TALK
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
889 public static function provideGetTalkPage_bad() {
891 [ Title
::makeTitle( NS_SPECIAL
, 'Test' ) ],
892 [ Title
::makeTitle( NS_MEDIA
, 'Test' ) ],
893 [ Title
::makeTitle( NS_MAIN
, '', 'Kittens' ) ],
894 [ Title
::makeTitle( NS_MAIN
, 'Kittens', '', 'acme' ) ],
898 public static function provideGetSubjectPage_good() {
900 [ Title
::makeTitle( NS_TALK
, 'Test' ), Title
::makeTitle( NS_MAIN
, 'Test' ) ],
901 [ Title
::makeTitle( NS_MAIN
, 'Test' ), Title
::makeTitle( NS_MAIN
, 'Test' ) ],
905 public static function provideGetOtherPage_good() {
907 [ Title
::makeTitle( NS_MAIN
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
908 [ Title
::makeTitle( NS_TALK
, 'Test' ), Title
::makeTitle( NS_MAIN
, 'Test' ) ],
913 * @dataProvider provideCanHaveTalkPage
914 * @covers Title::canHaveTalkPage
916 * @param Title $title
917 * @param bool $expected
919 public function testCanHaveTalkPage( Title
$title, $expected ) {
920 $actual = $title->canHaveTalkPage();
921 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
925 * @dataProvider provideIsWatchable
926 * @covers Title::isWatchable
928 * @param Title $title
929 * @param bool $expected
931 public function testIsWatchable( Title
$title, $expected ) {
932 $actual = $title->canHaveTalkPage();
933 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
937 * @dataProvider provideGetTalkPage_good
938 * @covers Title::getTalkPageIfDefined
940 public function testGetTalkPage_good( Title
$title, Title
$expected ) {
941 $actual = $title->getTalkPage();
942 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
946 * @dataProvider provideGetTalkPage_bad
947 * @covers Title::getTalkPageIfDefined
949 public function testGetTalkPage_bad( Title
$title ) {
950 $this->setExpectedException( MWException
::class );
951 $title->getTalkPage();
955 * @dataProvider provideGetTalkPage_good
956 * @covers Title::getTalkPageIfDefined
958 public function testGetTalkPageIfDefined_good( Title
$title, Title
$expected ) {
959 $actual = $title->getTalkPageIfDefined();
960 $this->assertNotNull( $actual, $title->getPrefixedDBkey() );
961 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
965 * @dataProvider provideGetTalkPage_bad
966 * @covers Title::getTalkPageIfDefined
968 public function testGetTalkPageIfDefined_bad( Title
$title ) {
969 $talk = $title->getTalkPageIfDefined();
972 $title->getPrefixedDBkey()
977 * @dataProvider provideGetSubjectPage_good
978 * @covers Title::getSubjectPage
980 public function testGetSubjectPage_good( Title
$title, Title
$expected ) {
981 $actual = $title->getSubjectPage();
982 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
986 * @dataProvider provideGetOtherPage_good
987 * @covers Title::getOtherPage
989 public function testGetOtherPage_good( Title
$title, Title
$expected ) {
990 $actual = $title->getOtherPage();
991 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
995 * @dataProvider provideGetTalkPage_bad
996 * @covers Title::getOtherPage
998 public function testGetOtherPage_bad( Title
$title ) {
999 $this->setExpectedException( MWException
::class );
1000 $title->getOtherPage();
1003 public function provideCreateFragmentTitle() {
1005 [ Title
::makeTitle( NS_MAIN
, 'Test' ), 'foo' ],
1006 [ Title
::makeTitle( NS_TALK
, 'Test', 'foo' ), '' ],
1007 [ Title
::makeTitle( NS_CATEGORY
, 'Test', 'foo' ), 'bar' ],
1008 [ Title
::makeTitle( NS_MAIN
, 'Test1', '', 'interwiki' ), 'baz' ]
1013 * @covers Title::createFragmentTarget
1014 * @dataProvider provideCreateFragmentTitle
1016 public function testCreateFragmentTitle( Title
$title, $fragment ) {
1017 $this->mergeMwGlobalArrayValue( 'wgHooks', [
1018 'InterwikiLoadPrefix' => [
1019 function ( $prefix, &$iwdata ) {
1020 if ( $prefix === 'interwiki' ) {
1022 'iw_url' => 'http://example.com/',
1032 $fragmentTitle = $title->createFragmentTarget( $fragment );
1034 $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
1035 $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
1036 $this->assertEquals( $title->getInterwiki(), $fragmentTitle->getInterwiki() );
1037 $this->assertEquals( $fragment, $fragmentTitle->getFragment() );
1040 public function provideGetPrefixedText() {
1044 Title
::makeTitle( NS_MAIN
, 'Foo bar' ),
1049 Title
::makeTitle( NS_USER
, 'Foo bar' ),
1054 Title
::makeTitle( NS_USER_TALK
, 'Foo bar' ),
1057 // fragment not included
1059 Title
::makeTitle( NS_MAIN
, 'Foo bar', 'fragment' ),
1064 Title
::makeTitle( NS_MEDIA
, 'Foo bar' ),
1067 // non-existent namespace
1069 Title
::makeTitle( 100777, 'Foo bar' ),
1070 'Special:Badtitle/NS100777:Foo bar'
1076 * @covers Title::getPrefixedText
1077 * @dataProvider provideGetPrefixedText
1079 public function testGetPrefixedText( Title
$title, $expected ) {
1080 $this->assertEquals( $expected, $title->getPrefixedText() );
1083 public function provideGetPrefixedDBKey() {
1087 Title
::makeTitle( NS_MAIN
, 'Foo_bar' ),
1092 Title
::makeTitle( NS_USER
, 'Foo_bar' ),
1097 Title
::makeTitle( NS_USER_TALK
, 'Foo_bar' ),
1100 // fragment not included
1102 Title
::makeTitle( NS_MAIN
, 'Foo_bar', 'fragment' ),
1107 Title
::makeTitle( NS_MEDIA
, 'Foo_bar' ),
1110 // non-existent namespace
1112 Title
::makeTitle( 100777, 'Foo_bar' ),
1113 'Special:Badtitle/NS100777:Foo_bar'
1119 * @covers Title::getPrefixedDBKey
1120 * @dataProvider provideGetPrefixedDBKey
1122 public function testGetPrefixedDBKey( Title
$title, $expected ) {
1123 $this->assertEquals( $expected, $title->getPrefixedDBkey() );
1127 * @covers Title::getFragmentForURL
1128 * @dataProvider provideGetFragmentForURL
1130 * @param string $titleStr
1131 * @param string $expected
1133 public function testGetFragmentForURL( $titleStr, $expected ) {
1134 $this->setMwGlobals( [
1135 'wgFragmentMode' => [ 'html5' ],
1136 'wgExternalInterwikiFragmentMode' => 'legacy',
1138 $dbw = wfGetDB( DB_MASTER
);
1139 $dbw->insert( 'interwiki',
1142 'iw_prefix' => 'de',
1143 'iw_url' => 'http://de.wikipedia.org/wiki/',
1144 'iw_api' => 'http://de.wikipedia.org/w/api.php',
1145 'iw_wikiid' => 'dewiki',
1150 'iw_prefix' => 'zz',
1151 'iw_url' => 'http://zzwiki.org/wiki/',
1152 'iw_api' => 'http://zzwiki.org/w/api.php',
1153 'iw_wikiid' => 'zzwiki',
1162 $title = Title
::newFromText( $titleStr );
1163 self
::assertEquals( $expected, $title->getFragmentForURL() );
1165 $dbw->delete( 'interwiki', '*', __METHOD__
);
1168 public function provideGetFragmentForURL() {
1171 [ 'Foo#ümlåût', '#ümlåût' ],
1172 [ 'de:Foo#Bå®', '#Bå®' ],
1173 [ 'zz:Foo#тест', '#.D1.82.D0.B5.D1.81.D1.82' ],
1178 * @covers Title::isRawHtmlMessage
1179 * @dataProvider provideIsRawHtmlMessage
1181 public function testIsRawHtmlMessage( $textForm, $expected ) {
1182 $this->setMwGlobals( 'wgRawHtmlMessages', [
1188 $title = Title
::newFromText( $textForm );
1189 $this->assertSame( $expected, $title->isRawHtmlMessage() );
1192 public function provideIsRawHtmlMessage() {
1194 [ 'MediaWiki:Foobar', true ],
1195 [ 'MediaWiki:Foo bar', true ],
1196 [ 'MediaWiki:Foo-bar', true ],
1197 [ 'MediaWiki:foo bar', true ],
1198 [ 'MediaWiki:foo-bar', true ],
1199 [ 'MediaWiki:foobar', true ],
1200 [ 'MediaWiki:some-other-message', false ],
1201 [ 'Main Page', false ],
1205 public function provideEquals() {
1207 Title
::newFromText( 'Main Page' ),
1208 Title
::newFromText( 'Main Page' ),
1212 Title
::newFromText( 'Main Page' ),
1213 Title
::newFromText( 'Not The Main Page' ),
1217 Title
::newFromText( 'Main Page' ),
1218 Title
::newFromText( 'Project:Main Page' ),
1222 Title
::newFromText( 'File:Example.png' ),
1223 Title
::newFromText( 'Image:Example.png' ),
1227 Title
::newFromText( 'Special:Version' ),
1228 Title
::newFromText( 'Special:Version' ),
1232 Title
::newFromText( 'Special:Version' ),
1233 Title
::newFromText( 'Special:Recentchanges' ),
1237 Title
::newFromText( 'Special:Version' ),
1238 Title
::newFromText( 'Main Page' ),
1242 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1243 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1247 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1248 Title
::makeTitle( NS_MAIN
, 'Bar', '', '' ),
1252 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1253 Title
::makeTitle( NS_TALK
, 'Foo', '', '' ),
1257 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1258 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1262 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1263 Title
::makeTitle( NS_MAIN
, 'Foo', 'Baz', '' ),
1267 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1268 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1272 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1273 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1277 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1278 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1284 * @covers Title::equals
1285 * @dataProvider provideEquals
1287 public function testEquals( Title
$firstValue, /* LinkTarget */ $secondValue, $expectedSame ) {
1290 $firstValue->equals( $secondValue )
1295 * @covers Title::newMainPage
1297 public function testNewMainPage() {
1298 $mock = $this->createMock( MessageCache
::class );
1299 $mock->method( 'get' )->willReturn( 'Foresheet' );
1300 $mock->method( 'transform' )->willReturn( 'Foresheet' );
1302 $this->setService( 'MessageCache', $mock );
1306 Title
::newMainPage()->getText()
1311 * @covers Title::newMainPage
1313 public function testNewMainPageWithLocal() {
1314 $local = $this->createMock( MessageLocalizer
::class );
1315 $local->method( 'msg' )->willReturn( new RawMessage( 'Prime Article' ) );
1319 Title
::newMainPage( $local )->getText()