[BUGFIX] l10n_mode for "pages" table and group fields. 14/12714/14
authorJohannes Feustel <s@feustel.eu>
Thu, 11 Apr 2013 10:12:46 +0000 (12:12 +0200)
committerMarkus Klein <klein.t3@mfc-linz.at>
Tue, 19 Nov 2013 22:13:41 +0000 (23:13 +0100)
This patch respects the l10modes for pages, and mergeIfNotBlank for
type "group" fields.

Change-Id: I18a4caffc5761f91dae4ae0cf175ccd51ffe8c29
Fixes: #38766
Releases: 6.2, 6.1, 6.0
Reviewed-on: https://review.typo3.org/12714
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
Reviewed-by: Markus Klein
Tested-by: Markus Klein
typo3/sysext/frontend/Classes/Page/PageRepository.php
typo3/sysext/frontend/Tests/Unit/Page/PageRepositoryTest.php

index b828cbc..4ce6d4f 100644 (file)
@@ -329,8 +329,17 @@ class PageRepository {
                }
                // Create output:
                if (is_array($pageInput)) {
-                       // If the input was an array, simply overlay the newfound array and return...
-                       return is_array($row) ? array_merge($pageInput, $row) : $pageInput;
+                       if (is_array($row)) {
+                               // Overwrite the original field with the overlay
+                               foreach ($row as $fieldName => $fieldValue) {
+                                       if ($fieldName !== 'uid' && $fieldName !== 'pid') {
+                                               if ($this->shouldFieldBeOverlaid('pages_language_overlay', $fieldName, $fieldValue)) {
+                                                       $pageInput[$fieldName] = $fieldValue;
+                                               }
+                                       }
+                               }
+                       }
+                       return $pageInput;
                } else {
                        // Always an array in return
                        return is_array($row) ? $row : array();
@@ -383,10 +392,7 @@ class PageRepository {
                                                                }
                                                                foreach ($row as $fN => $fV) {
                                                                        if ($fN != 'uid' && $fN != 'pid' && isset($olrow[$fN])) {
-                                                                               if (
-                                                                                       $GLOBALS['TCA'][$table]['columns'][$fN]['l10n_mode'] != 'exclude'
-                                                                                       && ($GLOBALS['TCA'][$table]['columns'][$fN]['l10n_mode'] != 'mergeIfNotBlank' || strcmp(trim($olrow[$fN]), ''))
-                                                                               ) {
+                                                                               if ($this->shouldFieldBeOverlaid($table, $fN, $olrow[$fN])) {
                                                                                        $row[$fN] = $olrow[$fN];
                                                                                }
                                                                        } elseif ($fN == 'uid') {
@@ -1221,4 +1227,36 @@ class PageRepository {
                return $ws['_ACCESS'] != '';
        }
 
+       /**
+        * Determine if a field needs an overlay
+        *
+        * @param string $table TCA tablename
+        * @param string $field TCA fieldname
+        * @param mixed $value Current value of the field
+        * @return boolean Returns TRUE if a given record field needs to be overlaid
+        */
+       protected function shouldFieldBeOverlaid($table, $field, $value) {
+               $l10n_mode = isset($GLOBALS['TCA'][$table]['columns'][$field]['l10n_mode'])
+                       ? $GLOBALS['TCA'][$table]['columns'][$field]['l10n_mode']
+                       : '';
+
+               $shouldFieldBeOverlaid = TRUE;
+
+               if ($l10n_mode === 'exclude') {
+                       $shouldFieldBeOverlaid = FALSE;
+               } elseif ($l10n_mode === 'mergeIfNotBlank') {
+                       $checkValue = $value;
+
+                       // 0 values are considered blank when coming from a group field
+                       if (empty($value) && $GLOBALS['TCA'][$table]['columns'][$field]['config']['type'] === 'group') {
+                               $checkValue = '';
+                       }
+
+                       if (trim($checkValue) === '') {
+                               $shouldFieldBeOverlaid = FALSE;
+                       }
+               }
+
+               return $shouldFieldBeOverlaid;
+       }
 }
index 6cc3432..5e9f4c3 100644 (file)
@@ -37,7 +37,7 @@ class PageRepositoryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        protected $typo3DbBackup;
 
        /**
-        * @var \TYPO3\CMS\Frontend\Page\PageRepository
+        * @var \TYPO3\CMS\Frontend\Page\PageRepository|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
         */
        protected $pageSelectObject;
 
@@ -47,7 +47,7 @@ class PageRepositoryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        public function setUp() {
                $this->typo3DbBackup = $GLOBALS['TYPO3_DB'];
                $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array('exec_SELECTquery', 'sql_fetch_assoc', 'sql_free_result'));
-               $this->pageSelectObject = $this->getMock('TYPO3\\CMS\\Frontend\\Page\\PageRepository', array('getMultipleGroupsWhereClause'));
+               $this->pageSelectObject = $this->getAccessibleMock('TYPO3\\CMS\\Frontend\\Page\\PageRepository', array('getMultipleGroupsWhereClause'));
                $this->pageSelectObject->expects($this->any())->method('getMultipleGroupsWhereClause')->will($this->returnValue(' AND 1=1'));
        }
 
@@ -56,6 +56,7 @@ class PageRepositoryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         */
        public function tearDown() {
                $GLOBALS['TYPO3_DB'] = $this->typo3DbBackup;
+               unset($this->pageSelectObject);
        }
 
        /**
@@ -120,9 +121,79 @@ class PageRepositoryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                )));
        }
 
+       /////////////////////////////////////////
+       // Tests concerning shouldFieldBeOverlaid
+       /////////////////////////////////////////
+       /**
+        * @test
+        * @dataProvider getShouldFieldBeOverlaidData
+        */
+       public function shouldFieldBeOverlaid($field, $table, $value, $expected, $comment = '') {
+               $GLOBALS['TCA']['fake_table']['columns'] = array(
+                       'exclude' => array(
+                               'l10n_mode' => 'exclude',
+                               'config' => array('type' => 'input'),
+                       ),
+                       'mergeIfNotBlank' => array(
+                               'l10n_mode' => 'mergeIfNotBlank',
+                               'config' => array('type' => 'input'),
+                       ),
+                       'mergeIfNotBlank_group' => array(
+                               'l10n_mode' => 'mergeIfNotBlank',
+                               'config' => array('type' => 'group'),
+                       ),
+                       'default' => array(
+                               // no l10n_mode set
+                               'config' => array('type' => 'input'),
+                       ),
+                       'noCopy' => array(
+                               'l10n_mode' => 'noCopy',
+                               'config' => array('type' => 'input'),
+                       ),
+                       'prefixLangTitle' => array(
+                               'l10n_mode' => 'prefixLangTitle',
+                               'config' => array('type' => 'input'),
+                       ),
+               );
+
+               $result = $this->pageSelectObject->_call('shouldFieldBeOverlaid', $table, $field, $value);
+               unset($GLOBALS['TCA']['fake_table']);
+
+               $this->assertSame($expected, $result, $comment);
+       }
+
+       /**
+        * Data provider for shouldFieldBeOverlaid
+        */
+       public function getShouldFieldBeOverlaidData() {
+               return array(
+                       array('default',               'fake_table', 'foobar', TRUE,  'default is to merge non-empty string'),
+                       array('default',               'fake_table', '',       TRUE,  'default is to merge empty string'),
+
+                       array('exclude',               'fake_table', '',       FALSE, 'exclude field with empty string'),
+                       array('exclude',               'fake_table', 'foobar', FALSE, 'exclude field with non-empty string'),
+
+                       array('mergeIfNotBlank',       'fake_table', '',       FALSE, 'mergeIfNotBlank is not merged with empty string'),
+                       array('mergeIfNotBlank',       'fake_table', 0,        TRUE,  'mergeIfNotBlank is merged with 0'),
+                       array('mergeIfNotBlank',       'fake_table', '0',      TRUE,  'mergeIfNotBlank is merged with "0"'),
+                       array('mergeIfNotBlank',       'fake_table', 'foobar', TRUE,  'mergeIfNotBlank is merged with non-empty string'),
+
+                       array('mergeIfNotBlank_group', 'fake_table', '',       FALSE, 'mergeIfNotBlank on group is not merged empty string'),
+                       array('mergeIfNotBlank_group', 'fake_table', 0,        FALSE, 'mergeIfNotBlank on group is not merged with 0'),
+                       array('mergeIfNotBlank_group', 'fake_table', '0',      FALSE, 'mergeIfNotBlank on group is not merged with "0"'),
+                       array('mergeIfNotBlank_group', 'fake_table', 'foobar', TRUE,  'mergeIfNotBlank on group is merged with non-empty string'),
+
+                       array('noCopy',                'fake_table', 'foobar', TRUE,  'noCopy is merged with non-empty string'),
+                       array('noCopy',                'fake_table', '',       TRUE,  'noCopy is merged with empty string'),
+
+                       array('prefixLangTitle',       'fake_table', 'foobar', TRUE,  'prefixLangTitle is merged with non-empty string'),
+                       array('prefixLangTitle',       'fake_table', '',       TRUE,  'prefixLangTitle is merged with empty string'),
+               );
+       }
+
+       ////////////////////////////////
+       // Tests concerning workspaces
        ////////////////////////////////
-// Tests concerning workspaces
-////////////////////////////////
 
        /**
         * @test
@@ -236,4 +307,4 @@ class PageRepositoryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        }
 
 
-}
\ No newline at end of file
+}