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 'wgLocalInterwiki' => '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
217 public function testBug31100FixSpecialName( $text, $expectedParam ) {
218 $title = Title
::newFromText( $text );
219 $fixed = $title->fixSpecialName();
220 $stuff = explode( '/', $fixed->getDBkey(), 2 );
221 if ( count( $stuff ) == 2 ) {
226 $this->assertEquals( $expectedParam, $par, "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter" );
229 public static function provideBug31100() {
231 array( 'Special:Version', null ),
232 array( 'Special:Version/', '' ),
233 array( 'Special:Version/param', 'param' ),
238 * Auth-less test of Title::isValidMoveOperation
241 * @param string $source
242 * @param string $target
243 * @param array|string|bool $expected Required error
244 * @dataProvider provideTestIsValidMoveOperation
245 * @covers Title::isValidMoveOperation
247 public function testIsValidMoveOperation( $source, $target, $expected ) {
248 $title = Title
::newFromText( $source );
249 $nt = Title
::newFromText( $target );
250 $errors = $title->isValidMoveOperation( $nt, false );
251 if ( $expected === true ) {
252 $this->assertTrue( $errors );
254 $errors = $this->flattenErrorsArray( $errors );
255 foreach ( (array)$expected as $error ) {
256 $this->assertContains( $error, $errors );
262 * Provides test parameter values for testIsValidMoveOperation()
264 public function dataTestIsValidMoveOperation() {
266 array( 'Test', 'Test', 'selfmove' ),
267 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
272 * Auth-less test of Title::userCan
274 * @param array $whitelistRegexp
275 * @param string $source
276 * @param string $action
277 * @param array|string|bool $expected Required error
279 * @covers Title::checkReadPermissions
280 * @dataProvider dataWgWhitelistReadRegexp
282 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
283 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
284 // usually have only one regex, it is more concise to write the lonely regex
285 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
287 if ( is_string( $whitelistRegexp ) ) {
288 $whitelistRegexp = array( $whitelistRegexp );
291 $title = Title
::newFromDBkey( $source );
293 global $wgGroupPermissions;
294 $oldPermissions = $wgGroupPermissions;
295 // Disallow all so we can ensure our regex works
296 $wgGroupPermissions = array();
297 $wgGroupPermissions['*']['read'] = false;
299 global $wgWhitelistRead;
300 $oldWhitelist = $wgWhitelistRead;
301 // Undo any LocalSettings explicite whitelists so they won't cause a
302 // failing test to succeed. Set it to some random non sense just
303 // to make sure we properly test Title::checkReadPermissions()
304 $wgWhitelistRead = array( 'some random non sense title' );
306 global $wgWhitelistReadRegexp;
307 $oldWhitelistRegexp = $wgWhitelistReadRegexp;
308 $wgWhitelistReadRegexp = $whitelistRegexp;
310 // Just use $wgUser which in test is a user object for '127.0.0.1'
312 // Invalidate user rights cache to take in account $wgGroupPermissions
314 $wgUser->clearInstanceCache();
315 $errors = $title->userCan( $action, $wgUser );
318 $wgGroupPermissions = $oldPermissions;
319 $wgWhitelistRead = $oldWhitelist;
320 $wgWhitelistReadRegexp = $oldWhitelistRegexp;
322 if ( is_bool( $expected ) ) {
323 # Forge the assertion message depending on the assertion expectation
324 $allowableness = $expected
325 ?
" should be allowed"
326 : " should NOT be allowed";
327 $this->assertEquals( $expected, $errors, "User action '$action' on [[$source]] $allowableness." );
329 $errors = $this->flattenErrorsArray( $errors );
330 foreach ( (array)$expected as $error ) {
331 $this->assertContains( $error, $errors );
337 * Provides test parameter values for testWgWhitelistReadRegexp()
339 public function dataWgWhitelistReadRegexp() {
344 // Everything, if this doesn't work, we're really in trouble
345 array( '/.*/', 'Main_Page', 'read', $ALLOWED ),
346 array( '/.*/', 'Main_Page', 'edit', $DISALLOWED ),
348 // We validate against the title name, not the db key
349 array( '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ),
351 array( '/^Main/', 'Main_Page', 'read', $ALLOWED ),
352 array( '/^Main.*/', 'Main_Page', 'read', $ALLOWED ),
354 array( '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ),
356 // ...without unicode modifier
357 array( '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ),
358 // ...with unicode modifier
359 array( '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ),
361 array( '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ),
362 array( '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ),
364 // From DefaultSettings.php:
365 array( "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ),
366 array( "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ),
369 array( '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ),
370 array( null, 'Special:Newpages', 'read', $DISALLOWED ),
375 public function flattenErrorsArray( $errors ) {
377 foreach ( $errors as $error ) {
378 $result[] = $error[0];
384 public static function provideTestIsValidMoveOperation() {
386 array( 'Test', 'Test', 'selfmove' ),
387 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
392 * @dataProvider provideGetPageViewLanguage
393 * @covers Title::getPageViewLanguage
395 public function testGetPageViewLanguage( $expected, $titleText, $contLang, $lang, $variant, $msg = '' ) {
396 global $wgLanguageCode, $wgContLang, $wgLang, $wgDefaultLanguageVariant, $wgAllowUserJs;
398 // Setup environnement for this test
399 $wgLanguageCode = $contLang;
400 $wgContLang = Language
::factory( $contLang );
401 $wgLang = Language
::factory( $lang );
402 $wgDefaultLanguageVariant = $variant;
403 $wgAllowUserJs = true;
405 $title = Title
::newFromText( $titleText );
406 $this->assertInstanceOf( 'Title', $title,
407 "Test must be passed a valid title text, you gave '$titleText'"
409 $this->assertEquals( $expected,
410 $title->getPageViewLanguage()->getCode(),
415 public static function provideGetPageViewLanguage() {
419 # - wgContLang (expected in most case)
420 # - wgLang (on some specific pages)
421 # - wgDefaultLanguageVariant
424 array( 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ),
425 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ),
426 array( 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ),
428 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ),
429 array( 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ),
430 array( 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ),
431 array( 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ),
432 array( 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ),
433 array( 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ),
434 array( 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ),
435 array( 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
437 array( 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ),
438 array( 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ),
439 array( 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ),
440 array( 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ),
441 array( 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ),
442 array( 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ),
443 array( 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ),
444 array( 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ),
445 array( 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ),
446 array( 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
448 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
449 array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),
455 * @dataProvider provideBaseTitleCases
456 * @covers Title::getBaseText
458 public function testGetBaseText( $title, $expected, $msg = '' ) {
459 $title = Title
::newFromText( $title );
460 $this->assertEquals( $expected,
461 $title->getBaseText(),
466 public static function provideBaseTitleCases() {
468 # Title, expected base, optional message
469 array( 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
470 array( 'User:Foo/Bar/Baz', 'Foo/Bar' ),
475 * @dataProvider provideRootTitleCases
476 * @covers Title::getRootText
478 public function testGetRootText( $title, $expected, $msg = '' ) {
479 $title = Title
::newFromText( $title );
480 $this->assertEquals( $expected,
481 $title->getRootText(),
486 public static function provideRootTitleCases() {
488 # Title, expected base, optional message
489 array( 'User:John_Doe/subOne/subTwo', 'John Doe' ),
490 array( 'User:Foo/Bar/Baz', 'Foo' ),
495 * @todo Handle $wgNamespacesWithSubpages cases
496 * @dataProvider provideSubpageTitleCases
497 * @covers Title::getSubpageText
499 public function testGetSubpageText( $title, $expected, $msg = '' ) {
500 $title = Title
::newFromText( $title );
501 $this->assertEquals( $expected,
502 $title->getSubpageText(),
507 public static function provideSubpageTitleCases() {
509 # Title, expected base, optional message
510 array( 'User:John_Doe/subOne/subTwo', 'subTwo' ),
511 array( 'User:John_Doe/subOne', 'subOne' ),