AbstractMenuContentObjectTest.php 21.9 KB
Newer Older
1
<?php
2

3
declare(strict_types=1);
4

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

namespace TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu;

20
use Doctrine\DBAL\Statement;
21
use Prophecy\Argument;
22
23
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
24
use TYPO3\CMS\Core\Cache\Frontend\VariableFrontend;
25
26
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\LanguageAspect;
27
use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder;
28
29
30
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
31
use TYPO3\CMS\Core\Domain\Repository\PageRepository;
32
use TYPO3\CMS\Core\Http\ServerRequest;
33
34
35
36
37
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Localization\LanguageServiceFactory;
use TYPO3\CMS\Core\Localization\LanguageStore;
use TYPO3\CMS\Core\Localization\Locales;
use TYPO3\CMS\Core\Localization\LocalizationFactory;
38
39
use TYPO3\CMS\Core\Routing\PageArguments;
use TYPO3\CMS\Core\Site\Entity\Site;
40
use TYPO3\CMS\Core\Utility\GeneralUtility;
41
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
42
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
43
use TYPO3\CMS\Frontend\ContentObject\Menu\AbstractMenuContentObject;
44
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
45
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
46

47
/**
48
 * Test case
49
 */
50
class AbstractMenuContentObjectTest extends UnitTestCase
51
{
52
53
    /**
     * @var AbstractMenuContentObject
54
     */
55
    protected $subject;
56

57
58
59
    /**
     * Set up this testcase
     */
60
    protected function setUp(): void
61
    {
62
        parent::setUp();
63
64
        $GLOBALS['TYPO3_REQUEST'] = (new ServerRequest('https://www.example.com', 'GET'))
            ->withAttribute('applicationType', SystemEnvironmentBuilder::REQUESTTYPE_FE);
65
        $this->subject = $this->getAccessibleMockForAbstractClass(AbstractMenuContentObject::class);
66
67
68
69
70
71
72
73
74
75
76
        $site = new Site('test', 1, [
            'base' => 'https://www.example.com',
            'languages' => [
                [
                    'languageId' => 0,
                    'title' => 'English',
                    'locale' => 'en_UK',
                    'base' => '/'
                ]
            ]
        ]);
77
78
79
80
81
82
        $cacheManagerProphecy = $this->prophesize(CacheManager::class);
        GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal());
        $cacheFrontendProphecy = $this->prophesize(FrontendInterface::class);
        $cacheManagerProphecy->getCache('l10n')->willReturn($cacheFrontendProphecy->reveal());
        $cacheFrontendProphecy->get(Argument::cetera())->willReturn(false);
        $cacheFrontendProphecy->set(Argument::cetera())->willReturn(null);
83
        $languageService = new LanguageService(new Locales(), new LocalizationFactory(new LanguageStore(), $cacheManagerProphecy->reveal()), $cacheFrontendProphecy->reveal());
84
        $languageServiceFactoryProphecy = $this->prophesize(LanguageServiceFactory::class);
85
        $languageServiceFactoryProphecy->createFromSiteLanguage(Argument::any())->willReturn($languageService);
86
        GeneralUtility::addInstance(LanguageServiceFactory::class, $languageServiceFactoryProphecy->reveal());
87
        $frontendUserProphecy = $this->prophesize(FrontendUserAuthentication::class);
88
        $GLOBALS['TSFE'] = $this->getMockBuilder(TypoScriptFrontendController::class)
89
            ->setConstructorArgs([new Context(), $site, $site->getDefaultLanguage(), new PageArguments(1, '1', []), $frontendUserProphecy->reveal()])
90
            ->setMethods(['initCaches'])
91
            ->getMock();
92
        $GLOBALS['TSFE']->cObj = new ContentObjectRenderer();
93
        $GLOBALS['TSFE']->page = [];
94
95
96
97
98
    }

    /**
     * Reset singleton instances
     */
99
    protected function tearDown(): void
100
101
102
    {
        GeneralUtility::purgeInstances();
        parent::tearDown();
103
    }
104

105
106
107
108
109
110
111
112
    ////////////////////////////////
    // Tests concerning sectionIndex
    ////////////////////////////////
    /**
     * Prepares a test for the method sectionIndex
     */
    protected function prepareSectionIndexTest()
    {
113
114
115
116
117
118
119
        $connectionProphet = $this->prophesize(Connection::class);
        $connectionProphet->getExpressionBuilder()->willReturn(new ExpressionBuilder($connectionProphet->reveal()));
        $connectionProphet->quoteIdentifier(Argument::cetera())->willReturnArgument(0);

        $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
        $connectionPoolProphet->getConnectionForTable('tt_content')->willReturn($connectionProphet->reveal());
        GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
120
    }
