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' ];
156 * See also mediawiki.Title.test.js
157 * @covers Title::secureAndSplit
158 * @dataProvider provideValidSecureAndSplit
159 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
161 public function testSecureAndSplitValid( $text ) {
162 $this->secureAndSplitGlobals();
163 $this->assertInstanceOf( Title
::class, Title
::newFromText( $text ), "Valid: $text" );
167 * See also mediawiki.Title.test.js
168 * @covers Title::secureAndSplit
169 * @dataProvider provideInvalidSecureAndSplit
170 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
172 public function testSecureAndSplitInvalid( $text, $expectedErrorMessage ) {
173 $this->secureAndSplitGlobals();
175 Title
::newFromTextThrow( $text ); // should throw
176 $this->assertTrue( false, "Invalid: $text" );
177 } catch ( MalformedTitleException
$ex ) {
178 $this->assertEquals( $expectedErrorMessage, $ex->getErrorMessage(), "Invalid: $text" );
182 public static function provideConvertByteClassToUnicodeClass() {
185 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
186 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
190 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
193 'QWERTY\\x66-\\xFD+',
194 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
202 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
205 'QWERTY\\x66-\\x80+\\x23',
206 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
209 'QWERTY\\x66-\\x80+\\xD3',
210 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
214 '\\\\\\u0080-\\uFFFF',
218 '\\-\\u0080-\\uFFFF',
222 'QWERTY\\-\\u0080-\\uFFFF',
230 'A-\\x7F\\u0080-\\uFFFF',
233 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
234 'f-wQWERTYFXZ\\u0080-\\uFFFF',
237 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
238 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
244 * @dataProvider provideConvertByteClassToUnicodeClass
245 * @covers Title::convertByteClassToUnicodeClass
247 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
248 $this->assertEquals( $unicodeClass, Title
::convertByteClassToUnicodeClass( $byteClass ) );
252 * @dataProvider provideSpecialNamesWithAndWithoutParameter
253 * @covers Title::fixSpecialName
255 public function testFixSpecialNameRetainsParameter( $text, $expectedParam ) {
256 $title = Title
::newFromText( $text );
257 $fixed = $title->fixSpecialName();
258 $stuff = explode( '/', $fixed->getDBkey(), 2 );
259 if ( count( $stuff ) == 2 ) {
267 "T33100 regression check: Title->fixSpecialName() should preserve parameter"
271 public static function provideSpecialNamesWithAndWithoutParameter() {
273 [ 'Special:Version', null ],
274 [ 'Special:Version/', '' ],
275 [ 'Special:Version/param', 'param' ],
280 * Auth-less test of Title::userCan
282 * @param array $whitelistRegexp
283 * @param string $source
284 * @param string $action
285 * @param array|string|bool $expected Required error
287 * @covers \Mediawiki\Permissions\PermissionManager::checkReadPermissions
288 * @dataProvider dataWgWhitelistReadRegexp
290 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
291 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
292 // usually have only one regex, it is more concise to write the lonely regex
293 // as a string. Thus we cast to a [] to honor $wgWhitelistReadRegexp
295 if ( is_string( $whitelistRegexp ) ) {
296 $whitelistRegexp = [ $whitelistRegexp ];
299 $this->setMwGlobals( [
300 // So User::isEveryoneAllowed( 'read' ) === false
301 'wgGroupPermissions' => [ '*' => [ 'read' => false ] ],
302 'wgWhitelistRead' => [ 'some random non sense title' ],
303 'wgWhitelistReadRegexp' => $whitelistRegexp,
306 $title = Title
::newFromDBkey( $source );
308 // New anonymous user with no rights
310 $this->overrideUserPermissions( $user, [] );
311 $errors = $title->userCan( $action, $user );
313 if ( is_bool( $expected ) ) {
314 # Forge the assertion message depending on the assertion expectation
315 $allowableness = $expected
316 ?
" should be allowed"
317 : " should NOT be allowed";
321 "User action '$action' on [[$source]] $allowableness."
324 $errors = $this->flattenErrorsArray( $errors );
325 foreach ( (array)$expected as $error ) {
326 $this->assertContains( $error, $errors );
332 * Provides test parameter values for testWgWhitelistReadRegexp()
334 public function dataWgWhitelistReadRegexp() {
339 // Everything, if this doesn't work, we're really in trouble
340 [ '/.*/', 'Main_Page', 'read', $ALLOWED ],
341 [ '/.*/', 'Main_Page', 'edit', $DISALLOWED ],
343 // We validate against the title name, not the db key
344 [ '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ],
346 [ '/^Main/', 'Main_Page', 'read', $ALLOWED ],
347 [ '/^Main.*/', 'Main_Page', 'read', $ALLOWED ],
349 [ '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ],
351 // ...without unicode modifier
352 [ '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ],
353 // ...with unicode modifier
354 [ '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ],
356 [ '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ],
357 [ '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ],
359 // From DefaultSettings.php:
360 [ "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ],
361 [ "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ],
364 [ '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ],
365 [ null, 'Special:Newpages', 'read', $DISALLOWED ],
370 public function flattenErrorsArray( $errors ) {
372 foreach ( $errors as $error ) {
373 $result[] = $error[0];
380 * @dataProvider provideGetPageViewLanguage
381 * @covers Title::getPageViewLanguage
383 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
384 $lang, $variant, $msg = ''
386 // Setup environnement for this test
387 $this->setMwGlobals( [
388 'wgDefaultLanguageVariant' => $variant,
389 'wgAllowUserJs' => true,
391 $this->setUserLang( $lang );
392 $this->setContentLang( $contLang );
394 $title = Title
::newFromText( $titleText );
395 $this->assertInstanceOf( Title
::class, $title,
396 "Test must be passed a valid title text, you gave '$titleText'"
398 $this->assertEquals( $expected,
399 $title->getPageViewLanguage()->getCode(),
404 public static function provideGetPageViewLanguage() {
408 # - content language (expected in most cases)
409 # - wgLang (on some specific pages)
410 # - wgDefaultLanguageVariant
413 [ 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ],
414 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ],
415 [ 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ],
417 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ],
418 [ 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ],
419 [ 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ],
420 [ 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ],
421 [ 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ],
422 [ 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ],
423 [ 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ],
424 [ 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ],
426 [ 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ],
427 [ 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ],
428 [ 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ],
429 [ 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ],
430 [ 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ],
431 [ 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ],
432 [ 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ],
433 [ 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ],
434 [ 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ],
435 [ 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ],
437 [ 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ],
438 [ 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ],
444 * @dataProvider provideBaseTitleCases
445 * @covers Title::getBaseText
447 public function testGetBaseText( $title, $expected, $msg = '' ) {
448 $title = Title
::newFromText( $title );
449 $this->assertSame( $expected,
450 $title->getBaseText(),
456 * @dataProvider provideBaseTitleCases
457 * @covers Title::getBaseTitle
459 public function testGetBaseTitle( $title, $expected, $msg = '' ) {
460 $title = Title
::newFromText( $title );
461 $base = $title->getBaseTitle();
462 $this->assertTrue( $base->isValid(), $msg );
464 $base->equals( Title
::makeTitleSafe( $title->getNamespace(), $expected ) ),
469 public static function provideBaseTitleCases() {
471 # Title, expected base, optional message
472 [ 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ],
473 [ 'User:Foo / Bar / Baz', 'Foo / Bar ' ],
478 * @dataProvider provideRootTitleCases
479 * @covers Title::getRootText
481 public function testGetRootText( $title, $expected, $msg = '' ) {
482 $title = Title
::newFromText( $title );
483 $this->assertEquals( $expected,
484 $title->getRootText(),
490 * @dataProvider provideRootTitleCases
491 * @covers Title::getRootTitle
493 public function testGetRootTitle( $title, $expected, $msg = '' ) {
494 $title = Title
::newFromText( $title );
495 $root = $title->getRootTitle();
496 $this->assertTrue( $root->isValid(), $msg );
498 $root->equals( Title
::makeTitleSafe( $title->getNamespace(), $expected ) ),
503 public static function provideRootTitleCases() {
505 # Title, expected base, optional message
506 [ 'User:John_Doe/subOne/subTwo', 'John Doe' ],
507 [ 'User:Foo / Bar / Baz', 'Foo ' ],
508 [ 'Talk:////', '////' ],
509 [ 'Template:////', '////' ],
510 [ 'Template:Foo////', 'Foo' ],
511 [ 'Template:Foo////Bar', 'Foo' ],
516 * @todo Handle $wgNamespacesWithSubpages cases
517 * @dataProvider provideSubpageTitleCases
518 * @covers Title::getSubpageText
520 public function testGetSubpageText( $title, $expected, $msg = '' ) {
521 $title = Title
::newFromText( $title );
522 $this->assertEquals( $expected,
523 $title->getSubpageText(),
528 public static function provideSubpageTitleCases() {
530 # Title, expected base, optional message
531 [ 'User:John_Doe/subOne/subTwo', 'subTwo' ],
532 [ 'User:John_Doe/subOne', 'subOne' ],
536 public function provideSubpage() {
537 // NOTE: avoid constructing Title objects in the provider, since it may access the database.
539 [ 'Foo', 'x', new TitleValue( NS_MAIN
, 'Foo/x' ) ],
540 [ 'Foo#bar', 'x', new TitleValue( NS_MAIN
, 'Foo/x' ) ],
541 [ 'User:Foo', 'x', new TitleValue( NS_USER
, 'Foo/x' ) ],
542 [ 'wiki:User:Foo', 'x', new TitleValue( NS_MAIN
, 'User:Foo/x', '', 'wiki' ) ],
547 * @dataProvider provideSubpage
548 * @covers Title::getSubpage
550 public function testSubpage( $title, $sub, LinkTarget
$expected ) {
551 $interwikiLookup = $this->getMock( InterwikiLookup
::class );
552 $interwikiLookup->expects( $this->any() )
553 ->method( 'isValidInterwiki' )
554 ->willReturnCallback(
555 function ( $prefix ) {
556 return $prefix == 'wiki';
560 $this->setService( 'InterwikiLookup', $interwikiLookup );
562 $title = Title
::newFromText( $title );
563 $expected = Title
::newFromLinkTarget( $expected );
564 $actual = $title->getSubpage( $sub );
566 // NOTE: convert to string for comparison
567 $this->assertSame( $expected->getPrefixedText(), $actual->getPrefixedText(), 'text form' );
568 $this->assertTrue( $expected->equals( $actual ), 'Title equality' );
571 public static function provideNewFromTitleValue() {
573 [ new TitleValue( NS_MAIN
, 'Foo' ) ],
574 [ new TitleValue( NS_MAIN
, 'Foo', 'bar' ) ],
575 [ new TitleValue( NS_USER
, 'Hansi_Maier' ) ],
580 * @covers Title::newFromTitleValue
581 * @dataProvider provideNewFromTitleValue
583 public function testNewFromTitleValue( TitleValue
$value ) {
584 $title = Title
::newFromTitleValue( $value );
586 $dbkey = str_replace( ' ', '_', $value->getText() );
587 $this->assertEquals( $dbkey, $title->getDBkey() );
588 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
589 $this->assertEquals( $value->getFragment(), $title->getFragment() );
593 * @covers Title::newFromLinkTarget
594 * @dataProvider provideNewFromTitleValue
596 public function testNewFromLinkTarget( LinkTarget
$value ) {
597 $title = Title
::newFromLinkTarget( $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
608 public function testNewFromLinkTarget_clone() {
609 $title = Title
::newFromText( __METHOD__
);
610 $this->assertSame( $title, Title
::newFromLinkTarget( $title ) );
612 // The Title::NEW_CLONE flag should ensure that a fresh instance is returned.
613 $clone = Title
::newFromLinkTarget( $title, Title
::NEW_CLONE
);
614 $this->assertNotSame( $title, $clone );
615 $this->assertTrue( $clone->equals( $title ) );
618 public function provideCastFromLinkTarget() {
619 return array_merge( [ [ null ] ], self
::provideNewFromTitleValue() );
623 * @covers Title::castFromLinkTarget
624 * @dataProvider provideCastFromLinkTarget
626 public function testCastFromLinkTarget( $value ) {
627 $title = Title
::castFromLinkTarget( $value );
629 if ( $value === null ) {
630 $this->assertNull( $title );
632 $dbkey = str_replace( ' ', '_', $value->getText() );
633 $this->assertSame( $dbkey, $title->getDBkey() );
634 $this->assertSame( $value->getNamespace(), $title->getNamespace() );
635 $this->assertSame( $value->getFragment(), $title->getFragment() );
639 public static function provideGetTitleValue() {
643 [ 'User:Hansi_Maier' ],
648 * @covers Title::getTitleValue
649 * @dataProvider provideGetTitleValue
651 public function testGetTitleValue( $text ) {
652 $title = Title
::newFromText( $text );
653 $value = $title->getTitleValue();
655 $dbkey = str_replace( ' ', '_', $value->getText() );
656 $this->assertEquals( $title->getDBkey(), $dbkey );
657 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
658 $this->assertEquals( $title->getFragment(), $value->getFragment() );
661 public static function provideGetFragment() {
664 [ 'Foo#bar', 'bar' ],
665 [ 'Foo#bär', 'bär' ],
667 // Inner whitespace is normalized
668 [ 'Foo#bar_bar', 'bar bar' ],
669 [ 'Foo#bar bar', 'bar bar' ],
670 [ 'Foo#bar bar', 'bar bar' ],
672 // Leading whitespace is kept, trailing whitespace is trimmed.
673 // XXX: Is this really want we want?
674 [ 'Foo#_bar_bar_', ' bar bar' ],
675 [ 'Foo# bar bar ', ' bar bar' ],
680 * @covers Title::getFragment
681 * @dataProvider provideGetFragment
683 * @param string $full
684 * @param string $fragment
686 public function testGetFragment( $full, $fragment ) {
687 $title = Title
::newFromText( $full );
688 $this->assertEquals( $fragment, $title->getFragment() );
692 * @covers Title::isAlwaysKnown
693 * @dataProvider provideIsAlwaysKnown
694 * @param string $page
695 * @param bool $isKnown
697 public function testIsAlwaysKnown( $page, $isKnown ) {
698 $title = Title
::newFromText( $page );
699 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
702 public static function provideIsAlwaysKnown() {
704 [ 'Some nonexistent page', false ],
707 [ 'Special:BlankPage', true ],
708 [ 'Special:SomeNonexistentSpecialPage', false ],
709 [ 'MediaWiki:Parentheses', true ],
710 [ 'MediaWiki:Some nonexistent message', false ],
715 * @covers Title::isValid
716 * @dataProvider provideIsValid
717 * @param Title $title
718 * @param bool $isValid
720 public function testIsValid( Title
$title, $isValid ) {
721 $this->assertEquals( $isValid, $title->isValid(), $title->getPrefixedText() );
724 public static function provideIsValid() {
726 [ Title
::makeTitle( NS_MAIN
, '' ), false ],
727 [ Title
::makeTitle( NS_MAIN
, '<>' ), false ],
728 [ Title
::makeTitle( NS_MAIN
, '|' ), false ],
729 [ Title
::makeTitle( NS_MAIN
, '#' ), false ],
730 [ Title
::makeTitle( NS_MAIN
, 'Test' ), true ],
731 [ Title
::makeTitle( NS_MAIN
, ' Test' ), false ],
732 [ Title
::makeTitle( NS_MAIN
, '_Test' ), false ],
733 [ Title
::makeTitle( NS_MAIN
, 'Test ' ), false ],
734 [ Title
::makeTitle( NS_MAIN
, 'Test_' ), false ],
735 [ Title
::makeTitle( NS_MAIN
, "Test\nthis" ), false ],
736 [ Title
::makeTitle( NS_MAIN
, "Test\tthis" ), false ],
737 [ Title
::makeTitle( -33, 'Test' ), false ],
738 [ Title
::makeTitle( 77663399, 'Test' ), false ],
743 * @covers Title::isAlwaysKnown
745 public function testIsAlwaysKnownOnInterwiki() {
746 $title = Title
::makeTitle( NS_MAIN
, 'Interwiki link', '', 'externalwiki' );
747 $this->assertTrue( $title->isAlwaysKnown() );
751 * @covers Title::exists
753 public function testExists() {
754 $title = Title
::makeTitle( NS_PROJECT
, 'New page' );
755 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
757 $article = new Article( $title );
758 $page = $article->getPage();
759 $page->doEditContent( new WikitextContent( 'Some [[link]]' ), 'summary' );
761 // Tell Title it doesn't know whether it exists
762 $title->mArticleID
= -1;
764 // Tell the link cache it doesn't exist when it really does
765 $linkCache->clearLink( $title );
766 $linkCache->addBadLinkObj( $title );
771 'exists() should rely on link cache unless GAID_FOR_UPDATE is used'
775 $title->exists( Title
::GAID_FOR_UPDATE
),
776 'exists() should re-query database when GAID_FOR_UPDATE is used'
780 public function provideCanHaveTalkPage() {
782 'User page has talk page' => [
783 Title
::makeTitle( NS_USER
, 'Jane' ), true
785 'Talke page has talk page' => [
786 Title
::makeTitle( NS_TALK
, 'Foo' ), true
788 'Special page cannot have talk page' => [
789 Title
::makeTitle( NS_SPECIAL
, 'Thing' ), false
791 'Virtual namespace cannot have talk page' => [
792 Title
::makeTitle( NS_MEDIA
, 'Kitten.jpg' ), false
794 'Relative link has no talk page' => [
795 Title
::makeTitle( NS_MAIN
, '', 'Kittens' ), false
797 'Interwiki link has no talk page' => [
798 Title
::makeTitle( NS_MAIN
, 'Kittens', '', 'acme' ), false
803 public function provideIsWatchable() {
805 'User page is watchable' => [
806 Title
::makeTitle( NS_USER
, 'Jane' ), true
808 'Talke page is watchable' => [
809 Title
::makeTitle( NS_TALK
, 'Foo' ), true
811 'Special page is not watchable' => [
812 Title
::makeTitle( NS_SPECIAL
, 'Thing' ), false
814 'Virtual namespace is not watchable' => [
815 Title
::makeTitle( NS_MEDIA
, 'Kitten.jpg' ), false
817 'Relative link is not watchable' => [
818 Title
::makeTitle( NS_MAIN
, '', 'Kittens' ), false
820 'Interwiki link is not watchable' => [
821 Title
::makeTitle( NS_MAIN
, 'Kittens', '', 'acme' ), false
826 public static function provideGetTalkPage_good() {
828 [ Title
::makeTitle( NS_MAIN
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
829 [ Title
::makeTitle( NS_TALK
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
833 public static function provideGetTalkPage_bad() {
835 [ Title
::makeTitle( NS_SPECIAL
, 'Test' ) ],
836 [ Title
::makeTitle( NS_MEDIA
, 'Test' ) ],
837 [ Title
::makeTitle( NS_MAIN
, '', 'Kittens' ) ],
838 [ Title
::makeTitle( NS_MAIN
, 'Kittens', '', 'acme' ) ],
842 public static function provideGetSubjectPage_good() {
844 [ Title
::makeTitle( NS_TALK
, 'Test' ), Title
::makeTitle( NS_MAIN
, 'Test' ) ],
845 [ Title
::makeTitle( NS_MAIN
, 'Test' ), Title
::makeTitle( NS_MAIN
, 'Test' ) ],
849 public static function provideGetOtherPage_good() {
851 [ Title
::makeTitle( NS_MAIN
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
852 [ Title
::makeTitle( NS_TALK
, 'Test' ), Title
::makeTitle( NS_MAIN
, 'Test' ) ],
857 * @dataProvider provideCanHaveTalkPage
858 * @covers Title::canHaveTalkPage
860 * @param Title $title
861 * @param bool $expected
863 public function testCanHaveTalkPage( Title
$title, $expected ) {
864 $actual = $title->canHaveTalkPage();
865 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
869 * @dataProvider provideIsWatchable
870 * @covers Title::isWatchable
872 * @param Title $title
873 * @param bool $expected
875 public function testIsWatchable( Title
$title, $expected ) {
876 $actual = $title->canHaveTalkPage();
877 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
881 * @dataProvider provideGetTalkPage_good
882 * @covers Title::getTalkPageIfDefined
884 public function testGetTalkPage_good( Title
$title, Title
$expected ) {
885 $actual = $title->getTalkPage();
886 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
890 * @dataProvider provideGetTalkPage_bad
891 * @covers Title::getTalkPageIfDefined
893 public function testGetTalkPage_bad( Title
$title ) {
894 $this->setExpectedException( MWException
::class );
895 $title->getTalkPage();
899 * @dataProvider provideGetTalkPage_good
900 * @covers Title::getTalkPageIfDefined
902 public function testGetTalkPageIfDefined_good( Title
$title, Title
$expected ) {
903 $actual = $title->getTalkPageIfDefined();
904 $this->assertNotNull( $actual, $title->getPrefixedDBkey() );
905 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
909 * @dataProvider provideGetTalkPage_bad
910 * @covers Title::getTalkPageIfDefined
912 public function testGetTalkPageIfDefined_bad( Title
$title ) {
913 $talk = $title->getTalkPageIfDefined();
916 $title->getPrefixedDBkey()
921 * @dataProvider provideGetSubjectPage_good
922 * @covers Title::getSubjectPage
924 public function testGetSubjectPage_good( Title
$title, Title
$expected ) {
925 $actual = $title->getSubjectPage();
926 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
930 * @dataProvider provideGetOtherPage_good
931 * @covers Title::getOtherPage
933 public function testGetOtherPage_good( Title
$title, Title
$expected ) {
934 $actual = $title->getOtherPage();
935 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
939 * @dataProvider provideGetTalkPage_bad
940 * @covers Title::getOtherPage
942 public function testGetOtherPage_bad( Title
$title ) {
943 $this->setExpectedException( MWException
::class );
944 $title->getOtherPage();
948 * @dataProvider provideIsMovable
949 * @covers Title::isMovable
951 * @param string|Title $title
952 * @param bool $expected
953 * @param callable|null $hookCallback For TitleIsMovable
955 public function testIsMovable( $title, $expected, $hookCallback = null ) {
956 if ( $hookCallback ) {
957 $this->setTemporaryHook( 'TitleIsMovable', $hookCallback );
959 if ( is_string( $title ) ) {
960 $title = Title
::newFromText( $title );
963 $this->assertSame( $expected, $title->isMovable() );
966 public static function provideIsMovable() {
968 'Simple title' => [ 'Foo', true ],
969 // @todo Should these next two really be true?
970 'Empty name' => [ Title
::makeTitle( NS_MAIN
, '' ), true ],
971 'Invalid name' => [ Title
::makeTitle( NS_MAIN
, '<' ), true ],
972 'Interwiki' => [ Title
::makeTitle( NS_MAIN
, 'Test', '', 'otherwiki' ), false ],
973 'Special page' => [ 'Special:FooBar', false ],
974 'Aborted by hook' => [ 'Hooked in place', false,
975 function ( Title
$title, &$result ) {
982 public function provideCreateFragmentTitle() {
984 [ Title
::makeTitle( NS_MAIN
, 'Test' ), 'foo' ],
985 [ Title
::makeTitle( NS_TALK
, 'Test', 'foo' ), '' ],
986 [ Title
::makeTitle( NS_CATEGORY
, 'Test', 'foo' ), 'bar' ],
987 [ Title
::makeTitle( NS_MAIN
, 'Test1', '', 'interwiki' ), 'baz' ]
992 * @covers Title::createFragmentTarget
993 * @dataProvider provideCreateFragmentTitle
995 public function testCreateFragmentTitle( Title
$title, $fragment ) {
996 $this->mergeMwGlobalArrayValue( 'wgHooks', [
997 'InterwikiLoadPrefix' => [
998 function ( $prefix, &$iwdata ) {
999 if ( $prefix === 'interwiki' ) {
1001 'iw_url' => 'http://example.com/',
1011 $fragmentTitle = $title->createFragmentTarget( $fragment );
1013 $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
1014 $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
1015 $this->assertEquals( $title->getInterwiki(), $fragmentTitle->getInterwiki() );
1016 $this->assertEquals( $fragment, $fragmentTitle->getFragment() );
1019 public function provideGetPrefixedText() {
1023 Title
::makeTitle( NS_MAIN
, 'Foo bar' ),
1028 Title
::makeTitle( NS_USER
, 'Foo bar' ),
1033 Title
::makeTitle( NS_USER_TALK
, 'Foo bar' ),
1036 // fragment not included
1038 Title
::makeTitle( NS_MAIN
, 'Foo bar', 'fragment' ),
1043 Title
::makeTitle( NS_MEDIA
, 'Foo bar' ),
1046 // non-existent namespace
1048 Title
::makeTitle( 100777, 'Foo bar' ),
1049 'Special:Badtitle/NS100777:Foo bar'
1055 * @covers Title::getPrefixedText
1056 * @dataProvider provideGetPrefixedText
1058 public function testGetPrefixedText( Title
$title, $expected ) {
1059 $this->assertEquals( $expected, $title->getPrefixedText() );
1062 public function provideGetPrefixedDBKey() {
1066 Title
::makeTitle( NS_MAIN
, 'Foo_bar' ),
1071 Title
::makeTitle( NS_USER
, 'Foo_bar' ),
1076 Title
::makeTitle( NS_USER_TALK
, 'Foo_bar' ),
1079 // fragment not included
1081 Title
::makeTitle( NS_MAIN
, 'Foo_bar', 'fragment' ),
1086 Title
::makeTitle( NS_MEDIA
, 'Foo_bar' ),
1089 // non-existent namespace
1091 Title
::makeTitle( 100777, 'Foo_bar' ),
1092 'Special:Badtitle/NS100777:Foo_bar'
1098 * @covers Title::getPrefixedDBKey
1099 * @dataProvider provideGetPrefixedDBKey
1101 public function testGetPrefixedDBKey( Title
$title, $expected ) {
1102 $this->assertEquals( $expected, $title->getPrefixedDBkey() );
1106 * @covers Title::getFragmentForURL
1107 * @dataProvider provideGetFragmentForURL
1109 * @param string $titleStr
1110 * @param string $expected
1112 public function testGetFragmentForURL( $titleStr, $expected ) {
1113 $this->setMwGlobals( [
1114 'wgFragmentMode' => [ 'html5' ],
1115 'wgExternalInterwikiFragmentMode' => 'legacy',
1117 $dbw = wfGetDB( DB_MASTER
);
1118 $dbw->insert( 'interwiki',
1121 'iw_prefix' => 'de',
1122 'iw_url' => 'http://de.wikipedia.org/wiki/',
1123 'iw_api' => 'http://de.wikipedia.org/w/api.php',
1124 'iw_wikiid' => 'dewiki',
1129 'iw_prefix' => 'zz',
1130 'iw_url' => 'http://zzwiki.org/wiki/',
1131 'iw_api' => 'http://zzwiki.org/w/api.php',
1132 'iw_wikiid' => 'zzwiki',
1141 $title = Title
::newFromText( $titleStr );
1142 self
::assertEquals( $expected, $title->getFragmentForURL() );
1144 $dbw->delete( 'interwiki', '*', __METHOD__
);
1147 public function provideGetFragmentForURL() {
1150 [ 'Foo#ümlåût', '#ümlåût' ],
1151 [ 'de:Foo#Bå®', '#Bå®' ],
1152 [ 'zz:Foo#тест', '#.D1.82.D0.B5.D1.81.D1.82' ],
1157 * @covers Title::isRawHtmlMessage
1158 * @dataProvider provideIsRawHtmlMessage
1160 public function testIsRawHtmlMessage( $textForm, $expected ) {
1161 $this->setMwGlobals( 'wgRawHtmlMessages', [
1167 $title = Title
::newFromText( $textForm );
1168 $this->assertSame( $expected, $title->isRawHtmlMessage() );
1171 public function provideIsRawHtmlMessage() {
1173 [ 'MediaWiki:Foobar', true ],
1174 [ 'MediaWiki:Foo bar', true ],
1175 [ 'MediaWiki:Foo-bar', true ],
1176 [ 'MediaWiki:foo bar', true ],
1177 [ 'MediaWiki:foo-bar', true ],
1178 [ 'MediaWiki:foobar', true ],
1179 [ 'MediaWiki:some-other-message', false ],
1180 [ 'Main Page', false ],
1184 public function provideEquals() {
1186 Title
::newFromText( 'Main Page' ),
1187 Title
::newFromText( 'Main Page' ),
1191 Title
::newFromText( 'Main Page' ),
1192 Title
::newFromText( 'Not The Main Page' ),
1196 Title
::newFromText( 'Main Page' ),
1197 Title
::newFromText( 'Project:Main Page' ),
1201 Title
::newFromText( 'File:Example.png' ),
1202 Title
::newFromText( 'Image:Example.png' ),
1206 Title
::newFromText( 'Special:Version' ),
1207 Title
::newFromText( 'Special:Version' ),
1211 Title
::newFromText( 'Special:Version' ),
1212 Title
::newFromText( 'Special:Recentchanges' ),
1216 Title
::newFromText( 'Special:Version' ),
1217 Title
::newFromText( 'Main Page' ),
1221 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1222 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1226 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1227 Title
::makeTitle( NS_MAIN
, 'Bar', '', '' ),
1231 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1232 Title
::makeTitle( NS_TALK
, 'Foo', '', '' ),
1236 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1237 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1241 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1242 Title
::makeTitle( NS_MAIN
, 'Foo', 'Baz', '' ),
1246 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1247 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1251 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1252 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1256 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1257 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1263 * @covers Title::equals
1264 * @dataProvider provideEquals
1266 public function testEquals( Title
$firstValue, /* LinkTarget */ $secondValue, $expectedSame ) {
1269 $firstValue->equals( $secondValue )
1274 * @covers Title::newMainPage
1276 public function testNewMainPage() {
1277 $mock = $this->createMock( MessageCache
::class );
1278 $mock->method( 'get' )->willReturn( 'Foresheet' );
1279 $mock->method( 'transform' )->willReturn( 'Foresheet' );
1281 $this->setService( 'MessageCache', $mock );
1285 Title
::newMainPage()->getText()
1290 * @covers Title::newMainPage
1292 public function testNewMainPageWithLocal() {
1293 $local = $this->createMock( MessageLocalizer
::class );
1294 $local->method( 'msg' )->willReturn( new RawMessage( 'Prime Article' ) );
1298 Title
::newMainPage( $local )->getText()