From 94279ac3d2937b628f9aff41822eebe9c975d1d5 Mon Sep 17 00:00:00 2001
From: Oliver Hader <oliver@typo3.org>
Date: Tue, 14 Feb 2017 15:03:14 +0100
Subject: [PATCH] [BUGFIX] allowLanguageSynchronization flaws with
 pages_language_overlay

The allowLanguageSynchronization does not work correctly with the tables
pages and pages_language_overlay. This change at least provides the basic
functionality for regular non-relational values (which might work, but are
not verified with tests properly).

Resolves: #79809
Releases: master
Change-Id: Ia8402799828490e695fd7bfed1d1df51c6c43f3e
Reviewed-on: https://review.typo3.org/51671
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
---
 .../Localization/DataMapProcessor.php         | 37 +++++++++++++------
 .../ForeignField/DataSet/LiveDefaultPages.csv | 10 ++---
 .../Regular/AbstractActionTestCase.php        | 11 +++++-
 .../Regular/Modify/ActionTest.php             | 14 +++++++
 .../DataSet/localizePageWSynchronization.csv  | 17 +++++++++
 5 files changed, 71 insertions(+), 18 deletions(-)
 create mode 100644 typo3/sysext/core/Tests/Functional/DataHandling/Regular/Modify/DataSet/localizePageWSynchronization.csv

diff --git a/typo3/sysext/core/Classes/DataHandling/Localization/DataMapProcessor.php b/typo3/sysext/core/Classes/DataHandling/Localization/DataMapProcessor.php
index 01dd6b0a0c30..2654a616b5d7 100644
--- a/typo3/sysext/core/Classes/DataHandling/Localization/DataMapProcessor.php
+++ b/typo3/sysext/core/Classes/DataHandling/Localization/DataMapProcessor.php
@@ -38,6 +38,10 @@ use TYPO3\CMS\Core\Utility\StringUtility;
  * the data-map directly, which ensures proper history entries as a side-effect.
  * For inline relational record editing, this processor either triggers the copy
  * or localize actions by instantiation a new local DataHandler instance.
