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