From b72cc6c615e33a1895409bea6bbd9a39a9448b95 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/+/80771
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Oliver Klee <typo3-coding@oliverklee.de>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: core-ci <typo3@b13.com>
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
---
 .../Classes/Utility/BackendUtility.php        |  4 +-
 .../Tests/Unit/Utility/BackendUtilityTest.php | 99 +++++++++++++++++++
 2 files changed, 101 insertions(+), 2 deletions(-)

diff --git a/typo3/sysext/backend/Classes/Utility/BackendUtility.php b/typo3/sysext/backend/Classes/Utility/BackendUtility.php
index 6aaee65b5764..1873c81bbb95 100644
--- a/typo3/sysext/backend/Classes/Utility/BackendUtility.php
+++ b/typo3/sysext/backend/Classes/Utility/BackendUtility.php
@@ -1688,7 +1688,7 @@ class BackendUtility
                 if ($format === 'date') {
                     // Handle native date field
                     if (($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;
                     }
@@ -1728,7 +1728,7 @@ class BackendUtility
                 } elseif ($format === 'datetime') {
                     // Handle native datetime field
                     if (($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 71370e92927d..0fa33f482773 100644
--- a/typo3/sysext/backend/Tests/Unit/Utility/BackendUtilityTest.php
+++ b/typo3/sysext/backend/Tests/Unit/Utility/BackendUtilityTest.php
@@ -188,6 +188,105 @@ final 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 getProcessedValueForDatetimeDbTypeDatetime(): void
+    {
+        $GLOBALS['TCA'] = [
+            'tt_content' => [
+                'columns' => [
+                    'header' => [
+                        'config' => [
+                            'type' => 'datetime',
+                            'dbType' => 'datetime',
+                        ],
+                    ],
+                ],
+            ],
+        ];
+        $value = '2022-09-23 00:03:00';
+        $expected = BackendUtility::datetime((int)strtotime($value));
+        $languageServiceMock = $this->createMock(LanguageService::class);
+        $languageServiceMock->method('sL')->willReturn('testLabel');
+        $GLOBALS['LANG'] = $languageServiceMock;
+        self::assertSame($expected, BackendUtility::getProcessedValue('tt_content', 'header', $value));
+    }
+
+    /**
+     * @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
+     */
+    public function getProcessedValueForDatetimeDbTypeDate(): void
+    {
+        $GLOBALS['TCA'] = [
+            'tt_content' => [
+                'columns' => [
+                    'header' => [
+                        'config' => [
+                            'type' => 'datetime',
+                            'format' => 'date',
+                            'dbType' => 'date',
+                            'disableAgeDisplay' => true,
+                        ],
+                    ],
+                ],
+            ],
+        ];
+        $value = '2022-09-23';
+        $expected = BackendUtility::date((int)strtotime($value));
+        $languageServiceMock = $this->createMock(LanguageService::class);
+        $languageServiceMock->method('sL')->willReturn('testLabel');
+        $GLOBALS['LANG'] = $languageServiceMock;
+        self::assertSame($expected, BackendUtility::getProcessedValue('tt_content', 'header', $value));
+    }
+
     /**
      * @test
      */
-- 
GitLab