From a8b9152f944511d36d94d0d15f5c90d09850185f Mon Sep 17 00:00:00 2001
From: Markus Klein <markus.klein@typo3.org>
Date: Wed, 11 Jan 2023 15:42:00 +0100
Subject: [PATCH] [BUGFIX] Ensure that formatValue for dates/times returns a
 string

The unit tests are adjusted to have strict type checking.

The bug is actually covered with a test already, only the assertion
was wrong.

Resolves: #99521
Releases: main, 11.5
Change-Id: I52a1aa1a969bcc9d32df4ab6f43726bef671fe66
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/77353
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Oliver Bartsch <bo@cedev.de>
Tested-by: Oliver Klee <typo3-coding@oliverklee.de>
Tested-by: core-ci <typo3@b13.com>
Reviewed-by: Oliver Bartsch <bo@cedev.de>
---
 .../backend/Classes/Form/Element/AbstractFormElement.php     | 5 +++--
 .../Tests/Unit/Form/Element/AbstractFormElementTest.php      | 2 +-
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/typo3/sysext/backend/Classes/Form/Element/AbstractFormElement.php b/typo3/sysext/backend/Classes/Form/Element/AbstractFormElement.php
index 239ab2f59f24..8dc920906031 100644
--- a/typo3/sysext/backend/Classes/Form/Element/AbstractFormElement.php
+++ b/typo3/sysext/backend/Classes/Form/Element/AbstractFormElement.php
@@ -187,7 +187,7 @@ abstract class AbstractFormElement extends AbstractNode
      * @param array $formatOptions Format options
      * @return string Formatted field value
      */
-    protected function formatValue($format, $itemValue, $formatOptions = [])
+    protected function formatValue($format, $itemValue, $formatOptions = []): string
     {
         switch ($format) {
             case 'date':
@@ -287,7 +287,8 @@ abstract class AbstractFormElement extends AbstractNode
             default:
                 // Do nothing e.g. when $format === ''
         }
-        return $itemValue;
+        // Make sure we have a string in the end. $itemValue could be null, for instance.
+        return (string)$itemValue;
     }
 
     /**
diff --git a/typo3/sysext/backend/Tests/Unit/Form/Element/AbstractFormElementTest.php b/typo3/sysext/backend/Tests/Unit/Form/Element/AbstractFormElementTest.php
index 91bc8d2b678d..ec36959c6575 100644
--- a/typo3/sysext/backend/Tests/Unit/Form/Element/AbstractFormElementTest.php
+++ b/typo3/sysext/backend/Tests/Unit/Form/Element/AbstractFormElementTest.php
@@ -276,6 +276,6 @@ class AbstractFormElementTest extends UnitTestCase
         $result = $subject->_call('formatValue', $config['format'], $itemValue, $config['format.'] ?? []);
         date_default_timezone_set($timezoneBackup);
 
-        self::assertEquals($expectedResult, $result);
+        self::assertSame($expectedResult, $result);
     }
 }
-- 
GitLab