7 class TitleTest
extends MediaWikiTestCase
{
8 protected function setUp() {
11 $this->setMwGlobals( [
12 'wgAllowUserJs' => false,
13 'wgDefaultLanguageVariant' => false,
14 'wgMetaNamespace' => 'Project',
16 $this->setUserLang( 'en' );
17 $this->setContentLang( 'en' );
21 * @covers Title::legalChars
23 public function testLegalChars() {
24 $titlechars = Title
::legalChars();
26 foreach ( range( 1, 255 ) as $num ) {
28 if ( strpos( "#[]{}<>|", $chr ) !== false ||
preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
30 (bool)preg_match( "/[$titlechars]/", $chr ),
31 "chr($num) = $chr is not a valid titlechar"
35 (bool)preg_match( "/[$titlechars]/", $chr ),
36 "chr($num) = $chr is a valid titlechar"
42 public static function provideValidSecureAndSplit() {
53 [ 'Talk:Foo:Sandbox' ],
54 [ 'File:Example.svg' ],
55 [ 'File_talk:Example.svg' ],
56 [ 'Foo/.../Sandbox' ],
60 // Length is 256 total, but only title part matters
61 [ 'Category:' . str_repeat( 'x', 248 ) ],
62 [ str_repeat( 'x', 252 ) ],
64 [ 'localtestiw: #anchor' ],
66 [ 'localtestiw:foo' ],
67 [ 'localtestiw: foo # anchor' ],
68 [ 'localtestiw: Talk: Sandbox # anchor' ],
70 [ 'remotetestiw: Talk: # anchor' ],
71 [ 'remotetestiw: #bar' ],
72 [ 'remotetestiw: Talk:' ],
73 [ 'remotetestiw: Talk: Foo' ],
74 [ 'localtestiw:remotetestiw:' ],
75 [ 'localtestiw:remotetestiw:foo' ]
79 public static function provideInvalidSecureAndSplit() {
81 [ '', 'title-invalid-empty' ],
82 [ ':', 'title-invalid-empty' ],
83 [ '__ __', 'title-invalid-empty' ],
84 [ ' __ ', 'title-invalid-empty' ],
85 // Bad characters forbidden regardless of wgLegalTitleChars
86 [ 'A [ B', 'title-invalid-characters' ],
87 [ 'A ] B', 'title-invalid-characters' ],
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 \t B", 'title-invalid-characters' ],
94 [ "A \n B", 'title-invalid-characters' ],
96 [ 'A%20B', 'title-invalid-characters' ],
97 [ 'A%23B', 'title-invalid-characters' ],
98 [ 'A%2523B', 'title-invalid-characters' ],
99 // XML/HTML character entity references
100 // Note: Commented out because they are not marked invalid by the PHP test as
101 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
105 // Subject of NS_TALK does not roundtrip to NS_MAIN
106 [ 'Talk:File:Example.svg', 'title-invalid-talk-namespace' ],
107 // Directory navigation
108 [ '.', 'title-invalid-relative' ],
109 [ '..', 'title-invalid-relative' ],
110 [ './Sandbox', 'title-invalid-relative' ],
111 [ '../Sandbox', 'title-invalid-relative' ],
112 [ 'Foo/./Sandbox', 'title-invalid-relative' ],
113 [ 'Foo/../Sandbox', 'title-invalid-relative' ],
114 [ 'Sandbox/.', 'title-invalid-relative' ],
115 [ 'Sandbox/..', 'title-invalid-relative' ],
117 [ 'A ~~~ Name', 'title-invalid-magic-tilde' ],
118 [ 'A ~~~~ Signature', 'title-invalid-magic-tilde' ],
119 [ 'A ~~~~~ Timestamp', 'title-invalid-magic-tilde' ],
121 [ str_repeat( 'x', 256 ), 'title-invalid-too-long' ],
122 // Namespace prefix without actual title
123 [ 'Talk:', 'title-invalid-empty' ],
124 [ 'Talk:#', 'title-invalid-empty' ],
125 [ 'Category: ', 'title-invalid-empty' ],
126 [ 'Category: #bar', 'title-invalid-empty' ],
128 [ 'localtestiw: Talk: # anchor', 'title-invalid-empty' ],
129 [ 'localtestiw: Talk:', 'title-invalid-empty' ]
133 private function secureAndSplitGlobals() {
134 $this->setMwGlobals( [
135 'wgLocalInterwikis' => [ 'localtestiw' ],
137 'InterwikiLoadPrefix' => [
138 function ( $prefix, &$data ) {
139 if ( $prefix === 'localtestiw' ) {
140 $data = [ 'iw_url' => 'localtestiw' ];
141 } elseif ( $prefix === 'remotetestiw' ) {
142 $data = [ 'iw_url' => 'remotetestiw' ];
150 // Reset TitleParser since we modified $wgLocalInterwikis
151 $this->setService( 'TitleParser', new MediaWikiTitleCodec(
152 Language
::factory( 'en' ),
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', 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::isValidMoveOperation
286 * @param string $source
287 * @param string $target
288 * @param array|string|bool $expected Required error
289 * @dataProvider provideTestIsValidMoveOperation
290 * @covers Title::isValidMoveOperation
291 * @covers Title::validateFileMoveOperation
293 public function testIsValidMoveOperation( $source, $target, $expected ) {
294 $this->setMwGlobals( 'wgContentHandlerUseDB', false );
295 $title = Title
::newFromText( $source );
296 $nt = Title
::newFromText( $target );
297 $errors = $title->isValidMoveOperation( $nt, false );
298 if ( $expected === true ) {
299 $this->assertTrue( $errors );
301 $errors = $this->flattenErrorsArray( $errors );
302 foreach ( (array)$expected as $error ) {
303 $this->assertContains( $error, $errors );
308 public static function provideTestIsValidMoveOperation() {
310 // for Title::isValidMoveOperation
311 [ 'Some page', '', 'badtitletext' ],
312 [ 'Test', 'Test', 'selfmove' ],
313 [ 'Special:FooBar', 'Test', 'immobile-source-namespace' ],
314 [ 'Test', 'Special:FooBar', 'immobile-target-namespace' ],
315 [ 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ],
316 [ 'Page', 'File:Test.jpg', 'nonfile-cannot-move-to-file' ],
317 // for Title::validateFileMoveOperation
318 [ 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ],
323 * Auth-less test of Title::userCan
325 * @param array $whitelistRegexp
326 * @param string $source
327 * @param string $action
328 * @param array|string|bool $expected Required error
330 * @covers Title::checkReadPermissions
331 * @dataProvider dataWgWhitelistReadRegexp
333 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
334 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
335 // usually have only one regex, it is more concise to write the lonely regex
336 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
338 if ( is_string( $whitelistRegexp ) ) {
339 $whitelistRegexp = [ $whitelistRegexp ];
342 $this->setMwGlobals( [
343 // So User::isEveryoneAllowed( 'read' ) === false
344 'wgGroupPermissions' => [ '*' => [ 'read' => false ] ],
345 'wgWhitelistRead' => [ 'some random non sense title' ],
346 'wgWhitelistReadRegexp' => $whitelistRegexp,
349 $title = Title
::newFromDBkey( $source );
351 // New anonymous user with no rights
354 $errors = $title->userCan( $action, $user );
356 if ( is_bool( $expected ) ) {
357 # Forge the assertion message depending on the assertion expectation
358 $allowableness = $expected
359 ?
" should be allowed"
360 : " should NOT be allowed";
364 "User action '$action' on [[$source]] $allowableness."
367 $errors = $this->flattenErrorsArray( $errors );
368 foreach ( (array)$expected as $error ) {
369 $this->assertContains( $error, $errors );
375 * Provides test parameter values for testWgWhitelistReadRegexp()
377 public function dataWgWhitelistReadRegexp() {
382 // Everything, if this doesn't work, we're really in trouble
383 [ '/.*/', 'Main_Page', 'read', $ALLOWED ],
384 [ '/.*/', 'Main_Page', 'edit', $DISALLOWED ],
386 // We validate against the title name, not the db key
387 [ '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ],
389 [ '/^Main/', 'Main_Page', 'read', $ALLOWED ],
390 [ '/^Main.*/', 'Main_Page', 'read', $ALLOWED ],
392 [ '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ],
394 // ...without unicode modifier
395 [ '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ],
396 // ...with unicode modifier
397 [ '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ],
399 [ '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ],
400 [ '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ],
402 // From DefaultSettings.php:
403 [ "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ],
404 [ "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ],
407 [ '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ],
408 [ null, 'Special:Newpages', 'read', $DISALLOWED ],
413 public function flattenErrorsArray( $errors ) {
415 foreach ( $errors as $error ) {
416 $result[] = $error[0];
423 * @dataProvider provideGetPageViewLanguage
424 * @covers Title::getPageViewLanguage
426 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
427 $lang, $variant, $msg = ''
429 // Setup environnement for this test
430 $this->setMwGlobals( [
431 'wgDefaultLanguageVariant' => $variant,
432 'wgAllowUserJs' => true,
434 $this->setUserLang( $lang );
435 $this->setContentLang( $contLang );
437 $title = Title
::newFromText( $titleText );
438 $this->assertInstanceOf( 'Title', $title,
439 "Test must be passed a valid title text, you gave '$titleText'"
441 $this->assertEquals( $expected,
442 $title->getPageViewLanguage()->getCode(),
447 public static function provideGetPageViewLanguage() {
451 # - wgContLang (expected in most case)
452 # - wgLang (on some specific pages)
453 # - wgDefaultLanguageVariant
456 [ 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ],
457 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ],
458 [ 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ],
460 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ],
461 [ 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ],
462 [ 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ],
463 [ 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ],
464 [ 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ],
465 [ 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ],
466 [ 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ],
467 [ 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ],
469 [ 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ],
470 [ 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ],
471 [ 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ],
472 [ 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ],
473 [ 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ],
474 [ 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ],
475 [ 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ],
476 [ 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ],
477 [ 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ],
478 [ 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ],
480 [ 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ],
481 [ 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ],
487 * @dataProvider provideBaseTitleCases
488 * @covers Title::getBaseText
490 public function testGetBaseText( $title, $expected, $msg = '' ) {
491 $title = Title
::newFromText( $title );
492 $this->assertEquals( $expected,
493 $title->getBaseText(),
498 public static function provideBaseTitleCases() {
500 # Title, expected base, optional message
501 [ 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ],
502 [ 'User:Foo/Bar/Baz', 'Foo/Bar' ],
507 * @dataProvider provideRootTitleCases
508 * @covers Title::getRootText
510 public function testGetRootText( $title, $expected, $msg = '' ) {
511 $title = Title
::newFromText( $title );
512 $this->assertEquals( $expected,
513 $title->getRootText(),
518 public static function provideRootTitleCases() {
520 # Title, expected base, optional message
521 [ 'User:John_Doe/subOne/subTwo', 'John Doe' ],
522 [ 'User:Foo/Bar/Baz', 'Foo' ],
527 * @todo Handle $wgNamespacesWithSubpages cases
528 * @dataProvider provideSubpageTitleCases
529 * @covers Title::getSubpageText
531 public function testGetSubpageText( $title, $expected, $msg = '' ) {
532 $title = Title
::newFromText( $title );
533 $this->assertEquals( $expected,
534 $title->getSubpageText(),
539 public static function provideSubpageTitleCases() {
541 # Title, expected base, optional message
542 [ 'User:John_Doe/subOne/subTwo', 'subTwo' ],
543 [ 'User:John_Doe/subOne', 'subOne' ],
547 public static function provideNewFromTitleValue() {
549 [ new TitleValue( NS_MAIN
, 'Foo' ) ],
550 [ new TitleValue( NS_MAIN
, 'Foo', 'bar' ) ],
551 [ new TitleValue( NS_USER
, 'Hansi_Maier' ) ],
556 * @dataProvider provideNewFromTitleValue
558 public function testNewFromTitleValue( TitleValue
$value ) {
559 $title = Title
::newFromTitleValue( $value );
561 $dbkey = str_replace( ' ', '_', $value->getText() );
562 $this->assertEquals( $dbkey, $title->getDBkey() );
563 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
564 $this->assertEquals( $value->getFragment(), $title->getFragment() );
567 public static function provideGetTitleValue() {
571 [ 'User:Hansi_Maier' ],
576 * @dataProvider provideGetTitleValue
578 public function testGetTitleValue( $text ) {
579 $title = Title
::newFromText( $text );
580 $value = $title->getTitleValue();
582 $dbkey = str_replace( ' ', '_', $value->getText() );
583 $this->assertEquals( $title->getDBkey(), $dbkey );
584 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
585 $this->assertEquals( $title->getFragment(), $value->getFragment() );
588 public static function provideGetFragment() {
591 [ 'Foo#bar', 'bar' ],
592 [ 'Foo#bär', 'bär' ],
594 // Inner whitespace is normalized
595 [ 'Foo#bar_bar', 'bar bar' ],
596 [ 'Foo#bar bar', 'bar bar' ],
597 [ 'Foo#bar bar', 'bar bar' ],
599 // Leading whitespace is kept, trailing whitespace is trimmed.
600 // XXX: Is this really want we want?
601 [ 'Foo#_bar_bar_', ' bar bar' ],
602 [ 'Foo# bar bar ', ' bar bar' ],
607 * @dataProvider provideGetFragment
609 * @param string $full
610 * @param string $fragment
612 public function testGetFragment( $full, $fragment ) {
613 $title = Title
::newFromText( $full );
614 $this->assertEquals( $fragment, $title->getFragment() );
618 * @covers Title::isAlwaysKnown
619 * @dataProvider provideIsAlwaysKnown
620 * @param string $page
621 * @param bool $isKnown
623 public function testIsAlwaysKnown( $page, $isKnown ) {
624 $title = Title
::newFromText( $page );
625 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
628 public static function provideIsAlwaysKnown() {
630 [ 'Some nonexistent page', false ],
633 [ 'Special:BlankPage', true ],
634 [ 'Special:SomeNonexistentSpecialPage', false ],
635 [ 'MediaWiki:Parentheses', true ],
636 [ 'MediaWiki:Some nonexistent message', false ],
641 * @covers Title::isAlwaysKnown
643 public function testIsAlwaysKnownOnInterwiki() {
644 $title = Title
::makeTitle( NS_MAIN
, 'Interwiki link', '', 'externalwiki' );
645 $this->assertTrue( $title->isAlwaysKnown() );
649 * @covers Title::exists
651 public function testExists() {
652 $title = Title
::makeTitle( NS_PROJECT
, 'New page' );
653 $linkCache = LinkCache
::singleton();
655 $article = new Article( $title );
656 $page = $article->getPage();
657 $page->doEditContent( new WikitextContent( 'Some [[link]]' ), 'summary' );
659 // Tell Title it doesn't know whether it exists
660 $title->mArticleID
= -1;
662 // Tell the link cache it doesn't exists when it really does
663 $linkCache->clearLink( $title );
664 $linkCache->addBadLinkObj( $title );
669 'exists() should rely on link cache unless GAID_FOR_UPDATE is used'
673 $title->exists( Title
::GAID_FOR_UPDATE
),
674 'exists() should re-query database when GAID_FOR_UPDATE is used'
678 public function provideCreateFragmentTitle() {
680 [ Title
::makeTitle( NS_MAIN
, 'Test' ), 'foo' ],
681 [ Title
::makeTitle( NS_TALK
, 'Test', 'foo' ), '' ],
682 [ Title
::makeTitle( NS_CATEGORY
, 'Test', 'foo' ), 'bar' ],
683 [ Title
::makeTitle( NS_MAIN
, 'Test1', '', 'interwiki' ), 'baz' ]
688 * @covers Title::createFragmentTarget
689 * @dataProvider provideCreateFragmentTitle
691 public function testCreateFragmentTitle( Title
$title, $fragment ) {
692 $this->mergeMwGlobalArrayValue( 'wgHooks', [
693 'InterwikiLoadPrefix' => [
694 function ( $prefix, &$iwdata ) {
695 if ( $prefix === 'interwiki' ) {
697 'iw_url' => 'http://example.com/',
707 $fragmentTitle = $title->createFragmentTarget( $fragment );
709 $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
710 $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
711 $this->assertEquals( $title->getInterwiki(), $fragmentTitle->getInterwiki() );
712 $this->assertEquals( $fragment, $fragmentTitle->getFragment() );
715 public function provideGetPrefixedText() {
719 Title
::makeTitle( NS_MAIN
, 'Foobar' ),
724 Title
::makeTitle( NS_USER
, 'Foobar' ),
727 // fragment not included
729 Title
::makeTitle( NS_MAIN
, 'Foobar', 'fragment' ),
734 Title
::makeTitle( NS_MEDIA
, 'Foobar' ),
737 // non-existent namespace
739 Title
::makeTitle( 100000, 'Foobar' ),
746 * @covers Title::getPrefixedText
747 * @dataProvider provideGetPrefixedText
749 public function testGetPrefixedText( Title
$title, $expected ) {
750 $this->assertEquals( $expected, $title->getPrefixedText() );