From 9eaa5a0c96d58bed00aacbf33e5d27c4d5e2e014 Mon Sep 17 00:00:00 2001 From: Georg Ringer <georg.ringer@gmail.com> Date: Tue, 11 Jun 2024 22:47:04 +0200 Subject: [PATCH] [BUGFIX] Allow integers as sort value in ArrayUtility::sortArraysByKey Cast the value to strings before using strcasecmp for sorting. This allows to use integers for sorting the array. Resolves: #102573 Releases: main, 12.4 Change-Id: I2dea688e6e5a24e7292a1fede2ca294f8bbd3347 Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/84631 Reviewed-by: Garvin Hicking <gh@faktor-e.de> Reviewed-by: Benni Mack <benni@typo3.org> Tested-by: Georg Ringer <georg.ringer@gmail.com> Tested-by: Benni Mack <benni@typo3.org> Tested-by: Garvin Hicking <gh@faktor-e.de> Reviewed-by: Georg Ringer <georg.ringer@gmail.com> Tested-by: core-ci <typo3@b13.com> --- .../core/Classes/Utility/ArrayUtility.php | 10 +- .../Tests/Unit/Utility/ArrayUtilityTest.php | 118 ++++++++++++++++++ 2 files changed, 126 insertions(+), 2 deletions(-) diff --git a/typo3/sysext/core/Classes/Utility/ArrayUtility.php b/typo3/sysext/core/Classes/Utility/ArrayUtility.php index 4e1bf28a2861..0f347206b170 100644 --- a/typo3/sysext/core/Classes/Utility/ArrayUtility.php +++ b/typo3/sysext/core/Classes/Utility/ArrayUtility.php @@ -361,10 +361,16 @@ class ArrayUtility return $arrays; } $sortResult = uasort($arrays, static function (array $a, array $b) use ($key, $ascending) { - if (!isset($a[$key]) || !isset($b[$key])) { + if (!isset($a[$key], $b[$key])) { throw new \RuntimeException('The specified sorting key "' . $key . '" is not available in the given array.', 1373727309); } - return $ascending ? strcasecmp($a[$key], $b[$key]) : strcasecmp($b[$key], $a[$key]); + if (!is_scalar($a[$key])) { + throw new \RuntimeException(sprintf('The specified sorting key "%s" is not a scalar value, given "%s".', $key, gettype($a[$key])), 1373727310); + } + if (!is_scalar($b[$key])) { + throw new \RuntimeException(sprintf('The specified sorting key "%s" is not a scalar value, given "%s".', $key, gettype($b[$key])), 1373727311); + } + return $ascending ? strcasecmp((string)$a[$key], (string)$b[$key]) : strcasecmp((string)$b[$key], (string)$a[$key]); }); if (!$sortResult) { throw new \RuntimeException('The function uasort() failed for unknown reasons.', 1373727329); diff --git a/typo3/sysext/core/Tests/Unit/Utility/ArrayUtilityTest.php b/typo3/sysext/core/Tests/Unit/Utility/ArrayUtilityTest.php index cf9ddea1e563..9c5890b1a1fc 100644 --- a/typo3/sysext/core/Tests/Unit/Utility/ArrayUtilityTest.php +++ b/typo3/sysext/core/Tests/Unit/Utility/ArrayUtilityTest.php @@ -1042,6 +1042,82 @@ final class ArrayUtilityTest extends UnitTestCase ], ], ], + 'order by integers as string' => [ + [ + 0 => [ + 'uid' => '23', + 'title' => 'b', + 'dummy' => 4, + ], + 1 => [ + 'uid' => '22', + 'title' => 'c', + 'dummy' => 2, + ], + 2 => [ + 'uid' => '24', + 'title' => 'a', + 'dummy' => 3, + ], + ], + 'uid', + true, + [ + 1 => [ + 'uid' => '22', + 'title' => 'c', + 'dummy' => 2, + ], + 0 => [ + 'uid' => '23', + 'title' => 'b', + 'dummy' => 4, + ], + 2 => [ + 'uid' => '24', + 'title' => 'a', + 'dummy' => 3, + ], + ], + ], + 'order by integers' => [ + [ + 0 => [ + 'uid' => 23, + 'title' => 'b', + 'dummy' => 4, + ], + 1 => [ + 'uid' => 22, + 'title' => 'c', + 'dummy' => 2, + ], + 2 => [ + 'uid' => 24, + 'title' => 'a', + 'dummy' => 3, + ], + ], + 'uid', + true, + [ + 1 => [ + 'uid' => 22, + 'title' => 'c', + 'dummy' => 2, + ], + 0 => [ + 'uid' => 23, + 'title' => 'b', + 'dummy' => 4, + ], + 2 => [ + 'uid' => 24, + 'title' => 'a', + 'dummy' => 3, + ], + ], + ], ]; } @@ -1067,6 +1143,48 @@ final class ArrayUtilityTest extends UnitTestCase ArrayUtility::sortArraysByKey([['a'], ['a']], 'dummy'); } + #[Test] + public function sortArraysByKeyThrowsExceptionForNonScalarKeyA(): void + { + $this->expectException(\RuntimeException::class); + $this->expectExceptionCode(1373727310); + + ArrayUtility::sortArraysByKey( + [ + [ + 'uid' => '23', + 'value' => new \stdClass(), + ], + 22 => [ + 'uid' => '22', + 'value' => 123, + ], + ], + 'value' + ); + } + + #[Test] + public function sortArraysByKeyThrowsExceptionForNonScalarKeyB(): void + { + $this->expectException(\RuntimeException::class); + $this->expectExceptionCode(1373727311); + + ArrayUtility::sortArraysByKey( + [ + [ + 'uid' => '23', + 'value' => 123, + ], + 22 => [ + 'uid' => '22', + 'value' => [], + ], + ], + 'value' + ); + } + /////////////////////// // Tests concerning arrayExport /////////////////////// -- GitLab