‪TYPO3CMS  ‪main
PreviewModuleTest.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 PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
31 final class ‪PreviewModuleTest extends UnitTestCase
32 {
33  protected bool ‪$resetSingletonInstances = true;
34 
35  public static function ‪simulateDateDataProvider(): array
36  {
37  return [
38  'timestamp' => [
39  (string)(new \DateTime('2018-01-01 12:00:15 UTC'))->getTimestamp(),
40  (new \DateTime('2018-01-01 12:00:15 UTC'))->getTimestamp(),
41  (new \DateTime('2018-01-01 12:00:00 UTC'))->getTimestamp(),
42  ],
43  'timestamp_1970' => [
44  (string)(new \DateTime('1970-01-01 00:00:15 UTC'))->getTimestamp(),
45  (new \DateTime('1970-01-01 00:00:60 UTC'))->getTimestamp(),
46  (new \DateTime('1970-01-01 00:00:60 UTC'))->getTimestamp(),
47  ],
48  ];
49  }
50 
51  #[DataProvider('simulateDateDataProvider')]
52  #[Test]
53  public function ‪initializeFrontendPreviewSetsDateForSimulation(string $dateToSimulate, int $expectedExecTime, int $expectedAccessTime): void
54  {
55  $configurationService = $this->getMockBuilder(ConfigurationService::class)->disableOriginalConstructor()->getMock();
56  $configurationService->expects(self::once())->method('getMainConfiguration')->willReturn([]);
57  $valueMap = [
58  ['preview', 'showHiddenPages', ''],
59  ['preview', 'simulateDate', $dateToSimulate],
60  ['preview', 'simulateUserGroup', ''],
61  ['preview', 'showScheduledRecords', ''],
62  ['preview', 'showHiddenRecords', ''],
63  ['preview', 'showFluidDebug', ''],
64  ];
65  $configurationService->method('getConfigurationOption')->withAnyParameters()->willReturnMap($valueMap);
66 
67  $previewModule = new ‪PreviewModule($this->createMock(CacheManager::class));
68  $previewModule->injectConfigurationService($configurationService);
69  $previewModule->enrich(new ‪ServerRequest());
70 
71  self::assertSame(‪$GLOBALS['SIM_EXEC_TIME'], $expectedExecTime, 'EXEC_TIME');
72  self::assertSame(‪$GLOBALS['SIM_ACCESS_TIME'], $expectedAccessTime, 'ACCESS_TIME');
73  }
74 
75  #[Test]
77  {
78  $request = (new ‪ServerRequest())->withAttribute('frontend.user', $this->getMockBuilder(FrontendUserAuthentication::class)->getMock());
79 
80  $configurationService = $this->getMockBuilder(ConfigurationService::class)->disableOriginalConstructor()->getMock();
81  $configurationService->expects(self::once())->method('getMainConfiguration')->willReturn([]);
82  $valueMap = [
83  ['preview', 'showHiddenPages', '0'],
84  ['preview', 'simulateDate', '0'],
85  ['preview', 'simulateUserGroup', '1'],
86  ['preview', 'showScheduledRecords', '0'],
87  ['preview', 'showHiddenRecords', '0'],
88  ['preview', 'showFluidDebug', '0'],
89  ];
90  $configurationService->method('getConfigurationOption')->withAnyParameters()->willReturnMap($valueMap);
91 
92  $context = $this->getMockBuilder(Context::class)->getMock();
93  $context->method('hasAspect')->with('frontend.preview')->willReturn(false);
94  $context->expects(self::any())->method('setAspect')
95  ->willReturnCallback(fn(string $name): bool => match (true) {
96  $name === 'date',
97  $name === 'visibility',
98  $name === 'frontend.user',
99  $name === 'frontend.preview' => true,
100  default => throw new \LogicException('Unexpected argument "' . $name . '" provided.', 1679482900),
101  });
102  GeneralUtility::setSingletonInstance(Context::class, $context);
103 
104  $previewModule = new ‪PreviewModule($this->createMock(CacheManager::class));
105  $previewModule->injectConfigurationService($configurationService);
106  $previewModule->enrich($request);
107  }
108 }
‪TYPO3\CMS\Adminpanel\Tests\Unit\Modules\PreviewModuleTest\initializeFrontendPreviewSetsDateForSimulation
‪initializeFrontendPreviewSetsDateForSimulation(string $dateToSimulate, int $expectedExecTime, int $expectedAccessTime)
Definition: PreviewModuleTest.php:53
‪TYPO3\CMS\Adminpanel\Tests\Unit\Modules
Definition: PreviewModuleTest.php:18
‪TYPO3\CMS\Adminpanel\Tests\Unit\Modules\PreviewModuleTest\initializeFrontendPreviewSetsUserGroupForSimulation
‪initializeFrontendPreviewSetsUserGroupForSimulation()
Definition: PreviewModuleTest.php:76
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:54
‪TYPO3\CMS\Adminpanel\Tests\Unit\Modules\PreviewModuleTest
Definition: PreviewModuleTest.php:32
‪TYPO3\CMS\Adminpanel\Tests\Unit\Modules\PreviewModuleTest\simulateDateDataProvider
‪static simulateDateDataProvider()
Definition: PreviewModuleTest.php:35
‪TYPO3\CMS\Adminpanel\Service\ConfigurationService
Definition: ConfigurationService.php:34
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:36
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Adminpanel\Tests\Unit\Modules\PreviewModuleTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: PreviewModuleTest.php:33
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Adminpanel\Modules\PreviewModule
Definition: PreviewModule.php:46
‪TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication
Definition: FrontendUserAuthentication.php:33
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52