| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Context\RequestContext; |
| 4 |
use MediaWiki\FileRepo\ThumbnailEntryPoint; |
| 5 |
use MediaWiki\MainConfigNames; |
| 6 |
use MediaWiki\Permissions\SimpleAuthority; |
| 7 |
use MediaWiki\Request\FauxRequest; |
| 8 |
use MediaWiki\Tests\FileRepo\TestRepoTrait; |
| 9 |
use MediaWiki\Tests\MockEnvironment; |
| 10 |
use MediaWiki\Title\Title; |
| 11 |
use MediaWiki\User\UserIdentityValue; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class ThumbnailEntryPointTest extends MediaWikiIntegrationTestCase { |
| 18 |
|
| 19 |
use TestRepoTrait; |
| 20 |
use MockHttpTrait; |
| 21 |
|
| 22 |
private const PNG_MAGIC = "\x89\x50\x4e\x47"; |
| 23 |
private const JPEG_MAGIC = "\xff\xd8\xff\xe0"; |
| 24 |
|
| 25 |
private const IMAGES_DIR = __DIR__ . '/../../data/media'; |
| 26 |
|
| 27 |
|
| 28 |
private static $uniqueWidth = 20; |
| 29 |
|
| 30 |
private ?MockEnvironment $environment = null; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public function addDBDataOnce() { |
| 36 |
|
| 37 |
|
| 38 |
RequestContext::getMain()->setUser( $this->getTestUser()->getUser() ); |
| 39 |
|
| 40 |
$this->initTestRepoGroup(); |
| 41 |
|
| 42 |
$this->importFileToTestRepo( self::IMAGES_DIR . '/greyscale-png.png', 'Test.png' ); |
| 43 |
$this->importFileToTestRepo( self::IMAGES_DIR . '/test.jpg', 'Icon.jpg' ); |
| 44 |
|
| 45 |
|
| 46 |
$this->importFileToTestRepo( self::IMAGES_DIR . '/greyscale-na-png.png', 'Test.png' ); |
| 47 |
$this->importFileToTestRepo( self::IMAGES_DIR . '/portrait-rotated.jpg', 'Icon.jpg' ); |
| 48 |
|
| 49 |
|
| 50 |
$title = Title::makeTitle( NS_FILE, 'Redirect_to_Test.png' ); |
| 51 |
$this->editPage( $title, '#REDIRECT [[File:Test.png]]' ); |
| 52 |
|
| 53 |
|
| 54 |
$file = $this->getTestRepo()->newFile( 'Icon.jpg' ); |
| 55 |
$history = $file->getHistory(); |
| 56 |
$oldFile = $history[0]; |
| 57 |
|
| 58 |
$this->getDb()->newUpdateQueryBuilder() |
| 59 |
->table( 'oldimage' ) |
| 60 |
->set( [ 'oi_deleted' => 1 ] ) |
| 61 |
->where( [ 'oi_archive_name' => $oldFile->getArchiveName() ] ) |
| 62 |
->caller( __METHOD__ ) |
| 63 |
->execute(); |
| 64 |
} |
| 65 |
|
| 66 |
public static function tearDownAfterClass(): void { |
| 67 |
self::destroyTestRepo(); |
| 68 |
parent::tearDownAfterClass(); |
| 69 |
} |
| 70 |
|
| 71 |
public function setUp(): void { |
| 72 |
parent::setUp(); |
| 73 |
|
| 74 |
$this->overrideConfigValue( MainConfigNames::ThumbLimits, [ 16, 24 ] ); |
| 75 |
$this->installTestRepoGroup(); |
| 76 |
} |
| 77 |
|
| 78 |
private function recordHeader( string $header ) { |
| 79 |
$this->environment->getFauxResponse()->header( $header ); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
private function makeEnvironment( $request ): MockEnvironment { |
| 88 |
if ( !$request ) { |
| 89 |
$request = new FauxRequest(); |
| 90 |
} |
| 91 |
|
| 92 |
if ( is_string( $request ) ) { |
| 93 |
$request = [ 'f' => $request, 'width' => self::$uniqueWidth++ ]; |
| 94 |
} |
| 95 |
|
| 96 |
if ( is_array( $request ) ) { |
| 97 |
$request = new FauxRequest( $request ); |
| 98 |
$request->setRequestURL( '/w/img.php' ); |
| 99 |
} |
| 100 |
|
| 101 |
$this->environment = new MockEnvironment( $request ); |
| 102 |
return $this->environment; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
private function getEntryPoint( |
| 112 |
?MockEnvironment $environment = null, |
| 113 |
$request = null |
| 114 |
) { |
| 115 |
if ( !$request && $environment ) { |
| 116 |
$request = $environment->getFauxRequest(); |
| 117 |
} |
| 118 |
|
| 119 |
if ( $request instanceof RequestContext ) { |
| 120 |
$context = $request; |
| 121 |
$request = $context->getRequest(); |
| 122 |
} else { |
| 123 |
$context = new RequestContext(); |
| 124 |
$context->setRequest( $request ); |
| 125 |
$context->setUser( $this->getTestUser()->getUser() ); |
| 126 |
} |
| 127 |
|
| 128 |
if ( !$environment ) { |
| 129 |
$environment = $this->makeEnvironment( $request ); |
| 130 |
} |
| 131 |
|
| 132 |
$context->setLanguage( 'qqx' ); |
| 133 |
|
| 134 |
$entryPoint = new ThumbnailEntryPoint( |
| 135 |
$context, |
| 136 |
$environment, |
| 137 |
$this->getServiceContainer() |
| 138 |
); |
| 139 |
|
| 140 |
$entryPoint->enableOutputCapture(); |
| 141 |
return $entryPoint; |
| 142 |
} |
| 143 |
|
| 144 |
public function testNotFound() { |
| 145 |
$env = $this->makeEnvironment( 'Missing_puppy.jpeg' ); |
| 146 |
$entryPoint = $this->getEntryPoint( $env ); |
| 147 |
|
| 148 |
$entryPoint->run(); |
| 149 |
|
| 150 |
$env->assertStatusCode( 404 ); |
| 151 |
$env->assertHeaderValue( |
| 152 |
'text/html; charset=utf-8', |
| 153 |
'Content-Type' |
| 154 |
); |
| 155 |
|
| 156 |
$output = $entryPoint->getCapturedOutput(); |
| 157 |
$this->assertStringContainsString( |
| 158 |
'<title>Error generating thumbnail</title>', |
| 159 |
$output |
| 160 |
); |
| 161 |
} |
| 162 |
|
| 163 |
public function testGenerateAndStreamThumbnail() { |
| 164 |
$env = $this->makeEnvironment( |
| 165 |
[ |
| 166 |
'f' => 'Test.png', |
| 167 |
'width' => 12 |
| 168 |
] |
| 169 |
); |
| 170 |
|
| 171 |
$entryPoint = $this->getEntryPoint( $env ); |
| 172 |
|
| 173 |
$entryPoint->run(); |
| 174 |
$output = $entryPoint->getCapturedOutput(); |
| 175 |
|
| 176 |
$response = $env->getFauxResponse(); |
| 177 |
$this->assertSame( 'image/png', $response->getHeader( 'Content-Type' ) ); |
| 178 |
$this->assertGreaterThan( 500, (int)$response->getHeader( 'Content-Length' ) ); |
| 179 |
|
| 180 |
$env->assertStatusCode( 200, $output ); |
| 181 |
|
| 182 |
$this->assertThumbnail( |
| 183 |
[ 'magic' => self::PNG_MAGIC, 'width' => 12, ], |
| 184 |
$output |
| 185 |
); |
| 186 |
|
| 187 |
return [ 'data' => $output, 'width' => 12 ]; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public function testStreamExistingThumbnail() { |
| 194 |
|
| 195 |
|
| 196 |
$handler = $this->getMockBuilder( BitmapHandler::class ) |
| 197 |
->onlyMethods( [ 'doTransform' ] ) |
| 198 |
->getMock(); |
| 199 |
|
| 200 |
$handler->expects( $this->never() )->method( 'doTransform' ); |
| 201 |
|
| 202 |
$factory = $this->createNoOpMock( MediaHandlerFactory::class, [ 'getHandler' ] ); |
| 203 |
$factory->method( 'getHandler' )->willReturn( $handler ); |
| 204 |
$this->setService( 'MediaHandlerFactory', $factory ); |
| 205 |
|
| 206 |
$env = $this->makeEnvironment( |
| 207 |
[ |
| 208 |
'f' => 'Test.png', |
| 209 |
'width' => 12 |
| 210 |
] |
| 211 |
); |
| 212 |
$entryPoint = $this->getEntryPoint( $env ); |
| 213 |
|
| 214 |
$entryPoint->run(); |
| 215 |
$output = $entryPoint->getCapturedOutput(); |
| 216 |
|
| 217 |
$env->assertStatusCode( 200 ); |
| 218 |
|
| 219 |
$this->assertThumbnail( |
| 220 |
[ 'magic' => self::PNG_MAGIC, 'width' => 12, ], |
| 221 |
$output |
| 222 |
); |
| 223 |
} |
| 224 |
|
| 225 |
public function testNoThumbName() { |
| 226 |
|
| 227 |
$factory = $this->createNoOpMock( MediaHandlerFactory::class, [ 'getHandler' ] ); |
| 228 |
$factory->method( 'getHandler' )->willReturn( false ); |
| 229 |
$this->setService( 'MediaHandlerFactory', $factory ); |
| 230 |
|
| 231 |
$env = $this->makeEnvironment( |
| 232 |
[ |
| 233 |
'f' => 'Test.png', |
| 234 |
'width' => self::$uniqueWidth++ |
| 235 |
] |
| 236 |
); |
| 237 |
$entryPoint = $this->getEntryPoint( $env ); |
| 238 |
|
| 239 |
$entryPoint->run(); |
| 240 |
$output = $entryPoint->getCapturedOutput(); |
| 241 |
|
| 242 |
$env->assertStatusCode( 400, $output ); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
public function testNoWidth() { |
| 247 |
$env = $this->makeEnvironment( |
| 248 |
[ |
| 249 |
'f' => 'Test.png', |
| 250 |
|
| 251 |
] |
| 252 |
); |
| 253 |
$entryPoint = $this->getEntryPoint( $env ); |
| 254 |
|
| 255 |
$entryPoint->run(); |
| 256 |
$output = $entryPoint->getCapturedOutput(); |
| 257 |
|
| 258 |
$env->assertStatusCode( 400, $output ); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
public function testNoWidthRedirect() { |
| 263 |
$env = $this->makeEnvironment( |
| 264 |
[ |
| 265 |
'f' => 'Redirect_to_Test.png', |
| 266 |
|
| 267 |
] |
| 268 |
); |
| 269 |
$entryPoint = $this->getEntryPoint( $env ); |
| 270 |
|
| 271 |
$entryPoint->run(); |
| 272 |
$output = $entryPoint->getCapturedOutput(); |
| 273 |
|
| 274 |
$env->assertStatusCode( 400, $output ); |
| 275 |
} |
| 276 |
|
| 277 |
public static function provideTransformError() { |
| 278 |
yield 'MediaTransformError' => [ |
| 279 |
new MediaTransformError( 'testing', 200, 100 ), |
| 280 |
500 |
| 281 |
]; |
| 282 |
|
| 283 |
yield 'thumbnail_image-failure-limit' => [ |
| 284 |
new MediaTransformError( 'thumbnail_image-failure-limit', 200, 100 ), |
| 285 |
429 |
| 286 |
]; |
| 287 |
|
| 288 |
yield 'no thumb' => [ |
| 289 |
false, |
| 290 |
500 |
| 291 |
]; |
| 292 |
|
| 293 |
yield 'no file path' => [ |
| 294 |
new ThumbnailImage( |
| 295 |
new UnregisteredLocalFile( false, false, 'dummy' ), |
| 296 |
'', |
| 297 |
false, |
| 298 |
[ 'width' => 1, 'height' => 1 ] |
| 299 |
), |
| 300 |
500 |
| 301 |
]; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
public function testTransformError( $transformOutput, $expectedCode ) { |
| 308 |
|
| 309 |
$handler = $this->getMockBuilder( BitmapHandler::class ) |
| 310 |
->onlyMethods( [ 'doTransform' ] ) |
| 311 |
->getMock(); |
| 312 |
|
| 313 |
$handler->method( 'doTransform' )->willReturn( $transformOutput ); |
| 314 |
|
| 315 |
$factory = $this->createNoOpMock( MediaHandlerFactory::class, [ 'getHandler' ] ); |
| 316 |
$factory->method( 'getHandler' )->willReturn( $handler ); |
| 317 |
$this->setService( 'MediaHandlerFactory', $factory ); |
| 318 |
|
| 319 |
$env = $this->makeEnvironment( |
| 320 |
[ |
| 321 |
'f' => 'Test.png', |
| 322 |
'width' => self::$uniqueWidth++ |
| 323 |
] |
| 324 |
); |
| 325 |
$entryPoint = $this->getEntryPoint( $env ); |
| 326 |
|
| 327 |
$entryPoint->run(); |
| 328 |
$output = $entryPoint->getCapturedOutput(); |
| 329 |
|
| 330 |
$env->assertStatusCode( $expectedCode, $output ); |
| 331 |
} |
| 332 |
|
| 333 |
public function testContentDisposition() { |
| 334 |
$env = $this->makeEnvironment( |
| 335 |
[ |
| 336 |
'f' => 'Test.png', |
| 337 |
'width' => 12, |
| 338 |
'download' => 1 |
| 339 |
] |
| 340 |
); |
| 341 |
|
| 342 |
$entryPoint = $this->getEntryPoint( $env ); |
| 343 |
$entryPoint->run(); |
| 344 |
$output = $entryPoint->getCapturedOutput(); |
| 345 |
|
| 346 |
$env->assertStatusCode( 200 ); |
| 347 |
$this->assertThumbnail( [ 'magic' => self::PNG_MAGIC, ], $output ); |
| 348 |
|
| 349 |
$env->assertHeaderValue( |
| 350 |
'attachment;filename*=UTF-8\'\'Test.png', |
| 351 |
'Content-Disposition' |
| 352 |
); |
| 353 |
} |
| 354 |
|
| 355 |
public static function provideThumbNameParam() { |
| 356 |
yield [ '12px-Test.png' ]; |
| 357 |
yield [ 'page123456-12px-xyz' ]; |
| 358 |
yield [ '12px-xyz' ]; |
| 359 |
yield [ 'xyzzy', 400 ]; |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
public function testThumbNameParam( $thumbName, $expected = 200 ) { |
| 366 |
$env = $this->makeEnvironment( |
| 367 |
[ |
| 368 |
'f' => 'Test.png', |
| 369 |
'thumbName' => $thumbName, |
| 370 |
] |
| 371 |
); |
| 372 |
|
| 373 |
$entryPoint = $this->getEntryPoint( $env ); |
| 374 |
|
| 375 |
$entryPoint->run(); |
| 376 |
$output = $entryPoint->getCapturedOutput(); |
| 377 |
|
| 378 |
$env->assertStatusCode( $expected, $output ); |
| 379 |
|
| 380 |
if ( $expected < 300 ) { |
| 381 |
$expectedProps = [ 'magic' => self::PNG_MAGIC ]; |
| 382 |
|
| 383 |
|
| 384 |
if ( preg_match( '/\b(\d+)px/', $thumbName, $matches ) ) { |
| 385 |
$expectedProps['width'] = (int)$matches[1]; |
| 386 |
} |
| 387 |
|
| 388 |
$this->assertThumbnail( |
| 389 |
$expectedProps, |
| 390 |
$output |
| 391 |
); |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
public function testAccessDenied() { |
| 396 |
|
| 397 |
$this->setGroupPermissions( '*', 'read', false ); |
| 398 |
|
| 399 |
|
| 400 |
$authority = new SimpleAuthority( |
| 401 |
new UserIdentityValue( 7, 'Heather' ), |
| 402 |
[] |
| 403 |
); |
| 404 |
|
| 405 |
$env = $this->makeEnvironment( 'Test.png' ); |
| 406 |
|
| 407 |
$context = $env->makeFauxContext(); |
| 408 |
$context->setAuthority( $authority ); |
| 409 |
|
| 410 |
$entryPoint = $this->getEntryPoint( |
| 411 |
$env, |
| 412 |
$context |
| 413 |
); |
| 414 |
|
| 415 |
$entryPoint->run(); |
| 416 |
|
| 417 |
$env->assertStatusCode( 403 ); |
| 418 |
$env->assertHeaderValue( |
| 419 |
'text/html; charset=utf-8', |
| 420 |
'Content-Type' |
| 421 |
); |
| 422 |
|
| 423 |
$output = $entryPoint->getCapturedOutput(); |
| 424 |
$this->assertStringContainsString( |
| 425 |
'<title>Error generating thumbnail</title>', |
| 426 |
$output |
| 427 |
); |
| 428 |
} |
| 429 |
|
| 430 |
public function testAccessOnPrivateWiki() { |
| 431 |
|
| 432 |
$this->setGroupPermissions( '*', 'read', false ); |
| 433 |
|
| 434 |
|
| 435 |
$authority = new SimpleAuthority( |
| 436 |
new UserIdentityValue( 7, 'Heather' ), |
| 437 |
[ 'read', 'renderfile', 'renderfile-nonstandard' ] |
| 438 |
); |
| 439 |
|
| 440 |
$env = $this->makeEnvironment( 'Test.png' ); |
| 441 |
|
| 442 |
$context = $env->makeFauxContext(); |
| 443 |
$context->setAuthority( $authority ); |
| 444 |
|
| 445 |
$entryPoint = $this->getEntryPoint( |
| 446 |
$env, |
| 447 |
$context |
| 448 |
); |
| 449 |
|
| 450 |
$entryPoint->run(); |
| 451 |
$output = $entryPoint->getCapturedOutput(); |
| 452 |
|
| 453 |
$env->assertStatusCode( 200 ); |
| 454 |
$this->assertThumbnail( [ 'magic' => self::PNG_MAGIC, ], $output ); |
| 455 |
} |
| 456 |
|
| 457 |
public static function provideRateLimit() { |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
yield [ '16', '24', 'renderfile' ]; |
| 463 |
yield [ self::$uniqueWidth++, self::$uniqueWidth++, 'renderfile-nonstandard' ]; |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
public function testRateLimited( $width1, $width2, $limit ) { |
| 470 |
|
| 471 |
$rateLimits = $this->getConfVar( MainConfigNames::RateLimits ); |
| 472 |
$rateLimits[$limit] = [ |
| 473 |
'ip' => [ 1, 60 ], |
| 474 |
'newbie' => [ 1, 60 ], |
| 475 |
'user' => [ 1, 60 ], |
| 476 |
]; |
| 477 |
|
| 478 |
$this->overrideConfigValue( MainConfigNames::RateLimits, $rateLimits ); |
| 479 |
|
| 480 |
|
| 481 |
$env = $this->makeEnvironment( [ 'f' => 'Test.png', 'width' => $width1 ] ); |
| 482 |
$entryPoint = $this->getEntryPoint( $env ); |
| 483 |
|
| 484 |
$entryPoint->run(); |
| 485 |
$entryPoint->getCapturedOutput(); |
| 486 |
$env->assertStatusCode( 200 ); |
| 487 |
|
| 488 |
|
| 489 |
$env = $this->makeEnvironment( [ 'f' => 'Test.png', 'width' => $width2 ] ); |
| 490 |
$entryPoint = $this->getEntryPoint( $env ); |
| 491 |
|
| 492 |
$entryPoint->run(); |
| 493 |
$entryPoint->getCapturedOutput(); |
| 494 |
$env->assertStatusCode( 429 ); |
| 495 |
|
| 496 |
$env->assertHeaderValue( |
| 497 |
'text/html; charset=utf-8', |
| 498 |
'Content-Type' |
| 499 |
); |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
public function testStreamOldFile( array $latestThumbnailInfo ) { |
| 506 |
$file = $this->getTestRepo()->newFile( 'Test.png' ); |
| 507 |
$history = $file->getHistory(); |
| 508 |
$oldFile = $history[0]; |
| 509 |
|
| 510 |
$env = $this->makeEnvironment( |
| 511 |
[ |
| 512 |
'f' => $oldFile->getArchiveName(), |
| 513 |
'width' => '12px', |
| 514 |
'archived' => 1, |
| 515 |
] |
| 516 |
); |
| 517 |
$entryPoint = $this->getEntryPoint( $env ); |
| 518 |
|
| 519 |
$entryPoint->run(); |
| 520 |
$output = $entryPoint->getCapturedOutput(); |
| 521 |
$env->assertStatusCode( 200 ); |
| 522 |
|
| 523 |
$this->assertNotSame( |
| 524 |
$latestThumbnailInfo['data'], |
| 525 |
$output, |
| 526 |
'Thumbnail for the old version should not be the same as the ' . |
| 527 |
'thumbnail for the latest version' |
| 528 |
); |
| 529 |
|
| 530 |
$this->assertThumbnail( |
| 531 |
[ 'magic' => self::PNG_MAGIC, 'width' => 12, ], |
| 532 |
$output |
| 533 |
); |
| 534 |
} |
| 535 |
|
| 536 |
public function testOldDeletedFile() { |
| 537 |
|
| 538 |
|
| 539 |
$file = $this->getTestRepo()->newFile( 'Icon.jpg' ); |
| 540 |
$history = $file->getHistory(); |
| 541 |
$oldFile = $history[0]; |
| 542 |
|
| 543 |
$env = $this->makeEnvironment( |
| 544 |
[ |
| 545 |
'f' => $oldFile->getArchiveName(), |
| 546 |
'width' => '12px', |
| 547 |
'archived' => 1, |
| 548 |
] |
| 549 |
); |
| 550 |
$entryPoint = $this->getEntryPoint( $env ); |
| 551 |
|
| 552 |
$entryPoint->run(); |
| 553 |
$output = $entryPoint->getCapturedOutput(); |
| 554 |
$env->assertStatusCode( 404, $output ); |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
public function testStreamOldFileRedirect( array $latestThumbnailInfo ) { |
| 561 |
$file = $this->getTestRepo()->newFile( 'Test.png' ); |
| 562 |
$history = $file->getHistory(); |
| 563 |
$oldFile = $history[0]; |
| 564 |
|
| 565 |
|
| 566 |
$archiveName = str_replace( |
| 567 |
'Test.png', |
| 568 |
'Redirect_to_Test.png', |
| 569 |
$oldFile->getArchiveName() |
| 570 |
); |
| 571 |
|
| 572 |
$env = $this->makeEnvironment( |
| 573 |
[ |
| 574 |
'f' => $archiveName, |
| 575 |
'width' => 12, |
| 576 |
'archived' => 1, |
| 577 |
] |
| 578 |
); |
| 579 |
$entryPoint = $this->getEntryPoint( $env ); |
| 580 |
|
| 581 |
$entryPoint->run(); |
| 582 |
$output = $entryPoint->getCapturedOutput(); |
| 583 |
|
| 584 |
$response = $env->getFauxResponse(); |
| 585 |
|
| 586 |
$this->assertSame( 302, $response->getStatusCode() ); |
| 587 |
|
| 588 |
$expected = '/' . urlencode( $oldFile->getArchiveName() ) . '/12px-Test.png'; |
| 589 |
$this->assertStringEndsWith( |
| 590 |
$expected, |
| 591 |
$response->getHeader( 'Location' ) |
| 592 |
); |
| 593 |
|
| 594 |
$this->assertSame( '', $output ); |
| 595 |
} |
| 596 |
|
| 597 |
public function testStreamTempFile() { |
| 598 |
$user = $this->getTestUser()->getUser(); |
| 599 |
$stash = new UploadStash( $this->getTestRepo(), $user ); |
| 600 |
$file = $stash->stashFile( self::IMAGES_DIR . '/adobergb.jpg' ); |
| 601 |
|
| 602 |
$env = $this->makeEnvironment( |
| 603 |
[ |
| 604 |
'f' => $file->getName(), |
| 605 |
'width' => 12, |
| 606 |
'temp' => 'yes', |
| 607 |
] |
| 608 |
); |
| 609 |
$entryPoint = $this->getEntryPoint( $env ); |
| 610 |
|
| 611 |
$entryPoint->run(); |
| 612 |
$output = $entryPoint->getCapturedOutput(); |
| 613 |
|
| 614 |
$env->assertStatusCode( 200 ); |
| 615 |
$this->assertThumbnail( |
| 616 |
[ 'magic' => self::JPEG_MAGIC, 'width' => 12, ], |
| 617 |
$output |
| 618 |
); |
| 619 |
} |
| 620 |
|
| 621 |
public function testRedirect() { |
| 622 |
$this->overrideConfigValue( MainConfigNames::VaryOnXFP, true ); |
| 623 |
|
| 624 |
$env = $this->makeEnvironment( |
| 625 |
[ |
| 626 |
'f' => 'Redirect_to_Test.png', |
| 627 |
'w' => 12 |
| 628 |
] |
| 629 |
); |
| 630 |
$entryPoint = $this->getEntryPoint( $env ); |
| 631 |
|
| 632 |
$entryPoint->run(); |
| 633 |
$output = $entryPoint->getCapturedOutput(); |
| 634 |
|
| 635 |
$response = $env->getFauxResponse(); |
| 636 |
|
| 637 |
$this->assertSame( 302, $response->getStatusCode() ); |
| 638 |
$this->assertStringEndsWith( |
| 639 |
'/Test.png/12px-Test.png', |
| 640 |
$response->getHeader( 'Location' ) |
| 641 |
); |
| 642 |
|
| 643 |
$this->assertSame( '', $output ); |
| 644 |
$env->assertHeaderValue( 'X-Forwarded-Proto', 'Vary' ); |
| 645 |
} |
| 646 |
|
| 647 |
public function testBadTitle() { |
| 648 |
$env = $this->makeEnvironment( '_/_' ); |
| 649 |
$entryPoint = $this->getEntryPoint( $env ); |
| 650 |
|
| 651 |
$entryPoint->run(); |
| 652 |
$output = $entryPoint->getCapturedOutput(); |
| 653 |
|
| 654 |
$env->assertStatusCode( 404 ); |
| 655 |
$env->assertHeaderValue( |
| 656 |
'text/html; charset=utf-8', |
| 657 |
'Content-Type' |
| 658 |
); |
| 659 |
|
| 660 |
$this->assertStringContainsString( |
| 661 |
'(badtitletext)', |
| 662 |
$output |
| 663 |
); |
| 664 |
} |
| 665 |
|
| 666 |
public static function provideOldFileWithBadTitle() { |
| 667 |
yield 'invalid title' => [ '_/_' ]; |
| 668 |
yield 'valid title without timestamp' => [ 'Test.png' ]; |
| 669 |
yield 'invalid title with timestamp' => [ '20200101002233!_/_' ]; |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
public function testOldFileWithBadTitle( $badTitle ) { |
| 676 |
$env = $this->makeEnvironment( [ |
| 677 |
'f' => $badTitle, |
| 678 |
'width' => 12, |
| 679 |
'archived' => 1 |
| 680 |
] ); |
| 681 |
$entryPoint = $this->getEntryPoint( $env ); |
| 682 |
|
| 683 |
$entryPoint->run(); |
| 684 |
$output = $entryPoint->getCapturedOutput(); |
| 685 |
|
| 686 |
$env->assertStatusCode( 404 ); |
| 687 |
$env->assertHeaderValue( |
| 688 |
'text/html; charset=utf-8', |
| 689 |
'Content-Type' |
| 690 |
); |
| 691 |
|
| 692 |
$this->assertStringContainsString( |
| 693 |
'(badtitletext)', |
| 694 |
$output |
| 695 |
); |
| 696 |
} |
| 697 |
|
| 698 |
public function testTooMuchWidth() { |
| 699 |
|
| 700 |
$env = $this->makeEnvironment( [ 'f' => 'Test.png', 'width' => 1200 ] ); |
| 701 |
|
| 702 |
$entryPoint = $this->getEntryPoint( $env ); |
| 703 |
|
| 704 |
$entryPoint->run(); |
| 705 |
$output = $entryPoint->getCapturedOutput(); |
| 706 |
|
| 707 |
$env->assertStatusCode( 400 ); |
| 708 |
$env->assertHeaderValue( |
| 709 |
'text/html; charset=utf-8', |
| 710 |
'Content-Type' |
| 711 |
); |
| 712 |
|
| 713 |
$this->assertStringContainsString( |
| 714 |
'(thumbnail_error: ', |
| 715 |
$output |
| 716 |
); |
| 717 |
$this->assertStringContainsString( |
| 718 |
'bigger than the source', |
| 719 |
$output |
| 720 |
); |
| 721 |
} |
| 722 |
|
| 723 |
public function testDeletedFile() { |
| 724 |
|
| 725 |
$icon = $this->getTestRepo()->newFile( 'Icon.jpg' ); |
| 726 |
|
| 727 |
$this->assertTrue( $icon->exists() ); |
| 728 |
$icon->deleteFile( 'testing', new UserIdentityValue( 0, 'Test' ) ); |
| 729 |
|
| 730 |
$env = $this->makeEnvironment( 'Icon.jpg' ); |
| 731 |
$entryPoint = $this->getEntryPoint( $env ); |
| 732 |
|
| 733 |
$entryPoint->run(); |
| 734 |
$output = $entryPoint->getCapturedOutput(); |
| 735 |
|
| 736 |
$env->assertStatusCode( 404 ); |
| 737 |
$env->assertHeaderValue( |
| 738 |
'text/html; charset=utf-8', |
| 739 |
'Content-Type' |
| 740 |
); |
| 741 |
|
| 742 |
$this->assertStringContainsString( |
| 743 |
'<title>Error generating thumbnail</title>', |
| 744 |
$output |
| 745 |
); |
| 746 |
} |
| 747 |
|
| 748 |
public function testNotModified() { |
| 749 |
$env = $this->makeEnvironment( |
| 750 |
[ |
| 751 |
'f' => 'Test.png', |
| 752 |
'width' => 12 |
| 753 |
] |
| 754 |
); |
| 755 |
|
| 756 |
$env->setServerInfo( 'HTTP_IF_MODIFIED_SINCE', '25250101001122' ); |
| 757 |
|
| 758 |
$entryPoint = $this->getEntryPoint( $env ); |
| 759 |
$entryPoint->run(); |
| 760 |
$output = $entryPoint->getCapturedOutput(); |
| 761 |
|
| 762 |
$response = $env->getFauxResponse(); |
| 763 |
$this->assertSame( 304, $response->getStatusCode() ); |
| 764 |
$this->assertSame( '', $output ); |
| 765 |
} |
| 766 |
|
| 767 |
public function testProxy() { |
| 768 |
$this->installTestRepoGroup( [ 'thumbProxyUrl' => 'https://images.acme.test/thumbnails/' ] ); |
| 769 |
$this->installMockHttp( 'PROXY RESPONSE' ); |
| 770 |
|
| 771 |
$env = $this->makeEnvironment( |
| 772 |
[ |
| 773 |
'f' => 'Test.png', |
| 774 |
'width' => self::$uniqueWidth++ |
| 775 |
] |
| 776 |
); |
| 777 |
$entryPoint = $this->getEntryPoint( $env ); |
| 778 |
|
| 779 |
$entryPoint->run(); |
| 780 |
$output = $entryPoint->getCapturedOutput(); |
| 781 |
|
| 782 |
$this->assertSame( 'PROXY RESPONSE', $output ); |
| 783 |
} |
| 784 |
|
| 785 |
public static function provideRepoCouldNotStreamFile() { |
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
yield 'non-existing thumbnail' => [ |
| 801 |
self::$uniqueWidth++, |
| 802 |
'No path supplied in thumbnail object', |
| 803 |
]; |
| 804 |
} |
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
public function testRepoCouldNotStreamFile( int $width, string $expectedError ) { |
| 811 |
$handler = $this->getMockBuilder( BitmapHandler::class ) |
| 812 |
->onlyMethods( [ 'doTransform' ] ) |
| 813 |
->getMock(); |
| 814 |
|
| 815 |
$file = $this->getTestRepo()->newFile( 'Test.png' ); |
| 816 |
$params = [ 'width' => $width, 'height' => $width ]; |
| 817 |
$handler->method( 'doTransform' )->willReturn( |
| 818 |
new ThumbnailImage( $file, '', false, $params ) |
| 819 |
); |
| 820 |
|
| 821 |
$factory = $this->createNoOpMock( MediaHandlerFactory::class, [ 'getHandler' ] ); |
| 822 |
$factory->method( 'getHandler' )->willReturn( $handler ); |
| 823 |
$this->setService( 'MediaHandlerFactory', $factory ); |
| 824 |
|
| 825 |
$env = $this->makeEnvironment( |
| 826 |
[ |
| 827 |
'f' => 'Test.png', |
| 828 |
'width' => $width |
| 829 |
] |
| 830 |
); |
| 831 |
$entryPoint = $this->getEntryPoint( $env ); |
| 832 |
|
| 833 |
$entryPoint->run(); |
| 834 |
$output = $entryPoint->getCapturedOutput(); |
| 835 |
|
| 836 |
$env->assertStatusCode( 500, $output ); |
| 837 |
$env->assertHeaderValue( |
| 838 |
'text/html; charset=utf-8', |
| 839 |
'Content-Type' |
| 840 |
); |
| 841 |
|
| 842 |
$this->assertStringContainsString( $expectedError, $output ); |
| 843 |
} |
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
private function assertThumbnail( array $props, string $output ): void { |
| 850 |
if ( isset( $props['magic'] ) ) { |
| 851 |
$this->assertStringStartsWith( |
| 852 |
$props['magic'], |
| 853 |
$output, |
| 854 |
'Magic number should match' |
| 855 |
); |
| 856 |
} |
| 857 |
|
| 858 |
if ( isset( $props['width'] ) && function_exists( 'getimagesizefromstring' ) ) { |
| 859 |
[ $width, ] = getimagesizefromstring( $output ); |
| 860 |
$this->assertSame( |
| 861 |
$props['width'], |
| 862 |
$width |
| 863 |
); |
| 864 |
} |
| 865 |
} |
| 866 |
|
| 867 |
} |