‪TYPO3CMS  9.5
AbstractMenuContentObjectTest.php
Go to the documentation of this file.
1 <?php
2 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 use Doctrine\DBAL\Driver\Statement;
18 use Prophecy\Argument;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
34 class ‪AbstractMenuContentObjectTest extends UnitTestCase
35 {
39  protected ‪$subject;
40 
44  protected function ‪setUp()
45  {
46  $proxyClassName = $this->buildAccessibleProxy(AbstractMenuContentObject::class);
47  $this->subject = $this->getMockForAbstractClass($proxyClassName);
48  ‪$GLOBALS['TSFE'] = $this->getMockBuilder(\‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::class)
49  ->setConstructorArgs([‪$GLOBALS['TYPO3_CONF_VARS'], 1, 1])
50  ->setMethods(['initCaches'])
51  ->getMock();
52  ‪$GLOBALS['TSFE']->cObj = new ‪ContentObjectRenderer();
53  ‪$GLOBALS['TSFE']->page = [];
54  }
55 
59  protected function ‪tearDown()
60  {
61  GeneralUtility::purgeInstances();
62  parent::tearDown();
63  }
64 
66  // Tests concerning sectionIndex
68 
71  protected function ‪prepareSectionIndexTest()
72  {
73  $connectionProphet = $this->prophesize(Connection::class);
74  $connectionProphet->getExpressionBuilder()->willReturn(new ‪ExpressionBuilder($connectionProphet->reveal()));
75  $connectionProphet->quoteIdentifier(Argument::cetera())->willReturnArgument(0);
76 
77  $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
78  $connectionPoolProphet->getConnectionForTable('tt_content')->willReturn($connectionProphet->reveal());
79  GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
80  }
81 
86  {
88  $pageRepository = $this->getMockBuilder(PageRepository::class)->getMock();
89  $pageRepository->expects($this->once())->method('getPage')->will($this->returnValue(null));
90  $this->subject->_set('sys_page', $pageRepository);
91  $result = $this->subject->_call('sectionIndex', 'field');
92  $this->assertEquals($result, []);
93  }
94 
99  {
101  $pageRepository = $this->getMockBuilder(PageRepository::class)->getMock();
102  $pageRepository->expects($this->once())->method('getPage')->will($this->returnValue(null))->with(10);
103  $this->subject->_set('sys_page', $pageRepository);
104  $this->subject->_set('id', 10);
105  $result = $this->subject->_call('sectionIndex', 'field');
106  $this->assertEquals($result, []);
107  }
108 
113  {
114  $this->expectException(\UnexpectedValueException::class);
115  $this->expectExceptionCode(1337334849);
117  $pageRepository = $this->getMockBuilder(PageRepository::class)->getMock();
118  $pageRepository->expects($this->once())->method('getPage')->will($this->returnValue([]));
119  $this->subject->_set('sys_page', $pageRepository);
120  $this->subject->_set('id', 10);
121 
122  $cObject = $this->getMockBuilder(ContentObjectRenderer::class)->getMock();
123  $cObject->expects($this->once())->method('exec_getQuery')->will($this->returnValue(0));
124  $this->subject->_set('parent_cObj', $cObject);
125 
126  $this->subject->_call('sectionIndex', 'field');
127  }
128 
133  {
134  $statementProphet = $this->prophesize(Statement::class);
135  $statementProphet->fetch()->shouldBeCalledTimes(2)->willReturn(['uid' => 0, 'header' => 'NOT_OVERLAID'], false);
136 
138  $this->subject->_set('mconf', [
139  'sectionIndex.' => [
140  'type' => 'all'
141  ]
142  ]);
143  $context = GeneralUtility::makeInstance(Context::class);
144  $context->setAspect('language', new ‪LanguageAspect(1, 1, ‪LanguageAspect::OVERLAYS_MIXED));
145 
146  $pageRepository = $this->getMockBuilder(PageRepository::class)->setConstructorArgs([$context])->getMock();
147  $pageRepository->expects($this->once())->method('getPage')->will($this->returnValue(['_PAGES_OVERLAY_LANGUAGE' => 1]));
148  $pageRepository->expects($this->once())->method('getRecordOverlay')->will($this->returnValue(['uid' => 0, 'header' => 'OVERLAID']));
149  $this->subject->_set('sys_page', $pageRepository);
150 
151  $cObject = $this->getMockBuilder(ContentObjectRenderer::class)->getMock();
152  $cObject->expects($this->once())->method('exec_getQuery')->willReturn($statementProphet->reveal());
153  $this->subject->_set('parent_cObj', $cObject);
154 
155  $result = $this->subject->_call('sectionIndex', 'field');
156  $this->assertEquals($result[0]['title'], 'OVERLAID');
157  }
158 
162  public function ‪sectionIndexFiltersDataProvider()
163  {
164  return [
165  'unfiltered fields' => [
166  1,
167  [
168  'sectionIndex' => 1,
169  'header' => 'foo',
170  'header_layout' => 1
171  ]
172  ],
173  'with unset section index' => [
174  0,
175  [
176  'sectionIndex' => 0,
177  'header' => 'foo',
178  'header_layout' => 1
179  ]
180  ],
181  'with unset header' => [
182  0,
183  [
184  'sectionIndex' => 1,
185  'header' => '',
186  'header_layout' => 1
187  ]
188  ],
189  'with header layout 100' => [
190  0,
191  [
192  'sectionIndex' => 1,
193  'header' => 'foo',
194  'header_layout' => 100
195  ]
196  ]
197  ];
198  }
199 
206  public function ‪sectionIndexFilters($expectedAmount, array $dataRow)
207  {
208  $statementProphet = $this->prophesize(Statement::class);
209  $statementProphet->fetch()->willReturn($dataRow, false);
210 
212  $this->subject->_set('mconf', [
213  'sectionIndex.' => [
214  'type' => 'header'
215  ]
216  ]);
217 
218  $pageRepository = $this->getMockBuilder(PageRepository::class)->getMock();
219  $pageRepository->expects($this->once())->method('getPage')->will($this->returnValue(['_PAGES_OVERLAY_LANGUAGE' => 1]));
220  $pageRepository->expects($this->once())->method('getPage')->will($this->returnValue([]));
221  $this->subject->_set('sys_page', $pageRepository);
222 
223  $cObject = $this->getMockBuilder(ContentObjectRenderer::class)->getMock();
224  $cObject->expects($this->once())->method('exec_getQuery')->willReturn($statementProphet->reveal());
225  $this->subject->_set('parent_cObj', $cObject);
226 
227  $result = $this->subject->_call('sectionIndex', 'field');
228  $this->assertCount($expectedAmount, $result);
229  }
230 
235  {
236  return [
237  'no configuration' => [
238  [],
239  'colPos = 0'
240  ],
241  'with useColPos 2' => [
242  ['useColPos' => 2],
243  'colPos = 2'
244  ],
245  'with useColPos -1' => [
246  ['useColPos' => -1],
247  ''
248  ],
249  'with stdWrap useColPos' => [
250  [
251  'useColPos.' => [
252  'wrap' => '2|'
253  ]
254  ],
255  'colPos = 2'
256  ]
257  ];
258  }
259 
266  public function ‪sectionIndexQueriesWithDifferentColPos($configuration, $whereClausePrefix)
267  {
268  $statementProphet = $this->prophesize(Statement::class);
269  $statementProphet->fetch()->willReturn([]);
270 
272  $this->subject->_set('mconf', ['sectionIndex.' => $configuration]);
273 
274  $pageRepository = $this->getMockBuilder(PageRepository::class)->getMock();
275  $pageRepository->expects($this->once())->method('getPage')->will($this->returnValue([]));
276  $this->subject->_set('sys_page', $pageRepository);
277 
278  $queryConfiguration = [
279  'pidInList' => 12,
280  'orderBy' => 'field',
281  'languageField' => 'sys_language_uid',
282  'where' => $whereClausePrefix
283  ];
284 
285  $cObject = $this->getMockBuilder(ContentObjectRenderer::class)->getMock();
286  $cObject->expects($this->once())->method('exec_getQuery')
287  ->with('tt_content', $queryConfiguration)->willReturn($statementProphet->reveal());
288  $this->subject->_set('parent_cObj', $cObject);
289 
290  $this->subject->_call('sectionIndex', 'field', 12);
291  }
292 
294  // Tests concerning menu item states
296 
300  {
301  return [
302  'none excluded' => [
303  [12, 34, 56],
304  '1, 23, 456',
305  true
306  ],
307  'one excluded' => [
308  [1, 234, 567],
309  '1, 23, 456',
310  true
311  ],
312  'three excluded' => [
313  [1, 23, 456],
314  '1, 23, 456',
315  false
316  ],
317  'empty excludeList' => [
318  [1, 123, 45],
319  '',
320  true
321  ],
322  'empty menu' => [
323  [],
324  '1, 23, 456',
325  false
326  ],
327  ];
328  }
329 
337  public function ‪ifsubHasToCheckExcludeUidList($menuItems, $excludeUidList, $expectedResult)
338  {
339  $menu = [];
340  foreach ($menuItems as $page) {
341  $menu[] = ['uid' => $page];
342  }
343  $runtimeCacheMock = $this->getMockBuilder(VariableFrontend::class)->setMethods(['get', 'set'])->disableOriginalConstructor()->getMock();
344  $runtimeCacheMock->expects($this->once())->method('get')->with($this->anything())->willReturn(false);
345  $runtimeCacheMock->expects($this->once())->method('set')->with($this->anything(), ['result' => $expectedResult]);
346 
347  $proxyClassName = $this->buildAccessibleProxy(AbstractMenuContentObject::class);
348  $this->subject = $this->getMockForAbstractClass($proxyClassName, [], '', true, true, true, ['getRuntimeCache']);
349  $this->subject->expects($this->once())->method('getRuntimeCache')->willReturn($runtimeCacheMock);
351 
352  $pageRepository = $this->getMockBuilder(PageRepository::class)->getMock();
353  $pageRepository->expects($this->once())->method('getMenu')->will($this->returnValue($menu));
354  $this->subject->_set('sys_page', $pageRepository);
355  $this->subject->_set('menuArr', [
356  0 => ['uid' => 1]
357  ]);
358  $this->subject->_set('conf', ['excludeUidList' => $excludeUidList]);
359 
360  $this->assertEquals($expectedResult, $this->subject->_call('isItemState', 'IFSUB', 0));
361  }
362 
367  {
368  return [
369  'standard parameter without access protected setting' => [
370  [
371  'parameter' => 1,
372  'linkAccessRestrictedPages' => false,
373  'useCacheHash' => true
374  ],
375  [
376  'showAccessRestrictedPages' => false
377  ],
378  true,
379  ['uid' => 1],
380  '',
381  0,
382  '',
383  '',
384  '',
385  ''
386  ],
387  'standard parameter with access protected setting' => [
388  [
389  'parameter' => 10,
390  'linkAccessRestrictedPages' => true,
391  'useCacheHash' => true
392  ],
393  [
394  'showAccessRestrictedPages' => true
395  ],
396  true,
397  ['uid' => 10],
398  '',
399  0,
400  '',
401  '',
402  '',
403  ''
404  ],
405  'standard parameter with access protected setting "NONE" casts to boolean linkAccessRestrictedPages (delegates resolving to typoLink method internals)' => [
406  [
407  'parameter' => 10,
408  'linkAccessRestrictedPages' => true,
409  'useCacheHash' => true
410  ],
411  [
412  'showAccessRestrictedPages' => 'NONE'
413  ],
414  true,
415  ['uid' => 10],
416  '',
417  0,
418  '',
419  '',
420  '',
421  ''
422  ],
423  'standard parameter with access protected setting (int)67 casts to boolean linkAccessRestrictedPages (delegates resolving to typoLink method internals)' => [
424  [
425  'parameter' => 10,
426  'linkAccessRestrictedPages' => true,
427  'useCacheHash' => true
428  ],
429  [
430  'showAccessRestrictedPages' => 67
431  ],
432  true,
433  ['uid' => 10],
434  '',
435  0,
436  '',
437  '',
438  '',
439  ''
440  ],
441  'standard parameter with target' => [
442  [
443  'parameter' => 1,
444  'target' => '_blank',
445  'linkAccessRestrictedPages' => false,
446  'useCacheHash' => true
447  ],
448  [
449  'showAccessRestrictedPages' => false
450  ],
451  true,
452  ['uid' => 1],
453  '_blank',
454  0,
455  '',
456  '',
457  '',
458  ''
459  ],
460  'parameter with typeOverride=10' => [
461  [
462  'parameter' => '10,10',
463  'linkAccessRestrictedPages' => false,
464  'useCacheHash' => true
465  ],
466  [
467  'showAccessRestrictedPages' => false
468  ],
469  true,
470  ['uid' => 10],
471  '',
472  0,
473  '',
474  '',
475  '',
476  10
477  ],
478  'parameter with target and typeOverride=10' => [
479  [
480  'parameter' => '10,10',
481  'linkAccessRestrictedPages' => false,
482  'useCacheHash' => true,
483  'target' => '_self'
484  ],
485  [
486  'showAccessRestrictedPages' => false
487  ],
488  true,
489  ['uid' => 10],
490  '_self',
491  0,
492  '',
493  '',
494  '',
495  10
496  ],
497  'parameter with invalid value in typeOverride=foobar ignores typeOverride' => [
498  [
499  'parameter' => 20,
500  'linkAccessRestrictedPages' => false,
501  'useCacheHash' => true,
502  'target' => '_self'
503  ],
504  [
505  'showAccessRestrictedPages' => false
506  ],
507  true,
508  ['uid' => 20],
509  '_self',
510  0,
511  '',
512  '',
513  '',
514  'foobar'
515  ],
516  'standard parameter with section name' => [
517  [
518  'parameter' => 10,
519  'target' => '_blank',
520  'linkAccessRestrictedPages' => false,
521  'no_cache' => true,
522  'section' => 'section-name'
523  ],
524  [
525  'showAccessRestrictedPages' => false
526  ],
527  true,
528  [
529  'uid' => 10,
530  'sectionIndex_uid' => 'section-name'
531  ],
532  '_blank',
533  1,
534  '',
535  '',
536  '',
537  ''
538  ],
539  'standard parameter with additional parameters' => [
540  [
541  'parameter' => 10,
542  'linkAccessRestrictedPages' => false,
543  'no_cache' => true,
544  'section' => 'section-name',
545  'additionalParams' => '&test=foobar'
546  ],
547  [
548  'showAccessRestrictedPages' => false
549  ],
550  true,
551  [
552  'uid' => 10,
553  'sectionIndex_uid' => 'section-name'
554  ],
555  '',
556  1,
557  '',
558  '',
559  '&test=foobar',
560  ''
561  ],
562  'overridden page array uid value gets used as parameter' => [
563  [
564  'parameter' => 99,
565  'linkAccessRestrictedPages' => false,
566  'no_cache' => true,
567  'section' => 'section-name'
568  ],
569  [
570  'showAccessRestrictedPages' => false
571  ],
572  true,
573  [
574  'uid' => 10,
575  'sectionIndex_uid' => 'section-name'
576  ],
577  '',
578  1,
579  '',
580  ['uid' => 99],
581  '',
582  ''
583  ],
584  ];
585  }
586 
601  public function ‪menuTypoLinkCreatesExpectedTypoLinkConfiguration(array $expected, array $mconf, $useCacheHash, array $page, $oTarget, $no_cache, $script, $overrideArray = '', $addParams = '', $typeOverride = '')
602  {
603  $cObject = $this->getMockBuilder(ContentObjectRenderer::class)
604  ->setMethods(['typoLink'])
605  ->getMock();
606  $cObject->expects($this->once())->method('typoLink')->with('|', $expected);
607  $this->subject->_set('parent_cObj', $cObject);
608  $this->subject->_set('mconf', $mconf);
609  $this->subject->_set('useCacheHash', $useCacheHash);
610  $this->subject->_call('menuTypoLink', $page, $oTarget, $no_cache, $script, $overrideArray, $addParams, $typeOverride);
611  }
612 }
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder
Definition: ExpressionBuilder.php:33
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\menuTypoLinkCreatesExpectedTypoLinkConfiguration
‪menuTypoLinkCreatesExpectedTypoLinkConfiguration(array $expected, array $mconf, $useCacheHash, array $page, $oTarget, $no_cache, $script, $overrideArray='', $addParams='', $typeOverride='')
Definition: AbstractMenuContentObjectTest.php:600
‪TYPO3\CMS\Core\Context\LanguageAspect\OVERLAYS_MIXED
‪const OVERLAYS_MIXED
Definition: LanguageAspect.php:73
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\sectionIndexReturnsOverlaidRowBasedOnTheLanguageOfTheGivenPage
‪sectionIndexReturnsOverlaidRowBasedOnTheLanguageOfTheGivenPage()
Definition: AbstractMenuContentObjectTest.php:131
‪TYPO3
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\sectionIndexUsesTheInternalIdIfNoPageIdWasGiven
‪sectionIndexUsesTheInternalIdIfNoPageIdWasGiven()
Definition: AbstractMenuContentObjectTest.php:97
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\ifsubHasToCheckExcludeUidList
‪ifsubHasToCheckExcludeUidList($menuItems, $excludeUidList, $expectedResult)
Definition: AbstractMenuContentObjectTest.php:336
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\sectionIndexFiltersDataProvider
‪array sectionIndexFiltersDataProvider()
Definition: AbstractMenuContentObjectTest.php:161
‪TYPO3\CMS\Frontend\ContentObject\Menu\AbstractMenuContentObject
Definition: AbstractMenuContentObject.php:45
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:49
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\menuTypoLinkCreatesExpectedTypoLinkConfiurationDataProvider
‪array menuTypoLinkCreatesExpectedTypoLinkConfiurationDataProvider()
Definition: AbstractMenuContentObjectTest.php:365
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\tearDown
‪tearDown()
Definition: AbstractMenuContentObjectTest.php:58
‪TYPO3\CMS\Frontend\Page\PageRepository
Definition: PageRepository.php:53
‪TYPO3\CMS\Core\Cache\Frontend\VariableFrontend
Definition: VariableFrontend.php:24
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\setUp
‪setUp()
Definition: AbstractMenuContentObjectTest.php:43
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\sectionIndexReturnsEmptyArrayIfTheRequestedPageCouldNotBeFetched
‪sectionIndexReturnsEmptyArrayIfTheRequestedPageCouldNotBeFetched()
Definition: AbstractMenuContentObjectTest.php:84
‪TYPO3\CMS\Core\Context\LanguageAspect
Definition: LanguageAspect.php:55
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest
Definition: AbstractMenuContentObjectTest.php:35
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\sectionIndexQueriesWithDifferentColPos
‪sectionIndexQueriesWithDifferentColPos($configuration, $whereClausePrefix)
Definition: AbstractMenuContentObjectTest.php:265
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:31
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\ifsubHasToCheckExcludeUidListDataProvider
‪array ifsubHasToCheckExcludeUidListDataProvider()
Definition: AbstractMenuContentObjectTest.php:298
‪$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:38
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:91
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\prepareSectionIndexTest
‪prepareSectionIndexTest()
Definition: AbstractMenuContentObjectTest.php:70
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\sectionIndexQueriesWithDifferentColPosDataProvider
‪array sectionIndexQueriesWithDifferentColPosDataProvider()
Definition: AbstractMenuContentObjectTest.php:233
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\sectionIndexThrowsAnExceptionIfTheInternalQueryFails
‪sectionIndexThrowsAnExceptionIfTheInternalQueryFails()
Definition: AbstractMenuContentObjectTest.php:111
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu
Definition: AbstractMenuContentObjectTest.php:3
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\Menu\AbstractMenuContentObjectTest\sectionIndexFilters
‪sectionIndexFilters($expectedAmount, array $dataRow)
Definition: AbstractMenuContentObjectTest.php:205