From ebbde86c75f169d5c567a16cb0d5c44bf74890de Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stefan=20B=C3=BCrk?= <stefan@buerk.tech>
Date: Sat, 27 Nov 2021 16:49:33 +0100
Subject: [PATCH] [BUGFIX] Create auto-redirects with default language page uid

Creating auto redirects, if page slug is changed, has been extended
to add the target with LinkHandler 't3://' uri schema with #89327,
adding language argument '_language' to enforce the language for the
redirect, missing that that did not worked, which was solved through
adding '_language' argument handling in #96043.

As languge arguments '_language' and 'L' are ignored, if the provided
page uid is already the uid of the localized page, the parameter was
meaning less. Further this breaks language fallback handling, if the
localized page record is deleted.

This patch uses now the default language page uid for creating target
of auto-redirects to mitigate the issue that redirect will break if
localized page is deleted but language fallback is configured.

Resolves: #96103
Related: #96043
Related: #89327
Releases: master, 11.5, 10.4
Change-Id: I2ece06bf053bda4edb542910c3f51b1e6c8e052c
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/72327
Tested-by: core-ci <typo3@b13.com>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Benni Mack <benni@typo3.org>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Benni Mack <benni@typo3.org>
---
 .../redirects/Classes/Service/SlugService.php    |  5 +++--
 .../Tests/Functional/Service/SlugServiceTest.php | 16 ++++++++--------
 2 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/typo3/sysext/redirects/Classes/Service/SlugService.php b/typo3/sysext/redirects/Classes/Service/SlugService.php
index d02565ef3179..aad9870acf88 100644
--- a/typo3/sysext/redirects/Classes/Service/SlugService.php
+++ b/typo3/sysext/redirects/Classes/Service/SlugService.php
@@ -122,7 +122,7 @@ class SlugService implements LoggerAwareInterface
         if ($this->autoUpdateSlugs || $this->autoCreateRedirects) {
             $this->createCorrelationIds($pageId, $correlationId);
             if ($this->autoCreateRedirects) {
-                $this->createRedirect($currentSlug, (int)$currentPageRecord['uid'], (int)$currentPageRecord['sys_language_uid'], (int)$pageId);
+                $this->createRedirect($currentSlug, $defaultPageId, (int)$currentPageRecord['sys_language_uid'], (int)$pageId);
             }
             if ($this->autoUpdateSlugs) {
                 $this->checkSubPages($currentPageRecord, $currentSlug, $newSlug);
@@ -207,7 +207,8 @@ class SlugService implements LoggerAwareInterface
         foreach ($subPageRecords as $subPageRecord) {
             $newSlug = $this->updateSlug($subPageRecord, $oldSlugOfParentPage, $newSlugOfParentPage);
             if ($newSlug !== null && $this->autoCreateRedirects) {
-                $this->createRedirect($subPageRecord['slug'], (int)$subPageRecord['uid'], $languageUid, $pageId);
+                $subPageId = (int)$subPageRecord['sys_language_uid'] === 0 ? (int)$subPageRecord['uid'] : (int)$subPageRecord['l10n_parent'];
+                $this->createRedirect($subPageRecord['slug'], $subPageId, $languageUid, $pageId);
             }
         }
     }
diff --git a/typo3/sysext/redirects/Tests/Functional/Service/SlugServiceTest.php b/typo3/sysext/redirects/Tests/Functional/Service/SlugServiceTest.php
index c8d1ba990841..ddcca8458a4e 100644
--- a/typo3/sysext/redirects/Tests/Functional/Service/SlugServiceTest.php
+++ b/typo3/sysext/redirects/Tests/Functional/Service/SlugServiceTest.php
@@ -259,8 +259,8 @@ class SlugServiceTest extends FunctionalTestCase
 
         // This redirects should exists, after rebuildSlugsForSlugChange() has run
         $redirects = [
-            ['source_host' => 'de.example.com', 'source_path' => '/dummy-1-3', 'target' => 't3://page?uid=31&_language=1'],
-            ['source_host' => 'de.example.com', 'source_path' => '/dummy-1-3/dummy-1-3-8', 'target' => 't3://page?uid=32&_language=1'],
+            ['source_host' => 'de.example.com', 'source_path' => '/dummy-1-3', 'target' => 't3://page?uid=3&_language=1'],
+            ['source_host' => 'de.example.com', 'source_path' => '/dummy-1-3/dummy-1-3-8', 'target' => 't3://page?uid=8&_language=1'],
         ];
 
         $this->assertSlugsAndRedirectsExists($slugs, $redirects);
@@ -298,8 +298,8 @@ class SlugServiceTest extends FunctionalTestCase
 
         // This redirects should exists, after rebuildSlugsForSlugChange() has run
         $redirects = [
-            ['source_host' => 'de.example.com', 'source_path' => '/sub-folder/dummy-1-3', 'target' => 't3://page?uid=31&_language=1'],
-            ['source_host' => 'de.example.com', 'source_path' => '/sub-folder/dummy-1-3/dummy-1-3-8', 'target' => 't3://page?uid=32&_language=1'],
+            ['source_host' => 'de.example.com', 'source_path' => '/sub-folder/dummy-1-3', 'target' => 't3://page?uid=3&_language=1'],
+            ['source_host' => 'de.example.com', 'source_path' => '/sub-folder/dummy-1-3/dummy-1-3-8', 'target' => 't3://page?uid=8&_language=1'],
         ];
 
         $this->assertSlugsAndRedirectsExists($slugs, $redirects);
@@ -373,10 +373,10 @@ class SlugServiceTest extends FunctionalTestCase
 
         // This redirects should exists, after rebuildSlugsForSlugChange() has run
         $redirects = [
-            ['source_host' => 'de.example.com', 'source_path' => '/', 'target' => 't3://page?uid=5&_language=1'],
-            ['source_host' => 'de.example.com', 'source_path' => '/dummy-1-2', 'target' => 't3://page?uid=6&_language=1'],
-            ['source_host' => 'de.example.com', 'source_path' => '/dummy-1-3', 'target' => 't3://page?uid=7&_language=1'],
-            ['source_host' => 'de.example.com', 'source_path' => '/dummy-1-2/dummy-1-2-3', 'target' => 't3://page?uid=8&_language=1'],
+            ['source_host' => 'de.example.com', 'source_path' => '/', 'target' => 't3://page?uid=1&_language=1'],
+            ['source_host' => 'de.example.com', 'source_path' => '/dummy-1-2', 'target' => 't3://page?uid=2&_language=1'],
+            ['source_host' => 'de.example.com', 'source_path' => '/dummy-1-3', 'target' => 't3://page?uid=3&_language=1'],
+            ['source_host' => 'de.example.com', 'source_path' => '/dummy-1-2/dummy-1-2-3', 'target' => 't3://page?uid=4&_language=1'],
         ];
 
         $this->assertSlugsAndRedirectsExists($slugs, $redirects);
-- 
GitLab