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' ) ],
840 [ Title
::makeTitle( NS_MAIN
, '', 'Kittens' ) ],
841 [ Title
::makeTitle( NS_MAIN
, 'Kittens', '', 'acme' ) ],
845 public static function provideGetSubjectPage_good() {
847 [ Title
::makeTitle( NS_TALK
, 'Test' ), Title
::makeTitle( NS_MAIN
, 'Test' ) ],
848 [ Title
::makeTitle( NS_MAIN
, 'Test' ), Title
::makeTitle( NS_MAIN
, 'Test' ) ],
852 public static function provideGetOtherPage_good() {
854 [ Title
::makeTitle( NS_MAIN
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
855 [ Title
::makeTitle( NS_TALK
, 'Test' ), Title
::makeTitle( NS_MAIN
, 'Test' ) ],
860 * @dataProvider provideCanHaveTalkPage
861 * @covers Title::canHaveTalkPage
863 * @param Title $title
864 * @param bool $expected
866 public function testCanHaveTalkPage( Title
$title, $expected ) {
867 $actual = $title->canHaveTalkPage();
868 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
872 * @dataProvider provideIsWatchable
873 * @covers Title::isWatchable
875 * @param Title $title
876 * @param bool $expected
878 public function testIsWatchable( Title
$title, $expected ) {
879 $actual = $title->canHaveTalkPage();
880 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
884 * @dataProvider provideGetTalkPage_good
885 * @covers Title::getTalkPageIfDefined
887 public function testGetTalkPage_good( Title
$title, Title
$expected ) {
888 $actual = $title->getTalkPage();
889 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
893 * @dataProvider provideGetTalkPage_bad
894 * @covers Title::getTalkPageIfDefined
896 public function testGetTalkPage_bad( Title
$title ) {
897 $this->setExpectedException( MWException
::class );
898 $title->getTalkPage();
902 * @dataProvider provideGetTalkPage_good
903 * @covers Title::getTalkPageIfDefined
905 public function testGetTalkPageIfDefined_good( Title
$title, Title
$expected ) {
906 $actual = $title->getTalkPageIfDefined();
907 $this->assertNotNull( $actual, $title->getPrefixedDBkey() );
908 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
912 * @dataProvider provideGetTalkPage_bad
913 * @covers Title::getTalkPageIfDefined
915 public function testGetTalkPageIfDefined_bad( Title
$title ) {
916 $talk = $title->getTalkPageIfDefined();
919 $title->getPrefixedDBkey()
924 * @dataProvider provideGetSubjectPage_good
925 * @covers Title::getSubjectPage
927 public function testGetSubjectPage_good( Title
$title, Title
$expected ) {
928 $actual = $title->getSubjectPage();
929 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
933 * @dataProvider provideGetOtherPage_good
934 * @covers Title::getOtherPage
936 public function testGetOtherPage_good( Title
$title, Title
$expected ) {
937 $actual = $title->getOtherPage();
938 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
942 * @dataProvider provideGetTalkPage_bad
943 * @covers Title::getOtherPage
945 public function testGetOtherPage_bad( Title
$title ) {
946 $this->setExpectedException( MWException
::class );
947 $title->getOtherPage();
951 * @dataProvider provideIsMovable
952 * @covers Title::isMovable
954 * @param string|Title $title
955 * @param bool $expected
956 * @param callable|null $hookCallback For TitleIsMovable
958 public function testIsMovable( $title, $expected, $hookCallback = null ) {
959 if ( $hookCallback ) {
960 $this->setTemporaryHook( 'TitleIsMovable', $hookCallback );
962 if ( is_string( $title ) ) {
963 $title = Title
::newFromText( $title );
966 $this->assertSame( $expected, $title->isMovable() );
969 public static function provideIsMovable() {
971 'Simple title' => [ 'Foo', true ],
972 // @todo Should these next two really be true?
973 'Empty name' => [ Title
::makeTitle( NS_MAIN
, '' ), true ],
974 'Invalid name' => [ Title
::makeTitle( NS_MAIN
, '<' ), true ],
975 'Interwiki' => [ Title
::makeTitle( NS_MAIN
, 'Test', '', 'otherwiki' ), false ],
976 'Special page' => [ 'Special:FooBar', false ],
977 'Aborted by hook' => [ 'Hooked in place', false,
978 function ( Title
$title, &$result ) {
985 public function provideCreateFragmentTitle() {
987 [ Title
::makeTitle( NS_MAIN
, 'Test' ), 'foo' ],
988 [ Title
::makeTitle( NS_TALK
, 'Test', 'foo' ), '' ],
989 [ Title
::makeTitle( NS_CATEGORY
, 'Test', 'foo' ), 'bar' ],
990 [ Title
::makeTitle( NS_MAIN
, 'Test1', '', 'interwiki' ), 'baz' ]
995 * @covers Title::createFragmentTarget
996 * @dataProvider provideCreateFragmentTitle
998 public function testCreateFragmentTitle( Title
$title, $fragment ) {
999 $this->mergeMwGlobalArrayValue( 'wgHooks', [
1000 'InterwikiLoadPrefix' => [
1001 function ( $prefix, &$iwdata ) {
1002 if ( $prefix === 'interwiki' ) {
1004 'iw_url' => 'http://example.com/',
1014 $fragmentTitle = $title->createFragmentTarget( $fragment );
1016 $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
1017 $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
1018 $this->assertEquals( $title->getInterwiki(), $fragmentTitle->getInterwiki() );
1019 $this->assertEquals( $fragment, $fragmentTitle->getFragment() );
1022 public function provideGetPrefixedText() {
1026 Title
::makeTitle( NS_MAIN
, 'Foo bar' ),
1031 Title
::makeTitle( NS_USER
, 'Foo bar' ),
1036 Title
::makeTitle( NS_USER_TALK
, 'Foo bar' ),
1039 // fragment not included
1041 Title
::makeTitle( NS_MAIN
, 'Foo bar', 'fragment' ),
1046 Title
::makeTitle( NS_MEDIA
, 'Foo bar' ),
1049 // non-existent namespace
1051 Title
::makeTitle( 100777, 'Foo bar' ),
1052 'Special:Badtitle/NS100777:Foo bar'
1058 * @covers Title::getPrefixedText
1059 * @dataProvider provideGetPrefixedText
1061 public function testGetPrefixedText( Title
$title, $expected ) {
1062 $this->assertEquals( $expected, $title->getPrefixedText() );
1065 public function provideGetPrefixedDBKey() {
1069 Title
::makeTitle( NS_MAIN
, 'Foo_bar' ),
1074 Title
::makeTitle( NS_USER
, 'Foo_bar' ),
1079 Title
::makeTitle( NS_USER_TALK
, 'Foo_bar' ),
1082 // fragment not included
1084 Title
::makeTitle( NS_MAIN
, 'Foo_bar', 'fragment' ),
1089 Title
::makeTitle( NS_MEDIA
, 'Foo_bar' ),
1092 // non-existent namespace
1094 Title
::makeTitle( 100777, 'Foo_bar' ),
1095 'Special:Badtitle/NS100777:Foo_bar'
1101 * @covers Title::getPrefixedDBKey
1102 * @dataProvider provideGetPrefixedDBKey
1104 public function testGetPrefixedDBKey( Title
$title, $expected ) {
1105 $this->assertEquals( $expected, $title->getPrefixedDBkey() );
1109 * @covers Title::getFragmentForURL
1110 * @dataProvider provideGetFragmentForURL
1112 * @param string $titleStr
1113 * @param string $expected
1115 public function testGetFragmentForURL( $titleStr, $expected ) {
1116 $this->setMwGlobals( [
1117 'wgFragmentMode' => [ 'html5' ],
1118 'wgExternalInterwikiFragmentMode' => 'legacy',
1120 $dbw = wfGetDB( DB_MASTER
);
1121 $dbw->insert( 'interwiki',
1124 'iw_prefix' => 'de',
1125 'iw_url' => 'http://de.wikipedia.org/wiki/',
1126 'iw_api' => 'http://de.wikipedia.org/w/api.php',
1127 'iw_wikiid' => 'dewiki',
1132 'iw_prefix' => 'zz',
1133 'iw_url' => 'http://zzwiki.org/wiki/',
1134 'iw_api' => 'http://zzwiki.org/w/api.php',
1135 'iw_wikiid' => 'zzwiki',
1144 $title = Title
::newFromText( $titleStr );
1145 self
::assertEquals( $expected, $title->getFragmentForURL() );
1147 $dbw->delete( 'interwiki', '*', __METHOD__
);
1150 public function provideGetFragmentForURL() {
1153 [ 'Foo#ümlåût', '#ümlåût' ],
1154 [ 'de:Foo#Bå®', '#Bå®' ],
1155 [ 'zz:Foo#тест', '#.D1.82.D0.B5.D1.81.D1.82' ],
1160 * @covers Title::isRawHtmlMessage
1161 * @dataProvider provideIsRawHtmlMessage
1163 public function testIsRawHtmlMessage( $textForm, $expected ) {
1164 $this->setMwGlobals( 'wgRawHtmlMessages', [
1170 $title = Title
::newFromText( $textForm );
1171 $this->assertSame( $expected, $title->isRawHtmlMessage() );
1174 public function provideIsRawHtmlMessage() {
1176 [ 'MediaWiki:Foobar', true ],
1177 [ 'MediaWiki:Foo bar', true ],
1178 [ 'MediaWiki:Foo-bar', true ],
1179 [ 'MediaWiki:foo bar', true ],
1180 [ 'MediaWiki:foo-bar', true ],
1181 [ 'MediaWiki:foobar', true ],
1182 [ 'MediaWiki:some-other-message', false ],
1183 [ 'Main Page', false ],
1187 public function provideEquals() {
1189 Title
::newFromText( 'Main Page' ),
1190 Title
::newFromText( 'Main Page' ),
1194 Title
::newFromText( 'Main Page' ),
1195 Title
::newFromText( 'Not The Main Page' ),
1199 Title
::newFromText( 'Main Page' ),
1200 Title
::newFromText( 'Project:Main Page' ),
1204 Title
::newFromText( 'File:Example.png' ),
1205 Title
::newFromText( 'Image:Example.png' ),
1209 Title
::newFromText( 'Special:Version' ),
1210 Title
::newFromText( 'Special:Version' ),
1214 Title
::newFromText( 'Special:Version' ),
1215 Title
::newFromText( 'Special:Recentchanges' ),
1219 Title
::newFromText( 'Special:Version' ),
1220 Title
::newFromText( 'Main Page' ),
1224 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1225 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1229 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1230 Title
::makeTitle( NS_MAIN
, 'Bar', '', '' ),
1234 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1235 Title
::makeTitle( NS_TALK
, 'Foo', '', '' ),
1239 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1240 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1244 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1245 Title
::makeTitle( NS_MAIN
, 'Foo', 'Baz', '' ),
1249 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1250 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1254 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1255 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1259 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1260 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1266 * @covers Title::equals
1267 * @dataProvider provideEquals
1269 public function testEquals( Title
$firstValue, /* LinkTarget */ $secondValue, $expectedSame ) {
1272 $firstValue->equals( $secondValue )
1277 * @covers Title::newMainPage
1279 public function testNewMainPage() {
1280 $mock = $this->createMock( MessageCache
::class );
1281 $mock->method( 'get' )->willReturn( 'Foresheet' );
1282 $mock->method( 'transform' )->willReturn( 'Foresheet' );
1284 $this->setService( 'MessageCache', $mock );
1288 Title
::newMainPage()->getText()
1293 * @covers Title::newMainPage
1295 public function testNewMainPageWithLocal() {
1296 $local = $this->createMock( MessageLocalizer
::class );
1297 $local->method( 'msg' )->willReturn( new RawMessage( 'Prime Article' ) );
1301 Title
::newMainPage( $local )->getText()