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,
17 'wgMetaNamespace' => 'Project',
22 * @covers Title::legalChars
24 public function testLegalChars() {
25 $titlechars = Title
::legalChars();
27 foreach ( range( 1, 255 ) as $num ) {
29 if ( strpos( "#[]{}<>|", $chr ) !== false ||
preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
31 (bool)preg_match( "/[$titlechars]/", $chr ),
32 "chr($num) = $chr is not a valid titlechar"
36 (bool)preg_match( "/[$titlechars]/", $chr ),
37 "chr($num) = $chr is a valid titlechar"
43 public static function provideValidSecureAndSplit() {
53 array( 'Talk:Sandbox' ),
54 array( 'Talk:Foo:Sandbox' ),
55 array( 'File:Example.svg' ),
56 array( 'File_talk:Example.svg' ),
57 array( 'Foo/.../Sandbox' ),
58 array( 'Sandbox/...' ),
61 // Length is 256 total, but only title part matters
62 array( 'Category:' . str_repeat( 'x', 248 ) ),
63 array( str_repeat( 'x', 252 ) ),
65 array( 'localtestiw: #anchor' ),
66 array( 'localtestiw:' ),
67 array( 'localtestiw:foo' ),
68 array( 'localtestiw: foo # anchor' ),
69 array( 'localtestiw: Talk: Sandbox # anchor' ),
70 array( 'remotetestiw:' ),
71 array( 'remotetestiw: Talk: # anchor' ),
72 array( 'remotetestiw: #bar' ),
73 array( 'remotetestiw: Talk:' ),
74 array( 'remotetestiw: Talk: Foo' ),
75 array( 'localtestiw:remotetestiw:' ),
76 array( 'localtestiw:remotetestiw:foo' )
80 public static function provideInvalidSecureAndSplit() {
86 // Bad characters forbidden regardless of wgLegalTitleChars
98 // XML/HTML character entity references
99 // Note: Commented out because they are not marked invalid by the PHP test as
100 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
104 // Subject of NS_TALK does not roundtrip to NS_MAIN
105 array( 'Talk:File:Example.svg' ),
106 // Directory navigation
109 array( './Sandbox' ),
110 array( '../Sandbox' ),
111 array( 'Foo/./Sandbox' ),
112 array( 'Foo/../Sandbox' ),
113 array( 'Sandbox/.' ),
114 array( 'Sandbox/..' ),
116 array( 'A ~~~ Name' ),
117 array( 'A ~~~~ Signature' ),
118 array( 'A ~~~~~ Timestamp' ),
119 array( str_repeat( 'x', 256 ) ),
120 // Namespace prefix without actual title
123 array( 'Category: ' ),
124 array( 'Category: #bar' ),
126 array( 'localtestiw: Talk: # anchor' ),
127 array( 'localtestiw: Talk:' )
131 private function secureAndSplitGlobals() {
132 $this->setMwGlobals( array(
133 'wgLocalInterwikis' => array( 'localtestiw' ),
135 'InterwikiLoadPrefix' => array(
136 function ( $prefix, &$data ) {
137 if ( $prefix === 'localtestiw' ) {
138 $data = array( 'iw_url' => 'localtestiw' );
139 } elseif ( $prefix === 'remotetestiw' ) {
140 $data = array( '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 ) {
167 $this->secureAndSplitGlobals();
168 $this->assertNull( Title
::newFromText( $text ), "Invalid: $text" );
171 public static function provideConvertByteClassToUnicodeClass() {
174 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
175 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
179 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
182 'QWERTY\\x66-\\xFD+',
183 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
191 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
194 'QWERTY\\x66-\\x80+\\x23',
195 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
198 'QWERTY\\x66-\\x80+\\xD3',
199 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
203 '\\\\\\u0080-\\uFFFF',
207 '\\-\\u0080-\\uFFFF',
211 'QWERTY\\-\\u0080-\\uFFFF',
219 'A-\\x7F\\u0080-\\uFFFF',
222 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
223 'f-wQWERTYFXZ\\u0080-\\uFFFF',
226 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
227 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
233 * @dataProvider provideConvertByteClassToUnicodeClass
234 * @covers Title::convertByteClassToUnicodeClass
236 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
237 $this->assertEquals( $unicodeClass, Title
::convertByteClassToUnicodeClass( $byteClass ) );
241 * @dataProvider provideSpecialNamesWithAndWithoutParameter
242 * @covers Title::fixSpecialName
244 public function testFixSpecialNameRetainsParameter( $text, $expectedParam ) {
245 $title = Title
::newFromText( $text );
246 $fixed = $title->fixSpecialName();
247 $stuff = explode( '/', $fixed->getDBkey(), 2 );
248 if ( count( $stuff ) == 2 ) {
256 "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter"
260 public static function provideSpecialNamesWithAndWithoutParameter() {
262 array( 'Special:Version', null ),
263 array( 'Special:Version/', '' ),
264 array( 'Special:Version/param', 'param' ),
269 * Auth-less test of Title::isValidMoveOperation
272 * @param string $source
273 * @param string $target
274 * @param array|string|bool $expected Required error
275 * @dataProvider provideTestIsValidMoveOperation
276 * @covers Title::isValidMoveOperation
277 * @covers Title::validateFileMoveOperation
279 public function testIsValidMoveOperation( $source, $target, $expected ) {
280 $this->setMwGlobals( 'wgContentHandlerUseDB', false );
281 $title = Title
::newFromText( $source );
282 $nt = Title
::newFromText( $target );
283 $errors = $title->isValidMoveOperation( $nt, false );
284 if ( $expected === true ) {
285 $this->assertTrue( $errors );
287 $errors = $this->flattenErrorsArray( $errors );
288 foreach ( (array)$expected as $error ) {
289 $this->assertContains( $error, $errors );
294 public static function provideTestIsValidMoveOperation() {
296 // for Title::isValidMoveOperation
297 array( 'Some page', '', 'badtitletext' ),
298 array( 'Test', 'Test', 'selfmove' ),
299 array( 'Special:FooBar', 'Test', 'immobile-source-namespace' ),
300 array( 'Test', 'Special:FooBar', 'immobile-target-namespace' ),
301 array( 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ),
302 array( 'Page', 'File:Test.jpg', 'nonfile-cannot-move-to-file' ),
303 // for Title::validateFileMoveOperation
304 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ),
309 * Auth-less test of Title::userCan
311 * @param array $whitelistRegexp
312 * @param string $source
313 * @param string $action
314 * @param array|string|bool $expected Required error
316 * @covers Title::checkReadPermissions
317 * @dataProvider dataWgWhitelistReadRegexp
319 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
320 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
321 // usually have only one regex, it is more concise to write the lonely regex
322 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
324 if ( is_string( $whitelistRegexp ) ) {
325 $whitelistRegexp = array( $whitelistRegexp );
328 $title = Title
::newFromDBkey( $source );
330 global $wgGroupPermissions;
331 $oldPermissions = $wgGroupPermissions;
332 // Disallow all so we can ensure our regex works
333 $wgGroupPermissions = array();
334 $wgGroupPermissions['*']['read'] = false;
336 global $wgWhitelistRead;
337 $oldWhitelist = $wgWhitelistRead;
338 // Undo any LocalSettings explicite whitelists so they won't cause a
339 // failing test to succeed. Set it to some random non sense just
340 // to make sure we properly test Title::checkReadPermissions()
341 $wgWhitelistRead = array( 'some random non sense title' );
343 global $wgWhitelistReadRegexp;
344 $oldWhitelistRegexp = $wgWhitelistReadRegexp;
345 $wgWhitelistReadRegexp = $whitelistRegexp;
347 // Just use $wgUser which in test is a user object for '127.0.0.1'
349 // Invalidate user rights cache to take in account $wgGroupPermissions
351 $wgUser->clearInstanceCache();
352 $errors = $title->userCan( $action, $wgUser );
355 $wgGroupPermissions = $oldPermissions;
356 $wgWhitelistRead = $oldWhitelist;
357 $wgWhitelistReadRegexp = $oldWhitelistRegexp;
359 if ( is_bool( $expected ) ) {
360 # Forge the assertion message depending on the assertion expectation
361 $allowableness = $expected
362 ?
" should be allowed"
363 : " should NOT be allowed";
367 "User action '$action' on [[$source]] $allowableness."
370 $errors = $this->flattenErrorsArray( $errors );
371 foreach ( (array)$expected as $error ) {
372 $this->assertContains( $error, $errors );
378 * Provides test parameter values for testWgWhitelistReadRegexp()
380 public function dataWgWhitelistReadRegexp() {
385 // Everything, if this doesn't work, we're really in trouble
386 array( '/.*/', 'Main_Page', 'read', $ALLOWED ),
387 array( '/.*/', 'Main_Page', 'edit', $DISALLOWED ),
389 // We validate against the title name, not the db key
390 array( '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ),
392 array( '/^Main/', 'Main_Page', 'read', $ALLOWED ),
393 array( '/^Main.*/', 'Main_Page', 'read', $ALLOWED ),
395 array( '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ),
397 // ...without unicode modifier
398 array( '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ),
399 // ...with unicode modifier
400 array( '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ),
402 array( '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ),
403 array( '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ),
405 // From DefaultSettings.php:
406 array( "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ),
407 array( "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ),
410 array( '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ),
411 array( null, 'Special:Newpages', 'read', $DISALLOWED ),
416 public function flattenErrorsArray( $errors ) {
418 foreach ( $errors as $error ) {
419 $result[] = $error[0];
426 * @dataProvider provideGetPageViewLanguage
427 * @covers Title::getPageViewLanguage
429 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
430 $lang, $variant, $msg = ''
432 // Setup environnement for this test
433 $this->setMwGlobals( array(
434 'wgLanguageCode' => $contLang,
435 'wgContLang' => Language
::factory( $contLang ),
436 'wgLang' => Language
::factory( $lang ),
437 'wgDefaultLanguageVariant' => $variant,
438 '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 static 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 static 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 static 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 static 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() );