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'
92 $this->assertInstanceOf( 'Title', Title
::newFromText( $text ), "Valid: $text" );
101 // Bad characters forbidden regardless of wgLegalTitleChars
113 // XML/HTML character entity references
114 // Note: Commented out because they are not marked invalid by the PHP test as
115 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
119 // Subject of NS_TALK does not roundtrip to NS_MAIN
120 'Talk:File:Example.svg',
121 // Directory navigation
134 str_repeat( 'x', 256 ),
135 // Namespace prefix without actual title
142 'localtestiw: Talk: # anchor',
145 $this->assertNull( Title
::newFromText( $text ), "Invalid: $text" );
149 public static function provideConvertByteClassToUnicodeClass() {
152 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
153 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
157 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
160 'QWERTY\\x66-\\xFD+',
161 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
169 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
172 'QWERTY\\x66-\\x80+\\x23',
173 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
176 'QWERTY\\x66-\\x80+\\xD3',
177 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
181 '\\\\\\u0080-\\uFFFF',
185 '\\-\\u0080-\\uFFFF',
189 'QWERTY\\-\\u0080-\\uFFFF',
197 'A-\\x7F\\u0080-\\uFFFF',
200 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
201 'f-wQWERTYFXZ\\u0080-\\uFFFF',
204 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
205 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
211 * @dataProvider provideConvertByteClassToUnicodeClass
212 * @covers Title::convertByteClassToUnicodeClass
214 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
215 $this->assertEquals( $unicodeClass, Title
::convertByteClassToUnicodeClass( $byteClass ) );
219 * @dataProvider provideBug31100
220 * @covers Title::fixSpecialName
221 * @todo give this test a real name explaining what is being tested here
223 public function testBug31100FixSpecialName( $text, $expectedParam ) {
224 $title = Title
::newFromText( $text );
225 $fixed = $title->fixSpecialName();
226 $stuff = explode( '/', $fixed->getDBkey(), 2 );
227 if ( count( $stuff ) == 2 ) {
232 $this->assertEquals( $expectedParam, $par, "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter" );
235 public static function provideBug31100() {
237 array( 'Special:Version', null ),
238 array( 'Special:Version/', '' ),
239 array( 'Special:Version/param', 'param' ),
244 * Auth-less test of Title::isValidMoveOperation
247 * @param string $source
248 * @param string $target
249 * @param array|string|bool $expected Required error
250 * @dataProvider provideTestIsValidMoveOperation
251 * @covers Title::isValidMoveOperation
253 public function testIsValidMoveOperation( $source, $target, $expected ) {
254 $title = Title
::newFromText( $source );
255 $nt = Title
::newFromText( $target );
256 $errors = $title->isValidMoveOperation( $nt, false );
257 if ( $expected === true ) {
258 $this->assertTrue( $errors );
260 $errors = $this->flattenErrorsArray( $errors );
261 foreach ( (array)$expected as $error ) {
262 $this->assertContains( $error, $errors );
268 * Provides test parameter values for testIsValidMoveOperation()
270 public function dataTestIsValidMoveOperation() {
272 array( 'Test', 'Test', 'selfmove' ),
273 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
278 * Auth-less test of Title::userCan
280 * @param array $whitelistRegexp
281 * @param string $source
282 * @param string $action
283 * @param array|string|bool $expected Required error
285 * @covers Title::checkReadPermissions
286 * @dataProvider dataWgWhitelistReadRegexp
288 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
289 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
290 // usually have only one regex, it is more concise to write the lonely regex
291 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
293 if ( is_string( $whitelistRegexp ) ) {
294 $whitelistRegexp = array( $whitelistRegexp );
297 $title = Title
::newFromDBkey( $source );
299 global $wgGroupPermissions;
300 $oldPermissions = $wgGroupPermissions;
301 // Disallow all so we can ensure our regex works
302 $wgGroupPermissions = array();
303 $wgGroupPermissions['*']['read'] = false;
305 global $wgWhitelistRead;
306 $oldWhitelist = $wgWhitelistRead;
307 // Undo any LocalSettings explicite whitelists so they won't cause a
308 // failing test to succeed. Set it to some random non sense just
309 // to make sure we properly test Title::checkReadPermissions()
310 $wgWhitelistRead = array( 'some random non sense title' );
312 global $wgWhitelistReadRegexp;
313 $oldWhitelistRegexp = $wgWhitelistReadRegexp;
314 $wgWhitelistReadRegexp = $whitelistRegexp;
316 // Just use $wgUser which in test is a user object for '127.0.0.1'
318 // Invalidate user rights cache to take in account $wgGroupPermissions
320 $wgUser->clearInstanceCache();
321 $errors = $title->userCan( $action, $wgUser );
324 $wgGroupPermissions = $oldPermissions;
325 $wgWhitelistRead = $oldWhitelist;
326 $wgWhitelistReadRegexp = $oldWhitelistRegexp;
328 if ( is_bool( $expected ) ) {
329 # Forge the assertion message depending on the assertion expectation
330 $allowableness = $expected
331 ?
" should be allowed"
332 : " should NOT be allowed";
333 $this->assertEquals( $expected, $errors, "User action '$action' on [[$source]] $allowableness." );
335 $errors = $this->flattenErrorsArray( $errors );
336 foreach ( (array)$expected as $error ) {
337 $this->assertContains( $error, $errors );
343 * Provides test parameter values for testWgWhitelistReadRegexp()
345 public function dataWgWhitelistReadRegexp() {
350 // Everything, if this doesn't work, we're really in trouble
351 array( '/.*/', 'Main_Page', 'read', $ALLOWED ),
352 array( '/.*/', 'Main_Page', 'edit', $DISALLOWED ),
354 // We validate against the title name, not the db key
355 array( '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ),
357 array( '/^Main/', 'Main_Page', 'read', $ALLOWED ),
358 array( '/^Main.*/', 'Main_Page', 'read', $ALLOWED ),
360 array( '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ),
362 // ...without unicode modifier
363 array( '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ),
364 // ...with unicode modifier
365 array( '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ),
367 array( '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ),
368 array( '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ),
370 // From DefaultSettings.php:
371 array( "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ),
372 array( "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ),
375 array( '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ),
376 array( null, 'Special:Newpages', 'read', $DISALLOWED ),
381 public function flattenErrorsArray( $errors ) {
383 foreach ( $errors as $error ) {
384 $result[] = $error[0];
390 public static function provideTestIsValidMoveOperation() {
392 array( 'Test', 'Test', 'selfmove' ),
393 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
398 * @dataProvider provideGetPageViewLanguage
399 * @covers Title::getPageViewLanguage
401 public function testGetPageViewLanguage( $expected, $titleText, $contLang, $lang, $variant, $msg = '' ) {
402 global $wgLanguageCode, $wgContLang, $wgLang, $wgDefaultLanguageVariant, $wgAllowUserJs;
404 // Setup environnement for this test
405 $wgLanguageCode = $contLang;
406 $wgContLang = Language
::factory( $contLang );
407 $wgLang = Language
::factory( $lang );
408 $wgDefaultLanguageVariant = $variant;
409 $wgAllowUserJs = true;
411 $title = Title
::newFromText( $titleText );
412 $this->assertInstanceOf( 'Title', $title,
413 "Test must be passed a valid title text, you gave '$titleText'"
415 $this->assertEquals( $expected,
416 $title->getPageViewLanguage()->getCode(),
421 public static function provideGetPageViewLanguage() {
425 # - wgContLang (expected in most case)
426 # - wgLang (on some specific pages)
427 # - wgDefaultLanguageVariant
430 array( 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ),
431 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ),
432 array( 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ),
434 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ),
435 array( 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ),
436 array( 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ),
437 array( 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ),
438 array( 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ),
439 array( 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ),
440 array( 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ),
441 array( 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
443 array( 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ),
444 array( 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ),
445 array( 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ),
446 array( 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ),
447 array( 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ),
448 array( 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ),
449 array( 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ),
450 array( 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ),
451 array( 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ),
452 array( 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
454 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
455 array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),
461 * @dataProvider provideBaseTitleCases
462 * @covers Title::getBaseText
464 public function testGetBaseText( $title, $expected, $msg = '' ) {
465 $title = Title
::newFromText( $title );
466 $this->assertEquals( $expected,
467 $title->getBaseText(),
472 public static function provideBaseTitleCases() {
474 # Title, expected base, optional message
475 array( 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
476 array( 'User:Foo/Bar/Baz', 'Foo/Bar' ),
481 * @dataProvider provideRootTitleCases
482 * @covers Title::getRootText
484 public function testGetRootText( $title, $expected, $msg = '' ) {
485 $title = Title
::newFromText( $title );
486 $this->assertEquals( $expected,
487 $title->getRootText(),
492 public static function provideRootTitleCases() {
494 # Title, expected base, optional message
495 array( 'User:John_Doe/subOne/subTwo', 'John Doe' ),
496 array( 'User:Foo/Bar/Baz', 'Foo' ),
501 * @todo Handle $wgNamespacesWithSubpages cases
502 * @dataProvider provideSubpageTitleCases
503 * @covers Title::getSubpageText
505 public function testGetSubpageText( $title, $expected, $msg = '' ) {
506 $title = Title
::newFromText( $title );
507 $this->assertEquals( $expected,
508 $title->getSubpageText(),
513 public static function provideSubpageTitleCases() {
515 # Title, expected base, optional message
516 array( 'User:John_Doe/subOne/subTwo', 'subTwo' ),
517 array( 'User:John_Doe/subOne', 'subOne' ),