00c29ee42a8eff4bf245eddc805c6f0194af8fc6
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() {
87 // Bad characters forbidden regardless of wgLegalTitleChars
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' ),
107 // Directory navigation
110 array( './Sandbox' ),
111 array( '../Sandbox' ),
112 array( 'Foo/./Sandbox' ),
113 array( 'Foo/../Sandbox' ),
114 array( 'Sandbox/.' ),
115 array( 'Sandbox/..' ),
117 array( 'A ~~~ Name' ),
118 array( 'A ~~~~ Signature' ),
119 array( 'A ~~~~~ Timestamp' ),
120 array( str_repeat( 'x', 256 ) ),
121 // Namespace prefix without actual title
124 array( 'Category: ' ),
125 array( 'Category: #bar' ),
127 array( 'localtestiw: Talk: # anchor' ),
128 array( 'localtestiw: Talk:' )
132 private function secureAndSplitGlobals() {
133 $this->setMwGlobals( array(
134 'wgLocalInterwikis' => array( 'localtestiw' ),
136 'InterwikiLoadPrefix' => array(
137 function ( $prefix, &$data ) {
138 if ( $prefix === 'localtestiw' ) {
139 $data = array( 'iw_url' => 'localtestiw' );
140 } elseif ( $prefix === 'remotetestiw' ) {
141 $data = array( 'iw_url' => 'remotetestiw' );
151 * See also mediawiki.Title.test.js
152 * @covers Title::secureAndSplit
153 * @dataProvider provideValidSecureAndSplit
154 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
156 public function testSecureAndSplitValid( $text ) {
157 $this->secureAndSplitGlobals();
158 $this->assertInstanceOf( 'Title', Title
::newFromText( $text ), "Valid: $text" );
162 * See also mediawiki.Title.test.js
163 * @covers Title::secureAndSplit
164 * @dataProvider provideInvalidSecureAndSplit
165 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
167 public function testSecureAndSplitInvalid( $text ) {
168 $this->secureAndSplitGlobals();
169 $this->assertNull( Title
::newFromText( $text ), "Invalid: $text" );
172 public static function provideConvertByteClassToUnicodeClass() {
175 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
176 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
180 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
183 'QWERTY\\x66-\\xFD+',
184 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
192 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
195 'QWERTY\\x66-\\x80+\\x23',
196 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
199 'QWERTY\\x66-\\x80+\\xD3',
200 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
204 '\\\\\\u0080-\\uFFFF',
208 '\\-\\u0080-\\uFFFF',
212 'QWERTY\\-\\u0080-\\uFFFF',
220 'A-\\x7F\\u0080-\\uFFFF',
223 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
224 'f-wQWERTYFXZ\\u0080-\\uFFFF',
227 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
228 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
234 * @dataProvider provideConvertByteClassToUnicodeClass
235 * @covers Title::convertByteClassToUnicodeClass
237 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
238 $this->assertEquals( $unicodeClass, Title
::convertByteClassToUnicodeClass( $byteClass ) );
242 * @dataProvider provideSpecialNamesWithAndWithoutParameter
243 * @covers Title::fixSpecialName
245 public function testFixSpecialNameRetainsParameter( $text, $expectedParam ) {
246 $title = Title
::newFromText( $text );
247 $fixed = $title->fixSpecialName();
248 $stuff = explode( '/', $fixed->getDBkey(), 2 );
249 if ( count( $stuff ) == 2 ) {
257 "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter"
261 public static function provideSpecialNamesWithAndWithoutParameter() {
263 array( 'Special:Version', null ),
264 array( 'Special:Version/', '' ),
265 array( 'Special:Version/param', 'param' ),
270 * Auth-less test of Title::isValidMoveOperation
273 * @param string $source
274 * @param string $target
275 * @param array|string|bool $expected Required error
276 * @dataProvider provideTestIsValidMoveOperation
277 * @covers Title::isValidMoveOperation
278 * @covers Title::validateFileMoveOperation
280 public function testIsValidMoveOperation( $source, $target, $expected ) {
281 $this->setMwGlobals( 'wgContentHandlerUseDB', false );
282 $title = Title
::newFromText( $source );
283 $nt = Title
::newFromText( $target );
284 $errors = $title->isValidMoveOperation( $nt, false );
285 if ( $expected === true ) {
286 $this->assertTrue( $errors );
288 $errors = $this->flattenErrorsArray( $errors );
289 foreach ( (array)$expected as $error ) {
290 $this->assertContains( $error, $errors );
295 public static function provideTestIsValidMoveOperation() {
297 // for Title::isValidMoveOperation
298 array( 'Some page', '', 'badtitletext' ),
299 array( 'Test', 'Test', 'selfmove' ),
300 array( 'Special:FooBar', 'Test', 'immobile-source-namespace' ),
301 array( 'Test', 'Special:FooBar', 'immobile-target-namespace' ),
302 array( 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ),
303 array( 'Page', 'File:Test.jpg', 'nonfile-cannot-move-to-file' ),
304 // for Title::validateFileMoveOperation
305 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ),
310 * Auth-less test of Title::userCan
312 * @param array $whitelistRegexp
313 * @param string $source
314 * @param string $action
315 * @param array|string|bool $expected Required error
317 * @covers Title::checkReadPermissions
318 * @dataProvider dataWgWhitelistReadRegexp
320 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
321 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
322 // usually have only one regex, it is more concise to write the lonely regex
323 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
325 if ( is_string( $whitelistRegexp ) ) {
326 $whitelistRegexp = array( $whitelistRegexp );
329 $this->setMwGlobals( array(
330 // So User::isEveryoneAllowed( 'read' ) === false
331 'wgGroupPermissions' => array( '*' => array( 'read' => false ) ),
332 'wgWhitelistRead' => array( 'some random non sense title' ),
333 'wgWhitelistReadRegexp' => $whitelistRegexp,
336 $title = Title
::newFromDBkey( $source );
338 // New anonymous user with no rights
340 $user->mRights
= array();
341 $errors = $title->userCan( $action, $user );
343 if ( is_bool( $expected ) ) {
344 # Forge the assertion message depending on the assertion expectation
345 $allowableness = $expected
346 ?
" should be allowed"
347 : " should NOT be allowed";
351 "User action '$action' on [[$source]] $allowableness."
354 $errors = $this->flattenErrorsArray( $errors );
355 foreach ( (array)$expected as $error ) {
356 $this->assertContains( $error, $errors );
362 * Provides test parameter values for testWgWhitelistReadRegexp()
364 public function dataWgWhitelistReadRegexp() {
369 // Everything, if this doesn't work, we're really in trouble
370 array( '/.*/', 'Main_Page', 'read', $ALLOWED ),
371 array( '/.*/', 'Main_Page', 'edit', $DISALLOWED ),
373 // We validate against the title name, not the db key
374 array( '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ),
376 array( '/^Main/', 'Main_Page', 'read', $ALLOWED ),
377 array( '/^Main.*/', 'Main_Page', 'read', $ALLOWED ),
379 array( '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ),
381 // ...without unicode modifier
382 array( '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ),
383 // ...with unicode modifier
384 array( '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ),
386 array( '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ),
387 array( '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ),
389 // From DefaultSettings.php:
390 array( "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ),
391 array( "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ),
394 array( '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ),
395 array( null, 'Special:Newpages', 'read', $DISALLOWED ),
400 public function flattenErrorsArray( $errors ) {
402 foreach ( $errors as $error ) {
403 $result[] = $error[0];
410 * @dataProvider provideGetPageViewLanguage
411 * @covers Title::getPageViewLanguage
413 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
414 $lang, $variant, $msg = ''
416 // Setup environnement for this test
417 $this->setMwGlobals( array(
418 'wgLanguageCode' => $contLang,
419 'wgContLang' => Language
::factory( $contLang ),
420 'wgLang' => Language
::factory( $lang ),
421 'wgDefaultLanguageVariant' => $variant,
422 'wgAllowUserJs' => true,
425 $title = Title
::newFromText( $titleText );
426 $this->assertInstanceOf( 'Title', $title,
427 "Test must be passed a valid title text, you gave '$titleText'"
429 $this->assertEquals( $expected,
430 $title->getPageViewLanguage()->getCode(),
435 public static function provideGetPageViewLanguage() {
439 # - wgContLang (expected in most case)
440 # - wgLang (on some specific pages)
441 # - wgDefaultLanguageVariant
444 array( 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ),
445 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ),
446 array( 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ),
448 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ),
449 array( 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ),
450 array( 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ),
451 array( 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ),
452 array( 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ),
453 array( 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ),
454 array( 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ),
455 array( 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
457 array( 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ),
458 array( 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ),
459 array( 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ),
460 array( 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ),
461 array( 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ),
462 array( 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ),
463 array( 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ),
464 array( 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ),
465 array( 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ),
466 array( 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
468 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
469 array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),
475 * @dataProvider provideBaseTitleCases
476 * @covers Title::getBaseText
478 public function testGetBaseText( $title, $expected, $msg = '' ) {
479 $title = Title
::newFromText( $title );
480 $this->assertEquals( $expected,
481 $title->getBaseText(),
486 public static function provideBaseTitleCases() {
488 # Title, expected base, optional message
489 array( 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
490 array( 'User:Foo/Bar/Baz', 'Foo/Bar' ),
495 * @dataProvider provideRootTitleCases
496 * @covers Title::getRootText
498 public function testGetRootText( $title, $expected, $msg = '' ) {
499 $title = Title
::newFromText( $title );
500 $this->assertEquals( $expected,
501 $title->getRootText(),
506 public static function provideRootTitleCases() {
508 # Title, expected base, optional message
509 array( 'User:John_Doe/subOne/subTwo', 'John Doe' ),
510 array( 'User:Foo/Bar/Baz', 'Foo' ),
515 * @todo Handle $wgNamespacesWithSubpages cases
516 * @dataProvider provideSubpageTitleCases
517 * @covers Title::getSubpageText
519 public function testGetSubpageText( $title, $expected, $msg = '' ) {
520 $title = Title
::newFromText( $title );
521 $this->assertEquals( $expected,
522 $title->getSubpageText(),
527 public static function provideSubpageTitleCases() {
529 # Title, expected base, optional message
530 array( 'User:John_Doe/subOne/subTwo', 'subTwo' ),
531 array( 'User:John_Doe/subOne', 'subOne' ),
535 public static function provideNewFromTitleValue() {
537 array( new TitleValue( NS_MAIN
, 'Foo' ) ),
538 array( new TitleValue( NS_MAIN
, 'Foo', 'bar' ) ),
539 array( new TitleValue( NS_USER
, 'Hansi_Maier' ) ),
544 * @dataProvider provideNewFromTitleValue
546 public function testNewFromTitleValue( TitleValue
$value ) {
547 $title = Title
::newFromTitleValue( $value );
549 $dbkey = str_replace( ' ', '_', $value->getText() );
550 $this->assertEquals( $dbkey, $title->getDBkey() );
551 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
552 $this->assertEquals( $value->getFragment(), $title->getFragment() );
555 public static function provideGetTitleValue() {
559 array( 'User:Hansi_Maier' ),
564 * @dataProvider provideGetTitleValue
566 public function testGetTitleValue( $text ) {
567 $title = Title
::newFromText( $text );
568 $value = $title->getTitleValue();
570 $dbkey = str_replace( ' ', '_', $value->getText() );
571 $this->assertEquals( $title->getDBkey(), $dbkey );
572 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
573 $this->assertEquals( $title->getFragment(), $value->getFragment() );
576 public static function provideGetFragment() {
579 array( 'Foo#bar', 'bar' ),
580 array( 'Foo#bär', 'bär' ),
582 // Inner whitespace is normalized
583 array( 'Foo#bar_bar', 'bar bar' ),
584 array( 'Foo#bar bar', 'bar bar' ),
585 array( 'Foo#bar bar', 'bar bar' ),
587 // Leading whitespace is kept, trailing whitespace is trimmed.
588 // XXX: Is this really want we want?
589 array( 'Foo#_bar_bar_', ' bar bar' ),
590 array( 'Foo# bar bar ', ' bar bar' ),
595 * @dataProvider provideGetFragment
597 * @param string $full
598 * @param string $fragment
600 public function testGetFragment( $full, $fragment ) {
601 $title = Title
::newFromText( $full );
602 $this->assertEquals( $fragment, $title->getFragment() );
606 * @covers Title::isAlwaysKnown
607 * @dataProvider provideIsAlwaysKnown
608 * @param string $page
609 * @param bool $isKnown
611 public function testIsAlwaysKnown( $page, $isKnown ) {
612 $title = Title
::newFromText( $page );
613 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
616 public static function provideIsAlwaysKnown() {
618 array( 'Some nonexistent page', false ),
619 array( 'UTPage', false ),
620 array( '#test', true ),
621 array( 'Special:BlankPage', true ),
622 array( 'Special:SomeNonexistentSpecialPage', false ),
623 array( 'MediaWiki:Parentheses', true ),
624 array( 'MediaWiki:Some nonexistent message', false ),
629 * @covers Title::isAlwaysKnown
631 public function testIsAlwaysKnownOnInterwiki() {
632 $title = Title
::makeTitle( NS_MAIN
, 'Interwiki link', '', 'externalwiki' );
633 $this->assertTrue( $title->isAlwaysKnown() );