4 * @group GlobalFunctions
6 class GlobalTest
extends MediaWikiTestCase
{
7 protected function setUp() {
10 $readOnlyFile = $this->getNewTempFile();
11 unlink( $readOnlyFile );
13 $this->setMwGlobals( array(
14 'wgReadOnlyFile' => $readOnlyFile,
15 'wgUrlProtocols' => array(
20 'file://', # Non-default
26 * @dataProvider provideForWfArrayDiff2
27 * @covers ::wfArrayDiff2
29 public function testWfArrayDiff2( $a, $b, $expected ) {
31 wfArrayDiff2( $a, $b ), $expected
35 // @todo Provide more tests
36 public static function provideForWfArrayDiff2() {
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.
60 public function testRandom() {
62 wfRandom() == wfRandom()
67 * @covers ::wfRandomString
69 public function testRandomString() {
71 wfRandomString() == wfRandomString()
74 strlen( wfRandomString( 10 ) ), 10
77 preg_match( '/^[0-9a-f]+$/i', wfRandomString() ) === 1
82 * @covers ::wfUrlencode
84 public function testUrlencode() {
86 "%E7%89%B9%E5%88%A5:Contributions/Foobar",
87 wfUrlencode( "\xE7\x89\xB9\xE5\x88\xA5:Contributions/Foobar" ) );
91 * @covers ::wfExpandIRI
93 public function testExpandIRI() {
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() {
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' );
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() {
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
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' ),
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' ),
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() {
177 array( 'baz' => 'bar' ),
178 array( 'foo' => 'bar', 'baz' => 'overridden value' ) ) );
181 public static function provideCgiToArray() {
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' ) ) ),
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' ) ) ),
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() {
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() {
237 mimeTypeMatch( 'text/html',
238 array( 'application/xhtml+xml' => 1.0,
240 'text/plain' => 0.3 ) ) );
243 mimeTypeMatch( 'text/html',
244 array( 'image/*' => 1.0,
245 'text/*' => 0.5 ) ) );
248 mimeTypeMatch( 'text/html',
249 array( '*/*' => 1.0 ) ) );
251 mimeTypeMatch( 'text/html',
252 array( 'image/png' => 1.0,
253 'image/svg+xml' => 0.5 ) ) );
257 * @covers ::wfNegotiateType
259 public function testNegotiateType() {
263 array( 'application/xhtml+xml' => 1.0,
267 array( 'text/html' => 1.0 ) ) );
269 'application/xhtml+xml',
271 array( 'application/xhtml+xml' => 1.0,
275 array( 'application/xhtml+xml' => 1.0,
276 'text/html' => 0.5 ) ) );
280 array( 'text/html' => 1.0,
283 'application/xhtml+xml' => 0.2 ),
284 array( 'application/xhtml+xml' => 1.0,
285 'text/html' => 0.5 ) ) );
289 array( 'text/*' => 1.0,
292 array( 'application/xhtml+xml' => 1.0,
293 'text/html' => 0.5 ) ) );
296 array( 'text/*' => 1.0 ),
297 array( 'application/xhtml+xml' => 1.0 ) ) );
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
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" );
323 " 05This has böth UTF and control chars \n",
324 file_get_contents( $debugLogFile )
326 unlink( $debugLogFile );
329 $this->assertGreaterThan(
331 preg_replace( '/\D/', '', file_get_contents( $debugLogFile ) )
333 unlink( $debugLogFile );
336 $this->assertGreaterThan(
338 preg_replace( '/\D/', '', file_get_contents( $debugLogFile ) )
340 unlink( $debugLogFile );
344 * @covers ::wfClientAcceptsGzip
346 public function testClientAcceptsGzipTest() {
352 'compress, gzip' => true,
353 'gzip;q=1.0' => true,
356 'gzip;q=abcde' => true, //is this REALLY valid?
357 'gzip;q=12345678.9' => 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;
380 public function testSwapVarsTest() {
381 $this->hideDeprecated( 'swap' );
386 $this->assertEquals( $var1, 1, 'var1 is set originally' );
387 $this->assertEquals( $var2, 2, 'var1 is set originally' );
389 swap( $var1, $var2 );
391 $this->assertEquals( $var1, 2, 'var1 is swapped' );
392 $this->assertEquals( $var2, 1, 'var2 is swapped' );
396 * @covers ::wfPercent
398 public function testWfPercentTest() {
401 array( 6 / 7, '0.86%', 2, false ),
402 array( 3 / 3, '1%' ),
403 array( 22 / 7, '3.14286%', 5 ),
404 array( 3 / 6, '0.5%' ),
405 array( 1 / 3, '0%', 0 ),
406 array( 10 / 3, '0%', -1 ),
407 array( 3 / 4 / 5, '0.1%', 1 ),
408 array( 6 / 7 * 8, '6.8571428571%', 10 ),
411 foreach ( $pcts as $pct ) {
412 if ( !isset( $pct[2] ) ) {
415 if ( !isset( $pct[3] ) ) {
419 $this->assertEquals( wfPercent( $pct[0], $pct[2], $pct[3] ), $pct[1], $pct[1] );
424 * test @see wfShorthandToInteger()
425 * @dataProvider provideShorthand
426 * @covers ::wfShorthandToInteger
428 public function testWfShorthandToInteger( $shorthand, $expected ) {
429 $this->assertEquals( $expected,
430 wfShorthandToInteger( $shorthand )
434 /** array( shorthand, expected integer ) */
435 public static function provideShorthand() {
442 # Failures returns 0 :(
443 array( 'ABCDEFG', 0 ),
446 # Int, strings with spaces
450 array( ' 1023 ', 1023 ),
455 array( '1m', 1024 * 1024 ),
456 array( '1M', 1024 * 1024 ),
457 array( '1g', 1024 * 1024 * 1024 ),
458 array( '1G', 1024 * 1024 * 1024 ),
463 array( '-500', -500 ),
464 array( '-1k', -1024 ),
479 * @param string $old Text as it was in the database
480 * @param string $mine Text submitted while user was editing
481 * @param string $yours Text submitted by the user
482 * @param bool $expectedMergeResult Whether the merge should be a success
483 * @param string $expectedText Text after merge has been completed
485 * @dataProvider provideMerge()
489 public function testMerge( $old, $mine, $yours, $expectedMergeResult, $expectedText ) {
490 $this->checkHasDiff3();
493 $isMerged = wfMerge( $old, $mine, $yours, $mergedText );
495 $msg = 'Merge should be a ';
496 $msg .= $expectedMergeResult ?
'success' : 'failure';
497 $this->assertEquals( $expectedMergeResult, $isMerged, $msg );
500 // Verify the merged text
501 $this->assertEquals( $expectedText, $mergedText,
502 'is merged text as expected?' );
506 public static function provideMerge() {
507 $EXPECT_MERGE_SUCCESS = true;
508 $EXPECT_MERGE_FAILURE = false;
514 "one one one\n" . // trimmed
519 "one one one ONE ONE\n" .
521 "two two two\n", // with tailing whitespace
526 "two two TWO TWO", // trimmed
529 $EXPECT_MERGE_SUCCESS,
532 "one one one ONE ONE\n" .
534 "two two TWO TWO\n", // note: will always end in a newline
537 // #1: conflict, fail
540 "one one one", // trimmed
543 "one one one ONE ONE\n" .
546 "\n", // with tailing whitespace
551 "two two", // trimmed
553 $EXPECT_MERGE_FAILURE,
562 * @dataProvider provideMakeUrlIndexes()
563 * @covers ::wfMakeUrlIndexes
565 public function testMakeUrlIndexes( $url, $expected ) {
566 $index = wfMakeUrlIndexes( $url );
567 $this->assertEquals( $expected, $index, "wfMakeUrlIndexes(\"$url\")" );
570 public static function provideMakeUrlIndexes() {
574 'https://bugzilla.wikimedia.org/show_bug.cgi?id=28627',
575 array( 'https://org.wikimedia.bugzilla./show_bug.cgi?id=28627' )
578 // mailtos are handled special
579 // is this really right though? that final . probably belongs earlier?
580 'mailto:wiki@wikimedia.org',
581 array( 'mailto:org.wikimedia@wiki.' )
584 // file URL cases per bug 28627...
586 // three slashes: local filesystem path Unix-style
587 'file:///whatever/you/like.txt',
588 array( 'file://./whatever/you/like.txt' )
591 // three slashes: local filesystem path Windows-style
592 'file:///c:/whatever/you/like.txt',
593 array( 'file://./c:/whatever/you/like.txt' )
596 // two slashes: UNC filesystem path Windows-style
597 'file://intranet/whatever/you/like.txt',
598 array( 'file://intranet./whatever/you/like.txt' )
600 // Multiple-slash cases that can sorta work on Mozilla
601 // if you hack it just right are kinda pathological,
602 // and unreliable cross-platform or on IE which means they're
603 // unlikely to appear on intranets.
605 // Those will survive the algorithm but with results that
606 // are less consistent.
608 // protocol-relative URL cases per bug 29854...
610 '//bugzilla.wikimedia.org/show_bug.cgi?id=28627',
612 'http://org.wikimedia.bugzilla./show_bug.cgi?id=28627',
613 'https://org.wikimedia.bugzilla./show_bug.cgi?id=28627'
620 * @dataProvider provideWfMatchesDomainList
621 * @covers ::wfMatchesDomainList
623 public function testWfMatchesDomainList( $url, $domains, $expected, $description ) {
624 $actual = wfMatchesDomainList( $url, $domains );
625 $this->assertEquals( $expected, $actual, $description );
628 public static function provideWfMatchesDomainList() {
630 $protocols = array( 'HTTP' => 'http:', 'HTTPS' => 'https:', 'protocol-relative' => '' );
631 foreach ( $protocols as $pDesc => $p ) {
632 $a = array_merge( $a, array(
634 "$p//www.example.com",
637 "No matches for empty domains array, $pDesc URL"
640 "$p//www.example.com",
641 array( 'www.example.com' ),
643 "Exact match in domains array, $pDesc URL"
646 "$p//www.example.com",
647 array( 'example.com' ),
649 "Match without subdomain in domains array, $pDesc URL"
652 "$p//www.example2.com",
653 array( 'www.example.com', 'www.example2.com', 'www.example3.com' ),
655 "Exact match with other domains in array, $pDesc URL"
658 "$p//www.example2.com",
659 array( 'example.com', 'example2.com', 'example3,com' ),
661 "Match without subdomain with other domains in array, $pDesc URL"
664 "$p//www.example4.com",
665 array( 'example.com', 'example2.com', 'example3,com' ),
667 "Domain not in array, $pDesc URL"
670 "$p//nds-nl.wikipedia.org",
671 array( 'nl.wikipedia.org' ),
673 "Non-matching substring of domain, $pDesc URL"
682 * @covers ::wfMkdirParents
684 public function testWfMkdirParents() {
685 // Should not return true if file exists instead of directory
686 $fname = $this->getNewTempFile();
687 wfSuppressWarnings();
688 $ok = wfMkdirParents( $fname );
690 $this->assertFalse( $ok );
694 * @dataProvider provideWfShellMaintenanceCmdList
695 * @covers ::wfShellMaintenanceCmd
697 public function testWfShellMaintenanceCmd( $script, $parameters, $options,
698 $expected, $description
700 if ( wfIsWindows() ) {
701 // Approximation that's good enough for our purposes just now
702 $expected = str_replace( "'", '"', $expected );
704 $actual = wfShellMaintenanceCmd( $script, $parameters, $options );
705 $this->assertEquals( $expected, $actual, $description );
708 public static function provideWfShellMaintenanceCmdList() {
712 array( 'eval.php', array( '--help', '--test' ), array(),
713 "'$wgPhpCli' 'eval.php' '--help' '--test'",
714 "Called eval.php --help --test" ),
715 array( 'eval.php', array( '--help', '--test space' ), array( 'php' => 'php5' ),
716 "'php5' 'eval.php' '--help' '--test space'",
717 "Called eval.php --help --test with php option" ),
718 array( 'eval.php', array( '--help', '--test', 'X' ), array( 'wrapper' => 'MWScript.php' ),
719 "'$wgPhpCli' 'MWScript.php' 'eval.php' '--help' '--test' 'X'",
720 "Called eval.php --help --test with wrapper option" ),
723 array( '--help', '--test', 'y' ),
724 array( 'php' => 'php5', 'wrapper' => 'MWScript.php' ),
725 "'php5' 'MWScript.php' 'eval.php' '--help' '--test' 'y'",
726 "Called eval.php --help --test with wrapper and php option"
730 /* @todo many more! */