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',
85 // Length is 256 total, but only title part matters
86 'Category:' . str_repeat( 'x', 248 ),
87 str_repeat( 'x', 252 ),
89 'localtestiw: #anchor',
92 'localtestiw: foo # anchor',
93 'localtestiw: Talk: Sandbox # anchor',
95 'remotetestiw: Talk: # anchor',
97 'remotetestiw: Talk:',
98 'remotetestiw: Talk: Foo',
99 'localtestiw:remotetestiw:',
100 'localtestiw:remotetestiw:foo'
102 $this->assertInstanceOf( 'Title', Title
::newFromText( $text ), "Valid: $text" );
111 // Bad characters forbidden regardless of wgLegalTitleChars
123 // XML/HTML character entity references
124 // Note: Commented out because they are not marked invalid by the PHP test as
125 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
129 // Subject of NS_TALK does not roundtrip to NS_MAIN
130 'Talk:File:Example.svg',
131 // Directory navigation
144 str_repeat( 'x', 256 ),
145 // Namespace prefix without actual title
151 'localtestiw: Talk: # anchor',
154 $this->assertNull( Title
::newFromText( $text ), "Invalid: $text" );
158 public static function provideConvertByteClassToUnicodeClass() {
161 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
162 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
166 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
169 'QWERTY\\x66-\\xFD+',
170 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
178 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
181 'QWERTY\\x66-\\x80+\\x23',
182 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
185 'QWERTY\\x66-\\x80+\\xD3',
186 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
190 '\\\\\\u0080-\\uFFFF',
194 '\\-\\u0080-\\uFFFF',
198 'QWERTY\\-\\u0080-\\uFFFF',
206 'A-\\x7F\\u0080-\\uFFFF',
209 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
210 'f-wQWERTYFXZ\\u0080-\\uFFFF',
213 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
214 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
220 * @dataProvider provideConvertByteClassToUnicodeClass
221 * @covers Title::convertByteClassToUnicodeClass
223 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
224 $this->assertEquals( $unicodeClass, Title
::convertByteClassToUnicodeClass( $byteClass ) );
228 * @dataProvider provideBug31100
229 * @covers Title::fixSpecialName
230 * @todo give this test a real name explaining what is being tested here
232 public function testBug31100FixSpecialName( $text, $expectedParam ) {
233 $title = Title
::newFromText( $text );
234 $fixed = $title->fixSpecialName();
235 $stuff = explode( '/', $fixed->getDBkey(), 2 );
236 if ( count( $stuff ) == 2 ) {
244 "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter"
248 public static function provideBug31100() {
250 array( 'Special:Version', null ),
251 array( 'Special:Version/', '' ),
252 array( 'Special:Version/param', 'param' ),
257 * Auth-less test of Title::isValidMoveOperation
260 * @param string $source
261 * @param string $target
262 * @param array|string|bool $expected Required error
263 * @dataProvider provideTestIsValidMoveOperation
264 * @covers Title::isValidMoveOperation
266 public function testIsValidMoveOperation( $source, $target, $expected ) {
267 $title = Title
::newFromText( $source );
268 $nt = Title
::newFromText( $target );
269 $errors = $title->isValidMoveOperation( $nt, false );
270 if ( $expected === true ) {
271 $this->assertTrue( $errors );
273 $errors = $this->flattenErrorsArray( $errors );
274 foreach ( (array)$expected as $error ) {
275 $this->assertContains( $error, $errors );
281 * Provides test parameter values for testIsValidMoveOperation()
283 public function dataTestIsValidMoveOperation() {
285 array( 'Test', 'Test', 'selfmove' ),
286 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
291 * Auth-less test of Title::userCan
293 * @param array $whitelistRegexp
294 * @param string $source
295 * @param string $action
296 * @param array|string|bool $expected Required error
298 * @covers Title::checkReadPermissions
299 * @dataProvider dataWgWhitelistReadRegexp
301 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
302 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
303 // usually have only one regex, it is more concise to write the lonely regex
304 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
306 if ( is_string( $whitelistRegexp ) ) {
307 $whitelistRegexp = array( $whitelistRegexp );
310 $title = Title
::newFromDBkey( $source );
312 global $wgGroupPermissions;
313 $oldPermissions = $wgGroupPermissions;
314 // Disallow all so we can ensure our regex works
315 $wgGroupPermissions = array();
316 $wgGroupPermissions['*']['read'] = false;
318 global $wgWhitelistRead;
319 $oldWhitelist = $wgWhitelistRead;
320 // Undo any LocalSettings explicite whitelists so they won't cause a
321 // failing test to succeed. Set it to some random non sense just
322 // to make sure we properly test Title::checkReadPermissions()
323 $wgWhitelistRead = array( 'some random non sense title' );
325 global $wgWhitelistReadRegexp;
326 $oldWhitelistRegexp = $wgWhitelistReadRegexp;
327 $wgWhitelistReadRegexp = $whitelistRegexp;
329 // Just use $wgUser which in test is a user object for '127.0.0.1'
331 // Invalidate user rights cache to take in account $wgGroupPermissions
333 $wgUser->clearInstanceCache();
334 $errors = $title->userCan( $action, $wgUser );
337 $wgGroupPermissions = $oldPermissions;
338 $wgWhitelistRead = $oldWhitelist;
339 $wgWhitelistReadRegexp = $oldWhitelistRegexp;
341 if ( is_bool( $expected ) ) {
342 # Forge the assertion message depending on the assertion expectation
343 $allowableness = $expected
344 ?
" should be allowed"
345 : " should NOT be allowed";
349 "User action '$action' on [[$source]] $allowableness."
352 $errors = $this->flattenErrorsArray( $errors );
353 foreach ( (array)$expected as $error ) {
354 $this->assertContains( $error, $errors );
360 * Provides test parameter values for testWgWhitelistReadRegexp()
362 public function dataWgWhitelistReadRegexp() {
367 // Everything, if this doesn't work, we're really in trouble
368 array( '/.*/', 'Main_Page', 'read', $ALLOWED ),
369 array( '/.*/', 'Main_Page', 'edit', $DISALLOWED ),
371 // We validate against the title name, not the db key
372 array( '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ),
374 array( '/^Main/', 'Main_Page', 'read', $ALLOWED ),
375 array( '/^Main.*/', 'Main_Page', 'read', $ALLOWED ),
377 array( '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ),
379 // ...without unicode modifier
380 array( '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ),
381 // ...with unicode modifier
382 array( '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ),
384 array( '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ),
385 array( '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ),
387 // From DefaultSettings.php:
388 array( "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ),
389 array( "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ),
392 array( '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ),
393 array( null, 'Special:Newpages', 'read', $DISALLOWED ),
398 public function flattenErrorsArray( $errors ) {
400 foreach ( $errors as $error ) {
401 $result[] = $error[0];
407 public static function provideTestIsValidMoveOperation() {
409 array( 'Test', 'Test', 'selfmove' ),
410 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
415 * @dataProvider provideGetPageViewLanguage
416 * @covers Title::getPageViewLanguage
418 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
419 $lang, $variant, $msg = ''
421 global $wgLanguageCode, $wgContLang, $wgLang, $wgDefaultLanguageVariant, $wgAllowUserJs;
423 // Setup environnement for this test
424 $wgLanguageCode = $contLang;
425 $wgContLang = Language
::factory( $contLang );
426 $wgLang = Language
::factory( $lang );
427 $wgDefaultLanguageVariant = $variant;
428 $wgAllowUserJs = true;
430 $title = Title
::newFromText( $titleText );
431 $this->assertInstanceOf( 'Title', $title,
432 "Test must be passed a valid title text, you gave '$titleText'"
434 $this->assertEquals( $expected,
435 $title->getPageViewLanguage()->getCode(),
440 public static function provideGetPageViewLanguage() {
444 # - wgContLang (expected in most case)
445 # - wgLang (on some specific pages)
446 # - wgDefaultLanguageVariant
449 array( 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ),
450 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ),
451 array( 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ),
453 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ),
454 array( 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ),
455 array( 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ),
456 array( 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ),
457 array( 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ),
458 array( 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ),
459 array( 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ),
460 array( 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
462 array( 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ),
463 array( 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ),
464 array( 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ),
465 array( 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ),
466 array( 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ),
467 array( 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ),
468 array( 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ),
469 array( 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ),
470 array( 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ),
471 array( 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
473 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
474 array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),
480 * @dataProvider provideBaseTitleCases
481 * @covers Title::getBaseText
483 public function testGetBaseText( $title, $expected, $msg = '' ) {
484 $title = Title
::newFromText( $title );
485 $this->assertEquals( $expected,
486 $title->getBaseText(),
491 public static function provideBaseTitleCases() {
493 # Title, expected base, optional message
494 array( 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
495 array( 'User:Foo/Bar/Baz', 'Foo/Bar' ),
500 * @dataProvider provideRootTitleCases
501 * @covers Title::getRootText
503 public function testGetRootText( $title, $expected, $msg = '' ) {
504 $title = Title
::newFromText( $title );
505 $this->assertEquals( $expected,
506 $title->getRootText(),
511 public static function provideRootTitleCases() {
513 # Title, expected base, optional message
514 array( 'User:John_Doe/subOne/subTwo', 'John Doe' ),
515 array( 'User:Foo/Bar/Baz', 'Foo' ),
520 * @todo Handle $wgNamespacesWithSubpages cases
521 * @dataProvider provideSubpageTitleCases
522 * @covers Title::getSubpageText
524 public function testGetSubpageText( $title, $expected, $msg = '' ) {
525 $title = Title
::newFromText( $title );
526 $this->assertEquals( $expected,
527 $title->getSubpageText(),
532 public static function provideSubpageTitleCases() {
534 # Title, expected base, optional message
535 array( 'User:John_Doe/subOne/subTwo', 'subTwo' ),
536 array( 'User:John_Doe/subOne', 'subOne' ),
540 public function provideNewFromTitleValue() {
542 array( new TitleValue( NS_MAIN
, 'Foo' ) ),
543 array( new TitleValue( NS_MAIN
, 'Foo', 'bar' ) ),
544 array( new TitleValue( NS_USER
, 'Hansi_Maier' ) ),
549 * @dataProvider provideNewFromTitleValue
551 public function testNewFromTitleValue( TitleValue
$value ) {
552 $title = Title
::newFromTitleValue( $value );
554 $dbkey = str_replace( ' ', '_', $value->getText() );
555 $this->assertEquals( $dbkey, $title->getDBkey() );
556 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
557 $this->assertEquals( $value->getFragment(), $title->getFragment() );
560 public function provideGetTitleValue() {
564 array( 'User:Hansi_Maier' ),
569 * @dataProvider provideGetTitleValue
571 public function testGetTitleValue( $text ) {
572 $title = Title
::newFromText( $text );
573 $value = $title->getTitleValue();
575 $dbkey = str_replace( ' ', '_', $value->getText() );
576 $this->assertEquals( $title->getDBkey(), $dbkey );
577 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
578 $this->assertEquals( $title->getFragment(), $value->getFragment() );
581 public function provideGetFragment() {
584 array( 'Foo#bar', 'bar' ),
585 array( 'Foo#bär', 'bär' ),
587 // Inner whitespace is normalized
588 array( 'Foo#bar_bar', 'bar bar' ),
589 array( 'Foo#bar bar', 'bar bar' ),
590 array( 'Foo#bar bar', 'bar bar' ),
592 // Leading whitespace is kept, trailing whitespace is trimmed.
593 // XXX: Is this really want we want?
594 array( 'Foo#_bar_bar_', ' bar bar' ),
595 array( 'Foo# bar bar ', ' bar bar' ),
600 * @dataProvider provideGetFragment
602 * @param string $full
603 * @param string $fragment
605 public function testGetFragment( $full, $fragment ) {
606 $title = Title
::newFromText( $full );
607 $this->assertEquals( $fragment, $title->getFragment() );