[~BUGFIX] Extbase (Persistence): Fixed some broken MM settings for some IRRE setups...
[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 setRelationsDetectsManyToManyRelationOfTypeInline() {
282 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
283 $columnConfiguration = array(
284 'type' => 'inline',
285 'foreign_table' => 'tx_myextension_mm',
286 'foreign_field' => 'uid_local',
287 'foreign_label' => 'uid_foreign'
288 );
289 $mockDataMap = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMap'), array('setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'), array(), '', FALSE);
290 $mockDataMap->expects($this->never())->method('setOneToOneRelation');
291 $mockDataMap->expects($this->never())->method('setOneToManyRelation');
292 $mockDataMap->expects($this->once())->method('setManyToManyRelation');
293 $mockDataMap->_callRef('setRelations', $mockColumnMap, $columnConfiguration);
294 }
295
296 /**
297 * @test
298 */
299 public function columnMapIsInitializedWithManyToManyRelationOfTypeSelect() {
300 $leftColumnsDefinition = array(
301 'rights' => array(
302 'type' => 'select',
303 'foreign_table' => 'tx_myextension_righttable',
304 'foreign_table_where' => 'WHERE 1=1',
305 'MM' => 'tx_myextension_mm',
306 'MM_table_where' => 'WHERE 2=2',
307 ),
308 );
309 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
310 $mockColumnMap->expects($this->once())->method('setTypeOfRelation')->with($this->equalTo(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY));
311 $mockColumnMap->expects($this->once())->method('setChildTableName')->with($this->equalTo('tx_myextension_righttable'));
312 $mockColumnMap->expects($this->once())->method('setChildTableWhereStatement')->with($this->equalTo('WHERE 1=1'));
313 $mockColumnMap->expects($this->once())->method('setChildSortbyFieldName')->with($this->equalTo('sorting'));
314 $mockColumnMap->expects($this->once())->method('setParentKeyFieldName')->with($this->equalTo('uid_local'));
315 $mockColumnMap->expects($this->never())->method('setParentTableFieldName');
316 $mockColumnMap->expects($this->never())->method('setRelationTableMatchFields');
317 $mockColumnMap->expects($this->never())->method('setRelationTableInsertFields');
318
319 $mockDataMap = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMap'), array('dummy'), array(), '', FALSE);
320 $mockDataMap->_callRef('setManyToManyRelation', $mockColumnMap, $leftColumnsDefinition['rights']);
321 }
322
323 /**
324 * @test
325 */
326 public function columnMapIsInitializedWithOppositeManyToManyRelationOfTypeSelect() {
327 $rightColumnsDefinition = array(
328 'lefts' => array(
329 'type' => 'select',
330 'foreign_table' => 'tx_myextension_lefttable',
331 'MM' => 'tx_myextension_mm',
332 'MM_opposite_field' => 'rights'
333 ),
334 );
335 $leftColumnsDefinition['rights']['MM_opposite_field'] = 'opposite_field';
336 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
337 $mockColumnMap->expects($this->once())->method('setTypeOfRelation')->with($this->equalTo(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY));
338 $mockColumnMap->expects($this->once())->method('setChildTableName')->with($this->equalTo('tx_myextension_lefttable'));
339 $mockColumnMap->expects($this->once())->method('setChildTableWhereStatement')->with(NULL);
340 $mockColumnMap->expects($this->once())->method('setChildSortbyFieldName')->with($this->equalTo('sorting_foreign'));
341 $mockColumnMap->expects($this->once())->method('setParentKeyFieldName')->with($this->equalTo('uid_foreign'));
342 $mockColumnMap->expects($this->never())->method('setParentTableFieldName');
343 $mockColumnMap->expects($this->never())->method('setRelationTableMatchFields');
344 $mockColumnMap->expects($this->never())->method('setRelationTableInsertFields');
345
346 $mockDataMap = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMap'), array('dummy'), array(), '', FALSE);
347 $mockDataMap->_callRef('setManyToManyRelation', $mockColumnMap, $rightColumnsDefinition['lefts']);
348 }
349
350 /**
351 * @test
352 */
353 public function uid_foreignIsReturnedAsDefaultForChildKeyFieldName() {
354 $leftColumnsDefinition = array(
355 'rights' => array(
356 'type' => 'inline',
357 'foreign_table' => 'tx_myextension_mm',
358 'foreign_field' => 'uid_local',
359 )
360 );
361 $mockDataMap = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMap'), array('dummy'), array(), '', FALSE);
362 $result = $mockDataMap->_callRef('determineChildKeyFieldName', $leftColumnsDefinition['rights']);
363 $this->assertEquals('uid_foreign', $result);
364 }
365
366 /**
367 * @test
368 */
369 public function theChildKeyFieldNameIsReturnedForForeignLabel() {
370 $leftColumnsDefinition = array(
371 'rights' => array(
372 'type' => 'inline',
373 'foreign_table' => 'tx_myextension_mm',
374 'foreign_field' => 'uid_local',
375 'foreign_label' => 'the_child_key_field_name',
376 )
377 );
378 $mockDataMap = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMap'), array('dummy'), array(), '', FALSE);
379 $result = $mockDataMap->_callRef('determineChildKeyFieldName', $leftColumnsDefinition['rights']);
380 $this->assertEquals('the_child_key_field_name', $result);
381 }
382
383 /**
384 * @test
385 */
386 public function theChildKeyFieldNameIsReturnedForForeignSelector() {
387 $leftColumnsDefinition = array(
388 'rights' => array(
389 'type' => 'inline',
390 'foreign_table' => 'tx_myextension_mm',
391 'foreign_field' => 'uid_local',
392 'foreign_selector' => 'the_child_key_field_name',
393 )
394 );
395 $mockDataMap = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMap'), array('dummy'), array(), '', FALSE);
396 $result = $mockDataMap->_callRef('determineChildKeyFieldName', $leftColumnsDefinition['rights']);
397 $this->assertEquals('the_child_key_field_name', $result);
398 }
399
400 /**
401 * @test
402 */
403 public function columnMapIsInitializedWithManyToManyRelationOfTypeInline() {
404 $leftColumnsDefinition = array(
405 'rights' => array(
406 'type' => 'inline',
407 'foreign_table' => 'tx_myextension_mm',
408 'foreign_field' => 'uid_local',
409 'foreign_label' => 'uid_foreign',
410 'foreign_sortby' => 'sorting'
411 )
412 );
413 $relationTableColumnsDefiniton = array(
414 'uid_local' => array(
415 'config' => array('foreign_table' => 'tx_myextension_localtable')
416 ),
417 'uid_foreign' => array(
418 'config' => array('foreign_table' => 'tx_myextension_righttable')
419 )
420 );
421 $rightColumnsDefinition = array(
422 'lefts' => array(
423 'type' => 'inline',
424 'foreign_table' => 'tx_myextension_mm',
425 'foreign_field' => 'uid_foreign',
426 'foreign_label' => 'uid_local',
427 'foreign_sortby' => 'sorting_foreign'
428 )
429 );
430 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
431 $mockColumnMap->expects($this->once())->method('setTypeOfRelation')->with($this->equalTo(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY));
432 $mockColumnMap->expects($this->once())->method('setChildTableName')->with($this->equalTo('tx_myextension_righttable'));
433 $mockColumnMap->expects($this->never())->method('setChildTableWhereStatement');
434 $mockColumnMap->expects($this->once())->method('setChildSortbyFieldName')->with($this->equalTo('sorting'));
435 $mockColumnMap->expects($this->once())->method('setParentKeyFieldName')->with($this->equalTo('uid_local'));
436 $mockColumnMap->expects($this->never())->method('setParentTableFieldName');
437 $mockColumnMap->expects($this->never())->method('setRelationTableMatchFields');
438 $mockColumnMap->expects($this->never())->method('setRelationTableInsertFields');
439
440 $mockDataMap = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMap'), array('getColumnsDefinition', 'determineChildClassName'), array(), '', FALSE);
441 $mockDataMap->expects($this->once())->method('getColumnsDefinition')->with($this->equalTo('tx_myextension_mm'))->will($this->returnValue($relationTableColumnsDefiniton));
442 $mockDataMap->_callRef('setManyToManyRelation', $mockColumnMap, $leftColumnsDefinition['rights']);
443 }
444
445 }
446 ?>