[BUGFIX] getLabelsFromItemsList() returns no value 89/27689/4
authorXavier Perseguers <xavier@typo3.org>
Tue, 18 Feb 2014 13:01:31 +0000 (14:01 +0100)
committerHelmut Hummel <helmut.hummel@typo3.org>
Tue, 4 Mar 2014 17:09:50 +0000 (18:09 +0100)
Method getLabelsFromItemsList() is expected to return an empty list
when the column is populating items from a foreign_table configuration
option. Method getProcessedValue() is explicitly checking this to
retrieve the label from the corresponding foreign record if it could not
be resolved from the list of static items.

This reverts commits 5dd32b83e7085ed44713c9b97d5c2c47d820765e and
9596d4da3ac8110a76cb3eadabb9307b67971793 and moves the business logic
into method getProcessedValue().

Releases: 6.2, 6.1
Fixes: #54131
Change-Id: I15dcf0c4bcb76ecc85de8e3202b1376a24981b06
Reviewed-on: https://review.typo3.org/27689
Reviewed-by: Frans Saris
Tested-by: Frans Saris
Reviewed-by: Anja Leichsenring
Tested-by: Anja Leichsenring
Reviewed-by: Helmut Hummel
Tested-by: Helmut Hummel
typo3/sysext/backend/Classes/Utility/BackendUtility.php
typo3/sysext/backend/Tests/Unit/Utility/BackendUtilityTest.php

index 936a690..fe8dff2 100644 (file)
@@ -1863,20 +1863,14 @@ class BackendUtility {
                        if (is_array($GLOBALS['TCA'][$table]) && is_array($GLOBALS['TCA'][$table]['columns'][$column]) && is_array($GLOBALS['TCA'][$table]['columns'][$column]['config']['items'])) {
                                // Loop on all selected values
                                foreach ($values as $aValue) {
-                                       $valueFound = FALSE;
                                        foreach ($GLOBALS['TCA'][$table]['columns'][$column]['config']['items'] as $itemConfiguration) {
                                                // Loop on all available items
                                                // Keep matches and move on to next value
                                                if ($aValue == $itemConfiguration[1]) {
                                                        $labels[] = $GLOBALS['LANG']->sL($itemConfiguration[0]);
-                                                       $valueFound = TRUE;
                                                        break;
                                                }
                                        }
-                                       // no item label found then we use the plain db value
-                                       if (!$valueFound) {
-                                               $labels[] = $aValue;
-                                       }
                                }
                        }
                }
@@ -2106,6 +2100,10 @@ class BackendUtility {
                                                                $l = implode(', ', $lA);
                                                        }
                                                }
+                                               if (empty($l) && !empty($value)) {
+                                                       // Use plain database value when label is empty
+                                                       $l = $value;
+                                               }
                                        }
                                        break;
                                case 'group':
index 4954400..e2d4cf2 100644 (file)
@@ -496,13 +496,59 @@ class BackendUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        /**
         * @test
         */
-       public function getLabelsFromItemsListReturnsPlainValueIfItemIsNotFound() {
+       public function getProcessedValueReturnsLabelsForExistingValuesSolely() {
                $table = 'foobar';
                $col = 'someColumn';
                $tca = array(
                        'columns' => array(
                                'someColumn' => array(
                                        'config' => array(
+                                               'type' => 'select',
+                                               'items' => array(
+                                                       '0' => array('aFooLabel', 'foo'),
+                                                       '1' => array('aBarLabel', 'bar')
+                                               )
+                                       )
+                               )
+                       )
+               );
+               // Stub LanguageService and let sL() return the same value that came in again
+               $GLOBALS['LANG'] = $this->getMock('TYPO3\\CMS\\Lang\\LanguageService', array(), array(), '', FALSE);
+               $GLOBALS['LANG']->charSet = 'utf-8';
+               $GLOBALS['LANG']->csConvObj = $this->getMock('TYPO3\\CMS\\Core\\Charset\\CharsetConverter');
+               $GLOBALS['LANG']->expects($this->any())->method('sL')
+                       ->will($this->returnCallback(
+                               function($name) {
+                                       return $name;
+                               }
+                       ));
+               $GLOBALS['LANG']->csConvObj->expects($this->any())->method('crop')
+                       ->will($this->returnCallback(
+                               function($charset, $string, $len, $crop = '') {
+                                       return $string;
+                               }
+                       ));
+
+               $tcaBackup = $GLOBALS['TCA'][$table];
+               unset($GLOBALS['TCA'][$table]);
+               $GLOBALS['TCA'][$table] = $tca;
+               $label = $this->fixture->getProcessedValue($table, $col, 'foo,invalidKey,bar');
+               unset($GLOBALS['TCA'][$table]);
+               $GLOBALS['TCA'][$table] = $tcaBackup;
+               $this->assertEquals('aFooLabel, aBarLabel', $label);
+       }
+
+       /**
+        * @test
+        */
+       public function getProcessedValueReturnsPlainValueIfItemIsNotFound() {
+               $table = 'foobar';
+               $col = 'someColumn';
+               $tca = array(
+                       'columns' => array(
+                               'someColumn' => array(
+                                       'config' => array(
+                                               'type' => 'select',
                                                'items' => array(
                                                        '0' => array('aFooLabel', 'foo')
                                                )
@@ -512,20 +558,28 @@ class BackendUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                );
                // Stub LanguageService and let sL() return the same value that came in again
                $GLOBALS['LANG'] = $this->getMock('TYPO3\\CMS\\Lang\\LanguageService', array(), array(), '', FALSE);
+               $GLOBALS['LANG']->charSet = 'utf-8';
+               $GLOBALS['LANG']->csConvObj = $this->getMock('TYPO3\\CMS\\Core\\Charset\\CharsetConverter');
                $GLOBALS['LANG']->expects($this->any())->method('sL')
                        ->will($this->returnCallback(
                                function($name) {
                                        return $name;
                                }
                        ));
+               $GLOBALS['LANG']->csConvObj->expects($this->any())->method('crop')
+                       ->will($this->returnCallback(
+                               function($charset, $string, $len, $crop = '') {
+                                       return $string;
+                               }
+                       ));
 
                $tcaBackup = $GLOBALS['TCA'][$table];
                unset($GLOBALS['TCA'][$table]);
                $GLOBALS['TCA'][$table] = $tca;
-               $label = $this->fixture->getLabelsFromItemsList($table, $col, 'foo,something,missing');
+               $label = $this->fixture->getProcessedValue($table, $col, 'invalidKey');
                unset($GLOBALS['TCA'][$table]);
                $GLOBALS['TCA'][$table] = $tcaBackup;
-               $this->assertEquals('aFooLabel, something, missing', $label);
+               $this->assertEquals('invalidKey', $label);
        }
 
        /**