[TASK] Streamline $TSFE->simUserGroup usages 42/58342/3
authorBenni Mack <benni@typo3.org>
Thu, 20 Sep 2018 06:27:14 +0000 (08:27 +0200)
committerBenni Mack <benni@typo3.org>
Thu, 20 Sep 2018 09:32:02 +0000 (11:32 +0200)
First, this patch streamlines some naming issue in $TSFE->applyPreviewSettings()
(introduced by myself).

On top, it sorts out the usages of $TSFE->simUserGroup within
EXT:adminpanel.

So far this patch is "just" cosmetics but allows to further
sort out "simUserGroup" in TYPO3 v10.0.

Resolves: #86322
Releases: master
Change-Id: I5fabb90f36b83e206b390e5c7b1a7191461f85fe
Reviewed-on: https://review.typo3.org/58342
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Tobi Kretschmann <tobi@tobishome.de>
Tested-by: Tobi Kretschmann <tobi@tobishome.de>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
typo3/sysext/adminpanel/Classes/Modules/PreviewModule.php
typo3/sysext/adminpanel/Tests/Unit/Modules/PreviewModuleTest.php
typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php

index 6e1562f..cd0125e 100644 (file)
@@ -80,7 +80,7 @@ class PreviewModule extends AbstractModule implements InitializableInterface, Pa
             'showHiddenPages' => (bool)$this->getConfigOptionForModule('showHiddenPages'),
             'simulateDate' => $this->getConfigOptionForModule('simulateDate'),
             'showHiddenRecords' => (bool)$this->getConfigOptionForModule('showHiddenRecords'),
