3 use MediaWiki\MediaWikiServices
;
9 class TitleTest
extends MediaWikiTestCase
{
10 protected function setUp() {
13 $this->setMwGlobals( [
14 'wgAllowUserJs' => false,
15 'wgDefaultLanguageVariant' => false,
16 'wgMetaNamespace' => 'Project',
18 $this->setUserLang( 'en' );
19 $this->setContentLang( 'en' );
23 * @covers Title::legalChars
25 public function testLegalChars() {
26 $titlechars = Title
::legalChars();
28 foreach ( range( 1, 255 ) as $num ) {
30 if ( strpos( "#[]{}<>|", $chr ) !== false ||
preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
32 (bool)preg_match( "/[$titlechars]/", $chr ),
33 "chr($num) = $chr is not a valid titlechar"
37 (bool)preg_match( "/[$titlechars]/", $chr ),
38 "chr($num) = $chr is a valid titlechar"
44 public static function provideValidSecureAndSplit() {
55 [ 'Talk:Foo:Sandbox' ],
56 [ 'File:Example.svg' ],
57 [ 'File_talk:Example.svg' ],
58 [ 'Foo/.../Sandbox' ],
62 // Length is 256 total, but only title part matters
63 [ 'Category:' . str_repeat( 'x', 248 ) ],
64 [ str_repeat( 'x', 252 ) ],
66 [ 'localtestiw: #anchor' ],
68 [ 'localtestiw:foo' ],
69 [ 'localtestiw: foo # anchor' ],
70 [ 'localtestiw: Talk: Sandbox # anchor' ],
72 [ 'remotetestiw: Talk: # anchor' ],
73 [ 'remotetestiw: #bar' ],
74 [ 'remotetestiw: Talk:' ],
75 [ 'remotetestiw: Talk: Foo' ],
76 [ 'localtestiw:remotetestiw:' ],
77 [ 'localtestiw:remotetestiw:foo' ]
81 public static function provideInvalidSecureAndSplit() {
83 [ '', 'title-invalid-empty' ],
84 [ ':', 'title-invalid-empty' ],
85 [ '__ __', 'title-invalid-empty' ],
86 [ ' __ ', 'title-invalid-empty' ],
87 // Bad characters forbidden regardless of wgLegalTitleChars
88 [ 'A [ B', 'title-invalid-characters' ],
89 [ 'A ] B', 'title-invalid-characters' ],
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 \t B", 'title-invalid-characters' ],
96 [ "A \n B", 'title-invalid-characters' ],
98 [ 'A%20B', 'title-invalid-characters' ],
99 [ 'A%23B', 'title-invalid-characters' ],
100 [ 'A%2523B', 'title-invalid-characters' ],
101 // XML/HTML character entity references
102 // Note: Commented out because they are not marked invalid by the PHP test as
103 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
107 // Subject of NS_TALK does not roundtrip to NS_MAIN
108 [ 'Talk:File:Example.svg', 'title-invalid-talk-namespace' ],
109 // Directory navigation
110 [ '.', 'title-invalid-relative' ],
111 [ '..', 'title-invalid-relative' ],
112 [ './Sandbox', 'title-invalid-relative' ],
113 [ '../Sandbox', 'title-invalid-relative' ],
114 [ 'Foo/./Sandbox', 'title-invalid-relative' ],
115 [ 'Foo/../Sandbox', 'title-invalid-relative' ],
116 [ 'Sandbox/.', 'title-invalid-relative' ],
117 [ 'Sandbox/..', 'title-invalid-relative' ],
119 [ 'A ~~~ Name', 'title-invalid-magic-tilde' ],
120 [ 'A ~~~~ Signature', 'title-invalid-magic-tilde' ],
121 [ 'A ~~~~~ Timestamp', 'title-invalid-magic-tilde' ],
123 [ str_repeat( 'x', 256 ), 'title-invalid-too-long' ],
124 // Namespace prefix without actual title
125 [ 'Talk:', 'title-invalid-empty' ],
126 [ 'Talk:#', 'title-invalid-empty' ],
127 [ 'Category: ', 'title-invalid-empty' ],
128 [ 'Category: #bar', 'title-invalid-empty' ],
130 [ 'localtestiw: Talk: # anchor', 'title-invalid-empty' ],
131 [ 'localtestiw: Talk:', 'title-invalid-empty' ]
135 private function secureAndSplitGlobals() {
136 $this->setMwGlobals( [
137 'wgLocalInterwikis' => [ 'localtestiw' ],
139 'InterwikiLoadPrefix' => [
140 function ( $prefix, &$data ) {
141 if ( $prefix === 'localtestiw' ) {
142 $data = [ 'iw_url' => 'localtestiw' ];
143 } elseif ( $prefix === 'remotetestiw' ) {
144 $data = [ 'iw_url' => 'remotetestiw' ];
152 // Reset TitleParser since we modified $wgLocalInterwikis
153 $this->setService( 'TitleParser', new MediaWikiTitleCodec(
154 Language
::factory( 'en' ),
161 * See also mediawiki.Title.test.js
162 * @covers Title::secureAndSplit
163 * @dataProvider provideValidSecureAndSplit
164 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
166 public function testSecureAndSplitValid( $text ) {
167 $this->secureAndSplitGlobals();
168 $this->assertInstanceOf( Title
::class, Title
::newFromText( $text ), "Valid: $text" );
172 * See also mediawiki.Title.test.js
173 * @covers Title::secureAndSplit
174 * @dataProvider provideInvalidSecureAndSplit
175 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
177 public function testSecureAndSplitInvalid( $text, $expectedErrorMessage ) {
178 $this->secureAndSplitGlobals();
180 Title
::newFromTextThrow( $text ); // should throw
181 $this->assertTrue( false, "Invalid: $text" );
182 } catch ( MalformedTitleException
$ex ) {
183 $this->assertEquals( $expectedErrorMessage, $ex->getErrorMessage(), "Invalid: $text" );
187 public static function provideConvertByteClassToUnicodeClass() {
190 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
191 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
195 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
198 'QWERTY\\x66-\\xFD+',
199 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
207 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
210 'QWERTY\\x66-\\x80+\\x23',
211 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
214 'QWERTY\\x66-\\x80+\\xD3',
215 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
219 '\\\\\\u0080-\\uFFFF',
223 '\\-\\u0080-\\uFFFF',
227 'QWERTY\\-\\u0080-\\uFFFF',
235 'A-\\x7F\\u0080-\\uFFFF',
238 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
239 'f-wQWERTYFXZ\\u0080-\\uFFFF',
242 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
243 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
249 * @dataProvider provideConvertByteClassToUnicodeClass
250 * @covers Title::convertByteClassToUnicodeClass
252 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
253 $this->assertEquals( $unicodeClass, Title
::convertByteClassToUnicodeClass( $byteClass ) );
257 * @dataProvider provideSpecialNamesWithAndWithoutParameter
258 * @covers Title::fixSpecialName
260 public function testFixSpecialNameRetainsParameter( $text, $expectedParam ) {
261 $title = Title
::newFromText( $text );
262 $fixed = $title->fixSpecialName();
263 $stuff = explode( '/', $fixed->getDBkey(), 2 );
264 if ( count( $stuff ) == 2 ) {
272 "T33100 regression check: Title->fixSpecialName() should preserve parameter"
276 public static function provideSpecialNamesWithAndWithoutParameter() {
278 [ 'Special:Version', null ],
279 [ 'Special:Version/', '' ],
280 [ 'Special:Version/param', 'param' ],
285 * Auth-less test of Title::isValidMoveOperation
287 * @param string $source
288 * @param string $target
289 * @param array|string|bool $expected Required error
290 * @dataProvider provideTestIsValidMoveOperation
291 * @covers Title::isValidMoveOperation
292 * @covers Title::validateFileMoveOperation
294 public function testIsValidMoveOperation( $source, $target, $expected ) {
295 $this->setMwGlobals( 'wgContentHandlerUseDB', false );
296 $title = Title
::newFromText( $source );
297 $nt = Title
::newFromText( $target );
298 $errors = $title->isValidMoveOperation( $nt, false );
299 if ( $expected === true ) {
300 $this->assertTrue( $errors );
302 $errors = $this->flattenErrorsArray( $errors );
303 foreach ( (array)$expected as $error ) {
304 $this->assertContains( $error, $errors );
309 public static function provideTestIsValidMoveOperation() {
311 // for Title::isValidMoveOperation
312 [ 'Some page', '', 'badtitletext' ],
313 [ 'Test', 'Test', 'selfmove' ],
314 [ 'Special:FooBar', 'Test', 'immobile-source-namespace' ],
315 [ 'Test', 'Special:FooBar', 'immobile-target-namespace' ],
316 [ 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ],
317 [ 'Page', 'File:Test.jpg', 'nonfile-cannot-move-to-file' ],
318 // for Title::validateFileMoveOperation
319 [ 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ],
324 * Auth-less test of Title::userCan
326 * @param array $whitelistRegexp
327 * @param string $source
328 * @param string $action
329 * @param array|string|bool $expected Required error
331 * @covers Title::checkReadPermissions
332 * @dataProvider dataWgWhitelistReadRegexp
334 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
335 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
336 // usually have only one regex, it is more concise to write the lonely regex
337 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
339 if ( is_string( $whitelistRegexp ) ) {
340 $whitelistRegexp = [ $whitelistRegexp ];
343 $this->setMwGlobals( [
344 // So User::isEveryoneAllowed( 'read' ) === false
345 'wgGroupPermissions' => [ '*' => [ 'read' => false ] ],
346 'wgWhitelistRead' => [ 'some random non sense title' ],
347 'wgWhitelistReadRegexp' => $whitelistRegexp,
350 $title = Title
::newFromDBkey( $source );
352 // New anonymous user with no rights
355 $errors = $title->userCan( $action, $user );
357 if ( is_bool( $expected ) ) {
358 # Forge the assertion message depending on the assertion expectation
359 $allowableness = $expected
360 ?
" should be allowed"
361 : " should NOT be allowed";
365 "User action '$action' on [[$source]] $allowableness."
368 $errors = $this->flattenErrorsArray( $errors );
369 foreach ( (array)$expected as $error ) {
370 $this->assertContains( $error, $errors );
376 * Provides test parameter values for testWgWhitelistReadRegexp()
378 public function dataWgWhitelistReadRegexp() {
383 // Everything, if this doesn't work, we're really in trouble
384 [ '/.*/', 'Main_Page', 'read', $ALLOWED ],
385 [ '/.*/', 'Main_Page', 'edit', $DISALLOWED ],
387 // We validate against the title name, not the db key
388 [ '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ],
390 [ '/^Main/', 'Main_Page', 'read', $ALLOWED ],
391 [ '/^Main.*/', 'Main_Page', 'read', $ALLOWED ],
393 [ '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ],
395 // ...without unicode modifier
396 [ '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ],
397 // ...with unicode modifier
398 [ '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ],
400 [ '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ],
401 [ '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ],
403 // From DefaultSettings.php:
404 [ "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ],
405 [ "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ],
408 [ '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ],
409 [ null, 'Special:Newpages', 'read', $DISALLOWED ],
414 public function flattenErrorsArray( $errors ) {
416 foreach ( $errors as $error ) {
417 $result[] = $error[0];
424 * @dataProvider provideGetPageViewLanguage
425 * @covers Title::getPageViewLanguage
427 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
428 $lang, $variant, $msg = ''
430 // Setup environnement for this test
431 $this->setMwGlobals( [
432 'wgDefaultLanguageVariant' => $variant,
433 'wgAllowUserJs' => true,
435 $this->setUserLang( $lang );
436 $this->setContentLang( $contLang );
438 $title = Title
::newFromText( $titleText );
439 $this->assertInstanceOf( Title
::class, $title,
440 "Test must be passed a valid title text, you gave '$titleText'"
442 $this->assertEquals( $expected,
443 $title->getPageViewLanguage()->getCode(),
448 public static function provideGetPageViewLanguage() {
452 # - content language (expected in most cases)
453 # - wgLang (on some specific pages)
454 # - wgDefaultLanguageVariant
457 [ 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ],
458 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ],
459 [ 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ],
461 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ],
462 [ 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ],
463 [ 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ],
464 [ 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ],
465 [ 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ],
466 [ 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ],
467 [ 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ],
468 [ 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ],
470 [ 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ],
471 [ 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ],
472 [ 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ],
473 [ 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ],
474 [ 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ],
475 [ 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ],
476 [ 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ],
477 [ 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ],
478 [ 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ],
479 [ 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ],
481 [ 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ],
482 [ 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ],
488 * @dataProvider provideBaseTitleCases
489 * @covers Title::getBaseText
491 public function testGetBaseText( $title, $expected, $msg = '' ) {
492 $title = Title
::newFromText( $title );
493 $this->assertEquals( $expected,
494 $title->getBaseText(),
499 public static function provideBaseTitleCases() {
501 # Title, expected base, optional message
502 [ 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ],
503 [ 'User:Foo/Bar/Baz', 'Foo/Bar' ],
508 * @dataProvider provideRootTitleCases
509 * @covers Title::getRootText
511 public function testGetRootText( $title, $expected, $msg = '' ) {
512 $title = Title
::newFromText( $title );
513 $this->assertEquals( $expected,
514 $title->getRootText(),
519 public static function provideRootTitleCases() {
521 # Title, expected base, optional message
522 [ 'User:John_Doe/subOne/subTwo', 'John Doe' ],
523 [ 'User:Foo/Bar/Baz', 'Foo' ],
528 * @todo Handle $wgNamespacesWithSubpages cases
529 * @dataProvider provideSubpageTitleCases
530 * @covers Title::getSubpageText
532 public function testGetSubpageText( $title, $expected, $msg = '' ) {
533 $title = Title
::newFromText( $title );
534 $this->assertEquals( $expected,
535 $title->getSubpageText(),
540 public static function provideSubpageTitleCases() {
542 # Title, expected base, optional message
543 [ 'User:John_Doe/subOne/subTwo', 'subTwo' ],
544 [ 'User:John_Doe/subOne', 'subOne' ],
548 public static function provideNewFromTitleValue() {
550 [ new TitleValue( NS_MAIN
, 'Foo' ) ],
551 [ new TitleValue( NS_MAIN
, 'Foo', 'bar' ) ],
552 [ new TitleValue( NS_USER
, 'Hansi_Maier' ) ],
557 * @covers Title::newFromTitleValue
558 * @dataProvider provideNewFromTitleValue
560 public function testNewFromTitleValue( TitleValue
$value ) {
561 $title = Title
::newFromTitleValue( $value );
563 $dbkey = str_replace( ' ', '_', $value->getText() );
564 $this->assertEquals( $dbkey, $title->getDBkey() );
565 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
566 $this->assertEquals( $value->getFragment(), $title->getFragment() );
569 public static function provideGetTitleValue() {
573 [ 'User:Hansi_Maier' ],
578 * @covers Title::getTitleValue
579 * @dataProvider provideGetTitleValue
581 public function testGetTitleValue( $text ) {
582 $title = Title
::newFromText( $text );
583 $value = $title->getTitleValue();
585 $dbkey = str_replace( ' ', '_', $value->getText() );
586 $this->assertEquals( $title->getDBkey(), $dbkey );
587 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
588 $this->assertEquals( $title->getFragment(), $value->getFragment() );
591 public static function provideGetFragment() {
594 [ 'Foo#bar', 'bar' ],
595 [ 'Foo#bär', 'bär' ],
597 // Inner whitespace is normalized
598 [ 'Foo#bar_bar', 'bar bar' ],
599 [ 'Foo#bar bar', 'bar bar' ],
600 [ 'Foo#bar bar', 'bar bar' ],
602 // Leading whitespace is kept, trailing whitespace is trimmed.
603 // XXX: Is this really want we want?
604 [ 'Foo#_bar_bar_', ' bar bar' ],
605 [ 'Foo# bar bar ', ' bar bar' ],
610 * @covers Title::getFragment
611 * @dataProvider provideGetFragment
613 * @param string $full
614 * @param string $fragment
616 public function testGetFragment( $full, $fragment ) {
617 $title = Title
::newFromText( $full );
618 $this->assertEquals( $fragment, $title->getFragment() );
622 * @covers Title::isAlwaysKnown
623 * @dataProvider provideIsAlwaysKnown
624 * @param string $page
625 * @param bool $isKnown
627 public function testIsAlwaysKnown( $page, $isKnown ) {
628 $title = Title
::newFromText( $page );
629 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
632 public static function provideIsAlwaysKnown() {
634 [ 'Some nonexistent page', false ],
637 [ 'Special:BlankPage', true ],
638 [ 'Special:SomeNonexistentSpecialPage', false ],
639 [ 'MediaWiki:Parentheses', true ],
640 [ 'MediaWiki:Some nonexistent message', false ],
645 * @covers Title::isValid
646 * @dataProvider provideIsValid
647 * @param Title $title
648 * @param bool $isValid
650 public function testIsValid( Title
$title, $isValid ) {
651 $this->assertEquals( $isValid, $title->isValid(), $title->getPrefixedText() );
654 public static function provideIsValid() {
656 [ Title
::makeTitle( NS_MAIN
, '' ), false ],
657 [ Title
::makeTitle( NS_MAIN
, '<>' ), false ],
658 [ Title
::makeTitle( NS_MAIN
, '|' ), false ],
659 [ Title
::makeTitle( NS_MAIN
, '#' ), false ],
660 [ Title
::makeTitle( NS_MAIN
, 'Test' ), true ],
661 [ Title
::makeTitle( -33, 'Test' ), false ],
662 [ Title
::makeTitle( 77663399, 'Test' ), false ],
667 * @covers Title::isAlwaysKnown
669 public function testIsAlwaysKnownOnInterwiki() {
670 $title = Title
::makeTitle( NS_MAIN
, 'Interwiki link', '', 'externalwiki' );
671 $this->assertTrue( $title->isAlwaysKnown() );
675 * @covers Title::exists
677 public function testExists() {
678 $title = Title
::makeTitle( NS_PROJECT
, 'New page' );
679 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
681 $article = new Article( $title );
682 $page = $article->getPage();
683 $page->doEditContent( new WikitextContent( 'Some [[link]]' ), 'summary' );
685 // Tell Title it doesn't know whether it exists
686 $title->mArticleID
= -1;
688 // Tell the link cache it doesn't exists when it really does
689 $linkCache->clearLink( $title );
690 $linkCache->addBadLinkObj( $title );
695 'exists() should rely on link cache unless GAID_FOR_UPDATE is used'
699 $title->exists( Title
::GAID_FOR_UPDATE
),
700 'exists() should re-query database when GAID_FOR_UPDATE is used'
704 public function provideCanHaveTalkPage() {
706 'User page has talk page' => [
707 Title
::makeTitle( NS_USER
, 'Jane' ), true
709 'Talke page has talk page' => [
710 Title
::makeTitle( NS_TALK
, 'Foo' ), true
712 'Special page cannot have talk page' => [
713 Title
::makeTitle( NS_SPECIAL
, 'Thing' ), false
715 'Virtual namespace cannot have talk page' => [
716 Title
::makeTitle( NS_MEDIA
, 'Kitten.jpg' ), false
722 * @dataProvider provideCanHaveTalkPage
723 * @covers Title::canHaveTalkPage
725 * @param Title $title
726 * @param bool $expected
728 public function testCanHaveTalkPage( Title
$title, $expected ) {
729 $actual = $title->canHaveTalkPage();
730 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
734 * @dataProvider provideCanHaveTalkPage
735 * @covers Title::canTalk
737 * @param Title $title
738 * @param bool $expected
740 public function testCanTalk( Title
$title, $expected ) {
741 $actual = $title->canTalk();
742 $this->assertSame( $expected, $actual, $title->getPrefixedDBkey() );
745 public static function provideGetTalkPage_good() {
747 [ Title
::makeTitle( NS_MAIN
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
748 [ Title
::makeTitle( NS_TALK
, 'Test' ), Title
::makeTitle( NS_TALK
, 'Test' ) ],
753 * @dataProvider provideGetTalkPage_good
754 * @covers Title::getTalkPage
756 public function testGetTalkPage_good( Title
$title, Title
$expected ) {
757 $talk = $title->getTalkPage();
759 $expected->getPrefixedDBKey(),
760 $talk->getPrefixedDBKey(),
761 $title->getPrefixedDBKey()
766 * @dataProvider provideGetTalkPage_good
767 * @covers Title::getTalkPageIfDefined
769 public function testGetTalkPageIfDefined_good( Title
$title ) {
770 $talk = $title->getTalkPageIfDefined();
771 $this->assertInstanceOf(
774 $title->getPrefixedDBKey()
778 public static function provideGetTalkPage_bad() {
780 [ Title
::makeTitle( NS_SPECIAL
, 'Test' ) ],
781 [ Title
::makeTitle( NS_MEDIA
, 'Test' ) ],
786 * @dataProvider provideGetTalkPage_bad
787 * @covers Title::getTalkPageIfDefined
789 public function testGetTalkPageIfDefined_bad( Title
$title ) {
790 $talk = $title->getTalkPageIfDefined();
793 $title->getPrefixedDBKey()
797 public function provideCreateFragmentTitle() {
799 [ Title
::makeTitle( NS_MAIN
, 'Test' ), 'foo' ],
800 [ Title
::makeTitle( NS_TALK
, 'Test', 'foo' ), '' ],
801 [ Title
::makeTitle( NS_CATEGORY
, 'Test', 'foo' ), 'bar' ],
802 [ Title
::makeTitle( NS_MAIN
, 'Test1', '', 'interwiki' ), 'baz' ]
807 * @covers Title::createFragmentTarget
808 * @dataProvider provideCreateFragmentTitle
810 public function testCreateFragmentTitle( Title
$title, $fragment ) {
811 $this->mergeMwGlobalArrayValue( 'wgHooks', [
812 'InterwikiLoadPrefix' => [
813 function ( $prefix, &$iwdata ) {
814 if ( $prefix === 'interwiki' ) {
816 'iw_url' => 'http://example.com/',
826 $fragmentTitle = $title->createFragmentTarget( $fragment );
828 $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
829 $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
830 $this->assertEquals( $title->getInterwiki(), $fragmentTitle->getInterwiki() );
831 $this->assertEquals( $fragment, $fragmentTitle->getFragment() );
834 public function provideGetPrefixedText() {
838 Title
::makeTitle( NS_MAIN
, 'Foo bar' ),
843 Title
::makeTitle( NS_USER
, 'Foo bar' ),
848 Title
::makeTitle( NS_USER_TALK
, 'Foo bar' ),
851 // fragment not included
853 Title
::makeTitle( NS_MAIN
, 'Foo bar', 'fragment' ),
858 Title
::makeTitle( NS_MEDIA
, 'Foo bar' ),
861 // non-existent namespace
863 Title
::makeTitle( 100777, 'Foo bar' ),
864 'Special:Badtitle/NS100777:Foo bar'
870 * @covers Title::getPrefixedText
871 * @dataProvider provideGetPrefixedText
873 public function testGetPrefixedText( Title
$title, $expected ) {
874 $this->assertEquals( $expected, $title->getPrefixedText() );
877 public function provideGetPrefixedDBKey() {
881 Title
::makeTitle( NS_MAIN
, 'Foo_bar' ),
886 Title
::makeTitle( NS_USER
, 'Foo_bar' ),
891 Title
::makeTitle( NS_USER_TALK
, 'Foo_bar' ),
894 // fragment not included
896 Title
::makeTitle( NS_MAIN
, 'Foo_bar', 'fragment' ),
901 Title
::makeTitle( NS_MEDIA
, 'Foo_bar' ),
904 // non-existent namespace
906 Title
::makeTitle( 100777, 'Foo_bar' ),
907 'Special:Badtitle/NS100777:Foo_bar'
913 * @covers Title::getPrefixedDBKey
914 * @dataProvider provideGetPrefixedDBKey
916 public function testGetPrefixedDBKey( Title
$title, $expected ) {
917 $this->assertEquals( $expected, $title->getPrefixedDBkey() );
921 * @covers Title::getFragmentForURL
922 * @dataProvider provideGetFragmentForURL
924 * @param string $titleStr
925 * @param string $expected
927 public function testGetFragmentForURL( $titleStr, $expected ) {
928 $this->setMwGlobals( [
929 'wgFragmentMode' => [ 'html5' ],
930 'wgExternalInterwikiFragmentMode' => 'legacy',
932 $dbw = wfGetDB( DB_MASTER
);
933 $dbw->insert( 'interwiki',
937 'iw_url' => 'http://de.wikipedia.org/wiki/',
938 'iw_api' => 'http://de.wikipedia.org/w/api.php',
939 'iw_wikiid' => 'dewiki',
945 'iw_url' => 'http://zzwiki.org/wiki/',
946 'iw_api' => 'http://zzwiki.org/w/api.php',
947 'iw_wikiid' => 'zzwiki',
956 $title = Title
::newFromText( $titleStr );
957 self
::assertEquals( $expected, $title->getFragmentForURL() );
959 $dbw->delete( 'interwiki', '*', __METHOD__
);
962 public function provideGetFragmentForURL() {
965 [ 'Foo#ümlåût', '#ümlåût' ],
966 [ 'de:Foo#Bå®', '#Bå®' ],
967 [ 'zz:Foo#тест', '#.D1.82.D0.B5.D1.81.D1.82' ],
972 * @covers Title::isRawHtmlMessage
973 * @dataProvider provideIsRawHtmlMessage
975 public function testIsRawHtmlMessage( $textForm, $expected ) {
976 $this->setMwGlobals( 'wgRawHtmlMessages', [
982 $title = Title
::newFromText( $textForm );
983 $this->assertSame( $expected, $title->isRawHtmlMessage() );
986 public function provideIsRawHtmlMessage() {
988 [ 'MediaWiki:Foobar', true ],
989 [ 'MediaWiki:Foo bar', true ],
990 [ 'MediaWiki:Foo-bar', true ],
991 [ 'MediaWiki:foo bar', true ],
992 [ 'MediaWiki:foo-bar', true ],
993 [ 'MediaWiki:foobar', true ],
994 [ 'MediaWiki:some-other-message', false ],
995 [ 'Main Page', false ],