7 class TitleTest
extends MediaWikiTestCase
{
8 protected function setUp() {
11 $this->setMwGlobals( array(
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() {
54 array( 'Talk:Sandbox' ),
55 array( 'Talk:Foo:Sandbox' ),
56 array( 'File:Example.svg' ),
57 array( 'File_talk:Example.svg' ),
58 array( 'Foo/.../Sandbox' ),
59 array( 'Sandbox/...' ),
62 // Length is 256 total, but only title part matters
63 array( 'Category:' . str_repeat( 'x', 248 ) ),
64 array( str_repeat( 'x', 252 ) ),
66 array( 'localtestiw: #anchor' ),
67 array( 'localtestiw:' ),
68 array( 'localtestiw:foo' ),
69 array( 'localtestiw: foo # anchor' ),
70 array( 'localtestiw: Talk: Sandbox # anchor' ),
71 array( 'remotetestiw:' ),
72 array( 'remotetestiw: Talk: # anchor' ),
73 array( 'remotetestiw: #bar' ),
74 array( 'remotetestiw: Talk:' ),
75 array( 'remotetestiw: Talk: Foo' ),
76 array( 'localtestiw:remotetestiw:' ),
77 array( 'localtestiw:remotetestiw:foo' )
81 public static function provideInvalidSecureAndSplit() {
83 array( '', 'title-invalid-empty' ),
84 array( ':', 'title-invalid-empty' ),
85 array( '__ __', 'title-invalid-empty' ),
86 array( ' __ ', 'title-invalid-empty' ),
87 // Bad characters forbidden regardless of wgLegalTitleChars
88 array( 'A [ B', 'title-invalid-characters' ),
89 array( 'A ] B', 'title-invalid-characters' ),
90 array( 'A { B', 'title-invalid-characters' ),
91 array( 'A } B', 'title-invalid-characters' ),
92 array( 'A < B', 'title-invalid-characters' ),
93 array( 'A > B', 'title-invalid-characters' ),
94 array( 'A | B', 'title-invalid-characters' ),
96 array( 'A%20B', 'title-invalid-characters' ),
97 array( 'A%23B', 'title-invalid-characters' ),
98 array( '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 array( 'Talk:File:Example.svg', 'title-invalid-talk-namespace' ),
107 // Directory navigation
108 array( '.', 'title-invalid-relative' ),
109 array( '..', 'title-invalid-relative' ),
110 array( './Sandbox', 'title-invalid-relative' ),
111 array( '../Sandbox', 'title-invalid-relative' ),
112 array( 'Foo/./Sandbox', 'title-invalid-relative' ),
113 array( 'Foo/../Sandbox', 'title-invalid-relative' ),
114 array( 'Sandbox/.', 'title-invalid-relative' ),
115 array( 'Sandbox/..', 'title-invalid-relative' ),
117 array( 'A ~~~ Name', 'title-invalid-magic-tilde' ),
118 array( 'A ~~~~ Signature', 'title-invalid-magic-tilde' ),
119 array( 'A ~~~~~ Timestamp', 'title-invalid-magic-tilde' ),
121 array( str_repeat( 'x', 256 ), 'title-invalid-too-long' ),
122 // Namespace prefix without actual title
123 array( 'Talk:', 'title-invalid-empty' ),
124 array( 'Talk:#', 'title-invalid-empty' ),
125 array( 'Category: ', 'title-invalid-empty' ),
126 array( 'Category: #bar', 'title-invalid-empty' ),
128 array( 'localtestiw: Talk: # anchor', 'title-invalid-empty' ),
129 array( 'localtestiw: Talk:', 'title-invalid-empty' )
133 private function secureAndSplitGlobals() {
134 $this->setMwGlobals( array(
135 'wgLocalInterwikis' => array( 'localtestiw' ),
137 'InterwikiLoadPrefix' => array(
138 function ( $prefix, &$data ) {
139 if ( $prefix === 'localtestiw' ) {
140 $data = array( 'iw_url' => 'localtestiw' );
141 } elseif ( $prefix === 'remotetestiw' ) {
142 $data = array( '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 array( 'Special:Version', null ),
270 array( 'Special:Version/', '' ),
271 array( '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 array( 'Some page', '', 'badtitletext' ),
305 array( 'Test', 'Test', 'selfmove' ),
306 array( 'Special:FooBar', 'Test', 'immobile-source-namespace' ),
307 array( 'Test', 'Special:FooBar', 'immobile-target-namespace' ),
308 array( 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ),
309 array( 'Page', 'File:Test.jpg', 'nonfile-cannot-move-to-file' ),
310 // for Title::validateFileMoveOperation
311 array( '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 = array( $whitelistRegexp );
335 $this->setMwGlobals( array(
336 // So User::isEveryoneAllowed( 'read' ) === false
337 'wgGroupPermissions' => array( '*' => array( 'read' => false ) ),
338 'wgWhitelistRead' => array( 'some random non sense title' ),
339 'wgWhitelistReadRegexp' => $whitelistRegexp,
342 $title = Title
::newFromDBkey( $source );
344 // New anonymous user with no rights
346 $user->mRights
= array();
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 array( '/.*/', 'Main_Page', 'read', $ALLOWED ),
377 array( '/.*/', 'Main_Page', 'edit', $DISALLOWED ),
379 // We validate against the title name, not the db key
380 array( '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ),
382 array( '/^Main/', 'Main_Page', 'read', $ALLOWED ),
383 array( '/^Main.*/', 'Main_Page', 'read', $ALLOWED ),
385 array( '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ),
387 // ...without unicode modifier
388 array( '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ),
389 // ...with unicode modifier
390 array( '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ),
392 array( '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ),
393 array( '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ),
395 // From DefaultSettings.php:
396 array( "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ),
397 array( "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ),
400 array( '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ),
401 array( 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( array(
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 array( 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ),
451 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ),
452 array( 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ),
454 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ),
455 array( 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ),
456 array( 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ),
457 array( 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ),
458 array( 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ),
459 array( 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ),
460 array( 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ),
461 array( 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
463 array( 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ),
464 array( 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ),
465 array( 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ),
466 array( 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ),
467 array( 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ),
468 array( 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ),
469 array( 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ),
470 array( 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ),
471 array( 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ),
472 array( 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
474 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
475 array( '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 array( 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
496 array( '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 array( 'User:John_Doe/subOne/subTwo', 'John Doe' ),
516 array( '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 array( 'User:John_Doe/subOne/subTwo', 'subTwo' ),
537 array( 'User:John_Doe/subOne', 'subOne' ),
541 public static function provideNewFromTitleValue() {
543 array( new TitleValue( NS_MAIN
, 'Foo' ) ),
544 array( new TitleValue( NS_MAIN
, 'Foo', 'bar' ) ),
545 array( 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 array( '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 array( 'Foo#bar', 'bar' ),
586 array( 'Foo#bär', 'bär' ),
588 // Inner whitespace is normalized
589 array( 'Foo#bar_bar', 'bar bar' ),
590 array( 'Foo#bar bar', 'bar bar' ),
591 array( 'Foo#bar bar', 'bar bar' ),
593 // Leading whitespace is kept, trailing whitespace is trimmed.
594 // XXX: Is this really want we want?
595 array( 'Foo#_bar_bar_', ' bar bar' ),
596 array( '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 array( 'Some nonexistent page', false ),
625 array( 'UTPage', false ),
626 array( '#test', true ),
627 array( 'Special:BlankPage', true ),
628 array( 'Special:SomeNonexistentSpecialPage', false ),
629 array( 'MediaWiki:Parentheses', true ),
630 array( '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'