‪TYPO3CMS  ‪main
AbstractFormElementTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
26 final class ‪AbstractFormElementTest extends UnitTestCase
27 {
28  public static function ‪formatValueDataProvider(): array
29  {
30  return [
31  'format with empty format configuration' => [
32  [
33  'format' => '',
34  ],
35  '',
36  '',
37  ],
38  'format to date' => [
39  [
40  'format' => 'date',
41  ],
42  '1412358894',
43  '03-10-2014',
44  ],
45  'format to date with empty timestamp' => [
46  [
47  'format' => 'date',
48  ],
49  '0',
50  '',
51  ],
52  'format to date with blank timestamp' => [
53  [
54  'format' => 'date',
55  ],
56  '',
57  '',
58  ],
59  'format to date with option strftime' => [
60  [
61  'format' => 'date',
62  'format.' => [
63  'option' => '%d-%m',
64  'strftime' => true,
65  ],
66  ],
67  '1412358894',
68  '03-10',
69  ],
70  'format to date with option' => [
71  [
72  'format' => 'date',
73  'format.' => [
74  'option' => 'd-m',
75  ],
76  ],
77  '1412358894',
78  '03-10',
79  ],
80  'format to datetime' => [
81  [
82  'format' => 'datetime',
83  ],
84  '1412358894',
85  '03-10-14 17:54',
86  ],
87  'format to datetime with empty value' => [
88  [
89  'format' => 'datetime',
90  ],
91  '',
92  '',
93  ],
94  'format to datetime with null value' => [
95  [
96  'format' => 'datetime',
97  ],
98  null,
99  '',
100  ],
101  'format to time' => [
102  [
103  'format' => 'time',
104  ],
105  '64440',
106  '17:54',
107  ],
108  'format to time with empty value' => [
109  [
110  'format' => 'time',
111  ],
112  '',
113  '',
114  ],
115  'format to time with null value' => [
116  [
117  'format' => 'time',
118  ],
119  null,
120  '',
121  ],
122  'format to timesec' => [
123  [
124  'format' => 'timesec',
125  ],
126  '64494',
127  '17:54:54',
128  ],
129  'format to timesec with empty value' => [
130  [
131  'format' => 'timesec',
132  ],
133  '',
134  '',
135  ],
136  'format to timesec with null value' => [
137  [
138  'format' => 'timesec',
139  ],
140  null,
141  '',
142  ],
143  'format to year' => [
144  [
145  'format' => 'year',
146  ],
147  '1412358894',
148  '2014',
149  ],
150  'format to year with empty value' => [
151  [
152  'format' => 'year',
153  ],
154  '',
155  '',
156  ],
157  'format to year with null value' => [
158  [
159  'format' => 'year',
160  ],
161  null,
162  '',
163  ],
164  'format to int' => [
165  [
166  'format' => 'int',
167  ],
168  '123.00',
169  '123',
170  ],
171  'format to int with base' => [
172  [
173  'format' => 'int',
174  'format.' => [
175  'base' => 'oct',
176  ],
177  ],
178  '123',
179  '173',
180  ],
181  'format to int with empty value' => [
182  [
183  'format' => 'int',
184  ],
185  '',
186  '0',
187  ],
188  'format to float' => [
189  [
190  'format' => 'float',
191  ],
192  '123',
193  '123.00',
194  ],
195  'format to float with precision' => [
196  [
197  'format' => 'float',
198  'format.' => [
199  'precision' => '4',
200  ],
201  ],
202  '123',
203  '123.0000',
204  ],
205  'format to float with empty value' => [
206  [
207  'format' => 'float',
208  ],
209  '',
210  '0.00',
211  ],
212  'format to number' => [
213  [
214  'format' => 'number',
215  'format.' => [
216  'option' => 'b',
217  ],
218  ],
219  '123',
220  '1111011',
221  ],
222  'format to number with empty option' => [
223  [
224  'format' => 'number',
225  ],
226  '123',
227  '',
228  ],
229  'format to md5' => [
230  [
231  'format' => 'md5',
232  ],
233  'joh316',
234  'bacb98acf97e0b6112b1d1b650b84971',
235  ],
236  'format to md5 with empty value' => [
237  [
238  'format' => 'md5',
239  ],
240  '',
241  'd41d8cd98f00b204e9800998ecf8427e',
242  ],
243  'format to filesize' => [
244  [
245  'format' => 'filesize',
246  ],
247  '100000',
248  '98 Ki',
249  ],
250  'format to filesize with empty value' => [
251  [
252  'format' => 'filesize',
253  ],
254  '',
255  '0 ',
256  ],
257  'format to filesize with option appendByteSize' => [
258  [
259  'format' => 'filesize',
260  'format.' => [
261  'appendByteSize' => true,
262  ],
263  ],
264  '100000',
265  '98 Ki (100000)',
266  ],
267  ];
268  }
269 
270  #[DataProvider('formatValueDataProvider')]
271  #[Test]
272  public function ‪formatValueWithGivenConfiguration(array $config, ?string $itemValue, string $expectedResult): void
273  {
274  ‪$GLOBALS['BE_USER'] = new ‪BackendUserAuthentication();
275  $subject = $this->getAccessibleMock(AbstractFormElement::class, ['render'], [], '', false);
276  $timezoneBackup = date_default_timezone_get();
277  date_default_timezone_set('UTC');
278  $result = $subject->_call('formatValue', $config['format'], $itemValue, $config['format.'] ?? []);
279  date_default_timezone_set($timezoneBackup);
280 
281  self::assertSame($expectedResult, $result);
282  }
283 }
‪TYPO3\CMS\Backend\Tests\Unit\Form\Element\AbstractFormElementTest
Definition: AbstractFormElementTest.php:27
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement
Definition: AbstractFormElement.php:37
‪TYPO3\CMS\Backend\Tests\Unit\Form\Element\AbstractFormElementTest\formatValueDataProvider
‪static formatValueDataProvider()
Definition: AbstractFormElementTest.php:28
‪TYPO3\CMS\Backend\Tests\Unit\Form\Element\AbstractFormElementTest\formatValueWithGivenConfiguration
‪formatValueWithGivenConfiguration(array $config, ?string $itemValue, string $expectedResult)
Definition: AbstractFormElementTest.php:272
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Backend\Tests\Unit\Form\Element
Definition: AbstractFormElementTest.php:18
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25