3 class GlobalTest
extends MediaWikiTestCase
{
4 protected function setUp() {
7 $readOnlyFile = tempnam( wfTempDir(), "mwtest_readonly" );
8 unlink( $readOnlyFile );
10 $this->setMwGlobals( array(
11 'wgReadOnlyFile' => $readOnlyFile,
12 'wgUrlProtocols' => array(
17 'file://', # Non-default
22 protected function tearDown() {
23 global $wgReadOnlyFile;
25 if ( file_exists( $wgReadOnlyFile ) ) {
26 unlink( $wgReadOnlyFile );
32 /** @dataProvider provideForWfArrayDiff2 */
33 public function testWfArrayDiff2( $a, $b, $expected ) {
35 wfArrayDiff2( $a, $b ), $expected
39 // @todo Provide more tests
40 public static function provideForWfArrayDiff2() {
49 array( array( 'a' ), array( 'a', 'b', 'c' ) ),
50 array( array( 'a' ), array( 'a', 'b' ) ),
51 array( 1 => array( 'a', 'b', 'c' ) ),
56 function testRandom() {
57 # This could hypothetically fail, but it shouldn't ;)
59 wfRandom() == wfRandom() );
62 function testUrlencode() {
64 "%E7%89%B9%E5%88%A5:Contributions/Foobar",
65 wfUrlencode( "\xE7\x89\xB9\xE5\x88\xA5:Contributions/Foobar" ) );
68 function testExpandIRI() {
70 "https://te.wikibooks.org/wiki/ఉబుంటు_వాడుకరి_మార్గదర్శని",
71 wfExpandIRI( "https://te.wikibooks.org/wiki/%E0%B0%89%E0%B0%AC%E0%B1%81%E0%B0%82%E0%B0%9F%E0%B1%81_%E0%B0%B5%E0%B0%BE%E0%B0%A1%E0%B1%81%E0%B0%95%E0%B0%B0%E0%B0%BF_%E0%B0%AE%E0%B0%BE%E0%B0%B0%E0%B1%8D%E0%B0%97%E0%B0%A6%E0%B0%B0%E0%B1%8D%E0%B0%B6%E0%B0%A8%E0%B0%BF" ) );
74 function testReadOnlyEmpty() {
78 $this->assertFalse( wfReadOnly() );
79 $this->assertFalse( wfReadOnly() );
82 function testReadOnlySet() {
83 global $wgReadOnly, $wgReadOnlyFile;
85 $f = fopen( $wgReadOnlyFile, "wt" );
86 fwrite( $f, 'Message' );
88 $wgReadOnly = null; # Check on $wgReadOnlyFile
90 $this->assertTrue( wfReadOnly() );
91 $this->assertTrue( wfReadOnly() ); # Check cached
93 unlink( $wgReadOnlyFile );
94 $wgReadOnly = null; # Clean cache
96 $this->assertFalse( wfReadOnly() );
97 $this->assertFalse( wfReadOnly() );
100 function testQuotedPrintable() {
102 "=?UTF-8?Q?=C4=88u=20legebla=3F?=",
103 UserMailer
::quotedPrintable( "\xc4\x88u legebla?", "UTF-8" ) );
106 function testTime() {
108 $this->assertInternalType( 'float', $start );
110 $this->assertTrue( $end > $start, "Time is running backwards!" );
113 public static function provideArrayToCGI() {
115 array( array(), '' ), // empty
116 array( array( 'foo' => 'bar' ), 'foo=bar' ), // string test
117 array( array( 'foo' => '' ), 'foo=' ), // empty string test
118 array( array( 'foo' => 1 ), 'foo=1' ), // number test
119 array( array( 'foo' => true ), 'foo=1' ), // true test
120 array( array( 'foo' => false ), '' ), // false test
121 array( array( 'foo' => null ), '' ), // null test
122 array( array( 'foo' => 'A&B=5+6@!"\'' ), 'foo=A%26B%3D5%2B6%40%21%22%27' ), // urlencoding test
123 array( array( 'foo' => 'bar', 'baz' => 'is', 'asdf' => 'qwerty' ), 'foo=bar&baz=is&asdf=qwerty' ), // multi-item test
124 array( array( 'foo' => array( 'bar' => 'baz' ) ), 'foo%5Bbar%5D=baz' ),
125 array( array( 'foo' => array( 'bar' => 'baz', 'qwerty' => 'asdf' ) ), 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf' ),
126 array( array( 'foo' => array( 'bar', 'baz' ) ), 'foo%5B0%5D=bar&foo%5B1%5D=baz' ),
127 array( array( 'foo' => array( 'bar' => array( 'bar' => 'baz' ) ) ), 'foo%5Bbar%5D%5Bbar%5D=baz' ),
132 * @dataProvider provideArrayToCGI
134 function testArrayToCGI( $array, $result ) {
135 $this->assertEquals( $result, wfArrayToCgi( $array ) );
139 function testArrayToCGI2() {
143 array( 'baz' => 'bar' ),
144 array( 'foo' => 'bar', 'baz' => 'overridden value' ) ) );
147 public static function provideCgiToArray() {
149 array( '', array() ), // empty
150 array( 'foo=bar', array( 'foo' => 'bar' ) ), // string
151 array( 'foo=', array( 'foo' => '' ) ), // empty string
152 array( 'foo', array( 'foo' => '' ) ), // missing =
153 array( 'foo=bar&qwerty=asdf', array( 'foo' => 'bar', 'qwerty' => 'asdf' ) ), // multiple value
154 array( 'foo=A%26B%3D5%2B6%40%21%22%27', array( 'foo' => 'A&B=5+6@!"\'' ) ), // urldecoding test
155 array( 'foo%5Bbar%5D=baz', array( 'foo' => array( 'bar' => 'baz' ) ) ),
156 array( 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf', array( 'foo' => array( 'bar' => 'baz', 'qwerty' => 'asdf' ) ) ),
157 array( 'foo%5B0%5D=bar&foo%5B1%5D=baz', array( 'foo' => array( 0 => 'bar', 1 => 'baz' ) ) ),
158 array( 'foo%5Bbar%5D%5Bbar%5D=baz', array( 'foo' => array( 'bar' => array( 'bar' => 'baz' ) ) ) ),
163 * @dataProvider provideCgiToArray
165 function testCgiToArray( $cgi, $result ) {
166 $this->assertEquals( $result, wfCgiToArray( $cgi ) );
169 public static function provideCgiRoundTrip() {
174 array( 'foo=bar&baz=biz' ),
175 array( 'foo=A%26B%3D5%2B6%40%21%22%27' ),
176 array( 'foo%5Bbar%5D=baz' ),
177 array( 'foo%5B0%5D=bar&foo%5B1%5D=baz' ),
178 array( 'foo%5Bbar%5D%5Bbar%5D=baz' ),
183 * @dataProvider provideCgiRoundTrip
185 function testCgiRoundTrip( $cgi ) {
186 $this->assertEquals( $cgi, wfArrayToCgi( wfCgiToArray( $cgi ) ) );
189 function testMimeTypeMatch() {
192 mimeTypeMatch( 'text/html',
193 array( 'application/xhtml+xml' => 1.0,
195 'text/plain' => 0.3 ) ) );
198 mimeTypeMatch( 'text/html',
199 array( 'image/*' => 1.0,
200 'text/*' => 0.5 ) ) );
203 mimeTypeMatch( 'text/html',
204 array( '*/*' => 1.0 ) ) );
206 mimeTypeMatch( 'text/html',
207 array( 'image/png' => 1.0,
208 'image/svg+xml' => 0.5 ) ) );
211 function testNegotiateType() {
215 array( 'application/xhtml+xml' => 1.0,
219 array( 'text/html' => 1.0 ) ) );
221 'application/xhtml+xml',
223 array( 'application/xhtml+xml' => 1.0,
227 array( 'application/xhtml+xml' => 1.0,
228 'text/html' => 0.5 ) ) );
232 array( 'text/html' => 1.0,
235 'application/xhtml+xml' => 0.2 ),
236 array( 'application/xhtml+xml' => 1.0,
237 'text/html' => 0.5 ) ) );
241 array( 'text/*' => 1.0,
244 array( 'application/xhtml+xml' => 1.0,
245 'text/html' => 0.5 ) ) );
248 array( 'text/*' => 1.0 ),
249 array( 'application/xhtml+xml' => 1.0 ) ) );
252 function testFallbackMbstringFunctions() {
254 if ( !extension_loaded( 'mbstring' ) ) {
255 $this->markTestSkipped( "The mb_string functions must be installed to test the fallback functions" );
258 $sampleUTF = "Östergötland_coat_of_arms.png";
262 $substr_params = array(
272 foreach ( $substr_params as $param_set ) {
273 $old_param_set = $param_set;
274 array_unshift( $param_set, $sampleUTF );
277 MWFunction
::callArray( 'mb_substr', $param_set ),
278 MWFunction
::callArray( 'Fallback::mb_substr', $param_set ),
279 'Fallback mb_substr with params ' . implode( ', ', $old_param_set )
286 mb_strlen( $sampleUTF ),
287 Fallback
::mb_strlen( $sampleUTF ),
293 $strpos_params = array(
297 //array( 'oat_', 100 ),
302 foreach ( $strpos_params as $param_set ) {
303 $old_param_set = $param_set;
304 array_unshift( $param_set, $sampleUTF );
307 MWFunction
::callArray( 'mb_strpos', $param_set ),
308 MWFunction
::callArray( 'Fallback::mb_strpos', $param_set ),
309 'Fallback mb_strpos with params ' . implode( ', ', $old_param_set )
313 MWFunction
::callArray( 'mb_strrpos', $param_set ),
314 MWFunction
::callArray( 'Fallback::mb_strrpos', $param_set ),
315 'Fallback mb_strrpos with params ' . implode( ', ', $old_param_set )
322 function testDebugFunctionTest() {
324 global $wgDebugLogFile, $wgDebugTimestamps;
326 $old_log_file = $wgDebugLogFile;
327 $wgDebugLogFile = tempnam( wfTempDir(), 'mw-' );
328 # @todo FIXME: $wgDebugTimestamps should be tested
329 $old_wgDebugTimestamps = $wgDebugTimestamps;
330 $wgDebugTimestamps = false;
333 wfDebug( "This is a normal string" );
334 $this->assertEquals( "This is a normal string", file_get_contents( $wgDebugLogFile ) );
335 unlink( $wgDebugLogFile );
337 wfDebug( "This is nöt an ASCII string" );
338 $this->assertEquals( "This is nöt an ASCII string", file_get_contents( $wgDebugLogFile ) );
339 unlink( $wgDebugLogFile );
342 wfDebug( "\00305This has böth UTF and control chars\003" );
343 $this->assertEquals( " 05This has böth UTF and control chars ", file_get_contents( $wgDebugLogFile ) );
344 unlink( $wgDebugLogFile );
347 $this->assertGreaterThan( 5000, preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) ) );
348 unlink( $wgDebugLogFile );
351 $this->assertGreaterThan( 5000000, preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) ) );
352 unlink( $wgDebugLogFile );
355 $wgDebugLogFile = $old_log_file;
356 $wgDebugTimestamps = $old_wgDebugTimestamps;
359 function testClientAcceptsGzipTest() {
365 'compress, gzip' => true,
366 'gzip;q=1.0' => true,
369 'gzip;q=abcde' => true, //is this REALLY valid?
370 'gzip;q=12345678.9' => true,
374 if ( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) {
375 $old_server_setting = $_SERVER['HTTP_ACCEPT_ENCODING'];
378 foreach ( $settings as $encoding => $expect ) {
379 $_SERVER['HTTP_ACCEPT_ENCODING'] = $encoding;
381 $this->assertEquals( $expect, wfClientAcceptsGzip( true ),
382 "'$encoding' => " . wfBoolToStr( $expect ) );
385 if ( isset( $old_server_setting ) ) {
386 $_SERVER['HTTP_ACCEPT_ENCODING'] = $old_server_setting;
390 function testSwapVarsTest() {
394 $this->assertEquals( $var1, 1, 'var1 is set originally' );
395 $this->assertEquals( $var2, 2, 'var1 is set originally' );
397 swap( $var1, $var2 );
399 $this->assertEquals( $var1, 2, 'var1 is swapped' );
400 $this->assertEquals( $var2, 1, 'var2 is swapped' );
404 function testWfPercentTest() {
407 array( 6 / 7, '0.86%', 2, false ),
408 array( 3 / 3, '1%' ),
409 array( 22 / 7, '3.14286%', 5 ),
410 array( 3 / 6, '0.5%' ),
411 array( 1 / 3, '0%', 0 ),
412 array( 10 / 3, '0%', -1 ),
413 array( 3 / 4 / 5, '0.1%', 1 ),
414 array( 6 / 7 * 8, '6.8571428571%', 10 ),
417 foreach ( $pcts as $pct ) {
418 if ( !isset( $pct[2] ) ) {
421 if ( !isset( $pct[3] ) ) {
425 $this->assertEquals( wfPercent( $pct[0], $pct[2], $pct[3] ), $pct[1], $pct[1] );
430 * test @see wfShorthandToInteger()
431 * @dataProvider provideShorthand
433 public function testWfShorthandToInteger( $shorthand, $expected ) {
434 $this->assertEquals( $expected,
435 wfShorthandToInteger( $shorthand )
439 /** array( shorthand, expected integer ) */
440 public static function provideShorthand() {
447 # Failures returns 0 :(
448 array( 'ABCDEFG', 0 ),
451 # Int, strings with spaces
455 array( ' 1023 ', 1023 ),
460 array( '1m', 1024 * 1024 ),
461 array( '1M', 1024 * 1024 ),
462 array( '1g', 1024 * 1024 * 1024 ),
463 array( '1G', 1024 * 1024 * 1024 ),
468 array( '-500', -500 ),
469 array( '-1k', -1024 ),
484 * @param String $old: Text as it was in the database
485 * @param String $mine: Text submitted while user was editing
486 * @param String $yours: Text submitted by the user
487 * @param Boolean $expectedMergeResult Whether the merge should be a success
488 * @param String $expectedText: Text after merge has been completed
490 * @dataProvider provideMerge()
493 public function testMerge( $old, $mine, $yours, $expectedMergeResult, $expectedText ) {
494 $this->checkHasDiff3();
497 $isMerged = wfMerge( $old, $mine, $yours, $mergedText );
499 $msg = 'Merge should be a ';
500 $msg .= $expectedMergeResult ?
'success' : 'failure';
501 $this->assertEquals( $expectedMergeResult, $isMerged, $msg );
504 // Verify the merged text
505 $this->assertEquals( $expectedText, $mergedText,
506 'is merged text as expected?' );
510 public static function provideMerge() {
511 $EXPECT_MERGE_SUCCESS = true;
512 $EXPECT_MERGE_FAILURE = false;
518 "one one one\n" . // trimmed
523 "one one one ONE ONE\n" .
525 "two two two\n", // with tailing whitespace
530 "two two TWO TWO", // trimmed
533 $EXPECT_MERGE_SUCCESS,
536 "one one one ONE ONE\n" .
538 "two two TWO TWO\n", // note: will always end in a newline
541 // #1: conflict, fail
544 "one one one", // trimmed
547 "one one one ONE ONE\n" .
550 "\n", // with tailing whitespace
555 "two two", // trimmed
557 $EXPECT_MERGE_FAILURE,
566 * @dataProvider provideMakeUrlIndexes()
568 function testMakeUrlIndexes( $url, $expected ) {
569 $index = wfMakeUrlIndexes( $url );
570 $this->assertEquals( $expected, $index, "wfMakeUrlIndexes(\"$url\")" );
573 function provideMakeUrlIndexes() {
577 'https://bugzilla.wikimedia.org/show_bug.cgi?id=28627',
578 array( 'https://org.wikimedia.bugzilla./show_bug.cgi?id=28627' )
581 // mailtos are handled special
582 // is this really right though? that final . probably belongs earlier?
583 'mailto:wiki@wikimedia.org',
584 array( 'mailto:org.wikimedia@wiki.' )
587 // file URL cases per bug 28627...
589 // three slashes: local filesystem path Unix-style
590 'file:///whatever/you/like.txt',
591 array( 'file://./whatever/you/like.txt' )
594 // three slashes: local filesystem path Windows-style
595 'file:///c:/whatever/you/like.txt',
596 array( 'file://./c:/whatever/you/like.txt' )
599 // two slashes: UNC filesystem path Windows-style
600 'file://intranet/whatever/you/like.txt',
601 array( 'file://intranet./whatever/you/like.txt' )
603 // Multiple-slash cases that can sorta work on Mozilla
604 // if you hack it just right are kinda pathological,
605 // and unreliable cross-platform or on IE which means they're
606 // unlikely to appear on intranets.
608 // Those will survive the algorithm but with results that
609 // are less consistent.
611 // protocol-relative URL cases per bug 29854...
613 '//bugzilla.wikimedia.org/show_bug.cgi?id=28627',
615 'http://org.wikimedia.bugzilla./show_bug.cgi?id=28627',
616 'https://org.wikimedia.bugzilla./show_bug.cgi?id=28627'
623 * @dataProvider provideWfMatchesDomainList
625 function testWfMatchesDomainList( $url, $domains, $expected, $description ) {
626 $actual = wfMatchesDomainList( $url, $domains );
627 $this->assertEquals( $expected, $actual, $description );
630 function provideWfMatchesDomainList() {
632 $protocols = array( 'HTTP' => 'http:', 'HTTPS' => 'https:', 'protocol-relative' => '' );
633 foreach ( $protocols as $pDesc => $p ) {
634 $a = array_merge( $a, array(
635 array( "$p//www.example.com", array(), false, "No matches for empty domains array, $pDesc URL" ),
636 array( "$p//www.example.com", array( 'www.example.com' ), true, "Exact match in domains array, $pDesc URL" ),
637 array( "$p//www.example.com", array( 'example.com' ), true, "Match without subdomain in domains array, $pDesc URL" ),
638 array( "$p//www.example2.com", array( 'www.example.com', 'www.example2.com', 'www.example3.com' ), true, "Exact match with other domains in array, $pDesc URL" ),
639 array( "$p//www.example2.com", array( 'example.com', 'example2.com', 'example3,com' ), true, "Match without subdomain with other domains in array, $pDesc URL" ),
640 array( "$p//www.example4.com", array( 'example.com', 'example2.com', 'example3,com' ), false, "Domain not in array, $pDesc URL" ),
642 // FIXME: This is a bug in wfMatchesDomainList(). If and when this is fixed, update this test case
643 array( "$p//nds-nl.wikipedia.org", array( 'nl.wikipedia.org' ), true, "Substrings of domains match while they shouldn't, $pDesc URL" ),
650 * @dataProvider provideWfShellMaintenanceCmdList
652 function testWfShellMaintenanceCmd( $script, $parameters, $options, $expected, $description ) {
653 if ( wfIsWindows() ) {
654 // Approximation that's good enough for our purposes just now
655 $expected = str_replace( "'", '"', $expected );
657 $actual = wfShellMaintenanceCmd( $script, $parameters, $options );
658 $this->assertEquals( $expected, $actual, $description );
661 function provideWfShellMaintenanceCmdList() {
664 array( 'eval.php', array( '--help', '--test' ), array(),
665 "'$wgPhpCli' 'eval.php' '--help' '--test'",
666 "Called eval.php --help --test" ),
667 array( 'eval.php', array( '--help', '--test space' ), array( 'php' => 'php5' ),
668 "'php5' 'eval.php' '--help' '--test space'",
669 "Called eval.php --help --test with php option" ),
670 array( 'eval.php', array( '--help', '--test', 'X' ), array( 'wrapper' => 'MWScript.php' ),
671 "'$wgPhpCli' 'MWScript.php' 'eval.php' '--help' '--test' 'X'",
672 "Called eval.php --help --test with wrapper option" ),
673 array( 'eval.php', array( '--help', '--test', 'y' ), array( 'php' => 'php5', 'wrapper' => 'MWScript.php' ),
674 "'php5' 'MWScript.php' 'eval.php' '--help' '--test' 'y'",
675 "Called eval.php --help --test with wrapper and php option" ),
678 /* TODO: many more! */