+ *
+ * Namings in this class:
+ * + forTableName, forId always refers to dependencies data is provided *for*
+ * + fromTableName, fromId always refers to ancestors data is retrieved *from*
  */
 class DataMapProcessor
 {
@@ -108,28 +112,37 @@ class DataMapProcessor
      */
     protected function collectItems(string $tableName, array $idValues)
     {
-        if (!$this->isApplicable($tableName)) {
+        $forTableName = $tableName;
+        if ($forTableName === 'pages') {
+            $forTableName = 'pages_language_overlay';
+        }
+
+        if (!$this->isApplicable($forTableName)) {
             return;
         }
 
         $fieldNames = [
             'uid' => 'uid',
             'l10n_state' => 'l10n_state',
-            'language' => $GLOBALS['TCA'][$tableName]['ctrl']['languageField'],
-            'parent' => $GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'],
+            'language' => $GLOBALS['TCA'][$forTableName]['ctrl']['languageField'],
+            'parent' => $GLOBALS['TCA'][$forTableName]['ctrl']['transOrigPointerField'],
         ];
-        if (!empty($GLOBALS['TCA'][$tableName]['ctrl']['translationSource'])) {
-            $fieldNames['source'] = $GLOBALS['TCA'][$tableName]['ctrl']['translationSource'];
+        if (!empty($GLOBALS['TCA'][$forTableName]['ctrl']['translationSource'])) {
+            $fieldNames['source'] = $GLOBALS['TCA'][$forTableName]['ctrl']['translationSource'];
         }
 
-        $translationValues = $this->fetchTranslationValues(
-            $tableName,
-            $fieldNames,
-            $this->filterNumericIds(array_keys($idValues))
-        );
+        $translationValues = [];
+        // Fetching parent/source pointer values does not make sense for pages
+        if ($tableName !== 'pages') {
+            $translationValues = $this->fetchTranslationValues(
+                $tableName,
+                $fieldNames,
+                $this->filterNumericIds(array_keys($idValues))
+            );
+        }
 
         $dependencies = $this->fetchDependencies(
-            $tableName,
+            $forTableName,
             $this->filterNumericIds(array_keys($idValues))
         );
 
@@ -559,7 +572,7 @@ class DataMapProcessor
     }
 
     /**
-     * Create arary of dependent records
+     * Fetches translation dependencies for a given parent/source record ids.
      *
      * @param string $tableName
      * @param array $ids
diff --git a/typo3/sysext/core/Tests/Functional/DataHandling/IRRE/ForeignField/DataSet/LiveDefaultPages.csv b/typo3/sysext/core/Tests/Functional/DataHandling/IRRE/ForeignField/DataSet/LiveDefaultPages.csv
index 335d96c76764..198fc67552f3 100644
--- a/typo3/sysext/core/Tests/Functional/DataHandling/IRRE/ForeignField/DataSet/LiveDefaultPages.csv
+++ b/typo3/sysext/core/Tests/Functional/DataHandling/IRRE/ForeignField/DataSet/LiveDefaultPages.csv
@@ -1,6 +1,6 @@
 "pages",,,,,,,,,,,
-,"uid","pid","sorting","deleted","t3_origuid","t3ver_wsid","t3ver_state","t3ver_stage","t3ver_oid","t3ver_move_id","title"
-,1,0,256,0,0,0,0,0,0,0,"FunctionalTest"
-,88,1,256,0,0,0,0,0,0,0,"DataHandlerTest"
-,89,88,256,0,0,0,0,0,0,0,"Relations"
-,90,88,512,0,0,0,0,0,0,0,"Target"
+,"uid","pid","sorting","deleted","t3_origuid","t3ver_wsid","t3ver_state","t3ver_stage","t3ver_oid","t3ver_move_id","title","media"
+,1,0,256,0,0,0,0,0,0,0,"FunctionalTest",0
+,88,1,256,0,0,0,0,0,0,0,"DataHandlerTest",0
+,89,88,256,0,0,0,0,0,0,0,"Relations",0
+,90,88,512,0,0,0,0,0,0,0,"Target",0
diff --git a/typo3/sysext/core/Tests/Functional/DataHandling/Regular/AbstractActionTestCase.php b/typo3/sysext/core/Tests/Functional/DataHandling/Regular/AbstractActionTestCase.php
index 145a75b87273..6662fbf22f21 100644
--- a/typo3/sysext/core/Tests/Functional/DataHandling/Regular/AbstractActionTestCase.php
+++ b/typo3/sysext/core/Tests/Functional/DataHandling/Regular/AbstractActionTestCase.php
@@ -259,7 +259,16 @@ abstract class AbstractActionTestCase extends \TYPO3\CMS\Core\Tests\Functional\D
      */
     public function localizePage()
     {
-        $this->actionService->localizeRecord(self::TABLE_Page, self::VALUE_PageId, self::VALUE_LanguageId);
+        $localizedTableIds = $this->actionService->localizeRecord(self::TABLE_Page, self::VALUE_PageId, self::VALUE_LanguageId);
+        $this->recordIds['localizedPageId'] = $localizedTableIds[self::TABLE_Page][self::VALUE_PageId];
+    }
+
+    public function localizePageWithLanguageSynchronization()
+    {
+        $GLOBALS['TCA']['pages_language_overlay']['columns']['title']['config']['behaviour']['allowLanguageSynchronization'] = true;
+        $localizedTableIds = $this->actionService->localizeRecord(self::TABLE_Page, self::VALUE_PageId, self::VALUE_LanguageId);
+        $this->recordIds['localizedPageId'] = $localizedTableIds[self::TABLE_Page][self::VALUE_PageId];
+        $this->actionService->modifyRecord(self::TABLE_Page, self::VALUE_PageId, ['title' => 'Testing #1']);
     }
 
     /**
diff --git a/typo3/sysext/core/Tests/Functional/DataHandling/Regular/Modify/ActionTest.php b/typo3/sysext/core/Tests/Functional/DataHandling/Regular/Modify/ActionTest.php
index 133a6b30a709..3959ea29eb19 100644
--- a/typo3/sysext/core/Tests/Functional/DataHandling/Regular/Modify/ActionTest.php
+++ b/typo3/sysext/core/Tests/Functional/DataHandling/Regular/Modify/ActionTest.php
@@ -363,6 +363,20 @@ class ActionTest extends \TYPO3\CMS\Core\Tests\Functional\DataHandling\Regular\A
             ->setTable(self::TABLE_Page)->setField('title')->setValues('[Translate to Dansk:] Relations'));
     }
 
+    /**
+     * @test
+     * @see DataSet/localizePageWSynchronization.csv
+     */
+    public function localizePageWithLanguageSynchronization()
+    {
+        parent::localizePageWithLanguageSynchronization();
+        $this->assertAssertionDataSet('localizePageWSynchronization');
+
+        $responseSections = $this->getFrontendResponse(self::VALUE_PageId, self::VALUE_LanguageId)->getResponseSections();
+        $this->assertThat($responseSections, $this->getRequestSectionHasRecordConstraint()
+            ->setTable(self::TABLE_Page)->setField('title')->setValues('Testing #1'));
+    }
+
     /**
      * @test
      * @see DataSet/changePageRecordSorting.csv
diff --git a/typo3/sysext/core/Tests/Functional/DataHandling/Regular/Modify/DataSet/localizePageWSynchronization.csv b/typo3/sysext/core/Tests/Functional/DataHandling/Regular/Modify/DataSet/localizePageWSynchronization.csv
new file mode 100644
index 000000000000..9e3a944b960f
--- /dev/null
+++ b/typo3/sysext/core/Tests/Functional/DataHandling/Regular/Modify/DataSet/localizePageWSynchronization.csv
@@ -0,0 +1,17 @@
+pages
+,uid,pid,sorting,deleted,t3_origuid,t3ver_wsid,t3ver_state,t3ver_stage,t3ver_oid,t3ver_move_id,title
+,1,0,256,0,0,0,0,0,0,0,FunctionalTest
+,88,1,256,0,0,0,0,0,0,0,DataHandlerTest
+,89,88,256,0,0,0,0,0,0,0,Testing #1
+,90,88,512,0,0,0,0,0,0,0,Target
+pages_language_overlay
+,uid,pid,deleted,sys_language_uid,t3ver_wsid,t3ver_state,t3ver_stage,t3ver_oid,title
+,1,89,0,1,0,0,0,0,Testing #1
+tt_content
+,uid,pid,sorting,deleted,sys_language_uid,l18n_parent,l10n_source,t3_origuid,t3ver_wsid,t3ver_state,t3ver_stage,t3ver_oid,t3ver_move_id,header
+,297,89,256,0,0,0,0,0,0,0,0,0,0,Regular Element #1
+,298,89,512,0,0,0,0,0,0,0,0,0,0,Regular Element #2
+,299,89,768,0,0,0,0,0,0,0,0,0,0,Regular Element #3
+,300,89,1024,0,1,299,299,299,0,0,0,0,0,[Translate to Dansk:] Regular Element #3
+,301,89,384,0,1,297,297,297,0,0,0,0,0,[Translate to Dansk:] Regular Element #1
+,302,89,448,0,2,297,301,301,0,0,0,0,0,[Translate to Deutsch:] [Translate to Dansk:] Regular Element #1
-- 
GitLab