5 * ^--- needed for language cache stuff
7 class TitleTest
extends MediaWikiTestCase
{
8 protected function setUp() {
11 $this->setMwGlobals( array(
12 'wgLanguageCode' => 'en',
13 'wgContLang' => Language
::factory( 'en' ),
15 'wgLang' => Language
::factory( 'en' ),
16 'wgAllowUserJs' => false,
17 'wgDefaultLanguageVariant' => false,
22 * @covers Title::legalChars
24 public function testLegalChars() {
25 $titlechars = Title
::legalChars();
27 foreach ( range( 1, 255 ) as $num ) {
29 if ( strpos( "#[]{}<>|", $chr ) !== false ||
preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
30 $this->assertFalse( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is not a valid titlechar" );
32 $this->assertTrue( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is a valid titlechar" );
38 * See also mediawiki.Title.test.js
39 * @covers Title::secureAndSplit
40 * @todo This method should be split into 2 separate tests each with a provider
42 public function testSecureAndSplit() {
43 $this->setMwGlobals( array(
44 'wgLocalInterwikis' => array( 'localtestiw' ),
46 'InterwikiLoadPrefix' => array(
47 function ( $prefix, &$data ) {
48 if ( $prefix === 'localtestiw' ) {
49 $data = array( 'iw_url' => 'localtestiw' );
50 } elseif ( $prefix === 'remotetestiw' ) {
51 $data = array( 'iw_url' => 'remotetestiw' );
70 'File_talk:Example.svg',
74 // Length is 256 total, but only title part matters
75 'Category:' . str_repeat( 'x', 248 ),
76 str_repeat( 'x', 252 ),
78 'localtestiw: #anchor',
80 'localtestiw: foo # anchor',
81 'localtestiw: Talk: Sandbox # anchor',
83 'remotetestiw: Talk: # anchor',
85 'remotetestiw: Talk:',
86 'remotetestiw: Talk: Foo'
88 $this->assertInstanceOf( 'Title', Title
::newFromText( $text ), "Valid: $text" );
97 // Bad characters forbidden regardless of wgLegalTitleChars
109 // XML/HTML character entity references
110 // Note: Commented out because they are not marked invalid by the PHP test as
111 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
115 // Subject of NS_TALK does not roundtrip to NS_MAIN
116 'Talk:File:Example.svg',
117 // Directory navigation
130 str_repeat( 'x', 256 ),
131 // Namespace prefix without actual title
137 'localtestiw: Talk: # anchor',
140 $this->assertNull( Title
::newFromText( $text ), "Invalid: $text" );
144 public static function provideConvertByteClassToUnicodeClass() {
147 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
148 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
152 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
155 'QWERTY\\x66-\\xFD+',
156 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
164 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
167 'QWERTY\\x66-\\x80+\\x23',
168 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
171 'QWERTY\\x66-\\x80+\\xD3',
172 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
176 '\\\\\\u0080-\\uFFFF',
180 '\\-\\u0080-\\uFFFF',
184 'QWERTY\\-\\u0080-\\uFFFF',
192 'A-\\x7F\\u0080-\\uFFFF',
195 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
196 'f-wQWERTYFXZ\\u0080-\\uFFFF',
199 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
200 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
206 * @dataProvider provideConvertByteClassToUnicodeClass
207 * @covers Title::convertByteClassToUnicodeClass
209 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
210 $this->assertEquals( $unicodeClass, Title
::convertByteClassToUnicodeClass( $byteClass ) );
214 * @dataProvider provideBug31100
215 * @covers Title::fixSpecialName
216 * @todo give this test a real name explaining what is being tested here
218 public function testBug31100FixSpecialName( $text, $expectedParam ) {
219 $title = Title
::newFromText( $text );
220 $fixed = $title->fixSpecialName();
221 $stuff = explode( '/', $fixed->getDBkey(), 2 );
222 if ( count( $stuff ) == 2 ) {
227 $this->assertEquals( $expectedParam, $par, "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter" );
230 public static function provideBug31100() {
232 array( 'Special:Version', null ),
233 array( 'Special:Version/', '' ),
234 array( 'Special:Version/param', 'param' ),
239 * Auth-less test of Title::isValidMoveOperation
242 * @param string $source
243 * @param string $target
244 * @param array|string|bool $expected Required error
245 * @dataProvider provideTestIsValidMoveOperation
246 * @covers Title::isValidMoveOperation
248 public function testIsValidMoveOperation( $source, $target, $expected ) {
249 $title = Title
::newFromText( $source );
250 $nt = Title
::newFromText( $target );
251 $errors = $title->isValidMoveOperation( $nt, false );
252 if ( $expected === true ) {
253 $this->assertTrue( $errors );
255 $errors = $this->flattenErrorsArray( $errors );
256 foreach ( (array)$expected as $error ) {
257 $this->assertContains( $error, $errors );
263 * Provides test parameter values for testIsValidMoveOperation()
265 public function dataTestIsValidMoveOperation() {
267 array( 'Test', 'Test', 'selfmove' ),
268 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
273 * Auth-less test of Title::userCan
275 * @param array $whitelistRegexp
276 * @param string $source
277 * @param string $action
278 * @param array|string|bool $expected Required error
280 * @covers Title::checkReadPermissions
281 * @dataProvider dataWgWhitelistReadRegexp
283 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
284 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
285 // usually have only one regex, it is more concise to write the lonely regex
286 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
288 if ( is_string( $whitelistRegexp ) ) {
289 $whitelistRegexp = array( $whitelistRegexp );
292 $title = Title
::newFromDBkey( $source );
294 global $wgGroupPermissions;
295 $oldPermissions = $wgGroupPermissions;
296 // Disallow all so we can ensure our regex works
297 $wgGroupPermissions = array();
298 $wgGroupPermissions['*']['read'] = false;
300 global $wgWhitelistRead;
301 $oldWhitelist = $wgWhitelistRead;
302 // Undo any LocalSettings explicite whitelists so they won't cause a
303 // failing test to succeed. Set it to some random non sense just
304 // to make sure we properly test Title::checkReadPermissions()
305 $wgWhitelistRead = array( 'some random non sense title' );
307 global $wgWhitelistReadRegexp;
308 $oldWhitelistRegexp = $wgWhitelistReadRegexp;
309 $wgWhitelistReadRegexp = $whitelistRegexp;
311 // Just use $wgUser which in test is a user object for '127.0.0.1'
313 // Invalidate user rights cache to take in account $wgGroupPermissions
315 $wgUser->clearInstanceCache();
316 $errors = $title->userCan( $action, $wgUser );
319 $wgGroupPermissions = $oldPermissions;
320 $wgWhitelistRead = $oldWhitelist;
321 $wgWhitelistReadRegexp = $oldWhitelistRegexp;
323 if ( is_bool( $expected ) ) {
324 # Forge the assertion message depending on the assertion expectation
325 $allowableness = $expected
326 ?
" should be allowed"
327 : " should NOT be allowed";
328 $this->assertEquals( $expected, $errors, "User action '$action' on [[$source]] $allowableness." );
330 $errors = $this->flattenErrorsArray( $errors );
331 foreach ( (array)$expected as $error ) {
332 $this->assertContains( $error, $errors );
338 * Provides test parameter values for testWgWhitelistReadRegexp()
340 public function dataWgWhitelistReadRegexp() {
345 // Everything, if this doesn't work, we're really in trouble
346 array( '/.*/', 'Main_Page', 'read', $ALLOWED ),
347 array( '/.*/', 'Main_Page', 'edit', $DISALLOWED ),
349 // We validate against the title name, not the db key
350 array( '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ),
352 array( '/^Main/', 'Main_Page', 'read', $ALLOWED ),
353 array( '/^Main.*/', 'Main_Page', 'read', $ALLOWED ),
355 array( '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ),
357 // ...without unicode modifier
358 array( '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ),
359 // ...with unicode modifier
360 array( '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ),
362 array( '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ),
363 array( '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ),
365 // From DefaultSettings.php:
366 array( "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ),
367 array( "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ),
370 array( '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ),
371 array( null, 'Special:Newpages', 'read', $DISALLOWED ),
376 public function flattenErrorsArray( $errors ) {
378 foreach ( $errors as $error ) {
379 $result[] = $error[0];
385 public static function provideTestIsValidMoveOperation() {
387 array( 'Test', 'Test', 'selfmove' ),
388 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
393 * @dataProvider provideGetPageViewLanguage
394 * @covers Title::getPageViewLanguage
396 public function testGetPageViewLanguage( $expected, $titleText, $contLang, $lang, $variant, $msg = '' ) {
397 global $wgLanguageCode, $wgContLang, $wgLang, $wgDefaultLanguageVariant, $wgAllowUserJs;
399 // Setup environnement for this test
400 $wgLanguageCode = $contLang;
401 $wgContLang = Language
::factory( $contLang );
402 $wgLang = Language
::factory( $lang );
403 $wgDefaultLanguageVariant = $variant;
404 $wgAllowUserJs = true;
406 $title = Title
::newFromText( $titleText );
407 $this->assertInstanceOf( 'Title', $title,
408 "Test must be passed a valid title text, you gave '$titleText'"
410 $this->assertEquals( $expected,
411 $title->getPageViewLanguage()->getCode(),
416 public static function provideGetPageViewLanguage() {
420 # - wgContLang (expected in most case)
421 # - wgLang (on some specific pages)
422 # - wgDefaultLanguageVariant
425 array( 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ),
426 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ),
427 array( 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ),
429 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ),
430 array( 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ),
431 array( 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ),
432 array( 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ),
433 array( 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ),
434 array( 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ),
435 array( 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ),
436 array( 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
438 array( 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ),
439 array( 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ),
440 array( 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ),
441 array( 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ),
442 array( 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ),
443 array( 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ),
444 array( 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ),
445 array( 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ),
446 array( 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ),
447 array( 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
449 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
450 array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),
456 * @dataProvider provideBaseTitleCases
457 * @covers Title::getBaseText
459 public function testGetBaseText( $title, $expected, $msg = '' ) {
460 $title = Title
::newFromText( $title );
461 $this->assertEquals( $expected,
462 $title->getBaseText(),
467 public static function provideBaseTitleCases() {
469 # Title, expected base, optional message
470 array( 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
471 array( 'User:Foo/Bar/Baz', 'Foo/Bar' ),
476 * @dataProvider provideRootTitleCases
477 * @covers Title::getRootText
479 public function testGetRootText( $title, $expected, $msg = '' ) {
480 $title = Title
::newFromText( $title );
481 $this->assertEquals( $expected,
482 $title->getRootText(),
487 public static function provideRootTitleCases() {
489 # Title, expected base, optional message
490 array( 'User:John_Doe/subOne/subTwo', 'John Doe' ),
491 array( 'User:Foo/Bar/Baz', 'Foo' ),
496 * @todo Handle $wgNamespacesWithSubpages cases
497 * @dataProvider provideSubpageTitleCases
498 * @covers Title::getSubpageText
500 public function testGetSubpageText( $title, $expected, $msg = '' ) {
501 $title = Title
::newFromText( $title );
502 $this->assertEquals( $expected,
503 $title->getSubpageText(),
508 public static function provideSubpageTitleCases() {
510 # Title, expected base, optional message
511 array( 'User:John_Doe/subOne/subTwo', 'subTwo' ),
512 array( 'User:John_Doe/subOne', 'subOne' ),