3 use MediaWiki\Interwiki\InterwikiLookup
;
4 use MediaWiki\Linker\LinkTarget
;
5 use MediaWiki\MediaWikiServices
;
6 use Wikimedia\TestingAccessWrapper
;
12 class TitleTest
extends MediaWikiTestCase
{
13 protected function setUp() {
16 $this->setMwGlobals( [
17 'wgAllowUserJs' => false,
18 'wgDefaultLanguageVariant' => false,
19 'wgMetaNamespace' => 'Project',
21 $this->setUserLang( 'en' );
22 $this->setContentLang( 'en' );
25 protected function tearDown() {
26 // For testNewMainPage
27 MessageCache
::destroyInstance();
32 * @covers Title::legalChars
34 public function testLegalChars() {
35 $titlechars = Title
::legalChars();
37 foreach ( range( 1, 255 ) as $num ) {
39 if ( strpos( "#[]{}<>|", $chr ) !== false ||
preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
41 (bool)preg_match( "/[$titlechars]/", $chr ),
42 "chr($num) = $chr is not a valid titlechar"
46 (bool)preg_match( "/[$titlechars]/", $chr ),
47 "chr($num) = $chr is a valid titlechar"
53 public static function provideValidSecureAndSplit() {
64 [ 'Talk:Foo:Sandbox' ],
65 [ 'File:Example.svg' ],
66 [ 'File_talk:Example.svg' ],
67 [ 'Foo/.../Sandbox' ],
71 // Length is 256 total, but only title part matters
72 [ 'Category:' . str_repeat( 'x', 248 ) ],
73 [ str_repeat( 'x', 252 ) ],
75 [ 'localtestiw: #anchor' ],
77 [ 'localtestiw:foo' ],
78 [ 'localtestiw: foo # anchor' ],
79 [ 'localtestiw: Talk: Sandbox # anchor' ],
81 [ 'remotetestiw: Talk: # anchor' ],
82 [ 'remotetestiw: #bar' ],
83 [ 'remotetestiw: Talk:' ],
84 [ 'remotetestiw: Talk: Foo' ],
85 [ 'localtestiw:remotetestiw:' ],
86 [ 'localtestiw:remotetestiw:foo' ]
90 public static function provideInvalidSecureAndSplit() {
92 [ '', 'title-invalid-empty' ],
93 [ ':', 'title-invalid-empty' ],
94 [ '__ __', 'title-invalid-empty' ],
95 [ ' __ ', 'title-invalid-empty' ],
96 // Bad characters forbidden regardless of wgLegalTitleChars
97 [ 'A [ B', 'title-invalid-characters' ],
98 [ 'A ] B', 'title-invalid-characters' ],
99 [ 'A { B', 'title-invalid-characters' ],
100 [ 'A } B', 'title-invalid-characters' ],
101 [ 'A < B', 'title-invalid-characters' ],
102 [ 'A > B', 'title-invalid-characters' ],
103 [ 'A | B', 'title-invalid-characters' ],
104 [ "A \t B", 'title-invalid-characters' ],
105 [ "A \n B", 'title-invalid-characters' ],
107 [ 'A%20B', 'title-invalid-characters' ],
108 [ 'A%23B', 'title-invalid-characters' ],
109 [ 'A%2523B', 'title-invalid-characters' ],
110 // XML/HTML character entity references
111 // Note: Commented out because they are not marked invalid by the PHP test as
112 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
116 // Subject of NS_TALK does not roundtrip to NS_MAIN
117 [ 'Talk:File:Example.svg', 'title-invalid-talk-namespace' ],
118 // Directory navigation
119 [ '.', 'title-invalid-relative' ],
120 [ '..', 'title-invalid-relative' ],
121 [ './Sandbox', 'title-invalid-relative' ],
122 [ '../Sandbox', 'title-invalid-relative' ],
123 [ 'Foo/./Sandbox', 'title-invalid-relative' ],
124 [ 'Foo/../Sandbox', 'title-invalid-relative' ],
125 [ 'Sandbox/.', 'title-invalid-relative' ],
126 [ 'Sandbox/..', 'title-invalid-relative' ],
128 [ 'A ~~~ Name', 'title-invalid-magic-tilde' ],
129 [ 'A ~~~~ Signature', 'title-invalid-magic-tilde' ],
130 [ 'A ~~~~~ Timestamp', 'title-invalid-magic-tilde' ],
132 [ str_repeat( 'x', 256 ), 'title-invalid-too-long' ],
133 // Namespace prefix without actual title
134 [ 'Talk:', 'title-invalid-empty' ],
135 [ 'Talk:#', 'title-invalid-empty' ],
136 [ 'Category: ', 'title-invalid-empty' ],
137 [ 'Category: #bar', 'title-invalid-empty' ],
139 [ 'localtestiw: Talk: # anchor', 'title-invalid-empty' ],
140 [ 'localtestiw: Talk:', 'title-invalid-empty' ]
144 private function secureAndSplitGlobals() {
145 $this->setMwGlobals( [
146 'wgLocalInterwikis' => [ 'localtestiw' ],
148 'InterwikiLoadPrefix' => [
149 function ( $prefix, &$data ) {
150 if ( $prefix === 'localtestiw' ) {
151 $data = [ 'iw_url' => 'localtestiw' ];
152 } elseif ( $prefix === 'remotetestiw' ) {
153 $data = [ 'iw_url' => 'remotetestiw' ];
161 // Reset services since we modified $wgLocalInterwikis
162 $this->overrideMwServices();
166 * See also mediawiki.Title.test.js
167 * @covers Title::secureAndSplit
168 * @dataProvider provideValidSecureAndSplit
169 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
171 public function testSecureAndSplitValid( $text ) {
172 $this->secureAndSplitGlobals();
173 $this->assertInstanceOf( Title
::class, Title
::newFromText( $text ), "Valid: $text" );
177 * See also mediawiki.Title.test.js
178 * @covers Title::secureAndSplit
179 * @dataProvider provideInvalidSecureAndSplit
180 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
182 public function testSecureAndSplitInvalid( $text, $expectedErrorMessage ) {
183 $this->secureAndSplitGlobals();
185 Title
::newFromTextThrow( $text ); // should throw
186 $this->assertTrue( false, "Invalid: $text" );
187 } catch ( MalformedTitleException
$ex ) {
188 $this->assertEquals( $expectedErrorMessage, $ex->getErrorMessage(), "Invalid: $text" );
192 public static function provideConvertByteClassToUnicodeClass() {
195 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
196 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
200 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
203 'QWERTY\\x66-\\xFD+',
204 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
212 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
215 'QWERTY\\x66-\\x80+\\x23',
216 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
219 'QWERTY\\x66-\\x80+\\xD3',
220 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
224 '\\\\\\u0080-\\uFFFF',
228 '\\-\\u0080-\\uFFFF',
232 'QWERTY\\-\\u0080-\\uFFFF',
240 'A-\\x7F\\u0080-\\uFFFF',
243 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
244 'f-wQWERTYFXZ\\u0080-\\uFFFF',
247 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
248 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
254 * @dataProvider provideConvertByteClassToUnicodeClass
255 * @covers Title::convertByteClassToUnicodeClass
257 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
258 $this->assertEquals( $unicodeClass, Title
::convertByteClassToUnicodeClass( $byteClass ) );
262 * @dataProvider provideSpecialNamesWithAndWithoutParameter
263 * @covers Title::fixSpecialName
265 public function testFixSpecialNameRetainsParameter( $text, $expectedParam ) {
266 $title = Title
::newFromText( $text );
267 $fixed = $title->fixSpecialName();
268 $stuff = explode( '/', $fixed->getDBkey(), 2 );
269 if ( count( $stuff ) == 2 ) {
277 "T33100 regression check: Title->fixSpecialName() should preserve parameter"
281 public static function provideSpecialNamesWithAndWithoutParameter() {
283 [ 'Special:Version', null ],
284 [ 'Special:Version/', '' ],
285 [ 'Special:Version/param', 'param' ],
290 * Auth-less test of Title::isValidMoveOperation
292 * @param string $source
293 * @param string $target
294 * @param array|string|bool $expected Required error
295 * @dataProvider provideTestIsValidMoveOperation
296 * @covers Title::isValidMoveOperation
298 public function testIsValidMoveOperation( $source, $target, $expected ) {
299 $this->hideDeprecated( 'Title::isValidMoveOperation' );
301 $this->setMwGlobals( 'wgContentHandlerUseDB', false );
302 $title = Title
::newFromText( $source );
303 $nt = Title
::newFromText( $target );
304 $errors = $title->isValidMoveOperation( $nt, false );
305 if ( $expected === true ) {
306 $this->assertTrue( $errors );
308 $errors = $this->flattenErrorsArray( $errors );
309 foreach ( (array)$expected as $error ) {
310 $this->assertContains( $error, $errors );
315 public static function provideTestIsValidMoveOperation() {
317 // for Title::isValidMoveOperation
318 [ 'Some page', '', 'badtitletext' ],
319 [ 'Test', 'Test', 'selfmove' ],
320 [ 'Special:FooBar', 'Test', 'immobile-source-namespace' ],
321 [ 'Test', 'Special:FooBar', 'immobile-target-namespace' ],
322 [ 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ],
323 [ 'Page', 'File:Test.jpg', 'nonfile-cannot-move-to-file' ],
324 [ 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ],
329 * Auth-less test of Title::userCan
331 * @param array $whitelistRegexp
332 * @param string $source
333 * @param string $action
334 * @param array|string|bool $expected Required error
336 * @covers \Mediawiki\Permissions\PermissionManager::checkReadPermissions
337 * @dataProvider dataWgWhitelistReadRegexp
339 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
340 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
341 // usually have only one regex, it is more concise to write the lonely regex
342 // as a string. Thus we cast to a [] to honor $wgWhitelistReadRegexp
344 if ( is_string( $whitelistRegexp ) ) {
345 $whitelistRegexp = [ $whitelistRegexp ];
348 $this->setMwGlobals( [
349 // So User::isEveryoneAllowed( 'read' ) === false
350 'wgGroupPermissions' => [ '*' => [ 'read' => false ] ],
351 'wgWhitelistRead' => [ 'some random non sense title' ],
352 'wgWhitelistReadRegexp' => $whitelistRegexp,
355 $title = Title
::newFromDBkey( $source );
357 // New anonymous user with no rights
359 $this->overrideUserPermissions( $user, [] );
360 $errors = $title->userCan( $action, $user );
362 if ( is_bool( $expected ) ) {
363 # Forge the assertion message depending on the assertion expectation
364 $allowableness = $expected
365 ?
" should be allowed"
366 : " should NOT be allowed";
370 "User action '$action' on [[$source]] $allowableness."
373 $errors = $this->flattenErrorsArray( $errors );
374 foreach ( (array)$expected as $error ) {
375 $this->assertContains( $error, $errors );
381 * Provides test parameter values for testWgWhitelistReadRegexp()
383 public function dataWgWhitelistReadRegexp() {
388 // Everything, if this doesn't work, we're really in trouble
389 [ '/.*/', 'Main_Page', 'read', $ALLOWED ],
390 [ '/.*/', 'Main_Page', 'edit', $DISALLOWED ],
392 // We validate against the title name, not the db key
393 [ '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ],
395 [ '/^Main/', 'Main_Page', 'read', $ALLOWED ],
396 [ '/^Main.*/', 'Main_Page', 'read', $ALLOWED ],
398 [ '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ],
400 // ...without unicode modifier
401 [ '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ],
402 // ...with unicode modifier
403 [ '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ],
405 [ '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ],
406 [ '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ],
408 // From DefaultSettings.php:
409 [ "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ],
410 [ "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ],
413 [ '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ],
414 [ null, 'Special:Newpages', 'read', $DISALLOWED ],
419 public function flattenErrorsArray( $errors ) {
421 foreach ( $errors as $error ) {
422 $result[] = $error[0];
429 * @dataProvider provideGetPageViewLanguage
430 * @covers Title::getPageViewLanguage
432 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
433 $lang, $variant, $msg = ''
435 // Setup environnement for this test
436 $this->setMwGlobals( [
437 'wgDefaultLanguageVariant' => $variant,
438 'wgAllowUserJs' => true,
440 $this->setUserLang( $lang );
441 $this->setContentLang( $contLang );
443 $title = Title
::newFromText( $titleText );
444 $this->assertInstanceOf( Title
::class, $title,
445 "Test must be passed a valid title text, you gave '$titleText'"
447 $this->assertEquals( $expected,
448 $title->getPageViewLanguage()->getCode(),
453 public static function provideGetPageViewLanguage() {
457 # - content language (expected in most cases)
458 # - wgLang (on some specific pages)
459 # - wgDefaultLanguageVariant
462 [ 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ],
463 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ],
464 [ 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ],
466 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ],
467 [ 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ],
468 [ 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ],
469 [ 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ],
470 [ 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ],
471 [ 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ],
472 [ 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ],
473 [ 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ],
475 [ 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ],
476 [ 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ],
477 [ 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ],
478 [ 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ],
479 [ 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ],
480 [ 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ],
481 [ 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ],
482 [ 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ],
483 [ 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ],
484 [ 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ],
486 [ 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ],
487 [ 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ],
493 * @dataProvider provideBaseTitleCases
494 * @covers Title::getBaseText
496 public function testGetBaseText( $title, $expected, $msg = '' ) {
497 $title = Title
::newFromText( $title );
498 $this->assertSame( $expected,
499 $title->getBaseText(),
505 * @dataProvider provideBaseTitleCases
506 * @covers Title::getBaseTitle
508 public function testGetBaseTitle( $title, $expected, $msg = '' ) {
509 $title = Title
::newFromText( $title );
510 $base = $title->getBaseTitle();
511 $this->assertTrue( $base->isValid(), $msg );
513 $base->equals( Title
::makeTitleSafe( $title->getNamespace(), $expected ) ),
518 public static function provideBaseTitleCases() {
520 # Title, expected base, optional message
521 [ 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ],
522 [ 'User:Foo / Bar / Baz', 'Foo / Bar ' ],
527 * @dataProvider provideRootTitleCases
528 * @covers Title::getRootText
530 public function testGetRootText( $title, $expected, $msg = '' ) {
531 $title = Title
::newFromText( $title );
532 $this->assertEquals( $expected,
533 $title->getRootText(),
539 * @dataProvider provideRootTitleCases
540 * @covers Title::getRootTitle
542 public function testGetRootTitle( $title, $expected, $msg = '' ) {
543 $title = Title
::newFromText( $title );
544 $root = $title->getRootTitle();
545 $this->assertTrue( $root->isValid(), $msg );
547 $root->equals( Title
::makeTitleSafe( $title->getNamespace(), $expected ) ),
552 public static function provideRootTitleCases() {
554 # Title, expected base, optional message
555 [ 'User:John_Doe/subOne/subTwo', 'John Doe' ],
556 [ 'User:Foo / Bar / Baz', 'Foo ' ],
561 * @todo Handle $wgNamespacesWithSubpages cases
562 * @dataProvider provideSubpageTitleCases
563 * @covers Title::getSubpageText
565 public function testGetSubpageText( $title, $expected, $msg = '' ) {
566 $title = Title
::newFromText( $title );
567 $this->assertEquals( $expected,
568 $title->getSubpageText(),
573 public static function provideSubpageTitleCases() {
575 # Title, expected base, optional message
576 [ 'User:John_Doe/subOne/subTwo', 'subTwo' ],
577 [ 'User:John_Doe/subOne', 'subOne' ],
581 public function provideSubpage() {
582 // NOTE: avoid constructing Title objects in the provider, since it may access the database.
584 [ 'Foo', 'x', new TitleValue( NS_MAIN
, 'Foo/x' ) ],
585 [ 'Foo#bar', 'x', new TitleValue( NS_MAIN
, 'Foo/x' ) ],
586 [ 'User:Foo', 'x', new TitleValue( NS_USER
, 'Foo/x' ) ],
587 [ 'wiki:User:Foo', 'x', new TitleValue( NS_MAIN
, 'User:Foo/x', '', 'wiki' ) ],
592 * @dataProvider provideSubpage
593 * @covers Title::getSubpage
595 public function testSubpage( $title, $sub, LinkTarget
$expected ) {
596 $interwikiLookup = $this->getMock( InterwikiLookup
::class );
597 $interwikiLookup->expects( $this->any() )
598 ->method( 'isValidInterwiki' )
599 ->willReturnCallback(
600 function ( $prefix ) {
601 return $prefix == 'wiki';
605 $this->setService( 'InterwikiLookup', $interwikiLookup );
607 $title = Title
::newFromText( $title );
608 $expected = Title
::newFromLinkTarget( $expected );
609 $actual = $title->getSubpage( $sub );
611 // NOTE: convert to string for comparison
612 $this->assertSame( $expected->getPrefixedText(), $actual->getPrefixedText(), 'text form' );
613 $this->assertTrue( $expected->equals( $actual ), 'Title equality' );
616 public static function provideNewFromTitleValue() {
618 [ new TitleValue( NS_MAIN
, 'Foo' ) ],
619 [ new TitleValue( NS_MAIN
, 'Foo', 'bar' ) ],
620 [ new TitleValue( NS_USER
, 'Hansi_Maier' ) ],
625 * @covers Title::newFromTitleValue
626 * @dataProvider provideNewFromTitleValue
628 public function testNewFromTitleValue( TitleValue
$value ) {
629 $title = Title
::newFromTitleValue( $value );
631 $dbkey = str_replace( ' ', '_', $value->getText() );
632 $this->assertEquals( $dbkey, $title->getDBkey() );
633 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
634 $this->assertEquals( $value->getFragment(), $title->getFragment() );
638 * @covers Title::newFromLinkTarget
639 * @dataProvider provideNewFromTitleValue
641 public function testNewFromLinkTarget( LinkTarget
$value ) {
642 $title = Title
::newFromLinkTarget( $value );
644 $dbkey = str_replace( ' ', '_', $value->getText() );
645 $this->assertEquals( $dbkey, $title->getDBkey() );
646 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
647 $this->assertEquals( $value->getFragment(), $title->getFragment() );
651 * @covers Title::newFromLinkTarget
653 public function testNewFromLinkTarget_clone() {
654 $title = Title
::newFromText( __METHOD__
);
655 $this->assertSame( $title, Title
::newFromLinkTarget( $title ) );
657 // The Title::NEW_CLONE flag should ensure that a fresh instance is returned.
658 $clone = Title
::newFromLinkTarget( $title, Title
::NEW_CLONE
);
659 $this->assertNotSame( $title, $clone );
660 $this->assertTrue( $clone->equals( $title ) );
663 public function provideCastFromLinkTarget() {
664 return array_merge( [ [ null ] ], self
::provideNewFromTitleValue() );
668 * @covers Title::castFromLinkTarget
669 * @dataProvider provideCastFromLinkTarget
671 public function testCastFromLinkTarget( $value ) {
672 $title = Title
::castFromLinkTarget( $value );
674 if ( $value === null ) {
675 $this->assertNull( $title );
677 $dbkey = str_replace( ' ', '_', $value->getText() );
678 $this->assertSame( $dbkey, $title->getDBkey() );
679 $this->assertSame( $value->getNamespace(), $title->getNamespace() );
680 $this->assertSame( $value->getFragment(), $title->getFragment() );
684 public static function provideGetTitleValue() {
688 [ 'User:Hansi_Maier' ],
693 * @covers Title::getTitleValue
694 * @dataProvider provideGetTitleValue
696 public function testGetTitleValue( $text ) {
697 $title = Title
::newFromText( $text );
698 $value = $title->getTitleValue();
700 $dbkey = str_replace( ' ', '_', $value->getText() );
701 $this->assertEquals( $title->getDBkey(), $dbkey );
702 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
703 $this->assertEquals( $title->getFragment(), $value->getFragment() );
706 public static function provideGetFragment() {
709 [ 'Foo#bar', 'bar' ],
710 [ 'Foo#bär', 'bär' ],
712 // Inner whitespace is normalized
713 [ 'Foo#bar_bar', 'bar bar' ],
714 [ 'Foo#bar bar', 'bar bar' ],
715 [ 'Foo#bar bar', 'bar bar' ],
717 // Leading whitespace is kept, trailing whitespace is trimmed.
718 // XXX: Is this really want we want?
719 [ 'Foo#_bar_bar_', ' bar bar' ],
720 [ 'Foo# bar bar ', ' bar bar' ],
725 * @covers Title::getFragment
726 * @dataProvider provideGetFragment
728 * @param string $full
729 * @param string $fragment
731 public function testGetFragment( $full, $fragment ) {
732 $title = Title
::newFromText( $full );
733 $this->assertEquals( $fragment, $title->getFragment() );
737 * @covers Title::isAlwaysKnown
738 * @dataProvider provideIsAlwaysKnown
739 * @param string $page
740 * @param bool $isKnown
742 public function testIsAlwaysKnown( $page, $isKnown ) {
743 $title = Title
::newFromText( $page );
744 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
747 public static function provideIsAlwaysKnown() {
749 [ 'Some nonexistent page', false ],
752 [ 'Special:BlankPage', true ],
753 [ 'Special:SomeNonexistentSpecialPage', false ],
754 [ 'MediaWiki:Parentheses', true ],
755 [ 'MediaWiki:Some nonexistent message', false ],
760 * @covers Title::isValid
761 * @dataProvider provideIsValid
762 * @param Title $title
763 * @param bool $isValid
765 public function testIsValid( Title
$title, $isValid ) {
766 $this->assertEquals( $isValid, $title->isValid(), $title->getPrefixedText() );
769 public static function provideIsValid() {
771 [ Title
::makeTitle( NS_MAIN
, '' ), false ],
772 [ Title
::makeTitle( NS_MAIN
, '<>' ), false ],
773 [ Title
::makeTitle( NS_MAIN
, '|' ), false ],
774 [ Title
::makeTitle( NS_MAIN
, '#' ), false ],
775 [ Title
::makeTitle( NS_MAIN
, 'Test' ), true ],
776 [ Title
::makeTitle( NS_MAIN
, ' Test' ), false ],
777 [ Title
::makeTitle( NS_MAIN
, '_Test' ), false ],
778 [ Title
::makeTitle( NS_MAIN
, 'Test ' ), false ],
779 [ Title
::makeTitle( NS_MAIN
, 'Test_' ), false ],
780 [ Title
::makeTitle( NS_MAIN
, "Test\nthis" ), false ],
781 [ Title
::makeTitle( NS_MAIN
, "Test\tthis" ), false ],
782 [ Title
::makeTitle( -33, 'Test' ), false ],
783 [ Title
::makeTitle( 77663399, 'Test' ), false ],
788 * @covers Title::isAlwaysKnown
790 public function testIsAlwaysKnownOnInterwiki() {
791 $title = Title
::makeTitle( NS_MAIN
, 'Interwiki link', '', 'externalwiki' );
792 $this->assertTrue( $title->isAlwaysKnown() );
796 * @covers Title::exists
798 public function testExists() {
799 $title = Title
::makeTitle( NS_PROJECT
, 'New page' );
800 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
802 $article = new Article( $title );
803 $page = $article->getPage();
804 $page->doEditContent( new WikitextContent( 'Some [[link]]' ), 'summary' );
806 // Tell Title it doesn't know whether it exists
807 $title->mArticleID
= -1;
809 // Tell the link cache it doesn't exists when it really does
810 $linkCache->clearLink( $title );
811 $linkCache->addBadLinkObj( $title );
816 'exists() should rely on link cache unless GAID_FOR_UPDATE is used'
820 $title->exists( Title
::GAID_FOR_UPDATE
),
821 'exists() should re-query database when GAID_FOR_UPDATE is used'
825 public function provideCanHaveTalkPage() {
827 'User page has talk page' => [
828 Title
::makeTitle( NS_USER
, 'Jane' ), true
830 'Talke page has talk page' => [
831 Title
::makeTitle( NS_TALK
, 'Foo' ), true
833 'Special page cannot have talk page' => [
834 Title
::makeTitle( NS_SPECIAL
, 'Thing' ), false
836 'Virtual namespace cannot have talk page' => [
837 Title
::makeTitle( NS_MEDIA
, 'Kitten.jpg' ), false
839 'Relative link has no talk page' => [
840 Title
::makeTitle( NS_MAIN
, '', 'Kittens' ), false
842 'Interwiki link has no talk page' => [
843 Title
::makeTitle( NS_MAIN
, 'Kittens', '', 'acme' ), false
848 public function provideIsWatchable() {
850 'User page is watchable' => [
851 Title
::makeTitle( NS_USER
, 'Jane' ), true
853 'Talke page is watchable' => [
854 Title
::makeTitle( NS_TALK
, 'Foo' ), true
856 'Special page is not watchable' => [
857 Title
::makeTitle( NS_SPECIAL
, 'Thing' ), false
859 'Virtual namespace is not watchable' => [
860 Title
::makeTitle( NS_MEDIA
, 'Kitten.jpg' ), false
862 'Relative link is not watchable' => [
863 Title
::makeTitle( NS_MAIN
, '', 'Kittens' ), false
865 'Interwiki link is not watchable' => [
866 Title
::makeTitle( NS_MAIN
, 'Kittens', '', 'acme' ), false
871 public static function provideGetTalkPage_good() {
873 [ Title
::makeTitle( NS_MAIN
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
874 [ Title
::makeTitle( NS_TALK
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
878 public static function provideGetTalkPage_bad() {
880 [ Title
::makeTitle( NS_SPECIAL
, 'Test' ) ],
881 [ Title
::makeTitle( NS_MEDIA
, 'Test' ) ],
882 [ Title
::makeTitle( NS_MAIN
, '', 'Kittens' ) ],
883 [ Title
::makeTitle( NS_MAIN
, 'Kittens', '', 'acme' ) ],
887 public static function provideGetSubjectPage_good() {
889 [ Title
::makeTitle( NS_TALK
, 'Test' ), Title
::makeTitle( NS_MAIN
, 'Test' ) ],
890 [ Title
::makeTitle( NS_MAIN
, 'Test' ), Title
::makeTitle( NS_MAIN
, 'Test' ) ],
894 public static function provideGetOtherPage_good() {
896 [ Title
::makeTitle( NS_MAIN
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
897 [ Title
::makeTitle( NS_TALK
, 'Test' ), Title
::makeTitle( NS_MAIN
, 'Test' ) ],
902 * @dataProvider provideCanHaveTalkPage
903 * @covers Title::canHaveTalkPage
905 * @param Title $title
906 * @param bool $expected
908 public function testCanHaveTalkPage( Title
$title, $expected ) {
909 $actual = $title->canHaveTalkPage();
910 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
914 * @dataProvider provideIsWatchable
915 * @covers Title::isWatchable
917 * @param Title $title
918 * @param bool $expected
920 public function testIsWatchable( Title
$title, $expected ) {
921 $actual = $title->canHaveTalkPage();
922 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
926 * @dataProvider provideGetTalkPage_good
927 * @covers Title::getTalkPageIfDefined
929 public function testGetTalkPage_good( Title
$title, Title
$expected ) {
930 $actual = $title->getTalkPage();
931 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
935 * @dataProvider provideGetTalkPage_bad
936 * @covers Title::getTalkPageIfDefined
938 public function testGetTalkPage_bad( Title
$title ) {
939 $this->setExpectedException( MWException
::class );
940 $title->getTalkPage();
944 * @dataProvider provideGetTalkPage_good
945 * @covers Title::getTalkPageIfDefined
947 public function testGetTalkPageIfDefined_good( Title
$title, Title
$expected ) {
948 $actual = $title->getTalkPageIfDefined();
949 $this->assertNotNull( $actual, $title->getPrefixedDBkey() );
950 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
954 * @dataProvider provideGetTalkPage_bad
955 * @covers Title::getTalkPageIfDefined
957 public function testGetTalkPageIfDefined_bad( Title
$title ) {
958 $talk = $title->getTalkPageIfDefined();
961 $title->getPrefixedDBkey()
966 * @dataProvider provideGetSubjectPage_good
967 * @covers Title::getSubjectPage
969 public function testGetSubjectPage_good( Title
$title, Title
$expected ) {
970 $actual = $title->getSubjectPage();
971 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
975 * @dataProvider provideGetOtherPage_good
976 * @covers Title::getOtherPage
978 public function testGetOtherPage_good( Title
$title, Title
$expected ) {
979 $actual = $title->getOtherPage();
980 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
984 * @dataProvider provideGetTalkPage_bad
985 * @covers Title::getOtherPage
987 public function testGetOtherPage_bad( Title
$title ) {
988 $this->setExpectedException( MWException
::class );
989 $title->getOtherPage();
992 public function provideCreateFragmentTitle() {
994 [ Title
::makeTitle( NS_MAIN
, 'Test' ), 'foo' ],
995 [ Title
::makeTitle( NS_TALK
, 'Test', 'foo' ), '' ],
996 [ Title
::makeTitle( NS_CATEGORY
, 'Test', 'foo' ), 'bar' ],
997 [ Title
::makeTitle( NS_MAIN
, 'Test1', '', 'interwiki' ), 'baz' ]
1002 * @covers Title::createFragmentTarget
1003 * @dataProvider provideCreateFragmentTitle
1005 public function testCreateFragmentTitle( Title
$title, $fragment ) {
1006 $this->mergeMwGlobalArrayValue( 'wgHooks', [
1007 'InterwikiLoadPrefix' => [
1008 function ( $prefix, &$iwdata ) {
1009 if ( $prefix === 'interwiki' ) {
1011 'iw_url' => 'http://example.com/',
1021 $fragmentTitle = $title->createFragmentTarget( $fragment );
1023 $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
1024 $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
1025 $this->assertEquals( $title->getInterwiki(), $fragmentTitle->getInterwiki() );
1026 $this->assertEquals( $fragment, $fragmentTitle->getFragment() );
1029 public function provideGetPrefixedText() {
1033 Title
::makeTitle( NS_MAIN
, 'Foo bar' ),
1038 Title
::makeTitle( NS_USER
, 'Foo bar' ),
1043 Title
::makeTitle( NS_USER_TALK
, 'Foo bar' ),
1046 // fragment not included
1048 Title
::makeTitle( NS_MAIN
, 'Foo bar', 'fragment' ),
1053 Title
::makeTitle( NS_MEDIA
, 'Foo bar' ),
1056 // non-existent namespace
1058 Title
::makeTitle( 100777, 'Foo bar' ),
1059 'Special:Badtitle/NS100777:Foo bar'
1065 * @covers Title::getPrefixedText
1066 * @dataProvider provideGetPrefixedText
1068 public function testGetPrefixedText( Title
$title, $expected ) {
1069 $this->assertEquals( $expected, $title->getPrefixedText() );
1072 public function provideGetPrefixedDBKey() {
1076 Title
::makeTitle( NS_MAIN
, 'Foo_bar' ),
1081 Title
::makeTitle( NS_USER
, 'Foo_bar' ),
1086 Title
::makeTitle( NS_USER_TALK
, 'Foo_bar' ),
1089 // fragment not included
1091 Title
::makeTitle( NS_MAIN
, 'Foo_bar', 'fragment' ),
1096 Title
::makeTitle( NS_MEDIA
, 'Foo_bar' ),
1099 // non-existent namespace
1101 Title
::makeTitle( 100777, 'Foo_bar' ),
1102 'Special:Badtitle/NS100777:Foo_bar'
1108 * @covers Title::getPrefixedDBKey
1109 * @dataProvider provideGetPrefixedDBKey
1111 public function testGetPrefixedDBKey( Title
$title, $expected ) {
1112 $this->assertEquals( $expected, $title->getPrefixedDBkey() );
1116 * @covers Title::getFragmentForURL
1117 * @dataProvider provideGetFragmentForURL
1119 * @param string $titleStr
1120 * @param string $expected
1122 public function testGetFragmentForURL( $titleStr, $expected ) {
1123 $this->setMwGlobals( [
1124 'wgFragmentMode' => [ 'html5' ],
1125 'wgExternalInterwikiFragmentMode' => 'legacy',
1127 $dbw = wfGetDB( DB_MASTER
);
1128 $dbw->insert( 'interwiki',
1131 'iw_prefix' => 'de',
1132 'iw_url' => 'http://de.wikipedia.org/wiki/',
1133 'iw_api' => 'http://de.wikipedia.org/w/api.php',
1134 'iw_wikiid' => 'dewiki',
1139 'iw_prefix' => 'zz',
1140 'iw_url' => 'http://zzwiki.org/wiki/',
1141 'iw_api' => 'http://zzwiki.org/w/api.php',
1142 'iw_wikiid' => 'zzwiki',
1151 $title = Title
::newFromText( $titleStr );
1152 self
::assertEquals( $expected, $title->getFragmentForURL() );
1154 $dbw->delete( 'interwiki', '*', __METHOD__
);
1157 public function provideGetFragmentForURL() {
1160 [ 'Foo#ümlåût', '#ümlåût' ],
1161 [ 'de:Foo#Bå®', '#Bå®' ],
1162 [ 'zz:Foo#тест', '#.D1.82.D0.B5.D1.81.D1.82' ],
1167 * @covers Title::isRawHtmlMessage
1168 * @dataProvider provideIsRawHtmlMessage
1170 public function testIsRawHtmlMessage( $textForm, $expected ) {
1171 $this->setMwGlobals( 'wgRawHtmlMessages', [
1177 $title = Title
::newFromText( $textForm );
1178 $this->assertSame( $expected, $title->isRawHtmlMessage() );
1181 public function provideIsRawHtmlMessage() {
1183 [ 'MediaWiki:Foobar', true ],
1184 [ 'MediaWiki:Foo bar', true ],
1185 [ 'MediaWiki:Foo-bar', true ],
1186 [ 'MediaWiki:foo bar', true ],
1187 [ 'MediaWiki:foo-bar', true ],
1188 [ 'MediaWiki:foobar', true ],
1189 [ 'MediaWiki:some-other-message', false ],
1190 [ 'Main Page', false ],
1194 public function provideEquals() {
1196 Title
::newFromText( 'Main Page' ),
1197 Title
::newFromText( 'Main Page' ),
1201 Title
::newFromText( 'Main Page' ),
1202 Title
::newFromText( 'Not The Main Page' ),
1206 Title
::newFromText( 'Main Page' ),
1207 Title
::newFromText( 'Project:Main Page' ),
1211 Title
::newFromText( 'File:Example.png' ),
1212 Title
::newFromText( 'Image:Example.png' ),
1216 Title
::newFromText( 'Special:Version' ),
1217 Title
::newFromText( 'Special:Version' ),
1221 Title
::newFromText( 'Special:Version' ),
1222 Title
::newFromText( 'Special:Recentchanges' ),
1226 Title
::newFromText( 'Special:Version' ),
1227 Title
::newFromText( 'Main Page' ),
1231 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1232 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1236 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1237 Title
::makeTitle( NS_MAIN
, 'Bar', '', '' ),
1241 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1242 Title
::makeTitle( NS_TALK
, 'Foo', '', '' ),
1246 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1247 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1251 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1252 Title
::makeTitle( NS_MAIN
, 'Foo', 'Baz', '' ),
1256 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1257 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1261 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1262 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1266 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1267 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1273 * @covers Title::equals
1274 * @dataProvider provideEquals
1276 public function testEquals( Title
$firstValue, /* LinkTarget */ $secondValue, $expectedSame ) {
1279 $firstValue->equals( $secondValue )
1284 * @covers Title::newMainPage
1286 public function testNewMainPage() {
1287 $msgCache = TestingAccessWrapper
::newFromClass( MessageCache
::class );
1288 $msgCache->instance
= $this->createMock( MessageCache
::class );
1289 $msgCache->instance
->method( 'get' )->willReturn( 'Foresheet' );
1290 $msgCache->instance
->method( 'transform' )->willReturn( 'Foresheet' );
1294 Title
::newMainPage()->getText()
1299 * @covers Title::newMainPage
1301 public function testNewMainPageWithLocal() {
1302 $local = $this->createMock( MessageLocalizer
::class );
1303 $local->method( 'msg' )->willReturn( new RawMessage( 'Prime Article' ) );
1307 Title
::newMainPage( $local )->getText()