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