[!!!][TASK] Use native trigger_error and ErrorHandler for deprecations
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Form / FormDataProvider / TcaFlexPrepareTest.php
1 <?php
2 namespace TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider;
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 Prophecy\Argument;
18 use Prophecy\Prophecy\ObjectProphecy;
19 use TYPO3\CMS\Backend\Form\FormDataProvider\TcaFlexPrepare;
20 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
21 use TYPO3\CMS\Core\Cache\CacheManager;
22 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25 /**
26 * Test case
27 */
28 class TcaFlexPrepareTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
29 {
30 /**
31 * @var TcaFlexPrepare
32 */
33 protected $subject;
34
35 /**
36 * @var BackendUserAuthentication|ObjectProphecy
37 */
38 protected $backendUserProphecy;
39
40 /**
41 * @var array A backup of registered singleton instances
42 */
43 protected $singletonInstances = [];
44
45 protected function setUp()
46 {
47 $this->singletonInstances = GeneralUtility::getSingletonInstances();
48
49 // Suppress cache foo in xml helpers of GeneralUtility
50 /** @var CacheManager|ObjectProphecy $cacheManagerProphecy */
51 $cacheManagerProphecy = $this->prophesize(CacheManager::class);
52 GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal());
53 $cacheFrontendProphecy = $this->prophesize(FrontendInterface::class);
54 $cacheManagerProphecy->getCache(Argument::cetera())->willReturn($cacheFrontendProphecy->reveal());
55
56 $this->subject = new TcaFlexPrepare();
57 }
58
59 protected function tearDown()
60 {
61 GeneralUtility::purgeInstances();
62 GeneralUtility::resetSingletonInstances($this->singletonInstances);
63 parent::tearDown();
64 }
65
66 /**
67 * @test
68 */
69 public function addDataKeepsExistingDataStructure()
70 {
71 $input = [
72 'systemLanguageRows' => [],
73 'tableName' => 'aTableName',
74 'databaseRow' => [
75 'aField' => [
76 'data' => [],
77 'meta' => [],
78 ],
79 ],
80 'processedTca' => [
81 'columns' => [
82 'aField' => [
83 'config' => [
84 'type' => 'flex',
85 'dataStructureIdentifier' => '{"type":"tca","tableName":"aTableName","fieldName":"aField","dataStructureKey":"default"}',
86 'ds' => [
87 'sheets' => [
88 'sDEF' => [
89 'ROOT' => [
90 'type' => 'array',
91 'el' => [
92 'aFlexField' => [
93 'label' => 'aFlexFieldLabel',
94 'config' => [
95 'type' => 'input',
96 ],
97 ],
98 ],
99 ],
100 ],
101 ],
102 'meta' => [],
103 ],
104 ],
105 ],
106 ],
107 ],
108 ];
109 $expected = $input;
110 $this->assertEquals($expected, $this->subject->addData($input));
111 }
112
113 /**
114 * @test
115 */
116 public function addDataSetsParsedDataStructureArray()
117 {
118 $input = [
119 'systemLanguageRows' => [],
120 'tableName' => 'aTableName',
121 'databaseRow' => [
122 'aField' => [
123 'data' => [],
124 'meta' => [],
125 ],
126 ],
127 'processedTca' => [
128 'columns' => [
129 'aField' => [
130 'config' => [
131 'type' => 'flex',
132 'ds' => [
133 'default' => '
134 <T3DataStructure>
135 <ROOT>
136 <type>array</type>
137 <el>
138 <aFlexField>
139 <TCEforms>
140 <label>aFlexFieldLabel</label>
141 <config>
142 <type>input</type>
143 </config>
144 </TCEforms>
145 </aFlexField>
146 </el>
147 </ROOT>
148 </T3DataStructure>
149 ',
150 ],
151 ],
152 ],
153 ],
154 ],
155 ];
156
157 $GLOBALS['TCA']['aTableName']['columns'] = $input['processedTca']['columns'];
158
159 $expected = $input;
160 $expected['processedTca']['columns']['aField']['config']['dataStructureIdentifier']
161 = '{"type":"tca","tableName":"aTableName","fieldName":"aField","dataStructureKey":"default"}';
162 $expected['processedTca']['columns']['aField']['config']['ds'] = [
163 'sheets' => [
164 'sDEF' => [
165 'ROOT' => [
166 'type' => 'array',
167 'el' => [
168 'aFlexField' => [
169 'label' => 'aFlexFieldLabel',
170 'config' => [
171 'type' => 'input',
172 ],
173 ],
174 ],
175 ],
176 ],
177 ],
178 'meta' => [],
179 ];
180
181 $this->assertEquals($expected, $this->subject->addData($input));
182 }
183
184 /**
185 * @test
186 */
187 public function addDataSetsParsedDataStructureArrayWithSheets()
188 {
189 $input = [
190 'systemLanguageRows' => [],
191 'tableName' => 'aTableName',
192 'databaseRow' => [
193 'aField' => [
194 'data' => [],
195 'meta' => [],
196 ],
197 ],
198 'processedTca' => [
199 'columns' => [
200 'aField' => [
201 'config' => [
202 'type' => 'flex',
203 'ds' => [
204 'default' => '
205 <T3DataStructure>
206 <sheets>
207 <sDEF>
208 <ROOT>
209 <TCEforms>
210 <sheetTitle>aTitle</sheetTitle>
211 </TCEforms>
212 <type>array</type>
213 <el>
214 <aFlexField>
215 <TCEforms>
216 <label>aFlexFieldLabel</label>
217 <config>
218 <type>input</type>
219 </config>
220 </TCEforms>
221 </aFlexField>
222 </el>
223 </ROOT>
224 </sDEF>
225 </sheets>
226 </T3DataStructure>
227 ',
228 ],
229 ],
230 ],
231 ],
232 ],
233 ];
234
235 $GLOBALS['TCA']['aTableName']['columns'] = $input['processedTca']['columns'];
236
237 $expected = $input;
238 $expected['processedTca']['columns']['aField']['config']['dataStructureIdentifier']
239 = '{"type":"tca","tableName":"aTableName","fieldName":"aField","dataStructureKey":"default"}';
240 $expected['processedTca']['columns']['aField']['config']['ds'] = [
241 'sheets' => [
242 'sDEF' => [
243 'ROOT' => [
244 'type' => 'array',
245 'el' => [
246 'aFlexField' => [
247 'label' => 'aFlexFieldLabel',
248 'config' => [
249 'type' => 'input',
250 ],
251 ],
252 ],
253 'sheetTitle' => 'aTitle',
254 ],
255 ],
256 ],
257 'meta' => [],
258 ];
259
260 $this->assertEquals($expected, $this->subject->addData($input));
261 }
262
263 /**
264 * @test
265 */
266 public function addDataInitializesDatabaseRowValueIfNoDataStringIsGiven()
267 {
268 $input = [
269 'databaseRow' => [],
270 'tableName' => 'aTableName',
271 'systemLanguageRows' => [],
272 'processedTca' => [
273 'columns' => [
274 'aField' => [
275 'config' => [
276 'type' => 'flex',
277 'ds' => [
278 'default' => '
279 <T3DataStructure>
280 <ROOT></ROOT>
281 </T3DataStructure>
282 ',
283 ],
284 ],
285 ],
286 ],
287 ],
288 ];
289
290 $GLOBALS['TCA']['aTableName']['columns'] = $input['processedTca']['columns'];
291
292 $expected = $input;
293 $expected['processedTca']['columns']['aField']['config']['dataStructureIdentifier']
294 = '{"type":"tca","tableName":"aTableName","fieldName":"aField","dataStructureKey":"default"}';
295 $expected['processedTca']['columns']['aField']['config']['ds'] = [
296 'ROOT' => '',
297 'meta' => [],
298 ];
299 $expected['databaseRow']['aField'] = [
300 'data' => [],
301 'meta' => []
302 ];
303
304 $this->assertEquals($expected, $this->subject->addData($input));
305 }
306 }