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