| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\MainConfigNames; |
| 4 |
use MediaWiki\Page\PageIdentityValue; |
| 5 |
use MediaWiki\Title\TitleValue; |
| 6 |
use MediaWiki\WikiMap\WikiMap; |
| 7 |
use Wikimedia\FileBackend\FSFile\FSFile; |
| 8 |
use Wikimedia\FileBackend\FSFile\TempFSFile; |
| 9 |
use Wikimedia\FileBackend\FSFileBackend; |
| 10 |
use Wikimedia\TestingAccessWrapper; |
| 11 |
|
| 12 |
class FileTest extends MediaWikiMediaTestCase { |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
public function testCanAnimateThumbIfAppropriate( $filename, $expected ) { |
| 21 |
$this->overrideConfigValue( MainConfigNames::MaxAnimatedGifArea, 9000 ); |
| 22 |
$file = $this->dataFile( $filename ); |
| 23 |
$this->assertEquals( $expected, $file->canAnimateThumbIfAppropriate() ); |
| 24 |
} |
| 25 |
|
| 26 |
public static function providerCanAnimate() { |
| 27 |
return [ |
| 28 |
[ 'nonanimated.gif', true ], |
| 29 |
[ 'jpeg-comment-utf.jpg', true ], |
| 30 |
[ 'test.tiff', true ], |
| 31 |
[ 'Animated_PNG_example_bouncing_beach_ball.png', false ], |
| 32 |
[ 'greyscale-png.png', true ], |
| 33 |
[ 'Toll_Texas_1.svg', true ], |
| 34 |
[ 'LoremIpsum.djvu', true ], |
| 35 |
[ '80x60-2layers.xcf', true ], |
| 36 |
[ 'Soccer_ball_animated.svg', false ], |
| 37 |
[ 'Bishzilla_blink.gif', false ], |
| 38 |
[ 'animated.gif', true ], |
| 39 |
]; |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public function testGetThumbnailBucket( $data ) { |
| 47 |
$this->overrideConfigValues( [ |
| 48 |
MainConfigNames::ThumbnailBuckets => $data['buckets'], |
| 49 |
MainConfigNames::ThumbnailMinimumBucketDistance => $data['minimumBucketDistance'], |
| 50 |
] ); |
| 51 |
|
| 52 |
$fileMock = $this->getMockBuilder( File::class ) |
| 53 |
->setConstructorArgs( [ 'fileMock', false ] ) |
| 54 |
->onlyMethods( [ 'getWidth' ] ) |
| 55 |
->getMockForAbstractClass(); |
| 56 |
|
| 57 |
$fileMock->method( 'getWidth' ) |
| 58 |
->willReturn( $data['width'] ); |
| 59 |
|
| 60 |
$this->assertEquals( |
| 61 |
$data['expectedBucket'], |
| 62 |
$fileMock->getThumbnailBucket( $data['requestedWidth'] ), |
| 63 |
$data['message'] ); |
| 64 |
} |
| 65 |
|
| 66 |
public static function getThumbnailBucketProvider() { |
| 67 |
$defaultBuckets = [ 256, 512, 1024, 2048, 4096 ]; |
| 68 |
|
| 69 |
return [ |
| 70 |
[ [ |
| 71 |
'buckets' => $defaultBuckets, |
| 72 |
'minimumBucketDistance' => 0, |
| 73 |
'width' => 3000, |
| 74 |
'requestedWidth' => 120, |
| 75 |
'expectedBucket' => 256, |
| 76 |
'message' => 'Picking bucket bigger than requested size' |
| 77 |
] ], |
| 78 |
[ [ |
| 79 |
'buckets' => $defaultBuckets, |
| 80 |
'minimumBucketDistance' => 0, |
| 81 |
'width' => 3000, |
| 82 |
'requestedWidth' => 300, |
| 83 |
'expectedBucket' => 512, |
| 84 |
'message' => 'Picking bucket bigger than requested size' |
| 85 |
] ], |
| 86 |
[ [ |
| 87 |
'buckets' => $defaultBuckets, |
| 88 |
'minimumBucketDistance' => 0, |
| 89 |
'width' => 3000, |
| 90 |
'requestedWidth' => 1024, |
| 91 |
'expectedBucket' => 2048, |
| 92 |
'message' => 'Picking bucket bigger than requested size' |
| 93 |
] ], |
| 94 |
[ [ |
| 95 |
'buckets' => $defaultBuckets, |
| 96 |
'minimumBucketDistance' => 0, |
| 97 |
'width' => 3000, |
| 98 |
'requestedWidth' => 2048, |
| 99 |
'expectedBucket' => false, |
| 100 |
'message' => 'Picking no bucket because none is bigger than the requested size' |
| 101 |
] ], |
| 102 |
[ [ |
| 103 |
'buckets' => $defaultBuckets, |
| 104 |
'minimumBucketDistance' => 0, |
| 105 |
'width' => 3000, |
| 106 |
'requestedWidth' => 3500, |
| 107 |
'expectedBucket' => false, |
| 108 |
'message' => 'Picking no bucket because requested size is bigger than original' |
| 109 |
] ], |
| 110 |
[ [ |
| 111 |
'buckets' => [ 1024 ], |
| 112 |
'minimumBucketDistance' => 0, |
| 113 |
'width' => 3000, |
| 114 |
'requestedWidth' => 1024, |
| 115 |
'expectedBucket' => false, |
| 116 |
'message' => 'Picking no bucket because requested size equals biggest bucket' |
| 117 |
] ], |
| 118 |
[ [ |
| 119 |
'buckets' => null, |
| 120 |
'minimumBucketDistance' => 0, |
| 121 |
'width' => 3000, |
| 122 |
'requestedWidth' => 1024, |
| 123 |
'expectedBucket' => false, |
| 124 |
'message' => 'Picking no bucket because no buckets have been specified' |
| 125 |
] ], |
| 126 |
[ [ |
| 127 |
'buckets' => [ 256, 512 ], |
| 128 |
'minimumBucketDistance' => 10, |
| 129 |
'width' => 3000, |
| 130 |
'requestedWidth' => 245, |
| 131 |
'expectedBucket' => 256, |
| 132 |
'message' => 'Requested width is distant enough from next bucket for it to be picked' |
| 133 |
] ], |
| 134 |
[ [ |
| 135 |
'buckets' => [ 256, 512 ], |
| 136 |
'minimumBucketDistance' => 10, |
| 137 |
'width' => 3000, |
| 138 |
'requestedWidth' => 246, |
| 139 |
'expectedBucket' => 512, |
| 140 |
'message' => 'Requested width is too close to next bucket, picking next one' |
| 141 |
] ], |
| 142 |
]; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public function testGetThumbnailSource( $data ) { |
| 150 |
$backendMock = $this->getMockBuilder( FSFileBackend::class ) |
| 151 |
->setConstructorArgs( [ [ 'name' => 'backendMock', 'wikiId' => WikiMap::getCurrentWikiId() ] ] ) |
| 152 |
->getMock(); |
| 153 |
|
| 154 |
$repoMock = $this->getMockBuilder( FileRepo::class ) |
| 155 |
->setConstructorArgs( [ [ 'name' => 'repoMock', 'backend' => $backendMock ] ] ) |
| 156 |
->onlyMethods( [ 'fileExists', 'getLocalReference' ] ) |
| 157 |
->getMock(); |
| 158 |
|
| 159 |
$tempDir = wfTempDir(); |
| 160 |
$fsFile = new FSFile( 'fsFilePath' ); |
| 161 |
|
| 162 |
$repoMock->method( 'fileExists' ) |
| 163 |
->willReturn( true ); |
| 164 |
|
| 165 |
$repoMock->method( 'getLocalReference' ) |
| 166 |
->willReturn( $fsFile ); |
| 167 |
|
| 168 |
$handlerMock = $this->getMockBuilder( BitmapHandler::class ) |
| 169 |
->onlyMethods( [ 'supportsBucketing' ] )->getMock(); |
| 170 |
$handlerMock->method( 'supportsBucketing' ) |
| 171 |
->willReturn( $data['supportsBucketing'] ); |
| 172 |
|
| 173 |
$fileMock = $this->getMockBuilder( File::class ) |
| 174 |
->setConstructorArgs( [ 'fileMock', $repoMock ] ) |
| 175 |
->onlyMethods( [ 'getThumbnailBucket', 'getLocalRefPath', 'getHandler' ] ) |
| 176 |
->getMockForAbstractClass(); |
| 177 |
|
| 178 |
$fileMock->method( 'getThumbnailBucket' ) |
| 179 |
->willReturn( $data['thumbnailBucket'] ); |
| 180 |
|
| 181 |
$fileMock->method( 'getLocalRefPath' ) |
| 182 |
->willReturn( 'localRefPath' ); |
| 183 |
|
| 184 |
$fileMock->method( 'getHandler' ) |
| 185 |
->willReturn( $handlerMock ); |
| 186 |
|
| 187 |
$reflection = new ReflectionClass( $fileMock ); |
| 188 |
$reflection_property = $reflection->getProperty( 'handler' ); |
| 189 |
$reflection_property->setValue( $fileMock, $handlerMock ); |
| 190 |
|
| 191 |
if ( $data['tmpBucketedThumbCache'] !== null ) { |
| 192 |
foreach ( $data['tmpBucketedThumbCache'] as &$tmpBucketed ) { |
| 193 |
$tmpBucketed = str_replace( '/tmp', $tempDir, $tmpBucketed ); |
| 194 |
} |
| 195 |
$reflection_property = $reflection->getProperty( 'tmpBucketedThumbCache' ); |
| 196 |
$reflection_property->setValue( $fileMock, $data['tmpBucketedThumbCache'] ); |
| 197 |
} |
| 198 |
|
| 199 |
$result = $fileMock->getThumbnailSource( |
| 200 |
[ 'physicalWidth' => $data['physicalWidth'] ] ); |
| 201 |
|
| 202 |
$this->assertEquals( |
| 203 |
str_replace( '/tmp', $tempDir, $data['expectedPath'] ), |
| 204 |
$result['path'], |
| 205 |
$data['message'] |
| 206 |
); |
| 207 |
} |
| 208 |
|
| 209 |
public static function getThumbnailSourceProvider() { |
| 210 |
return [ |
| 211 |
[ [ |
| 212 |
'supportsBucketing' => true, |
| 213 |
'tmpBucketedThumbCache' => null, |
| 214 |
'thumbnailBucket' => 1024, |
| 215 |
'physicalWidth' => 2048, |
| 216 |
'expectedPath' => 'fsFilePath', |
| 217 |
'message' => 'Path downloaded from storage' |
| 218 |
] ], |
| 219 |
[ [ |
| 220 |
'supportsBucketing' => true, |
| 221 |
'tmpBucketedThumbCache' => [ 1024 => '/tmp/shouldnotexist' . rand() ], |
| 222 |
'thumbnailBucket' => 1024, |
| 223 |
'physicalWidth' => 2048, |
| 224 |
'expectedPath' => 'fsFilePath', |
| 225 |
'message' => 'Path downloaded from storage because temp file is missing' |
| 226 |
] ], |
| 227 |
[ [ |
| 228 |
'supportsBucketing' => true, |
| 229 |
'tmpBucketedThumbCache' => [ 1024 => '/tmp' ], |
| 230 |
'thumbnailBucket' => 1024, |
| 231 |
'physicalWidth' => 2048, |
| 232 |
'expectedPath' => '/tmp', |
| 233 |
'message' => 'Temporary path because temp file was found' |
| 234 |
] ], |
| 235 |
[ [ |
| 236 |
'supportsBucketing' => false, |
| 237 |
'tmpBucketedThumbCache' => null, |
| 238 |
'thumbnailBucket' => 1024, |
| 239 |
'physicalWidth' => 2048, |
| 240 |
'expectedPath' => 'localRefPath', |
| 241 |
'message' => 'Original file path because bucketing is unsupported by handler' |
| 242 |
] ], |
| 243 |
[ [ |
| 244 |
'supportsBucketing' => true, |
| 245 |
'tmpBucketedThumbCache' => null, |
| 246 |
'thumbnailBucket' => false, |
| 247 |
'physicalWidth' => 2048, |
| 248 |
'expectedPath' => 'localRefPath', |
| 249 |
'message' => 'Original file path because no width provided' |
| 250 |
] ], |
| 251 |
]; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
public function testGenerateBucketsIfNeeded( $data ) { |
| 259 |
$this->overrideConfigValue( MainConfigNames::ThumbnailBuckets, $data['buckets'] ); |
| 260 |
|
| 261 |
$backendMock = $this->getMockBuilder( FSFileBackend::class ) |
| 262 |
->setConstructorArgs( [ [ 'name' => 'backendMock', 'wikiId' => WikiMap::getCurrentWikiId() ] ] ) |
| 263 |
->getMock(); |
| 264 |
|
| 265 |
$repoMock = $this->getMockBuilder( FileRepo::class ) |
| 266 |
->setConstructorArgs( [ [ 'name' => 'repoMock', 'backend' => $backendMock ] ] ) |
| 267 |
->onlyMethods( [ 'fileExists', 'getLocalReference' ] ) |
| 268 |
->getMock(); |
| 269 |
|
| 270 |
$fileMock = $this->getMockBuilder( File::class ) |
| 271 |
->setConstructorArgs( [ 'fileMock', $repoMock ] ) |
| 272 |
->onlyMethods( [ 'getWidth', 'getBucketThumbPath', 'makeTransformTmpFile', |
| 273 |
'generateAndSaveThumb', 'getHandler' ] ) |
| 274 |
->getMockForAbstractClass(); |
| 275 |
|
| 276 |
$handlerMock = $this->getMockBuilder( JpegHandler::class ) |
| 277 |
->onlyMethods( [ 'supportsBucketing' ] )->getMock(); |
| 278 |
$handlerMock->method( 'supportsBucketing' ) |
| 279 |
->willReturn( true ); |
| 280 |
|
| 281 |
$fileMock->method( 'getHandler' ) |
| 282 |
->willReturn( $handlerMock ); |
| 283 |
|
| 284 |
$reflectionMethod = new ReflectionMethod( File::class, 'generateBucketsIfNeeded' ); |
| 285 |
|
| 286 |
$fileMock->method( 'getWidth' ) |
| 287 |
->willReturn( $data['width'] ); |
| 288 |
|
| 289 |
$fileMock->expects( $data['expectedGetBucketThumbPathCalls'] ) |
| 290 |
->method( 'getBucketThumbPath' ); |
| 291 |
|
| 292 |
$repoMock->expects( $data['expectedFileExistsCalls'] ) |
| 293 |
->method( 'fileExists' ) |
| 294 |
->willReturn( $data['fileExistsReturn'] ); |
| 295 |
|
| 296 |
$fileMock->expects( $data['expectedMakeTransformTmpFile'] ) |
| 297 |
->method( 'makeTransformTmpFile' ) |
| 298 |
->willReturn( $data['makeTransformTmpFileReturn'] ); |
| 299 |
|
| 300 |
$fileMock->expects( $data['expectedGenerateAndSaveThumb'] ) |
| 301 |
->method( 'generateAndSaveThumb' ) |
| 302 |
->willReturn( $data['generateAndSaveThumbReturn'] ); |
| 303 |
|
| 304 |
$this->assertEquals( $data['expectedResult'], |
| 305 |
$reflectionMethod->invoke( |
| 306 |
$fileMock, |
| 307 |
[ |
| 308 |
'physicalWidth' => $data['physicalWidth'], |
| 309 |
'physicalHeight' => $data['physicalHeight'] ] |
| 310 |
), |
| 311 |
$data['message'] ); |
| 312 |
} |
| 313 |
|
| 314 |
public function generateBucketsIfNeededProvider() { |
| 315 |
$defaultBuckets = [ 256, 512, 1024, 2048, 4096 ]; |
| 316 |
|
| 317 |
return [ |
| 318 |
[ [ |
| 319 |
'buckets' => $defaultBuckets, |
| 320 |
'width' => 256, |
| 321 |
'physicalWidth' => 256, |
| 322 |
'physicalHeight' => 100, |
| 323 |
'expectedGetBucketThumbPathCalls' => $this->never(), |
| 324 |
'expectedFileExistsCalls' => $this->never(), |
| 325 |
'fileExistsReturn' => null, |
| 326 |
'expectedMakeTransformTmpFile' => $this->never(), |
| 327 |
'makeTransformTmpFileReturn' => false, |
| 328 |
'expectedGenerateAndSaveThumb' => $this->never(), |
| 329 |
'generateAndSaveThumbReturn' => false, |
| 330 |
'expectedResult' => false, |
| 331 |
'message' => 'No bucket found, nothing to generate' |
| 332 |
] ], |
| 333 |
[ [ |
| 334 |
'buckets' => $defaultBuckets, |
| 335 |
'width' => 5000, |
| 336 |
'physicalWidth' => 300, |
| 337 |
'physicalHeight' => 200, |
| 338 |
'expectedGetBucketThumbPathCalls' => $this->once(), |
| 339 |
'expectedFileExistsCalls' => $this->once(), |
| 340 |
'fileExistsReturn' => true, |
| 341 |
'expectedMakeTransformTmpFile' => $this->never(), |
| 342 |
'makeTransformTmpFileReturn' => false, |
| 343 |
'expectedGenerateAndSaveThumb' => $this->never(), |
| 344 |
'generateAndSaveThumbReturn' => false, |
| 345 |
'expectedResult' => false, |
| 346 |
'message' => 'File already exists, no reason to generate buckets' |
| 347 |
] ], |
| 348 |
[ [ |
| 349 |
'buckets' => $defaultBuckets, |
| 350 |
'width' => 5000, |
| 351 |
'physicalWidth' => 300, |
| 352 |
'physicalHeight' => 200, |
| 353 |
'expectedGetBucketThumbPathCalls' => $this->once(), |
| 354 |
'expectedFileExistsCalls' => $this->once(), |
| 355 |
'fileExistsReturn' => false, |
| 356 |
'expectedMakeTransformTmpFile' => $this->once(), |
| 357 |
'makeTransformTmpFileReturn' => false, |
| 358 |
'expectedGenerateAndSaveThumb' => $this->never(), |
| 359 |
'generateAndSaveThumbReturn' => false, |
| 360 |
'expectedResult' => false, |
| 361 |
'message' => 'Cannot generate temp file for bucket' |
| 362 |
] ], |
| 363 |
[ [ |
| 364 |
'buckets' => $defaultBuckets, |
| 365 |
'width' => 5000, |
| 366 |
'physicalWidth' => 300, |
| 367 |
'physicalHeight' => 200, |
| 368 |
'expectedGetBucketThumbPathCalls' => $this->once(), |
| 369 |
'expectedFileExistsCalls' => $this->once(), |
| 370 |
'fileExistsReturn' => false, |
| 371 |
'expectedMakeTransformTmpFile' => $this->once(), |
| 372 |
'makeTransformTmpFileReturn' => new TempFSFile( '/tmp/foo' ), |
| 373 |
'expectedGenerateAndSaveThumb' => $this->once(), |
| 374 |
'generateAndSaveThumbReturn' => false, |
| 375 |
'expectedResult' => false, |
| 376 |
'message' => 'Bucket image could not be generated' |
| 377 |
] ], |
| 378 |
[ [ |
| 379 |
'buckets' => $defaultBuckets, |
| 380 |
'width' => 5000, |
| 381 |
'physicalWidth' => 300, |
| 382 |
'physicalHeight' => 200, |
| 383 |
'expectedGetBucketThumbPathCalls' => $this->once(), |
| 384 |
'expectedFileExistsCalls' => $this->once(), |
| 385 |
'fileExistsReturn' => false, |
| 386 |
'expectedMakeTransformTmpFile' => $this->once(), |
| 387 |
'makeTransformTmpFileReturn' => new TempFSFile( '/tmp/foo' ), |
| 388 |
'expectedGenerateAndSaveThumb' => $this->once(), |
| 389 |
'generateAndSaveThumbReturn' => new ThumbnailImage( false, 'bar', false, false ), |
| 390 |
'expectedResult' => true, |
| 391 |
'message' => 'Bucket image could not be generated' |
| 392 |
] ], |
| 393 |
]; |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
public function testGetDisplayWidthHeight( $dim, $expected ) { |
| 403 |
$fileMock = $this->getMockBuilder( File::class ) |
| 404 |
->setConstructorArgs( [ 'fileMock', false ] ) |
| 405 |
->onlyMethods( [ 'getWidth', 'getHeight' ] ) |
| 406 |
->getMockForAbstractClass(); |
| 407 |
|
| 408 |
$fileMock->method( 'getWidth' )->willReturn( $dim[2] ); |
| 409 |
$fileMock->method( 'getHeight' )->willReturn( $dim[3] ); |
| 410 |
|
| 411 |
$actual = $fileMock->getDisplayWidthHeight( $dim[0], $dim[1] ); |
| 412 |
$this->assertEquals( $expected, $actual ); |
| 413 |
} |
| 414 |
|
| 415 |
public static function providerGetDisplayWidthHeight() { |
| 416 |
return [ |
| 417 |
[ |
| 418 |
[ 1024.0, 768.0, 600.0, 600.0 ], |
| 419 |
[ 600.0, 600.0 ] |
| 420 |
], |
| 421 |
[ |
| 422 |
[ 1024.0, 768.0, 1600.0, 600.0 ], |
| 423 |
[ 1024.0, 384.0 ] |
| 424 |
], |
| 425 |
[ |
| 426 |
[ 1024.0, 768.0, 1024.0, 768.0 ], |
| 427 |
[ 1024.0, 768.0 ] |
| 428 |
], |
| 429 |
[ |
| 430 |
[ 1024.0, 768.0, 800.0, 1000.0 ], |
| 431 |
[ 614.0, 768.0 ] |
| 432 |
], |
| 433 |
[ |
| 434 |
[ 1024.0, 768.0, 0, 1000 ], |
| 435 |
[ 0, 0 ] |
| 436 |
], |
| 437 |
[ |
| 438 |
[ 1024.0, 768.0, 2000, 0 ], |
| 439 |
[ 0, 0 ] |
| 440 |
], |
| 441 |
]; |
| 442 |
} |
| 443 |
|
| 444 |
public static function provideNormalizeTitle() { |
| 445 |
yield [ 'some name.jpg', 'Some_name.jpg' ]; |
| 446 |
yield [ new TitleValue( NS_FILE, 'Some_name.jpg' ), 'Some_name.jpg' ]; |
| 447 |
yield [ new TitleValue( NS_MEDIA, 'Some_name.jpg' ), 'Some_name.jpg' ]; |
| 448 |
yield [ new PageIdentityValue( 0, NS_FILE, 'Some_name.jpg', false ), 'Some_name.jpg' ]; |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
public function testNormalizeTitle( $title, $expected ) { |
| 456 |
$actual = File::normalizeTitle( $title ); |
| 457 |
|
| 458 |
$this->assertSame( NS_FILE, $actual->getNamespace() ); |
| 459 |
$this->assertSame( $expected, $actual->getDBkey() ); |
| 460 |
} |
| 461 |
|
| 462 |
public static function provideNormalizeTitleFails() { |
| 463 |
yield [ '' ]; |
| 464 |
yield [ '#' ]; |
| 465 |
yield [ new TitleValue( NS_USER, 'Some_name.jpg' ) ]; |
| 466 |
yield [ new PageIdentityValue( 0, NS_USER, 'Some_name.jpg', false ) ]; |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
public function testNormalizeTitleFails( $title ) { |
| 474 |
$actual = File::normalizeTitle( $title ); |
| 475 |
$this->assertNull( $actual ); |
| 476 |
|
| 477 |
$this->expectException( RuntimeException::class ); |
| 478 |
File::normalizeTitle( $title, 'exception' ); |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
public function testSetHandlerState() { |
| 486 |
$obj = (object)[]; |
| 487 |
$file = new class extends File { |
| 488 |
public function __construct() { |
| 489 |
} |
| 490 |
}; |
| 491 |
$this->assertNull( $file->getHandlerState( 'test' ) ); |
| 492 |
$file->setHandlerState( 'test', $obj ); |
| 493 |
$this->assertSame( $obj, $file->getHandlerState( 'test' ) ); |
| 494 |
} |
| 495 |
|
| 496 |
public static function provideThumbNameSteps() { |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
$jpeg = [ |
| 501 |
'filename' => 'test.jpg', |
| 502 |
'type' => 'image/jpeg', |
| 503 |
]; |
| 504 |
|
| 505 |
$tiff = [ |
| 506 |
'filename' => 'doc.tiff', |
| 507 |
'type' => 'image/tiff', |
| 508 |
]; |
| 509 |
|
| 510 |
$svg = [ |
| 511 |
'filename' => 'logo.svg', |
| 512 |
'type' => 'image/svg+xml', |
| 513 |
]; |
| 514 |
|
| 515 |
$disabled = [ |
| 516 |
'enabled' => false, |
| 517 |
'originalWidth' => 500, |
| 518 |
'thumbWidth' => 52, |
| 519 |
'expected' => 52 |
| 520 |
]; |
| 521 |
yield 'unchanged when disabled jpeg' => $jpeg + $disabled; |
| 522 |
yield 'unchanged when disabled tiff' => $tiff + $disabled; |
| 523 |
yield 'unchanged when disabled svg' => $svg + $disabled; |
| 524 |
|
| 525 |
$roundUp = [ |
| 526 |
'enabled' => true, |
| 527 |
'originalWidth' => 500, |
| 528 |
'thumbWidth' => 52, |
| 529 |
'expected' => 100 |
| 530 |
]; |
| 531 |
yield 'round up to next step for jpeg' => $jpeg + $roundUp; |
| 532 |
yield 'round up to next step for tiff' => $tiff + $roundUp; |
| 533 |
yield 'round up to next step for svg' => $svg + $roundUp; |
| 534 |
|
| 535 |
yield 'thumb under first step and original for jpeg serves original' => $jpeg + [ |
| 536 |
'enabled' => true, |
| 537 |
'originalWidth' => 90, |
| 538 |
'thumbWidth' => 52, |
| 539 |
'expected' => 90 |
| 540 |
]; |
| 541 |
yield 'thumb under first step and original for tiff transforms original' => $tiff + [ |
| 542 |
'enabled' => true, |
| 543 |
'originalWidth' => 90, |
| 544 |
'thumbWidth' => 52, |
| 545 |
'expected' => 90 |
| 546 |
]; |
| 547 |
yield 'thumb under first step and original for svg scales up' => $svg + [ |
| 548 |
'enabled' => true, |
| 549 |
'originalWidth' => 90, |
| 550 |
'thumbWidth' => 52, |
| 551 |
'expected' => 100 |
| 552 |
]; |
| 553 |
|
| 554 |
yield 'thumb between penultimate step and original for jpeg serves original' => $jpeg + [ |
| 555 |
'enabled' => true, |
| 556 |
'originalWidth' => 180, |
| 557 |
'thumbWidth' => 130, |
| 558 |
'expected' => 180 |
| 559 |
]; |
| 560 |
|
| 561 |
yield 'thumb between penultimate step and original for tiff rounds down' => $tiff + [ |
| 562 |
'enabled' => true, |
| 563 |
'originalWidth' => 180, |
| 564 |
'thumbWidth' => 130, |
| 565 |
'expected' => 100 |
| 566 |
]; |
| 567 |
yield 'thumb between penultimate step and original for svg scales up' => $svg + [ |
| 568 |
'enabled' => true, |
| 569 |
'originalWidth' => 180, |
| 570 |
'thumbWidth' => 130, |
| 571 |
'expected' => 200 |
| 572 |
]; |
| 573 |
|
| 574 |
yield 'thumb beyond original jpg but under penultimate step falls back to original' => $jpeg + [ |
| 575 |
'enabled' => true, |
| 576 |
'originalWidth' => 140, |
| 577 |
'thumbWidth' => 190, |
| 578 |
'expected' => 140 |
| 579 |
]; |
| 580 |
|
| 581 |
yield 'thumb beyond original tiff but under penultimate step rounds down' => $tiff + [ |
| 582 |
'enabled' => true, |
| 583 |
'originalWidth' => 140, |
| 584 |
'thumbWidth' => 190, |
| 585 |
'expected' => 100 |
| 586 |
]; |
| 587 |
yield 'thumb beyond original svg but under penultimate step scales up' => $svg + [ |
| 588 |
'enabled' => true, |
| 589 |
'originalWidth' => 140, |
| 590 |
'thumbWidth' => 190, |
| 591 |
'expected' => 200 |
| 592 |
]; |
| 593 |
|
| 594 |
$beyondSteps = [ |
| 595 |
'enabled' => true, |
| 596 |
'originalWidth' => 2345, |
| 597 |
'thumbWidth' => 1252, |
| 598 |
'expected' => 1000 |
| 599 |
]; |
| 600 |
yield 'thumb beyond last step for jpeg falls back to the largest standard' => $jpeg + $beyondSteps; |
| 601 |
yield 'thumb beyond last step for tiff falls back to the largest standard' => $tiff + $beyondSteps; |
| 602 |
yield 'thumb beyond last step for svg falls back to the largest standard' => $svg + $beyondSteps; |
| 603 |
} |
| 604 |
|
| 605 |
private function assertThumbNameEquals( |
| 606 |
string $filename, |
| 607 |
string $type, |
| 608 |
int $originalWidth, |
| 609 |
array $params, |
| 610 |
int $expected, |
| 611 |
bool $nativeSvg = false |
| 612 |
) { |
| 613 |
$file = $this->dataFile( $filename, $type ); |
| 614 |
$fileObj = TestingAccessWrapper::newFromObject( $file ); |
| 615 |
$fileObj->sizeAndMetadata = [ |
| 616 |
'width' => $originalWidth, |
| 617 |
'height' => $originalWidth, |
| 618 |
'metadata' => [] |
| 619 |
]; |
| 620 |
|
| 621 |
$file->getHandler()->normaliseParams( $file, $params ); |
| 622 |
$thumbName = $file->thumbName( $params ); |
| 623 |
$thumbUrl = $file->getThumbUrl( $thumbName ); |
| 624 |
$thumbPath = $file->getThumbPath( $thumbName ); |
| 625 |
$thumb = $file->getHandler()->getTransform( $file, $thumbPath, $thumbUrl, $params ); |
| 626 |
$actual = array_last( explode( '/', $thumb->getUrl() ) ); |
| 627 |
|
| 628 |
if ( ( $expected >= $originalWidth && $type === 'image/jpeg' ) || $nativeSvg ) { |
| 629 |
$this->assertEquals( $filename, $actual ); |
| 630 |
} else { |
| 631 |
$expectedThumb = $expected . 'px-' . $filename; |
| 632 |
if ( $type === 'image/svg+xml' || $type === 'image/tiff' ) { |
| 633 |
$expectedThumb .= '.png'; |
| 634 |
} |
| 635 |
$this->assertEquals( $expectedThumb, $actual ); |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
public function testThumbNameSteps( |
| 645 |
string $filename, |
| 646 |
string $type, |
| 647 |
bool $enabled, |
| 648 |
int $originalWidth, |
| 649 |
int $thumbWidth, |
| 650 |
int $expected |
| 651 |
) { |
| 652 |
$this->overrideConfigValues( [ |
| 653 |
MainConfigNames::ThumbnailSteps => [ 100, 200, 1000 ], |
| 654 |
MainConfigNames::ThumbnailStepsRatio => $enabled ? 1.0 : 0.0, |
| 655 |
MainConfigNames::SVGNativeRendering => false, |
| 656 |
MainConfigNames::TiffThumbnailType => [ 'png', 'image/png' ], |
| 657 |
] ); |
| 658 |
|
| 659 |
$params = [ |
| 660 |
'width' => $thumbWidth, |
| 661 |
'height' => $thumbWidth, |
| 662 |
'physicalWidth' => $thumbWidth, |
| 663 |
'physicalHeight' => $thumbWidth |
| 664 |
]; |
| 665 |
$this->assertThumbNameEquals( $filename, $type, $originalWidth, $params, $expected ); |
| 666 |
if ( $type === 'image/svg+xml' ) { |
| 667 |
$this->overrideConfigValue( MainConfigNames::SVGNativeRendering, true ); |
| 668 |
$this->assertThumbNameEquals( $filename, $type, $originalWidth, $params, $expected, true ); |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
public function testThumbNameStepsRatio() { |
| 677 |
$this->overrideConfigValues( [ |
| 678 |
MainConfigNames::ThumbnailSteps => [ 10, 100, 200 ], |
| 679 |
|
| 680 |
MainConfigNames::ThumbnailStepsRatio => 0.5, |
| 681 |
] ); |
| 682 |
|
| 683 |
$params = [ 'width' => 90, 'height' => 90, 'physicalWidth' => 90, 'physicalHeight' => 90 ]; |
| 684 |
$this->assertThumbNameEquals( 'test1.jpg', 'image/jpeg', 500, $params, 100 ); |
| 685 |
|
| 686 |
$params = [ 'width' => 90, 'height' => 90, 'physicalWidth' => 90, 'physicalHeight' => 90 ]; |
| 687 |
$this->assertThumbNameEquals( 'test2.jpg', 'image/jpeg', 500, $params, 90 ); |
| 688 |
} |
| 689 |
} |