From 4249feaea505717191d8959eaa3513e34db0d561 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stefan=20B=C3=BCrk?= <stefan@buerk.tech>
Date: Tue, 5 Sep 2023 17:02:13 +0200
Subject: [PATCH] [TASK] Add tests to show working regexp redirects with query
 parameter
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

With #101739 a bug has been reported, which is
not correct.

This change adds functional tests to `ext:redirects`
to show and cover that:

* regexp redirects with a pattern not covering params
  but query arguments are given in the request uri and
  "Respect GET Parameters" has been set do not redirect.
* RegExp Redirects with a pattern not covering params
  and no GET arguments are given in the request uri but
  "Respect GET Parameters" has been set redirects to the
  target url.

Resolves: #101739
Releases: main, 12.4, 11.5
Change-Id: I023889ba6fefe5a4b06b392e02dece4b6ac517c0
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80848
Tested-by: core-ci <typo3@b13.com>
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
Tested-by: Stefan Bürk <stefan@buerk.tech>
---
 .../Fixtures/RedirectService_regexp.csv       |  2 +
 .../Service/RedirectServiceTest.php           | 58 +++++++++++++++++++
 2 files changed, 60 insertions(+)

diff --git a/typo3/sysext/redirects/Tests/Functional/Service/Fixtures/RedirectService_regexp.csv b/typo3/sysext/redirects/Tests/Functional/Service/Fixtures/RedirectService_regexp.csv
index eae834aac8af..501e7b3941f2 100644
--- a/typo3/sysext/redirects/Tests/Functional/Service/Fixtures/RedirectService_regexp.csv
+++ b/typo3/sysext/redirects/Tests/Functional/Service/Fixtures/RedirectService_regexp.csv
@@ -20,3 +20,5 @@ sys_redirect,,,,,,,,,,,
 ,14,0,301,0,0,acme.com,#^/respect-keep-relative-target-([a-zA-Z]{1}[a-zA-Z0-9]+)\?param1=([a-zA-Z]{1}[a-zA-Z0-9]+)$#,/$1/$2,1,1,1
 ,15,0,301,0,0,acme.com,#^/other-relative-target-with-unsafe-capture-group-(.*)#,/offer-$1,1,1,0
 ,16,0,301,0,0,acme.com,#^/other-redirect-with-unsafe-capture-group-(.*)#,https://anotherdomain.com/offer$1,1,1,0
+# * @see https://forge.typo3.org/issues/101739
+,17,0,301,0,0,acme.com,#^/regexp-respect-get-parameter$#,https://anotherdomain.com/regexp-respect-get-parameter,1,1,0
diff --git a/typo3/sysext/redirects/Tests/Functional/Service/RedirectServiceTest.php b/typo3/sysext/redirects/Tests/Functional/Service/RedirectServiceTest.php
index 4edbeb5ef7da..a2640975718d 100644
--- a/typo3/sysext/redirects/Tests/Functional/Service/RedirectServiceTest.php
+++ b/typo3/sysext/redirects/Tests/Functional/Service/RedirectServiceTest.php
@@ -337,6 +337,64 @@ final class RedirectServiceTest extends FunctionalTestCase
         self::assertEquals($expectedRedirectUri, $response->getHeader('location')[0]);
     }
 
+    /**
+     * @test
+     * @see https://forge.typo3.org/issues/101739
+     */
+    public function regexpWithNoParamRegexpAndRespectingGetParameteresIssuesNotFoundStatusIfParamsAreGivenInUrl(): void
+    {
+        $url = 'https://acme.com/regexp-respect-get-parameter?param1=value1';
+        $expectedStatusCode = 404;
+        $this->importCSVDataSet(__DIR__ . '/Fixtures/RedirectService_regexp.csv');
+        $this->writeSiteConfiguration(
+            'acme-com',
+            $this->buildSiteConfiguration(1, 'https://acme.com/')
+        );
+        $this->setUpFrontendRootPage(
+            1,
+            ['typo3/sysext/redirects/Tests/Functional/Service/Fixtures/Redirects.typoscript']
+        );
+
+        $response = $this->executeFrontendSubRequest(
+            new InternalRequest($url),
+            null,
+            false
+        );
+        self::assertSame($expectedStatusCode, $response->getStatusCode());
+    }
+
+    /**
+     * @test
+     * @see https://forge.typo3.org/issues/101739
+     */
+    public function regexpWithNoParamRegexpAndRespectingGetParameteresRedirectsIfNoParamsAreGiven(): void
+    {
+        $url = 'https://acme.com/regexp-respect-get-parameter';
+        $expectedStatusCode = 301;
+        $expectedRedirectUid = 17;
+        $expectedRedirectUri = 'https://anotherdomain.com/regexp-respect-get-parameter';
+        $this->importCSVDataSet(__DIR__ . '/Fixtures/RedirectService_regexp.csv');
+        $this->writeSiteConfiguration(
+            'acme-com',
+            $this->buildSiteConfiguration(1, 'https://acme.com/')
+        );
+        $this->setUpFrontendRootPage(
+            1,
+            ['typo3/sysext/redirects/Tests/Functional/Service/Fixtures/Redirects.typoscript']
+        );
+
+        $response = $this->executeFrontendSubRequest(
+            new InternalRequest($url),
+            null,
+            false
+        );
+        self::assertSame($expectedStatusCode, $response->getStatusCode());
+        self::assertIsArray($response->getHeader('X-Redirect-By'));
+        self::assertIsArray($response->getHeader('location'));
+        self::assertSame('TYPO3 Redirect ' . $expectedRedirectUid, $response->getHeader('X-Redirect-By')[0]);
+        self::assertSame($expectedRedirectUri, $response->getHeader('location')[0]);
+    }
+
     public static function samePathWithSameDomainT3TargetDataProvider(): array
     {
         return [
-- 
GitLab