5 * ^--- needed for language cache stuff
9 class TitleTest
extends MediaWikiTestCase
{
10 protected function setUp() {
13 $this->setMwGlobals( array(
14 'wgLanguageCode' => 'en',
15 'wgContLang' => Language
::factory( 'en' ),
17 'wgLang' => Language
::factory( 'en' ),
18 'wgAllowUserJs' => false,
19 'wgDefaultLanguageVariant' => false,
24 * @covers Title::legalChars
26 public function testLegalChars() {
27 $titlechars = Title
::legalChars();
29 foreach ( range( 1, 255 ) as $num ) {
31 if ( strpos( "#[]{}<>|", $chr ) !== false ||
preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
33 (bool)preg_match( "/[$titlechars]/", $chr ),
34 "chr($num) = $chr is not a valid titlechar"
38 (bool)preg_match( "/[$titlechars]/", $chr ),
39 "chr($num) = $chr is a valid titlechar"
46 * See also mediawiki.Title.test.js
47 * @covers Title::secureAndSplit
48 * @todo This method should be split into 2 separate tests each with a provider
49 * @note: This mainly tests MediaWikiTitleCodec::parseTitle().
51 public function testSecureAndSplit() {
52 $this->setMwGlobals( array(
53 'wgLocalInterwikis' => array( 'localtestiw' ),
55 'InterwikiLoadPrefix' => array(
56 function ( $prefix, &$data ) {
57 if ( $prefix === 'localtestiw' ) {
58 $data = array( 'iw_url' => 'localtestiw' );
59 } elseif ( $prefix === 'remotetestiw' ) {
60 $data = array( 'iw_url' => 'remotetestiw' );
80 'File_talk:Example.svg',
84 // Length is 256 total, but only title part matters
85 'Category:' . str_repeat( 'x', 248 ),
86 str_repeat( 'x', 252 ),
88 'localtestiw: #anchor',
90 'localtestiw: foo # anchor',
91 'localtestiw: Talk: Sandbox # anchor',
93 'remotetestiw: Talk: # anchor',
95 'remotetestiw: Talk:',
96 'remotetestiw: Talk: Foo',
97 'localtestiw:remotetestiw:',
98 'localtestiw:remotetestiw:foo'
100 $this->assertInstanceOf( 'Title', Title
::newFromText( $text ), "Valid: $text" );
109 // Bad characters forbidden regardless of wgLegalTitleChars
121 // XML/HTML character entity references
122 // Note: Commented out because they are not marked invalid by the PHP test as
123 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
127 // Subject of NS_TALK does not roundtrip to NS_MAIN
128 'Talk:File:Example.svg',
129 // Directory navigation
142 str_repeat( 'x', 256 ),
143 // Namespace prefix without actual title
150 'localtestiw: Talk: # anchor',
153 $this->assertNull( Title
::newFromText( $text ), "Invalid: $text" );
157 public static function provideConvertByteClassToUnicodeClass() {
160 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
161 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
165 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
168 'QWERTY\\x66-\\xFD+',
169 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
177 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
180 'QWERTY\\x66-\\x80+\\x23',
181 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
184 'QWERTY\\x66-\\x80+\\xD3',
185 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
189 '\\\\\\u0080-\\uFFFF',
193 '\\-\\u0080-\\uFFFF',
197 'QWERTY\\-\\u0080-\\uFFFF',
205 'A-\\x7F\\u0080-\\uFFFF',
208 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
209 'f-wQWERTYFXZ\\u0080-\\uFFFF',
212 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
213 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
219 * @dataProvider provideConvertByteClassToUnicodeClass
220 * @covers Title::convertByteClassToUnicodeClass
222 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
223 $this->assertEquals( $unicodeClass, Title
::convertByteClassToUnicodeClass( $byteClass ) );
227 * @dataProvider provideBug31100
228 * @covers Title::fixSpecialName
229 * @todo give this test a real name explaining what is being tested here
231 public function testBug31100FixSpecialName( $text, $expectedParam ) {
232 $title = Title
::newFromText( $text );
233 $fixed = $title->fixSpecialName();
234 $stuff = explode( '/', $fixed->getDBkey(), 2 );
235 if ( count( $stuff ) == 2 ) {
243 "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter"
247 public static function provideBug31100() {
249 array( 'Special:Version', null ),
250 array( 'Special:Version/', '' ),
251 array( 'Special:Version/param', 'param' ),
256 * Auth-less test of Title::isValidMoveOperation
259 * @param string $source
260 * @param string $target
261 * @param array|string|bool $expected Required error
262 * @dataProvider provideTestIsValidMoveOperation
263 * @covers Title::isValidMoveOperation
265 public function testIsValidMoveOperation( $source, $target, $expected ) {
266 $title = Title
::newFromText( $source );
267 $nt = Title
::newFromText( $target );
268 $errors = $title->isValidMoveOperation( $nt, false );
269 if ( $expected === true ) {
270 $this->assertTrue( $errors );
272 $errors = $this->flattenErrorsArray( $errors );
273 foreach ( (array)$expected as $error ) {
274 $this->assertContains( $error, $errors );
280 * Provides test parameter values for testIsValidMoveOperation()
282 public function dataTestIsValidMoveOperation() {
284 array( 'Test', 'Test', 'selfmove' ),
285 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
290 * Auth-less test of Title::userCan
292 * @param array $whitelistRegexp
293 * @param string $source
294 * @param string $action
295 * @param array|string|bool $expected Required error
297 * @covers Title::checkReadPermissions
298 * @dataProvider dataWgWhitelistReadRegexp
300 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
301 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
302 // usually have only one regex, it is more concise to write the lonely regex
303 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
305 if ( is_string( $whitelistRegexp ) ) {
306 $whitelistRegexp = array( $whitelistRegexp );
309 $title = Title
::newFromDBkey( $source );
311 global $wgGroupPermissions;
312 $oldPermissions = $wgGroupPermissions;
313 // Disallow all so we can ensure our regex works
314 $wgGroupPermissions = array();
315 $wgGroupPermissions['*']['read'] = false;
317 global $wgWhitelistRead;
318 $oldWhitelist = $wgWhitelistRead;
319 // Undo any LocalSettings explicite whitelists so they won't cause a
320 // failing test to succeed. Set it to some random non sense just
321 // to make sure we properly test Title::checkReadPermissions()
322 $wgWhitelistRead = array( 'some random non sense title' );
324 global $wgWhitelistReadRegexp;
325 $oldWhitelistRegexp = $wgWhitelistReadRegexp;
326 $wgWhitelistReadRegexp = $whitelistRegexp;
328 // Just use $wgUser which in test is a user object for '127.0.0.1'
330 // Invalidate user rights cache to take in account $wgGroupPermissions
332 $wgUser->clearInstanceCache();
333 $errors = $title->userCan( $action, $wgUser );
336 $wgGroupPermissions = $oldPermissions;
337 $wgWhitelistRead = $oldWhitelist;
338 $wgWhitelistReadRegexp = $oldWhitelistRegexp;
340 if ( is_bool( $expected ) ) {
341 # Forge the assertion message depending on the assertion expectation
342 $allowableness = $expected
343 ?
" should be allowed"
344 : " should NOT be allowed";
348 "User action '$action' on [[$source]] $allowableness."
351 $errors = $this->flattenErrorsArray( $errors );
352 foreach ( (array)$expected as $error ) {
353 $this->assertContains( $error, $errors );
359 * Provides test parameter values for testWgWhitelistReadRegexp()
361 public function dataWgWhitelistReadRegexp() {
366 // Everything, if this doesn't work, we're really in trouble
367 array( '/.*/', 'Main_Page', 'read', $ALLOWED ),
368 array( '/.*/', 'Main_Page', 'edit', $DISALLOWED ),
370 // We validate against the title name, not the db key
371 array( '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ),
373 array( '/^Main/', 'Main_Page', 'read', $ALLOWED ),
374 array( '/^Main.*/', 'Main_Page', 'read', $ALLOWED ),
376 array( '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ),
378 // ...without unicode modifier
379 array( '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ),
380 // ...with unicode modifier
381 array( '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ),
383 array( '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ),
384 array( '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ),
386 // From DefaultSettings.php:
387 array( "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ),
388 array( "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ),
391 array( '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ),
392 array( null, 'Special:Newpages', 'read', $DISALLOWED ),
397 public function flattenErrorsArray( $errors ) {
399 foreach ( $errors as $error ) {
400 $result[] = $error[0];
406 public static function provideTestIsValidMoveOperation() {
408 array( 'Test', 'Test', 'selfmove' ),
409 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
414 * @dataProvider provideGetPageViewLanguage
415 * @covers Title::getPageViewLanguage
417 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
418 $lang, $variant, $msg = ''
420 global $wgLanguageCode, $wgContLang, $wgLang, $wgDefaultLanguageVariant, $wgAllowUserJs;
422 // Setup environnement for this test
423 $wgLanguageCode = $contLang;
424 $wgContLang = Language
::factory( $contLang );
425 $wgLang = Language
::factory( $lang );
426 $wgDefaultLanguageVariant = $variant;
427 $wgAllowUserJs = true;
429 $title = Title
::newFromText( $titleText );
430 $this->assertInstanceOf( 'Title', $title,
431 "Test must be passed a valid title text, you gave '$titleText'"
433 $this->assertEquals( $expected,
434 $title->getPageViewLanguage()->getCode(),
439 public static function provideGetPageViewLanguage() {
443 # - wgContLang (expected in most case)
444 # - wgLang (on some specific pages)
445 # - wgDefaultLanguageVariant
448 array( 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ),
449 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ),
450 array( 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ),
452 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ),
453 array( 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ),
454 array( 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ),
455 array( 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ),
456 array( 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ),
457 array( 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ),
458 array( 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ),
459 array( 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
461 array( 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ),
462 array( 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ),
463 array( 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ),
464 array( 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ),
465 array( 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ),
466 array( 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ),
467 array( 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ),
468 array( 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ),
469 array( 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ),
470 array( 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
472 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
473 array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),
479 * @dataProvider provideBaseTitleCases
480 * @covers Title::getBaseText
482 public function testGetBaseText( $title, $expected, $msg = '' ) {
483 $title = Title
::newFromText( $title );
484 $this->assertEquals( $expected,
485 $title->getBaseText(),
490 public static function provideBaseTitleCases() {
492 # Title, expected base, optional message
493 array( 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
494 array( 'User:Foo/Bar/Baz', 'Foo/Bar' ),
499 * @dataProvider provideRootTitleCases
500 * @covers Title::getRootText
502 public function testGetRootText( $title, $expected, $msg = '' ) {
503 $title = Title
::newFromText( $title );
504 $this->assertEquals( $expected,
505 $title->getRootText(),
510 public static function provideRootTitleCases() {
512 # Title, expected base, optional message
513 array( 'User:John_Doe/subOne/subTwo', 'John Doe' ),
514 array( 'User:Foo/Bar/Baz', 'Foo' ),
519 * @todo Handle $wgNamespacesWithSubpages cases
520 * @dataProvider provideSubpageTitleCases
521 * @covers Title::getSubpageText
523 public function testGetSubpageText( $title, $expected, $msg = '' ) {
524 $title = Title
::newFromText( $title );
525 $this->assertEquals( $expected,
526 $title->getSubpageText(),
531 public static function provideSubpageTitleCases() {
533 # Title, expected base, optional message
534 array( 'User:John_Doe/subOne/subTwo', 'subTwo' ),
535 array( 'User:John_Doe/subOne', 'subOne' ),
539 public function provideNewFromTitleValue() {
541 array( new TitleValue( NS_MAIN
, 'Foo' ) ),
542 array( new TitleValue( NS_MAIN
, 'Foo', 'bar' ) ),
543 array( new TitleValue( NS_USER
, 'Hansi_Maier' ) ),
548 * @dataProvider provideNewFromTitleValue
550 public function testNewFromTitleValue( TitleValue
$value ) {
551 $title = Title
::newFromTitleValue( $value );
553 $dbkey = str_replace( ' ', '_', $value->getText() );
554 $this->assertEquals( $dbkey, $title->getDBkey() );
555 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
556 $this->assertEquals( $value->getFragment(), $title->getFragment() );
559 public function provideGetTitleValue() {
563 array( 'User:Hansi_Maier' ),
568 * @dataProvider provideGetTitleValue
570 public function testGetTitleValue( $text ) {
571 $title = Title
::newFromText( $text );
572 $value = $title->getTitleValue();
574 $dbkey = str_replace( ' ', '_', $value->getText() );
575 $this->assertEquals( $title->getDBkey(), $dbkey );
576 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
577 $this->assertEquals( $title->getFragment(), $value->getFragment() );
580 public function provideGetFragment() {
583 array( 'Foo#bar', 'bar' ),
584 array( 'Foo#bär', 'bär' ),
586 // Inner whitespace is normalized
587 array( 'Foo#bar_bar', 'bar bar' ),
588 array( 'Foo#bar bar', 'bar bar' ),
589 array( 'Foo#bar bar', 'bar bar' ),
591 // Leading whitespace is kept, trailing whitespace is trimmed.
592 // XXX: Is this really want we want?
593 array( 'Foo#_bar_bar_', ' bar bar' ),
594 array( 'Foo# bar bar ', ' bar bar' ),
599 * @dataProvider provideGetFragment
601 * @param string $full
602 * @param string $fragment
604 public function testGetFragment( $full, $fragment ) {
605 $title = Title
::newFromText( $full );
606 $this->assertEquals( $fragment, $title->getFragment() );