TcaItemsProcessorFunctionsTest.php 20 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php

declare(strict_types=1);

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

namespace TYPO3\CMS\Core\Tests\Unit\Hooks;

use Prophecy\Argument;
21
use Prophecy\PhpUnit\ProphecyTrait;
22
23
24
use Psr\EventDispatcher\EventDispatcherInterface;
use TYPO3\CMS\Backend\Module\ModuleFactory;
use TYPO3\CMS\Backend\Module\ModuleProvider;
25
26
27
28
29
30
31
32
33
34
35
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Hooks\TcaItemsProcessorFunctions;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Imaging\IconRegistry;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

class TcaItemsProcessorFunctionsTest extends UnitTestCase
{
36
37
    use ProphecyTrait;

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    protected function setUp(): void
    {
        parent::setUp();

        // Default LANG prophecy just returns incoming value as label if calling ->sL()
        $languageServiceProphecy = $this->prophesize(LanguageService::class);
        $languageServiceProphecy->sL(Argument::cetera())->willReturnArgument(0);
        $GLOBALS['LANG'] = $languageServiceProphecy->reveal();

        $iconRegistryProphecy = $this->prophesize(IconRegistry::class);
        GeneralUtility::setSingletonInstance(IconRegistry::class, $iconRegistryProphecy->reveal());
        $iconFactoryProphecy = $this->prophesize(IconFactory::class);
        // Two instances are needed due to the push/pop behavior of addInstance()
        GeneralUtility::addInstance(IconFactory::class, $iconFactoryProphecy->reveal());
        GeneralUtility::addInstance(IconFactory::class, $iconFactoryProphecy->reveal());
    }

    /**
     * Tear down
     */
    protected function tearDown(): void
    {
        GeneralUtility::purgeInstances();
        parent::tearDown();
    }

    /**
     * @test
     */
    public function populateAvailableTablesTest(): void
    {
        $fieldDefinition = ['items' => [0 => ['---', 0]]];

        $GLOBALS['TCA'] = [
            'notInResult' => [
                'ctrl' => [
                    'adminOnly' => true,
                ],
            ],
            'aTable' => [
                'ctrl' => [
                    'title' => 'aTitle',
                ],
            ],
        ];

        $expected = [
            'items' => [
                0 => [
                    '---',
                    0,
                ],
                1 => [
                    0 => 'aTitle',
                    1 => 'aTable',
                    2 => null,
94
95
                ],
            ],
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
        ];

        (new TcaItemsProcessorFunctions())->populateAvailableTables($fieldDefinition);
        self::assertSame($expected, $fieldDefinition);
    }

    /**
     * @test
     */
    public function populateAvailablePageTypesTest(): void
    {
        $fieldDefinition = ['items' => []];

        (new TcaItemsProcessorFunctions())->populateAvailablePageTypes($fieldDefinition);
        self::assertSame($fieldDefinition, $fieldDefinition);

        $GLOBALS['TCA']['pages']['columns']['doktype']['config']['items'] = [
            0 => [
                0 => 'Divider',
                1 => '--div--',
            ],
            1 => [
                0 => 'invalid',
            ],
            2 => [
                0 => 'aLabel',
                1 => 'aValue',
            ],
        ];

        $fieldDefinition = ['items' => [0 => ['---', 0]]];

        $expected = [
            'items' => [
                0 => [
                    0 => '---',
132
                    1 => 0,
133
134
135
136
137
                ],
                1 => [
                    0 => 'aLabel',
                    1 => 'aValue',
                    2 => null,
138
139
                ],
            ],
140
141
142
143
144
145
146
147
148
        ];

        (new TcaItemsProcessorFunctions())->populateAvailablePageTypes($fieldDefinition);
        self::assertSame($expected, $fieldDefinition);
    }

