MediaWiki
REL1_22
|
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 00144 00148 public function testArrayToCGI2() { 00149 $this->assertEquals( 00150 "baz=bar&foo=bar", 00151 wfArrayToCgi( 00152 array( 'baz' => 'bar' ), 00153 array( 'foo' => 'bar', 'baz' => 'overridden value' ) ) ); 00154 } 00155 00156 public static function provideCgiToArray() { 00157 return array( 00158 array( '', array() ), // empty 00159 array( 'foo=bar', array( 'foo' => 'bar' ) ), // string 00160 array( 'foo=', array( 'foo' => '' ) ), // empty string 00161 array( 'foo', array( 'foo' => '' ) ), // missing = 00162 array( 'foo=bar&qwerty=asdf', array( 'foo' => 'bar', 'qwerty' => 'asdf' ) ), // multiple value 00163 array( 'foo=A%26B%3D5%2B6%40%21%22%27', array( 'foo' => 'A&B=5+6@!"\'' ) ), // urldecoding test 00164 array( 'foo%5Bbar%5D=baz', array( 'foo' => array( 'bar' => 'baz' ) ) ), 00165 array( 'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf', array( 'foo' => array( 'bar' => 'baz', 'qwerty' => 'asdf' ) ) ), 00166 array( 'foo%5B0%5D=bar&foo%5B1%5D=baz', array( 'foo' => array( 0 => 'bar', 1 => 'baz' ) ) ), 00167 array( 'foo%5Bbar%5D%5Bbar%5D=baz', array( 'foo' => array( 'bar' => array( 'bar' => 'baz' ) ) ) ), 00168 ); 00169 } 00170 00175 public function testCgiToArray( $cgi, $result ) { 00176 $this->assertEquals( $result, wfCgiToArray( $cgi ) ); 00177 } 00178 00179 public static function provideCgiRoundTrip() { 00180 return array( 00181 array( '' ), 00182 array( 'foo=bar' ), 00183 array( 'foo=' ), 00184 array( 'foo=bar&baz=biz' ), 00185 array( 'foo=A%26B%3D5%2B6%40%21%22%27' ), 00186 array( 'foo%5Bbar%5D=baz' ), 00187 array( 'foo%5B0%5D=bar&foo%5B1%5D=baz' ), 00188 array( 'foo%5Bbar%5D%5Bbar%5D=baz' ), 00189 ); 00190 } 00191 00196 public function testCgiRoundTrip( $cgi ) { 00197 $this->assertEquals( $cgi, wfArrayToCgi( wfCgiToArray( $cgi ) ) ); 00198 } 00199 00203 public function testMimeTypeMatch() { 00204 $this->assertEquals( 00205 'text/html', 00206 mimeTypeMatch( 'text/html', 00207 array( 'application/xhtml+xml' => 1.0, 00208 'text/html' => 0.7, 00209 'text/plain' => 0.3 ) ) ); 00210 $this->assertEquals( 00211 'text/*', 00212 mimeTypeMatch( 'text/html', 00213 array( 'image/*' => 1.0, 00214 'text/*' => 0.5 ) ) ); 00215 $this->assertEquals( 00216 '*/*', 00217 mimeTypeMatch( 'text/html', 00218 array( '*/*' => 1.0 ) ) ); 00219 $this->assertNull( 00220 mimeTypeMatch( 'text/html', 00221 array( 'image/png' => 1.0, 00222 'image/svg+xml' => 0.5 ) ) ); 00223 } 00224 00228 public function testNegotiateType() { 00229 $this->assertEquals( 00230 'text/html', 00231 wfNegotiateType( 00232 array( 'application/xhtml+xml' => 1.0, 00233 'text/html' => 0.7, 00234 'text/plain' => 0.5, 00235 'text/*' => 0.2 ), 00236 array( 'text/html' => 1.0 ) ) ); 00237 $this->assertEquals( 00238 'application/xhtml+xml', 00239 wfNegotiateType( 00240 array( 'application/xhtml+xml' => 1.0, 00241 'text/html' => 0.7, 00242 'text/plain' => 0.5, 00243 'text/*' => 0.2 ), 00244 array( 'application/xhtml+xml' => 1.0, 00245 'text/html' => 0.5 ) ) ); 00246 $this->assertEquals( 00247 'text/html', 00248 wfNegotiateType( 00249 array( 'text/html' => 1.0, 00250 'text/plain' => 0.5, 00251 'text/*' => 0.5, 00252 'application/xhtml+xml' => 0.2 ), 00253 array( 'application/xhtml+xml' => 1.0, 00254 'text/html' => 0.5 ) ) ); 00255 $this->assertEquals( 00256 'text/html', 00257 wfNegotiateType( 00258 array( 'text/*' => 1.0, 00259 'image/*' => 0.7, 00260 '*/*' => 0.3 ), 00261 array( 'application/xhtml+xml' => 1.0, 00262 'text/html' => 0.5 ) ) ); 00263 $this->assertNull( 00264 wfNegotiateType( 00265 array( 'text/*' => 1.0 ), 00266 array( 'application/xhtml+xml' => 1.0 ) ) ); 00267 } 00268 00273 public function testDebugFunctionTest() { 00274 00275 global $wgDebugLogFile, $wgDebugTimestamps; 00276 00277 $old_log_file = $wgDebugLogFile; 00278 $wgDebugLogFile = tempnam( wfTempDir(), 'mw-' ); 00279 # @todo FIXME: $wgDebugTimestamps should be tested 00280 $old_wgDebugTimestamps = $wgDebugTimestamps; 00281 $wgDebugTimestamps = false; 00282 00283 wfDebug( "This is a normal string" ); 00284 $this->assertEquals( "This is a normal string", file_get_contents( $wgDebugLogFile ) ); 00285 unlink( $wgDebugLogFile ); 00286 00287 wfDebug( "This is nöt an ASCII string" ); 00288 $this->assertEquals( "This is nöt an ASCII string", file_get_contents( $wgDebugLogFile ) ); 00289 unlink( $wgDebugLogFile ); 00290 00291 wfDebug( "\00305This has böth UTF and control chars\003" ); 00292 $this->assertEquals( " 05This has böth UTF and control chars ", file_get_contents( $wgDebugLogFile ) ); 00293 unlink( $wgDebugLogFile ); 00294 00295 wfDebugMem(); 00296 $this->assertGreaterThan( 5000, preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) ) ); 00297 unlink( $wgDebugLogFile ); 00298 00299 wfDebugMem( true ); 00300 $this->assertGreaterThan( 5000000, preg_replace( '/\D/', '', file_get_contents( $wgDebugLogFile ) ) ); 00301 unlink( $wgDebugLogFile ); 00302 00303 $wgDebugLogFile = $old_log_file; 00304 $wgDebugTimestamps = $old_wgDebugTimestamps; 00305 } 00306 00310 public function testClientAcceptsGzipTest() { 00311 00312 $settings = array( 00313 'gzip' => true, 00314 'bzip' => false, 00315 '*' => false, 00316 'compress, gzip' => true, 00317 'gzip;q=1.0' => true, 00318 'foozip' => false, 00319 'foo*zip' => false, 00320 'gzip;q=abcde' => true, //is this REALLY valid? 00321 'gzip;q=12345678.9' => true, 00322 ' gzip' => true, 00323 ); 00324 00325 if ( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) { 00326 $old_server_setting = $_SERVER['HTTP_ACCEPT_ENCODING']; 00327 } 00328 00329 foreach ( $settings as $encoding => $expect ) { 00330 $_SERVER['HTTP_ACCEPT_ENCODING'] = $encoding; 00331 00332 $this->assertEquals( $expect, wfClientAcceptsGzip( true ), 00333 "'$encoding' => " . wfBoolToStr( $expect ) ); 00334 } 00335 00336 if ( isset( $old_server_setting ) ) { 00337 $_SERVER['HTTP_ACCEPT_ENCODING'] = $old_server_setting; 00338 } 00339 } 00340 00344 public function testSwapVarsTest() { 00345 $var1 = 1; 00346 $var2 = 2; 00347 00348 $this->assertEquals( $var1, 1, 'var1 is set originally' ); 00349 $this->assertEquals( $var2, 2, 'var1 is set originally' ); 00350 00351 swap( $var1, $var2 ); 00352 00353 $this->assertEquals( $var1, 2, 'var1 is swapped' ); 00354 $this->assertEquals( $var2, 1, 'var2 is swapped' ); 00355 } 00356 00360 public function testWfPercentTest() { 00361 00362 $pcts = array( 00363 array( 6 / 7, '0.86%', 2, false ), 00364 array( 3 / 3, '1%' ), 00365 array( 22 / 7, '3.14286%', 5 ), 00366 array( 3 / 6, '0.5%' ), 00367 array( 1 / 3, '0%', 0 ), 00368 array( 10 / 3, '0%', -1 ), 00369 array( 3 / 4 / 5, '0.1%', 1 ), 00370 array( 6 / 7 * 8, '6.8571428571%', 10 ), 00371 ); 00372 00373 foreach ( $pcts as $pct ) { 00374 if ( !isset( $pct[2] ) ) { 00375 $pct[2] = 2; 00376 } 00377 if ( !isset( $pct[3] ) ) { 00378 $pct[3] = true; 00379 } 00380 00381 $this->assertEquals( wfPercent( $pct[0], $pct[2], $pct[3] ), $pct[1], $pct[1] ); 00382 } 00383 } 00384 00390 public function testWfShorthandToInteger( $shorthand, $expected ) { 00391 $this->assertEquals( $expected, 00392 wfShorthandToInteger( $shorthand ) 00393 ); 00394 } 00395 00397 public static function provideShorthand() { 00398 return array( 00399 # Null, empty ... 00400 array( '', -1 ), 00401 array( ' ', -1 ), 00402 array( null, -1 ), 00403 00404 # Failures returns 0 :( 00405 array( 'ABCDEFG', 0 ), 00406 array( 'Ak', 0 ), 00407 00408 # Int, strings with spaces 00409 array( 1, 1 ), 00410 array( ' 1 ', 1 ), 00411 array( 1023, 1023 ), 00412 array( ' 1023 ', 1023 ), 00413 00414 # kilo, Mega, Giga 00415 array( '1k', 1024 ), 00416 array( '1K', 1024 ), 00417 array( '1m', 1024 * 1024 ), 00418 array( '1M', 1024 * 1024 ), 00419 array( '1g', 1024 * 1024 * 1024 ), 00420 array( '1G', 1024 * 1024 * 1024 ), 00421 00422 # Negatives 00423 array( -1, -1 ), 00424 array( -500, -500 ), 00425 array( '-500', -500 ), 00426 array( '-1k', -1024 ), 00427 00428 # Zeroes 00429 array( '0', 0 ), 00430 array( '0k', 0 ), 00431 array( '0M', 0 ), 00432 array( '0G', 0 ), 00433 array( '-0', 0 ), 00434 array( '-0k', 0 ), 00435 array( '-0M', 0 ), 00436 array( '-0G', 0 ), 00437 ); 00438 } 00439 00451 public function testMerge( $old, $mine, $yours, $expectedMergeResult, $expectedText ) { 00452 $this->checkHasDiff3(); 00453 00454 $mergedText = null; 00455 $isMerged = wfMerge( $old, $mine, $yours, $mergedText ); 00456 00457 $msg = 'Merge should be a '; 00458 $msg .= $expectedMergeResult ? 'success' : 'failure'; 00459 $this->assertEquals( $expectedMergeResult, $isMerged, $msg ); 00460 00461 if ( $isMerged ) { 00462 // Verify the merged text 00463 $this->assertEquals( $expectedText, $mergedText, 00464 'is merged text as expected?' ); 00465 } 00466 } 00467 00468 public static function provideMerge() { 00469 $EXPECT_MERGE_SUCCESS = true; 00470 $EXPECT_MERGE_FAILURE = false; 00471 00472 return array( 00473 // #0: clean merge 00474 array( 00475 // old: 00476 "one one one\n" . // trimmed 00477 "\n" . 00478 "two two two", 00479 00480 // mine: 00481 "one one one ONE ONE\n" . 00482 "\n" . 00483 "two two two\n", // with tailing whitespace 00484 00485 // yours: 00486 "one one one\n" . 00487 "\n" . 00488 "two two TWO TWO", // trimmed 00489 00490 // ok: 00491 $EXPECT_MERGE_SUCCESS, 00492 00493 // result: 00494 "one one one ONE ONE\n" . 00495 "\n" . 00496 "two two TWO TWO\n", // note: will always end in a newline 00497 ), 00498 00499 // #1: conflict, fail 00500 array( 00501 // old: 00502 "one one one", // trimmed 00503 00504 // mine: 00505 "one one one ONE ONE\n" . 00506 "\n" . 00507 "bla bla\n" . 00508 "\n", // with tailing whitespace 00509 00510 // yours: 00511 "one one one\n" . 00512 "\n" . 00513 "two two", // trimmed 00514 00515 $EXPECT_MERGE_FAILURE, 00516 00517 // result: 00518 null, 00519 ), 00520 ); 00521 } 00522 00527 public function testMakeUrlIndexes( $url, $expected ) { 00528 $index = wfMakeUrlIndexes( $url ); 00529 $this->assertEquals( $expected, $index, "wfMakeUrlIndexes(\"$url\")" ); 00530 } 00531 00532 public static function provideMakeUrlIndexes() { 00533 return array( 00534 array( 00535 // just a regular :) 00536 'https://bugzilla.wikimedia.org/show_bug.cgi?id=28627', 00537 array( 'https://org.wikimedia.bugzilla./show_bug.cgi?id=28627' ) 00538 ), 00539 array( 00540 // mailtos are handled special 00541 // is this really right though? that final . probably belongs earlier? 00542 'mailto:[email protected]', 00543 array( 'mailto:org.wikimedia@wiki.' ) 00544 ), 00545 00546 // file URL cases per bug 28627... 00547 array( 00548 // three slashes: local filesystem path Unix-style 00549 'file:///whatever/you/like.txt', 00550 array( 'file://./whatever/you/like.txt' ) 00551 ), 00552 array( 00553 // three slashes: local filesystem path Windows-style 00554 'file:///c:/whatever/you/like.txt', 00555 array( 'file://./c:/whatever/you/like.txt' ) 00556 ), 00557 array( 00558 // two slashes: UNC filesystem path Windows-style 00559 'file://intranet/whatever/you/like.txt', 00560 array( 'file://intranet./whatever/you/like.txt' ) 00561 ), 00562 // Multiple-slash cases that can sorta work on Mozilla 00563 // if you hack it just right are kinda pathological, 00564 // and unreliable cross-platform or on IE which means they're 00565 // unlikely to appear on intranets. 00566 // 00567 // Those will survive the algorithm but with results that 00568 // are less consistent. 00569 00570 // protocol-relative URL cases per bug 29854... 00571 array( 00572 '//bugzilla.wikimedia.org/show_bug.cgi?id=28627', 00573 array( 00574 'http://org.wikimedia.bugzilla./show_bug.cgi?id=28627', 00575 'https://org.wikimedia.bugzilla./show_bug.cgi?id=28627' 00576 ) 00577 ), 00578 ); 00579 } 00580 00585 public function testWfMatchesDomainList( $url, $domains, $expected, $description ) { 00586 $actual = wfMatchesDomainList( $url, $domains ); 00587 $this->assertEquals( $expected, $actual, $description ); 00588 } 00589 00590 public static function provideWfMatchesDomainList() { 00591 $a = array(); 00592 $protocols = array( 'HTTP' => 'http:', 'HTTPS' => 'https:', 'protocol-relative' => '' ); 00593 foreach ( $protocols as $pDesc => $p ) { 00594 $a = array_merge( $a, array( 00595 array( "$p//www.example.com", array(), false, "No matches for empty domains array, $pDesc URL" ), 00596 array( "$p//www.example.com", array( 'www.example.com' ), true, "Exact match in domains array, $pDesc URL" ), 00597 array( "$p//www.example.com", array( 'example.com' ), true, "Match without subdomain in domains array, $pDesc URL" ), 00598 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" ), 00599 array( "$p//www.example2.com", array( 'example.com', 'example2.com', 'example3,com' ), true, "Match without subdomain with other domains in array, $pDesc URL" ), 00600 array( "$p//www.example4.com", array( 'example.com', 'example2.com', 'example3,com' ), false, "Domain not in array, $pDesc URL" ), 00601 array( "$p//nds-nl.wikipedia.org", array( 'nl.wikipedia.org' ), false, "Non-matching substring of domain, $pDesc URL" ), 00602 ) ); 00603 } 00604 00605 return $a; 00606 } 00607 00611 public function testWfMkdirParents() { 00612 // Should not return true if file exists instead of directory 00613 $fname = $this->getNewTempFile(); 00614 wfSuppressWarnings(); 00615 $ok = wfMkdirParents( $fname ); 00616 wfRestoreWarnings(); 00617 $this->assertFalse( $ok ); 00618 } 00619 00624 public function testWfShellMaintenanceCmd( $script, $parameters, $options, $expected, $description ) { 00625 if ( wfIsWindows() ) { 00626 // Approximation that's good enough for our purposes just now 00627 $expected = str_replace( "'", '"', $expected ); 00628 } 00629 $actual = wfShellMaintenanceCmd( $script, $parameters, $options ); 00630 $this->assertEquals( $expected, $actual, $description ); 00631 } 00632 00633 public static function provideWfShellMaintenanceCmdList() { 00634 global $wgPhpCli; 00635 00636 return array( 00637 array( 'eval.php', array( '--help', '--test' ), array(), 00638 "'$wgPhpCli' 'eval.php' '--help' '--test'", 00639 "Called eval.php --help --test" ), 00640 array( 'eval.php', array( '--help', '--test space' ), array( 'php' => 'php5' ), 00641 "'php5' 'eval.php' '--help' '--test space'", 00642 "Called eval.php --help --test with php option" ), 00643 array( 'eval.php', array( '--help', '--test', 'X' ), array( 'wrapper' => 'MWScript.php' ), 00644 "'$wgPhpCli' 'MWScript.php' 'eval.php' '--help' '--test' 'X'", 00645 "Called eval.php --help --test with wrapper option" ), 00646 array( 'eval.php', array( '--help', '--test', 'y' ), array( 'php' => 'php5', 'wrapper' => 'MWScript.php' ), 00647 "'php5' 'MWScript.php' 'eval.php' '--help' '--test' 'y'", 00648 "Called eval.php --help --test with wrapper and php option" ), 00649 ); 00650 } 00651 /* @TODO many more! */ 00652 }