5 * @group GlobalFunctions
7 class GlobalTest
extends MediaWikiTestCase
{
8 protected function setUp() {
11 $readOnlyFile = $this->getNewTempFile();
12 unlink( $readOnlyFile );
14 $this->setMwGlobals( [
15 'wgReadOnlyFile' => $readOnlyFile,
21 'file://', # Non-default
27 * @dataProvider provideForWfArrayDiff2
28 * @covers ::wfArrayDiff2
30 public function testWfArrayDiff2( $a, $b, $expected ) {
32 wfArrayDiff2( $a, $b ), $expected
36 // @todo Provide more tests
37 public static function provideForWfArrayDiff2() {
46 [ [ 'a' ], [ 'a', 'b', 'c' ] ],
47 [ [ 'a' ], [ 'a', 'b' ] ],
48 [ 1 => [ 'a', 'b', 'c' ] ],
54 * Test cases for random functions could hypothetically fail,
55 * even though they shouldn't.
61 public function testRandom() {
63 wfRandom() == wfRandom()
68 * @covers ::wfRandomString
70 public function testRandomString() {
72 wfRandomString() == wfRandomString()
75 strlen( wfRandomString( 10 ) ), 10
78 preg_match( '/^[0-9a-f]+$/i', wfRandomString() ) === 1
83 * @covers ::wfUrlencode
85 public function testUrlencode() {
87 "%E7%89%B9%E5%88%A5:Contributions/Foobar",
88 wfUrlencode( "\xE7\x89\xB9\xE5\x88\xA5:Contributions/Foobar" ) );
92 * @covers ::wfExpandIRI
94 public function testExpandIRI() {
96 "https://te.wikibooks.org/wiki/ఉబుంటు_వాడుకరి_మార్గదర్శని",
97 wfExpandIRI( "https://te.wikibooks.org/wiki/"
98 . "%E0%B0%89%E0%B0%AC%E0%B1%81%E0%B0%82%E0%B0%9F%E0%B1%81_"
99 . "%E0%B0%B5%E0%B0%BE%E0%B0%A1%E0%B1%81%E0%B0%95%E0%B0%B0%E0%B0%BF_"
100 . "%E0%B0%AE%E0%B0%BE%E0%B0%B0%E0%B1%8D%E0%B0%97%E0%B0%A6%E0%B0%B0"
101 . "%E0%B1%8D%E0%B0%B6%E0%B0%A8%E0%B0%BF" ) );
105 * @covers ::wfReadOnly
107 public function testReadOnlyEmpty() {
111 $this->assertFalse( wfReadOnly() );
112 $this->assertFalse( wfReadOnly() );
116 * @covers ::wfReadOnly
118 public function testReadOnlySet() {
119 global $wgReadOnly, $wgReadOnlyFile;
121 $f = fopen( $wgReadOnlyFile, "wt" );
122 fwrite( $f, 'Message' );
124 $wgReadOnly = null; # Check on $wgReadOnlyFile
126 $this->assertTrue( wfReadOnly() );
127 $this->assertTrue( wfReadOnly() ); # Check cached
129 unlink( $wgReadOnlyFile );
130 $wgReadOnly = null; # Clean cache
132 $this->assertFalse( wfReadOnly() );
133 $this->assertFalse( wfReadOnly() );
136 public static function provideArrayToCGI() {
139 [ [ 'foo' => 'bar' ], 'foo=bar' ], // string test
140 [ [ 'foo' => '' ], 'foo=' ], // empty string test
141 [ [ 'foo' => 1 ], 'foo=1' ], // number test
142 [ [ 'foo' => true ], 'foo=1' ], // true test
143 [ [ 'foo' => false ], '' ], // false test
144 [ [ 'foo' => null ], '' ], // null test
145 [ [ 'foo' => 'A&B=5+6@!"\'' ], 'foo=A%26B%3D5%2B6%40%21%22%27' ], // urlencoding test
147 [ 'foo' => 'bar', 'baz' => 'is', 'asdf' => 'qwerty' ],
148 'foo=bar&baz=is&asdf=qwerty'
149 ], // multi-item test
150 [ [ 'foo' => [ 'bar' => 'baz' ] ], 'foo%5Bbar%5D=baz' ],
152 [ 'foo' => [ 'bar' => 'baz', 'qwerty' => 'asdf' ] ],
153 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf'
155 [ [ 'foo' => [ 'bar', 'baz' ] ], 'foo%5B0%5D=bar&foo%5B1%5D=baz' ],
157 [ 'foo' => [ 'bar' => [ 'bar' => 'baz' ] ] ],
158 'foo%5Bbar%5D%5Bbar%5D=baz'
164 * @dataProvider provideArrayToCGI
165 * @covers ::wfArrayToCgi
167 public function testArrayToCGI( $array, $result ) {
168 $this->assertEquals( $result, wfArrayToCgi( $array ) );
172 * @covers ::wfArrayToCgi
174 public function testArrayToCGI2() {
179 [ 'foo' => 'bar', 'baz' => 'overridden value' ] ) );
182 public static function provideCgiToArray() {
185 [ 'foo=bar', [ 'foo' => 'bar' ] ], // string
186 [ 'foo=', [ 'foo' => '' ] ], // empty string
187 [ 'foo', [ 'foo' => '' ] ], // missing =
188 [ 'foo=bar&qwerty=asdf', [ 'foo' => 'bar', 'qwerty' => 'asdf' ] ], // multiple value
189 [ 'foo=A%26B%3D5%2B6%40%21%22%27', [ 'foo' => 'A&B=5+6@!"\'' ] ], // urldecoding test
190 [ 'foo%5Bbar%5D=baz', [ 'foo' => [ 'bar' => 'baz' ] ] ],
192 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf',
193 [ 'foo' => [ 'bar' => 'baz', 'qwerty' => 'asdf' ] ]
195 [ 'foo%5B0%5D=bar&foo%5B1%5D=baz', [ 'foo' => [ 0 => 'bar', 1 => 'baz' ] ] ],
197 'foo%5Bbar%5D%5Bbar%5D=baz',
198 [ 'foo' => [ 'bar' => [ 'bar' => 'baz' ] ] ]
204 * @dataProvider provideCgiToArray
205 * @covers ::wfCgiToArray
207 public function testCgiToArray( $cgi, $result ) {
208 $this->assertEquals( $result, wfCgiToArray( $cgi ) );
211 public static function provideCgiRoundTrip() {
216 [ 'foo=bar&baz=biz' ],
217 [ 'foo=A%26B%3D5%2B6%40%21%22%27' ],
218 [ 'foo%5Bbar%5D=baz' ],
219 [ 'foo%5B0%5D=bar&foo%5B1%5D=baz' ],
220 [ 'foo%5Bbar%5D%5Bbar%5D=baz' ],
225 * @dataProvider provideCgiRoundTrip
226 * @covers ::wfArrayToCgi
228 public function testCgiRoundTrip( $cgi ) {
229 $this->assertEquals( $cgi, wfArrayToCgi( wfCgiToArray( $cgi ) ) );
233 * @covers ::mimeTypeMatch
235 public function testMimeTypeMatch() {
238 mimeTypeMatch( 'text/html',
239 [ 'application/xhtml+xml' => 1.0,
241 'text/plain' => 0.3 ] ) );
244 mimeTypeMatch( 'text/html',
246 'text/*' => 0.5 ] ) );
249 mimeTypeMatch( 'text/html',
250 [ '*/*' => 1.0 ] ) );
252 mimeTypeMatch( 'text/html',
253 [ 'image/png' => 1.0,
254 'image/svg+xml' => 0.5 ] ) );
258 * @covers ::wfNegotiateType
260 public function testNegotiateType() {
264 [ 'application/xhtml+xml' => 1.0,
268 [ 'text/html' => 1.0 ] ) );
270 'application/xhtml+xml',
272 [ 'application/xhtml+xml' => 1.0,
276 [ 'application/xhtml+xml' => 1.0,
277 'text/html' => 0.5 ] ) );
281 [ 'text/html' => 1.0,
284 'application/xhtml+xml' => 0.2 ],
285 [ 'application/xhtml+xml' => 1.0,
286 'text/html' => 0.5 ] ) );
293 [ 'application/xhtml+xml' => 1.0,
294 'text/html' => 0.5 ] ) );
298 [ 'application/xhtml+xml' => 1.0 ] ) );
303 * @covers ::wfDebugMem
305 public function testDebugFunctionTest() {
306 $debugLogFile = $this->getNewTempFile();
308 $this->setMwGlobals( [
309 'wgDebugLogFile' => $debugLogFile,
310 # @todo FIXME: $wgDebugTimestamps should be tested
311 'wgDebugTimestamps' => false
314 wfDebug( "This is a normal string" );
315 $this->assertEquals( "This is a normal string\n", file_get_contents( $debugLogFile ) );
316 unlink( $debugLogFile );
318 wfDebug( "This is nöt an ASCII string" );
319 $this->assertEquals( "This is nöt an ASCII string\n", file_get_contents( $debugLogFile ) );
320 unlink( $debugLogFile );
322 wfDebug( "\00305This has böth UTF and control chars\003" );
324 " 05This has böth UTF and control chars \n",
325 file_get_contents( $debugLogFile )
327 unlink( $debugLogFile );
330 $this->assertGreaterThan(
332 preg_replace( '/\D/', '', file_get_contents( $debugLogFile ) )
334 unlink( $debugLogFile );
337 $this->assertGreaterThan(
339 preg_replace( '/\D/', '', file_get_contents( $debugLogFile ) )
341 unlink( $debugLogFile );
345 * @covers ::wfClientAcceptsGzip
347 public function testClientAcceptsGzipTest() {
353 'compress, gzip' => true,
354 'gzip;q=1.0' => true,
357 'gzip;q=abcde' => true, // is this REALLY valid?
358 'gzip;q=12345678.9' => true,
362 if ( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) {
363 $old_server_setting = $_SERVER['HTTP_ACCEPT_ENCODING'];
366 foreach ( $settings as $encoding => $expect ) {
367 $_SERVER['HTTP_ACCEPT_ENCODING'] = $encoding;
369 $this->assertEquals( $expect, wfClientAcceptsGzip( true ),
370 "'$encoding' => " . wfBoolToStr( $expect ) );
373 if ( isset( $old_server_setting ) ) {
374 $_SERVER['HTTP_ACCEPT_ENCODING'] = $old_server_setting;
379 * @covers ::wfPercent
381 public function testWfPercentTest() {
384 [ 6 / 7, '0.86%', 2, false ],
386 [ 22 / 7, '3.14286%', 5 ],
389 [ 10 / 3, '0%', -1 ],
390 [ 3 / 4 / 5, '0.1%', 1 ],
391 [ 6 / 7 * 8, '6.8571428571%', 10 ],
394 foreach ( $pcts as $pct ) {
395 if ( !isset( $pct[2] ) ) {
398 if ( !isset( $pct[3] ) ) {
402 $this->assertEquals( wfPercent( $pct[0], $pct[2], $pct[3] ), $pct[1], $pct[1] );
407 * test @see wfShorthandToInteger()
408 * @dataProvider provideShorthand
409 * @covers ::wfShorthandToInteger
411 public function testWfShorthandToInteger( $shorthand, $expected ) {
412 $this->assertEquals( $expected,
413 wfShorthandToInteger( $shorthand )
417 public static function provideShorthand() {
418 // Syntax: [ shorthand, expected integer ]
425 # Failures returns 0 :(
429 # Int, strings with spaces
438 [ '1m', 1024 * 1024 ],
439 [ '1M', 1024 * 1024 ],
440 [ '1g', 1024 * 1024 * 1024 ],
441 [ '1G', 1024 * 1024 * 1024 ],
462 * @param string $old Text as it was in the database
463 * @param string $mine Text submitted while user was editing
464 * @param string $yours Text submitted by the user
465 * @param bool $expectedMergeResult Whether the merge should be a success
466 * @param string $expectedText Text after merge has been completed
468 * @dataProvider provideMerge()
472 public function testMerge( $old, $mine, $yours, $expectedMergeResult, $expectedText ) {
473 $this->markTestSkippedIfNoDiff3();
476 $isMerged = wfMerge( $old, $mine, $yours, $mergedText );
478 $msg = 'Merge should be a ';
479 $msg .= $expectedMergeResult ?
'success' : 'failure';
480 $this->assertEquals( $expectedMergeResult, $isMerged, $msg );
483 // Verify the merged text
484 $this->assertEquals( $expectedText, $mergedText,
485 'is merged text as expected?' );
489 public static function provideMerge() {
490 $EXPECT_MERGE_SUCCESS = true;
491 $EXPECT_MERGE_FAILURE = false;
497 "one one one\n" . // trimmed
502 "one one one ONE ONE\n" .
504 "two two two\n", // with tailing whitespace
509 "two two TWO TWO", // trimmed
512 $EXPECT_MERGE_SUCCESS,
515 "one one one ONE ONE\n" .
517 "two two TWO TWO\n", // note: will always end in a newline
520 // #1: conflict, fail
523 "one one one", // trimmed
526 "one one one ONE ONE\n" .
529 "\n", // with tailing whitespace
534 "two two", // trimmed
536 $EXPECT_MERGE_FAILURE,
545 * @dataProvider provideMakeUrlIndexes()
546 * @covers ::wfMakeUrlIndexes
548 public function testMakeUrlIndexes( $url, $expected ) {
549 $index = wfMakeUrlIndexes( $url );
550 $this->assertEquals( $expected, $index, "wfMakeUrlIndexes(\"$url\")" );
553 public static function provideMakeUrlIndexes() {
555 // Testcase for T30627
557 'https://example.org/test.cgi?id=12345',
558 [ 'https://org.example./test.cgi?id=12345' ]
561 // mailtos are handled special
562 // is this really right though? that final . probably belongs earlier?
563 'mailto:wiki@wikimedia.org',
564 [ 'mailto:org.wikimedia@wiki.' ]
567 // file URL cases per T30627...
569 // three slashes: local filesystem path Unix-style
570 'file:///whatever/you/like.txt',
571 [ 'file://./whatever/you/like.txt' ]
574 // three slashes: local filesystem path Windows-style
575 'file:///c:/whatever/you/like.txt',
576 [ 'file://./c:/whatever/you/like.txt' ]
579 // two slashes: UNC filesystem path Windows-style
580 'file://intranet/whatever/you/like.txt',
581 [ 'file://intranet./whatever/you/like.txt' ]
583 // Multiple-slash cases that can sorta work on Mozilla
584 // if you hack it just right are kinda pathological,
585 // and unreliable cross-platform or on IE which means they're
586 // unlikely to appear on intranets.
587 // Those will survive the algorithm but with results that
588 // are less consistent.
590 // protocol-relative URL cases per T31854...
592 '//example.org/test.cgi?id=12345',
594 'http://org.example./test.cgi?id=12345',
595 'https://org.example./test.cgi?id=12345'
602 * @dataProvider provideWfMatchesDomainList
603 * @covers ::wfMatchesDomainList
605 public function testWfMatchesDomainList( $url, $domains, $expected, $description ) {
606 $actual = wfMatchesDomainList( $url, $domains );
607 $this->assertEquals( $expected, $actual, $description );
610 public static function provideWfMatchesDomainList() {
612 $protocols = [ 'HTTP' => 'http:', 'HTTPS' => 'https:', 'protocol-relative' => '' ];
613 foreach ( $protocols as $pDesc => $p ) {
614 $a = array_merge( $a, [
616 "$p//www.example.com",
619 "No matches for empty domains array, $pDesc URL"
622 "$p//www.example.com",
623 [ 'www.example.com' ],
625 "Exact match in domains array, $pDesc URL"
628 "$p//www.example.com",
631 "Match without subdomain in domains array, $pDesc URL"
634 "$p//www.example2.com",
635 [ 'www.example.com', 'www.example2.com', 'www.example3.com' ],
637 "Exact match with other domains in array, $pDesc URL"
640 "$p//www.example2.com",
641 [ 'example.com', 'example2.com', 'example3,com' ],
643 "Match without subdomain with other domains in array, $pDesc URL"
646 "$p//www.example4.com",
647 [ 'example.com', 'example2.com', 'example3,com' ],
649 "Domain not in array, $pDesc URL"
652 "$p//nds-nl.wikipedia.org",
653 [ 'nl.wikipedia.org' ],
655 "Non-matching substring of domain, $pDesc URL"
664 * @covers ::wfMkdirParents
666 public function testWfMkdirParents() {
667 // Should not return true if file exists instead of directory
668 $fname = $this->getNewTempFile();
669 MediaWiki\
suppressWarnings();
670 $ok = wfMkdirParents( $fname );
671 MediaWiki\restoreWarnings
();
672 $this->assertFalse( $ok );
676 * @dataProvider provideWfShellWikiCmdList
677 * @covers ::wfShellWikiCmd
679 public function testWfShellWikiCmd( $script, $parameters, $options,
680 $expected, $description
682 if ( wfIsWindows() ) {
683 // Approximation that's good enough for our purposes just now
684 $expected = str_replace( "'", '"', $expected );
686 $actual = wfShellWikiCmd( $script, $parameters, $options );
687 $this->assertEquals( $expected, $actual, $description );
690 public function wfWikiID() {
691 $this->setMwGlobals( [
692 'wgDBname' => 'example',
700 $this->setMwGlobals( [
701 'wgDBname' => 'example',
702 'wgDBprefix' => 'mw_',
710 public function testWfMemcKey() {
711 $cache = ObjectCache
::getLocalClusterInstance();
713 $cache->makeKey( 'foo', 123, 'bar' ),
714 wfMemcKey( 'foo', 123, 'bar' )
718 public function testWfForeignMemcKey() {
719 $cache = ObjectCache
::getLocalClusterInstance();
720 $keyspace = $this->readAttribute( $cache, 'keyspace' );
722 wfForeignMemcKey( $keyspace, '', 'foo', 'bar' ),
723 $cache->makeKey( 'foo', 'bar' )
727 public function testWfGlobalCacheKey() {
728 $cache = ObjectCache
::getLocalClusterInstance();
730 $cache->makeGlobalKey( 'foo', 123, 'bar' ),
731 wfGlobalCacheKey( 'foo', 123, 'bar' )
735 public static function provideWfShellWikiCmdList() {
739 [ 'eval.php', [ '--help', '--test' ], [],
740 "'$wgPhpCli' 'eval.php' '--help' '--test'",
741 "Called eval.php --help --test" ],
742 [ 'eval.php', [ '--help', '--test space' ], [ 'php' => 'php5' ],
743 "'php5' 'eval.php' '--help' '--test space'",
744 "Called eval.php --help --test with php option" ],
745 [ 'eval.php', [ '--help', '--test', 'X' ], [ 'wrapper' => 'MWScript.php' ],
746 "'$wgPhpCli' 'MWScript.php' 'eval.php' '--help' '--test' 'X'",
747 "Called eval.php --help --test with wrapper option" ],
750 [ '--help', '--test', 'y' ],
751 [ 'php' => 'php5', 'wrapper' => 'MWScript.php' ],
752 "'php5' 'MWScript.php' 'eval.php' '--help' '--test' 'y'",
753 "Called eval.php --help --test with wrapper and php option"
757 /* @todo many more! */