4d4493df9f6d473670c9144e8578cafbf8f74556
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Form / Element / AbstractFormElementTest.php
1 <?php
2 namespace TYPO3\CMS\Backend\Tests\Unit\Form;
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\Backend\Form\Element\AbstractFormElement;
18
19 /**
20 * Test case
21 */
22 class AbstractFormElementTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
23 {
24 /**
25 * Subject is not notice free, disable E_NOTICES
26 */
27 protected static $suppressNotices = true;
28
29 /**
30 * @return array
31 */
32 public function formatValueDataProvider()
33 {
34 return [
35 'format with empty format configuration' => [
36 [
37 'format' => '',
38 ],
39 '',
40 '',
41 ],
42 'format to date' => [
43 [
44 'format' => 'date',
45 ],
46 '1412358894',
47 '03-10-2014'
48 ],
49 'format to date with empty timestamp' => [
50 [
51 'format' => 'date',
52 ],
53 '0',
54 ''
55 ],
56 'format to date with blank timestamp' => [
57 [
58 'format' => 'date',
59 ],
60 '',
61 ''
62 ],
63 'format to date with option strftime' => [
64 [
65 'format' => 'date',
66 'format.' => [
67 'option' => '%d-%m',
68 'strftime' => true,
69 ],
70 ],
71 '1412358894',
72 '03-10'
73 ],
74 'format to date with option' => [
75 [
76 'format' => 'date',
77 'format.' => [
78 'option' => 'd-m',
79 ],
80 ],
81 '1412358894',
82 '03-10'
83 ],
84 'format to datetime' => [
85 [
86 'format' => 'datetime',
87 ],
88 '1412358894',
89 '17:54 03-10-2014'
90 ],
91 'format to datetime with empty value' => [
92 [
93 'format' => 'datetime',
94 ],
95 '',
96 ''
97 ],
98 'format to datetime with null value' => [
99 [
100 'format' => 'datetime',
101 ],
102 null,
103 ''
104 ],
105 'format to time' => [
106 [
107 'format' => 'time',
108 ],
109 '64440',
110 '17:54'
111 ],
112 'format to time with empty value' => [
113 [
114 'format' => 'time',
115 ],
116 '',
117 ''
118 ],
119 'format to time with null value' => [
120 [
121 'format' => 'time',
122 ],
123 null,
124 ''
125 ],
126 'format to timesec' => [
127 [
128 'format' => 'timesec',
129 ],
130 '64494',
131 '17:54:54'
132 ],
133 'format to timesec with empty value' => [
134 [
135 'format' => 'timesec',
136 ],
137 '',
138 ''
139 ],
140 'format to timesec with null value' => [
141 [
142 'format' => 'timesec',
143 ],
144 null,
145 ''
146 ],
147 'format to year' => [
148 [
149 'format' => 'year',
150 ],
151 '1412358894',
152 '2014'
153 ],
154 'format to year with empty value' => [
155 [
156 'format' => 'year',
157 ],
158 '',
159 ''
160 ],
161 'format to year with null value' => [
162 [
163 'format' => 'year',
164 ],
165 null,
166 ''
167 ],
168 'format to int' => [
169 [
170 'format' => 'int',
171 ],
172 '123.00',
173 '123'
174 ],
175 'format to int with base' => [
176 [
177 'format' => 'int',
178 'format.' => [
179 'base' => 'oct',
180 ],
181 ],
182 '123',
183 '173'
184 ],
185 'format to int with empty value' => [
186 [
187 'format' => 'int',
188 ],
189 '',
190 '0'
191 ],
192 'format to float' => [
193 [
194 'format' => 'float',
195 ],
196 '123',
197 '123.00'
198 ],
199 'format to float with precision' => [
200 [
201 'format' => 'float',
202 'format.' => [
203 'precision' => '4',
204 ],
205 ],
206 '123',
207 '123.0000'
208 ],
209 'format to float with empty value' => [
210 [
211 'format' => 'float',
212 ],
213 '',
214 '0.00'
215 ],
216 'format to number' => [
217 [
218 'format' => 'number',
219 'format.' => [
220 'option' => 'b',
221 ],
222 ],
223 '123',
224 '1111011'
225 ],
226 'format to number with empty option' => [
227 [
228 'format' => 'number',
229 ],
230 '123',
231 ''
232 ],
233 'format to md5' => [
234 [
235 'format' => 'md5',
236 ],
237 'joh316',
238 'bacb98acf97e0b6112b1d1b650b84971'
239 ],
240 'format to md5 with empty value' => [
241 [
242 'format' => 'md5',
243 ],
244 '',
245 'd41d8cd98f00b204e9800998ecf8427e'
246 ],
247 'format to filesize' => [
248 [
249 'format' => 'filesize',
250 ],
251 '100000',
252 '98 Ki'
253 ],
254 'format to filesize with empty value' => [
255 [
256 'format' => 'filesize',
257 ],
258 '',
259 '0 '
260 ],
261 'format to filesize with option appendByteSize' => [
262 [
263 'format' => 'filesize',
264 'format.' => [
265 'appendByteSize' => true,
266 ],
267 ],
268 '100000',
269 '98 Ki (100000)'
270 ],
271 ];
272 }
273
274 /**
275 * @param array $config
276 * @param string $itemValue
277 * @param string $expectedResult
278 * @dataProvider formatValueDataProvider
279 * @test
280 */
281 public function formatValueWithGivenConfiguration($config, $itemValue, $expectedResult)
282 {
283 /** @var AbstractFormElement|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface */
284 $subject = $this->getAccessibleMock(AbstractFormElement::class, ['render'], [], '', false);
285 $timezoneBackup = date_default_timezone_get();
286 date_default_timezone_set('UTC');
287 $result = $subject->_call('formatValue', $config['format'], $itemValue, $config['format.']);
288 date_default_timezone_set($timezoneBackup);
289
290 $this->assertEquals($expectedResult, $result);
291 }
292 }