[BUGFIX] Generate thumbnails for files without width/height
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Resource / Processing / LocalPreviewHelperTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Resource\Processing;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Resource\File;
18 use TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper;
19 use TYPO3\CMS\Core\Resource\Processing\TaskInterface;
20 use TYPO3\CMS\Core\Tests\UnitTestCase;
21
22 /**
23 * Testcase for \TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper
24 */
25 class LocalPreviewHelperTest extends UnitTestCase {
26
27 /**
28 * @test
29 */
30 public function processProvidesDefaultSizeIfNotConfigured() {
31 $file = $this->getMockBuilder(File::class)
32 ->disableOriginalConstructor()
33 ->getMock();
34 // Use size slightly larger than default size to ensure processing
35 $file->expects($this->any())->method('getProperty')->will($this->returnValueMap(array(
36 array('width', 65),
37 array('height', 65),
38 )));
39
40 $task = $this->getMock(TaskInterface::class);
41 $task->expects($this->once())->method('getSourceFile')->willReturn($file);
42 $task->expects($this->once())->method('getConfiguration')->willReturn(array());
43
44 $localPreviewHelper = $this->getMockBuilder(LocalPreviewHelper::class)
45 ->disableOriginalConstructor()
46 ->setMethods(array('getTemporaryFilePath', 'generatePreviewFromFile'))
47 ->getMock();
48 $localPreviewHelper->expects($this->once())->method('getTemporaryFilePath')->willReturn('test/file');
49 // Assert that by default 64x64 is used as preview size
50 $localPreviewHelper->expects($this->once())->method('generatePreviewFromFile')
51 ->with($file, array('width' => 64, 'height' => 64), 'test/file');
52
53 $localPreviewHelper->process($task);
54 }
55
56 /**
57 * @test
58 */
59 public function processDoesNotScaleUpImages() {
60 $file = $this->getMockBuilder(File::class)
61 ->disableOriginalConstructor()
62 ->getMock();
63 $file->expects($this->any())->method('getProperty')->will($this->returnValueMap(array(
64 array('width', 20),
65 array('height', 20),
66 )));
67
68 $localPreviewHelper = $this->getMockBuilder(LocalPreviewHelper::class)
69 ->disableOriginalConstructor()
70 ->setMethods(array('dummy'))
71 ->getMock();
72
73 $task = $this->getMock(TaskInterface::class);
74 $task->expects($this->once())->method('getSourceFile')->willReturn($file);
75 $task->expects($this->once())->method('getConfiguration')->willReturn(array('width' => 30, 'height' => 30));
76
77 $this->assertNull($localPreviewHelper->process($task));
78 }
79
80 /**
81 * @test
82 */
83 public function processGeneratesPreviewEvenIfSourceFileHasNoSize() {
84 $file = $this->getMockBuilder(File::class)
85 ->disableOriginalConstructor()
86 ->getMock();
87 $file->expects($this->any())->method('getProperty')->will($this->returnValueMap(array(
88 array('width', 0),
89 array('height', 0),
90 )));
91
92 $task = $this->getMock(TaskInterface::class);
93 $task->expects($this->once())->method('getSourceFile')->willReturn($file);
94 $task->expects($this->once())->method('getConfiguration')->willReturn(array());
95
96 $localPreviewHelper = $this->getMockBuilder(LocalPreviewHelper::class)
97 ->disableOriginalConstructor()
98 ->setMethods(array('getTemporaryFilePath', 'generatePreviewFromFile'))
99 ->getMock();
100 $expectedResult = array('width' => 20, 'height' => 20, 'filePath' => 'test/file');
101 $localPreviewHelper->expects($this->once())->method('generatePreviewFromFile')->willReturn($expectedResult);
102
103 $this->assertEquals($expectedResult, $localPreviewHelper->process($task));
104 }
105 }