121

122
123
124
125
126
127
    /**
     * @test
     */
    public function sectionIndexReturnsEmptyArrayIfTheRequestedPageCouldNotBeFetched()
    {
        $this->prepareSectionIndexTest();
128
        $pageRepository = $this->getMockBuilder(PageRepository::class)->getMock();
129
        $pageRepository->expects(self::once())->method('getPage')->willReturn(null);
130
        $this->subject->_set('sys_page', $pageRepository);
131
        $result = $this->subject->_call('sectionIndex', 'field');
132
        self::assertEquals($result, []);
133
    }
134

135
136
137
138
139
140
    /**
     * @test
     */
    public function sectionIndexUsesTheInternalIdIfNoPageIdWasGiven()
    {
        $this->prepareSectionIndexTest();
141
        $pageRepository = $this->getMockBuilder(PageRepository::class)->getMock();
142
        $pageRepository->expects(self::once())->method('getPage')->willReturn(null)->with(10);
143
144
        $this->subject->_set('sys_page', $pageRepository);
        $this->subject->_set('id', 10);
145
        $result = $this->subject->_call('sectionIndex', 'field');
146
        self::assertEquals($result, []);
147
    }
148

149
150
151
152
153
    /**
     * @test
     */
    public function sectionIndexThrowsAnExceptionIfTheInternalQueryFails()
    {
154
155
        $this->expectException(\UnexpectedValueException::class);
        $this->expectExceptionCode(1337334849);
156
        $this->prepareSectionIndexTest();
157
        $pageRepository = $this->getMockBuilder(PageRepository::class)->getMock();
158
        $pageRepository->expects(self::once())->method('getPage')->willReturn([]);
159
160
161
162
        $this->subject->_set('sys_page', $pageRepository);
        $this->subject->_set('id', 10);

        $cObject = $this->getMockBuilder(ContentObjectRenderer::class)->getMock();
163
        $cObject->expects(self::once())->method('exec_getQuery')->willReturn(0);
164
165
        $this->subject->_set('parent_cObj', $cObject);

166
167
        $this->subject->_call('sectionIndex', 'field');
    }
168

169
170
171
172
173
    /**
     * @test
     */
    public function sectionIndexReturnsOverlaidRowBasedOnTheLanguageOfTheGivenPage()
    {
174
        $statementProphet = $this->prophesize(Statement::class);
175
        $statementProphet->fetchAssociative()->shouldBeCalledTimes(2)->willReturn(['uid' => 0, 'header' => 'NOT_OVERLAID'], false);
176

177
        $this->prepareSectionIndexTest();
178
179
180
181
182
        $this->subject->_set('mconf', [
            'sectionIndex.' => [
                'type' => 'all'
            ]
        ]);
183
184
        $context = GeneralUtility::makeInstance(Context::class);
        $context->setAspect('language', new LanguageAspect(1, 1, LanguageAspect::OVERLAYS_MIXED));
185
186

        $pageRepository = $this->getMockBuilder(PageRepository::class)->setConstructorArgs([$context])->getMock();
187
188
        $pageRepository->expects(self::once())->method('getPage')->willReturn(['_PAGES_OVERLAY_LANGUAGE' => 1]);
        $pageRepository->expects(self::once())->method('getRecordOverlay')->willReturn(['uid' => 0, 'header' => 'OVERLAID']);
189
190
191
        $this->subject->_set('sys_page', $pageRepository);

        $cObject = $this->getMockBuilder(ContentObjectRenderer::class)->getMock();
192
        $cObject->expects(self::once())->method('exec_getQuery')->willReturn($statementProphet->reveal());
193
194
        $this->subject->_set('parent_cObj', $cObject);

195
        $result = $this->subject->_call('sectionIndex', 'field');
196
        self::assertEquals($result[0]['title'], 'OVERLAID');
197
    }
198

