[BUGFIX] Avoid PHP warnings in BackendUtility if no BE user is set 52/58752/5
authorAnja <aleichsenring@ab-softlab.de>
Sun, 28 Oct 2018 10:44:55 +0000 (11:44 +0100)
committerBenni Mack <benni@typo3.org>
Sun, 28 Oct 2018 16:06:13 +0000 (17:06 +0100)
BackendUtility no longer tries to resolve the workspace property
of a not set BackendUserAuthentication. This prevents warnings from
being thrown.

Change-Id: I30d739273a0107e70987beadd3649d8b6bb67f60
Resolves: #86617
Releases: master
Reviewed-on: https://review.typo3.org/58752
Reviewed-by: Daniel Goerz <daniel.goerz@posteo.de>
Tested-by: Daniel Goerz <daniel.goerz@posteo.de>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
typo3/sysext/backend/Classes/Utility/BackendUtility.php
typo3/sysext/backend/Tests/Unit/Utility/BackendUtilityTest.php

index 40fa487..eeeee6b 100644 (file)
@@ -3993,7 +3993,7 @@ class BackendUtility
             }
             // If ID of current online version is found, look up the PID value of that:
             if ($oid
-                && ($ignoreWorkspaceMatch || (int)$wsid === (int)static::getBackendUserAuthentication()->workspace)
+                && ($ignoreWorkspaceMatch || (static::getBackendUserAuthentication() instanceof BackendUserAuthentication && (int)$wsid === (int)static::getBackendUserAuthentication()->workspace))
             ) {
                 $oidRec = self::getRecord($table, $oid, 'pid');
                 if (is_array($oidRec)) {
@@ -4040,7 +4040,7 @@ class BackendUtility
         // I don't know if this move can be useful for users to toggle. Technically it can help debugging.
         $previewMovePlaceholders = true;
         // Initialize workspace ID
-        if ($wsid == -99) {
+        if ($wsid == -99 && static::getBackendUserAuthentication() instanceof BackendUserAuthentication) {
             $wsid = static::getBackendUserAuthentication()->workspace;
         }
         // Check if workspace is different from zero and record is set:
@@ -4236,7 +4236,7 @@ class BackendUtility
      */
     public static function versioningPlaceholderClause($table)
     {
-        if (static::isTableWorkspaceEnabled($table)) {
+        if (static::isTableWorkspaceEnabled($table) && static::getBackendUserAuthentication() instanceof BackendUserAuthentication) {
             $currentWorkspace = (int)static::getBackendUserAuthentication()->workspace;
             return ' AND (' . $table . '.t3ver_state <= ' . new VersionState(VersionState::DEFAULT_STATE) . ' OR ' . $table . '.t3ver_wsid = ' . $currentWorkspace . ')';
         }
@@ -4253,7 +4253,7 @@ class BackendUtility
     public static function getWorkspaceWhereClause($table, $workspaceId = null)
     {
         $whereClause = '';
-        if (self::isTableWorkspaceEnabled($table)) {
+        if (self::isTableWorkspaceEnabled($table) && static::getBackendUserAuthentication() instanceof BackendUserAuthentication) {
             if ($workspaceId === null) {
                 $workspaceId = static::getBackendUserAuthentication()->workspace;
             }
@@ -4273,12 +4273,15 @@ class BackendUtility
      */
     public static function wsMapId($table, $uid)
     {
-        $wsRec = self::getWorkspaceVersionOfRecord(
-            static::getBackendUserAuthentication()->workspace,
-            $table,
-            $uid,
-            'uid'
-        );
+        $wsRec = null;
+        if (static::getBackendUserAuthentication() instanceof BackendUserAuthentication) {
+            $wsRec = self::getWorkspaceVersionOfRecord(
+                static::getBackendUserAuthentication()->workspace,
+                $table,
+                $uid,
+                'uid'
+            );
+        }
         return is_array($wsRec) ? $wsRec['uid'] : $uid;
     }
 
@@ -4293,7 +4296,7 @@ class BackendUtility
      */
     public static function getMovePlaceholder($table, $uid, $fields = '*', $workspace = null)
     {
-        if ($workspace === null) {
+        if ($workspace === null && static::getBackendUserAuthentication() instanceof BackendUserAuthentication) {
             $workspace = static::getBackendUserAuthentication()->workspace;
         }
         if ((int)$workspace !== 0 && $GLOBALS['TCA'][$table] && static::isTableWorkspaceEnabled($table)) {
index 375ee3c..e3e34cc 100644 (file)
@@ -1066,6 +1066,24 @@ class BackendUtilityTest extends UnitTestCase
 
     /**
      * @test
+     */
+    public function fixVersioningPidDoesNotChangeValuesForNoBeUserAvailable()
+    {
+        $GLOBALS['BE_USER'] = null;
+        $tableName = 'table_a';
+        $GLOBALS['TCA'][$tableName]['ctrl']['versioningWS'] = 'not_empty';
+        $rr = [
+            'pid' => -1,
+            't3ver_oid' => 7,
+            't3ver_wsid' => 42,
+        ];
+        $reference = $rr;
+        BackendUtility::fixVersioningPid($tableName, $rr);
+        $this->assertSame($reference, $rr);
+    }
+
+    /**
+     * @test
      * @dataProvider unfitResolveFileReferencesTableConfig
      */
     public function returnNullForUnfitTableConfigInResolveFileReferences(array $config)
@@ -1115,6 +1133,33 @@ class BackendUtilityTest extends UnitTestCase
     /**
      * @test
      */
+    public function workspaceOLDoesNotChangeValuesForNoBeUserAvailable()
+    {
+        $GLOBALS['BE_USER'] = null;
+        $tableName = 'table_a';
+        $row = [
+            'uid' => 1,
+            'pid' => 17,
+        ];
+        $reference = $row;
+        BackendUtility::workspaceOL($tableName, $row);
+        $this->assertSame($reference, $row);
+    }
+
+    /**
+     * @test
+     */
+    public function versioningPlaceholderClauseReturnsEmptyIfNoBeUserIsAvailable()
+    {
+        $GLOBALS['BE_USER'] = null;
+        $tableName = 'table_a';
+        $GLOBALS['TCA'][$tableName]['ctrl']['versioningWS'] = 'not_empty';
+        $this->assertSame('', BackendUtility::versioningPlaceholderClause($tableName));
+    }
+
+    /**
+     * @test
+     */
     public function resolveFileReferencesReturnsEmptyResultForNoReferencesAvailable()
     {
         $tableName = 'table_a';
@@ -1142,4 +1187,36 @@ class BackendUtilityTest extends UnitTestCase
 
         $this->assertEmpty(BackendUtility::resolveFileReferences($tableName, $fieldName, $elementData));
     }
+
+    /**
+     * @test
+     */
+    public function getWorkspaceWhereClauseReturnsEmptyIfNoBeUserIsAvailable()
+    {
+        $GLOBALS['BE_USER'] = null;
+        $tableName = 'table_a';
+        $GLOBALS['TCA'][$tableName]['ctrl']['versioningWS'] = 'not_empty';
+        $this->assertSame('', BackendUtility::getWorkspaceWhereClause($tableName));
+    }
+
+    /**
+     * @test
+     */
+    public function wsMapIdReturnsLiveIdIfNoBeUserIsAvailable()
+    {
+        $GLOBALS['BE_USER'] = null;
+        $tableName = 'table_a';
+        $uid = 42;
+        $this->assertSame(42, BackendUtility::wsMapId($tableName, $uid));
+    }
+
+    /**
+     * @test
+     */
+    public function getMovePlaceholderReturnsFalseIfNoBeUserIsAvailable()
+    {
+        $GLOBALS['BE_USER'] = null;
+        $tableName = 'table_a';
+        $this->assertFalse(BackendUtility::getMovePlaceholder($tableName, 42));
+    }
 }