‪TYPO3CMS  ‪main
TcaJsonTest.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;
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
24 
25 final class ‪TcaJsonTest extends UnitTestCase
26 {
27  public static function ‪resultArrayDataProvider(): \Generator
28  {
29  yield 'Only handle new records' => [
30  [
31  'command' => 'edit',
32  'tableName' => 'aTable',
33  'databaseRow' => [
34  'aField' => '{"foo":"bar"}',
35  ],
36  'processedTca' => [
37  'columns' => [
38  'aField' => [
39  'config' => [
40  'type' => 'json',
41  ],
42  ],
43  ],
44  ],
45  ],
46  [
47  'command' => 'edit',
48  'tableName' => 'aTable',
49  'databaseRow' => [
50  'aField' => '{"foo":"bar"}',
51  ],
52  'processedTca' => [
53  'columns' => [
54  'aField' => [
55  'config' => [
56  'type' => 'json',
57  ],
58  ],
59  ],
60  ],
61  ],
62  ];
63  yield 'Only handle TCA type "json" records' => [
64  [
65  'command' => 'new',
66  'tableName' => 'aTable',
67  'databaseRow' => [
68  'aField' => '{"foo":"bar"}',
69  ],
70  'processedTca' => [
71  'columns' => [
72  'aField' => [
73  'config' => [
74  'type' => 'text',
75  ],
76  ],
77  ],
78  ],
79  ],
80  [
81  'command' => 'new',
82  'tableName' => 'aTable',
83  'databaseRow' => [
84  'aField' => '{"foo":"bar"}',
85  ],
86  'processedTca' => [
87  'columns' => [
88  'aField' => [
89  'config' => [
90  'type' => 'text',
91  ],
92  ],
93  ],
94  ],
95  ],
96  ];
97  yield 'Only handles string values' => [
98  [
99  'command' => 'new',
100  'tableName' => 'aTable',
101  'databaseRow' => [
102  'aField' => ['foo' => 'bar'],
103  ],
104  'processedTca' => [
105  'columns' => [
106  'aField' => [
107  'config' => [
108  'type' => 'json',
109  ],
110  ],
111  ],
112  ],
113  ],
114  [
115  'command' => 'new',
116  'tableName' => 'aTable',
117  'databaseRow' => [
118  'aField' => ['foo' => 'bar'],
119  ],
120  'processedTca' => [
121  'columns' => [
122  'aField' => [
123  'config' => [
124  'type' => 'json',
125  ],
126  ],
127  ],
128  ],
129  ],
130  ];
131  yield 'String values are properly decoded' => [
132  [
133  'command' => 'new',
134  'tableName' => 'aTable',
135  'databaseRow' => [
136  'aField' => '{"foo":"bar"}',
137  ],
138  'processedTca' => [
139  'columns' => [
140  'aField' => [
141  'config' => [
142  'type' => 'json',
143  ],
144  ],
145  ],
146  ],
147  ],
148  [
149  'command' => 'new',
150  'tableName' => 'aTable',
151  'databaseRow' => [
152  'aField' => ['foo' => 'bar'],
153  ],
154  'processedTca' => [
155  'columns' => [
156  'aField' => [
157  'config' => [
158  'type' => 'json',
159  ],
160  ],
161  ],
162  ],
163  ],
164  ];
165  yield 'Invalid values are handled properly' => [
166  [
167  'command' => 'new',
168  'tableName' => 'aTable',
169  'databaseRow' => [
170  'aField' => '_-invalid-_',
171  ],
172  'processedTca' => [
173  'columns' => [
174  'aField' => [
175  'config' => [
176  'type' => 'json',
177  ],
178  ],
179  ],
180  ],
181  ],
182  [
183  'command' => 'new',
184  'tableName' => 'aTable',
185  'databaseRow' => [
186  'aField' => [],
187  ],
188  'processedTca' => [
189  'columns' => [
190  'aField' => [
191  'config' => [
192  'type' => 'json',
193  ],
194  ],
195  ],
196  ],
197  ],
198  ];
199  yield 'Initialize empty values' => [
200  [
201  'command' => 'new',
202  'tableName' => 'aTable',
203  'databaseRow' => [
204  'aField' => '',
205  ],
206  'processedTca' => [
207  'columns' => [
208  'aField' => [
209  'config' => [
210  'type' => 'json',
211  ],
212  ],
213  ],
214  ],
215  ],
216  [
217  'command' => 'new',
218  'tableName' => 'aTable',
219  'databaseRow' => [
220  'aField' => [],
221  ],
222  'processedTca' => [
223  'columns' => [
224  'aField' => [
225  'config' => [
226  'type' => 'json',
227  ],
228  ],
229  ],
230  ],
231  ],
232  ];
233  }
234 
235  #[DataProvider('resultArrayDataProvider')]
236  #[Test]
237  public function ‪addDataDoesHandleJsonRecords(array $input, array $expected): void
238  {
239  self::assertSame($expected, (new ‪TcaJson())->addData($input));
240  }
241 }
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaJsonTest\addDataDoesHandleJsonRecords
‪addDataDoesHandleJsonRecords(array $input, array $expected)
Definition: TcaJsonTest.php:237
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaJson
Definition: TcaJson.php:26
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaJsonTest\resultArrayDataProvider
‪static resultArrayDataProvider()
Definition: TcaJsonTest.php:27
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaJsonTest
Definition: TcaJsonTest.php:26
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider
Definition: DatabaseDefaultLanguagePageRowTest.php:18