199
200
201
202
203
    /**
     * @return array
     */
    public function sectionIndexFiltersDataProvider()
    {
204
205
        return [
            'unfiltered fields' => [
206
                1,
207
                [
208
209
210
                    'sectionIndex' => 1,
                    'header' => 'foo',
                    'header_layout' => 1
211
212
213
                ]
            ],
            'with unset section index' => [
214
                0,
215
                [
216
217
218
                    'sectionIndex' => 0,
                    'header' => 'foo',
                    'header_layout' => 1
219
220
221
                ]
            ],
            'with unset header' => [
222
                0,
223
                [
224
225
226
                    'sectionIndex' => 1,
                    'header' => '',
                    'header_layout' => 1
227
228
229
                ]
            ],
            'with header layout 100' => [
230
                0,
231
                [
232
233
234
                    'sectionIndex' => 1,
                    'header' => 'foo',
                    'header_layout' => 100
235
236
237
                ]
            ]
        ];
238
    }
239

240
241
242
243
244
245
246
247
    /**
     * @test
     * @dataProvider sectionIndexFiltersDataProvider
     * @param int $expectedAmount
     * @param array $dataRow
     */
    public function sectionIndexFilters($expectedAmount, array $dataRow)
    {
248
        $statementProphet = $this->prophesize(Statement::class);
249
        $statementProphet->fetchAssociative()->willReturn($dataRow, false);
250

251
        $this->prepareSectionIndexTest();
252
253
254
255
256
257
258
        $this->subject->_set('mconf', [
            'sectionIndex.' => [
                'type' => 'header'
            ]
        ]);

        $pageRepository = $this->getMockBuilder(PageRepository::class)->getMock();
259
260
        $pageRepository->expects(self::once())->method('getPage')->willReturn(['_PAGES_OVERLAY_LANGUAGE' => 1]);
        $pageRepository->expects(self::once())->method('getPage')->willReturn([]);
261
262
263
        $this->subject->_set('sys_page', $pageRepository);

        $cObject = $this->getMockBuilder(ContentObjectRenderer::class)->getMock();
264
        $cObject->expects(self::once())->method('exec_getQuery')->willReturn($statementProphet->reveal());
265
266
        $this->subject->_set('parent_cObj', $cObject);

267
        $result = $this->subject->_call('sectionIndex', 'field');
268
        self::assertCount($expectedAmount, $result);
269
    }
270

271
272
273
274
275
    /**
     * @return array
     */
    public function sectionIndexQueriesWithDifferentColPosDataProvider()
    {
276
277
278
        return [
            'no configuration' => [
                [],
279
                '0',
280
                'colPos = 0'
281
282
            ],
            'with useColPos 2' => [
283
284
285
286
                [
                    'useColPos' => 2
                ],
                '2',
287
                'colPos = 2'
288
289
            ],
            'with useColPos -1' => [
290
291
292
293
                [
                    'useColPos' => -1
                ],
                '-1',
294
                ''
295
296
297
298
            ],
            'with stdWrap useColPos' => [
                [
                    'useColPos.' => [
299
                        'wrap' => '2|'
300
301
                    ]
                ],
302
                '2',
303
                'colPos = 2'
304
305
            ]
        ];
306
    }
307

308
309
310
311
    /**
     * @test
     * @dataProvider sectionIndexQueriesWithDifferentColPosDataProvider
     * @param array $configuration
312
     * @param array $colPosFromStdWrapValue
313
314
     * @param string $whereClausePrefix
     */
315
    public function sectionIndexQueriesWithDifferentColPos($configuration, $colPosFromStdWrapValue, $whereClausePrefix)
316
    {
317
        $statementProphet = $this->prophesize(Statement::class);
318
        $statementProphet->fetchAssociative()->willReturn([]);
319

320
        $this->prepareSectionIndexTest();
321
322
323
        $this->subject->_set('mconf', ['sectionIndex.' => $configuration]);

        $pageRepository = $this->getMockBuilder(PageRepository::class)->getMock();
324
        $pageRepository->expects(self::once())->method('getPage')->willReturn([]);
325
326
        $this->subject->_set('sys_page', $pageRepository);

327
        $queryConfiguration = [
328
329
330
331
            'pidInList' => 12,
            'orderBy' => 'field',
            'languageField' => 'sys_language_uid',
            'where' => $whereClausePrefix
332
        ];
333
334

        $cObject = $this->getMockBuilder(ContentObjectRenderer::class)->getMock();
335
        $cObject
336
            ->expects(self::once())
337
338
339
340
341
342
343
344
            ->method('stdWrapValue')
            ->with('useColPos', $configuration)
            ->willReturn($colPosFromStdWrapValue);
        $cObject
            ->expects(self::once())
            ->method('exec_getQuery')
            ->with('tt_content', $queryConfiguration)
            ->willReturn($statementProphet->reveal());
345
        $this->subject->parent_cObj = $cObject;
346

347
348
        $this->subject->_call('sectionIndex', 'field', 12);
    }
