[TASK] Functional tests for LocalizationController 70/48470/2
authorAndreas Fernandez <a.fernandez@scripting-base.de>
Thu, 17 Mar 2016 14:21:15 +0000 (15:21 +0100)
committerNicole Cordes <typo3@cordes.co>
Tue, 7 Jun 2016 07:07:49 +0000 (09:07 +0200)
The LocalizationController used by the localization wizard is
now covered by some functional tests.
For better testability, the DataHandler processing in the
controller is moved into its own method.

Resolves: #75160
Related: #73617
Releases: master, 7.6
Change-Id: I3275f51d3dfec55ace3d8dbe7130c3f18b963341
Reviewed-on: https://review.typo3.org/48470
Reviewed-by: Nicole Cordes <typo3@cordes.co>
Tested-by: Nicole Cordes <typo3@cordes.co>
typo3/sysext/backend/Classes/Controller/Page/LocalizationController.php
typo3/sysext/backend/Tests/Functional/Controller/Page/Fixtures/tt_content-danish-language.xml [new file with mode: 0644]
typo3/sysext/backend/Tests/Functional/Controller/Page/Fixtures/tt_content-default-language.xml [new file with mode: 0644]
typo3/sysext/backend/Tests/Functional/Controller/Page/LocalizationControllerTest.php [new file with mode: 0644]
typo3/sysext/backend/Tests/Functional/Fixtures/pages.xml [new file with mode: 0644]

index 35f9a32..551e2aa 100644 (file)
@@ -191,6 +191,19 @@ class LocalizationController
             return $response;
         }
 
