[BUGFIX] Fix regression in GeneralUtility::rmFromList() 90/36990/3
authorMarkus Klein <klein.t3@reelworx.at>
Tue, 17 Feb 2015 12:55:20 +0000 (13:55 +0100)
committerMarkus Klein <klein.t3@reelworx.at>
Wed, 18 Feb 2015 10:59:17 +0000 (11:59 +0100)
Removing multiple occurrences of the same element now works again.

Resolves: #65123
Releases: master, 6.2
Change-Id: I3e973f019b68ebe19cee0e01f20b9e51e6a7ae28
Reviewed-on: http://review.typo3.org/36990
Reviewed-by: Markus Klein <klein.t3@reelworx.at>
Tested-by: Markus Klein <klein.t3@reelworx.at>
typo3/sysext/core/Classes/Utility/GeneralUtility.php
typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php

index 50be2ef..8704a14 100755 (executable)
@@ -723,19 +723,21 @@ class GeneralUtility {
        /**
         * Removes an item from a comma-separated list of items.
         *
+        * If $element contains a comma, the behaviour of this method is undefined.
+        * Empty elements in the list are preserved.
+        *
         * @param string $element Element to remove
         * @param string $list Comma-separated list of items (string)
         * @return string New comma-separated list of items
         */
        static public function rmFromList($element, $list) {
-               return trim(
-                       str_replace(
-                               ',' . $element . ',',
-                               ',',
-                               ',' . $list . ','
-                       ),
-                       ','
-               );
+               $items = explode(',', $list);
+               foreach ($items as $k => $v) {
+                       if ($v == $element) {
+                               unset($items[$k]);
+                       }
+               }
+               return implode(',', $items);
        }
 
        /**
index 137df69..4180e23 100644 (file)
@@ -586,9 +586,16 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                        'Element at end of list' => array('one,two,removeme', 'one,two'),
                        'One item list' => array('removeme', ''),
                        'Element not contained in list' => array('one,two,three', 'one,two,three'),
+                       'Empty element survives' => array('one,,three,,removeme', 'one,,three,'),
+                       'Empty element survives at start' => array(',removeme,three,removeme', ',three'),
+                       'Empty element survives at end' => array('removeme,three,removeme,', 'three,'),
                        'Empty list' => array('', ''),
-                       'List with leading comma is trimmed afterwards' => array(',one,two,removeme', 'one,two'),
-                       'List with trailing comma is trimmed afterwards' => array('one,two,removeme,', 'one,two'),
+                       'List contains removeme multiple times' => array('removeme,notme,removeme,removeme', 'notme'),
+                       'List contains removeme multiple times nothing else' => array('removeme,removeme,removeme', ''),
+                       'List contains removeme multiple times nothing else 2x' => array('removeme,removeme', ''),
+                       'List contains removeme multiple times nothing else 3x' => array('removeme,removeme,removeme', ''),
+                       'List contains removeme multiple times nothing else 4x' => array('removeme,removeme,removeme,removeme', ''),
+                       'List contains removeme multiple times nothing else 5x' => array('removeme,removeme,removeme,removeme,removeme', ''),
                );
        }