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