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