[+BUGFIX] Extbase (Persistence): Fixed broken IRRE setups. The type of relation is...
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Persistence / Mapper / DataMap_testcase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 require_once(PATH_tslib . 'class.tslib_content.php');
29
30 class Tx_Extbase_Persistence_Mapper_DataMap_testcase extends Tx_Extbase_BaseTestCase {
31
32 public function setUp() {
33 require_once(t3lib_extMgm::extPath('blog_example') . 'Classes/Domain/Model/Blog.php');
34
35 $GLOBALS['TSFE']->fe_user = $this->getMock('tslib_feUserAuth');
36 $GLOBALS['TSFE'] = $this->getMock('tslib_fe', array('includeTCA'), array(), '', FALSE);
37 $this->setupTca();
38 $GLOBALS['TSFE']->expects($this->any())
39 ->method('includeTCA')
40 ->will($this->returnValue(NULL));
41
42
43 $GLOBALS['TSFE']->fe_user->user['uid'] = 999;
44 $GLOBALS['TSFE']->id = 42;
45 }
46
47 public function setupTCA() {
48 global $TCA;
49 global $_EXTKEY;
50 $TCA['tx_blogexample_domain_model_blog'] = array (
51 'ctrl' => array (
52 'title' => 'LLL:EXT:blog_example/Resources/Language/locallang_db.xml:tx_blogexample_domain_model_blog',
53 'label' => 'name',
54 'tstamp' => 'tstamp',
55 'prependAtCopy' => 'LLL:EXT:lang/locallang_general.xml:LGL.prependAtCopy',
56 'delete' => 'deleted',
57 'enablecolumns' => array (
58 'disabled' => 'hidden'
59 ),
60 'iconfile' => t3lib_extMgm::extRelPath($_EXTKEY).'Resources/Icons/icon_tx_blogexample_domain_model_blog.gif'
61 ),
62 'interface' => array(
63 'showRecordFieldList' => 'hidden, name, description, logo, posts'
64 ),
65 'columns' => array(
66 'hidden' => array(
67 'exclude' => 1,
68 'label' => 'LLL:EXT:lang/locallang_general.xml:LGL.hidden',
69 'config' => array(
70 'type' => 'check'
71 )
72 ),
73 'name' => array(
74 'exclude' => 0,
75 'label' => 'LLL:EXT:blog_example/Resources/Language/locallang_db.xml:tx_blogexample_domain_model_blog.name',
76 'config' => array(
77 'type' => 'input',
78 'size' => 20,
79 'eval' => 'trim,required',
80 'max' => 256
81 )
82 ),
83 'description' => array(
84 'exclude' => 1,
85 'label' => 'LLL:EXT:blog_example/Resources/Language/locallang_db.xml:tx_blogexample_domain_model_blog.description',
86 'config' => array(
87 'type' => 'text',
88 'eval' => 'required',
89 'rows' => 30,
90 'cols' => 80,
91 )
92 ),
93 'logo' => array(
94 'exclude' => 1,
95 'label' => 'LLL:EXT:blog_example/Resources/Language/locallang_db.xml:tx_blogexample_domain_model_blog.logo',
96 'config' => array(
97 'type' => 'group',
98 'internal_type' => 'file',
99 'allowed' => $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'],
100 'max_size' => 3000,
101 'uploadfolder' => 'uploads/pics',
102 'show_thumbs' => 1,
103 'size' => 1,
104 'maxitems' => 1,
105 'minitems' => 0
106 )
107 ),
108 'posts' => array(
109 'exclude' => 1,
110 'label' => 'LLL:EXT:blog_example/Resources/Language/locallang_db.xml:tx_blogexample_domain_model_blog.posts',
111 'config' => array(
112 'type' => 'inline',
113 'foreign_class' => 'Tx_BlogExample_Domain_Model_Post',
114 'foreign_table' => 'tx_blogexample_domain_model_post',
115 'foreign_field' => 'blog',
116 'foreign_table_field' => 'blog_table',
117 'appearance' => array(
118 'newRecordLinkPosition' => 'bottom',
119 'collapseAll' => 1,
120 'expandSingle' => 1,
121 ),
122 )
123 ),
124 'author' => array(
125 'exclude' => 1,
126 'label' => 'LLL:EXT:blog_example/Resources/Language/locallang_db.xml:tx_blogexample_domain_model_blog.author',
127 'config' => array(
128 'type' => 'select',
129 'foreign_class' => 'Tx_BlogExample_Domain_Model_Author',
130 'foreign_table' => 'tx_blogexample_domain_model_author',
131 'maxitems' => 1,
132 )
133 ),
134 ),
135 'types' => array(
136 '1' => array('showitem' => 'hidden, name, description, logo, posts')
137 ),
138 'palettes' => array(
139 '1' => array('showitem' => '')
140 )
141 );
142 }
143
144 // public function test_DataMapCanBeInitialized() {
145 // $dataMap = new Tx_Extbase_Persistence_Mapper_DataMap('Tx_BlogExample_Domain_Model_Blog');
146 // $columnMaps = $dataMap->getColumnMaps();
147 // $this->assertEquals(10, count($columnMaps), 'The data map was not initialized (wrong number of column maps set).');
148 // }
149 //
150 // public function test_DeletedColumnNameCanBeResolved() {
151 // $dataMap = new Tx_Extbase_Persistence_Mapper_DataMap('Tx_BlogExample_Domain_Model_Blog');
152 // $deletedColumnName = $dataMap->getDeletedColumnName();
153 // $this->assertEquals($deletedColumnName, 'deleted', 'The deleted column name could not be resolved.');
154 // }
155 //
156 // public function test_HiddenColumnNameCanBeResolved() {
157 // $dataMap = new Tx_Extbase_Persistence_Mapper_DataMap('Tx_BlogExample_Domain_Model_Blog');
158 // $hiddenColumnName = $dataMap->getHiddenColumnName();
159 // $this->assertEquals($hiddenColumnName, 'hidden', 'The hidden column name could not be resolved.');
160 // }
161 //
162 // public function test_ColumnCanBeAdded() {
163 // $dataMap = new Tx_Extbase_Persistence_Mapper_DataMap('Tx_BlogExample_Domain_Model_Blog');
164 // $dataMap->addColumn('test_column');
165 // $columnMaps = $dataMap->getColumnMaps();
166 // $columnMap = array_pop($columnMaps);
167 // $this->assertType('Tx_Extbase_Persistence_Mapper_ColumnMap', $columnMap, 'The column could not be added.');
168 // }
169 //
170 // public function test_PersistablePropertyCanBeChecked() {
171 // $dataMap = new Tx_Extbase_Persistence_Mapper_DataMap('Tx_BlogExample_Domain_Model_Blog');
172 // $dataMap->addColumn('configured_property');
173 // $this->assertTrue($dataMap->isPersistableProperty('configuredProperty'), 'The persistable property was marked as unpersistable.');
174 // $this->assertFalse($dataMap->isPersistableProperty('unconfiguredProperty'), 'The unpersistable property was marked asersistable.');
175 // }
176 //
177 // public function test_HasManyColumnIsRegisteredForForeignTable() {
178 // $dataMap = new Tx_Extbase_Persistence_Mapper_DataMap('Tx_BlogExample_Domain_Model_Blog');
179 // $this->assertEquals(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_MANY, $dataMap->getColumnMap('posts')->getTypeOfRelation(), 'The posts relation was not of type HAS_MANY.');
180 // }
181 //
182 // public function test_HasOneColumnIsRegisteredForForeignTableWithMaxsizeOne() {
183 // $dataMap = new Tx_Extbase_Persistence_Mapper_DataMap('Tx_BlogExample_Domain_Model_Blog');
184 // $this->assertEquals(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_ONE, $dataMap->getColumnMap('author')->getTypeOfRelation(), 'The author relation was not of type HAS_ONE.');
185 // }
186
187 /**
188 * @test
189 */
190 public function setRelationsDetectsOneToOneRelationOfTypeSelect() {
191 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
192 $columnConfiguration = array(
193 'type' => 'select',
194 'foreign_table' => 'tx_myextension_bar',
195 'foreign_field' => 'parentid',
196 'foreign_table_field' => 'parenttable',
197 'maxitems' => '1'
198 );
199 $mockDataMap = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMap'), array('setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'), array(), '', FALSE);
200 $mockDataMap->expects($this->once())->method('setOneToOneRelation');
201 $mockDataMap->expects($this->never())->method('setOneToManyRelation');
202 $mockDataMap->expects($this->never())->method('setManyToManyRelation');
203 $mockDataMap->_callRef('setRelations', $mockColumnMap, $columnConfiguration);
204 }
205
206 /**
207 * @test
208 */
209 public function setRelationsDetectsOneToOneRelationOfTypeInline() {
210 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
211 $columnConfiguration = array(
212 'type' => 'inline',
213 'foreign_table' => 'tx_myextension_bar',
214 'foreign_field' => 'parentid',
215 'foreign_table_field' => 'parenttable',
216 'maxitems' => '1'
217 );
218 $mockDataMap = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMap'), array('setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'), array(), '', FALSE);
219 $mockDataMap->expects($this->once())->method('setOneToOneRelation');
220 $mockDataMap->expects($this->never())->method('setOneToManyRelation');
221 $mockDataMap->expects($this->never())->method('setManyToManyRelation');
222 $mockDataMap->_callRef('setRelations', $mockColumnMap, $columnConfiguration);
223 }
224
225 /**
226 * @test
227 */
228 public function setRelationsDetectsOneToManyRelationOfTypeSelect() {
229 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
230 $columnConfiguration = array(
231 'type' => 'select',
232 'foreign_table' => 'tx_myextension_bar',
233 'foreign_field' => 'parentid',
234 'foreign_table_field' => 'parenttable'
235 );
236 $mockDataMap = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMap'), array('setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'), array(), '', FALSE);
237 $mockDataMap->expects($this->never())->method('setOneToOneRelation');
238 $mockDataMap->expects($this->once())->method('setOneToManyRelation');
239 $mockDataMap->expects($this->never())->method('setManyToManyRelation');
240 $mockDataMap->_callRef('setRelations', $mockColumnMap, $columnConfiguration);
241 }
242
243 /**
244 * @test
245 */
246 public function setRelationsDetectsOneToManyRelationWitTypeInline() {
247 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
248 $columnConfiguration = array(
249 'type' => 'inline',
250 'foreign_table' => 'tx_myextension_bar',
251 'foreign_field' => 'parentid',
252 'foreign_table_field' => 'parenttable'
253 );
254 $mockDataMap = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMap'), array('setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'), array(), '', FALSE);
255 $mockDataMap->expects($this->never())->method('setOneToOneRelation');
256 $mockDataMap->expects($this->once())->method('setOneToManyRelation');
257 $mockDataMap->expects($this->never())->method('setManyToManyRelation');
258 $mockDataMap->_callRef('setRelations', $mockColumnMap, $columnConfiguration);
259 }
260
261 /**
262 * @test
263 */
264 public function setRelationsDetectsManyToManyRelationOfTypeSelect() {
265 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
266 $columnConfiguration = array(
267 'type' => 'select',
268 'foreign_table' => 'tx_myextension_bar',
269 'MM' => 'tx_myextension_mm'
270 );
271 $mockDataMap = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMap'), array('setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'), array(), '', FALSE);
272 $mockDataMap->expects($this->never())->method('setOneToOneRelation');
273 $mockDataMap->expects($this->never())->method('setOneToManyRelation');
274 $mockDataMap->expects($this->once())->method('setManyToManyRelation');
275 $mockDataMap->_callRef('setRelations', $mockColumnMap, $columnConfiguration);
276 }
277
278 /**
279 * @test
280 */
281 public function setRelationsDetectsManyToManyRelationOfTypeInlineWithIntermediateTable() {
282 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
283 $columnConfiguration = array(
284 'type' => 'inline',
285 'foreign_table' => 'tx_myextension_righttable',
286 'MM' => 'tx_myextension_mm'
287 );
288 $mockDataMap = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMap'), array('setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'), array(), '', FALSE);
289 $mockDataMap->expects($this->never())->method('setOneToOneRelation');
290 $mockDataMap->expects($this->never())->method('setOneToManyRelation');
291 $mockDataMap->expects($this->once())->method('setManyToManyRelation');
292 $mockDataMap->_callRef('setRelations', $mockColumnMap, $columnConfiguration);
293 }
294
295 /**
296 * @test
297 */
298 public function setRelationsDetectsManyToManyRelationOfTypeInlineWithForeignSelector() {
299 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
300 $columnConfiguration = array(
301 'type' => 'inline',
302 'foreign_table' => 'tx_myextension_mm',
303 'foreign_field' => 'uid_local',
304 'foreign_selector' => 'uid_foreign'
305 );
306 $mockDataMap = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMap'), array('setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'), array(), '', FALSE);
307 $mockDataMap->expects($this->never())->method('setOneToOneRelation');
308 $mockDataMap->expects($this->never())->method('setOneToManyRelation');
309 $mockDataMap->expects($this->once())->method('setManyToManyRelation');
310 $mockDataMap->_callRef('setRelations', $mockColumnMap, $columnConfiguration);
311 }
312
313 /**
314 * @test
315 */
316 public function columnMapIsInitializedWithManyToManyRelationOfTypeSelect() {
317 $leftColumnsDefinition = array(
318 'rights' => array(
319 'type' => 'select',
320 'foreign_table' => 'tx_myextension_righttable',
321 'foreign_table_where' => 'WHERE 1=1',
322 'MM' => 'tx_myextension_mm',
323 'MM_table_where' => 'WHERE 2=2',
324 ),
325 );
326 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
327 $mockColumnMap->expects($this->once())->method('setTypeOfRelation')->with($this->equalTo(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY));
328 $mockColumnMap->expects($this->once())->method('setChildTableName')->with($this->equalTo('tx_myextension_righttable'));
329 $mockColumnMap->expects($this->once())->method('setChildTableWhereStatement')->with($this->equalTo('WHERE 1=1'));
330 $mockColumnMap->expects($this->once())->method('setChildSortbyFieldName')->with($this->equalTo('sorting'));
331 $mockColumnMap->expects($this->once())->method('setParentKeyFieldName')->with($this->equalTo('uid_local'));
332 $mockColumnMap->expects($this->never())->method('setParentTableFieldName');
333 $mockColumnMap->expects($this->never())->method('setRelationTableMatchFields');
334 $mockColumnMap->expects($this->never())->method('setRelationTableInsertFields');
335
336 $mockDataMap = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMap'), array('dummy'), array(), '', FALSE);
337 $mockDataMap->_callRef('setManyToManyRelation', $mockColumnMap, $leftColumnsDefinition['rights']);
338 }
339
340 /**
341 * @test
342 */
343 public function columnMapIsInitializedWithOppositeManyToManyRelationOfTypeSelect() {
344 $rightColumnsDefinition = array(
345 'lefts' => array(
346 'type' => 'select',
347 'foreign_table' => 'tx_myextension_lefttable',
348 'MM' => 'tx_myextension_mm',
349 'MM_opposite_field' => 'rights'
350 ),
351 );
352 $leftColumnsDefinition['rights']['MM_opposite_field'] = 'opposite_field';
353 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
354 $mockColumnMap->expects($this->once())->method('setTypeOfRelation')->with($this->equalTo(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY));
355 $mockColumnMap->expects($this->once())->method('setChildTableName')->with($this->equalTo('tx_myextension_lefttable'));
356 $mockColumnMap->expects($this->once())->method('setChildTableWhereStatement')->with(NULL);
357 $mockColumnMap->expects($this->once())->method('setChildSortbyFieldName')->with($this->equalTo('sorting_foreign'));
358 $mockColumnMap->expects($this->once())->method('setParentKeyFieldName')->with($this->equalTo('uid_foreign'));
359 $mockColumnMap->expects($this->never())->method('setParentTableFieldName');
360 $mockColumnMap->expects($this->never())->method('setRelationTableMatchFields');
361 $mockColumnMap->expects($this->never())->method('setRelationTableInsertFields');
362
363 $mockDataMap = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMap'), array('dummy'), array(), '', FALSE);
364 $mockDataMap->_callRef('setManyToManyRelation', $mockColumnMap, $rightColumnsDefinition['lefts']);
365 }
366
367 /**
368 * @test
369 */
370 public function columnMapIsInitializedWithManyToManyRelationOfTypeInlineAndIntermediateTable() {
371 $leftColumnsDefinition = array(
372 'rights' => array(
373 'type' => 'inline',
374 'foreign_table' => 'tx_myextension_righttable',
375 'MM' => 'tx_myextension_mm',
376 'foreign_sortby' => 'sorting'
377 )
378 );
379 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
380 $mockColumnMap->expects($this->once())->method('setTypeOfRelation')->with($this->equalTo(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY));
381 $mockColumnMap->expects($this->once())->method('setChildTableName')->with($this->equalTo('tx_myextension_righttable'));
382 $mockColumnMap->expects($this->once())->method('setChildTableWhereStatement');
383 $mockColumnMap->expects($this->once())->method('setChildSortbyFieldName')->with($this->equalTo('sorting'));
384 $mockColumnMap->expects($this->once())->method('setParentKeyFieldName')->with($this->equalTo('uid_local'));
385 $mockColumnMap->expects($this->never())->method('setParentTableFieldName');
386 $mockColumnMap->expects($this->never())->method('setRelationTableMatchFields');
387 $mockColumnMap->expects($this->never())->method('setRelationTableInsertFields');
388
389 $mockDataMap = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMap'), array('getColumnsDefinition'), array(), '', FALSE);
390 $mockDataMap->expects($this->never())->method('getColumnsDefinition');
391 $mockDataMap->_callRef('setManyToManyRelation', $mockColumnMap, $leftColumnsDefinition['rights']);
392 }
393
394 /**
395 * @test
396 */
397 public function columnMapIsInitializedWithManyToManyRelationOfTypeInlineAndForeignSelector() {
398 $leftColumnsDefinition = array(
399 'rights' => array(
400 'type' => 'inline',
401 'foreign_table' => 'tx_myextension_mm',
402 'foreign_field' => 'uid_local',
403 'foreign_selector' => 'uid_foreign',
404 'foreign_sortby' => 'sorting'
405 )
406 );
407 $relationTableColumnsDefiniton = array(
408 'uid_local' => array(
409 'config' => array('foreign_table' => 'tx_myextension_localtable')
410 ),
411 'uid_foreign' => array(
412 'config' => array('foreign_table' => 'tx_myextension_righttable')
413 )
414 );
415 $rightColumnsDefinition = array(
416 'lefts' => array(
417 'type' => 'inline',
418 'foreign_table' => 'tx_myextension_mm',
419 'foreign_field' => 'uid_foreign',
420 'foreign_selector' => 'uid_local',
421 'foreign_sortby' => 'sorting_foreign'
422 )
423 );
424 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
425 $mockColumnMap->expects($this->once())->method('setTypeOfRelation')->with($this->equalTo(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY));
426 $mockColumnMap->expects($this->once())->method('setChildTableName')->with($this->equalTo('tx_myextension_righttable'));
427 $mockColumnMap->expects($this->never())->method('setChildTableWhereStatement');
428 $mockColumnMap->expects($this->once())->method('setChildSortbyFieldName')->with($this->equalTo('sorting'));
429 $mockColumnMap->expects($this->once())->method('setParentKeyFieldName')->with($this->equalTo('uid_local'));
430 $mockColumnMap->expects($this->never())->method('setParentTableFieldName');
431 $mockColumnMap->expects($this->never())->method('setRelationTableMatchFields');
432 $mockColumnMap->expects($this->never())->method('setRelationTableInsertFields');
433
434 $mockDataMap = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMap'), array('getColumnsDefinition'), array(), '', FALSE);
435 $mockDataMap->expects($this->once())->method('getColumnsDefinition')->with($this->equalTo('tx_myextension_mm'))->will($this->returnValue($relationTableColumnsDefiniton));
436 $mockDataMap->_callRef('setManyToManyRelation', $mockColumnMap, $leftColumnsDefinition['rights']);
437 }
438
439 }
440 ?>