-            'simulateUserGroup' => $this->getConfigOptionForModule('simulateUserGroup'),
+            'simulateUserGroup' => (int)$this->getConfigOptionForModule('simulateUserGroup'),
             'showFluidDebug' => (bool)$this->getConfigOptionForModule('showFluidDebug'),
         ];
         $this->initializeFrontendPreview(
@@ -166,44 +166,45 @@ class PreviewModule extends AbstractModule implements InitializableInterface, Pa
      * @param bool $showHiddenPages
      * @param bool $showHiddenRecords
      * @param string $simulateDate
-     * @param string $simulateUserGroup
+     * @param int $simulateUserGroup UID of the fe_group to simulate
      */
     protected function initializeFrontendPreview(
         bool $showHiddenPages,
         bool $showHiddenRecords,
         string $simulateDate,
-        string $simulateUserGroup
+        int $simulateUserGroup
     ): void {
         $context = GeneralUtility::makeInstance(Context::class);
         $typoScriptFrontendController = $this->getTypoScriptFrontendController();
         $typoScriptFrontendController->clear_preview();
         $typoScriptFrontendController->fePreview = 1;
 
+        // Modify visibility settings (hidden pages + hidden content)
+        $context->setAspect('visibility', GeneralUtility::makeInstance(VisibilityAspect::class, $showHiddenPages, $showHiddenRecords));
+
         // Simulate date
         $simTime = null;
         if ($simulateDate) {
             $simTime = $this->parseDate($simulateDate);
+            if ($simTime) {
+                $GLOBALS['SIM_EXEC_TIME'] = $simTime;
+                $GLOBALS['SIM_ACCESS_TIME'] = $simTime - $simTime % 60;
+                $context->setAspect('date', GeneralUtility::makeInstance(DateTimeAspect::class, new \DateTimeImmutable('@' . $GLOBALS['SIM_EXEC_TIME'])));
+            }
         }
-        if ($simTime) {
-            $GLOBALS['SIM_EXEC_TIME'] = $simTime;
-            $GLOBALS['SIM_ACCESS_TIME'] = $simTime - $simTime % 60;
-            $context->setAspect('date', GeneralUtility::makeInstance(DateTimeAspect::class, new \DateTimeImmutable('@' . $GLOBALS['SIM_EXEC_TIME'])));
-        }
-        $context->setAspect('visibility', GeneralUtility::makeInstance(VisibilityAspect::class, $showHiddenPages, $showHiddenRecords));
-        // simulate user
-        $typoScriptFrontendController->simUserGroup = (int)$simulateUserGroup;
-        if ($typoScriptFrontendController->simUserGroup) {
-            if ($typoScriptFrontendController->fe_user->user) {
-                $typoScriptFrontendController->fe_user->user[$typoScriptFrontendController->fe_user->usergroup_column] = $typoScriptFrontendController->simUserGroup;
-            } else {
+        // simulate usergroup
+        if ($simulateUserGroup) {
+            $typoScriptFrontendController->simUserGroup = $simulateUserGroup;
+            if (!$typoScriptFrontendController->fe_user instanceof FrontendUserAuthentication) {
                 $typoScriptFrontendController->fe_user = GeneralUtility::makeInstance(FrontendUserAuthentication::class);
-                $typoScriptFrontendController->fe_user->user = [
-                    $typoScriptFrontendController->fe_user->usergroup_column => $typoScriptFrontendController->simUserGroup,
-                ];
             }
-            $context->setAspect('frontend.user', GeneralUtility::makeInstance(UserAspect::class, $typoScriptFrontendController->fe_user ?: null));
+            if (!is_array($typoScriptFrontendController->fe_user->user)) {
+                $typoScriptFrontendController->fe_user->user = [];
+            }
+            $typoScriptFrontendController->fe_user->user[$typoScriptFrontendController->fe_user->usergroup_column] = $simulateUserGroup;
+            $context->setAspect('frontend.user', GeneralUtility::makeInstance(UserAspect::class, $typoScriptFrontendController->fe_user ?: null, [$simulateUserGroup]));
         }
-        if (!$typoScriptFrontendController->simUserGroup && !$simTime && !$showHiddenPages && !$showHiddenRecords) {
+        if (!$simulateUserGroup && !$simTime && !$showHiddenPages && !$showHiddenRecords) {
             $typoScriptFrontendController->fePreview = 0;
         }
     }
index 012c607..ca87cbc 100644 (file)
@@ -7,7 +7,9 @@ use Prophecy\Argument;
 use Psr\Http\Message\ServerRequestInterface;
 use TYPO3\CMS\Adminpanel\Modules\PreviewModule;
 use TYPO3\CMS\Adminpanel\Service\ConfigurationService;
+use TYPO3\CMS\Core\Context\Context;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
 
@@ -56,4 +58,33 @@ class PreviewModuleTest extends UnitTestCase
         self::assertSame($GLOBALS['SIM_EXEC_TIME'], $expectedExecTime, 'EXEC_TIME');
         self::assertSame($GLOBALS['SIM_ACCESS_TIME'], $expectedAccessTime, 'ACCESS_TIME');
     }
+
+    /**
+     * @test
+     */
+    public function initializeFrontendPreviewSetsUsergroupForSimulation(): void
+    {
+        $this->resetSingletonInstances = true;
+        $request = $this->prophesize(ServerRequestInterface::class);
+        $tsfe = $this->prophesize(TypoScriptFrontendController::class);
+        $feUser = $this->prophesize(FrontendUserAuthentication::class);
+        $tsfe->fe_user = $feUser->reveal();
+        $GLOBALS['TSFE'] = $tsfe->reveal();
+        $configurationService = $this->prophesize(ConfigurationService::class);
+        $configurationService->getMainConfiguration()->willReturn([]);
+        $configurationService->getConfigurationOption('preview', 'showHiddenPages')->willReturn('0');
+        $configurationService->getConfigurationOption('preview', 'simulateDate')->willReturn('0');
+        $configurationService->getConfigurationOption('preview', 'showHiddenRecords')->willReturn('0');
+        $configurationService->getConfigurationOption('preview', 'showFluidDebug')->willReturn('0');
+        $configurationService->getConfigurationOption('preview', 'simulateUserGroup')->willReturn('1');
+        $context = $this->prophesize(Context::class);
+        GeneralUtility::setSingletonInstance(Context::class, $context->reveal());
+
+        GeneralUtility::setSingletonInstance(ConfigurationService::class, $configurationService->reveal());
+
+        $previewModule = new PreviewModule();
+        $previewModule->initializeModule($request->reveal());
+
+        $context->setAspect('frontend.user', Argument::any())->shouldHaveBeenCalled();
+    }
 }
index 6c00bfe..6618114 100644 (file)
@@ -315,6 +315,7 @@ class TypoScriptFrontendController implements LoggerAwareInterface
     /**
      * Value that contains the simulated usergroup if any
      * @var int
+     * @internal only to be used in AdminPanel, and within TYPO3 Core
      */
     public $simUserGroup = 0;
 
@@ -1238,9 +1239,9 @@ class TypoScriptFrontendController implements LoggerAwareInterface
         if (!$backendUser) {
             return null;
         }
-        $originalFrontendUser = null;
+        $originalFrontendUserGroup = null;
         if ($this->fe_user->user) {
-            $originalFrontendUser = $this->fe_user->user[$this->fe_user->usergroup_column];
+            $originalFrontendUserGroup = $this->fe_user->user[$this->fe_user->usergroup_column];
         }
 
         // The preview flag is set if the current page turns out to be hidden
@@ -1255,7 +1256,7 @@ class TypoScriptFrontendController implements LoggerAwareInterface
         if ($this->whichWorkspace() > 0) {
             $this->fePreview = 1;
         }
-        return $this->simUserGroup ? $originalFrontendUser : null;
+        return $this->simUserGroup ? $originalFrontendUserGroup : null;
     }
 
     /**