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