    /**
     * @test
     */
149
    public function populateAvailableUserModulesTest(): void
150
    {
151
152
153
154
155
156
157
158
159
160
161
162
163
164
        $moduleProviderProphecy = $this->prophesize(ModuleProvider::class);
        GeneralUtility::addInstance(ModuleProvider::class, $moduleProviderProphecy->reveal());
        $eventDispatcherProphecy = $this->prophesize(EventDispatcherInterface::class);
        $eventDispatcherProphecy->dispatch(Argument::any())->willReturnArgument();

        $moduleFactory = new ModuleFactory(
            $this->prophesize(IconRegistry::class)->reveal(),
            $eventDispatcherProphecy->reveal()
        );

        $moduleProviderProphecy->getUserModules()->willReturn([
            'aModule' => $moduleFactory->createModule('aModule', [
                'iconIdentifier' => 'a-module',
                'labels' => 'LLL:EXT:a-module/locallang',
165
                'packageName' => 'typo3/cms-testing',
166
167
168
169
            ]),
            'bModule' => $moduleFactory->createModule('bModule', [
                'iconIdentifier' => 'b-module',
                'labels' => 'LLL:EXT:b-module/locallang',
170
                'packageName' => 'typo3/cms-testing',
171
            ]),
172
173
174
175
176
        ]);

        $fieldDefinition = $expected = ['items' => []];
        $expected['items'] = [
            0 => [
177
                0 => 'LLL:EXT:a-module/locallang:mlang_tabs_tab',
178
                1 => 'aModule',
179
                2 => 'a-module',
180
181
                3 => null,
                4 => [
182
183
                    'title' => 'LLL:EXT:a-module/locallang:mlang_labels_tablabel',
                    'description' => 'LLL:EXT:a-module/locallang:mlang_labels_tabdescr',
184
185
                ],
            ],
186
187
            1 => [
                0 => 'LLL:EXT:b-module/locallang:mlang_tabs_tab',
188
                1 => 'bModule',
189
                2 => 'b-module',
190
191
                3 => null,
                4 => [
192
193
                    'title' => 'LLL:EXT:b-module/locallang:mlang_labels_tablabel',
                    'description' => 'LLL:EXT:b-module/locallang:mlang_labels_tabdescr',
194
195
196
197
198
199
200
201
202
203
204
205
                ],
            ],
        ];

        (new TcaItemsProcessorFunctions())->populateAvailableUserModules($fieldDefinition);
        self::assertSame($expected, $fieldDefinition);
    }

    /**
     * @test
     * @dataProvider populateExcludeFieldsTestDataProvider
     */
206
    public function populateExcludeFieldsTest(array $tca, array $expectedItems): void
207
208
209
210
    {
        $GLOBALS['TCA'] = $tca;
        $fieldDefinition = ['items' => []];
        $expected = [
211
            'items' => $expectedItems,
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
        ];

        (new TcaItemsProcessorFunctions())->populateExcludeFields($fieldDefinition);
        self::assertSame($expected, $fieldDefinition);
    }

    public function populateExcludeFieldsTestDataProvider(): array
    {
        return [
            'Table with exclude and non exclude field returns exclude item' => [
                [
                    // input tca
                    'fooTable' => [
                        'ctrl' => [
                            'title' => 'fooTableTitle',
                        ],
                        'columns' => [
                            'bar' => [
                                'label' => 'barColumnTitle',
231
                                'exclude' => true,
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
                            ],
                            'baz' => [
                                'label' => 'bazColumnTitle',
                            ],
                        ],
                    ],
                ],
                [
                    // expected items
                    'fooTable' => [
                        0 => 'fooTableTitle',
                        1 => '--div--',
                        2 => null,
                    ],
                    0 => [
                        0 => 'barColumnTitle (bar)',
                        1 => 'fooTable:bar',
                        2 => 'empty-empty',
                    ],
                ],
            ],
            'Root level table with ignored root level restriction returns exclude item' => [
                [
                    // input tca
                    'fooTable' => [
                        'ctrl' => [
                            'title' => 'fooTableTitle',
                            'rootLevel' => 1,
                            'security' => [
                                'ignoreRootLevelRestriction' => true,
                            ],
                        ],
                        'columns' => [
                            'bar' => [
                                'label' => 'barColumnTitle',
                                'exclude' => true,
                            ],
                        ],
                    ],
                ],
                [
                    // expected items
                    'fooTable' => [
                        0 => 'fooTableTitle',
                        1 => '--div--',
                        2 => null,
                    ],
                    0 => [
                        0 => 'barColumnTitle (bar)',
                        1 => 'fooTable:bar',
                        2 => 'empty-empty',
                    ],
                ],
            ],
            'Root level table without ignored root level restriction returns no item' => [
                [
                    // input tca
                    'fooTable' => [
                        'ctrl' => [
                            'title' => 'fooTableTitle',
                            'rootLevel' => 1,
                        ],
                        'columns' => [
                            'bar' => [
                                'label' => 'barColumnTitle',
                                'exclude' => true,
                            ],
                        ],
                    ],
                ],
                [
                    // no items
                ],
            ],
            'Admin table returns no item' => [
                [
                    // input tca
                    'fooTable' => [
                        'ctrl' => [
                            'title' => 'fooTableTitle',
                            'adminOnly' => true,
                        ],
                        'columns' => [
                            'bar' => [
                                'label' => 'barColumnTitle',
                                'exclude' => true,
                            ],
                        ],
                    ],
                ],
                [
                    // no items
                ],
            ],
        ];
    }

