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' ];
148 // Reset TitleParser since we modified $wgLocalInterwikis
149 $this->setService( 'TitleParser', new MediaWikiTitleCodec(
150 Language
::factory( 'en' ),
157 * See also mediawiki.Title.test.js
158 * @covers Title::secureAndSplit
159 * @dataProvider provideValidSecureAndSplit
160 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
162 public function testSecureAndSplitValid( $text ) {
163 $this->secureAndSplitGlobals();
164 $this->assertInstanceOf( 'Title', Title
::newFromText( $text ), "Valid: $text" );
168 * See also mediawiki.Title.test.js
169 * @covers Title::secureAndSplit
170 * @dataProvider provideInvalidSecureAndSplit
171 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
173 public function testSecureAndSplitInvalid( $text, $expectedErrorMessage ) {
174 $this->secureAndSplitGlobals();
176 Title
::newFromTextThrow( $text ); // should throw
177 $this->assertTrue( false, "Invalid: $text" );
178 } catch ( MalformedTitleException
$ex ) {
179 $this->assertEquals( $expectedErrorMessage, $ex->getErrorMessage(), "Invalid: $text" );
183 public static function provideConvertByteClassToUnicodeClass() {
186 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
187 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
191 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
194 'QWERTY\\x66-\\xFD+',
195 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
203 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
206 'QWERTY\\x66-\\x80+\\x23',
207 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
210 'QWERTY\\x66-\\x80+\\xD3',
211 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
215 '\\\\\\u0080-\\uFFFF',
219 '\\-\\u0080-\\uFFFF',
223 'QWERTY\\-\\u0080-\\uFFFF',
231 'A-\\x7F\\u0080-\\uFFFF',
234 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
235 'f-wQWERTYFXZ\\u0080-\\uFFFF',
238 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
239 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
245 * @dataProvider provideConvertByteClassToUnicodeClass
246 * @covers Title::convertByteClassToUnicodeClass
248 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
249 $this->assertEquals( $unicodeClass, Title
::convertByteClassToUnicodeClass( $byteClass ) );
253 * @dataProvider provideSpecialNamesWithAndWithoutParameter
254 * @covers Title::fixSpecialName
256 public function testFixSpecialNameRetainsParameter( $text, $expectedParam ) {
257 $title = Title
::newFromText( $text );
258 $fixed = $title->fixSpecialName();
259 $stuff = explode( '/', $fixed->getDBkey(), 2 );
260 if ( count( $stuff ) == 2 ) {
268 "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter"
272 public static function provideSpecialNamesWithAndWithoutParameter() {
274 [ 'Special:Version', null ],
275 [ 'Special:Version/', '' ],
276 [ 'Special:Version/param', 'param' ],
281 * Auth-less test of Title::isValidMoveOperation
284 * @param string $source
285 * @param string $target
286 * @param array|string|bool $expected Required error
287 * @dataProvider provideTestIsValidMoveOperation
288 * @covers Title::isValidMoveOperation
289 * @covers Title::validateFileMoveOperation
291 public function testIsValidMoveOperation( $source, $target, $expected ) {
292 $this->setMwGlobals( 'wgContentHandlerUseDB', false );
293 $title = Title
::newFromText( $source );
294 $nt = Title
::newFromText( $target );
295 $errors = $title->isValidMoveOperation( $nt, false );
296 if ( $expected === true ) {
297 $this->assertTrue( $errors );
299 $errors = $this->flattenErrorsArray( $errors );
300 foreach ( (array)$expected as $error ) {
301 $this->assertContains( $error, $errors );
306 public static function provideTestIsValidMoveOperation() {
308 // for Title::isValidMoveOperation
309 [ 'Some page', '', 'badtitletext' ],
310 [ 'Test', 'Test', 'selfmove' ],
311 [ 'Special:FooBar', 'Test', 'immobile-source-namespace' ],
312 [ 'Test', 'Special:FooBar', 'immobile-target-namespace' ],
313 [ 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ],
314 [ 'Page', 'File:Test.jpg', 'nonfile-cannot-move-to-file' ],
315 // for Title::validateFileMoveOperation
316 [ 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ],
321 * Auth-less test of Title::userCan
323 * @param array $whitelistRegexp
324 * @param string $source
325 * @param string $action
326 * @param array|string|bool $expected Required error
328 * @covers Title::checkReadPermissions
329 * @dataProvider dataWgWhitelistReadRegexp
331 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
332 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
333 // usually have only one regex, it is more concise to write the lonely regex
334 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
336 if ( is_string( $whitelistRegexp ) ) {
337 $whitelistRegexp = [ $whitelistRegexp ];
340 $this->setMwGlobals( [
341 // So User::isEveryoneAllowed( 'read' ) === false
342 'wgGroupPermissions' => [ '*' => [ 'read' => false ] ],
343 'wgWhitelistRead' => [ 'some random non sense title' ],
344 'wgWhitelistReadRegexp' => $whitelistRegexp,
347 $title = Title
::newFromDBkey( $source );
349 // New anonymous user with no rights
352 $errors = $title->userCan( $action, $user );
354 if ( is_bool( $expected ) ) {
355 # Forge the assertion message depending on the assertion expectation
356 $allowableness = $expected
357 ?
" should be allowed"
358 : " should NOT be allowed";
362 "User action '$action' on [[$source]] $allowableness."
365 $errors = $this->flattenErrorsArray( $errors );
366 foreach ( (array)$expected as $error ) {
367 $this->assertContains( $error, $errors );
373 * Provides test parameter values for testWgWhitelistReadRegexp()
375 public function dataWgWhitelistReadRegexp() {
380 // Everything, if this doesn't work, we're really in trouble
381 [ '/.*/', 'Main_Page', 'read', $ALLOWED ],
382 [ '/.*/', 'Main_Page', 'edit', $DISALLOWED ],
384 // We validate against the title name, not the db key
385 [ '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ],
387 [ '/^Main/', 'Main_Page', 'read', $ALLOWED ],
388 [ '/^Main.*/', 'Main_Page', 'read', $ALLOWED ],
390 [ '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ],
392 // ...without unicode modifier
393 [ '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ],
394 // ...with unicode modifier
395 [ '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ],
397 [ '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ],
398 [ '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ],
400 // From DefaultSettings.php:
401 [ "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ],
402 [ "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ],
405 [ '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ],
406 [ null, 'Special:Newpages', 'read', $DISALLOWED ],
411 public function flattenErrorsArray( $errors ) {
413 foreach ( $errors as $error ) {
414 $result[] = $error[0];
421 * @dataProvider provideGetPageViewLanguage
422 * @covers Title::getPageViewLanguage
424 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
425 $lang, $variant, $msg = ''
427 // Setup environnement for this test
428 $this->setMwGlobals( [
429 'wgDefaultLanguageVariant' => $variant,
430 'wgAllowUserJs' => true,
432 $this->setUserLang( $lang );
433 $this->setContentLang( $contLang );
435 $title = Title
::newFromText( $titleText );
436 $this->assertInstanceOf( 'Title', $title,
437 "Test must be passed a valid title text, you gave '$titleText'"
439 $this->assertEquals( $expected,
440 $title->getPageViewLanguage()->getCode(),
445 public static function provideGetPageViewLanguage() {
449 # - wgContLang (expected in most case)
450 # - wgLang (on some specific pages)
451 # - wgDefaultLanguageVariant
454 [ 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ],
455 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ],
456 [ 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ],
458 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ],
459 [ 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ],
460 [ 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ],
461 [ 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ],
462 [ 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ],
463 [ 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ],
464 [ 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ],
465 [ 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ],
467 [ 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ],
468 [ 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ],
469 [ 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ],
470 [ 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ],
471 [ 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ],
472 [ 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ],
473 [ 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ],
474 [ 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ],
475 [ 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ],
476 [ 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ],
478 [ 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ],
479 [ 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ],
485 * @dataProvider provideBaseTitleCases
486 * @covers Title::getBaseText
488 public function testGetBaseText( $title, $expected, $msg = '' ) {
489 $title = Title
::newFromText( $title );
490 $this->assertEquals( $expected,
491 $title->getBaseText(),
496 public static function provideBaseTitleCases() {
498 # Title, expected base, optional message
499 [ 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ],
500 [ 'User:Foo/Bar/Baz', 'Foo/Bar' ],
505 * @dataProvider provideRootTitleCases
506 * @covers Title::getRootText
508 public function testGetRootText( $title, $expected, $msg = '' ) {
509 $title = Title
::newFromText( $title );
510 $this->assertEquals( $expected,
511 $title->getRootText(),
516 public static function provideRootTitleCases() {
518 # Title, expected base, optional message
519 [ 'User:John_Doe/subOne/subTwo', 'John Doe' ],
520 [ 'User:Foo/Bar/Baz', 'Foo' ],
525 * @todo Handle $wgNamespacesWithSubpages cases
526 * @dataProvider provideSubpageTitleCases
527 * @covers Title::getSubpageText
529 public function testGetSubpageText( $title, $expected, $msg = '' ) {
530 $title = Title
::newFromText( $title );
531 $this->assertEquals( $expected,
532 $title->getSubpageText(),
537 public static function provideSubpageTitleCases() {
539 # Title, expected base, optional message
540 [ 'User:John_Doe/subOne/subTwo', 'subTwo' ],
541 [ 'User:John_Doe/subOne', 'subOne' ],
545 public static function provideNewFromTitleValue() {
547 [ new TitleValue( NS_MAIN
, 'Foo' ) ],
548 [ new TitleValue( NS_MAIN
, 'Foo', 'bar' ) ],
549 [ new TitleValue( NS_USER
, 'Hansi_Maier' ) ],
554 * @dataProvider provideNewFromTitleValue
556 public function testNewFromTitleValue( TitleValue
$value ) {
557 $title = Title
::newFromTitleValue( $value );
559 $dbkey = str_replace( ' ', '_', $value->getText() );
560 $this->assertEquals( $dbkey, $title->getDBkey() );
561 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
562 $this->assertEquals( $value->getFragment(), $title->getFragment() );
565 public static function provideGetTitleValue() {
569 [ 'User:Hansi_Maier' ],
574 * @dataProvider provideGetTitleValue
576 public function testGetTitleValue( $text ) {
577 $title = Title
::newFromText( $text );
578 $value = $title->getTitleValue();
580 $dbkey = str_replace( ' ', '_', $value->getText() );
581 $this->assertEquals( $title->getDBkey(), $dbkey );
582 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
583 $this->assertEquals( $title->getFragment(), $value->getFragment() );
586 public static function provideGetFragment() {
589 [ 'Foo#bar', 'bar' ],
590 [ 'Foo#bär', 'bär' ],
592 // Inner whitespace is normalized
593 [ 'Foo#bar_bar', 'bar bar' ],
594 [ 'Foo#bar bar', 'bar bar' ],
595 [ 'Foo#bar bar', 'bar bar' ],
597 // Leading whitespace is kept, trailing whitespace is trimmed.
598 // XXX: Is this really want we want?
599 [ 'Foo#_bar_bar_', ' bar bar' ],
600 [ 'Foo# bar bar ', ' bar bar' ],
605 * @dataProvider provideGetFragment
607 * @param string $full
608 * @param string $fragment
610 public function testGetFragment( $full, $fragment ) {
611 $title = Title
::newFromText( $full );
612 $this->assertEquals( $fragment, $title->getFragment() );
616 * @covers Title::isAlwaysKnown
617 * @dataProvider provideIsAlwaysKnown
618 * @param string $page
619 * @param bool $isKnown
621 public function testIsAlwaysKnown( $page, $isKnown ) {
622 $title = Title
::newFromText( $page );
623 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
626 public static function provideIsAlwaysKnown() {
628 [ 'Some nonexistent page', false ],
631 [ 'Special:BlankPage', true ],
632 [ 'Special:SomeNonexistentSpecialPage', false ],
633 [ 'MediaWiki:Parentheses', true ],
634 [ 'MediaWiki:Some nonexistent message', false ],
639 * @covers Title::isAlwaysKnown
641 public function testIsAlwaysKnownOnInterwiki() {
642 $title = Title
::makeTitle( NS_MAIN
, 'Interwiki link', '', 'externalwiki' );
643 $this->assertTrue( $title->isAlwaysKnown() );
647 * @covers Title::exists
649 public function testExists() {
650 $title = Title
::makeTitle( NS_PROJECT
, 'New page' );
651 $linkCache = LinkCache
::singleton();
653 $article = new Article( $title );
654 $page = $article->getPage();
655 $page->doEditContent( new WikitextContent( 'Some [[link]]' ), 'summary' );
657 // Tell Title it doesn't know whether it exists
658 $title->mArticleID
= -1;
660 // Tell the link cache it doesn't exists when it really does
661 $linkCache->clearLink( $title );
662 $linkCache->addBadLinkObj( $title );
667 'exists() should rely on link cache unless GAID_FOR_UPDATE is used'
671 $title->exists( Title
::GAID_FOR_UPDATE
),
672 'exists() should re-query database when GAID_FOR_UPDATE is used'
676 public function provideCreateFragmentTitle() {
678 [ Title
::makeTitle( NS_MAIN
, 'Test' ), 'foo' ],
679 [ Title
::makeTitle( NS_TALK
, 'Test', 'foo' ), '' ],
680 [ Title
::makeTitle( NS_CATEGORY
, 'Test', 'foo' ), 'bar' ],
681 [ Title
::makeTitle( NS_MAIN
, 'Test1', '', 'interwiki' ), 'baz' ]
686 * @covers Title::createFragmentTarget
687 * @dataProvider provideCreateFragmentTitle
689 public function testCreateFragmentTitle( Title
$title, $fragment ) {
690 $this->mergeMwGlobalArrayValue( 'wgHooks', [
691 'InterwikiLoadPrefix' => [
692 function ( $prefix, &$iwdata ) {
693 if ( $prefix === 'interwiki' ) {
695 'iw_url' => 'http://example.com/',
705 $fragmentTitle = $title->createFragmentTarget( $fragment );
707 $this->assertEquals( $title->getNamespace(), $fragmentTitle->getNamespace() );
708 $this->assertEquals( $title->getText(), $fragmentTitle->getText() );
709 $this->assertEquals( $title->getInterwiki(), $fragmentTitle->getInterwiki() );
710 $this->assertEquals( $fragment, $fragmentTitle->getFragment() );