From 5c95229af666e4ce39dd5a22baecd3390d649c49 Mon Sep 17 00:00:00 2001
From: Oliver Hader <oliver@typo3.org>
Date: Thu, 14 Mar 2024 11:25:31 +0100
Subject: [PATCH] [TASK] Apply stricter URI route generation assertions
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Several tests for URI route generation are stricter and assert
that either the whole URI matches, or that at least the `?cHash=`
suffix is applied for the corresponding test permutations.

Besides that, individual URI assertions are streamlined to use
AbstractEnhancerLinkGeneratorTestCase::assertGeneratedUriEquals.

Resolves: #103396
Releases: main, 12.4, 11.5
Change-Id: I65ae10fa2984021da653bac33579feb6d10a6464
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/83463
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
Tested-by: Oliver Hader <oliver.hader@typo3.org>
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
Tested-by: Benni Mack <benni@typo3.org>
Tested-by: core-ci <typo3@b13.com>
Tested-by: Stefan Bürk <stefan@buerk.tech>
Reviewed-by: Benni Mack <benni@typo3.org>
---
 .../AbstractEnhancerLinkGeneratorTestCase.php | 18 +++++++++--
 .../LocaleModifierTest.php                    | 32 +------------------
 .../PersistedAliasMapperTest.php              | 28 +---------------
 .../PersistedPatternMapperTest.php            | 31 +-----------------
 .../EnhancerLinkGenerator/RouteTest.php       |  6 ++--
 .../StaticRangeMapperTest.php                 | 28 +---------------
 .../StaticValueMapperTest.php                 | 31 +-----------------
 7 files changed, 24 insertions(+), 150 deletions(-)

diff --git a/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/AbstractEnhancerLinkGeneratorTestCase.php b/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/AbstractEnhancerLinkGeneratorTestCase.php
index ad9aaef86bcc..3e7f174bd42b 100644
--- a/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/AbstractEnhancerLinkGeneratorTestCase.php
+++ b/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/AbstractEnhancerLinkGeneratorTestCase.php
@@ -109,7 +109,13 @@ abstract class AbstractEnhancerLinkGeneratorTestCase extends AbstractTestCase
         self::assertStringStartsWith($expectation, (string)$response->getBody());
     }
 
-    protected function assertGeneratedUriEquals(TestSet $testSet): void
+    /**
+     * In case non-`$strict` assertions are performed (using `assertStringStartsWith`), the corresponding
+     * expectations need to be specific (e.g. ending with `?cHash=` to assert that part of the URI).
+     *
+     * @param bool $strict Whether to use `assertSame` instead of `assertStringStartsWith`
+     */
+    protected function assertGeneratedUriEquals(TestSet $testSet, bool $strict = true): void
     {
         $builder = Builder::create();
         $enhancerConfiguration = $builder->compileEnhancerConfiguration($testSet);
@@ -122,6 +128,9 @@ abstract class AbstractEnhancerLinkGeneratorTestCase extends AbstractTestCase
         $this->mergeSiteConfiguration('acme-com', [
             'routeEnhancers' => ['Enhancer' => $enhancerConfiguration],
         ]);
+        $this->mergeSiteConfiguration('archive-acme-com', [
+            'routeEnhancers' => ['Enhancer' => $enhancerConfiguration],
+        ]);
 
         $response = $this->executeFrontendSubRequest(
             (new InternalRequest('https://acme.us/'))
@@ -136,6 +145,11 @@ abstract class AbstractEnhancerLinkGeneratorTestCase extends AbstractTestCase
                 ])
         );
 
-        self::assertStringStartsWith($expectation, (string)$response->getBody());
+        $actual = (string)$response->getBody();
+        if ($strict) {
+            self::assertSame($expectation, $actual);
+        } else {
+            self::assertStringStartsWith($expectation, $actual);
+        }
     }
 }
