BackendUtilityTest.php 40.1 KB
Newer Older
1
<?php
2

3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
7
8
 * 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.
9
 *
10
11
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
14
 * The TYPO3 project - inspiring people to share!
 */
15

16
17
namespace TYPO3\CMS\Backend\Tests\Unit\Utility;

18
19
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
20
use Psr\EventDispatcher\EventDispatcherInterface;
21
use TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher;
22
23
use TYPO3\CMS\Backend\Tests\Unit\Utility\Fixtures\LabelFromItemListMergedReturnsCorrectFieldsFixture;
use TYPO3\CMS\Backend\Utility\BackendUtility;
24
25
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
26
27
use TYPO3\CMS\Core\Configuration\Event\ModifyLoadedPageTsConfigEvent;
use TYPO3\CMS\Core\Configuration\Loader\PageTsConfigLoader;
28
use TYPO3\CMS\Core\Configuration\Parser\PageTsConfigParser;
29
use TYPO3\CMS\Core\Database\RelationHandler;
30
use TYPO3\CMS\Core\Localization\LanguageService;
31
32
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Site\SiteFinder;
33
use TYPO3\CMS\Core\Utility\GeneralUtility;
34
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
35

36
/**
37
 * Test case
38
 */
39
class BackendUtilityTest extends UnitTestCase
40
{
41
42
43
44
45
    /**
     * @var bool
     */
    protected $resetSingletonInstances = true;

46
47
48
49
50
51
52
53
54
55
    ///////////////////////////////////////
    // Tests concerning calcAge
    ///////////////////////////////////////
    /**
     * Data provider for calcAge function
     *
     * @return array
     */
    public function calcAgeDataProvider()
    {
56
57
        return [
            'Single year' => [
58
59
                'seconds' => 60 * 60 * 24 * 365,
                'expectedLabel' => '1 year'
60
61
            ],
            'Plural years' => [
62
63
                'seconds' => 60 * 60 * 24 * 365 * 2,
                'expectedLabel' => '2 yrs'
64
65
            ],
            'Single negative year' => [
66
67
                'seconds' => 60 * 60 * 24 * 365 * -1,
                'expectedLabel' => '-1 year'
68
69
            ],
            'Plural negative years' => [
70
71
                'seconds' => 60 * 60 * 24 * 365 * 2 * -1,
                'expectedLabel' => '-2 yrs'
72
73
            ],
            'Single day' => [
74
75
                'seconds' => 60 * 60 * 24,
                'expectedLabel' => '1 day'
76
77
            ],
            'Plural days' => [
78
79
                'seconds' => 60 * 60 * 24 * 2,
                'expectedLabel' => '2 days'
80
81
            ],
            'Single negative day' => [
82
83
                'seconds' => 60 * 60 * 24 * -1,
                'expectedLabel' => '-1 day'
84
85
            ],
            'Plural negative days' => [
86
87
                'seconds' => 60 * 60 * 24 * 2 * -1,
                'expectedLabel' => '-2 days'
88
89
            ],
            'Single hour' => [
90
91
                'seconds' => 60 * 60,
                'expectedLabel' => '1 hour'
92
93
            ],
            'Plural hours' => [
94
95
                'seconds' => 60 * 60 * 2,
                'expectedLabel' => '2 hrs'
96
97
            ],
            'Single negative hour' => [
98
99
                'seconds' => 60 * 60 * -1,
                'expectedLabel' => '-1 hour'
100
101
            ],
            'Plural negative hours' => [
102
103
                'seconds' => 60 * 60 * 2 * -1,
                'expectedLabel' => '-2 hrs'
104
105
            ],
            'Single minute' => [
106
107
                'seconds' => 60,
                'expectedLabel' => '1 min'
108
109
            ],
            'Plural minutes' => [
110
111
                'seconds' => 60 * 2,
                'expectedLabel' => '2 min'
112
113
            ],
            'Single negative minute' => [
114
115
                'seconds' => 60 * -1,
                'expectedLabel' => '-1 min'
116
117
            ],
            'Plural negative minutes' => [
118
119
                'seconds' => 60 * 2 * -1,
                'expectedLabel' => '-2 min'
120
121
            ],
            'Zero seconds' => [
122
123
                'seconds' => 0,
                'expectedLabel' => '0 min'
124
125
            ]
        ];
126
    }
127

128
129
130
131
132
133
134
135
136
    /**
     * @test
     * @dataProvider calcAgeDataProvider
     *
     * @param int $seconds
     * @param string $expectedLabel
     */
    public function calcAgeReturnsExpectedValues($seconds, $expectedLabel)
    {
137
        self::assertSame($expectedLabel, BackendUtility::calcAge($seconds));
138
    }
139

140
141
142
143
144
    ///////////////////////////////////////
    // Tests concerning getProcessedValue
    ///////////////////////////////////////
    /**
     * @test
145
     * @see https://forge.typo3.org/issues/20994
146
147
148
     */
    public function getProcessedValueForZeroStringIsZero()
    {
149
150
151
152
153
        $GLOBALS['TCA'] = [
            'tt_content' => [
                'columns' => [
                    'header' => [
                        'config' => [
154
                            'type' => 'input',
155
156
157
158
159
                        ],
                    ],
                ],
            ],
        ];
160
        $GLOBALS['LANG'] = [];
161
        self::assertEquals('0', BackendUtility::getProcessedValue('tt_content', 'header', '0'));
162
    }
163

164
165
166
167
168
    /**
     * @test
     */
    public function getProcessedValueForGroup()
    {
169
170
171
172
173
        $GLOBALS['TCA'] = [
            'tt_content' => [
                'columns' => [
                    'multimedia' => [
                        'config' => [
174
                            'type' => 'group',
175
176
177
178
179
                        ],
                    ],
                ],
            ],
        ];
180
        $GLOBALS['LANG'] = [];
181
        self::assertSame('1, 2', BackendUtility::getProcessedValue('tt_content', 'multimedia', '1,2'));
182
    }
183

184
185
186
187
188
    /**
     * @test
     */
    public function getProcessedValueForGroupWithOneAllowedTable()
    {
189
190
191
192
193
        $GLOBALS['TCA'] = [
            'tt_content' => [
                'columns' => [
                    'pages' => [
                        'config' => [
194
195
196
197
198
199
                            'type' => 'group',
                            'allowed' => 'pages',
                            'internal_type' => 'db',
                            'maxitems' => 22,
                            'minitems' => 0,
                            'size' => 3,
200
201
202
203
                        ],
                    ],
                ],
            ],
204
205
206
207
208
209
210
211
212
213
214
215
            'pages' => [
                'ctrl' => [
                    'label' => 'title'
                ],
                'columns' => [
                    'title' => [
                        'config' => [
                            'type' => 'input'
                        ]
                    ]
                ]
            ]
216
        ];
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248

        $languageServiceProphecy = $this->prophesize(LanguageService::class);
        $languageServiceProphecy->sL(Argument::cetera())->willReturnArgument(0);
        $GLOBALS['LANG'] = $languageServiceProphecy->reveal();

        /** @var RelationHandler|ObjectProphecy $relationHandlerProphet */
        $relationHandlerProphet = $this->prophesize(RelationHandler::class);
        $relationHandlerProphet->start(Argument::cetera())->shouldBeCalled();
        $relationHandlerProphet->getFromDB()->willReturn([]);
        $relationHandlerProphet->getResolvedItemArray()->willReturn([
            [
                'table' => 'pages',
                'uid' => 1,
                'record' => [
                    'uid' => 1,
                    'pid' => 0,
                    'title' => 'Page 1'
                ]
            ],
            [
                'table' => 'pages',
                'uid' => 2,
                'record' => [
                    'uid' => 2,
                    'pid' => 0,
                    'title' => 'Page 2'
                ]
            ]
        ]);
        GeneralUtility::addInstance(RelationHandler::class, $relationHandlerProphet->reveal());

        self::assertSame('Page 1, Page 2', BackendUtility::getProcessedValue('tt_content', 'pages', '1,2'));
249
    }
Christian Kuhn's avatar
Christian Kuhn committed
250

251
252
253
254
255
    /**
     * @test
     */
    public function getProcessedValueForGroupWithMultipleAllowedTables()
    {
256
257
        $GLOBALS['TCA'] = [
            'index_config' => [
258
259
260
                'ctrl' => [
                    'label' => 'title'
                ],
261
                'columns' => [
262
263
264
265
266
                    'title' => [
                        'config' => [
                            'type' => 'input'
                        ]
                    ],
267
268
                    'indexcfgs' => [
                        'config' => [
269
270
271
272
                            'type' => 'group',
                            'internal_type' => 'db',
                            'allowed' => 'index_config,pages',
                            'size' => 5,
273
274
275
276
                        ],
                    ],
                ],
            ],
277
278
279
280
281
282
283
284
285
286
287
288
            'pages' => [
                'ctrl' => [
                    'label' => 'title'
                ],
                'columns' => [
                    'title' => [
                        'config' => [
                            'type' => 'input'
                        ]
                    ]
                ]
            ]
289
        ];
290

291
292
293
        $languageServiceProphecy = $this->prophesize(LanguageService::class);
        $languageServiceProphecy->sL(Argument::cetera())->willReturnArgument(0);
        $GLOBALS['LANG'] = $languageServiceProphecy->reveal();
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
        /** @var RelationHandler|ObjectProphecy $relationHandlerProphet */
        $relationHandlerProphet = $this->prophesize(RelationHandler::class);
        $relationHandlerProphet->start(Argument::cetera())->shouldBeCalled();
        $relationHandlerProphet->getFromDB()->willReturn([]);
        $relationHandlerProphet->getResolvedItemArray()->willReturn([
            [
                'table' => 'pages',
                'uid' => 1,
                'record' => [
                    'uid' => 1,
                    'pid' => 0,
                    'title' => 'Page 1'
                ]
            ],
            [
                'table' => 'index_config',
                'uid' => 2,
                'record' => [
                    'uid' => 2,
                    'pid' => 0,
                    'title' => 'Configuration 2'
                ]
            ]
        ]);
        GeneralUtility::addInstance(RelationHandler::class, $relationHandlerProphet->reveal());
        self::assertSame('Page 1, Configuration 2', BackendUtility::getProcessedValue('index_config', 'indexcfgs', 'pages_1,index_config_2'));
321
322
    }

323
324
325
326
327
    /**
     * @test
     */
    public function getProcessedValueForSelectWithMMRelation()
    {
328
329
330
        /** @var RelationHandler|ObjectProphecy $relationHandlerProphet */
        $relationHandlerProphet = $this->prophesize(RelationHandler::class);
        $relationHandlerProphet->start(Argument::cetera())->shouldBeCalled();
331
332
        $relationHandlerProphet->getFromDB()->willReturn([]);
        $relationHandlerProphet->getResolvedItemArray()->willReturn([
333
            [
334
                'table' => 'sys_category',
335
                'uid' => 1,
336
337
338
339
340
                'record' => [
                    'uid' => 2,
                    'pid' => 0,
                    'title' => 'Category 1'
                ]
341
342
            ],
            [
343
                'table' => 'sys_category',
344
                'uid' => 2,
345
346
347
348
349
350
351
                'record' => [
                    'uid' => 2,
                    'pid' => 0,
                    'title' => 'Category 2'
                ]
            ]
        ]);
352

353
354
        $relationHandlerInstance = $relationHandlerProphet->reveal();
        $relationHandlerInstance->tableArray['sys_category'] = [1, 2];
355
356

        GeneralUtility::addInstance(RelationHandler::class, $relationHandlerInstance);
357

358
359
360
361
362
        $GLOBALS['TCA'] = [
            'pages' => [
                'columns' => [
                    'categories' => [
                        'config' => [
363
364
365
                            'type' => 'select',
                            'foreign_table' => 'sys_category',
                            'MM' => 'sys_category_record_mm',
366
                            'MM_match_fields' => [
367
368
                                'fieldname' => 'categories',
                                'tablesnames' => 'pages',
369
                            ],
370
                            'MM_opposite_field' => 'items',
371
372
373
374
375
376
377
                        ],
                    ],
                ],
            ],
            'sys_category' => [
                'ctrl' => ['label' => 'title'],
                'columns' => [
378
379
380
381
382
                    'title' => [
                        'config' => [
                            'type' => 'input'
                        ]
                    ],
383
384
                    'items' => [
                        'config' => [
385
386
387
388
                            'type' => 'group',
                            'internal_type' => 'db',
                            'allowed' => '*',
                            'MM' => 'sys_category_record_mm',
389
390
391
392
393
394
                            'MM_oppositeUsage' => [],
                        ]
                    ]
                ],
            ],
        ];
395
396
397
398

        $languageServiceProphecy = $this->prophesize(LanguageService::class);
        $languageServiceProphecy->sL(Argument::cetera())->willReturnArgument(0);
        $GLOBALS['LANG'] = $languageServiceProphecy->reveal();
399

400
        self::assertSame(
401
402
            'Category 1, Category 2',
            BackendUtility::getProcessedValue(
403
404
405
406
407
408
409
410
411
                'pages',
                'categories',
                '2',
                0,
                false,
                false,
                1
            )
        );
412
    }
Christian Kuhn's avatar
Christian Kuhn committed
413

414
415
416
417
418
419
420
421
422
    /**
     * @test
     */
    public function getProcessedValueDisplaysAgeForDateInputFieldsIfSettingAbsent()
    {
        /** @var ObjectProphecy $languageServiceProphecy */
        $languageServiceProphecy = $this->prophesize(LanguageService::class);
        $languageServiceProphecy->sL(Argument::cetera())->willReturn(' min| hrs| days| yrs| min| hour| day| year');
        $GLOBALS['LANG'] = $languageServiceProphecy->reveal();
423

424
        $GLOBALS['EXEC_TIME'] = mktime(0, 0, 0, 8, 30, 2015);
425

426
427
428
429
430
431
432
433
434
435
436
437
        $GLOBALS['TCA'] = [
            'tt_content' => [
                'columns' => [
                    'date' => [
                        'config' => [
                            'type' => 'input',
                            'eval' => 'date',
                        ],
                    ],
                ],
            ],
        ];
438
        self::assertSame('28-08-15 (-2 days)', BackendUtility::getProcessedValue('tt_content', 'date', mktime(0, 0, 0, 8, 28, 2015)));
439
    }
440

441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
    /**
     * @return array
     */
    public function inputTypeDateDisplayOptions()
    {
        return [
            'typeSafe Setting' => [
                true,
                '28-08-15',
            ],
            'non typesafe setting' => [
                1,
                '28-08-15',
            ],
            'setting disabled typesafe' => [
                false,
                '28-08-15 (-2 days)',
            ],
            'setting disabled not typesafe' => [
                0,
                '28-08-15 (-2 days)',
            ],
        ];
    }
465

466
467
468
469
470
471
472
473
474
475
476
477
478
479
    /**
     * @test
     *
     * @dataProvider inputTypeDateDisplayOptions
     *
     * @param string $input
     * @param string $expected
     */
    public function getProcessedValueHandlesAgeDisplayCorrectly($input, $expected)
    {
        /** @var ObjectProphecy $languageServiceProphecy */
        $languageServiceProphecy = $this->prophesize(LanguageService::class);
        $languageServiceProphecy->sL(Argument::cetera())->willReturn(' min| hrs| days| yrs| min| hour| day| year');
        $GLOBALS['LANG'] = $languageServiceProphecy->reveal();
480

481
        $GLOBALS['EXEC_TIME'] = mktime(0, 0, 0, 8, 30, 2015);
482

483
484
485
486
487
488
489
490
491
492
493
494
495
        $GLOBALS['TCA'] = [
            'tt_content' => [
                'columns' => [
                    'date' => [
                        'config' => [
                            'type' => 'input',
                            'eval' => 'date',
                            'disableAgeDisplay' => $input,
                        ],
                    ],
                ],
            ],
        ];
496
        self::assertSame($expected, BackendUtility::getProcessedValue('tt_content', 'date', mktime(0, 0, 0, 8, 28, 2015)));
497
    }
498

499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
    /**
     * @test
     */
    public function getProcessedValueForCheckWithSingleItem()
    {
        $GLOBALS['TCA'] = [
            'tt_content' => [
                'columns' => [
                    'hide' => [
                        'config' => [
                            'type' => 'check',
                            'items' => [
                                [
                                    0 => '',
                                    1 => '',
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ];
521
        $languageServiceProphecy = $this->prophesize(LanguageService::class);
522
523
524
        $languageServiceProphecy->sL('LLL:EXT:core/Resources/Private/Language/locallang_common.xlf:yes')->willReturn('Yes');
        $languageServiceProphecy->sL('LLL:EXT:core/Resources/Private/Language/locallang_common.xlf:no')->willReturn('No');
        $GLOBALS['LANG'] = $languageServiceProphecy->reveal();
525
        self::assertSame('Yes', BackendUtility::getProcessedValue('tt_content', 'hide', 1));
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
    }

    /**
     * @test
     */
    public function getProcessedValueForCheckWithSingleItemInvertStateDisplay()
    {
        $GLOBALS['TCA'] = [
            'tt_content' => [
                'columns' => [
                    'hide' => [
                        'config' => [
                            'type' => 'check',
                            'items' => [
                                [
                                    0 => '',
                                    1 => '',
                                    'invertStateDisplay' => true,
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ];
551
        $languageServiceProphecy = $this->prophesize(LanguageService::class);
552
553
554
        $languageServiceProphecy->sL('LLL:EXT:core/Resources/Private/Language/locallang_common.xlf:yes')->willReturn('Yes');
        $languageServiceProphecy->sL('LLL:EXT:core/Resources/Private/Language/locallang_common.xlf:no')->willReturn('No');
        $GLOBALS['LANG'] = $languageServiceProphecy->reveal();
555
        self::assertSame('No', BackendUtility::getProcessedValue('tt_content', 'hide', 1));
556
557
    }

558
559
560
    /**
     * Tests concerning getCommonSelectFields
     */
561

562
563
564
565
566
567
568
    /**
     * Data provider for getCommonSelectFieldsReturnsCorrectFields
     *
     * @return array The test data with $table, $prefix, $presetFields, $tca, $expectedFields
     */
    public function getCommonSelectFieldsReturnsCorrectFieldsDataProvider()
    {
569
570
        return [
            'only uid' => [
571
572
                'table' => 'test_table',
                'prefix' => '',
573
574
                'presetFields' => [],
                'tca' => [],
575
                'expectedFields' => 'uid'
576
577
            ],
            'label set' => [
578
579
                'table' => 'test_table',
                'prefix' => '',
580
581
582
                'presetFields' => [],
                'tca' => [
                    'ctrl' => [
583
                        'label' => 'label'
584
585
                    ]
                ],
586
                'expectedFields' => 'uid,label'
587
588
            ],
            'label_alt set' => [
589
590
                'table' => 'test_table',
                'prefix' => '',
591
592
593
                'presetFields' => [],
                'tca' => [
                    'ctrl' => [
594
                        'label_alt' => 'label,label2'
595
596
                    ]
                ],
597
                'expectedFields' => 'uid,label,label2'
598
599
            ],
            'versioningWS set' => [
600
601
                'table' => 'test_table',
                'prefix' => '',
602
603
604
                'presetFields' => [],
                'tca' => [
                    'ctrl' => [
605
                        'versioningWS' => true
606
607
                    ]
                ],
608
                'expectedFields' => 'uid,t3ver_state,t3ver_wsid'
609
610
            ],
            'selicon_field set' => [
611
612
                'table' => 'test_table',
                'prefix' => '',
613
614
615
                'presetFields' => [],
                'tca' => [
                    'ctrl' => [
616
                        'selicon_field' => 'field'
617
618
                    ]
                ],
619
                'expectedFields' => 'uid,field'
620
621
            ],
            'typeicon_column set' => [
622
623
                'table' => 'test_table',
                'prefix' => '',
624
625
626
                'presetFields' => [],
                'tca' => [
                    'ctrl' => [
627
                        'typeicon_column' => 'field'
628
629
                    ]
                ],
630
                'expectedFields' => 'uid,field'
631
632
            ],
            'enablecolumns set' => [
633
634
                'table' => 'test_table',
                'prefix' => '',
635
636
637
638
                'presetFields' => [],
                'tca' => [
                    'ctrl' => [
                        'enablecolumns' => [
639
640
641
642
                            'disabled' => 'hidden',
                            'starttime' => 'start',
                            'endtime' => 'stop',
                            'fe_group' => 'groups'
643
644
645
                        ]
                    ]
                ],
646
                'expectedFields' => 'uid,hidden,start,stop,groups'
647
648
            ],
            'label set to uid' => [
649
650
                'table' => 'test_table',
                'prefix' => '',
651
652
653
                'presetFields' => [],
                'tca' => [
                    'ctrl' => [
654
                        'label' => 'uid'
655
656
                    ]
                ],
657
                'expectedFields' => 'uid'
658
659
            ]
        ];
660
    }
661

662
663
664
665
666
667
668
669
670
671
    /**
     * @test
     * @dataProvider getCommonSelectFieldsReturnsCorrectFieldsDataProvider
     *
     * @param string $table
     * @param string $prefix
     * @param array $presetFields
     * @param array $tca
     * @param string $expectedFields
     */
672
    public function getCommonSelectFieldsReturnsCorrectFields($table, $prefix, array $presetFields, array $tca, $expectedFields = '')
673
674
675
    {
        $GLOBALS['TCA'][$table] = $tca;
        $selectFields = BackendUtility::getCommonSelectFields($table, $prefix, $presetFields);
676
        self::assertEquals($selectFields, $expectedFields);
677
    }
678

679
680
681
    /**
     * Tests concerning getLabelFromItemlist
     */
682

683
684
685
686
687
688
689
    /**
     * Data provider for getLabelFromItemlistReturnsCorrectFields
     *
     * @return array The test data with $table, $col, $key, $expectedLabel
     */
    public function getLabelFromItemlistReturnsCorrectFieldsDataProvider()
    {
690
691
        return [
            'item set' => [
692
693
694
                'table' => 'tt_content',
                'col' => 'menu_type',
                'key' => '1',
695
696
697
698
699
700
701
702
703
704
705
706
707
                'tca' => [
                    'columns' => [
                        'menu_type' => [
                            'config' => [
                                'items' => [
                                    ['Item 1', '0'],
                                    ['Item 2', '1'],
                                    ['Item 3', '3']
                                ]
                            ]
                        ]
                    ]
                ],
708
                'expectedLabel' => 'Item 2'
709
710
            ],
            'item set twice' => [
711
712
713
                'table' => 'tt_content',
                'col' => 'menu_type',
                'key' => '1',
714
715
716
717
718
719
720
721
722
723
724
725
726
727
                'tca' => [
                    'columns' => [
                        'menu_type' => [
                            'config' => [
                                'items' => [
                                    ['Item 1', '0'],
                                    ['Item 2a', '1'],
                                    ['Item 2b', '1'],
                                    ['Item 3', '3']
                                ]
                            ]
                        ]
                    ]
                ],
728
                'expectedLabel' => 'Item 2a'
729
730
            ],
            'item not found' => [
731
732
733
                'table' => 'tt_content',
                'col' => 'menu_type',
                'key' => '5',
734
735
736
737
738
739
740
741
742
743
744
745
746
                'tca' => [
                    'columns' => [
                        'menu_type' => [
                            'config' => [
                                'items' => [
                                    ['Item 1', '0'],
                                    ['Item 2', '1'],
                                    ['Item 3', '2']
                                ]
                            ]
                        ]
                    ]
                ],
747
                'expectedLabel' => null
748
749
            ]
        ];
750
    }
751

752
753
754
755
756
757
758
759
760
761
    /**
     * @test
     * @dataProvider getLabelFromItemlistReturnsCorrectFieldsDataProvider
     *
     * @param string $table
     * @param string $col
     * @param string $key
     * @param array $tca
     * @param string $expectedLabel
     */
762
    public function getLabelFromItemlistReturnsCorrectFields($table, $col, $key, array $tca, $expectedLabel = '')
763
764
765
    {
        $GLOBALS['TCA'][$table] = $tca;
        $label = BackendUtility::getLabelFromItemlist($table, $col, $key);
766
        self::assertEquals($label, $expectedLabel);
767
    }
768

769
770
771
    /**
     * Tests concerning getLabelFromItemListMerged
     */
772

773
774
775
776
777
778
779
    /**
     * Data provider for getLabelFromItemListMerged
     *
     * @return array The test data with $pageId, $table, $column, $key, $expectedLabel
     */
    public function getLabelFromItemListMergedReturnsCorrectFieldsDataProvider()
    {
780
781
        return [
            'no field found' => [
782
783
784
785
                'pageId' => '123',
                'table' => 'tt_content',
                'col' => 'menu_type',
                'key' => '10',
786
787
788
789
790
791
792
793
794
795
796
797
798
                'tca' => [
                    'columns' => [
                        'menu_type' => [
                            'config' => [
                                'items' => [
                                    ['Item 1', '0'],
                                    ['Item 2', '1'],
                                    ['Item 3', '3']
                                ]
                            ]
                        ]
                    ]
                ],
799
                'expectedLabel' => ''
800
801
            ],
            'no tsconfig set' => [
802
803
804
805
                'pageId' => '123',
                'table' => 'tt_content',
                'col' => 'menu_type',
                'key' => '1',
806
807
808
809
810
811
812
813
814
815
816
817
818
                'tca' => [
                    'columns' => [
                        'menu_type' => [
                            'config' => [
                                'items' => [
                                    ['Item 1', '0'],
                                    ['Item 2', '1'],
                                    ['Item 3', '3']
                                ]
                            ]
                        ]
                    ]
                ],
819
                'expectedLabel' => 'Item 2'
820
821
            ]
        ];
822
    }
823

824
825
826
827
828
829
830
831
832
833
834
    /**
     * @test
     * @dataProvider getLabelFromItemListMergedReturnsCorrectFieldsDataProvider
     *
     * @param int $pageId
     * @param string $table
     * @param string $column
     * @param string $key
     * @param array $tca
     * @param string $expectedLabel
     */
835
    public function getLabelFromItemListMergedReturnsCorrectFields($pageId, $table, $column, $key, array $tca, $expectedLabel = '')
836
837
    {
        $GLOBALS['TCA'][$table] = $tca;
838

839
        self::assertEquals($expectedLabel, LabelFromItemListMergedReturnsCorrectFieldsFixture::getLabelFromItemListMerged($pageId, $table, $column, $key));
840
    }
Christian Kuhn's avatar
Christian Kuhn committed
841

842
843
844
    /**
     * Tests concerning getFuncCheck
     */
845

846
847
848
849
850
    /**
     * @test
     */
    public function getFuncCheckReturnsInputTagWithValueAttribute()
    {
851
        self::assertStringMatchesFormat('<input %Svalue="1"%S/>', BackendUtility::getFuncCheck('params', 'test', true));
852
    }
853

854
855
856
    /*
     * Tests concerning getLabelsFromItemsList
     */
857

858
859
860
861
862
    /**
     * @return array
     */
    public function getLabelsFromItemsListDataProvider()
    {
863
864
        return [
            'return value if found' => [
865
866
867
                'foobar', // table
                'someColumn', // col
                'foo, bar', // keyList
868
869
870
871
872
873
874
875
876
877
878
879
880
                [ // TCA
                    'columns' => [
                        'someColumn' => [
                            'config' => [
                                'items' => [
                                    '0' => ['aFooLabel', 'foo'],
                                    '1' => ['aBarLabel', 'bar']
                                ]
                            ]
                        ]
                    ]
                ],
                [], // page TSconfig
881
                'aFooLabel, aBarLabel' // expected
882
883
            ],
            'page TSconfig overrules TCA' => [
884
885
886
                'foobar', // table
                'someColumn', // col
                'foo,bar, add', // keyList
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
                [ // TCA
                    'columns' => [
                        'someColumn' => [
                            'config' => [
                                'items' => [
                                    '0' => ['aFooLabel', 'foo'],
                                    '1' => ['aBarLabel', 'bar']
                                ]
                            ]
                        ]
                    ]
                ],
                [ // page TSconfig
                    'addItems.' => ['add' => 'aNewLabel'],
                    'altLabels.' => ['bar' => 'aBarDiffLabel'],
                ],
903
                'aFooLabel, aBarDiffLabel, aNewLabel' // expected
904
905
            ]
        ];
906
    }
907

908
909
910
911
912
913
914
915
916
917
918
919
920
921
    /**
     * @test
     * @dataProvider getLabelsFromItemsListDataProvider
     *
     * @param string $table
     * @param string $col
     * @param string $keyList
     * @param array $tca
     * @param array $pageTsConfig
     * @param string $expectedLabel
     */
    public function getLabelsFromItemsListReturnsCorrectValue($table, $col, $keyList, $tca, array $pageTsConfig, $expectedLabel)
    {
        // Stub LanguageService and let sL() return the same value that came in again
922
        $GLOBALS['LANG'] = $this->createMock(LanguageService::class);
923
        $GLOBALS['LANG']->expects(self::any())->method('sL')->willReturnArgument(0);
924

925
926
        $GLOBALS['TCA'][$table] = $tca;
        $label = BackendUtility::getLabelsFromItemsList($table, $col, $keyList, $pageTsConfig);
927
        self::assertEquals($expectedLabel, $label);
928
    }
929

930
931
932
933
934
935
936
    /**
     * @test
     */
    public function getProcessedValueReturnsLabelsForExistingValuesSolely()
    {
        $table = 'foobar';
        $col = 'someColumn';
937
938
939
940
        $tca = [
            'columns' => [
                'someColumn' => [
                    'config' => [
941
                        'type' => 'select',
942
943
944
945
946
947
948
949
                        'items' => [
                            '0' => ['aFooLabel', 'foo'],
                            '1' => ['aBarLabel', 'bar']
                        ]
                    ]
                ]
            ]
        ];
950
        // Stub LanguageService and let sL() return the same value that came in again
951
        $GLOBALS['LANG'] = $this->createMock(LanguageService::class);
952
        $GLOBALS['LANG']->expects(self::any())->method('sL')->willReturnArgument(0);
953

954
955
        $GLOBALS['TCA'][$table] = $tca;
        $label = BackendUtility::getProcessedValue($table, $col, 'foo,invalidKey,bar');
956
        self::assertEquals('aFooLabel, aBarLabel', $label);
957
    }
958

959
960
961
962
963
964
965
    /**
     * @test
     */
    public function getProcessedValueReturnsPlainValueIfItemIsNotFound()
    {
        $table = 'foobar';
        $col = 'someColumn';
966
967
968
969
        $tca = [
            'columns' => [
                'someColumn' => [
                    'config' => [
970
                        'type' => 'select',
971
972
973
974
975
976
977
                        'items' => [
                            '0' => ['aFooLabel', 'foo']
                        ]
                    ]
                ]
            ]
        ];
978
        // Stub LanguageService and let sL() return the same value that came in again
979
        $GLOBALS['LANG'] = $this->createMock(LanguageService::class);
980
        $GLOBALS['LANG']->expects(self::any())->method('sL')->willReturnArgument(0);
981

982
983
        $GLOBALS['TCA'][$table] = $tca;
        $label = BackendUtility::getProcessedValue($table, $col, 'invalidKey');
984
        self::assertEquals('invalidKey', $label);
985
    }
986

987
988
989
    /**
     * @test
     */
990
991
    public function dateTimeAgeReturnsCorrectValues()
    {
992
993
994
995
996
997
        /** @var ObjectProphecy|LanguageService $languageServiceProphecy */
        $languageServiceProphecy = $this->prophesize(LanguageService::class);
        $languageServiceProphecy->sL(Argument::cetera())->willReturn(' min| hrs| days| yrs| min| hour| day| year');
        $GLOBALS['LANG'] = $languageServiceProphecy->reveal();
        $GLOBALS['EXEC_TIME'] = mktime(0, 0, 0, 3, 23, 2016);

998
999
        self::assertSame('24-03-16 00:00 (-1 day)', BackendUtility::dateTimeAge($GLOBALS['EXEC_TIME'] + 86400));
        self::assertSame('24-03-16 (-1 day)', BackendUtility::dateTimeAge($GLOBALS['EXEC_TIME'] + 86400, 1, 'date'));
1000
    }
1001

1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
    /**
     * @test
     */
    public function purgeComputedPropertyNamesRemovesPropertiesStartingWithUnderscore()
    {
        $propertyNames = [
            'uid',
            'pid',
            '_ORIG_PID'
        ];
        $computedPropertyNames = BackendUtility::purgeComputedPropertyNames($propertyNames);
        self::assertSame(['uid', 'pid'], $computedPropertyNames);
    }

    /**
     * @test
     */
    public function purgeComputedPropertiesFromRecordRemovesPropertiesStartingWithUnderscore()
    {
        $record = [
            'uid'       => 1,
            'pid'       => 2,
            '_ORIG_PID' => 1
        ];
        $expected = [
            'uid' => 1,
            'pid' => 2
        ];
        $computedProperties = BackendUtility::purgeComputedPropertiesFromRecord($record);
        self::assertSame($expected, $computedProperties);
    }
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047

    public function splitTableUidDataProvider()
    {
        return [
            'simple' => [
                'pages_23',
                ['pages', '23']
            ],
            'complex' => [
                'tt_content_13',
                ['tt_content', '13']
            ],
            'multiple underscores' => [
                'tx_runaway_domain_model_crime_scene_1234',
                ['tx_runaway_domain_model_crime_scene', '1234']
1048
1049
1050
1051
            ],
            'no underscore' => [
                'foo',
                ['', 'foo']
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
            ]
        ];
    }

    /**
     * @test
     * @dataProvider splitTableUidDataProvider
     */
    public function splitTableUid($input, $expected)
    {
        $result = BackendUtility::splitTable_Uid($input);
        self::assertSame($expected, $result);
    }
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075

    /**
     * Tests if the method getPagesTSconfig can be called without having a GLOBAL['BE_USER'] object.
     * However, this test also shows all the various other dependencies this method has.
     *
     * @test
     */
    public function getPagesTSconfigWorksWithoutInitializedBackendUser()
    {
        $expected = ['called.' => ['config']];
        $pageId = 13;
1076
1077
1078
1079
        $eventDispatcherProphecy = $this->prophesize(EventDispatcherInterface::class);
        $eventDispatcherProphecy->dispatch(Argument::any())->willReturn(new ModifyLoadedPageTsConfigEvent([], []));
        $loader = new PageTsConfigLoader($eventDispatcherProphecy->reveal());
        GeneralUtility::addInstance(PageTsConfigLoader::class, $loader);
1080
1081
1082
1083
1084
1085
        $parserProphecy = $this->prophesize(PageTsConfigParser::class);
        $parserProphecy->parse(Argument::cetera())->willReturn($expected);
        GeneralUtility::addInstance(PageTsConfigParser::class, $parserProphecy->reveal());

        $matcherProphecy = $this->prophesize(ConditionMatcher::class);
        GeneralUtility::addInstance(ConditionMatcher::class, $matcherProphecy->reveal());
1086

1087
1088
1089
1090
1091
1092
        $siteFinder = $this->prophesize(SiteFinder::class);
        $siteFinder->getSiteByPageId($pageId)->willReturn(
            new Site('dummy', $pageId, ['base' => 'https://example.com'])
        );
        GeneralUtility::addInstance(SiteFinder::class, $siteFinder->reveal());

1093
1094
        $cacheManagerProphecy = $this->prophesize(CacheManager::class);
        $cacheProphecy = $this->prophesize(FrontendInterface::class);
1095
        $cacheManagerProphecy->getCache('runtime')->willReturn($cacheProphecy->reveal());
1096
        $cacheHashProphecy = $this->prophesize(FrontendInterface::class);
1097
        $cacheManagerProphecy->getCache('hash')->willReturn($cacheHashProphecy->reveal());
1098
1099
1100
1101
1102
        $cacheProphecy->has(Argument::cetera())->willReturn(false);
        $cacheProphecy->get(Argument::cetera())->willReturn(false);
        $cacheProphecy->set(Argument::cetera())->willReturn(false);
        $cacheProphecy->get('backendUtilityBeGetRootLine')->willReturn(['13--1' => []]);
        GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal());
1103

1104
        $result = BackendUtility::getPagesTSconfig($pageId);
1105
        self::assertEquals($expected, $result);
1106
    }
1107
1108
1109
1110
1111
1112
1113
1114
1115

    /**
     * @test
     */
    public function returnNullForMissingTcaConfigInResolveFileReferences()
    {
        $tableName = 'table_a';
        $fieldName = 'field_a';
        $GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config'] = [];
1116
        self::assertNull(BackendUtility::resolveFileReferences($tableName, $fieldName, []));
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
    }

    /**
     * @test
     * @dataProvider unfitResolveFileReferencesTableConfig
     */
    public function returnNullForUnfitTableConfigInResolveFileReferences(array $config)
    {
        $tableName = 'table_a';
        $fieldName = 'field_a';
        $GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config'] = $config;
1128
        self::assertNull(BackendUtility::resolveFileReferences($tableName, $fieldName, []));
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
    }

    public function unfitResolveFileReferencesTableConfig(): array
    {
        return [
            'invalid table' => [
                [
                    'type' => 'inline',
                    'foreign_table' => 'table_b',
                ],
            ],
            'empty table' => [
                [
                    'type' => 'inline',
                    'foreign_table' => '',
                ],
            ],
            'invalid type' => [
                [
                    'type' => 'select',
                    'foreign_table' => 'sys_file_reference',
                ],
            ],
            'empty type' => [
                [
                    'type' => '',
                    'foreign_table' => 'sys_file_reference',
                ],
            ],
            'empty' => [
                [
                    'type' => '',
                    'foreign_table' => '',
                ],
            ],
        ];
    }

1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
    /**
     * @test
     */
    public function workspaceOLDoesNotChangeValuesForNoBeUserAvailable()
    {
        $GLOBALS['BE_USER'] = null;
        $tableName = 'table_a';
        $row = [
            'uid' => 1,
            'pid' => 17,
        ];
        $reference = $row;
        BackendUtility::workspaceOL($tableName, $row);
1180
        self::assertSame($reference, $row);
1181
1182
    }

1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
    /**
     * @test
     */
    public function resolveFileReferencesReturnsEmptyResultForNoReferencesAvailable()
    {
        $tableName = 'table_a';
        $fieldName = 'field_a';
        $relationHandler = $this->prophesize(RelationHandler::class);
        $relationHandler->start(
            'foo',
            'sys_file_reference',
            '',
            42,
            $tableName,
            ['type' => 'inline', 'foreign_table' => 'sys_file_reference']
        )->shouldBeCalled();
        $relationHandler->tableArray = ['sys_file_reference' => []];
        $relationHandler->processDeletePlaceholder()->shouldBeCalled();
        GeneralUtility::addInstance(RelationHandler::class, $relationHandler->reveal());
        $GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config'] = [
            'type' => 'inline',
            'foreign_table' => 'sys_file_reference',
        ];
        $elementData = [
            $fieldName => 'foo',
            'uid' => 42,
        ];

1211
        self::assertEmpty(BackendUtility::resolveFileReferences($tableName, $fieldName, $elementData));
1212
    }
1213
1214
1215
1216
1217
1218
1219
1220
1221

    /**
     * @test
     */
    public function wsMapIdReturnsLiveIdIfNoBeUserIsAvailable()
    {
        $GLOBALS['BE_USER'] = null;
        $tableName = 'table_a';
        $uid = 42;
1222
        self::assertSame(42, BackendUtility::wsMapId($tableName, $uid));
1223
    }
1224
}