[BUGFIX] Notice free FormEngine testing
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Form / FormDataProvider / TcaGroupTest.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\Prophecy\ObjectProphecy;
18 use TYPO3\CMS\Backend\Clipboard\Clipboard;
19 use TYPO3\CMS\Backend\Form\FormDataProvider\TcaGroup;
20 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
21 use TYPO3\CMS\Core\Database\RelationHandler;
22 use TYPO3\CMS\Core\Resource\Folder;
23 use TYPO3\CMS\Core\Resource\ResourceFactory;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26
27 /**
28 * Test case
29 */
30 class TcaGroupTest extends UnitTestCase
31 {
32 /**
33 * @var array
34 */
35 protected $singletonInstances;
36
37 protected function setUp()
38 {
39 $this->singletonInstances = GeneralUtility::getSingletonInstances();
40 }
41
42 protected function tearDown()
43 {
44 GeneralUtility::resetSingletonInstances($this->singletonInstances);
45 parent::tearDown();
46 }
47
48 /**
49 * @test
50 */
51 public function addDataReturnsFieldUnchangedIfFieldIsNotTypeGroup()
52 {
53 $input = [
54 'databaseRow' => [
55 'aField' => 'aValue',
56 ],
57 'processedTca' => [
58 'columns' => [
59 'aField' => [
60 'config' => [
61 'type' => 'foo',
62 ],
63 ],
64 ],
65 ],
66 ];
67 $expected = $input;
68 $this->assertSame($expected, (new TcaGroup)->addData($input));
69 }
70
71 /**
72 * @test
73 */
74 public function addDataThrowsExceptionWithTypeGroupAndNoValidInternalType()
75 {
76 $input = [
77 'tableName' => 'aTable',
78 'processedTca' => [
79 'columns' => [
80 'aField' => [
81 'config' => [
82 'type' => 'group',
83 'internal_type' => 'foo',
84 ],
85 ],
86 ],
87 ],
88 ];
89 $this->expectException(\UnexpectedValueException::class);
90 $this->expectExceptionCode(1438780511);
91 (new TcaGroup)->addData($input);
92 }
93
94 /**
95 * @test
96 */
97 public function addDataSetsUploadFolderForFileReference()
98 {
99 $input = [
100 'databaseRow' => [
101 'aField' => '',
102 ],
103 'processedTca' => [
104 'columns' => [
105 'aField' => [
106 'config' => [
107 'type' => 'group',
108 'internal_type' => 'file_reference',
109 'maxitems' => 99999,
110 ],
111 ],
112 ],
113 ],
114 ];
115
116 $expected = $input;
117 $expected['databaseRow']['aField'] = [];
118 $expected['processedTca']['columns']['aField']['config']['allowed'] = '*';
119 $expected['processedTca']['columns']['aField']['config']['clipboardElements'] = [];
120 $expected['processedTca']['columns']['aField']['config']['uploadfolder'] = '';
121
122 $clipboardProphecy = $this->prophesize(Clipboard::class);
123 GeneralUtility::addInstance(Clipboard::class, $clipboardProphecy->reveal());
124 $clipboardProphecy->initializeClipboard()->shouldBeCalled();
125 $clipboardProphecy->elFromTable('_FILE')->shouldBeCalled()->willReturn([]);
126
127 $this->assertEquals($expected, (new TcaGroup)->addData($input));
128 }
129
130 /**
131 * @test
132 */
133 public function addDataSetsFileData()
134 {
135 $input = [
136 'databaseRow' => [
137 'aField' => '/aDir/aFile.txt,/anotherDir/anotherFile.css',
138 ],
139 'processedTca' => [
140 'columns' => [
141 'aField' => [
142 'config' => [
143 'type' => 'group',
144 'internal_type' => 'file',
145 'maxitems' => 99999,
146 ],
147 ],
148 ],
149 ],
150 ];
151
152 $clipboardProphecy = $this->prophesize(Clipboard::class);
153 GeneralUtility::addInstance(Clipboard::class, $clipboardProphecy->reveal());
154 $clipboardProphecy->initializeClipboard()->shouldBeCalled();
155 $clipboardProphecy->elFromTable('_FILE')->shouldBeCalled()->willReturn([]);
156
157 $expected = $input;
158 $expected['databaseRow']['aField'] = [
159 [
160 'uidOrPath' => '/aDir/aFile.txt',
161 'title' => '/aDir/aFile.txt',
162 ],
163 [
164 'uidOrPath' => '/anotherDir/anotherFile.css',
165 'title' => '/anotherDir/anotherFile.css',
166 ],
167 ];
168 $expected['processedTca']['columns']['aField']['config']['clipboardElements'] = [];
169 $expected['processedTca']['columns']['aField']['config']['allowed'] = '*';
170 $this->assertEquals($expected, (new TcaGroup)->addData($input));
171 }
172
173 /**
174 * @test
175 */
176 public function addDataSetsFolderData()
177 {
178 $input = [
179 'databaseRow' => [
180 'aField' => '1:/aFolder/anotherFolder/',
181 ],
182 'processedTca' => [
183 'columns' => [
184 'aField' => [
185 'config' => [
186 'type' => 'group',
187 'internal_type' => 'folder',
188 'maxitems' => 99999,
189 ],
190 ],
191 ],
192 ],
193 ];
194
195 /** @var Folder|ObjectProphecy $relationHandlerProphecy */
196 $folderProphecy = $this->prophesize(Folder::class);
197
198 /** @var ResourceFactory|ObjectProphecy $relationHandlerProphecy */
199 $resourceFactoryProphecy = $this->prophesize(ResourceFactory::class);
200 GeneralUtility::setSingletonInstance(ResourceFactory::class, $resourceFactoryProphecy->reveal());
201 $resourceFactoryProphecy->retrieveFileOrFolderObject('1:/aFolder/anotherFolder/')
202 ->shouldBeCalled()
203 ->willReturn($folderProphecy->reveal());
204
205 $expected = $input;
206 $expected['databaseRow']['aField'] = [
207 [
208 'folder' => '1:/aFolder/anotherFolder/',
209 ]
210 ];
211 $expected['processedTca']['columns']['aField']['config']['clipboardElements'] = [];
212 $this->assertSame($expected, (new TcaGroup)->addData($input));
213 }
214
215 /**
216 * @test
217 */
218 public function addDataSetsDatabaseData()
219 {
220 $aFieldConfig = [
221 'type' => 'group',
222 'internal_type' => 'db',
223 'MM' => 'mmTableName',
224 'allowed' => 'aForeignTable',
225 'maxitems' => 99999,
226 ];
227 $input = [
228 'tableName' => 'aTable',
229 'databaseRow' => [
230 'uid' => 42,
231 'aField' => '1,2',
232 ],
233 'processedTca' => [
234 'columns' => [
235 'aField' => [
236 'config' => $aFieldConfig,
237 ],
238 ],
239 ],
240 ];
241
242 $backendUserProphecy = $this->prophesize(BackendUserAuthentication::class);
243 $GLOBALS['BE_USER'] = $backendUserProphecy->reveal();
244
245 $clipboardProphecy = $this->prophesize(Clipboard::class);
246 GeneralUtility::addInstance(Clipboard::class, $clipboardProphecy->reveal());
247 $clipboardProphecy->initializeClipboard()->shouldBeCalled();
248 $clipboardProphecy->elFromTable('aForeignTable')->shouldBeCalled()->willReturn([]);
249
250 /** @var RelationHandler|ObjectProphecy $relationHandlerProphecy */
251 $relationHandlerProphecy = $this->prophesize(RelationHandler::class);
252 GeneralUtility::addInstance(RelationHandler::class, $relationHandlerProphecy->reveal());
253 $relationHandlerProphecy->start('1,2', 'aForeignTable', 'mmTableName', 42, 'aTable', $aFieldConfig)->shouldBeCalled();
254 $relationHandlerProphecy->getFromDB()->shouldBeCalled();
255 $relationHandlerProphecy->getResolvedItemArray()->shouldBeCalled()->willReturn([
256 [
257 'table' => 'aForeignTable',
258 'uid' => 1,
259 ],
260 [
261 'table' => 'aForeignTable',
262 'uid' => 2,
263 ],
264 ]);
265
266 $expected = $input;
267 $expected['databaseRow']['aField'] = [
268 [
269 'table' => 'aForeignTable',
270 'uid' => null,
271 'title' => '',
272 'row' => null,
273 ],
274 [
275 'table' => 'aForeignTable',
276 'uid' => null,
277 'title' => '',
278 'row' => null,
279 ]
280 ];
281 $expected['processedTca']['columns']['aField']['config']['clipboardElements'] = [];
282
283 $this->assertSame($expected, (new TcaGroup)->addData($input));
284 }
285 }