7 class TitleTest
extends MediaWikiTestCase
{
8 protected function setUp() {
11 $this->setMwGlobals( [
12 'wgLanguageCode' => 'en',
13 'wgContLang' => Language
::factory( 'en' ),
15 'wgLang' => Language
::factory( 'en' ),
16 'wgAllowUserJs' => false,
17 'wgDefaultLanguageVariant' => false,
18 'wgMetaNamespace' => 'Project',
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' ],
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' ];
152 * See also mediawiki.Title.test.js
153 * @covers Title::secureAndSplit
154 * @dataProvider provideValidSecureAndSplit
155 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
157 public function testSecureAndSplitValid( $text ) {
158 $this->secureAndSplitGlobals();
159 $this->assertInstanceOf( 'Title', Title
::newFromText( $text ), "Valid: $text" );
163 * See also mediawiki.Title.test.js
164 * @covers Title::secureAndSplit
165 * @dataProvider provideInvalidSecureAndSplit
166 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
168 public function testSecureAndSplitInvalid( $text, $expectedErrorMessage ) {
169 $this->secureAndSplitGlobals();
171 Title
::newFromTextThrow( $text ); // should throw
172 $this->assertTrue( false, "Invalid: $text" );
173 } catch ( MalformedTitleException
$ex ) {
174 $this->assertEquals( $expectedErrorMessage, $ex->getErrorMessage(), "Invalid: $text" );
178 public static function provideConvertByteClassToUnicodeClass() {
181 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
182 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
186 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
189 'QWERTY\\x66-\\xFD+',
190 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
198 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
201 'QWERTY\\x66-\\x80+\\x23',
202 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
205 'QWERTY\\x66-\\x80+\\xD3',
206 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
210 '\\\\\\u0080-\\uFFFF',
214 '\\-\\u0080-\\uFFFF',
218 'QWERTY\\-\\u0080-\\uFFFF',
226 'A-\\x7F\\u0080-\\uFFFF',
229 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
230 'f-wQWERTYFXZ\\u0080-\\uFFFF',
233 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
234 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
240 * @dataProvider provideConvertByteClassToUnicodeClass
241 * @covers Title::convertByteClassToUnicodeClass
243 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
244 $this->assertEquals( $unicodeClass, Title
::convertByteClassToUnicodeClass( $byteClass ) );
248 * @dataProvider provideSpecialNamesWithAndWithoutParameter
249 * @covers Title::fixSpecialName
251 public function testFixSpecialNameRetainsParameter( $text, $expectedParam ) {
252 $title = Title
::newFromText( $text );
253 $fixed = $title->fixSpecialName();
254 $stuff = explode( '/', $fixed->getDBkey(), 2 );
255 if ( count( $stuff ) == 2 ) {
263 "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter"
267 public static function provideSpecialNamesWithAndWithoutParameter() {
269 [ 'Special:Version', null ],
270 [ 'Special:Version/', '' ],
271 [ 'Special:Version/param', 'param' ],
276 * Auth-less test of Title::isValidMoveOperation
279 * @param string $source
280 * @param string $target
281 * @param array|string|bool $expected Required error
282 * @dataProvider provideTestIsValidMoveOperation
283 * @covers Title::isValidMoveOperation
284 * @covers Title::validateFileMoveOperation
286 public function testIsValidMoveOperation( $source, $target, $expected ) {
287 $this->setMwGlobals( 'wgContentHandlerUseDB', false );
288 $title = Title
::newFromText( $source );
289 $nt = Title
::newFromText( $target );
290 $errors = $title->isValidMoveOperation( $nt, false );
291 if ( $expected === true ) {
292 $this->assertTrue( $errors );
294 $errors = $this->flattenErrorsArray( $errors );
295 foreach ( (array)$expected as $error ) {
296 $this->assertContains( $error, $errors );
301 public static function provideTestIsValidMoveOperation() {
303 // for Title::isValidMoveOperation
304 [ 'Some page', '', 'badtitletext' ],
305 [ 'Test', 'Test', 'selfmove' ],
306 [ 'Special:FooBar', 'Test', 'immobile-source-namespace' ],
307 [ 'Test', 'Special:FooBar', 'immobile-target-namespace' ],
308 [ 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ],
309 [ 'Page', 'File:Test.jpg', 'nonfile-cannot-move-to-file' ],
310 // for Title::validateFileMoveOperation
311 [ 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ],
316 * Auth-less test of Title::userCan
318 * @param array $whitelistRegexp
319 * @param string $source
320 * @param string $action
321 * @param array|string|bool $expected Required error
323 * @covers Title::checkReadPermissions
324 * @dataProvider dataWgWhitelistReadRegexp
326 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
327 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
328 // usually have only one regex, it is more concise to write the lonely regex
329 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
331 if ( is_string( $whitelistRegexp ) ) {
332 $whitelistRegexp = [ $whitelistRegexp ];
335 $this->setMwGlobals( [
336 // So User::isEveryoneAllowed( 'read' ) === false
337 'wgGroupPermissions' => [ '*' => [ 'read' => false ] ],
338 'wgWhitelistRead' => [ 'some random non sense title' ],
339 'wgWhitelistReadRegexp' => $whitelistRegexp,
342 $title = Title
::newFromDBkey( $source );
344 // New anonymous user with no rights
347 $errors = $title->userCan( $action, $user );
349 if ( is_bool( $expected ) ) {
350 # Forge the assertion message depending on the assertion expectation
351 $allowableness = $expected
352 ?
" should be allowed"
353 : " should NOT be allowed";
357 "User action '$action' on [[$source]] $allowableness."
360 $errors = $this->flattenErrorsArray( $errors );
361 foreach ( (array)$expected as $error ) {
362 $this->assertContains( $error, $errors );
368 * Provides test parameter values for testWgWhitelistReadRegexp()
370 public function dataWgWhitelistReadRegexp() {
375 // Everything, if this doesn't work, we're really in trouble
376 [ '/.*/', 'Main_Page', 'read', $ALLOWED ],
377 [ '/.*/', 'Main_Page', 'edit', $DISALLOWED ],
379 // We validate against the title name, not the db key
380 [ '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ],
382 [ '/^Main/', 'Main_Page', 'read', $ALLOWED ],
383 [ '/^Main.*/', 'Main_Page', 'read', $ALLOWED ],
385 [ '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ],
387 // ...without unicode modifier
388 [ '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ],
389 // ...with unicode modifier
390 [ '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ],
392 [ '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ],
393 [ '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ],
395 // From DefaultSettings.php:
396 [ "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ],
397 [ "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ],
400 [ '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ],
401 [ null, 'Special:Newpages', 'read', $DISALLOWED ],
406 public function flattenErrorsArray( $errors ) {
408 foreach ( $errors as $error ) {
409 $result[] = $error[0];
416 * @dataProvider provideGetPageViewLanguage
417 * @covers Title::getPageViewLanguage
419 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
420 $lang, $variant, $msg = ''
422 // Setup environnement for this test
423 $this->setMwGlobals( [
424 'wgLanguageCode' => $contLang,
425 'wgContLang' => Language
::factory( $contLang ),
426 'wgLang' => Language
::factory( $lang ),
427 'wgDefaultLanguageVariant' => $variant,
428 'wgAllowUserJs' => true,
431 $title = Title
::newFromText( $titleText );
432 $this->assertInstanceOf( 'Title', $title,
433 "Test must be passed a valid title text, you gave '$titleText'"
435 $this->assertEquals( $expected,
436 $title->getPageViewLanguage()->getCode(),
441 public static function provideGetPageViewLanguage() {
445 # - wgContLang (expected in most case)
446 # - wgLang (on some specific pages)
447 # - wgDefaultLanguageVariant
450 [ 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ],
451 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ],
452 [ 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ],
454 [ 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ],
455 [ 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ],
456 [ 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ],
457 [ 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ],
458 [ 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ],
459 [ 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ],
460 [ 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ],
461 [ 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ],
463 [ 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ],
464 [ 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ],
465 [ 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ],
466 [ 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ],
467 [ 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ],
468 [ 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ],
469 [ 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ],
470 [ 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ],
471 [ 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ],
472 [ 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ],
474 [ 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ],
475 [ 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ],
481 * @dataProvider provideBaseTitleCases
482 * @covers Title::getBaseText
484 public function testGetBaseText( $title, $expected, $msg = '' ) {
485 $title = Title
::newFromText( $title );
486 $this->assertEquals( $expected,
487 $title->getBaseText(),
492 public static function provideBaseTitleCases() {
494 # Title, expected base, optional message
495 [ 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ],
496 [ 'User:Foo/Bar/Baz', 'Foo/Bar' ],
501 * @dataProvider provideRootTitleCases
502 * @covers Title::getRootText
504 public function testGetRootText( $title, $expected, $msg = '' ) {
505 $title = Title
::newFromText( $title );
506 $this->assertEquals( $expected,
507 $title->getRootText(),
512 public static function provideRootTitleCases() {
514 # Title, expected base, optional message
515 [ 'User:John_Doe/subOne/subTwo', 'John Doe' ],
516 [ 'User:Foo/Bar/Baz', 'Foo' ],
521 * @todo Handle $wgNamespacesWithSubpages cases
522 * @dataProvider provideSubpageTitleCases
523 * @covers Title::getSubpageText
525 public function testGetSubpageText( $title, $expected, $msg = '' ) {
526 $title = Title
::newFromText( $title );
527 $this->assertEquals( $expected,
528 $title->getSubpageText(),
533 public static function provideSubpageTitleCases() {
535 # Title, expected base, optional message
536 [ 'User:John_Doe/subOne/subTwo', 'subTwo' ],
537 [ 'User:John_Doe/subOne', 'subOne' ],
541 public static function provideNewFromTitleValue() {
543 [ new TitleValue( NS_MAIN
, 'Foo' ) ],
544 [ new TitleValue( NS_MAIN
, 'Foo', 'bar' ) ],
545 [ new TitleValue( NS_USER
, 'Hansi_Maier' ) ],
550 * @dataProvider provideNewFromTitleValue
552 public function testNewFromTitleValue( TitleValue
$value ) {
553 $title = Title
::newFromTitleValue( $value );
555 $dbkey = str_replace( ' ', '_', $value->getText() );
556 $this->assertEquals( $dbkey, $title->getDBkey() );
557 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
558 $this->assertEquals( $value->getFragment(), $title->getFragment() );
561 public static function provideGetTitleValue() {
565 [ 'User:Hansi_Maier' ],
570 * @dataProvider provideGetTitleValue
572 public function testGetTitleValue( $text ) {
573 $title = Title
::newFromText( $text );
574 $value = $title->getTitleValue();
576 $dbkey = str_replace( ' ', '_', $value->getText() );
577 $this->assertEquals( $title->getDBkey(), $dbkey );
578 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
579 $this->assertEquals( $title->getFragment(), $value->getFragment() );
582 public static function provideGetFragment() {
585 [ 'Foo#bar', 'bar' ],
586 [ 'Foo#bär', 'bär' ],
588 // Inner whitespace is normalized
589 [ 'Foo#bar_bar', 'bar bar' ],
590 [ 'Foo#bar bar', 'bar bar' ],
591 [ 'Foo#bar bar', 'bar bar' ],
593 // Leading whitespace is kept, trailing whitespace is trimmed.
594 // XXX: Is this really want we want?
595 [ 'Foo#_bar_bar_', ' bar bar' ],
596 [ 'Foo# bar bar ', ' bar bar' ],
601 * @dataProvider provideGetFragment
603 * @param string $full
604 * @param string $fragment
606 public function testGetFragment( $full, $fragment ) {
607 $title = Title
::newFromText( $full );
608 $this->assertEquals( $fragment, $title->getFragment() );
612 * @covers Title::isAlwaysKnown
613 * @dataProvider provideIsAlwaysKnown
614 * @param string $page
615 * @param bool $isKnown
617 public function testIsAlwaysKnown( $page, $isKnown ) {
618 $title = Title
::newFromText( $page );
619 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
622 public static function provideIsAlwaysKnown() {
624 [ 'Some nonexistent page', false ],
627 [ 'Special:BlankPage', true ],
628 [ 'Special:SomeNonexistentSpecialPage', false ],
629 [ 'MediaWiki:Parentheses', true ],
630 [ 'MediaWiki:Some nonexistent message', false ],
635 * @covers Title::isAlwaysKnown
637 public function testIsAlwaysKnownOnInterwiki() {
638 $title = Title
::makeTitle( NS_MAIN
, 'Interwiki link', '', 'externalwiki' );
639 $this->assertTrue( $title->isAlwaysKnown() );
643 * @covers Title::exists
645 public function testExists() {
646 $title = Title
::makeTitle( NS_PROJECT
, 'New page' );
647 $linkCache = LinkCache
::singleton();
649 $article = new Article( $title );
650 $page = $article->getPage();
651 $page->doEditContent( new WikitextContent( 'Some [[link]]' ), 'summary' );
653 // Tell Title it doesn't know whether it exists
654 $title->mArticleID
= -1;
656 // Tell the link cache it doesn't exists when it really does
657 $linkCache->clearLink( $title );
658 $linkCache->addBadLinkObj( $title );
663 'exists() should rely on link cache unless GAID_FOR_UPDATE is used'
667 $title->exists( Title
::GAID_FOR_UPDATE
),
668 'exists() should re-query database when GAID_FOR_UPDATE is used'