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