747ce3d29931e2597a40063b4df1cf624a23d315
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Persistence / Generic / Mapper / DataMapFactoryTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Mapper;
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\Core\DataHandling\TableColumnSubType;
18 use TYPO3\CMS\Core\DataHandling\TableColumnType;
19 use TYPO3\CMS\Extbase\Persistence\Generic\Exception\InvalidClassException;
20 use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap;
21 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
22
23 /**
24 * Test case
25 */
26 class DataMapFactoryTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
27 {
28 /**
29 * Subject is not notice free, disable E_NOTICES
30 */
31 protected static $suppressNotices = true;
32
33 /**
34 * @return array
35 */
36 public function oneToOneRelation()
37 {
38 return [
39 ['Tx_Myext_Domain_Model_Foo'],
40 [\TYPO3\CMS\Extbase\Domain\Model\FrontendUser::class]
41 ];
42 }
43
44 /**
45 * @test
46 * @dataProvider oneToOneRelation
47 */
48 public function setRelationsDetectsOneToOneRelation($className)
49 {
50 $mockColumnMap = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::class);
51 $columnConfiguration = [
52 'type' => 'select',
53 'foreign_table' => 'tx_myextension_bar',
54 'foreign_field' => 'parentid'
55 ];
56 $propertyMetaData = [
57 'type' => $className,
58 'elementType' => null
59 ];
60 $mockDataMapFactory = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory::class, ['setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'], [], '', false);
61 $mockDataMapFactory->expects($this->once())->method('setOneToOneRelation')->will($this->returnValue($mockColumnMap));
62 $mockDataMapFactory->expects($this->never())->method('setOneToManyRelation');
63 $mockDataMapFactory->expects($this->never())->method('setManyToManyRelation');
64 $mockDataMapFactory->_callRef('setRelations', $mockColumnMap, $columnConfiguration, $propertyMetaData);
65 }
66
67 /**
68 * @test
69 */
70 public function settingOneToOneRelationSetsRelationTableMatchFields()
71 {
72 $mockColumnMap = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::class);
73 $matchFields = [
74 'fieldname' => 'foo_model'
75 ];
76 $columnConfiguration = [
77 'type' => 'select',
78 'foreign_table' => 'tx_myextension_bar',
79 'foreign_field' => 'parentid',
80 'foreign_match_fields' => $matchFields
81 ];
82
83 $mockColumnMap->expects($this->once())
84 ->method('setRelationTableMatchFields')
85 ->with($matchFields);
86 $mockDataMapFactory = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory::class, ['dummy'], [], '', false);
87 $mockDataMapFactory->_call('setOneToOneRelation', $mockColumnMap, $columnConfiguration);
88 }
89
90 /**
91 * @test
92 */
93 public function settingOneToManyRelationSetsRelationTableMatchFields()
94 {
95 $mockColumnMap = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::class);
96 $matchFields = [
97 'fieldname' => 'foo_model'
98 ];
99 $columnConfiguration = [
100 'type' => 'select',
101 'foreign_table' => 'tx_myextension_bar',
102 'foreign_field' => 'parentid',
103 'foreign_match_fields' => $matchFields
104 ];
105
106 $mockColumnMap->expects($this->once())
107 ->method('setRelationTableMatchFields')
108 ->with($matchFields);
109 $mockDataMapFactory = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory::class, ['dummy'], [], '', false);
110 $mockDataMapFactory->_call('setOneToManyRelation', $mockColumnMap, $columnConfiguration);
111 }
112
113 /**
114 * @test
115 */
116 public function setRelationsDetectsOneToOneRelationWithIntermediateTable()
117 {
118 $mockColumnMap = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::class);
119 $columnConfiguration = [
120 'type' => 'select',
121 'foreign_table' => 'tx_myextension_bar',
122 'MM' => 'tx_myextension_mm'
123 ];
124 $propertyMetaData = [
125 'type' => 'Tx_Myext_Domain_Model_Foo',
126 'elementType' => null
127 ];
128 $mockDataMapFactory = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory::class, ['setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'], [], '', false);
129 $mockDataMapFactory->expects($this->never())->method('setOneToOneRelation');
130 $mockDataMapFactory->expects($this->never())->method('setOneToManyRelation');
131 $mockDataMapFactory->expects($this->once())->method('setManyToManyRelation')->will($this->returnValue($mockColumnMap));
132 $mockDataMapFactory->_callRef('setRelations', $mockColumnMap, $columnConfiguration, $propertyMetaData);
133 }
134
135 /**
136 * @test
137 */
138 public function setRelationsDetectsOneToManyRelation()
139 {
140 $mockColumnMap = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::class);
141 $columnConfiguration = [
142 'type' => 'select',
143 'foreign_table' => 'tx_myextension_bar',
144 'foreign_field' => 'parentid',
145 'foreign_table_field' => 'parenttable'
146 ];
147 $propertyMetaData = [
148 'type' => \TYPO3\CMS\Extbase\Persistence\ObjectStorage::class,
149 'elementType' => 'Tx_Myext_Domain_Model_Foo'
150 ];
151 $mockDataMapFactory = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory::class, ['setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'], [], '', false);
152 $mockDataMapFactory->expects($this->never())->method('setOneToOneRelation');
153 $mockDataMapFactory->expects($this->once())->method('setOneToManyRelation')->will($this->returnValue($mockColumnMap));
154 $mockDataMapFactory->expects($this->never())->method('setManyToManyRelation');
155 $mockDataMapFactory->_callRef('setRelations', $mockColumnMap, $columnConfiguration, $propertyMetaData);
156 }
157
158 /**
159 * @test
160 */
161 public function setRelationsDetectsSelectRenderTypeSingleAsNonRelational()
162 {
163 $columnMap = new \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap('foo', 'foo');
164 $columnConfiguration = [
165 'type' => 'select',
166 'renderType' => 'selectSingle',
167 'items' => [
168 ['One', 1],
169 ['Two', 2],
170 ['Three', 3],
171 ],
172 ];
173 $propertyMetaData = [];
174 $mockDataMapFactory = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory::class, ['setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'], [], '', false);
175 $mockDataMapFactory->expects($this->never())->method('setOneToOneRelation');
176 $mockDataMapFactory->expects($this->never())->method('setOneToManyRelation');
177 $mockDataMapFactory->expects($this->never())->method('setManyToManyRelation');
178 $actualColumnMap = $mockDataMapFactory->_callRef('setRelations', $columnMap, $columnConfiguration, $propertyMetaData);
179 $this->assertSame($columnMap::RELATION_NONE, $actualColumnMap->getTypeOfRelation());
180 }
181
182 /**
183 * @return array
184 */
185 public function columnConfigurationIsInitializedWithMaxItemsEvaluationForTypeGroupDataProvider()
186 {
187 return [
188 'maxitems not set' => ['', 'RELATION_HAS_MANY'],
189 'maxitems equals 1' => ['1', 'RELATION_NONE'],
190 'maxitems higher than 1' => ['10', 'RELATION_HAS_MANY']
191 ];
192 }
193
194 /**
195 * @test
196 *
197 * @dataProvider columnConfigurationIsInitializedWithMaxItemsEvaluationForTypeGroupDataProvider
198 */
199 public function setRelationsDetectsTypeGroupAndRelationManyToMany($maxitems, $relation)
200 {
201 $columnMap = new \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap('foo', 'foo');
202 if (empty($maxitems)) {
203 $columnConfiguration = [
204 'type' => 'group',
205 ];
206 } else {
207 $columnConfiguration = [
208 'type' => 'group',
209 'maxitems' => $maxitems
210 ];
211 }
212 $propertyMetaData = [];
213 $mockDataMapFactory = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory::class, ['setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'], [], '', false);
214 $mockDataMapFactory->expects($this->never())->method('setOneToOneRelation');
215 $mockDataMapFactory->expects($this->never())->method('setOneToManyRelation');
216 $mockDataMapFactory->expects($this->never())->method('setManyToManyRelation');
217 $actualColumnMap = $mockDataMapFactory->_callRef('setRelations', $columnMap, $columnConfiguration, $propertyMetaData);
218 $this->assertSame($relation, $actualColumnMap->getTypeOfRelation());
219 }
220
221 /**
222 * @test
223 */
224 public function setRelationsDetectsManyToManyRelationOfTypeSelect()
225 {
226 $mockColumnMap = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::class);
227 $columnConfiguration = [
228 'type' => 'select',
229 'foreign_table' => 'tx_myextension_bar',
230 'MM' => 'tx_myextension_mm'
231 ];
232 $propertyMetaData = [
233 'type' => \TYPO3\CMS\Extbase\Persistence\ObjectStorage::class,
234 'elementType' => 'Tx_Myext_Domain_Model_Foo'
235 ];
236 $mockDataMapFactory = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory::class, ['setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'], [], '', false);
237 $mockDataMapFactory->expects($this->never())->method('setOneToOneRelation');
238 $mockDataMapFactory->expects($this->never())->method('setOneToManyRelation');
239 $mockDataMapFactory->expects($this->once())->method('setManyToManyRelation')->will($this->returnValue($mockColumnMap));
240 $mockDataMapFactory->_callRef('setRelations', $mockColumnMap, $columnConfiguration, $propertyMetaData);
241 }
242
243 /**
244 * @test
245 */
246 public function setRelationsDetectsManyToManyRelationOfTypeInlineWithIntermediateTable()
247 {
248 $mockColumnMap = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::class);
249 $columnConfiguration = [
250 'type' => 'inline',
251 'foreign_table' => 'tx_myextension_righttable',
252 'MM' => 'tx_myextension_mm'
253 ];
254 $propertyMetaData = [
255 'type' => \TYPO3\CMS\Extbase\Persistence\ObjectStorage::class,
256 'elementType' => 'Tx_Myext_Domain_Model_Foo'
257 ];
258 $mockDataMapFactory = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory::class, ['setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'], [], '', false);
259 $mockDataMapFactory->expects($this->never())->method('setOneToOneRelation');
260 $mockDataMapFactory->expects($this->never())->method('setOneToManyRelation');
261 $mockDataMapFactory->expects($this->once())->method('setManyToManyRelation')->will($this->returnValue($mockColumnMap));
262 $mockDataMapFactory->_callRef('setRelations', $mockColumnMap, $columnConfiguration, $propertyMetaData);
263 }
264
265 /**
266 * @test
267 */
268 public function columnMapIsInitializedWithManyToManyRelationOfTypeSelect()
269 {
270 $leftColumnsDefinition = [
271 'rights' => [
272 'type' => 'select',
273 'foreign_table' => 'tx_myextension_righttable',
274 'foreign_table_where' => 'WHERE 1=1',
275 'MM' => 'tx_myextension_mm',
276 'MM_table_where' => 'WHERE 2=2'
277 ]
278 ];
279 $mockColumnMap = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::class);
280 $mockColumnMap->expects($this->once())->method('setTypeOfRelation')->with($this->equalTo(ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY));
281 $mockColumnMap->expects($this->once())->method('setRelationTableName')->with($this->equalTo('tx_myextension_mm'));
282 $mockColumnMap->expects($this->once())->method('setChildTableName')->with($this->equalTo('tx_myextension_righttable'));
283 $mockColumnMap->expects($this->once())->method('setChildTableWhereStatement')->with($this->equalTo('WHERE 1=1'));
284 $mockColumnMap->expects($this->once())->method('setChildSortByFieldName')->with($this->equalTo('sorting'));
285 $mockColumnMap->expects($this->once())->method('setParentKeyFieldName')->with($this->equalTo('uid_local'));
286 $mockColumnMap->expects($this->never())->method('setParentTableFieldName');
287 $mockColumnMap->expects($this->never())->method('setRelationTableMatchFields');
288 $mockColumnMap->expects($this->never())->method('setRelationTableInsertFields');
289 $mockDataMapFactory = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory::class, ['dummy'], [], '', false);
290 $mockDataMapFactory->_callRef('setManyToManyRelation', $mockColumnMap, $leftColumnsDefinition['rights']);
291 }
292
293 /**
294 * @test
295 */
296 public function columnMapIsInitializedWithOppositeManyToManyRelationOfTypeSelect()
297 {
298 $rightColumnsDefinition = [
299 'lefts' => [
300 'type' => 'select',
301 'foreign_table' => 'tx_myextension_lefttable',
302 'MM' => 'tx_myextension_mm',
303 'MM_opposite_field' => 'rights'
304 ]
305 ];
306 $leftColumnsDefinition['rights']['MM_opposite_field'] = 'opposite_field';
307 $mockColumnMap = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::class);
308 $mockColumnMap->expects($this->once())->method('setTypeOfRelation')->with($this->equalTo(ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY));
309 $mockColumnMap->expects($this->once())->method('setRelationTableName')->with($this->equalTo('tx_myextension_mm'));
310 $mockColumnMap->expects($this->once())->method('setChildTableName')->with($this->equalTo('tx_myextension_lefttable'));
311 $mockColumnMap->expects($this->once())->method('setChildTableWhereStatement')->with(null);
312 $mockColumnMap->expects($this->once())->method('setChildSortByFieldName')->with($this->equalTo('sorting_foreign'));
313 $mockColumnMap->expects($this->once())->method('setParentKeyFieldName')->with($this->equalTo('uid_foreign'));
314 $mockColumnMap->expects($this->never())->method('setParentTableFieldName');
315 $mockColumnMap->expects($this->never())->method('setRelationTableMatchFields');
316 $mockColumnMap->expects($this->never())->method('setRelationTableInsertFields');
317 $mockDataMapFactory = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory::class, ['dummy'], [], '', false);
318 $mockDataMapFactory->_callRef('setManyToManyRelation', $mockColumnMap, $rightColumnsDefinition['lefts']);
319 }
320
321 /**
322 * @test
323 */
324 public function columnMapIsInitializedWithManyToManyRelationOfTypeInlineAndIntermediateTable()
325 {
326 $leftColumnsDefinition = [
327 'rights' => [
328 'type' => 'inline',
329 'foreign_table' => 'tx_myextension_righttable',
330 'MM' => 'tx_myextension_mm',
331 'foreign_sortby' => 'sorting'
332 ]
333 ];
334 $mockColumnMap = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::class);
335 $mockColumnMap->expects($this->once())->method('setTypeOfRelation')->with($this->equalTo(ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY));
336 $mockColumnMap->expects($this->once())->method('setRelationTableName')->with($this->equalTo('tx_myextension_mm'));
337 $mockColumnMap->expects($this->once())->method('setChildTableName')->with($this->equalTo('tx_myextension_righttable'));
338 $mockColumnMap->expects($this->once())->method('setChildTableWhereStatement');
339 $mockColumnMap->expects($this->once())->method('setChildSortByFieldName')->with($this->equalTo('sorting'));
340 $mockColumnMap->expects($this->once())->method('setParentKeyFieldName')->with($this->equalTo('uid_local'));
341 $mockColumnMap->expects($this->never())->method('setParentTableFieldName');
342 $mockColumnMap->expects($this->never())->method('setRelationTableMatchFields');
343 $mockColumnMap->expects($this->never())->method('setRelationTableInsertFields');
344 $mockDataMapFactory = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory::class, ['getColumnsDefinition'], [], '', false);
345 $mockDataMapFactory->expects($this->never())->method('getColumnsDefinition');
346 $mockDataMapFactory->_callRef('setManyToManyRelation', $mockColumnMap, $leftColumnsDefinition['rights']);
347 }
348
349 /**
350 * @test
351 */
352 public function columnMapIsInitializedWithManyToManyRelationWithoutPidColumn()
353 {
354 $leftColumnsDefinition = [
355 'rights' => [
356 'type' => 'select',
357 'foreign_table' => 'tx_myextension_righttable',
358 'foreign_table_where' => 'WHERE 1=1',
359 'MM' => 'tx_myextension_mm'
360 ]
361 ];
362 $mockColumnMap = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::class);
363 $mockColumnMap->expects($this->once())->method('setRelationTableName')->with($this->equalTo('tx_myextension_mm'));
364 $mockColumnMap->expects($this->once())->method('getRelationTableName')->will($this->returnValue('tx_myextension_mm'));
365 $mockColumnMap->expects($this->never())->method('setrelationTablePageIdColumnName');
366 $mockDataMapFactory = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory::class, ['getControlSection'], [], '', false);
367 $mockDataMapFactory->expects($this->once())->method('getControlSection')->with($this->equalTo('tx_myextension_mm'))->will($this->returnValue(null));
368 $mockDataMapFactory->_callRef('setManyToManyRelation', $mockColumnMap, $leftColumnsDefinition['rights']);
369 }
370
371 /**
372 * @test
373 */
374 public function columnMapIsInitializedWithManyToManyRelationWithPidColumn()
375 {
376 $leftColumnsDefinition = [
377 'rights' => [
378 'type' => 'select',
379 'foreign_table' => 'tx_myextension_righttable',
380 'foreign_table_where' => 'WHERE 1=1',
381 'MM' => 'tx_myextension_mm'
382 ]
383 ];
384 $mockColumnMap = $this->createMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::class);
385 $mockColumnMap->expects($this->once())->method('setRelationTableName')->with($this->equalTo('tx_myextension_mm'));
386 $mockColumnMap->expects($this->once())->method('getRelationTableName')->will($this->returnValue('tx_myextension_mm'));
387 $mockColumnMap->expects($this->once())->method('setrelationTablePageIdColumnName')->with($this->equalTo('pid'));
388 $mockDataMapFactory = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory::class, ['getControlSection'], [], '', false);
389 $mockDataMapFactory->expects($this->once())->method('getControlSection')->with($this->equalTo('tx_myextension_mm'))->will($this->returnValue(['ctrl' => ['foo' => 'bar']]));
390 $mockDataMapFactory->_callRef('setManyToManyRelation', $mockColumnMap, $leftColumnsDefinition['rights']);
391 }
392
393 /**
394 * @return array
395 */
396 public function columnMapIsInitializedWithFieldEvaluationsForDateTimeFieldsDataProvider()
397 {
398 return [
399 'date field' => ['date', 'date'],
400 'datetime field' => ['datetime', 'datetime'],
401 'time field' => ['time', 'time'],
402 'no date/datetime/time field' => ['', null],
403 ];
404 }
405
406 /**
407 * @param string $type
408 * @param string|null $expectedValue
409 * @test
410 * @dataProvider columnMapIsInitializedWithFieldEvaluationsForDateTimeFieldsDataProvider
411 */
412 public function columnMapIsInitializedWithFieldEvaluationsForDateTimeFields($type, $expectedValue)
413 {
414 $columnDefinition = [
415 'type' => 'input',
416 'dbType' => $type,
417 'eval' => $type,
418 ];
419
420 $mockColumnMap = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::class)
421 ->setMethods(['setDateTimeStorageFormat'])
422 ->disableOriginalConstructor()
423 ->getMock();
424
425 if ($expectedValue !== null) {
426 $mockColumnMap->expects($this->once())->method('setDateTimeStorageFormat')->with($this->equalTo($type));
427 } else {
428 $mockColumnMap->expects($this->never())->method('setDateTimeStorageFormat');
429 }
430
431 $accessibleClassName = $this->buildAccessibleProxy(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory::class);
432 $accessibleDataMapFactory = new $accessibleClassName();
433 $accessibleDataMapFactory->_callRef('setFieldEvaluations', $mockColumnMap, $columnDefinition);
434 }
435
436 /**
437 * @test
438 */
439 public function buildDataMapThrowsExceptionIfClassNameIsNotKnown()
440 {
441 $this->expectException(InvalidClassException::class);
442 // @TODO expectExceptionCode is 0
443 $mockDataMapFactory = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory::class, ['getControlSection'], [], '', false);
444 $cacheMock = $this->getMockBuilder(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class)
445 ->setMethods(['get'])
446 ->disableOriginalConstructor()
447 ->getMock();
448 $cacheMock->expects($this->any())->method('get')->will($this->returnValue(false));
449 $mockDataMapFactory->_set('dataMapCache', $cacheMock);
450 $mockDataMapFactory->buildDataMap('UnknownObject');
451 }
452
453 /**
454 * @test
455 */
456 public function buildDataMapFetchesSubclassesRecursively()
457 {
458 $this->markTestSkipped('Incomplete mocking in a complex scenario. This should be a functional test');
459 $configuration = [
460 'persistence' => [
461 'classes' => [
462 \TYPO3\CMS\Extbase\Domain\Model\FrontendUser::class => [
463 'subclasses' => [
464 'Tx_SampleExt_Domain_Model_LevelOne1' => 'Tx_SampleExt_Domain_Model_LevelOne1',
465 'Tx_SampleExt_Domain_Model_LevelOne2' => 'Tx_SampleExt_Domain_Model_LevelOne2'
466 ]
467 ],
468 'Tx_SampleExt_Domain_Model_LevelOne1' => [
469 'subclasses' => [
470 'Tx_SampleExt_Domain_Model_LevelTwo1' => 'Tx_SampleExt_Domain_Model_LevelTwo1',
471 'Tx_SampleExt_Domain_Model_LevelTwo2' => 'Tx_SampleExt_Domain_Model_LevelTwo2'
472 ]
473 ],
474 'Tx_SampleExt_Domain_Model_LevelOne2' => [
475 'subclasses' => []
476 ]
477 ]
478 ]
479 ];
480 $expectedSubclasses = [
481 'Tx_SampleExt_Domain_Model_LevelOne1',
482 'Tx_SampleExt_Domain_Model_LevelTwo1',
483 'Tx_SampleExt_Domain_Model_LevelTwo2',
484 'Tx_SampleExt_Domain_Model_LevelOne2'
485 ];
486
487 /** @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject $objectManager */
488 $objectManager = $this->getMockBuilder(\TYPO3\CMS\Extbase\Object\ObjectManager::class)
489 ->setMethods(['dummy'])
490 ->disableOriginalConstructor()
491 ->getMock();
492
493 /** @var $configurationManager \TYPO3\CMS\Extbase\Configuration\ConfigurationManager|\PHPUnit_Framework_MockObject_MockObject */
494 $configurationManager = $this->createMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManager::class);
495 $configurationManager->expects($this->once())->method('getConfiguration')->with('Framework')->will($this->returnValue($configuration));
496 /** @var \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory $dataMapFactory */
497 $dataMapFactory = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory::class, ['test']);
498 $dataMapFactory->_set('reflectionService', new \TYPO3\CMS\Extbase\Reflection\ReflectionService());
499 $dataMapFactory->_set('objectManager', $objectManager);
500 $dataMapFactory->_set('configurationManager', $configurationManager);
501 $cacheMock = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class);
502 $cacheMock->expects($this->any())->method('get')->will($this->returnValue(false));
503 $dataMapFactory->_set('dataMapCache', $cacheMock);
504 $dataMap = $dataMapFactory->buildDataMap(\TYPO3\CMS\Extbase\Domain\Model\FrontendUser::class);
505 $this->assertSame($expectedSubclasses, $dataMap->getSubclasses());
506 }
507
508 /**
509 * @return array
510 */
511 public function classNameTableNameMappings()
512 {
513 return [
514 'Core classes' => [\TYPO3\CMS\Belog\Domain\Model\LogEntry::class, 'tx_belog_domain_model_logentry'],
515 'Core classes with namespaces and leading backslash' => [\TYPO3\CMS\Belog\Domain\Model\LogEntry::class, 'tx_belog_domain_model_logentry'],
516 'Extension classes' => ['ExtbaseTeam\\BlogExample\\Domain\\Model\\Blog', 'tx_blogexample_domain_model_blog'],
517 'Extension classes with namespaces and leading backslash' => ['\\ExtbaseTeam\\BlogExample\\Domain\\Model\\Blog', 'tx_blogexample_domain_model_blog'],
518 'Extension classes without namespace' => ['Tx_News_Domain_Model_News', 'tx_news_domain_model_news'],
519 'Extension classes without namespace but leading slash' => ['\\Tx_News_Domain_Model_News', 'tx_news_domain_model_news'],
520 ];
521 }
522
523 /**
524 * @test
525 * @dataProvider classNameTableNameMappings
526 */
527 public function resolveTableNameReturnsExpectedTablenames($className, $expected)
528 {
529 $dataMapFactory = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory::class, ['dummy']);
530 $this->assertSame($expected, $dataMapFactory->_call('resolveTableName', $className));
531 }
532
533 /**
534 * @test
535 */
536 public function createColumnMapReturnsAValidColumnMap()
537 {
538 /** @var $dataMapFactory \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory */
539 $dataMapFactory = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory::class, ['dummy']);
540
541 /** @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject $objectManager */
542 $objectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
543 $columnMap = $this->getMockBuilder(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::class)
544 ->setConstructorArgs(['column', 'property'])
545 ->getMock();
546 $objectManager->expects($this->once())->method('get')->will($this->returnValue($columnMap));
547
548 $dataMapFactory->_set('objectManager', $objectManager);
549
550 $this->assertEquals(
551 $columnMap,
552 $dataMapFactory->_call('createColumnMap', 'column', 'property')
553 );
554 }
555
556 /**
557 * @return array
558 */
559 public function tcaConfigurationsContainingTypeAndInternalType()
560 {
561 return [
562 [['type' => 'input'], TableColumnType::INPUT, null],
563 [['type' => 'text'], TableColumnType::TEXT, null],
564 [['type' => 'check'], TableColumnType::CHECK, null],
565 [['type' => 'radio'], TableColumnType::RADIO, null],
566 [['type' => 'select'], TableColumnType::SELECT, null],
567 [['type' => 'group', 'internal_type' => 'db'], TableColumnType::GROUP, TableColumnSubType::DB],
568 [['type' => 'group', 'internal_type' => 'file'], TableColumnType::GROUP, TableColumnSubType::FILE],
569 [['type' => 'group', 'internal_type' => 'file_reference'], TableColumnType::GROUP, TableColumnSubType::FILE_REFERENCE],
570 [['type' => 'group', 'internal_type' => 'folder'], TableColumnType::GROUP, TableColumnSubType::FOLDER],
571 [['type' => 'none'], TableColumnType::NONE, null],
572 [['type' => 'passthrough'], TableColumnType::PASSTHROUGH, null],
573 [['type' => 'user'], TableColumnType::USER, null],
574 [['type' => 'flex'], TableColumnType::FLEX, null],
575 [['type' => 'inline'], TableColumnType::INLINE, null],
576 ];
577 }
578
579 /**
580 * @test
581 * @dataProvider tcaConfigurationsContainingTypeAndInternalType
582 *
583 * @param array $columnConfiguration
584 * @param string $type
585 * @param string $internalType
586 */
587 public function setTypeDetectsTypeAndInternalTypeProperly(array $columnConfiguration, $type, $internalType)
588 {
589 /** @var $dataMapFactory \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory | AccessibleObjectInterface */
590 $dataMapFactory = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory::class, ['dummy']);
591
592 /** @var ColumnMap $columnMap */
593 $columnMap = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::class, ['dummy'], [], '', false);
594
595 $dataMapFactory->_call('setType', $columnMap, $columnConfiguration);
596
597 $this->assertEquals($type, (string)$columnMap->getType());
598 $this->assertEquals($internalType, (string)$columnMap->getInternalType());
599 }
600 }