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 ' ],
557 [ 'Talk:////', '////' ],
558 [ 'Template:////', '////' ],
559 [ 'Template:Foo////', 'Foo' ],
560 [ 'Template:Foo////Bar', 'Foo' ],
565 * @todo Handle $wgNamespacesWithSubpages cases
566 * @dataProvider provideSubpageTitleCases
567 * @covers Title::getSubpageText
569 public function testGetSubpageText( $title, $expected, $msg = '' ) {
570 $title = Title
::newFromText( $title );
571 $this->assertEquals( $expected,
572 $title->getSubpageText(),
577 public static function provideSubpageTitleCases() {
579 # Title, expected base, optional message
580 [ 'User:John_Doe/subOne/subTwo', 'subTwo' ],
581 [ 'User:John_Doe/subOne', 'subOne' ],
585 public function provideSubpage() {
586 // NOTE: avoid constructing Title objects in the provider, since it may access the database.
588 [ 'Foo', 'x', new TitleValue( NS_MAIN
, 'Foo/x' ) ],
589 [ 'Foo#bar', 'x', new TitleValue( NS_MAIN
, 'Foo/x' ) ],
590 [ 'User:Foo', 'x', new TitleValue( NS_USER
, 'Foo/x' ) ],
591 [ 'wiki:User:Foo', 'x', new TitleValue( NS_MAIN
, 'User:Foo/x', '', 'wiki' ) ],
596 * @dataProvider provideSubpage
597 * @covers Title::getSubpage
599 public function testSubpage( $title, $sub, LinkTarget
$expected ) {
600 $interwikiLookup = $this->getMock( InterwikiLookup
::class );
601 $interwikiLookup->expects( $this->any() )
602 ->method( 'isValidInterwiki' )
603 ->willReturnCallback(
604 function ( $prefix ) {
605 return $prefix == 'wiki';
609 $this->setService( 'InterwikiLookup', $interwikiLookup );
611 $title = Title
::newFromText( $title );
612 $expected = Title
::newFromLinkTarget( $expected );
613 $actual = $title->getSubpage( $sub );
615 // NOTE: convert to string for comparison
616 $this->assertSame( $expected->getPrefixedText(), $actual->getPrefixedText(), 'text form' );
617 $this->assertTrue( $expected->equals( $actual ), 'Title equality' );
620 public static function provideNewFromTitleValue() {
622 [ new TitleValue( NS_MAIN
, 'Foo' ) ],
623 [ new TitleValue( NS_MAIN
, 'Foo', 'bar' ) ],
624 [ new TitleValue( NS_USER
, 'Hansi_Maier' ) ],
629 * @covers Title::newFromTitleValue
630 * @dataProvider provideNewFromTitleValue
632 public function testNewFromTitleValue( TitleValue
$value ) {
633 $title = Title
::newFromTitleValue( $value );
635 $dbkey = str_replace( ' ', '_', $value->getText() );
636 $this->assertEquals( $dbkey, $title->getDBkey() );
637 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
638 $this->assertEquals( $value->getFragment(), $title->getFragment() );
642 * @covers Title::newFromLinkTarget
643 * @dataProvider provideNewFromTitleValue
645 public function testNewFromLinkTarget( LinkTarget
$value ) {
646 $title = Title
::newFromLinkTarget( $value );
648 $dbkey = str_replace( ' ', '_', $value->getText() );
649 $this->assertEquals( $dbkey, $title->getDBkey() );
650 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
651 $this->assertEquals( $value->getFragment(), $title->getFragment() );
655 * @covers Title::newFromLinkTarget
657 public function testNewFromLinkTarget_clone() {
658 $title = Title
::newFromText( __METHOD__
);
659 $this->assertSame( $title, Title
::newFromLinkTarget( $title ) );
661 // The Title::NEW_CLONE flag should ensure that a fresh instance is returned.
662 $clone = Title
::newFromLinkTarget( $title, Title
::NEW_CLONE
);
663 $this->assertNotSame( $title, $clone );
664 $this->assertTrue( $clone->equals( $title ) );
667 public function provideCastFromLinkTarget() {
668 return array_merge( [ [ null ] ], self
::provideNewFromTitleValue() );
672 * @covers Title::castFromLinkTarget
673 * @dataProvider provideCastFromLinkTarget
675 public function testCastFromLinkTarget( $value ) {
676 $title = Title
::castFromLinkTarget( $value );
678 if ( $value === null ) {
679 $this->assertNull( $title );
681 $dbkey = str_replace( ' ', '_', $value->getText() );
682 $this->assertSame( $dbkey, $title->getDBkey() );
683 $this->assertSame( $value->getNamespace(), $title->getNamespace() );
684 $this->assertSame( $value->getFragment(), $title->getFragment() );
688 public static function provideGetTitleValue() {
692 [ 'User:Hansi_Maier' ],
697 * @covers Title::getTitleValue
698 * @dataProvider provideGetTitleValue
700 public function testGetTitleValue( $text ) {
701 $title = Title
::newFromText( $text );
702 $value = $title->getTitleValue();
704 $dbkey = str_replace( ' ', '_', $value->getText() );
705 $this->assertEquals( $title->getDBkey(), $dbkey );
706 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
707 $this->assertEquals( $title->getFragment(), $value->getFragment() );
710 public static function provideGetFragment() {
713 [ 'Foo#bar', 'bar' ],
714 [ 'Foo#bär', 'bär' ],
716 // Inner whitespace is normalized
717 [ 'Foo#bar_bar', 'bar bar' ],
718 [ 'Foo#bar bar', 'bar bar' ],
719 [ 'Foo#bar bar', 'bar bar' ],
721 // Leading whitespace is kept, trailing whitespace is trimmed.
722 // XXX: Is this really want we want?
723 [ 'Foo#_bar_bar_', ' bar bar' ],
724 [ 'Foo# bar bar ', ' bar bar' ],
729 * @covers Title::getFragment
730 * @dataProvider provideGetFragment
732 * @param string $full
733 * @param string $fragment
735 public function testGetFragment( $full, $fragment ) {
736 $title = Title
::newFromText( $full );
737 $this->assertEquals( $fragment, $title->getFragment() );
741 * @covers Title::isAlwaysKnown
742 * @dataProvider provideIsAlwaysKnown
743 * @param string $page
744 * @param bool $isKnown
746 public function testIsAlwaysKnown( $page, $isKnown ) {
747 $title = Title
::newFromText( $page );
748 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
751 public static function provideIsAlwaysKnown() {
753 [ 'Some nonexistent page', false ],
756 [ 'Special:BlankPage', true ],
757 [ 'Special:SomeNonexistentSpecialPage', false ],
758 [ 'MediaWiki:Parentheses', true ],
759 [ 'MediaWiki:Some nonexistent message', false ],
764 * @covers Title::isValid
765 * @dataProvider provideIsValid
766 * @param Title $title
767 * @param bool $isValid
769 public function testIsValid( Title
$title, $isValid ) {
770 $this->assertEquals( $isValid, $title->isValid(), $title->getPrefixedText() );
773 public static function provideIsValid() {
775 [ Title
::makeTitle( NS_MAIN
, '' ), false ],
776 [ Title
::makeTitle( NS_MAIN
, '<>' ), false ],
777 [ Title
::makeTitle( NS_MAIN
, '|' ), false ],
778 [ Title
::makeTitle( NS_MAIN
, '#' ), false ],
779 [ Title
::makeTitle( NS_MAIN
, 'Test' ), true ],
780 [ Title
::makeTitle( NS_MAIN
, ' Test' ), false ],
781 [ Title
::makeTitle( NS_MAIN
, '_Test' ), false ],
782 [ Title
::makeTitle( NS_MAIN
, 'Test ' ), false ],
783 [ Title
::makeTitle( NS_MAIN
, 'Test_' ), false ],
784 [ Title
::makeTitle( NS_MAIN
, "Test\nthis" ), false ],
785 [ Title
::makeTitle( NS_MAIN
, "Test\tthis" ), false ],
786 [ Title
::makeTitle( -33, 'Test' ), false ],
787 [ Title
::makeTitle( 77663399, 'Test' ), false ],
792 * @covers Title::isAlwaysKnown
794 public function testIsAlwaysKnownOnInterwiki() {
795 $title = Title
::makeTitle( NS_MAIN
, 'Interwiki link', '', 'externalwiki' );
796 $this->assertTrue( $title->isAlwaysKnown() );
800 * @covers Title::exists
802 public function testExists() {
803 $title = Title
::makeTitle( NS_PROJECT
, 'New page' );
804 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
806 $article = new Article( $title );
807 $page = $article->getPage();
808 $page->doEditContent( new WikitextContent( 'Some [[link]]' ), 'summary' );
810 // Tell Title it doesn't know whether it exists
811 $title->mArticleID
= -1;
813 // Tell the link cache it doesn't exist when it really does
814 $linkCache->clearLink( $title );
815 $linkCache->addBadLinkObj( $title );
820 'exists() should rely on link cache unless GAID_FOR_UPDATE is used'
824 $title->exists( Title
::GAID_FOR_UPDATE
),
825 'exists() should re-query database when GAID_FOR_UPDATE is used'
829 public function provideCanHaveTalkPage() {
831 'User page has talk page' => [
832 Title
::makeTitle( NS_USER
, 'Jane' ), true
834 'Talke page has talk page' => [
835 Title
::makeTitle( NS_TALK
, 'Foo' ), true
837 'Special page cannot have talk page' => [
838 Title
::makeTitle( NS_SPECIAL
, 'Thing' ), false
840 'Virtual namespace cannot have talk page' => [
841 Title
::makeTitle( NS_MEDIA
, 'Kitten.jpg' ), false
843 'Relative link has no talk page' => [
844 Title
::makeTitle( NS_MAIN
, '', 'Kittens' ), false
846 'Interwiki link has no talk page' => [
847 Title
::makeTitle( NS_MAIN
, 'Kittens', '', 'acme' ), false
852 public function provideIsWatchable() {
854 'User page is watchable' => [
855 Title
::makeTitle( NS_USER
, 'Jane' ), true
857 'Talke page is watchable' => [
858 Title
::makeTitle( NS_TALK
, 'Foo' ), true
860 'Special page is not watchable' => [
861 Title
::makeTitle( NS_SPECIAL
, 'Thing' ), false
863 'Virtual namespace is not watchable' => [
864 Title
::makeTitle( NS_MEDIA
, 'Kitten.jpg' ), false
866 'Relative link is not watchable' => [
867 Title
::makeTitle( NS_MAIN
, '', 'Kittens' ), false
869 'Interwiki link is not watchable' => [
870 Title
::makeTitle( NS_MAIN
, 'Kittens', '', 'acme' ), false
875 public static function provideGetTalkPage_good() {
877 [ Title
::makeTitle( NS_MAIN
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
878 [ Title
::makeTitle( NS_TALK
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
882 public static function provideGetTalkPage_bad() {
884 [ Title
::makeTitle( NS_SPECIAL
, 'Test' ) ],
885 [ Title
::makeTitle( NS_MEDIA
, 'Test' ) ],
886 [ Title
::makeTitle( NS_MAIN
, '', 'Kittens' ) ],
887 [ Title
::makeTitle( NS_MAIN
, 'Kittens', '', 'acme' ) ],
891 public static function provideGetSubjectPage_good() {
893 [ Title
::makeTitle( NS_TALK
, 'Test' ), Title
::makeTitle( NS_MAIN
, 'Test' ) ],
894 [ Title
::makeTitle( NS_MAIN
, 'Test' ), Title
::makeTitle( NS_MAIN
, 'Test' ) ],
898 public static function provideGetOtherPage_good() {
900 [ Title
::makeTitle( NS_MAIN
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
901 [ Title
::makeTitle( NS_TALK
, 'Test' ), Title
::makeTitle( NS_MAIN
, 'Test' ) ],
906 * @dataProvider provideCanHaveTalkPage
907 * @covers Title::canHaveTalkPage
909 * @param Title $title
910 * @param bool $expected
912 public function testCanHaveTalkPage( Title
$title, $expected ) {
913 $actual = $title->canHaveTalkPage();
914 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
918 * @dataProvider provideIsWatchable
919 * @covers Title::isWatchable
921 * @param Title $title
922 * @param bool $expected
924 public function testIsWatchable( Title
$title, $expected ) {
925 $actual = $title->canHaveTalkPage();
926 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
930 * @dataProvider provideGetTalkPage_good
931 * @covers Title::getTalkPageIfDefined
933 public function testGetTalkPage_good( Title
$title, Title
$expected ) {
934 $actual = $title->getTalkPage();
935 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
939 * @dataProvider provideGetTalkPage_bad
940 * @covers Title::getTalkPageIfDefined
942 public function testGetTalkPage_bad( Title
$title ) {
943 $this->setExpectedException( MWException
::class );
944 $title->getTalkPage();
948 * @dataProvider provideGetTalkPage_good
949 * @covers Title::getTalkPageIfDefined
951 public function testGetTalkPageIfDefined_good( Title
$title, Title
$expected ) {
952 $actual = $title->getTalkPageIfDefined();
953 $this->assertNotNull( $actual, $title->getPrefixedDBkey() );
954 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
958 * @dataProvider provideGetTalkPage_bad
959 * @covers Title::getTalkPageIfDefined
961 public function testGetTalkPageIfDefined_bad( Title
$title ) {
962 $talk = $title->getTalkPageIfDefined();
965 $title->getPrefixedDBkey()
970 * @dataProvider provideGetSubjectPage_good
971 * @covers Title::getSubjectPage
973 public function testGetSubjectPage_good( Title
$title, Title
$expected ) {
974 $actual = $title->getSubjectPage();
975 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
979 * @dataProvider provideGetOtherPage_good
980 * @covers Title::getOtherPage
982 public function testGetOtherPage_good( Title
$title, Title
$expected ) {
983 $actual = $title->getOtherPage();
984 $this->assertTrue( $expected->equals( $actual ), $title->getPrefixedDBkey() );
988 * @dataProvider provideGetTalkPage_bad
989 * @covers Title::getOtherPage
991 public function testGetOtherPage_bad( Title
$title ) {
992 $this->setExpectedException( MWException
::class );
993 $title->getOtherPage();
996 public function provideCreateFragmentTitle() {
998 [ Title
::makeTitle( NS_MAIN
, 'Test' ), 'foo' ],
999 [ Title
::makeTitle( NS_TALK
, 'Test', 'foo' ), '' ],
1000 [ Title
::makeTitle( NS_CATEGORY
, 'Test', 'foo' ), 'bar' ],
1001 [ Title
::makeTitle( NS_MAIN
, 'Test1', '', 'interwiki' ), 'baz' ]
1006 * @covers Title::createFragmentTarget
1007 * @dataProvider provideCreateFragmentTitle
1009 public function testCreateFragmentTitle( Title
$title, $fragment ) {
1010 $this->mergeMwGlobalArrayValue( 'wgHooks', [
1011 'InterwikiLoadPrefix' => [
1012 function ( $prefix, &$iwdata ) {
1013 if ( $prefix === 'interwiki' ) {
1015 'iw_url' => 'http://example.com/',
1025 $fragmentTitle = $title->createFragmentTarget( $fragment );
1027 $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
1028 $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
1029 $this->assertEquals( $title->getInterwiki(), $fragmentTitle->getInterwiki() );
1030 $this->assertEquals( $fragment, $fragmentTitle->getFragment() );
1033 public function provideGetPrefixedText() {
1037 Title
::makeTitle( NS_MAIN
, 'Foo bar' ),
1042 Title
::makeTitle( NS_USER
, 'Foo bar' ),
1047 Title
::makeTitle( NS_USER_TALK
, 'Foo bar' ),
1050 // fragment not included
1052 Title
::makeTitle( NS_MAIN
, 'Foo bar', 'fragment' ),
1057 Title
::makeTitle( NS_MEDIA
, 'Foo bar' ),
1060 // non-existent namespace
1062 Title
::makeTitle( 100777, 'Foo bar' ),
1063 'Special:Badtitle/NS100777:Foo bar'
1069 * @covers Title::getPrefixedText
1070 * @dataProvider provideGetPrefixedText
1072 public function testGetPrefixedText( Title
$title, $expected ) {
1073 $this->assertEquals( $expected, $title->getPrefixedText() );
1076 public function provideGetPrefixedDBKey() {
1080 Title
::makeTitle( NS_MAIN
, 'Foo_bar' ),
1085 Title
::makeTitle( NS_USER
, 'Foo_bar' ),
1090 Title
::makeTitle( NS_USER_TALK
, 'Foo_bar' ),
1093 // fragment not included
1095 Title
::makeTitle( NS_MAIN
, 'Foo_bar', 'fragment' ),
1100 Title
::makeTitle( NS_MEDIA
, 'Foo_bar' ),
1103 // non-existent namespace
1105 Title
::makeTitle( 100777, 'Foo_bar' ),
1106 'Special:Badtitle/NS100777:Foo_bar'
1112 * @covers Title::getPrefixedDBKey
1113 * @dataProvider provideGetPrefixedDBKey
1115 public function testGetPrefixedDBKey( Title
$title, $expected ) {
1116 $this->assertEquals( $expected, $title->getPrefixedDBkey() );
1120 * @covers Title::getFragmentForURL
1121 * @dataProvider provideGetFragmentForURL
1123 * @param string $titleStr
1124 * @param string $expected
1126 public function testGetFragmentForURL( $titleStr, $expected ) {
1127 $this->setMwGlobals( [
1128 'wgFragmentMode' => [ 'html5' ],
1129 'wgExternalInterwikiFragmentMode' => 'legacy',
1131 $dbw = wfGetDB( DB_MASTER
);
1132 $dbw->insert( 'interwiki',
1135 'iw_prefix' => 'de',
1136 'iw_url' => 'http://de.wikipedia.org/wiki/',
1137 'iw_api' => 'http://de.wikipedia.org/w/api.php',
1138 'iw_wikiid' => 'dewiki',
1143 'iw_prefix' => 'zz',
1144 'iw_url' => 'http://zzwiki.org/wiki/',
1145 'iw_api' => 'http://zzwiki.org/w/api.php',
1146 'iw_wikiid' => 'zzwiki',
1155 $title = Title
::newFromText( $titleStr );
1156 self
::assertEquals( $expected, $title->getFragmentForURL() );
1158 $dbw->delete( 'interwiki', '*', __METHOD__
);
1161 public function provideGetFragmentForURL() {
1164 [ 'Foo#ümlåût', '#ümlåût' ],
1165 [ 'de:Foo#Bå®', '#Bå®' ],
1166 [ 'zz:Foo#тест', '#.D1.82.D0.B5.D1.81.D1.82' ],
1171 * @covers Title::isRawHtmlMessage
1172 * @dataProvider provideIsRawHtmlMessage
1174 public function testIsRawHtmlMessage( $textForm, $expected ) {
1175 $this->setMwGlobals( 'wgRawHtmlMessages', [
1181 $title = Title
::newFromText( $textForm );
1182 $this->assertSame( $expected, $title->isRawHtmlMessage() );
1185 public function provideIsRawHtmlMessage() {
1187 [ 'MediaWiki:Foobar', true ],
1188 [ 'MediaWiki:Foo bar', true ],
1189 [ 'MediaWiki:Foo-bar', true ],
1190 [ 'MediaWiki:foo bar', true ],
1191 [ 'MediaWiki:foo-bar', true ],
1192 [ 'MediaWiki:foobar', true ],
1193 [ 'MediaWiki:some-other-message', false ],
1194 [ 'Main Page', false ],
1198 public function provideEquals() {
1200 Title
::newFromText( 'Main Page' ),
1201 Title
::newFromText( 'Main Page' ),
1205 Title
::newFromText( 'Main Page' ),
1206 Title
::newFromText( 'Not The Main Page' ),
1210 Title
::newFromText( 'Main Page' ),
1211 Title
::newFromText( 'Project:Main Page' ),
1215 Title
::newFromText( 'File:Example.png' ),
1216 Title
::newFromText( 'Image:Example.png' ),
1220 Title
::newFromText( 'Special:Version' ),
1221 Title
::newFromText( 'Special:Version' ),
1225 Title
::newFromText( 'Special:Version' ),
1226 Title
::newFromText( 'Special:Recentchanges' ),
1230 Title
::newFromText( 'Special:Version' ),
1231 Title
::newFromText( 'Main Page' ),
1235 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1236 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1240 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1241 Title
::makeTitle( NS_MAIN
, 'Bar', '', '' ),
1245 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1246 Title
::makeTitle( NS_TALK
, 'Foo', '', '' ),
1250 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1251 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1255 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1256 Title
::makeTitle( NS_MAIN
, 'Foo', 'Baz', '' ),
1260 Title
::makeTitle( NS_MAIN
, 'Foo', 'Bar', '' ),
1261 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1265 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1266 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1270 Title
::makeTitle( NS_MAIN
, 'Foo', '', '' ),
1271 Title
::makeTitle( NS_MAIN
, 'Foo', '', 'baz' ),
1277 * @covers Title::equals
1278 * @dataProvider provideEquals
1280 public function testEquals( Title
$firstValue, /* LinkTarget */ $secondValue, $expectedSame ) {
1283 $firstValue->equals( $secondValue )
1288 * @covers Title::newMainPage
1290 public function testNewMainPage() {
1291 $msgCache = TestingAccessWrapper
::newFromClass( MessageCache
::class );
1292 $msgCache->instance
= $this->createMock( MessageCache
::class );
1293 $msgCache->instance
->method( 'get' )->willReturn( 'Foresheet' );
1294 $msgCache->instance
->method( 'transform' )->willReturn( 'Foresheet' );
1298 Title
::newMainPage()->getText()
1303 * @covers Title::newMainPage
1305 public function testNewMainPageWithLocal() {
1306 $local = $this->createMock( MessageLocalizer
::class );
1307 $local->method( 'msg' )->willReturn( new RawMessage( 'Prime Article' ) );
1311 Title
::newMainPage( $local )->getText()