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' ],
94 [ 'A%20B', 'title-invalid-characters' ],
95 [ 'A%23B', 'title-invalid-characters' ],
96 [ 'A%2523B', 'title-invalid-characters' ],
97 // XML/HTML character entity references
98 // Note: Commented out because they are not marked invalid by the PHP test as
99 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
103 // Subject of NS_TALK does not roundtrip to NS_MAIN
104 [ 'Talk:File:Example.svg', 'title-invalid-talk-namespace' ],
105 // Directory navigation
106 [ '.', 'title-invalid-relative' ],
107 [ '..', 'title-invalid-relative' ],
108 [ './Sandbox', 'title-invalid-relative' ],
109 [ '../Sandbox', 'title-invalid-relative' ],
110 [ 'Foo/./Sandbox', 'title-invalid-relative' ],
111 [ 'Foo/../Sandbox', 'title-invalid-relative' ],
112 [ 'Sandbox/.', 'title-invalid-relative' ],
113 [ 'Sandbox/..', 'title-invalid-relative' ],
115 [ 'A ~~~ Name', 'title-invalid-magic-tilde' ],
116 [ 'A ~~~~ Signature', 'title-invalid-magic-tilde' ],
117 [ 'A ~~~~~ Timestamp', 'title-invalid-magic-tilde' ],
119 [ str_repeat( 'x', 256 ), 'title-invalid-too-long' ],
120 // Namespace prefix without actual title
121 [ 'Talk:', 'title-invalid-empty' ],
122 [ 'Talk:#', 'title-invalid-empty' ],
123 [ 'Category: ', 'title-invalid-empty' ],
124 [ 'Category: #bar', 'title-invalid-empty' ],
126 [ 'localtestiw: Talk: # anchor', 'title-invalid-empty' ],
127 [ 'localtestiw: Talk:', 'title-invalid-empty' ]
131 private function secureAndSplitGlobals() {
132 $this->setMwGlobals( [
133 'wgLocalInterwikis' => [ 'localtestiw' ],
135 'InterwikiLoadPrefix' => [
136 function ( $prefix, &$data ) {
137 if ( $prefix === 'localtestiw' ) {
138 $data = [ 'iw_url' => 'localtestiw' ];
139 } elseif ( $prefix === 'remotetestiw' ) {
140 $data = [ 'iw_url' => 'remotetestiw' ];
150 * See also mediawiki.Title.test.js
151 * @covers Title::secureAndSplit
152 * @dataProvider provideValidSecureAndSplit
153 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
155 public function testSecureAndSplitValid( $text ) {
156 $this->secureAndSplitGlobals();
157 $this->assertInstanceOf( 'Title', Title
::newFromText( $text ), "Valid: $text" );
161 * See also mediawiki.Title.test.js
162 * @covers Title::secureAndSplit
163 * @dataProvider provideInvalidSecureAndSplit
164 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
166 public function testSecureAndSplitInvalid( $text, $expectedErrorMessage ) {
167 $this->secureAndSplitGlobals();
169 Title
::newFromTextThrow( $text ); // should throw
170 $this->assertTrue( false, "Invalid: $text" );
171 } catch ( MalformedTitleException
$ex ) {
172 $this->assertEquals( $expectedErrorMessage, $ex->getErrorMessage(), "Invalid: $text" );
176 public static function provideConvertByteClassToUnicodeClass() {
179 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
180 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
184 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
187 'QWERTY\\x66-\\xFD+',
188 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
196 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
199 'QWERTY\\x66-\\x80+\\x23',
200 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
203 'QWERTY\\x66-\\x80+\\xD3',
204 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
208 '\\\\\\u0080-\\uFFFF',
212 '\\-\\u0080-\\uFFFF',
216 'QWERTY\\-\\u0080-\\uFFFF',
224 'A-\\x7F\\u0080-\\uFFFF',
227 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
228 'f-wQWERTYFXZ\\u0080-\\uFFFF',
231 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
232 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
238 * @dataProvider provideConvertByteClassToUnicodeClass
239 * @covers Title::convertByteClassToUnicodeClass
241 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
242 $this->assertEquals( $unicodeClass, Title
::convertByteClassToUnicodeClass( $byteClass ) );
246 * @dataProvider provideSpecialNamesWithAndWithoutParameter
247 * @covers Title::fixSpecialName
249 public function testFixSpecialNameRetainsParameter( $text, $expectedParam ) {
250 $title = Title
::newFromText( $text );
251 $fixed = $title->fixSpecialName();
252 $stuff = explode( '/', $fixed->getDBkey(), 2 );
253 if ( count( $stuff ) == 2 ) {
261 "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter"
265 public static function provideSpecialNamesWithAndWithoutParameter() {
267 [ 'Special:Version', null ],
268 [ 'Special:Version/', '' ],
269 [ 'Special:Version/param', 'param' ],
274 * Auth-less test of Title::isValidMoveOperation
277 * @param string $source
278 * @param string $target
279 * @param array|string|bool $expected Required error
280 * @dataProvider provideTestIsValidMoveOperation
281 * @covers Title::isValidMoveOperation
282 * @covers Title::validateFileMoveOperation
284 public function testIsValidMoveOperation( $source, $target, $expected ) {
285 $this->setMwGlobals( 'wgContentHandlerUseDB', false );
286 $title = Title
::newFromText( $source );
287 $nt = Title
::newFromText( $target );
288 $errors = $title->isValidMoveOperation( $nt, false );
289 if ( $expected === true ) {
290 $this->assertTrue( $errors );
292 $errors = $this->flattenErrorsArray( $errors );
293 foreach ( (array)$expected as $error ) {
294 $this->assertContains( $error, $errors );
299 public static function provideTestIsValidMoveOperation() {
301 // for Title::isValidMoveOperation
302 [ 'Some page', '', 'badtitletext' ],
303 [ 'Test', 'Test', 'selfmove' ],
304 [ 'Special:FooBar', 'Test', 'immobile-source-namespace' ],
305 [ 'Test', 'Special:FooBar', 'immobile-target-namespace' ],
306 [ 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ],
307 [ 'Page', 'File:Test.jpg', 'nonfile-cannot-move-to-file' ],
308 // for Title::validateFileMoveOperation
309 [ 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ],
314 * Auth-less test of Title::userCan
316 * @param array $whitelistRegexp
317 * @param string $source
318 * @param string $action
319 * @param array|string|bool $expected Required error
321 * @covers Title::checkReadPermissions
322 * @dataProvider dataWgWhitelistReadRegexp
324 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
325 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
326 // usually have only one regex, it is more concise to write the lonely regex
327 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
329 if ( is_string( $whitelistRegexp ) ) {
330 $whitelistRegexp = [ $whitelistRegexp ];
333 $this->setMwGlobals( [
334 // So User::isEveryoneAllowed( 'read' ) === false
335 'wgGroupPermissions' => [ '*' => [ 'read' => false ] ],
336 'wgWhitelistRead' => [ 'some random non sense title' ],
337 'wgWhitelistReadRegexp' => $whitelistRegexp,
340 $title = Title
::newFromDBkey( $source );
342 // New anonymous user with no rights
345 $errors = $title->userCan( $action, $user );
347 if ( is_bool( $expected ) ) {
348 # Forge the assertion message depending on the assertion expectation
349 $allowableness = $expected
350 ?
" should be allowed"
351 : " should NOT be allowed";
355 "User action '$action' on [[$source]] $allowableness."
358 $errors = $this->flattenErrorsArray( $errors );
359 foreach ( (array)$expected as $error ) {
360 $this->assertContains( $error, $errors );
366 * Provides test parameter values for testWgWhitelistReadRegexp()
368 public function dataWgWhitelistReadRegexp() {
373 // Everything, if this doesn't work, we're really in trouble
374 [ '/.*/', 'Main_Page', 'read', $ALLOWED ],
375 [ '/.*/', 'Main_Page', 'edit', $DISALLOWED ],
377 // We validate against the title name, not the db key
378 [ '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ],
380 [ '/^Main/', 'Main_Page', 'read', $ALLOWED ],
381 [ '/^Main.*/', 'Main_Page', 'read', $ALLOWED ],
383 [ '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ],
385 // ...without unicode modifier
386 [ '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ],
387 // ...with unicode modifier
388 [ '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ],
390 [ '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ],
391 [ '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ],
393 // From DefaultSettings.php:
394 [ "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ],
395 [ "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ],
398 [ '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ],
399 [ null, 'Special:Newpages', 'read', $DISALLOWED ],
404 public function flattenErrorsArray( $errors ) {
406 foreach ( $errors as $error ) {
407 $result[] = $error[0];
414 * @dataProvider provideGetPageViewLanguage
415 * @covers Title::getPageViewLanguage
417 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
418 $lang, $variant, $msg = ''
420 // Setup environnement for this test
421 $this->setMwGlobals( [
422 'wgDefaultLanguageVariant' => $variant,
423 'wgAllowUserJs' => true,
425 $this->setUserLang( $lang );
426 $this->setContentLang( $contLang );
428 $title = Title
::newFromText( $titleText );
429 $this->assertInstanceOf( 'Title', $title,
430 "Test must be passed a valid title text, you gave '$titleText'"
432 $this->assertEquals( $expected,
433 $title->getPageViewLanguage()->getCode(),
438 public static function provideGetPageViewLanguage() {
442 # - wgContLang (expected in most case)
443 # - wgLang (on some specific pages)
444 # - wgDefaultLanguageVariant
447 [ 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ],
448 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ],
449 [ 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ],
451 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ],
452 [ 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ],
453 [ 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ],
454 [ 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ],
455 [ 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ],
456 [ 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ],
457 [ 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ],
458 [ 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ],
460 [ 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ],
461 [ 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ],
462 [ 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ],
463 [ 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ],
464 [ 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ],
465 [ 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ],
466 [ 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ],
467 [ 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ],
468 [ 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ],
469 [ 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ],
471 [ 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ],
472 [ 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ],
478 * @dataProvider provideBaseTitleCases
479 * @covers Title::getBaseText
481 public function testGetBaseText( $title, $expected, $msg = '' ) {
482 $title = Title
::newFromText( $title );
483 $this->assertEquals( $expected,
484 $title->getBaseText(),
489 public static function provideBaseTitleCases() {
491 # Title, expected base, optional message
492 [ 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ],
493 [ 'User:Foo/Bar/Baz', 'Foo/Bar' ],
498 * @dataProvider provideRootTitleCases
499 * @covers Title::getRootText
501 public function testGetRootText( $title, $expected, $msg = '' ) {
502 $title = Title
::newFromText( $title );
503 $this->assertEquals( $expected,
504 $title->getRootText(),
509 public static function provideRootTitleCases() {
511 # Title, expected base, optional message
512 [ 'User:John_Doe/subOne/subTwo', 'John Doe' ],
513 [ 'User:Foo/Bar/Baz', 'Foo' ],
518 * @todo Handle $wgNamespacesWithSubpages cases
519 * @dataProvider provideSubpageTitleCases
520 * @covers Title::getSubpageText
522 public function testGetSubpageText( $title, $expected, $msg = '' ) {
523 $title = Title
::newFromText( $title );
524 $this->assertEquals( $expected,
525 $title->getSubpageText(),
530 public static function provideSubpageTitleCases() {
532 # Title, expected base, optional message
533 [ 'User:John_Doe/subOne/subTwo', 'subTwo' ],
534 [ 'User:John_Doe/subOne', 'subOne' ],
538 public static function provideNewFromTitleValue() {
540 [ new TitleValue( NS_MAIN
, 'Foo' ) ],
541 [ new TitleValue( NS_MAIN
, 'Foo', 'bar' ) ],
542 [ new TitleValue( NS_USER
, 'Hansi_Maier' ) ],
547 * @dataProvider provideNewFromTitleValue
549 public function testNewFromTitleValue( TitleValue
$value ) {
550 $title = Title
::newFromTitleValue( $value );
552 $dbkey = str_replace( ' ', '_', $value->getText() );
553 $this->assertEquals( $dbkey, $title->getDBkey() );
554 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
555 $this->assertEquals( $value->getFragment(), $title->getFragment() );
558 public static function provideGetTitleValue() {
562 [ 'User:Hansi_Maier' ],
567 * @dataProvider provideGetTitleValue
569 public function testGetTitleValue( $text ) {
570 $title = Title
::newFromText( $text );
571 $value = $title->getTitleValue();
573 $dbkey = str_replace( ' ', '_', $value->getText() );
574 $this->assertEquals( $title->getDBkey(), $dbkey );
575 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
576 $this->assertEquals( $title->getFragment(), $value->getFragment() );
579 public static function provideGetFragment() {
582 [ 'Foo#bar', 'bar' ],
583 [ 'Foo#bär', 'bär' ],
585 // Inner whitespace is normalized
586 [ 'Foo#bar_bar', 'bar bar' ],
587 [ 'Foo#bar bar', 'bar bar' ],
588 [ 'Foo#bar bar', 'bar bar' ],
590 // Leading whitespace is kept, trailing whitespace is trimmed.
591 // XXX: Is this really want we want?
592 [ 'Foo#_bar_bar_', ' bar bar' ],
593 [ 'Foo# bar bar ', ' bar bar' ],
598 * @dataProvider provideGetFragment
600 * @param string $full
601 * @param string $fragment
603 public function testGetFragment( $full, $fragment ) {
604 $title = Title
::newFromText( $full );
605 $this->assertEquals( $fragment, $title->getFragment() );
609 * @covers Title::isAlwaysKnown
610 * @dataProvider provideIsAlwaysKnown
611 * @param string $page
612 * @param bool $isKnown
614 public function testIsAlwaysKnown( $page, $isKnown ) {
615 $title = Title
::newFromText( $page );
616 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
619 public static function provideIsAlwaysKnown() {
621 [ 'Some nonexistent page', false ],
624 [ 'Special:BlankPage', true ],
625 [ 'Special:SomeNonexistentSpecialPage', false ],
626 [ 'MediaWiki:Parentheses', true ],
627 [ 'MediaWiki:Some nonexistent message', false ],
632 * @covers Title::isAlwaysKnown
634 public function testIsAlwaysKnownOnInterwiki() {
635 $title = Title
::makeTitle( NS_MAIN
, 'Interwiki link', '', 'externalwiki' );
636 $this->assertTrue( $title->isAlwaysKnown() );
640 * @covers Title::exists
642 public function testExists() {
643 $title = Title
::makeTitle( NS_PROJECT
, 'New page' );
644 $linkCache = LinkCache
::singleton();
646 $article = new Article( $title );
647 $page = $article->getPage();
648 $page->doEditContent( new WikitextContent( 'Some [[link]]' ), 'summary' );
650 // Tell Title it doesn't know whether it exists
651 $title->mArticleID
= -1;
653 // Tell the link cache it doesn't exists when it really does
654 $linkCache->clearLink( $title );
655 $linkCache->addBadLinkObj( $title );
660 'exists() should rely on link cache unless GAID_FOR_UPDATE is used'
664 $title->exists( Title
::GAID_FOR_UPDATE
),
665 'exists() should re-query database when GAID_FOR_UPDATE is used'
669 public function provideCreateFragmentTitle() {
671 [ Title
::makeTitle( NS_MAIN
, 'Test' ), 'foo' ],
672 [ Title
::makeTitle( NS_TALK
, 'Test', 'foo' ), '' ],
673 [ Title
::makeTitle( NS_CATEGORY
, 'Test', 'foo' ), 'bar' ],
674 [ Title
::makeTitle( NS_MAIN
, 'Test1', '', 'interwiki' ), 'baz' ]
679 * @covers Title::createFragmentTarget
680 * @dataProvider provideCreateFragmentTitle
682 public function testCreateFragmentTitle( Title
$title, $fragment ) {
683 $this->mergeMwGlobalArrayValue( 'wgHooks', [
684 'InterwikiLoadPrefix' => [
685 function ( $prefix, &$iwdata ) {
686 if ( $prefix === 'interwiki' ) {
688 'iw_url' => 'http://example.com/',
698 $fragmentTitle = $title->createFragmentTarget( $fragment );
700 $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
701 $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
702 $this->assertEquals( $title->getInterwiki(), $fragmentTitle->getInterwiki() );
703 $this->assertEquals( $fragment, $fragmentTitle->getFragment() );