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' ) ),
65 public function testRandom() {
66 # This could hypothetically fail, but it shouldn't ;)
68 wfRandom() == wfRandom() );
72 * @covers ::wfUrlencode
74 public function testUrlencode() {
76 "%E7%89%B9%E5%88%A5:Contributions/Foobar",
77 wfUrlencode( "\xE7\x89\xB9\xE5\x88\xA5:Contributions/Foobar" ) );
81 * @covers ::wfExpandIRI
83 public function testExpandIRI() {
85 "https://te.wikibooks.org/wiki/ఉబుంటు_వాడుకరి_మార్గదర్శని",
86 wfExpandIRI( "https://te.wikibooks.org/wiki/"
87 . "%E0%B0%89%E0%B0%AC%E0%B1%81%E0%B0%82%E0%B0%9F%E0%B1%81_"
88 . "%E0%B0%B5%E0%B0%BE%E0%B0%A1%E0%B1%81%E0%B0%95%E0%B0%B0%E0%B0%BF_"
89 . "%E0%B0%AE%E0%B0%BE%E0%B0%B0%E0%B1%8D%E0%B0%97%E0%B0%A6%E0%B0%B0"
90 . "%E0%B1%8D%E0%B0%B6%E0%B0%A8%E0%B0%BF" ) );
94 * @covers ::wfReadOnly
96 public function testReadOnlyEmpty() {
100 $this->assertFalse( wfReadOnly() );
101 $this->assertFalse( wfReadOnly() );
105 * @covers ::wfReadOnly
107 public function testReadOnlySet() {
108 global $wgReadOnly, $wgReadOnlyFile;
110 $f = fopen( $wgReadOnlyFile, "wt" );
111 fwrite( $f, 'Message' );
113 $wgReadOnly = null; # Check on $wgReadOnlyFile
115 $this->assertTrue( wfReadOnly() );
116 $this->assertTrue( wfReadOnly() ); # Check cached
118 unlink( $wgReadOnlyFile );
119 $wgReadOnly = null; # Clean cache
121 $this->assertFalse( wfReadOnly() );
122 $this->assertFalse( wfReadOnly() );
125 public static function provideArrayToCGI() {
127 array( array(), '' ), // empty
128 array( array( 'foo' => 'bar' ), 'foo=bar' ), // string test
129 array( array( 'foo' => '' ), 'foo=' ), // empty string test
130 array( array( 'foo' => 1 ), 'foo=1' ), // number test
131 array( array( 'foo' => true ), 'foo=1' ), // true test
132 array( array( 'foo' => false ), '' ), // false test
133 array( array( 'foo' => null ), '' ), // null test
134 array( array( 'foo' => 'A&B=5+6@!"\'' ), 'foo=A%26B%3D5%2B6%40%21%22%27' ), // urlencoding test
136 array( 'foo' => 'bar', 'baz' => 'is', 'asdf' => 'qwerty' ),
137 'foo=bar&baz=is&asdf=qwerty'
138 ), // multi-item test
139 array( array( 'foo' => array( 'bar' => 'baz' ) ), 'foo%5Bbar%5D=baz' ),
141 array( 'foo' => array( 'bar' => 'baz', 'qwerty' => 'asdf' ) ),
142 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf'
144 array( array( 'foo' => array( 'bar', 'baz' ) ), 'foo%5B0%5D=bar&foo%5B1%5D=baz' ),
146 array( 'foo' => array( 'bar' => array( 'bar' => 'baz' ) ) ),
147 'foo%5Bbar%5D%5Bbar%5D=baz'
153 * @dataProvider provideArrayToCGI
154 * @covers ::wfArrayToCgi
156 public function testArrayToCGI( $array, $result ) {
157 $this->assertEquals( $result, wfArrayToCgi( $array ) );
161 * @covers ::wfArrayToCgi
163 public function testArrayToCGI2() {
167 array( 'baz' => 'bar' ),
168 array( 'foo' => 'bar', 'baz' => 'overridden value' ) ) );
171 public static function provideCgiToArray() {
173 array( '', array() ), // empty
174 array( 'foo=bar', array( 'foo' => 'bar' ) ), // string
175 array( 'foo=', array( 'foo' => '' ) ), // empty string
176 array( 'foo', array( 'foo' => '' ) ), // missing =
177 array( 'foo=bar&qwerty=asdf', array( 'foo' => 'bar', 'qwerty' => 'asdf' ) ), // multiple value
178 array( 'foo=A%26B%3D5%2B6%40%21%22%27', array( 'foo' => 'A&B=5+6@!"\'' ) ), // urldecoding test
179 array( 'foo%5Bbar%5D=baz', array( 'foo' => array( 'bar' => 'baz' ) ) ),
181 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf',
182 array( 'foo' => array( 'bar' => 'baz', 'qwerty' => 'asdf' ) )
184 array( 'foo%5B0%5D=bar&foo%5B1%5D=baz', array( 'foo' => array( 0 => 'bar', 1 => 'baz' ) ) ),
186 'foo%5Bbar%5D%5Bbar%5D=baz',
187 array( 'foo' => array( 'bar' => array( 'bar' => 'baz' ) ) )
193 * @dataProvider provideCgiToArray
194 * @covers ::wfCgiToArray
196 public function testCgiToArray( $cgi, $result ) {
197 $this->assertEquals( $result, wfCgiToArray( $cgi ) );
200 public static function provideCgiRoundTrip() {
205 array( 'foo=bar&baz=biz' ),
206 array( 'foo=A%26B%3D5%2B6%40%21%22%27' ),
207 array( 'foo%5Bbar%5D=baz' ),
208 array( 'foo%5B0%5D=bar&foo%5B1%5D=baz' ),
209 array( 'foo%5Bbar%5D%5Bbar%5D=baz' ),
214 * @dataProvider provideCgiRoundTrip
215 * @covers ::wfArrayToCgi
217 public function testCgiRoundTrip( $cgi ) {
218 $this->assertEquals( $cgi, wfArrayToCgi( wfCgiToArray( $cgi ) ) );
222 * @covers ::mimeTypeMatch
224 public function testMimeTypeMatch() {
227 mimeTypeMatch( 'text/html',
228 array( 'application/xhtml+xml' => 1.0,
230 'text/plain' => 0.3 ) ) );
233 mimeTypeMatch( 'text/html',
234 array( 'image/*' => 1.0,
235 'text/*' => 0.5 ) ) );
238 mimeTypeMatch( 'text/html',
239 array( '*/*' => 1.0 ) ) );
241 mimeTypeMatch( 'text/html',
242 array( 'image/png' => 1.0,
243 'image/svg+xml' => 0.5 ) ) );
247 * @covers ::wfNegotiateType
249 public function testNegotiateType() {
253 array( 'application/xhtml+xml' => 1.0,
257 array( 'text/html' => 1.0 ) ) );
259 'application/xhtml+xml',
261 array( 'application/xhtml+xml' => 1.0,
265 array( 'application/xhtml+xml' => 1.0,
266 'text/html' => 0.5 ) ) );
270 array( 'text/html' => 1.0,
273 'application/xhtml+xml' => 0.2 ),
274 array( 'application/xhtml+xml' => 1.0,
275 'text/html' => 0.5 ) ) );
279 array( 'text/*' => 1.0,
282 array( 'application/xhtml+xml' => 1.0,
283 'text/html' => 0.5 ) ) );
286 array( 'text/*' => 1.0 ),
287 array( 'application/xhtml+xml' => 1.0 ) ) );
292 * @covers ::wfDebugMem
294 public function testDebugFunctionTest() {
296 global $wgDebugLogFile, $wgDebugTimestamps;
298 $old_log_file = $wgDebugLogFile;
299 $wgDebugLogFile = tempnam( wfTempDir(), 'mw-' );
300 # @todo FIXME: $wgDebugTimestamps should be tested
301 $old_wgDebugTimestamps = $wgDebugTimestamps;
302 $wgDebugTimestamps = false;
304 wfDebug( "This is a normal string" );
305 $this->assertEquals( "This is a normal string", file_get_contents( $wgDebugLogFile ) );
306 unlink( $wgDebugLogFile );
308 wfDebug( "This is nöt an ASCII string" );
309 $this->assertEquals( "This is nöt an ASCII string", file_get_contents( $wgDebugLogFile ) );
310 unlink( $wgDebugLogFile );
312 wfDebug( "\00305This has böth UTF and control chars\003" );
314 " 05This has böth UTF and control chars ",
315 file_get_contents( $wgDebugLogFile )
317 unlink( $wgDebugLogFile );
320 $this->assertGreaterThan(
322 preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) )
324 unlink( $wgDebugLogFile );
327 $this->assertGreaterThan(
329 preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) )
331 unlink( $wgDebugLogFile );
333 $wgDebugLogFile = $old_log_file;
334 $wgDebugTimestamps = $old_wgDebugTimestamps;
338 * @covers ::wfClientAcceptsGzip
340 public function testClientAcceptsGzipTest() {
346 'compress, gzip' => true,
347 'gzip;q=1.0' => true,
350 'gzip;q=abcde' => true, //is this REALLY valid?
351 'gzip;q=12345678.9' => true,
355 if ( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) {
356 $old_server_setting = $_SERVER['HTTP_ACCEPT_ENCODING'];
359 foreach ( $settings as $encoding => $expect ) {
360 $_SERVER['HTTP_ACCEPT_ENCODING'] = $encoding;
362 $this->assertEquals( $expect, wfClientAcceptsGzip( true ),
363 "'$encoding' => " . wfBoolToStr( $expect ) );
366 if ( isset( $old_server_setting ) ) {
367 $_SERVER['HTTP_ACCEPT_ENCODING'] = $old_server_setting;
374 public function testSwapVarsTest() {
375 $this->hideDeprecated( 'swap' );
380 $this->assertEquals( $var1, 1, 'var1 is set originally' );
381 $this->assertEquals( $var2, 2, 'var1 is set originally' );
383 swap( $var1, $var2 );
385 $this->assertEquals( $var1, 2, 'var1 is swapped' );
386 $this->assertEquals( $var2, 1, 'var2 is swapped' );
390 * @covers ::wfPercent
392 public function testWfPercentTest() {
395 array( 6 / 7, '0.86%', 2, false ),
396 array( 3 / 3, '1%' ),
397 array( 22 / 7, '3.14286%', 5 ),
398 array( 3 / 6, '0.5%' ),
399 array( 1 / 3, '0%', 0 ),
400 array( 10 / 3, '0%', -1 ),
401 array( 3 / 4 / 5, '0.1%', 1 ),
402 array( 6 / 7 * 8, '6.8571428571%', 10 ),
405 foreach ( $pcts as $pct ) {
406 if ( !isset( $pct[2] ) ) {
409 if ( !isset( $pct[3] ) ) {
413 $this->assertEquals( wfPercent( $pct[0], $pct[2], $pct[3] ), $pct[1], $pct[1] );
418 * test @see wfShorthandToInteger()
419 * @dataProvider provideShorthand
420 * @covers ::wfShorthandToInteger
422 public function testWfShorthandToInteger( $shorthand, $expected ) {
423 $this->assertEquals( $expected,
424 wfShorthandToInteger( $shorthand )
428 /** array( shorthand, expected integer ) */
429 public static function provideShorthand() {
436 # Failures returns 0 :(
437 array( 'ABCDEFG', 0 ),
440 # Int, strings with spaces
444 array( ' 1023 ', 1023 ),
449 array( '1m', 1024 * 1024 ),
450 array( '1M', 1024 * 1024 ),
451 array( '1g', 1024 * 1024 * 1024 ),
452 array( '1G', 1024 * 1024 * 1024 ),
457 array( '-500', -500 ),
458 array( '-1k', -1024 ),
473 * @param string $old Text as it was in the database
474 * @param string $mine Text submitted while user was editing
475 * @param string $yours Text submitted by the user
476 * @param bool $expectedMergeResult Whether the merge should be a success
477 * @param string $expectedText Text after merge has been completed
479 * @dataProvider provideMerge()
483 public function testMerge( $old, $mine, $yours, $expectedMergeResult, $expectedText ) {
484 $this->checkHasDiff3();
487 $isMerged = wfMerge( $old, $mine, $yours, $mergedText );
489 $msg = 'Merge should be a ';
490 $msg .= $expectedMergeResult ?
'success' : 'failure';
491 $this->assertEquals( $expectedMergeResult, $isMerged, $msg );
494 // Verify the merged text
495 $this->assertEquals( $expectedText, $mergedText,
496 'is merged text as expected?' );
500 public static function provideMerge() {
501 $EXPECT_MERGE_SUCCESS = true;
502 $EXPECT_MERGE_FAILURE = false;
508 "one one one\n" . // trimmed
513 "one one one ONE ONE\n" .
515 "two two two\n", // with tailing whitespace
520 "two two TWO TWO", // trimmed
523 $EXPECT_MERGE_SUCCESS,
526 "one one one ONE ONE\n" .
528 "two two TWO TWO\n", // note: will always end in a newline
531 // #1: conflict, fail
534 "one one one", // trimmed
537 "one one one ONE ONE\n" .
540 "\n", // with tailing whitespace
545 "two two", // trimmed
547 $EXPECT_MERGE_FAILURE,
556 * @dataProvider provideMakeUrlIndexes()
557 * @covers ::wfMakeUrlIndexes
559 public function testMakeUrlIndexes( $url, $expected ) {
560 $index = wfMakeUrlIndexes( $url );
561 $this->assertEquals( $expected, $index, "wfMakeUrlIndexes(\"$url\")" );
564 public static function provideMakeUrlIndexes() {
568 'https://bugzilla.wikimedia.org/show_bug.cgi?id=28627',
569 array( 'https://org.wikimedia.bugzilla./show_bug.cgi?id=28627' )
572 // mailtos are handled special
573 // is this really right though? that final . probably belongs earlier?
574 'mailto:wiki@wikimedia.org',
575 array( 'mailto:org.wikimedia@wiki.' )
578 // file URL cases per bug 28627...
580 // three slashes: local filesystem path Unix-style
581 'file:///whatever/you/like.txt',
582 array( 'file://./whatever/you/like.txt' )
585 // three slashes: local filesystem path Windows-style
586 'file:///c:/whatever/you/like.txt',
587 array( 'file://./c:/whatever/you/like.txt' )
590 // two slashes: UNC filesystem path Windows-style
591 'file://intranet/whatever/you/like.txt',
592 array( 'file://intranet./whatever/you/like.txt' )
594 // Multiple-slash cases that can sorta work on Mozilla
595 // if you hack it just right are kinda pathological,
596 // and unreliable cross-platform or on IE which means they're
597 // unlikely to appear on intranets.
599 // Those will survive the algorithm but with results that
600 // are less consistent.
602 // protocol-relative URL cases per bug 29854...
604 '//bugzilla.wikimedia.org/show_bug.cgi?id=28627',
606 'http://org.wikimedia.bugzilla./show_bug.cgi?id=28627',
607 'https://org.wikimedia.bugzilla./show_bug.cgi?id=28627'
614 * @dataProvider provideWfMatchesDomainList
615 * @covers ::wfMatchesDomainList
617 public function testWfMatchesDomainList( $url, $domains, $expected, $description ) {
618 $actual = wfMatchesDomainList( $url, $domains );
619 $this->assertEquals( $expected, $actual, $description );
622 public static function provideWfMatchesDomainList() {
624 $protocols = array( 'HTTP' => 'http:', 'HTTPS' => 'https:', 'protocol-relative' => '' );
625 foreach ( $protocols as $pDesc => $p ) {
626 $a = array_merge( $a, array(
628 "$p//www.example.com",
631 "No matches for empty domains array, $pDesc URL"
634 "$p//www.example.com",
635 array( 'www.example.com' ),
637 "Exact match in domains array, $pDesc URL"
640 "$p//www.example.com",
641 array( 'example.com' ),
643 "Match without subdomain in domains array, $pDesc URL"
646 "$p//www.example2.com",
647 array( 'www.example.com', 'www.example2.com', 'www.example3.com' ),
649 "Exact match with other domains in array, $pDesc URL"
652 "$p//www.example2.com",
653 array( 'example.com', 'example2.com', 'example3,com' ),
655 "Match without subdomain with other domains in array, $pDesc URL"
658 "$p//www.example4.com",
659 array( 'example.com', 'example2.com', 'example3,com' ),
661 "Domain not in array, $pDesc URL"
664 "$p//nds-nl.wikipedia.org",
665 array( 'nl.wikipedia.org' ),
667 "Non-matching substring of domain, $pDesc URL"
676 * @covers ::wfMkdirParents
678 public function testWfMkdirParents() {
679 // Should not return true if file exists instead of directory
680 $fname = $this->getNewTempFile();
681 wfSuppressWarnings();
682 $ok = wfMkdirParents( $fname );
684 $this->assertFalse( $ok );
688 * @dataProvider provideWfShellMaintenanceCmdList
689 * @covers ::wfShellMaintenanceCmd
691 public function testWfShellMaintenanceCmd( $script, $parameters, $options,
692 $expected, $description
694 if ( wfIsWindows() ) {
695 // Approximation that's good enough for our purposes just now
696 $expected = str_replace( "'", '"', $expected );
698 $actual = wfShellMaintenanceCmd( $script, $parameters, $options );
699 $this->assertEquals( $expected, $actual, $description );
702 public static function provideWfShellMaintenanceCmdList() {
706 array( 'eval.php', array( '--help', '--test' ), array(),
707 "'$wgPhpCli' 'eval.php' '--help' '--test'",
708 "Called eval.php --help --test" ),
709 array( 'eval.php', array( '--help', '--test space' ), array( 'php' => 'php5' ),
710 "'php5' 'eval.php' '--help' '--test space'",
711 "Called eval.php --help --test with php option" ),
712 array( 'eval.php', array( '--help', '--test', 'X' ), array( 'wrapper' => 'MWScript.php' ),
713 "'$wgPhpCli' 'MWScript.php' 'eval.php' '--help' '--test' 'X'",
714 "Called eval.php --help --test with wrapper option" ),
717 array( '--help', '--test', 'y' ),
718 array( 'php' => 'php5', 'wrapper' => 'MWScript.php' ),
719 "'php5' 'MWScript.php' 'eval.php' '--help' '--test' 'y'",
720 "Called eval.php --help --test with wrapper and php option"
724 /* @todo many more! */