From 46037481f1a9f44d3ea1f442850eb7832159621a Mon Sep 17 00:00:00 2001
From: Torben Hansen <derhansen@gmail.com>
Date: Sun, 18 Feb 2024 19:28:47 +0100
Subject: [PATCH] [TASK] Prevent undefined encryptionKey in tests

With #103046, `encryptionKey` has been removed from
`DefaultConfiguration.php`. This leads to PHP array
key warnings in unit tests.

The patch sets it for various tests.

Resolves: #103148
Related: #103046
Releases: main, 12.4, 11.5
Change-Id: Ife279449c2db569c8d63a0cc19009f1a3ca278d5
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/83074
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: core-ci <typo3@b13.com>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
---
 .../Controller/FormInlineAjaxControllerTest.php  |  6 ++++++
 .../AbstractFormProtectionTest.php               | 10 +---------
 .../FormProtection/BackendFormProtectionTest.php |  1 +
 .../InstallToolFormProtectionTest.php            |  6 ++++++
 .../core/Tests/Unit/Page/ImportMapTest.php       |  1 +
 .../Tests/Unit/Utility/GeneralUtilityTest.php    |  3 +++
 .../FormDefinitionConversionServiceTest.php      |  1 +
 .../FormDefinitionValidationServiceTest.php      | 16 ++++++++++------
 .../Middleware/PageArgumentValidatorTest.php     |  1 +
 9 files changed, 30 insertions(+), 15 deletions(-)

diff --git a/typo3/sysext/backend/Tests/Unit/Controller/FormInlineAjaxControllerTest.php b/typo3/sysext/backend/Tests/Unit/Controller/FormInlineAjaxControllerTest.php
index b2d8d119cfc4..1a8433685e67 100644
--- a/typo3/sysext/backend/Tests/Unit/Controller/FormInlineAjaxControllerTest.php
+++ b/typo3/sysext/backend/Tests/Unit/Controller/FormInlineAjaxControllerTest.php
@@ -24,6 +24,12 @@ use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
 
 final class FormInlineAjaxControllerTest extends UnitTestCase
 {
+    protected function setUp(): void
+    {
+        parent::setUp();
+        $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = '';
+    }
+
     /**
      * @test
      */
diff --git a/typo3/sysext/core/Tests/Unit/FormProtection/AbstractFormProtectionTest.php b/typo3/sysext/core/Tests/Unit/FormProtection/AbstractFormProtectionTest.php
index d05ab6a73c27..0e0d2b6585b0 100644
--- a/typo3/sysext/core/Tests/Unit/FormProtection/AbstractFormProtectionTest.php
+++ b/typo3/sysext/core/Tests/Unit/FormProtection/AbstractFormProtectionTest.php
@@ -31,11 +31,9 @@ final class AbstractFormProtectionTest extends UnitTestCase
     {
         parent::setUp();
         $this->subject = new FormProtectionTesting();
+        $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = '';
     }
 
-    /////////////////////////////////////////
-    // Tests concerning the basic functions
-    /////////////////////////////////////////
     /**
      * @test
      */
@@ -85,9 +83,6 @@ final class AbstractFormProtectionTest extends UnitTestCase
         $subject->clean();
     }
 
-    ///////////////////////////////////
-    // Tests concerning generateToken
-    ///////////////////////////////////
     /**
      * @test
      */
@@ -138,9 +133,6 @@ final class AbstractFormProtectionTest extends UnitTestCase
         self::assertEquals($this->subject->generateToken('foo', 'edit', 'bar'), $this->subject->generateToken('foo', 'edit', 'bar'));
     }
 
-    ///////////////////////////////////
-    // Tests concerning validateToken
-    ///////////////////////////////////
     /**
      * @test
      */
diff --git a/typo3/sysext/core/Tests/Unit/FormProtection/BackendFormProtectionTest.php b/typo3/sysext/core/Tests/Unit/FormProtection/BackendFormProtectionTest.php
index 2d9a7d348736..2848da7385a3 100644
--- a/typo3/sysext/core/Tests/Unit/FormProtection/BackendFormProtectionTest.php
+++ b/typo3/sysext/core/Tests/Unit/FormProtection/BackendFormProtectionTest.php
@@ -41,6 +41,7 @@ final class BackendFormProtectionTest extends UnitTestCase
                 throw new \Exception('Closure called', 1442592030);
             }
         );
+        $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = '';
     }
 
     /**
diff --git a/typo3/sysext/core/Tests/Unit/FormProtection/InstallToolFormProtectionTest.php b/typo3/sysext/core/Tests/Unit/FormProtection/InstallToolFormProtectionTest.php
index 3dcf3167565c..4571d6840708 100644
--- a/typo3/sysext/core/Tests/Unit/FormProtection/InstallToolFormProtectionTest.php
+++ b/typo3/sysext/core/Tests/Unit/FormProtection/InstallToolFormProtectionTest.php
@@ -23,6 +23,12 @@ use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
 
 final class InstallToolFormProtectionTest extends UnitTestCase
 {
+    protected function setUp(): void
+    {
+        parent::setUp();
+        $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = '';
+    }
+
     /**
      * @test
      */
diff --git a/typo3/sysext/core/Tests/Unit/Page/ImportMapTest.php b/typo3/sysext/core/Tests/Unit/Page/ImportMapTest.php
index f5f64a267095..211d58cce5af 100644
--- a/typo3/sysext/core/Tests/Unit/Page/ImportMapTest.php
+++ b/typo3/sysext/core/Tests/Unit/Page/ImportMapTest.php
@@ -51,6 +51,7 @@ final class ImportMapTest extends UnitTestCase
         );
         $this->backupPackageManager = \Closure::bind(fn(): PackageManager => ExtensionManagementUtility::$packageManager, null, ExtensionManagementUtility::class)();
         ExtensionManagementUtility::setPackageManager($this->mockPackageManager());