+        $this->process($params);
+
+        $response->getBody()->write(json_encode([]));
+        return $response;
+    }
+
+    /**
+     * Processes the localization actions
+     *
+     * @param array $params
+     */
+    protected function process($params)
+    {
         $pageId = (int)$params['pageId'];
         $srcLanguageId = (int)$params['srcLanguageId'];
         $destLanguageId = (int)$params['destLanguageId'];
@@ -251,9 +264,6 @@ class LocalizationController
         $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
         $dataHandler->start([], $cmd);
         $dataHandler->process_cmdmap();
-
-        $response->getBody()->write(json_encode([]));
-        return $response;
     }
 
     /**
diff --git a/typo3/sysext/backend/Tests/Functional/Controller/Page/Fixtures/tt_content-danish-language.xml b/typo3/sysext/backend/Tests/Functional/Controller/Page/Fixtures/tt_content-danish-language.xml
new file mode 100644 (file)
index 0000000..80dc69c
--- /dev/null
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="utf-8"?>
+<dataset>
+       <tt_content>
+               <uid>4</uid>
+               <pid>1</pid>
+               <sys_language_uid>1</sys_language_uid>
+               <l18n_parent>1</l18n_parent>
+               <deleted>0</deleted>
+               <t3ver_oid>0</t3ver_oid>
+               <t3ver_wsid>0</t3ver_wsid>
+               <header>Test indhold 1</header>
+       </tt_content>
+       <tt_content>
+               <uid>5</uid>
+               <pid>1</pid>
+               <sys_language_uid>1</sys_language_uid>
+               <l18n_parent>2</l18n_parent>
+               <deleted>0</deleted>
+               <t3ver_oid>0</t3ver_oid>
+               <t3ver_wsid>0</t3ver_wsid>
+               <header>Test indhold 2</header>
+       </tt_content>
+       <tt_content>
+               <uid>6</uid>
+               <pid>1</pid>
+               <sys_language_uid>1</sys_language_uid>
+               <l18n_parent>3</l18n_parent>
+               <deleted>0</deleted>
+               <t3ver_oid>0</t3ver_oid>
+               <t3ver_wsid>0</t3ver_wsid>
+               <header>Test indhold 3</header>
+       </tt_content>
+</dataset>
\ No newline at end of file
diff --git a/typo3/sysext/backend/Tests/Functional/Controller/Page/Fixtures/tt_content-default-language.xml b/typo3/sysext/backend/Tests/Functional/Controller/Page/Fixtures/tt_content-default-language.xml
new file mode 100644 (file)
index 0000000..6471b84
--- /dev/null
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8"?>
+<dataset>
+       <tt_content>
+               <uid>1</uid>
+               <pid>1</pid>
+               <header>Test content 1</header>
+               <deleted>0</deleted>
+               <t3ver_oid>0</t3ver_oid>
+               <t3ver_wsid>0</t3ver_wsid>
+       </tt_content>
+       <tt_content>
+               <uid>2</uid>
+               <pid>1</pid>
+               <header>Test content 2</header>
+               <deleted>0</deleted>
+               <t3ver_oid>0</t3ver_oid>
+               <t3ver_wsid>0</t3ver_wsid>
+       </tt_content>
+       <tt_content>
+               <uid>3</uid>
+               <pid>1</pid>
+               <header>Test content 3</header>
+               <deleted>0</deleted>
+               <t3ver_oid>0</t3ver_oid>
+               <t3ver_wsid>0</t3ver_wsid>
+       </tt_content>
+</dataset>
\ No newline at end of file
diff --git a/typo3/sysext/backend/Tests/Functional/Controller/Page/LocalizationControllerTest.php b/typo3/sysext/backend/Tests/Functional/Controller/Page/LocalizationControllerTest.php
new file mode 100644 (file)
index 0000000..98b4c9b
--- /dev/null
@@ -0,0 +1,276 @@
+<?php
+namespace TYPO3\CMS\Backend\Tests\Functional\Controller\Page;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Backend\Controller\Page\LocalizationController;
+use TYPO3\CMS\Core\Core\Bootstrap;
+use TYPO3\CMS\Core\DataHandling\DataHandler;
+use TYPO3\CMS\Core\Tests\FunctionalTestCase;
+
+/**
+ * Test case for TYPO3\CMS\Backend\Controller\Page\LocalizationController
+ */
+class LocalizationControllerTest extends FunctionalTestCase
+{
+    /**
+     * @var LocalizationController
+     */
+    protected $subject;
+
+    /**
+     * Sets up this test case.
+     *
+     * @return void
+     */
+    protected function setUp()
+    {
+        parent::setUp();
+
+        $this->setUpBackendUserFromFixture(1);
+        Bootstrap::getInstance()->initializeLanguageObject();
+
+        $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/backend/Tests/Functional/Fixtures/pages.xml');
+        $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/sys_language.xml');
+        $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/backend/Tests/Functional/Controller/Page/Fixtures/tt_content-default-language.xml');
+
+        $this->subject = new LocalizationController();
+    }
+
+    /**
+     * @test
+     */
+    public function recordsGetTranslatedFromDefaultLanguage()
+    {
+        $params = [
+            'pageId' => 1,
+            'srcLanguageId' => 0,
+            'destLanguageId' => 1,
+            'uidList' => [1, 2, 3],
+            'action' => LocalizationController::ACTION_LOCALIZE,
+        ];
+        $this->callInaccessibleMethod($this->subject, 'process', $params);
+
+        $expectedResults = [
+            [
+                'pid' => '1',
+                'sys_language_uid' => '1',
+                'l18n_parent' => '1',
+                'header' => '[Translate to Dansk:] Test content 1',
+            ],
+            [
+                'pid' => '1',
+                'sys_language_uid' => '1',
+                'l18n_parent' => '2',
+                'header' => '[Translate to Dansk:] Test content 2',
+            ],
+            [
+                'pid' => '1',
+                'sys_language_uid' => '1',
+                'l18n_parent' => '3',
+                'header' => '[Translate to Dansk:] Test content 3',
+            ],
+        ];
+        $results = $this->getDatabaseConnection()->exec_SELECTgetRows('pid, sys_language_uid, l18n_parent, header', 'tt_content', 'pid = 1 AND sys_language_uid = 1');
+        $this->assertSame($expectedResults, $results);
+    }
+
+    /**
+     * @test
+     */
+    public function recordsGetTranslatedFromDifferentTranslation()
+    {
+        $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/backend/Tests/Functional/Controller/Page/Fixtures/tt_content-danish-language.xml');
+
+        $params = [
+            'pageId' => 1,
+            'srcLanguageId' => 1,
+            'destLanguageId' => 2,
+            'uidList' => [4, 5, 6], // uids of tt_content-danish-language
+            'action' => LocalizationController::ACTION_LOCALIZE,
+        ];
+        $this->callInaccessibleMethod($this->subject, 'process', $params);
+
+        $expectedResults = [
+            [
+                'pid' => '1',
+                'sys_language_uid' => '2',
+                'l18n_parent' => '1',
+                'header' => 'Test indhold 1 (copy 1)',
+            ],
+            [
+                'pid' => '1',
+                'sys_language_uid' => '2',
+                'l18n_parent' => '2',
+                'header' => 'Test indhold 2 (copy 1)',
+            ],
+            [
+                'pid' => '1',
+                'sys_language_uid' => '2',
+                'l18n_parent' => '3',
+                'header' => 'Test indhold 3 (copy 1)',
+            ],
+        ];
+        $results = $this->getDatabaseConnection()->exec_SELECTgetRows('pid, sys_language_uid, l18n_parent, header', 'tt_content', 'pid = 1 AND sys_language_uid = 2');
+        $this->assertSame($expectedResults, $results);
+    }
+
+    /**
+     * @test
+     */
+    public function recordsGetCopiedFromDefaultLanguage()
+    {
+        $params = [
+            'pageId' => 1,
+            'srcLanguageId' => 0,
+            'destLanguageId' => 2,
+            'uidList' => [1, 2, 3],
+            'action' => LocalizationController::ACTION_COPY,
+        ];
+        $this->callInaccessibleMethod($this->subject, 'process', $params);
+
+        $expectedResults = [
+            [
+                'pid' => '1',
+                'sys_language_uid' => '2',
+                'l18n_parent' => '0',
+                'header' => 'Test content 1 (copy 1)',
+            ],
+            [
+                'pid' => '1',
+                'sys_language_uid' => '2',
+                'l18n_parent' => '0',
+                'header' => 'Test content 2 (copy 1)',
+            ],
+            [
+                'pid' => '1',
+                'sys_language_uid' => '2',
+                'l18n_parent' => '0',
+                'header' => 'Test content 3 (copy 1)',
+            ],
+        ];
+        $results = $this->getDatabaseConnection()->exec_SELECTgetRows('pid, sys_language_uid, l18n_parent, header', 'tt_content', 'pid = 1 AND sys_language_uid = 2');
+        $this->assertSame($expectedResults, $results);
+    }
+
+    /**
+     * @test
+     */
+    public function recordsGetCopiedFromAnotherLanguage()
+    {
+        $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/backend/Tests/Functional/Controller/Page/Fixtures/tt_content-danish-language.xml');
+
+        $params = [
+            'pageId' => 1,
+            'srcLanguageId' => 1,
+            'destLanguageId' => 2,
+            'uidList' => [4, 5, 6], // uids of tt_content-danish-language
+            'action' => LocalizationController::ACTION_COPY,
+        ];
+        $this->callInaccessibleMethod($this->subject, 'process', $params);
+
+        $expectedResults = [
+            [
+                'pid' => '1',
+                'sys_language_uid' => '2',
+                'l18n_parent' => '0',
+                'header' => 'Test indhold 1 (copy 1)',
+            ],
+            [
+                'pid' => '1',
+                'sys_language_uid' => '2',
+                'l18n_parent' => '0',
+                'header' => 'Test indhold 2 (copy 1)',
+            ],
+            [
+                'pid' => '1',
+                'sys_language_uid' => '2',
+                'l18n_parent' => '0',
+                'header' => 'Test indhold 3 (copy 1)',
+            ],
+        ];
+        $results = $this->getDatabaseConnection()->exec_SELECTgetRows('pid, sys_language_uid, l18n_parent, header', 'tt_content', 'pid = 1 AND sys_language_uid = 2');
+        $this->assertSame($expectedResults, $results);
+    }
+
+    /**
+     * @test
+     */
+    public function copyingNewContentFromLanguageIntoExistingLocalizationHasSameOrdering()
+    {
+        $params = [
+            'pageId' => 1,
+            'srcLanguageId' => 0,
+            'destLanguageId' => 1,
+            'uidList' => [1, 2, 3],
+            'action' => LocalizationController::ACTION_COPY,
+        ];
+        $this->callInaccessibleMethod($this->subject, 'process', $params);
+
+        // Create another content element in default language
+        $data = [
+            'tt_content' => [
+                'NEW123456' => [
+                    'sys_language_uid' => 0,
+                    'header' => 'Test content 2.5',
+                    'pid' => -2,
+                ],
+            ],
+        ];
+        $dataHandler = new DataHandler();
+        $dataHandler->start($data, []);
+        $dataHandler->process_datamap();
+        $dataHandler->process_cmdmap();
+        $newContentElementUid = $dataHandler->substNEWwithIDs['NEW123456'];
+
+        // Copy the new content element
+        $params = [
+            'pageId' => 1,
+            'srcLanguageId' => 0,
+            'destLanguageId' => 1,
+            'uidList' => [$newContentElementUid],
+            'action' => LocalizationController::ACTION_COPY,
+        ];
+        $this->callInaccessibleMethod($this->subject, 'process', $params);
+
+        $expectedResults = [
+            [
+                'pid' => '1',
+                'sys_language_uid' => '1',
+                'l18n_parent' => '0',
+                'header' => 'Test content 1 (copy 1)',
+            ],
+            [
+                'pid' => '1',
+                'sys_language_uid' => '1',
+                'l18n_parent' => '0',
+                'header' => 'Test content 2 (copy 1)',
+            ],
+            [
+                'pid' => '1',
+                'sys_language_uid' => '1',
+                'l18n_parent' => '0',
+                'header' => 'Test content 2.5 (copy 1)',
+            ],
+            [
+                'pid' => '1',
+                'sys_language_uid' => '1',
+                'l18n_parent' => '0',
+                'header' => 'Test content 3 (copy 1)',
+            ],
+        ];
+        $results = $this->getDatabaseConnection()->exec_SELECTgetRows('pid, sys_language_uid, l18n_parent, header', 'tt_content', 'pid = 1 AND sys_language_uid = 1', '', 'sorting ASC');
+        $this->assertSame($expectedResults, $results);
+    }
+}
\ No newline at end of file
diff --git a/typo3/sysext/backend/Tests/Functional/Fixtures/pages.xml b/typo3/sysext/backend/Tests/Functional/Fixtures/pages.xml
new file mode 100644 (file)
index 0000000..7251de6
--- /dev/null
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8"?>
+<dataset>
+       <pages>
+               <uid>1</uid>
+               <pid>0</pid>
+               <title>Localization</title>
+               <deleted>0</deleted>
+               <perms_everybody>15</perms_everybody>
+       </pages>
+</dataset>
\ No newline at end of file