ed103c407a6ef7ab00e384402ec48d0b41333909
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Persistence / Generic / Storage / Typo3DbQueryParserTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Persistence\Generic\Storage;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 class Typo3DbQueryParserTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
17
18 /**
19 * @test
20 */
21 public function addSysLanguageStatementWorksForDefaultLanguage() {
22 $table = uniqid('tx_coretest_table');
23 $GLOBALS['TCA'][$table]['ctrl'] = array(
24 'languageField' => 'sys_language_uid'
25 );
26 /** @var \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings|\PHPUnit_Framework_MockObject_MockObject $querySettings */
27 $querySettings = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings::class);
28 $sql = array();
29 $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, array('dummy'), array(), '', FALSE);
30 $mockTypo3DbQueryParser->_callRef('addSysLanguageStatement', $table, $sql, $querySettings);
31 $expectedSql = array('additionalWhereClause' => array('(' . $table . '.sys_language_uid IN (0,-1))'));
32 $this->assertSame($expectedSql, $sql);
33 }
34
35 /**
36 * @test
37 */
38 public function addSysLanguageStatementWorksForNonDefaultLanguage() {
39 $table = uniqid('tx_coretest_table');
40 $GLOBALS['TCA'][$table]['ctrl'] = array(
41 'languageField' => 'sys_language_uid'
42 );
43 $sql = array();
44 /** @var \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings|\PHPUnit_Framework_MockObject_MockObject $querySettings */
45 $querySettings = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings::class, array('dummy'));
46 $querySettings->setLanguageUid('1');
47 $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, array('dummy'), array(), '', FALSE);
48 $mockTypo3DbQueryParser->_callRef('addSysLanguageStatement', $table, $sql, $querySettings);
49 $result = array('additionalWhereClause' => array('(' . $table . '.sys_language_uid IN (1,-1))'));
50 $this->assertSame($result, $sql);
51 }
52
53 /**
54 * @test
55 */
56 public function addSysLanguageStatementWorksInBackendContextWithNoGlobalTypoScriptFrontendControllerAvailable() {
57 $table = uniqid('tx_coretest_table');
58 $GLOBALS['TCA'][$table]['ctrl'] = array(
59 'languageField' => 'sys_language_uid'
60 );
61 $sql = array();
62 $querySettings = new \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings();
63 $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, array('dummy'), array(), '', FALSE);
64 $mockTypo3DbQueryParser->_callRef('addSysLanguageStatement', $table, $sql, $querySettings);
65 $expectedSql = array('additionalWhereClause' => array('(' . $table . '.sys_language_uid IN (0,-1))'));
66 $this->assertSame($expectedSql, $sql);
67 }
68
69 /**
70 * @test
71 */
72 public function addSysLanguageStatementWorksForDefaultLanguageWithoutDeleteStatementReturned() {
73 $table = uniqid('tx_coretest_table');
74 $GLOBALS['TCA'][$table]['ctrl'] = array(
75 'languageField' => 'sys_language_uid',
76 'delete' => 'deleted'
77 );
78 $sql = array();
79 $querySettings = new \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings();
80 $querySettings->setLanguageUid(0);
81 $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, array('dummy'), array(), '', FALSE);
82 $mockTypo3DbQueryParser->_callRef('addSysLanguageStatement', $table, $sql, $querySettings);
83 $expectedSql = array('additionalWhereClause' => array('(' . $table . '.sys_language_uid IN (0,-1))'));
84 $this->assertSame($expectedSql, $sql);
85 }
86
87 /**
88 * @test
89 */
90 public function addSysLanguageStatementWorksForForeignLanguageWithoutSubselection() {
91 $table = uniqid('tx_coretest_table');
92 $GLOBALS['TCA'][$table]['ctrl'] = array(
93 'languageField' => 'sys_language_uid'
94 );
95 $sql = array();
96 $querySettings = new \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings();
97 $querySettings->setLanguageUid(2);
98 $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, array('dummy'), array(), '', FALSE);
99 $mockTypo3DbQueryParser->_callRef('addSysLanguageStatement', $table, $sql, $querySettings);
100 $expectedSql = array('additionalWhereClause' => array('(' . $table . '.sys_language_uid IN (2,-1))'));
101 $this->assertSame($expectedSql, $sql);
102 }
103
104 /**
105 * @test
106 */
107 public function addSysLanguageStatementWorksForForeignLanguageWithSubselectionWithoutDeleteStatementReturned() {
108 $table = uniqid('tx_coretest_table');
109 $GLOBALS['TCA'][$table]['ctrl'] = array(
110 'languageField' => 'sys_language_uid',
111 'transOrigPointerField' => 'l10n_parent'
112 );
113 $sql = array();
114 $querySettings = new \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings();
115 $querySettings->setLanguageUid(2);
116 $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, array('dummy'), array(), '', FALSE);
117 $mockTypo3DbQueryParser->_callRef('addSysLanguageStatement', $table, $sql, $querySettings);
118 $expectedSql = array('additionalWhereClause' => array('(' . $table . '.sys_language_uid IN (2,-1) OR (' . $table . '.sys_language_uid=0 AND ' . $table . '.uid NOT IN (SELECT ' . $table . '.l10n_parent FROM ' . $table . ' WHERE ' . $table . '.l10n_parent>0 AND ' . $table . '.sys_language_uid=2)))'));
119 $this->assertSame($expectedSql, $sql);
120 }
121
122 /**
123 * @test
124 */
125 public function addSysLanguageStatementWorksForForeignLanguageWithSubselectionTakesDeleteStatementIntoAccountIfNecessary() {
126 $table = uniqid('tx_coretest_table');
127 $GLOBALS['TCA'][$table]['ctrl'] = array(
128 'languageField' => 'sys_language_uid',
129 'transOrigPointerField' => 'l10n_parent',
130 'delete' => 'deleted'
131 );
132 $sql = array();
133 $querySettings = new \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings();
134 $querySettings->setLanguageUid(2);
135 $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, array('dummy'), array(), '', FALSE);
136 $mockTypo3DbQueryParser->_callRef('addSysLanguageStatement', $table, $sql, $querySettings);
137 $expectedSql = array('additionalWhereClause' => array(
138 '(' . $table . '.sys_language_uid IN (2,-1)' .
139 ' OR (' . $table . '.sys_language_uid=0 AND ' . $table . '.uid NOT IN (' .
140 'SELECT ' . $table . '.l10n_parent FROM ' . $table .
141 ' WHERE ' . $table . '.l10n_parent>0 AND ' .
142 $table . '.sys_language_uid=2 AND ' .
143 $table . '.deleted=0)))')
144 );
145 $this->assertSame($expectedSql, $sql);
146 }
147
148 /**
149 * @test
150 */
151 public function addSysLanguageStatementWorksInBackendContextWithSubselectionTakesDeleteStatementIntoAccountIfNecessary() {
152 $table = uniqid('tx_coretest_table');
153 $table = 'tt_content';
154 $GLOBALS['TCA'][$table]['ctrl'] = array(
155 'languageField' => 'sys_language_uid',
156 'transOrigPointerField' => 'l10n_parent',
157 'delete' => 'deleted'
158 );
159 $sql = array();
160 $querySettings = new \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings();
161 $querySettings->setLanguageUid(2);
162 $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, array('dummy'), array(), '', FALSE);
163 $mockTypo3DbQueryParser->_callRef('addSysLanguageStatement', $table, $sql, $querySettings);
164 $expectedSql = array('additionalWhereClause' => array(
165 '(' . $table . '.sys_language_uid IN (2,-1)' .
166 ' OR (' . $table . '.sys_language_uid=0 AND ' . $table . '.uid NOT IN (' .
167 'SELECT ' . $table . '.l10n_parent FROM ' . $table .
168 ' WHERE ' . $table . '.l10n_parent>0 AND ' .
169 $table . '.sys_language_uid=2 AND ' .
170 $table . '.deleted=0)))')
171 );
172 $this->assertSame($expectedSql, $sql);
173 }
174
175 /**
176 * @test
177 */
178 public function orderStatementGenerationWorks() {
179 $mockSource = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\Selector::class, array('getNodeTypeName'), array(), '', FALSE);
180 $mockSource->expects($this->any())->method('getNodeTypeName')->will($this->returnValue('Tx_MyExt_ClassName'));
181 $mockDataMapper = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class, array('convertPropertyNameToColumnName', 'convertClassNameToTableName'), array(), '', FALSE);
182 $mockDataMapper->expects($this->once())->method('convertClassNameToTableName')->with('Tx_MyExt_ClassName')->will($this->returnValue('tx_myext_tablename'));
183 $mockDataMapper->expects($this->once())->method('convertPropertyNameToColumnName')->with('fooProperty', 'Tx_MyExt_ClassName')->will($this->returnValue('converted_fieldname'));
184 $sql = array();
185 $orderings = array('fooProperty' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING);
186 $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, array('dummy'), array(), '', FALSE);
187 $mockTypo3DbQueryParser->_set('dataMapper', $mockDataMapper);
188 $mockTypo3DbQueryParser->_callRef('parseOrderings', $orderings, $mockSource, $sql);
189 $expectedSql = array('orderings' => array('tx_myext_tablename.converted_fieldname ASC'));
190 $this->assertSame($expectedSql, $sql);
191 }
192
193 /**
194 * @test
195 * @expectedException \TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedOrderException
196 */
197 public function orderStatementGenerationThrowsExceptionOnUnsupportedOrder() {
198 $mockSource = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\Selector::class, array('getNodeTypeName'), array(), '', FALSE);
199 $mockSource->expects($this->never())->method('getNodeTypeName');
200 $mockDataMapper = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class, array('convertPropertyNameToColumnName', 'convertClassNameToTableName'), array(), '', FALSE);
201 $mockDataMapper->expects($this->never())->method('convertClassNameToTableName');
202 $mockDataMapper->expects($this->never())->method('convertPropertyNameToColumnName');
203 $sql = array();
204 $orderings = array('fooProperty' => 'unsupported_order');
205 $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, array('dummy'), array(), '', FALSE);
206 $mockTypo3DbQueryParser->_set('dataMapper', $mockDataMapper);
207 $mockTypo3DbQueryParser->_callRef('parseOrderings', $orderings, $mockSource, $sql);
208 }
209
210 /**
211 * @test
212 */
213 public function orderStatementGenerationWorksWithMultipleOrderings() {
214 $mockSource = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\Selector::class, array('getNodeTypeName'), array(), '', FALSE);
215 $mockSource->expects($this->any())->method('getNodeTypeName')->will($this->returnValue('Tx_MyExt_ClassName'));
216 $mockDataMapper = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class, array('convertPropertyNameToColumnName', 'convertClassNameToTableName'), array(), '', FALSE);
217 $mockDataMapper->expects($this->any())->method('convertClassNameToTableName')->with('Tx_MyExt_ClassName')->will($this->returnValue('tx_myext_tablename'));
218 $mockDataMapper->expects($this->any())->method('convertPropertyNameToColumnName')->will($this->returnValue('converted_fieldname'));
219 $sql = array();
220 $orderings = array(
221 'fooProperty' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
222 'barProperty' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING
223 );
224 $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, array('dummy'), array(), '', FALSE);
225 $mockTypo3DbQueryParser->_set('dataMapper', $mockDataMapper);
226 $mockTypo3DbQueryParser->_callRef('parseOrderings', $orderings, $mockSource, $sql);
227 $expectedSql = array('orderings' => array('tx_myext_tablename.converted_fieldname ASC', 'tx_myext_tablename.converted_fieldname DESC'));
228 $this->assertSame($expectedSql, $sql);
229 }
230
231 /**
232 * @test
233 */
234 public function comparisonStatementGenerationWorksWithArrayAsOperand2() {
235 $dummyClassname = 'TYPO3\\CMS\\Extbase\\Tests\\Unit\\Persistence\\Generic\\Storage\\DummyClass';
236 $dummyTablename = 'tx_extbase_tests_dummytable';
237 $mockSource = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\Selector::class, array('getNodeTypeName'), array(), '', FALSE);
238 $mockSource->expects($this->any())->method('getNodeTypeName')->will($this->returnValue($dummyClassname));
239
240 $columnMap = new \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap('', '');
241 $columnMap->setTypeOfRelation(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY);
242 $columnMap->setRelationTableName('sys_category_record_mm');
243 $columnMap->setRelationTableMatchFields(array(
244 'tablenames' => $dummyTablename,
245 'fieldname' => 'categories'
246 ));
247 $columnMap->setParentKeyFieldName('uid_foreign');
248 $columnMap->setChildKeyFieldName('uid_local');
249
250 $mockDataMap = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMap::class, array('getColumnMap'), array(), '', FALSE);
251 $mockDataMap->expects($this->any())->method('getColumnMap')->will($this->returnValue($columnMap));
252
253 $mockDataMapper = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class, array('convertPropertyNameToColumnName', 'convertClassNameToTableName', 'getDataMap'), array(), '', FALSE);
254 $mockDataMapper->expects($this->any())->method('convertClassNameToTableName')->with($dummyClassname)->will($this->returnValue($dummyTablename));
255 $mockDataMapper->expects($this->any())->method('convertPropertyNameToColumnName')->will($this->returnValue('converted_fieldname'));
256 $mockDataMapper->expects($this->any())->method('getDataMap')->will($this->returnValue($mockDataMap));
257
258 $mockDatabaseHandle = $this->getMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class, array('fullQuoteStr'), array(), '', FALSE);
259 $mockDatabaseHandle->expects($this->at(0))->method('fullQuoteStr')->with($dummyTablename, 'sys_category_record_mm')->will($this->returnValue('\'' . $dummyTablename . '\''));
260 $mockDatabaseHandle->expects($this->at(1))->method('fullQuoteStr')->with('categories', 'sys_category_record_mm')->will($this->returnValue("'categories'"));
261
262 $sql = array();
263 $comparison = new \TYPO3\CMS\Extbase\Persistence\Generic\Qom\Comparison(
264 /* operand1 */
265 new \TYPO3\CMS\Extbase\Persistence\Generic\Qom\PropertyValue($dummyTablename,'categories'),
266 /* operator */
267 \TYPO3\CMS\Extbase\Persistence\QueryInterface::OPERATOR_CONTAINS,
268 /* operand2 */
269 array(1, 3)
270 );
271 $comparison->setParameterIdentifier('categories');
272 $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, array('dummy'), array(), '', FALSE);
273 $mockTypo3DbQueryParser->_set('dataMapper', $mockDataMapper);
274 $mockTypo3DbQueryParser->_set('databaseHandle', $mockDatabaseHandle);
275 $mockTypo3DbQueryParser->_callRef('parseComparison', $comparison, $mockSource, $sql);
276 $expectedSql = array('where' => array($dummyTablename . '.uid IN (SELECT uid_foreign FROM sys_category_record_mm WHERE uid_local IN (:categories) AND tablenames = \'' . $dummyTablename . '\' AND fieldname = \'categories\')'));
277 $this->assertSame($expectedSql, $sql);
278 }
279
280 public function providerForVisibilityConstraintStatement() {
281 return array(
282 'in be: include all' => array('BE', TRUE, array(), TRUE, NULL),
283 'in be: ignore enable fields but do not include deleted' => array('BE', TRUE, array(), FALSE, array('tx_foo_table.deleted_column=0')),
284 '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)')),
285 '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')),
286 'in fe: include all' => array('FE', TRUE, array(), TRUE, NULL),
287 'in fe: ignore enable fields but do not include deleted' => array('FE', TRUE, array(), FALSE, array('tx_foo_table.deleted_column=0')),
288 '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')),
289 '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'))
290 );
291 }
292
293 /**
294 * @test
295 * @dataProvider providerForVisibilityConstraintStatement
296 */
297 public function visibilityConstraintStatementIsGeneratedAccordingToTheQuerySettings($mode, $ignoreEnableFields, $enableFieldsToBeIgnored, $deletedValue, $expectedSql) {
298 $tableName = 'tx_foo_table';
299 $GLOBALS['TCA'][$tableName]['ctrl'] = array(
300 'enablecolumns' => array(
301 'disabled' => 'disabled_column',
302 'starttime' => 'starttime_column'
303 ),
304 'delete' => 'deleted_column'
305 );
306 $GLOBALS['TSFE'] = new \stdClass();
307 $GLOBALS['TSFE']->sys_page = new \TYPO3\CMS\Frontend\Page\PageRepository();
308 $GLOBALS['SIM_ACCESS_TIME'] = 123456789;
309 $mockQuerySettings = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings::class, array('getIgnoreEnableFields', 'getEnableFieldsToBeIgnored', 'getIncludeDeleted'), array(), '', FALSE);
310 $mockQuerySettings->expects($this->once())->method('getIgnoreEnableFields')->will($this->returnValue($ignoreEnableFields));
311 $mockQuerySettings->expects($this->once())->method('getEnableFieldsToBeIgnored')->will($this->returnValue($enableFieldsToBeIgnored));
312 $mockQuerySettings->expects($this->once())->method('getIncludeDeleted')->will($this->returnValue($deletedValue));
313 $sql = array();
314
315 /** @var $mockEnvironmentService \TYPO3\CMS\Extbase\Service\EnvironmentService | \PHPUnit_Framework_MockObject_MockObject */
316 $mockEnvironmentService = $this->getMock(\TYPO3\CMS\Extbase\Service\EnvironmentService::class, array('isEnvironmentInFrontendMode'));
317 $mockEnvironmentService->expects($this->any())->method('isEnvironmentInFrontendMode')->will($this->returnValue($mode == 'FE'));
318
319 $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, array('dummy'), array(), '', FALSE);
320 $mockTypo3DbQueryParser->_set('environmentService', $mockEnvironmentService);
321 $mockTypo3DbQueryParser->_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 FE: respectEnableFields=false' => array('FE', FALSE, NULL),
331 'in FE: respectEnableFields=true' => array('FE', TRUE, array('tx_foo_table.deleted_column=0 AND tx_foo_table.disabled_column=0 AND tx_foo_table.starttime_column<=123456789'))
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 /** @var \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings $mockQuerySettings */
352 $mockQuerySettings = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings::class, array('dummy'), array(), '', FALSE);
353 $mockQuerySettings->setIgnoreEnableFields(!$respectEnableFields);
354 $mockQuerySettings->setIncludeDeleted(!$respectEnableFields);
355 $sql = array();
356
357 /** @var $mockEnvironmentService \TYPO3\CMS\Extbase\Service\EnvironmentService | \PHPUnit_Framework_MockObject_MockObject */
358 $mockEnvironmentService = $this->getMock(\TYPO3\CMS\Extbase\Service\EnvironmentService::class, array('isEnvironmentInFrontendMode'));
359 $mockEnvironmentService->expects($this->any())->method('isEnvironmentInFrontendMode')->will($this->returnValue($mode == 'FE'));
360
361 $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, array('dummy'), array(), '', FALSE);
362 $mockTypo3DbQueryParser->_set('environmentService', $mockEnvironmentService);
363 $mockTypo3DbQueryParser->_callRef('addVisibilityConstraintStatement', $mockQuerySettings, $tableName, $sql);
364 $this->assertSame($expectedSql, $sql['additionalWhereClause']);
365 unset($GLOBALS['TCA'][$tableName]);
366 }
367
368 /**
369 * @test
370 * @expectedException \TYPO3\CMS\Extbase\Persistence\Generic\Exception\InconsistentQuerySettingsException
371 */
372 public function visibilityConstraintStatementGenerationThrowsExceptionIfTheQuerySettingsAreInconsistent() {
373 $tableName = 'tx_foo_table';
374 $GLOBALS['TCA'][$tableName]['ctrl'] = array(
375 'enablecolumns' => array(
376 'disabled' => 'disabled_column'
377 ),
378 'delete' => 'deleted_column'
379 );
380 $mockQuerySettings = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings::class, array('getIgnoreEnableFields', 'getEnableFieldsToBeIgnored', 'getIncludeDeleted'), array(), '', FALSE);
381 $mockQuerySettings->expects($this->once())->method('getIgnoreEnableFields')->will($this->returnValue(FALSE));
382 $mockQuerySettings->expects($this->once())->method('getEnableFieldsToBeIgnored')->will($this->returnValue(array()));
383 $mockQuerySettings->expects($this->once())->method('getIncludeDeleted')->will($this->returnValue(TRUE));
384 $sql = array();
385
386 /** @var $mockEnvironmentService \TYPO3\CMS\Extbase\Service\EnvironmentService | \PHPUnit_Framework_MockObject_MockObject */
387 $mockEnvironmentService = $this->getMock(\TYPO3\CMS\Extbase\Service\EnvironmentService::class, array('isEnvironmentInFrontendMode'));
388 $mockEnvironmentService->expects($this->any())->method('isEnvironmentInFrontendMode')->will($this->returnValue(TRUE));
389
390 $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, array('dummy'), array(), '', FALSE);
391 $mockTypo3DbQueryParser->_set('environmentService', $mockEnvironmentService);
392 $mockTypo3DbQueryParser->_callRef('addVisibilityConstraintStatement', $mockQuerySettings, $tableName, $sql);
393 unset($GLOBALS['TCA'][$tableName]);
394 }
395 /**
396 * DataProvider for addPageIdStatement Tests
397 */
398 public function providerForAddPageIdStatementData() {
399 $table = uniqid('tx_coretest_table');
400 return array(
401 'set Pid to zero if rootLevel = 1' => array(
402 '1',
403 $table,
404 array('additionalWhereClause' => array($table . '.pid = 0'))
405 ),
406 'set Pid to given Pids if rootLevel = 0' => array(
407 '0',
408 $table,
409 array('additionalWhereClause' => array($table . '.pid IN (42, 27)'))
410 ),
411 'set no statement if rootLevel = -1' => array(
412 '-1',
413 $table,
414 array()
415 )
416 );
417 }
418
419 /**
420 * @test
421 * @dataProvider providerForAddPageIdStatementData
422 */
423 public function addPageIdStatementSetsPidToZeroIfTableDeclaresRootlevel($rootLevel, $table, $expectedSql) {
424
425 $GLOBALS['TCA'][$table]['ctrl'] = array(
426 'rootLevel' => $rootLevel
427 );
428 $sql = array();
429 $storagePageIds = array(42,27);
430 $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, array('dummy'), array(), '', FALSE);
431 $mockFrontendVariableCache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class, array(), array(), '', FALSE);
432 $mockTypo3DbQueryParser->_set('tableColumnCache', $mockFrontendVariableCache);
433 $mockFrontendVariableCache->expects($this->once())->method('get')->will($this->returnValue(array('pid' => '42')));
434 $mockTypo3DbQueryParser->_callRef('addPageIdStatement', $table, $sql, $storagePageIds);
435
436 $this->assertSame($expectedSql, $sql);
437 }
438
439 }