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