Merge "Change message on Special:Wantedfiles based on if foreign repo on."
[lhc/web/wiklou.git] / tests / phpunit / includes / TitleTest.php
1 <?php
2
3 /**
4 * @group Database
5 * ^--- needed for language cache stuff
6 *
7 * @group Title
8 */
9 class TitleTest extends MediaWikiTestCase {
10 protected function setUp() {
11 parent::setUp();
12
13 $this->setMwGlobals( array(
14 'wgLanguageCode' => 'en',
15 'wgContLang' => Language::factory( 'en' ),
16 // User language
17 'wgLang' => Language::factory( 'en' ),
18 'wgAllowUserJs' => false,
19 'wgDefaultLanguageVariant' => false,
20 ) );
21 }
22
23 /**
24 * @covers Title::legalChars
25 */
26 public function testLegalChars() {
27 $titlechars = Title::legalChars();
28
29 foreach ( range( 1, 255 ) as $num ) {
30 $chr = chr( $num );
31 if ( strpos( "#[]{}<>|", $chr ) !== false || preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
32 $this->assertFalse(
33 (bool)preg_match( "/[$titlechars]/", $chr ),
34 "chr($num) = $chr is not a valid titlechar"
35 );
36 } else {
37 $this->assertTrue(
38 (bool)preg_match( "/[$titlechars]/", $chr ),
39 "chr($num) = $chr is a valid titlechar"
40 );
41 }
42 }
43 }
44
45 /**
46 * See also mediawiki.Title.test.js
47 * @covers Title::secureAndSplit
48 * @todo This method should be split into 2 separate tests each with a provider
49 * @note: This mainly tests MediaWikiTitleCodec::parseTitle().
50 */
51 public function testSecureAndSplit() {
52 $this->setMwGlobals( array(
53 'wgLocalInterwikis' => array( 'localtestiw' ),
54 'wgHooks' => array(
55 'InterwikiLoadPrefix' => array(
56 function ( $prefix, &$data ) {
57 if ( $prefix === 'localtestiw' ) {
58 $data = array( 'iw_url' => 'localtestiw' );
59 } elseif ( $prefix === 'remotetestiw' ) {
60 $data = array( 'iw_url' => 'remotetestiw' );
61 }
62 return false;
63 }
64 )
65 )
66 ));
67 // Valid
68 foreach ( array(
69 'Sandbox',
70 'A "B"',
71 'A \'B\'',
72 '.com',
73 '~',
74 '#',
75 '"',
76 '\'',
77 'Talk:Sandbox',
78 'Talk:Foo:Sandbox',
79 'File:Example.svg',
80 'File_talk:Example.svg',
81 'Foo/.../Sandbox',
82 'Sandbox/...',
83 'A~~',
84 ':A',
85 // Length is 256 total, but only title part matters
86 'Category:' . str_repeat( 'x', 248 ),
87 str_repeat( 'x', 252 ),
88 // interwiki prefix
89 'localtestiw: #anchor',
90 'localtestiw:',
91 'localtestiw:foo',
92 'localtestiw: foo # anchor',
93 'localtestiw: Talk: Sandbox # anchor',
94 'remotetestiw:',
95 'remotetestiw: Talk: # anchor',
96 'remotetestiw: #bar',
97 'remotetestiw: Talk:',
98 'remotetestiw: Talk: Foo',
99 'localtestiw:remotetestiw:',
100 'localtestiw:remotetestiw:foo'
101 ) as $text ) {
102 $this->assertInstanceOf( 'Title', Title::newFromText( $text ), "Valid: $text" );
103 }
104
105 // Invalid
106 foreach ( array(
107 '',
108 ':',
109 '__ __',
110 ' __ ',
111 // Bad characters forbidden regardless of wgLegalTitleChars
112 'A [ B',
113 'A ] B',
114 'A { B',
115 'A } B',
116 'A < B',
117 'A > B',
118 'A | B',
119 // URL encoding
120 'A%20B',
121 'A%23B',
122 'A%2523B',
123 // XML/HTML character entity references
124 // Note: Commented out because they are not marked invalid by the PHP test as
125 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
126 //'A &eacute; B',
127 //'A &#233; B',
128 //'A &#x00E9; B',
129 // Subject of NS_TALK does not roundtrip to NS_MAIN
130 'Talk:File:Example.svg',
131 // Directory navigation
132 '.',
133 '..',
134 './Sandbox',
135 '../Sandbox',
136 'Foo/./Sandbox',
137 'Foo/../Sandbox',
138 'Sandbox/.',
139 'Sandbox/..',
140 // Tilde
141 'A ~~~ Name',
142 'A ~~~~ Signature',
143 'A ~~~~~ Timestamp',
144 str_repeat( 'x', 256 ),
145 // Namespace prefix without actual title
146 'Talk:',
147 'Talk:#',
148 'Category: ',
149 'Category: #bar',
150 // interwiki prefix
151 'localtestiw: Talk: # anchor',
152 'localtestiw: Talk:'
153 ) as $text ) {
154 $this->assertNull( Title::newFromText( $text ), "Invalid: $text" );
155 }
156 }
157
158 public static function provideConvertByteClassToUnicodeClass() {
159 return array(
160 array(
161 ' %!"$&\'()*,\\-.\\/0-9:;=?@A-Z\\\\^_`a-z~\\x80-\\xFF+',
162 ' %!"$&\'()*,\\-./0-9:;=?@A-Z\\\\\\^_`a-z~+\\u0080-\\uFFFF',
163 ),
164 array(
165 'QWERTYf-\\xFF+',
166 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
167 ),
168 array(
169 'QWERTY\\x66-\\xFD+',
170 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
171 ),
172 array(
173 'QWERTYf-y+',
174 'QWERTYf-y+',
175 ),
176 array(
177 'QWERTYf-\\x80+',
178 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
179 ),
180 array(
181 'QWERTY\\x66-\\x80+\\x23',
182 'QWERTYf-\\x7F+#\\u0080-\\uFFFF',
183 ),
184 array(
185 'QWERTY\\x66-\\x80+\\xD3',
186 'QWERTYf-\\x7F+\\u0080-\\uFFFF',
187 ),
188 array(
189 '\\\\\\x99',
190 '\\\\\\u0080-\\uFFFF',
191 ),
192 array(
193 '-\\x99',
194 '\\-\\u0080-\\uFFFF',
195 ),
196 array(
197 'QWERTY\\-\\x99',
198 'QWERTY\\-\\u0080-\\uFFFF',
199 ),
200 array(
201 '\\\\x99',
202 '\\\\x99',
203 ),
204 array(
205 'A-\\x9F',
206 'A-\\x7F\\u0080-\\uFFFF',
207 ),
208 array(
209 '\\x66-\\x77QWERTY\\x88-\\x91FXZ',
210 'f-wQWERTYFXZ\\u0080-\\uFFFF',
211 ),
212 array(
213 '\\x66-\\x99QWERTY\\xAA-\\xEEFXZ',
214 'f-\\x7FQWERTYFXZ\\u0080-\\uFFFF',
215 ),
216 );
217 }
218
219 /**
220 * @dataProvider provideConvertByteClassToUnicodeClass
221 * @covers Title::convertByteClassToUnicodeClass
222 */
223 public function testConvertByteClassToUnicodeClass( $byteClass, $unicodeClass ) {
224 $this->assertEquals( $unicodeClass, Title::convertByteClassToUnicodeClass( $byteClass ) );
225 }
226
227 /**
228 * @dataProvider provideBug31100
229 * @covers Title::fixSpecialName
230 * @todo give this test a real name explaining what is being tested here
231 */
232 public function testBug31100FixSpecialName( $text, $expectedParam ) {
233 $title = Title::newFromText( $text );
234 $fixed = $title->fixSpecialName();
235 $stuff = explode( '/', $fixed->getDBkey(), 2 );
236 if ( count( $stuff ) == 2 ) {
237 $par = $stuff[1];
238 } else {
239 $par = null;
240 }
241 $this->assertEquals(
242 $expectedParam,
243 $par,
244 "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter"
245 );
246 }
247
248 public static function provideBug31100() {
249 return array(
250 array( 'Special:Version', null ),
251 array( 'Special:Version/', '' ),
252 array( 'Special:Version/param', 'param' ),
253 );
254 }
255
256 /**
257 * Auth-less test of Title::isValidMoveOperation
258 *
259 * @group Database
260 * @param string $source
261 * @param string $target
262 * @param array|string|bool $expected Required error
263 * @dataProvider provideTestIsValidMoveOperation
264 * @covers Title::isValidMoveOperation
265 */
266 public function testIsValidMoveOperation( $source, $target, $expected ) {
267 $title = Title::newFromText( $source );
268 $nt = Title::newFromText( $target );
269 $errors = $title->isValidMoveOperation( $nt, false );
270 if ( $expected === true ) {
271 $this->assertTrue( $errors );
272 } else {
273 $errors = $this->flattenErrorsArray( $errors );
274 foreach ( (array)$expected as $error ) {
275 $this->assertContains( $error, $errors );
276 }
277 }
278 }
279
280 /**
281 * Provides test parameter values for testIsValidMoveOperation()
282 */
283 public function dataTestIsValidMoveOperation() {
284 return array(
285 array( 'Test', 'Test', 'selfmove' ),
286 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
287 );
288 }
289
290 /**
291 * Auth-less test of Title::userCan
292 *
293 * @param array $whitelistRegexp
294 * @param string $source
295 * @param string $action
296 * @param array|string|bool $expected Required error
297 *
298 * @covers Title::checkReadPermissions
299 * @dataProvider dataWgWhitelistReadRegexp
300 */
301 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
302 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
303 // usually have only one regex, it is more concise to write the lonely regex
304 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
305 // type requisite.
306 if ( is_string( $whitelistRegexp ) ) {
307 $whitelistRegexp = array( $whitelistRegexp );
308 }
309
310 $title = Title::newFromDBkey( $source );
311
312 global $wgGroupPermissions;
313 $oldPermissions = $wgGroupPermissions;
314 // Disallow all so we can ensure our regex works
315 $wgGroupPermissions = array();
316 $wgGroupPermissions['*']['read'] = false;
317
318 global $wgWhitelistRead;
319 $oldWhitelist = $wgWhitelistRead;
320 // Undo any LocalSettings explicite whitelists so they won't cause a
321 // failing test to succeed. Set it to some random non sense just
322 // to make sure we properly test Title::checkReadPermissions()
323 $wgWhitelistRead = array( 'some random non sense title' );
324
325 global $wgWhitelistReadRegexp;
326 $oldWhitelistRegexp = $wgWhitelistReadRegexp;
327 $wgWhitelistReadRegexp = $whitelistRegexp;
328
329 // Just use $wgUser which in test is a user object for '127.0.0.1'
330 global $wgUser;
331 // Invalidate user rights cache to take in account $wgGroupPermissions
332 // change above.
333 $wgUser->clearInstanceCache();
334 $errors = $title->userCan( $action, $wgUser );
335
336 // Restore globals
337 $wgGroupPermissions = $oldPermissions;
338 $wgWhitelistRead = $oldWhitelist;
339 $wgWhitelistReadRegexp = $oldWhitelistRegexp;
340
341 if ( is_bool( $expected ) ) {
342 # Forge the assertion message depending on the assertion expectation
343 $allowableness = $expected
344 ? " should be allowed"
345 : " should NOT be allowed";
346 $this->assertEquals(
347 $expected,
348 $errors,
349 "User action '$action' on [[$source]] $allowableness."
350 );
351 } else {
352 $errors = $this->flattenErrorsArray( $errors );
353 foreach ( (array)$expected as $error ) {
354 $this->assertContains( $error, $errors );
355 }
356 }
357 }
358
359 /**
360 * Provides test parameter values for testWgWhitelistReadRegexp()
361 */
362 public function dataWgWhitelistReadRegexp() {
363 $ALLOWED = true;
364 $DISALLOWED = false;
365
366 return array(
367 // Everything, if this doesn't work, we're really in trouble
368 array( '/.*/', 'Main_Page', 'read', $ALLOWED ),
369 array( '/.*/', 'Main_Page', 'edit', $DISALLOWED ),
370
371 // We validate against the title name, not the db key
372 array( '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ),
373 // Main page
374 array( '/^Main/', 'Main_Page', 'read', $ALLOWED ),
375 array( '/^Main.*/', 'Main_Page', 'read', $ALLOWED ),
376 // With spaces
377 array( '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ),
378 // Unicode multibyte
379 // ...without unicode modifier
380 array( '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ),
381 // ...with unicode modifier
382 array( '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ),
383 // Case insensitive
384 array( '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ),
385 array( '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ),
386
387 // From DefaultSettings.php:
388 array( "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ),
389 array( "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ),
390
391 // With namespaces:
392 array( '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ),
393 array( null, 'Special:Newpages', 'read', $DISALLOWED ),
394
395 );
396 }
397
398 public function flattenErrorsArray( $errors ) {
399 $result = array();
400 foreach ( $errors as $error ) {
401 $result[] = $error[0];
402 }
403
404 return $result;
405 }
406
407 public static function provideTestIsValidMoveOperation() {
408 return array(
409 array( 'Test', 'Test', 'selfmove' ),
410 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
411 );
412 }
413
414 /**
415 * @dataProvider provideGetPageViewLanguage
416 * @covers Title::getPageViewLanguage
417 */
418 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
419 $lang, $variant, $msg = ''
420 ) {
421 global $wgLanguageCode, $wgContLang, $wgLang, $wgDefaultLanguageVariant, $wgAllowUserJs;
422
423 // Setup environnement for this test
424 $wgLanguageCode = $contLang;
425 $wgContLang = Language::factory( $contLang );
426 $wgLang = Language::factory( $lang );
427 $wgDefaultLanguageVariant = $variant;
428 $wgAllowUserJs = true;
429
430 $title = Title::newFromText( $titleText );
431 $this->assertInstanceOf( 'Title', $title,
432 "Test must be passed a valid title text, you gave '$titleText'"
433 );
434 $this->assertEquals( $expected,
435 $title->getPageViewLanguage()->getCode(),
436 $msg
437 );
438 }
439
440 public static function provideGetPageViewLanguage() {
441 # Format:
442 # - expected
443 # - Title name
444 # - wgContLang (expected in most case)
445 # - wgLang (on some specific pages)
446 # - wgDefaultLanguageVariant
447 # - Optional message
448 return array(
449 array( 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ),
450 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ),
451 array( 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ),
452
453 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ),
454 array( 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ),
455 array( 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ),
456 array( 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ),
457 array( 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ),
458 array( 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ),
459 array( 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ),
460 array( 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
461
462 array( 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ),
463 array( 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ),
464 array( 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ),
465 array( 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ),
466 array( 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ),
467 array( 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ),
468 array( 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ),
469 array( 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ),
470 array( 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ),
471 array( 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
472
473 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
474 array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),
475
476 );
477 }
478
479 /**
480 * @dataProvider provideBaseTitleCases
481 * @covers Title::getBaseText
482 */
483 public function testGetBaseText( $title, $expected, $msg = '' ) {
484 $title = Title::newFromText( $title );
485 $this->assertEquals( $expected,
486 $title->getBaseText(),
487 $msg
488 );
489 }
490
491 public static function provideBaseTitleCases() {
492 return array(
493 # Title, expected base, optional message
494 array( 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
495 array( 'User:Foo/Bar/Baz', 'Foo/Bar' ),
496 );
497 }
498
499 /**
500 * @dataProvider provideRootTitleCases
501 * @covers Title::getRootText
502 */
503 public function testGetRootText( $title, $expected, $msg = '' ) {
504 $title = Title::newFromText( $title );
505 $this->assertEquals( $expected,
506 $title->getRootText(),
507 $msg
508 );
509 }
510
511 public static function provideRootTitleCases() {
512 return array(
513 # Title, expected base, optional message
514 array( 'User:John_Doe/subOne/subTwo', 'John Doe' ),
515 array( 'User:Foo/Bar/Baz', 'Foo' ),
516 );
517 }
518
519 /**
520 * @todo Handle $wgNamespacesWithSubpages cases
521 * @dataProvider provideSubpageTitleCases
522 * @covers Title::getSubpageText
523 */
524 public function testGetSubpageText( $title, $expected, $msg = '' ) {
525 $title = Title::newFromText( $title );
526 $this->assertEquals( $expected,
527 $title->getSubpageText(),
528 $msg
529 );
530 }
531
532 public static function provideSubpageTitleCases() {
533 return array(
534 # Title, expected base, optional message
535 array( 'User:John_Doe/subOne/subTwo', 'subTwo' ),
536 array( 'User:John_Doe/subOne', 'subOne' ),
537 );
538 }
539
540 public function provideNewFromTitleValue() {
541 return array(
542 array( new TitleValue( NS_MAIN, 'Foo' ) ),
543 array( new TitleValue( NS_MAIN, 'Foo', 'bar' ) ),
544 array( new TitleValue( NS_USER, 'Hansi_Maier' ) ),
545 );
546 }
547
548 /**
549 * @dataProvider provideNewFromTitleValue
550 */
551 public function testNewFromTitleValue( TitleValue $value ) {
552 $title = Title::newFromTitleValue( $value );
553
554 $dbkey = str_replace( ' ', '_', $value->getText() );
555 $this->assertEquals( $dbkey, $title->getDBkey() );
556 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
557 $this->assertEquals( $value->getFragment(), $title->getFragment() );
558 }
559
560 public function provideGetTitleValue() {
561 return array(
562 array( 'Foo' ),
563 array( 'Foo#bar' ),
564 array( 'User:Hansi_Maier' ),
565 );
566 }
567
568 /**
569 * @dataProvider provideGetTitleValue
570 */
571 public function testGetTitleValue( $text ) {
572 $title = Title::newFromText( $text );
573 $value = $title->getTitleValue();
574
575 $dbkey = str_replace( ' ', '_', $value->getText() );
576 $this->assertEquals( $title->getDBkey(), $dbkey );
577 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
578 $this->assertEquals( $title->getFragment(), $value->getFragment() );
579 }
580
581 public function provideGetFragment() {
582 return array(
583 array( 'Foo', '' ),
584 array( 'Foo#bar', 'bar' ),
585 array( 'Foo#bär', 'bär' ),
586
587 // Inner whitespace is normalized
588 array( 'Foo#bar_bar', 'bar bar' ),
589 array( 'Foo#bar bar', 'bar bar' ),
590 array( 'Foo#bar bar', 'bar bar' ),
591
592 // Leading whitespace is kept, trailing whitespace is trimmed.
593 // XXX: Is this really want we want?
594 array( 'Foo#_bar_bar_', ' bar bar' ),
595 array( 'Foo# bar bar ', ' bar bar' ),
596 );
597 }
598
599 /**
600 * @dataProvider provideGetFragment
601 *
602 * @param string $full
603 * @param string $fragment
604 */
605 public function testGetFragment( $full, $fragment ) {
606 $title = Title::newFromText( $full );
607 $this->assertEquals( $fragment, $title->getFragment() );
608 }
609 }