    /**
     * @test
     */
    public function populateExcludeFieldsWithFlexFormTest(): void
    {
        $GLOBALS['TCA'] = [
            'fooTable' => [
                'ctrl' => [
                    'title' => 'fooTableTitle',
                ],
                'columns' => [
                    'aFlexField' => [
                        'label' => 'aFlexFieldTitle',
                        'config' => [
                            'type' => 'flex',
                            'title' => 'title',
                            'ds' => [
                                'dummy' => '
									<T3DataStructure>
										<ROOT>
											<type>array</type>
											<el>
												<input1>
													<TCEforms>
														<label>flexInputLabel</label>
														<exclude>1</exclude>
														<config>
															<type>input</type>
															<size>23</size>
														</config>
													</TCEforms>
												</input1>
											</el>
										</ROOT>
									</T3DataStructure>
								',
                            ],
                        ],
                    ],
                ],
            ],
        ];

        $fieldDefinition = ['items' => []];
        $expected = [
            'items' => [
                'fooTableTitle aFlexFieldTitle dummy' => [
                    0 => 'fooTableTitle aFlexFieldTitle dummy',
                    1 => '--div--',
                    2 => null,
                ],
                0 => [
                    0 => 'flexInputLabel (input1)',
                    1 => 'fooTable:aFlexField;dummy;sDEF;input1',
                    2 => 'empty-empty',
                ],
385
            ],
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
        ];

        $cacheManagerProphecy = $this->prophesize(CacheManager::class);
        $cacheProphecy = $this->prophesize(FrontendInterface::class);
        $cacheManagerProphecy->getCache('runtime')->willReturn($cacheProphecy->reveal());
        GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal());

