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