[FEATURE] Improved translation handling for statement generation
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Persistence / Storage / Typo3DbBackendTest.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_Tests_Unit_Persistence_Storage_Typo3DbBackendTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
29
30 /**
31 * Enable backup of global and system variables
32 *
33 * @var boolean
34 */
35 protected $backupGlobals = true;
36
37 /**
38 * Exclude TYPO3_DB from backup/ restore of $GLOBALS
39 * because resource types cannot be handled during serializing
40 *
41 * @var array
42 */
43 protected $backupGlobalsBlacklist = array('TYPO3_DB');
44
45 /**
46 * This is the data provider for the statement generation with a basic comparison
47 *
48 * @return array An array of data
49 */
50 public function providerForBasicComparison() {
51 return array(
52 'equal' => array(
53 Tx_Extbase_Persistence_QueryInterface::OPERATOR_EQUAL_TO,
54 "SELECT table_name_from_selector.* FROM table_name_from_selector WHERE table_name_from_property.foo = 'baz'"
55 ),
56 'less' => array(
57 Tx_Extbase_Persistence_QueryInterface::OPERATOR_LESS_THAN,
58 "SELECT table_name_from_selector.* FROM table_name_from_selector WHERE table_name_from_property.foo < 'baz'"
59 ),
60 'less or equal' => array(
61 Tx_Extbase_Persistence_QueryInterface::OPERATOR_LESS_THAN_OR_EQUAL_TO,
62 "SELECT table_name_from_selector.* FROM table_name_from_selector WHERE table_name_from_property.foo <= 'baz'"
63 ),
64 'greater' => array(
65 Tx_Extbase_Persistence_QueryInterface::OPERATOR_GREATER_THAN,
66 "SELECT table_name_from_selector.* FROM table_name_from_selector WHERE table_name_from_property.foo > 'baz'"
67 ),
68 'greater or equal' => array(
69 Tx_Extbase_Persistence_QueryInterface::OPERATOR_GREATER_THAN_OR_EQUAL_TO,
70 "SELECT table_name_from_selector.* FROM table_name_from_selector WHERE table_name_from_property.foo >= 'baz'"
71 ),
72
73 );
74 }
75
76 /**
77 * @test
78 */
79 public function getStatementWorksWithMinimalisticQueryObjectModel() {
80 $this->markTestIncomplete();
81 }
82
83 /**
84 * @test
85 */
86 public function getStatementWorksWithBasicEqualsCondition() {
87 $this->markTestIncomplete();
88 }
89
90 /**
91 * @test
92 * @expectedException Tx_Extbase_Persistence_Storage_Exception_BadConstraint
93 */
94 public function countRowsWithStatementConstraintResultsInAnException() {
95 $this->markTestIncomplete();
96 }
97
98 /**
99 * @test
100 */
101 public function joinStatementGenerationWorks() {
102 $this->markTestIncomplete();
103 }
104
105 /**
106 * @test
107 */
108 public function addSysLanguageStatementWorksForDefaultLanguage() {
109 $table = uniqid('tx_coretest_table');
110 $GLOBALS['TCA'][$table]['ctrl'] = array(
111 'languageField' => 'sys_language_uid'
112 );
113
114 $tsfe = $this->getMock('tslib_fe', array(), array(), '', false);
115 $tsfe->sys_language_uid = 0;
116 $GLOBALS['TSFE'] = $tsfe;
117
118 $sql = array();
119 $mockTypo3DbBackend = $this->getMock(
120 $this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'),
121 array('dummy'),
122 array(),
123 '',
124 false);
125 $mockTypo3DbBackend->_callRef('addSysLanguageStatement', $table, $sql);
126
127 $expectedSql = array('additionalWhereClause' => array('(' . $table . '.sys_language_uid IN (0,-1))'));
128
129 $this->assertSame($expectedSql, $sql);
130 }
131
132 /**
133 * @test
134 */
135 public function addSysLanguageStatementWorksForDefaultLanguageWithoutDeleteStatementReturned() {
136 $table = uniqid('tx_coretest_table');
137 $GLOBALS['TCA'][$table]['ctrl'] = array(
138 'languageField' => 'sys_language_uid',
139 'delete' => 'deleted'
140 );
141
142 $tsfe = $this->getMock('tslib_fe', array(), array(), '', false);
143 $tsfe->sys_language_uid = 0;
144 $GLOBALS['TSFE'] = $tsfe;
145
146 $sql = array();
147 $mockTypo3DbBackend = $this->getMock(
148 $this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'),
149 array('dummy'),
150 array(),
151 '',
152 false);
153 $mockTypo3DbBackend->_callRef('addSysLanguageStatement', $table, $sql);
154
155 $expectedSql = array('additionalWhereClause' => array('(' . $table . '.sys_language_uid IN (0,-1))'));
156
157 $this->assertSame($expectedSql, $sql);
158 }
159
160 /**
161 * @test
162 */
163 public function addSysLanguageStatementWorksForForeignLanguageWithoutSubselection() {
164 $table = uniqid('tx_coretest_table');
165 $GLOBALS['TCA'][$table]['ctrl'] = array(
166 'languageField' => 'sys_language_uid'
167 );
168
169 $tsfe = $this->getMock('tslib_fe', array(), array(), '', false);
170 $tsfe->sys_language_uid = 2;
171 $GLOBALS['TSFE'] = $tsfe;
172
173 $sql = array();
174 $mockTypo3DbBackend = $this->getMock(
175 $this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'),
176 array('dummy'),
177 array(),
178 '',
179 false);
180 $mockTypo3DbBackend->_callRef('addSysLanguageStatement', $table, $sql);
181
182 $expectedSql = array('additionalWhereClause' => array('(' . $table . '.sys_language_uid IN (2,-1))'));
183
184 $this->assertSame($expectedSql, $sql);
185 }
186
187 /**
188 * @test
189 */
190 public function addSysLanguageStatementWorksForForeignLanguageWithSubselectionWithoutDeleteStatementReturned() {
191 $table = uniqid('tx_coretest_table');
192 $GLOBALS['TCA'][$table]['ctrl'] = array(
193 'languageField' => 'sys_language_uid',
194 'transOrigPointerField' => 'l18n_parent'
195 );
196
197 $tsfe = $this->getMock('tslib_fe', array(), array(), '', false);
198 $tsfe->sys_language_uid = 2;
199 $GLOBALS['TSFE'] = $tsfe;
200
201 $sql = array();
202 $mockTypo3DbBackend = $this->getMock(
203 $this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'),
204 array('dummy'),
205 array(),
206 '',
207 false);
208 $mockTypo3DbBackend->_callRef('addSysLanguageStatement', $table, $sql);
209
210 $expectedSql = array('additionalWhereClause' => array('(' . $table . '.sys_language_uid IN (2,-1) OR (' .
211 $table . '.sys_language_uid=0 AND ' . $table . '.uid NOT IN (SELECT ' . $table . '.l18n_parent FROM ' . $table .
212 ' WHERE ' . $table . '.l18n_parent>0 AND ' . $table . '.sys_language_uid>0)))'));
213
214 $this->assertSame($expectedSql, $sql);
215 }
216
217 /**
218 * @test
219 */
220 public function orderStatementGenerationWorks() {
221 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array('getNodeTypeName'), array(), '', FALSE);
222 $mockSource->expects($this->any())->method('getNodeTypeName')->will($this->returnValue('Tx_MyExt_ClassName'));
223
224 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName', 'convertClassNameToTableName'), array(), '', FALSE);
225 $mockDataMapper->expects($this->once())->method('convertClassNameToTableName')->with('Tx_MyExt_ClassName')->will($this->returnValue('tx_myext_tablename'));
226 $mockDataMapper->expects($this->once())->method('convertPropertyNameToColumnName')->with('fooProperty', 'Tx_MyExt_ClassName')->will($this->returnValue('converted_fieldname'));
227
228 $sql = array();
229 $orderings = array('fooProperty' => Tx_Extbase_Persistence_QueryInterface::ORDER_ASCENDING);
230 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserOrderings'), array(), '', FALSE);
231 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
232 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
233
234 $expecedSql = array('orderings' => array('tx_myext_tablename.converted_fieldname ASC'));
235 $this->assertSame($expecedSql, $sql);
236 }
237
238 /**
239 * @test
240 * @expectedException Tx_Extbase_Persistence_Exception_UnsupportedOrder
241 */
242 public function orderStatementGenerationThrowsExceptionOnUnsupportedOrder() {
243 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array('getNodeTypeName'), array(), '', FALSE);
244 $mockSource->expects($this->never())->method('getNodeTypeName');
245
246 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName', 'convertClassNameToTableName'), array(), '', FALSE);
247 $mockDataMapper->expects($this->never())->method('convertClassNameToTableName');
248 $mockDataMapper->expects($this->never())->method('convertPropertyNameToColumnName');
249
250 $sql = array();
251 $orderings = array('fooProperty' => 'unsupported_order');
252 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserOrderings'), array(), '', FALSE);
253 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
254 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
255 }
256
257 /**
258 * @test
259 */
260 public function orderStatementGenerationWorksWithMultipleOrderings() {
261 $mockSource = $this->getMock('Tx_Extbase_Persistence_QOM_Selector', array('getNodeTypeName'), array(), '', FALSE);
262 $mockSource->expects($this->any())->method('getNodeTypeName')->will($this->returnValue('Tx_MyExt_ClassName'));
263
264 $mockDataMapper = $this->getMock('Tx_Extbase_Persistence_Mapper_DataMapper', array('convertPropertyNameToColumnName', 'convertClassNameToTableName'), array(), '', FALSE);
265 $mockDataMapper->expects($this->any())->method('convertClassNameToTableName')->with('Tx_MyExt_ClassName')->will($this->returnValue('tx_myext_tablename'));
266 $mockDataMapper->expects($this->any())->method('convertPropertyNameToColumnName')->will($this->returnValue('converted_fieldname'));
267
268 $sql = array();
269 $orderings = array(
270 'fooProperty' => Tx_Extbase_Persistence_QueryInterface::ORDER_ASCENDING,
271 'barProperty' => Tx_Extbase_Persistence_QueryInterface::ORDER_DESCENDING
272 );
273 $mockTypo3DbBackend = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_Persistence_Storage_Typo3DbBackend'), array('parserOrderings'), array(), '', FALSE);
274 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
275 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
276
277 $expecedSql = array('orderings' => array('tx_myext_tablename.converted_fieldname ASC', 'tx_myext_tablename.converted_fieldname DESC'));
278 $this->assertSame($expecedSql, $sql);
279 }
280
281 /**
282 * @test
283 */
284 public function doLanguageAndWorkspaceOverlayChangesUidIfInPreview() {
285 $comparisonRow = array(
286 'uid' => '43',
287 'pid' => '42',
288 '_ORIG_pid' => '-1',
289 '_ORIG_uid' => '43'
290 );
291
292 $row = array(
293 'uid' => '42',
294 'pid' => '42',
295 );
296
297 $workspaceVersion = array(
298 'uid' => '43',
299 'pid' => '-1',
300 );
301
302 $languageUid = 2;
303 $workspaceUid = 2;
304
305 $sourceMock = new Tx_Extbase_Persistence_QOM_Selector('tx_foo', 'Tx_Foo');
306
307 /** @var $pageSelectMock t3lib_pageSelect|PHPUnit_Framework_MockObject_MockObject */
308 $pageSelectMock = $this->getMock('t3lib_pageSelect', array('movePlhOL', 'getWorkspaceVersionOfRecord'));
309 $pageSelectMock->versioningPreview = TRUE;
310
311 $pageSelectMock->expects($this->once())
312 ->method('getWorkspaceVersionOfRecord')
313 ->with($workspaceUid, 'tx_foo', '42')
314 ->will($this->returnValue($workspaceVersion));
315
316 $mockTypo3DbBackend = $this->getAccessibleMock(
317 'Tx_Extbase_Persistence_Storage_Typo3DbBackend',
318 array('dummy'),
319 array(), '', FALSE);
320
321
322 $mockTypo3DbBackend->_set('pageSelectObject', $pageSelectMock);
323
324 $this->assertSame(
325 array($comparisonRow),
326 $mockTypo3DbBackend->_call('doLanguageAndWorkspaceOverlay', $sourceMock, array($row), $languageUid, $workspaceUid)
327 );
328 }
329 }
330 ?>