4 * @group GlobalFunctions
6 class GlobalTest
extends MediaWikiTestCase
{
7 protected function setUp() {
10 $readOnlyFile = tempnam( wfTempDir(), "mwtest_readonly" );
11 unlink( $readOnlyFile );
13 $this->setMwGlobals( array(
14 'wgReadOnlyFile' => $readOnlyFile,
15 'wgUrlProtocols' => array(
20 'file://', # Non-default
25 protected function tearDown() {
26 global $wgReadOnlyFile;
28 if ( file_exists( $wgReadOnlyFile ) ) {
29 unlink( $wgReadOnlyFile );
36 * @dataProvider provideForWfArrayDiff2
37 * @covers ::wfArrayDiff2
39 public function testWfArrayDiff2( $a, $b, $expected ) {
41 wfArrayDiff2( $a, $b ), $expected
45 // @todo Provide more tests
46 public static function provideForWfArrayDiff2() {
55 array( array( 'a' ), array( 'a', 'b', 'c' ) ),
56 array( array( 'a' ), array( 'a', 'b' ) ),
57 array( 1 => array( 'a', 'b', 'c' ) ),
63 * Test cases for random functions could hypothetically fail,
64 * even though they shouldn't.
70 public function testRandom() {
72 wfRandom() == wfRandom()
77 * @covers ::wfRandomString
79 public function testRandomString() {
81 wfRandomString() == wfRandomString()
84 strlen( wfRandomString( 10 ) ), 10
87 preg_match( '/^[0-9a-f]+$/i', wfRandomString() ) === 1
92 * @covers ::wfUrlencode
94 public function testUrlencode() {
96 "%E7%89%B9%E5%88%A5:Contributions/Foobar",
97 wfUrlencode( "\xE7\x89\xB9\xE5\x88\xA5:Contributions/Foobar" ) );
101 * @covers ::wfExpandIRI
103 public function testExpandIRI() {
105 "https://te.wikibooks.org/wiki/ఉబుంటు_వాడుకరి_మార్గదర్శని",
106 wfExpandIRI( "https://te.wikibooks.org/wiki/"
107 . "%E0%B0%89%E0%B0%AC%E0%B1%81%E0%B0%82%E0%B0%9F%E0%B1%81_"
108 . "%E0%B0%B5%E0%B0%BE%E0%B0%A1%E0%B1%81%E0%B0%95%E0%B0%B0%E0%B0%BF_"
109 . "%E0%B0%AE%E0%B0%BE%E0%B0%B0%E0%B1%8D%E0%B0%97%E0%B0%A6%E0%B0%B0"
110 . "%E0%B1%8D%E0%B0%B6%E0%B0%A8%E0%B0%BF" ) );
114 * @covers ::wfReadOnly
116 public function testReadOnlyEmpty() {
120 $this->assertFalse( wfReadOnly() );
121 $this->assertFalse( wfReadOnly() );
125 * @covers ::wfReadOnly
127 public function testReadOnlySet() {
128 global $wgReadOnly, $wgReadOnlyFile;
130 $f = fopen( $wgReadOnlyFile, "wt" );
131 fwrite( $f, 'Message' );
133 $wgReadOnly = null; # Check on $wgReadOnlyFile
135 $this->assertTrue( wfReadOnly() );
136 $this->assertTrue( wfReadOnly() ); # Check cached
138 unlink( $wgReadOnlyFile );
139 $wgReadOnly = null; # Clean cache
141 $this->assertFalse( wfReadOnly() );
142 $this->assertFalse( wfReadOnly() );
145 public static function provideArrayToCGI() {
147 array( array(), '' ), // empty
148 array( array( 'foo' => 'bar' ), 'foo=bar' ), // string test
149 array( array( 'foo' => '' ), 'foo=' ), // empty string test
150 array( array( 'foo' => 1 ), 'foo=1' ), // number test
151 array( array( 'foo' => true ), 'foo=1' ), // true test
152 array( array( 'foo' => false ), '' ), // false test
153 array( array( 'foo' => null ), '' ), // null test
154 array( array( 'foo' => 'A&B=5+6@!"\'' ), 'foo=A%26B%3D5%2B6%40%21%22%27' ), // urlencoding test
156 array( 'foo' => 'bar', 'baz' => 'is', 'asdf' => 'qwerty' ),
157 'foo=bar&baz=is&asdf=qwerty'
158 ), // multi-item test
159 array( array( 'foo' => array( 'bar' => 'baz' ) ), 'foo%5Bbar%5D=baz' ),
161 array( 'foo' => array( 'bar' => 'baz', 'qwerty' => 'asdf' ) ),
162 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf'
164 array( array( 'foo' => array( 'bar', 'baz' ) ), 'foo%5B0%5D=bar&foo%5B1%5D=baz' ),
166 array( 'foo' => array( 'bar' => array( 'bar' => 'baz' ) ) ),
167 'foo%5Bbar%5D%5Bbar%5D=baz'
173 * @dataProvider provideArrayToCGI
174 * @covers ::wfArrayToCgi
176 public function testArrayToCGI( $array, $result ) {
177 $this->assertEquals( $result, wfArrayToCgi( $array ) );
181 * @covers ::wfArrayToCgi
183 public function testArrayToCGI2() {
187 array( 'baz' => 'bar' ),
188 array( 'foo' => 'bar', 'baz' => 'overridden value' ) ) );
191 public static function provideCgiToArray() {
193 array( '', array() ), // empty
194 array( 'foo=bar', array( 'foo' => 'bar' ) ), // string
195 array( 'foo=', array( 'foo' => '' ) ), // empty string
196 array( 'foo', array( 'foo' => '' ) ), // missing =
197 array( 'foo=bar&qwerty=asdf', array( 'foo' => 'bar', 'qwerty' => 'asdf' ) ), // multiple value
198 array( 'foo=A%26B%3D5%2B6%40%21%22%27', array( 'foo' => 'A&B=5+6@!"\'' ) ), // urldecoding test
199 array( 'foo%5Bbar%5D=baz', array( 'foo' => array( 'bar' => 'baz' ) ) ),
201 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf',
202 array( 'foo' => array( 'bar' => 'baz', 'qwerty' => 'asdf' ) )
204 array( 'foo%5B0%5D=bar&foo%5B1%5D=baz', array( 'foo' => array( 0 => 'bar', 1 => 'baz' ) ) ),
206 'foo%5Bbar%5D%5Bbar%5D=baz',
207 array( 'foo' => array( 'bar' => array( 'bar' => 'baz' ) ) )
213 * @dataProvider provideCgiToArray
214 * @covers ::wfCgiToArray
216 public function testCgiToArray( $cgi, $result ) {
217 $this->assertEquals( $result, wfCgiToArray( $cgi ) );
220 public static function provideCgiRoundTrip() {
225 array( 'foo=bar&baz=biz' ),
226 array( 'foo=A%26B%3D5%2B6%40%21%22%27' ),
227 array( 'foo%5Bbar%5D=baz' ),
228 array( 'foo%5B0%5D=bar&foo%5B1%5D=baz' ),
229 array( 'foo%5Bbar%5D%5Bbar%5D=baz' ),
234 * @dataProvider provideCgiRoundTrip
235 * @covers ::wfArrayToCgi
237 public function testCgiRoundTrip( $cgi ) {
238 $this->assertEquals( $cgi, wfArrayToCgi( wfCgiToArray( $cgi ) ) );
242 * @covers ::mimeTypeMatch
244 public function testMimeTypeMatch() {
247 mimeTypeMatch( 'text/html',
248 array( 'application/xhtml+xml' => 1.0,
250 'text/plain' => 0.3 ) ) );
253 mimeTypeMatch( 'text/html',
254 array( 'image/*' => 1.0,
255 'text/*' => 0.5 ) ) );
258 mimeTypeMatch( 'text/html',
259 array( '*/*' => 1.0 ) ) );
261 mimeTypeMatch( 'text/html',
262 array( 'image/png' => 1.0,
263 'image/svg+xml' => 0.5 ) ) );
267 * @covers ::wfNegotiateType
269 public function testNegotiateType() {
273 array( 'application/xhtml+xml' => 1.0,
277 array( 'text/html' => 1.0 ) ) );
279 'application/xhtml+xml',
281 array( 'application/xhtml+xml' => 1.0,
285 array( 'application/xhtml+xml' => 1.0,
286 'text/html' => 0.5 ) ) );
290 array( 'text/html' => 1.0,
293 'application/xhtml+xml' => 0.2 ),
294 array( 'application/xhtml+xml' => 1.0,
295 'text/html' => 0.5 ) ) );
299 array( 'text/*' => 1.0,
302 array( 'application/xhtml+xml' => 1.0,
303 'text/html' => 0.5 ) ) );
306 array( 'text/*' => 1.0 ),
307 array( 'application/xhtml+xml' => 1.0 ) ) );
312 * @covers ::wfDebugMem
314 public function testDebugFunctionTest() {
316 global $wgDebugLogFile, $wgDebugTimestamps;
318 $old_log_file = $wgDebugLogFile;
319 $wgDebugLogFile = tempnam( wfTempDir(), 'mw-' );
320 # @todo FIXME: $wgDebugTimestamps should be tested
321 $old_wgDebugTimestamps = $wgDebugTimestamps;
322 $wgDebugTimestamps = false;
324 wfDebug( "This is a normal string" );
325 $this->assertEquals( "This is a normal string\n", file_get_contents( $wgDebugLogFile ) );
326 unlink( $wgDebugLogFile );
328 wfDebug( "This is nöt an ASCII string" );
329 $this->assertEquals( "This is nöt an ASCII string\n", file_get_contents( $wgDebugLogFile ) );
330 unlink( $wgDebugLogFile );
332 wfDebug( "\00305This has böth UTF and control chars\003" );
334 " 05This has böth UTF and control chars \n",
335 file_get_contents( $wgDebugLogFile )
337 unlink( $wgDebugLogFile );
340 $this->assertGreaterThan(
342 preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) )
344 unlink( $wgDebugLogFile );
347 $this->assertGreaterThan(
349 preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) )
351 unlink( $wgDebugLogFile );
353 $wgDebugLogFile = $old_log_file;
354 $wgDebugTimestamps = $old_wgDebugTimestamps;
358 * @covers ::wfClientAcceptsGzip
360 public function testClientAcceptsGzipTest() {
366 'compress, gzip' => true,
367 'gzip;q=1.0' => true,
370 'gzip;q=abcde' => true, //is this REALLY valid?
371 'gzip;q=12345678.9' => true,
375 if ( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) {
376 $old_server_setting = $_SERVER['HTTP_ACCEPT_ENCODING'];
379 foreach ( $settings as $encoding => $expect ) {
380 $_SERVER['HTTP_ACCEPT_ENCODING'] = $encoding;
382 $this->assertEquals( $expect, wfClientAcceptsGzip( true ),
383 "'$encoding' => " . wfBoolToStr( $expect ) );
386 if ( isset( $old_server_setting ) ) {
387 $_SERVER['HTTP_ACCEPT_ENCODING'] = $old_server_setting;
394 public function testSwapVarsTest() {
395 $this->hideDeprecated( 'swap' );
400 $this->assertEquals( $var1, 1, 'var1 is set originally' );
401 $this->assertEquals( $var2, 2, 'var1 is set originally' );
403 swap( $var1, $var2 );
405 $this->assertEquals( $var1, 2, 'var1 is swapped' );
406 $this->assertEquals( $var2, 1, 'var2 is swapped' );
410 * @covers ::wfPercent
412 public function testWfPercentTest() {
415 array( 6 / 7, '0.86%', 2, false ),
416 array( 3 / 3, '1%' ),
417 array( 22 / 7, '3.14286%', 5 ),
418 array( 3 / 6, '0.5%' ),
419 array( 1 / 3, '0%', 0 ),
420 array( 10 / 3, '0%', -1 ),
421 array( 3 / 4 / 5, '0.1%', 1 ),
422 array( 6 / 7 * 8, '6.8571428571%', 10 ),
425 foreach ( $pcts as $pct ) {
426 if ( !isset( $pct[2] ) ) {
429 if ( !isset( $pct[3] ) ) {
433 $this->assertEquals( wfPercent( $pct[0], $pct[2], $pct[3] ), $pct[1], $pct[1] );
438 * test @see wfShorthandToInteger()
439 * @dataProvider provideShorthand
440 * @covers ::wfShorthandToInteger
442 public function testWfShorthandToInteger( $shorthand, $expected ) {
443 $this->assertEquals( $expected,
444 wfShorthandToInteger( $shorthand )
448 /** array( shorthand, expected integer ) */
449 public static function provideShorthand() {
456 # Failures returns 0 :(
457 array( 'ABCDEFG', 0 ),
460 # Int, strings with spaces
464 array( ' 1023 ', 1023 ),
469 array( '1m', 1024 * 1024 ),
470 array( '1M', 1024 * 1024 ),
471 array( '1g', 1024 * 1024 * 1024 ),
472 array( '1G', 1024 * 1024 * 1024 ),
477 array( '-500', -500 ),
478 array( '-1k', -1024 ),
493 * @param string $old Text as it was in the database
494 * @param string $mine Text submitted while user was editing
495 * @param string $yours Text submitted by the user
496 * @param bool $expectedMergeResult Whether the merge should be a success
497 * @param string $expectedText Text after merge has been completed
499 * @dataProvider provideMerge()
503 public function testMerge( $old, $mine, $yours, $expectedMergeResult, $expectedText ) {
504 $this->checkHasDiff3();
507 $isMerged = wfMerge( $old, $mine, $yours, $mergedText );
509 $msg = 'Merge should be a ';
510 $msg .= $expectedMergeResult ?
'success' : 'failure';
511 $this->assertEquals( $expectedMergeResult, $isMerged, $msg );
514 // Verify the merged text
515 $this->assertEquals( $expectedText, $mergedText,
516 'is merged text as expected?' );
520 public static function provideMerge() {
521 $EXPECT_MERGE_SUCCESS = true;
522 $EXPECT_MERGE_FAILURE = false;
528 "one one one\n" . // trimmed
533 "one one one ONE ONE\n" .
535 "two two two\n", // with tailing whitespace
540 "two two TWO TWO", // trimmed
543 $EXPECT_MERGE_SUCCESS,
546 "one one one ONE ONE\n" .
548 "two two TWO TWO\n", // note: will always end in a newline
551 // #1: conflict, fail
554 "one one one", // trimmed
557 "one one one ONE ONE\n" .
560 "\n", // with tailing whitespace
565 "two two", // trimmed
567 $EXPECT_MERGE_FAILURE,
576 * @dataProvider provideMakeUrlIndexes()
577 * @covers ::wfMakeUrlIndexes
579 public function testMakeUrlIndexes( $url, $expected ) {
580 $index = wfMakeUrlIndexes( $url );
581 $this->assertEquals( $expected, $index, "wfMakeUrlIndexes(\"$url\")" );
584 public static function provideMakeUrlIndexes() {
588 'https://bugzilla.wikimedia.org/show_bug.cgi?id=28627',
589 array( 'https://org.wikimedia.bugzilla./show_bug.cgi?id=28627' )
592 // mailtos are handled special
593 // is this really right though? that final . probably belongs earlier?
594 'mailto:wiki@wikimedia.org',
595 array( 'mailto:org.wikimedia@wiki.' )
598 // file URL cases per bug 28627...
600 // three slashes: local filesystem path Unix-style
601 'file:///whatever/you/like.txt',
602 array( 'file://./whatever/you/like.txt' )
605 // three slashes: local filesystem path Windows-style
606 'file:///c:/whatever/you/like.txt',
607 array( 'file://./c:/whatever/you/like.txt' )
610 // two slashes: UNC filesystem path Windows-style
611 'file://intranet/whatever/you/like.txt',
612 array( 'file://intranet./whatever/you/like.txt' )
614 // Multiple-slash cases that can sorta work on Mozilla
615 // if you hack it just right are kinda pathological,
616 // and unreliable cross-platform or on IE which means they're
617 // unlikely to appear on intranets.
619 // Those will survive the algorithm but with results that
620 // are less consistent.
622 // protocol-relative URL cases per bug 29854...
624 '//bugzilla.wikimedia.org/show_bug.cgi?id=28627',
626 'http://org.wikimedia.bugzilla./show_bug.cgi?id=28627',
627 'https://org.wikimedia.bugzilla./show_bug.cgi?id=28627'
634 * @dataProvider provideWfMatchesDomainList
635 * @covers ::wfMatchesDomainList
637 public function testWfMatchesDomainList( $url, $domains, $expected, $description ) {
638 $actual = wfMatchesDomainList( $url, $domains );
639 $this->assertEquals( $expected, $actual, $description );
642 public static function provideWfMatchesDomainList() {
644 $protocols = array( 'HTTP' => 'http:', 'HTTPS' => 'https:', 'protocol-relative' => '' );
645 foreach ( $protocols as $pDesc => $p ) {
646 $a = array_merge( $a, array(
648 "$p//www.example.com",
651 "No matches for empty domains array, $pDesc URL"
654 "$p//www.example.com",
655 array( 'www.example.com' ),
657 "Exact match in domains array, $pDesc URL"
660 "$p//www.example.com",
661 array( 'example.com' ),
663 "Match without subdomain in domains array, $pDesc URL"
666 "$p//www.example2.com",
667 array( 'www.example.com', 'www.example2.com', 'www.example3.com' ),
669 "Exact match with other domains in array, $pDesc URL"
672 "$p//www.example2.com",
673 array( 'example.com', 'example2.com', 'example3,com' ),
675 "Match without subdomain with other domains in array, $pDesc URL"
678 "$p//www.example4.com",
679 array( 'example.com', 'example2.com', 'example3,com' ),
681 "Domain not in array, $pDesc URL"
684 "$p//nds-nl.wikipedia.org",
685 array( 'nl.wikipedia.org' ),
687 "Non-matching substring of domain, $pDesc URL"
696 * @covers ::wfMkdirParents
698 public function testWfMkdirParents() {
699 // Should not return true if file exists instead of directory
700 $fname = $this->getNewTempFile();
701 wfSuppressWarnings();
702 $ok = wfMkdirParents( $fname );
704 $this->assertFalse( $ok );
708 * @dataProvider provideWfShellMaintenanceCmdList
709 * @covers ::wfShellMaintenanceCmd
711 public function testWfShellMaintenanceCmd( $script, $parameters, $options,
712 $expected, $description
714 if ( wfIsWindows() ) {
715 // Approximation that's good enough for our purposes just now
716 $expected = str_replace( "'", '"', $expected );
718 $actual = wfShellMaintenanceCmd( $script, $parameters, $options );
719 $this->assertEquals( $expected, $actual, $description );
722 public static function provideWfShellMaintenanceCmdList() {
726 array( 'eval.php', array( '--help', '--test' ), array(),
727 "'$wgPhpCli' 'eval.php' '--help' '--test'",
728 "Called eval.php --help --test" ),
729 array( 'eval.php', array( '--help', '--test space' ), array( 'php' => 'php5' ),
730 "'php5' 'eval.php' '--help' '--test space'",
731 "Called eval.php --help --test with php option" ),
732 array( 'eval.php', array( '--help', '--test', 'X' ), array( 'wrapper' => 'MWScript.php' ),
733 "'$wgPhpCli' 'MWScript.php' 'eval.php' '--help' '--test' 'X'",
734 "Called eval.php --help --test with wrapper option" ),
737 array( '--help', '--test', 'y' ),
738 array( 'php' => 'php5', 'wrapper' => 'MWScript.php' ),
739 "'php5' 'MWScript.php' 'eval.php' '--help' '--test' 'y'",
740 "Called eval.php --help --test with wrapper and php option"
744 /* @todo many more! */