8150f85bcdfe6681974dd83ecd51eb31f2dd9623
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Persistence / Generic / Storage / Typo3DbBackendTest.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
31 /**
32 * Test case
33 */
34 class Typo3DbBackendTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
35
36 /**
37 * This is the data provider for the statement generation with a basic comparison
38 *
39 * @return array An array of data
40 */
41 public function providerForBasicComparison() {
42 return array(
43 'equal' => array(
44 \TYPO3\CMS\Extbase\Persistence\QueryInterface::OPERATOR_EQUAL_TO,
45 'SELECT table_name_from_selector.* FROM table_name_from_selector WHERE table_name_from_property.foo = \'baz\''
46 ),
47 'less' => array(
48 \TYPO3\CMS\Extbase\Persistence\QueryInterface::OPERATOR_LESS_THAN,
49 'SELECT table_name_from_selector.* FROM table_name_from_selector WHERE table_name_from_property.foo < \'baz\''
50 ),
51 'less or equal' => array(
52 \TYPO3\CMS\Extbase\Persistence\QueryInterface::OPERATOR_LESS_THAN_OR_EQUAL_TO,
53 'SELECT table_name_from_selector.* FROM table_name_from_selector WHERE table_name_from_property.foo <= \'baz\''
54 ),
55 'greater' => array(
56 \TYPO3\CMS\Extbase\Persistence\QueryInterface::OPERATOR_GREATER_THAN,
57 'SELECT table_name_from_selector.* FROM table_name_from_selector WHERE table_name_from_property.foo > \'baz\''
58 ),
59 'greater or equal' => array(
60 \TYPO3\CMS\Extbase\Persistence\QueryInterface::OPERATOR_GREATER_THAN_OR_EQUAL_TO,
61 'SELECT table_name_from_selector.* FROM table_name_from_selector WHERE table_name_from_property.foo >= \'baz\''
62 )
63 );
64 }
65
66 /**
67 * @test
68 */
69 public function addSysLanguageStatementWorksForDefaultLanguage() {
70 $table = uniqid('tx_coretest_table');
71 $GLOBALS['TCA'][$table]['ctrl'] = array(
72 'languageField' => 'sys_language_uid'
73 );
74 /** @var \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings|\PHPUnit_Framework_MockObject_MockObject $querySettings */
75 $querySettings = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings');
76 $sql = array();
77 $mockTypo3DbBackend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend', array('dummy'), array(), '', FALSE);
78 $mockTypo3DbBackend->_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 addSysLanguageStatementWorksForNonDefaultLanguage() {
87 $table = uniqid('tx_coretest_table');
88 $GLOBALS['TCA'][$table]['ctrl'] = array(
89 'languageField' => 'sys_language_uid'
90 );
91 $sql = array();
92 /** @var \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings|\PHPUnit_Framework_MockObject_MockObject $querySettings */
93 $querySettings = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings', array('dummy'));
94 $querySettings->setLanguageUid('1');
95 $mockTypo3DbBackend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend', array('dummy'), array(), '', FALSE);
96 $mockTypo3DbBackend->_callRef('addSysLanguageStatement', $table, $sql, $querySettings);
97 $result = array('additionalWhereClause' => array('(' . $table . '.sys_language_uid IN (1,-1))'));
98 $this->assertSame($result, $sql);
99 }
100
101 /**
102 * @test
103 */
104 public function addSysLanguageStatementWorksInBackendContextWithNoGlobalTypoScriptFrontendControllerAvailable() {
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 $mockTypo3DbBackend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend', array('dummy'), array(), '', FALSE);
112 $mockTypo3DbBackend->_callRef('addSysLanguageStatement', $table, $sql, $querySettings);
113 $expectedSql = array('additionalWhereClause' => array('(' . $table . '.sys_language_uid IN (0,-1))'));
114 $this->assertSame($expectedSql, $sql);
115 }
116
117 /**
118 * @test
119 */
120 public function addSysLanguageStatementWorksForDefaultLanguageWithoutDeleteStatementReturned() {
121 $table = uniqid('tx_coretest_table');
122 $GLOBALS['TCA'][$table]['ctrl'] = array(
123 'languageField' => 'sys_language_uid',
124 'delete' => 'deleted'
125 );
126 $sql = array();
127 $querySettings = new \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings();
128 $querySettings->setLanguageUid(0);
129 $mockTypo3DbBackend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend', array('dummy'), array(), '', FALSE);
130 $mockTypo3DbBackend->_callRef('addSysLanguageStatement', $table, $sql, $querySettings);
131 $expectedSql = array('additionalWhereClause' => array('(' . $table . '.sys_language_uid IN (0,-1))'));
132 $this->assertSame($expectedSql, $sql);
133 }
134
135 /**
136 * @test
137 */
138 public function addSysLanguageStatementWorksForForeignLanguageWithoutSubselection() {
139 $table = uniqid('tx_coretest_table');
140 $GLOBALS['TCA'][$table]['ctrl'] = array(
141 'languageField' => 'sys_language_uid'
142 );
143 $sql = array();
144 $querySettings = new \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings();
145 $querySettings->setLanguageUid(2);
146 $mockTypo3DbBackend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend', array('dummy'), array(), '', FALSE);
147 $mockTypo3DbBackend->_callRef('addSysLanguageStatement', $table, $sql, $querySettings);
148 $expectedSql = array('additionalWhereClause' => array('(' . $table . '.sys_language_uid IN (2,-1))'));
149 $this->assertSame($expectedSql, $sql);
150 }
151
152 /**
153 * @test
154 */
155 public function addSysLanguageStatementWorksForForeignLanguageWithSubselectionWithoutDeleteStatementReturned() {
156 $table = uniqid('tx_coretest_table');
157 $GLOBALS['TCA'][$table]['ctrl'] = array(
158 'languageField' => 'sys_language_uid',
159 'transOrigPointerField' => 'l10n_parent'
160 );
161 $sql = array();
162 $querySettings = new \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings();
163 $querySettings->setLanguageUid(2);
164 $mockTypo3DbBackend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend', array('dummy'), array(), '', FALSE);
165 $mockTypo3DbBackend->_callRef('addSysLanguageStatement', $table, $sql, $querySettings);
166 $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)))'));
167 $this->assertSame($expectedSql, $sql);
168 }
169
170 /**
171 * @test
172 */
173 public function addSysLanguageStatementWorksForForeignLanguageWithSubselectionTakesDeleteStatementIntoAccountIfNecessary() {
174 $table = uniqid('tx_coretest_table');
175 $GLOBALS['TCA'][$table]['ctrl'] = array(
176 'languageField' => 'sys_language_uid',
177 'transOrigPointerField' => 'l10n_parent',
178 'delete' => 'deleted'
179 );
180 $sql = array();
181 $querySettings = new \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings();
182 $querySettings->setLanguageUid(2);
183 $mockTypo3DbBackend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend', array('dummy'), array(), '', FALSE);
184 $mockTypo3DbBackend->_callRef('addSysLanguageStatement', $table, $sql, $querySettings);
185 $expectedSql = array('additionalWhereClause' => array(
186 '(' . $table . '.sys_language_uid IN (2,-1)' .
187 ' OR (' . $table . '.sys_language_uid=0 AND ' . $table . '.uid NOT IN (' .
188 'SELECT ' . $table . '.l10n_parent FROM ' . $table .
189 ' WHERE ' . $table . '.l10n_parent>0 AND ' .
190 $table . '.sys_language_uid>0 AND ' .
191 $table . '.deleted=0)))')
192 );
193 $this->assertSame($expectedSql, $sql);
194 }
195
196 /**
197 * @test
198 */
199 public function addSysLanguageStatementWorksInBackendContextWithSubselectionTakesDeleteStatementIntoAccountIfNecessary() {
200 $table = uniqid('tx_coretest_table');
201 $table = 'tt_content';
202 $GLOBALS['TCA'][$table]['ctrl'] = array(
203 'languageField' => 'sys_language_uid',
204 'transOrigPointerField' => 'l10n_parent',
205 'delete' => 'deleted'
206 );
207 $sql = array();
208 $querySettings = new \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings();
209 $querySettings->setLanguageUid(2);
210 $mockTypo3DbBackend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend', array('dummy'), array(), '', FALSE);
211 $mockTypo3DbBackend->_callRef('addSysLanguageStatement', $table, $sql, $querySettings);
212 $expectedSql = array('additionalWhereClause' => array(
213 '(' . $table . '.sys_language_uid IN (2,-1)' .
214 ' OR (' . $table . '.sys_language_uid=0 AND ' . $table . '.uid NOT IN (' .
215 'SELECT ' . $table . '.l10n_parent FROM ' . $table .
216 ' WHERE ' . $table . '.l10n_parent>0 AND ' .
217 $table . '.sys_language_uid>0 AND ' .
218 $table . '.deleted=0)))')
219 );
220 $this->assertSame($expectedSql, $sql);
221 }
222
223 /**
224 * @test
225 */
226 public function orderStatementGenerationWorks() {
227 $mockSource = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Qom\\Selector', array('getNodeTypeName'), array(), '', FALSE);
228 $mockSource->expects($this->any())->method('getNodeTypeName')->will($this->returnValue('Tx_MyExt_ClassName'));
229 $mockDataMapper = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper', array('convertPropertyNameToColumnName', 'convertClassNameToTableName'), array(), '', FALSE);
230 $mockDataMapper->expects($this->once())->method('convertClassNameToTableName')->with('Tx_MyExt_ClassName')->will($this->returnValue('tx_myext_tablename'));
231 $mockDataMapper->expects($this->once())->method('convertPropertyNameToColumnName')->with('fooProperty', 'Tx_MyExt_ClassName')->will($this->returnValue('converted_fieldname'));
232 $sql = array();
233 $orderings = array('fooProperty' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING);
234 $mockTypo3DbBackend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend', array('parserOrderings'), array(), '', FALSE);
235 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
236 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
237 $expectedSql = array('orderings' => array('tx_myext_tablename.converted_fieldname ASC'));
238 $this->assertSame($expectedSql, $sql);
239 }
240
241 /**
242 * @test
243 * @expectedException \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedOrderException
244 */
245 public function orderStatementGenerationThrowsExceptionOnUnsupportedOrder() {
246 $mockSource = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Qom\\Selector', array('getNodeTypeName'), array(), '', FALSE);
247 $mockSource->expects($this->never())->method('getNodeTypeName');
248 $mockDataMapper = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper', array('convertPropertyNameToColumnName', 'convertClassNameToTableName'), array(), '', FALSE);
249 $mockDataMapper->expects($this->never())->method('convertClassNameToTableName');
250 $mockDataMapper->expects($this->never())->method('convertPropertyNameToColumnName');
251 $sql = array();
252 $orderings = array('fooProperty' => 'unsupported_order');
253 $mockTypo3DbBackend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend', array('parserOrderings'), array(), '', FALSE);
254 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
255 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
256 }
257
258 /**
259 * @test
260 */
261 public function orderStatementGenerationWorksWithMultipleOrderings() {
262 $mockSource = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Qom\\Selector', array('getNodeTypeName'), array(), '', FALSE);
263 $mockSource->expects($this->any())->method('getNodeTypeName')->will($this->returnValue('Tx_MyExt_ClassName'));
264 $mockDataMapper = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\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 $sql = array();
268 $orderings = array(
269 'fooProperty' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
270 'barProperty' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING
271 );
272 $mockTypo3DbBackend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend', array('parserOrderings'), array(), '', FALSE);
273 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
274 $mockTypo3DbBackend->_callRef('parseOrderings', $orderings, $mockSource, $sql);
275 $expectedSql = array('orderings' => array('tx_myext_tablename.converted_fieldname ASC', 'tx_myext_tablename.converted_fieldname DESC'));
276 $this->assertSame($expectedSql, $sql);
277 }
278
279 public function providerForVisibilityConstraintStatement() {
280 return array(
281 'in be: include all' => array('BE', TRUE, array(), TRUE, NULL),
282 'in be: ignore enable fields but do not include deleted' => array('BE', TRUE, array(), FALSE, array('tx_foo_table.deleted_column=0')),
283 '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)')),
284 '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')),
285 'in fe: include all' => array('FE', TRUE, array(), TRUE, NULL),
286 'in fe: ignore enable fields but do not include deleted' => array('FE', TRUE, array(), FALSE, array('tx_foo_table.deleted_column=0')),
287 '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')),
288 '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'))
289 );
290 }
291
292 /**
293 * @test
294 * @dataProvider providerForVisibilityConstraintStatement
295 */
296 public function visibilityConstraintStatementIsGeneratedAccordingToTheQuerySettings($mode, $ignoreEnableFields, $enableFieldsToBeIgnored, $deletedValue, $expectedSql) {
297 $tableName = 'tx_foo_table';
298 $GLOBALS['TCA'][$tableName]['ctrl'] = array(
299 'enablecolumns' => array(
300 'disabled' => 'disabled_column',
301 'starttime' => 'starttime_column'
302 ),
303 'delete' => 'deleted_column'
304 );
305 $GLOBALS['TSFE'] = new \stdClass();
306 $GLOBALS['TSFE']->sys_page = new \TYPO3\CMS\Frontend\Page\PageRepository();
307 $GLOBALS['SIM_ACCESS_TIME'] = 123456789;
308 $mockQuerySettings = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings', array('getIgnoreEnableFields', 'getEnableFieldsToBeIgnored', 'getIncludeDeleted'), array(), '', FALSE);
309 $mockQuerySettings->expects($this->once())->method('getIgnoreEnableFields')->will($this->returnValue($ignoreEnableFields));
310 $mockQuerySettings->expects($this->once())->method('getEnableFieldsToBeIgnored')->will($this->returnValue($enableFieldsToBeIgnored));
311 $mockQuerySettings->expects($this->once())->method('getIncludeDeleted')->will($this->returnValue($deletedValue));
312 $sql = array();
313
314 /** @var $mockEnvironmentService \TYPO3\CMS\Extbase\Service\EnvironmentService | \PHPUnit_Framework_MockObject_MockObject */
315 $mockEnvironmentService = $this->getMock('TYPO3\\CMS\\Extbase\\Service\\EnvironmentService', array('isEnvironmentInFrontendMode'));
316 $mockEnvironmentService->expects($this->any())->method('isEnvironmentInFrontendMode')->will($this->returnValue($mode == 'FE'));
317
318 /** @var $mockTypo3DbBackend \TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbBackend | \PHPUnit_Framework_MockObject_MockObject */
319 $mockTypo3DbBackend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend', array('dummy'), array(), '', FALSE);
320 $mockTypo3DbBackend->_set('environmentService', $mockEnvironmentService);
321 $mockTypo3DbBackend->_callRef('addVisibilityConstraintStatement', $mockQuerySettings, $tableName, $sql);
322 $this->assertSame($expectedSql, $sql['additionalWhereClause']);
323 unset($GLOBALS['TCA'][$tableName]);
324 }
325
326 public function providerForRespectEnableFields() {
327 return array(
328 'in be: respectEnableFields=false' => array('BE', FALSE, NULL),
329 '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')),
330 'in be: respectEnableFields=false' => array('FE', FALSE, NULL),
331 'in be: 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'))
332 );
333 }
334
335 /**
336 * @test
337 * @dataProvider providerForRespectEnableFields
338 */
339 public function respectEnableFieldsSettingGeneratesCorrectStatement($mode, $respectEnableFields, $expectedSql) {
340 $tableName = 'tx_foo_table';
341 $GLOBALS['TCA'][$tableName]['ctrl'] = array(
342 'enablecolumns' => array(
343 'disabled' => 'disabled_column',
344 'starttime' => 'starttime_column'
345 ),
346 'delete' => 'deleted_column'
347 );
348 $GLOBALS['TSFE'] = new \stdClass();
349 $GLOBALS['TSFE']->sys_page = new \TYPO3\CMS\Frontend\Page\PageRepository();
350 $GLOBALS['SIM_ACCESS_TIME'] = 123456789;
351 $mockQuerySettings = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings', array('dummy'), array(), '', FALSE);
352 $mockQuerySettings->setRespectEnableFields($respectEnableFields);
353 $sql = array();
354
355 /** @var $mockEnvironmentService \TYPO3\CMS\Extbase\Service\EnvironmentService | \PHPUnit_Framework_MockObject_MockObject */
356 $mockEnvironmentService = $this->getMock('TYPO3\\CMS\\Extbase\\Service\\EnvironmentService', array('isEnvironmentInFrontendMode'));
357 $mockEnvironmentService->expects($this->any())->method('isEnvironmentInFrontendMode')->will($this->returnValue($mode == 'FE'));
358
359 /** @var $mockTypo3DbBackend \TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbBackend | \PHPUnit_Framework_MockObject_MockObject */
360 $mockTypo3DbBackend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend', array('dummy'), array(), '', FALSE);
361 $mockTypo3DbBackend->_set('environmentService', $mockEnvironmentService);
362 $mockTypo3DbBackend->_callRef('addVisibilityConstraintStatement', $mockQuerySettings, $tableName, $sql);
363 $this->assertSame($expectedSql, $sql['additionalWhereClause']);
364 unset($GLOBALS['TCA'][$tableName]);
365 }
366
367 /**
368 * @test
369 * @expectedException \TYPO3\CMS\Extbase\Persistence\Generic\Exception\InconsistentQuerySettingsException
370 */
371 public function visibilityConstraintStatementGenerationThrowsExceptionIfTheQuerySettingsAreInconsistent() {
372 $tableName = 'tx_foo_table';
373 $GLOBALS['TCA'][$tableName]['ctrl'] = array(
374 'enablecolumns' => array(
375 'disabled' => 'disabled_column'
376 ),
377 'delete' => 'deleted_column'
378 );
379 $mockQuerySettings = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings', array('getIgnoreEnableFields', 'getEnableFieldsToBeIgnored', 'getIncludeDeleted'), array(), '', FALSE);
380 $mockQuerySettings->expects($this->once())->method('getIgnoreEnableFields')->will($this->returnValue(FALSE));
381 $mockQuerySettings->expects($this->once())->method('getEnableFieldsToBeIgnored')->will($this->returnValue(array()));
382 $mockQuerySettings->expects($this->once())->method('getIncludeDeleted')->will($this->returnValue(TRUE));
383 $sql = array();
384
385 /** @var $mockEnvironmentService \TYPO3\CMS\Extbase\Service\EnvironmentService | \PHPUnit_Framework_MockObject_MockObject */
386 $mockEnvironmentService = $this->getMock('TYPO3\\CMS\\Extbase\\Service\\EnvironmentService', array('isEnvironmentInFrontendMode'));
387 $mockEnvironmentService->expects($this->any())->method('isEnvironmentInFrontendMode')->will($this->returnValue(TRUE));
388
389 /** @var $mockTypo3DbBackend \TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbBackend | \PHPUnit_Framework_MockObject_MockObject */
390 $mockTypo3DbBackend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend', array('dummy'), array(), '', FALSE);
391 $mockTypo3DbBackend->_set('environmentService', $mockEnvironmentService);
392 $mockTypo3DbBackend->_callRef('addVisibilityConstraintStatement', $mockQuerySettings, $tableName, $sql);
393 unset($GLOBALS['TCA'][$tableName]);
394 }
395
396 /**
397 * @test
398 */
399 public function uidOfAlreadyPersistedValueObjectIsDeterminedCorrectly() {
400 $mockValueObject = $this->getMock('TYPO3\\CMS\\Extbase\\DomainObject\\AbstractValueObject', array('_getProperties'), array(), '', FALSE);
401 $mockValueObject->expects($this->once())->method('_getProperties')->will($this->returnValue(array('propertyName' => 'propertyValue')));
402 $mockColumnMap = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMap', array('isPersistableProperty', 'getColumnName'), array(), '', FALSE);
403 $mockColumnMap->expects($this->any())->method('getColumnName')->will($this->returnValue('column_name'));
404 $tableName = 'tx_foo_table';
405 $mockDataMap = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMap', array('isPersistableProperty', 'getColumnMap', 'getTableName'), array(), '', FALSE);
406 $mockDataMap->expects($this->any())->method('isPersistableProperty')->will($this->returnValue(TRUE));
407 $mockDataMap->expects($this->any())->method('getColumnMap')->will($this->returnValue($mockColumnMap));
408 $mockDataMap->expects($this->any())->method('getTableName')->will($this->returnValue($tableName));
409 $mockDataMapper = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper', array('getDataMap'), array(), '', FALSE);
410 $mockDataMapper->expects($this->once())->method('getDataMap')->will($this->returnValue($mockDataMap));
411 $expectedStatement = 'SELECT * FROM tx_foo_table WHERE column_name=?';
412 $expectedParameters = array('plainPropertyValue');
413 $expectedUid = 52;
414 $mockDataBaseHandle = $this->getMock('TYPO3\CMS\Core\Database\DatabaseConnection', array('sql_query', 'sql_fetch_assoc'), array(), '', FALSE);
415 $mockDataBaseHandle->expects($this->once())->method('sql_query')->will($this->returnValue('resource'));
416 $mockDataBaseHandle->expects($this->any())->method('sql_fetch_assoc')->with('resource')->will($this->returnValue(array('uid' => $expectedUid)));
417 $mockTypo3DbBackend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend', array('getPlainValue', 'checkSqlErrors', 'replacePlaceholders', 'addVisibilityConstraintStatement'), array(), '', FALSE);
418 $mockTypo3DbBackend->expects($this->once())->method('getPlainValue')->will($this->returnValue('plainPropertyValue'));
419 $mockTypo3DbBackend->expects($this->once())->method('addVisibilityConstraintStatement')->with($this->isInstanceOf('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\QuerySettingsInterface'), $tableName, $this->isType('array'));
420 $mockTypo3DbBackend->expects($this->once())->method('replacePlaceholders')->with($expectedStatement, $expectedParameters);
421 $mockTypo3DbBackend->_set('dataMapper', $mockDataMapper);
422 $mockTypo3DbBackend->_set('databaseHandle', $mockDataBaseHandle);
423 $result = $mockTypo3DbBackend->_callRef('getUidOfAlreadyPersistedValueObject', $mockValueObject);
424 $this->assertSame($expectedUid, $result);
425 }
426
427 /**
428 * @test
429 */
430 public function doLanguageAndWorkspaceOverlayChangesUidIfInPreview() {
431 $comparisonRow = array(
432 'uid' => '43',
433 'pid' => '42',
434 '_ORIG_pid' => '-1',
435 '_ORIG_uid' => '43'
436 );
437 $row = array(
438 'uid' => '42',
439 'pid' => '42'
440 );
441 $workspaceVersion = array(
442 'uid' => '43',
443 'pid' => '-1'
444 );
445 /** @var \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings|\PHPUnit_Framework_MockObject_MockObject $querySettings */
446 $mockQuerySettings = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Typo3QuerySettings', array('dummy'), array(), '', FALSE);
447
448 $workspaceUid = 2;
449 $sourceMock = new \TYPO3\CMS\Extbase\Persistence\Generic\Qom\Selector('tx_foo', 'Tx_Foo');
450 /** @var $pageRepositoryMock \TYPO3\CMS\Frontend\Page\PageRepository|\PHPUnit_Framework_MockObject_MockObject */
451 $pageRepositoryMock = $this->getMock('TYPO3\\CMS\\Frontend\\Page\\PageRepository', array('movePlhOL', 'getWorkspaceVersionOfRecord'));
452 $pageRepositoryMock->versioningPreview = TRUE;
453 $pageRepositoryMock->expects($this->once())->method('getWorkspaceVersionOfRecord')->with($workspaceUid, 'tx_foo', '42')->will($this->returnValue($workspaceVersion));
454 $mockTypo3DbBackend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend', array('dummy'), array(), '', FALSE);
455 $mockTypo3DbBackend->_set('pageRepository', $pageRepositoryMock);
456 $this->assertSame(array($comparisonRow), $mockTypo3DbBackend->_call('doLanguageAndWorkspaceOverlay', $sourceMock, array($row), $mockQuerySettings, $workspaceUid));
457 }
458
459 /**
460 * DataProvider for addPageIdStatement Tests
461 */
462 public function providerForAddPageIdStatementData() {
463 $table = uniqid('tx_coretest_table');
464 return array(
465 'set Pid to zero if rootLevel = 1' => array(
466 '1',
467 $table,
468 array('additionalWhereClause' => array($table . '.pid = 0'))
469 ),
470 'set Pid to given Pids if rootLevel = 0' => array(
471 '0',
472 $table,
473 array('additionalWhereClause' => array($table . '.pid IN (42, 27)'))
474 ),
475 'set no statement if rootLevel = -1' => array(
476 '-1',
477 $table,
478 array()
479 )
480 );
481 }
482
483 /**
484 * @test
485 * @dataProvider providerForAddPageIdStatementData
486 */
487 public function addPageIdStatementSetsPidToZeroIfTableDeclaresRootlevel($rootLevel, $table, $expectedSql) {
488
489 $GLOBALS['TCA'][$table]['ctrl'] = array(
490 'rootLevel' => $rootLevel
491 );
492 $sql = array();
493 $storagePageIds = array(42,27);
494 $mockTypo3DbBackend = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend', array('dummy'), array(), '', FALSE);
495 $mockFrontendVariableCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\VariableFrontend', array(), array(), '', FALSE);
496 $mockTypo3DbBackend->_set('tableColumnCache', $mockFrontendVariableCache);
497 $mockFrontendVariableCache->expects($this->once())->method('get')->will($this->returnValue(array('pid' => '42')));
498 $mockTypo3DbBackend->_callRef('addPageIdStatement', $table, $sql, $storagePageIds);
499
500 $this->assertSame($expectedSql, $sql);
501 }
502
503 /**
504 * @test
505 * @expectedException \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnexpectedTypeException
506 */
507 public function getPlainValueThrowsExceptionIfInputIsArray() {
508 $mockTypo3DbBackend = $this->getAccessibleMock(
509 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend',
510 array('dummy'),
511 array(),
512 '',
513 FALSE
514 );
515 $mockTypo3DbBackend->_call('getPlainValue', array());
516 }
517
518 /**
519 * @test
520 */
521 public function getPlainValueReturnsTimestampIfDateTimeObjectIsGiven() {
522 $mockTypo3DbBackend = $this->getAccessibleMock(
523 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend',
524 array('dummy'),
525 array(),
526 '',
527 FALSE
528 );
529 $input = new \DateTime('@1365866253');
530 $this->assertSame('1365866253', $mockTypo3DbBackend->_call('getPlainValue', $input));
531 }
532
533 /**
534 * @test
535 */
536 public function getPlainValueReturnsIntegerOneIfValueIsBooleanTrue() {
537 $mockTypo3DbBackend = $this->getAccessibleMock(
538 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend',
539 array('dummy'),
540 array(),
541 '',
542 FALSE
543 );
544 $this->assertSame(1, $mockTypo3DbBackend->_call('getPlainValue', TRUE));
545 }
546
547 /**
548 * @test
549 */
550 public function getPlainValueReturnsIntegerZeroIfValueIsBooleanFalse() {
551 $mockTypo3DbBackend = $this->getAccessibleMock(
552 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend',
553 array('dummy'),
554 array(),
555 '',
556 FALSE
557 );
558 $this->assertSame(0, $mockTypo3DbBackend->_call('getPlainValue', FALSE));
559 }
560
561 /**
562 * @test
563 * @expectedException \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnexpectedTypeException
564 */
565 public function getPlainValueCallsGetRealInstanceOnInputIfInputIsInstanceOfLazyLoadingProxy() {
566 $mockTypo3DbBackend = $this->getAccessibleMock(
567 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend',
568 array('dummy'),
569 array(),
570 '',
571 FALSE
572 );
573 $input = $this->getMock(
574 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\LazyLoadingProxy',
575 array(),
576 array(),
577 '',
578 FALSE
579 );
580 $input
581 ->expects($this->once())
582 ->method('_loadRealInstance');
583 $mockTypo3DbBackend->_call('getPlainValue', $input);
584 }
585
586 /**
587 * @test
588 */
589 public function getPlainValueCallsGetUidOnDomainObjectInterfaceInput() {
590 $mockTypo3DbBackend = $this->getAccessibleMock(
591 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend',
592 array('dummy'),
593 array(),
594 '',
595 FALSE
596 );
597 $input = $this->getMock(
598 'TYPO3\\CMS\\Extbase\\DomainObject\\DomainObjectInterface',
599 array(),
600 array(),
601 '',
602 FALSE
603 );
604 $input
605 ->expects($this->once())
606 ->method('getUid')
607 ->will($this->returnValue(23));
608 $this->assertSame(23, $mockTypo3DbBackend->_call('getPlainValue', $input));
609 }
610
611 /**
612 * @test
613 */
614 public function getPlainValueReturnsSimpleType() {
615 $mockTypo3DbBackend = $this->getAccessibleMock(
616 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Storage\\Typo3DbBackend',
617 array('dummy'),
618 array(),
619 '',
620 FALSE
621 );
622 $value = uniqid('foo_');
623 $this->assertSame($value, $mockTypo3DbBackend->_call('getPlainValue', $value));
624 }
625
626 }