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()
78 strlen( wfRandomString( 10 ) ), 10
81 preg_match( '/^[0-9a-f]+$/i', wfRandomString() ) === 1
86 * @covers ::wfUrlencode
88 public function testUrlencode() {
90 "%E7%89%B9%E5%88%A5:Contributions/Foobar",
91 wfUrlencode( "\xE7\x89\xB9\xE5\x88\xA5:Contributions/Foobar" ) );
95 * @covers ::wfExpandIRI
97 public function testExpandIRI() {
99 "https://te.wikibooks.org/wiki/ఉబుంటు_వాడుకరి_మార్గదర్శని",
100 wfExpandIRI( "https://te.wikibooks.org/wiki/"
101 . "%E0%B0%89%E0%B0%AC%E0%B1%81%E0%B0%82%E0%B0%9F%E0%B1%81_"
102 . "%E0%B0%B5%E0%B0%BE%E0%B0%A1%E0%B1%81%E0%B0%95%E0%B0%B0%E0%B0%BF_"
103 . "%E0%B0%AE%E0%B0%BE%E0%B0%B0%E0%B1%8D%E0%B0%97%E0%B0%A6%E0%B0%B0"
104 . "%E0%B1%8D%E0%B0%B6%E0%B0%A8%E0%B0%BF" ) );
108 * Intended to cover the relevant bits of ServiceWiring.php, as well as GlobalFunctions.php
109 * @covers ::wfReadOnly
111 public function testReadOnlyEmpty() {
112 $this->assertFalse( wfReadOnly() );
113 $this->assertFalse( wfReadOnly() );
117 * Intended to cover the relevant bits of ServiceWiring.php, as well as GlobalFunctions.php
118 * @covers ::wfReadOnly
120 public function testReadOnlySet() {
121 global $wgReadOnlyFile;
123 $f = fopen( $wgReadOnlyFile, "wt" );
124 fwrite( $f, 'Message' );
127 // Reset the service to avoid cached results
128 $this->overrideMwServices();
130 $this->assertTrue( wfReadOnly() );
131 $this->assertTrue( wfReadOnly() ); # Check cached
135 * This behaviour could probably be deprecated. Several extensions rely on it as of 1.29.
136 * @covers ::wfReadOnlyReason
138 public function testReadOnlyGlobalChange() {
139 $this->assertFalse( wfReadOnlyReason() );
141 $this->setMwGlobals( [
142 'wgReadOnly' => 'reason'
144 $this->overrideMwServices();
146 $this->assertSame( 'reason', wfReadOnlyReason() );
149 public static function provideArrayToCGI() {
152 [ [ 'foo' => 'bar' ], 'foo=bar' ], // string test
153 [ [ 'foo' => '' ], 'foo=' ], // empty string test
154 [ [ 'foo' => 1 ], 'foo=1' ], // number test
155 [ [ 'foo' => true ], 'foo=1' ], // true test
156 [ [ 'foo' => false ], '' ], // false test
157 [ [ 'foo' => null ], '' ], // null test
158 [ [ 'foo' => 'A&B=5+6@!"\'' ], 'foo=A%26B%3D5%2B6%40%21%22%27' ], // urlencoding test
160 [ 'foo' => 'bar', 'baz' => 'is', 'asdf' => 'qwerty' ],
161 'foo=bar&baz=is&asdf=qwerty'
162 ], // multi-item test
163 [ [ 'foo' => [ 'bar' => 'baz' ] ], 'foo%5Bbar%5D=baz' ],
165 [ 'foo' => [ 'bar' => 'baz', 'qwerty' => 'asdf' ] ],
166 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf'
168 [ [ 'foo' => [ 'bar', 'baz' ] ], 'foo%5B0%5D=bar&foo%5B1%5D=baz' ],
170 [ 'foo' => [ 'bar' => [ 'bar' => 'baz' ] ] ],
171 'foo%5Bbar%5D%5Bbar%5D=baz'
177 * @dataProvider provideArrayToCGI
178 * @covers ::wfArrayToCgi
180 public function testArrayToCGI( $array, $result ) {
181 $this->assertEquals( $result, wfArrayToCgi( $array ) );
185 * @covers ::wfArrayToCgi
187 public function testArrayToCGI2() {
192 [ 'foo' => 'bar', 'baz' => 'overridden value' ] ) );
195 public static function provideCgiToArray() {
198 [ 'foo=bar', [ 'foo' => 'bar' ] ], // string
199 [ 'foo=', [ 'foo' => '' ] ], // empty string
200 [ 'foo', [ 'foo' => '' ] ], // missing =
201 [ 'foo=bar&qwerty=asdf', [ 'foo' => 'bar', 'qwerty' => 'asdf' ] ], // multiple value
202 [ 'foo=A%26B%3D5%2B6%40%21%22%27', [ 'foo' => 'A&B=5+6@!"\'' ] ], // urldecoding test
203 [ 'foo%5Bbar%5D=baz', [ 'foo' => [ 'bar' => 'baz' ] ] ],
205 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf',
206 [ 'foo' => [ 'bar' => 'baz', 'qwerty' => 'asdf' ] ]
208 [ 'foo%5B0%5D=bar&foo%5B1%5D=baz', [ 'foo' => [ 0 => 'bar', 1 => 'baz' ] ] ],
210 'foo%5Bbar%5D%5Bbar%5D=baz',
211 [ 'foo' => [ 'bar' => [ 'bar' => 'baz' ] ] ]
217 * @dataProvider provideCgiToArray
218 * @covers ::wfCgiToArray
220 public function testCgiToArray( $cgi, $result ) {
221 $this->assertEquals( $result, wfCgiToArray( $cgi ) );
224 public static function provideCgiRoundTrip() {
229 [ 'foo=bar&baz=biz' ],
230 [ 'foo=A%26B%3D5%2B6%40%21%22%27' ],
231 [ 'foo%5Bbar%5D=baz' ],
232 [ 'foo%5B0%5D=bar&foo%5B1%5D=baz' ],
233 [ 'foo%5Bbar%5D%5Bbar%5D=baz' ],
238 * @dataProvider provideCgiRoundTrip
239 * @covers ::wfArrayToCgi
241 public function testCgiRoundTrip( $cgi ) {
242 $this->assertEquals( $cgi, wfArrayToCgi( wfCgiToArray( $cgi ) ) );
246 * @covers ::mimeTypeMatch
248 public function testMimeTypeMatch() {
251 mimeTypeMatch( 'text/html',
252 [ 'application/xhtml+xml' => 1.0,
254 'text/plain' => 0.3 ] ) );
257 mimeTypeMatch( 'text/html',
259 'text/*' => 0.5 ] ) );
262 mimeTypeMatch( 'text/html',
263 [ '*/*' => 1.0 ] ) );
265 mimeTypeMatch( 'text/html',
266 [ 'image/png' => 1.0,
267 'image/svg+xml' => 0.5 ] ) );
271 * @covers ::wfNegotiateType
273 public function testNegotiateType() {
277 [ 'application/xhtml+xml' => 1.0,
281 [ 'text/html' => 1.0 ] ) );
283 'application/xhtml+xml',
285 [ 'application/xhtml+xml' => 1.0,
289 [ 'application/xhtml+xml' => 1.0,
290 'text/html' => 0.5 ] ) );
294 [ 'text/html' => 1.0,
297 'application/xhtml+xml' => 0.2 ],
298 [ 'application/xhtml+xml' => 1.0,
299 'text/html' => 0.5 ] ) );
306 [ 'application/xhtml+xml' => 1.0,
307 'text/html' => 0.5 ] ) );
311 [ 'application/xhtml+xml' => 1.0 ] ) );
316 * @covers ::wfDebugMem
318 public function testDebugFunctionTest() {
319 $debugLogFile = $this->getNewTempFile();
321 $this->setMwGlobals( [
322 'wgDebugLogFile' => $debugLogFile,
323 # @todo FIXME: $wgDebugTimestamps should be tested
324 'wgDebugTimestamps' => false,
326 $this->setLogger( 'wfDebug', new LegacyLogger( 'wfDebug' ) );
328 wfDebug( "This is a normal string" );
329 $this->assertEquals( "This is a normal string\n", file_get_contents( $debugLogFile ) );
330 unlink( $debugLogFile );
332 wfDebug( "This is nöt an ASCII string" );
333 $this->assertEquals( "This is nöt an ASCII string\n", file_get_contents( $debugLogFile ) );
334 unlink( $debugLogFile );
336 wfDebug( "\00305This has böth UTF and control chars\003" );
338 " 05This has böth UTF and control chars \n",
339 file_get_contents( $debugLogFile )
341 unlink( $debugLogFile );
344 $this->assertGreaterThan(
346 preg_replace( '/\D/', '', file_get_contents( $debugLogFile ) )
348 unlink( $debugLogFile );
351 $this->assertGreaterThan(
353 preg_replace( '/\D/', '', file_get_contents( $debugLogFile ) )
355 unlink( $debugLogFile );
359 * @covers ::wfClientAcceptsGzip
361 public function testClientAcceptsGzipTest() {
366 'compress, gzip' => true,
367 'gzip;q=1.0' => true,
370 'gzip;q=abcde' => true, // is this REALLY valid?
371 'gzip;q=12345678.9' => true,
375 if ( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) {
376 $old_server_setting = $_SERVER['HTTP_ACCEPT_ENCODING'];
379 foreach ( $settings as $encoding => $expect ) {
380 $_SERVER['HTTP_ACCEPT_ENCODING'] = $encoding;
382 $this->assertEquals( $expect, wfClientAcceptsGzip( true ),
383 "'$encoding' => " . wfBoolToStr( $expect ) );
386 if ( isset( $old_server_setting ) ) {
387 $_SERVER['HTTP_ACCEPT_ENCODING'] = $old_server_setting;
392 * @covers ::wfPercent
394 public function testWfPercentTest() {
396 [ 6 / 7, '0.86%', 2, false ],
398 [ 22 / 7, '3.14286%', 5 ],
401 [ 10 / 3, '0%', -1 ],
402 [ 3 / 4 / 5, '0.1%', 1 ],
403 [ 6 / 7 * 8, '6.8571428571%', 10 ],
406 foreach ( $pcts as $pct ) {
407 if ( !isset( $pct[2] ) ) {
410 if ( !isset( $pct[3] ) ) {
414 $this->assertEquals( wfPercent( $pct[0], $pct[2], $pct[3] ), $pct[1], $pct[1] );
419 * test @see wfShorthandToInteger()
420 * @dataProvider provideShorthand
421 * @covers ::wfShorthandToInteger
423 public function testWfShorthandToInteger( $shorthand, $expected ) {
424 $this->assertEquals( $expected,
425 wfShorthandToInteger( $shorthand )
429 public static function provideShorthand() {
430 // Syntax: [ shorthand, expected integer ]
437 # Failures returns 0 :(
441 # Int, strings with spaces
450 [ '1m', 1024 * 1024 ],
451 [ '1M', 1024 * 1024 ],
452 [ '1g', 1024 * 1024 * 1024 ],
453 [ '1G', 1024 * 1024 * 1024 ],
476 public function testMerge_worksWithLessParameters() {
477 $this->markTestSkippedIfNoDiff3();
480 $successfulMerge = wfMerge( "old1\n\nold2", "old1\n\nnew2", "new1\n\nold2", $mergedText );
483 $conflictingMerge = wfMerge( 'old', 'old and mine', 'old and yours', $mergedText );
485 $this->assertEquals( true, $successfulMerge );
486 $this->assertEquals( false, $conflictingMerge );
490 * @param string $old Text as it was in the database
491 * @param string $mine Text submitted while user was editing
492 * @param string $yours Text submitted by the user
493 * @param bool $expectedMergeResult Whether the merge should be a success
494 * @param string $expectedText Text after merge has been completed
495 * @param string $expectedMergeAttemptResult Diff3 output if conflicts occur
497 * @dataProvider provideMerge()
501 public function testMerge( $old, $mine, $yours, $expectedMergeResult, $expectedText,
502 $expectedMergeAttemptResult ) {
503 $this->markTestSkippedIfNoDiff3();
506 $attemptMergeResult = null;
507 $isMerged = wfMerge( $old, $mine, $yours, $mergedText, $mergeAttemptResult );
509 $msg = 'Merge should be a ';
510 $msg .= $expectedMergeResult ?
'success' : 'failure';
511 $this->assertEquals( $expectedMergeResult, $isMerged, $msg );
512 $this->assertEquals( $expectedMergeAttemptResult, $mergeAttemptResult );
515 // Verify the merged text
516 $this->assertEquals( $expectedText, $mergedText,
517 'is merged text as expected?' );
521 public static function provideMerge() {
522 $EXPECT_MERGE_SUCCESS = true;
523 $EXPECT_MERGE_FAILURE = false;
529 "one one one\n" . // trimmed
534 "one one one ONE ONE\n" .
536 "two two two\n", // with tailing whitespace
541 "two two TWO TWO", // trimmed
544 $EXPECT_MERGE_SUCCESS,
547 "one one one ONE ONE\n" .
549 "two two TWO TWO\n", // note: will always end in a newline
551 // mergeAttemptResult:
555 // #1: conflict, fail
558 "one one one", // trimmed
561 "one one one ONE ONE\n" .
564 "\n", // with tailing whitespace
569 "two two", // trimmed
571 $EXPECT_MERGE_FAILURE,
576 // mergeAttemptResult:
587 * @dataProvider provideWfMatchesDomainList
588 * @covers ::wfMatchesDomainList
590 public function testWfMatchesDomainList( $url, $domains, $expected, $description ) {
591 $actual = wfMatchesDomainList( $url, $domains );
592 $this->assertEquals( $expected, $actual, $description );
595 public static function provideWfMatchesDomainList() {
597 $protocols = [ 'HTTP' => 'http:', 'HTTPS' => 'https:', 'protocol-relative' => '' ];
598 foreach ( $protocols as $pDesc => $p ) {
599 $a = array_merge( $a, [
601 "$p//www.example.com",
604 "No matches for empty domains array, $pDesc URL"
607 "$p//www.example.com",
608 [ 'www.example.com' ],
610 "Exact match in domains array, $pDesc URL"
613 "$p//www.example.com",
616 "Match without subdomain in domains array, $pDesc URL"
619 "$p//www.example2.com",
620 [ 'www.example.com', 'www.example2.com', 'www.example3.com' ],
622 "Exact match with other domains in array, $pDesc URL"
625 "$p//www.example2.com",
626 [ 'example.com', 'example2.com', 'example3,com' ],
628 "Match without subdomain with other domains in array, $pDesc URL"
631 "$p//www.example4.com",
632 [ 'example.com', 'example2.com', 'example3,com' ],
634 "Domain not in array, $pDesc URL"
637 "$p//nds-nl.wikipedia.org",
638 [ 'nl.wikipedia.org' ],
640 "Non-matching substring of domain, $pDesc URL"
649 * @covers ::wfMkdirParents
651 public function testWfMkdirParents() {
652 // Should not return true if file exists instead of directory
653 $fname = $this->getNewTempFile();
654 Wikimedia\
suppressWarnings();
655 $ok = wfMkdirParents( $fname );
656 Wikimedia\restoreWarnings
();
657 $this->assertFalse( $ok );
661 * @dataProvider provideWfShellWikiCmdList
662 * @covers ::wfShellWikiCmd
664 public function testWfShellWikiCmd( $script, $parameters, $options,
665 $expected, $description
667 if ( wfIsWindows() ) {
668 // Approximation that's good enough for our purposes just now
669 $expected = str_replace( "'", '"', $expected );
671 $actual = wfShellWikiCmd( $script, $parameters, $options );
672 $this->assertEquals( $expected, $actual, $description );
675 public function wfWikiID() {
676 $this->setMwGlobals( [
677 'wgDBname' => 'example',
685 $this->setMwGlobals( [
686 'wgDBname' => 'example',
687 'wgDBprefix' => 'mw_',
696 * @covers ::wfMemcKey
698 public function testWfMemcKey() {
699 $cache = ObjectCache
::getLocalClusterInstance();
701 $cache->makeKey( 'foo', 123, 'bar' ),
702 wfMemcKey( 'foo', 123, 'bar' )
707 * @covers ::wfForeignMemcKey
709 public function testWfForeignMemcKey() {
710 $cache = ObjectCache
::getLocalClusterInstance();
711 $keyspace = $this->readAttribute( $cache, 'keyspace' );
713 wfForeignMemcKey( $keyspace, '', 'foo', 'bar' ),
714 $cache->makeKey( 'foo', 'bar' )
719 * @covers ::wfGlobalCacheKey
721 public function testWfGlobalCacheKey() {
722 $cache = ObjectCache
::getLocalClusterInstance();
724 $cache->makeGlobalKey( 'foo', 123, 'bar' ),
725 wfGlobalCacheKey( 'foo', 123, 'bar' )
729 public static function provideWfShellWikiCmdList() {
733 [ 'eval.php', [ '--help', '--test' ], [],
734 "'$wgPhpCli' 'eval.php' '--help' '--test'",
735 "Called eval.php --help --test" ],
736 [ 'eval.php', [ '--help', '--test space' ], [ 'php' => 'php5' ],
737 "'php5' 'eval.php' '--help' '--test space'",
738 "Called eval.php --help --test with php option" ],
739 [ 'eval.php', [ '--help', '--test', 'X' ], [ 'wrapper' => 'MWScript.php' ],
740 "'$wgPhpCli' 'MWScript.php' 'eval.php' '--help' '--test' 'X'",
741 "Called eval.php --help --test with wrapper option" ],
744 [ '--help', '--test', 'y' ],
745 [ 'php' => 'php5', 'wrapper' => 'MWScript.php' ],
746 "'php5' 'MWScript.php' 'eval.php' '--help' '--test' 'y'",
747 "Called eval.php --help --test with wrapper and php option"
751 /* @todo many more! */