3 use MediaWiki\Interwiki\InterwikiLookup
;
4 use MediaWiki\Linker\LinkTarget
;
5 use MediaWiki\MediaWikiServices
;
11 class TitleTest
extends MediaWikiTestCase
{
12 protected function setUp() {
15 $this->setMwGlobals( [
16 'wgAllowUserJs' => false,
17 'wgDefaultLanguageVariant' => false,
18 'wgMetaNamespace' => 'Project',
20 $this->setUserLang( 'en' );
21 $this->setContentLang( 'en' );
25 * @covers Title::legalChars
27 public function testLegalChars() {
28 $titlechars = Title
::legalChars();
30 foreach ( range( 1, 255 ) as $num ) {
32 if ( strpos( "#[]{}<>|", $chr ) !== false ||
preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
34 (bool)preg_match( "/[$titlechars]/", $chr ),
35 "chr($num) = $chr is not a valid titlechar"
39 (bool)preg_match( "/[$titlechars]/", $chr ),
40 "chr($num) = $chr is a valid titlechar"
46 public static function provideValidSecureAndSplit() {
57 [ 'Talk:Foo:Sandbox' ],
58 [ 'File:Example.svg' ],
59 [ 'File_talk:Example.svg' ],
60 [ 'Foo/.../Sandbox' ],
64 // Length is 256 total, but only title part matters
65 [ 'Category:' . str_repeat( 'x', 248 ) ],
66 [ str_repeat( 'x', 252 ) ],
68 [ 'localtestiw: #anchor' ],
70 [ 'localtestiw:foo' ],
71 [ 'localtestiw: foo # anchor' ],
72 [ 'localtestiw: Talk: Sandbox # anchor' ],
74 [ 'remotetestiw: Talk: # anchor' ],
75 [ 'remotetestiw: #bar' ],
76 [ 'remotetestiw: Talk:' ],
77 [ 'remotetestiw: Talk: Foo' ],
78 [ 'localtestiw:remotetestiw:' ],
79 [ 'localtestiw:remotetestiw:foo' ]
83 public static function provideInvalidSecureAndSplit() {
85 [ '', 'title-invalid-empty' ],
86 [ ':', 'title-invalid-empty' ],
87 [ '__ __', 'title-invalid-empty' ],
88 [ ' __ ', 'title-invalid-empty' ],
89 // Bad characters forbidden regardless of wgLegalTitleChars
90 [ 'A [ B', 'title-invalid-characters' ],
91 [ 'A ] B', 'title-invalid-characters' ],
92 [ 'A { B', 'title-invalid-characters' ],
93 [ 'A } B', 'title-invalid-characters' ],
94 [ 'A < B', 'title-invalid-characters' ],
95 [ 'A > B', 'title-invalid-characters' ],
96 [ 'A | B', 'title-invalid-characters' ],
97 [ "A \t B", 'title-invalid-characters' ],
98 [ "A \n B", 'title-invalid-characters' ],
100 [ 'A%20B', 'title-invalid-characters' ],
101 [ 'A%23B', 'title-invalid-characters' ],
102 [ 'A%2523B', 'title-invalid-characters' ],
103 // XML/HTML character entity references
104 // Note: Commented out because they are not marked invalid by the PHP test as
105 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
109 // Subject of NS_TALK does not roundtrip to NS_MAIN
110 [ 'Talk:File:Example.svg', 'title-invalid-talk-namespace' ],
111 // Directory navigation
112 [ '.', 'title-invalid-relative' ],
113 [ '..', 'title-invalid-relative' ],
114 [ './Sandbox', 'title-invalid-relative' ],
115 [ '../Sandbox', 'title-invalid-relative' ],
116 [ 'Foo/./Sandbox', 'title-invalid-relative' ],
117 [ 'Foo/../Sandbox', 'title-invalid-relative' ],
118 [ 'Sandbox/.', 'title-invalid-relative' ],
119 [ 'Sandbox/..', 'title-invalid-relative' ],
121 [ 'A ~~~ Name', 'title-invalid-magic-tilde' ],
122 [ 'A ~~~~ Signature', 'title-invalid-magic-tilde' ],
123 [ 'A ~~~~~ Timestamp', 'title-invalid-magic-tilde' ],
125 [ str_repeat( 'x', 256 ), 'title-invalid-too-long' ],
126 // Namespace prefix without actual title
127 [ 'Talk:', 'title-invalid-empty' ],
128 [ 'Talk:#', 'title-invalid-empty' ],
129 [ 'Category: ', 'title-invalid-empty' ],
130 [ 'Category: #bar', 'title-invalid-empty' ],
132 [ 'localtestiw: Talk: # anchor', 'title-invalid-empty' ],
133 [ 'localtestiw: Talk:', 'title-invalid-empty' ]
137 private function secureAndSplitGlobals() {
138 $this->setMwGlobals( [
139 'wgLocalInterwikis' => [ 'localtestiw' ],
141 'InterwikiLoadPrefix' => [
142 function ( $prefix, &$data ) {
143 if ( $prefix === 'localtestiw' ) {
144 $data = [ 'iw_url' => 'localtestiw' ];
145 } elseif ( $prefix === 'remotetestiw' ) {
146 $data = [ 'iw_url' => 'remotetestiw' ];
154 // Reset services since we modified $wgLocalInterwikis
155 $this->overrideMwServices();
159 * See also mediawiki.Title.test.js
160 * @covers Title::secureAndSplit
161 * @dataProvider provideValidSecureAndSplit
162 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
164 public function testSecureAndSplitValid( $text ) {
165 $this->secureAndSplitGlobals();
166 $this->assertInstanceOf( Title
::class, Title
::newFromText( $text ), "Valid: $text" );
170 * See also mediawiki.Title.test.js
171 * @covers Title::secureAndSplit
172 * @dataProvider provideInvalidSecureAndSplit
173 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
175 public function testSecureAndSplitInvalid( $text, $expectedErrorMessage ) {
176 $this->secureAndSplitGlobals();
178 Title
::newFromTextThrow( $text ); // should throw
179 $this->assertTrue( false, "Invalid: $text" );
180 } catch ( MalformedTitleException
$ex ) {
181 $this->assertEquals( $expectedErrorMessage, $ex->getErrorMessage(), "Invalid: $text" );
185 public static function provideConvertByteClassToUnicodeClass() {
188 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
189 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
193 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
196 'QWERTY\\x66-\\xFD+',
197 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
205 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
208 'QWERTY\\x66-\\x80+\\x23',
209 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
212 'QWERTY\\x66-\\x80+\\xD3',
213 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
217 '\\\\\\u0080-\\uFFFF',
221 '\\-\\u0080-\\uFFFF',
225 'QWERTY\\-\\u0080-\\uFFFF',
233 'A-\\x7F\\u0080-\\uFFFF',
236 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
237 'f-wQWERTYFXZ\\u0080-\\uFFFF',
240 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
241 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
247 * @dataProvider provideConvertByteClassToUnicodeClass
248 * @covers Title::convertByteClassToUnicodeClass
250 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
251 $this->assertEquals( $unicodeClass, Title
::convertByteClassToUnicodeClass( $byteClass ) );
255 * @dataProvider provideSpecialNamesWithAndWithoutParameter
256 * @covers Title::fixSpecialName
258 public function testFixSpecialNameRetainsParameter( $text, $expectedParam ) {
259 $title = Title
::newFromText( $text );
260 $fixed = $title->fixSpecialName();
261 $stuff = explode( '/', $fixed->getDBkey(), 2 );
262 if ( count( $stuff ) == 2 ) {
270 "T33100 regression check: Title->fixSpecialName() should preserve parameter"
274 public static function provideSpecialNamesWithAndWithoutParameter() {
276 [ 'Special:Version', null ],
277 [ 'Special:Version/', '' ],
278 [ 'Special:Version/param', 'param' ],
283 * Auth-less test of Title::userCan
285 * @param array $whitelistRegexp
286 * @param string $source
287 * @param string $action
288 * @param array|string|bool $expected Required error
290 * @covers \Mediawiki\Permissions\PermissionManager::checkReadPermissions
291 * @dataProvider dataWgWhitelistReadRegexp
293 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
294 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
295 // usually have only one regex, it is more concise to write the lonely regex
296 // as a string. Thus we cast to a [] to honor $wgWhitelistReadRegexp
298 if ( is_string( $whitelistRegexp ) ) {
299 $whitelistRegexp = [ $whitelistRegexp ];
302 $this->setMwGlobals( [
303 // So User::isEveryoneAllowed( 'read' ) === false
304 'wgGroupPermissions' => [ '*' => [ 'read' => false ] ],
305 'wgWhitelistRead' => [ 'some random non sense title' ],
306 'wgWhitelistReadRegexp' => $whitelistRegexp,
309 $title = Title
::newFromDBkey( $source );
311 // New anonymous user with no rights
313 $this->overrideUserPermissions( $user, [] );
314 $errors = $title->userCan( $action, $user );
316 if ( is_bool( $expected ) ) {
317 # Forge the assertion message depending on the assertion expectation
318 $allowableness = $expected
319 ?
" should be allowed"
320 : " should NOT be allowed";
324 "User action '$action' on [[$source]] $allowableness."
327 $errors = $this->flattenErrorsArray( $errors );
328 foreach ( (array)$expected as $error ) {
329 $this->assertContains( $error, $errors );
335 * Provides test parameter values for testWgWhitelistReadRegexp()
337 public function dataWgWhitelistReadRegexp() {
342 // Everything, if this doesn't work, we're really in trouble
343 [ '/.*/', 'Main_Page', 'read', $ALLOWED ],
344 [ '/.*/', 'Main_Page', 'edit', $DISALLOWED ],
346 // We validate against the title name, not the db key
347 [ '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ],
349 [ '/^Main/', 'Main_Page', 'read', $ALLOWED ],
350 [ '/^Main.*/', 'Main_Page', 'read', $ALLOWED ],
352 [ '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ],
354 // ...without unicode modifier
355 [ '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ],
356 // ...with unicode modifier
357 [ '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ],
359 [ '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ],
360 [ '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ],
362 // From DefaultSettings.php:
363 [ "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ],
364 [ "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ],
367 [ '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ],
368 [ null, 'Special:Newpages', 'read', $DISALLOWED ],
373 public function flattenErrorsArray( $errors ) {
375 foreach ( $errors as $error ) {
376 $result[] = $error[0];
383 * @dataProvider provideGetPageViewLanguage
384 * @covers Title::getPageViewLanguage
386 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
387 $lang, $variant, $msg = ''
389 // Setup environnement for this test
390 $this->setMwGlobals( [
391 'wgDefaultLanguageVariant' => $variant,
392 'wgAllowUserJs' => true,
394 $this->setUserLang( $lang );
395 $this->setContentLang( $contLang );
397 $title = Title
::newFromText( $titleText );
398 $this->assertInstanceOf( Title
::class, $title,
399 "Test must be passed a valid title text, you gave '$titleText'"
401 $this->assertEquals( $expected,
402 $title->getPageViewLanguage()->getCode(),
407 public static function provideGetPageViewLanguage() {
411 # - content language (expected in most cases)
412 # - wgLang (on some specific pages)
413 # - wgDefaultLanguageVariant
416 [ 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ],
417 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ],
418 [ 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ],
420 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ],
421 [ 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ],
422 [ 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ],
423 [ 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ],
424 [ 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ],
425 [ 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ],
426 [ 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ],
427 [ 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ],
429 [ 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ],
430 [ 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ],
431 [ 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ],
432 [ 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ],
433 [ 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ],
434 [ 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ],
435 [ 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ],
436 [ 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ],
437 [ 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ],
438 [ 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ],
440 [ 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ],
441 [ 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ],
447 * @dataProvider provideBaseTitleCases
448 * @covers Title::getBaseText
450 public function testGetBaseText( $title, $expected, $msg = '' ) {
451 $title = Title
::newFromText( $title );
452 $this->assertSame( $expected,
453 $title->getBaseText(),
459 * @dataProvider provideBaseTitleCases
460 * @covers Title::getBaseTitle
462 public function testGetBaseTitle( $title, $expected, $msg = '' ) {
463 $title = Title
::newFromText( $title );
464 $base = $title->getBaseTitle();
465 $this->assertTrue( $base->isValid(), $msg );
467 $base->equals( Title
::makeTitleSafe( $title->getNamespace(), $expected ) ),
472 public static function provideBaseTitleCases() {
474 # Title, expected base, optional message
475 [ 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ],
476 [ 'User:Foo / Bar / Baz', 'Foo / Bar ' ],
481 * @dataProvider provideRootTitleCases
482 * @covers Title::getRootText
484 public function testGetRootText( $title, $expected, $msg = '' ) {
485 $title = Title
::newFromText( $title );
486 $this->assertEquals( $expected,
487 $title->getRootText(),
493 * @dataProvider provideRootTitleCases
494 * @covers Title::getRootTitle
496 public function testGetRootTitle( $title, $expected, $msg = '' ) {
497 $title = Title
::newFromText( $title );
498 $root = $title->getRootTitle();
499 $this->assertTrue( $root->isValid(), $msg );
501 $root->equals( Title
::makeTitleSafe( $title->getNamespace(), $expected ) ),
506 public static function provideRootTitleCases() {
508 # Title, expected base, optional message
509 [ 'User:John_Doe/subOne/subTwo', 'John Doe' ],
510 [ 'User:Foo / Bar / Baz', 'Foo ' ],
511 [ 'Talk:////', '////' ],
512 [ 'Template:////', '////' ],
513 [ 'Template:Foo////', 'Foo' ],
514 [ 'Template:Foo////Bar', 'Foo' ],
519 * @todo Handle $wgNamespacesWithSubpages cases
520 * @dataProvider provideSubpageTitleCases
521 * @covers Title::getSubpageText
523 public function testGetSubpageText( $title, $expected, $msg = '' ) {
524 $title = Title
::newFromText( $title );
525 $this->assertEquals( $expected,
526 $title->getSubpageText(),
531 public static function provideSubpageTitleCases() {
533 # Title, expected base, optional message
534 [ 'User:John_Doe/subOne/subTwo', 'subTwo' ],
535 [ 'User:John_Doe/subOne', 'subOne' ],
539 public function provideSubpage() {
540 // NOTE: avoid constructing Title objects in the provider, since it may access the database.
542 [ 'Foo', 'x', new TitleValue( NS_MAIN
, 'Foo/x' ) ],
543 [ 'Foo#bar', 'x', new TitleValue( NS_MAIN
, 'Foo/x' ) ],
544 [ 'User:Foo', 'x', new TitleValue( NS_USER
, 'Foo/x' ) ],
545 [ 'wiki:User:Foo', 'x', new TitleValue( NS_MAIN
, 'User:Foo/x', '', 'wiki' ) ],
550 * @dataProvider provideSubpage
551 * @covers Title::getSubpage
553 public function testSubpage( $title, $sub, LinkTarget
$expected ) {
554 $interwikiLookup = $this->getMock( InterwikiLookup
::class );
555 $interwikiLookup->expects( $this->any() )
556 ->method( 'isValidInterwiki' )
557 ->willReturnCallback(
558 function ( $prefix ) {
559 return $prefix == 'wiki';
563 $this->setService( 'InterwikiLookup', $interwikiLookup );
565 $title = Title
::newFromText( $title );
566 $expected = Title
::newFromLinkTarget( $expected );
567 $actual = $title->getSubpage( $sub );
569 // NOTE: convert to string for comparison
570 $this->assertSame( $expected->getPrefixedText(), $actual->getPrefixedText(), 'text form' );
571 $this->assertTrue( $expected->equals( $actual ), 'Title equality' );
574 public static function provideNewFromTitleValue() {
576 [ new TitleValue( NS_MAIN
, 'Foo' ) ],
577 [ new TitleValue( NS_MAIN
, 'Foo', 'bar' ) ],
578 [ new TitleValue( NS_USER
, 'Hansi_Maier' ) ],
583 * @covers Title::newFromTitleValue
584 * @dataProvider provideNewFromTitleValue
586 public function testNewFromTitleValue( TitleValue
$value ) {
587 $title = Title
::newFromTitleValue( $value );
589 $dbkey = str_replace( ' ', '_', $value->getText() );
590 $this->assertEquals( $dbkey, $title->getDBkey() );
591 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
592 $this->assertEquals( $value->getFragment(), $title->getFragment() );
596 * @covers Title::newFromLinkTarget
597 * @dataProvider provideNewFromTitleValue
599 public function testNewFromLinkTarget( LinkTarget
$value ) {
600 $title = Title
::newFromLinkTarget( $value );
602 $dbkey = str_replace( ' ', '_', $value->getText() );
603 $this->assertEquals( $dbkey, $title->getDBkey() );
604 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
605 $this->assertEquals( $value->getFragment(), $title->getFragment() );
609 * @covers Title::newFromLinkTarget
611 public function testNewFromLinkTarget_clone() {
612 $title = Title
::newFromText( __METHOD__
);
613 $this->assertSame( $title, Title
::newFromLinkTarget( $title ) );
615 // The Title::NEW_CLONE flag should ensure that a fresh instance is returned.
616 $clone = Title
::newFromLinkTarget( $title, Title
::NEW_CLONE
);
617 $this->assertNotSame( $title, $clone );
618 $this->assertTrue( $clone->equals( $title ) );
621 public function provideCastFromLinkTarget() {
622 return array_merge( [ [ null ] ], self
::provideNewFromTitleValue() );
626 * @covers Title::castFromLinkTarget
627 * @dataProvider provideCastFromLinkTarget
629 public function testCastFromLinkTarget( $value ) {
630 $title = Title
::castFromLinkTarget( $value );
632 if ( $value === null ) {
633 $this->assertNull( $title );
635 $dbkey = str_replace( ' ', '_', $value->getText() );
636 $this->assertSame( $dbkey, $title->getDBkey() );
637 $this->assertSame( $value->getNamespace(), $title->getNamespace() );
638 $this->assertSame( $value->getFragment(), $title->getFragment() );
642 public static function provideGetTitleValue() {
646 [ 'User:Hansi_Maier' ],
651 * @covers Title::getTitleValue
652 * @dataProvider provideGetTitleValue
654 public function testGetTitleValue( $text ) {
655 $title = Title
::newFromText( $text );
656 $value = $title->getTitleValue();
658 $dbkey = str_replace( ' ', '_', $value->getText() );
659 $this->assertEquals( $title->getDBkey(), $dbkey );
660 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
661 $this->assertEquals( $title->getFragment(), $value->getFragment() );
664 public static function provideGetFragment() {
667 [ 'Foo#bar', 'bar' ],
668 [ 'Foo#bär', 'bär' ],
670 // Inner whitespace is normalized
671 [ 'Foo#bar_bar', 'bar bar' ],
672 [ 'Foo#bar bar', 'bar bar' ],
673 [ 'Foo#bar bar', 'bar bar' ],
675 // Leading whitespace is kept, trailing whitespace is trimmed.
676 // XXX: Is this really want we want?
677 [ 'Foo#_bar_bar_', ' bar bar' ],
678 [ 'Foo# bar bar ', ' bar bar' ],
683 * @covers Title::getFragment
684 * @dataProvider provideGetFragment
686 * @param string $full
687 * @param string $fragment
689 public function testGetFragment( $full, $fragment ) {
690 $title = Title
::newFromText( $full );
691 $this->assertEquals( $fragment, $title->getFragment() );
695 * @covers Title::isAlwaysKnown
696 * @dataProvider provideIsAlwaysKnown
697 * @param string $page
698 * @param bool $isKnown
700 public function testIsAlwaysKnown( $page, $isKnown ) {
701 $title = Title
::newFromText( $page );
702 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
705 public static function provideIsAlwaysKnown() {
707 [ 'Some nonexistent page', false ],
710 [ 'Special:BlankPage', true ],
711 [ 'Special:SomeNonexistentSpecialPage', false ],
712 [ 'MediaWiki:Parentheses', true ],
713 [ 'MediaWiki:Some nonexistent message', false ],
718 * @covers Title::isValid
719 * @dataProvider provideIsValid
720 * @param Title $title
721 * @param bool $isValid
723 public function testIsValid( Title
$title, $isValid ) {
724 $this->assertEquals( $isValid, $title->isValid(), $title->getPrefixedText() );
727 public static function provideIsValid() {
729 [ Title
::makeTitle( NS_MAIN
, '' ), false ],
730 [ Title
::makeTitle( NS_MAIN
, '<>' ), false ],
731 [ Title
::makeTitle( NS_MAIN
, '|' ), false ],
732 [ Title
::makeTitle( NS_MAIN
, '#' ), false ],
733 [ Title
::makeTitle( NS_MAIN
, 'Test' ), true ],
734 [ Title
::makeTitle( NS_MAIN
, ' Test' ), false ],
735 [ Title
::makeTitle( NS_MAIN
, '_Test' ), false ],
736 [ Title
::makeTitle( NS_MAIN
, 'Test ' ), false ],
737 [ Title
::makeTitle( NS_MAIN
, 'Test_' ), false ],
738 [ Title
::makeTitle( NS_MAIN
, "Test\nthis" ), false ],
739 [ Title
::makeTitle( NS_MAIN
, "Test\tthis" ), false ],
740 [ Title
::makeTitle( -33, 'Test' ), false ],
741 [ Title
::makeTitle( 77663399, 'Test' ), false ],
746 * @covers Title::isAlwaysKnown
748 public function testIsAlwaysKnownOnInterwiki() {
749 $title = Title
::makeTitle( NS_MAIN
, 'Interwiki link', '', 'externalwiki' );
750 $this->assertTrue( $title->isAlwaysKnown() );
754 * @covers Title::exists
756 public function testExists() {
757 $title = Title
::makeTitle( NS_PROJECT
, 'New page' );
758 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
760 $article = new Article( $title );
761 $page = $article->getPage();
762 $page->doEditContent( new WikitextContent( 'Some [[link]]' ), 'summary' );
764 // Tell Title it doesn't know whether it exists
765 $title->mArticleID
= -1;
767 // Tell the link cache it doesn't exist when it really does
768 $linkCache->clearLink( $title );
769 $linkCache->addBadLinkObj( $title );
774 'exists() should rely on link cache unless GAID_FOR_UPDATE is used'
778 $title->exists( Title
::GAID_FOR_UPDATE
),
779 'exists() should re-query database when GAID_FOR_UPDATE is used'
783 public function provideCanHaveTalkPage() {
785 'User page has talk page' => [
786 Title
::makeTitle( NS_USER
, 'Jane' ), true
788 'Talke page has talk page' => [
789 Title
::makeTitle( NS_TALK
, 'Foo' ), true
791 'Special page cannot have talk page' => [
792 Title
::makeTitle( NS_SPECIAL
, 'Thing' ), false
794 'Virtual namespace cannot have talk page' => [
795 Title
::makeTitle( NS_MEDIA
, 'Kitten.jpg' ), false
797 'Relative link has no talk page' => [
798 Title
::makeTitle( NS_MAIN
, '', 'Kittens' ), false
800 'Interwiki link has no talk page' => [
801 Title
::makeTitle( NS_MAIN
, 'Kittens', '', 'acme' ), false
806 public function provideIsWatchable() {
808 'User page is watchable' => [
809 Title
::makeTitle( NS_USER
, 'Jane' ), true
811 'Talke page is watchable' => [
812 Title
::makeTitle( NS_TALK
, 'Foo' ), true
814 'Special page is not watchable' => [
815 Title
::makeTitle( NS_SPECIAL
, 'Thing' ), false
817 'Virtual namespace is not watchable' => [
818 Title
::makeTitle( NS_MEDIA
, 'Kitten.jpg' ), false
820 'Relative link is not watchable' => [
821 Title
::makeTitle( NS_MAIN
, '', 'Kittens' ), false
823 'Interwiki link is not watchable' => [
824 Title
::makeTitle( NS_MAIN
, 'Kittens', '', 'acme' ), false
829 public static function provideGetTalkPage_good() {
831 [ Title
::makeTitle( NS_MAIN
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
832 [ Title
::makeTitle( NS_TALK
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
836 public static function provideGetTalkPage_bad() {
838 [ Title
::makeTitle( NS_SPECIAL
, 'Test' ) ],
839 [ Title
::makeTitle( NS_MEDIA
, 'Test' ) ],
843 public static function provideGetTalkPage_broken() {
844 // These cases *should* be bad, but are not treated as bad, for backwards compatibility.
845 // See discussion on T227817.
848 Title
::makeTitle( NS_MAIN
, '', 'Kittens' ),
849 Title
::makeTitle( NS_TALK
, '' ), // Section is lost!
853 Title
::makeTitle( NS_MAIN
, 'Kittens', '', 'acme' ),
854 Title
::makeTitle( NS_TALK
, 'Kittens', '' ), // Interwiki prefix is lost!
860 public static function provideGetSubjectPage_good() {
862 [ Title
::makeTitle( NS_TALK
, 'Test' ), Title
::makeTitle( NS_MAIN
, 'Test' ) ],
863 [ Title
::makeTitle( NS_MAIN
, 'Test' ), Title
::makeTitle( NS_MAIN
, 'Test' ) ],
867 public static function provideGetOtherPage_good() {
869 [ Title
::makeTitle( NS_MAIN
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
870 [ Title
::makeTitle( NS_TALK
, 'Test' ), Title
::makeTitle( NS_MAIN
, 'Test' ) ],
875 * @dataProvider provideCanHaveTalkPage
876 * @covers Title::canHaveTalkPage
878 * @param Title $title
879 * @param bool $expected
881 public function testCanHaveTalkPage( Title
$title, $expected ) {
882 $actual = $title->canHaveTalkPage();
883 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
887 * @dataProvider provideIsWatchable
888 * @covers Title::isWatchable
890 * @param Title $title
891 * @param bool $expected
893 public function testIsWatchable( Title
$title, $expected ) {
894 $actual = $title->canHaveTalkPage();
895 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
899 * @dataProvider provideGetTalkPage_good
900 * @covers Title::getTalkPageIfDefined
902 public function testGetTalkPage_good( Title
$title, Title
$expected ) {
903 $actual = $title->getTalkPage();
904 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
908 * @dataProvider provideGetTalkPage_bad
909 * @covers Title::getTalkPageIfDefined
911 public function testGetTalkPage_bad( Title
$title ) {
912 $this->setExpectedException( MWException
::class );
913 $title->getTalkPage();
917 * @dataProvider provideGetTalkPage_broken
918 * @covers Title::getTalkPageIfDefined
920 public function testGetTalkPage_broken( Title
$title, Title
$expected, $valid ) {
921 $errorLevel = error_reporting( E_ERROR
);
923 // NOTE: Eventually we want to throw in this case. But while there is still code that
924 // calls this method without checking, we want to avoid fatal errors.
925 // See discussion on T227817.
926 $result = $title->getTalkPage();
927 $this->assertTrue( $expected->equals( $result ) );
928 $this->assertSame( $valid, $result->isValid() );
930 error_reporting( $errorLevel );
934 * @dataProvider provideGetTalkPage_good
935 * @covers Title::getTalkPageIfDefined
937 public function testGetTalkPageIfDefined_good( Title
$title, Title
$expected ) {
938 $actual = $title->getTalkPageIfDefined();
939 $this->assertNotNull( $actual, $title->getPrefixedDBkey() );
940 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
944 * @dataProvider provideGetTalkPage_bad
945 * @covers Title::getTalkPageIfDefined
947 public function testGetTalkPageIfDefined_bad( Title
$title ) {
948 $talk = $title->getTalkPageIfDefined();
951 $title->getPrefixedDBkey()
956 * @dataProvider provideGetSubjectPage_good
957 * @covers Title::getSubjectPage
959 public function testGetSubjectPage_good( Title
$title, Title
$expected ) {
960 $actual = $title->getSubjectPage();
961 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
965 * @dataProvider provideGetOtherPage_good
966 * @covers Title::getOtherPage
968 public function testGetOtherPage_good( Title
$title, Title
$expected ) {
969 $actual = $title->getOtherPage();
970 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
974 * @dataProvider provideGetTalkPage_bad
975 * @covers Title::getOtherPage
977 public function testGetOtherPage_bad( Title
$title ) {
978 $this->setExpectedException( MWException
::class );
979 $title->getOtherPage();
983 * @dataProvider provideIsMovable
984 * @covers Title::isMovable
986 * @param string|Title $title
987 * @param bool $expected
988 * @param callable|null $hookCallback For TitleIsMovable
990 public function testIsMovable( $title, $expected, $hookCallback = null ) {
991 if ( $hookCallback ) {
992 $this->setTemporaryHook( 'TitleIsMovable', $hookCallback );
994 if ( is_string( $title ) ) {
995 $title = Title
::newFromText( $title );
998 $this->assertSame( $expected, $title->isMovable() );
1001 public static function provideIsMovable() {
1003 'Simple title' => [ 'Foo', true ],
1004 // @todo Should these next two really be true?
1005 'Empty name' => [ Title
::makeTitle( NS_MAIN
, '' ), true ],
1006 'Invalid name' => [ Title
::makeTitle( NS_MAIN
, '<' ), true ],
1007 'Interwiki' => [ Title
::makeTitle( NS_MAIN
, 'Test', '', 'otherwiki' ), false ],
1008 'Special page' => [ 'Special:FooBar', false ],
1009 'Aborted by hook' => [ 'Hooked in place', false,
1010 function ( Title
$title, &$result ) {
1017 public function provideCreateFragmentTitle() {
1019 [ Title
::makeTitle( NS_MAIN
, 'Test' ), 'foo' ],
1020 [ Title
::makeTitle( NS_TALK
, 'Test', 'foo' ), '' ],
1021 [ Title
::makeTitle( NS_CATEGORY
, 'Test', 'foo' ), 'bar' ],
1022 [ Title
::makeTitle( NS_MAIN
, 'Test1', '', 'interwiki' ), 'baz' ]
1027 * @covers Title::createFragmentTarget
1028 * @dataProvider provideCreateFragmentTitle
1030 public function testCreateFragmentTitle( Title
$title, $fragment ) {
1031 $this->mergeMwGlobalArrayValue( 'wgHooks', [
1032 'InterwikiLoadPrefix' => [
1033 function ( $prefix, &$iwdata ) {
1034 if ( $prefix === 'interwiki' ) {
1036 'iw_url' => 'http://example.com/',
1046 $fragmentTitle = $title->createFragmentTarget( $fragment );
1048 $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
1049 $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
1050 $this->assertEquals( $title->getInterwiki(), $fragmentTitle->getInterwiki() );
1051 $this->assertEquals( $fragment, $fragmentTitle->getFragment() );
1054 public function provideGetPrefixedText() {
1058 Title
::makeTitle( NS_MAIN
, 'Foo bar' ),
1063 Title
::makeTitle( NS_USER
, 'Foo bar' ),
1068 Title
::makeTitle( NS_USER_TALK
, 'Foo bar' ),
1071 // fragment not included
1073 Title
::makeTitle( NS_MAIN
, 'Foo bar', 'fragment' ),
1078 Title
::makeTitle( NS_MEDIA
, 'Foo bar' ),
1081 // non-existent namespace
1083 Title
::makeTitle( 100777, 'Foo bar' ),
1084 'Special:Badtitle/NS100777:Foo bar'
1090 * @covers Title::getPrefixedText
1091 * @dataProvider provideGetPrefixedText
1093 public function testGetPrefixedText( Title
$title, $expected ) {
1094 $this->assertEquals( $expected, $title->getPrefixedText() );
1097 public function provideGetPrefixedDBKey() {
1101 Title
::makeTitle( NS_MAIN
, 'Foo_bar' ),
1106 Title
::makeTitle( NS_USER
, 'Foo_bar' ),
1111 Title
::makeTitle( NS_USER_TALK
, 'Foo_bar' ),
1114 // fragment not included
1116 Title
::makeTitle( NS_MAIN
, 'Foo_bar', 'fragment' ),
1121 Title
::makeTitle( NS_MEDIA
, 'Foo_bar' ),
1124 // non-existent namespace
1126 Title
::makeTitle( 100777, 'Foo_bar' ),
1127 'Special:Badtitle/NS100777:Foo_bar'
1133 * @covers Title::getPrefixedDBKey
1134 * @dataProvider provideGetPrefixedDBKey
1136 public function testGetPrefixedDBKey( Title
$title, $expected ) {
1137 $this->assertEquals( $expected, $title->getPrefixedDBkey() );
1141 * @covers Title::getFragmentForURL
1142 * @dataProvider provideGetFragmentForURL
1144 * @param string $titleStr
1145 * @param string $expected
1147 public function testGetFragmentForURL( $titleStr, $expected ) {
1148 $this->setMwGlobals( [
1149 'wgFragmentMode' => [ 'html5' ],
1150 'wgExternalInterwikiFragmentMode' => 'legacy',
1152 $dbw = wfGetDB( DB_MASTER
);
1153 $dbw->insert( 'interwiki',
1156 'iw_prefix' => 'de',
1157 'iw_url' => 'http://de.wikipedia.org/wiki/',
1158 'iw_api' => 'http://de.wikipedia.org/w/api.php',
1159 'iw_wikiid' => 'dewiki',
1164 'iw_prefix' => 'zz',
1165 'iw_url' => 'http://zzwiki.org/wiki/',
1166 'iw_api' => 'http://zzwiki.org/w/api.php',
1167 'iw_wikiid' => 'zzwiki',
1176 $title = Title
::newFromText( $titleStr );
1177 self
::assertEquals( $expected, $title->getFragmentForURL() );
1179 $dbw->delete( 'interwiki', '*', __METHOD__
);
1182 public function provideGetFragmentForURL() {
1185 [ 'Foo#ümlåût', '#ümlåût' ],
1186 [ 'de:Foo#Bå®', '#Bå®' ],
1187 [ 'zz:Foo#тест', '#.D1.82.D0.B5.D1.81.D1.82' ],
1192 * @covers Title::isRawHtmlMessage
1193 * @dataProvider provideIsRawHtmlMessage
1195 public function testIsRawHtmlMessage( $textForm, $expected ) {
1196 $this->setMwGlobals( 'wgRawHtmlMessages', [
1202 $title = Title
::newFromText( $textForm );
1203 $this->assertSame( $expected, $title->isRawHtmlMessage() );
1206 public function provideIsRawHtmlMessage() {
1208 [ 'MediaWiki:Foobar', true ],
1209 [ 'MediaWiki:Foo bar', true ],
1210 [ 'MediaWiki:Foo-bar', true ],
1211 [ 'MediaWiki:foo bar', true ],
1212 [ 'MediaWiki:foo-bar', true ],
1213 [ 'MediaWiki:foobar', true ],
1214 [ 'MediaWiki:some-other-message', false ],
1215 [ 'Main Page', false ],
1219 public function provideEquals() {
1221 Title
::newFromText( 'Main Page' ),
1222 Title
::newFromText( 'Main Page' ),
1226 Title
::newFromText( 'Main Page' ),
1227 Title
::newFromText( 'Not The Main Page' ),
1231 Title
::newFromText( 'Main Page' ),
1232 Title
::newFromText( 'Project:Main Page' ),
1236 Title
::newFromText( 'File:Example.png' ),
1237 Title
::newFromText( 'Image:Example.png' ),
1241 Title
::newFromText( 'Special:Version' ),
1242 Title
::newFromText( 'Special:Version' ),
1246 Title
::newFromText( 'Special:Version' ),
1247 Title
::newFromText( 'Special:Recentchanges' ),
1251 Title
::newFromText( 'Special:Version' ),
1252 Title
::newFromText( 'Main Page' ),
1256 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1257 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1261 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1262 Title
::makeTitle( NS_MAIN
, 'Bar', '', '' ),
1266 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1267 Title
::makeTitle( NS_TALK
, 'Foo', '', '' ),
1271 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1272 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1276 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1277 Title
::makeTitle( NS_MAIN
, 'Foo', 'Baz', '' ),
1281 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1282 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1286 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1287 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1291 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1292 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1298 * @covers Title::equals
1299 * @dataProvider provideEquals
1301 public function testEquals( Title
$firstValue, /* LinkTarget */ $secondValue, $expectedSame ) {
1304 $firstValue->equals( $secondValue )
1309 * @covers Title::newMainPage
1311 public function testNewMainPage() {
1312 $mock = $this->createMock( MessageCache
::class );
1313 $mock->method( 'get' )->willReturn( 'Foresheet' );
1314 $mock->method( 'transform' )->willReturn( 'Foresheet' );
1316 $this->setService( 'MessageCache', $mock );
1320 Title
::newMainPage()->getText()
1325 * @covers Title::newMainPage
1327 public function testNewMainPageWithLocal() {
1328 $local = $this->createMock( MessageLocalizer
::class );
1329 $local->method( 'msg' )->willReturn( new RawMessage( 'Prime Article' ) );
1333 Title
::newMainPage( $local )->getText()