[BUGFIX] Initialize uploadfolder key for TCA group file_reference
[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\TestingFramework\Core\Unit\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 addDataSetsUploadFolderForFileReference()
101 {
102 $input = [
103 'databaseRow' => [
104 'aField' => '',
105 ],
106 'processedTca' => [
107 'columns' => [
108 'aField' => [
109 'config' => [
110 'type' => 'group',
111 'internal_type' => 'file_reference',
112 'maxitems' => 99999,
113 ],
114 ],
115 ],
116 ],
117 ];
118
119 $expected = $input;
120 $expected['databaseRow']['aField'] = [];
121 $expected['processedTca']['columns']['aField']['config']['allowed'] = '*';
122 $expected['processedTca']['columns']['aField']['config']['clipboardElements'] = [];
123 $expected['processedTca']['columns']['aField']['config']['uploadfolder'] = '';
124
125 $clipboardProphecy = $this->prophesize(Clipboard::class);
126 GeneralUtility::addInstance(Clipboard::class, $clipboardProphecy->reveal());
127 $clipboardProphecy->initializeClipboard()->shouldBeCalled();
128 $clipboardProphecy->elFromTable('_FILE')->shouldBeCalled()->willReturn([]);
129
130 $this->assertEquals($expected, $this->subject->addData($input));
131 }
132
133 /**
134 * @test
135 */
136 public function addDataSetsFileData()
137 {
138 $input = [
139 'databaseRow' => [
140 'aField' => '/aDir/aFile.txt,/anotherDir/anotherFile.css',
141 ],
142 'processedTca' => [
143 'columns' => [
144 'aField' => [
145 'config' => [
146 'type' => 'group',
147 'internal_type' => 'file',
148 'maxitems' => 99999,
149 ],
150 ],
151 ],
152 ],
153 ];
154
155 $clipboardProphecy = $this->prophesize(Clipboard::class);
156 GeneralUtility::addInstance(Clipboard::class, $clipboardProphecy->reveal());
157 $clipboardProphecy->initializeClipboard()->shouldBeCalled();
158 $clipboardProphecy->elFromTable('_FILE')->shouldBeCalled()->willReturn([]);
159
160 $expected = $input;
161 $expected['databaseRow']['aField'] = [
162 [
163 'uidOrPath' => '/aDir/aFile.txt',
164 'title' => '/aDir/aFile.txt',
165 ],
166 [
167 'uidOrPath' => '/anotherDir/anotherFile.css',
168 'title' => '/anotherDir/anotherFile.css',
169 ],
170 ];
171 $expected['processedTca']['columns']['aField']['config']['clipboardElements'] = [];
172 $expected['processedTca']['columns']['aField']['config']['allowed'] = '*';
173 $this->assertEquals($expected, $this->subject->addData($input));
174 }
175
176 /**
177 * @test
178 */
179 public function addDataSetsFolderData()
180 {
181 $input = [
182 'databaseRow' => [
183 'aField' => '1:/aFolder/anotherFolder/',
184 ],
185 'processedTca' => [
186 'columns' => [
187 'aField' => [
188 'config' => [
189 'type' => 'group',
190 'internal_type' => 'folder',
191 'maxitems' => 99999,
192 ],
193 ],
194 ],
195 ],
196 ];
197
198 /** @var Folder|ObjectProphecy $relationHandlerProphecy */
199 $folderProphecy = $this->prophesize(Folder::class);
200
201 /** @var ResourceFactory|ObjectProphecy $relationHandlerProphecy */
202 $resourceFactoryProphecy = $this->prophesize(ResourceFactory::class);
203 GeneralUtility::setSingletonInstance(ResourceFactory::class, $resourceFactoryProphecy->reveal());
204 $resourceFactoryProphecy->retrieveFileOrFolderObject('1:/aFolder/anotherFolder/')
205 ->shouldBeCalled()
206 ->willReturn($folderProphecy->reveal());
207
208 $expected = $input;
209 $expected['databaseRow']['aField'] = [
210 [
211 'folder' => '1:/aFolder/anotherFolder/',
212 ]
213 ];
214 $expected['processedTca']['columns']['aField']['config']['clipboardElements'] = [];
215 $this->assertSame($expected, $this->subject->addData($input));
216 }
217
218 /**
219 * @test
220 */
221 public function addDataSetsDatabaseData()
222 {
223 $aFieldConfig = [
224 'type' => 'group',
225 'internal_type' => 'db',
226 'MM' => 'mmTableName',
227 'allowed' => 'aForeignTable',
228 'maxitems' => 99999,
229 ];
230 $input = [
231 'tableName' => 'aTable',
232 'databaseRow' => [
233 'uid' => 42,
234 'aField' => '1,2',
235 ],
236 'processedTca' => [
237 'columns' => [
238 'aField' => [
239 'config' => $aFieldConfig,
240 ],
241 ],
242 ],
243 ];
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, $this->subject->addData($input));
284 }
285 }