3 use MediaWiki\Logger\LegacyLogger
;
7 * @group GlobalFunctions
9 class GlobalTest
extends MediaWikiTestCase
{
10 protected function setUp() {
13 $readOnlyFile = $this->getNewTempFile();
14 unlink( $readOnlyFile );
16 $this->setMwGlobals( [
18 'wgReadOnlyFile' => $readOnlyFile,
24 'file://', # Non-default
30 * @dataProvider provideForWfArrayDiff2
31 * @covers ::wfArrayDiff2
33 public function testWfArrayDiff2( $a, $b, $expected ) {
35 wfArrayDiff2( $a, $b ), $expected
39 // @todo Provide more tests
40 public static function provideForWfArrayDiff2() {
49 [ [ 'a' ], [ 'a', 'b', 'c' ] ],
50 [ [ 'a' ], [ 'a', 'b' ] ],
51 [ 1 => [ 'a', 'b', 'c' ] ],
57 * Test cases for random functions could hypothetically fail,
58 * even though they shouldn't.
64 public function testRandom() {
66 wfRandom() == wfRandom()
71 * @covers ::wfRandomString
73 public function testRandomString() {
75 wfRandomString() == wfRandomString()
77 $this->assertSame( 10, strlen( wfRandomString( 10 ) ), 'length' );
78 $this->assertSame( 1, preg_match( '/^[0-9a-f]+$/i', wfRandomString() ), 'pattern' );
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 * Intended to cover the relevant bits of ServiceWiring.php, as well as GlobalFunctions.php
105 * @covers ::wfReadOnly
107 public function testReadOnlyEmpty() {
108 $this->assertFalse( wfReadOnly() );
109 $this->assertFalse( wfReadOnly() );
113 * Intended to cover the relevant bits of ServiceWiring.php, as well as GlobalFunctions.php
114 * @covers ::wfReadOnly
116 public function testReadOnlySet() {
117 global $wgReadOnlyFile;
119 $f = fopen( $wgReadOnlyFile, "wt" );
120 fwrite( $f, 'Message' );
123 // Reset the service to avoid cached results
124 $this->overrideMwServices();
126 $this->assertTrue( wfReadOnly() );
127 $this->assertTrue( wfReadOnly() ); # Check cached
131 * This behaviour could probably be deprecated. Several extensions rely on it as of 1.29.
132 * @covers ::wfReadOnlyReason
134 public function testReadOnlyGlobalChange() {
135 $this->assertFalse( wfReadOnlyReason() );
137 $this->setMwGlobals( [
138 'wgReadOnly' => 'reason'
140 $this->overrideMwServices();
142 $this->assertSame( 'reason', wfReadOnlyReason() );
145 public static function provideArrayToCGI() {
148 [ [ 'foo' => 'bar' ], 'foo=bar' ], // string test
149 [ [ 'foo' => '' ], 'foo=' ], // empty string test
150 [ [ 'foo' => 1 ], 'foo=1' ], // number test
151 [ [ 'foo' => true ], 'foo=1' ], // true test
152 [ [ 'foo' => false ], '' ], // false test
153 [ [ 'foo' => null ], '' ], // null test
154 [ [ 'foo' => 'A&B=5+6@!"\'' ], 'foo=A%26B%3D5%2B6%40%21%22%27' ], // urlencoding test
156 [ 'foo' => 'bar', 'baz' => 'is', 'asdf' => 'qwerty' ],
157 'foo=bar&baz=is&asdf=qwerty'
158 ], // multi-item test
159 [ [ 'foo' => [ 'bar' => 'baz' ] ], 'foo%5Bbar%5D=baz' ],
161 [ 'foo' => [ 'bar' => 'baz', 'qwerty' => 'asdf' ] ],
162 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf'
164 [ [ 'foo' => [ 'bar', 'baz' ] ], 'foo%5B0%5D=bar&foo%5B1%5D=baz' ],
166 [ 'foo' => [ 'bar' => [ '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() {
188 [ 'foo' => 'bar', 'baz' => 'overridden value' ] ) );
191 public static function provideCgiToArray() {
194 [ 'foo=bar', [ 'foo' => 'bar' ] ], // string
195 [ 'foo=', [ 'foo' => '' ] ], // empty string
196 [ 'foo', [ 'foo' => '' ] ], // missing =
197 [ 'foo=bar&qwerty=asdf', [ 'foo' => 'bar', 'qwerty' => 'asdf' ] ], // multiple value
198 [ 'foo=A%26B%3D5%2B6%40%21%22%27', [ 'foo' => 'A&B=5+6@!"\'' ] ], // urldecoding test
199 [ 'foo%5Bbar%5D=baz', [ 'foo' => [ 'bar' => 'baz' ] ] ],
201 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf',
202 [ 'foo' => [ 'bar' => 'baz', 'qwerty' => 'asdf' ] ]
204 [ 'foo%5B0%5D=bar&foo%5B1%5D=baz', [ 'foo' => [ 0 => 'bar', 1 => 'baz' ] ] ],
206 'foo%5Bbar%5D%5Bbar%5D=baz',
207 [ 'foo' => [ 'bar' => [ '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 [ 'foo=bar&baz=biz' ],
226 [ 'foo=A%26B%3D5%2B6%40%21%22%27' ],
227 [ 'foo%5Bbar%5D=baz' ],
228 [ 'foo%5B0%5D=bar&foo%5B1%5D=baz' ],
229 [ '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 [ 'application/xhtml+xml' => 1.0,
250 'text/plain' => 0.3 ] ) );
253 mimeTypeMatch( 'text/html',
255 'text/*' => 0.5 ] ) );
258 mimeTypeMatch( 'text/html',
259 [ '*/*' => 1.0 ] ) );
261 mimeTypeMatch( 'text/html',
262 [ 'image/png' => 1.0,
263 'image/svg+xml' => 0.5 ] ) );
267 * @covers ::wfNegotiateType
269 public function testNegotiateType() {
273 [ 'application/xhtml+xml' => 1.0,
277 [ 'text/html' => 1.0 ] ) );
279 'application/xhtml+xml',
281 [ 'application/xhtml+xml' => 1.0,
285 [ 'application/xhtml+xml' => 1.0,
286 'text/html' => 0.5 ] ) );
290 [ 'text/html' => 1.0,
293 'application/xhtml+xml' => 0.2 ],
294 [ 'application/xhtml+xml' => 1.0,
295 'text/html' => 0.5 ] ) );
302 [ 'application/xhtml+xml' => 1.0,
303 'text/html' => 0.5 ] ) );
307 [ 'application/xhtml+xml' => 1.0 ] ) );
312 * @covers ::wfDebugMem
314 public function testDebugFunctionTest() {
315 $debugLogFile = $this->getNewTempFile();
317 $this->setMwGlobals( [
318 'wgDebugLogFile' => $debugLogFile,
319 # @todo FIXME: $wgDebugTimestamps should be tested
320 'wgDebugTimestamps' => false,
322 $this->setLogger( 'wfDebug', new LegacyLogger( 'wfDebug' ) );
324 wfDebug( "This is a normal string" );
325 $this->assertEquals( "This is a normal string\n", file_get_contents( $debugLogFile ) );
326 unlink( $debugLogFile );
328 wfDebug( "This is nöt an ASCII string" );
329 $this->assertEquals( "This is nöt an ASCII string\n", file_get_contents( $debugLogFile ) );
330 unlink( $debugLogFile );
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( $debugLogFile )
337 unlink( $debugLogFile );
340 $this->assertGreaterThan(
342 preg_replace( '/\D/', '', file_get_contents( $debugLogFile ) )
344 unlink( $debugLogFile );
347 $this->assertGreaterThan(
349 preg_replace( '/\D/', '', file_get_contents( $debugLogFile ) )
351 unlink( $debugLogFile );
355 * @covers ::wfClientAcceptsGzip
357 public function testClientAcceptsGzipTest() {
362 'compress, gzip' => true,
363 'gzip;q=1.0' => true,
366 'gzip;q=abcde' => true, // is this REALLY valid?
367 'gzip;q=12345678.9' => true,
371 if ( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) {
372 $old_server_setting = $_SERVER['HTTP_ACCEPT_ENCODING'];
375 foreach ( $settings as $encoding => $expect ) {
376 $_SERVER['HTTP_ACCEPT_ENCODING'] = $encoding;
378 $this->assertEquals( $expect, wfClientAcceptsGzip( true ),
379 "'$encoding' => " . wfBoolToStr( $expect ) );
382 if ( isset( $old_server_setting ) ) {
383 $_SERVER['HTTP_ACCEPT_ENCODING'] = $old_server_setting;
388 * @covers ::wfPercent
390 public function testWfPercentTest() {
392 [ 6 / 7, '0.86%', 2, false ],
394 [ 22 / 7, '3.14286%', 5 ],
397 [ 10 / 3, '0%', -1 ],
398 [ 3 / 4 / 5, '0.1%', 1 ],
399 [ 6 / 7 * 8, '6.8571428571%', 10 ],
402 foreach ( $pcts as $pct ) {
403 if ( !isset( $pct[2] ) ) {
406 if ( !isset( $pct[3] ) ) {
410 $this->assertEquals( wfPercent( $pct[0], $pct[2], $pct[3] ), $pct[1], $pct[1] );
415 * test @see wfShorthandToInteger()
416 * @dataProvider provideShorthand
417 * @covers ::wfShorthandToInteger
419 public function testWfShorthandToInteger( $shorthand, $expected ) {
420 $this->assertEquals( $expected,
421 wfShorthandToInteger( $shorthand )
425 public static function provideShorthand() {
426 // Syntax: [ shorthand, expected integer ]
433 # Failures returns 0 :(
437 # Int, strings with spaces
446 [ '1m', 1024 * 1024 ],
447 [ '1M', 1024 * 1024 ],
448 [ '1g', 1024 * 1024 * 1024 ],
449 [ '1G', 1024 * 1024 * 1024 ],
472 public function testMerge_worksWithLessParameters() {
473 $this->markTestSkippedIfNoDiff3();
476 $successfulMerge = wfMerge( "old1\n\nold2", "old1\n\nnew2", "new1\n\nold2", $mergedText );
479 $conflictingMerge = wfMerge( 'old', 'old and mine', 'old and yours', $mergedText );
481 $this->assertEquals( true, $successfulMerge );
482 $this->assertEquals( false, $conflictingMerge );
486 * @param string $old Text as it was in the database
487 * @param string $mine Text submitted while user was editing
488 * @param string $yours Text submitted by the user
489 * @param bool $expectedMergeResult Whether the merge should be a success
490 * @param string $expectedText Text after merge has been completed
491 * @param string $expectedMergeAttemptResult Diff3 output if conflicts occur
493 * @dataProvider provideMerge()
497 public function testMerge( $old, $mine, $yours, $expectedMergeResult, $expectedText,
498 $expectedMergeAttemptResult ) {
499 $this->markTestSkippedIfNoDiff3();
502 $attemptMergeResult = null;
503 $isMerged = wfMerge( $old, $mine, $yours, $mergedText, $mergeAttemptResult );
505 $msg = 'Merge should be a ';
506 $msg .= $expectedMergeResult ?
'success' : 'failure';
507 $this->assertEquals( $expectedMergeResult, $isMerged, $msg );
508 $this->assertEquals( $expectedMergeAttemptResult, $mergeAttemptResult );
511 // Verify the merged text
512 $this->assertEquals( $expectedText, $mergedText,
513 'is merged text as expected?' );
517 public static function provideMerge() {
518 $EXPECT_MERGE_SUCCESS = true;
519 $EXPECT_MERGE_FAILURE = false;
525 "one one one\n" . // trimmed
530 "one one one ONE ONE\n" .
532 "two two two\n", // with tailing whitespace
537 "two two TWO TWO", // trimmed
540 $EXPECT_MERGE_SUCCESS,
543 "one one one ONE ONE\n" .
545 "two two TWO TWO\n", // note: will always end in a newline
547 // mergeAttemptResult:
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,
572 // mergeAttemptResult:
583 * @dataProvider provideWfMatchesDomainList
584 * @covers ::wfMatchesDomainList
586 public function testWfMatchesDomainList( $url, $domains, $expected, $description ) {
587 $actual = wfMatchesDomainList( $url, $domains );
588 $this->assertEquals( $expected, $actual, $description );
591 public static function provideWfMatchesDomainList() {
593 $protocols = [ 'HTTP' => 'http:', 'HTTPS' => 'https:', 'protocol-relative' => '' ];
594 foreach ( $protocols as $pDesc => $p ) {
595 $a = array_merge( $a, [
597 "$p//www.example.com",
600 "No matches for empty domains array, $pDesc URL"
603 "$p//www.example.com",
604 [ 'www.example.com' ],
606 "Exact match in domains array, $pDesc URL"
609 "$p//www.example.com",
612 "Match without subdomain in domains array, $pDesc URL"
615 "$p//www.example2.com",
616 [ 'www.example.com', 'www.example2.com', 'www.example3.com' ],
618 "Exact match with other domains in array, $pDesc URL"
621 "$p//www.example2.com",
622 [ 'example.com', 'example2.com', 'example3,com' ],
624 "Match without subdomain with other domains in array, $pDesc URL"
627 "$p//www.example4.com",
628 [ 'example.com', 'example2.com', 'example3,com' ],
630 "Domain not in array, $pDesc URL"
633 "$p//nds-nl.wikipedia.org",
634 [ 'nl.wikipedia.org' ],
636 "Non-matching substring of domain, $pDesc URL"
645 * @covers ::wfMkdirParents
647 public function testWfMkdirParents() {
648 // Should not return true if file exists instead of directory
649 $fname = $this->getNewTempFile();
650 Wikimedia\
suppressWarnings();
651 $ok = wfMkdirParents( $fname );
652 Wikimedia\restoreWarnings
();
653 $this->assertFalse( $ok );
657 * @dataProvider provideWfShellWikiCmdList
658 * @covers ::wfShellWikiCmd
660 public function testWfShellWikiCmd( $script, $parameters, $options,
661 $expected, $description
663 if ( wfIsWindows() ) {
664 // Approximation that's good enough for our purposes just now
665 $expected = str_replace( "'", '"', $expected );
667 $actual = wfShellWikiCmd( $script, $parameters, $options );
668 $this->assertEquals( $expected, $actual, $description );
671 public function wfWikiID() {
672 $this->setMwGlobals( [
673 'wgDBname' => 'example',
681 $this->setMwGlobals( [
682 'wgDBname' => 'example',
683 'wgDBprefix' => 'mw_',
692 * @covers ::wfMemcKey
694 public function testWfMemcKey() {
695 $cache = ObjectCache
::getLocalClusterInstance();
697 $cache->makeKey( 'foo', 123, 'bar' ),
698 wfMemcKey( 'foo', 123, 'bar' )
703 * @covers ::wfForeignMemcKey
705 public function testWfForeignMemcKey() {
706 $cache = ObjectCache
::getLocalClusterInstance();
707 $keyspace = $this->readAttribute( $cache, 'keyspace' );
709 wfForeignMemcKey( $keyspace, '', 'foo', 'bar' ),
710 $cache->makeKey( 'foo', 'bar' )
715 * @covers ::wfGlobalCacheKey
717 public function testWfGlobalCacheKey() {
718 $cache = ObjectCache
::getLocalClusterInstance();
719 $this->hideDeprecated( 'wfGlobalCacheKey' );
721 $cache->makeGlobalKey( 'foo', 123, 'bar' ),
722 wfGlobalCacheKey( 'foo', 123, 'bar' )
726 public static function provideWfShellWikiCmdList() {
730 [ 'eval.php', [ '--help', '--test' ], [],
731 "'$wgPhpCli' 'eval.php' '--help' '--test'",
732 "Called eval.php --help --test" ],
733 [ 'eval.php', [ '--help', '--test space' ], [ 'php' => 'php5' ],
734 "'php5' 'eval.php' '--help' '--test space'",
735 "Called eval.php --help --test with php option" ],
736 [ 'eval.php', [ '--help', '--test', 'X' ], [ 'wrapper' => 'MWScript.php' ],
737 "'$wgPhpCli' 'MWScript.php' 'eval.php' '--help' '--test' 'X'",
738 "Called eval.php --help --test with wrapper option" ],
741 [ '--help', '--test', 'y' ],
742 [ 'php' => 'php5', 'wrapper' => 'MWScript.php' ],
743 "'php5' 'MWScript.php' 'eval.php' '--help' '--test' 'y'",
744 "Called eval.php --help --test with wrapper and php option"
748 /* @todo many more! */