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 );
33 * @dataProvider provideForWfArrayDiff2
34 * @covers ::wfArrayDiff2
36 public function testWfArrayDiff2( $a, $b, $expected ) {
38 wfArrayDiff2( $a, $b ), $expected
42 // @todo Provide more tests
43 public static function provideForWfArrayDiff2() {
52 array( array( 'a' ), array( 'a', 'b', 'c' ) ),
53 array( array( 'a' ), array( 'a', 'b' ) ),
54 array( 1 => array( 'a', 'b', 'c' ) ),
62 public function testRandom() {
63 # This could hypothetically fail, but it shouldn't ;)
65 wfRandom() == wfRandom() );
69 * @covers ::wfUrlencode
71 public function testUrlencode() {
73 "%E7%89%B9%E5%88%A5:Contributions/Foobar",
74 wfUrlencode( "\xE7\x89\xB9\xE5\x88\xA5:Contributions/Foobar" ) );
78 * @covers ::wfExpandIRI
80 public function testExpandIRI() {
82 "https://te.wikibooks.org/wiki/ఉబుంటు_వాడుకరి_మార్గదర్శని",
83 wfExpandIRI( "https://te.wikibooks.org/wiki/"
84 . "%E0%B0%89%E0%B0%AC%E0%B1%81%E0%B0%82%E0%B0%9F%E0%B1%81_"
85 . "%E0%B0%B5%E0%B0%BE%E0%B0%A1%E0%B1%81%E0%B0%95%E0%B0%B0%E0%B0%BF_"
86 . "%E0%B0%AE%E0%B0%BE%E0%B0%B0%E0%B1%8D%E0%B0%97%E0%B0%A6%E0%B0%B0"
87 . "%E0%B1%8D%E0%B0%B6%E0%B0%A8%E0%B0%BF" ) );
91 * @covers ::wfReadOnly
93 public function testReadOnlyEmpty() {
97 $this->assertFalse( wfReadOnly() );
98 $this->assertFalse( wfReadOnly() );
102 * @covers ::wfReadOnly
104 public function testReadOnlySet() {
105 global $wgReadOnly, $wgReadOnlyFile;
107 $f = fopen( $wgReadOnlyFile, "wt" );
108 fwrite( $f, 'Message' );
110 $wgReadOnly = null; # Check on $wgReadOnlyFile
112 $this->assertTrue( wfReadOnly() );
113 $this->assertTrue( wfReadOnly() ); # Check cached
115 unlink( $wgReadOnlyFile );
116 $wgReadOnly = null; # Clean cache
118 $this->assertFalse( wfReadOnly() );
119 $this->assertFalse( wfReadOnly() );
122 public static function provideArrayToCGI() {
124 array( array(), '' ), // empty
125 array( array( 'foo' => 'bar' ), 'foo=bar' ), // string test
126 array( array( 'foo' => '' ), 'foo=' ), // empty string test
127 array( array( 'foo' => 1 ), 'foo=1' ), // number test
128 array( array( 'foo' => true ), 'foo=1' ), // true test
129 array( array( 'foo' => false ), '' ), // false test
130 array( array( 'foo' => null ), '' ), // null test
131 array( array( 'foo' => 'A&B=5+6@!"\'' ), 'foo=A%26B%3D5%2B6%40%21%22%27' ), // urlencoding test
133 array( 'foo' => 'bar', 'baz' => 'is', 'asdf' => 'qwerty' ),
134 'foo=bar&baz=is&asdf=qwerty'
135 ), // multi-item test
136 array( array( 'foo' => array( 'bar' => 'baz' ) ), 'foo%5Bbar%5D=baz' ),
138 array( 'foo' => array( 'bar' => 'baz', 'qwerty' => 'asdf' ) ),
139 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf'
141 array( array( 'foo' => array( 'bar', 'baz' ) ), 'foo%5B0%5D=bar&foo%5B1%5D=baz' ),
143 array( 'foo' => array( 'bar' => array( 'bar' => 'baz' ) ) ),
144 'foo%5Bbar%5D%5Bbar%5D=baz'
150 * @dataProvider provideArrayToCGI
151 * @covers ::wfArrayToCgi
153 public function testArrayToCGI( $array, $result ) {
154 $this->assertEquals( $result, wfArrayToCgi( $array ) );
158 * @covers ::wfArrayToCgi
160 public function testArrayToCGI2() {
164 array( 'baz' => 'bar' ),
165 array( 'foo' => 'bar', 'baz' => 'overridden value' ) ) );
168 public static function provideCgiToArray() {
170 array( '', array() ), // empty
171 array( 'foo=bar', array( 'foo' => 'bar' ) ), // string
172 array( 'foo=', array( 'foo' => '' ) ), // empty string
173 array( 'foo', array( 'foo' => '' ) ), // missing =
174 array( 'foo=bar&qwerty=asdf', array( 'foo' => 'bar', 'qwerty' => 'asdf' ) ), // multiple value
175 array( 'foo=A%26B%3D5%2B6%40%21%22%27', array( 'foo' => 'A&B=5+6@!"\'' ) ), // urldecoding test
176 array( 'foo%5Bbar%5D=baz', array( 'foo' => array( 'bar' => 'baz' ) ) ),
178 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf',
179 array( 'foo' => array( 'bar' => 'baz', 'qwerty' => 'asdf' ) )
181 array( 'foo%5B0%5D=bar&foo%5B1%5D=baz', array( 'foo' => array( 0 => 'bar', 1 => 'baz' ) ) ),
183 'foo%5Bbar%5D%5Bbar%5D=baz',
184 array( 'foo' => array( 'bar' => array( 'bar' => 'baz' ) ) )
190 * @dataProvider provideCgiToArray
191 * @covers ::wfCgiToArray
193 public function testCgiToArray( $cgi, $result ) {
194 $this->assertEquals( $result, wfCgiToArray( $cgi ) );
197 public static function provideCgiRoundTrip() {
202 array( 'foo=bar&baz=biz' ),
203 array( 'foo=A%26B%3D5%2B6%40%21%22%27' ),
204 array( 'foo%5Bbar%5D=baz' ),
205 array( 'foo%5B0%5D=bar&foo%5B1%5D=baz' ),
206 array( 'foo%5Bbar%5D%5Bbar%5D=baz' ),
211 * @dataProvider provideCgiRoundTrip
212 * @covers ::wfArrayToCgi
214 public function testCgiRoundTrip( $cgi ) {
215 $this->assertEquals( $cgi, wfArrayToCgi( wfCgiToArray( $cgi ) ) );
219 * @covers ::mimeTypeMatch
221 public function testMimeTypeMatch() {
224 mimeTypeMatch( 'text/html',
225 array( 'application/xhtml+xml' => 1.0,
227 'text/plain' => 0.3 ) ) );
230 mimeTypeMatch( 'text/html',
231 array( 'image/*' => 1.0,
232 'text/*' => 0.5 ) ) );
235 mimeTypeMatch( 'text/html',
236 array( '*/*' => 1.0 ) ) );
238 mimeTypeMatch( 'text/html',
239 array( 'image/png' => 1.0,
240 'image/svg+xml' => 0.5 ) ) );
244 * @covers ::wfNegotiateType
246 public function testNegotiateType() {
250 array( 'application/xhtml+xml' => 1.0,
254 array( 'text/html' => 1.0 ) ) );
256 'application/xhtml+xml',
258 array( 'application/xhtml+xml' => 1.0,
262 array( 'application/xhtml+xml' => 1.0,
263 'text/html' => 0.5 ) ) );
267 array( 'text/html' => 1.0,
270 'application/xhtml+xml' => 0.2 ),
271 array( 'application/xhtml+xml' => 1.0,
272 'text/html' => 0.5 ) ) );
276 array( 'text/*' => 1.0,
279 array( 'application/xhtml+xml' => 1.0,
280 'text/html' => 0.5 ) ) );
283 array( 'text/*' => 1.0 ),
284 array( 'application/xhtml+xml' => 1.0 ) ) );
289 * @covers ::wfDebugMem
291 public function testDebugFunctionTest() {
293 global $wgDebugLogFile, $wgDebugTimestamps;
295 $old_log_file = $wgDebugLogFile;
296 $wgDebugLogFile = tempnam( wfTempDir(), 'mw-' );
297 # @todo FIXME: $wgDebugTimestamps should be tested
298 $old_wgDebugTimestamps = $wgDebugTimestamps;
299 $wgDebugTimestamps = false;
301 wfDebug( "This is a normal string" );
302 $this->assertEquals( "This is a normal string", file_get_contents( $wgDebugLogFile ) );
303 unlink( $wgDebugLogFile );
305 wfDebug( "This is nöt an ASCII string" );
306 $this->assertEquals( "This is nöt an ASCII string", file_get_contents( $wgDebugLogFile ) );
307 unlink( $wgDebugLogFile );
309 wfDebug( "\00305This has böth UTF and control chars\003" );
311 " 05This has böth UTF and control chars ",
312 file_get_contents( $wgDebugLogFile )
314 unlink( $wgDebugLogFile );
317 $this->assertGreaterThan(
319 preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) )
321 unlink( $wgDebugLogFile );
324 $this->assertGreaterThan(
326 preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) )
328 unlink( $wgDebugLogFile );
330 $wgDebugLogFile = $old_log_file;
331 $wgDebugTimestamps = $old_wgDebugTimestamps;
335 * @covers ::wfClientAcceptsGzip
337 public function testClientAcceptsGzipTest() {
343 'compress, gzip' => true,
344 'gzip;q=1.0' => true,
347 'gzip;q=abcde' => true, //is this REALLY valid?
348 'gzip;q=12345678.9' => true,
352 if ( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) {
353 $old_server_setting = $_SERVER['HTTP_ACCEPT_ENCODING'];
356 foreach ( $settings as $encoding => $expect ) {
357 $_SERVER['HTTP_ACCEPT_ENCODING'] = $encoding;
359 $this->assertEquals( $expect, wfClientAcceptsGzip( true ),
360 "'$encoding' => " . wfBoolToStr( $expect ) );
363 if ( isset( $old_server_setting ) ) {
364 $_SERVER['HTTP_ACCEPT_ENCODING'] = $old_server_setting;
371 public function testSwapVarsTest() {
375 $this->assertEquals( $var1, 1, 'var1 is set originally' );
376 $this->assertEquals( $var2, 2, 'var1 is set originally' );
378 swap( $var1, $var2 );
380 $this->assertEquals( $var1, 2, 'var1 is swapped' );
381 $this->assertEquals( $var2, 1, 'var2 is swapped' );
385 * @covers ::wfPercent
387 public function testWfPercentTest() {
390 array( 6 / 7, '0.86%', 2, false ),
391 array( 3 / 3, '1%' ),
392 array( 22 / 7, '3.14286%', 5 ),
393 array( 3 / 6, '0.5%' ),
394 array( 1 / 3, '0%', 0 ),
395 array( 10 / 3, '0%', -1 ),
396 array( 3 / 4 / 5, '0.1%', 1 ),
397 array( 6 / 7 * 8, '6.8571428571%', 10 ),
400 foreach ( $pcts as $pct ) {
401 if ( !isset( $pct[2] ) ) {
404 if ( !isset( $pct[3] ) ) {
408 $this->assertEquals( wfPercent( $pct[0], $pct[2], $pct[3] ), $pct[1], $pct[1] );
413 * test @see wfShorthandToInteger()
414 * @dataProvider provideShorthand
415 * @covers ::wfShorthandToInteger
417 public function testWfShorthandToInteger( $shorthand, $expected ) {
418 $this->assertEquals( $expected,
419 wfShorthandToInteger( $shorthand )
423 /** array( shorthand, expected integer ) */
424 public static function provideShorthand() {
431 # Failures returns 0 :(
432 array( 'ABCDEFG', 0 ),
435 # Int, strings with spaces
439 array( ' 1023 ', 1023 ),
444 array( '1m', 1024 * 1024 ),
445 array( '1M', 1024 * 1024 ),
446 array( '1g', 1024 * 1024 * 1024 ),
447 array( '1G', 1024 * 1024 * 1024 ),
452 array( '-500', -500 ),
453 array( '-1k', -1024 ),
468 * @param string $old Text as it was in the database
469 * @param string $mine Text submitted while user was editing
470 * @param string $yours Text submitted by the user
471 * @param bool $expectedMergeResult Whether the merge should be a success
472 * @param string $expectedText Text after merge has been completed
474 * @dataProvider provideMerge()
478 public function testMerge( $old, $mine, $yours, $expectedMergeResult, $expectedText ) {
479 $this->checkHasDiff3();
482 $isMerged = wfMerge( $old, $mine, $yours, $mergedText );
484 $msg = 'Merge should be a ';
485 $msg .= $expectedMergeResult ?
'success' : 'failure';
486 $this->assertEquals( $expectedMergeResult, $isMerged, $msg );
489 // Verify the merged text
490 $this->assertEquals( $expectedText, $mergedText,
491 'is merged text as expected?' );
495 public static function provideMerge() {
496 $EXPECT_MERGE_SUCCESS = true;
497 $EXPECT_MERGE_FAILURE = false;
503 "one one one\n" . // trimmed
508 "one one one ONE ONE\n" .
510 "two two two\n", // with tailing whitespace
515 "two two TWO TWO", // trimmed
518 $EXPECT_MERGE_SUCCESS,
521 "one one one ONE ONE\n" .
523 "two two TWO TWO\n", // note: will always end in a newline
526 // #1: conflict, fail
529 "one one one", // trimmed
532 "one one one ONE ONE\n" .
535 "\n", // with tailing whitespace
540 "two two", // trimmed
542 $EXPECT_MERGE_FAILURE,
551 * @dataProvider provideMakeUrlIndexes()
552 * @covers ::wfMakeUrlIndexes
554 public function testMakeUrlIndexes( $url, $expected ) {
555 $index = wfMakeUrlIndexes( $url );
556 $this->assertEquals( $expected, $index, "wfMakeUrlIndexes(\"$url\")" );
559 public static function provideMakeUrlIndexes() {
563 'https://bugzilla.wikimedia.org/show_bug.cgi?id=28627',
564 array( 'https://org.wikimedia.bugzilla./show_bug.cgi?id=28627' )
567 // mailtos are handled special
568 // is this really right though? that final . probably belongs earlier?
569 'mailto:wiki@wikimedia.org',
570 array( 'mailto:org.wikimedia@wiki.' )
573 // file URL cases per bug 28627...
575 // three slashes: local filesystem path Unix-style
576 'file:///whatever/you/like.txt',
577 array( 'file://./whatever/you/like.txt' )
580 // three slashes: local filesystem path Windows-style
581 'file:///c:/whatever/you/like.txt',
582 array( 'file://./c:/whatever/you/like.txt' )
585 // two slashes: UNC filesystem path Windows-style
586 'file://intranet/whatever/you/like.txt',
587 array( 'file://intranet./whatever/you/like.txt' )
589 // Multiple-slash cases that can sorta work on Mozilla
590 // if you hack it just right are kinda pathological,
591 // and unreliable cross-platform or on IE which means they're
592 // unlikely to appear on intranets.
594 // Those will survive the algorithm but with results that
595 // are less consistent.
597 // protocol-relative URL cases per bug 29854...
599 '//bugzilla.wikimedia.org/show_bug.cgi?id=28627',
601 'http://org.wikimedia.bugzilla./show_bug.cgi?id=28627',
602 'https://org.wikimedia.bugzilla./show_bug.cgi?id=28627'
609 * @dataProvider provideWfMatchesDomainList
610 * @covers ::wfMatchesDomainList
612 public function testWfMatchesDomainList( $url, $domains, $expected, $description ) {
613 $actual = wfMatchesDomainList( $url, $domains );
614 $this->assertEquals( $expected, $actual, $description );
617 public static function provideWfMatchesDomainList() {
619 $protocols = array( 'HTTP' => 'http:', 'HTTPS' => 'https:', 'protocol-relative' => '' );
620 foreach ( $protocols as $pDesc => $p ) {
621 $a = array_merge( $a, array(
623 "$p//www.example.com",
626 "No matches for empty domains array, $pDesc URL"
629 "$p//www.example.com",
630 array( 'www.example.com' ),
632 "Exact match in domains array, $pDesc URL"
635 "$p//www.example.com",
636 array( 'example.com' ),
638 "Match without subdomain in domains array, $pDesc URL"
641 "$p//www.example2.com",
642 array( 'www.example.com', 'www.example2.com', 'www.example3.com' ),
644 "Exact match with other domains in array, $pDesc URL"
647 "$p//www.example2.com",
648 array( 'example.com', 'example2.com', 'example3,com' ),
650 "Match without subdomain with other domains in array, $pDesc URL"
653 "$p//www.example4.com",
654 array( 'example.com', 'example2.com', 'example3,com' ),
656 "Domain not in array, $pDesc URL"
659 "$p//nds-nl.wikipedia.org",
660 array( 'nl.wikipedia.org' ),
662 "Non-matching substring of domain, $pDesc URL"
671 * @covers ::wfMkdirParents
673 public function testWfMkdirParents() {
674 // Should not return true if file exists instead of directory
675 $fname = $this->getNewTempFile();
676 wfSuppressWarnings();
677 $ok = wfMkdirParents( $fname );
679 $this->assertFalse( $ok );
683 * @dataProvider provideWfShellMaintenanceCmdList
684 * @covers ::wfShellMaintenanceCmd
686 public function testWfShellMaintenanceCmd( $script, $parameters, $options,
687 $expected, $description
689 if ( wfIsWindows() ) {
690 // Approximation that's good enough for our purposes just now
691 $expected = str_replace( "'", '"', $expected );
693 $actual = wfShellMaintenanceCmd( $script, $parameters, $options );
694 $this->assertEquals( $expected, $actual, $description );
697 public static function provideWfShellMaintenanceCmdList() {
701 array( 'eval.php', array( '--help', '--test' ), array(),
702 "'$wgPhpCli' 'eval.php' '--help' '--test'",
703 "Called eval.php --help --test" ),
704 array( 'eval.php', array( '--help', '--test space' ), array( 'php' => 'php5' ),
705 "'php5' 'eval.php' '--help' '--test space'",
706 "Called eval.php --help --test with php option" ),
707 array( 'eval.php', array( '--help', '--test', 'X' ), array( 'wrapper' => 'MWScript.php' ),
708 "'$wgPhpCli' 'MWScript.php' 'eval.php' '--help' '--test' 'X'",
709 "Called eval.php --help --test with wrapper option" ),
712 array( '--help', '--test', 'y' ),
713 array( 'php' => 'php5', 'wrapper' => 'MWScript.php' ),
714 "'php5' 'MWScript.php' 'eval.php' '--help' '--test' 'y'",
715 "Called eval.php --help --test with wrapper and php option"
719 /* @TODO many more! */