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"
43 * See also mediawiki.Title.test.js
44 * @covers Title::secureAndSplit
45 * @todo This method should be split into 2 separate tests each with a provider
46 * @note This mainly tests MediaWikiTitleCodec::parseTitle().
48 public function testSecureAndSplit() {
49 $this->setMwGlobals( array(
50 'wgLocalInterwikis' => array( 'localtestiw' ),
52 'InterwikiLoadPrefix' => array(
53 function ( $prefix, &$data ) {
54 if ( $prefix === 'localtestiw' ) {
55 $data = array( 'iw_url' => 'localtestiw' );
56 } elseif ( $prefix === 'remotetestiw' ) {
57 $data = array( 'iw_url' => 'remotetestiw' );
77 'File_talk:Example.svg',
82 // Length is 256 total, but only title part matters
83 'Category:' . str_repeat( 'x', 248 ),
84 str_repeat( 'x', 252 ),
86 'localtestiw: #anchor',
89 'localtestiw: foo # anchor',
90 'localtestiw: Talk: Sandbox # anchor',
92 'remotetestiw: Talk: # anchor',
94 'remotetestiw: Talk:',
95 'remotetestiw: Talk: Foo',
96 'localtestiw:remotetestiw:',
97 'localtestiw:remotetestiw:foo'
99 $this->assertInstanceOf( 'Title', Title
::newFromText( $text ), "Valid: $text" );
108 // Bad characters forbidden regardless of wgLegalTitleChars
120 // XML/HTML character entity references
121 // Note: Commented out because they are not marked invalid by the PHP test as
122 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
126 // Subject of NS_TALK does not roundtrip to NS_MAIN
127 'Talk:File:Example.svg',
128 // Directory navigation
141 str_repeat( 'x', 256 ),
142 // Namespace prefix without actual title
148 'localtestiw: Talk: # anchor',
151 $this->assertNull( Title
::newFromText( $text ), "Invalid: $text" );
155 public static function provideConvertByteClassToUnicodeClass() {
158 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
159 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
163 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
166 'QWERTY\\x66-\\xFD+',
167 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
175 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
178 'QWERTY\\x66-\\x80+\\x23',
179 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
182 'QWERTY\\x66-\\x80+\\xD3',
183 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
187 '\\\\\\u0080-\\uFFFF',
191 '\\-\\u0080-\\uFFFF',
195 'QWERTY\\-\\u0080-\\uFFFF',
203 'A-\\x7F\\u0080-\\uFFFF',
206 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
207 'f-wQWERTYFXZ\\u0080-\\uFFFF',
210 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
211 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
217 * @dataProvider provideConvertByteClassToUnicodeClass
218 * @covers Title::convertByteClassToUnicodeClass
220 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
221 $this->assertEquals( $unicodeClass, Title
::convertByteClassToUnicodeClass( $byteClass ) );
225 * @dataProvider provideBug31100
226 * @covers Title::fixSpecialName
227 * @todo give this test a real name explaining what is being tested here
229 public function testBug31100FixSpecialName( $text, $expectedParam ) {
230 $title = Title
::newFromText( $text );
231 $fixed = $title->fixSpecialName();
232 $stuff = explode( '/', $fixed->getDBkey(), 2 );
233 if ( count( $stuff ) == 2 ) {
241 "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter"
245 public static function provideBug31100() {
247 array( 'Special:Version', null ),
248 array( 'Special:Version/', '' ),
249 array( 'Special:Version/param', 'param' ),
254 * Auth-less test of Title::isValidMoveOperation
257 * @param string $source
258 * @param string $target
259 * @param array|string|bool $expected Required error
260 * @dataProvider provideTestIsValidMoveOperation
261 * @covers Title::isValidMoveOperation
263 public function testIsValidMoveOperation( $source, $target, $expected ) {
264 $title = Title
::newFromText( $source );
265 $nt = Title
::newFromText( $target );
266 $errors = $title->isValidMoveOperation( $nt, false );
267 if ( $expected === true ) {
268 $this->assertTrue( $errors );
270 $errors = $this->flattenErrorsArray( $errors );
271 foreach ( (array)$expected as $error ) {
272 $this->assertContains( $error, $errors );
278 * Provides test parameter values for testIsValidMoveOperation()
280 public function dataTestIsValidMoveOperation() {
282 array( 'Test', 'Test', 'selfmove' ),
283 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
288 * Auth-less test of Title::userCan
290 * @param array $whitelistRegexp
291 * @param string $source
292 * @param string $action
293 * @param array|string|bool $expected Required error
295 * @covers Title::checkReadPermissions
296 * @dataProvider dataWgWhitelistReadRegexp
298 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
299 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
300 // usually have only one regex, it is more concise to write the lonely regex
301 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
303 if ( is_string( $whitelistRegexp ) ) {
304 $whitelistRegexp = array( $whitelistRegexp );
307 $title = Title
::newFromDBkey( $source );
309 global $wgGroupPermissions;
310 $oldPermissions = $wgGroupPermissions;
311 // Disallow all so we can ensure our regex works
312 $wgGroupPermissions = array();
313 $wgGroupPermissions['*']['read'] = false;
315 global $wgWhitelistRead;
316 $oldWhitelist = $wgWhitelistRead;
317 // Undo any LocalSettings explicite whitelists so they won't cause a
318 // failing test to succeed. Set it to some random non sense just
319 // to make sure we properly test Title::checkReadPermissions()
320 $wgWhitelistRead = array( 'some random non sense title' );
322 global $wgWhitelistReadRegexp;
323 $oldWhitelistRegexp = $wgWhitelistReadRegexp;
324 $wgWhitelistReadRegexp = $whitelistRegexp;
326 // Just use $wgUser which in test is a user object for '127.0.0.1'
328 // Invalidate user rights cache to take in account $wgGroupPermissions
330 $wgUser->clearInstanceCache();
331 $errors = $title->userCan( $action, $wgUser );
334 $wgGroupPermissions = $oldPermissions;
335 $wgWhitelistRead = $oldWhitelist;
336 $wgWhitelistReadRegexp = $oldWhitelistRegexp;
338 if ( is_bool( $expected ) ) {
339 # Forge the assertion message depending on the assertion expectation
340 $allowableness = $expected
341 ?
" should be allowed"
342 : " should NOT be allowed";
346 "User action '$action' on [[$source]] $allowableness."
349 $errors = $this->flattenErrorsArray( $errors );
350 foreach ( (array)$expected as $error ) {
351 $this->assertContains( $error, $errors );
357 * Provides test parameter values for testWgWhitelistReadRegexp()
359 public function dataWgWhitelistReadRegexp() {
364 // Everything, if this doesn't work, we're really in trouble
365 array( '/.*/', 'Main_Page', 'read', $ALLOWED ),
366 array( '/.*/', 'Main_Page', 'edit', $DISALLOWED ),
368 // We validate against the title name, not the db key
369 array( '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ),
371 array( '/^Main/', 'Main_Page', 'read', $ALLOWED ),
372 array( '/^Main.*/', 'Main_Page', 'read', $ALLOWED ),
374 array( '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ),
376 // ...without unicode modifier
377 array( '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ),
378 // ...with unicode modifier
379 array( '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ),
381 array( '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ),
382 array( '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ),
384 // From DefaultSettings.php:
385 array( "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ),
386 array( "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ),
389 array( '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ),
390 array( null, 'Special:Newpages', 'read', $DISALLOWED ),
395 public function flattenErrorsArray( $errors ) {
397 foreach ( $errors as $error ) {
398 $result[] = $error[0];
404 public static function provideTestIsValidMoveOperation() {
406 array( 'Test', 'Test', 'selfmove' ),
407 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
412 * @dataProvider provideGetPageViewLanguage
413 * @covers Title::getPageViewLanguage
415 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
416 $lang, $variant, $msg = ''
418 global $wgLanguageCode, $wgContLang, $wgLang, $wgDefaultLanguageVariant, $wgAllowUserJs;
420 // Setup environnement for this test
421 $wgLanguageCode = $contLang;
422 $wgContLang = Language
::factory( $contLang );
423 $wgLang = Language
::factory( $lang );
424 $wgDefaultLanguageVariant = $variant;
425 $wgAllowUserJs = true;
427 $title = Title
::newFromText( $titleText );
428 $this->assertInstanceOf( 'Title', $title,
429 "Test must be passed a valid title text, you gave '$titleText'"
431 $this->assertEquals( $expected,
432 $title->getPageViewLanguage()->getCode(),
437 public static function provideGetPageViewLanguage() {
441 # - wgContLang (expected in most case)
442 # - wgLang (on some specific pages)
443 # - wgDefaultLanguageVariant
446 array( 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ),
447 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ),
448 array( 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ),
450 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ),
451 array( 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ),
452 array( 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ),
453 array( 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ),
454 array( 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ),
455 array( 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ),
456 array( 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ),
457 array( 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
459 array( 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ),
460 array( 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ),
461 array( 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ),
462 array( 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ),
463 array( 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ),
464 array( 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ),
465 array( 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ),
466 array( 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ),
467 array( 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ),
468 array( 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
470 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
471 array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),
477 * @dataProvider provideBaseTitleCases
478 * @covers Title::getBaseText
480 public function testGetBaseText( $title, $expected, $msg = '' ) {
481 $title = Title
::newFromText( $title );
482 $this->assertEquals( $expected,
483 $title->getBaseText(),
488 public static function provideBaseTitleCases() {
490 # Title, expected base, optional message
491 array( 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
492 array( 'User:Foo/Bar/Baz', 'Foo/Bar' ),
497 * @dataProvider provideRootTitleCases
498 * @covers Title::getRootText
500 public function testGetRootText( $title, $expected, $msg = '' ) {
501 $title = Title
::newFromText( $title );
502 $this->assertEquals( $expected,
503 $title->getRootText(),
508 public static function provideRootTitleCases() {
510 # Title, expected base, optional message
511 array( 'User:John_Doe/subOne/subTwo', 'John Doe' ),
512 array( 'User:Foo/Bar/Baz', 'Foo' ),
517 * @todo Handle $wgNamespacesWithSubpages cases
518 * @dataProvider provideSubpageTitleCases
519 * @covers Title::getSubpageText
521 public function testGetSubpageText( $title, $expected, $msg = '' ) {
522 $title = Title
::newFromText( $title );
523 $this->assertEquals( $expected,
524 $title->getSubpageText(),
529 public static function provideSubpageTitleCases() {
531 # Title, expected base, optional message
532 array( 'User:John_Doe/subOne/subTwo', 'subTwo' ),
533 array( 'User:John_Doe/subOne', 'subOne' ),
537 public function provideNewFromTitleValue() {
539 array( new TitleValue( NS_MAIN
, 'Foo' ) ),
540 array( new TitleValue( NS_MAIN
, 'Foo', 'bar' ) ),
541 array( new TitleValue( NS_USER
, 'Hansi_Maier' ) ),
546 * @dataProvider provideNewFromTitleValue
548 public function testNewFromTitleValue( TitleValue
$value ) {
549 $title = Title
::newFromTitleValue( $value );
551 $dbkey = str_replace( ' ', '_', $value->getText() );
552 $this->assertEquals( $dbkey, $title->getDBkey() );
553 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
554 $this->assertEquals( $value->getFragment(), $title->getFragment() );
557 public function provideGetTitleValue() {
561 array( 'User:Hansi_Maier' ),
566 * @dataProvider provideGetTitleValue
568 public function testGetTitleValue( $text ) {
569 $title = Title
::newFromText( $text );
570 $value = $title->getTitleValue();
572 $dbkey = str_replace( ' ', '_', $value->getText() );
573 $this->assertEquals( $title->getDBkey(), $dbkey );
574 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
575 $this->assertEquals( $title->getFragment(), $value->getFragment() );
578 public function provideGetFragment() {
581 array( 'Foo#bar', 'bar' ),
582 array( 'Foo#bär', 'bär' ),
584 // Inner whitespace is normalized
585 array( 'Foo#bar_bar', 'bar bar' ),
586 array( 'Foo#bar bar', 'bar bar' ),
587 array( 'Foo#bar bar', 'bar bar' ),
589 // Leading whitespace is kept, trailing whitespace is trimmed.
590 // XXX: Is this really want we want?
591 array( 'Foo#_bar_bar_', ' bar bar' ),
592 array( 'Foo# bar bar ', ' bar bar' ),
597 * @dataProvider provideGetFragment
599 * @param string $full
600 * @param string $fragment
602 public function testGetFragment( $full, $fragment ) {
603 $title = Title
::newFromText( $full );
604 $this->assertEquals( $fragment, $title->getFragment() );
608 * @covers Title::isAlwaysKnown
609 * @dataProvider provideIsAlwaysKnown
610 * @param string $page
611 * @param bool $isKnown
613 public function testIsAlwaysKnown( $page, $isKnown ) {
614 $title = Title
::newFromText( $page );
615 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
618 public function provideIsAlwaysKnown() {
620 array( 'Some nonexistent page', false ),
621 array( 'UTPage', false ),
622 array( '#test', true ),
623 array( 'Special:BlankPage', true ),
624 array( 'Special:SomeNonexistentSpecialPage', false ),
625 array( 'MediaWiki:Parentheses', true ),
626 array( 'MediaWiki:Some nonexistent message', false ),
631 * @covers Title::isAlwaysKnown
633 public function testIsAlwaysKnownOnInterwiki() {
634 $title = Title
::makeTitle( NS_MAIN
, 'Interwiki link', '', 'externalwiki' );
635 $this->assertTrue( $title->isAlwaysKnown() );