TYPO3 CMS  TYPO3_7-6
Typo3DbQueryParserTest.php
Go to the documentation of this file.
1 <?php
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 
19 
21 {
25  public function addSysLanguageStatementWorksForDefaultLanguage()
26  {
27  $table = $this->getUniqueId('tx_coretest_table');
28  $GLOBALS['TCA'][$table]['ctrl'] = [
29  'languageField' => 'sys_language_uid'
30  ];
32  $querySettings = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings::class);
33  $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, ['dummy'], [], '', false);
34  $sql = $mockTypo3DbQueryParser->_callRef('getSysLanguageStatement', $table, $table, $querySettings);
35  $expectedSql = '(' . $table . '.sys_language_uid IN (0,-1))';
36  $this->assertSame($expectedSql, $sql);
37  }
38 
42  public function addSysLanguageStatementWorksForNonDefaultLanguage()
43  {
44  $table = $this->getUniqueId('tx_coretest_table');
45  $GLOBALS['TCA'][$table]['ctrl'] = [
46  'languageField' => 'sys_language_uid'
47  ];
49  $querySettings = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings::class, ['dummy']);
50  $querySettings->setLanguageUid('1');
51  $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, ['dummy'], [], '', false);
52  $sql = $mockTypo3DbQueryParser->_callRef('getSysLanguageStatement', $table, $table, $querySettings);
53  $result = '(' . $table . '.sys_language_uid IN (1,-1))';
54  $this->assertSame($result, $sql);
55  }
56 
61  {
62  $table = $this->getUniqueId('tx_coretest_table');
63  $GLOBALS['TCA'][$table]['ctrl'] = [
64  'languageField' => 'sys_language_uid'
65  ];
66  $querySettings = new \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings();
67  $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, ['dummy'], [], '', false);
68  $sql = $mockTypo3DbQueryParser->_callRef('getSysLanguageStatement', $table, $table, $querySettings);
69  $expectedSql = '(' . $table . '.sys_language_uid IN (0,-1))';
70  $this->assertSame($expectedSql, $sql);
71  }
72 
77  {
78  $table = $this->getUniqueId('tx_coretest_table');
79  $GLOBALS['TCA'][$table]['ctrl'] = [
80  'languageField' => 'sys_language_uid',
81  'delete' => 'deleted'
82  ];
83  $querySettings = new \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings();
84  $querySettings->setLanguageUid(0);
85  $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, ['dummy'], [], '', false);
86  $sql = $mockTypo3DbQueryParser->_callRef('getSysLanguageStatement', $table, $table, $querySettings);
87  $expectedSql = '(' . $table . '.sys_language_uid IN (0,-1))';
88  $this->assertSame($expectedSql, $sql);
89  }
90 
95  {
96  $table = $this->getUniqueId('tx_coretest_table');
97  $GLOBALS['TCA'][$table]['ctrl'] = [
98  'languageField' => 'sys_language_uid'
99  ];
100  $querySettings = new \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings();
101  $querySettings->setLanguageUid(2);
102  $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, ['dummy'], [], '', false);
103  $sql = $mockTypo3DbQueryParser->_callRef('getSysLanguageStatement', $table, $table, $querySettings);
104  $expectedSql = '(' . $table . '.sys_language_uid IN (2,-1))';
105  $this->assertSame($expectedSql, $sql);
106  }
107 
112  {
113  $table = $this->getUniqueId('tx_coretest_table');
114  $GLOBALS['TCA'][$table]['ctrl'] = [
115  'languageField' => 'sys_language_uid',
116  'transOrigPointerField' => 'l10n_parent'
117  ];
118  $querySettings = new \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings();
119  $querySettings->setLanguageUid(2);
120  $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, ['dummy'], [], '', false);
121  $sql = $mockTypo3DbQueryParser->_callRef('getSysLanguageStatement', $table, $table, $querySettings);
122  $expectedSql = '(' . $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)))';
123  $this->assertSame($expectedSql, $sql);
124  }
125 
130  {
131  $table = $this->getUniqueId('tx_coretest_table');
132  $GLOBALS['TCA'][$table]['ctrl'] = [
133  'languageField' => 'sys_language_uid',
134  'transOrigPointerField' => 'l10n_parent',
135  'delete' => 'deleted'
136  ];
137  $querySettings = new \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings();
138  $querySettings->setLanguageUid(2);
139  $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, ['dummy'], [], '', false);
140  $sql= $mockTypo3DbQueryParser->_callRef('getSysLanguageStatement', $table, $table, $querySettings);
141  $expectedSql = '(' . $table . '.sys_language_uid IN (2,-1)' .
142  ' OR (' . $table . '.sys_language_uid=0 AND ' . $table . '.uid NOT IN (' .
143  'SELECT ' . $table . '.l10n_parent FROM ' . $table .
144  ' WHERE ' . $table . '.l10n_parent>0 AND ' .
145  $table . '.sys_language_uid=2 AND ' .
146  $table . '.deleted=0)))';
147  $this->assertSame($expectedSql, $sql);
148  }
149 
154  {
155  $table = 'tt_content';
156  $GLOBALS['TCA'][$table]['ctrl'] = [
157  'languageField' => 'sys_language_uid',
158  'transOrigPointerField' => 'l10n_parent',
159  'delete' => 'deleted'
160  ];
161  $querySettings = new \TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings();
162  $querySettings->setLanguageUid(2);
163  $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, ['dummy'], [], '', false);
164  $sql = $mockTypo3DbQueryParser->_callRef('getSysLanguageStatement', $table, $table, $querySettings);
165  $expectedSql = '(' . $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  $this->assertSame($expectedSql, $sql);
172  }
173 
178  {
179  $mockSource = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\Selector::class, ['getNodeTypeName'], [], '', 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, ['convertPropertyNameToColumnName', 'convertClassNameToTableName'], [], '', 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 = [];
186  $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, ['dummy'], [], '', false);
187  $mockTypo3DbQueryParser->_set('dataMapper', $mockDataMapper);
188  $mockTypo3DbQueryParser->_callRef('parseOrderings', $orderings, $mockSource, $sql);
189  $expectedSql = ['orderings' => ['tx_myext_tablename.converted_fieldname ASC']];
190  $this->assertSame($expectedSql, $sql);
191  }
192 
198  {
199  $mockSource = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\Selector::class, ['getNodeTypeName'], [], '', false);
200  $mockSource->expects($this->never())->method('getNodeTypeName');
201  $mockDataMapper = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class, ['convertPropertyNameToColumnName', 'convertClassNameToTableName'], [], '', false);
202  $mockDataMapper->expects($this->never())->method('convertClassNameToTableName');
203  $mockDataMapper->expects($this->never())->method('convertPropertyNameToColumnName');
204  $sql = [];
205  $orderings = ['fooProperty' => 'unsupported_order'];
206  $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, ['dummy'], [], '', false);
207  $mockTypo3DbQueryParser->_set('dataMapper', $mockDataMapper);
208  $mockTypo3DbQueryParser->_callRef('parseOrderings', $orderings, $mockSource, $sql);
209  }
210 
215  {
216  $mockSource = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Qom\Selector::class, ['getNodeTypeName'], [], '', false);
217  $mockSource->expects($this->any())->method('getNodeTypeName')->will($this->returnValue('Tx_MyExt_ClassName'));
218  $mockDataMapper = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class, ['convertPropertyNameToColumnName', 'convertClassNameToTableName'], [], '', false);
219  $mockDataMapper->expects($this->any())->method('convertClassNameToTableName')->with('Tx_MyExt_ClassName')->will($this->returnValue('tx_myext_tablename'));
220  $mockDataMapper->expects($this->any())->method('convertPropertyNameToColumnName')->will($this->returnValue('converted_fieldname'));
221  $sql = [];
222  $orderings = [
225  ];
226  $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, ['dummy'], [], '', false);
227  $mockTypo3DbQueryParser->_set('dataMapper', $mockDataMapper);
228  $mockTypo3DbQueryParser->_callRef('parseOrderings', $orderings, $mockSource, $sql);
229  $expectedSql = ['orderings' => ['tx_myext_tablename.converted_fieldname ASC', 'tx_myext_tablename.converted_fieldname DESC']];
230  $this->assertSame($expectedSql, $sql);
231  }
232 
234  {
235  return [
236  'in be: include all' => ['BE', true, [], true, ''],
237  'in be: ignore enable fields but do not include deleted' => ['BE', true, [], false, 'tx_foo_table.deleted_column=0'],
238  'in be: respect enable fields but include deleted' => ['BE', false, [], true, 'tx_foo_table.disabled_column=0 AND (tx_foo_table.starttime_column<=123456789)'],
239  'in be: respect enable fields and do not include deleted' => ['BE', false, [], false, 'tx_foo_table.disabled_column=0 AND (tx_foo_table.starttime_column<=123456789) AND tx_foo_table.deleted_column=0'],
240  'in fe: include all' => ['FE', true, [], true, ''],
241  'in fe: ignore enable fields but do not include deleted' => ['FE', true, [], false, 'tx_foo_table.deleted_column=0'],
242  'in fe: ignore only starttime and do not include deleted' => ['FE', true, ['starttime'], false, 'tx_foo_table.deleted_column=0 AND tx_foo_table.disabled_column=0'],
243  'in fe: respect enable fields and do not include deleted' => ['FE', false, [], false, 'tx_foo_table.deleted_column=0 AND tx_foo_table.disabled_column=0 AND tx_foo_table.starttime_column<=123456789']
244  ];
245  }
246 
251  public function visibilityConstraintStatementIsGeneratedAccordingToTheQuerySettings($mode, $ignoreEnableFields, $enableFieldsToBeIgnored, $deletedValue, $expectedSql)
252  {
253  $tableName = 'tx_foo_table';
254  $GLOBALS['TCA'][$tableName]['ctrl'] = [
255  'enablecolumns' => [
256  'disabled' => 'disabled_column',
257  'starttime' => 'starttime_column'
258  ],
259  'delete' => 'deleted_column'
260  ];
261  $GLOBALS['TSFE'] = new \stdClass();
262  $GLOBALS['TSFE']->sys_page = new \TYPO3\CMS\Frontend\Page\PageRepository();
263  $GLOBALS['SIM_ACCESS_TIME'] = 123456789;
264  $mockQuerySettings = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings::class, ['getIgnoreEnableFields', 'getEnableFieldsToBeIgnored', 'getIncludeDeleted'], [], '', false);
265  $mockQuerySettings->expects($this->once())->method('getIgnoreEnableFields')->will($this->returnValue($ignoreEnableFields));
266  $mockQuerySettings->expects($this->once())->method('getEnableFieldsToBeIgnored')->will($this->returnValue($enableFieldsToBeIgnored));
267  $mockQuerySettings->expects($this->once())->method('getIncludeDeleted')->will($this->returnValue($deletedValue));
268 
270  $mockEnvironmentService = $this->getMock(\TYPO3\CMS\Extbase\Service\EnvironmentService::class, ['isEnvironmentInFrontendMode']);
271  $mockEnvironmentService->expects($this->any())->method('isEnvironmentInFrontendMode')->will($this->returnValue($mode == 'FE'));
272 
273  $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, ['dummy'], [], '', false);
274  $mockTypo3DbQueryParser->_set('environmentService', $mockEnvironmentService);
275  $resultSql = $mockTypo3DbQueryParser->_callRef('getVisibilityConstraintStatement', $mockQuerySettings, $tableName, $tableName);
276  $this->assertSame($expectedSql, $resultSql);
277  unset($GLOBALS['TCA'][$tableName]);
278  }
279 
281  {
282  return [
283  'in be: respectEnableFields=false' => ['BE', false, ''],
284  'in be: respectEnableFields=true' => ['BE', true, 'tx_foo_table.disabled_column=0 AND (tx_foo_table.starttime_column<=123456789) AND tx_foo_table.deleted_column=0'],
285  'in FE: respectEnableFields=false' => ['FE', false, ''],
286  'in FE: respectEnableFields=true' => ['FE', true, 'tx_foo_table.deleted_column=0 AND tx_foo_table.disabled_column=0 AND tx_foo_table.starttime_column<=123456789']
287  ];
288  }
289 
294  public function respectEnableFieldsSettingGeneratesCorrectStatement($mode, $respectEnableFields, $expectedSql)
295  {
296  $tableName = 'tx_foo_table';
297  $GLOBALS['TCA'][$tableName]['ctrl'] = [
298  'enablecolumns' => [
299  'disabled' => 'disabled_column',
300  'starttime' => 'starttime_column'
301  ],
302  'delete' => 'deleted_column'
303  ];
304  $GLOBALS['TSFE'] = new \stdClass();
305  $GLOBALS['TSFE']->sys_page = new \TYPO3\CMS\Frontend\Page\PageRepository();
306  $GLOBALS['SIM_ACCESS_TIME'] = 123456789;
308  $mockQuerySettings = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings::class, ['dummy'], [], '', false);
309  $mockQuerySettings->setIgnoreEnableFields(!$respectEnableFields);
310  $mockQuerySettings->setIncludeDeleted(!$respectEnableFields);
311 
313  $mockEnvironmentService = $this->getMock(\TYPO3\CMS\Extbase\Service\EnvironmentService::class, ['isEnvironmentInFrontendMode']);
314  $mockEnvironmentService->expects($this->any())->method('isEnvironmentInFrontendMode')->will($this->returnValue($mode == 'FE'));
315 
316  $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, ['dummy'], [], '', false);
317  $mockTypo3DbQueryParser->_set('environmentService', $mockEnvironmentService);
318  $actualSql = $mockTypo3DbQueryParser->_callRef('getVisibilityConstraintStatement', $mockQuerySettings, $tableName, $tableName);
319  $this->assertSame($expectedSql, $actualSql);
320  unset($GLOBALS['TCA'][$tableName]);
321  }
322 
327  public function visibilityConstraintStatementGenerationThrowsExceptionIfTheQuerySettingsAreInconsistent()
328  {
329  $tableName = 'tx_foo_table';
330  $GLOBALS['TCA'][$tableName]['ctrl'] = [
331  'enablecolumns' => [
332  'disabled' => 'disabled_column'
333  ],
334  'delete' => 'deleted_column'
335  ];
336  $mockQuerySettings = $this->getMock(\TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings::class, ['getIgnoreEnableFields', 'getEnableFieldsToBeIgnored', 'getIncludeDeleted'], [], '', false);
337  $mockQuerySettings->expects($this->once())->method('getIgnoreEnableFields')->will($this->returnValue(false));
338  $mockQuerySettings->expects($this->once())->method('getEnableFieldsToBeIgnored')->will($this->returnValue([]));
339  $mockQuerySettings->expects($this->once())->method('getIncludeDeleted')->will($this->returnValue(true));
340 
342  $mockEnvironmentService = $this->getMock(\TYPO3\CMS\Extbase\Service\EnvironmentService::class, ['isEnvironmentInFrontendMode']);
343  $mockEnvironmentService->expects($this->any())->method('isEnvironmentInFrontendMode')->will($this->returnValue(true));
344 
345  $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, ['dummy'], [], '', false);
346  $mockTypo3DbQueryParser->_set('environmentService', $mockEnvironmentService);
347  $mockTypo3DbQueryParser->_callRef('getVisibilityConstraintStatement', $mockQuerySettings, $tableName, $tableName);
348  unset($GLOBALS['TCA'][$tableName]);
349  }
350 
355  {
356  $table = $this->getUniqueId('tx_coretest_table');
357  return [
358  'set Pid to zero if rootLevel = 1' => [
359  '1',
360  $table,
361  $table . '.pid = 0'
362  ],
363  'set Pid to given Pids if rootLevel = 0' => [
364  '0',
365  $table,
366  $table . '.pid IN (42,27)'
367  ],
368  'add 0 to given Pids if rootLevel = -1' => [
369  '-1',
370  $table,
371  $table . '.pid IN (42,27,0)'
372  ],
373  'set Pid to zero if rootLevel = -1 and no further pids given' => [
374  '-1',
375  $table,
376  $table . '.pid = 0',
377  []
378  ],
379  'set no statement for invalid configuration' => [
380  '2',
381  $table,
382  ''
383  ]
384  ];
385  }
386 
391  public function addPageIdStatementSetsPidToZeroIfTableDeclaresRootlevel($rootLevel, $table, $expectedSql, $storagePageIds = [42, 27])
392  {
393  $GLOBALS['TCA'][$table]['ctrl'] = [
394  'rootLevel' => $rootLevel
395  ];
396  $mockTypo3DbQueryParser = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Persistence\Generic\Storage\Typo3DbQueryParser::class, ['dummy'], [], '', false);
397  $mockDatabaseHandle = $this->prophesize(DatabaseConnection::class);
398  $mockDatabaseHandle->cleanIntArray(Argument::cetera())->willReturnArgument(0);
399  $mockTypo3DbQueryParser->_set('databaseHandle', $mockDatabaseHandle->reveal());
400  $sql = $mockTypo3DbQueryParser->_callRef('getPageIdStatement', $table, $table, $storagePageIds);
401 
402  $this->assertSame($expectedSql, $sql);
403  }
404 }
addPageIdStatementSetsPidToZeroIfTableDeclaresRootlevel($rootLevel, $table, $expectedSql, $storagePageIds=[42, 27])
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
$sql
Definition: server.php:84