3 use MediaWiki\Linker\LinkTarget
;
4 use MediaWiki\MediaWikiServices
;
5 use Wikimedia\TestingAccessWrapper
;
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' );
24 protected function tearDown() {
25 // For testNewMainPage
26 MessageCache
::destroyInstance();
31 * @covers Title::legalChars
33 public function testLegalChars() {
34 $titlechars = Title
::legalChars();
36 foreach ( range( 1, 255 ) as $num ) {
38 if ( strpos( "#[]{}<>|", $chr ) !== false ||
preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
40 (bool)preg_match( "/[$titlechars]/", $chr ),
41 "chr($num) = $chr is not a valid titlechar"
45 (bool)preg_match( "/[$titlechars]/", $chr ),
46 "chr($num) = $chr is a valid titlechar"
52 public static function provideValidSecureAndSplit() {
63 [ 'Talk:Foo:Sandbox' ],
64 [ 'File:Example.svg' ],
65 [ 'File_talk:Example.svg' ],
66 [ 'Foo/.../Sandbox' ],
70 // Length is 256 total, but only title part matters
71 [ 'Category:' . str_repeat( 'x', 248 ) ],
72 [ str_repeat( 'x', 252 ) ],
74 [ 'localtestiw: #anchor' ],
76 [ 'localtestiw:foo' ],
77 [ 'localtestiw: foo # anchor' ],
78 [ 'localtestiw: Talk: Sandbox # anchor' ],
80 [ 'remotetestiw: Talk: # anchor' ],
81 [ 'remotetestiw: #bar' ],
82 [ 'remotetestiw: Talk:' ],
83 [ 'remotetestiw: Talk: Foo' ],
84 [ 'localtestiw:remotetestiw:' ],
85 [ 'localtestiw:remotetestiw:foo' ]
89 public static function provideInvalidSecureAndSplit() {
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' ],
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.
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' ],
127 [ 'A ~~~ Name', 'title-invalid-magic-tilde' ],
128 [ 'A ~~~~ Signature', 'title-invalid-magic-tilde' ],
129 [ 'A ~~~~~ Timestamp', 'title-invalid-magic-tilde' ],
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' ],
138 [ 'localtestiw: Talk: # anchor', 'title-invalid-empty' ],
139 [ 'localtestiw: Talk:', 'title-invalid-empty' ]
143 private function secureAndSplitGlobals() {
144 $this->setMwGlobals( [
145 'wgLocalInterwikis' => [ 'localtestiw' ],
147 'InterwikiLoadPrefix' => [
148 function ( $prefix, &$data ) {
149 if ( $prefix === 'localtestiw' ) {
150 $data = [ 'iw_url' => 'localtestiw' ];
151 } elseif ( $prefix === 'remotetestiw' ) {
152 $data = [ 'iw_url' => 'remotetestiw' ];
160 // Reset services since we modified $wgLocalInterwikis
161 $this->overrideMwServices();
165 * See also mediawiki.Title.test.js
166 * @covers Title::secureAndSplit
167 * @dataProvider provideValidSecureAndSplit
168 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
170 public function testSecureAndSplitValid( $text ) {
171 $this->secureAndSplitGlobals();
172 $this->assertInstanceOf( Title
::class, Title
::newFromText( $text ), "Valid: $text" );
176 * See also mediawiki.Title.test.js
177 * @covers Title::secureAndSplit
178 * @dataProvider provideInvalidSecureAndSplit
179 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
181 public function testSecureAndSplitInvalid( $text, $expectedErrorMessage ) {
182 $this->secureAndSplitGlobals();
184 Title
::newFromTextThrow( $text ); // should throw
185 $this->assertTrue( false, "Invalid: $text" );
186 } catch ( MalformedTitleException
$ex ) {
187 $this->assertEquals( $expectedErrorMessage, $ex->getErrorMessage(), "Invalid: $text" );
191 public static function provideConvertByteClassToUnicodeClass() {
194 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
195 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
199 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
202 'QWERTY\\x66-\\xFD+',
203 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
211 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
214 'QWERTY\\x66-\\x80+\\x23',
215 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
218 'QWERTY\\x66-\\x80+\\xD3',
219 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
223 '\\\\\\u0080-\\uFFFF',
227 '\\-\\u0080-\\uFFFF',
231 'QWERTY\\-\\u0080-\\uFFFF',
239 'A-\\x7F\\u0080-\\uFFFF',
242 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
243 'f-wQWERTYFXZ\\u0080-\\uFFFF',
246 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
247 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
253 * @dataProvider provideConvertByteClassToUnicodeClass
254 * @covers Title::convertByteClassToUnicodeClass
256 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
257 $this->assertEquals( $unicodeClass, Title
::convertByteClassToUnicodeClass( $byteClass ) );
261 * @dataProvider provideSpecialNamesWithAndWithoutParameter
262 * @covers Title::fixSpecialName
264 public function testFixSpecialNameRetainsParameter( $text, $expectedParam ) {
265 $title = Title
::newFromText( $text );
266 $fixed = $title->fixSpecialName();
267 $stuff = explode( '/', $fixed->getDBkey(), 2 );
268 if ( count( $stuff ) == 2 ) {
276 "T33100 regression check: Title->fixSpecialName() should preserve parameter"
280 public static function provideSpecialNamesWithAndWithoutParameter() {
282 [ 'Special:Version', null ],
283 [ 'Special:Version/', '' ],
284 [ 'Special:Version/param', 'param' ],
289 * Auth-less test of Title::isValidMoveOperation
291 * @param string $source
292 * @param string $target
293 * @param array|string|bool $expected Required error
294 * @dataProvider provideTestIsValidMoveOperation
295 * @covers Title::isValidMoveOperation
297 public function testIsValidMoveOperation( $source, $target, $expected ) {
298 $this->hideDeprecated( 'Title::isValidMoveOperation' );
300 $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() {
316 // for Title::isValidMoveOperation
317 [ 'Some page', '', 'badtitletext' ],
318 [ 'Test', 'Test', 'selfmove' ],
319 [ 'Special:FooBar', 'Test', 'immobile-source-namespace' ],
320 [ 'Test', 'Special:FooBar', 'immobile-target-namespace' ],
321 [ 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ],
322 [ 'Page', 'File:Test.jpg', 'nonfile-cannot-move-to-file' ],
323 [ 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ],
328 * Auth-less test of Title::userCan
330 * @param array $whitelistRegexp
331 * @param string $source
332 * @param string $action
333 * @param array|string|bool $expected Required error
335 * @covers \Mediawiki\Permissions\PermissionManager::checkReadPermissions
336 * @dataProvider dataWgWhitelistReadRegexp
338 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
339 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
340 // usually have only one regex, it is more concise to write the lonely regex
341 // as a string. Thus we cast to a [] to honor $wgWhitelistReadRegexp
343 if ( is_string( $whitelistRegexp ) ) {
344 $whitelistRegexp = [ $whitelistRegexp ];
347 $this->setMwGlobals( [
348 // So User::isEveryoneAllowed( 'read' ) === false
349 'wgGroupPermissions' => [ '*' => [ 'read' => false ] ],
350 'wgWhitelistRead' => [ 'some random non sense title' ],
351 'wgWhitelistReadRegexp' => $whitelistRegexp,
354 $title = Title
::newFromDBkey( $source );
356 // New anonymous user with no rights
358 $this->overrideUserPermissions( $user, [] );
359 $errors = $title->userCan( $action, $user );
361 if ( is_bool( $expected ) ) {
362 # Forge the assertion message depending on the assertion expectation
363 $allowableness = $expected
364 ?
" should be allowed"
365 : " should NOT be allowed";
369 "User action '$action' on [[$source]] $allowableness."
372 $errors = $this->flattenErrorsArray( $errors );
373 foreach ( (array)$expected as $error ) {
374 $this->assertContains( $error, $errors );
380 * Provides test parameter values for testWgWhitelistReadRegexp()
382 public function dataWgWhitelistReadRegexp() {
387 // Everything, if this doesn't work, we're really in trouble
388 [ '/.*/', 'Main_Page', 'read', $ALLOWED ],
389 [ '/.*/', 'Main_Page', 'edit', $DISALLOWED ],
391 // We validate against the title name, not the db key
392 [ '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ],
394 [ '/^Main/', 'Main_Page', 'read', $ALLOWED ],
395 [ '/^Main.*/', 'Main_Page', 'read', $ALLOWED ],
397 [ '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ],
399 // ...without unicode modifier
400 [ '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ],
401 // ...with unicode modifier
402 [ '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ],
404 [ '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ],
405 [ '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ],
407 // From DefaultSettings.php:
408 [ "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ],
409 [ "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ],
412 [ '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ],
413 [ null, 'Special:Newpages', 'read', $DISALLOWED ],
418 public function flattenErrorsArray( $errors ) {
420 foreach ( $errors as $error ) {
421 $result[] = $error[0];
428 * @dataProvider provideGetPageViewLanguage
429 * @covers Title::getPageViewLanguage
431 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
432 $lang, $variant, $msg = ''
434 // Setup environnement for this test
435 $this->setMwGlobals( [
436 'wgDefaultLanguageVariant' => $variant,
437 'wgAllowUserJs' => true,
439 $this->setUserLang( $lang );
440 $this->setContentLang( $contLang );
442 $title = Title
::newFromText( $titleText );
443 $this->assertInstanceOf( Title
::class, $title,
444 "Test must be passed a valid title text, you gave '$titleText'"
446 $this->assertEquals( $expected,
447 $title->getPageViewLanguage()->getCode(),
452 public static function provideGetPageViewLanguage() {
456 # - content language (expected in most cases)
457 # - wgLang (on some specific pages)
458 # - wgDefaultLanguageVariant
461 [ 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ],
462 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ],
463 [ 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ],
465 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ],
466 [ 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ],
467 [ 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ],
468 [ 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ],
469 [ 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ],
470 [ 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ],
471 [ 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ],
472 [ 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ],
474 [ 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ],
475 [ 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ],
476 [ 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ],
477 [ 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ],
478 [ 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ],
479 [ 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ],
480 [ 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ],
481 [ 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ],
482 [ 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ],
483 [ 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ],
485 [ 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ],
486 [ 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ],
492 * @dataProvider provideBaseTitleCases
493 * @covers Title::getBaseText
495 public function testGetBaseText( $title, $expected, $msg = '' ) {
496 $title = Title
::newFromText( $title );
497 $this->assertSame( $expected,
498 $title->getBaseText(),
504 * @dataProvider provideBaseTitleCases
505 * @covers Title::getBaseTitle
507 public function testGetBaseTitle( $title, $expected, $msg = '' ) {
508 $title = Title
::newFromText( $title );
509 $base = $title->getBaseTitle();
510 $this->assertTrue( $base->isValid(), $msg );
512 $base->equals( Title
::makeTitleSafe( $title->getNamespace(), $expected ) ),
517 public static function provideBaseTitleCases() {
519 # Title, expected base, optional message
520 [ 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ],
521 [ 'User:Foo / Bar / Baz', 'Foo / Bar ' ],
526 * @dataProvider provideRootTitleCases
527 * @covers Title::getRootText
529 public function testGetRootText( $title, $expected, $msg = '' ) {
530 $title = Title
::newFromText( $title );
531 $this->assertEquals( $expected,
532 $title->getRootText(),
538 * @dataProvider provideRootTitleCases
539 * @covers Title::getRootTitle
541 public function testGetRootTitle( $title, $expected, $msg = '' ) {
542 $title = Title
::newFromText( $title );
543 $root = $title->getRootTitle();
544 $this->assertTrue( $root->isValid(), $msg );
546 $root->equals( Title
::makeTitleSafe( $title->getNamespace(), $expected ) ),
551 public static function provideRootTitleCases() {
553 # Title, expected base, optional message
554 [ 'User:John_Doe/subOne/subTwo', 'John Doe' ],
555 [ 'User:Foo / Bar / Baz', 'Foo ' ],
556 [ 'Talk:////', '////' ],
557 [ 'Template:////', '////' ],
558 [ 'Template:Foo////', 'Foo' ],
559 [ 'Template:Foo////Bar', 'Foo' ],
564 * @todo Handle $wgNamespacesWithSubpages cases
565 * @dataProvider provideSubpageTitleCases
566 * @covers Title::getSubpageText
568 public function testGetSubpageText( $title, $expected, $msg = '' ) {
569 $title = Title
::newFromText( $title );
570 $this->assertEquals( $expected,
571 $title->getSubpageText(),
576 public static function provideSubpageTitleCases() {
578 # Title, expected base, optional message
579 [ 'User:John_Doe/subOne/subTwo', 'subTwo' ],
580 [ 'User:John_Doe/subOne', 'subOne' ],
584 public static function provideNewFromTitleValue() {
586 [ new TitleValue( NS_MAIN
, 'Foo' ) ],
587 [ new TitleValue( NS_MAIN
, 'Foo', 'bar' ) ],
588 [ new TitleValue( NS_USER
, 'Hansi_Maier' ) ],
593 * @covers Title::newFromTitleValue
594 * @dataProvider provideNewFromTitleValue
596 public function testNewFromTitleValue( TitleValue
$value ) {
597 $title = Title
::newFromTitleValue( $value );
599 $dbkey = str_replace( ' ', '_', $value->getText() );
600 $this->assertEquals( $dbkey, $title->getDBkey() );
601 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
602 $this->assertEquals( $value->getFragment(), $title->getFragment() );
606 * @covers Title::newFromLinkTarget
607 * @dataProvider provideNewFromTitleValue
609 public function testNewFromLinkTarget( LinkTarget
$value ) {
610 $title = Title
::newFromLinkTarget( $value );
612 $dbkey = str_replace( ' ', '_', $value->getText() );
613 $this->assertEquals( $dbkey, $title->getDBkey() );
614 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
615 $this->assertEquals( $value->getFragment(), $title->getFragment() );
619 * @covers Title::newFromLinkTarget
621 public function testNewFromLinkTarget_clone() {
622 $title = Title
::newFromText( __METHOD__
);
623 $this->assertSame( $title, Title
::newFromLinkTarget( $title ) );
625 // The Title::NEW_CLONE flag should ensure that a fresh instance is returned.
626 $clone = Title
::newFromLinkTarget( $title, Title
::NEW_CLONE
);
627 $this->assertNotSame( $title, $clone );
628 $this->assertTrue( $clone->equals( $title ) );
631 public function provideCastFromLinkTarget() {
632 return array_merge( [ [ null ] ], self
::provideNewFromTitleValue() );
636 * @covers Title::castFromLinkTarget
637 * @dataProvider provideCastFromLinkTarget
639 public function testCastFromLinkTarget( $value ) {
640 $title = Title
::castFromLinkTarget( $value );
642 if ( $value === null ) {
643 $this->assertNull( $title );
645 $dbkey = str_replace( ' ', '_', $value->getText() );
646 $this->assertSame( $dbkey, $title->getDBkey() );
647 $this->assertSame( $value->getNamespace(), $title->getNamespace() );
648 $this->assertSame( $value->getFragment(), $title->getFragment() );
652 public static function provideGetTitleValue() {
656 [ 'User:Hansi_Maier' ],
661 * @covers Title::getTitleValue
662 * @dataProvider provideGetTitleValue
664 public function testGetTitleValue( $text ) {
665 $title = Title
::newFromText( $text );
666 $value = $title->getTitleValue();
668 $dbkey = str_replace( ' ', '_', $value->getText() );
669 $this->assertEquals( $title->getDBkey(), $dbkey );
670 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
671 $this->assertEquals( $title->getFragment(), $value->getFragment() );
674 public static function provideGetFragment() {
677 [ 'Foo#bar', 'bar' ],
678 [ 'Foo#bär', 'bär' ],
680 // Inner whitespace is normalized
681 [ 'Foo#bar_bar', 'bar bar' ],
682 [ 'Foo#bar bar', 'bar bar' ],
683 [ 'Foo#bar bar', 'bar bar' ],
685 // Leading whitespace is kept, trailing whitespace is trimmed.
686 // XXX: Is this really want we want?
687 [ 'Foo#_bar_bar_', ' bar bar' ],
688 [ 'Foo# bar bar ', ' bar bar' ],
693 * @covers Title::getFragment
694 * @dataProvider provideGetFragment
696 * @param string $full
697 * @param string $fragment
699 public function testGetFragment( $full, $fragment ) {
700 $title = Title
::newFromText( $full );
701 $this->assertEquals( $fragment, $title->getFragment() );
705 * @covers Title::isAlwaysKnown
706 * @dataProvider provideIsAlwaysKnown
707 * @param string $page
708 * @param bool $isKnown
710 public function testIsAlwaysKnown( $page, $isKnown ) {
711 $title = Title
::newFromText( $page );
712 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
715 public static function provideIsAlwaysKnown() {
717 [ 'Some nonexistent page', false ],
720 [ 'Special:BlankPage', true ],
721 [ 'Special:SomeNonexistentSpecialPage', false ],
722 [ 'MediaWiki:Parentheses', true ],
723 [ 'MediaWiki:Some nonexistent message', false ],
728 * @covers Title::isValid
729 * @dataProvider provideIsValid
730 * @param Title $title
731 * @param bool $isValid
733 public function testIsValid( Title
$title, $isValid ) {
734 $this->assertEquals( $isValid, $title->isValid(), $title->getPrefixedText() );
737 public static function provideIsValid() {
739 [ Title
::makeTitle( NS_MAIN
, '' ), false ],
740 [ Title
::makeTitle( NS_MAIN
, '<>' ), false ],
741 [ Title
::makeTitle( NS_MAIN
, '|' ), false ],
742 [ Title
::makeTitle( NS_MAIN
, '#' ), false ],
743 [ Title
::makeTitle( NS_MAIN
, 'Test' ), true ],
744 [ Title
::makeTitle( NS_MAIN
, ' Test' ), false ],
745 [ Title
::makeTitle( NS_MAIN
, '_Test' ), false ],
746 [ Title
::makeTitle( NS_MAIN
, 'Test ' ), false ],
747 [ Title
::makeTitle( NS_MAIN
, 'Test_' ), false ],
748 [ Title
::makeTitle( NS_MAIN
, "Test\nthis" ), false ],
749 [ Title
::makeTitle( NS_MAIN
, "Test\tthis" ), false ],
750 [ Title
::makeTitle( -33, 'Test' ), false ],
751 [ Title
::makeTitle( 77663399, 'Test' ), false ],
756 * @covers Title::isAlwaysKnown
758 public function testIsAlwaysKnownOnInterwiki() {
759 $title = Title
::makeTitle( NS_MAIN
, 'Interwiki link', '', 'externalwiki' );
760 $this->assertTrue( $title->isAlwaysKnown() );
764 * @covers Title::exists
766 public function testExists() {
767 $title = Title
::makeTitle( NS_PROJECT
, 'New page' );
768 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
770 $article = new Article( $title );
771 $page = $article->getPage();
772 $page->doEditContent( new WikitextContent( 'Some [[link]]' ), 'summary' );
774 // Tell Title it doesn't know whether it exists
775 $title->mArticleID
= -1;
777 // Tell the link cache it doesn't exists when it really does
778 $linkCache->clearLink( $title );
779 $linkCache->addBadLinkObj( $title );
784 'exists() should rely on link cache unless GAID_FOR_UPDATE is used'
788 $title->exists( Title
::GAID_FOR_UPDATE
),
789 'exists() should re-query database when GAID_FOR_UPDATE is used'
793 public function provideCanHaveTalkPage() {
795 'User page has talk page' => [
796 Title
::makeTitle( NS_USER
, 'Jane' ), true
798 'Talke page has talk page' => [
799 Title
::makeTitle( NS_TALK
, 'Foo' ), true
801 'Special page cannot have talk page' => [
802 Title
::makeTitle( NS_SPECIAL
, 'Thing' ), false
804 'Virtual namespace cannot have talk page' => [
805 Title
::makeTitle( NS_MEDIA
, 'Kitten.jpg' ), false
807 'Relative link has no talk page' => [
808 Title
::makeTitle( NS_MAIN
, '', 'Kittens' ), false
810 'Interwiki link has no talk page' => [
811 Title
::makeTitle( NS_MAIN
, 'Kittens', '', 'acme' ), false
816 public function provideIsWatchable() {
818 'User page is watchable' => [
819 Title
::makeTitle( NS_USER
, 'Jane' ), true
821 'Talke page is watchable' => [
822 Title
::makeTitle( NS_TALK
, 'Foo' ), true
824 'Special page is not watchable' => [
825 Title
::makeTitle( NS_SPECIAL
, 'Thing' ), false
827 'Virtual namespace is not watchable' => [
828 Title
::makeTitle( NS_MEDIA
, 'Kitten.jpg' ), false
830 'Relative link is not watchable' => [
831 Title
::makeTitle( NS_MAIN
, '', 'Kittens' ), false
833 'Interwiki link is not watchable' => [
834 Title
::makeTitle( NS_MAIN
, 'Kittens', '', 'acme' ), false
839 public static function provideGetTalkPage_good() {
841 [ Title
::makeTitle( NS_MAIN
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
842 [ Title
::makeTitle( NS_TALK
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
846 public static function provideGetTalkPage_bad() {
848 [ Title
::makeTitle( NS_SPECIAL
, 'Test' ) ],
849 [ Title
::makeTitle( NS_MEDIA
, 'Test' ) ],
850 [ Title
::makeTitle( NS_MAIN
, '', 'Kittens' ) ],
851 [ Title
::makeTitle( NS_MAIN
, 'Kittens', '', 'acme' ) ],
855 public static function provideGetSubjectPage_good() {
857 [ Title
::makeTitle( NS_TALK
, 'Test' ), Title
::makeTitle( NS_MAIN
, 'Test' ) ],
858 [ Title
::makeTitle( NS_MAIN
, 'Test' ), Title
::makeTitle( NS_MAIN
, 'Test' ) ],
862 public static function provideGetOtherPage_good() {
864 [ Title
::makeTitle( NS_MAIN
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
865 [ Title
::makeTitle( NS_TALK
, 'Test' ), Title
::makeTitle( NS_MAIN
, 'Test' ) ],
870 * @dataProvider provideCanHaveTalkPage
871 * @covers Title::canHaveTalkPage
873 * @param Title $title
874 * @param bool $expected
876 public function testCanHaveTalkPage( Title
$title, $expected ) {
877 $actual = $title->canHaveTalkPage();
878 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
882 * @dataProvider provideIsWatchable
883 * @covers Title::isWatchable
885 * @param Title $title
886 * @param bool $expected
888 public function testIsWatchable( Title
$title, $expected ) {
889 $actual = $title->canHaveTalkPage();
890 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
894 * @dataProvider provideGetTalkPage_good
895 * @covers Title::getTalkPageIfDefined
897 public function testGetTalkPage_good( Title
$title, Title
$expected ) {
898 $actual = $title->getTalkPage();
899 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
903 * @dataProvider provideGetTalkPage_bad
904 * @covers Title::getTalkPageIfDefined
906 public function testGetTalkPage_bad( Title
$title ) {
907 $this->setExpectedException( MWException
::class );
908 $title->getTalkPage();
912 * @dataProvider provideGetTalkPage_good
913 * @covers Title::getTalkPageIfDefined
915 public function testGetTalkPageIfDefined_good( Title
$title, Title
$expected ) {
916 $actual = $title->getTalkPageIfDefined();
917 $this->assertNotNull( $actual, $title->getPrefixedDBkey() );
918 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
922 * @dataProvider provideGetTalkPage_bad
923 * @covers Title::getTalkPageIfDefined
925 public function testGetTalkPageIfDefined_bad( Title
$title ) {
926 $talk = $title->getTalkPageIfDefined();
929 $title->getPrefixedDBkey()
934 * @dataProvider provideGetSubjectPage_good
935 * @covers Title::getSubjectPage
937 public function testGetSubjectPage_good( Title
$title, Title
$expected ) {
938 $actual = $title->getSubjectPage();
939 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
943 * @dataProvider provideGetOtherPage_good
944 * @covers Title::getOtherPage
946 public function testGetOtherPage_good( Title
$title, Title
$expected ) {
947 $actual = $title->getOtherPage();
948 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
952 * @dataProvider provideGetTalkPage_bad
953 * @covers Title::getOtherPage
955 public function testGetOtherPage_bad( Title
$title ) {
956 $this->setExpectedException( MWException
::class );
957 $title->getOtherPage();
960 public function provideCreateFragmentTitle() {
962 [ Title
::makeTitle( NS_MAIN
, 'Test' ), 'foo' ],
963 [ Title
::makeTitle( NS_TALK
, 'Test', 'foo' ), '' ],
964 [ Title
::makeTitle( NS_CATEGORY
, 'Test', 'foo' ), 'bar' ],
965 [ Title
::makeTitle( NS_MAIN
, 'Test1', '', 'interwiki' ), 'baz' ]
970 * @covers Title::createFragmentTarget
971 * @dataProvider provideCreateFragmentTitle
973 public function testCreateFragmentTitle( Title
$title, $fragment ) {
974 $this->mergeMwGlobalArrayValue( 'wgHooks', [
975 'InterwikiLoadPrefix' => [
976 function ( $prefix, &$iwdata ) {
977 if ( $prefix === 'interwiki' ) {
979 'iw_url' => 'http://example.com/',
989 $fragmentTitle = $title->createFragmentTarget( $fragment );
991 $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
992 $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
993 $this->assertEquals( $title->getInterwiki(), $fragmentTitle->getInterwiki() );
994 $this->assertEquals( $fragment, $fragmentTitle->getFragment() );
997 public function provideGetPrefixedText() {
1001 Title
::makeTitle( NS_MAIN
, 'Foo bar' ),
1006 Title
::makeTitle( NS_USER
, 'Foo bar' ),
1011 Title
::makeTitle( NS_USER_TALK
, 'Foo bar' ),
1014 // fragment not included
1016 Title
::makeTitle( NS_MAIN
, 'Foo bar', 'fragment' ),
1021 Title
::makeTitle( NS_MEDIA
, 'Foo bar' ),
1024 // non-existent namespace
1026 Title
::makeTitle( 100777, 'Foo bar' ),
1027 'Special:Badtitle/NS100777:Foo bar'
1033 * @covers Title::getPrefixedText
1034 * @dataProvider provideGetPrefixedText
1036 public function testGetPrefixedText( Title
$title, $expected ) {
1037 $this->assertEquals( $expected, $title->getPrefixedText() );
1040 public function provideGetPrefixedDBKey() {
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::getPrefixedDBKey
1077 * @dataProvider provideGetPrefixedDBKey
1079 public function testGetPrefixedDBKey( Title
$title, $expected ) {
1080 $this->assertEquals( $expected, $title->getPrefixedDBkey() );
1084 * @covers Title::getFragmentForURL
1085 * @dataProvider provideGetFragmentForURL
1087 * @param string $titleStr
1088 * @param string $expected
1090 public function testGetFragmentForURL( $titleStr, $expected ) {
1091 $this->setMwGlobals( [
1092 'wgFragmentMode' => [ 'html5' ],
1093 'wgExternalInterwikiFragmentMode' => 'legacy',
1095 $dbw = wfGetDB( DB_MASTER
);
1096 $dbw->insert( 'interwiki',
1099 'iw_prefix' => 'de',
1100 'iw_url' => 'http://de.wikipedia.org/wiki/',
1101 'iw_api' => 'http://de.wikipedia.org/w/api.php',
1102 'iw_wikiid' => 'dewiki',
1107 'iw_prefix' => 'zz',
1108 'iw_url' => 'http://zzwiki.org/wiki/',
1109 'iw_api' => 'http://zzwiki.org/w/api.php',
1110 'iw_wikiid' => 'zzwiki',
1119 $title = Title
::newFromText( $titleStr );
1120 self
::assertEquals( $expected, $title->getFragmentForURL() );
1122 $dbw->delete( 'interwiki', '*', __METHOD__
);
1125 public function provideGetFragmentForURL() {
1128 [ 'Foo#ümlåût', '#ümlåût' ],
1129 [ 'de:Foo#Bå®', '#Bå®' ],
1130 [ 'zz:Foo#тест', '#.D1.82.D0.B5.D1.81.D1.82' ],
1135 * @covers Title::isRawHtmlMessage
1136 * @dataProvider provideIsRawHtmlMessage
1138 public function testIsRawHtmlMessage( $textForm, $expected ) {
1139 $this->setMwGlobals( 'wgRawHtmlMessages', [
1145 $title = Title
::newFromText( $textForm );
1146 $this->assertSame( $expected, $title->isRawHtmlMessage() );
1149 public function provideIsRawHtmlMessage() {
1151 [ 'MediaWiki:Foobar', true ],
1152 [ 'MediaWiki:Foo bar', true ],
1153 [ 'MediaWiki:Foo-bar', true ],
1154 [ 'MediaWiki:foo bar', true ],
1155 [ 'MediaWiki:foo-bar', true ],
1156 [ 'MediaWiki:foobar', true ],
1157 [ 'MediaWiki:some-other-message', false ],
1158 [ 'Main Page', false ],
1162 public function provideEquals() {
1164 Title
::newFromText( 'Main Page' ),
1165 Title
::newFromText( 'Main Page' ),
1169 Title
::newFromText( 'Main Page' ),
1170 Title
::newFromText( 'Not The Main Page' ),
1174 Title
::newFromText( 'Main Page' ),
1175 Title
::newFromText( 'Project:Main Page' ),
1179 Title
::newFromText( 'File:Example.png' ),
1180 Title
::newFromText( 'Image:Example.png' ),
1184 Title
::newFromText( 'Special:Version' ),
1185 Title
::newFromText( 'Special:Version' ),
1189 Title
::newFromText( 'Special:Version' ),
1190 Title
::newFromText( 'Special:Recentchanges' ),
1194 Title
::newFromText( 'Special:Version' ),
1195 Title
::newFromText( 'Main Page' ),
1199 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1200 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1204 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1205 Title
::makeTitle( NS_MAIN
, 'Bar', '', '' ),
1209 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1210 Title
::makeTitle( NS_TALK
, 'Foo', '', '' ),
1214 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1215 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1219 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1220 Title
::makeTitle( NS_MAIN
, 'Foo', 'Baz', '' ),
1224 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1225 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1229 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1230 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1234 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1235 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1241 * @covers Title::equals
1242 * @dataProvider provideEquals
1244 public function testEquals( Title
$firstValue, /* LinkTarget */ $secondValue, $expectedSame ) {
1247 $firstValue->equals( $secondValue )
1252 * @covers Title::newMainPage
1254 public function testNewMainPage() {
1255 $msgCache = TestingAccessWrapper
::newFromClass( MessageCache
::class );
1256 $msgCache->instance
= $this->createMock( MessageCache
::class );
1257 $msgCache->instance
->method( 'get' )->willReturn( 'Foresheet' );
1258 $msgCache->instance
->method( 'transform' )->willReturn( 'Foresheet' );
1262 Title
::newMainPage()->getText()
1267 * @covers Title::newMainPage
1269 public function testNewMainPageWithLocal() {
1270 $local = $this->createMock( MessageLocalizer
::class );
1271 $local->method( 'msg' )->willReturn( new RawMessage( 'Prime Article' ) );
1275 Title
::newMainPage( $local )->getText()