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