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 unlink( $debugLogFile );
325 wfDebug( "This is a normal string" );
326 $this->assertEquals( "This is a normal string\n", file_get_contents( $debugLogFile ) );
328 unlink( $debugLogFile );
329 wfDebug( "This is nöt an ASCII string" );
330 $this->assertEquals( "This is nöt an ASCII string\n", file_get_contents( $debugLogFile ) );
332 unlink( $debugLogFile );
333 wfDebug( "\00305This has böth UTF and control chars\003" );
335 " 05This has böth UTF and control chars \n",
336 file_get_contents( $debugLogFile )
339 unlink( $debugLogFile );
341 $this->assertGreaterThan(
343 preg_replace( '/\D/', '', file_get_contents( $debugLogFile ) )
346 unlink( $debugLogFile );
348 $this->assertGreaterThan(
350 preg_replace( '/\D/', '', file_get_contents( $debugLogFile ) )
353 unlink( $debugLogFile );
357 * @covers ::wfClientAcceptsGzip
359 public function testClientAcceptsGzipTest() {
364 'compress, gzip' => true,
365 'gzip;q=1.0' => true,
368 'gzip;q=abcde' => true, // is this REALLY valid?
369 'gzip;q=12345678.9' => true,
373 if ( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) {
374 $old_server_setting = $_SERVER['HTTP_ACCEPT_ENCODING'];
377 foreach ( $settings as $encoding => $expect ) {
378 $_SERVER['HTTP_ACCEPT_ENCODING'] = $encoding;
380 $this->assertEquals( $expect, wfClientAcceptsGzip( true ),
381 "'$encoding' => " . wfBoolToStr( $expect ) );
384 if ( isset( $old_server_setting ) ) {
385 $_SERVER['HTTP_ACCEPT_ENCODING'] = $old_server_setting;
390 * @covers ::wfPercent
392 public function testWfPercentTest() {
394 [ 6 / 7, '0.86%', 2, false ],
396 [ 22 / 7, '3.14286%', 5 ],
399 [ 10 / 3, '0%', -1 ],
400 [ 3 / 4 / 5, '0.1%', 1 ],
401 [ 6 / 7 * 8, '6.8571428571%', 10 ],
404 foreach ( $pcts as $pct ) {
405 if ( !isset( $pct[2] ) ) {
408 if ( !isset( $pct[3] ) ) {
412 $this->assertEquals( wfPercent( $pct[0], $pct[2], $pct[3] ), $pct[1], $pct[1] );
417 * test @see wfShorthandToInteger()
418 * @dataProvider provideShorthand
419 * @covers ::wfShorthandToInteger
421 public function testWfShorthandToInteger( $shorthand, $expected ) {
422 $this->assertEquals( $expected,
423 wfShorthandToInteger( $shorthand )
427 public static function provideShorthand() {
428 // Syntax: [ shorthand, expected integer ]
435 # Failures returns 0 :(
439 # Int, strings with spaces
448 [ '1m', 1024 * 1024 ],
449 [ '1M', 1024 * 1024 ],
450 [ '1g', 1024 * 1024 * 1024 ],
451 [ '1G', 1024 * 1024 * 1024 ],
474 public function testMerge_worksWithLessParameters() {
475 $this->markTestSkippedIfNoDiff3();
478 $successfulMerge = wfMerge( "old1\n\nold2", "old1\n\nnew2", "new1\n\nold2", $mergedText );
481 $conflictingMerge = wfMerge( 'old', 'old and mine', 'old and yours', $mergedText );
483 $this->assertEquals( true, $successfulMerge );
484 $this->assertEquals( false, $conflictingMerge );
488 * @param string $old Text as it was in the database
489 * @param string $mine Text submitted while user was editing
490 * @param string $yours Text submitted by the user
491 * @param bool $expectedMergeResult Whether the merge should be a success
492 * @param string $expectedText Text after merge has been completed
493 * @param string $expectedMergeAttemptResult Diff3 output if conflicts occur
495 * @dataProvider provideMerge()
499 public function testMerge( $old, $mine, $yours, $expectedMergeResult, $expectedText,
500 $expectedMergeAttemptResult ) {
501 $this->markTestSkippedIfNoDiff3();
504 $attemptMergeResult = null;
505 $isMerged = wfMerge( $old, $mine, $yours, $mergedText, $mergeAttemptResult );
507 $msg = 'Merge should be a ';
508 $msg .= $expectedMergeResult ?
'success' : 'failure';
509 $this->assertEquals( $expectedMergeResult, $isMerged, $msg );
510 $this->assertEquals( $expectedMergeAttemptResult, $mergeAttemptResult );
513 // Verify the merged text
514 $this->assertEquals( $expectedText, $mergedText,
515 'is merged text as expected?' );
519 public static function provideMerge() {
520 $EXPECT_MERGE_SUCCESS = true;
521 $EXPECT_MERGE_FAILURE = false;
527 "one one one\n" . // trimmed
532 "one one one ONE ONE\n" .
534 "two two two\n", // with tailing whitespace
539 "two two TWO TWO", // trimmed
542 $EXPECT_MERGE_SUCCESS,
545 "one one one ONE ONE\n" .
547 "two two TWO TWO\n", // note: will always end in a newline
549 // mergeAttemptResult:
553 // #1: conflict, fail
556 "one one one", // trimmed
559 "one one one ONE ONE\n" .
562 "\n", // with tailing whitespace
567 "two two", // trimmed
569 $EXPECT_MERGE_FAILURE,
574 // mergeAttemptResult:
585 * @dataProvider provideWfMatchesDomainList
586 * @covers ::wfMatchesDomainList
588 public function testWfMatchesDomainList( $url, $domains, $expected, $description ) {
589 $actual = wfMatchesDomainList( $url, $domains );
590 $this->assertEquals( $expected, $actual, $description );
593 public static function provideWfMatchesDomainList() {
595 $protocols = [ 'HTTP' => 'http:', 'HTTPS' => 'https:', 'protocol-relative' => '' ];
596 foreach ( $protocols as $pDesc => $p ) {
597 $a = array_merge( $a, [
599 "$p//www.example.com",
602 "No matches for empty domains array, $pDesc URL"
605 "$p//www.example.com",
606 [ 'www.example.com' ],
608 "Exact match in domains array, $pDesc URL"
611 "$p//www.example.com",
614 "Match without subdomain in domains array, $pDesc URL"
617 "$p//www.example2.com",
618 [ 'www.example.com', 'www.example2.com', 'www.example3.com' ],
620 "Exact match with other domains in array, $pDesc URL"
623 "$p//www.example2.com",
624 [ 'example.com', 'example2.com', 'example3,com' ],
626 "Match without subdomain with other domains in array, $pDesc URL"
629 "$p//www.example4.com",
630 [ 'example.com', 'example2.com', 'example3,com' ],
632 "Domain not in array, $pDesc URL"
635 "$p//nds-nl.wikipedia.org",
636 [ 'nl.wikipedia.org' ],
638 "Non-matching substring of domain, $pDesc URL"
647 * @covers ::wfMkdirParents
649 public function testWfMkdirParents() {
650 // Should not return true if file exists instead of directory
651 $fname = $this->getNewTempFile();
652 Wikimedia\
suppressWarnings();
653 $ok = wfMkdirParents( $fname );
654 Wikimedia\restoreWarnings
();
655 $this->assertFalse( $ok );
659 * @dataProvider provideWfShellWikiCmdList
660 * @covers ::wfShellWikiCmd
662 public function testWfShellWikiCmd( $script, $parameters, $options,
663 $expected, $description
665 if ( wfIsWindows() ) {
666 // Approximation that's good enough for our purposes just now
667 $expected = str_replace( "'", '"', $expected );
669 $actual = wfShellWikiCmd( $script, $parameters, $options );
670 $this->assertEquals( $expected, $actual, $description );
673 public function wfWikiID() {
674 $this->setMwGlobals( [
675 'wgDBname' => 'example',
683 $this->setMwGlobals( [
684 'wgDBname' => 'example',
685 'wgDBprefix' => 'mw_',
694 * @covers ::wfMemcKey
696 public function testWfMemcKey() {
697 $cache = ObjectCache
::getLocalClusterInstance();
699 $cache->makeKey( 'foo', 123, 'bar' ),
700 wfMemcKey( 'foo', 123, 'bar' )
705 * @covers ::wfForeignMemcKey
707 public function testWfForeignMemcKey() {
708 $cache = ObjectCache
::getLocalClusterInstance();
709 $keyspace = $this->readAttribute( $cache, 'keyspace' );
711 wfForeignMemcKey( $keyspace, '', 'foo', 'bar' ),
712 $cache->makeKey( 'foo', 'bar' )
717 * @covers ::wfGlobalCacheKey
719 public function testWfGlobalCacheKey() {
720 $cache = ObjectCache
::getLocalClusterInstance();
721 $this->hideDeprecated( 'wfGlobalCacheKey' );
723 $cache->makeGlobalKey( 'foo', 123, 'bar' ),
724 wfGlobalCacheKey( 'foo', 123, 'bar' )
728 public static function provideWfShellWikiCmdList() {
732 [ 'eval.php', [ '--help', '--test' ], [],
733 "'$wgPhpCli' 'eval.php' '--help' '--test'",
734 "Called eval.php --help --test" ],
735 [ 'eval.php', [ '--help', '--test space' ], [ 'php' => 'php5' ],
736 "'php5' 'eval.php' '--help' '--test space'",
737 "Called eval.php --help --test with php option" ],
738 [ 'eval.php', [ '--help', '--test', 'X' ], [ 'wrapper' => 'MWScript.php' ],
739 "'$wgPhpCli' 'MWScript.php' 'eval.php' '--help' '--test' 'X'",
740 "Called eval.php --help --test with wrapper option" ],
743 [ '--help', '--test', 'y' ],
744 [ 'php' => 'php5', 'wrapper' => 'MWScript.php' ],
745 "'php5' 'MWScript.php' 'eval.php' '--help' '--test' 'y'",
746 "Called eval.php --help --test with wrapper and php option"
750 /* @todo many more! */