‪TYPO3CMS  11.5
TcaPreparationTest.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 
20 use Prophecy\Argument;
21 use Prophecy\PhpUnit\ProphecyTrait;
27 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
28 
32 class ‪TcaPreparationTest extends UnitTestCase
33 {
34  use ProphecyTrait;
35 
43  public function ‪configureCategoryRelations(array $input, array $expected): void
44  {
45  self::assertEquals($expected, (new ‪TcaPreparation())->prepare($input));
46  }
47 
48  public function ‪configureCategoryRelationsDataProvider(): \Generator
49  {
50  yield 'No category field' => [
51  [
52  'aTable' => [
53  'columns' => [
54  'foo' => [
55  'config' => [
56  'type' => 'select',
57  'foreign_table' => 'sys_category',
58  ],
59  ],
60  ],
61  ],
62  ],
63  [
64  'aTable' => [
65  'columns' => [
66  'foo' => [
67  'config' => [
68  'type' => 'select',
69  'foreign_table' => 'sys_category',
70  ],
71  ],
72  ],
73  ],
74  ],
75  ];
76  yield 'category field without relationship given (falls back to manyToMany)' => [
77  [
78  'aTable' => [
79  'columns' => [
80  'aField' => [
81  'config' => [
82  'type' => 'category',
83  'minitems' => 1,
84  ],
85  ],
86  ],
87  ],
88  ],
89  [
90  'aTable' => [
91  'columns' => [
92  'aField' => [
93  'config' => [
94  'type' => 'category',
95  'minitems' => 1,
96  'size' => 20,
97  'default' => 0,
98  'foreign_table' => 'sys_category',
99  'foreign_table_where' => ' AND {#sys_category}.{#sys_language_uid} IN (-1, 0)',
100  'relationship' => 'manyToMany',
101  'maxitems' => 99999,
102  'MM' => 'sys_category_record_mm',
103  'MM_opposite_field' => 'items',
104  'MM_match_fields' => [
105  'tablenames' => 'aTable',
106  'fieldname' => 'aField',
107  ],
108  ],
109  'label' => 'LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:sys_category.categories',
110  'exclude' => true,
111  ],
112  ],
113  ],
114  'sys_category' => [
115  'columns' => [
116  'items' => [
117  'config' => [
118  'MM_oppositeUsage' => [
119  'aTable' => [
120  'aField',
121  ],
122  ],
123  ],
124  ],
125  ],
126  ],
127  ],
128  ];
129  yield 'category field with oneToOne relationship and custom foreign_table_* options' => [
130  [
131  'aTable' => [
132  'columns' => [
133  'aField' => [
134  'config' => [
135  'type' => 'category',
136  'foreign_table' => 'some_table',
137  'foreign_table_where' => ' AND sys_category.pid IN (###PAGE_TSCONFIG_IDLIST###)',
138  'relationship' => 'oneToOne',
139  'minitems' => 1,
140  ],
141  ],
142  ],
143  ],
144  ],
145  [
146  'aTable' => [
147  'columns' => [
148  'aField' => [
149  'config' => [
150  'type' => 'category',
151  'relationship' => 'oneToOne',
152  'minitems' => 1,
153  'size' => 20,
154  'default' => 0,
155  'foreign_table' => 'sys_category',
156  'foreign_table_where' => ' AND sys_category.pid IN (###PAGE_TSCONFIG_IDLIST###)',
157  'maxitems' => 1,
158  ],
159  'label' => 'LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:sys_category.categories',
160  ],
161  ],
162  ],
163  ],
164  ];
165  yield 'categoryField with oneToMany relationship' => [
166  [
167  'aTable' => [
168  'columns' => [
169  'aField' => [
170  'config' => [
171  'type' => 'category',
172  'relationship' => 'oneToMany',
173  'size' => 123,
174  'maxitems' => 0,
175  ],
176  ],
177  ],
178  ],
179  ],
180  [
181  'aTable' => [
182  'columns' => [
183  'aField' => [
184  'config' => [
185  'type' => 'category',
186  'relationship' => 'oneToMany',
187  'size' => 123,
188  'foreign_table' => 'sys_category',
189  'foreign_table_where' => ' AND {#sys_category}.{#sys_language_uid} IN (-1, 0)',
190  'maxitems' => 99999,
191  ],
192  'label' => 'LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:sys_category.categories',
193  ],
194  ],
195  ],
196  ],
197  ];
198  yield 'categoryField with manyToMany relationship' => [
199  [
200  'aTable' => [
201  'columns' => [
202  'aField' => [
203  'exclude' => false,
204  'config' => [
205  'type' => 'category',
206  'relationship' => 'manyToMany',
207  'default' => 123,
208  'maxitems' => 123,
209  'foreign_table' => 'will_be_overwritten',
210  'MM' => 'will_be_overwritten',
211  ],
212  ],
213  ],
214  ],
215  ],
216  [
217  'aTable' => [
218  'columns' => [
219  'aField' => [
220  'config' => [
221  'type' => 'category',
222  'relationship' => 'manyToMany',
223  'size' => 20,
224  'default' => 123,
225  'foreign_table' => 'sys_category',
226  'foreign_table_where' => ' AND {#sys_category}.{#sys_language_uid} IN (-1, 0)',
227  'maxitems' => 123,
228  'MM' => 'sys_category_record_mm',
229  'MM_opposite_field' => 'items',
230  'MM_match_fields' => [
231  'tablenames' => 'aTable',
232  'fieldname' => 'aField',
233  ],
234  ],
235  'label' => 'LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:sys_category.categories',
236  'exclude' => false,
237  ],
238  ],
239  ],
240  'sys_category' => [
241  'columns' => [
242  'items' => [
243  'config' => [
244  'MM_oppositeUsage' => [
245  'aTable' => [
246  'aField',
247  ],
248  ],
249  ],
250  ],
251  ],
252  ],
253  ],
254  ];
255  }
256 
264  public function ‪configureCategoryRelationsThrowsExceptionOnInvalidMaxitems(array $input, int $excpetionCode): void
265  {
266  $this->expectExceptionCode($excpetionCode);
267  $this->expectException(\RuntimeException::class);
268  (new ‪TcaPreparation())->prepare($input);
269  }
270 
272  {
273  yield 'oneToOne relationship with maxitems=2' => [
274  [
275  'aTable' => [
276  'columns' => [
277  'foo' => [
278  'config' => [
279  'type' => 'category',
280  'relationship' => 'oneToOne',
281  'maxitems' => 2,
282  ],
283  ],
284  ],
285  ],
286  ],
287  1627335016,
288  ];
289  yield 'oneToMany relationship with maxitems=1' => [
290  [
291  'aTable' => [
292  'columns' => [
293  'foo' => [
294  'config' => [
295  'type' => 'category',
296  'relationship' => 'oneToMany',
297  'maxitems' => 1,
298  ],
299  ],
300  ],
301  ],
302  ],
303  1627335017,
304  ];
305  }
306 
311  public function ‪prepareQuotingOfTableNamesAndColumnNames(array $input, array $expected): void
312  {
313  $connection = $this->prophesize(Connection::class);
314  $connection->quoteIdentifier('tt_content')->willReturn('`tt_content`');
315  $connection->quoteIdentifier('CType')->willReturn('`CType`');
316  $connection->quoteIdentifier('uid_local')->willReturn('`uid_local`');
317  $connection->quoteIdentifier('title')->willReturn('`title`');
318  $connectionPool = $this->prophesize(ConnectionPool::class);
319  $connectionPool->getConnectionForTable(Argument::any())->willReturn($connection->reveal());
320  GeneralUtility::addInstance(ConnectionPool::class, $connectionPool->reveal());
321  $features = $this->prophesize(Features::class);
322  $features->isFeatureEnabled('runtimeDbQuotingOfTcaConfiguration')->willReturn(false);
323  GeneralUtility::addInstance(Features::class, $features->reveal());
324  $subject = new ‪TcaPreparation();
325  self::assertEquals($expected, $subject->prepare($input));
326  }
327 
329  {
330  return [
331  [
332  [
333  'aTable' => [
334  'columns' => [
335  'foo' => [
336  'config' => [
337  'type' => 'inline',
338  'foreign_table_where' => 'AND {#tt_content}.{#CType} IN (\'text\',\'textpic\',\'textmedia\') ORDER BY {#tt_content}.{#CType} ASC',
339  'MM_table_where' => 'AND {#uid_local} = ###REC_FIELD_category###',
340  'search' => [
341  'andWhere' => '{#CType}=\'text\' OR {#CType}=\'textpic\' OR {#CType}=\'textmedia\' AND {#title}=\'foo\'',
342  ],
343  ],
344  ],
345  ],
346  ],
347  ],
348  [
349  'aTable' => [
350  'columns' => [
351  'foo' => [
352  'config' => [
353  'type' => 'inline',
354  'foreign_table_where' => 'AND `tt_content`.`CType` IN (\'text\',\'textpic\',\'textmedia\') ORDER BY `tt_content`.`CType` ASC',
355  'MM_table_where' => 'AND `uid_local` = ###REC_FIELD_category###',
356  'search' => [
357  'andWhere' => '`CType`=\'text\' OR `CType`=\'textpic\' OR `CType`=\'textmedia\' AND `title`=\'foo\'',
358  ],
359  ],
360  ],
361  ],
362  ],
363  ],
364  ],
365  ];
366  }
367 
372  {
373  $this->expectExceptionCode(1627898896);
374  $this->expectException(\RuntimeException::class);
375  (new ‪TcaPreparation())->prepare([
376  'aTable' => [
377  'columns' => [
378  'foo' => [
379  'config' => [
380  'type' => 'category',
381  'relationship' => 'invalid',
382  ],
383  ],
384  ],
385  ],
386  ]);
387  }
388 }
‪TYPO3\CMS\Core\Tests\Unit\Preparations\TcaPreparationTest\configureCategoryRelationsThrowsExceptionOnInvalidRelationship
‪configureCategoryRelationsThrowsExceptionOnInvalidRelationship()
Definition: TcaPreparationTest.php:370
‪TYPO3\CMS\Core\Tests\Unit\Preparations
Definition: TcaPreparationTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Preparations\TcaPreparationTest\prepareQuotingOfTableNamesAndColumnNamesDataProvider
‪prepareQuotingOfTableNamesAndColumnNamesDataProvider()
Definition: TcaPreparationTest.php:327
‪TYPO3\CMS\Core\Tests\Unit\Preparations\TcaPreparationTest\configureCategoryRelationsThrowsExceptionOnInvalidMaxitemsDataProvider
‪configureCategoryRelationsThrowsExceptionOnInvalidMaxitemsDataProvider()
Definition: TcaPreparationTest.php:270
‪TYPO3\CMS\Core\Tests\Unit\Preparations\TcaPreparationTest\configureCategoryRelationsDataProvider
‪configureCategoryRelationsDataProvider()
Definition: TcaPreparationTest.php:47
‪TYPO3\CMS\Core\Preparations\TcaPreparation
Definition: TcaPreparation.php:31
‪TYPO3\CMS\Core\Configuration\Features
Definition: Features.php:56
‪TYPO3\CMS\Core\Tests\Unit\Preparations\TcaPreparationTest\configureCategoryRelations
‪configureCategoryRelations(array $input, array $expected)
Definition: TcaPreparationTest.php:42
‪TYPO3\CMS\Core\Tests\Unit\Preparations\TcaPreparationTest\prepareQuotingOfTableNamesAndColumnNames
‪prepareQuotingOfTableNamesAndColumnNames(array $input, array $expected)
Definition: TcaPreparationTest.php:310
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:38
‪TYPO3\CMS\Core\Tests\Unit\Preparations\TcaPreparationTest
Definition: TcaPreparationTest.php:33
‪TYPO3\CMS\Core\Tests\Unit\Preparations\TcaPreparationTest\configureCategoryRelationsThrowsExceptionOnInvalidMaxitems
‪configureCategoryRelationsThrowsExceptionOnInvalidMaxitems(array $input, int $excpetionCode)
Definition: TcaPreparationTest.php:263
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50