3 class GlobalTest
extends MediaWikiTestCase
{
5 global $wgReadOnlyFile, $wgUrlProtocols;
6 $this->originals
['wgReadOnlyFile'] = $wgReadOnlyFile;
7 $this->originals
['wgUrlProtocols'] = $wgUrlProtocols;
8 $wgReadOnlyFile = tempnam( wfTempDir(), "mwtest_readonly" );
9 $wgUrlProtocols[] = 'file://';
10 unlink( $wgReadOnlyFile );
14 global $wgReadOnlyFile, $wgUrlProtocols;
15 if ( file_exists( $wgReadOnlyFile ) ) {
16 unlink( $wgReadOnlyFile );
18 $wgReadOnlyFile = $this->originals
['wgReadOnlyFile'];
19 $wgUrlProtocols = $this->originals
['wgUrlProtocols'];
22 /** @dataProvider provideForWfArrayDiff2 */
23 public function testWfArrayDiff2( $a, $b, $expected ) {
25 wfArrayDiff2( $a, $b), $expected
29 // @todo Provide more tests
30 public function provideForWfArrayDiff2() {
39 array( array( 'a'), array( 'a', 'b', 'c' )),
40 array( array( 'a'), array( 'a', 'b' )),
41 array( 1 => array( 'a', 'b', 'c' ) ),
46 function testRandom() {
47 # This could hypothetically fail, but it shouldn't ;)
49 wfRandom() == wfRandom() );
52 function testUrlencode() {
54 "%E7%89%B9%E5%88%A5:Contributions/Foobar",
55 wfUrlencode( "\xE7\x89\xB9\xE5\x88\xA5:Contributions/Foobar" ) );
58 function testExpandIRI() {
60 "https://te.wikibooks.org/wiki/ఉబుంటు_వాడుకరి_మార్గదర్శని",
61 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" ) );
64 function testReadOnlyEmpty() {
68 $this->assertFalse( wfReadOnly() );
69 $this->assertFalse( wfReadOnly() );
72 function testReadOnlySet() {
73 global $wgReadOnly, $wgReadOnlyFile;
75 $f = fopen( $wgReadOnlyFile, "wt" );
76 fwrite( $f, 'Message' );
78 $wgReadOnly = null; # Check on $wgReadOnlyFile
80 $this->assertTrue( wfReadOnly() );
81 $this->assertTrue( wfReadOnly() ); # Check cached
83 unlink( $wgReadOnlyFile );
84 $wgReadOnly = null; # Clean cache
86 $this->assertFalse( wfReadOnly() );
87 $this->assertFalse( wfReadOnly() );
90 function testQuotedPrintable() {
92 "=?UTF-8?Q?=C4=88u=20legebla=3F?=",
93 UserMailer
::quotedPrintable( "\xc4\x88u legebla?", "UTF-8" ) );
98 $this->assertInternalType( 'float', $start );
100 $this->assertTrue( $end > $start, "Time is running backwards!" );
103 function dataArrayToCGI() {
105 array( array(), '' ), // empty
106 array( array( 'foo' => 'bar' ), 'foo=bar' ), // string test
107 array( array( 'foo' => '' ), 'foo=' ), // empty string test
108 array( array( 'foo' => 1 ), 'foo=1' ), // number test
109 array( array( 'foo' => true ), 'foo=1' ), // true test
110 array( array( 'foo' => false ), '' ), // false test
111 array( array( 'foo' => null ), '' ), // null test
112 array( array( 'foo' => 'A&B=5+6@!"\'' ), 'foo=A%26B%3D5%2B6%40%21%22%27' ), // urlencoding test
113 array( array( 'foo' => 'bar', 'baz' => 'is', 'asdf' => 'qwerty' ), 'foo=bar&baz=is&asdf=qwerty' ), // multi-item test
114 array( array( 'foo' => array( 'bar' => 'baz' ) ), 'foo%5Bbar%5D=baz' ),
115 array( array( 'foo' => array( 'bar' => 'baz', 'qwerty' => 'asdf' ) ), 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf' ),
116 array( array( 'foo' => array( 'bar', 'baz' ) ), 'foo%5B0%5D=bar&foo%5B1%5D=baz' ),
117 array( array( 'foo' => array( 'bar' => array( 'bar' => 'baz' ) ) ), 'foo%5Bbar%5D%5Bbar%5D=baz' ),
122 * @dataProvider dataArrayToCGI
124 function testArrayToCGI( $array, $result ) {
125 $this->assertEquals( $result, wfArrayToCGI( $array ) );
129 function testArrayToCGI2() {
133 array( 'baz' => 'bar' ),
134 array( 'foo' => 'bar', 'baz' => 'overridden value' ) ) );
137 function dataCgiToArray() {
139 array( '', array() ), // empty
140 array( 'foo=bar', array( 'foo' => 'bar' ) ), // string
141 array( 'foo=', array( 'foo' => '' ) ), // empty string
142 array( 'foo', array( 'foo' => '' ) ), // missing =
143 array( 'foo=bar&qwerty=asdf', array( 'foo' => 'bar', 'qwerty' => 'asdf' ) ), // multiple value
144 array( 'foo=A%26B%3D5%2B6%40%21%22%27', array( 'foo' => 'A&B=5+6@!"\'' ) ), // urldecoding test
145 array( 'foo%5Bbar%5D=baz', array( 'foo' => array( 'bar' => 'baz' ) ) ),
146 array( 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf', array( 'foo' => array( 'bar' => 'baz', 'qwerty' => 'asdf' ) ) ),
147 array( 'foo%5B0%5D=bar&foo%5B1%5D=baz', array( 'foo' => array( 0 => 'bar', 1 => 'baz' ) ) ),
148 array( 'foo%5Bbar%5D%5Bbar%5D=baz', array( 'foo' => array( 'bar' => array( 'bar' => 'baz' ) ) ) ),
153 * @dataProvider dataCgiToArray
155 function testCgiToArray( $cgi, $result ) {
156 $this->assertEquals( $result, wfCgiToArray( $cgi ) );
159 function dataCgiRoundTrip() {
164 array( 'foo=bar&baz=biz' ),
165 array( 'foo=A%26B%3D5%2B6%40%21%22%27' ),
166 array( 'foo%5Bbar%5D=baz' ),
167 array( 'foo%5B0%5D=bar&foo%5B1%5D=baz' ),
168 array( 'foo%5Bbar%5D%5Bbar%5D=baz' ),
173 * @dataProvider dataCgiRoundTrip
175 function testCgiRoundTrip( $cgi ) {
176 $this->assertEquals( $cgi, wfArrayToCGI( wfCgiToArray( $cgi ) ) );
179 function testMimeTypeMatch() {
182 mimeTypeMatch( 'text/html',
183 array( 'application/xhtml+xml' => 1.0,
185 'text/plain' => 0.3 ) ) );
188 mimeTypeMatch( 'text/html',
189 array( 'image/*' => 1.0,
190 'text/*' => 0.5 ) ) );
193 mimeTypeMatch( 'text/html',
194 array( '*/*' => 1.0 ) ) );
196 mimeTypeMatch( 'text/html',
197 array( 'image/png' => 1.0,
198 'image/svg+xml' => 0.5 ) ) );
201 function testNegotiateType() {
205 array( 'application/xhtml+xml' => 1.0,
209 array( 'text/html' => 1.0 ) ) );
211 'application/xhtml+xml',
213 array( 'application/xhtml+xml' => 1.0,
217 array( 'application/xhtml+xml' => 1.0,
218 'text/html' => 0.5 ) ) );
222 array( 'text/html' => 1.0,
225 'application/xhtml+xml' => 0.2 ),
226 array( 'application/xhtml+xml' => 1.0,
227 'text/html' => 0.5 ) ) );
231 array( 'text/*' => 1.0,
234 array( 'application/xhtml+xml' => 1.0,
235 'text/html' => 0.5 ) ) );
238 array( 'text/*' => 1.0 ),
239 array( 'application/xhtml+xml' => 1.0 ) ) );
242 function testFallbackMbstringFunctions() {
244 if( !extension_loaded( 'mbstring' ) ) {
245 $this->markTestSkipped( "The mb_string functions must be installed to test the fallback functions" );
248 $sampleUTF = "Östergötland_coat_of_arms.png";
252 $substr_params = array(
262 foreach( $substr_params as $param_set ) {
263 $old_param_set = $param_set;
264 array_unshift( $param_set, $sampleUTF );
267 MWFunction
::callArray( 'mb_substr', $param_set ),
268 MWFunction
::callArray( 'Fallback::mb_substr', $param_set ),
269 'Fallback mb_substr with params ' . implode( ', ', $old_param_set )
276 mb_strlen( $sampleUTF ),
277 Fallback
::mb_strlen( $sampleUTF ),
283 $strpos_params = array(
287 //array( 'oat_', 100 ),
292 foreach( $strpos_params as $param_set ) {
293 $old_param_set = $param_set;
294 array_unshift( $param_set, $sampleUTF );
297 MWFunction
::callArray( 'mb_strpos', $param_set ),
298 MWFunction
::callArray( 'Fallback::mb_strpos', $param_set ),
299 'Fallback mb_strpos with params ' . implode( ', ', $old_param_set )
303 MWFunction
::callArray( 'mb_strrpos', $param_set ),
304 MWFunction
::callArray( 'Fallback::mb_strrpos', $param_set ),
305 'Fallback mb_strrpos with params ' . implode( ', ', $old_param_set )
312 function testDebugFunctionTest() {
314 global $wgDebugLogFile, $wgDebugTimestamps;
316 $old_log_file = $wgDebugLogFile;
317 $wgDebugLogFile = tempnam( wfTempDir(), 'mw-' );
318 # @todo FIXME: This setting should be tested
319 $wgDebugTimestamps = false;
323 wfDebug( "This is a normal string" );
324 $this->assertEquals( "This is a normal string", file_get_contents( $wgDebugLogFile ) );
325 unlink( $wgDebugLogFile );
328 wfDebug( "This is nöt an ASCII string" );
329 $this->assertEquals( "This is nöt an ASCII string", file_get_contents( $wgDebugLogFile ) );
330 unlink( $wgDebugLogFile );
333 wfDebug( "\00305This has böth UTF and control chars\003" );
334 $this->assertEquals( " 05This has böth UTF and control chars ", file_get_contents( $wgDebugLogFile ) );
335 unlink( $wgDebugLogFile );
338 $this->assertGreaterThan( 5000, preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) ) );
339 unlink( $wgDebugLogFile );
342 $this->assertGreaterThan( 5000000, preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) ) );
343 unlink( $wgDebugLogFile );
347 $wgDebugLogFile = $old_log_file;
351 function testClientAcceptsGzipTest() {
357 'compress, gzip' => true,
358 'gzip;q=1.0' => true,
361 'gzip;q=abcde' => true, //is this REALLY valid?
362 'gzip;q=12345678.9' => true,
366 if( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) $old_server_setting = $_SERVER['HTTP_ACCEPT_ENCODING'];
368 foreach ( $settings as $encoding => $expect ) {
369 $_SERVER['HTTP_ACCEPT_ENCODING'] = $encoding;
371 $this->assertEquals( $expect, wfClientAcceptsGzip( true ),
372 "'$encoding' => " . wfBoolToStr( $expect ) );
375 if( isset( $old_server_setting ) ) $_SERVER['HTTP_ACCEPT_ENCODING'] = $old_server_setting;
381 function testSwapVarsTest() {
387 $this->assertEquals( $var1, 1, 'var1 is set originally' );
388 $this->assertEquals( $var2, 2, 'var1 is set originally' );
390 swap( $var1, $var2 );
392 $this->assertEquals( $var1, 2, 'var1 is swapped' );
393 $this->assertEquals( $var2, 1, 'var2 is swapped' );
398 function testWfPercentTest() {
401 array( 6/7, '0.86%', 2, false ),
403 array( 22/7, '3.14286%', 5 ),
404 array( 3/6, '0.5%' ),
405 array( 1/3, '0%', 0 ),
406 array( 10/3, '0%', -1 ),
407 array( 3/4/5, '0.1%', 1 ),
408 array( 6/7*8, '6.8571428571%', 10 ),
411 foreach( $pcts as $pct ) {
412 if( !isset( $pct[2] ) ) $pct[2] = 2;
413 if( !isset( $pct[3] ) ) $pct[3] = true;
415 $this->assertEquals( wfPercent( $pct[0], $pct[2], $pct[3] ), $pct[1], $pct[1] );
421 function testInStringTest() {
423 $this->assertTrue( in_string( 'foo', 'foobar' ), 'foo is in foobar' );
424 $this->assertFalse( in_string( 'Bar', 'foobar' ), 'Case-sensitive by default' );
425 $this->assertTrue( in_string( 'Foo', 'foobar', true ), 'Case-insensitive when asked' );
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 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 * @dataProvider provideMakeUrlIndexes()
486 function testMakeUrlIndexes( $url, $expected ) {
487 $index = wfMakeUrlIndexes( $url );
488 $this->assertEquals( $expected, $index, "wfMakeUrlIndexes(\"$url\")" );
491 function provideMakeUrlIndexes() {
495 'https://bugzilla.wikimedia.org/show_bug.cgi?id=28627',
496 array( 'https://org.wikimedia.bugzilla./show_bug.cgi?id=28627' )
499 // mailtos are handled special
500 // is this really right though? that final . probably belongs earlier?
501 'mailto:wiki@wikimedia.org',
502 array( 'mailto:org.wikimedia@wiki.' )
505 // file URL cases per bug 28627...
507 // three slashes: local filesystem path Unix-style
508 'file:///whatever/you/like.txt',
509 array( 'file://./whatever/you/like.txt' )
512 // three slashes: local filesystem path Windows-style
513 'file:///c:/whatever/you/like.txt',
514 array( 'file://./c:/whatever/you/like.txt' )
517 // two slashes: UNC filesystem path Windows-style
518 'file://intranet/whatever/you/like.txt',
519 array( 'file://intranet./whatever/you/like.txt' )
521 // Multiple-slash cases that can sorta work on Mozilla
522 // if you hack it just right are kinda pathological,
523 // and unreliable cross-platform or on IE which means they're
524 // unlikely to appear on intranets.
526 // Those will survive the algorithm but with results that
527 // are less consistent.
529 // protocol-relative URL cases per bug 29854...
531 '//bugzilla.wikimedia.org/show_bug.cgi?id=28627',
533 'http://org.wikimedia.bugzilla./show_bug.cgi?id=28627',
534 'https://org.wikimedia.bugzilla./show_bug.cgi?id=28627'
541 * @dataProvider provideWfMatchesDomainList
543 function testWfMatchesDomainList( $url, $domains, $expected, $description ) {
544 $actual = wfMatchesDomainList( $url, $domains );
545 $this->assertEquals( $expected, $actual, $description );
548 function provideWfMatchesDomainList() {
550 $protocols = array( 'HTTP' => 'http:', 'HTTPS' => 'https:', 'protocol-relative' => '' );
551 foreach ( $protocols as $pDesc => $p ) {
552 $a = array_merge( $a, array(
553 array( "$p//www.example.com", array(), false, "No matches for empty domains array, $pDesc URL" ),
554 array( "$p//www.example.com", array( 'www.example.com' ), true, "Exact match in domains array, $pDesc URL" ),
555 array( "$p//www.example.com", array( 'example.com' ), true, "Match without subdomain in domains array, $pDesc URL" ),
556 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" ),
557 array( "$p//www.example2.com", array( 'example.com', 'example2.com', 'example3,com' ), true, "Match without subdomain with other domains in array, $pDesc URL" ),
558 array( "$p//www.example4.com", array( 'example.com', 'example2.com', 'example3,com' ), false, "Domain not in array, $pDesc URL" ),
560 // FIXME: This is a bug in wfMatchesDomainList(). If and when this is fixed, update this test case
561 array( "$p//nds-nl.wikipedia.org", array( 'nl.wikipedia.org' ), true, "Substrings of domains match while they shouldn't, $pDesc URL" ),
568 * @dataProvider provideWfShellMaintenanceCmdList
570 function testWfShellMaintenanceCmd( $script, $parameters, $options, $expected, $description ) {
571 if( wfIsWindows() ) {
572 // Approximation that's good enough for our purposes just now
573 $expected = str_replace( "'", '"', $expected );
575 $actual = wfShellMaintenanceCmd( $script, $parameters, $options );
576 $this->assertEquals( $expected, $actual, $description );
579 function provideWfShellMaintenanceCmdList() {
582 array( 'eval.php', array( '--help', '--test' ), array(),
583 "'$wgPhpCli' 'eval.php' '--help' '--test'",
584 "Called eval.php --help --test" ),
585 array( 'eval.php', array( '--help', '--test space' ), array('php' => 'php5'),
586 "'php5' 'eval.php' '--help' '--test space'",
587 "Called eval.php --help --test with php option" ),
588 array( 'eval.php', array( '--help', '--test', 'X' ), array('wrapper' => 'MWScript.php'),
589 "'$wgPhpCli' 'MWScript.php' 'eval.php' '--help' '--test' 'X'",
590 "Called eval.php --help --test with wrapper option" ),
591 array( 'eval.php', array( '--help', '--test', 'y' ), array('php' => 'php5', 'wrapper' => 'MWScript.php'),
592 "'php5' 'MWScript.php' 'eval.php' '--help' '--test' 'y'",
593 "Called eval.php --help --test with wrapper and php option" ),
596 /* TODO: many more! */