+        $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = '';
     }
 
     protected function tearDown(): void
diff --git a/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php b/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php
index 5aad9563274c..f511dd452e06 100644
--- a/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php
+++ b/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php
@@ -1780,6 +1780,7 @@ final class GeneralUtilityTest extends UnitTestCase
      */
     public function hmacReturnsHashOfProperLength(): void
     {
+        $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = '';
         $hmac = GeneralUtility::hmac('message');
         self::assertTrue(!empty($hmac) && is_string($hmac));
         self::assertEquals(strlen($hmac), 40);
@@ -1790,6 +1791,7 @@ final class GeneralUtilityTest extends UnitTestCase
      */
     public function hmacReturnsEqualHashesForEqualInput(): void
     {
+        $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = '';
         $msg0 = 'message';
         $msg1 = 'message';
         self::assertEquals(GeneralUtility::hmac($msg0), GeneralUtility::hmac($msg1));
@@ -1800,6 +1802,7 @@ final class GeneralUtilityTest extends UnitTestCase
      */
     public function hmacReturnsNoEqualHashesForNonEqualInput(): void
     {
+        $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = '';
         $msg0 = 'message0';
         $msg1 = 'message1';
         self::assertNotEquals(GeneralUtility::hmac($msg0), GeneralUtility::hmac($msg1));
diff --git a/typo3/sysext/form/Tests/Unit/Domain/Configuration/FormDefinitionConversionServiceTest.php b/typo3/sysext/form/Tests/Unit/Domain/Configuration/FormDefinitionConversionServiceTest.php
index 9f2360b282e9..f45864c60463 100644
--- a/typo3/sysext/form/Tests/Unit/Domain/Configuration/FormDefinitionConversionServiceTest.php
+++ b/typo3/sysext/form/Tests/Unit/Domain/Configuration/FormDefinitionConversionServiceTest.php
@@ -30,6 +30,7 @@ final class FormDefinitionConversionServiceTest extends UnitTestCase
      */
     public function addHmacDataAddsHmacHashes(): void
     {
+        $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = '';
         $formDefinitionConversionService = $this->getAccessibleMock(
             FormDefinitionConversionService::class,
             [
diff --git a/typo3/sysext/form/Tests/Unit/Domain/Configuration/FormDefinitionValidationServiceTest.php b/typo3/sysext/form/Tests/Unit/Domain/Configuration/FormDefinitionValidationServiceTest.php
index 0c66bc9ecf42..3554fa39e7d5 100644
--- a/typo3/sysext/form/Tests/Unit/Domain/Configuration/FormDefinitionValidationServiceTest.php
+++ b/typo3/sysext/form/Tests/Unit/Domain/Configuration/FormDefinitionValidationServiceTest.php
@@ -247,7 +247,8 @@ final class FormDefinitionValidationServiceTest extends UnitTestCase
 
     public static function validateAllPropertyValuesFromCreatableFormElementDataProvider(): array
     {
-        $encryptionKeyBackup = $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'];
+        // Be aware that the $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] assignment in setUp is done
+        // after the dataProvider intitialization. Therefore, the encryption key must also be defined here.
         $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = '12345';
 
         $sessionToken = '54321';
@@ -274,8 +275,9 @@ final class FormDefinitionValidationServiceTest extends UnitTestCase
             ],
         ];
 
-        // be aware that backup globals does not impact globals used in data providers as these are called before the setUp/tearDown is done
-        $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = $encryptionKeyBackup;
+        // Unset global encryption key, so following tests do not use it. Data providers are not covered by phpunit backupGlobals.
+        // @todo: Refactor this out of the data provider.
+        unset($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']);
 
         return [
             [
@@ -422,7 +424,8 @@ final class FormDefinitionValidationServiceTest extends UnitTestCase
 
     public static function validateAllPropertyValuesFromCreatablePropertyCollectionElementDataProvider(): array
     {
-        $encryptionKeyBackup = $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'];
+        // Be aware that the $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] assignment in setUp is done
+        // after the dataProvider intitialization. Therefore, the encryption key must also be defined here.
         $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = '12345';
 
         $sessionToken = '54321';
@@ -449,8 +452,9 @@ final class FormDefinitionValidationServiceTest extends UnitTestCase
             ],
         ];
 
-        // be aware that backup globals does not impact globals used in data providers as these are called before the setUp/tearDown is done
-        $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = $encryptionKeyBackup;
+        // Unset global encryption key, so following tests do not use it. Data providers are not covered by phpunit backupGlobals.
+        // @todo: Refactor this out of the data provider.
+        unset($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']);
 
         return [
             [
diff --git a/typo3/sysext/frontend/Tests/Unit/Middleware/PageArgumentValidatorTest.php b/typo3/sysext/frontend/Tests/Unit/Middleware/PageArgumentValidatorTest.php
index 62a473275b07..19b13f4e8c19 100644
--- a/typo3/sysext/frontend/Tests/Unit/Middleware/PageArgumentValidatorTest.php
+++ b/typo3/sysext/frontend/Tests/Unit/Middleware/PageArgumentValidatorTest.php
@@ -55,6 +55,7 @@ final class PageArgumentValidatorTest extends UnitTestCase
                 return new Response();
             }
         };
+        $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = '';
     }
 
     /**
-- 
GitLab