[TASK] Remove deprecated methods/properties in Extbase
[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 /** @var \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings $mockQuerySettings */
317 $mockQuerySettings = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings', array('dummy'), array(), '', FALSE);
318 $mockQuerySettings->setIgnoreEnableFields(!$respectEnableFields);
319 $mockQuerySettings->setIncludeDeleted(!$respectEnableFields);
320 $sql = array();
321
322 /** @var $mockEnvironmentService \TYPO3\CMS\Extbase\Service\EnvironmentService | \PHPUnit_Framework_MockObject_MockObject */
323 $mockEnvironmentService = $this->getMock('TYPO3\\CMS\\Extbase\\Service\\EnvironmentService', array('isEnvironmentInFrontendMode'));
324 $mockEnvironmentService->expects($this->any())->method('isEnvironmentInFrontendMode')->will($this->returnValue($mode == 'FE'));
325
326 $mockTypo3DbQueryParser = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser', array('dummy'), array(), '', FALSE);
327 $mockTypo3DbQueryParser->_set('environmentService', $mockEnvironmentService);
328 $mockTypo3DbQueryParser->_callRef('addVisibilityConstraintStatement', $mockQuerySettings, $tableName, $sql);
329 $this->assertSame($expectedSql, $sql['additionalWhereClause']);
330 unset($GLOBALS['TCA'][$tableName]);
331 }
332
333 /**
334 * @test
335 * @expectedException \TYPO3\CMS\Extbase\Persistence\Generic\Exception\InconsistentQuerySettingsException
336 */
337 public function visibilityConstraintStatementGenerationThrowsExceptionIfTheQuerySettingsAreInconsistent() {
338 $tableName = 'tx_foo_table';
339 $GLOBALS['TCA'][$tableName]['ctrl'] = array(
340 'enablecolumns' => array(
341 'disabled' => 'disabled_column'
342 ),
343 'delete' => 'deleted_column'
344 );
345 $mockQuerySettings = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings', array('getIgnoreEnableFields', 'getEnableFieldsToBeIgnored', 'getIncludeDeleted'), array(), '', FALSE);
346 $mockQuerySettings->expects($this->once())->method('getIgnoreEnableFields')->will($this->returnValue(FALSE));
347 $mockQuerySettings->expects($this->once())->method('getEnableFieldsToBeIgnored')->will($this->returnValue(array()));
348 $mockQuerySettings->expects($this->once())->method('getIncludeDeleted')->will($this->returnValue(TRUE));
349 $sql = array();
350
351 /** @var $mockEnvironmentService \TYPO3\CMS\Extbase\Service\EnvironmentService | \PHPUnit_Framework_MockObject_MockObject */
352 $mockEnvironmentService = $this->getMock('TYPO3\\CMS\\Extbase\\Service\\EnvironmentService', array('isEnvironmentInFrontendMode'));
353 $mockEnvironmentService->expects($this->any())->method('isEnvironmentInFrontendMode')->will($this->returnValue(TRUE));
354
355 $mockTypo3DbQueryParser = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser', array('dummy'), array(), '', FALSE);
356 $mockTypo3DbQueryParser->_set('environmentService', $mockEnvironmentService);
357 $mockTypo3DbQueryParser->_callRef('addVisibilityConstraintStatement', $mockQuerySettings, $tableName, $sql);
358 unset($GLOBALS['TCA'][$tableName]);
359 }
360 /**
361 * DataProvider for addPageIdStatement Tests
362 */
363 public function providerForAddPageIdStatementData() {
364 $table = uniqid('tx_coretest_table');
365 return array(
366 'set Pid to zero if rootLevel = 1' => array(
367 '1',
368 $table,
369 array('additionalWhereClause' => array($table . '.pid = 0'))
370 ),
371 'set Pid to given Pids if rootLevel = 0' => array(
372 '0',
373 $table,
374 array('additionalWhereClause' => array($table . '.pid IN (42, 27)'))
375 ),
376 'set no statement if rootLevel = -1' => array(
377 '-1',
378 $table,
379 array()
380 )
381 );
382 }
383
384 /**
385 * @test
386 * @dataProvider providerForAddPageIdStatementData
387 */
388 public function addPageIdStatementSetsPidToZeroIfTableDeclaresRootlevel($rootLevel, $table, $expectedSql) {
389
390 $GLOBALS['TCA'][$table]['ctrl'] = array(
391 'rootLevel' => $rootLevel
392 );
393 $sql = array();
394 $storagePageIds = array(42,27);
395 $mockTypo3DbQueryParser = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser', array('dummy'), array(), '', FALSE);
396 $mockFrontendVariableCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\VariableFrontend', array(), array(), '', FALSE);
397 $mockTypo3DbQueryParser->_set('tableColumnCache', $mockFrontendVariableCache);
398 $mockFrontendVariableCache->expects($this->once())->method('get')->will($this->returnValue(array('pid' => '42')));
399 $mockTypo3DbQueryParser->_callRef('addPageIdStatement', $table, $sql, $storagePageIds);
400
401 $this->assertSame($expectedSql, $sql);
402 }
403
404 /**
405 * @test
406 * @expectedException \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnexpectedTypeException
407 */
408 public function getPlainValueThrowsExceptionIfInputIsArray() {
409 $mockTypo3DbQueryParser = $this->getAccessibleMock(
410 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser',
411 array('dummy'),
412 array(),
413 '',
414 FALSE
415 );
416 $mockTypo3DbQueryParser->_call('getPlainValue', array());
417 }
418
419 /**
420 * @test
421 */
422 public function getPlainValueReturnsTimestampIfDateTimeObjectIsGiven() {
423 $mockTypo3DbQueryParser = $this->getAccessibleMock(
424 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser',
425 array('dummy'),
426 array(),
427 '',
428 FALSE
429 );
430 $input = new \DateTime('@1365866253');
431 $this->assertSame('1365866253', $mockTypo3DbQueryParser->_call('getPlainValue', $input));
432 }
433
434 /**
435 * @test
436 */
437 public function getPlainValueReturnsIntegerOneIfValueIsBooleanTrue() {
438 $mockTypo3DbQueryParser = $this->getAccessibleMock(
439 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser',
440 array('dummy'),
441 array(),
442 '',
443 FALSE
444 );
445 $this->assertSame(1, $mockTypo3DbQueryParser->_call('getPlainValue', TRUE));
446 }
447
448 /**
449 * @test
450 */
451 public function getPlainValueReturnsIntegerZeroIfValueIsBooleanFalse() {
452 $mockTypo3DbQueryParser = $this->getAccessibleMock(
453 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser',
454 array('dummy'),
455 array(),
456 '',
457 FALSE
458 );
459 $this->assertSame(0, $mockTypo3DbQueryParser->_call('getPlainValue', FALSE));
460 }
461
462 /**
463 * @test
464 * @expectedException \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnexpectedTypeException
465 */
466 public function getPlainValueCallsGetRealInstanceOnInputIfInputIsInstanceOfLazyLoadingProxy() {
467 $mockTypo3DbQueryParser = $this->getAccessibleMock(
468 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser',
469 array('dummy'),
470 array(),
471 '',
472 FALSE
473 );
474 $input = $this->getMock(
475 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\LazyLoadingProxy',
476 array(),
477 array(),
478 '',
479 FALSE
480 );
481 $input
482 ->expects($this->once())
483 ->method('_loadRealInstance');
484 $mockTypo3DbQueryParser->_call('getPlainValue', $input);
485 }
486
487 /**
488 * @test
489 */
490 public function getPlainValueCallsGetUidOnDomainObjectInterfaceInput() {
491 $mockTypo3DbQueryParser = $this->getAccessibleMock(
492 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser',
493 array('dummy'),
494 array(),
495 '',
496 FALSE
497 );
498 $input = $this->getMock(
499 'TYPO3\\CMS\\Extbase\\DomainObject\\DomainObjectInterface',
500 array(),
501 array(),
502 '',
503 FALSE
504 );
505 $input
506 ->expects($this->once())
507 ->method('getUid')
508 ->will($this->returnValue(23));
509 $this->assertSame(23, $mockTypo3DbQueryParser->_call('getPlainValue', $input));
510 }
511
512 /**
513 * @test
514 */
515 public function getPlainValueReturnsSimpleType() {
516 $mockTypo3DbQueryParser = $this->getAccessibleMock(
517 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbQueryParser',
518 array('dummy'),
519 array(),
520 '',
521 FALSE
522 );
523 $value = uniqid('foo_');
524 $this->assertSame($value, $mockTypo3DbQueryParser->_call('getPlainValue', $value));
525 }
526
527 }