Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / tests / phpunit / includes / GlobalFunctions / GlobalTest.php
blob56f97469ae9570bf6dc4692f35f4ee7790fb3453
1 <?php
3 /**
4 * @group GlobalFunctions
5 */
6 class GlobalTest extends MediaWikiTestCase {
7 protected function setUp() {
8 parent::setUp();
10 $readOnlyFile = $this->getNewTempFile();
11 unlink( $readOnlyFile );
13 $this->setMwGlobals( array(
14 'wgReadOnlyFile' => $readOnlyFile,
15 'wgUrlProtocols' => array(
16 'http://',
17 'https://',
18 'mailto:',
19 '//',
20 'file://', # Non-default
22 ) );
25 /**
26 * @dataProvider provideForWfArrayDiff2
27 * @covers ::wfArrayDiff2
29 public function testWfArrayDiff2( $a, $b, $expected ) {
30 $this->assertEquals(
31 wfArrayDiff2( $a, $b ), $expected
35 // @todo Provide more tests
36 public static function provideForWfArrayDiff2() {
37 // $a $b $expected
38 return array(
39 array(
40 array( 'a', 'b' ),
41 array( 'a', 'b' ),
42 array(),
44 array(
45 array( array( 'a' ), array( 'a', 'b', 'c' ) ),
46 array( array( 'a' ), array( 'a', 'b' ) ),
47 array( 1 => array( 'a', 'b', 'c' ) ),
53 * Test cases for random functions could hypothetically fail,
54 * even though they shouldn't.
57 /**
58 * @covers ::wfRandom
60 public function testRandom() {
61 $this->assertFalse(
62 wfRandom() == wfRandom()
66 /**
67 * @covers ::wfRandomString
69 public function testRandomString() {
70 $this->assertFalse(
71 wfRandomString() == wfRandomString()
73 $this->assertEquals(
74 strlen( wfRandomString( 10 ) ), 10
76 $this->assertTrue(
77 preg_match( '/^[0-9a-f]+$/i', wfRandomString() ) === 1
81 /**
82 * @covers ::wfUrlencode
84 public function testUrlencode() {
85 $this->assertEquals(
86 "%E7%89%B9%E5%88%A5:Contributions/Foobar",
87 wfUrlencode( "\xE7\x89\xB9\xE5\x88\xA5:Contributions/Foobar" ) );
90 /**
91 * @covers ::wfExpandIRI
93 public function testExpandIRI() {
94 $this->assertEquals(
95 "https://te.wikibooks.org/wiki/ఉబుంటు_వాడుకరి_మార్గదర్శని",
96 wfExpandIRI( "https://te.wikibooks.org/wiki/"
97 . "%E0%B0%89%E0%B0%AC%E0%B1%81%E0%B0%82%E0%B0%9F%E0%B1%81_"
98 . "%E0%B0%B5%E0%B0%BE%E0%B0%A1%E0%B1%81%E0%B0%95%E0%B0%B0%E0%B0%BF_"
99 . "%E0%B0%AE%E0%B0%BE%E0%B0%B0%E0%B1%8D%E0%B0%97%E0%B0%A6%E0%B0%B0"
100 . "%E0%B1%8D%E0%B0%B6%E0%B0%A8%E0%B0%BF" ) );
104 * @covers ::wfReadOnly
106 public function testReadOnlyEmpty() {
107 global $wgReadOnly;
108 $wgReadOnly = null;
110 $this->assertFalse( wfReadOnly() );
111 $this->assertFalse( wfReadOnly() );
115 * @covers ::wfReadOnly
117 public function testReadOnlySet() {
118 global $wgReadOnly, $wgReadOnlyFile;
120 $f = fopen( $wgReadOnlyFile, "wt" );
121 fwrite( $f, 'Message' );
122 fclose( $f );
123 $wgReadOnly = null; # Check on $wgReadOnlyFile
125 $this->assertTrue( wfReadOnly() );
126 $this->assertTrue( wfReadOnly() ); # Check cached
128 unlink( $wgReadOnlyFile );
129 $wgReadOnly = null; # Clean cache
131 $this->assertFalse( wfReadOnly() );
132 $this->assertFalse( wfReadOnly() );
135 public static function provideArrayToCGI() {
136 return array(
137 array( array(), '' ), // empty
138 array( array( 'foo' => 'bar' ), 'foo=bar' ), // string test
139 array( array( 'foo' => '' ), 'foo=' ), // empty string test
140 array( array( 'foo' => 1 ), 'foo=1' ), // number test
141 array( array( 'foo' => true ), 'foo=1' ), // true test
142 array( array( 'foo' => false ), '' ), // false test
143 array( array( 'foo' => null ), '' ), // null test
144 array( array( 'foo' => 'A&B=5+6@!"\'' ), 'foo=A%26B%3D5%2B6%40%21%22%27' ), // urlencoding test
145 array(
146 array( 'foo' => 'bar', 'baz' => 'is', 'asdf' => 'qwerty' ),
147 'foo=bar&baz=is&asdf=qwerty'
148 ), // multi-item test
149 array( array( 'foo' => array( 'bar' => 'baz' ) ), 'foo%5Bbar%5D=baz' ),
150 array(
151 array( 'foo' => array( 'bar' => 'baz', 'qwerty' => 'asdf' ) ),
152 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf'
154 array( array( 'foo' => array( 'bar', 'baz' ) ), 'foo%5B0%5D=bar&foo%5B1%5D=baz' ),
155 array(
156 array( 'foo' => array( 'bar' => array( 'bar' => 'baz' ) ) ),
157 'foo%5Bbar%5D%5Bbar%5D=baz'
163 * @dataProvider provideArrayToCGI
164 * @covers ::wfArrayToCgi
166 public function testArrayToCGI( $array, $result ) {
167 $this->assertEquals( $result, wfArrayToCgi( $array ) );
171 * @covers ::wfArrayToCgi
173 public function testArrayToCGI2() {
174 $this->assertEquals(
175 "baz=bar&foo=bar",
176 wfArrayToCgi(
177 array( 'baz' => 'bar' ),
178 array( 'foo' => 'bar', 'baz' => 'overridden value' ) ) );
181 public static function provideCgiToArray() {
182 return array(
183 array( '', array() ), // empty
184 array( 'foo=bar', array( 'foo' => 'bar' ) ), // string
185 array( 'foo=', array( 'foo' => '' ) ), // empty string
186 array( 'foo', array( 'foo' => '' ) ), // missing =
187 array( 'foo=bar&qwerty=asdf', array( 'foo' => 'bar', 'qwerty' => 'asdf' ) ), // multiple value
188 array( 'foo=A%26B%3D5%2B6%40%21%22%27', array( 'foo' => 'A&B=5+6@!"\'' ) ), // urldecoding test
189 array( 'foo%5Bbar%5D=baz', array( 'foo' => array( 'bar' => 'baz' ) ) ),
190 array(
191 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf',
192 array( 'foo' => array( 'bar' => 'baz', 'qwerty' => 'asdf' ) )
194 array( 'foo%5B0%5D=bar&foo%5B1%5D=baz', array( 'foo' => array( 0 => 'bar', 1 => 'baz' ) ) ),
195 array(
196 'foo%5Bbar%5D%5Bbar%5D=baz',
197 array( 'foo' => array( 'bar' => array( 'bar' => 'baz' ) ) )
203 * @dataProvider provideCgiToArray
204 * @covers ::wfCgiToArray
206 public function testCgiToArray( $cgi, $result ) {
207 $this->assertEquals( $result, wfCgiToArray( $cgi ) );
210 public static function provideCgiRoundTrip() {
211 return array(
212 array( '' ),
213 array( 'foo=bar' ),
214 array( 'foo=' ),
215 array( 'foo=bar&baz=biz' ),
216 array( 'foo=A%26B%3D5%2B6%40%21%22%27' ),
217 array( 'foo%5Bbar%5D=baz' ),
218 array( 'foo%5B0%5D=bar&foo%5B1%5D=baz' ),
219 array( 'foo%5Bbar%5D%5Bbar%5D=baz' ),
224 * @dataProvider provideCgiRoundTrip
225 * @covers ::wfArrayToCgi
227 public function testCgiRoundTrip( $cgi ) {
228 $this->assertEquals( $cgi, wfArrayToCgi( wfCgiToArray( $cgi ) ) );
232 * @covers ::mimeTypeMatch
234 public function testMimeTypeMatch() {
235 $this->assertEquals(
236 'text/html',
237 mimeTypeMatch( 'text/html',
238 array( 'application/xhtml+xml' => 1.0,
239 'text/html' => 0.7,
240 'text/plain' => 0.3 ) ) );
241 $this->assertEquals(
242 'text/*',
243 mimeTypeMatch( 'text/html',
244 array( 'image/*' => 1.0,
245 'text/*' => 0.5 ) ) );
246 $this->assertEquals(
247 '*/*',
248 mimeTypeMatch( 'text/html',
249 array( '*/*' => 1.0 ) ) );
250 $this->assertNull(
251 mimeTypeMatch( 'text/html',
252 array( 'image/png' => 1.0,
253 'image/svg+xml' => 0.5 ) ) );
257 * @covers ::wfNegotiateType
259 public function testNegotiateType() {
260 $this->assertEquals(
261 'text/html',
262 wfNegotiateType(
263 array( 'application/xhtml+xml' => 1.0,
264 'text/html' => 0.7,
265 'text/plain' => 0.5,
266 'text/*' => 0.2 ),
267 array( 'text/html' => 1.0 ) ) );
268 $this->assertEquals(
269 'application/xhtml+xml',
270 wfNegotiateType(
271 array( 'application/xhtml+xml' => 1.0,
272 'text/html' => 0.7,
273 'text/plain' => 0.5,
274 'text/*' => 0.2 ),
275 array( 'application/xhtml+xml' => 1.0,
276 'text/html' => 0.5 ) ) );
277 $this->assertEquals(
278 'text/html',
279 wfNegotiateType(
280 array( 'text/html' => 1.0,
281 'text/plain' => 0.5,
282 'text/*' => 0.5,
283 'application/xhtml+xml' => 0.2 ),
284 array( 'application/xhtml+xml' => 1.0,
285 'text/html' => 0.5 ) ) );
286 $this->assertEquals(
287 'text/html',
288 wfNegotiateType(
289 array( 'text/*' => 1.0,
290 'image/*' => 0.7,
291 '*/*' => 0.3 ),
292 array( 'application/xhtml+xml' => 1.0,
293 'text/html' => 0.5 ) ) );
294 $this->assertNull(
295 wfNegotiateType(
296 array( 'text/*' => 1.0 ),
297 array( 'application/xhtml+xml' => 1.0 ) ) );
301 * @covers ::wfDebug
302 * @covers ::wfDebugMem
304 public function testDebugFunctionTest() {
305 $debugLogFile = $this->getNewTempFile();
307 $this->setMwGlobals( array(
308 'wgDebugLogFile' => $debugLogFile,
309 #  @todo FIXME: $wgDebugTimestamps should be tested
310 'wgDebugTimestamps' => false
311 ) );
313 wfDebug( "This is a normal string" );
314 $this->assertEquals( "This is a normal string\n", file_get_contents( $debugLogFile ) );
315 unlink( $debugLogFile );
317 wfDebug( "This is nöt an ASCII string" );
318 $this->assertEquals( "This is nöt an ASCII string\n", file_get_contents( $debugLogFile ) );
319 unlink( $debugLogFile );
321 wfDebug( "\00305This has böth UTF and control chars\003" );
322 $this->assertEquals(
323 " 05This has böth UTF and control chars \n",
324 file_get_contents( $debugLogFile )
326 unlink( $debugLogFile );
328 wfDebugMem();
329 $this->assertGreaterThan(
330 1000,
331 preg_replace( '/\D/', '', file_get_contents( $debugLogFile ) )
333 unlink( $debugLogFile );
335 wfDebugMem( true );
336 $this->assertGreaterThan(
337 1000000,
338 preg_replace( '/\D/', '', file_get_contents( $debugLogFile ) )
340 unlink( $debugLogFile );
344 * @covers ::wfClientAcceptsGzip
346 public function testClientAcceptsGzipTest() {
348 $settings = array(
349 'gzip' => true,
350 'bzip' => false,
351 '*' => false,
352 'compress, gzip' => true,
353 'gzip;q=1.0' => true,
354 'foozip' => false,
355 'foo*zip' => false,
356 'gzip;q=abcde' => true, // is this REALLY valid?
357 'gzip;q=12345678.9' => true,
358 ' gzip' => true,
361 if ( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) {
362 $old_server_setting = $_SERVER['HTTP_ACCEPT_ENCODING'];
365 foreach ( $settings as $encoding => $expect ) {
366 $_SERVER['HTTP_ACCEPT_ENCODING'] = $encoding;
368 $this->assertEquals( $expect, wfClientAcceptsGzip( true ),
369 "'$encoding' => " . wfBoolToStr( $expect ) );
372 if ( isset( $old_server_setting ) ) {
373 $_SERVER['HTTP_ACCEPT_ENCODING'] = $old_server_setting;
378 * @covers ::wfPercent
380 public function testWfPercentTest() {
382 $pcts = array(
383 array( 6 / 7, '0.86%', 2, false ),
384 array( 3 / 3, '1%' ),
385 array( 22 / 7, '3.14286%', 5 ),
386 array( 3 / 6, '0.5%' ),
387 array( 1 / 3, '0%', 0 ),
388 array( 10 / 3, '0%', -1 ),
389 array( 3 / 4 / 5, '0.1%', 1 ),
390 array( 6 / 7 * 8, '6.8571428571%', 10 ),
393 foreach ( $pcts as $pct ) {
394 if ( !isset( $pct[2] ) ) {
395 $pct[2] = 2;
397 if ( !isset( $pct[3] ) ) {
398 $pct[3] = true;
401 $this->assertEquals( wfPercent( $pct[0], $pct[2], $pct[3] ), $pct[1], $pct[1] );
406 * test @see wfShorthandToInteger()
407 * @dataProvider provideShorthand
408 * @covers ::wfShorthandToInteger
410 public function testWfShorthandToInteger( $shorthand, $expected ) {
411 $this->assertEquals( $expected,
412 wfShorthandToInteger( $shorthand )
416 /** array( shorthand, expected integer ) */
417 public static function provideShorthand() {
418 return array(
419 # Null, empty ...
420 array( '', -1 ),
421 array( ' ', -1 ),
422 array( null, -1 ),
424 # Failures returns 0 :(
425 array( 'ABCDEFG', 0 ),
426 array( 'Ak', 0 ),
428 # Int, strings with spaces
429 array( 1, 1 ),
430 array( ' 1 ', 1 ),
431 array( 1023, 1023 ),
432 array( ' 1023 ', 1023 ),
434 # kilo, Mega, Giga
435 array( '1k', 1024 ),
436 array( '1K', 1024 ),
437 array( '1m', 1024 * 1024 ),
438 array( '1M', 1024 * 1024 ),
439 array( '1g', 1024 * 1024 * 1024 ),
440 array( '1G', 1024 * 1024 * 1024 ),
442 # Negatives
443 array( -1, -1 ),
444 array( -500, -500 ),
445 array( '-500', -500 ),
446 array( '-1k', -1024 ),
448 # Zeroes
449 array( '0', 0 ),
450 array( '0k', 0 ),
451 array( '0M', 0 ),
452 array( '0G', 0 ),
453 array( '-0', 0 ),
454 array( '-0k', 0 ),
455 array( '-0M', 0 ),
456 array( '-0G', 0 ),
461 * @param string $old Text as it was in the database
462 * @param string $mine Text submitted while user was editing
463 * @param string $yours Text submitted by the user
464 * @param bool $expectedMergeResult Whether the merge should be a success
465 * @param string $expectedText Text after merge has been completed
467 * @dataProvider provideMerge()
468 * @group medium
469 * @covers ::wfMerge
471 public function testMerge( $old, $mine, $yours, $expectedMergeResult, $expectedText ) {
472 $this->checkHasDiff3();
474 $mergedText = null;
475 $isMerged = wfMerge( $old, $mine, $yours, $mergedText );
477 $msg = 'Merge should be a ';
478 $msg .= $expectedMergeResult ? 'success' : 'failure';
479 $this->assertEquals( $expectedMergeResult, $isMerged, $msg );
481 if ( $isMerged ) {
482 // Verify the merged text
483 $this->assertEquals( $expectedText, $mergedText,
484 'is merged text as expected?' );
488 public static function provideMerge() {
489 $EXPECT_MERGE_SUCCESS = true;
490 $EXPECT_MERGE_FAILURE = false;
492 return array(
493 // #0: clean merge
494 array(
495 // old:
496 "one one one\n" . // trimmed
497 "\n" .
498 "two two two",
500 // mine:
501 "one one one ONE ONE\n" .
502 "\n" .
503 "two two two\n", // with tailing whitespace
505 // yours:
506 "one one one\n" .
507 "\n" .
508 "two two TWO TWO", // trimmed
510 // ok:
511 $EXPECT_MERGE_SUCCESS,
513 // result:
514 "one one one ONE ONE\n" .
515 "\n" .
516 "two two TWO TWO\n", // note: will always end in a newline
519 // #1: conflict, fail
520 array(
521 // old:
522 "one one one", // trimmed
524 // mine:
525 "one one one ONE ONE\n" .
526 "\n" .
527 "bla bla\n" .
528 "\n", // with tailing whitespace
530 // yours:
531 "one one one\n" .
532 "\n" .
533 "two two", // trimmed
535 $EXPECT_MERGE_FAILURE,
537 // result:
538 null,
544 * @dataProvider provideMakeUrlIndexes()
545 * @covers ::wfMakeUrlIndexes
547 public function testMakeUrlIndexes( $url, $expected ) {
548 $index = wfMakeUrlIndexes( $url );
549 $this->assertEquals( $expected, $index, "wfMakeUrlIndexes(\"$url\")" );
552 public static function provideMakeUrlIndexes() {
553 return array(
554 // Testcase for T30627
555 array(
556 'https://example.org/test.cgi?id=12345',
557 array( 'https://org.example./test.cgi?id=12345' )
559 array(
560 // mailtos are handled special
561 // is this really right though? that final . probably belongs earlier?
562 'mailto:wiki@wikimedia.org',
563 array( 'mailto:org.wikimedia@wiki.' )
566 // file URL cases per T30627...
567 array(
568 // three slashes: local filesystem path Unix-style
569 'file:///whatever/you/like.txt',
570 array( 'file://./whatever/you/like.txt' )
572 array(
573 // three slashes: local filesystem path Windows-style
574 'file:///c:/whatever/you/like.txt',
575 array( 'file://./c:/whatever/you/like.txt' )
577 array(
578 // two slashes: UNC filesystem path Windows-style
579 'file://intranet/whatever/you/like.txt',
580 array( 'file://intranet./whatever/you/like.txt' )
582 // Multiple-slash cases that can sorta work on Mozilla
583 // if you hack it just right are kinda pathological,
584 // and unreliable cross-platform or on IE which means they're
585 // unlikely to appear on intranets.
586 // Those will survive the algorithm but with results that
587 // are less consistent.
589 // protocol-relative URL cases per T31854...
590 array(
591 '//example.org/test.cgi?id=12345',
592 array(
593 'http://org.example./test.cgi?id=12345',
594 'https://org.example./test.cgi?id=12345'
601 * @dataProvider provideWfMatchesDomainList
602 * @covers ::wfMatchesDomainList
604 public function testWfMatchesDomainList( $url, $domains, $expected, $description ) {
605 $actual = wfMatchesDomainList( $url, $domains );
606 $this->assertEquals( $expected, $actual, $description );
609 public static function provideWfMatchesDomainList() {
610 $a = array();
611 $protocols = array( 'HTTP' => 'http:', 'HTTPS' => 'https:', 'protocol-relative' => '' );
612 foreach ( $protocols as $pDesc => $p ) {
613 $a = array_merge( $a, array(
614 array(
615 "$p//www.example.com",
616 array(),
617 false,
618 "No matches for empty domains array, $pDesc URL"
620 array(
621 "$p//www.example.com",
622 array( 'www.example.com' ),
623 true,
624 "Exact match in domains array, $pDesc URL"
626 array(
627 "$p//www.example.com",
628 array( 'example.com' ),
629 true,
630 "Match without subdomain in domains array, $pDesc URL"
632 array(
633 "$p//www.example2.com",
634 array( 'www.example.com', 'www.example2.com', 'www.example3.com' ),
635 true,
636 "Exact match with other domains in array, $pDesc URL"
638 array(
639 "$p//www.example2.com",
640 array( 'example.com', 'example2.com', 'example3,com' ),
641 true,
642 "Match without subdomain with other domains in array, $pDesc URL"
644 array(
645 "$p//www.example4.com",
646 array( 'example.com', 'example2.com', 'example3,com' ),
647 false,
648 "Domain not in array, $pDesc URL"
650 array(
651 "$p//nds-nl.wikipedia.org",
652 array( 'nl.wikipedia.org' ),
653 false,
654 "Non-matching substring of domain, $pDesc URL"
656 ) );
659 return $a;
663 * @covers ::wfMkdirParents
665 public function testWfMkdirParents() {
666 // Should not return true if file exists instead of directory
667 $fname = $this->getNewTempFile();
668 MediaWiki\suppressWarnings();
669 $ok = wfMkdirParents( $fname );
670 MediaWiki\restoreWarnings();
671 $this->assertFalse( $ok );
675 * @dataProvider provideWfShellWikiCmdList
676 * @covers ::wfShellWikiCmd
678 public function testWfShellWikiCmd( $script, $parameters, $options,
679 $expected, $description
681 if ( wfIsWindows() ) {
682 // Approximation that's good enough for our purposes just now
683 $expected = str_replace( "'", '"', $expected );
685 $actual = wfShellWikiCmd( $script, $parameters, $options );
686 $this->assertEquals( $expected, $actual, $description );
689 public function wfWikiID() {
690 $this->setMwGlobals( array(
691 'wgDBname' => 'example',
692 'wgDBprefix' => '',
693 ) );
694 $this->assertEquals(
695 wfWikiID(),
696 'example'
699 $this->setMwGlobals( array(
700 'wgDBname' => 'example',
701 'wgDBprefix' => 'mw_',
702 ) );
703 $this->assertEquals(
704 wfWikiID(),
705 'example-mw_'
709 public function testWfMemcKey() {
710 $cache = ObjectCache::getLocalClusterInstance();
711 $this->assertEquals(
712 $cache->makeKey( 'foo', 123, 'bar' ),
713 wfMemcKey( 'foo', 123, 'bar' )
717 public function testWfForeignMemcKey() {
718 $cache = ObjectCache::getLocalClusterInstance();
719 $keyspace = $this->readAttribute( $cache, 'keyspace' );
720 $this->assertEquals(
721 wfForeignMemcKey( $keyspace, '', 'foo', 'bar' ),
722 $cache->makeKey( 'foo', 'bar' )
726 public function testWfGlobalCacheKey() {
727 $cache = ObjectCache::getLocalClusterInstance();
728 $this->assertEquals(
729 $cache->makeGlobalKey( 'foo', 123, 'bar' ),
730 wfGlobalCacheKey( 'foo', 123, 'bar' )
734 public static function provideWfShellWikiCmdList() {
735 global $wgPhpCli;
737 return array(
738 array( 'eval.php', array( '--help', '--test' ), array(),
739 "'$wgPhpCli' 'eval.php' '--help' '--test'",
740 "Called eval.php --help --test" ),
741 array( 'eval.php', array( '--help', '--test space' ), array( 'php' => 'php5' ),
742 "'php5' 'eval.php' '--help' '--test space'",
743 "Called eval.php --help --test with php option" ),
744 array( 'eval.php', array( '--help', '--test', 'X' ), array( 'wrapper' => 'MWScript.php' ),
745 "'$wgPhpCli' 'MWScript.php' 'eval.php' '--help' '--test' 'X'",
746 "Called eval.php --help --test with wrapper option" ),
747 array(
748 'eval.php',
749 array( '--help', '--test', 'y' ),
750 array( 'php' => 'php5', 'wrapper' => 'MWScript.php' ),
751 "'php5' 'MWScript.php' 'eval.php' '--help' '--test' 'y'",
752 "Called eval.php --help --test with wrapper and php option"
756 /* @todo many more! */