From f12e9d29d476616026e224cc6f9f2156bf877275 Mon Sep 17 00:00:00 2001
From: Nikita Hovratov <nikita.h@live.de>
Date: Fri, 12 Nov 2021 14:24:33 +0100
Subject: [PATCH] [TASK] Add correct test for shortcut issue in language menu
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The added tests for #89871 do not prove the
correctness of the patch. They go green, without the
fix being applied. This is a blocker for another
regression bugfix, which depends on these tests.

The issue lied in the language menu, not the normal
menu. For this, a new method createLanguageMenu has
been added to AbstractLocalizedPagesTestCase.php. It
is now used to assure the generated links always
point to the target language and not to the current
one.

Resolves: #95966
Related: #89871
Related: #94677
Releases: master
Change-Id: I20d202c2eae58597ee59312ef0896debb2f93d39
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/72162
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: core-ci <typo3@b13.com>
Tested-by: Stefan Bürk <stefan@buerk.tech>
Tested-by: Benni Mack <benni@typo3.org>
---
 .../AbstractLocalizedPagesTestCase.php        | 19 +++++++++++++++++++
 .../Fixtures/ScenarioD.yaml                   |  1 +
 .../LocalizedPageRendering/ScenarioDTest.php  | 15 +++++++++++++++
 3 files changed, 35 insertions(+)

diff --git a/typo3/sysext/frontend/Tests/Functional/SiteHandling/LocalizedPageRendering/AbstractLocalizedPagesTestCase.php b/typo3/sysext/frontend/Tests/Functional/SiteHandling/LocalizedPageRendering/AbstractLocalizedPagesTestCase.php
index 66e8b2790898..08f5494f3c0b 100644
--- a/typo3/sysext/frontend/Tests/Functional/SiteHandling/LocalizedPageRendering/AbstractLocalizedPagesTestCase.php
+++ b/typo3/sysext/frontend/Tests/Functional/SiteHandling/LocalizedPageRendering/AbstractLocalizedPagesTestCase.php
@@ -147,4 +147,23 @@ abstract class AbstractLocalizedPagesTestCase extends AbstractTestCase
 
         self::assertSame($expectation, $json);
     }
+
+    protected function createLanguageMenu(string $url): array
+    {
+        $this->setUpFrontendRootPage(
+            1000,
+            ['typo3/sysext/frontend/Tests/Functional/SiteHandling/Fixtures/LinkGenerator.typoscript'],
+            ['title' => 'ACME Root']
+        );
+
+        $response = $this->executeFrontendSubRequest(
+            (new InternalRequest($url))->withInstructions([$this->createLanguageMenuProcessorInstruction(['languages' => 'auto'])]),
+            $this->internalRequestContext
+        );
+
+        $json = json_decode((string)$response->getBody(), true);
+        $json = $this->filterMenu($json);
+
+        return $json;
+    }
 }
diff --git a/typo3/sysext/frontend/Tests/Functional/SiteHandling/LocalizedPageRendering/Fixtures/ScenarioD.yaml b/typo3/sysext/frontend/Tests/Functional/SiteHandling/LocalizedPageRendering/Fixtures/ScenarioD.yaml
index 844262ed94f4..aa9b3713657d 100644
--- a/typo3/sysext/frontend/Tests/Functional/SiteHandling/LocalizedPageRendering/Fixtures/ScenarioD.yaml
+++ b/typo3/sysext/frontend/Tests/Functional/SiteHandling/LocalizedPageRendering/Fixtures/ScenarioD.yaml
@@ -52,3 +52,4 @@ entities:
         - self: {id: 1500, title: 'EN: Company', slug: '/company'}
           languageVariants:
             - self: {id: 1501, title: 'DE: Unternehmen', language: 1, slug: '/shortcut-to-about', type: *pageShortcut, shortcut: 1200, shortcut_mode: 0, l18n_cfg: 0, l10n_state: '{"shortcut":"custom","shortcut_mode":"custom"}'}
+            - self: {id: 1502, title: 'DE-CH: Unternehmen', language: 2, slug: '/unternehmen'}
diff --git a/typo3/sysext/frontend/Tests/Functional/SiteHandling/LocalizedPageRendering/ScenarioDTest.php b/typo3/sysext/frontend/Tests/Functional/SiteHandling/LocalizedPageRendering/ScenarioDTest.php
index e74e115b860e..1af7c67240c9 100644
--- a/typo3/sysext/frontend/Tests/Functional/SiteHandling/LocalizedPageRendering/ScenarioDTest.php
+++ b/typo3/sysext/frontend/Tests/Functional/SiteHandling/LocalizedPageRendering/ScenarioDTest.php
@@ -175,6 +175,7 @@ class ScenarioDTest extends AbstractLocalizedPagesTestCase
                     ['title' => 'DE-CH: Ãœber uns', 'link' => '/de-ch/ueber-uns'],
                     ['title' => 'DE-CH: Produkte', 'link' => '/de-ch/produkte'],
                     ['title' => 'EN: Shortcut to welcome', 'link' => ''],
+                    ['title' => 'DE-CH: Unternehmen', 'link' => '/de-ch/unternehmen'],
                 ],
             ],
         ];
@@ -191,4 +192,18 @@ class ScenarioDTest extends AbstractLocalizedPagesTestCase
     {
         $this->assertMenu($url, $expectedMenu);
     }
+
+    /**
+     * @test
+     */
+    public function languageMenuHasLanguageShortcutsWithLanguageSpecificUrls(): void
+    {
+        $expectedMenu = [
+            ['title' => 'English', 'link' => '/en/company'],
+            ['title' => 'German', 'link' => '/de/ueber-uns'],
+            ['title' => 'Swiss German', 'link' => '/de-ch/unternehmen'],
+        ];
+
+        self::assertSame($expectedMenu, $this->createLanguageMenu('https://acme.com/de-ch/unternehmen'));
+    }
 }
-- 
GitLab