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 TitleParser since we modified $wgLocalInterwikis
161 $this->setService( 'TitleParser', new MediaWikiTitleCodec(
162 Language
::factory( 'en' ),
169 * See also mediawiki.Title.test.js
170 * @covers Title::secureAndSplit
171 * @dataProvider provideValidSecureAndSplit
172 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
174 public function testSecureAndSplitValid( $text ) {
175 $this->secureAndSplitGlobals();
176 $this->assertInstanceOf( Title
::class, Title
::newFromText( $text ), "Valid: $text" );
180 * See also mediawiki.Title.test.js
181 * @covers Title::secureAndSplit
182 * @dataProvider provideInvalidSecureAndSplit
183 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
185 public function testSecureAndSplitInvalid( $text, $expectedErrorMessage ) {
186 $this->secureAndSplitGlobals();
188 Title
::newFromTextThrow( $text ); // should throw
189 $this->assertTrue( false, "Invalid: $text" );
190 } catch ( MalformedTitleException
$ex ) {
191 $this->assertEquals( $expectedErrorMessage, $ex->getErrorMessage(), "Invalid: $text" );
195 public static function provideConvertByteClassToUnicodeClass() {
198 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
199 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
203 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
206 'QWERTY\\x66-\\xFD+',
207 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
215 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
218 'QWERTY\\x66-\\x80+\\x23',
219 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
222 'QWERTY\\x66-\\x80+\\xD3',
223 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
227 '\\\\\\u0080-\\uFFFF',
231 '\\-\\u0080-\\uFFFF',
235 'QWERTY\\-\\u0080-\\uFFFF',
243 'A-\\x7F\\u0080-\\uFFFF',
246 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
247 'f-wQWERTYFXZ\\u0080-\\uFFFF',
250 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
251 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
257 * @dataProvider provideConvertByteClassToUnicodeClass
258 * @covers Title::convertByteClassToUnicodeClass
260 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
261 $this->assertEquals( $unicodeClass, Title
::convertByteClassToUnicodeClass( $byteClass ) );
265 * @dataProvider provideSpecialNamesWithAndWithoutParameter
266 * @covers Title::fixSpecialName
268 public function testFixSpecialNameRetainsParameter( $text, $expectedParam ) {
269 $title = Title
::newFromText( $text );
270 $fixed = $title->fixSpecialName();
271 $stuff = explode( '/', $fixed->getDBkey(), 2 );
272 if ( count( $stuff ) == 2 ) {
280 "T33100 regression check: Title->fixSpecialName() should preserve parameter"
284 public static function provideSpecialNamesWithAndWithoutParameter() {
286 [ 'Special:Version', null ],
287 [ 'Special:Version/', '' ],
288 [ 'Special:Version/param', 'param' ],
293 * Auth-less test of Title::isValidMoveOperation
295 * @param string $source
296 * @param string $target
297 * @param array|string|bool $expected Required error
298 * @dataProvider provideTestIsValidMoveOperation
299 * @covers Title::isValidMoveOperation
301 public function testIsValidMoveOperation( $source, $target, $expected ) {
302 $this->setMwGlobals( 'wgContentHandlerUseDB', false );
303 $title = Title
::newFromText( $source );
304 $nt = Title
::newFromText( $target );
305 $errors = $title->isValidMoveOperation( $nt, false );
306 if ( $expected === true ) {
307 $this->assertTrue( $errors );
309 $errors = $this->flattenErrorsArray( $errors );
310 foreach ( (array)$expected as $error ) {
311 $this->assertContains( $error, $errors );
316 public static function provideTestIsValidMoveOperation() {
318 // for Title::isValidMoveOperation
319 [ 'Some page', '', 'badtitletext' ],
320 [ 'Test', 'Test', 'selfmove' ],
321 [ 'Special:FooBar', 'Test', 'immobile-source-namespace' ],
322 [ 'Test', 'Special:FooBar', 'immobile-target-namespace' ],
323 [ 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ],
324 [ 'Page', 'File:Test.jpg', 'nonfile-cannot-move-to-file' ],
325 [ 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ],
330 * Auth-less test of Title::userCan
332 * @param array $whitelistRegexp
333 * @param string $source
334 * @param string $action
335 * @param array|string|bool $expected Required error
337 * @covers \Mediawiki\Permissions\PermissionManager::checkReadPermissions
338 * @dataProvider dataWgWhitelistReadRegexp
340 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
341 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
342 // usually have only one regex, it is more concise to write the lonely regex
343 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
345 if ( is_string( $whitelistRegexp ) ) {
346 $whitelistRegexp = [ $whitelistRegexp ];
349 $this->setMwGlobals( [
350 // So User::isEveryoneAllowed( 'read' ) === false
351 'wgGroupPermissions' => [ '*' => [ 'read' => false ] ],
352 'wgWhitelistRead' => [ 'some random non sense title' ],
353 'wgWhitelistReadRegexp' => $whitelistRegexp,
356 $title = Title
::newFromDBkey( $source );
358 // New anonymous user with no rights
361 $errors = $title->userCan( $action, $user );
363 if ( is_bool( $expected ) ) {
364 # Forge the assertion message depending on the assertion expectation
365 $allowableness = $expected
366 ?
" should be allowed"
367 : " should NOT be allowed";
371 "User action '$action' on [[$source]] $allowableness."
374 $errors = $this->flattenErrorsArray( $errors );
375 foreach ( (array)$expected as $error ) {
376 $this->assertContains( $error, $errors );
382 * Provides test parameter values for testWgWhitelistReadRegexp()
384 public function dataWgWhitelistReadRegexp() {
389 // Everything, if this doesn't work, we're really in trouble
390 [ '/.*/', 'Main_Page', 'read', $ALLOWED ],
391 [ '/.*/', 'Main_Page', 'edit', $DISALLOWED ],
393 // We validate against the title name, not the db key
394 [ '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ],
396 [ '/^Main/', 'Main_Page', 'read', $ALLOWED ],
397 [ '/^Main.*/', 'Main_Page', 'read', $ALLOWED ],
399 [ '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ],
401 // ...without unicode modifier
402 [ '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ],
403 // ...with unicode modifier
404 [ '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ],
406 [ '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ],
407 [ '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ],
409 // From DefaultSettings.php:
410 [ "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ],
411 [ "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ],
414 [ '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ],
415 [ null, 'Special:Newpages', 'read', $DISALLOWED ],
420 public function flattenErrorsArray( $errors ) {
422 foreach ( $errors as $error ) {
423 $result[] = $error[0];
430 * @dataProvider provideGetPageViewLanguage
431 * @covers Title::getPageViewLanguage
433 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
434 $lang, $variant, $msg = ''
436 // Setup environnement for this test
437 $this->setMwGlobals( [
438 'wgDefaultLanguageVariant' => $variant,
439 'wgAllowUserJs' => true,
441 $this->setUserLang( $lang );
442 $this->setContentLang( $contLang );
444 $title = Title
::newFromText( $titleText );
445 $this->assertInstanceOf( Title
::class, $title,
446 "Test must be passed a valid title text, you gave '$titleText'"
448 $this->assertEquals( $expected,
449 $title->getPageViewLanguage()->getCode(),
454 public static function provideGetPageViewLanguage() {
458 # - content language (expected in most cases)
459 # - wgLang (on some specific pages)
460 # - wgDefaultLanguageVariant
463 [ 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ],
464 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ],
465 [ 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ],
467 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ],
468 [ 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ],
469 [ 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ],
470 [ 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ],
471 [ 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ],
472 [ 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ],
473 [ 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ],
474 [ 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ],
476 [ 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ],
477 [ 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ],
478 [ 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ],
479 [ 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ],
480 [ 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ],
481 [ 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ],
482 [ 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ],
483 [ 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ],
484 [ 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ],
485 [ 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ],
487 [ 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ],
488 [ 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ],
494 * @dataProvider provideBaseTitleCases
495 * @covers Title::getBaseText
497 public function testGetBaseText( $title, $expected, $msg = '' ) {
498 $title = Title
::newFromText( $title );
499 $this->assertEquals( $expected,
500 $title->getBaseText(),
505 public static function provideBaseTitleCases() {
507 # Title, expected base, optional message
508 [ 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ],
509 [ 'User:Foo/Bar/Baz', 'Foo/Bar' ],
514 * @dataProvider provideRootTitleCases
515 * @covers Title::getRootText
517 public function testGetRootText( $title, $expected, $msg = '' ) {
518 $title = Title
::newFromText( $title );
519 $this->assertEquals( $expected,
520 $title->getRootText(),
525 public static function provideRootTitleCases() {
527 # Title, expected base, optional message
528 [ 'User:John_Doe/subOne/subTwo', 'John Doe' ],
529 [ 'User:Foo/Bar/Baz', 'Foo' ],
534 * @todo Handle $wgNamespacesWithSubpages cases
535 * @dataProvider provideSubpageTitleCases
536 * @covers Title::getSubpageText
538 public function testGetSubpageText( $title, $expected, $msg = '' ) {
539 $title = Title
::newFromText( $title );
540 $this->assertEquals( $expected,
541 $title->getSubpageText(),
546 public static function provideSubpageTitleCases() {
548 # Title, expected base, optional message
549 [ 'User:John_Doe/subOne/subTwo', 'subTwo' ],
550 [ 'User:John_Doe/subOne', 'subOne' ],
554 public static function provideNewFromTitleValue() {
556 [ new TitleValue( NS_MAIN
, 'Foo' ) ],
557 [ new TitleValue( NS_MAIN
, 'Foo', 'bar' ) ],
558 [ new TitleValue( NS_USER
, 'Hansi_Maier' ) ],
563 * @covers Title::newFromTitleValue
564 * @dataProvider provideNewFromTitleValue
566 public function testNewFromTitleValue( TitleValue
$value ) {
567 $title = Title
::newFromTitleValue( $value );
569 $dbkey = str_replace( ' ', '_', $value->getText() );
570 $this->assertEquals( $dbkey, $title->getDBkey() );
571 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
572 $this->assertEquals( $value->getFragment(), $title->getFragment() );
576 * @covers Title::newFromLinkTarget
577 * @dataProvider provideNewFromTitleValue
579 public function testNewFromLinkTarget( LinkTarget
$value ) {
580 $title = Title
::newFromLinkTarget( $value );
582 $dbkey = str_replace( ' ', '_', $value->getText() );
583 $this->assertEquals( $dbkey, $title->getDBkey() );
584 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
585 $this->assertEquals( $value->getFragment(), $title->getFragment() );
589 * @covers Title::newFromLinkTarget
591 public function testNewFromLinkTarget_clone() {
592 $title = Title
::newFromText( __METHOD__
);
593 $this->assertSame( $title, Title
::newFromLinkTarget( $title ) );
595 // The Title::NEW_CLONE flag should ensure that a fresh instance is returned.
596 $clone = Title
::newFromLinkTarget( $title, Title
::NEW_CLONE
);
597 $this->assertNotSame( $title, $clone );
598 $this->assertTrue( $clone->equals( $title ) );
601 public static function provideGetTitleValue() {
605 [ 'User:Hansi_Maier' ],
610 * @covers Title::getTitleValue
611 * @dataProvider provideGetTitleValue
613 public function testGetTitleValue( $text ) {
614 $title = Title
::newFromText( $text );
615 $value = $title->getTitleValue();
617 $dbkey = str_replace( ' ', '_', $value->getText() );
618 $this->assertEquals( $title->getDBkey(), $dbkey );
619 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
620 $this->assertEquals( $title->getFragment(), $value->getFragment() );
623 public static function provideGetFragment() {
626 [ 'Foo#bar', 'bar' ],
627 [ 'Foo#bär', 'bär' ],
629 // Inner whitespace is normalized
630 [ 'Foo#bar_bar', 'bar bar' ],
631 [ 'Foo#bar bar', 'bar bar' ],
632 [ 'Foo#bar bar', 'bar bar' ],
634 // Leading whitespace is kept, trailing whitespace is trimmed.
635 // XXX: Is this really want we want?
636 [ 'Foo#_bar_bar_', ' bar bar' ],
637 [ 'Foo# bar bar ', ' bar bar' ],
642 * @covers Title::getFragment
643 * @dataProvider provideGetFragment
645 * @param string $full
646 * @param string $fragment
648 public function testGetFragment( $full, $fragment ) {
649 $title = Title
::newFromText( $full );
650 $this->assertEquals( $fragment, $title->getFragment() );
654 * @covers Title::isAlwaysKnown
655 * @dataProvider provideIsAlwaysKnown
656 * @param string $page
657 * @param bool $isKnown
659 public function testIsAlwaysKnown( $page, $isKnown ) {
660 $title = Title
::newFromText( $page );
661 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
664 public static function provideIsAlwaysKnown() {
666 [ 'Some nonexistent page', false ],
669 [ 'Special:BlankPage', true ],
670 [ 'Special:SomeNonexistentSpecialPage', false ],
671 [ 'MediaWiki:Parentheses', true ],
672 [ 'MediaWiki:Some nonexistent message', false ],
677 * @covers Title::isValid
678 * @dataProvider provideIsValid
679 * @param Title $title
680 * @param bool $isValid
682 public function testIsValid( Title
$title, $isValid ) {
683 $this->assertEquals( $isValid, $title->isValid(), $title->getPrefixedText() );
686 public static function provideIsValid() {
688 [ Title
::makeTitle( NS_MAIN
, '' ), false ],
689 [ Title
::makeTitle( NS_MAIN
, '<>' ), false ],
690 [ Title
::makeTitle( NS_MAIN
, '|' ), false ],
691 [ Title
::makeTitle( NS_MAIN
, '#' ), false ],
692 [ Title
::makeTitle( NS_MAIN
, 'Test' ), true ],
693 [ Title
::makeTitle( -33, 'Test' ), false ],
694 [ Title
::makeTitle( 77663399, 'Test' ), false ],
699 * @covers Title::isAlwaysKnown
701 public function testIsAlwaysKnownOnInterwiki() {
702 $title = Title
::makeTitle( NS_MAIN
, 'Interwiki link', '', 'externalwiki' );
703 $this->assertTrue( $title->isAlwaysKnown() );
707 * @covers Title::exists
709 public function testExists() {
710 $title = Title
::makeTitle( NS_PROJECT
, 'New page' );
711 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
713 $article = new Article( $title );
714 $page = $article->getPage();
715 $page->doEditContent( new WikitextContent( 'Some [[link]]' ), 'summary' );
717 // Tell Title it doesn't know whether it exists
718 $title->mArticleID
= -1;
720 // Tell the link cache it doesn't exists when it really does
721 $linkCache->clearLink( $title );
722 $linkCache->addBadLinkObj( $title );
727 'exists() should rely on link cache unless GAID_FOR_UPDATE is used'
731 $title->exists( Title
::GAID_FOR_UPDATE
),
732 'exists() should re-query database when GAID_FOR_UPDATE is used'
736 public function provideCanHaveTalkPage() {
738 'User page has talk page' => [
739 Title
::makeTitle( NS_USER
, 'Jane' ), true
741 'Talke page has talk page' => [
742 Title
::makeTitle( NS_TALK
, 'Foo' ), true
744 'Special page cannot have talk page' => [
745 Title
::makeTitle( NS_SPECIAL
, 'Thing' ), false
747 'Virtual namespace cannot have talk page' => [
748 Title
::makeTitle( NS_MEDIA
, 'Kitten.jpg' ), false
754 * @dataProvider provideCanHaveTalkPage
755 * @covers Title::canHaveTalkPage
757 * @param Title $title
758 * @param bool $expected
760 public function testCanHaveTalkPage( Title
$title, $expected ) {
761 $actual = $title->canHaveTalkPage();
762 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
765 public static function provideGetTalkPage_good() {
767 [ Title
::makeTitle( NS_MAIN
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
768 [ Title
::makeTitle( NS_TALK
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
773 * @dataProvider provideGetTalkPage_good
774 * @covers Title::getTalkPage
776 public function testGetTalkPage_good( Title
$title, Title
$expected ) {
777 $talk = $title->getTalkPage();
779 $expected->getPrefixedDBKey(),
780 $talk->getPrefixedDBKey(),
781 $title->getPrefixedDBKey()
786 * @dataProvider provideGetTalkPage_good
787 * @covers Title::getTalkPageIfDefined
789 public function testGetTalkPageIfDefined_good( Title
$title ) {
790 $talk = $title->getTalkPageIfDefined();
791 $this->assertInstanceOf(
794 $title->getPrefixedDBKey()
798 public static function provideGetTalkPage_bad() {
800 [ Title
::makeTitle( NS_SPECIAL
, 'Test' ) ],
801 [ Title
::makeTitle( NS_MEDIA
, 'Test' ) ],
806 * @dataProvider provideGetTalkPage_bad
807 * @covers Title::getTalkPageIfDefined
809 public function testGetTalkPageIfDefined_bad( Title
$title ) {
810 $talk = $title->getTalkPageIfDefined();
813 $title->getPrefixedDBKey()
817 public function provideCreateFragmentTitle() {
819 [ Title
::makeTitle( NS_MAIN
, 'Test' ), 'foo' ],
820 [ Title
::makeTitle( NS_TALK
, 'Test', 'foo' ), '' ],
821 [ Title
::makeTitle( NS_CATEGORY
, 'Test', 'foo' ), 'bar' ],
822 [ Title
::makeTitle( NS_MAIN
, 'Test1', '', 'interwiki' ), 'baz' ]
827 * @covers Title::createFragmentTarget
828 * @dataProvider provideCreateFragmentTitle
830 public function testCreateFragmentTitle( Title
$title, $fragment ) {
831 $this->mergeMwGlobalArrayValue( 'wgHooks', [
832 'InterwikiLoadPrefix' => [
833 function ( $prefix, &$iwdata ) {
834 if ( $prefix === 'interwiki' ) {
836 'iw_url' => 'http://example.com/',
846 $fragmentTitle = $title->createFragmentTarget( $fragment );
848 $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
849 $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
850 $this->assertEquals( $title->getInterwiki(), $fragmentTitle->getInterwiki() );
851 $this->assertEquals( $fragment, $fragmentTitle->getFragment() );
854 public function provideGetPrefixedText() {
858 Title
::makeTitle( NS_MAIN
, 'Foo bar' ),
863 Title
::makeTitle( NS_USER
, 'Foo bar' ),
868 Title
::makeTitle( NS_USER_TALK
, 'Foo bar' ),
871 // fragment not included
873 Title
::makeTitle( NS_MAIN
, 'Foo bar', 'fragment' ),
878 Title
::makeTitle( NS_MEDIA
, 'Foo bar' ),
881 // non-existent namespace
883 Title
::makeTitle( 100777, 'Foo bar' ),
884 'Special:Badtitle/NS100777:Foo bar'
890 * @covers Title::getPrefixedText
891 * @dataProvider provideGetPrefixedText
893 public function testGetPrefixedText( Title
$title, $expected ) {
894 $this->assertEquals( $expected, $title->getPrefixedText() );
897 public function provideGetPrefixedDBKey() {
901 Title
::makeTitle( NS_MAIN
, 'Foo_bar' ),
906 Title
::makeTitle( NS_USER
, 'Foo_bar' ),
911 Title
::makeTitle( NS_USER_TALK
, 'Foo_bar' ),
914 // fragment not included
916 Title
::makeTitle( NS_MAIN
, 'Foo_bar', 'fragment' ),
921 Title
::makeTitle( NS_MEDIA
, 'Foo_bar' ),
924 // non-existent namespace
926 Title
::makeTitle( 100777, 'Foo_bar' ),
927 'Special:Badtitle/NS100777:Foo_bar'
933 * @covers Title::getPrefixedDBKey
934 * @dataProvider provideGetPrefixedDBKey
936 public function testGetPrefixedDBKey( Title
$title, $expected ) {
937 $this->assertEquals( $expected, $title->getPrefixedDBkey() );
941 * @covers Title::getFragmentForURL
942 * @dataProvider provideGetFragmentForURL
944 * @param string $titleStr
945 * @param string $expected
947 public function testGetFragmentForURL( $titleStr, $expected ) {
948 $this->setMwGlobals( [
949 'wgFragmentMode' => [ 'html5' ],
950 'wgExternalInterwikiFragmentMode' => 'legacy',
952 $dbw = wfGetDB( DB_MASTER
);
953 $dbw->insert( 'interwiki',
957 'iw_url' => 'http://de.wikipedia.org/wiki/',
958 'iw_api' => 'http://de.wikipedia.org/w/api.php',
959 'iw_wikiid' => 'dewiki',
965 'iw_url' => 'http://zzwiki.org/wiki/',
966 'iw_api' => 'http://zzwiki.org/w/api.php',
967 'iw_wikiid' => 'zzwiki',
976 $title = Title
::newFromText( $titleStr );
977 self
::assertEquals( $expected, $title->getFragmentForURL() );
979 $dbw->delete( 'interwiki', '*', __METHOD__
);
982 public function provideGetFragmentForURL() {
985 [ 'Foo#ümlåût', '#ümlåût' ],
986 [ 'de:Foo#Bå®', '#Bå®' ],
987 [ 'zz:Foo#тест', '#.D1.82.D0.B5.D1.81.D1.82' ],
992 * @covers Title::isRawHtmlMessage
993 * @dataProvider provideIsRawHtmlMessage
995 public function testIsRawHtmlMessage( $textForm, $expected ) {
996 $this->setMwGlobals( 'wgRawHtmlMessages', [
1002 $title = Title
::newFromText( $textForm );
1003 $this->assertSame( $expected, $title->isRawHtmlMessage() );
1006 public function provideIsRawHtmlMessage() {
1008 [ 'MediaWiki:Foobar', true ],
1009 [ 'MediaWiki:Foo bar', true ],
1010 [ 'MediaWiki:Foo-bar', true ],
1011 [ 'MediaWiki:foo bar', true ],
1012 [ 'MediaWiki:foo-bar', true ],
1013 [ 'MediaWiki:foobar', true ],
1014 [ 'MediaWiki:some-other-message', false ],
1015 [ 'Main Page', false ],
1019 public function provideEquals() {
1021 Title
::newFromText( 'Main Page' ),
1022 Title
::newFromText( 'Main Page' ),
1026 Title
::newFromText( 'Main Page' ),
1027 Title
::newFromText( 'Not The Main Page' ),
1031 Title
::newFromText( 'Main Page' ),
1032 Title
::newFromText( 'Project:Main Page' ),
1036 Title
::newFromText( 'File:Example.png' ),
1037 Title
::newFromText( 'Image:Example.png' ),
1041 Title
::newFromText( 'Special:Version' ),
1042 Title
::newFromText( 'Special:Version' ),
1046 Title
::newFromText( 'Special:Version' ),
1047 Title
::newFromText( 'Special:Recentchanges' ),
1051 Title
::newFromText( 'Special:Version' ),
1052 Title
::newFromText( 'Main Page' ),
1056 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1057 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1061 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1062 Title
::makeTitle( NS_MAIN
, 'Bar', '', '' ),
1066 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1067 Title
::makeTitle( NS_TALK
, 'Foo', '', '' ),
1071 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1072 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1076 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1077 Title
::makeTitle( NS_MAIN
, 'Foo', 'Baz', '' ),
1081 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1082 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1086 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1087 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1091 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1092 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1098 * @covers Title::equals
1099 * @dataProvider provideEquals
1101 public function testEquals( Title
$firstValue, /* LinkTarget */ $secondValue, $expectedSame ) {
1104 $firstValue->equals( $secondValue )
1109 * @covers Title::newMainPage
1111 public function testNewMainPage() {
1112 $msgCache = TestingAccessWrapper
::newFromClass( MessageCache
::class );
1113 $msgCache->instance
= $this->createMock( MessageCache
::class );
1114 $msgCache->instance
->method( 'get' )->willReturn( 'Foresheet' );
1115 $msgCache->instance
->method( 'transform' )->willReturn( 'Foresheet' );
1119 Title
::newMainPage()->getText()
1124 * @covers Title::newMainPage
1126 public function testNewMainPageWithLocal() {
1127 $local = $this->createMock( MessageLocalizer
::class );
1128 $local->method( 'msg' )->willReturn( new RawMessage( 'Prime Article' ) );
1132 Title
::newMainPage( $local )->getText()