From f8ce4c4a992ed77e67ac9f3ad43f6db288bb9806 Mon Sep 17 00:00:00 2001
From: Devid Messner <devid.messner@konverto.eu>
Date: Tue, 30 Jun 2020 13:35:56 +0200
Subject: [PATCH] [BUGFIX] Set the correct PageUid in the MountpointInfos

Replace the uid of the translated page with
the uid of the original page in the mountpoint parameter.

Resolves: #91328
Releases: master, 10.4
Change-Id: I6eeceff8b191cd76e134fda59e67550b58dfa985
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/64952
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Benni Mack <benni@typo3.org>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Benni Mack <benni@typo3.org>
---
 .../Domain/Repository/PageRepository.php      |  6 +-
 .../Domain/Repository/PageRepositoryTest.php  | 59 +++++++++++++++++++
 .../core/Tests/Functional/Fixtures/pages.xml  | 42 +++++++++++++
 .../ContentObject/ContentObjectRenderer.php   |  2 +-
 .../Classes/Typolink/PageLinkBuilder.php      |  2 +-
 5 files changed, 106 insertions(+), 5 deletions(-)

diff --git a/typo3/sysext/core/Classes/Domain/Repository/PageRepository.php b/typo3/sysext/core/Classes/Domain/Repository/PageRepository.php
index 359ff6c0a505..0c8c4c33a6ff 100644
--- a/typo3/sysext/core/Classes/Domain/Repository/PageRepository.php
+++ b/typo3/sysext/core/Classes/Domain/Repository/PageRepository.php
@@ -1085,7 +1085,7 @@ class PageRepository implements LoggerAwareInterface
                 ->removeAll()
                 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
 
