[+BUGFIX] Extbase (Persistence): Fixed check for existence of a pid column in interme...
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Persistence / Mapper / DataMapFactory_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_DataMapFactory_testcase extends Tx_Extbase_BaseTestCase {
31
32 /**
33 * @test
34 */
35 public function setRelationsDetectsOneToOneRelationOfTypeSelect() {
36 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
37 $columnConfiguration = array(
38 'type' => 'select',
39 'foreign_table' => 'tx_myextension_bar',
40 'foreign_field' => 'parentid',
41 'foreign_table_field' => 'parenttable',
42 'maxitems' => '1'
43 );
44 $mockDataMapFactory = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMapFactory'), array('setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'), array(), '', FALSE);
45 $mockDataMapFactory->expects($this->once())->method('setOneToOneRelation');
46 $mockDataMapFactory->expects($this->never())->method('setOneToManyRelation');
47 $mockDataMapFactory->expects($this->never())->method('setManyToManyRelation');
48 $mockDataMapFactory->_callRef('setRelations', $mockColumnMap, $columnConfiguration);
49 }
50
51 /**
52 * @test
53 */
54 public function setRelationsDetectsOneToOneRelationOfTypeSelectWithDefaultMaxitems() {
55 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
56 $columnConfiguration = array(
57 'type' => 'select',
58 'foreign_table' => 'tx_myextension_bar',
59 'foreign_field' => 'parentid',
60 'foreign_table_field' => 'parenttable'
61 );
62 $mockDataMapFactory = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMapFactory'), array('setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'), array(), '', FALSE);
63 $mockDataMapFactory->expects($this->once())->method('setOneToOneRelation');
64 $mockDataMapFactory->expects($this->never())->method('setOneToManyRelation');
65 $mockDataMapFactory->expects($this->never())->method('setManyToManyRelation');
66 $mockDataMapFactory->_callRef('setRelations', $mockColumnMap, $columnConfiguration);
67 }
68
69 /**
70 * @test
71 */
72 public function setRelationsDetectsOneToOneRelationOfTypeInline() {
73 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
74 $columnConfiguration = array(
75 'type' => 'inline',
76 'foreign_table' => 'tx_myextension_bar',
77 'foreign_field' => 'parentid',
78 'foreign_table_field' => 'parenttable',
79 'maxitems' => '1'
80 );
81 $mockDataMapFactory = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMapFactory'), array('setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'), array(), '', FALSE);
82 $mockDataMapFactory->expects($this->once())->method('setOneToOneRelation');
83 $mockDataMapFactory->expects($this->never())->method('setOneToManyRelation');
84 $mockDataMapFactory->expects($this->never())->method('setManyToManyRelation');
85 $mockDataMapFactory->_callRef('setRelations', $mockColumnMap, $columnConfiguration);
86 }
87
88 /**
89 * @test
90 */
91 public function setRelationsDetectsOneToManyRelationOfTypeSelect() {
92 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
93 $columnConfiguration = array(
94 'type' => 'select',
95 'foreign_table' => 'tx_myextension_bar',
96 'foreign_field' => 'parentid',
97 'foreign_table_field' => 'parenttable',
98 'maxitems' => 9999
99 );
100 $mockDataMapFactory = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMapFactory'), array('setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'), array(), '', FALSE);
101 $mockDataMapFactory->expects($this->never())->method('setOneToOneRelation');
102 $mockDataMapFactory->expects($this->once())->method('setOneToManyRelation');
103 $mockDataMapFactory->expects($this->never())->method('setManyToManyRelation');
104 $mockDataMapFactory->_callRef('setRelations', $mockColumnMap, $columnConfiguration);
105 }
106
107 /**
108 * @test
109 */
110 public function setRelationsDetectsOneToManyRelationWitTypeInline() {
111 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
112 $columnConfiguration = array(
113 'type' => 'inline',
114 'foreign_table' => 'tx_myextension_bar',
115 'foreign_field' => 'parentid',
116 'foreign_table_field' => 'parenttable',
117 'maxitems' => 9999
118 );
119 $mockDataMapFactory = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMapFactory'), array('setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'), array(), '', FALSE);
120 $mockDataMapFactory->expects($this->never())->method('setOneToOneRelation');
121 $mockDataMapFactory->expects($this->once())->method('setOneToManyRelation');
122 $mockDataMapFactory->expects($this->never())->method('setManyToManyRelation');
123 $mockDataMapFactory->_callRef('setRelations', $mockColumnMap, $columnConfiguration);
124 }
125
126 /**
127 * @test
128 */
129 public function setRelationsDetectsManyToManyRelationOfTypeSelect() {
130 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
131 $columnConfiguration = array(
132 'type' => 'select',
133 'foreign_table' => 'tx_myextension_bar',
134 'MM' => 'tx_myextension_mm'
135 );
136 $mockDataMapFactory = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMapFactory'), array('setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'), array(), '', FALSE);
137 $mockDataMapFactory->expects($this->never())->method('setOneToOneRelation');
138 $mockDataMapFactory->expects($this->never())->method('setOneToManyRelation');
139 $mockDataMapFactory->expects($this->once())->method('setManyToManyRelation');
140 $mockDataMapFactory->_callRef('setRelations', $mockColumnMap, $columnConfiguration);
141 }
142
143 /**
144 * @test
145 */
146 public function setRelationsDetectsManyToManyRelationOfTypeInlineWithIntermediateTable() {
147 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
148 $columnConfiguration = array(
149 'type' => 'inline',
150 'foreign_table' => 'tx_myextension_righttable',
151 'MM' => 'tx_myextension_mm'
152 );
153 $mockDataMapFactory = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMapFactory'), array('setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'), array(), '', FALSE);
154 $mockDataMapFactory->expects($this->never())->method('setOneToOneRelation');
155 $mockDataMapFactory->expects($this->never())->method('setOneToManyRelation');
156 $mockDataMapFactory->expects($this->once())->method('setManyToManyRelation');
157 $mockDataMapFactory->_callRef('setRelations', $mockColumnMap, $columnConfiguration);
158 }
159
160 /**
161 * @test
162 */
163 public function setRelationsDetectsManyToManyRelationOfTypeInlineWithForeignSelector() {
164 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
165 $columnConfiguration = array(
166 'type' => 'inline',
167 'foreign_table' => 'tx_myextension_mm',
168 'foreign_field' => 'uid_local',
169 'foreign_selector' => 'uid_foreign'
170 );
171 $mockDataMapFactory = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMapFactory'), array('setOneToOneRelation', 'setOneToManyRelation', 'setManyToManyRelation'), array(), '', FALSE);
172 $mockDataMapFactory->expects($this->never())->method('setOneToOneRelation');
173 $mockDataMapFactory->expects($this->never())->method('setOneToManyRelation');
174 $mockDataMapFactory->expects($this->once())->method('setManyToManyRelation');
175 $mockDataMapFactory->_callRef('setRelations', $mockColumnMap, $columnConfiguration);
176 }
177
178 /**
179 * @test
180 */
181 public function columnMapIsInitializedWithManyToManyRelationOfTypeSelect() {
182 $leftColumnsDefinition = array(
183 'rights' => array(
184 'type' => 'select',
185 'foreign_table' => 'tx_myextension_righttable',
186 'foreign_table_where' => 'WHERE 1=1',
187 'MM' => 'tx_myextension_mm',
188 'MM_table_where' => 'WHERE 2=2',
189 ),
190 );
191 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
192 $mockColumnMap->expects($this->once())->method('setTypeOfRelation')->with($this->equalTo(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY));
193 $mockColumnMap->expects($this->once())->method('setRelationTableName')->with($this->equalTo('tx_myextension_mm'));
194 $mockColumnMap->expects($this->once())->method('setChildTableName')->with($this->equalTo('tx_myextension_righttable'));
195 $mockColumnMap->expects($this->once())->method('setChildTableWhereStatement')->with($this->equalTo('WHERE 1=1'));
196 $mockColumnMap->expects($this->once())->method('setChildSortbyFieldName')->with($this->equalTo('sorting'));
197 $mockColumnMap->expects($this->once())->method('setParentKeyFieldName')->with($this->equalTo('uid_local'));
198 $mockColumnMap->expects($this->never())->method('setParentTableFieldName');
199 $mockColumnMap->expects($this->never())->method('setRelationTableMatchFields');
200 $mockColumnMap->expects($this->never())->method('setRelationTableInsertFields');
201
202 $mockDataMapFactory = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMapFactory'), array('dummy'), array(), '', FALSE);
203 $mockDataMapFactory->_callRef('setManyToManyRelation', $mockColumnMap, $leftColumnsDefinition['rights']);
204 }
205
206 /**
207 * @test
208 */
209 public function columnMapIsInitializedWithOppositeManyToManyRelationOfTypeSelect() {
210 $rightColumnsDefinition = array(
211 'lefts' => array(
212 'type' => 'select',
213 'foreign_table' => 'tx_myextension_lefttable',
214 'MM' => 'tx_myextension_mm',
215 'MM_opposite_field' => 'rights'
216 ),
217 );
218 $leftColumnsDefinition['rights']['MM_opposite_field'] = 'opposite_field';
219 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
220 $mockColumnMap->expects($this->once())->method('setTypeOfRelation')->with($this->equalTo(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY));
221 $mockColumnMap->expects($this->once())->method('setRelationTableName')->with($this->equalTo('tx_myextension_mm'));
222 $mockColumnMap->expects($this->once())->method('setChildTableName')->with($this->equalTo('tx_myextension_lefttable'));
223 $mockColumnMap->expects($this->once())->method('setChildTableWhereStatement')->with(NULL);
224 $mockColumnMap->expects($this->once())->method('setChildSortbyFieldName')->with($this->equalTo('sorting_foreign'));
225 $mockColumnMap->expects($this->once())->method('setParentKeyFieldName')->with($this->equalTo('uid_foreign'));
226 $mockColumnMap->expects($this->never())->method('setParentTableFieldName');
227 $mockColumnMap->expects($this->never())->method('setRelationTableMatchFields');
228 $mockColumnMap->expects($this->never())->method('setRelationTableInsertFields');
229
230 $mockDataMapFactory = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMapFactory'), array('dummy'), array(), '', FALSE);
231 $mockDataMapFactory->_callRef('setManyToManyRelation', $mockColumnMap, $rightColumnsDefinition['lefts']);
232 }
233
234 /**
235 * @test
236 */
237 public function columnMapIsInitializedWithManyToManyRelationOfTypeInlineAndIntermediateTable() {
238 $leftColumnsDefinition = array(
239 'rights' => array(
240 'type' => 'inline',
241 'foreign_table' => 'tx_myextension_righttable',
242 'MM' => 'tx_myextension_mm',
243 'foreign_sortby' => 'sorting'
244 )
245 );
246 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
247 $mockColumnMap->expects($this->once())->method('setTypeOfRelation')->with($this->equalTo(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY));
248 $mockColumnMap->expects($this->once())->method('setRelationTableName')->with($this->equalTo('tx_myextension_mm'));
249 $mockColumnMap->expects($this->once())->method('setChildTableName')->with($this->equalTo('tx_myextension_righttable'));
250 $mockColumnMap->expects($this->once())->method('setChildTableWhereStatement');
251 $mockColumnMap->expects($this->once())->method('setChildSortbyFieldName')->with($this->equalTo('sorting'));
252 $mockColumnMap->expects($this->once())->method('setParentKeyFieldName')->with($this->equalTo('uid_local'));
253 $mockColumnMap->expects($this->never())->method('setParentTableFieldName');
254 $mockColumnMap->expects($this->never())->method('setRelationTableMatchFields');
255 $mockColumnMap->expects($this->never())->method('setRelationTableInsertFields');
256
257 $mockDataMapFactory = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMapFactory'), array('getColumnsDefinition'), array(), '', FALSE);
258 $mockDataMapFactory->expects($this->never())->method('getColumnsDefinition');
259 $mockDataMapFactory->_callRef('setManyToManyRelation', $mockColumnMap, $leftColumnsDefinition['rights']);
260 }
261
262 /**
263 * @test
264 */
265 public function columnMapIsInitializedWithManyToManyRelationOfTypeInlineAndForeignSelector() {
266 $leftColumnsDefinition = array(
267 'rights' => array(
268 'type' => 'inline',
269 'foreign_table' => 'tx_myextension_mm',
270 'foreign_field' => 'uid_local',
271 'foreign_selector' => 'uid_foreign',
272 'foreign_sortby' => 'sorting'
273 )
274 );
275 $relationTableColumnsDefiniton = array(
276 'uid_local' => array(
277 'config' => array('foreign_table' => 'tx_myextension_localtable')
278 ),
279 'uid_foreign' => array(
280 'config' => array('foreign_table' => 'tx_myextension_righttable')
281 )
282 );
283 $rightColumnsDefinition = array(
284 'lefts' => array(
285 'type' => 'inline',
286 'foreign_table' => 'tx_myextension_mm',
287 'foreign_field' => 'uid_foreign',
288 'foreign_selector' => 'uid_local',
289 'foreign_sortby' => 'sorting_foreign'
290 )
291 );
292 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
293 $mockColumnMap->expects($this->once())->method('setTypeOfRelation')->with($this->equalTo(Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY));
294 $mockColumnMap->expects($this->once())->method('setRelationTableName')->with($this->equalTo('tx_myextension_mm'));
295 $mockColumnMap->expects($this->once())->method('setChildTableName')->with($this->equalTo('tx_myextension_righttable'));
296 $mockColumnMap->expects($this->never())->method('setChildTableWhereStatement');
297 $mockColumnMap->expects($this->once())->method('setChildSortbyFieldName')->with($this->equalTo('sorting'));
298 $mockColumnMap->expects($this->once())->method('setParentKeyFieldName')->with($this->equalTo('uid_local'));
299 $mockColumnMap->expects($this->never())->method('setParentTableFieldName');
300 $mockColumnMap->expects($this->never())->method('setRelationTableMatchFields');
301 $mockColumnMap->expects($this->never())->method('setRelationTableInsertFields');
302
303 $mockDataMapFactory = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMapFactory'), array('getColumnsDefinition'), array(), '', FALSE);
304 $mockDataMapFactory->expects($this->once())->method('getColumnsDefinition')->with($this->equalTo('tx_myextension_mm'))->will($this->returnValue($relationTableColumnsDefiniton));
305 $mockDataMapFactory->_callRef('setManyToManyRelation', $mockColumnMap, $leftColumnsDefinition['rights']);
306 }
307
308 /**
309 * @test
310 */
311 public function columnMapIsInitializedWithManyToManyRelationWithoutPidColumn() {
312 $leftColumnsDefinition = array(
313 'rights' => array(
314 'type' => 'select',
315 'foreign_table' => 'tx_myextension_righttable',
316 'foreign_table_where' => 'WHERE 1=1',
317 'MM' => 'tx_myextension_mm',
318 ),
319 );
320 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
321 $mockColumnMap->expects($this->once())->method('setRelationTableName')->with($this->equalTo('tx_myextension_mm'));
322 $mockColumnMap->expects($this->once())->method('getRelationTableName')->will($this->returnValue('tx_myextension_mm'));
323 $mockColumnMap->expects($this->never())->method('setrelationTablePageIdColumnName');
324
325 $mockDataMapFactory = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMapFactory'), array('getControlSection'), array(), '', FALSE);
326 $mockDataMapFactory->expects($this->once())->method('getControlSection')->with($this->equalTo('tx_myextension_mm'))->will($this->returnValue(NULL));
327 $mockDataMapFactory->_callRef('setManyToManyRelation', $mockColumnMap, $leftColumnsDefinition['rights']);
328 }
329
330 /**
331 * @test
332 */
333 public function columnMapIsInitializedWithManyToManyRelationWithPidColumn() {
334 $leftColumnsDefinition = array(
335 'rights' => array(
336 'type' => 'select',
337 'foreign_table' => 'tx_myextension_righttable',
338 'foreign_table_where' => 'WHERE 1=1',
339 'MM' => 'tx_myextension_mm',
340 ),
341 );
342 $mockColumnMap = $this->getMock('Tx_Extbase_Persistence_Mapper_ColumnMap', array(), array(), '', FALSE);
343 $mockColumnMap->expects($this->once())->method('setRelationTableName')->with($this->equalTo('tx_myextension_mm'));
344 $mockColumnMap->expects($this->once())->method('getRelationTableName')->will($this->returnValue('tx_myextension_mm'));
345 $mockColumnMap->expects($this->once())->method('setrelationTablePageIdColumnName')->with($this->equalTo('pid'));
346
347 $mockDataMapFactory = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Mapper_DataMapFactory'), array('getControlSection'), array(), '', FALSE);
348 $mockDataMapFactory->expects($this->once())->method('getControlSection')->with($this->equalTo('tx_myextension_mm'))->will($this->returnValue(array('ctrl' => array('foo' => 'bar'))));
349 $mockDataMapFactory->_callRef('setManyToManyRelation', $mockColumnMap, $leftColumnsDefinition['rights']);
350 }
351
352 }
353 ?>