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