From da471b0e88db4dee1af78767bc533274f0fd10ac Mon Sep 17 00:00:00 2001
From: Achim Fritz <achim.fritz@b13.de>
Date: Thu, 31 Aug 2023 14:13:11 +0200
Subject: [PATCH] [BUGFIX] Avoid strtotime(null) in BU::getProcessedValue()

BackendUtility has a couple of strtotime() calls that
may end up as strtotime(null). This emits a PHP
E_DEPRECATED error since PHP 8.1. Add a cast.

Resolves: #101811
Resolves: #101805
Releases: main, 12.4, 11.5
Change-Id: I5cca1b2fd3bc0d6792f6d61a59bdbc5b4850d15d
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80796
Tested-by: core-ci <typo3@b13.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
Tested-by: Oliver Klee <typo3-coding@oliverklee.de>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
---
 .../Classes/Utility/BackendUtility.php        |  4 +-
 .../Tests/Unit/Utility/BackendUtilityTest.php | 47 +++++++++++++++++++
 2 files changed, 49 insertions(+), 2 deletions(-)

diff --git a/typo3/sysext/backend/Classes/Utility/BackendUtility.php b/typo3/sysext/backend/Classes/Utility/BackendUtility.php
index 61cf5906ed05..3b03527e41af 100644
--- a/typo3/sysext/backend/Classes/Utility/BackendUtility.php
+++ b/typo3/sysext/backend/Classes/Utility/BackendUtility.php
@@ -1669,7 +1669,7 @@ class BackendUtility
                     if (GeneralUtility::inList($theColConf['eval'] ?? '', 'date')) {
                         // Handle native date field
                         if (isset($theColConf['dbType']) && $theColConf['dbType'] === 'date') {
-                            $value = $value === $dateTimeFormats['date']['empty'] ? 0 : (int)strtotime($value);
+                            $value = $value === $dateTimeFormats['date']['empty'] ? 0 : (int)strtotime((string)$value);
                         } else {
                             $value = (int)$value;
                         }
@@ -1716,7 +1716,7 @@ class BackendUtility
                     } elseif (GeneralUtility::inList($theColConf['eval'] ?? '', 'datetime')) {
                         // Handle native datetime field
                         if (isset($theColConf['dbType']) && $theColConf['dbType'] === 'datetime') {
-                            $value = $value === $dateTimeFormats['datetime']['empty'] ? 0 : (int)strtotime($value);
+                            $value = $value === $dateTimeFormats['datetime']['empty'] ? 0 : (int)strtotime((string)$value);
                         } else {
                             $value = (int)$value;
                         }
diff --git a/typo3/sysext/backend/Tests/Unit/Utility/BackendUtilityTest.php b/typo3/sysext/backend/Tests/Unit/Utility/BackendUtilityTest.php
index 804b683cdb7c..e47b8236d061 100644
--- a/typo3/sysext/backend/Tests/Unit/Utility/BackendUtilityTest.php
+++ b/typo3/sysext/backend/Tests/Unit/Utility/BackendUtilityTest.php
@@ -202,6 +202,53 @@ class BackendUtilityTest extends UnitTestCase
         self::assertSame('', BackendUtility::getProcessedValue('tt_content', 'pi_flexform', null));
     }
 
+    /**
+     * @test
+     */
+    public function getProcessedValueForDatetimeDbTypeDateNull(): void
+    {
+        $GLOBALS['TCA'] = [
+            'tt_content' => [
+                'columns' => [
+                    'header' => [
+                        'config' => [
+                            'type' => 'datetime',
+                            'dbType' => 'date',
+                            'format' => 'date',
+                        ],
+                    ],
+                ],
+            ],
+        ];
+        $languageServiceMock = $this->createMock(LanguageService::class);
+        $languageServiceMock->method('sL')->willReturn('testLabel');
+        $GLOBALS['LANG'] = $languageServiceMock;
+        self::assertSame('', BackendUtility::getProcessedValue('tt_content', 'header', null));
+    }
+
+    /**
+     * @test
+     */
+    public function getProcessedValueForDatetimeDbTypeDatetimeNull(): void
+    {
+        $GLOBALS['TCA'] = [
+            'tt_content' => [
+                'columns' => [
+                    'header' => [
+                        'config' => [
+                            'type' => 'datetime',
+                            'dbType' => 'datetime',
+                        ],
+                    ],
+                ],
+            ],
+        ];
+        $languageServiceMock = $this->createMock(LanguageService::class);
+        $languageServiceMock->method('sL')->willReturn('testLabel');
+        $GLOBALS['LANG'] = $languageServiceMock;
+        self::assertSame('', BackendUtility::getProcessedValue('tt_content', 'header', null));
+    }
+
     /**
      * @test
      */
-- 
GitLab