From 439e6c29c81c2e864aa3c3e7efc529ccc1b1b45b Mon Sep 17 00:00:00 2001
From: Markus Klein <markus.klein@typo3.org>
Date: Wed, 11 Jan 2023 15:52:28 +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/+/77354
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: core-ci <typo3@b13.com>
Tested-by: Oliver Bartsch <bo@cedev.de>
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
Tested-by: Oliver Klee <typo3-coding@oliverklee.de>
Reviewed-by: Oliver Bartsch <bo@cedev.de>
---
 .../backend/Classes/Form/Element/AbstractFormElement.php       | 3 ++-
 .../backend/Classes/Form/Element/InputDateTimeElement.php      | 2 +-
 .../Tests/Unit/Form/Element/AbstractFormElementTest.php        | 2 +-
 3 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 e48be6b947a0..27866c5276bb 100644
--- a/typo3/sysext/backend/Classes/Form/Element/AbstractFormElement.php
+++ b/typo3/sysext/backend/Classes/Form/Element/AbstractFormElement.php
@@ -296,7 +296,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/Classes/Form/Element/InputDateTimeElement.php b/typo3/sysext/backend/Classes/Form/Element/InputDateTimeElement.php
index 19a17976d412..46d19a4303d9 100644
--- a/typo3/sysext/backend/Classes/Form/Element/InputDateTimeElement.php
+++ b/typo3/sysext/backend/Classes/Form/Element/InputDateTimeElement.php
@@ -198,7 +198,7 @@ class InputDateTimeElement extends AbstractFormElement
         $expansionHtml[] =      '<div class="form-wizards-element">';
         $expansionHtml[] =          '<div class="input-group">';
         $expansionHtml[] =              '<input type="text" ' . GeneralUtility::implodeAttributes($attributes, true) . ' />';
-        $expansionHtml[] =              '<input type="hidden" name="' . $parameterArray['itemFormElName'] . '" value="' . htmlspecialchars($itemValue) . '" />';
+        $expansionHtml[] =              '<input type="hidden" name="' . $parameterArray['itemFormElName'] . '" value="' . htmlspecialchars((string)$itemValue) . '" />';
         $expansionHtml[] =              '<button class="btn btn-default" type="button" data-global-event="click" data-action-focus="#' . $attributes['id'] . '">';
         $expansionHtml[] =                  $this->iconFactory->getIcon('actions-edit-pick-date', Icon::SIZE_SMALL)->render();
         $expansionHtml[] =              '</button>';
diff --git a/typo3/sysext/backend/Tests/Unit/Form/Element/AbstractFormElementTest.php b/typo3/sysext/backend/Tests/Unit/Form/Element/AbstractFormElementTest.php
index b9174ee59902..3408dfd1b161 100644
--- a/typo3/sysext/backend/Tests/Unit/Form/Element/AbstractFormElementTest.php
+++ b/typo3/sysext/backend/Tests/Unit/Form/Element/AbstractFormElementTest.php
@@ -279,6 +279,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