        (new TcaItemsProcessorFunctions())->populateExcludeFields($fieldDefinition);
        self::assertSame($expected, $fieldDefinition);
    }

    /**
     * @test
     * @dataProvider populateExplicitAuthValuesTestDataProvider
     *
     * @param array $tca
     * @param array $expectedItems
     */
    public function populateExplicitAuthValuesTest(array $tca, array $expectedItems): void
    {
        $GLOBALS['TCA'] = $tca;
        $fieldDefinition = ['items' => []];
        $expected = [
409
            'items' => $expectedItems,
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
        ];
        (new TcaItemsProcessorFunctions())->populateExplicitAuthValues($fieldDefinition);
        self::assertSame($expected, $fieldDefinition);
    }

    public function populateExplicitAuthValuesTestDataProvider(): \Generator
    {
        yield 'ExplicitAllow fields with special explicit values' => [
            [
                'fooTable' => [
                    'ctrl' => [
                        'title' => 'fooTableTitle',
                    ],
                    'columns' => [
                        'aField' => [
                            'label' => 'aFieldTitle',
                            'config' => [
                                'type' => 'select',
                                'renderType' => 'selectSingle',
                                'authMode' => 'explicitAllow',
                                'items' => [
                                    0 => [
                                        'anItemTitle',
                                        'anItemValue',
                                    ],
435
                                ],
436
437
438
439
440
441
442
443
444
445
446
                            ],
                        ],
                    ],
                ],
            ],
            [
                0 => [
                    0 => 'fooTableTitle: aFieldTitle',
                    1 => '--div--',
                ],
                1 => [
447
448
                    0 => 'anItemTitle',
                    1 => 'fooTable:aField:anItemValue',
449
450
                    2 => 'status-status-permission-granted',
                ],
451
            ],
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
        ];
    }

    /**
     * @test
     */
    public function populateCustomPermissionOptionsTest(): void
    {
        $GLOBALS['TYPO3_CONF_VARS']['BE']['customPermOptions'] = [
            'aKey' => [
                'header' => 'aHeader',
                'items' => [
                    'anItemKey' => [
                        0 => 'anItemTitle',
                    ],
                    'anotherKey' => [
                        0 => 'anotherTitle',
                        1 => 'status-status-permission-denied',
                        2 => 'aDescription',
                    ],
                ],
473
            ],
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
        ];
        $fieldDefinition = ['items' => []];
        $expected = [
            'items' => [
                0 => [
                    0 => 'aHeader',
                    1 => '--div--',
                ],
                1 => [
                    0 => 'anItemTitle',
                    1 => 'aKey:anItemKey',
                    2 => 'empty-empty',
                    3 => null,
                    4 => '',
                ],
                2 => [
                    0 => 'anotherTitle',
                    1 => 'aKey:anotherKey',
                    2 => 'empty-empty',
                    3 => null,
                    4 => 'aDescription',
                ],
496
            ],
497
498
499
500
        ];
        (new TcaItemsProcessorFunctions())->populateCustomPermissionOptions($fieldDefinition);
        self::assertSame($expected, $fieldDefinition);
    }
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536

    /**
     * @test
     */
    public function populateAvailableCategoryFieldsThrowsExceptionOnMissingTable(): void
    {
        $this->expectException(\UnexpectedValueException::class);
        $this->expectExceptionCode(1627565458);

        $fieldDefinition = ['items' => [], 'config' => []];
        (new TcaItemsProcessorFunctions())->populateAvailableCategoryFields($fieldDefinition);
    }

    /**
     * @test
     */
    public function populateAvailableCategoryFieldsThrowsExceptionOnInvalidTable(): void
    {
        $this->expectException(\RuntimeException::class);
        $this->expectExceptionCode(1627565459);

        $fieldDefinition = ['items' => [], 'config' => ['itemsProcConfig' => ['table' => 'aTable']]];
        (new TcaItemsProcessorFunctions())->populateAvailableCategoryFields($fieldDefinition);
    }

    /**
     * @test
     * @dataProvider populateAvailableCategoryFieldsDataProvider
     */
    public function populateAvailableCategoryFields(array $itemsProcConfig, array $expectedItems): void
    {
        $GLOBALS['TCA']['aTable']['columns'] = [
            'aField' => [
                'label' => 'aField label',
                'config' => [
                    'type' => 'category',
537
538
                    'relationship' => 'manyToMany',
                ],
539
540
541
542
543
            ],
            'bField' => [
                'label' => 'bField label',
                'config' => [
                    'type' => 'category',
544
                ],
545
546
547
548
549
            ],
            'cField' => [
                'label' => 'cField label',
                'config' => [
                    'type' => 'category',
550
551
                    'relationship' => 'oneToMany',
                ],
552
553
554
555
556
            ],
            'dField' => [
                'label' => 'dField label',
                'config' => [
                    'type' => 'category',
557
558
559
                    'relationship' => 'manyToMany',
                ],
            ],
560
561
562
563
564
565
566
567
568
569
570
571
        ];
        $fieldDefinition = ['items' => [], 'config' => ['itemsProcConfig' => $itemsProcConfig]];
        $expected = $fieldDefinition;
        $expected['items'] = $expectedItems;
        (new TcaItemsProcessorFunctions())->populateAvailableCategoryFields($fieldDefinition);
        self::assertSame($expected, $fieldDefinition);
    }

    public function populateAvailableCategoryFieldsDataProvider(): \Generator
    {
        yield 'falls back to default relationship (manyToMany)' => [
            [
572
                'table' => 'aTable',
573
574
575
576
577
578
579
580
581
            ],
            [
                0 => [
                    0 => 'aField label',
                    1 => 'aField',
                ],
                1 => [
                    0 => 'dField label',
                    1 => 'dField',
582
583
                ],
            ],
584
585
586
587
        ];
        yield 'relationship oneToMany given' => [
            [
                'table' => 'aTable',
588
                'allowedRelationships' => ['oneToMany'],
589
590
591
592
593
594
            ],
            [
                0 => [
                    0 => 'cField label',
                    1 => 'cField',
                ],
595
            ],
596
597
598
599
        ];
        yield 'relationship oneToOne given' => [
            [
                'table' => 'aTable',
600
                'allowedRelationships' => ['oneToOne'],
601
            ],
602
            [],
603
604
605
606
        ];
        yield 'multiple relationships given' => [
            [
                'table' => 'aTable',
607
                'allowedRelationships' => ['oneToOne', 'oneToMany', 'manyToMany'],
608
609
610
611
612
613
614
615
616
617
618
619
620
            ],
            [
                0 => [
                    0 => 'aField label',
                    1 => 'aField',
                ],
                1 => [
                    0 => 'cField label',
                    1 => 'cField',
                ],
                2 => [
                    0 => 'dField label',
                    1 => 'dField',
621
622
                ],
            ],
623
624
        ];
    }
625
}