‪TYPO3CMS  11.5
QueryGeneratorTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
24 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
25 
26 class ‪QueryGeneratorTest extends FunctionalTestCase
27 {
28  protected function ‪setUp(): void
29  {
30  parent::setUp();
31  $this->setUpBackendUserFromFixture(1);
32  ‪$GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->create('default');
33  }
34 
39  {
40  $id = 1;
41  $depth = 0;
42  $subject = $this->getAccessibleMock(QueryGenerator::class, ['dummy'], [], '', false);
43  $treeList = $subject->_call('getTreeList', $id, $depth);
44  self::assertEquals($id, $treeList);
45  }
46 
51  {
52  $id = 0;
53  $depth = 1;
54  $subject = $this->getAccessibleMock(QueryGenerator::class, ['dummy'], [], '', false);
55  $treeList = $subject->_call('getTreeList', $id, $depth);
56  self::assertEquals($id, $treeList);
57  }
58 
63  {
64  $id = -1;
65  $depth = 0;
66  $subject = $this->getAccessibleMock(QueryGenerator::class, ['dummy'], [], '', false);
67  $treeList = $subject->_call('getTreeList', $id, $depth);
68  self::assertEquals(1, $treeList);
69  }
70 
75  {
76  $id = 0;
77  $depth = 0;
78  $begin = 1;
79  $subject = $this->getAccessibleMock(QueryGenerator::class, ['dummy'], [], '', false);
80  $treeList = $subject->_call('getTreeList', $id, $depth, $begin);
81  self::assertSame('', $treeList);
82  }
83 
88  {
89  $id = 1;
90  $depth = 1;
91  $subject = $this->getAccessibleMock(QueryGenerator::class, ['dummy'], [], '', false);
92  $treeList = $subject->_call('getTreeList', $id, $depth);
93  self::assertEquals($id, $treeList);
94  }
95 
99  public function ‪getTreeListRespectsPermClauses(): void
100  {
101  $this->importCSVDataSet(__DIR__ . '/Fixtures/TestGetPageTreeStraightTreeSet.csv');
102  $id = 1;
103  $depth = 99;
104  $subject = $this->getAccessibleMock(QueryGenerator::class, ['dummy'], [], '', false);
105  $treeList = $subject->_call('getTreeList', $id, $depth, 0, 'hidden=0');
106  self::assertSame('1,2,3,4,5', $treeList);
107  }
108 
113  public function ‪getTreeListReturnsListOfIdsWithBeginSetToZero(int $id, int $depth, string $expectation): void
114  {
115  $this->importCSVDataSet(__DIR__ . '/Fixtures/TestGetPageTreeStraightTreeSet.csv');
116  $subject = $this->getAccessibleMock(QueryGenerator::class, ['dummy'], [], '', false);
117  $treeList = $subject->_call('getTreeList', $id, $depth);
118  self::assertSame($expectation, $treeList);
119  }
120 
125  {
126  return [
127  // [$id, $depth, $expectation]
128  [
129  1,
130  1,
131  '1,2',
132  ],
133  [
134  1,
135  2,
136  '1,2,3',
137  ],
138  [
139  1,
140  99,
141  '1,2,3,4,5,6',
142  ],
143  [
144  2,
145  1,
146  '2,3',
147  ],
148  ];
149  }
150 
155  public function ‪getTreeListReturnsListOfIdsWithBeginSetToMinusOne(int $id, int $depth, string $expectation): void
156  {
157  $this->importCSVDataSet(__DIR__ . '/Fixtures/TestGetPageTreeStraightTreeSet.csv');
158  $subject = $this->getAccessibleMock(QueryGenerator::class, ['dummy'], [], '', false);
159  $treeList = $subject->_call('getTreeList', $id, $depth, -1);
160  self::assertSame($expectation, $treeList);
161  }
162 
167  {
168  return [
169  // [$id, $depth, $expectation]
170  [
171  1,
172  1,
173  ',2',
174  ],
175  [
176  1,
177  2,
178  ',2,3',
179  ],
180  [
181  1,
182  99,
183  ',2,3,4,5,6',
184  ],
185  [
186  2,
187  1,
188  ',3',
189  ],
190  ];
191  }
192 
197  {
198  $id = 1;
199  $depth = 3;
200  $this->importCSVDataSet(__DIR__ . '/Fixtures/TestGetPageTreeBranchedTreeSet.csv');
201  $subject = $this->getAccessibleMock(QueryGenerator::class, ['dummy'], [], '', false);
202  $treeList = $subject->_call('getTreeList', $id, $depth);
203  self::assertSame('1,2,3,4,5', $treeList);
204  }
205 
210  {
211  $id = 1;
212  $depth = 3;
213  $begin = 1;
214  $this->importCSVDataSet(__DIR__ . '/Fixtures/TestGetPageTreeBranchedTreeSet.csv');
215  $subject = $this->getAccessibleMock(QueryGenerator::class, ['dummy'], [], '', false);
216  $treeList = $subject->_call('getTreeList', $id, $depth, $begin);
217  self::assertSame('2,3,4,5', $treeList);
218  }
219 
224  {
225  $id = 1;
226  $depth = 3;
227  $begin = 2;
228  $this->importCSVDataSet(__DIR__ . '/Fixtures/TestGetPageTreeBranchedTreeSet.csv');
229  $subject = $this->getAccessibleMock(QueryGenerator::class, ['dummy'], [], '', false);
230  $treeList = $subject->_call('getTreeList', $id, $depth, $begin);
231  self::assertSame('3,5', $treeList);
232  }
233 
234  public function ‪getQueryWithIdOrDateDataProvider(): array
235  {
236  return [
237  'pid 5134' => [
238  5134,
239  null,
240  "pid = '5134'",
241  ],
242  'unix timestamp' => [
243  1522863047,
244  null,
245  "pid = '1522863047'",
246  ],
247  'pid 5134 as string' => [
248  '5134',
249  null,
250  "pid = '5134'",
251  ],
252  'unix timestamp as string' => [
253  '1522863047',
254  null,
255  "pid = '1522863047'",
256  ],
257  'ISO 8601 date string' => [
258  '2018-04-04T17:30:47Z',
259  null,
260  "pid = '1522863047'",
261  ],
262  'pid 5134 and second input value 5135' => [
263  5134,
264  5135,
265  'pid >= 5134 AND pid <= 5135',
266  'comparison' => 100,
267  ],
268  'ISO 8601 date string as first and second input' => [
269  '2018-04-04T17:30:47Z',
270  '2018-04-04T17:30:48Z',
271  'pid >= 1522863047 AND pid <= 1522863048',
272  'comparison' => 100,
273  ],
274  ];
275  }
276 
286  public function ‪getQueryWithIdOrDate($inputValue, $inputValue1, string $expected, int $comparison = 64): void
287  {
288  ‪$GLOBALS['TCA'] = [
289  'aTable' => [
290  'columns' => [],
291  ],
292  ];
293  $inputConf = [
294  [
295  'operator' => '',
296  'type' => 'FIELD_pid',
297  'comparison' => $comparison,
298  'inputValue' => $inputValue,
299  'inputValue1' => $inputValue1,
300  ],
301  ];
302  $subject = $this->getAccessibleMock(QueryGenerator::class, ['dummy'], [], '', false);
303  $subject->_call('init', 'queryConfig', 'aTable');
304  self::assertSame($expected, trim($subject->_call('getQuery', $inputConf), "\n\r"));
305  }
306 
307  public function ‪arbitraryDataIsEscapedDataProvider(): array
308  {
309  $dataSet = [];
310  $injectors = [
311  // INJ'ECT
312  'INJ%quoteCharacter%ECT',
313  // INJ '--
314  // ' ECT
315  'INJ %quoteCharacter%%commentStart% %commentEnd%%quoteCharacter% ECT',
316  ];
317  $subject = $this->getAccessibleMock(QueryGenerator::class, ['dummy'], [], '', false);
318  $comparisons = array_keys($subject->_get('compSQL'));
319  foreach ($injectors as $injector) {
320  foreach ($comparisons as $comparison) {
321  $dataSet[] = [
322  $injector,
323  [
324  'queryTable' => 'tt_content',
325  'queryFields' => 'uid,' . $injector,
326  'queryGroup' => $injector,
327  'queryOrder' => $injector,
328  'queryLimit' => $injector,
329  'queryConfig' => [
330  [
331  'operator' => $injector,
332  'type' => 'FIELD_category_field', // falls back to CType (first field)
333  'comparison' => $comparison,
334  'inputValue' => $injector,
335  ],
336  [
337  'operator' => $injector,
338  'type' => 'FIELD_category_field',
339  'comparison' => $comparison,
340  'inputValue' => $injector,
341  ],
342  ],
343  ],
344  ];
345  }
346  }
347  return $dataSet;
348  }
349 
355  public function ‪arbitraryDataIsEscaped(string $injector, array $settings): void
356  {
357  $databasePlatform = GeneralUtility::makeInstance(ConnectionPool::class)
358  ->getConnectionForTable('tt_content')->getDatabasePlatform();
359  $replacements = [
360  '%quoteCharacter%' => $databasePlatform->getStringLiteralQuoteCharacter(),
361  '%commentStart%' => $databasePlatform->getSqlCommentStartString(),
362  '%commentEnd%' => $databasePlatform->getSqlCommentEndString(),
363  ];
364  $injector = str_replace(array_keys($replacements), $replacements, $injector);
365  $settings = $this->‪prepareSettings($settings, $replacements);
366  $settings['queryConfig'] = serialize($settings['queryConfig']);
367 
368  $subject = $this->getAccessibleMock(QueryGenerator::class, ['dummy'], [], '', false);
369  $subject->_call('init', 'queryConfig', $settings['queryTable']);
370  $subject->_call('makeSelectorTable', $settings);
371  $subject->_set('enablePrefix', true);
372 
373  $queryString = $subject->_call('getQuery', $subject->_get('queryConfig'));
374  $query = $subject->_call('getSelectQuery', $queryString);
375 
376  self::assertStringNotContainsString($injector, $query);
377  }
378 
379  protected function ‪prepareSettings(array $settings, array $replacements): array
380  {
381  foreach ($settings as $settingKey => &$settingValue) {
382  if (is_string($settingValue)) {
383  $settingValue = str_replace(array_keys($replacements), $replacements, $settingValue);
384  }
385  if (is_array($settingValue)) {
386  $settingValue = $this->‪prepareSettings($settingValue, $replacements);
387  }
388  }
389  return $settings;
390  }
391 }
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:25
‪TYPO3\CMS\Lowlevel\Tests\Functional\Database
Definition: QueryGeneratorTest.php:18
‪TYPO3\CMS\Lowlevel\Tests\Functional\Database\QueryGeneratorTest\getQueryWithIdOrDate
‪getQueryWithIdOrDate($inputValue, $inputValue1, string $expected, int $comparison=64)
Definition: QueryGeneratorTest.php:286
‪TYPO3\CMS\Lowlevel\Tests\Functional\Database\QueryGeneratorTest\getTreeListReturnsIncomingIdIfNoSubPageRecordsOfThatIdExist
‪getTreeListReturnsIncomingIdIfNoSubPageRecordsOfThatIdExist()
Definition: QueryGeneratorTest.php:87
‪TYPO3\CMS\Lowlevel\Tests\Functional\Database\QueryGeneratorTest\getTreeListReturnsListOfPageIdsOfABranchedTreeWithBeginSetToOne
‪getTreeListReturnsListOfPageIdsOfABranchedTreeWithBeginSetToOne()
Definition: QueryGeneratorTest.php:209
‪TYPO3\CMS\Lowlevel\Database\QueryGenerator
Definition: QueryGenerator.php:49
‪TYPO3\CMS\Lowlevel\Tests\Functional\Database\QueryGeneratorTest\dataForGetTreeListReturnsListOfIdsWithBeginSetToMinusOne
‪array dataForGetTreeListReturnsListOfIdsWithBeginSetToMinusOne()
Definition: QueryGeneratorTest.php:166
‪TYPO3\CMS\Lowlevel\Tests\Functional\Database\QueryGeneratorTest\getTreeListReturnsListOfPageIdsOfABranchedTreeWithBeginSetToZero
‪getTreeListReturnsListOfPageIdsOfABranchedTreeWithBeginSetToZero()
Definition: QueryGeneratorTest.php:196
‪TYPO3\CMS\Lowlevel\Tests\Functional\Database\QueryGeneratorTest\arbitraryDataIsEscapedDataProvider
‪arbitraryDataIsEscapedDataProvider()
Definition: QueryGeneratorTest.php:307
‪TYPO3\CMS\Lowlevel\Tests\Functional\Database\QueryGeneratorTest\getTreeListRespectsPermClauses
‪getTreeListRespectsPermClauses()
Definition: QueryGeneratorTest.php:99
‪TYPO3\CMS\Lowlevel\Tests\Functional\Database\QueryGeneratorTest\getTreeListReturnsIngoingIdIfIdIsZero
‪getTreeListReturnsIngoingIdIfIdIsZero()
Definition: QueryGeneratorTest.php:50
‪TYPO3\CMS\Lowlevel\Tests\Functional\Database\QueryGeneratorTest\getTreeListReturnsListOfIdsWithBeginSetToMinusOne
‪getTreeListReturnsListOfIdsWithBeginSetToMinusOne(int $id, int $depth, string $expectation)
Definition: QueryGeneratorTest.php:155
‪TYPO3\CMS\Lowlevel\Tests\Functional\Database\QueryGeneratorTest
Definition: QueryGeneratorTest.php:27
‪TYPO3\CMS\Lowlevel\Tests\Functional\Database\QueryGeneratorTest\arbitraryDataIsEscaped
‪arbitraryDataIsEscaped(string $injector, array $settings)
Definition: QueryGeneratorTest.php:355
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Lowlevel\Tests\Functional\Database\QueryGeneratorTest\prepareSettings
‪prepareSettings(array $settings, array $replacements)
Definition: QueryGeneratorTest.php:379
‪TYPO3\CMS\Lowlevel\Tests\Functional\Database\QueryGeneratorTest\setUp
‪setUp()
Definition: QueryGeneratorTest.php:28
‪TYPO3\CMS\Lowlevel\Tests\Functional\Database\QueryGeneratorTest\getTreeListReturnsListOfPageIdsOfABranchedTreeWithBeginSetToTwo
‪getTreeListReturnsListOfPageIdsOfABranchedTreeWithBeginSetToTwo()
Definition: QueryGeneratorTest.php:223
‪TYPO3\CMS\Lowlevel\Tests\Functional\Database\QueryGeneratorTest\getTreeListReturnsListOfIdsWithBeginSetToZero
‪getTreeListReturnsListOfIdsWithBeginSetToZero(int $id, int $depth, string $expectation)
Definition: QueryGeneratorTest.php:113
‪TYPO3\CMS\Lowlevel\Tests\Functional\Database\QueryGeneratorTest\getTreeListReturnsPositiveIngoingIdIfIdIsNegative
‪getTreeListReturnsPositiveIngoingIdIfIdIsNegative()
Definition: QueryGeneratorTest.php:62
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Lowlevel\Tests\Functional\Database\QueryGeneratorTest\getQueryWithIdOrDateDataProvider
‪getQueryWithIdOrDateDataProvider()
Definition: QueryGeneratorTest.php:234
‪TYPO3\CMS\Lowlevel\Tests\Functional\Database\QueryGeneratorTest\dataForGetTreeListReturnsListOfIdsWithBeginSetToZero
‪array dataForGetTreeListReturnsListOfIdsWithBeginSetToZero()
Definition: QueryGeneratorTest.php:124
‪TYPO3\CMS\Lowlevel\Tests\Functional\Database\QueryGeneratorTest\getTreeListReturnsIngoingIdIfDepthIsZero
‪getTreeListReturnsIngoingIdIfDepthIsZero()
Definition: QueryGeneratorTest.php:38
‪TYPO3\CMS\Lowlevel\Tests\Functional\Database\QueryGeneratorTest\getTreeListReturnsEmptyStringIfIdAndDepthAreZeroAndBeginDoesNotEqualZero
‪getTreeListReturnsEmptyStringIfIdAndDepthAreZeroAndBeginDoesNotEqualZero()
Definition: QueryGeneratorTest.php:74