[~CONFIGURATION] Extbase: Set lockType to empty string instead of 'system'.
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Persistence / Storage / Typo3DbBackend_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 class Tx_Extbase_Persistence_Storage_Typo3DbBackend_testcase extends Tx_Extbase_BaseTestCase {
29
30 /**
31 * This is the data provider for the statement generation with a basic comparison
32 *
33 * @return array An array of data
34 */
35 public function providerForBasicComparison() {
36 return array(
37 'equal' => array(
38 Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_EQUAL_TO,
39 "SELECT table_name_from_selector.* FROM table_name_from_selector WHERE table_name_from_property.foo = 'baz'"
40 ),
41 'less' => array(
42 Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_LESS_THAN,
43 "SELECT table_name_from_selector.* FROM table_name_from_selector WHERE table_name_from_property.foo < 'baz'"
44 ),
45 'less or equal' => array(
46 Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_LESS_THAN_OR_EQUAL_TO,
47 "SELECT table_name_from_selector.* FROM table_name_from_selector WHERE table_name_from_property.foo <= 'baz'"
48 ),
49 'greater' => array(
50 Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_GREATER_THAN,
51 "SELECT table_name_from_selector.* FROM table_name_from_selector WHERE table_name_from_property.foo > 'baz'"
52 ),
53 'greater or equal' => array(
54 Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_OPERATOR_GREATER_THAN_OR_EQUAL_TO,
55 "SELECT table_name_from_selector.* FROM table_name_from_selector WHERE table_name_from_property.foo >= 'baz'"
56 ),
57
58 );
59 }
60
61 /**
62 * @test
63 */
64 public function parseQueryWorksWithMinimalisticQueryObjectModel() {
65 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
66 $mockSource->expects($this->any())->method('getSelectorName')->will($this->returnValue('selector_name'));
67 $mockSource->expects($this->any())->method('getNodeTypeName')->will($this->returnValue('nodetype_name'));
68
69 $mockQueryObjectModel = $this->getMock('Tx_Extbase_Persistence_QOM_QueryObjectModel', array(), array(), '', FALSE);
70 $mockQueryObjectModel->expects($this->any())->method('getSource')->will($this->returnValue($mockSource));
71 $mockQueryObjectModel->expects($this->any())->method('getBoundVariableValues')->will($this->returnValue(array()));
72 $mockQueryObjectModel->expects($this->any())->method('getOrderings')->will($this->returnValue(array()));
73
74
75 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parseOrderings'), array(), '', FALSE);
76 $mockTypo3DbBackend->expects($this->any())->method('parseOrderings');
77
78 $resultingStatement = $mockTypo3DbBackend->parseQuery($mockQueryObjectModel);
79 $expectedStatement = 'SELECT selector_name.* FROM selector_name';
80 $this->assertEquals($expectedStatement, $resultingStatement);
81 }
82
83 /**
84 * @test
85 */
86 public function parseQueryWorksWithBasicEqualsCondition() {
87 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
88 $mockSource->expects($this->any())->method('getSelectorName')->will($this->returnValue('selector_name'));
89 $mockSource->expects($this->any())->method('getNodeTypeName')->will($this->returnValue('nodetype_name'));
90
91 $mockQueryObjectModel = $this->getMock('Tx_Extbase_Persistence_QOM_QueryObjectModel', array(), array(), '', FALSE);
92 $mockQueryObjectModel->expects($this->any())->method('getSource')->will($this->returnValue($mockSource));
93 $mockQueryObjectModel->expects($this->any())->method('getBoundVariableValues')->will($this->returnValue(array()));
94 $mockQueryObjectModel->expects($this->any())->method('getOrderings')->will($this->returnValue(array()));
95
96
97 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parseOrderings'), array(), '', FALSE);
98 $mockTypo3DbBackend->expects($this->any())->method('parseOrderings');
99
100 $resultingStatement = $mockTypo3DbBackend->parseQuery($mockQueryObjectModel);
101 $expectedStatement = 'SELECT selector_name.* FROM selector_name';
102 $this->assertEquals($expectedStatement, $resultingStatement);
103 }
104
105 /**
106 * @test
107 */
108 public function joinStatementGenerationWorks() {
109 $mockLeftSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
110 $mockLeftSource->expects($this->any())->method('getSelectorName')->will($this->returnValue('left_selector_name'));
111 $mockLeftSource->expects($this->any())->method('getNodeTypeName')->will($this->returnValue('left_nodetype_name'));
112
113 $mockRightSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
114 $mockRightSource->expects($this->any())->method('getSelectorName')->will($this->returnValue('right_selector_name'));
115 $mockRightSource->expects($this->any())->method('getNodeTypeName')->will($this->returnValue('right_nodetype_name'));
116
117 $mockJoinCondition = $this->getMock('Tx_Extbase_Persistence_QOM_EquiJoinCondition', array('getSelector1Name', 'getSelector2Name', 'getProperty1Name', 'getProperty2Name'), array(), '', FALSE);
118 $mockJoinCondition->expects($this->any())->method('getSelector1Name')->will($this->returnValue('first_selector'));
119 $mockJoinCondition->expects($this->any())->method('getSelector2Name')->will($this->returnValue('second_selector'));
120 $mockJoinCondition->expects($this->any())->method('getProperty1Name')->will($this->returnValue('firstProperty'));
121 $mockJoinCondition->expects($this->any())->method('getProperty2Name')->will($this->returnValue('secondProperty'));
122
123 $mockJoin = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_QOM_Join'), array('getLeft', 'getRight'), array(), '', FALSE);
124 $mockJoin->_set('joinCondition', $mockJoinCondition);
125 $mockJoin->_set('joinType', $mockJoinCondition);
126 $mockJoin->expects($this->any())->method('getLeft')->will($this->returnValue($mockLeftSource));
127 $mockJoin->expects($this->any())->method('getRight')->will($this->returnValue($mockRightSource));
128
129 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName'), array(), '', FALSE);
130 $mockDataMapper->expects($this->any())
131 ->method('convertPropertyNameToColumnName')
132 ->with(
133 $this->logicalOr(
134 $this->equalTo('firstProperty'),
135 $this->equalTo('secondProperty')
136 )
137 )
138 ->will($this->returnValue('resulting_fieldname'));
139
140 $sql = array();
141 $parameters = array();
142 $mockQueryObjectModel = $this->getMock('Tx_Extbase_Persistence_QOM_QueryObjectModelInterface');
143 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserJoin'), array(), '', FALSE);
144 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
145 $mockTypo3DbBackend->_callRef('parseJoin', $mockQueryObjectModel, $mockJoin, $sql, $parameters);
146
147 $expecedSql = array(
148 'fields' => array('left_selector_name.*', 'right_selector_name.*'),
149 'tables' => array(
150 'left_selector_name LEFT JOIN right_selector_name',
151 'ON first_selector.resulting_fieldname = second_selector.resulting_fieldname'
152 )
153 );
154
155 $this->assertEquals($expecedSql, $sql);
156 }
157
158 /**
159 * @test
160 */
161 public function orderStatementGenerationWorks() {
162 $mockPropertyValue = $this->getMock('Tx_Extbase_Persistence_QOM_PropertyValue', array('getPropertyName', 'getSelectorname'), array(), '', FALSE);
163 $mockPropertyValue->expects($this->once())->method('getPropertyName')->will($this->returnValue('fooProperty'));
164 $mockPropertyValue->expects($this->once())->method('getSelectorName')->will($this->returnValue('tx_myext_bar'));
165
166 $mockOrdering1 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_QOM_Ordering'), array('getOrder', 'getOperand'), array(), '', FALSE);
167 $mockOrdering1->expects($this->once())->method('getOrder')->will($this->returnValue(Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_ORDER_ASCENDING));
168 $mockOrdering1->expects($this->once())->method('getOperand')->will($this->returnValue($mockPropertyValue));
169 $orderings = array($mockOrdering1);
170
171 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
172
173 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName'), array(), '', FALSE);
174 $mockDataMapper->expects($this->once())->method('convertPropertyNameToColumnName')->with('fooProperty', 'tx_myext_bar')->will($this->returnValue('bar_field'));
175
176 $sql = array();
177 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserOrderings'), array(), '', FALSE);
178 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
179 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
180
181 $expecedSql = array('orderings' => array('tx_myext_bar.bar_field ASC'));
182 $this->assertEquals($expecedSql, $sql);
183 }
184
185 /**
186 * @test
187 * @expectedException Tx_Extbase_Persistence_Exception_UnsupportedOrder
188 */
189 public function orderStatementGenerationThrowsExceptionOnUnsupportedOrder() {
190 $mockPropertyValue = $this->getMock('Tx_Extbase_Persistence_QOM_PropertyValue', array('getPropertyName', 'getSelectorname'), array(), '', FALSE);
191 $mockPropertyValue->expects($this->never())->method('getPropertyName');
192 $mockPropertyValue->expects($this->never())->method('getSelectorName');
193
194 $mockOrdering1 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_QOM_Ordering'), array('getOrder', 'getOperand'), array(), '', FALSE);
195 $mockOrdering1->expects($this->once())->method('getOrder')->will($this->returnValue('unsupported_order'));
196 $mockOrdering1->expects($this->once())->method('getOperand')->will($this->returnValue($mockPropertyValue));
197 $orderings = array($mockOrdering1);
198
199 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
200
201 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName'), array(), '', FALSE);
202 $mockDataMapper->expects($this->never())->method('convertPropertyNameToColumnName');
203
204 $sql = array();
205 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserOrderings'), array(), '', FALSE);
206 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
207 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
208 }
209
210 /**
211 * @test
212 */
213 public function orderStatementGenerationWorksWithMultipleOrderings() {
214 $mockPropertyValue1 = $this->getMock('Tx_Extbase_Persistence_QOM_PropertyValue', array('getPropertyName', 'getSelectorname'), array(), '', FALSE);
215 $mockPropertyValue1->expects($this->atLeastOnce())->method('getPropertyName')->will($this->returnValue('fooProperty'));
216 $mockPropertyValue1->expects($this->atLeastOnce())->method('getSelectorName')->will($this->returnValue('tx_myext_bar'));
217
218 $mockPropertyValue2 = $this->getMock('Tx_Extbase_Persistence_QOM_PropertyValue', array('getPropertyName', 'getSelectorname'), array(), '', FALSE);
219 $mockPropertyValue2->expects($this->atLeastOnce())->method('getPropertyName')->will($this->returnValue('barProperty'));
220 $mockPropertyValue2->expects($this->atLeastOnce())->method('getSelectorName')->will($this->returnValue('tx_myext_blub'));
221
222 $mockOrdering1 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_QOM_Ordering'), array('getOrder', 'getOperand'), array(), '', FALSE);
223 $mockOrdering1->expects($this->once())->method('getOrder')->will($this->returnValue(Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_ORDER_ASCENDING));
224 $mockOrdering1->expects($this->once())->method('getOperand')->will($this->returnValue($mockPropertyValue1));
225 $mockOrdering2 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_QOM_Ordering'), array('getOrder', 'getOperand'), array(), '', FALSE);
226 $mockOrdering2->expects($this->once())->method('getOrder')->will($this->returnValue(Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_ORDER_DESCENDING));
227 $mockOrdering2->expects($this->once())->method('getOperand')->will($this->returnValue($mockPropertyValue2));
228 $orderings = array($mockOrdering1, $mockOrdering2);
229
230 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
231
232 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName'), array(), '', FALSE);
233 $mockDataMapper->expects($this->atLeastOnce())->method('convertPropertyNameToColumnName')->will($this->returnValue('foo_field'));
234
235 $sql = array();
236 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserOrderings'), array(), '', FALSE);
237 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
238 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
239
240 $expecedSql = array('orderings' => array('tx_myext_bar.foo_field ASC', 'tx_myext_blub.foo_field DESC'));
241 $this->assertEquals($expecedSql, $sql);
242 }
243
244 /**
245 * @test
246 */
247 public function orderStatementGenerationWorksWithDescendingOrder() {
248 $mockPropertyValue = $this->getMock('Tx_Extbase_Persistence_QOM_PropertyValue', array('getPropertyName', 'getSelectorname'), array(), '', FALSE);
249 $mockPropertyValue->expects($this->once())->method('getPropertyName')->will($this->returnValue('fooProperty'));
250 $mockPropertyValue->expects($this->once())->method('getSelectorName')->will($this->returnValue(''));
251
252 $mockOrdering1 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_QOM_Ordering'), array('getOrder', 'getOperand'), array(), '', FALSE);
253 $mockOrdering1->expects($this->once())->method('getOrder')->will($this->returnValue(Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_ORDER_DESCENDING));
254 $mockOrdering1->expects($this->once())->method('getOperand')->will($this->returnValue($mockPropertyValue));
255 $orderings = array($mockOrdering1);
256
257 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
258
259 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName'), array(), '', FALSE);
260 $mockDataMapper->expects($this->once())->method('convertPropertyNameToColumnName')->with('fooProperty', '')->will($this->returnValue('bar_property'));
261
262 $sql = array();
263 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserOrderings'), array(), '', FALSE);
264 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
265 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
266
267 $expecedSql = array('orderings' => array('bar_property DESC'));
268 $this->assertEquals($expecedSql, $sql);
269 }
270
271 /**
272 * @test
273 */
274 public function orderStatementGenerationWorksWithTheSourceSelectorNameIfNotSpecifiedInThePropertyValue() {
275 $mockPropertyValue = $this->getMock('Tx_Extbase_Persistence_QOM_PropertyValue', array('getPropertyName', 'getSelectorname'), array(), '', FALSE);
276 $mockPropertyValue->expects($this->once())->method('getPropertyName')->will($this->returnValue('fooProperty'));
277 $mockPropertyValue->expects($this->once())->method('getSelectorName')->will($this->returnValue(''));
278
279 $mockOrdering1 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_QOM_Ordering'), array('getOrder', 'getOperand'), array(), '', FALSE);
280 $mockOrdering1->expects($this->once())->method('getOrder')->will($this->returnValue(Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_ORDER_ASCENDING));
281 $mockOrdering1->expects($this->once())->method('getOperand')->will($this->returnValue($mockPropertyValue));
282 $orderings = array($mockOrdering1);
283
284 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array(), array(), '', FALSE);
285
286 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName'), array(), '', FALSE);
287 $mockDataMapper->expects($this->once())->method('convertPropertyNameToColumnName')->with('fooProperty', '')->will($this->returnValue('bar_property'));
288
289 $sql = array();
290 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserOrderings'), array(), '', FALSE);
291 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
292 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
293
294 $expecedSql = array('orderings' => array('bar_property ASC'));
295 $this->assertEquals($expecedSql, $sql);
296 }
297
298 }
299 ?>