[BUGFIX] Fix FileList ordering for upper/lowercase 50/37250/8
authorAlexander Opitz <opitz.alexander@googlemail.com>
Thu, 26 Feb 2015 09:44:39 +0000 (10:44 +0100)
committerFrank Nägler <typo3@naegler.net>
Fri, 5 Jun 2015 09:36:12 +0000 (11:36 +0200)
File names which only differ in upper/lowercase writing are now sorted
stable with uppercase first.

Resolves: #65319
Releases: master, 6.2
Change-Id: Ib1b907cb8aed6364aadc4f028cfc9294b5c18884
Reviewed-on: http://review.typo3.org/37250
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Tested-by: Andreas Fernandez <typo3@scripting-base.de>
Reviewed-by: Frank Nägler <typo3@naegler.net>
Tested-by: Frank Nägler <typo3@naegler.net>
typo3/sysext/core/Classes/Utility/ResourceUtility.php
typo3/sysext/core/Tests/Unit/Utility/ResourceUtilityTest.php

index 00d62fc..9aafb90 100644 (file)
@@ -34,7 +34,7 @@ class ResourceUtility {
                if (strpos($elementA, '/') === FALSE) {
                        // first element is a file
                        if (strpos($elementB, '/') === FALSE) {
-                               $result = strnatcasecmp($elementA, $elementB);
+                               $result = self::nameCompareSortingHelper($elementA, $elementB);
                        } else {
                                // second element is a directory => always sort it first
                                $result = 1;
@@ -54,7 +54,7 @@ class ResourceUtility {
                                        $result = self::recursiveFileListSortingHelper($elementA, $elementB);
                                } else {
                                        // different directories => sort by current directories
-                                       $result = strnatcasecmp($pathPartA, $pathPartB);
+                                       $result = self::nameCompareSortingHelper($pathPartA, $pathPartB);
                                }
                        }
                }
@@ -62,4 +62,22 @@ class ResourceUtility {
                return $result;
        }
 
+       /**
+        * This is a helper method that can be used with u?sort methods to sort a list of names in natural order. With
+        * capitalized first if both equal in lowercase.
+        *
+        * @param string $elementA
+        * @param string $elementB
+        * @return int
+        */
+       static public function nameCompareSortingHelper($elementA, $elementB) {
+               $result = strnatcasecmp($elementA, $elementB);
+               if ($result === 0) {
+                       // Both are same in case insensitive so it's ok to check then now unnaturally.
+                       $result = strcmp($elementA, $elementB);
+               }
+
+               return $result;
+       }
+
 }
index c5e9fa8..c0e89db 100644 (file)
@@ -21,6 +21,9 @@ use TYPO3\CMS\Core\Utility\ResourceUtility;
  */
 class ResourceUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
 
+       /**
+        * @return array
+        */
        public function recursiveFileListSortingHelperTestDataProvider() {
                return array(
                        'normal file list' => array(
@@ -39,6 +42,10 @@ class ResourceUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                                array('alllower', 'allCAPS', 'ALLcaps', 'mIxedinanotherway', 'ALLCAPS', 'MiXeDcApItAlIzAtIoN'),
                                array('ALLCAPS', 'ALLcaps', 'allCAPS', 'alllower', 'MiXeDcApItAlIzAtIoN', 'mIxedinanotherway')
                        ),
+                       'mixed capitalization reversed' => array(
+                               array('MiXeDcApItAlIzAtIoN', 'mIxedinanotherway', 'ALLcaps', 'allCAPS', 'ALLCAPS', 'alllower'),
+                               array('ALLCAPS', 'ALLcaps', 'allCAPS', 'alllower', 'MiXeDcApItAlIzAtIoN', 'mIxedinanotherway')
+                       ),
                        'recursive list with one sublevel' => array(
                                array('fileA', 'fileB', 'anotherDir/someFile', 'someDir/someFile', 'anotherDir/anotherFile'),
                                array('anotherDir/anotherFile', 'anotherDir/someFile', 'someDir/someFile', 'fileA', 'fileB')
@@ -57,6 +64,8 @@ class ResourceUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        /**
         * @dataProvider recursiveFileListSortingHelperTestDataProvider
         * @test
+        * @param array $unsortedList
+        * @param array $expectedList
         */
        public function recursiveFileListSortingHelperCorrectlySorts($unsortedList, $expectedList) {
                $result = $unsortedList;
@@ -64,8 +73,7 @@ class ResourceUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                        $result,
                        array(ResourceUtility::class, 'recursiveFileListSortingHelper')
                );
-
-               $this->assertEquals($expectedList, $result);
+               $this->assertSame($expectedList, $result);
        }
 
 }