diff --git a/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/LocaleModifierTest.php b/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/LocaleModifierTest.php
index 93dceb74a010..a35306c53f88 100644
--- a/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/LocaleModifierTest.php
+++ b/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/LocaleModifierTest.php
@@ -27,7 +27,6 @@ use TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\TestSet;
 use TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableItem;
 use TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Variables;
 use TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableValue;
-use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
 
 final class LocaleModifierTest extends AbstractEnhancerLinkGeneratorTestCase
 {
@@ -107,36 +106,7 @@ final class LocaleModifierTest extends AbstractEnhancerLinkGeneratorTestCase
     #[Test]
     public function localeModifierIsApplied(TestSet $testSet): void
     {
-        $builder = Builder::create();
-        $enhancerConfiguration = $builder->compileEnhancerConfiguration($testSet);
-        $additionalParameters = $builder->compileGenerateParameters($testSet);
-        /** @var LanguageContext $languageContext */
-        $languageContext = $testSet->getSingleApplicable(LanguageContext::class);
-        $targetLanguageId = $languageContext->getLanguageId();
-        $expectation = $builder->compileUrl($testSet);
-
-        $this->mergeSiteConfiguration('acme-com', [
-            'routeEnhancers' => ['Enhancer' => $enhancerConfiguration],
-        ]);
-        $this->mergeSiteConfiguration('archive-acme-com', [
-            'routeEnhancers' => ['Enhancer' => $enhancerConfiguration],
-        ]);
-
-        $response = $this->executeFrontendSubRequest(
-            (new InternalRequest('https://acme.us/'))
-                ->withPageId(1100)
-                ->withInstructions([
-                    $this->createTypoLinkUrlInstruction([
-                        'parameter' => $testSet->getTargetPageId(),
-                        'language' => $targetLanguageId,
-                        'additionalParams' => $additionalParameters,
-                        'forceAbsoluteUrl' => 1,
-                    ]),
-                ])
-        );
-
-        $body = (string)$response->getBody();
-        self::assertStringStartsWith($expectation, $body);
+        $this->assertGeneratedUriEquals($testSet, false);
     }
 
     /**
diff --git a/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/PersistedAliasMapperTest.php b/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/PersistedAliasMapperTest.php
index cf0953532295..74aa3bcbd642 100644
--- a/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/PersistedAliasMapperTest.php
+++ b/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/PersistedAliasMapperTest.php
@@ -27,7 +27,6 @@ use TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\TestSet;
 use TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableItem;
 use TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Variables;
 use TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableValue;
-use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
 
 final class PersistedAliasMapperTest extends AbstractEnhancerLinkGeneratorTestCase
 {
@@ -85,32 +84,7 @@ final class PersistedAliasMapperTest extends AbstractEnhancerLinkGeneratorTestCa
     #[Test]
     public function persistedAliasMapperIsApplied(TestSet $testSet): void
     {
-        $builder = Builder::create();
-        $enhancerConfiguration = $builder->compileEnhancerConfiguration($testSet);
-        $additionalParameters = $builder->compileGenerateParameters($testSet);
-        /** @var LanguageContext $languageContext */
-        $languageContext = $testSet->getSingleApplicable(LanguageContext::class);
-        $targetLanguageId = $languageContext->getLanguageId();
-        $expectation = $builder->compileUrl($testSet);
-
-        $this->mergeSiteConfiguration('acme-com', [
-            'routeEnhancers' => ['Enhancer' => $enhancerConfiguration],
-        ]);
-
-        $response = $this->executeFrontendSubRequest(
-            (new InternalRequest('https://acme.us/'))
-                ->withPageId(1100)
-                ->withInstructions([
-                    $this->createTypoLinkUrlInstruction([
-                        'parameter' => 1100,
-                        'language' => $targetLanguageId,
-                        'additionalParams' => $additionalParameters,
-                        'forceAbsoluteUrl' => 1,
-                    ]),
-                ])
-        );
-
-        self::assertSame($expectation, (string)$response->getBody());
+        $this->assertGeneratedUriEquals($testSet);
     }
 
     /**
diff --git a/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/PersistedPatternMapperTest.php b/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/PersistedPatternMapperTest.php
index 6fe7e38f1ca2..4089ddd4813d 100644
--- a/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/PersistedPatternMapperTest.php
+++ b/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/PersistedPatternMapperTest.php
@@ -27,7 +27,6 @@ use TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\TestSet;
 use TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableItem;
 use TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Variables;
 use TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableValue;
-use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
 
 final class PersistedPatternMapperTest extends AbstractEnhancerLinkGeneratorTestCase
 {
@@ -103,35 +102,7 @@ final class PersistedPatternMapperTest extends AbstractEnhancerLinkGeneratorTest
     #[Test]
     public function persistedPatternMapperIsApplied(TestSet $testSet): void
     {
-        $builder = Builder::create();
-        $enhancerConfiguration = $builder->compileEnhancerConfiguration($testSet);
-        $additionalParameters = $builder->compileGenerateParameters($testSet);
-        /** @var LanguageContext $languageContext */
-        $languageContext = $testSet->getSingleApplicable(LanguageContext::class);
-        $targetLanguageId = $languageContext->getLanguageId();
-        $expectation = $builder->compileUrl($testSet);
-
-        $this->mergeSiteConfiguration('acme-com', [
-            'routeEnhancers' => ['Enhancer' => $enhancerConfiguration],
-        ]);
-        $this->mergeSiteConfiguration('archive-acme-com', [
-            'routeEnhancers' => ['Enhancer' => $enhancerConfiguration],
-        ]);
-
-        $response = $this->executeFrontendSubRequest(
-            (new InternalRequest('https://acme.us/'))
-                ->withPageId(1100)
-                ->withInstructions([
-                    $this->createTypoLinkUrlInstruction([
-                        'parameter' => $testSet->getTargetPageId(),
-                        'language' => $targetLanguageId,
-                        'additionalParams' => $additionalParameters,
-                        'forceAbsoluteUrl' => 1,
-                    ]),
-                ])
-        );
-
-        self::assertSame($expectation, (string)$response->getBody());
+        $this->assertGeneratedUriEquals($testSet);
     }
 
     /**
diff --git a/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/RouteTest.php b/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/RouteTest.php
index aeea6ade7118..dc1c080f1888 100644
--- a/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/RouteTest.php
+++ b/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/RouteTest.php
@@ -139,7 +139,7 @@ final class RouteTest extends AbstractEnhancerLinkGeneratorTestCase
                     ->withTargetPageId(1100)
                     ->withUrl(
                         VariableValue::create(
-                            'https://acme.us/welcome/enhance/hundred/20[[pathSuffix]]',
+                            'https://acme.us/welcome/enhance/hundred/20[[pathSuffix]]?cHash=',
                             Variables::create(['pathSuffix' => ''])
                         )
                     ),
@@ -148,7 +148,7 @@ final class RouteTest extends AbstractEnhancerLinkGeneratorTestCase
                     ->withTargetPageId(1100)
                     ->withUrl(
                         VariableValue::create(
-                            'https://acme.fr/bienvenue/enhance/cent/20[[pathSuffix]]',
+                            'https://acme.fr/bienvenue/enhance/cent/20[[pathSuffix]]?cHash=',
                             Variables::create(['pathSuffix' => ''])
                         )
                     )
@@ -211,7 +211,7 @@ final class RouteTest extends AbstractEnhancerLinkGeneratorTestCase
     #[Test]
     public function routeDefaultsForMultipleParametersAreConsidered(TestSet $testSet): void
     {
-        $this->assertGeneratedUriEquals($testSet);
+        $this->assertGeneratedUriEquals($testSet, false);
     }
 
     public static function routeRequirementsHavingAspectsAreConsideredDataProvider(): array
diff --git a/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/StaticRangeMapperTest.php b/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/StaticRangeMapperTest.php
index 8254605d000a..dcebee001d27 100644
--- a/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/StaticRangeMapperTest.php
+++ b/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/StaticRangeMapperTest.php
@@ -28,7 +28,6 @@ use TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableI
 use TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Variables;
 use TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariablesContext;
 use TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableValue;
-use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
 
 final class StaticRangeMapperTest extends AbstractEnhancerLinkGeneratorTestCase
 {
@@ -95,32 +94,7 @@ final class StaticRangeMapperTest extends AbstractEnhancerLinkGeneratorTestCase
     #[Test]
     public function staticRangeMapperIsApplied(TestSet $testSet): void
     {
-        $builder = Builder::create();
-        $enhancerConfiguration = $builder->compileEnhancerConfiguration($testSet);
-        $additionalParameters = $builder->compileGenerateParameters($testSet);
-        /** @var LanguageContext $languageContext */
-        $languageContext = $testSet->getSingleApplicable(LanguageContext::class);
-        $targetLanguageId = $languageContext->getLanguageId();
-        $expectation = $builder->compileUrl($testSet);
-
-        $this->mergeSiteConfiguration('acme-com', [
-            'routeEnhancers' => ['Enhancer' => $enhancerConfiguration],
-        ]);
-
-        $response = $this->executeFrontendSubRequest(
-            (new InternalRequest('https://acme.us/'))
-                ->withPageId(1100)
-                ->withInstructions([
-                    $this->createTypoLinkUrlInstruction([
-                        'parameter' => $testSet->getTargetPageId(),
-                        'language' => $targetLanguageId,
-                        'additionalParams' => $additionalParameters,
-                        'forceAbsoluteUrl' => 1,
-                    ]),
-                ])
-        );
-
-        self::assertStringStartsWith($expectation, (string)$response->getBody());
+        $this->assertGeneratedUriEquals($testSet);
     }
 
     /**
diff --git a/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/StaticValueMapperTest.php b/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/StaticValueMapperTest.php
index cefb74552b75..23a5f01c4bca 100644
--- a/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/StaticValueMapperTest.php
+++ b/typo3/sysext/frontend/Tests/Functional/SiteHandling/EnhancerLinkGenerator/StaticValueMapperTest.php
@@ -27,7 +27,6 @@ use TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\TestSet;
 use TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableItem;
 use TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\Variables;
 use TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\Framework\Builder\VariableValue;
-use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
 
 final class StaticValueMapperTest extends AbstractEnhancerLinkGeneratorTestCase
 {
@@ -111,35 +110,7 @@ final class StaticValueMapperTest extends AbstractEnhancerLinkGeneratorTestCase
     #[Test]
     public function staticValueMapperIsApplied(TestSet $testSet): void
     {
-        $builder = Builder::create();
-        $enhancerConfiguration = $builder->compileEnhancerConfiguration($testSet);
-        $additionalParameters = $builder->compileGenerateParameters($testSet);
-        /** @var LanguageContext $languageContext */
-        $languageContext = $testSet->getSingleApplicable(LanguageContext::class);
-        $targetLanguageId = $languageContext->getLanguageId();
-        $expectation = $builder->compileUrl($testSet);
-
-        $this->mergeSiteConfiguration('acme-com', [
-            'routeEnhancers' => ['Enhancer' => $enhancerConfiguration],
-        ]);
-        $this->mergeSiteConfiguration('archive-acme-com', [
-            'routeEnhancers' => ['Enhancer' => $enhancerConfiguration],
-        ]);
-
-        $response = $this->executeFrontendSubRequest(
-            (new InternalRequest('https://acme.us/'))
-                ->withPageId(1100)
-                ->withInstructions([
-                    $this->createTypoLinkUrlInstruction([
-                        'parameter' => $testSet->getTargetPageId(),
-                        'language' => $targetLanguageId,
-                        'additionalParams' => $additionalParameters,
-                        'forceAbsoluteUrl' => 1,
-                    ]),
-                ])
-        );
-
-        self::assertStringStartsWith($expectation, (string)$response->getBody());
+        $this->assertGeneratedUriEquals($testSet);
     }
 
     /**
-- 
GitLab