Merge "Fix font of mw-ui-button"
[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 provideBug31100
226 * @covers Title::fixSpecialName
227 * @todo give this test a real name explaining what is being tested here
228 */
229 public function testBug31100FixSpecialName( $text, $expectedParam ) {
230 $title = Title::newFromText( $text );
231 $fixed = $title->fixSpecialName();
232 $stuff = explode( '/', $fixed->getDBkey(), 2 );
233 if ( count( $stuff ) == 2 ) {
234 $par = $stuff[1];
235 } else {
236 $par = null;
237 }
238 $this->assertEquals(
239 $expectedParam,
240 $par,
241 "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter"
242 );
243 }
244
245 public static function provideBug31100() {
246 return array(
247 array( 'Special:Version', null ),
248 array( 'Special:Version/', '' ),
249 array( 'Special:Version/param', 'param' ),
250 );
251 }
252
253 /**
254 * Auth-less test of Title::isValidMoveOperation
255 *
256 * @group Database
257 * @param string $source
258 * @param string $target
259 * @param array|string|bool $expected Required error
260 * @dataProvider provideTestIsValidMoveOperation
261 * @covers Title::isValidMoveOperation
262 */
263 public function testIsValidMoveOperation( $source, $target, $expected ) {
264 $title = Title::newFromText( $source );
265 $nt = Title::newFromText( $target );
266 $errors = $title->isValidMoveOperation( $nt, false );
267 if ( $expected === true ) {
268 $this->assertTrue( $errors );
269 } else {
270 $errors = $this->flattenErrorsArray( $errors );
271 foreach ( (array)$expected as $error ) {
272 $this->assertContains( $error, $errors );
273 }
274 }
275 }
276
277 /**
278 * Provides test parameter values for testIsValidMoveOperation()
279 */
280 public function dataTestIsValidMoveOperation() {
281 return array(
282 array( 'Test', 'Test', 'selfmove' ),
283 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
284 );
285 }
286
287 /**
288 * Auth-less test of Title::userCan
289 *
290 * @param array $whitelistRegexp
291 * @param string $source
292 * @param string $action
293 * @param array|string|bool $expected Required error
294 *
295 * @covers Title::checkReadPermissions
296 * @dataProvider dataWgWhitelistReadRegexp
297 */
298 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
299 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
300 // usually have only one regex, it is more concise to write the lonely regex
301 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
302 // type requisite.
303 if ( is_string( $whitelistRegexp ) ) {
304 $whitelistRegexp = array( $whitelistRegexp );
305 }
306
307 $title = Title::newFromDBkey( $source );
308
309 global $wgGroupPermissions;
310 $oldPermissions = $wgGroupPermissions;
311 // Disallow all so we can ensure our regex works
312 $wgGroupPermissions = array();
313 $wgGroupPermissions['*']['read'] = false;
314
315 global $wgWhitelistRead;
316 $oldWhitelist = $wgWhitelistRead;
317 // Undo any LocalSettings explicite whitelists so they won't cause a
318 // failing test to succeed. Set it to some random non sense just
319 // to make sure we properly test Title::checkReadPermissions()
320 $wgWhitelistRead = array( 'some random non sense title' );
321
322 global $wgWhitelistReadRegexp;
323 $oldWhitelistRegexp = $wgWhitelistReadRegexp;
324 $wgWhitelistReadRegexp = $whitelistRegexp;
325
326 // Just use $wgUser which in test is a user object for '127.0.0.1'
327 global $wgUser;
328 // Invalidate user rights cache to take in account $wgGroupPermissions
329 // change above.
330 $wgUser->clearInstanceCache();
331 $errors = $title->userCan( $action, $wgUser );
332
333 // Restore globals
334 $wgGroupPermissions = $oldPermissions;
335 $wgWhitelistRead = $oldWhitelist;
336 $wgWhitelistReadRegexp = $oldWhitelistRegexp;
337
338 if ( is_bool( $expected ) ) {
339 # Forge the assertion message depending on the assertion expectation
340 $allowableness = $expected
341 ? " should be allowed"
342 : " should NOT be allowed";
343 $this->assertEquals(
344 $expected,
345 $errors,
346 "User action '$action' on [[$source]] $allowableness."
347 );
348 } else {
349 $errors = $this->flattenErrorsArray( $errors );
350 foreach ( (array)$expected as $error ) {
351 $this->assertContains( $error, $errors );
352 }
353 }
354 }
355
356 /**
357 * Provides test parameter values for testWgWhitelistReadRegexp()
358 */
359 public function dataWgWhitelistReadRegexp() {
360 $ALLOWED = true;
361 $DISALLOWED = false;
362
363 return array(
364 // Everything, if this doesn't work, we're really in trouble
365 array( '/.*/', 'Main_Page', 'read', $ALLOWED ),
366 array( '/.*/', 'Main_Page', 'edit', $DISALLOWED ),
367
368 // We validate against the title name, not the db key
369 array( '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ),
370 // Main page
371 array( '/^Main/', 'Main_Page', 'read', $ALLOWED ),
372 array( '/^Main.*/', 'Main_Page', 'read', $ALLOWED ),
373 // With spaces
374 array( '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ),
375 // Unicode multibyte
376 // ...without unicode modifier
377 array( '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ),
378 // ...with unicode modifier
379 array( '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ),
380 // Case insensitive
381 array( '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ),
382 array( '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ),
383
384 // From DefaultSettings.php:
385 array( "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ),
386 array( "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ),
387
388 // With namespaces:
389 array( '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ),
390 array( null, 'Special:Newpages', 'read', $DISALLOWED ),
391
392 );
393 }
394
395 public function flattenErrorsArray( $errors ) {
396 $result = array();
397 foreach ( $errors as $error ) {
398 $result[] = $error[0];
399 }
400
401 return $result;
402 }
403
404 public static function provideTestIsValidMoveOperation() {
405 return array(
406 array( 'Test', 'Test', 'selfmove' ),
407 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
408 );
409 }
410
411 /**
412 * @dataProvider provideGetPageViewLanguage
413 * @covers Title::getPageViewLanguage
414 */
415 public function testGetPageViewLanguage( $expected, $titleText, $contLang,
416 $lang, $variant, $msg = ''
417 ) {
418 global $wgLanguageCode, $wgContLang, $wgLang, $wgDefaultLanguageVariant, $wgAllowUserJs;
419
420 // Setup environnement for this test
421 $wgLanguageCode = $contLang;
422 $wgContLang = Language::factory( $contLang );
423 $wgLang = Language::factory( $lang );
424 $wgDefaultLanguageVariant = $variant;
425 $wgAllowUserJs = true;
426
427 $title = Title::newFromText( $titleText );
428 $this->assertInstanceOf( 'Title', $title,
429 "Test must be passed a valid title text, you gave '$titleText'"
430 );
431 $this->assertEquals( $expected,
432 $title->getPageViewLanguage()->getCode(),
433 $msg
434 );
435 }
436
437 public static function provideGetPageViewLanguage() {
438 # Format:
439 # - expected
440 # - Title name
441 # - wgContLang (expected in most case)
442 # - wgLang (on some specific pages)
443 # - wgDefaultLanguageVariant
444 # - Optional message
445 return array(
446 array( 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ),
447 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ),
448 array( 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ),
449
450 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ),
451 array( 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ),
452 array( 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ),
453 array( 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ),
454 array( 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ),
455 array( 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ),
456 array( 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ),
457 array( 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
458
459 array( 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ),
460 array( 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ),
461 array( 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ),
462 array( 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ),
463 array( 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ),
464 array( 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ),
465 array( 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ),
466 array( 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ),
467 array( 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ),
468 array( 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
469
470 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
471 array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),
472
473 );
474 }
475
476 /**
477 * @dataProvider provideBaseTitleCases
478 * @covers Title::getBaseText
479 */
480 public function testGetBaseText( $title, $expected, $msg = '' ) {
481 $title = Title::newFromText( $title );
482 $this->assertEquals( $expected,
483 $title->getBaseText(),
484 $msg
485 );
486 }
487
488 public static function provideBaseTitleCases() {
489 return array(
490 # Title, expected base, optional message
491 array( 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
492 array( 'User:Foo/Bar/Baz', 'Foo/Bar' ),
493 );
494 }
495
496 /**
497 * @dataProvider provideRootTitleCases
498 * @covers Title::getRootText
499 */
500 public function testGetRootText( $title, $expected, $msg = '' ) {
501 $title = Title::newFromText( $title );
502 $this->assertEquals( $expected,
503 $title->getRootText(),
504 $msg
505 );
506 }
507
508 public static function provideRootTitleCases() {
509 return array(
510 # Title, expected base, optional message
511 array( 'User:John_Doe/subOne/subTwo', 'John Doe' ),
512 array( 'User:Foo/Bar/Baz', 'Foo' ),
513 );
514 }
515
516 /**
517 * @todo Handle $wgNamespacesWithSubpages cases
518 * @dataProvider provideSubpageTitleCases
519 * @covers Title::getSubpageText
520 */
521 public function testGetSubpageText( $title, $expected, $msg = '' ) {
522 $title = Title::newFromText( $title );
523 $this->assertEquals( $expected,
524 $title->getSubpageText(),
525 $msg
526 );
527 }
528
529 public static function provideSubpageTitleCases() {
530 return array(
531 # Title, expected base, optional message
532 array( 'User:John_Doe/subOne/subTwo', 'subTwo' ),
533 array( 'User:John_Doe/subOne', 'subOne' ),
534 );
535 }
536
537 public function provideNewFromTitleValue() {
538 return array(
539 array( new TitleValue( NS_MAIN, 'Foo' ) ),
540 array( new TitleValue( NS_MAIN, 'Foo', 'bar' ) ),
541 array( new TitleValue( NS_USER, 'Hansi_Maier' ) ),
542 );
543 }
544
545 /**
546 * @dataProvider provideNewFromTitleValue
547 */
548 public function testNewFromTitleValue( TitleValue $value ) {
549 $title = Title::newFromTitleValue( $value );
550
551 $dbkey = str_replace( ' ', '_', $value->getText() );
552 $this->assertEquals( $dbkey, $title->getDBkey() );
553 $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
554 $this->assertEquals( $value->getFragment(), $title->getFragment() );
555 }
556
557 public function provideGetTitleValue() {
558 return array(
559 array( 'Foo' ),
560 array( 'Foo#bar' ),
561 array( 'User:Hansi_Maier' ),
562 );
563 }
564
565 /**
566 * @dataProvider provideGetTitleValue
567 */
568 public function testGetTitleValue( $text ) {
569 $title = Title::newFromText( $text );
570 $value = $title->getTitleValue();
571
572 $dbkey = str_replace( ' ', '_', $value->getText() );
573 $this->assertEquals( $title->getDBkey(), $dbkey );
574 $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
575 $this->assertEquals( $title->getFragment(), $value->getFragment() );
576 }
577
578 public function provideGetFragment() {
579 return array(
580 array( 'Foo', '' ),
581 array( 'Foo#bar', 'bar' ),
582 array( 'Foo#bär', 'bär' ),
583
584 // Inner whitespace is normalized
585 array( 'Foo#bar_bar', 'bar bar' ),
586 array( 'Foo#bar bar', 'bar bar' ),
587 array( 'Foo#bar bar', 'bar bar' ),
588
589 // Leading whitespace is kept, trailing whitespace is trimmed.
590 // XXX: Is this really want we want?
591 array( 'Foo#_bar_bar_', ' bar bar' ),
592 array( 'Foo# bar bar ', ' bar bar' ),
593 );
594 }
595
596 /**
597 * @dataProvider provideGetFragment
598 *
599 * @param string $full
600 * @param string $fragment
601 */
602 public function testGetFragment( $full, $fragment ) {
603 $title = Title::newFromText( $full );
604 $this->assertEquals( $fragment, $title->getFragment() );
605 }
606
607 /**
608 * @covers Title::isAlwaysKnown
609 * @dataProvider provideIsAlwaysKnown
610 * @param string $page
611 * @param bool $isKnown
612 */
613 public function testIsAlwaysKnown( $page, $isKnown ) {
614 $title = Title::newFromText( $page );
615 $this->assertEquals( $isKnown, $title->isAlwaysKnown() );
616 }
617
618 public function provideIsAlwaysKnown() {
619 return array(
620 array( 'Some nonexistent page', false ),
621 array( 'UTPage', false ),
622 array( '#test', true ),
623 array( 'Special:BlankPage', true ),
624 array( 'Special:SomeNonexistentSpecialPage', false ),
625 array( 'MediaWiki:Parentheses', true ),
626 array( 'MediaWiki:Some nonexistent message', false ),
627 );
628 }
629
630 /**
631 * @covers Title::isAlwaysKnown
632 */
633 public function testIsAlwaysKnownOnInterwiki() {
634 $title = Title::makeTitle( NS_MAIN, 'Interwiki link', '', 'externalwiki' );
635 $this->assertTrue( $title->isAlwaysKnown() );
636 }
637 }