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