4 * @group GlobalFunctions
6 class GlobalTest
extends MediaWikiTestCase
{
7 protected function setUp() {
10 $readOnlyFile = $this->getNewTempFile();
11 unlink( $readOnlyFile );
13 $this->setMwGlobals( array(
14 'wgReadOnlyFile' => $readOnlyFile,
15 'wgUrlProtocols' => array(
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 array( array( 'a' ), array( 'a', 'b', 'c' ) ),
46 array( array( 'a' ), array( 'a', 'b' ) ),
47 array( 1 => array( '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() {
137 array( array(), '' ), // empty
138 array( array( 'foo' => 'bar' ), 'foo=bar' ), // string test
139 array( array( 'foo' => '' ), 'foo=' ), // empty string test
140 array( array( 'foo' => 1 ), 'foo=1' ), // number test
141 array( array( 'foo' => true ), 'foo=1' ), // true test
142 array( array( 'foo' => false ), '' ), // false test
143 array( array( 'foo' => null ), '' ), // null test
144 array( array( 'foo' => 'A&B=5+6@!"\'' ), 'foo=A%26B%3D5%2B6%40%21%22%27' ), // urlencoding test
146 array( 'foo' => 'bar', 'baz' => 'is', 'asdf' => 'qwerty' ),
147 'foo=bar&baz=is&asdf=qwerty'
148 ), // multi-item test
149 array( array( 'foo' => array( 'bar' => 'baz' ) ), 'foo%5Bbar%5D=baz' ),
151 array( 'foo' => array( 'bar' => 'baz', 'qwerty' => 'asdf' ) ),
152 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf'
154 array( array( 'foo' => array( 'bar', 'baz' ) ), 'foo%5B0%5D=bar&foo%5B1%5D=baz' ),
156 array( 'foo' => array( 'bar' => array( '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() {
177 array( 'baz' => 'bar' ),
178 array( 'foo' => 'bar', 'baz' => 'overridden value' ) ) );
181 public static function provideCgiToArray() {
183 array( '', array() ), // empty
184 array( 'foo=bar', array( 'foo' => 'bar' ) ), // string
185 array( 'foo=', array( 'foo' => '' ) ), // empty string
186 array( 'foo', array( 'foo' => '' ) ), // missing =
187 array( 'foo=bar&qwerty=asdf', array( 'foo' => 'bar', 'qwerty' => 'asdf' ) ), // multiple value
188 array( 'foo=A%26B%3D5%2B6%40%21%22%27', array( 'foo' => 'A&B=5+6@!"\'' ) ), // urldecoding test
189 array( 'foo%5Bbar%5D=baz', array( 'foo' => array( 'bar' => 'baz' ) ) ),
191 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf',
192 array( 'foo' => array( 'bar' => 'baz', 'qwerty' => 'asdf' ) )
194 array( 'foo%5B0%5D=bar&foo%5B1%5D=baz', array( 'foo' => array( 0 => 'bar', 1 => 'baz' ) ) ),
196 'foo%5Bbar%5D%5Bbar%5D=baz',
197 array( 'foo' => array( 'bar' => array( '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 array( 'foo=bar&baz=biz' ),
216 array( 'foo=A%26B%3D5%2B6%40%21%22%27' ),
217 array( 'foo%5Bbar%5D=baz' ),
218 array( 'foo%5B0%5D=bar&foo%5B1%5D=baz' ),
219 array( '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 array( 'application/xhtml+xml' => 1.0,
240 'text/plain' => 0.3 ) ) );
243 mimeTypeMatch( 'text/html',
244 array( 'image/*' => 1.0,
245 'text/*' => 0.5 ) ) );
248 mimeTypeMatch( 'text/html',
249 array( '*/*' => 1.0 ) ) );
251 mimeTypeMatch( 'text/html',
252 array( 'image/png' => 1.0,
253 'image/svg+xml' => 0.5 ) ) );
257 * @covers ::wfNegotiateType
259 public function testNegotiateType() {
263 array( 'application/xhtml+xml' => 1.0,
267 array( 'text/html' => 1.0 ) ) );
269 'application/xhtml+xml',
271 array( 'application/xhtml+xml' => 1.0,
275 array( 'application/xhtml+xml' => 1.0,
276 'text/html' => 0.5 ) ) );
280 array( 'text/html' => 1.0,
283 'application/xhtml+xml' => 0.2 ),
284 array( 'application/xhtml+xml' => 1.0,
285 'text/html' => 0.5 ) ) );
289 array( 'text/*' => 1.0,
292 array( 'application/xhtml+xml' => 1.0,
293 'text/html' => 0.5 ) ) );
296 array( 'text/*' => 1.0 ),
297 array( 'application/xhtml+xml' => 1.0 ) ) );
302 * @covers ::wfDebugMem
304 public function testDebugFunctionTest() {
305 $debugLogFile = $this->getNewTempFile();
307 $this->setMwGlobals( array(
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 array( 6 / 7, '0.86%', 2, false ),
384 array( 3 / 3, '1%' ),
385 array( 22 / 7, '3.14286%', 5 ),
386 array( 3 / 6, '0.5%' ),
387 array( 1 / 3, '0%', 0 ),
388 array( 10 / 3, '0%', -1 ),
389 array( 3 / 4 / 5, '0.1%', 1 ),
390 array( 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 /** array( shorthand, expected integer ) */
417 public static function provideShorthand() {
424 # Failures returns 0 :(
425 array( 'ABCDEFG', 0 ),
428 # Int, strings with spaces
432 array( ' 1023 ', 1023 ),
437 array( '1m', 1024 * 1024 ),
438 array( '1M', 1024 * 1024 ),
439 array( '1g', 1024 * 1024 * 1024 ),
440 array( '1G', 1024 * 1024 * 1024 ),
445 array( '-500', -500 ),
446 array( '-1k', -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->checkHasDiff3();
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 array( '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 array( '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 array( 'file://./whatever/you/like.txt' )
573 // three slashes: local filesystem path Windows-style
574 'file:///c:/whatever/you/like.txt',
575 array( 'file://./c:/whatever/you/like.txt' )
578 // two slashes: UNC filesystem path Windows-style
579 'file://intranet/whatever/you/like.txt',
580 array( '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 = array( 'HTTP' => 'http:', 'HTTPS' => 'https:', 'protocol-relative' => '' );
612 foreach ( $protocols as $pDesc => $p ) {
613 $a = array_merge( $a, array(
615 "$p//www.example.com",
618 "No matches for empty domains array, $pDesc URL"
621 "$p//www.example.com",
622 array( 'www.example.com' ),
624 "Exact match in domains array, $pDesc URL"
627 "$p//www.example.com",
628 array( 'example.com' ),
630 "Match without subdomain in domains array, $pDesc URL"
633 "$p//www.example2.com",
634 array( '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 array( 'example.com', 'example2.com', 'example3,com' ),
642 "Match without subdomain with other domains in array, $pDesc URL"
645 "$p//www.example4.com",
646 array( 'example.com', 'example2.com', 'example3,com' ),
648 "Domain not in array, $pDesc URL"
651 "$p//nds-nl.wikipedia.org",
652 array( '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( array(
691 'wgDBname' => 'example',
699 $this->setMwGlobals( array(
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 array( 'eval.php', array( '--help', '--test' ), array(),
739 "'$wgPhpCli' 'eval.php' '--help' '--test'",
740 "Called eval.php --help --test" ),
741 array( 'eval.php', array( '--help', '--test space' ), array( 'php' => 'php5' ),
742 "'php5' 'eval.php' '--help' '--test space'",
743 "Called eval.php --help --test with php option" ),
744 array( 'eval.php', array( '--help', '--test', 'X' ), array( 'wrapper' => 'MWScript.php' ),
745 "'$wgPhpCli' 'MWScript.php' 'eval.php' '--help' '--test' 'X'",
746 "Called eval.php --help --test with wrapper option" ),
749 array( '--help', '--test', 'y' ),
750 array( '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! */