[BUGFIX] Prevent division by zero when scaling image
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Imaging / GraphicalFunctionsTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Imaging;
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\Imaging\GraphicalFunctions;
18 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
19
20 class GraphicalFunctionsTest extends UnitTestCase
21 {
22 protected $resetSingletonInstances = true;
23
24 /**
25 * Dataprovider for getScaleForImage
26 *
27 * @return array
28 */
29 public function getScaleForImageDataProvider()
30 {
31 return [
32 'Get image scale for a width of 150px' => [
33 [
34 170,
35 136,
36 ],
37 '150',
38 '',
39 [],
40 [
41 'crs' => false,
42 'origW' => 150,
43 'origH' => 0,
44 'max' => 0,
45 0 => 150,
46 1 => (float)120
47 ],
48 ],
49 'Get image scale with a maximum width of 100px' => [
50 [
51 170,
52 136,
53 ],
54 '',
55 '',
56 [
57 'maxW' => 100
58 ],
59 [
60 'crs' => false,
61 'origW' => 100,
62 'origH' => 0,
63 'max' => 1,
64 0 => 100,
65 1 => (float)80
66 ],
67 ],
68 'Get image scale with a minimum width of 200px' => [
69 [
70 170,
71 136,
72 ],
73 '',
74 '',
75 [
76 'minW' => 200
77 ],
78 [
79 'crs' => false,
80 'origW' => 0,
81 'origH' => 0,
82 'max' => 0,
83 0 => 200,
84 1 => (float)136
85 ],
86 ],
87 'No PHP warning for zero in input dimensions when scaling' => [
88 [0, 0],
89 '50',
90 '',
91 [],
92 [
93 'crs' => false,
94 'origW' => 50,
95 'origH' => 0,
96 'max' => 0,
97 0 => 0,
98 1 => 0
99 ],
100 ],
101 ];
102 }
103
104 /**
105 * @test
106 * @dataProvider getScaleForImageDataProvider
107 */
108 public function getScaleForImage($info, $width, $height, $options, $expected)
109 {
110 $result = (new GraphicalFunctions())->getImageScale($info, $width, $height, $options);
111 $this->assertEquals($result, $expected);
112 }
113
114 /**
115 * @test
116 */
117 public function imageMagickIdentifyReturnsFormattedValues()
118 {
119 $file = 'myImageFile.png';
120 $expected = [
121 '123',
122 '234',
123 'png',
124 'myImageFile.png',
125 'png'
126 ];
127
128 $subject = $this->getAccessibleMock(GraphicalFunctions::class, ['executeIdentifyCommandForImageFile'], [], '', false);
129 $subject->_set('processorEnabled', true);
130 $subject->expects($this->once())->method('executeIdentifyCommandForImageFile')->with($file)->willReturn('123 234 png PNG');
131 $result = $subject->imageMagickIdentify($file);
132 $this->assertEquals($result, $expected);
133 }
134
135 /**
136 * @test
137 */
138 public function imageMagickIdentifyReturnsFormattedValuesWithOffset()
139 {
140 $file = 'myImageFile.png';
141 $expected = [
142 '200+0+0',
143 '400+0+0',
144 'png',
145 'myImageFile.png',
146 'png'
147 ];
148
149 $subject = $this->getAccessibleMock(GraphicalFunctions::class, ['executeIdentifyCommandForImageFile'], [], '', false);
150 $subject->_set('processorEnabled', true);
151 $subject->expects($this->once())->method('executeIdentifyCommandForImageFile')->with($file)->willReturn('200+0+0 400+0+0 png PNG');
152 $result = $subject->imageMagickIdentify($file);
153 $this->assertEquals($result, $expected);
154 }
155 }