| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Logger\LegacyLogger; |
| 4 |
use MediaWiki\MainConfigNames; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
class GlobalTest extends MediaWikiIntegrationTestCase { |
| 11 |
protected function setUp(): void { |
| 12 |
parent::setUp(); |
| 13 |
|
| 14 |
$this->overrideConfigValues( [ |
| 15 |
MainConfigNames::UrlProtocols => [ |
| 16 |
'http://', |
| 17 |
'https://', |
| 18 |
'mailto:', |
| 19 |
'//', |
| 20 |
'file://', |
| 21 |
], |
| 22 |
] ); |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public function testWfArrayDiff2( $a, $b, $expected ) { |
| 30 |
$this->expectDeprecationAndContinue( '/wfArrayDiff2/' ); |
| 31 |
$this->assertEquals( |
| 32 |
$expected, wfArrayDiff2( $a, $b ) |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
public static function provideForWfArrayDiff2() { |
| 38 |
|
| 39 |
return [ |
| 40 |
[ |
| 41 |
[ 'a', 'b' ], |
| 42 |
[ 'a', 'b' ], |
| 43 |
[], |
| 44 |
], |
| 45 |
[ |
| 46 |
[ [ 'a' ], [ 'a', 'b', 'c' ] ], |
| 47 |
[ [ 'a' ], [ 'a', 'b' ] ], |
| 48 |
[ 1 => [ 'a', 'b', 'c' ] ], |
| 49 |
], |
| 50 |
]; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function testRandom() { |
| 62 |
$this->assertFalse( |
| 63 |
wfRandom() == wfRandom() |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public function testRandomString() { |
| 71 |
$this->assertFalse( |
| 72 |
wfRandomString() == wfRandomString() |
| 73 |
); |
| 74 |
$this->assertSame( 10, strlen( wfRandomString( 10 ) ), 'length' ); |
| 75 |
$this->assertSame( 1, preg_match( '/^[0-9a-f]+$/i', wfRandomString() ), 'pattern' ); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public function testUrlencode() { |
| 82 |
$this->assertEquals( |
| 83 |
"%E7%89%B9%E5%88%A5:Contributions/Foobar", |
| 84 |
wfUrlencode( "\xE7\x89\xB9\xE5\x88\xA5:Contributions/Foobar" ) ); |
| 85 |
} |
| 86 |
|
| 87 |
public static function provideArrayToCGI() { |
| 88 |
return [ |
| 89 |
[ [], '' ], |
| 90 |
[ [ 'foo' => 'bar' ], 'foo=bar' ], |
| 91 |
[ [ 'foo' => '' ], 'foo=' ], |
| 92 |
[ [ 'foo' => 1 ], 'foo=1' ], |
| 93 |
[ [ 'foo' => true ], 'foo=1' ], |
| 94 |
[ [ 'foo' => false ], '' ], |
| 95 |
[ [ 'foo' => null ], '' ], |
| 96 |
[ [ 'foo' => 'A&B=5+6@!"\'' ], 'foo=A%26B%3D5%2B6%40%21%22%27' ], |
| 97 |
[ |
| 98 |
[ 'foo' => 'bar', 'baz' => 'is', 'asdf' => 'qwerty' ], |
| 99 |
'foo=bar&baz=is&asdf=qwerty' |
| 100 |
], |
| 101 |
[ [ 'foo' => [ 'bar' => 'baz' ] ], 'foo%5Bbar%5D=baz' ], |
| 102 |
[ |
| 103 |
[ 'foo' => [ 'bar' => 'baz', 'qwerty' => 'asdf' ] ], |
| 104 |
'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf' |
| 105 |
], |
| 106 |
[ [ 'foo' => [ 'bar', 'baz' ] ], 'foo%5B0%5D=bar&foo%5B1%5D=baz' ], |
| 107 |
[ |
| 108 |
[ 'foo' => [ 'bar' => [ 'bar' => 'baz' ] ] ], |
| 109 |
'foo%5Bbar%5D%5Bbar%5D=baz' |
| 110 |
], |
| 111 |
]; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public function testArrayToCGI( $array, $result ) { |
| 119 |
$this->assertEquals( $result, wfArrayToCgi( $array ) ); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public function testArrayToCGI2() { |
| 126 |
$this->assertEquals( |
| 127 |
"baz=bar&foo=bar", |
| 128 |
wfArrayToCgi( |
| 129 |
[ 'baz' => 'bar' ], |
| 130 |
[ 'foo' => 'bar', 'baz' => 'overridden value' ] ) ); |
| 131 |
} |
| 132 |
|
| 133 |
public static function provideCgiToArray() { |
| 134 |
return [ |
| 135 |
[ '', [] ], |
| 136 |
[ 'foo=bar', [ 'foo' => 'bar' ] ], |
| 137 |
[ 'foo=', [ 'foo' => '' ] ], |
| 138 |
[ 'foo', [ 'foo' => '' ] ], |
| 139 |
[ 'foo=bar&qwerty=asdf', [ 'foo' => 'bar', 'qwerty' => 'asdf' ] ], |
| 140 |
[ 'foo=A%26B%3D5%2B6%40%21%22%27', [ 'foo' => 'A&B=5+6@!"\'' ] ], |
| 141 |
[ 'foo[bar]=baz', [ 'foo' => [ 'bar' => 'baz' ] ] ], |
| 142 |
[ 'foo%5Bbar%5D=baz', [ 'foo' => [ 'bar' => 'baz' ] ] ], |
| 143 |
[ |
| 144 |
'foo%5Bbar%5D=baz&foo%5Bqwerty%5D=asdf', |
| 145 |
[ 'foo' => [ 'bar' => 'baz', 'qwerty' => 'asdf' ] ] |
| 146 |
], |
| 147 |
[ 'foo%5B0%5D=bar&foo%5B1%5D=baz', [ 'foo' => [ 0 => 'bar', 1 => 'baz' ] ] ], |
| 148 |
[ |
| 149 |
'foo%5Bbar%5D%5Bbar%5D=baz', |
| 150 |
[ 'foo' => [ 'bar' => [ 'bar' => 'baz' ] ] ] |
| 151 |
], |
| 152 |
[ 'foo[]=x&foo[]=y', [ 'foo' => [ '' => 'y' ] ] ], |
| 153 |
[ 'foo=x&foo[]=y', [ 'foo' => [ '' => 'y' ] ] ], |
| 154 |
]; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public function testCgiToArray( $cgi, $result ) { |
| 162 |
$this->assertEquals( $result, wfCgiToArray( $cgi ) ); |
| 163 |
} |
| 164 |
|
| 165 |
public static function provideCgiRoundTrip() { |
| 166 |
return [ |
| 167 |
[ '' ], |
| 168 |
[ 'foo=bar' ], |
| 169 |
[ 'foo=' ], |
| 170 |
[ 'foo=bar&baz=biz' ], |
| 171 |
[ 'foo=A%26B%3D5%2B6%40%21%22%27' ], |
| 172 |
[ 'foo%5Bbar%5D=baz' ], |
| 173 |
[ 'foo%5B0%5D=bar&foo%5B1%5D=baz' ], |
| 174 |
[ 'foo%5Bbar%5D%5Bbar%5D=baz' ], |
| 175 |
]; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public function testCgiRoundTrip( $cgi ) { |
| 183 |
$this->assertEquals( $cgi, wfArrayToCgi( wfCgiToArray( $cgi ) ) ); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public function testDebugFunctionTest() { |
| 190 |
$debugLogFile = $this->getNewTempFile(); |
| 191 |
|
| 192 |
$this->overrideConfigValue( MainConfigNames::DebugLogFile, $debugLogFile ); |
| 193 |
$this->setLogger( 'wfDebug', new LegacyLogger( 'wfDebug' ) ); |
| 194 |
|
| 195 |
unlink( $debugLogFile ); |
| 196 |
wfDebug( "This is a normal string" ); |
| 197 |
$this->assertEquals( "This is a normal string\n", file_get_contents( $debugLogFile ) ); |
| 198 |
|
| 199 |
unlink( $debugLogFile ); |
| 200 |
wfDebug( "This is nöt an ASCII string" ); |
| 201 |
$this->assertEquals( "This is nöt an ASCII string\n", file_get_contents( $debugLogFile ) ); |
| 202 |
|
| 203 |
unlink( $debugLogFile ); |
| 204 |
wfDebug( "\00305This has böth UTF and control chars\003" ); |
| 205 |
$this->assertEquals( |
| 206 |
" 05This has böth UTF and control chars \n", |
| 207 |
file_get_contents( $debugLogFile ) |
| 208 |
); |
| 209 |
|
| 210 |
unlink( $debugLogFile ); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public function testClientAcceptsGzipTest() { |
| 217 |
$settings = [ |
| 218 |
'gzip' => true, |
| 219 |
'bzip' => false, |
| 220 |
'*' => false, |
| 221 |
'compress, gzip' => true, |
| 222 |
'gzip;q=1.0' => true, |
| 223 |
'foozip' => false, |
| 224 |
'foo*zip' => false, |
| 225 |
'gzip;q=abcde' => true, |
| 226 |
'gzip;q=12345678.9' => true, |
| 227 |
' gzip' => true, |
| 228 |
]; |
| 229 |
|
| 230 |
if ( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) { |
| 231 |
$old_server_setting = $_SERVER['HTTP_ACCEPT_ENCODING']; |
| 232 |
} |
| 233 |
|
| 234 |
foreach ( $settings as $encoding => $expect ) { |
| 235 |
$_SERVER['HTTP_ACCEPT_ENCODING'] = $encoding; |
| 236 |
|
| 237 |
$this->assertEquals( $expect, wfClientAcceptsGzip( true ), |
| 238 |
"'$encoding' => " . wfBoolToStr( $expect ) ); |
| 239 |
} |
| 240 |
|
| 241 |
if ( isset( $old_server_setting ) ) { |
| 242 |
$_SERVER['HTTP_ACCEPT_ENCODING'] = $old_server_setting; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
public function testWfPercentTest( float $input, |
| 251 |
string $expected, |
| 252 |
int $accuracy = 2, |
| 253 |
bool $round = true |
| 254 |
) { |
| 255 |
$this->assertSame( $expected, wfPercent( $input, $accuracy, $round ) ); |
| 256 |
} |
| 257 |
|
| 258 |
public static function provideWfPercentTest() { |
| 259 |
return [ |
| 260 |
[ 6 / 7, '0.86%', 2, false ], |
| 261 |
[ 3 / 3, '1%' ], |
| 262 |
[ 22 / 7, '3.14286%', 5 ], |
| 263 |
[ 3 / 6, '0.5%' ], |
| 264 |
[ 1 / 3, '0%', 0 ], |
| 265 |
[ 10 / 3, '0%', -1 ], |
| 266 |
[ 123.456, '120%', -1 ], |
| 267 |
[ 3 / 4 / 5, '0.1%', 1 ], |
| 268 |
[ 6 / 7 * 8, '6.8571428571%', 10 ], |
| 269 |
]; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public function testWfShorthandToInteger( $shorthand, $expected ) { |
| 278 |
$this->assertEquals( $expected, |
| 279 |
wfShorthandToInteger( $shorthand ) |
| 280 |
); |
| 281 |
} |
| 282 |
|
| 283 |
public static function provideShorthand() { |
| 284 |
|
| 285 |
return [ |
| 286 |
|
| 287 |
[ '', -1 ], |
| 288 |
[ ' ', -1 ], |
| 289 |
[ null, -1 ], |
| 290 |
|
| 291 |
|
| 292 |
[ 'ABCDEFG', 0 ], |
| 293 |
[ 'Ak', 0 ], |
| 294 |
|
| 295 |
|
| 296 |
[ 1, 1 ], |
| 297 |
[ ' 1 ', 1 ], |
| 298 |
[ 1023, 1023 ], |
| 299 |
[ ' 1023 ', 1023 ], |
| 300 |
|
| 301 |
|
| 302 |
[ '1k', 1024 ], |
| 303 |
[ '1K', 1024 ], |
| 304 |
[ '1m', 1024 * 1024 ], |
| 305 |
[ '1M', 1024 * 1024 ], |
| 306 |
[ '1g', 1024 * 1024 * 1024 ], |
| 307 |
[ '1G', 1024 * 1024 * 1024 ], |
| 308 |
|
| 309 |
|
| 310 |
[ -1, -1 ], |
| 311 |
[ -500, -500 ], |
| 312 |
[ '-500', -500 ], |
| 313 |
[ '-1k', -1024 ], |
| 314 |
|
| 315 |
|
| 316 |
[ '0', 0 ], |
| 317 |
[ '0k', 0 ], |
| 318 |
[ '0M', 0 ], |
| 319 |
[ '0G', 0 ], |
| 320 |
[ '-0', 0 ], |
| 321 |
[ '-0k', 0 ], |
| 322 |
[ '-0M', 0 ], |
| 323 |
[ '-0G', 0 ], |
| 324 |
]; |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
public function testMerge_worksWithLessParameters() { |
| 331 |
$this->markTestSkippedIfNoDiff3(); |
| 332 |
|
| 333 |
$mergedText = null; |
| 334 |
$successfulMerge = wfMerge( "old1\n\nold2", "old1\n\nnew2", "new1\n\nold2", $mergedText ); |
| 335 |
|
| 336 |
$mergedText = null; |
| 337 |
$conflictingMerge = wfMerge( 'old', 'old and mine', 'old and yours', $mergedText ); |
| 338 |
|
| 339 |
$this->assertTrue( $successfulMerge ); |
| 340 |
$this->assertFalse( $conflictingMerge ); |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
public function testMerge( |
| 356 |
$old, $mine, $yours, $expectedMergeResult, $expectedText, $expectedMergeAttemptResult |
| 357 |
) { |
| 358 |
$this->markTestSkippedIfNoDiff3(); |
| 359 |
|
| 360 |
$mergedText = null; |
| 361 |
$mergeAttemptResult = null; |
| 362 |
$isMerged = wfMerge( $old, $mine, $yours, $mergedText, $mergeAttemptResult ); |
| 363 |
|
| 364 |
$msg = 'Merge should be a '; |
| 365 |
$msg .= $expectedMergeResult ? 'success' : 'failure'; |
| 366 |
$this->assertEquals( $expectedMergeResult, $isMerged, $msg ); |
| 367 |
$this->assertEquals( $expectedMergeAttemptResult, $mergeAttemptResult ); |
| 368 |
|
| 369 |
if ( $isMerged ) { |
| 370 |
|
| 371 |
$this->assertEquals( $expectedText, $mergedText, |
| 372 |
'is merged text as expected?' ); |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
public static function provideMerge() { |
| 377 |
$EXPECT_MERGE_SUCCESS = true; |
| 378 |
$EXPECT_MERGE_FAILURE = false; |
| 379 |
|
| 380 |
return [ |
| 381 |
|
| 382 |
[ |
| 383 |
|
| 384 |
"one one one\n" . |
| 385 |
"\n" . |
| 386 |
"two two two", |
| 387 |
|
| 388 |
|
| 389 |
"one one one ONE ONE\n" . |
| 390 |
"\n" . |
| 391 |
"two two two\n", |
| 392 |
|
| 393 |
|
| 394 |
"one one one\n" . |
| 395 |
"\n" . |
| 396 |
"two two TWO TWO", |
| 397 |
|
| 398 |
|
| 399 |
$EXPECT_MERGE_SUCCESS, |
| 400 |
|
| 401 |
|
| 402 |
"one one one ONE ONE\n" . |
| 403 |
"\n" . |
| 404 |
"two two TWO TWO\n", |
| 405 |
|
| 406 |
|
| 407 |
"", |
| 408 |
], |
| 409 |
|
| 410 |
|
| 411 |
[ |
| 412 |
|
| 413 |
"one one one", |
| 414 |
|
| 415 |
|
| 416 |
"one one one ONE ONE\n" . |
| 417 |
"\n" . |
| 418 |
"bla bla\n" . |
| 419 |
"\n", |
| 420 |
|
| 421 |
|
| 422 |
"one one one\n" . |
| 423 |
"\n" . |
| 424 |
"two two", |
| 425 |
|
| 426 |
$EXPECT_MERGE_FAILURE, |
| 427 |
|
| 428 |
|
| 429 |
null, |
| 430 |
|
| 431 |
|
| 432 |
"1,3c\n" . |
| 433 |
"one one one\n" . |
| 434 |
"\n" . |
| 435 |
"two two\n" . |
| 436 |
".\n", |
| 437 |
], |
| 438 |
]; |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
public function testWfMatchesDomainList( $url, $domains, $expected ) { |
| 447 |
$actual = wfMatchesDomainList( $url, $domains ); |
| 448 |
$this->assertEquals( $expected, $actual ); |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
public function testWfMkdirParents() { |
| 455 |
|
| 456 |
$fname = $this->getNewTempFile(); |
| 457 |
$this->assertFalse( @wfMkdirParents( $fname ) ); |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
public function testWfShellWikiCmd( $script, $parameters, $options, |
| 465 |
$expected, $description |
| 466 |
) { |
| 467 |
if ( wfIsWindows() ) { |
| 468 |
|
| 469 |
$expected = str_replace( "'", '"', $expected ); |
| 470 |
} |
| 471 |
$actual = wfShellWikiCmd( $script, $parameters, $options ); |
| 472 |
$this->assertEquals( $expected, $actual, $description ); |
| 473 |
} |
| 474 |
|
| 475 |
public static function provideWfShellWikiCmdList() { |
| 476 |
global $wgPhpCli; |
| 477 |
|
| 478 |
return [ |
| 479 |
[ 'eval.php', [ '--help', '--test' ], [], |
| 480 |
"'$wgPhpCli' 'eval.php' '--help' '--test'", |
| 481 |
"Called eval.php --help --test" ], |
| 482 |
[ 'eval.php', [ '--help', '--test space' ], [ 'php' => 'php5' ], |
| 483 |
"'php5' 'eval.php' '--help' '--test space'", |
| 484 |
"Called eval.php --help --test with php option" ], |
| 485 |
[ 'eval.php', [ '--help', '--test', 'X' ], [ 'wrapper' => 'MWScript.php' ], |
| 486 |
"'$wgPhpCli' 'MWScript.php' 'eval.php' '--help' '--test' 'X'", |
| 487 |
"Called eval.php --help --test with wrapper option" ], |
| 488 |
[ |
| 489 |
'eval.php', |
| 490 |
[ '--help', '--test', 'y' ], |
| 491 |
[ 'php' => 'php5', 'wrapper' => 'MWScript.php' ], |
| 492 |
"'php5' 'MWScript.php' 'eval.php' '--help' '--test' 'y'", |
| 493 |
"Called eval.php --help --test with wrapper and php option" |
| 494 |
], |
| 495 |
]; |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
} |