MediaWiki
REL1_23
|
00001 <?php 00002 00003 class GlobalTest extends MediaWikiTestCase { 00004 protected function setUp() { 00005 parent::setUp(); 00006 00007 $readOnlyFile = tempnam( wfTempDir(), "mwtest_readonly" ); 00008 unlink( $readOnlyFile ); 00009 00010 $this->setMwGlobals( array( 00011 'wgReadOnlyFile' => $readOnlyFile, 00012 'wgUrlProtocols' => array( 00013 'http://', 00014 'https://', 00015 'mailto:', 00016 '//', 00017 'file://', # Non-default 00018 ), 00019 ) ); 00020 } 00021 00022 protected function tearDown() { 00023 global $wgReadOnlyFile; 00024 00025 if ( file_exists( $wgReadOnlyFile ) ) { 00026 unlink( $wgReadOnlyFile ); 00027 } 00028 00029 parent::tearDown(); 00030 } 00031 00036 public function testWfArrayDiff2( $a, $b, $expected ) { 00037 $this->assertEquals( 00038 wfArrayDiff2( $a, $b ), $expected 00039 ); 00040 } 00041 00042 // @todo Provide more tests 00043 public static function provideForWfArrayDiff2() { 00044 // $a $b $expected 00045 return array( 00046 array( 00047 array( 'a', 'b' ), 00048 array( 'a', 'b' ), 00049 array(), 00050 ), 00051 array( 00052 array( array( 'a' ), array( 'a', 'b', 'c' ) ), 00053 array( array( 'a' ), array( 'a', 'b' ) ), 00054 array( 1 => array( 'a', 'b', 'c' ) ), 00055 ), 00056 ); 00057 } 00058 00062 public function testRandom() { 00063 # This could hypothetically fail, but it shouldn't ;) 00064 $this->assertFalse( 00065 wfRandom() == wfRandom() ); 00066 } 00067 00071 public function testUrlencode() { 00072 $this->assertEquals( 00073 "%E7%89%B9%E5%88%A5:Contributions/Foobar", 00074 wfUrlencode( "\xE7\x89\xB9\xE5\x88\xA5:Contributions/Foobar" ) ); 00075 } 00076 00080 public function testExpandIRI() { 00081 $this->assertEquals( 00082 "https://te.wikibooks.org/wiki/ఉబుంటు_వాడుకరి_మార్గదర్శని", 00083 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" ) ); 00084 } 00085 00089 public function testReadOnlyEmpty() { 00090 global $wgReadOnly; 00091 $wgReadOnly = null; 00092 00093 $this->assertFalse( wfReadOnly() ); 00094 $this->assertFalse( wfReadOnly() ); 00095 } 00096 00100 public function testReadOnlySet() { 00101 global $wgReadOnly, $wgReadOnlyFile; 00102 00103 $f = fopen( $wgReadOnlyFile, "wt" ); 00104 fwrite( $f, 'Message' ); 00105 fclose( $f ); 00106 $wgReadOnly = null; # Check on $wgReadOnlyFile 00107 00108 $this->assertTrue( wfReadOnly() ); 00109 $this->assertTrue( wfReadOnly() ); # Check cached 00110 00111 unlink( $wgReadOnlyFile ); 00112 $wgReadOnly = null; # Clean cache 00113 00114 $this->assertFalse( wfReadOnly() ); 00115 $this->assertFalse( wfReadOnly() ); 00116 } 00117 00118 public static function provideArrayToCGI() { 00119 return array( 00120 array( array(), '' ), // empty 00121 array( array( 'foo' => 'bar' ), 'foo=bar' ), // string test 00122 array( array( 'foo' => '' ), 'foo=' ), // empty string test 00123 array( array( 'foo' => 1 ), 'foo=1' ), // number test 00124 array( array( 'foo' => true ), 'foo=1' ), // true test 00125 array( array( 'foo' => false ), '' ), // false test 00126 array( array( 'foo' => null ), '' ), // null test 00127 array( array( 'foo' => 'A&B=5+6@!"\'' ), 'foo=A%26B%3D5%2B6%40%21%22%27' ), // urlencoding test 00128 array( array( 'foo' => 'bar', 'baz' => 'is', 'asdf' => 'qwerty' ), 'foo=bar&baz=is&asdf=qwerty' ), // multi-item test 00129 array( array( 'foo' => array( 'bar' => 'baz' ) ), 'foo%5Bbar%5D=baz' ), 00130 array( array( 'foo' => array( 'bar' => 'baz', 'qwerty' => 'asdf' ) ), 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf' ), 00131 array( array( 'foo' => array( 'bar', 'baz' ) ), 'foo%5B0%5D=bar&foo%5B1%5D=baz' ), 00132 array( array( 'foo' => array( 'bar' => array( 'bar' => 'baz' ) ) ), 'foo%5Bbar%5D%5Bbar%5D=baz' ), 00133 ); 00134 } 00135 00140 public function testArrayToCGI( $array, $result ) { 00141 $this->assertEquals( $result, wfArrayToCgi( $array ) ); 00142 } 00143 00147 public function testArrayToCGI2() { 00148 $this->assertEquals( 00149 "baz=bar&foo=bar", 00150 wfArrayToCgi( 00151 array( 'baz' => 'bar' ), 00152 array( 'foo' => 'bar', 'baz' => 'overridden value' ) ) ); 00153 } 00154 00155 public static function provideCgiToArray() { 00156 return array( 00157 array( '', array() ), // empty 00158 array( 'foo=bar', array( 'foo' => 'bar' ) ), // string 00159 array( 'foo=', array( 'foo' => '' ) ), // empty string 00160 array( 'foo', array( 'foo' => '' ) ), // missing = 00161 array( 'foo=bar&qwerty=asdf', array( 'foo' => 'bar', 'qwerty' => 'asdf' ) ), // multiple value 00162 array( 'foo=A%26B%3D5%2B6%40%21%22%27', array( 'foo' => 'A&B=5+6@!"\'' ) ), // urldecoding test 00163 array( 'foo%5Bbar%5D=baz', array( 'foo' => array( 'bar' => 'baz' ) ) ), 00164 array( 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf', array( 'foo' => array( 'bar' => 'baz', 'qwerty' => 'asdf' ) ) ), 00165 array( 'foo%5B0%5D=bar&foo%5B1%5D=baz', array( 'foo' => array( 0 => 'bar', 1 => 'baz' ) ) ), 00166 array( 'foo%5Bbar%5D%5Bbar%5D=baz', array( 'foo' => array( 'bar' => array( 'bar' => 'baz' ) ) ) ), 00167 ); 00168 } 00169 00174 public function testCgiToArray( $cgi, $result ) { 00175 $this->assertEquals( $result, wfCgiToArray( $cgi ) ); 00176 } 00177 00178 public static function provideCgiRoundTrip() { 00179 return array( 00180 array( '' ), 00181 array( 'foo=bar' ), 00182 array( 'foo=' ), 00183 array( 'foo=bar&baz=biz' ), 00184 array( 'foo=A%26B%3D5%2B6%40%21%22%27' ), 00185 array( 'foo%5Bbar%5D=baz' ), 00186 array( 'foo%5B0%5D=bar&foo%5B1%5D=baz' ), 00187 array( 'foo%5Bbar%5D%5Bbar%5D=baz' ), 00188 ); 00189 } 00190 00195 public function testCgiRoundTrip( $cgi ) { 00196 $this->assertEquals( $cgi, wfArrayToCgi( wfCgiToArray( $cgi ) ) ); 00197 } 00198 00202 public function testMimeTypeMatch() { 00203 $this->assertEquals( 00204 'text/html', 00205 mimeTypeMatch( 'text/html', 00206 array( 'application/xhtml+xml' => 1.0, 00207 'text/html' => 0.7, 00208 'text/plain' => 0.3 ) ) ); 00209 $this->assertEquals( 00210 'text/*', 00211 mimeTypeMatch( 'text/html', 00212 array( 'image/*' => 1.0, 00213 'text/*' => 0.5 ) ) ); 00214 $this->assertEquals( 00215 '*/*', 00216 mimeTypeMatch( 'text/html', 00217 array( '*/*' => 1.0 ) ) ); 00218 $this->assertNull( 00219 mimeTypeMatch( 'text/html', 00220 array( 'image/png' => 1.0, 00221 'image/svg+xml' => 0.5 ) ) ); 00222 } 00223 00227 public function testNegotiateType() { 00228 $this->assertEquals( 00229 'text/html', 00230 wfNegotiateType( 00231 array( 'application/xhtml+xml' => 1.0, 00232 'text/html' => 0.7, 00233 'text/plain' => 0.5, 00234 'text/*' => 0.2 ), 00235 array( 'text/html' => 1.0 ) ) ); 00236 $this->assertEquals( 00237 'application/xhtml+xml', 00238 wfNegotiateType( 00239 array( 'application/xhtml+xml' => 1.0, 00240 'text/html' => 0.7, 00241 'text/plain' => 0.5, 00242 'text/*' => 0.2 ), 00243 array( 'application/xhtml+xml' => 1.0, 00244 'text/html' => 0.5 ) ) ); 00245 $this->assertEquals( 00246 'text/html', 00247 wfNegotiateType( 00248 array( 'text/html' => 1.0, 00249 'text/plain' => 0.5, 00250 'text/*' => 0.5, 00251 'application/xhtml+xml' => 0.2 ), 00252 array( 'application/xhtml+xml' => 1.0, 00253 'text/html' => 0.5 ) ) ); 00254 $this->assertEquals( 00255 'text/html', 00256 wfNegotiateType( 00257 array( 'text/*' => 1.0, 00258 'image/*' => 0.7, 00259 '*/*' => 0.3 ), 00260 array( 'application/xhtml+xml' => 1.0, 00261 'text/html' => 0.5 ) ) ); 00262 $this->assertNull( 00263 wfNegotiateType( 00264 array( 'text/*' => 1.0 ), 00265 array( 'application/xhtml+xml' => 1.0 ) ) ); 00266 } 00267 00272 public function testDebugFunctionTest() { 00273 00274 global $wgDebugLogFile, $wgDebugTimestamps; 00275 00276 $old_log_file = $wgDebugLogFile; 00277 $wgDebugLogFile = tempnam( wfTempDir(), 'mw-' ); 00278 # @todo FIXME: $wgDebugTimestamps should be tested 00279 $old_wgDebugTimestamps = $wgDebugTimestamps; 00280 $wgDebugTimestamps = false; 00281 00282 wfDebug( "This is a normal string" ); 00283 $this->assertEquals( "This is a normal string", file_get_contents( $wgDebugLogFile ) ); 00284 unlink( $wgDebugLogFile ); 00285 00286 wfDebug( "This is nöt an ASCII string" ); 00287 $this->assertEquals( "This is nöt an ASCII string", file_get_contents( $wgDebugLogFile ) ); 00288 unlink( $wgDebugLogFile ); 00289 00290 wfDebug( "\00305This has böth UTF and control chars\003" ); 00291 $this->assertEquals( " 05This has böth UTF and control chars ", file_get_contents( $wgDebugLogFile ) ); 00292 unlink( $wgDebugLogFile ); 00293 00294 wfDebugMem(); 00295 $this->assertGreaterThan( 1000, preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) ) ); 00296 unlink( $wgDebugLogFile ); 00297 00298 wfDebugMem( true ); 00299 $this->assertGreaterThan( 1000000, preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) ) ); 00300 unlink( $wgDebugLogFile ); 00301 00302 $wgDebugLogFile = $old_log_file; 00303 $wgDebugTimestamps = $old_wgDebugTimestamps; 00304 } 00305 00309 public function testClientAcceptsGzipTest() { 00310 00311 $settings = array( 00312 'gzip' => true, 00313 'bzip' => false, 00314 '*' => false, 00315 'compress, gzip' => true, 00316 'gzip;q=1.0' => true, 00317 'foozip' => false, 00318 'foo*zip' => false, 00319 'gzip;q=abcde' => true, //is this REALLY valid? 00320 'gzip;q=12345678.9' => true, 00321 ' gzip' => true, 00322 ); 00323 00324 if ( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) { 00325 $old_server_setting = $_SERVER['HTTP_ACCEPT_ENCODING']; 00326 } 00327 00328 foreach ( $settings as $encoding => $expect ) { 00329 $_SERVER['HTTP_ACCEPT_ENCODING'] = $encoding; 00330 00331 $this->assertEquals( $expect, wfClientAcceptsGzip( true ), 00332 "'$encoding' => " . wfBoolToStr( $expect ) ); 00333 } 00334 00335 if ( isset( $old_server_setting ) ) { 00336 $_SERVER['HTTP_ACCEPT_ENCODING'] = $old_server_setting; 00337 } 00338 } 00339 00343 public function testSwapVarsTest() { 00344 $var1 = 1; 00345 $var2 = 2; 00346 00347 $this->assertEquals( $var1, 1, 'var1 is set originally' ); 00348 $this->assertEquals( $var2, 2, 'var1 is set originally' ); 00349 00350 swap( $var1, $var2 ); 00351 00352 $this->assertEquals( $var1, 2, 'var1 is swapped' ); 00353 $this->assertEquals( $var2, 1, 'var2 is swapped' ); 00354 } 00355 00359 public function testWfPercentTest() { 00360 00361 $pcts = array( 00362 array( 6 / 7, '0.86%', 2, false ), 00363 array( 3 / 3, '1%' ), 00364 array( 22 / 7, '3.14286%', 5 ), 00365 array( 3 / 6, '0.5%' ), 00366 array( 1 / 3, '0%', 0 ), 00367 array( 10 / 3, '0%', -1 ), 00368 array( 3 / 4 / 5, '0.1%', 1 ), 00369 array( 6 / 7 * 8, '6.8571428571%', 10 ), 00370 ); 00371 00372 foreach ( $pcts as $pct ) { 00373 if ( !isset( $pct[2] ) ) { 00374 $pct[2] = 2; 00375 } 00376 if ( !isset( $pct[3] ) ) { 00377 $pct[3] = true; 00378 } 00379 00380 $this->assertEquals( wfPercent( $pct[0], $pct[2], $pct[3] ), $pct[1], $pct[1] ); 00381 } 00382 } 00383 00389 public function testWfShorthandToInteger( $shorthand, $expected ) { 00390 $this->assertEquals( $expected, 00391 wfShorthandToInteger( $shorthand ) 00392 ); 00393 } 00394 00396 public static function provideShorthand() { 00397 return array( 00398 # Null, empty ... 00399 array( '', -1 ), 00400 array( ' ', -1 ), 00401 array( null, -1 ), 00402 00403 # Failures returns 0 :( 00404 array( 'ABCDEFG', 0 ), 00405 array( 'Ak', 0 ), 00406 00407 # Int, strings with spaces 00408 array( 1, 1 ), 00409 array( ' 1 ', 1 ), 00410 array( 1023, 1023 ), 00411 array( ' 1023 ', 1023 ), 00412 00413 # kilo, Mega, Giga 00414 array( '1k', 1024 ), 00415 array( '1K', 1024 ), 00416 array( '1m', 1024 * 1024 ), 00417 array( '1M', 1024 * 1024 ), 00418 array( '1g', 1024 * 1024 * 1024 ), 00419 array( '1G', 1024 * 1024 * 1024 ), 00420 00421 # Negatives 00422 array( -1, -1 ), 00423 array( -500, -500 ), 00424 array( '-500', -500 ), 00425 array( '-1k', -1024 ), 00426 00427 # Zeroes 00428 array( '0', 0 ), 00429 array( '0k', 0 ), 00430 array( '0M', 0 ), 00431 array( '0G', 0 ), 00432 array( '-0', 0 ), 00433 array( '-0k', 0 ), 00434 array( '-0M', 0 ), 00435 array( '-0G', 0 ), 00436 ); 00437 } 00438 00450 public function testMerge( $old, $mine, $yours, $expectedMergeResult, $expectedText ) { 00451 $this->checkHasDiff3(); 00452 00453 $mergedText = null; 00454 $isMerged = wfMerge( $old, $mine, $yours, $mergedText ); 00455 00456 $msg = 'Merge should be a '; 00457 $msg .= $expectedMergeResult ? 'success' : 'failure'; 00458 $this->assertEquals( $expectedMergeResult, $isMerged, $msg ); 00459 00460 if ( $isMerged ) { 00461 // Verify the merged text 00462 $this->assertEquals( $expectedText, $mergedText, 00463 'is merged text as expected?' ); 00464 } 00465 } 00466 00467 public static function provideMerge() { 00468 $EXPECT_MERGE_SUCCESS = true; 00469 $EXPECT_MERGE_FAILURE = false; 00470 00471 return array( 00472 // #0: clean merge 00473 array( 00474 // old: 00475 "one one one\n" . // trimmed 00476 "\n" . 00477 "two two two", 00478 00479 // mine: 00480 "one one one ONE ONE\n" . 00481 "\n" . 00482 "two two two\n", // with tailing whitespace 00483 00484 // yours: 00485 "one one one\n" . 00486 "\n" . 00487 "two two TWO TWO", // trimmed 00488 00489 // ok: 00490 $EXPECT_MERGE_SUCCESS, 00491 00492 // result: 00493 "one one one ONE ONE\n" . 00494 "\n" . 00495 "two two TWO TWO\n", // note: will always end in a newline 00496 ), 00497 00498 // #1: conflict, fail 00499 array( 00500 // old: 00501 "one one one", // trimmed 00502 00503 // mine: 00504 "one one one ONE ONE\n" . 00505 "\n" . 00506 "bla bla\n" . 00507 "\n", // with tailing whitespace 00508 00509 // yours: 00510 "one one one\n" . 00511 "\n" . 00512 "two two", // trimmed 00513 00514 $EXPECT_MERGE_FAILURE, 00515 00516 // result: 00517 null, 00518 ), 00519 ); 00520 } 00521 00526 public function testMakeUrlIndexes( $url, $expected ) { 00527 $index = wfMakeUrlIndexes( $url ); 00528 $this->assertEquals( $expected, $index, "wfMakeUrlIndexes(\"$url\")" ); 00529 } 00530 00531 public static function provideMakeUrlIndexes() { 00532 return array( 00533 array( 00534 // just a regular :) 00535 'https://bugzilla.wikimedia.org/show_bug.cgi?id=28627', 00536 array( 'https://org.wikimedia.bugzilla./show_bug.cgi?id=28627' ) 00537 ), 00538 array( 00539 // mailtos are handled special 00540 // is this really right though? that final . probably belongs earlier? 00541 'mailto:[email protected]', 00542 array( 'mailto:org.wikimedia@wiki.' ) 00543 ), 00544 00545 // file URL cases per bug 28627... 00546 array( 00547 // three slashes: local filesystem path Unix-style 00548 'file:///whatever/you/like.txt', 00549 array( 'file://./whatever/you/like.txt' ) 00550 ), 00551 array( 00552 // three slashes: local filesystem path Windows-style 00553 'file:///c:/whatever/you/like.txt', 00554 array( 'file://./c:/whatever/you/like.txt' ) 00555 ), 00556 array( 00557 // two slashes: UNC filesystem path Windows-style 00558 'file://intranet/whatever/you/like.txt', 00559 array( 'file://intranet./whatever/you/like.txt' ) 00560 ), 00561 // Multiple-slash cases that can sorta work on Mozilla 00562 // if you hack it just right are kinda pathological, 00563 // and unreliable cross-platform or on IE which means they're 00564 // unlikely to appear on intranets. 00565 // 00566 // Those will survive the algorithm but with results that 00567 // are less consistent. 00568 00569 // protocol-relative URL cases per bug 29854... 00570 array( 00571 '//bugzilla.wikimedia.org/show_bug.cgi?id=28627', 00572 array( 00573 'http://org.wikimedia.bugzilla./show_bug.cgi?id=28627', 00574 'https://org.wikimedia.bugzilla./show_bug.cgi?id=28627' 00575 ) 00576 ), 00577 ); 00578 } 00579 00584 public function testWfMatchesDomainList( $url, $domains, $expected, $description ) { 00585 $actual = wfMatchesDomainList( $url, $domains ); 00586 $this->assertEquals( $expected, $actual, $description ); 00587 } 00588 00589 public static function provideWfMatchesDomainList() { 00590 $a = array(); 00591 $protocols = array( 'HTTP' => 'http:', 'HTTPS' => 'https:', 'protocol-relative' => '' ); 00592 foreach ( $protocols as $pDesc => $p ) { 00593 $a = array_merge( $a, array( 00594 array( "$p//www.example.com", array(), false, "No matches for empty domains array, $pDesc URL" ), 00595 array( "$p//www.example.com", array( 'www.example.com' ), true, "Exact match in domains array, $pDesc URL" ), 00596 array( "$p//www.example.com", array( 'example.com' ), true, "Match without subdomain in domains array, $pDesc URL" ), 00597 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" ), 00598 array( "$p//www.example2.com", array( 'example.com', 'example2.com', 'example3,com' ), true, "Match without subdomain with other domains in array, $pDesc URL" ), 00599 array( "$p//www.example4.com", array( 'example.com', 'example2.com', 'example3,com' ), false, "Domain not in array, $pDesc URL" ), 00600 array( "$p//nds-nl.wikipedia.org", array( 'nl.wikipedia.org' ), false, "Non-matching substring of domain, $pDesc URL" ), 00601 ) ); 00602 } 00603 00604 return $a; 00605 } 00606 00610 public function testWfMkdirParents() { 00611 // Should not return true if file exists instead of directory 00612 $fname = $this->getNewTempFile(); 00613 wfSuppressWarnings(); 00614 $ok = wfMkdirParents( $fname ); 00615 wfRestoreWarnings(); 00616 $this->assertFalse( $ok ); 00617 } 00618 00623 public function testWfShellMaintenanceCmd( $script, $parameters, $options, $expected, $description ) { 00624 if ( wfIsWindows() ) { 00625 // Approximation that's good enough for our purposes just now 00626 $expected = str_replace( "'", '"', $expected ); 00627 } 00628 $actual = wfShellMaintenanceCmd( $script, $parameters, $options ); 00629 $this->assertEquals( $expected, $actual, $description ); 00630 } 00631 00632 public static function provideWfShellMaintenanceCmdList() { 00633 global $wgPhpCli; 00634 00635 return array( 00636 array( 'eval.php', array( '--help', '--test' ), array(), 00637 "'$wgPhpCli' 'eval.php' '--help' '--test'", 00638 "Called eval.php --help --test" ), 00639 array( 'eval.php', array( '--help', '--test space' ), array( 'php' => 'php5' ), 00640 "'php5' 'eval.php' '--help' '--test space'", 00641 "Called eval.php --help --test with php option" ), 00642 array( 'eval.php', array( '--help', '--test', 'X' ), array( 'wrapper' => 'MWScript.php' ), 00643 "'$wgPhpCli' 'MWScript.php' 'eval.php' '--help' '--test' 'X'", 00644 "Called eval.php --help --test with wrapper option" ), 00645 array( 'eval.php', array( '--help', '--test', 'y' ), array( 'php' => 'php5', 'wrapper' => 'MWScript.php' ), 00646 "'php5' 'MWScript.php' 'eval.php' '--help' '--test' 'y'", 00647 "Called eval.php --help --test with wrapper and php option" ), 00648 ); 00649 } 00650 /* @TODO many more! */ 00651 }