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 ) ) {
32 $this->assertFalse( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is not a valid titlechar" );
34 $this->assertTrue( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is a valid titlechar" );
40 * See also mediawiki.Title.test.js
41 * @covers Title::secureAndSplit
42 * @todo This method should be split into 2 separate tests each with a provider
43 * @note: This mainly tests MediaWikiTitleCodec::parseTitle().
45 public function testSecureAndSplit() {
46 $this->setMwGlobals( array(
47 'wgLocalInterwikis' => array( 'localtestiw' ),
49 'InterwikiLoadPrefix' => array(
50 function ( $prefix, &$data ) {
51 if ( $prefix === 'localtestiw' ) {
52 $data = array( 'iw_url' => 'localtestiw' );
53 } elseif ( $prefix === 'remotetestiw' ) {
54 $data = array( 'iw_url' => 'remotetestiw' );
74 'File_talk:Example.svg',
78 // Length is 256 total, but only title part matters
79 'Category:' . str_repeat( 'x', 248 ),
80 str_repeat( 'x', 252 ),
82 'localtestiw: #anchor',
84 'localtestiw: foo # anchor',
85 'localtestiw: Talk: Sandbox # anchor',
87 'remotetestiw: Talk: # anchor',
89 'remotetestiw: Talk:',
90 'remotetestiw: Talk: Foo',
91 'localtestiw:remotetestiw:',
92 'localtestiw:remotetestiw:foo'
94 $this->assertInstanceOf( 'Title', Title
::newFromText( $text ), "Valid: $text" );
103 // Bad characters forbidden regardless of wgLegalTitleChars
115 // XML/HTML character entity references
116 // Note: Commented out because they are not marked invalid by the PHP test as
117 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
121 // Subject of NS_TALK does not roundtrip to NS_MAIN
122 'Talk:File:Example.svg',
123 // Directory navigation
136 str_repeat( 'x', 256 ),
137 // Namespace prefix without actual title
144 'localtestiw: Talk: # anchor',
147 $this->assertNull( Title
::newFromText( $text ), "Invalid: $text" );
151 public static function provideConvertByteClassToUnicodeClass() {
154 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
155 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
159 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
162 'QWERTY\\x66-\\xFD+',
163 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
171 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
174 'QWERTY\\x66-\\x80+\\x23',
175 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
178 'QWERTY\\x66-\\x80+\\xD3',
179 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
183 '\\\\\\u0080-\\uFFFF',
187 '\\-\\u0080-\\uFFFF',
191 'QWERTY\\-\\u0080-\\uFFFF',
199 'A-\\x7F\\u0080-\\uFFFF',
202 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
203 'f-wQWERTYFXZ\\u0080-\\uFFFF',
206 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
207 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
213 * @dataProvider provideConvertByteClassToUnicodeClass
214 * @covers Title::convertByteClassToUnicodeClass
216 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
217 $this->assertEquals( $unicodeClass, Title
::convertByteClassToUnicodeClass( $byteClass ) );
221 * @dataProvider provideBug31100
222 * @covers Title::fixSpecialName
223 * @todo give this test a real name explaining what is being tested here
225 public function testBug31100FixSpecialName( $text, $expectedParam ) {
226 $title = Title
::newFromText( $text );
227 $fixed = $title->fixSpecialName();
228 $stuff = explode( '/', $fixed->getDBkey(), 2 );
229 if ( count( $stuff ) == 2 ) {
234 $this->assertEquals( $expectedParam, $par, "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter" );
237 public static function provideBug31100() {
239 array( 'Special:Version', null ),
240 array( 'Special:Version/', '' ),
241 array( 'Special:Version/param', 'param' ),
246 * Auth-less test of Title::isValidMoveOperation
249 * @param string $source
250 * @param string $target
251 * @param array|string|bool $expected Required error
252 * @dataProvider provideTestIsValidMoveOperation
253 * @covers Title::isValidMoveOperation
255 public function testIsValidMoveOperation( $source, $target, $expected ) {
256 $title = Title
::newFromText( $source );
257 $nt = Title
::newFromText( $target );
258 $errors = $title->isValidMoveOperation( $nt, false );
259 if ( $expected === true ) {
260 $this->assertTrue( $errors );
262 $errors = $this->flattenErrorsArray( $errors );
263 foreach ( (array)$expected as $error ) {
264 $this->assertContains( $error, $errors );
270 * Provides test parameter values for testIsValidMoveOperation()
272 public function dataTestIsValidMoveOperation() {
274 array( 'Test', 'Test', 'selfmove' ),
275 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
280 * Auth-less test of Title::userCan
282 * @param array $whitelistRegexp
283 * @param string $source
284 * @param string $action
285 * @param array|string|bool $expected Required error
287 * @covers Title::checkReadPermissions
288 * @dataProvider dataWgWhitelistReadRegexp
290 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
291 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
292 // usually have only one regex, it is more concise to write the lonely regex
293 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
295 if ( is_string( $whitelistRegexp ) ) {
296 $whitelistRegexp = array( $whitelistRegexp );
299 $title = Title
::newFromDBkey( $source );
301 global $wgGroupPermissions;
302 $oldPermissions = $wgGroupPermissions;
303 // Disallow all so we can ensure our regex works
304 $wgGroupPermissions = array();
305 $wgGroupPermissions['*']['read'] = false;
307 global $wgWhitelistRead;
308 $oldWhitelist = $wgWhitelistRead;
309 // Undo any LocalSettings explicite whitelists so they won't cause a
310 // failing test to succeed. Set it to some random non sense just
311 // to make sure we properly test Title::checkReadPermissions()
312 $wgWhitelistRead = array( 'some random non sense title' );
314 global $wgWhitelistReadRegexp;
315 $oldWhitelistRegexp = $wgWhitelistReadRegexp;
316 $wgWhitelistReadRegexp = $whitelistRegexp;
318 // Just use $wgUser which in test is a user object for '127.0.0.1'
320 // Invalidate user rights cache to take in account $wgGroupPermissions
322 $wgUser->clearInstanceCache();
323 $errors = $title->userCan( $action, $wgUser );
326 $wgGroupPermissions = $oldPermissions;
327 $wgWhitelistRead = $oldWhitelist;
328 $wgWhitelistReadRegexp = $oldWhitelistRegexp;
330 if ( is_bool( $expected ) ) {
331 # Forge the assertion message depending on the assertion expectation
332 $allowableness = $expected
333 ?
" should be allowed"
334 : " should NOT be allowed";
335 $this->assertEquals( $expected, $errors, "User action '$action' on [[$source]] $allowableness." );
337 $errors = $this->flattenErrorsArray( $errors );
338 foreach ( (array)$expected as $error ) {
339 $this->assertContains( $error, $errors );
345 * Provides test parameter values for testWgWhitelistReadRegexp()
347 public function dataWgWhitelistReadRegexp() {
352 // Everything, if this doesn't work, we're really in trouble
353 array( '/.*/', 'Main_Page', 'read', $ALLOWED ),
354 array( '/.*/', 'Main_Page', 'edit', $DISALLOWED ),
356 // We validate against the title name, not the db key
357 array( '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ),
359 array( '/^Main/', 'Main_Page', 'read', $ALLOWED ),
360 array( '/^Main.*/', 'Main_Page', 'read', $ALLOWED ),
362 array( '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ),
364 // ...without unicode modifier
365 array( '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ),
366 // ...with unicode modifier
367 array( '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ),
369 array( '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ),
370 array( '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ),
372 // From DefaultSettings.php:
373 array( "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ),
374 array( "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ),
377 array( '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ),
378 array( null, 'Special:Newpages', 'read', $DISALLOWED ),
383 public function flattenErrorsArray( $errors ) {
385 foreach ( $errors as $error ) {
386 $result[] = $error[0];
392 public static function provideTestIsValidMoveOperation() {
394 array( 'Test', 'Test', 'selfmove' ),
395 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
400 * @dataProvider provideGetPageViewLanguage
401 * @covers Title::getPageViewLanguage
403 public function testGetPageViewLanguage( $expected, $titleText, $contLang, $lang, $variant, $msg = '' ) {
404 global $wgLanguageCode, $wgContLang, $wgLang, $wgDefaultLanguageVariant, $wgAllowUserJs;
406 // Setup environnement for this test
407 $wgLanguageCode = $contLang;
408 $wgContLang = Language
::factory( $contLang );
409 $wgLang = Language
::factory( $lang );
410 $wgDefaultLanguageVariant = $variant;
411 $wgAllowUserJs = true;
413 $title = Title
::newFromText( $titleText );
414 $this->assertInstanceOf( 'Title', $title,
415 "Test must be passed a valid title text, you gave '$titleText'"
417 $this->assertEquals( $expected,
418 $title->getPageViewLanguage()->getCode(),
423 public static function provideGetPageViewLanguage() {
427 # - wgContLang (expected in most case)
428 # - wgLang (on some specific pages)
429 # - wgDefaultLanguageVariant
432 array( 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ),
433 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ),
434 array( 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ),
436 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ),
437 array( 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ),
438 array( 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ),
439 array( 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ),
440 array( 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ),
441 array( 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ),
442 array( 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ),
443 array( 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
445 array( 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ),
446 array( 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ),
447 array( 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ),
448 array( 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ),
449 array( 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ),
450 array( 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ),
451 array( 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ),
452 array( 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ),
453 array( 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ),
454 array( 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
456 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
457 array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),
463 * @dataProvider provideBaseTitleCases
464 * @covers Title::getBaseText
466 public function testGetBaseText( $title, $expected, $msg = '' ) {
467 $title = Title
::newFromText( $title );
468 $this->assertEquals( $expected,
469 $title->getBaseText(),
474 public static function provideBaseTitleCases() {
476 # Title, expected base, optional message
477 array( 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
478 array( 'User:Foo/Bar/Baz', 'Foo/Bar' ),
483 * @dataProvider provideRootTitleCases
484 * @covers Title::getRootText
486 public function testGetRootText( $title, $expected, $msg = '' ) {
487 $title = Title
::newFromText( $title );
488 $this->assertEquals( $expected,
489 $title->getRootText(),
494 public static function provideRootTitleCases() {
496 # Title, expected base, optional message
497 array( 'User:John_Doe/subOne/subTwo', 'John Doe' ),
498 array( 'User:Foo/Bar/Baz', 'Foo' ),
503 * @todo Handle $wgNamespacesWithSubpages cases
504 * @dataProvider provideSubpageTitleCases
505 * @covers Title::getSubpageText
507 public function testGetSubpageText( $title, $expected, $msg = '' ) {
508 $title = Title
::newFromText( $title );
509 $this->assertEquals( $expected,
510 $title->getSubpageText(),
515 public static function provideSubpageTitleCases() {
517 # Title, expected base, optional message
518 array( 'User:John_Doe/subOne/subTwo', 'subTwo' ),
519 array( 'User:John_Doe/subOne', 'subOne' ),
523 public function provideNewFromTitleValue() {
525 array( new TitleValue( NS_MAIN
, 'Foo' ) ),
526 array( new TitleValue( NS_MAIN
, 'Foo', 'bar' ) ),
527 array( new TitleValue( NS_USER
, 'Hansi_Maier' ) ),
532 * @dataProvider provideNewFromTitleValue
534 public function testNewFromTitleValue( TitleValue
$value ) {
535 $title = Title
::newFromTitleValue( $value );
537 $dbkey = str_replace( ' ', '_', $value->getText() );
538 $this->assertEquals( $dbkey, $title->getDBkey() );
539 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
540 $this->assertEquals( $value->getFragment(), $title->getFragment() );
543 public function provideGetTitleValue() {
547 array( 'User:Hansi_Maier' ),
552 * @dataProvider provideGetTitleValue
554 public function testGetTitleValue( $text ) {
555 $title = Title
::newFromText( $text );
556 $value = $title->getTitleValue();
558 $dbkey = str_replace( ' ', '_', $value->getText() );
559 $this->assertEquals( $title->getDBkey(), $dbkey );
560 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
561 $this->assertEquals( $title->getFragment(), $value->getFragment() );
564 public function provideGetFragment() {
567 array( 'Foo#bar', 'bar' ),
568 array( 'Foo#bär', 'bär' ),
570 // Inner whitespace is normalized
571 array( 'Foo#bar_bar', 'bar bar' ),
572 array( 'Foo#bar bar', 'bar bar' ),
573 array( 'Foo#bar bar', 'bar bar' ),
575 // Leading whitespace is kept, trailing whitespace is trimmed.
576 // XXX: Is this really want we want?
577 array( 'Foo#_bar_bar_', ' bar bar' ),
578 array( 'Foo# bar bar ', ' bar bar' ),
583 * @dataProvider provideGetFragment
585 * @param string $full
586 * @param string $fragment
588 public function testGetFragment( $full, $fragment ) {
589 $title = Title
::newFromText( $full );
590 $this->assertEquals( $fragment, $title->getFragment() );