7bccefedf5bec1c1dd26a54a2d17babe13673bdf
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Persistence / Generic / Storage / Typo3DbQueryParserTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Storage;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * This class is a backport of the corresponding class of TYPO3 Flow.
8 * All credits go to the TYPO3 Flow team.
9 * All rights reserved
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 * A copy is found in the text file GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 class Typo3DbQueryParserTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
31
32 /**
33 * @test
34 */
35 public function addSysLanguageStatementWorksForDefaultLanguage() {
36 $table = uniqid('tx_coretest_table');
37 $GLOBALS['TCA'][$table]['ctrl'] = array(
38 'languageField' => 'sys_language_uid'
39 );
40 /** @var \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings|\PHPUnit_Framework_MockObject_MockObject $querySettings */
41 $querySettings = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');
42 $sql = array();
43 $mockTypo3DbQueryParser = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser', array('dummy'), array(), '', FALSE);
44 $mockTypo3DbQueryParser->_callRef('addSysLanguageStatement', $table, $sql, $querySettings);
45 $expectedSql = array('additionalWhereClause' => array('(' . $table . '.sys_language_uid IN (0,-1))'));
46 $this->assertSame($expectedSql, $sql);
47 }
48
49 /**
50 * @test
51 */
52 public function addSysLanguageStatementWorksForNonDefaultLanguage() {
53 $table = uniqid('tx_coretest_table');
54 $GLOBALS['TCA'][$table]['ctrl'] = array(
55 'languageField' => 'sys_language_uid'
56 );
57 $sql = array();
58 /** @var \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings|\PHPUnit_Framework_MockObject_MockObject $querySettings */
59 $querySettings = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings', array('dummy'));
60 $querySettings->setLanguageUid('1');
61 $mockTypo3DbQueryParser = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser', array('dummy'), array(), '', FALSE);
62 $mockTypo3DbQueryParser->_callRef('addSysLanguageStatement', $table, $sql, $querySettings);
63 $result = array('additionalWhereClause' => array('(' . $table . '.sys_language_uid IN (1,-1))'));
64 $this->assertSame($result, $sql);
65 }
66
67 /**
68 * @test
69 */
70 public function addSysLanguageStatementWorksInBackendContextWithNoGlobalTypoScriptFrontendControllerAvailable() {
71 $table = uniqid('tx_coretest_table');
72 $GLOBALS['TCA'][$table]['ctrl'] = array(
73 'languageField' => 'sys_language_uid'
74 );
75 $sql = array();
76 $querySettings = new \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings();
77 $mockTypo3DbQueryParser = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser', array('dummy'), array(), '', FALSE);
78 $mockTypo3DbQueryParser->_callRef('addSysLanguageStatement', $table, $sql, $querySettings);
79 $expectedSql = array('additionalWhereClause' => array('(' . $table . '.sys_language_uid IN (0,-1))'));
80 $this->assertSame($expectedSql, $sql);
81 }
82
83 /**
84 * @test
85 */
86 public function addSysLanguageStatementWorksForDefaultLanguageWithoutDeleteStatementReturned() {
87 $table = uniqid('tx_coretest_table');
88 $GLOBALS['TCA'][$table]['ctrl'] = array(
89 'languageField' => 'sys_language_uid',
90 'delete' => 'deleted'
91 );
92 $sql = array();
93 $querySettings = new \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings();
94 $querySettings->setLanguageUid(0);
95 $mockTypo3DbQueryParser = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser', array('dummy'), array(), '', FALSE);
96 $mockTypo3DbQueryParser->_callRef('addSysLanguageStatement', $table, $sql, $querySettings);
97 $expectedSql = array('additionalWhereClause' => array('(' . $table . '.sys_language_uid IN (0,-1))'));
98 $this->assertSame($expectedSql, $sql);
99 }
100
101 /**
102 * @test
103 */
104 public function addSysLanguageStatementWorksForForeignLanguageWithoutSubselection() {
105 $table = uniqid('tx_coretest_table');
106 $GLOBALS['TCA'][$table]['ctrl'] = array(
107 'languageField' => 'sys_language_uid'
108 );
109 $sql = array();
110 $querySettings = new \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings();
111 $querySettings->setLanguageUid(2);
112 $mockTypo3DbQueryParser = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser', array('dummy'), array(), '', FALSE);
113 $mockTypo3DbQueryParser->_callRef('addSysLanguageStatement', $table, $sql, $querySettings);
114 $expectedSql = array('additionalWhereClause' => array('(' . $table . '.sys_language_uid IN (2,-1))'));
115 $this->assertSame($expectedSql, $sql);
116 }
117
118 /**
119 * @test
120 */
121 public function addSysLanguageStatementWorksForForeignLanguageWithSubselectionWithoutDeleteStatementReturned() {
122 $table = uniqid('tx_coretest_table');
123 $GLOBALS['TCA'][$table]['ctrl'] = array(
124 'languageField' => 'sys_language_uid',
125 'transOrigPointerField' => 'l10n_parent'
126 );
127 $sql = array();
128 $querySettings = new \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings();
129 $querySettings->setLanguageUid(2);
130 $mockTypo3DbQueryParser = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser', array('dummy'), array(), '', FALSE);
131 $mockTypo3DbQueryParser->_callRef('addSysLanguageStatement', $table, $sql, $querySettings);
132 $expectedSql = array('additionalWhereClause' => array('(' . $table . '.sys_language_uid IN (2,-1) OR (' . $table . '.sys_language_uid=0 AND ' . $table . '.uid NOT IN (SELECT ' . $table . '.l10n_parent FROM ' . $table . ' WHERE ' . $table . '.l10n_parent>0 AND ' . $table . '.sys_language_uid>0)))'));
133 $this->assertSame($expectedSql, $sql);
134 }
135
136 /**
137 * @test
138 */
139 public function addSysLanguageStatementWorksForForeignLanguageWithSubselectionTakesDeleteStatementIntoAccountIfNecessary() {
140 $table = uniqid('tx_coretest_table');
141 $GLOBALS['TCA'][$table]['ctrl'] = array(
142 'languageField' => 'sys_language_uid',
143 'transOrigPointerField' => 'l10n_parent',
144 'delete' => 'deleted'
145 );
146 $sql = array();
147 $querySettings = new \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings();
148 $querySettings->setLanguageUid(2);
149 $mockTypo3DbQueryParser = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser', array('dummy'), array(), '', FALSE);
150 $mockTypo3DbQueryParser->_callRef('addSysLanguageStatement', $table, $sql, $querySettings);
151 $expectedSql = array('additionalWhereClause' => array(
152 '(' . $table . '.sys_language_uid IN (2,-1)' .
153 ' OR (' . $table . '.sys_language_uid=0 AND ' . $table . '.uid NOT IN (' .
154 'SELECT ' . $table . '.l10n_parent FROM ' . $table .
155 ' WHERE ' . $table . '.l10n_parent>0 AND ' .
156 $table . '.sys_language_uid>0 AND ' .
157 $table . '.deleted=0)))')
158 );
159 $this->assertSame($expectedSql, $sql);
160 }
161
162 /**
163 * @test
164 */
165 public function addSysLanguageStatementWorksInBackendContextWithSubselectionTakesDeleteStatementIntoAccountIfNecessary() {
166 $table = uniqid('tx_coretest_table');
167 $table = 'tt_content';
168 $GLOBALS['TCA'][$table]['ctrl'] = array(
169 'languageField' => 'sys_language_uid',
170 'transOrigPointerField' => 'l10n_parent',
171 'delete' => 'deleted'
172 );
173 $sql = array();
174 $querySettings = new \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings();
175 $querySettings->setLanguageUid(2);
176 $mockTypo3DbQueryParser = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser', array('dummy'), array(), '', FALSE);
177 $mockTypo3DbQueryParser->_callRef('addSysLanguageStatement', $table, $sql, $querySettings);
178 $expectedSql = array('additionalWhereClause' => array(
179 '(' . $table . '.sys_language_uid IN (2,-1)' .
180 ' OR (' . $table . '.sys_language_uid=0 AND ' . $table . '.uid NOT IN (' .
181 'SELECT ' . $table . '.l10n_parent FROM ' . $table .
182 ' WHERE ' . $table . '.l10n_parent>0 AND ' .
183 $table . '.sys_language_uid>0 AND ' .
184 $table . '.deleted=0)))')
185 );
186 $this->assertSame($expectedSql, $sql);
187 }
188
189 /**
190 * @test
191 */
192 public function orderStatementGenerationWorks() {
193 $mockSource = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Qom\\Selector', array('getNodeTypeName'), array(), '', FALSE);
194 $mockSource->expects($this->any())->method('getNodeTypeName')->will($this->returnValue('Tx_MyExt_ClassName'));
195 $mockDataMapper = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper', array('convertPropertyNameToColumnName', 'convertClassNameToTableName'), array(), '', FALSE);
196 $mockDataMapper->expects($this->once())->method('convertClassNameToTableName')->with('Tx_MyExt_ClassName')->will($this->returnValue('tx_myext_tablename'));
197 $mockDataMapper->expects($this->once())->method('convertPropertyNameToColumnName')->with('fooProperty', 'Tx_MyExt_ClassName')->will($this->returnValue('converted_fieldname'));
198 $sql = array();
199 $orderings = array('fooProperty' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING);
200 $mockTypo3DbQueryParser = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser', array('dummy'), array(), '', FALSE);
201 $mockTypo3DbQueryParser->_set('dataMapper', $mockDataMapper);
202 $mockTypo3DbQueryParser->_callRef('parseOrderings', $orderings, $mockSource, $sql);
203 $expectedSql = array('orderings' => array('tx_myext_tablename.converted_fieldname ASC'));
204 $this->assertSame($expectedSql, $sql);
205 }
206
207 /**
208 * @test
209 * @expectedException \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedOrderException
210 */
211 public function orderStatementGenerationThrowsExceptionOnUnsupportedOrder() {
212 $mockSource = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Qom\\Selector', array('getNodeTypeName'), array(), '', FALSE);
213 $mockSource->expects($this->never())->method('getNodeTypeName');
214 $mockDataMapper = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper', array('convertPropertyNameToColumnName', 'convertClassNameToTableName'), array(), '', FALSE);
215 $mockDataMapper->expects($this->never())->method('convertClassNameToTableName');
216 $mockDataMapper->expects($this->never())->method('convertPropertyNameToColumnName');
217 $sql = array();
218 $orderings = array('fooProperty' => 'unsupported_order');
219 $mockTypo3DbQueryParser = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser', array('dummy'), array(), '', FALSE);
220 $mockTypo3DbQueryParser->_set('dataMapper', $mockDataMapper);
221 $mockTypo3DbQueryParser->_callRef('parseOrderings', $orderings, $mockSource, $sql);
222 }
223
224 /**
225 * @test
226 */
227 public function orderStatementGenerationWorksWithMultipleOrderings() {
228 $mockSource = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Qom\\Selector', array('getNodeTypeName'), array(), '', FALSE);
229 $mockSource->expects($this->any())->method('getNodeTypeName')->will($this->returnValue('Tx_MyExt_ClassName'));
230 $mockDataMapper = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper', array('convertPropertyNameToColumnName', 'convertClassNameToTableName'), array(), '', FALSE);
231 $mockDataMapper->expects($this->any())->method('convertClassNameToTableName')->with('Tx_MyExt_ClassName')->will($this->returnValue('tx_myext_tablename'));
232 $mockDataMapper->expects($this->any())->method('convertPropertyNameToColumnName')->will($this->returnValue('converted_fieldname'));
233 $sql = array();
234 $orderings = array(
235 'fooProperty' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
236 'barProperty' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING
237 );
238 $mockTypo3DbQueryParser = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser', array('dummy'), array(), '', FALSE);
239 $mockTypo3DbQueryParser->_set('dataMapper', $mockDataMapper);
240 $mockTypo3DbQueryParser->_callRef('parseOrderings', $orderings, $mockSource, $sql);
241 $expectedSql = array('orderings' => array('tx_myext_tablename.converted_fieldname ASC', 'tx_myext_tablename.converted_fieldname DESC'));
242 $this->assertSame($expectedSql, $sql);
243 }
244
245 public function providerForVisibilityConstraintStatement() {
246 return array(
247 'in be: include all' => array('BE', TRUE, array(), TRUE, NULL),
248 'in be: ignore enable fields but do not include deleted' => array('BE', TRUE, array(), FALSE, array('tx_foo_table.deleted_column=0')),
249 'in be: respect enable fields but include deleted' => array('BE', FALSE, array(), TRUE, array('tx_foo_table.disabled_column=0 AND (tx_foo_table.starttime_column<=123456789)')),
250 'in be: respect enable fields and do not include deleted' => array('BE', FALSE, array(), FALSE, array('tx_foo_table.disabled_column=0 AND (tx_foo_table.starttime_column<=123456789) AND tx_foo_table.deleted_column=0')),
251 'in fe: include all' => array('FE', TRUE, array(), TRUE, NULL),
252 'in fe: ignore enable fields but do not include deleted' => array('FE', TRUE, array(), FALSE, array('tx_foo_table.deleted_column=0')),
253 'in fe: ignore only starttime and do not include deleted' => array('FE', TRUE, array('starttime'), FALSE, array('tx_foo_table.deleted_column=0 AND tx_foo_table.disabled_column=0')),
254 'in fe: respect enable fields and do not include deleted' => array('FE', FALSE, array(), FALSE, array('tx_foo_table.deleted_column=0 AND tx_foo_table.disabled_column=0 AND tx_foo_table.starttime_column<=123456789'))
255 );
256 }
257
258 /**
259 * @test
260 * @dataProvider providerForVisibilityConstraintStatement
261 */
262 public function visibilityConstraintStatementIsGeneratedAccordingToTheQuerySettings($mode, $ignoreEnableFields, $enableFieldsToBeIgnored, $deletedValue, $expectedSql) {
263 $tableName = 'tx_foo_table';
264 $GLOBALS['TCA'][$tableName]['ctrl'] = array(
265 'enablecolumns' => array(
266 'disabled' => 'disabled_column',
267 'starttime' => 'starttime_column'
268 ),
269 'delete' => 'deleted_column'
270 );
271 $GLOBALS['TSFE'] = new \stdClass();
272 $GLOBALS['TSFE']->sys_page = new \TYPO3\CMS\Frontend\Page\PageRepository();
273 $GLOBALS['SIM_ACCESS_TIME'] = 123456789;
274 $mockQuerySettings = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings', array('getIgnoreEnableFields', 'getEnableFieldsToBeIgnored', 'getIncludeDeleted'), array(), '', FALSE);
275 $mockQuerySettings->expects($this->once())->method('getIgnoreEnableFields')->will($this->returnValue($ignoreEnableFields));
276 $mockQuerySettings->expects($this->once())->method('getEnableFieldsToBeIgnored')->will($this->returnValue($enableFieldsToBeIgnored));
277 $mockQuerySettings->expects($this->once())->method('getIncludeDeleted')->will($this->returnValue($deletedValue));
278 $sql = array();
279
280 /** @var $mockEnvironmentService \TYPO3\CMS\Extbase\Service\EnvironmentService | \PHPUnit_Framework_MockObject_MockObject */
281 $mockEnvironmentService = $this->getMock('TYPO3\\CMS\\Extbase\\Service\\EnvironmentService', array('isEnvironmentInFrontendMode'));
282 $mockEnvironmentService->expects($this->any())->method('isEnvironmentInFrontendMode')->will($this->returnValue($mode == 'FE'));
283
284 $mockTypo3DbQueryParser = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser', array('dummy'), array(), '', FALSE);
285 $mockTypo3DbQueryParser->_set('environmentService', $mockEnvironmentService);
286 $mockTypo3DbQueryParser->_callRef('addVisibilityConstraintStatement', $mockQuerySettings, $tableName, $sql);
287 $this->assertSame($expectedSql, $sql['additionalWhereClause']);
288 unset($GLOBALS['TCA'][$tableName]);
289 }
290
291 public function providerForRespectEnableFields() {
292 return array(
293 'in be: respectEnableFields=false' => array('BE', FALSE, NULL),
294 'in be: respectEnableFields=true' => array('BE', TRUE, array('tx_foo_table.disabled_column=0 AND (tx_foo_table.starttime_column<=123456789) AND tx_foo_table.deleted_column=0')),
295 'in FE: respectEnableFields=false' => array('FE', FALSE, NULL),
296 'in FE: respectEnableFields=true' => array('FE', TRUE, array('tx_foo_table.deleted_column=0 AND tx_foo_table.disabled_column=0 AND tx_foo_table.starttime_column<=123456789'))
297 );
298 }
299
300 /**
301 * @test
302 * @dataProvider providerForRespectEnableFields
303 */
304 public function respectEnableFieldsSettingGeneratesCorrectStatement($mode, $respectEnableFields, $expectedSql) {
305 $tableName = 'tx_foo_table';
306 $GLOBALS['TCA'][$tableName]['ctrl'] = array(
307 'enablecolumns' => array(
308 'disabled' => 'disabled_column',
309 'starttime' => 'starttime_column'
310 ),
311 'delete' => 'deleted_column'
312 );
313 $GLOBALS['TSFE'] = new \stdClass();
314 $GLOBALS['TSFE']->sys_page = new \TYPO3\CMS\Frontend\Page\PageRepository();
315 $GLOBALS['SIM_ACCESS_TIME'] = 123456789;
316 $mockQuerySettings = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings', array('dummy'), array(), '', FALSE);
317 $mockQuerySettings->setRespectEnableFields($respectEnableFields);
318 $sql = array();
319
320 /** @var $mockEnvironmentService \TYPO3\CMS\Extbase\Service\EnvironmentService | \PHPUnit_Framework_MockObject_MockObject */
321 $mockEnvironmentService = $this->getMock('TYPO3\\CMS\\Extbase\\Service\\EnvironmentService', array('isEnvironmentInFrontendMode'));
322 $mockEnvironmentService->expects($this->any())->method('isEnvironmentInFrontendMode')->will($this->returnValue($mode == 'FE'));
323
324 $mockTypo3DbQueryParser = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser', array('dummy'), array(), '', FALSE);
325 $mockTypo3DbQueryParser->_set('environmentService', $mockEnvironmentService);
326 $mockTypo3DbQueryParser->_callRef('addVisibilityConstraintStatement', $mockQuerySettings, $tableName, $sql);
327 $this->assertSame($expectedSql, $sql['additionalWhereClause']);
328 unset($GLOBALS['TCA'][$tableName]);
329 }
330
331 /**
332 * @test
333 * @expectedException \TYPO3\CMS\Extbase\Persistence\Generic\Exception\InconsistentQuerySettingsException
334 */
335 public function visibilityConstraintStatementGenerationThrowsExceptionIfTheQuerySettingsAreInconsistent() {
336 $tableName = 'tx_foo_table';
337 $GLOBALS['TCA'][$tableName]['ctrl'] = array(
338 'enablecolumns' => array(
339 'disabled' => 'disabled_column'
340 ),
341 'delete' => 'deleted_column'
342 );
343 $mockQuerySettings = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings', array('getIgnoreEnableFields', 'getEnableFieldsToBeIgnored', 'getIncludeDeleted'), array(), '', FALSE);
344 $mockQuerySettings->expects($this->once())->method('getIgnoreEnableFields')->will($this->returnValue(FALSE));
345 $mockQuerySettings->expects($this->once())->method('getEnableFieldsToBeIgnored')->will($this->returnValue(array()));
346 $mockQuerySettings->expects($this->once())->method('getIncludeDeleted')->will($this->returnValue(TRUE));
347 $sql = array();
348
349 /** @var $mockEnvironmentService \TYPO3\CMS\Extbase\Service\EnvironmentService | \PHPUnit_Framework_MockObject_MockObject */
350 $mockEnvironmentService = $this->getMock('TYPO3\\CMS\\Extbase\\Service\\EnvironmentService', array('isEnvironmentInFrontendMode'));
351 $mockEnvironmentService->expects($this->any())->method('isEnvironmentInFrontendMode')->will($this->returnValue(TRUE));
352
353 $mockTypo3DbQueryParser = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser', array('dummy'), array(), '', FALSE);
354 $mockTypo3DbQueryParser->_set('environmentService', $mockEnvironmentService);
355 $mockTypo3DbQueryParser->_callRef('addVisibilityConstraintStatement', $mockQuerySettings, $tableName, $sql);
356 unset($GLOBALS['TCA'][$tableName]);
357 }
358 /**
359 * DataProvider for addPageIdStatement Tests
360 */
361 public function providerForAddPageIdStatementData() {
362 $table = uniqid('tx_coretest_table');
363 return array(
364 'set Pid to zero if rootLevel = 1' => array(
365 '1',
366 $table,
367 array('additionalWhereClause' => array($table . '.pid = 0'))
368 ),
369 'set Pid to given Pids if rootLevel = 0' => array(
370 '0',
371 $table,
372 array('additionalWhereClause' => array($table . '.pid IN (42, 27)'))
373 ),
374 'set no statement if rootLevel = -1' => array(
375 '-1',
376 $table,
377 array()
378 )
379 );
380 }
381
382 /**
383 * @test
384 * @dataProvider providerForAddPageIdStatementData
385 */
386 public function addPageIdStatementSetsPidToZeroIfTableDeclaresRootlevel($rootLevel, $table, $expectedSql) {
387
388 $GLOBALS['TCA'][$table]['ctrl'] = array(
389 'rootLevel' => $rootLevel
390 );
391 $sql = array();
392 $storagePageIds = array(42,27);
393 $mockTypo3DbQueryParser = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser', array('dummy'), array(), '', FALSE);
394 $mockFrontendVariableCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\VariableFrontend', array(), array(), '', FALSE);
395 $mockTypo3DbQueryParser->_set('tableColumnCache', $mockFrontendVariableCache);
396 $mockFrontendVariableCache->expects($this->once())->method('get')->will($this->returnValue(array('pid' => '42')));
397 $mockTypo3DbQueryParser->_callRef('addPageIdStatement', $table, $sql, $storagePageIds);
398
399 $this->assertSame($expectedSql, $sql);
400 }
401
402 /**
403 * @test
404 * @expectedException \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnexpectedTypeException
405 */
406 public function getPlainValueThrowsExceptionIfInputIsArray() {
407 $mockTypo3DbQueryParser = $this->getAccessibleMock(
408 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser',
409 array('dummy'),
410 array(),
411 '',
412 FALSE
413 );
414 $mockTypo3DbQueryParser->_call('getPlainValue', array());
415 }
416
417 /**
418 * @test
419 */
420 public function getPlainValueReturnsTimestampIfDateTimeObjectIsGiven() {
421 $mockTypo3DbQueryParser = $this->getAccessibleMock(
422 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser',
423 array('dummy'),
424 array(),
425 '',
426 FALSE
427 );
428 $input = new \DateTime('@1365866253');
429 $this->assertSame('1365866253', $mockTypo3DbQueryParser->_call('getPlainValue', $input));
430 }
431
432 /**
433 * @test
434 */
435 public function getPlainValueReturnsIntegerOneIfValueIsBooleanTrue() {
436 $mockTypo3DbQueryParser = $this->getAccessibleMock(
437 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser',
438 array('dummy'),
439 array(),
440 '',
441 FALSE
442 );
443 $this->assertSame(1, $mockTypo3DbQueryParser->_call('getPlainValue', TRUE));
444 }
445
446 /**
447 * @test
448 */
449 public function getPlainValueReturnsIntegerZeroIfValueIsBooleanFalse() {
450 $mockTypo3DbQueryParser = $this->getAccessibleMock(
451 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser',
452 array('dummy'),
453 array(),
454 '',
455 FALSE
456 );
457 $this->assertSame(0, $mockTypo3DbQueryParser->_call('getPlainValue', FALSE));
458 }
459
460 /**
461 * @test
462 * @expectedException \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnexpectedTypeException
463 */
464 public function getPlainValueCallsGetRealInstanceOnInputIfInputIsInstanceOfLazyLoadingProxy() {
465 $mockTypo3DbQueryParser = $this->getAccessibleMock(
466 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser',
467 array('dummy'),
468 array(),
469 '',
470 FALSE
471 );
472 $input = $this->getMock(
473 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\LazyLoadingProxy',
474 array(),
475 array(),
476 '',
477 FALSE
478 );
479 $input
480 ->expects($this->once())
481 ->method('_loadRealInstance');
482 $mockTypo3DbQueryParser->_call('getPlainValue', $input);
483 }
484
485 /**
486 * @test
487 */
488 public function getPlainValueCallsGetUidOnDomainObjectInterfaceInput() {
489 $mockTypo3DbQueryParser = $this->getAccessibleMock(
490 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser',
491 array('dummy'),
492 array(),
493 '',
494 FALSE
495 );
496 $input = $this->getMock(
497 'TYPO3\\CMS\\Extbase\\DomainObject\\DomainObjectInterface',
498 array(),
499 array(),
500 '',
501 FALSE
502 );
503 $input
504 ->expects($this->once())
505 ->method('getUid')
506 ->will($this->returnValue(23));
507 $this->assertSame(23, $mockTypo3DbQueryParser->_call('getPlainValue', $input));
508 }
509
510 /**
511 * @test
512 */
513 public function getPlainValueReturnsSimpleType() {
514 $mockTypo3DbQueryParser = $this->getAccessibleMock(
515 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser',
516 array('dummy'),
517 array(),
518 '',
519 FALSE
520 );
521 $value = uniqid('foo_');
522 $this->assertSame($value, $mockTypo3DbQueryParser->_call('getPlainValue', $value));
523 }
524
525 }