‪TYPO3CMS  10.4
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 
29 class ‪QueryGeneratorTest extends FunctionalTestCase
30 {
31  protected function ‪setUp(): void
32  {
33  parent::setUp();
34  $this->setUpBackendUserFromFixture(1);
35  ‪$GLOBALS['LANG'] = ‪LanguageService::create('default');
36  }
37 
42  {
43  $id = 1;
44  $depth = 0;
45 
46  $queryGenerator = new ‪QueryGenerator();
47  $treeList = $queryGenerator->getTreeList($id, $depth);
48 
49  self::assertSame($id, $treeList);
50  }
51 
56  {
57  $id = 0;
58  $depth = 1;
59 
60  $queryGenerator = new ‪QueryGenerator();
61  $treeList = $queryGenerator->getTreeList($id, $depth);
62 
63  self::assertSame($id, $treeList);
64  }
65 
70  {
71  $id = -1;
72  $depth = 0;
73 
74  $queryGenerator = new ‪QueryGenerator();
75  $treeList = $queryGenerator->getTreeList($id, $depth);
76 
77  self::assertSame(1, $treeList);
78  }
79 
84  {
85  $id = 0;
86  $depth = 0;
87  $begin = 1;
88 
89  $queryGenerator = new ‪QueryGenerator();
90  $treeList = $queryGenerator->getTreeList($id, $depth, $begin);
91 
92  self::assertSame('', $treeList);
93  }
94 
99  {
100  $id = 1;
101  $depth = 1;
102 
103  $queryGenerator = new ‪QueryGenerator();
104  $treeList = $queryGenerator->getTreeList($id, $depth);
105 
106  self::assertSame($id, $treeList);
107  }
108 
112  public function ‪getTreeListRespectsPermClauses(): void
113  {
114  $this->importCSVDataSet(__DIR__ . '/Fixtures/DataSet/TestGetPageTreeStraightTreeSet.csv');
115 
116  $id = 1;
117  $depth = 99;
118 
119  $queryGenerator = new ‪QueryGenerator();
120  $treeList = $queryGenerator->getTreeList($id, $depth, 0, 'hidden = 0');
121 
122  self::assertSame('1,2,3,4,5', $treeList);
123  }
124 
132  public function ‪getTreeListReturnsListOfIdsWithBeginSetToZero(int $id, int $depth, $expectation): void
133  {
134  $this->importCSVDataSet(__DIR__ . '/Fixtures/DataSet/TestGetPageTreeStraightTreeSet.csv');
135 
136  $queryGenerator = new ‪QueryGenerator();
137  $treeList = $queryGenerator->getTreeList($id, $depth);
138 
139  self::assertSame($expectation, $treeList);
140  }
141 
146  {
147  return [
148  // [$id, $depth, $expectation]
149  [
150  1,
151  1,
152  '1,2'
153  ],
154  [
155  1,
156  2,
157  '1,2,3'
158  ],
159  [
160  1,
161  99,
162  '1,2,3,4,5,6'
163  ],
164  [
165  2,
166  1,
167  '2,3'
168  ],
169  ];
170  }
171 
179  public function ‪getTreeListReturnsListOfIdsWithBeginSetToMinusOne(int $id, int $depth, $expectation): void
180  {
181  $this->importCSVDataSet(__DIR__ . '/Fixtures/DataSet/TestGetPageTreeStraightTreeSet.csv');
182 
183  $queryGenerator = new ‪QueryGenerator();
184  $treeList = $queryGenerator->getTreeList($id, $depth, -1);
185 
186  self::assertSame($expectation, $treeList);
187  }
188 
193  {
194  return [
195  // [$id, $depth, $expectation]
196  [
197  1,
198  1,
199  ',2'
200  ],
201  [
202  1,
203  2,
204  ',2,3'
205  ],
206  [
207  1,
208  99,
209  ',2,3,4,5,6'
210  ],
211  [
212  2,
213  1,
214  ',3'
215  ],
216  ];
217  }
218 
223  {
224  $id = 1;
225  $depth = 3;
226 
227  $this->importCSVDataSet(__DIR__ . '/Fixtures/DataSet/TestGetPageTreeBranchedTreeSet.csv');
228 
229  $queryGenerator = new ‪QueryGenerator();
230  $treeList = $queryGenerator->getTreeList($id, $depth);
231 
232  self::assertSame('1,2,3,4,5', $treeList);
233  }
234 
239  {
240  $id = 1;
241  $depth = 3;
242  $begin = 1;
243 
244  $this->importCSVDataSet(__DIR__ . '/Fixtures/DataSet/TestGetPageTreeBranchedTreeSet.csv');
245 
246  $queryGenerator = new ‪QueryGenerator();
247  $treeList = $queryGenerator->getTreeList($id, $depth, $begin);
248 
249  self::assertSame('2,3,4,5', $treeList);
250  }
251 
256  {
257  $id = 1;
258  $depth = 3;
259  $begin = 2;
260 
261  $this->importCSVDataSet(__DIR__ . '/Fixtures/DataSet/TestGetPageTreeBranchedTreeSet.csv');
262 
263  $queryGenerator = new ‪QueryGenerator();
264  $treeList = $queryGenerator->getTreeList($id, $depth, $begin);
265 
266  self::assertSame('3,5', $treeList);
267  }
268 
269  public function ‪getQueryWithIdOrDateDataProvider(): array
270  {
271  return [
272  'pid 5134' => [
273  5134,
274  null,
275  "pid = '5134'",
276  ],
277  'unix timestamp' => [
278  1522863047,
279  null,
280  "pid = '1522863047'",
281  ],
282  'pid 5134 as string' => [
283  '5134',
284  null,
285  "pid = '5134'",
286  ],
287  'unix timestamp as string' => [
288  '1522863047',
289  null,
290  "pid = '1522863047'",
291  ],
292  'ISO 8601 date string' => [
293  '2018-04-04T17:30:47Z',
294  null,
295  "pid = '1522863047'",
296  ],
297  'pid 5134 and second input value 5135' => [
298  5134,
299  5135,
300  'pid >= 5134 AND pid <= 5135',
301  'comparison' => 100,
302  ],
303  'ISO 8601 date string as first and second input' => [
304  '2018-04-04T17:30:47Z',
305  '2018-04-04T17:30:48Z',
306  'pid >= 1522863047 AND pid <= 1522863048',
307  'comparison' => 100,
308  ],
309  ];
310  }
311 
321  public function ‪getQueryWithIdOrDate($inputValue, $inputValue1, string $expected, int $comparison = 64)
322  {
323  ‪$GLOBALS['TCA'] = [];
324  ‪$GLOBALS['TCA']['aTable'] = [];
325  $queryGenerator = new ‪QueryGenerator();
326 
327  $inputConf = [
328  [
329  'operator' => '',
330  'type' => 'FIELD_pid',
331  'comparison' => $comparison,
332  'inputValue' => $inputValue,
333  'inputValue1' => $inputValue1,
334  ],
335  ];
336 
337  $queryGenerator->init('queryConfig', 'aTable');
338  self::assertSame($expected, trim($queryGenerator->getQuery($inputConf), "\n\r"));
339  }
340 
341  public function ‪arbitraryDataIsEscapedDataProvider(): array
342  {
343  $dataSet = [];
344  $injectors = [
345  // INJ'ECT
346  'INJ%quoteCharacter%ECT',
347  // INJ '--
348  // ' ECT
349  'INJ %quoteCharacter%%commentStart% %commentEnd%%quoteCharacter% ECT'
350  ];
351  $comparisons = array_keys((new ‪QueryGenerator())->compSQL);
352  foreach ($injectors as $injector) {
353  foreach ($comparisons as $comparison) {
354  $dataSet[] = [
355  $injector,
356  [
357  'queryTable' => 'tt_content',
358  'queryFields' => 'uid,' . $injector,
359  'queryGroup' => $injector,
360  'queryOrder' => $injector,
361  'queryLimit' => $injector,
362  'queryConfig' => serialize([
363  [
364  'operator' => $injector,
365  'type' => 'FIELD_category_field', // falls back to CType (first field)
366  'comparison' => $comparison,
367  'inputValue' => $injector,
368 
369  ],
370  [
371  'operator' => $injector,
372  'type' => 'FIELD_category_field',
373  'comparison' => $comparison,
374  'inputValue' => $injector,
375 
376  ],
377  ]),
378  ],
379  ];
380  }
381  }
382  return $dataSet;
383  }
384 
393  public function ‪arbitraryDataIsEscaped(string $injector, array $settings)
394  {
395  $databasePlatform = GeneralUtility::makeInstance(ConnectionPool::class)
396  ->getConnectionForTable('tt_content')->getDatabasePlatform();
397  $replacements = [
398  '%quoteCharacter%' => $databasePlatform->getStringLiteralQuoteCharacter(),
399  '%commentStart%' => $databasePlatform->getSqlCommentStartString(),
400  '%commentEnd%' => $databasePlatform->getSqlCommentEndString()
401  ];
402  $injector = str_replace(array_keys($replacements), $replacements, $injector);
403  $settings = $this->‪prepareSettings($settings, $replacements);
404 
405  $queryGenerator = new ‪QueryGenerator();
406  $queryGenerator->init('queryConfig', $settings['queryTable']);
407  $queryGenerator->makeSelectorTable($settings);
408  $queryGenerator->enablePrefix = true;
409 
410  $queryString = $queryGenerator->getQuery($queryGenerator->queryConfig);
411  $query = $queryGenerator->getSelectQuery($queryString);
412 
413  self::assertStringNotContainsString($injector, $query);
414  }
415 
416  protected function ‪prepareSettings(array $settings, array $replacements): array
417  {
418  foreach ($settings as $settingKey => &$settingValue) {
419  if (is_string($settingValue)) {
420  $settingValue = str_replace(array_keys($replacements), $replacements, $settingValue);
421  }
422  if (is_array($settingValue)) {
423  $settingValue = $this->‪prepareSettings($settingValue, $replacements);
424  }
425  }
426  return $settings;
427  }
428 }
‪TYPO3\CMS\Core\Tests\Functional\Database\QueryGeneratorTest\prepareSettings
‪prepareSettings(array $settings, array $replacements)
Definition: QueryGeneratorTest.php:416
‪TYPO3\CMS\Core\Tests\Functional\Database\QueryGeneratorTest\getTreeListReturnsListOfIdsWithBeginSetToMinusOne
‪getTreeListReturnsListOfIdsWithBeginSetToMinusOne(int $id, int $depth, $expectation)
Definition: QueryGeneratorTest.php:179
‪TYPO3\CMS\Core\Tests\Functional\Database\QueryGeneratorTest\arbitraryDataIsEscapedDataProvider
‪arbitraryDataIsEscapedDataProvider()
Definition: QueryGeneratorTest.php:341
‪TYPO3\CMS\Core\Tests\Functional\Database\QueryGeneratorTest\getTreeListReturnsListOfIdsWithBeginSetToZero
‪getTreeListReturnsListOfIdsWithBeginSetToZero(int $id, int $depth, $expectation)
Definition: QueryGeneratorTest.php:132
‪TYPO3\CMS\Core\Tests\Functional\Database\QueryGeneratorTest\getQueryWithIdOrDateDataProvider
‪getQueryWithIdOrDateDataProvider()
Definition: QueryGeneratorTest.php:269
‪TYPO3\CMS\Core\Tests\Functional\Database\QueryGeneratorTest\arbitraryDataIsEscaped
‪arbitraryDataIsEscaped(string $injector, array $settings)
Definition: QueryGeneratorTest.php:393
‪TYPO3\CMS\Core\Tests\Functional\Database\QueryGeneratorTest\getTreeListReturnsIngoingIdIfDepthIsZero
‪getTreeListReturnsIngoingIdIfDepthIsZero()
Definition: QueryGeneratorTest.php:41
‪TYPO3\CMS\Core\Database\QueryGenerator
Definition: QueryGenerator.php:32
‪TYPO3\CMS\Core\Tests\Functional\Database\QueryGeneratorTest\setUp
‪setUp()
Definition: QueryGeneratorTest.php:31
‪TYPO3\CMS\Core\Tests\Functional\Database
‪TYPO3\CMS\Core\Tests\Functional\Database\QueryGeneratorTest
Definition: QueryGeneratorTest.php:30
‪TYPO3\CMS\Core\Tests\Functional\Database\QueryGeneratorTest\getQueryWithIdOrDate
‪getQueryWithIdOrDate($inputValue, $inputValue1, string $expected, int $comparison=64)
Definition: QueryGeneratorTest.php:321
‪TYPO3\CMS\Core\Tests\Functional\Database\QueryGeneratorTest\getTreeListReturnsEmptyStringIfIdAndDepthAreZeroAndBeginDoesNotEqualZero
‪getTreeListReturnsEmptyStringIfIdAndDepthAreZeroAndBeginDoesNotEqualZero()
Definition: QueryGeneratorTest.php:83
‪TYPO3\CMS\Core\Tests\Functional\Database\QueryGeneratorTest\getTreeListReturnsListOfPageIdsOfABranchedTreeWithBeginSetToTwo
‪getTreeListReturnsListOfPageIdsOfABranchedTreeWithBeginSetToTwo()
Definition: QueryGeneratorTest.php:255
‪TYPO3\CMS\Core\Tests\Functional\Database\QueryGeneratorTest\getTreeListReturnsListOfPageIdsOfABranchedTreeWithBeginSetToZero
‪getTreeListReturnsListOfPageIdsOfABranchedTreeWithBeginSetToZero()
Definition: QueryGeneratorTest.php:222
‪TYPO3\CMS\Core\Tests\Functional\Database\QueryGeneratorTest\dataForGetTreeListReturnsListOfIdsWithBeginSetToMinusOne
‪array dataForGetTreeListReturnsListOfIdsWithBeginSetToMinusOne()
Definition: QueryGeneratorTest.php:192
‪TYPO3\CMS\Core\Tests\Functional\Database\QueryGeneratorTest\getTreeListReturnsPositiveIngoingIdIfIdIsNegative
‪getTreeListReturnsPositiveIngoingIdIfIdIsNegative()
Definition: QueryGeneratorTest.php:69
‪TYPO3\CMS\Core\Tests\Functional\Database\QueryGeneratorTest\getTreeListReturnsIngoingIdIfIdIsZero
‪getTreeListReturnsIngoingIdIfIdIsZero()
Definition: QueryGeneratorTest.php:55
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Tests\Functional\Database\QueryGeneratorTest\getTreeListReturnsIncomingIdIfNoSubPageRecordsOfThatIdExist
‪getTreeListReturnsIncomingIdIfNoSubPageRecordsOfThatIdExist()
Definition: QueryGeneratorTest.php:98
‪TYPO3\CMS\Core\Tests\Functional\Database\QueryGeneratorTest\getTreeListRespectsPermClauses
‪getTreeListRespectsPermClauses()
Definition: QueryGeneratorTest.php:112
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Tests\Functional\Database\QueryGeneratorTest\getTreeListReturnsListOfPageIdsOfABranchedTreeWithBeginSetToOne
‪getTreeListReturnsListOfPageIdsOfABranchedTreeWithBeginSetToOne()
Definition: QueryGeneratorTest.php:238
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Tests\Functional\Database\QueryGeneratorTest\dataForGetTreeListReturnsListOfIdsWithBeginSetToZero
‪array dataForGetTreeListReturnsListOfIdsWithBeginSetToZero()
Definition: QueryGeneratorTest.php:145
‪TYPO3\CMS\Core\Localization\LanguageService\create
‪static static create(string $locale)
Definition: LanguageService.php:430