-            $pageRec = $queryBuilder->select('uid', 'pid', 'doktype', 'mount_pid', 'mount_pid_ol', 't3ver_state')
+            $pageRec = $queryBuilder->select('uid', 'pid', 'doktype', 'mount_pid', 'mount_pid_ol', 't3ver_state', 'l10n_parent')
                 ->from('pages')
                 ->where(
                     $queryBuilder->expr()->eq(
@@ -1106,7 +1106,7 @@ class PageRepository implements LoggerAwareInterface
         }
         // Set first Page uid:
         if (!$firstPageUid) {
-            $firstPageUid = $pageRec['uid'];
+            $firstPageUid = (int)($pageRec['l10n_parent'] ?: $pageRec['uid']);
         }
         // Look for mount pid value plus other required circumstances:
         $mount_pid = (int)$pageRec['mount_pid'];
@@ -1117,7 +1117,7 @@ class PageRepository implements LoggerAwareInterface
                 ->removeAll()
                 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
 
-            $mountRec = $queryBuilder->select('uid', 'pid', 'doktype', 'mount_pid', 'mount_pid_ol', 't3ver_state')
+            $mountRec = $queryBuilder->select('uid', 'pid', 'doktype', 'mount_pid', 'mount_pid_ol', 't3ver_state', 'l10n_parent')
                 ->from('pages')
                 ->where(
                     $queryBuilder->expr()->eq(
diff --git a/typo3/sysext/core/Tests/Functional/Domain/Repository/PageRepositoryTest.php b/typo3/sysext/core/Tests/Functional/Domain/Repository/PageRepositoryTest.php
index 9db7f4cd333e..a87691127a88 100644
--- a/typo3/sysext/core/Tests/Functional/Domain/Repository/PageRepositoryTest.php
+++ b/typo3/sysext/core/Tests/Functional/Domain/Repository/PageRepositoryTest.php
@@ -97,6 +97,35 @@ class PageRepositoryTest extends FunctionalTestCase
         self::assertCount(4, $rows);
     }
 
+    /**
+     * @test
+     */
+    public function getMenuWithMountPoint()
+    {
+        $subject = new PageRepository();
+        $rows = $subject->getMenu([1000]);
+        self::assertEquals('root default language', $rows[1003]['title']);
+        self::assertEquals('1001', $rows[1003]['uid']);
+        self::assertEquals('1001-1003', $rows[1003]['_MP_PARAM']);
+        self::assertCount(2, $rows);
+    }
+
+    /**
+     * @test
+     */
+    public function getMenuPageOverlayWithMountPoint()
+    {
+        $subject = new PageRepository(new Context([
+            'language' => new LanguageAspect(1)
+        ]));
+        $rows = $subject->getMenu([1000]);
+        self::assertEquals('root translation', $rows[1003]['title']);
+        self::assertEquals('1001', $rows[1003]['uid']);
+        self::assertEquals('1002', $rows[1003]['_PAGES_OVERLAY_UID']);
+        self::assertEquals('1001-1003', $rows[1003]['_MP_PARAM']);
+        self::assertCount(2, $rows);
+    }
+
     /**
      * @test
      */
@@ -400,6 +429,36 @@ class PageRepositoryTest extends FunctionalTestCase
         self::assertSame($expectedSQL, $subject->where_hid_del);
     }
 
+    ////////////////////////////////
+    // Tests concerning mountpoints
+    ////////////////////////////////
+    ///
+    /**
+     * @test
+     */
+    public function getMountPointInfoForDefaultLanguage()
+    {
+        $subject = new PageRepository();
+        $mountPointInfo = $subject->getMountPointInfo(1003);
+        self::assertEquals('1001-1003', $mountPointInfo['MPvar']);
+    }
+
+    /**
+     * @test
+     */
+    public function getMountPointInfoForTranslation()
+    {
+        $mpVar = '1001-1003';
+        $subject = new PageRepository(new Context([
+            'language' => new LanguageAspect(1)
+        ]));
+        $mountPointInfo = $subject->getMountPointInfo(1003);
+        self::assertEquals($mpVar, $mountPointInfo['MPvar']);
+
+        $mountPointInfo = $subject->getMountPointInfo(1004);
+        self::assertEquals($mpVar, $mountPointInfo['MPvar']);
+    }
+
     ////////////////////////////////
     // Tests concerning workspaces
     ////////////////////////////////
diff --git a/typo3/sysext/core/Tests/Functional/Fixtures/pages.xml b/typo3/sysext/core/Tests/Functional/Fixtures/pages.xml
index 3eabf2b2271e..db75fca99d62 100644
--- a/typo3/sysext/core/Tests/Functional/Fixtures/pages.xml
+++ b/typo3/sysext/core/Tests/Functional/Fixtures/pages.xml
@@ -132,4 +132,46 @@
         <title>Attrappe 1-3-9</title>
         <deleted>0</deleted>
     </pages>
+    <pages>
+        <uid>1000</uid>
+        <pid>0</pid>
+        <doktype>1</doktype>
+        <sys_language_uid>0</sys_language_uid>
+        <title>MountPoint tests</title>
+    </pages>
+
+    <pages>
+        <uid>1001</uid>
+        <pid>1000</pid>
+        <doktype>1</doktype>
+        <sys_language_uid>0</sys_language_uid>
+        <title>root default language</title>
+    </pages>
+    <pages>
+        <uid>1002</uid>
+        <pid>1000</pid>
+        <doktype>1</doktype>
+        <l10n_parent>1001</l10n_parent>
+        <sys_language_uid>1</sys_language_uid>
+        <title>root translation</title>
+    </pages>
+    <pages>
+        <uid>1003</uid>
+        <pid>1000</pid>
+        <doktype>7</doktype>
+        <mount_pid>1001</mount_pid>
+        <mount_pid_ol>1</mount_pid_ol>
+        <sys_language_uid>0</sys_language_uid>
+        <title>Mount default language</title>
+    </pages>
+    <pages>
+        <uid>1004</uid>
+        <pid>1000</pid>
+        <doktype>7</doktype>
+        <mount_pid>1001</mount_pid>
+        <mount_pid_ol>1</mount_pid_ol>
+        <l10n_parent>1003</l10n_parent>
+        <sys_language_uid>1</sys_language_uid>
+        <title>Mount translation</title>
+    </pages>
 </dataset>
diff --git a/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php b/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
index 96ab80d71e68..284970bf1d0b 100644
--- a/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
+++ b/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
@@ -5381,7 +5381,7 @@ class ContentObjectRenderer implements LoggerAwareInterface
         }
 
         // Init vars:
-        $allFields = 'uid,hidden,starttime,endtime,fe_group,extendToSubpages,doktype,php_tree_stop,mount_pid,mount_pid_ol,t3ver_state' . $addSelectFields;
+        $allFields = 'uid,hidden,starttime,endtime,fe_group,extendToSubpages,doktype,php_tree_stop,mount_pid,mount_pid_ol,t3ver_state,l10n_parent' . $addSelectFields;
         $depth = (int)$depth;
         $begin = (int)$begin;
         $theList = [];
diff --git a/typo3/sysext/frontend/Classes/Typolink/PageLinkBuilder.php b/typo3/sysext/frontend/Classes/Typolink/PageLinkBuilder.php
index a16a63266f5f..6f1f9ee69963 100644
--- a/typo3/sysext/frontend/Classes/Typolink/PageLinkBuilder.php
+++ b/typo3/sysext/frontend/Classes/Typolink/PageLinkBuilder.php
@@ -586,7 +586,7 @@ class PageLinkBuilder extends AbstractTypolinkBuilder
                 ->removeAll()
                 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
             $queryResult = $queryBuilder
-                ->select('uid', 'pid', 'doktype', 'mount_pid', 'mount_pid_ol', 't3ver_state')
+                ->select('uid', 'pid', 'doktype', 'mount_pid', 'mount_pid_ol', 't3ver_state', 'l10n_parent')
                 ->from('pages')
                 ->where(
                     $queryBuilder->expr()->eq(
-- 
GitLab