349

350
351
352
353
354
355
356
357
    ////////////////////////////////////
    // Tests concerning menu item states
    ////////////////////////////////////
    /**
     * @return array
     */
    public function ifsubHasToCheckExcludeUidListDataProvider()
    {
358
359
360
        return [
            'none excluded' => [
                [12, 34, 56],
361
362
                '1, 23, 456',
                true
363
364
365
            ],
            'one excluded' => [
                [1, 234, 567],
366
367
                '1, 23, 456',
                true
368
369
370
            ],
            'three excluded' => [
                [1, 23, 456],
371
372
                '1, 23, 456',
                false
373
374
375
            ],
            'empty excludeList' => [
                [1, 123, 45],
376
377
                '',
                true
378
379
380
            ],
            'empty menu' => [
                [],
381
382
                '1, 23, 456',
                false
383
384
            ],
        ];
385
    }
386

387
388
389
390
391
392
393
394
395
    /**
     * @test
     * @dataProvider ifsubHasToCheckExcludeUidListDataProvider
     * @param array $menuItems
     * @param string $excludeUidList
     * @param bool $expectedResult
     */
    public function ifsubHasToCheckExcludeUidList($menuItems, $excludeUidList, $expectedResult)
    {
396
        $menu = [];
397
        foreach ($menuItems as $page) {
398
            $menu[] = ['uid' => $page];
399
        }
400
        $runtimeCacheMock = $this->getMockBuilder(VariableFrontend::class)->setMethods(['get', 'set'])->disableOriginalConstructor()->getMock();
401
402
        $runtimeCacheMock->expects(self::once())->method('get')->with(self::anything())->willReturn(false);
        $runtimeCacheMock->expects(self::once())->method('set')->with(self::anything(), ['result' => $expectedResult]);
403

404
        $this->subject = $this->getAccessibleMockForAbstractClass(AbstractMenuContentObject::class, [], '', true, true, true, ['getRuntimeCache']);
405
406
        $cObjectMock = $this->getMockBuilder(ContentObjectRenderer::class)->getMock();
        $cObjectMock
407
            ->expects(self::once())
408
409
410
411
            ->method('stdWrapValue')
            ->with('excludeUidList', ['excludeUidList' => $excludeUidList])
            ->willReturn($excludeUidList);
        $this->subject->parent_cObj = $cObjectMock;
412
        $this->subject->expects(self::once())->method('getRuntimeCache')->willReturn($runtimeCacheMock);
413
        $this->prepareSectionIndexTest();
414

415
        $pageRepository = $this->getMockBuilder(PageRepository::class)->getMock();
416
        $pageRepository->expects(self::once())->method('getMenu')->willReturn($menu);
417
418
        $this->subject->_set('sys_page', $pageRepository);
        $this->subject->_set('menuArr', [
419
            0 => ['uid' => 1]
420
421
        ]);
        $this->subject->_set('conf', ['excludeUidList' => $excludeUidList]);
422

423
        self::assertEquals($expectedResult, $this->subject->_call('isItemState', 'IFSUB', 0));
424
    }
425
426
427
428

    /**
     * @return array
     */
429
    public function menuTypoLinkCreatesExpectedTypoLinkConfigurationDataProvider()
