[!!!][TASK] Move TYPO3_DB to extension
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Form / FormDataProvider / DatabaseRecordTypeValueTest.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 TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRecordTypeValue;
18
19 /**
20 * Test case
21 */
22 class DatabaseRecordTypeValueTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
23 {
24 /**
25 * @var DatabaseRecordTypeValue|\PHPUnit_Framework_MockObject_MockObject
26 */
27 protected $subject;
28
29 protected function setUp()
30 {
31 $this->subject = $this->getMockBuilder(DatabaseRecordTypeValue::class)
32 ->setMethods(['getDatabaseRow'])
33 ->getMock();
34 }
35
36 /**
37 * @test
38 */
39 public function addDataThrowsExceptionIfTcaTypesAreEmpty()
40 {
41 $input = [
42 'processedTca' => [
43 'types' => [],
44 ],
45 ];
46
47 $this->expectException(\UnexpectedValueException::class);
48 $this->expectExceptionCode(1438185331);
49
50 $this->subject->addData($input);
51 }
52
53 /**
54 * @test
55 */
56 public function addDataKeepsExistingTcaRecordTypeValue()
57 {
58 $input = [
59 'recordTypeValue' => 'egon',
60 'processedTca' => [
61 'types' => [
62 '1' => 'foo',
63 ],
64 ],
65 ];
66 $expected = $input;
67 $this->assertSame($expected, $this->subject->addData($input));
68 }
69
70 /**
71 * @test
72 */
73 public function addDataKeepsExistingTcaRecordTypeValueWithValueZero()
74 {
75 $input = [
76 'recordTypeValue' => 0,
77 'processedTca' => [
78 'types' => [
79 '1' => 'foo',
80 ],
81 ],
82 ];
83 $expected = $input;
84 $this->assertSame($expected, $this->subject->addData($input));
85 }
86
87 /**
88 * @test
89 */
90 public function addDataSetsRecordTypeValueToHistoricalOneIfTypeZeroIsNotDefined()
91 {
92 $input = [
93 'recordTypeValue' => '',
94 'processedTca' => [
95 'types' => [
96 '1' => 'foo',
97 ],
98 ],
99 ];
100 $expected = $input;
101 $expected['recordTypeValue'] = '1';
102 $this->assertSame($expected, $this->subject->addData($input));
103 }
104
105 /**
106 * @test
107 */
108 public function addDataSetsRecordTypeValueToZero()
109 {
110 $input = [
111 'recordTypeValue' => '',
112 'processedTca' => [
113 'types' => [
114 '0' => 'foo',
115 ],
116 ],
117 ];
118
119 $expected = $input;
120 $expected['recordTypeValue'] = '0';
121
122 $this->assertSame($expected, $this->subject->addData($input));
123 }
124
125 /**
126 * @test
127 */
128 public function addDataThrowsExceptionIfTypePointsToANotExistingField()
129 {
130 $input = [
131 'recordTypeValue' => '',
132 'processedTca' => [
133 'ctrl' => [
134 'type' => 'notExists',
135 ],
136 'types' => [
137 '0' => 'foo',
138 ],
139 ],
140 'databaseRow' => [
141 'uid' => 23,
142 ],
143 ];
144
145 $this->expectException(\UnexpectedValueException::class);
146 $this->expectExceptionCode(1438183881);
147
148 $this->subject->addData($input);
149 }
150
151 /**
152 * @test
153 */
154 public function addDataSetsRecordTypeValueToValueOfDatabaseField()
155 {
156 $input = [
157 'recordTypeValue' => '',
158 'processedTca' => [
159 'ctrl' => [
160 'type' => 'aField',
161 ],
162 'types' => [
163 '3' => 'foo',
164 ],
165 ],
166 'databaseRow' => [
167 'aField' => 3,
168 ],
169 ];
170
171 $expected = $input;
172 $expected['recordTypeValue'] = '3';
173
174 $this->assertSame($expected, $this->subject->addData($input));
175 }
176
177 /**
178 * @test
179 */
180 public function addDataSetsRecordTypeValueToZeroIfValueOfDatabaseFieldIsNotDefinedInTca()
181 {
182 $input = [
183 'recordTypeValue' => '',
184 'processedTca' => [
185 'ctrl' => [
186 'type' => 'aField',
187 ],
188 'types' => [
189 '0' => 'foo',
190 ],
191 ],
192 'databaseRow' => [
193 'aField' => 3,
194 ],
195 ];
196
197 $expected = $input;
198 $expected['recordTypeValue'] = '0';
199
200 $this->assertSame($expected, $this->subject->addData($input));
201 }
202
203 /**
204 * @test
205 */
206 public function addDataSetsRecordTypeValueToZeroIfValueOfDatabaseFieldIsEmptyString()
207 {
208 $input = [
209 'recordTypeValue' => '',
210 'processedTca' => [
211 'ctrl' => [
212 'type' => 'aField',
213 ],
214 'types' => [
215 '0' => 'foo',
216 ],
217 ],
218 'databaseRow' => [
219 'aField' => '',
220 ],
221 ];
222
223 $expected = $input;
224 $expected['recordTypeValue'] = '0';
225
226 $this->assertSame($expected, $this->subject->addData($input));
227 }
228
229 /**
230 * @test
231 */
232 public function addDataThrowsExceptionIfValueTypesNotExistsAndNoFallbackExists()
233 {
234 $input = [
235 'recordTypeValue' => '',
236 'processedTca' => [
237 'ctrl' => [
238 'type' => 'aField',
239 ],
240 'types' => [
241 '42' => 'foo',
242 ],
243 ],
244 'databaseRow' => [
245 'aField' => 23,
246 ],
247 ];
248
249 $this->expectException(\UnexpectedValueException::class);
250 $this->expectExceptionCode(1438185437);
251
252 $this->subject->addData($input);
253 }
254
255 /**
256 * @test
257 */
258 public function addDataThrowsExceptionForForeignTypeConfigurationNotAsSelectOrGroup()
259 {
260 $input = [
261 'recordTypeValue' => '',
262 'processedTca' => [
263 'ctrl' => [
264 'type' => 'localField:foreignField',
265 ],
266 'columns' => [
267 'localField' => [
268 'config' => [
269 'type' => 'input',
270 ],
271 ],
272 ],
273 'types' => [
274 '3' => 'foo',
275 ],
276 ],
277 ];
278
279 $this->expectException(\UnexpectedValueException::class);
280 $this->expectExceptionCode(1325862241);
281
282 $this->subject->addData($input);
283 }
284
285 /**
286 * @test
287 */
288 public function addDataThrowsExceptionForForeignTypeIfPointerConfigurationHasNoTable()
289 {
290 $input = [
291 'recordTypeValue' => '',
292 'processedTca' => [
293 'ctrl' => [
294 'type' => 'localField:foreignField',
295 ],
296 'columns' => [
297 'localField' => [
298 'config' => [
299 'type' => 'select',
300 ],
301 ],
302 ],
303 'types' => [
304 '3' => 'foo',
305 ],
306 ],
307 'databaseRow' => [
308 'localField' => 3,
309 ],
310 ];
311
312 $this->expectException(\UnexpectedValueException::class);
313 $this->expectExceptionCode(1438253614);
314
315 $this->subject->addData($input);
316 }
317
318 /**
319 * @test
320 */
321 public function addDataSetsTypeValueFromForeignTableRecord()
322 {
323 $input = [
324 'recordTypeValue' => '',
325 'processedTca' => [
326 'ctrl' => [
327 'type' => 'localField:foreignField',
328 ],
329 'columns' => [
330 'localField' => [
331 'config' => [
332 'type' => 'select',
333 'foreign_table' => 'foreignTable',
334 ],
335 ],
336 ],
337 'types' => [
338 '3' => 'foo',
339 ],
340 ],
341 'databaseRow' => [
342 // Point to record 42 in foreignTable
343 'localField' => 42,
344 ],
345 ];
346
347 $foreignRecordResult = [
348 'foreignField' => 3,
349 ];
350
351 $this->subject->expects($this->once())
352 ->method('getDatabaseRow')
353 ->with('foreignTable', 42, 'foreignField')
354 ->willReturn($foreignRecordResult);
355
356 $expected = $input;
357 $expected['recordTypeValue'] = '3';
358
359 $this->assertSame($expected, $this->subject->addData($input));
360 }
361
362 /**
363 * @test
364 */
365 public function addDataSetsTypeValueFromNestedTcaGroupField()
366 {
367 $input = [
368 'recordTypeValue' => '',
369 'processedTca' => [
370 'ctrl' => [
371 'type' => 'uid_local:type',
372 ],
373 'columns' => [
374 'uid_local' => [
375 'config' => [
376 'type' => 'group',
377 'internal_type' => 'db',
378 'size' => 1,
379 'maxitems' => 1,
380 'minitems' => 0,
381 'allowed' => 'sys_file'
382 ],
383 ],
384 ],
385 'types' => [
386 '2' => 'foo',
387 ],
388 ],
389 'databaseRow' => [
390 // Processed group field
391 'uid_local' => [
392 [
393 'uid' => 222,
394 ],
395 ],
396 ],
397 ];
398
399 $foreignRecordResult = [
400 'type' => 2,
401 ];
402
403 $this->subject->expects($this->once())
404 ->method('getDatabaseRow')
405 ->with('sys_file', 222, 'type')
406 ->willReturn($foreignRecordResult);
407
408 $expected = $input;
409 $expected['recordTypeValue'] = '2';
410
411 $this->assertSame($expected, $this->subject->addData($input));
412 }
413 }