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 ) );
145 * @covers ::wfArrayToCgi
147 public function testArrayToCGI2() {
151 array( 'baz' => 'bar' ),
152 array( 'foo' => 'bar', 'baz' => 'overridden value' ) ) );
155 public static function provideCgiToArray() {
157 array( '', array() ), // empty
158 array( 'foo=bar', array( 'foo' => 'bar' ) ), // string
159 array( 'foo=', array( 'foo' => '' ) ), // empty string
160 array( 'foo', array( 'foo' => '' ) ), // missing =
161 array( 'foo=bar&qwerty=asdf', array( 'foo' => 'bar', 'qwerty' => 'asdf' ) ), // multiple value
162 array( 'foo=A%26B%3D5%2B6%40%21%22%27', array( 'foo' => 'A&B=5+6@!"\'' ) ), // urldecoding test
163 array( 'foo%5Bbar%5D=baz', array( 'foo' => array( 'bar' => 'baz' ) ) ),
164 array( 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf', array( 'foo' => array( 'bar' => 'baz', 'qwerty' => 'asdf' ) ) ),
165 array( 'foo%5B0%5D=bar&foo%5B1%5D=baz', array( 'foo' => array( 0 => 'bar', 1 => 'baz' ) ) ),
166 array( 'foo%5Bbar%5D%5Bbar%5D=baz', array( 'foo' => array( 'bar' => array( 'bar' => 'baz' ) ) ) ),
171 * @dataProvider provideCgiToArray
172 * @covers ::wfCgiToArray
174 public function testCgiToArray( $cgi, $result ) {
175 $this->assertEquals( $result, wfCgiToArray( $cgi ) );
178 public static function provideCgiRoundTrip() {
183 array( 'foo=bar&baz=biz' ),
184 array( 'foo=A%26B%3D5%2B6%40%21%22%27' ),
185 array( 'foo%5Bbar%5D=baz' ),
186 array( 'foo%5B0%5D=bar&foo%5B1%5D=baz' ),
187 array( 'foo%5Bbar%5D%5Bbar%5D=baz' ),
192 * @dataProvider provideCgiRoundTrip
193 * @covers ::wfArrayToCgi
195 public function testCgiRoundTrip( $cgi ) {
196 $this->assertEquals( $cgi, wfArrayToCgi( wfCgiToArray( $cgi ) ) );
200 * @covers ::mimeTypeMatch
202 public function testMimeTypeMatch() {
205 mimeTypeMatch( 'text/html',
206 array( 'application/xhtml+xml' => 1.0,
208 'text/plain' => 0.3 ) ) );
211 mimeTypeMatch( 'text/html',
212 array( 'image/*' => 1.0,
213 'text/*' => 0.5 ) ) );
216 mimeTypeMatch( 'text/html',
217 array( '*/*' => 1.0 ) ) );
219 mimeTypeMatch( 'text/html',
220 array( 'image/png' => 1.0,
221 'image/svg+xml' => 0.5 ) ) );
225 * @covers ::wfNegotiateType
227 public function testNegotiateType() {
231 array( 'application/xhtml+xml' => 1.0,
235 array( 'text/html' => 1.0 ) ) );
237 'application/xhtml+xml',
239 array( 'application/xhtml+xml' => 1.0,
243 array( 'application/xhtml+xml' => 1.0,
244 'text/html' => 0.5 ) ) );
248 array( 'text/html' => 1.0,
251 'application/xhtml+xml' => 0.2 ),
252 array( 'application/xhtml+xml' => 1.0,
253 'text/html' => 0.5 ) ) );
257 array( 'text/*' => 1.0,
260 array( 'application/xhtml+xml' => 1.0,
261 'text/html' => 0.5 ) ) );
264 array( 'text/*' => 1.0 ),
265 array( 'application/xhtml+xml' => 1.0 ) ) );
270 * @covers ::wfDebugMem
272 public function testDebugFunctionTest() {
274 global $wgDebugLogFile, $wgDebugTimestamps;
276 $old_log_file = $wgDebugLogFile;
277 $wgDebugLogFile = tempnam( wfTempDir(), 'mw-' );
278 # @todo FIXME: $wgDebugTimestamps should be tested
279 $old_wgDebugTimestamps = $wgDebugTimestamps;
280 $wgDebugTimestamps = false;
282 wfDebug( "This is a normal string" );
283 $this->assertEquals( "This is a normal string", file_get_contents( $wgDebugLogFile ) );
284 unlink( $wgDebugLogFile );
286 wfDebug( "This is nöt an ASCII string" );
287 $this->assertEquals( "This is nöt an ASCII string", file_get_contents( $wgDebugLogFile ) );
288 unlink( $wgDebugLogFile );
290 wfDebug( "\00305This has böth UTF and control chars\003" );
291 $this->assertEquals( " 05This has böth UTF and control chars ", file_get_contents( $wgDebugLogFile ) );
292 unlink( $wgDebugLogFile );
295 $this->assertGreaterThan( 1000, preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) ) );
296 unlink( $wgDebugLogFile );
299 $this->assertGreaterThan( 1000000, preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) ) );
300 unlink( $wgDebugLogFile );
302 $wgDebugLogFile = $old_log_file;
303 $wgDebugTimestamps = $old_wgDebugTimestamps;
307 * @covers ::wfClientAcceptsGzip
309 public function testClientAcceptsGzipTest() {
315 'compress, gzip' => true,
316 'gzip;q=1.0' => true,
319 'gzip;q=abcde' => true, //is this REALLY valid?
320 'gzip;q=12345678.9' => true,
324 if ( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) {
325 $old_server_setting = $_SERVER['HTTP_ACCEPT_ENCODING'];
328 foreach ( $settings as $encoding => $expect ) {
329 $_SERVER['HTTP_ACCEPT_ENCODING'] = $encoding;
331 $this->assertEquals( $expect, wfClientAcceptsGzip( true ),
332 "'$encoding' => " . wfBoolToStr( $expect ) );
335 if ( isset( $old_server_setting ) ) {
336 $_SERVER['HTTP_ACCEPT_ENCODING'] = $old_server_setting;
343 public function testSwapVarsTest() {
347 $this->assertEquals( $var1, 1, 'var1 is set originally' );
348 $this->assertEquals( $var2, 2, 'var1 is set originally' );
350 swap( $var1, $var2 );
352 $this->assertEquals( $var1, 2, 'var1 is swapped' );
353 $this->assertEquals( $var2, 1, 'var2 is swapped' );
357 * @covers ::wfPercent
359 public function testWfPercentTest() {
362 array( 6 / 7, '0.86%', 2, false ),
363 array( 3 / 3, '1%' ),
364 array( 22 / 7, '3.14286%', 5 ),
365 array( 3 / 6, '0.5%' ),
366 array( 1 / 3, '0%', 0 ),
367 array( 10 / 3, '0%', -1 ),
368 array( 3 / 4 / 5, '0.1%', 1 ),
369 array( 6 / 7 * 8, '6.8571428571%', 10 ),
372 foreach ( $pcts as $pct ) {
373 if ( !isset( $pct[2] ) ) {
376 if ( !isset( $pct[3] ) ) {
380 $this->assertEquals( wfPercent( $pct[0], $pct[2], $pct[3] ), $pct[1], $pct[1] );
385 * test @see wfShorthandToInteger()
386 * @dataProvider provideShorthand
387 * @covers ::wfShorthandToInteger
389 public function testWfShorthandToInteger( $shorthand, $expected ) {
390 $this->assertEquals( $expected,
391 wfShorthandToInteger( $shorthand )
395 /** array( shorthand, expected integer ) */
396 public static function provideShorthand() {
403 # Failures returns 0 :(
404 array( 'ABCDEFG', 0 ),
407 # Int, strings with spaces
411 array( ' 1023 ', 1023 ),
416 array( '1m', 1024 * 1024 ),
417 array( '1M', 1024 * 1024 ),
418 array( '1g', 1024 * 1024 * 1024 ),
419 array( '1G', 1024 * 1024 * 1024 ),
424 array( '-500', -500 ),
425 array( '-1k', -1024 ),
440 * @param string $old Text as it was in the database
441 * @param string $mine Text submitted while user was editing
442 * @param string $yours Text submitted by the user
443 * @param bool $expectedMergeResult Whether the merge should be a success
444 * @param string $expectedText Text after merge has been completed
446 * @dataProvider provideMerge()
450 public function testMerge( $old, $mine, $yours, $expectedMergeResult, $expectedText ) {
451 $this->checkHasDiff3();
454 $isMerged = wfMerge( $old, $mine, $yours, $mergedText );
456 $msg = 'Merge should be a ';
457 $msg .= $expectedMergeResult ?
'success' : 'failure';
458 $this->assertEquals( $expectedMergeResult, $isMerged, $msg );
461 // Verify the merged text
462 $this->assertEquals( $expectedText, $mergedText,
463 'is merged text as expected?' );
467 public static function provideMerge() {
468 $EXPECT_MERGE_SUCCESS = true;
469 $EXPECT_MERGE_FAILURE = false;
475 "one one one\n" . // trimmed
480 "one one one ONE ONE\n" .
482 "two two two\n", // with tailing whitespace
487 "two two TWO TWO", // trimmed
490 $EXPECT_MERGE_SUCCESS,
493 "one one one ONE ONE\n" .
495 "two two TWO TWO\n", // note: will always end in a newline
498 // #1: conflict, fail
501 "one one one", // trimmed
504 "one one one ONE ONE\n" .
507 "\n", // with tailing whitespace
512 "two two", // trimmed
514 $EXPECT_MERGE_FAILURE,
523 * @dataProvider provideMakeUrlIndexes()
524 * @covers ::wfMakeUrlIndexes
526 public function testMakeUrlIndexes( $url, $expected ) {
527 $index = wfMakeUrlIndexes( $url );
528 $this->assertEquals( $expected, $index, "wfMakeUrlIndexes(\"$url\")" );
531 public static function provideMakeUrlIndexes() {
535 'https://bugzilla.wikimedia.org/show_bug.cgi?id=28627',
536 array( 'https://org.wikimedia.bugzilla./show_bug.cgi?id=28627' )
539 // mailtos are handled special
540 // is this really right though? that final . probably belongs earlier?
541 'mailto:wiki@wikimedia.org',
542 array( 'mailto:org.wikimedia@wiki.' )
545 // file URL cases per bug 28627...
547 // three slashes: local filesystem path Unix-style
548 'file:///whatever/you/like.txt',
549 array( 'file://./whatever/you/like.txt' )
552 // three slashes: local filesystem path Windows-style
553 'file:///c:/whatever/you/like.txt',
554 array( 'file://./c:/whatever/you/like.txt' )
557 // two slashes: UNC filesystem path Windows-style
558 'file://intranet/whatever/you/like.txt',
559 array( 'file://intranet./whatever/you/like.txt' )
561 // Multiple-slash cases that can sorta work on Mozilla
562 // if you hack it just right are kinda pathological,
563 // and unreliable cross-platform or on IE which means they're
564 // unlikely to appear on intranets.
566 // Those will survive the algorithm but with results that
567 // are less consistent.
569 // protocol-relative URL cases per bug 29854...
571 '//bugzilla.wikimedia.org/show_bug.cgi?id=28627',
573 'http://org.wikimedia.bugzilla./show_bug.cgi?id=28627',
574 'https://org.wikimedia.bugzilla./show_bug.cgi?id=28627'
581 * @dataProvider provideWfMatchesDomainList
582 * @covers ::wfMatchesDomainList
584 public function testWfMatchesDomainList( $url, $domains, $expected, $description ) {
585 $actual = wfMatchesDomainList( $url, $domains );
586 $this->assertEquals( $expected, $actual, $description );
589 public static function provideWfMatchesDomainList() {
591 $protocols = array( 'HTTP' => 'http:', 'HTTPS' => 'https:', 'protocol-relative' => '' );
592 foreach ( $protocols as $pDesc => $p ) {
593 $a = array_merge( $a, array(
594 array( "$p//www.example.com", array(), false, "No matches for empty domains array, $pDesc URL" ),
595 array( "$p//www.example.com", array( 'www.example.com' ), true, "Exact match in domains array, $pDesc URL" ),
596 array( "$p//www.example.com", array( 'example.com' ), true, "Match without subdomain in domains array, $pDesc URL" ),
597 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" ),
598 array( "$p//www.example2.com", array( 'example.com', 'example2.com', 'example3,com' ), true, "Match without subdomain with other domains in array, $pDesc URL" ),
599 array( "$p//www.example4.com", array( 'example.com', 'example2.com', 'example3,com' ), false, "Domain not in array, $pDesc URL" ),
600 array( "$p//nds-nl.wikipedia.org", array( 'nl.wikipedia.org' ), false, "Non-matching substring of domain, $pDesc URL" ),
608 * @covers ::wfMkdirParents
610 public function testWfMkdirParents() {
611 // Should not return true if file exists instead of directory
612 $fname = $this->getNewTempFile();
613 wfSuppressWarnings();
614 $ok = wfMkdirParents( $fname );
616 $this->assertFalse( $ok );
620 * @dataProvider provideWfShellMaintenanceCmdList
621 * @covers ::wfShellMaintenanceCmd
623 public function testWfShellMaintenanceCmd( $script, $parameters, $options, $expected, $description ) {
624 if ( wfIsWindows() ) {
625 // Approximation that's good enough for our purposes just now
626 $expected = str_replace( "'", '"', $expected );
628 $actual = wfShellMaintenanceCmd( $script, $parameters, $options );
629 $this->assertEquals( $expected, $actual, $description );
632 public static function provideWfShellMaintenanceCmdList() {
636 array( 'eval.php', array( '--help', '--test' ), array(),
637 "'$wgPhpCli' 'eval.php' '--help' '--test'",
638 "Called eval.php --help --test" ),
639 array( 'eval.php', array( '--help', '--test space' ), array( 'php' => 'php5' ),
640 "'php5' 'eval.php' '--help' '--test space'",
641 "Called eval.php --help --test with php option" ),
642 array( 'eval.php', array( '--help', '--test', 'X' ), array( 'wrapper' => 'MWScript.php' ),
643 "'$wgPhpCli' 'MWScript.php' 'eval.php' '--help' '--test' 'X'",
644 "Called eval.php --help --test with wrapper option" ),
645 array( 'eval.php', array( '--help', '--test', 'y' ), array( 'php' => 'php5', 'wrapper' => 'MWScript.php' ),
646 "'php5' 'MWScript.php' 'eval.php' '--help' '--test' 'y'",
647 "Called eval.php --help --test with wrapper and php option" ),
650 /* @TODO many more! */