430
431
432
433
434
    {
        return [
            'standard parameter without access protected setting' => [
                [
                    'parameter' => 1,
435
                    'linkAccessRestrictedPages' => false
436
437
438
439
440
441
442
443
444
445
446
447
                ],
                [
                    'showAccessRestrictedPages' => false
                ],
                ['uid' => 1],
                '',
                0,
                ''
            ],
            'standard parameter with access protected setting' => [
                [
                    'parameter' => 10,
448
                    'linkAccessRestrictedPages' => true
449
450
451
452
453
454
455
456
457
458
459
460
                ],
                [
                    'showAccessRestrictedPages' => true
                ],
                ['uid' => 10],
                '',
                0,
                ''
            ],
            'standard parameter with access protected setting "NONE" casts to boolean linkAccessRestrictedPages (delegates resolving to typoLink method internals)' => [
                [
                    'parameter' => 10,
461
                    'linkAccessRestrictedPages' => true
462
463
464
465
466
467
468
469
470
471
472
473
                ],
                [
                    'showAccessRestrictedPages' => 'NONE'
                ],
                ['uid' => 10],
                '',
                0,
                ''
            ],
            'standard parameter with access protected setting (int)67 casts to boolean linkAccessRestrictedPages (delegates resolving to typoLink method internals)' => [
                [
                    'parameter' => 10,
474
                    'linkAccessRestrictedPages' => true
475
476
477
478
479
480
481
482
483
484
485
486
487
                ],
                [
                    'showAccessRestrictedPages' => 67
                ],
                ['uid' => 10],
                '',
                0,
                ''
            ],
            'standard parameter with target' => [
                [
                    'parameter' => 1,
                    'target' => '_blank',
488
                    'linkAccessRestrictedPages' => false
489
490
491
492
493
494
495
496
497
498
499
500
                ],
                [
                    'showAccessRestrictedPages' => false
                ],
                ['uid' => 1],
                '_blank',
                0,
                ''
            ],
            'parameter with typeOverride=10' => [
                [
                    'parameter' => '10,10',
501
                    'linkAccessRestrictedPages' => false
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
                ],
                [
                    'showAccessRestrictedPages' => false
                ],
                ['uid' => 10],
                '',
                '',
                10
            ],
            'parameter with target and typeOverride=10' => [
                [
                    'parameter' => '10,10',
                    'linkAccessRestrictedPages' => false,
                    'target' => '_self'
                ],
                [
                    'showAccessRestrictedPages' => false
                ],
                ['uid' => 10],
                '_self',
                '',
523
                '10'
524
525
526
527
528
529
530
531
532
533
534
535
536
            ],
            'parameter with invalid value in typeOverride=foobar ignores typeOverride' => [
                [
                    'parameter' => 20,
                    'linkAccessRestrictedPages' => false,
                    'target' => '_self'
                ],
                [
                    'showAccessRestrictedPages' => false
                ],
                ['uid' => 20],
                '_self',
                '',
537
538
                'foobar',
                20
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
            ],
            'standard parameter with section name' => [
                [
                    'parameter' => 10,
                    'target' => '_blank',
                    'linkAccessRestrictedPages' => false,
                    'section' => 'section-name'
                ],
                [
                    'showAccessRestrictedPages' => false
                ],
                [
                    'uid' => 10,
                    'sectionIndex_uid' => 'section-name'
                ],
                '_blank',
                '',
                ''
            ],
            'standard parameter with additional parameters' => [
                [
                    'parameter' => 10,
                    'linkAccessRestrictedPages' => false,
                    'section' => 'section-name',
                    'additionalParams' => '&test=foobar'
                ],
                [
                    'showAccessRestrictedPages' => false
                ],
                [
                    'uid' => 10,
                    'sectionIndex_uid' => 'section-name'
                ],
                '',
                '&test=foobar',
574
                '',
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
            ],
            'overridden page array uid value gets used as parameter' => [
                [
                    'parameter' => 99,
                    'linkAccessRestrictedPages' => false,
                    'section' => 'section-name'
                ],
                [
                    'showAccessRestrictedPages' => false
                ],
                [
                    'uid' => 10,
                    'sectionIndex_uid' => 'section-name'
                ],
                '',
                '',
                '',
592
                99
593
594
595
596
597
598
            ],
        ];
    }

    /**
     * @test
599
     * @dataProvider menuTypoLinkCreatesExpectedTypoLinkConfigurationDataProvider
600
601
602
603
604
605
     * @param array $expected
     * @param array $mconf
     * @param array $page
     * @param mixed $oTarget
     * @param string $addParams
     * @param string $typeOverride
606
     * @param int $overrideId
607
     */
608
    public function menuTypoLinkCreatesExpectedTypoLinkConfiguration(array $expected, array $mconf, array $page, $oTarget, $addParams = '', $typeOverride = '', int $overrideId = null)
609
    {
610
        $cObject = $this->getMockBuilder(ContentObjectRenderer::class)
611
612
            ->setMethods(['typoLink'])
            ->getMock();
613
        $cObject->expects(self::once())->method('typoLink')->with('|', $expected);
614
615
        $this->subject->_set('parent_cObj', $cObject);
        $this->subject->_set('mconf', $mconf);
616
        $this->subject->_call('menuTypoLink', $page, $oTarget, $addParams, $typeOverride, $overrideId);
617
    }
618
}