[BUGFIX] Ignore --linebreak-- fields in removeDuplicatesForInsertion 45/35745/2
authorChristian Ludwig <cl@viazenetti.de>
Wed, 12 Nov 2014 15:12:17 +0000 (16:12 +0100)
committerMarkus Klein <klein.t3@reelworx.at>
Fri, 2 Jan 2015 18:46:10 +0000 (19:46 +0100)
ExtensionManagementUtility::removeDuplicatesForInsertion() ignores
--linebreak-- fields when looking for duplicates.
In addition the method uses less regular expressions to
make it more performant and easier to understand.

Resolves: #57824
Releases: master, 6.2
Change-Id: Ia064abe81501ec4649dc8fb1fe613d5e5497ff9c
Reviewed-on: http://review.typo3.org/35745
Reviewed-by: Markus Klein <klein.t3@reelworx.at>
Tested-by: Markus Klein <klein.t3@reelworx.at>
typo3/sysext/core/Classes/Utility/ExtensionManagementUtility.php
typo3/sysext/core/Tests/Unit/Utility/ExtensionManagementUtilityTest.php

index 6f789ba..e7c9064 100644 (file)
@@ -644,18 +644,19 @@ class ExtensionManagementUtility {
         */
        static protected function removeDuplicatesForInsertion($insertionList, $list = '') {
                $insertionListParts = preg_split('/\\s*,\\s*/', $insertionList);
-               $insertionList = implode(', ', array_unique($insertionListParts));
-               if ($list === '') {
-                       return $insertionList;
+               $listMatches = array();
+               if ($list !== '') {
+                       preg_match_all('/(?:^|,)\\s*\\b([^;,]+)\\b[^,]*/', $list, $listMatches);
+                       $listMatches = $listMatches[1];
                }
-               // Get a list of fieldNames that are present in the list.
-               preg_match_all('/(?:^|,)\\s*\\b([^;,]+)\\b[^,]*/', $list, $listMatches);
-               // Remove the field names from the insertionlist.
-               $fieldReplacePatterns = array();
-               foreach ($listMatches[1] as $fieldName) {
-                       $fieldReplacePatterns[] = '/(?:^|,)\\s*\\b' . preg_quote($fieldName, '/') . '\\b[^,$]*/';
+
+               $cleanInsertionListParts = array();
+               foreach ($insertionListParts as $fieldName) {
+                       if ($fieldName == '--linebreak--' || (!in_array($fieldName, $cleanInsertionListParts) && !in_array($fieldName, $listMatches))) {
+                               $cleanInsertionListParts[] = $fieldName;
+                       }
                }
-               return preg_replace($fieldReplacePatterns, '', $insertionList);
+               return implode(', ', $cleanInsertionListParts);
        }
 
        /**
index 5712da8..db13914 100644 (file)
@@ -116,6 +116,9 @@ class ExtensionManagementUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
                                '  }' .
                                '  public static function emitTcaIsBeingBuiltSignal(array $tca) {' .
                                '  }' .
+                               '  public static function removeDuplicatesForInsertion($insertionList, $list = \'\') {' .
+                               '    return parent::removeDuplicatesForInsertion($insertionList, $list);' .
+                               '  }' .
                                '}'
                        );
                }
@@ -485,6 +488,41 @@ class ExtensionManagementUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
        }
 
        /**
+        * @return array
+        */
+       public function removeDuplicatesForInsertionRemovesDuplicatesDataProvider() {
+               return array(
+                       'Simple' => array(
+                               'field_b, field_d, field_c',
+                               'field_a, field_b, field_c',
+                               'field_d'
+                       ),
+                       'with linebreaks' => array(
+                               'field_b, --linebreak--, field_d, --linebreak--, field_c',
+                               'field_a, field_b, field_c',
+                               '--linebreak--, field_d, --linebreak--'
+                       ),
+                       'with linebreaks in list and insertion list' => array(
+                               'field_b, --linebreak--, field_d, --linebreak--, field_c',
+                               'field_a, field_b, --linebreak--, field_c',
+                               '--linebreak--, field_d, --linebreak--'
+                       ),
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider removeDuplicatesForInsertionRemovesDuplicatesDataProvider
+        * @param $insertionList
+        * @param $list
+        * @param $expected
+        */
+       public function removeDuplicatesForInsertionRemovesDuplicates($insertionList, $list, $expected) {
+               $result = ExtensionManagementUtilityAccessibleProxy::removeDuplicatesForInsertion($insertionList, $list);
+               $this->assertSame($expected, $result);
+       }
+
+       /**
         * Tests whether fields can be added to all palettes of a regular field before existing ones.
         *
         * @test