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