4 * @group GlobalFunctions
6 class GlobalTest
extends MediaWikiTestCase
{
7 protected function setUp() {
10 $readOnlyFile = $this->getNewTempFile();
11 unlink( $readOnlyFile );
13 $this->setMwGlobals( [
14 'wgReadOnlyFile' => $readOnlyFile,
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 [ [ 'a' ], [ 'a', 'b', 'c' ] ],
46 [ [ 'a' ], [ 'a', 'b' ] ],
47 [ 1 => [ '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() {
138 [ [ 'foo' => 'bar' ], 'foo=bar' ], // string test
139 [ [ 'foo' => '' ], 'foo=' ], // empty string test
140 [ [ 'foo' => 1 ], 'foo=1' ], // number test
141 [ [ 'foo' => true ], 'foo=1' ], // true test
142 [ [ 'foo' => false ], '' ], // false test
143 [ [ 'foo' => null ], '' ], // null test
144 [ [ 'foo' => 'A&B=5+6@!"\'' ], 'foo=A%26B%3D5%2B6%40%21%22%27' ], // urlencoding test
146 [ 'foo' => 'bar', 'baz' => 'is', 'asdf' => 'qwerty' ],
147 'foo=bar&baz=is&asdf=qwerty'
148 ], // multi-item test
149 [ [ 'foo' => [ 'bar' => 'baz' ] ], 'foo%5Bbar%5D=baz' ],
151 [ 'foo' => [ 'bar' => 'baz', 'qwerty' => 'asdf' ] ],
152 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf'
154 [ [ 'foo' => [ 'bar', 'baz' ] ], 'foo%5B0%5D=bar&foo%5B1%5D=baz' ],
156 [ 'foo' => [ 'bar' => [ '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() {
178 [ 'foo' => 'bar', 'baz' => 'overridden value' ] ) );
181 public static function provideCgiToArray() {
184 [ 'foo=bar', [ 'foo' => 'bar' ] ], // string
185 [ 'foo=', [ 'foo' => '' ] ], // empty string
186 [ 'foo', [ 'foo' => '' ] ], // missing =
187 [ 'foo=bar&qwerty=asdf', [ 'foo' => 'bar', 'qwerty' => 'asdf' ] ], // multiple value
188 [ 'foo=A%26B%3D5%2B6%40%21%22%27', [ 'foo' => 'A&B=5+6@!"\'' ] ], // urldecoding test
189 [ 'foo%5Bbar%5D=baz', [ 'foo' => [ 'bar' => 'baz' ] ] ],
191 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf',
192 [ 'foo' => [ 'bar' => 'baz', 'qwerty' => 'asdf' ] ]
194 [ 'foo%5B0%5D=bar&foo%5B1%5D=baz', [ 'foo' => [ 0 => 'bar', 1 => 'baz' ] ] ],
196 'foo%5Bbar%5D%5Bbar%5D=baz',
197 [ 'foo' => [ 'bar' => [ '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 [ 'foo=bar&baz=biz' ],
216 [ 'foo=A%26B%3D5%2B6%40%21%22%27' ],
217 [ 'foo%5Bbar%5D=baz' ],
218 [ 'foo%5B0%5D=bar&foo%5B1%5D=baz' ],
219 [ '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 [ 'application/xhtml+xml' => 1.0,
240 'text/plain' => 0.3 ] ) );
243 mimeTypeMatch( 'text/html',
245 'text/*' => 0.5 ] ) );
248 mimeTypeMatch( 'text/html',
249 [ '*/*' => 1.0 ] ) );
251 mimeTypeMatch( 'text/html',
252 [ 'image/png' => 1.0,
253 'image/svg+xml' => 0.5 ] ) );
257 * @covers ::wfNegotiateType
259 public function testNegotiateType() {
263 [ 'application/xhtml+xml' => 1.0,
267 [ 'text/html' => 1.0 ] ) );
269 'application/xhtml+xml',
271 [ 'application/xhtml+xml' => 1.0,
275 [ 'application/xhtml+xml' => 1.0,
276 'text/html' => 0.5 ] ) );
280 [ 'text/html' => 1.0,
283 'application/xhtml+xml' => 0.2 ],
284 [ 'application/xhtml+xml' => 1.0,
285 'text/html' => 0.5 ] ) );
292 [ 'application/xhtml+xml' => 1.0,
293 'text/html' => 0.5 ] ) );
297 [ 'application/xhtml+xml' => 1.0 ] ) );
302 * @covers ::wfDebugMem
304 public function testDebugFunctionTest() {
305 $debugLogFile = $this->getNewTempFile();
307 $this->setMwGlobals( [
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;
378 * @covers ::wfPercent
380 public function testWfPercentTest() {
383 [ 6 / 7, '0.86%', 2, false ],
385 [ 22 / 7, '3.14286%', 5 ],
388 [ 10 / 3, '0%', -1 ],
389 [ 3 / 4 / 5, '0.1%', 1 ],
390 [ 6 / 7 * 8, '6.8571428571%', 10 ],
393 foreach ( $pcts as $pct ) {
394 if ( !isset( $pct[2] ) ) {
397 if ( !isset( $pct[3] ) ) {
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 public static function provideShorthand() {
417 // Syntax: [ shorthand, expected integer ]
424 # Failures returns 0 :(
428 # Int, strings with spaces
437 [ '1m', 1024 * 1024 ],
438 [ '1M', 1024 * 1024 ],
439 [ '1g', 1024 * 1024 * 1024 ],
440 [ '1G', 1024 * 1024 * 1024 ],
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()
471 public function testMerge( $old, $mine, $yours, $expectedMergeResult, $expectedText ) {
472 $this->markTestSkippedIfNoDiff3();
475 $isMerged = wfMerge( $old, $mine, $yours, $mergedText );
477 $msg = 'Merge should be a ';
478 $msg .= $expectedMergeResult ?
'success' : 'failure';
479 $this->assertEquals( $expectedMergeResult, $isMerged, $msg );
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;
496 "one one one\n" . // trimmed
501 "one one one ONE ONE\n" .
503 "two two two\n", // with tailing whitespace
508 "two two TWO TWO", // trimmed
511 $EXPECT_MERGE_SUCCESS,
514 "one one one ONE ONE\n" .
516 "two two TWO TWO\n", // note: will always end in a newline
519 // #1: conflict, fail
522 "one one one", // trimmed
525 "one one one ONE ONE\n" .
528 "\n", // with tailing whitespace
533 "two two", // trimmed
535 $EXPECT_MERGE_FAILURE,
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() {
554 // Testcase for T30627
556 'https://example.org/test.cgi?id=12345',
557 [ 'https://org.example./test.cgi?id=12345' ]
560 // mailtos are handled special
561 // is this really right though? that final . probably belongs earlier?
562 'mailto:wiki@wikimedia.org',
563 [ 'mailto:org.wikimedia@wiki.' ]
566 // file URL cases per T30627...
568 // three slashes: local filesystem path Unix-style
569 'file:///whatever/you/like.txt',
570 [ 'file://./whatever/you/like.txt' ]
573 // three slashes: local filesystem path Windows-style
574 'file:///c:/whatever/you/like.txt',
575 [ 'file://./c:/whatever/you/like.txt' ]
578 // two slashes: UNC filesystem path Windows-style
579 'file://intranet/whatever/you/like.txt',
580 [ '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...
591 '//example.org/test.cgi?id=12345',
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() {
611 $protocols = [ 'HTTP' => 'http:', 'HTTPS' => 'https:', 'protocol-relative' => '' ];
612 foreach ( $protocols as $pDesc => $p ) {
613 $a = array_merge( $a, [
615 "$p//www.example.com",
618 "No matches for empty domains array, $pDesc URL"
621 "$p//www.example.com",
622 [ 'www.example.com' ],
624 "Exact match in domains array, $pDesc URL"
627 "$p//www.example.com",
630 "Match without subdomain in domains array, $pDesc URL"
633 "$p//www.example2.com",
634 [ 'www.example.com', 'www.example2.com', 'www.example3.com' ],
636 "Exact match with other domains in array, $pDesc URL"
639 "$p//www.example2.com",
640 [ 'example.com', 'example2.com', 'example3,com' ],
642 "Match without subdomain with other domains in array, $pDesc URL"
645 "$p//www.example4.com",
646 [ 'example.com', 'example2.com', 'example3,com' ],
648 "Domain not in array, $pDesc URL"
651 "$p//nds-nl.wikipedia.org",
652 [ 'nl.wikipedia.org' ],
654 "Non-matching substring of domain, $pDesc URL"
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( [
691 'wgDBname' => 'example',
699 $this->setMwGlobals( [
700 'wgDBname' => 'example',
701 'wgDBprefix' => 'mw_',
709 public function testWfMemcKey() {
710 $cache = ObjectCache
::getLocalClusterInstance();
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' );
721 wfForeignMemcKey( $keyspace, '', 'foo', 'bar' ),
722 $cache->makeKey( 'foo', 'bar' )
726 public function testWfGlobalCacheKey() {
727 $cache = ObjectCache
::getLocalClusterInstance();
729 $cache->makeGlobalKey( 'foo', 123, 'bar' ),
730 wfGlobalCacheKey( 'foo', 123, 'bar' )
734 public static function provideWfShellWikiCmdList() {
738 [ 'eval.php', [ '--help', '--test' ], [],
739 "'$wgPhpCli' 'eval.php' '--help' '--test'",
740 "Called eval.php --help --test" ],
741 [ 'eval.php', [ '--help', '--test space' ], [ 'php' => 'php5' ],
742 "'php5' 'eval.php' '--help' '--test space'",
743 "Called eval.php --help --test with php option" ],
744 [ 'eval.php', [ '--help', '--test', 'X' ], [ 'wrapper' => 'MWScript.php' ],
745 "'$wgPhpCli' 'MWScript.php' 'eval.php' '--help' '--test' 'X'",
746 "Called eval.php --help --test with wrapper option" ],
749 [ '--help', '--test', 'y' ],
750 [ '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! */