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