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() {
372 $this->hideDeprecated( 'swap' );
377 $this->assertEquals( $var1, 1, 'var1 is set originally' );
378 $this->assertEquals( $var2, 2, 'var1 is set originally' );
380 swap( $var1, $var2 );
382 $this->assertEquals( $var1, 2, 'var1 is swapped' );
383 $this->assertEquals( $var2, 1, 'var2 is swapped' );
387 * @covers ::wfPercent
389 public function testWfPercentTest() {
392 array( 6 / 7, '0.86%', 2, false ),
393 array( 3 / 3, '1%' ),
394 array( 22 / 7, '3.14286%', 5 ),
395 array( 3 / 6, '0.5%' ),
396 array( 1 / 3, '0%', 0 ),
397 array( 10 / 3, '0%', -1 ),
398 array( 3 / 4 / 5, '0.1%', 1 ),
399 array( 6 / 7 * 8, '6.8571428571%', 10 ),
402 foreach ( $pcts as $pct ) {
403 if ( !isset( $pct[2] ) ) {
406 if ( !isset( $pct[3] ) ) {
410 $this->assertEquals( wfPercent( $pct[0], $pct[2], $pct[3] ), $pct[1], $pct[1] );
415 * test @see wfShorthandToInteger()
416 * @dataProvider provideShorthand
417 * @covers ::wfShorthandToInteger
419 public function testWfShorthandToInteger( $shorthand, $expected ) {
420 $this->assertEquals( $expected,
421 wfShorthandToInteger( $shorthand )
425 /** array( shorthand, expected integer ) */
426 public static function provideShorthand() {
433 # Failures returns 0 :(
434 array( 'ABCDEFG', 0 ),
437 # Int, strings with spaces
441 array( ' 1023 ', 1023 ),
446 array( '1m', 1024 * 1024 ),
447 array( '1M', 1024 * 1024 ),
448 array( '1g', 1024 * 1024 * 1024 ),
449 array( '1G', 1024 * 1024 * 1024 ),
454 array( '-500', -500 ),
455 array( '-1k', -1024 ),
470 * @param string $old Text as it was in the database
471 * @param string $mine Text submitted while user was editing
472 * @param string $yours Text submitted by the user
473 * @param bool $expectedMergeResult Whether the merge should be a success
474 * @param string $expectedText Text after merge has been completed
476 * @dataProvider provideMerge()
480 public function testMerge( $old, $mine, $yours, $expectedMergeResult, $expectedText ) {
481 $this->checkHasDiff3();
484 $isMerged = wfMerge( $old, $mine, $yours, $mergedText );
486 $msg = 'Merge should be a ';
487 $msg .= $expectedMergeResult ?
'success' : 'failure';
488 $this->assertEquals( $expectedMergeResult, $isMerged, $msg );
491 // Verify the merged text
492 $this->assertEquals( $expectedText, $mergedText,
493 'is merged text as expected?' );
497 public static function provideMerge() {
498 $EXPECT_MERGE_SUCCESS = true;
499 $EXPECT_MERGE_FAILURE = false;
505 "one one one\n" . // trimmed
510 "one one one ONE ONE\n" .
512 "two two two\n", // with tailing whitespace
517 "two two TWO TWO", // trimmed
520 $EXPECT_MERGE_SUCCESS,
523 "one one one ONE ONE\n" .
525 "two two TWO TWO\n", // note: will always end in a newline
528 // #1: conflict, fail
531 "one one one", // trimmed
534 "one one one ONE ONE\n" .
537 "\n", // with tailing whitespace
542 "two two", // trimmed
544 $EXPECT_MERGE_FAILURE,
553 * @dataProvider provideMakeUrlIndexes()
554 * @covers ::wfMakeUrlIndexes
556 public function testMakeUrlIndexes( $url, $expected ) {
557 $index = wfMakeUrlIndexes( $url );
558 $this->assertEquals( $expected, $index, "wfMakeUrlIndexes(\"$url\")" );
561 public static function provideMakeUrlIndexes() {
565 'https://bugzilla.wikimedia.org/show_bug.cgi?id=28627',
566 array( 'https://org.wikimedia.bugzilla./show_bug.cgi?id=28627' )
569 // mailtos are handled special
570 // is this really right though? that final . probably belongs earlier?
571 'mailto:wiki@wikimedia.org',
572 array( 'mailto:org.wikimedia@wiki.' )
575 // file URL cases per bug 28627...
577 // three slashes: local filesystem path Unix-style
578 'file:///whatever/you/like.txt',
579 array( 'file://./whatever/you/like.txt' )
582 // three slashes: local filesystem path Windows-style
583 'file:///c:/whatever/you/like.txt',
584 array( 'file://./c:/whatever/you/like.txt' )
587 // two slashes: UNC filesystem path Windows-style
588 'file://intranet/whatever/you/like.txt',
589 array( 'file://intranet./whatever/you/like.txt' )
591 // Multiple-slash cases that can sorta work on Mozilla
592 // if you hack it just right are kinda pathological,
593 // and unreliable cross-platform or on IE which means they're
594 // unlikely to appear on intranets.
596 // Those will survive the algorithm but with results that
597 // are less consistent.
599 // protocol-relative URL cases per bug 29854...
601 '//bugzilla.wikimedia.org/show_bug.cgi?id=28627',
603 'http://org.wikimedia.bugzilla./show_bug.cgi?id=28627',
604 'https://org.wikimedia.bugzilla./show_bug.cgi?id=28627'
611 * @dataProvider provideWfMatchesDomainList
612 * @covers ::wfMatchesDomainList
614 public function testWfMatchesDomainList( $url, $domains, $expected, $description ) {
615 $actual = wfMatchesDomainList( $url, $domains );
616 $this->assertEquals( $expected, $actual, $description );
619 public static function provideWfMatchesDomainList() {
621 $protocols = array( 'HTTP' => 'http:', 'HTTPS' => 'https:', 'protocol-relative' => '' );
622 foreach ( $protocols as $pDesc => $p ) {
623 $a = array_merge( $a, array(
625 "$p//www.example.com",
628 "No matches for empty domains array, $pDesc URL"
631 "$p//www.example.com",
632 array( 'www.example.com' ),
634 "Exact match in domains array, $pDesc URL"
637 "$p//www.example.com",
638 array( 'example.com' ),
640 "Match without subdomain in domains array, $pDesc URL"
643 "$p//www.example2.com",
644 array( 'www.example.com', 'www.example2.com', 'www.example3.com' ),
646 "Exact match with other domains in array, $pDesc URL"
649 "$p//www.example2.com",
650 array( 'example.com', 'example2.com', 'example3,com' ),
652 "Match without subdomain with other domains in array, $pDesc URL"
655 "$p//www.example4.com",
656 array( 'example.com', 'example2.com', 'example3,com' ),
658 "Domain not in array, $pDesc URL"
661 "$p//nds-nl.wikipedia.org",
662 array( 'nl.wikipedia.org' ),
664 "Non-matching substring of domain, $pDesc URL"
673 * @covers ::wfMkdirParents
675 public function testWfMkdirParents() {
676 // Should not return true if file exists instead of directory
677 $fname = $this->getNewTempFile();
678 wfSuppressWarnings();
679 $ok = wfMkdirParents( $fname );
681 $this->assertFalse( $ok );
685 * @dataProvider provideWfShellMaintenanceCmdList
686 * @covers ::wfShellMaintenanceCmd
688 public function testWfShellMaintenanceCmd( $script, $parameters, $options,
689 $expected, $description
691 if ( wfIsWindows() ) {
692 // Approximation that's good enough for our purposes just now
693 $expected = str_replace( "'", '"', $expected );
695 $actual = wfShellMaintenanceCmd( $script, $parameters, $options );
696 $this->assertEquals( $expected, $actual, $description );
699 public static function provideWfShellMaintenanceCmdList() {
703 array( 'eval.php', array( '--help', '--test' ), array(),
704 "'$wgPhpCli' 'eval.php' '--help' '--test'",
705 "Called eval.php --help --test" ),
706 array( 'eval.php', array( '--help', '--test space' ), array( 'php' => 'php5' ),
707 "'php5' 'eval.php' '--help' '--test space'",
708 "Called eval.php --help --test with php option" ),
709 array( 'eval.php', array( '--help', '--test', 'X' ), array( 'wrapper' => 'MWScript.php' ),
710 "'$wgPhpCli' 'MWScript.php' 'eval.php' '--help' '--test' 'X'",
711 "Called eval.php --help --test with wrapper option" ),
714 array( '--help', '--test', 'y' ),
715 array( 'php' => 'php5', 'wrapper' => 'MWScript.php' ),
716 "'php5' 'MWScript.php' 'eval.php' '--help' '--test' 'y'",
717 "Called eval.php --help --test with wrapper and php option"
721 /* @TODO many more! */