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