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