‪TYPO3CMS  10.4
AbstractMenuContentObjectTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Doctrine\DBAL\Driver\Statement;
21 use Prophecy\Argument;
43 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
44 
48 class ‪AbstractMenuContentObjectTest extends UnitTestCase
49 {
53  protected ‪$subject;
54 
58  protected function ‪setUp(): void
59  {
60  parent::setUp();
61  ‪$GLOBALS['TYPO3_REQUEST'] = new ‪ServerRequest('https://www.example.com', 'GET');
62  $this->subject = $this->getAccessibleMockForAbstractClass(AbstractMenuContentObject::class);
63  $site = new ‪Site('test', 1, [
64  'base' => 'https://www.example.com',
65  'languages' => [
66  [
67  'languageId' => 0,
68  'title' => 'English',
69  'locale' => 'en_UK',
70  'base' => '/'
71  ]
72  ]
73  ]);
74  $cacheManagerProphecy = $this->prophesize(CacheManager::class);
75  GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal());
76  $cacheFrontendProphecy = $this->prophesize(FrontendInterface::class);
77  $cacheManagerProphecy->getCache('l10n')->willReturn($cacheFrontendProphecy->reveal());
78  $cacheFrontendProphecy->get(Argument::cetera())->willReturn(false);
79  $cacheFrontendProphecy->set(Argument::cetera())->willReturn(null);
80  $languageService = new ‪LanguageService(new ‪Locales(), new ‪LocalizationFactory(new ‪LanguageStore(), $cacheManagerProphecy->reveal()));
81  $languageServiceFactoryProphecy = $this->prophesize(LanguageServiceFactory::class);
82  $languageServiceFactoryProphecy->create(Argument::any())->willReturn($languageService);
83  GeneralUtility::addInstance(LanguageServiceFactory::class, $languageServiceFactoryProphecy->reveal());
84  ‪$GLOBALS['TSFE'] = $this->getMockBuilder(TypoScriptFrontendController::class)
85  ->setConstructorArgs([new ‪Context(), $site, $site->getDefaultLanguage(), new ‪PageArguments(1, '1', [])])
86  ->setMethods(['initCaches'])
87  ->getMock();
88  ‪$GLOBALS['TSFE']->cObj = new ‪ContentObjectRenderer();
89  ‪$GLOBALS['TSFE']->page = [];
90  }
91 
95  protected function ‪tearDown(): void
96  {
97  GeneralUtility::purgeInstances();
98  parent::tearDown();
99  }
100 
102  // Tests concerning sectionIndex
104 
107  protected function ‪prepareSectionIndexTest()
108  {
109  $connectionProphet = $this->prophesize(Connection::class);
110  $connectionProphet->getExpressionBuilder()->willReturn(new ‪ExpressionBuilder($connectionProphet->reveal()));
111  $connectionProphet->quoteIdentifier(Argument::cetera())->willReturnArgument(0);
112 
113  $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
114  $connectionPoolProphet->getConnectionForTable('tt_content')->willReturn($connectionProphet->reveal());
115  GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
116  }
117 
122  {
124  $pageRepository = $this->getMockBuilder(PageRepository::class)->getMock();
125  $pageRepository->expects(self::once())->method('getPage')->willReturn(null);
126  $this->subject->_set('sys_page', $pageRepository);
127  $result = $this->subject->_call('sectionIndex', 'field');
128  self::assertEquals($result, []);
129  }
130 
135  {
137  $pageRepository = $this->getMockBuilder(PageRepository::class)->getMock();
138  $pageRepository->expects(self::once())->method('getPage')->willReturn(null)->with(10);
139  $this->subject->_set('sys_page', $pageRepository);
140  $this->subject->_set('id', 10);
141  $result = $this->subject->_call('sectionIndex', 'field');
142  self::assertEquals($result, []);
143  }
144 
149  {
150  $this->expectException(\UnexpectedValueException::class);
151  $this->expectExceptionCode(1337334849);
153  $pageRepository = $this->getMockBuilder(PageRepository::class)->getMock();
154  $pageRepository->expects(self::once())->method('getPage')->willReturn([]);
155  $this->subject->_set('sys_page', $pageRepository);
156  $this->subject->_set('id', 10);
157 
158  $cObject = $this->getMockBuilder(ContentObjectRenderer::class)->getMock();
159  $cObject->expects(self::once())->method('exec_getQuery')->willReturn(0);
160  $this->subject->_set('parent_cObj', $cObject);
161 
162  $this->subject->_call('sectionIndex', 'field');
163  }
164 
169  {
170  $statementProphet = $this->prophesize(Statement::class);
171  $statementProphet->fetch()->shouldBeCalledTimes(2)->willReturn(['uid' => 0, 'header' => 'NOT_OVERLAID'], false);
172 
174  $this->subject->_set('mconf', [
175  'sectionIndex.' => [
176  'type' => 'all'
177  ]
178  ]);
179  $context = GeneralUtility::makeInstance(Context::class);
180  $context->setAspect('language', new ‪LanguageAspect(1, 1, ‪LanguageAspect::OVERLAYS_MIXED));
181 
182  $pageRepository = $this->getMockBuilder(PageRepository::class)->setConstructorArgs([$context])->getMock();
183  $pageRepository->expects(self::once())->method('getPage')->willReturn(['_PAGES_OVERLAY_LANGUAGE' => 1]);
184  $pageRepository->expects(self::once())->method('getRecordOverlay')->willReturn(['uid' => 0, 'header' => 'OVERLAID']);
185  $this->subject->_set('sys_page', $pageRepository);
186 
187  $cObject = $this->getMockBuilder(ContentObjectRenderer::class)->getMock();
188  $cObject->expects(self::once())->method('exec_getQuery')->willReturn($statementProphet->reveal());
189  $this->subject->_set('parent_cObj', $cObject);
190 
191  $result = $this->subject->_call('sectionIndex', 'field');
192  self::assertEquals($result[0]['title'], 'OVERLAID');
193  }
194 
198  public function ‪sectionIndexFiltersDataProvider()
199  {
200  return [
201  'unfiltered fields' => [
202  1,
203  [
204  'sectionIndex' => 1,
205  'header' => 'foo',
206  'header_layout' => 1
207  ]
208  ],
209  'with unset section index' => [
210  0,
211  [
212  'sectionIndex' => 0,
213  'header' => 'foo',
214  'header_layout' => 1
215  ]
216  ],
217  'with unset header' => [
218  0,
219  [
220  'sectionIndex' => 1,
221  'header' => '',
222  'header_layout' => 1
223  ]
224  ],
225  'with header layout 100' => [
226  0,
227  [
228  'sectionIndex' => 1,
229  'header' => 'foo',
230  'header_layout' => 100
231  ]
232  ]
233  ];
234  }
235 
242  public function ‪sectionIndexFilters($expectedAmount, array $dataRow)
243  {
244  $statementProphet = $this->prophesize(Statement::class);
245  $statementProphet->fetch()->willReturn($dataRow, false);
246 
248  $this->subject->_set('mconf', [
249  'sectionIndex.' => [
250  'type' => 'header'
251  ]
252  ]);
253 
254  $pageRepository = $this->getMockBuilder(PageRepository::class)->getMock();
255  $pageRepository->expects(self::once())->method('getPage')->willReturn(['_PAGES_OVERLAY_LANGUAGE' => 1]);
256  $pageRepository->expects(self::once())->method('getPage')->willReturn([]);
257  $this->subject->_set('sys_page', $pageRepository);
258 
259  $cObject = $this->getMockBuilder(ContentObjectRenderer::class)->getMock();
260  $cObject->expects(self::once())->method('exec_getQuery')->willReturn($statementProphet->reveal());
261  $this->subject->_set('parent_cObj', $cObject);
262 
263  $result = $this->subject->_call('sectionIndex', 'field');
264  self::assertCount($expectedAmount, $result);
265  }
266 
271  {
272  return [
273  'no configuration' => [
274  [],
275  'colPos = 0'
276  ],
277  'with useColPos 2' => [
278  ['useColPos' => 2],
279  'colPos = 2'
280  ],
281  'with useColPos -1' => [
282  ['useColPos' => -1],
283  ''
284  ],
285  'with stdWrap useColPos' => [
286  [
287  'useColPos.' => [
288  'wrap' => '2|'
289  ]
290  ],
291  'colPos = 2'
292  ]
293  ];
294  }
295 
302  public function ‪sectionIndexQueriesWithDifferentColPos($configuration, $whereClausePrefix)
303  {
304  $statementProphet = $this->prophesize(Statement::class);
305  $statementProphet->fetch()->willReturn([]);
306 
308  $this->subject->_set('mconf', ['sectionIndex.' => $configuration]);
309 
310  $pageRepository = $this->getMockBuilder(PageRepository::class)->getMock();
311  $pageRepository->expects(self::once())->method('getPage')->willReturn([]);
312  $this->subject->_set('sys_page', $pageRepository);
313 
314  $queryConfiguration = [
315  'pidInList' => 12,
316  'orderBy' => 'field',
317  'languageField' => 'sys_language_uid',
318  'where' => $whereClausePrefix
319  ];
320 
321  $cObject = $this->getMockBuilder(ContentObjectRenderer::class)->getMock();
322  $cObject->expects(self::once())->method('exec_getQuery')
323  ->with('tt_content', $queryConfiguration)->willReturn($statementProphet->reveal());
324  $this->subject->_set('parent_cObj', $cObject);
325 
326  $this->subject->_call('sectionIndex', 'field', 12);
327  }
328 
330  // Tests concerning menu item states
332 
336  {
337  return [
338  'none excluded' => [
339  [12, 34, 56],
340  '1, 23, 456',
341  true
342  ],
343  'one excluded' => [
344  [1, 234, 567],
345  '1, 23, 456',
346  true
347  ],
348  'three excluded' => [
349  [1, 23, 456],
350  '1, 23, 456',
351  false
352  ],
353  'empty excludeList' => [
354  [1, 123, 45],
355  '',
356  true
357  ],
358  'empty menu' => [
359  [],
360  '1, 23, 456',
361  false
362  ],
363  ];
364  }
365 
373  public function ‪ifsubHasToCheckExcludeUidList($menuItems, $excludeUidList, $expectedResult)
374  {
375  $menu = [];
376  foreach ($menuItems as $page) {
377  $menu[] = ['uid' => $page];
378  }
379  $runtimeCacheMock = $this->getMockBuilder(VariableFrontend::class)->setMethods(['get', 'set'])->disableOriginalConstructor()->getMock();
380  $runtimeCacheMock->expects(self::once())->method('get')->with(self::anything())->willReturn(false);
381  $runtimeCacheMock->expects(self::once())->method('set')->with(self::anything(), ['result' => $expectedResult]);
382 
383  $this->subject = $this->getAccessibleMockForAbstractClass(AbstractMenuContentObject::class, [], '', true, true, true, ['getRuntimeCache']);
384  $this->subject->expects(self::once())->method('getRuntimeCache')->willReturn($runtimeCacheMock);
386 
387  $pageRepository = $this->getMockBuilder(PageRepository::class)->getMock();
388  $pageRepository->expects(self::once())->method('getMenu')->willReturn($menu);
389  $this->subject->_set('sys_page', $pageRepository);
390  $this->subject->_set('menuArr', [
391  0 => ['uid' => 1]
392  ]);
393  $this->subject->_set('conf', ['excludeUidList' => $excludeUidList]);
394 
395  self::assertEquals($expectedResult, $this->subject->_call('isItemState', 'IFSUB', 0));
396  }
397 
402  {
403  return [
404  'standard parameter without access protected setting' => [
405  [
406  'parameter' => 1,
407  'linkAccessRestrictedPages' => false
408  ],
409  [
410  'showAccessRestrictedPages' => false
411  ],
412  ['uid' => 1],
413  '',
414  0,
415  ''
416  ],
417  'standard parameter with access protected setting' => [
418  [
419  'parameter' => 10,
420  'linkAccessRestrictedPages' => true
421  ],
422  [
423  'showAccessRestrictedPages' => true
424  ],
425  ['uid' => 10],
426  '',
427  0,
428  ''
429  ],
430  'standard parameter with access protected setting "NONE" casts to boolean linkAccessRestrictedPages (delegates resolving to typoLink method internals)' => [
431  [
432  'parameter' => 10,
433  'linkAccessRestrictedPages' => true
434  ],
435  [
436  'showAccessRestrictedPages' => 'NONE'
437  ],
438  ['uid' => 10],
439  '',
440  0,
441  ''
442  ],
443  'standard parameter with access protected setting (int)67 casts to boolean linkAccessRestrictedPages (delegates resolving to typoLink method internals)' => [
444  [
445  'parameter' => 10,
446  'linkAccessRestrictedPages' => true
447  ],
448  [
449  'showAccessRestrictedPages' => 67
450  ],
451  ['uid' => 10],
452  '',
453  0,
454  ''
455  ],
456  'standard parameter with target' => [
457  [
458  'parameter' => 1,
459  'target' => '_blank',
460  'linkAccessRestrictedPages' => false
461  ],
462  [
463  'showAccessRestrictedPages' => false
464  ],
465  ['uid' => 1],
466  '_blank',
467  0,
468  ''
469  ],
470  'parameter with typeOverride=10' => [
471  [
472  'parameter' => '10,10',
473  'linkAccessRestrictedPages' => false
474  ],
475  [
476  'showAccessRestrictedPages' => false
477  ],
478  ['uid' => 10],
479  '',
480  '',
481  10
482  ],
483  'parameter with target and typeOverride=10' => [
484  [
485  'parameter' => '10,10',
486  'linkAccessRestrictedPages' => false,
487  'target' => '_self'
488  ],
489  [
490  'showAccessRestrictedPages' => false
491  ],
492  ['uid' => 10],
493  '_self',
494  '',
495  '10'
496  ],
497  'parameter with invalid value in typeOverride=foobar ignores typeOverride' => [
498  [
499  'parameter' => 20,
500  'linkAccessRestrictedPages' => false,
501  'target' => '_self'
502  ],
503  [
504  'showAccessRestrictedPages' => false
505  ],
506  ['uid' => 20],
507  '_self',
508  '',
509  'foobar',
510  20
511  ],
512  'standard parameter with section name' => [
513  [
514  'parameter' => 10,
515  'target' => '_blank',
516  'linkAccessRestrictedPages' => false,
517  'section' => 'section-name'
518  ],
519  [
520  'showAccessRestrictedPages' => false
521  ],
522  [
523  'uid' => 10,
524  'sectionIndex_uid' => 'section-name'
525  ],
526  '_blank',
527  '',
528  ''
529  ],
530  'standard parameter with additional parameters' => [
531  [
532  'parameter' => 10,
533  'linkAccessRestrictedPages' => false,
534  'section' => 'section-name',
535  'additionalParams' => '&test=foobar'
536  ],
537  [
538  'showAccessRestrictedPages' => false
539  ],
540  [
541  'uid' => 10,
542  'sectionIndex_uid' => 'section-name'
543  ],
544  '',
545  '&test=foobar',
546  '',
547  ],
548  'overridden page array uid value gets used as parameter' => [
549  [
550  'parameter' => 99,
551  'linkAccessRestrictedPages' => false,
552  'section' => 'section-name'
553  ],
554  [
555  'showAccessRestrictedPages' => false
556  ],
557  [
558  'uid' => 10,
559  'sectionIndex_uid' => 'section-name'
560  ],
561  '',
562  '',
563  '',
564  99
565  ],
566  ];
567  }
568 
580  public function ‪menuTypoLinkCreatesExpectedTypoLinkConfiguration(array $expected, array $mconf, array $page, $oTarget, $addParams = '', $typeOverride = '', int $overrideId = null)
581  {
582  $cObject = $this->getMockBuilder(ContentObjectRenderer::class)
583  ->setMethods(['typoLink'])
584  ->getMock();
585  $cObject->expects(self::once())->method('typoLink')->with('|', $expected);
586  $this->subject->_set('parent_cObj', $cObject);
587  $this->subject->_set('mconf', $mconf);
588  $this->subject->_call('menuTypoLink', $page, $oTarget, $addParams, $typeOverride, $overrideId);
589  }
590 }
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:26
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\menuTypoLinkCreatesExpectedTypoLinkConfiguration
‪menuTypoLinkCreatesExpectedTypoLinkConfiguration(array $expected, array $mconf, array $page, $oTarget, $addParams='', $typeOverride='', int $overrideId=null)
Definition: AbstractMenuContentObjectTest.php:579
‪TYPO3\CMS\Core\Routing\PageArguments
Definition: PageArguments.php:26
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder
Definition: ExpressionBuilder.php:35
‪TYPO3\CMS\Core\Context\LanguageAspect\OVERLAYS_MIXED
‪const OVERLAYS_MIXED
Definition: LanguageAspect.php:75
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\sectionIndexReturnsOverlaidRowBasedOnTheLanguageOfTheGivenPage
‪sectionIndexReturnsOverlaidRowBasedOnTheLanguageOfTheGivenPage()
Definition: AbstractMenuContentObjectTest.php:167
‪TYPO3\CMS\Core\Localization\LocalizationFactory
Definition: LocalizationFactory.php:28
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\sectionIndexUsesTheInternalIdIfNoPageIdWasGiven
‪sectionIndexUsesTheInternalIdIfNoPageIdWasGiven()
Definition: AbstractMenuContentObjectTest.php:133
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\ifsubHasToCheckExcludeUidList
‪ifsubHasToCheckExcludeUidList($menuItems, $excludeUidList, $expectedResult)
Definition: AbstractMenuContentObjectTest.php:372
‪TYPO3\CMS\Core\Localization\Locales
Definition: Locales.php:30
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\sectionIndexFiltersDataProvider
‪array sectionIndexFiltersDataProvider()
Definition: AbstractMenuContentObjectTest.php:197
‪TYPO3\CMS\Frontend\ContentObject\Menu\AbstractMenuContentObject
Definition: AbstractMenuContentObject.php:46
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:40
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\menuTypoLinkCreatesExpectedTypoLinkConfigurationDataProvider
‪array menuTypoLinkCreatesExpectedTypoLinkConfigurationDataProvider()
Definition: AbstractMenuContentObjectTest.php:400
‪TYPO3\CMS\Core\Localization\LanguageStore
Definition: LanguageStore.php:27
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\tearDown
‪tearDown()
Definition: AbstractMenuContentObjectTest.php:94
‪TYPO3\CMS\Core\Cache\Frontend\VariableFrontend
Definition: VariableFrontend.php:25
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\setUp
‪setUp()
Definition: AbstractMenuContentObjectTest.php:57
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:35
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:37
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\sectionIndexReturnsEmptyArrayIfTheRequestedPageCouldNotBeFetched
‪sectionIndexReturnsEmptyArrayIfTheRequestedPageCouldNotBeFetched()
Definition: AbstractMenuContentObjectTest.php:120
‪TYPO3\CMS\Core\Context\LanguageAspect
Definition: LanguageAspect.php:57
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest
Definition: AbstractMenuContentObjectTest.php:49
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\sectionIndexQueriesWithDifferentColPos
‪sectionIndexQueriesWithDifferentColPos($configuration, $whereClausePrefix)
Definition: AbstractMenuContentObjectTest.php:301
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:36
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:98
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\ifsubHasToCheckExcludeUidListDataProvider
‪array ifsubHasToCheckExcludeUidListDataProvider()
Definition: AbstractMenuContentObjectTest.php:334
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\$subject
‪AbstractMenuContentObject $subject
Definition: AbstractMenuContentObjectTest.php:52
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:97
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\prepareSectionIndexTest
‪prepareSectionIndexTest()
Definition: AbstractMenuContentObjectTest.php:106
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Domain\Repository\PageRepository
Definition: PageRepository.php:52
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\sectionIndexQueriesWithDifferentColPosDataProvider
‪array sectionIndexQueriesWithDifferentColPosDataProvider()
Definition: AbstractMenuContentObjectTest.php:269
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\sectionIndexThrowsAnExceptionIfTheInternalQueryFails
‪sectionIndexThrowsAnExceptionIfTheInternalQueryFails()
Definition: AbstractMenuContentObjectTest.php:147
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu
Definition: AbstractMenuContentObjectTest.php:18
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\sectionIndexFilters
‪sectionIndexFilters($expectedAmount, array $dataRow)
Definition: AbstractMenuContentObjectTest.php:241