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