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/%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" ) );
87 * @covers ::wfReadOnly
89 public function testReadOnlyEmpty() {
93 $this->assertFalse( wfReadOnly() );
94 $this->assertFalse( wfReadOnly() );
98 * @covers ::wfReadOnly
100 public function testReadOnlySet() {
101 global $wgReadOnly, $wgReadOnlyFile;
103 $f = fopen( $wgReadOnlyFile, "wt" );
104 fwrite( $f, 'Message' );
106 $wgReadOnly = null; # Check on $wgReadOnlyFile
108 $this->assertTrue( wfReadOnly() );
109 $this->assertTrue( wfReadOnly() ); # Check cached
111 unlink( $wgReadOnlyFile );
112 $wgReadOnly = null; # Clean cache
114 $this->assertFalse( wfReadOnly() );
115 $this->assertFalse( wfReadOnly() );
118 public static function provideArrayToCGI() {
120 array( array(), '' ), // empty
121 array( array( 'foo' => 'bar' ), 'foo=bar' ), // string test
122 array( array( 'foo' => '' ), 'foo=' ), // empty string test
123 array( array( 'foo' => 1 ), 'foo=1' ), // number test
124 array( array( 'foo' => true ), 'foo=1' ), // true test
125 array( array( 'foo' => false ), '' ), // false test
126 array( array( 'foo' => null ), '' ), // null test
127 array( array( 'foo' => 'A&B=5+6@!"\'' ), 'foo=A%26B%3D5%2B6%40%21%22%27' ), // urlencoding test
128 array( array( 'foo' => 'bar', 'baz' => 'is', 'asdf' => 'qwerty' ), 'foo=bar&baz=is&asdf=qwerty' ), // multi-item test
129 array( array( 'foo' => array( 'bar' => 'baz' ) ), 'foo%5Bbar%5D=baz' ),
130 array( array( 'foo' => array( 'bar' => 'baz', 'qwerty' => 'asdf' ) ), 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf' ),
131 array( array( 'foo' => array( 'bar', 'baz' ) ), 'foo%5B0%5D=bar&foo%5B1%5D=baz' ),
132 array( array( 'foo' => array( 'bar' => array( 'bar' => 'baz' ) ) ), 'foo%5Bbar%5D%5Bbar%5D=baz' ),
137 * @dataProvider provideArrayToCGI
138 * @covers ::wfArrayToCgi
140 public function testArrayToCGI( $array, $result ) {
141 $this->assertEquals( $result, wfArrayToCgi( $array ) );
146 * @covers ::testWfArrayDiff2
148 public function testArrayToCGI2() {
152 array( 'baz' => 'bar' ),
153 array( 'foo' => 'bar', 'baz' => 'overridden value' ) ) );
156 public static function provideCgiToArray() {
158 array( '', array() ), // empty
159 array( 'foo=bar', array( 'foo' => 'bar' ) ), // string
160 array( 'foo=', array( 'foo' => '' ) ), // empty string
161 array( 'foo', array( 'foo' => '' ) ), // missing =
162 array( 'foo=bar&qwerty=asdf', array( 'foo' => 'bar', 'qwerty' => 'asdf' ) ), // multiple value
163 array( 'foo=A%26B%3D5%2B6%40%21%22%27', array( 'foo' => 'A&B=5+6@!"\'' ) ), // urldecoding test
164 array( 'foo%5Bbar%5D=baz', array( 'foo' => array( 'bar' => 'baz' ) ) ),
165 array( 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf', array( 'foo' => array( 'bar' => 'baz', 'qwerty' => 'asdf' ) ) ),
166 array( 'foo%5B0%5D=bar&foo%5B1%5D=baz', array( 'foo' => array( 0 => 'bar', 1 => 'baz' ) ) ),
167 array( 'foo%5Bbar%5D%5Bbar%5D=baz', array( 'foo' => array( 'bar' => array( 'bar' => 'baz' ) ) ) ),
172 * @dataProvider provideCgiToArray
173 * @covers ::wfCgiToArray
175 public function testCgiToArray( $cgi, $result ) {
176 $this->assertEquals( $result, wfCgiToArray( $cgi ) );
179 public static function provideCgiRoundTrip() {
184 array( 'foo=bar&baz=biz' ),
185 array( 'foo=A%26B%3D5%2B6%40%21%22%27' ),
186 array( 'foo%5Bbar%5D=baz' ),
187 array( 'foo%5B0%5D=bar&foo%5B1%5D=baz' ),
188 array( 'foo%5Bbar%5D%5Bbar%5D=baz' ),
193 * @dataProvider provideCgiRoundTrip
194 * @covers ::wfArrayToCgi
196 public function testCgiRoundTrip( $cgi ) {
197 $this->assertEquals( $cgi, wfArrayToCgi( wfCgiToArray( $cgi ) ) );
201 * @covers ::mimeTypeMatch
203 public function testMimeTypeMatch() {
206 mimeTypeMatch( 'text/html',
207 array( 'application/xhtml+xml' => 1.0,
209 'text/plain' => 0.3 ) ) );
212 mimeTypeMatch( 'text/html',
213 array( 'image/*' => 1.0,
214 'text/*' => 0.5 ) ) );
217 mimeTypeMatch( 'text/html',
218 array( '*/*' => 1.0 ) ) );
220 mimeTypeMatch( 'text/html',
221 array( 'image/png' => 1.0,
222 'image/svg+xml' => 0.5 ) ) );
226 * @covers ::wfNegotiateType
228 public function testNegotiateType() {
232 array( 'application/xhtml+xml' => 1.0,
236 array( 'text/html' => 1.0 ) ) );
238 'application/xhtml+xml',
240 array( 'application/xhtml+xml' => 1.0,
244 array( 'application/xhtml+xml' => 1.0,
245 'text/html' => 0.5 ) ) );
249 array( 'text/html' => 1.0,
252 'application/xhtml+xml' => 0.2 ),
253 array( 'application/xhtml+xml' => 1.0,
254 'text/html' => 0.5 ) ) );
258 array( 'text/*' => 1.0,
261 array( 'application/xhtml+xml' => 1.0,
262 'text/html' => 0.5 ) ) );
265 array( 'text/*' => 1.0 ),
266 array( 'application/xhtml+xml' => 1.0 ) ) );
271 * @covers ::wfDebugMem
273 public function testDebugFunctionTest() {
275 global $wgDebugLogFile, $wgDebugTimestamps;
277 $old_log_file = $wgDebugLogFile;
278 $wgDebugLogFile = tempnam( wfTempDir(), 'mw-' );
279 # @todo FIXME: $wgDebugTimestamps should be tested
280 $old_wgDebugTimestamps = $wgDebugTimestamps;
281 $wgDebugTimestamps = false;
283 wfDebug( "This is a normal string" );
284 $this->assertEquals( "This is a normal string", file_get_contents( $wgDebugLogFile ) );
285 unlink( $wgDebugLogFile );
287 wfDebug( "This is nöt an ASCII string" );
288 $this->assertEquals( "This is nöt an ASCII string", file_get_contents( $wgDebugLogFile ) );
289 unlink( $wgDebugLogFile );
291 wfDebug( "\00305This has böth UTF and control chars\003" );
292 $this->assertEquals( " 05This has böth UTF and control chars ", file_get_contents( $wgDebugLogFile ) );
293 unlink( $wgDebugLogFile );
296 $this->assertGreaterThan( 5000, preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) ) );
297 unlink( $wgDebugLogFile );
300 $this->assertGreaterThan( 5000000, preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) ) );
301 unlink( $wgDebugLogFile );
303 $wgDebugLogFile = $old_log_file;
304 $wgDebugTimestamps = $old_wgDebugTimestamps;
308 * @covers ::wfClientAcceptsGzip
310 public function testClientAcceptsGzipTest() {
316 'compress, gzip' => true,
317 'gzip;q=1.0' => true,
320 'gzip;q=abcde' => true, //is this REALLY valid?
321 'gzip;q=12345678.9' => true,
325 if ( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) {
326 $old_server_setting = $_SERVER['HTTP_ACCEPT_ENCODING'];
329 foreach ( $settings as $encoding => $expect ) {
330 $_SERVER['HTTP_ACCEPT_ENCODING'] = $encoding;
332 $this->assertEquals( $expect, wfClientAcceptsGzip( true ),
333 "'$encoding' => " . wfBoolToStr( $expect ) );
336 if ( isset( $old_server_setting ) ) {
337 $_SERVER['HTTP_ACCEPT_ENCODING'] = $old_server_setting;
344 public function testSwapVarsTest() {
348 $this->assertEquals( $var1, 1, 'var1 is set originally' );
349 $this->assertEquals( $var2, 2, 'var1 is set originally' );
351 swap( $var1, $var2 );
353 $this->assertEquals( $var1, 2, 'var1 is swapped' );
354 $this->assertEquals( $var2, 1, 'var2 is swapped' );
358 * @covers ::wfPercent
360 public function testWfPercentTest() {
363 array( 6 / 7, '0.86%', 2, false ),
364 array( 3 / 3, '1%' ),
365 array( 22 / 7, '3.14286%', 5 ),
366 array( 3 / 6, '0.5%' ),
367 array( 1 / 3, '0%', 0 ),
368 array( 10 / 3, '0%', -1 ),
369 array( 3 / 4 / 5, '0.1%', 1 ),
370 array( 6 / 7 * 8, '6.8571428571%', 10 ),
373 foreach ( $pcts as $pct ) {
374 if ( !isset( $pct[2] ) ) {
377 if ( !isset( $pct[3] ) ) {
381 $this->assertEquals( wfPercent( $pct[0], $pct[2], $pct[3] ), $pct[1], $pct[1] );
386 * test @see wfShorthandToInteger()
387 * @dataProvider provideShorthand
388 * @covers ::wfShorthandToInteger
390 public function testWfShorthandToInteger( $shorthand, $expected ) {
391 $this->assertEquals( $expected,
392 wfShorthandToInteger( $shorthand )
396 /** array( shorthand, expected integer ) */
397 public static function provideShorthand() {
404 # Failures returns 0 :(
405 array( 'ABCDEFG', 0 ),
408 # Int, strings with spaces
412 array( ' 1023 ', 1023 ),
417 array( '1m', 1024 * 1024 ),
418 array( '1M', 1024 * 1024 ),
419 array( '1g', 1024 * 1024 * 1024 ),
420 array( '1G', 1024 * 1024 * 1024 ),
425 array( '-500', -500 ),
426 array( '-1k', -1024 ),
441 * @param String $old: Text as it was in the database
442 * @param String $mine: Text submitted while user was editing
443 * @param String $yours: Text submitted by the user
444 * @param Boolean $expectedMergeResult Whether the merge should be a success
445 * @param String $expectedText: Text after merge has been completed
447 * @dataProvider provideMerge()
451 public function testMerge( $old, $mine, $yours, $expectedMergeResult, $expectedText ) {
452 $this->checkHasDiff3();
455 $isMerged = wfMerge( $old, $mine, $yours, $mergedText );
457 $msg = 'Merge should be a ';
458 $msg .= $expectedMergeResult ?
'success' : 'failure';
459 $this->assertEquals( $expectedMergeResult, $isMerged, $msg );
462 // Verify the merged text
463 $this->assertEquals( $expectedText, $mergedText,
464 'is merged text as expected?' );
468 public static function provideMerge() {
469 $EXPECT_MERGE_SUCCESS = true;
470 $EXPECT_MERGE_FAILURE = false;
476 "one one one\n" . // trimmed
481 "one one one ONE ONE\n" .
483 "two two two\n", // with tailing whitespace
488 "two two TWO TWO", // trimmed
491 $EXPECT_MERGE_SUCCESS,
494 "one one one ONE ONE\n" .
496 "two two TWO TWO\n", // note: will always end in a newline
499 // #1: conflict, fail
502 "one one one", // trimmed
505 "one one one ONE ONE\n" .
508 "\n", // with tailing whitespace
513 "two two", // trimmed
515 $EXPECT_MERGE_FAILURE,
524 * @dataProvider provideMakeUrlIndexes()
525 * @covers ::wfMakeUrlIndexes
527 public function testMakeUrlIndexes( $url, $expected ) {
528 $index = wfMakeUrlIndexes( $url );
529 $this->assertEquals( $expected, $index, "wfMakeUrlIndexes(\"$url\")" );
532 public static function provideMakeUrlIndexes() {
536 'https://bugzilla.wikimedia.org/show_bug.cgi?id=28627',
537 array( 'https://org.wikimedia.bugzilla./show_bug.cgi?id=28627' )
540 // mailtos are handled special
541 // is this really right though? that final . probably belongs earlier?
542 'mailto:wiki@wikimedia.org',
543 array( 'mailto:org.wikimedia@wiki.' )
546 // file URL cases per bug 28627...
548 // three slashes: local filesystem path Unix-style
549 'file:///whatever/you/like.txt',
550 array( 'file://./whatever/you/like.txt' )
553 // three slashes: local filesystem path Windows-style
554 'file:///c:/whatever/you/like.txt',
555 array( 'file://./c:/whatever/you/like.txt' )
558 // two slashes: UNC filesystem path Windows-style
559 'file://intranet/whatever/you/like.txt',
560 array( 'file://intranet./whatever/you/like.txt' )
562 // Multiple-slash cases that can sorta work on Mozilla
563 // if you hack it just right are kinda pathological,
564 // and unreliable cross-platform or on IE which means they're
565 // unlikely to appear on intranets.
567 // Those will survive the algorithm but with results that
568 // are less consistent.
570 // protocol-relative URL cases per bug 29854...
572 '//bugzilla.wikimedia.org/show_bug.cgi?id=28627',
574 'http://org.wikimedia.bugzilla./show_bug.cgi?id=28627',
575 'https://org.wikimedia.bugzilla./show_bug.cgi?id=28627'
582 * @dataProvider provideWfMatchesDomainList
583 * @covers ::wfMatchesDomainList
585 public function testWfMatchesDomainList( $url, $domains, $expected, $description ) {
586 $actual = wfMatchesDomainList( $url, $domains );
587 $this->assertEquals( $expected, $actual, $description );
590 public static function provideWfMatchesDomainList() {
592 $protocols = array( 'HTTP' => 'http:', 'HTTPS' => 'https:', 'protocol-relative' => '' );
593 foreach ( $protocols as $pDesc => $p ) {
594 $a = array_merge( $a, array(
595 array( "$p//www.example.com", array(), false, "No matches for empty domains array, $pDesc URL" ),
596 array( "$p//www.example.com", array( 'www.example.com' ), true, "Exact match in domains array, $pDesc URL" ),
597 array( "$p//www.example.com", array( 'example.com' ), true, "Match without subdomain in domains array, $pDesc URL" ),
598 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" ),
599 array( "$p//www.example2.com", array( 'example.com', 'example2.com', 'example3,com' ), true, "Match without subdomain with other domains in array, $pDesc URL" ),
600 array( "$p//www.example4.com", array( 'example.com', 'example2.com', 'example3,com' ), false, "Domain not in array, $pDesc URL" ),
601 array( "$p//nds-nl.wikipedia.org", array( 'nl.wikipedia.org' ), false, "Non-matching substring of domain, $pDesc URL" ),
609 * @covers ::wfMkdirParents
611 public function testWfMkdirParents() {
612 // Should not return true if file exists instead of directory
613 $fname = $this->getNewTempFile();
614 wfSuppressWarnings();
615 $ok = wfMkdirParents( $fname );
617 $this->assertFalse( $ok );
621 * @dataProvider provideWfShellMaintenanceCmdList
622 * @covers ::wfShellMaintenanceCmd
624 public function testWfShellMaintenanceCmd( $script, $parameters, $options, $expected, $description ) {
625 if ( wfIsWindows() ) {
626 // Approximation that's good enough for our purposes just now
627 $expected = str_replace( "'", '"', $expected );
629 $actual = wfShellMaintenanceCmd( $script, $parameters, $options );
630 $this->assertEquals( $expected, $actual, $description );
633 public static function provideWfShellMaintenanceCmdList() {
637 array( 'eval.php', array( '--help', '--test' ), array(),
638 "'$wgPhpCli' 'eval.php' '--help' '--test'",
639 "Called eval.php --help --test" ),
640 array( 'eval.php', array( '--help', '--test space' ), array( 'php' => 'php5' ),
641 "'php5' 'eval.php' '--help' '--test space'",
642 "Called eval.php --help --test with php option" ),
643 array( 'eval.php', array( '--help', '--test', 'X' ), array( 'wrapper' => 'MWScript.php' ),
644 "'$wgPhpCli' 'MWScript.php' 'eval.php' '--help' '--test' 'X'",
645 "Called eval.php --help --test with wrapper option" ),
646 array( 'eval.php', array( '--help', '--test', 'y' ), array( 'php' => 'php5', 'wrapper' => 'MWScript.php' ),
647 "'php5' 'MWScript.php' 'eval.php' '--help' '--test' 'y'",
648 "Called eval.php --help --test with wrapper and php option" ),
651 /* @TODO many more! */