Merge "Add two new debug log groups"
[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 */
217 public function testBug31100FixSpecialName( $text, $expectedParam ) {
218 $title = Title::newFromText( $text );
219 $fixed = $title->fixSpecialName();
220 $stuff = explode( '/', $fixed->getDBkey(), 2 );
221 if ( count( $stuff ) == 2 ) {
222 $par = $stuff[1];
223 } else {
224 $par = null;
225 }
226 $this->assertEquals( $expectedParam, $par, "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter" );
227 }
228
229 public static function provideBug31100() {
230 return array(
231 array( 'Special:Version', null ),
232 array( 'Special:Version/', '' ),
233 array( 'Special:Version/param', 'param' ),
234 );
235 }
236
237 /**
238 * Auth-less test of Title::isValidMoveOperation
239 *
240 * @group Database
241 * @param string $source
242 * @param string $target
243 * @param array|string|bool $expected Required error
244 * @dataProvider provideTestIsValidMoveOperation
245 * @covers Title::isValidMoveOperation
246 */
247 public function testIsValidMoveOperation( $source, $target, $expected ) {
248 $title = Title::newFromText( $source );
249 $nt = Title::newFromText( $target );
250 $errors = $title->isValidMoveOperation( $nt, false );
251 if ( $expected === true ) {
252 $this->assertTrue( $errors );
253 } else {
254 $errors = $this->flattenErrorsArray( $errors );
255 foreach ( (array)$expected as $error ) {
256 $this->assertContains( $error, $errors );
257 }
258 }
259 }
260
261 /**
262 * Provides test parameter values for testIsValidMoveOperation()
263 */
264 public function dataTestIsValidMoveOperation() {
265 return array(
266 array( 'Test', 'Test', 'selfmove' ),
267 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
268 );
269 }
270
271 /**
272 * Auth-less test of Title::userCan
273 *
274 * @param array $whitelistRegexp
275 * @param string $source
276 * @param string $action
277 * @param array|string|bool $expected Required error
278 *
279 * @covers Title::checkReadPermissions
280 * @dataProvider dataWgWhitelistReadRegexp
281 */
282 public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
283 // $wgWhitelistReadRegexp must be an array. Since the provided test cases
284 // usually have only one regex, it is more concise to write the lonely regex
285 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp
286 // type requisite.
287 if ( is_string( $whitelistRegexp ) ) {
288 $whitelistRegexp = array( $whitelistRegexp );
289 }
290
291 $title = Title::newFromDBkey( $source );
292
293 global $wgGroupPermissions;
294 $oldPermissions = $wgGroupPermissions;
295 // Disallow all so we can ensure our regex works
296 $wgGroupPermissions = array();
297 $wgGroupPermissions['*']['read'] = false;
298
299 global $wgWhitelistRead;
300 $oldWhitelist = $wgWhitelistRead;
301 // Undo any LocalSettings explicite whitelists so they won't cause a
302 // failing test to succeed. Set it to some random non sense just
303 // to make sure we properly test Title::checkReadPermissions()
304 $wgWhitelistRead = array( 'some random non sense title' );
305
306 global $wgWhitelistReadRegexp;
307 $oldWhitelistRegexp = $wgWhitelistReadRegexp;
308 $wgWhitelistReadRegexp = $whitelistRegexp;
309
310 // Just use $wgUser which in test is a user object for '127.0.0.1'
311 global $wgUser;
312 // Invalidate user rights cache to take in account $wgGroupPermissions
313 // change above.
314 $wgUser->clearInstanceCache();
315 $errors = $title->userCan( $action, $wgUser );
316
317 // Restore globals
318 $wgGroupPermissions = $oldPermissions;
319 $wgWhitelistRead = $oldWhitelist;
320 $wgWhitelistReadRegexp = $oldWhitelistRegexp;
321
322 if ( is_bool( $expected ) ) {
323 # Forge the assertion message depending on the assertion expectation
324 $allowableness = $expected
325 ? " should be allowed"
326 : " should NOT be allowed";
327 $this->assertEquals( $expected, $errors, "User action '$action' on [[$source]] $allowableness." );
328 } else {
329 $errors = $this->flattenErrorsArray( $errors );
330 foreach ( (array)$expected as $error ) {
331 $this->assertContains( $error, $errors );
332 }
333 }
334 }
335
336 /**
337 * Provides test parameter values for testWgWhitelistReadRegexp()
338 */
339 public function dataWgWhitelistReadRegexp() {
340 $ALLOWED = true;
341 $DISALLOWED = false;
342
343 return array(
344 // Everything, if this doesn't work, we're really in trouble
345 array( '/.*/', 'Main_Page', 'read', $ALLOWED ),
346 array( '/.*/', 'Main_Page', 'edit', $DISALLOWED ),
347
348 // We validate against the title name, not the db key
349 array( '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ),
350 // Main page
351 array( '/^Main/', 'Main_Page', 'read', $ALLOWED ),
352 array( '/^Main.*/', 'Main_Page', 'read', $ALLOWED ),
353 // With spaces
354 array( '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ),
355 // Unicode multibyte
356 // ...without unicode modifier
357 array( '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ),
358 // ...with unicode modifier
359 array( '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ),
360 // Case insensitive
361 array( '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ),
362 array( '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ),
363
364 // From DefaultSettings.php:
365 array( "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ),
366 array( "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ),
367
368 // With namespaces:
369 array( '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ),
370 array( null, 'Special:Newpages', 'read', $DISALLOWED ),
371
372 );
373 }
374
375 public function flattenErrorsArray( $errors ) {
376 $result = array();
377 foreach ( $errors as $error ) {
378 $result[] = $error[0];
379 }
380
381 return $result;
382 }
383
384 public static function provideTestIsValidMoveOperation() {
385 return array(
386 array( 'Test', 'Test', 'selfmove' ),
387 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
388 );
389 }
390
391 /**
392 * @dataProvider provideGetPageViewLanguage
393 * @covers Title::getPageViewLanguage
394 */
395 public function testGetPageViewLanguage( $expected, $titleText, $contLang, $lang, $variant, $msg = '' ) {
396 global $wgLanguageCode, $wgContLang, $wgLang, $wgDefaultLanguageVariant, $wgAllowUserJs;
397
398 // Setup environnement for this test
399 $wgLanguageCode = $contLang;
400 $wgContLang = Language::factory( $contLang );
401 $wgLang = Language::factory( $lang );
402 $wgDefaultLanguageVariant = $variant;
403 $wgAllowUserJs = true;
404
405 $title = Title::newFromText( $titleText );
406 $this->assertInstanceOf( 'Title', $title,
407 "Test must be passed a valid title text, you gave '$titleText'"
408 );
409 $this->assertEquals( $expected,
410 $title->getPageViewLanguage()->getCode(),
411 $msg
412 );
413 }
414
415 public static function provideGetPageViewLanguage() {
416 # Format:
417 # - expected
418 # - Title name
419 # - wgContLang (expected in most case)
420 # - wgLang (on some specific pages)
421 # - wgDefaultLanguageVariant
422 # - Optional message
423 return array(
424 array( 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ),
425 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ),
426 array( 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ),
427
428 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ),
429 array( 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ),
430 array( 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ),
431 array( 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ),
432 array( 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ),
433 array( 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ),
434 array( 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ),
435 array( 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
436
437 array( 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ),
438 array( 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ),
439 array( 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ),
440 array( 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ),
441 array( 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ),
442 array( 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ),
443 array( 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ),
444 array( 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ),
445 array( 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ),
446 array( 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
447
448 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ),
449 array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ),
450
451 );
452 }
453
454 /**
455 * @dataProvider provideBaseTitleCases
456 * @covers Title::getBaseText
457 */
458 public function testGetBaseText( $title, $expected, $msg = '' ) {
459 $title = Title::newFromText( $title );
460 $this->assertEquals( $expected,
461 $title->getBaseText(),
462 $msg
463 );
464 }
465
466 public static function provideBaseTitleCases() {
467 return array(
468 # Title, expected base, optional message
469 array( 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
470 array( 'User:Foo/Bar/Baz', 'Foo/Bar' ),
471 );
472 }
473
474 /**
475 * @dataProvider provideRootTitleCases
476 * @covers Title::getRootText
477 */
478 public function testGetRootText( $title, $expected, $msg = '' ) {
479 $title = Title::newFromText( $title );
480 $this->assertEquals( $expected,
481 $title->getRootText(),
482 $msg
483 );
484 }
485
486 public static function provideRootTitleCases() {
487 return array(
488 # Title, expected base, optional message
489 array( 'User:John_Doe/subOne/subTwo', 'John Doe' ),
490 array( 'User:Foo/Bar/Baz', 'Foo' ),
491 );
492 }
493
494 /**
495 * @todo Handle $wgNamespacesWithSubpages cases
496 * @dataProvider provideSubpageTitleCases
497 * @covers Title::getSubpageText
498 */
499 public function testGetSubpageText( $title, $expected, $msg = '' ) {
500 $title = Title::newFromText( $title );
501 $this->assertEquals( $expected,
502 $title->getSubpageText(),
503 $msg
504 );
505 }
506
507 public static function provideSubpageTitleCases() {
508 return array(
509 # Title, expected base, optional message
510 array( 'User:John_Doe/subOne/subTwo', 'subTwo' ),
511 array( 'User:John_Doe/subOne', 'subOne' ),
512 );
513 }
514 }