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( [
17 'wgReadOnlyFile' => $readOnlyFile,
23 'file://', # Non-default
29 * @dataProvider provideForWfArrayDiff2
30 * @covers ::wfArrayDiff2
32 public function testWfArrayDiff2( $a, $b, $expected ) {
34 wfArrayDiff2( $a, $b ), $expected
38 // @todo Provide more tests
39 public static function provideForWfArrayDiff2() {
48 [ [ 'a' ], [ 'a', 'b', 'c' ] ],
49 [ [ 'a' ], [ 'a', 'b' ] ],
50 [ 1 => [ 'a', 'b', 'c' ] ],
56 * Test cases for random functions could hypothetically fail,
57 * even though they shouldn't.
63 public function testRandom() {
65 wfRandom() == wfRandom()
70 * @covers ::wfRandomString
72 public function testRandomString() {
74 wfRandomString() == wfRandomString()
77 strlen( wfRandomString( 10 ) ), 10
80 preg_match( '/^[0-9a-f]+$/i', wfRandomString() ) === 1
85 * @covers ::wfUrlencode
87 public function testUrlencode() {
89 "%E7%89%B9%E5%88%A5:Contributions/Foobar",
90 wfUrlencode( "\xE7\x89\xB9\xE5\x88\xA5:Contributions/Foobar" ) );
94 * @covers ::wfExpandIRI
96 public function testExpandIRI() {
98 "https://te.wikibooks.org/wiki/ఉబుంటు_వాడుకరి_మార్గదర్శని",
99 wfExpandIRI( "https://te.wikibooks.org/wiki/"
100 . "%E0%B0%89%E0%B0%AC%E0%B1%81%E0%B0%82%E0%B0%9F%E0%B1%81_"
101 . "%E0%B0%B5%E0%B0%BE%E0%B0%A1%E0%B1%81%E0%B0%95%E0%B0%B0%E0%B0%BF_"
102 . "%E0%B0%AE%E0%B0%BE%E0%B0%B0%E0%B1%8D%E0%B0%97%E0%B0%A6%E0%B0%B0"
103 . "%E0%B1%8D%E0%B0%B6%E0%B0%A8%E0%B0%BF" ) );
107 * Intended to cover the relevant bits of ServiceWiring.php, as well as GlobalFunctions.php
108 * @covers ::wfReadOnly
110 public function testReadOnlyEmpty() {
114 MediaWiki\MediaWikiServices
::getInstance()->getReadOnlyMode()->clearCache();
115 $this->assertFalse( wfReadOnly() );
116 $this->assertFalse( wfReadOnly() );
120 * Intended to cover the relevant bits of ServiceWiring.php, as well as GlobalFunctions.php
121 * @covers ::wfReadOnly
123 public function testReadOnlySet() {
124 global $wgReadOnly, $wgReadOnlyFile;
126 $readOnlyMode = MediaWiki\MediaWikiServices
::getInstance()->getReadOnlyMode();
127 $readOnlyMode->clearCache();
129 $f = fopen( $wgReadOnlyFile, "wt" );
130 fwrite( $f, 'Message' );
132 $wgReadOnly = null; # Check on $wgReadOnlyFile
134 $this->assertTrue( wfReadOnly() );
135 $this->assertTrue( wfReadOnly() ); # Check cached
137 unlink( $wgReadOnlyFile );
138 $readOnlyMode->clearCache();
139 $this->assertFalse( wfReadOnly() );
140 $this->assertFalse( wfReadOnly() );
144 * This behaviour could probably be deprecated. Several extensions rely on it as of 1.29.
145 * @covers ::wfReadOnlyReason
147 public function testReadOnlyGlobalChange() {
148 $this->assertFalse( wfReadOnlyReason() );
149 $this->setMwGlobals( [
150 'wgReadOnly' => 'reason'
152 $this->assertSame( 'reason', wfReadOnlyReason() );
155 public static function provideArrayToCGI() {
158 [ [ 'foo' => 'bar' ], 'foo=bar' ], // string test
159 [ [ 'foo' => '' ], 'foo=' ], // empty string test
160 [ [ 'foo' => 1 ], 'foo=1' ], // number test
161 [ [ 'foo' => true ], 'foo=1' ], // true test
162 [ [ 'foo' => false ], '' ], // false test
163 [ [ 'foo' => null ], '' ], // null test
164 [ [ 'foo' => 'A&B=5+6@!"\'' ], 'foo=A%26B%3D5%2B6%40%21%22%27' ], // urlencoding test
166 [ 'foo' => 'bar', 'baz' => 'is', 'asdf' => 'qwerty' ],
167 'foo=bar&baz=is&asdf=qwerty'
168 ], // multi-item test
169 [ [ 'foo' => [ 'bar' => 'baz' ] ], 'foo%5Bbar%5D=baz' ],
171 [ 'foo' => [ 'bar' => 'baz', 'qwerty' => 'asdf' ] ],
172 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf'
174 [ [ 'foo' => [ 'bar', 'baz' ] ], 'foo%5B0%5D=bar&foo%5B1%5D=baz' ],
176 [ 'foo' => [ 'bar' => [ 'bar' => 'baz' ] ] ],
177 'foo%5Bbar%5D%5Bbar%5D=baz'
183 * @dataProvider provideArrayToCGI
184 * @covers ::wfArrayToCgi
186 public function testArrayToCGI( $array, $result ) {
187 $this->assertEquals( $result, wfArrayToCgi( $array ) );
191 * @covers ::wfArrayToCgi
193 public function testArrayToCGI2() {
198 [ 'foo' => 'bar', 'baz' => 'overridden value' ] ) );
201 public static function provideCgiToArray() {
204 [ 'foo=bar', [ 'foo' => 'bar' ] ], // string
205 [ 'foo=', [ 'foo' => '' ] ], // empty string
206 [ 'foo', [ 'foo' => '' ] ], // missing =
207 [ 'foo=bar&qwerty=asdf', [ 'foo' => 'bar', 'qwerty' => 'asdf' ] ], // multiple value
208 [ 'foo=A%26B%3D5%2B6%40%21%22%27', [ 'foo' => 'A&B=5+6@!"\'' ] ], // urldecoding test
209 [ 'foo%5Bbar%5D=baz', [ 'foo' => [ 'bar' => 'baz' ] ] ],
211 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf',
212 [ 'foo' => [ 'bar' => 'baz', 'qwerty' => 'asdf' ] ]
214 [ 'foo%5B0%5D=bar&foo%5B1%5D=baz', [ 'foo' => [ 0 => 'bar', 1 => 'baz' ] ] ],
216 'foo%5Bbar%5D%5Bbar%5D=baz',
217 [ 'foo' => [ 'bar' => [ 'bar' => 'baz' ] ] ]
223 * @dataProvider provideCgiToArray
224 * @covers ::wfCgiToArray
226 public function testCgiToArray( $cgi, $result ) {
227 $this->assertEquals( $result, wfCgiToArray( $cgi ) );
230 public static function provideCgiRoundTrip() {
235 [ 'foo=bar&baz=biz' ],
236 [ 'foo=A%26B%3D5%2B6%40%21%22%27' ],
237 [ 'foo%5Bbar%5D=baz' ],
238 [ 'foo%5B0%5D=bar&foo%5B1%5D=baz' ],
239 [ 'foo%5Bbar%5D%5Bbar%5D=baz' ],
244 * @dataProvider provideCgiRoundTrip
245 * @covers ::wfArrayToCgi
247 public function testCgiRoundTrip( $cgi ) {
248 $this->assertEquals( $cgi, wfArrayToCgi( wfCgiToArray( $cgi ) ) );
252 * @covers ::mimeTypeMatch
254 public function testMimeTypeMatch() {
257 mimeTypeMatch( 'text/html',
258 [ 'application/xhtml+xml' => 1.0,
260 'text/plain' => 0.3 ] ) );
263 mimeTypeMatch( 'text/html',
265 'text/*' => 0.5 ] ) );
268 mimeTypeMatch( 'text/html',
269 [ '*/*' => 1.0 ] ) );
271 mimeTypeMatch( 'text/html',
272 [ 'image/png' => 1.0,
273 'image/svg+xml' => 0.5 ] ) );
277 * @covers ::wfNegotiateType
279 public function testNegotiateType() {
283 [ 'application/xhtml+xml' => 1.0,
287 [ 'text/html' => 1.0 ] ) );
289 'application/xhtml+xml',
291 [ 'application/xhtml+xml' => 1.0,
295 [ 'application/xhtml+xml' => 1.0,
296 'text/html' => 0.5 ] ) );
300 [ 'text/html' => 1.0,
303 'application/xhtml+xml' => 0.2 ],
304 [ 'application/xhtml+xml' => 1.0,
305 'text/html' => 0.5 ] ) );
312 [ 'application/xhtml+xml' => 1.0,
313 'text/html' => 0.5 ] ) );
317 [ 'application/xhtml+xml' => 1.0 ] ) );
322 * @covers ::wfDebugMem
324 public function testDebugFunctionTest() {
325 $debugLogFile = $this->getNewTempFile();
327 $this->setMwGlobals( [
328 'wgDebugLogFile' => $debugLogFile,
329 # @todo FIXME: $wgDebugTimestamps should be tested
330 'wgDebugTimestamps' => false,
332 $this->setLogger( 'wfDebug', new LegacyLogger( 'wfDebug' ) );
334 wfDebug( "This is a normal string" );
335 $this->assertEquals( "This is a normal string\n", file_get_contents( $debugLogFile ) );
336 unlink( $debugLogFile );
338 wfDebug( "This is nöt an ASCII string" );
339 $this->assertEquals( "This is nöt an ASCII string\n", file_get_contents( $debugLogFile ) );
340 unlink( $debugLogFile );
342 wfDebug( "\00305This has böth UTF and control chars\003" );
344 " 05This has böth UTF and control chars \n",
345 file_get_contents( $debugLogFile )
347 unlink( $debugLogFile );
350 $this->assertGreaterThan(
352 preg_replace( '/\D/', '', file_get_contents( $debugLogFile ) )
354 unlink( $debugLogFile );
357 $this->assertGreaterThan(
359 preg_replace( '/\D/', '', file_get_contents( $debugLogFile ) )
361 unlink( $debugLogFile );
365 * @covers ::wfClientAcceptsGzip
367 public function testClientAcceptsGzipTest() {
372 'compress, gzip' => true,
373 'gzip;q=1.0' => true,
376 'gzip;q=abcde' => true, // is this REALLY valid?
377 'gzip;q=12345678.9' => true,
381 if ( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) {
382 $old_server_setting = $_SERVER['HTTP_ACCEPT_ENCODING'];
385 foreach ( $settings as $encoding => $expect ) {
386 $_SERVER['HTTP_ACCEPT_ENCODING'] = $encoding;
388 $this->assertEquals( $expect, wfClientAcceptsGzip( true ),
389 "'$encoding' => " . wfBoolToStr( $expect ) );
392 if ( isset( $old_server_setting ) ) {
393 $_SERVER['HTTP_ACCEPT_ENCODING'] = $old_server_setting;
398 * @covers ::wfPercent
400 public function testWfPercentTest() {
402 [ 6 / 7, '0.86%', 2, false ],
404 [ 22 / 7, '3.14286%', 5 ],
407 [ 10 / 3, '0%', -1 ],
408 [ 3 / 4 / 5, '0.1%', 1 ],
409 [ 6 / 7 * 8, '6.8571428571%', 10 ],
412 foreach ( $pcts as $pct ) {
413 if ( !isset( $pct[2] ) ) {
416 if ( !isset( $pct[3] ) ) {
420 $this->assertEquals( wfPercent( $pct[0], $pct[2], $pct[3] ), $pct[1], $pct[1] );
425 * test @see wfShorthandToInteger()
426 * @dataProvider provideShorthand
427 * @covers ::wfShorthandToInteger
429 public function testWfShorthandToInteger( $shorthand, $expected ) {
430 $this->assertEquals( $expected,
431 wfShorthandToInteger( $shorthand )
435 public static function provideShorthand() {
436 // Syntax: [ shorthand, expected integer ]
443 # Failures returns 0 :(
447 # Int, strings with spaces
456 [ '1m', 1024 * 1024 ],
457 [ '1M', 1024 * 1024 ],
458 [ '1g', 1024 * 1024 * 1024 ],
459 [ '1G', 1024 * 1024 * 1024 ],
482 public function testMerge_worksWithLessParameters() {
483 $this->markTestSkippedIfNoDiff3();
486 $successfulMerge = wfMerge( "old1\n\nold2", "old1\n\nnew2", "new1\n\nold2", $mergedText );
489 $conflictingMerge = wfMerge( 'old', 'old and mine', 'old and yours', $mergedText );
491 $this->assertEquals( true, $successfulMerge );
492 $this->assertEquals( false, $conflictingMerge );
496 * @param string $old Text as it was in the database
497 * @param string $mine Text submitted while user was editing
498 * @param string $yours Text submitted by the user
499 * @param bool $expectedMergeResult Whether the merge should be a success
500 * @param string $expectedText Text after merge has been completed
501 * @param string $expectedMergeAttemptResult Diff3 output if conflicts occur
503 * @dataProvider provideMerge()
507 public function testMerge( $old, $mine, $yours, $expectedMergeResult, $expectedText,
508 $expectedMergeAttemptResult ) {
509 $this->markTestSkippedIfNoDiff3();
512 $attemptMergeResult = null;
513 $isMerged = wfMerge( $old, $mine, $yours, $mergedText, $mergeAttemptResult );
515 $msg = 'Merge should be a ';
516 $msg .= $expectedMergeResult ?
'success' : 'failure';
517 $this->assertEquals( $expectedMergeResult, $isMerged, $msg );
518 $this->assertEquals( $expectedMergeAttemptResult, $mergeAttemptResult );
521 // Verify the merged text
522 $this->assertEquals( $expectedText, $mergedText,
523 'is merged text as expected?' );
527 public static function provideMerge() {
528 $EXPECT_MERGE_SUCCESS = true;
529 $EXPECT_MERGE_FAILURE = false;
535 "one one one\n" . // trimmed
540 "one one one ONE ONE\n" .
542 "two two two\n", // with tailing whitespace
547 "two two TWO TWO", // trimmed
550 $EXPECT_MERGE_SUCCESS,
553 "one one one ONE ONE\n" .
555 "two two TWO TWO\n", // note: will always end in a newline
557 // mergeAttemptResult:
561 // #1: conflict, fail
564 "one one one", // trimmed
567 "one one one ONE ONE\n" .
570 "\n", // with tailing whitespace
575 "two two", // trimmed
577 $EXPECT_MERGE_FAILURE,
582 // mergeAttemptResult:
593 * @dataProvider provideWfMatchesDomainList
594 * @covers ::wfMatchesDomainList
596 public function testWfMatchesDomainList( $url, $domains, $expected, $description ) {
597 $actual = wfMatchesDomainList( $url, $domains );
598 $this->assertEquals( $expected, $actual, $description );
601 public static function provideWfMatchesDomainList() {
603 $protocols = [ 'HTTP' => 'http:', 'HTTPS' => 'https:', 'protocol-relative' => '' ];
604 foreach ( $protocols as $pDesc => $p ) {
605 $a = array_merge( $a, [
607 "$p//www.example.com",
610 "No matches for empty domains array, $pDesc URL"
613 "$p//www.example.com",
614 [ 'www.example.com' ],
616 "Exact match in domains array, $pDesc URL"
619 "$p//www.example.com",
622 "Match without subdomain in domains array, $pDesc URL"
625 "$p//www.example2.com",
626 [ 'www.example.com', 'www.example2.com', 'www.example3.com' ],
628 "Exact match with other domains in array, $pDesc URL"
631 "$p//www.example2.com",
632 [ 'example.com', 'example2.com', 'example3,com' ],
634 "Match without subdomain with other domains in array, $pDesc URL"
637 "$p//www.example4.com",
638 [ 'example.com', 'example2.com', 'example3,com' ],
640 "Domain not in array, $pDesc URL"
643 "$p//nds-nl.wikipedia.org",
644 [ 'nl.wikipedia.org' ],
646 "Non-matching substring of domain, $pDesc URL"
655 * @covers ::wfMkdirParents
657 public function testWfMkdirParents() {
658 // Should not return true if file exists instead of directory
659 $fname = $this->getNewTempFile();
660 Wikimedia\
suppressWarnings();
661 $ok = wfMkdirParents( $fname );
662 Wikimedia\restoreWarnings
();
663 $this->assertFalse( $ok );
667 * @dataProvider provideWfShellWikiCmdList
668 * @covers ::wfShellWikiCmd
670 public function testWfShellWikiCmd( $script, $parameters, $options,
671 $expected, $description
673 if ( wfIsWindows() ) {
674 // Approximation that's good enough for our purposes just now
675 $expected = str_replace( "'", '"', $expected );
677 $actual = wfShellWikiCmd( $script, $parameters, $options );
678 $this->assertEquals( $expected, $actual, $description );
681 public function wfWikiID() {
682 $this->setMwGlobals( [
683 'wgDBname' => 'example',
691 $this->setMwGlobals( [
692 'wgDBname' => 'example',
693 'wgDBprefix' => 'mw_',
702 * @covers ::wfMemcKey
704 public function testWfMemcKey() {
705 $cache = ObjectCache
::getLocalClusterInstance();
707 $cache->makeKey( 'foo', 123, 'bar' ),
708 wfMemcKey( 'foo', 123, 'bar' )
713 * @covers ::wfForeignMemcKey
715 public function testWfForeignMemcKey() {
716 $cache = ObjectCache
::getLocalClusterInstance();
717 $keyspace = $this->readAttribute( $cache, 'keyspace' );
719 wfForeignMemcKey( $keyspace, '', 'foo', 'bar' ),
720 $cache->makeKey( 'foo', 'bar' )
725 * @covers ::wfGlobalCacheKey
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! */