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->setMwGlobals( 'wgContentHandlerUseDB', false );
299 $title = Title
::newFromText( $source );
300 $nt = Title
::newFromText( $target );
301 $errors = $title->isValidMoveOperation( $nt, false );
302 if ( $expected === true ) {
303 $this->assertTrue( $errors );
305 $errors = $this->flattenErrorsArray( $errors );
306 foreach ( (array)$expected as $error ) {
307 $this->assertContains( $error, $errors );
312 public static function provideTestIsValidMoveOperation() {
314 // for Title::isValidMoveOperation
315 [ 'Some page', '', 'badtitletext' ],
316 [ 'Test', 'Test', 'selfmove' ],
317 [ 'Special:FooBar', 'Test', 'immobile-source-namespace' ],
318 [ 'Test', 'Special:FooBar', 'immobile-target-namespace' ],
319 [ 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ],
320 [ 'Page', 'File:Test.jpg', 'nonfile-cannot-move-to-file' ],
321 [ 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ],
326 * Auth-less test of Title::userCan
328 * @param array $whitelistRegexp
329 * @param string $source
330 * @param string $action
331 * @param array|string|bool $expected Required error
333 * @covers \Mediawiki\Permissions\PermissionManager::checkReadPermissions
334 * @dataProvider dataWgWhitelistReadRegexp
336 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
337 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
338 // usually have only one regex, it is more concise to write the lonely regex
339 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
341 if ( is_string( $whitelistRegexp ) ) {
342 $whitelistRegexp = [ $whitelistRegexp ];
345 $this->setMwGlobals( [
346 // So User::isEveryoneAllowed( 'read' ) === false
347 'wgGroupPermissions' => [ '*' => [ 'read' => false ] ],
348 'wgWhitelistRead' => [ 'some random non sense title' ],
349 'wgWhitelistReadRegexp' => $whitelistRegexp,
352 $title = Title
::newFromDBkey( $source );
354 // New anonymous user with no rights
357 $errors = $title->userCan( $action, $user );
359 if ( is_bool( $expected ) ) {
360 # Forge the assertion message depending on the assertion expectation
361 $allowableness = $expected
362 ?
" should be allowed"
363 : " should NOT be allowed";
367 "User action '$action' on [[$source]] $allowableness."
370 $errors = $this->flattenErrorsArray( $errors );
371 foreach ( (array)$expected as $error ) {
372 $this->assertContains( $error, $errors );
378 * Provides test parameter values for testWgWhitelistReadRegexp()
380 public function dataWgWhitelistReadRegexp() {
385 // Everything, if this doesn't work, we're really in trouble
386 [ '/.*/', 'Main_Page', 'read', $ALLOWED ],
387 [ '/.*/', 'Main_Page', 'edit', $DISALLOWED ],
389 // We validate against the title name, not the db key
390 [ '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ],
392 [ '/^Main/', 'Main_Page', 'read', $ALLOWED ],
393 [ '/^Main.*/', 'Main_Page', 'read', $ALLOWED ],
395 [ '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ],
397 // ...without unicode modifier
398 [ '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ],
399 // ...with unicode modifier
400 [ '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ],
402 [ '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ],
403 [ '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ],
405 // From DefaultSettings.php:
406 [ "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ],
407 [ "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ],
410 [ '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ],
411 [ null, 'Special:Newpages', 'read', $DISALLOWED ],
416 public function flattenErrorsArray( $errors ) {
418 foreach ( $errors as $error ) {
419 $result[] = $error[0];
426 * @dataProvider provideGetPageViewLanguage
427 * @covers Title::getPageViewLanguage
429 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
430 $lang, $variant, $msg = ''
432 // Setup environnement for this test
433 $this->setMwGlobals( [
434 'wgDefaultLanguageVariant' => $variant,
435 'wgAllowUserJs' => true,
437 $this->setUserLang( $lang );
438 $this->setContentLang( $contLang );
440 $title = Title
::newFromText( $titleText );
441 $this->assertInstanceOf( Title
::class, $title,
442 "Test must be passed a valid title text, you gave '$titleText'"
444 $this->assertEquals( $expected,
445 $title->getPageViewLanguage()->getCode(),
450 public static function provideGetPageViewLanguage() {
454 # - content language (expected in most cases)
455 # - wgLang (on some specific pages)
456 # - wgDefaultLanguageVariant
459 [ 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ],
460 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ],
461 [ 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ],
463 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ],
464 [ 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ],
465 [ 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ],
466 [ 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ],
467 [ 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ],
468 [ 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ],
469 [ 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ],
470 [ 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ],
472 [ 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ],
473 [ 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ],
474 [ 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ],
475 [ 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ],
476 [ 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ],
477 [ 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ],
478 [ 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ],
479 [ 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ],
480 [ 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ],
481 [ 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ],
483 [ 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ],
484 [ 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ],
490 * @dataProvider provideBaseTitleCases
491 * @covers Title::getBaseText
493 public function testGetBaseText( $title, $expected, $msg = '' ) {
494 $title = Title
::newFromText( $title );
495 $this->assertEquals( $expected,
496 $title->getBaseText(),
501 public static function provideBaseTitleCases() {
503 # Title, expected base, optional message
504 [ 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ],
505 [ 'User:Foo/Bar/Baz', 'Foo/Bar' ],
510 * @dataProvider provideRootTitleCases
511 * @covers Title::getRootText
513 public function testGetRootText( $title, $expected, $msg = '' ) {
514 $title = Title
::newFromText( $title );
515 $this->assertEquals( $expected,
516 $title->getRootText(),
521 public static function provideRootTitleCases() {
523 # Title, expected base, optional message
524 [ 'User:John_Doe/subOne/subTwo', 'John Doe' ],
525 [ 'User:Foo/Bar/Baz', 'Foo' ],
530 * @todo Handle $wgNamespacesWithSubpages cases
531 * @dataProvider provideSubpageTitleCases
532 * @covers Title::getSubpageText
534 public function testGetSubpageText( $title, $expected, $msg = '' ) {
535 $title = Title
::newFromText( $title );
536 $this->assertEquals( $expected,
537 $title->getSubpageText(),
542 public static function provideSubpageTitleCases() {
544 # Title, expected base, optional message
545 [ 'User:John_Doe/subOne/subTwo', 'subTwo' ],
546 [ 'User:John_Doe/subOne', 'subOne' ],
550 public static function provideNewFromTitleValue() {
552 [ new TitleValue( NS_MAIN
, 'Foo' ) ],
553 [ new TitleValue( NS_MAIN
, 'Foo', 'bar' ) ],
554 [ new TitleValue( NS_USER
, 'Hansi_Maier' ) ],
559 * @covers Title::newFromTitleValue
560 * @dataProvider provideNewFromTitleValue
562 public function testNewFromTitleValue( TitleValue
$value ) {
563 $title = Title
::newFromTitleValue( $value );
565 $dbkey = str_replace( ' ', '_', $value->getText() );
566 $this->assertEquals( $dbkey, $title->getDBkey() );
567 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
568 $this->assertEquals( $value->getFragment(), $title->getFragment() );
572 * @covers Title::newFromLinkTarget
573 * @dataProvider provideNewFromTitleValue
575 public function testNewFromLinkTarget( LinkTarget
$value ) {
576 $title = Title
::newFromLinkTarget( $value );
578 $dbkey = str_replace( ' ', '_', $value->getText() );
579 $this->assertEquals( $dbkey, $title->getDBkey() );
580 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
581 $this->assertEquals( $value->getFragment(), $title->getFragment() );
585 * @covers Title::newFromLinkTarget
587 public function testNewFromLinkTarget_clone() {
588 $title = Title
::newFromText( __METHOD__
);
589 $this->assertSame( $title, Title
::newFromLinkTarget( $title ) );
591 // The Title::NEW_CLONE flag should ensure that a fresh instance is returned.
592 $clone = Title
::newFromLinkTarget( $title, Title
::NEW_CLONE
);
593 $this->assertNotSame( $title, $clone );
594 $this->assertTrue( $clone->equals( $title ) );
597 public function provideCastFromLinkTarget() {
598 return array_merge( [ [ null ] ], self
::provideNewFromTitleValue() );
602 * @covers Title::castFromLinkTarget
603 * @dataProvider provideCastFromLinkTarget
605 public function testCastFromLinkTarget( $value ) {
606 $title = Title
::castFromLinkTarget( $value );
608 if ( $value === null ) {
609 $this->assertNull( $title );
611 $dbkey = str_replace( ' ', '_', $value->getText() );
612 $this->assertSame( $dbkey, $title->getDBkey() );
613 $this->assertSame( $value->getNamespace(), $title->getNamespace() );
614 $this->assertSame( $value->getFragment(), $title->getFragment() );
618 public static function provideGetTitleValue() {
622 [ 'User:Hansi_Maier' ],
627 * @covers Title::getTitleValue
628 * @dataProvider provideGetTitleValue
630 public function testGetTitleValue( $text ) {
631 $title = Title
::newFromText( $text );
632 $value = $title->getTitleValue();
634 $dbkey = str_replace( ' ', '_', $value->getText() );
635 $this->assertEquals( $title->getDBkey(), $dbkey );
636 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
637 $this->assertEquals( $title->getFragment(), $value->getFragment() );
640 public static function provideGetFragment() {
643 [ 'Foo#bar', 'bar' ],
644 [ 'Foo#bär', 'bär' ],
646 // Inner whitespace is normalized
647 [ 'Foo#bar_bar', 'bar bar' ],
648 [ 'Foo#bar bar', 'bar bar' ],
649 [ 'Foo#bar bar', 'bar bar' ],
651 // Leading whitespace is kept, trailing whitespace is trimmed.
652 // XXX: Is this really want we want?
653 [ 'Foo#_bar_bar_', ' bar bar' ],
654 [ 'Foo# bar bar ', ' bar bar' ],
659 * @covers Title::getFragment
660 * @dataProvider provideGetFragment
662 * @param string $full
663 * @param string $fragment
665 public function testGetFragment( $full, $fragment ) {
666 $title = Title
::newFromText( $full );
667 $this->assertEquals( $fragment, $title->getFragment() );
671 * @covers Title::isAlwaysKnown
672 * @dataProvider provideIsAlwaysKnown
673 * @param string $page
674 * @param bool $isKnown
676 public function testIsAlwaysKnown( $page, $isKnown ) {
677 $title = Title
::newFromText( $page );
678 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
681 public static function provideIsAlwaysKnown() {
683 [ 'Some nonexistent page', false ],
686 [ 'Special:BlankPage', true ],
687 [ 'Special:SomeNonexistentSpecialPage', false ],
688 [ 'MediaWiki:Parentheses', true ],
689 [ 'MediaWiki:Some nonexistent message', false ],
694 * @covers Title::isValid
695 * @dataProvider provideIsValid
696 * @param Title $title
697 * @param bool $isValid
699 public function testIsValid( Title
$title, $isValid ) {
700 $this->assertEquals( $isValid, $title->isValid(), $title->getPrefixedText() );
703 public static function provideIsValid() {
705 [ Title
::makeTitle( NS_MAIN
, '' ), false ],
706 [ Title
::makeTitle( NS_MAIN
, '<>' ), false ],
707 [ Title
::makeTitle( NS_MAIN
, '|' ), false ],
708 [ Title
::makeTitle( NS_MAIN
, '#' ), false ],
709 [ Title
::makeTitle( NS_MAIN
, 'Test' ), true ],
710 [ Title
::makeTitle( -33, 'Test' ), false ],
711 [ Title
::makeTitle( 77663399, 'Test' ), false ],
716 * @covers Title::isAlwaysKnown
718 public function testIsAlwaysKnownOnInterwiki() {
719 $title = Title
::makeTitle( NS_MAIN
, 'Interwiki link', '', 'externalwiki' );
720 $this->assertTrue( $title->isAlwaysKnown() );
724 * @covers Title::exists
726 public function testExists() {
727 $title = Title
::makeTitle( NS_PROJECT
, 'New page' );
728 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
730 $article = new Article( $title );
731 $page = $article->getPage();
732 $page->doEditContent( new WikitextContent( 'Some [[link]]' ), 'summary' );
734 // Tell Title it doesn't know whether it exists
735 $title->mArticleID
= -1;
737 // Tell the link cache it doesn't exists when it really does
738 $linkCache->clearLink( $title );
739 $linkCache->addBadLinkObj( $title );
744 'exists() should rely on link cache unless GAID_FOR_UPDATE is used'
748 $title->exists( Title
::GAID_FOR_UPDATE
),
749 'exists() should re-query database when GAID_FOR_UPDATE is used'
753 public function provideCanHaveTalkPage() {
755 'User page has talk page' => [
756 Title
::makeTitle( NS_USER
, 'Jane' ), true
758 'Talke page has talk page' => [
759 Title
::makeTitle( NS_TALK
, 'Foo' ), true
761 'Special page cannot have talk page' => [
762 Title
::makeTitle( NS_SPECIAL
, 'Thing' ), false
764 'Virtual namespace cannot have talk page' => [
765 Title
::makeTitle( NS_MEDIA
, 'Kitten.jpg' ), false
771 * @dataProvider provideCanHaveTalkPage
772 * @covers Title::canHaveTalkPage
774 * @param Title $title
775 * @param bool $expected
777 public function testCanHaveTalkPage( Title
$title, $expected ) {
778 $actual = $title->canHaveTalkPage();
779 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
782 public static function provideGetTalkPage_good() {
784 [ Title
::makeTitle( NS_MAIN
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
785 [ Title
::makeTitle( NS_TALK
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
790 * @dataProvider provideGetTalkPage_good
791 * @covers Title::getTalkPageIfDefined
793 public function testGetTalkPageIfDefined_good( Title
$title ) {
794 $talk = $title->getTalkPageIfDefined();
795 $this->assertInstanceOf(
798 $title->getPrefixedDBKey()
802 public static function provideGetTalkPage_bad() {
804 [ Title
::makeTitle( NS_SPECIAL
, 'Test' ) ],
805 [ Title
::makeTitle( NS_MEDIA
, 'Test' ) ],
810 * @dataProvider provideGetTalkPage_bad
811 * @covers Title::getTalkPageIfDefined
813 public function testGetTalkPageIfDefined_bad( Title
$title ) {
814 $talk = $title->getTalkPageIfDefined();
817 $title->getPrefixedDBKey()
821 public function provideCreateFragmentTitle() {
823 [ Title
::makeTitle( NS_MAIN
, 'Test' ), 'foo' ],
824 [ Title
::makeTitle( NS_TALK
, 'Test', 'foo' ), '' ],
825 [ Title
::makeTitle( NS_CATEGORY
, 'Test', 'foo' ), 'bar' ],
826 [ Title
::makeTitle( NS_MAIN
, 'Test1', '', 'interwiki' ), 'baz' ]
831 * @covers Title::createFragmentTarget
832 * @dataProvider provideCreateFragmentTitle
834 public function testCreateFragmentTitle( Title
$title, $fragment ) {
835 $this->mergeMwGlobalArrayValue( 'wgHooks', [
836 'InterwikiLoadPrefix' => [
837 function ( $prefix, &$iwdata ) {
838 if ( $prefix === 'interwiki' ) {
840 'iw_url' => 'http://example.com/',
850 $fragmentTitle = $title->createFragmentTarget( $fragment );
852 $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
853 $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
854 $this->assertEquals( $title->getInterwiki(), $fragmentTitle->getInterwiki() );
855 $this->assertEquals( $fragment, $fragmentTitle->getFragment() );
858 public function provideGetPrefixedText() {
862 Title
::makeTitle( NS_MAIN
, 'Foo bar' ),
867 Title
::makeTitle( NS_USER
, 'Foo bar' ),
872 Title
::makeTitle( NS_USER_TALK
, 'Foo bar' ),
875 // fragment not included
877 Title
::makeTitle( NS_MAIN
, 'Foo bar', 'fragment' ),
882 Title
::makeTitle( NS_MEDIA
, 'Foo bar' ),
885 // non-existent namespace
887 Title
::makeTitle( 100777, 'Foo bar' ),
888 'Special:Badtitle/NS100777:Foo bar'
894 * @covers Title::getPrefixedText
895 * @dataProvider provideGetPrefixedText
897 public function testGetPrefixedText( Title
$title, $expected ) {
898 $this->assertEquals( $expected, $title->getPrefixedText() );
901 public function provideGetPrefixedDBKey() {
905 Title
::makeTitle( NS_MAIN
, 'Foo_bar' ),
910 Title
::makeTitle( NS_USER
, 'Foo_bar' ),
915 Title
::makeTitle( NS_USER_TALK
, 'Foo_bar' ),
918 // fragment not included
920 Title
::makeTitle( NS_MAIN
, 'Foo_bar', 'fragment' ),
925 Title
::makeTitle( NS_MEDIA
, 'Foo_bar' ),
928 // non-existent namespace
930 Title
::makeTitle( 100777, 'Foo_bar' ),
931 'Special:Badtitle/NS100777:Foo_bar'
937 * @covers Title::getPrefixedDBKey
938 * @dataProvider provideGetPrefixedDBKey
940 public function testGetPrefixedDBKey( Title
$title, $expected ) {
941 $this->assertEquals( $expected, $title->getPrefixedDBkey() );
945 * @covers Title::getFragmentForURL
946 * @dataProvider provideGetFragmentForURL
948 * @param string $titleStr
949 * @param string $expected
951 public function testGetFragmentForURL( $titleStr, $expected ) {
952 $this->setMwGlobals( [
953 'wgFragmentMode' => [ 'html5' ],
954 'wgExternalInterwikiFragmentMode' => 'legacy',
956 $dbw = wfGetDB( DB_MASTER
);
957 $dbw->insert( 'interwiki',
961 'iw_url' => 'http://de.wikipedia.org/wiki/',
962 'iw_api' => 'http://de.wikipedia.org/w/api.php',
963 'iw_wikiid' => 'dewiki',
969 'iw_url' => 'http://zzwiki.org/wiki/',
970 'iw_api' => 'http://zzwiki.org/w/api.php',
971 'iw_wikiid' => 'zzwiki',
980 $title = Title
::newFromText( $titleStr );
981 self
::assertEquals( $expected, $title->getFragmentForURL() );
983 $dbw->delete( 'interwiki', '*', __METHOD__
);
986 public function provideGetFragmentForURL() {
989 [ 'Foo#ümlåût', '#ümlåût' ],
990 [ 'de:Foo#Bå®', '#Bå®' ],
991 [ 'zz:Foo#тест', '#.D1.82.D0.B5.D1.81.D1.82' ],
996 * @covers Title::isRawHtmlMessage
997 * @dataProvider provideIsRawHtmlMessage
999 public function testIsRawHtmlMessage( $textForm, $expected ) {
1000 $this->setMwGlobals( 'wgRawHtmlMessages', [
1006 $title = Title
::newFromText( $textForm );
1007 $this->assertSame( $expected, $title->isRawHtmlMessage() );
1010 public function provideIsRawHtmlMessage() {
1012 [ 'MediaWiki:Foobar', true ],
1013 [ 'MediaWiki:Foo bar', true ],
1014 [ 'MediaWiki:Foo-bar', true ],
1015 [ 'MediaWiki:foo bar', true ],
1016 [ 'MediaWiki:foo-bar', true ],
1017 [ 'MediaWiki:foobar', true ],
1018 [ 'MediaWiki:some-other-message', false ],
1019 [ 'Main Page', false ],
1023 public function provideEquals() {
1025 Title
::newFromText( 'Main Page' ),
1026 Title
::newFromText( 'Main Page' ),
1030 Title
::newFromText( 'Main Page' ),
1031 Title
::newFromText( 'Not The Main Page' ),
1035 Title
::newFromText( 'Main Page' ),
1036 Title
::newFromText( 'Project:Main Page' ),
1040 Title
::newFromText( 'File:Example.png' ),
1041 Title
::newFromText( 'Image:Example.png' ),
1045 Title
::newFromText( 'Special:Version' ),
1046 Title
::newFromText( 'Special:Version' ),
1050 Title
::newFromText( 'Special:Version' ),
1051 Title
::newFromText( 'Special:Recentchanges' ),
1055 Title
::newFromText( 'Special:Version' ),
1056 Title
::newFromText( 'Main Page' ),
1060 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1061 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1065 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1066 Title
::makeTitle( NS_MAIN
, 'Bar', '', '' ),
1070 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1071 Title
::makeTitle( NS_TALK
, 'Foo', '', '' ),
1075 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1076 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1080 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1081 Title
::makeTitle( NS_MAIN
, 'Foo', 'Baz', '' ),
1085 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1086 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1090 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1091 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1095 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1096 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1102 * @covers Title::equals
1103 * @dataProvider provideEquals
1105 public function testEquals( Title
$firstValue, /* LinkTarget */ $secondValue, $expectedSame ) {
1108 $firstValue->equals( $secondValue )
1113 * @covers Title::newMainPage
1115 public function testNewMainPage() {
1116 $msgCache = TestingAccessWrapper
::newFromClass( MessageCache
::class );
1117 $msgCache->instance
= $this->createMock( MessageCache
::class );
1118 $msgCache->instance
->method( 'get' )->willReturn( 'Foresheet' );
1119 $msgCache->instance
->method( 'transform' )->willReturn( 'Foresheet' );
1123 Title
::newMainPage()->getText()
1128 * @covers Title::newMainPage
1130 public function testNewMainPageWithLocal() {
1131 $local = $this->createMock( MessageLocalizer
::class );
1132 $local->method( 'msg' )->willReturn( new RawMessage( 'Prime Article' ) );
1136 Title
::newMainPage( $local )->getText()