13 $file = $this->
dataFile( $filename );
14 $this->assertEquals( $file->canAnimateThumbIfAppropriate(), $expected );
19 [
'nonanimated.gif',
true ],
20 [
'jpeg-comment-utf.jpg',
true ],
21 [
'test.tiff',
true ],
22 [
'Animated_PNG_example_bouncing_beach_ball.png',
false ],
23 [
'greyscale-png.png',
true ],
24 [
'Toll_Texas_1.svg',
true ],
25 [
'LoremIpsum.djvu',
true ],
26 [
'80x60-2layers.xcf',
true ],
27 [
'Soccer_ball_animated.svg',
false ],
28 [
'Bishzilla_blink.gif',
false ],
29 [
'animated.gif',
true ],
38 $this->
setMwGlobals(
'wgThumbnailBuckets', $data[
'buckets'] );
39 $this->
setMwGlobals(
'wgThumbnailMinimumBucketDistance', $data[
'minimumBucketDistance'] );
42 ->setConstructorArgs( [
'fileMock',
false ] )
43 ->setMethods( [
'getWidth' ] )
44 ->getMockForAbstractClass();
46 $fileMock->expects( $this->
any() )
47 ->method(
'getWidth' )
48 ->will( $this->returnValue( $data[
'width'] ) );
51 $data[
'expectedBucket'],
52 $fileMock->getThumbnailBucket( $data[
'requestedWidth'] ),
57 $defaultBuckets = [ 256, 512, 1024, 2048, 4096 ];
61 'buckets' => $defaultBuckets,
62 'minimumBucketDistance' => 0,
64 'requestedWidth' => 120,
65 'expectedBucket' => 256,
66 'message' =>
'Picking bucket bigger than requested size'
69 'buckets' => $defaultBuckets,
70 'minimumBucketDistance' => 0,
72 'requestedWidth' => 300,
73 'expectedBucket' => 512,
74 'message' =>
'Picking bucket bigger than requested size'
77 'buckets' => $defaultBuckets,
78 'minimumBucketDistance' => 0,
80 'requestedWidth' => 1024,
81 'expectedBucket' => 2048,
82 'message' =>
'Picking bucket bigger than requested size'
85 'buckets' => $defaultBuckets,
86 'minimumBucketDistance' => 0,
88 'requestedWidth' => 2048,
89 'expectedBucket' =>
false,
90 'message' =>
'Picking no bucket because none is bigger than the requested size'
93 'buckets' => $defaultBuckets,
94 'minimumBucketDistance' => 0,
96 'requestedWidth' => 3500,
97 'expectedBucket' =>
false,
98 'message' =>
'Picking no bucket because requested size is bigger than original'
101 'buckets' => [ 1024 ],
102 'minimumBucketDistance' => 0,
104 'requestedWidth' => 1024,
105 'expectedBucket' =>
false,
106 'message' =>
'Picking no bucket because requested size equals biggest bucket'
110 'minimumBucketDistance' => 0,
112 'requestedWidth' => 1024,
113 'expectedBucket' =>
false,
114 'message' =>
'Picking no bucket because no buckets have been specified'
117 'buckets' => [ 256, 512 ],
118 'minimumBucketDistance' => 10,
120 'requestedWidth' => 245,
121 'expectedBucket' => 256,
122 'message' =>
'Requested width is distant enough from next bucket for it to be picked'
125 'buckets' => [ 256, 512 ],
126 'minimumBucketDistance' => 10,
128 'requestedWidth' => 246,
129 'expectedBucket' => 512,
130 'message' =>
'Requested width is too close to next bucket, picking next one'
141 ->setConstructorArgs( [ [
'name' =>
'backendMock',
'wikiId' =>
wfWikiID() ] ] )
145 ->setConstructorArgs( [ [
'name' =>
'repoMock',
'backend' => $backendMock ] ] )
146 ->setMethods( [
'fileExists',
'getLocalReference' ] )
149 $fsFile =
new FSFile(
'fsFilePath' );
151 $repoMock->expects( $this->
any() )
152 ->method(
'fileExists' )
153 ->will( $this->returnValue(
true ) );
155 $repoMock->expects( $this->
any() )
156 ->method(
'getLocalReference' )
157 ->will( $this->returnValue( $fsFile ) );
160 ->setMethods( [
'supportsBucketing' ] )->getMock();
161 $handlerMock->expects( $this->
any() )
162 ->method(
'supportsBucketing' )
163 ->will( $this->returnValue( $data[
'supportsBucketing'] ) );
166 ->setConstructorArgs( [
'fileMock', $repoMock ] )
167 ->setMethods( [
'getThumbnailBucket',
'getLocalRefPath',
'getHandler' ] )
168 ->getMockForAbstractClass();
170 $fileMock->expects( $this->
any() )
171 ->method(
'getThumbnailBucket' )
172 ->will( $this->returnValue( $data[
'thumbnailBucket'] ) );
174 $fileMock->expects( $this->
any() )
175 ->method(
'getLocalRefPath' )
176 ->will( $this->returnValue(
'localRefPath' ) );
178 $fileMock->expects( $this->
any() )
179 ->method(
'getHandler' )
180 ->will( $this->returnValue( $handlerMock ) );
182 $reflection =
new ReflectionClass( $fileMock );
183 $reflection_property = $reflection->getProperty(
'handler' );
184 $reflection_property->setAccessible(
true );
185 $reflection_property->setValue( $fileMock, $handlerMock );
187 if ( !is_null( $data[
'tmpBucketedThumbCache'] ) ) {
188 $reflection_property = $reflection->getProperty(
'tmpBucketedThumbCache' );
189 $reflection_property->setAccessible(
true );
190 $reflection_property->setValue( $fileMock, $data[
'tmpBucketedThumbCache'] );
193 $result = $fileMock->getThumbnailSource(
194 [
'physicalWidth' => $data[
'physicalWidth'] ] );
196 $this->assertEquals( $data[
'expectedPath'],
$result[
'path'], $data[
'message'] );
202 'supportsBucketing' =>
true,
203 'tmpBucketedThumbCache' =>
null,
204 'thumbnailBucket' => 1024,
205 'physicalWidth' => 2048,
206 'expectedPath' =>
'fsFilePath',
207 'message' =>
'Path downloaded from storage'
210 'supportsBucketing' =>
true,
211 'tmpBucketedThumbCache' => [ 1024 =>
'/tmp/shouldnotexist' . rand() ],
212 'thumbnailBucket' => 1024,
213 'physicalWidth' => 2048,
214 'expectedPath' =>
'fsFilePath',
215 'message' =>
'Path downloaded from storage because temp file is missing'
218 'supportsBucketing' =>
true,
219 'tmpBucketedThumbCache' => [ 1024 =>
'/tmp' ],
220 'thumbnailBucket' => 1024,
221 'physicalWidth' => 2048,
222 'expectedPath' =>
'/tmp',
223 'message' =>
'Temporary path because temp file was found'
226 'supportsBucketing' =>
false,
227 'tmpBucketedThumbCache' =>
null,
228 'thumbnailBucket' => 1024,
229 'physicalWidth' => 2048,
230 'expectedPath' =>
'localRefPath',
231 'message' =>
'Original file path because bucketing is unsupported by handler'
234 'supportsBucketing' =>
true,
235 'tmpBucketedThumbCache' =>
null,
236 'thumbnailBucket' =>
false,
237 'physicalWidth' => 2048,
238 'expectedPath' =>
'localRefPath',
239 'message' =>
'Original file path because no width provided'
249 $this->
setMwGlobals(
'wgThumbnailBuckets', $data[
'buckets'] );
252 ->setConstructorArgs( [ [
'name' =>
'backendMock',
'wikiId' =>
wfWikiID() ] ] )
256 ->setConstructorArgs( [ [
'name' =>
'repoMock',
'backend' => $backendMock ] ] )
257 ->setMethods( [
'fileExists',
'getLocalReference' ] )
261 ->setConstructorArgs( [
'fileMock', $repoMock ] )
262 ->setMethods( [
'getWidth',
'getBucketThumbPath',
'makeTransformTmpFile',
263 'generateAndSaveThumb',
'getHandler' ] )
264 ->getMockForAbstractClass();
267 ->setMethods( [
'supportsBucketing' ] )->getMock();
268 $handlerMock->expects( $this->
any() )
269 ->method(
'supportsBucketing' )
270 ->will( $this->returnValue(
true ) );
272 $fileMock->expects( $this->
any() )
273 ->method(
'getHandler' )
274 ->will( $this->returnValue( $handlerMock ) );
276 $reflectionMethod =
new ReflectionMethod(
File::class,
'generateBucketsIfNeeded' );
277 $reflectionMethod->setAccessible(
true );
279 $fileMock->expects( $this->
any() )
280 ->method(
'getWidth' )
281 ->will( $this->returnValue( $data[
'width'] ) );
283 $fileMock->expects( $data[
'expectedGetBucketThumbPathCalls'] )
284 ->method(
'getBucketThumbPath' );
286 $repoMock->expects( $data[
'expectedFileExistsCalls'] )
287 ->method(
'fileExists' )
288 ->will( $this->returnValue( $data[
'fileExistsReturn'] ) );
290 $fileMock->expects( $data[
'expectedMakeTransformTmpFile'] )
291 ->method(
'makeTransformTmpFile' )
292 ->will( $this->returnValue( $data[
'makeTransformTmpFileReturn'] ) );
294 $fileMock->expects( $data[
'expectedGenerateAndSaveThumb'] )
295 ->method(
'generateAndSaveThumb' )
296 ->will( $this->returnValue( $data[
'generateAndSaveThumbReturn'] ) );
298 $this->assertEquals( $data[
'expectedResult'],
299 $reflectionMethod->invoke(
302 'physicalWidth' => $data[
'physicalWidth'],
303 'physicalHeight' => $data[
'physicalHeight'] ]
309 $defaultBuckets = [ 256, 512, 1024, 2048, 4096 ];
313 'buckets' => $defaultBuckets,
315 'physicalWidth' => 256,
316 'physicalHeight' => 100,
317 'expectedGetBucketThumbPathCalls' => $this->never(),
318 'expectedFileExistsCalls' => $this->never(),
319 'fileExistsReturn' =>
null,
320 'expectedMakeTransformTmpFile' => $this->never(),
321 'makeTransformTmpFileReturn' =>
false,
322 'expectedGenerateAndSaveThumb' => $this->never(),
323 'generateAndSaveThumbReturn' =>
false,
324 'expectedResult' =>
false,
325 'message' =>
'No bucket found, nothing to generate'
328 'buckets' => $defaultBuckets,
330 'physicalWidth' => 300,
331 'physicalHeight' => 200,
332 'expectedGetBucketThumbPathCalls' => $this->once(),
333 'expectedFileExistsCalls' => $this->once(),
334 'fileExistsReturn' =>
true,
335 'expectedMakeTransformTmpFile' => $this->never(),
336 'makeTransformTmpFileReturn' =>
false,
337 'expectedGenerateAndSaveThumb' => $this->never(),
338 'generateAndSaveThumbReturn' =>
false,
339 'expectedResult' =>
false,
340 'message' =>
'File already exists, no reason to generate buckets'
343 'buckets' => $defaultBuckets,
345 'physicalWidth' => 300,
346 'physicalHeight' => 200,
347 'expectedGetBucketThumbPathCalls' => $this->once(),
348 'expectedFileExistsCalls' => $this->once(),
349 'fileExistsReturn' =>
false,
350 'expectedMakeTransformTmpFile' => $this->once(),
351 'makeTransformTmpFileReturn' =>
false,
352 'expectedGenerateAndSaveThumb' => $this->never(),
353 'generateAndSaveThumbReturn' =>
false,
354 'expectedResult' =>
false,
355 'message' =>
'Cannot generate temp file for bucket'
358 'buckets' => $defaultBuckets,
360 'physicalWidth' => 300,
361 'physicalHeight' => 200,
362 'expectedGetBucketThumbPathCalls' => $this->once(),
363 'expectedFileExistsCalls' => $this->once(),
364 'fileExistsReturn' =>
false,
365 'expectedMakeTransformTmpFile' => $this->once(),
366 'makeTransformTmpFileReturn' =>
new TempFSFile(
'/tmp/foo' ),
367 'expectedGenerateAndSaveThumb' => $this->once(),
368 'generateAndSaveThumbReturn' =>
false,
369 'expectedResult' =>
false,
370 'message' =>
'Bucket image could not be generated'
373 'buckets' => $defaultBuckets,
375 'physicalWidth' => 300,
376 'physicalHeight' => 200,
377 'expectedGetBucketThumbPathCalls' => $this->once(),
378 'expectedFileExistsCalls' => $this->once(),
379 'fileExistsReturn' =>
false,
380 'expectedMakeTransformTmpFile' => $this->once(),
381 'makeTransformTmpFileReturn' =>
new TempFSFile(
'/tmp/foo' ),
382 'expectedGenerateAndSaveThumb' => $this->once(),
383 'generateAndSaveThumbReturn' =>
new ThumbnailImage(
false,
'bar',
false,
false ),
384 'expectedResult' =>
true,
385 'message' =>
'Bucket image could not be generated'