6 class TitleTest
extends MediaWikiTestCase
{
7 protected function setUp() {
10 $this->setMwGlobals( array(
11 'wgLanguageCode' => 'en',
12 'wgContLang' => Language
::factory( 'en' ),
14 'wgLang' => Language
::factory( 'en' ),
15 'wgAllowUserJs' => false,
16 'wgDefaultLanguageVariant' => false,
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 function provideValidSecureAndSplit() {
52 array( 'Talk:Sandbox' ),
53 array( 'Talk:Foo:Sandbox' ),
54 array( 'File:Example.svg' ),
55 array( 'File_talk:Example.svg' ),
56 array( 'Foo/.../Sandbox' ),
57 array( 'Sandbox/...' ),
60 // Length is 256 total, but only title part matters
61 array( 'Category:' . str_repeat( 'x', 248 ) ),
62 array( str_repeat( 'x', 252 ) ),
64 array( 'localtestiw: #anchor' ),
65 array( 'localtestiw:' ),
66 array( 'localtestiw:foo' ),
67 array( 'localtestiw: foo # anchor' ),
68 array( 'localtestiw: Talk: Sandbox # anchor' ),
69 array( 'remotetestiw:' ),
70 array( 'remotetestiw: Talk: # anchor' ),
71 array( 'remotetestiw: #bar' ),
72 array( 'remotetestiw: Talk:' ),
73 array( 'remotetestiw: Talk: Foo' ),
74 array( 'localtestiw:remotetestiw:' ),
75 array( 'localtestiw:remotetestiw:foo' )
79 public function provideInvalidSecureAndSplit() {
85 // Bad characters forbidden regardless of wgLegalTitleChars
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 array( 'Talk:File:Example.svg' ),
105 // Directory navigation
108 array( './Sandbox' ),
109 array( '../Sandbox' ),
110 array( 'Foo/./Sandbox' ),
111 array( 'Foo/../Sandbox' ),
112 array( 'Sandbox/.' ),
113 array( 'Sandbox/..' ),
115 array( 'A ~~~ Name' ),
116 array( 'A ~~~~ Signature' ),
117 array( 'A ~~~~~ Timestamp' ),
118 array( str_repeat( 'x', 256 ) ),
119 // Namespace prefix without actual title
122 array( 'Category: ' ),
123 array( 'Category: #bar' ),
125 array( 'localtestiw: Talk: # anchor' ),
126 array( 'localtestiw: Talk:' )
130 private function secureAndSplitGlobals() {
131 $this->setMwGlobals( array(
132 'wgLocalInterwikis' => array( 'localtestiw' ),
134 'InterwikiLoadPrefix' => array(
135 function ( $prefix, &$data ) {
136 if ( $prefix === 'localtestiw' ) {
137 $data = array( 'iw_url' => 'localtestiw' );
138 } elseif ( $prefix === 'remotetestiw' ) {
139 $data = array( 'iw_url' => 'remotetestiw' );
149 * See also mediawiki.Title.test.js
150 * @covers Title::secureAndSplit
151 * @dataProvider provideValidSecureAndSplit
152 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
154 public function testSecureAndSplitValid( $text ) {
155 $this->secureAndSplitGlobals();
156 $this->assertInstanceOf( 'Title', Title
::newFromText( $text ), "Valid: $text" );
160 * See also mediawiki.Title.test.js
161 * @covers Title::secureAndSplit
162 * @dataProvider provideInvalidSecureAndSplit
163 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
165 public function testSecureAndSplitInvalid( $text ) {
166 $this->secureAndSplitGlobals();
167 $this->assertNull( Title
::newFromText( $text ), "Invalid: $text" );
170 public static function provideConvertByteClassToUnicodeClass() {
173 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
174 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
178 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
181 'QWERTY\\x66-\\xFD+',
182 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
190 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
193 'QWERTY\\x66-\\x80+\\x23',
194 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
197 'QWERTY\\x66-\\x80+\\xD3',
198 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
202 '\\\\\\u0080-\\uFFFF',
206 '\\-\\u0080-\\uFFFF',
210 'QWERTY\\-\\u0080-\\uFFFF',
218 'A-\\x7F\\u0080-\\uFFFF',
221 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
222 'f-wQWERTYFXZ\\u0080-\\uFFFF',
225 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
226 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
232 * @dataProvider provideConvertByteClassToUnicodeClass
233 * @covers Title::convertByteClassToUnicodeClass
235 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
236 $this->assertEquals( $unicodeClass, Title
::convertByteClassToUnicodeClass( $byteClass ) );
240 * @dataProvider provideSpecialNamesWithAndWithoutParameter
241 * @covers Title::fixSpecialName
243 public function testFixSpecialNameRetainsParameter( $text, $expectedParam ) {
244 $title = Title
::newFromText( $text );
245 $fixed = $title->fixSpecialName();
246 $stuff = explode( '/', $fixed->getDBkey(), 2 );
247 if ( count( $stuff ) == 2 ) {
255 "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter"
259 public static function provideSpecialNamesWithAndWithoutParameter() {
261 array( 'Special:Version', null ),
262 array( 'Special:Version/', '' ),
263 array( 'Special:Version/param', 'param' ),
268 * Auth-less test of Title::isValidMoveOperation
271 * @param string $source
272 * @param string $target
273 * @param array|string|bool $expected Required error
274 * @dataProvider provideTestIsValidMoveOperation
275 * @covers Title::isValidMoveOperation
277 public function testIsValidMoveOperation( $source, $target, $expected ) {
278 $title = Title
::newFromText( $source );
279 $nt = Title
::newFromText( $target );
280 $errors = $title->isValidMoveOperation( $nt, false );
281 if ( $expected === true ) {
282 $this->assertTrue( $errors );
284 $errors = $this->flattenErrorsArray( $errors );
285 foreach ( (array)$expected as $error ) {
286 $this->assertContains( $error, $errors );
292 * Provides test parameter values for testIsValidMoveOperation()
294 public function dataTestIsValidMoveOperation() {
296 array( 'Test', 'Test', 'selfmove' ),
297 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
302 * Auth-less test of Title::userCan
304 * @param array $whitelistRegexp
305 * @param string $source
306 * @param string $action
307 * @param array|string|bool $expected Required error
309 * @covers Title::checkReadPermissions
310 * @dataProvider dataWgWhitelistReadRegexp
312 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
313 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
314 // usually have only one regex, it is more concise to write the lonely regex
315 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
317 if ( is_string( $whitelistRegexp ) ) {
318 $whitelistRegexp = array( $whitelistRegexp );
321 $title = Title
::newFromDBkey( $source );
323 global $wgGroupPermissions;
324 $oldPermissions = $wgGroupPermissions;
325 // Disallow all so we can ensure our regex works
326 $wgGroupPermissions = array();
327 $wgGroupPermissions['*']['read'] = false;
329 global $wgWhitelistRead;
330 $oldWhitelist = $wgWhitelistRead;
331 // Undo any LocalSettings explicite whitelists so they won't cause a
332 // failing test to succeed. Set it to some random non sense just
333 // to make sure we properly test Title::checkReadPermissions()
334 $wgWhitelistRead = array( 'some random non sense title' );
336 global $wgWhitelistReadRegexp;
337 $oldWhitelistRegexp = $wgWhitelistReadRegexp;
338 $wgWhitelistReadRegexp = $whitelistRegexp;
340 // Just use $wgUser which in test is a user object for '127.0.0.1'
342 // Invalidate user rights cache to take in account $wgGroupPermissions
344 $wgUser->clearInstanceCache();
345 $errors = $title->userCan( $action, $wgUser );
348 $wgGroupPermissions = $oldPermissions;
349 $wgWhitelistRead = $oldWhitelist;
350 $wgWhitelistReadRegexp = $oldWhitelistRegexp;
352 if ( is_bool( $expected ) ) {
353 # Forge the assertion message depending on the assertion expectation
354 $allowableness = $expected
355 ?
" should be allowed"
356 : " should NOT be allowed";
360 "User action '$action' on [[$source]] $allowableness."
363 $errors = $this->flattenErrorsArray( $errors );
364 foreach ( (array)$expected as $error ) {
365 $this->assertContains( $error, $errors );
371 * Provides test parameter values for testWgWhitelistReadRegexp()
373 public function dataWgWhitelistReadRegexp() {
378 // Everything, if this doesn't work, we're really in trouble
379 array( '/.*/', 'Main_Page', 'read', $ALLOWED ),
380 array( '/.*/', 'Main_Page', 'edit', $DISALLOWED ),
382 // We validate against the title name, not the db key
383 array( '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ),
385 array( '/^Main/', 'Main_Page', 'read', $ALLOWED ),
386 array( '/^Main.*/', 'Main_Page', 'read', $ALLOWED ),
388 array( '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ),
390 // ...without unicode modifier
391 array( '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ),
392 // ...with unicode modifier
393 array( '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ),
395 array( '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ),
396 array( '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ),
398 // From DefaultSettings.php:
399 array( "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ),
400 array( "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ),
403 array( '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ),
404 array( null, 'Special:Newpages', 'read', $DISALLOWED ),
409 public function flattenErrorsArray( $errors ) {
411 foreach ( $errors as $error ) {
412 $result[] = $error[0];
418 public static function provideTestIsValidMoveOperation() {
420 array( 'Test', 'Test', 'selfmove' ),
421 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
426 * @dataProvider provideGetPageViewLanguage
427 * @covers Title::getPageViewLanguage
429 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
430 $lang, $variant, $msg = ''
432 global $wgLanguageCode, $wgContLang, $wgLang, $wgDefaultLanguageVariant, $wgAllowUserJs;
434 // Setup environnement for this test
435 $wgLanguageCode = $contLang;
436 $wgContLang = Language
::factory( $contLang );
437 $wgLang = Language
::factory( $lang );
438 $wgDefaultLanguageVariant = $variant;
439 $wgAllowUserJs = true;
441 $title = Title
::newFromText( $titleText );
442 $this->assertInstanceOf( 'Title', $title,
443 "Test must be passed a valid title text, you gave '$titleText'"
445 $this->assertEquals( $expected,
446 $title->getPageViewLanguage()->getCode(),
451 public static function provideGetPageViewLanguage() {
455 # - wgContLang (expected in most case)
456 # - wgLang (on some specific pages)
457 # - wgDefaultLanguageVariant
460 array( 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ),
461 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ),
462 array( 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ),
464 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ),
465 array( 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ),
466 array( 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ),
467 array( 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ),
468 array( 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ),
469 array( 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ),
470 array( 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ),
471 array( 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
473 array( 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ),
474 array( 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ),
475 array( 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ),
476 array( 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ),
477 array( 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ),
478 array( 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ),
479 array( 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ),
480 array( 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ),
481 array( 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ),
482 array( 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
484 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
485 array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),
491 * @dataProvider provideBaseTitleCases
492 * @covers Title::getBaseText
494 public function testGetBaseText( $title, $expected, $msg = '' ) {
495 $title = Title
::newFromText( $title );
496 $this->assertEquals( $expected,
497 $title->getBaseText(),
502 public static function provideBaseTitleCases() {
504 # Title, expected base, optional message
505 array( 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
506 array( 'User:Foo/Bar/Baz', 'Foo/Bar' ),
511 * @dataProvider provideRootTitleCases
512 * @covers Title::getRootText
514 public function testGetRootText( $title, $expected, $msg = '' ) {
515 $title = Title
::newFromText( $title );
516 $this->assertEquals( $expected,
517 $title->getRootText(),
522 public static function provideRootTitleCases() {
524 # Title, expected base, optional message
525 array( 'User:John_Doe/subOne/subTwo', 'John Doe' ),
526 array( 'User:Foo/Bar/Baz', 'Foo' ),
531 * @todo Handle $wgNamespacesWithSubpages cases
532 * @dataProvider provideSubpageTitleCases
533 * @covers Title::getSubpageText
535 public function testGetSubpageText( $title, $expected, $msg = '' ) {
536 $title = Title
::newFromText( $title );
537 $this->assertEquals( $expected,
538 $title->getSubpageText(),
543 public static function provideSubpageTitleCases() {
545 # Title, expected base, optional message
546 array( 'User:John_Doe/subOne/subTwo', 'subTwo' ),
547 array( 'User:John_Doe/subOne', 'subOne' ),
551 public function provideNewFromTitleValue() {
553 array( new TitleValue( NS_MAIN
, 'Foo' ) ),
554 array( new TitleValue( NS_MAIN
, 'Foo', 'bar' ) ),
555 array( new TitleValue( NS_USER
, 'Hansi_Maier' ) ),
560 * @dataProvider provideNewFromTitleValue
562 public function testNewFromTitleValue( TitleValue
$value ) {
563 $title = Title
::newFromTitleValue( $value );
565 $dbkey = str_replace( ' ', '_', $value->getText() );
566 $this->assertEquals( $dbkey, $title->getDBkey() );
567 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
568 $this->assertEquals( $value->getFragment(), $title->getFragment() );
571 public function provideGetTitleValue() {
575 array( 'User:Hansi_Maier' ),
580 * @dataProvider provideGetTitleValue
582 public function testGetTitleValue( $text ) {
583 $title = Title
::newFromText( $text );
584 $value = $title->getTitleValue();
586 $dbkey = str_replace( ' ', '_', $value->getText() );
587 $this->assertEquals( $title->getDBkey(), $dbkey );
588 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
589 $this->assertEquals( $title->getFragment(), $value->getFragment() );
592 public function provideGetFragment() {
595 array( 'Foo#bar', 'bar' ),
596 array( 'Foo#bär', 'bär' ),
598 // Inner whitespace is normalized
599 array( 'Foo#bar_bar', 'bar bar' ),
600 array( 'Foo#bar bar', 'bar bar' ),
601 array( 'Foo#bar bar', 'bar bar' ),
603 // Leading whitespace is kept, trailing whitespace is trimmed.
604 // XXX: Is this really want we want?
605 array( 'Foo#_bar_bar_', ' bar bar' ),
606 array( 'Foo# bar bar ', ' bar bar' ),
611 * @dataProvider provideGetFragment
613 * @param string $full
614 * @param string $fragment
616 public function testGetFragment( $full, $fragment ) {
617 $title = Title
::newFromText( $full );
618 $this->assertEquals( $fragment, $title->getFragment() );
622 * @covers Title::isAlwaysKnown
623 * @dataProvider provideIsAlwaysKnown
624 * @param string $page
625 * @param bool $isKnown
627 public function testIsAlwaysKnown( $page, $isKnown ) {
628 $title = Title
::newFromText( $page );
629 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
632 public function provideIsAlwaysKnown() {
634 array( 'Some nonexistent page', false ),
635 array( 'UTPage', false ),
636 array( '#test', true ),
637 array( 'Special:BlankPage', true ),
638 array( 'Special:SomeNonexistentSpecialPage', false ),
639 array( 'MediaWiki:Parentheses', true ),
640 array( 'MediaWiki:Some nonexistent message', false ),
645 * @covers Title::isAlwaysKnown
647 public function testIsAlwaysKnownOnInterwiki() {
648 $title = Title
::makeTitle( NS_MAIN
, 'Interwiki link', '', 'externalwiki' );
649 $this->assertTrue( $title->isAlwaysKnown() );