[BUGFIX] Allow getPagesTSconfig to be called without BE_USER 74/58174/2
authorBenni Mack <benni@typo3.org>
Tue, 4 Sep 2018 07:08:19 +0000 (09:08 +0200)
committerMarkus Klein <markus.klein@typo3.org>
Tue, 4 Sep 2018 08:03:27 +0000 (10:03 +0200)
Due to refactorings lately in BackendUtility::getPagesTSconfig() with
which results in a fatal error when clearing page cache while persisting an
extbase object.

The patch adds a test without a Backend User, and encapsulates this
with a simple if() statement. However, this also shows that some
other areas that are touched are not fully notice-free / strict, which
are tackled as well.

Resolves: #85310
Releases: master
Change-Id: I0f1318d395451e704e1783e841d96583efe2a560
Reviewed-on: https://review.typo3.org/58174
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Reviewed-by: Daniel Goerz <daniel.goerz@posteo.de>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Markus Klein <markus.klein@typo3.org>
typo3/sysext/backend/Classes/Utility/BackendUtility.php
typo3/sysext/backend/Tests/Unit/Utility/BackendUtilityTest.php
typo3/sysext/core/Classes/Context/UserAspect.php

index 79d027f..c5d830b 100644 (file)
@@ -878,18 +878,21 @@ class BackendUtility
             $tsConfig = $res['TSconfig'];
         }
         $cacheHash = $res['hash'];
-        // Get User TSconfig overlay
-
-        $userTSconfig = static::getBackendUserAuthentication()->getTSConfig() ?? [];
+        // Get User TSconfig overlay, if no backend user is logged-in, this needs to be checked as well
+        if (static::getBackendUserAuthentication()) {
+            $userTSconfig = static::getBackendUserAuthentication()->getTSConfig() ?? [];
+        } else {
+            $userTSconfig = [];
+        }
         $isCacheHashExtendedWithUserUid = false;
-        if (is_array($userTSconfig['page.'])) {
+        if (is_array($userTSconfig['page.'] ?? null)) {
             ArrayUtility::mergeRecursiveWithOverrule($tsConfig, $userTSconfig['page.']);
             $isCacheHashExtendedWithUserUid = true;
             $cacheHash .= '_user' . static::getBackendUserAuthentication()->user['uid'];
         }
 
         // Overlay page "mod." ts with user ts in a special and deprecated way
-        if (is_array($userTSconfig['mod.'])) {
+        if (is_array($userTSconfig['mod.'] ?? null)) {
             // @deprecated This entire "if" and variable $isCacheHashExtendedWithUserUid can be deleted in v10
             trigger_error(
                 'Overriding page TSconfig "mod." with user TSconfig "mod." is deprecated. Use user TSconfig "page.mod." instead',
@@ -4565,6 +4568,6 @@ class BackendUtility
      */
     protected static function getBackendUserAuthentication()
     {
-        return $GLOBALS['BE_USER'];
+        return $GLOBALS['BE_USER'] ?? null;
     }
 }
index 1d9ff58..1f54603 100644 (file)
@@ -16,11 +16,14 @@ namespace TYPO3\CMS\Backend\Tests\Unit\Utility;
 
 use Prophecy\Argument;
 use Prophecy\Prophecy\ObjectProphecy;
+use TYPO3\CMS\Backend\Configuration\TsConfigParser;
 use TYPO3\CMS\Backend\Tests\Unit\Utility\Fixtures\LabelFromItemListMergedReturnsCorrectFieldsFixture;
 use TYPO3\CMS\Backend\Tests\Unit\Utility\Fixtures\ProcessedValueForGroupWithMultipleAllowedTablesFixture;
 use TYPO3\CMS\Backend\Tests\Unit\Utility\Fixtures\ProcessedValueForGroupWithOneAllowedTableFixture;
 use TYPO3\CMS\Backend\Tests\Unit\Utility\Fixtures\ProcessedValueForSelectWithMMRelationFixture;
 use TYPO3\CMS\Backend\Utility\BackendUtility;
+use TYPO3\CMS\Core\Cache\CacheManager;
+use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
 use TYPO3\CMS\Core\Database\Connection;
 use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
@@ -35,6 +38,11 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
  */
 class BackendUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
 {
+    /**
+     * @var bool
+     */
+    protected $resetSingletonInstances = true;
+
     ///////////////////////////////////////
     // Tests concerning calcAge
     ///////////////////////////////////////
@@ -955,4 +963,33 @@ class BackendUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
         $result = BackendUtility::splitTable_Uid($input);
         self::assertSame($expected, $result);
     }
+
+    /**
+     * Tests if the method getPagesTSconfig can be called without having a GLOBAL['BE_USER'] object.
+     * However, this test also shows all the various other dependencies this method has.
+     *
+     * @test
+     */
+    public function getPagesTSconfigWorksWithoutInitializedBackendUser()
+    {
+        $expected = ['called.' => ['config']];
+        $pageId = 13;
+        $parserProphecy = $this->prophesize(TsConfigParser::class);
+        $parserProphecy->parseTSconfig(Argument::cetera())->willReturn(['hash' => $pageId, 'TSconfig' => $expected]);
+        GeneralUtility::addInstance(TsConfigParser::class, $parserProphecy->reveal());
+
+        $cacheManagerProphecy = $this->prophesize(CacheManager::class);
+        $cacheProphecy = $this->prophesize(FrontendInterface::class);
+        $cacheManagerProphecy->getCache('cache_runtime')->willReturn($cacheProphecy->reveal());
+        $cacheHashProphecy = $this->prophesize(FrontendInterface::class);
+        $cacheManagerProphecy->hasCache('extbase_reflection')->willReturn(false);
+        $cacheManagerProphecy->getCache('cache_hash')->willReturn($cacheHashProphecy->reveal());
+        $cacheProphecy->has(Argument::cetera())->willReturn(false);
+        $cacheProphecy->get(Argument::cetera())->willReturn(false);
+        $cacheProphecy->set(Argument::cetera())->willReturn(false);
+        $cacheProphecy->get('backendUtilityBeGetRootLine')->willReturn(['13--1' => []]);
+        GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal());
+        $result = BackendUtility::getPagesTSconfig($pageId);
+        $this->assertEquals($expected, $result);
+    }
 }
index cb9119a..07230bf 100644 (file)
@@ -103,7 +103,11 @@ class UserAspect implements AspectInterface
      */
     public function isAdmin(): bool
     {
-        return $this->user->user['admin'] === 1 ?? false;
+        $isAdmin = false;
+        if ($this->user instanceof BackendUserAuthentication) {
+            $isAdmin = $this->user->isAdmin();
+        }
+        return $isAdmin;
     }
 
     /**