From dab4b85596f1ce6fbd8ac9891c1226cb1f0387b1 Mon Sep 17 00:00:00 2001 From: mscherer Date: Fri, 26 Aug 2016 14:32:21 +0200 Subject: [PATCH 1/3] Backport Hash::sort() support for type locale. --- lib/Cake/Test/Case/Utility/HashTest.php | 28 +++++++++++++++++++++++++ lib/Cake/Utility/Hash.php | 3 +++ 2 files changed, 31 insertions(+) diff --git a/lib/Cake/Test/Case/Utility/HashTest.php b/lib/Cake/Test/Case/Utility/HashTest.php index d92c7614f..5af077282 100644 --- a/lib/Cake/Test/Case/Utility/HashTest.php +++ b/lib/Cake/Test/Case/Utility/HashTest.php @@ -1360,6 +1360,34 @@ class HashTest extends CakeTestCase { $this->assertEquals($sorted, $b); } +/** + * Test sort() with locale option. + * + * @return void + */ + public function testSortLocale() { + // get the current locale + $oldLocale = setlocale(LC_COLLATE, '0'); + // the de_DE.utf8 locale must be installed on the system where the test is performed + setlocale(LC_COLLATE, 'de_DE.utf8'); + $items = array( + array('Item' => array('entry' => 'Übergabe')), + array('Item' => array('entry' => 'Ostfriesland')), + array('Item' => array('entry' => 'Äpfel')), + array('Item' => array('entry' => 'Apfel')), + ); + $result = Hash::sort($items, '{n}.Item.entry', 'asc', 'locale'); + $expected = array( + array('Item' => array('entry' => 'Apfel')), + array('Item' => array('entry' => 'Äpfel')), + array('Item' => array('entry' => 'Ostfriesland')), + array('Item' => array('entry' => 'Übergabe')), + ); + $this->assertEquals($expected, $result); + // change to the original locale + setlocale(LC_COLLATE, $oldLocale); + } + /** * test sorting with out of order keys. * diff --git a/lib/Cake/Utility/Hash.php b/lib/Cake/Utility/Hash.php index 3669e1a64..1c6e0bf27 100644 --- a/lib/Cake/Utility/Hash.php +++ b/lib/Cake/Utility/Hash.php @@ -836,6 +836,7 @@ class Hash { * - `regular` For regular sorting (don't change types) * - `numeric` Compare values numerically * - `string` Compare values as strings + * - `locale` Compare items as strings, based on the current locale * - `natural` Compare items as strings using "natural ordering" in a human friendly way. * Will sort foo10 below foo2 as an example. Requires PHP 5.4 or greater or it will fallback to 'regular' * @@ -904,6 +905,8 @@ class Hash { $type = SORT_STRING; } elseif ($type === 'natural') { $type = SORT_NATURAL; + } elseif ($type === 'locale') { + $type = SORT_LOCALE_STRING; } else { $type = SORT_REGULAR; } From 03df288e78e7ae334082e3b007ae95890a7cd9ab Mon Sep 17 00:00:00 2001 From: mscherer Date: Fri, 26 Aug 2016 14:45:59 +0200 Subject: [PATCH 2/3] Re-add spacing. --- lib/Cake/Test/Case/Utility/HashTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/Cake/Test/Case/Utility/HashTest.php b/lib/Cake/Test/Case/Utility/HashTest.php index 5af077282..3dd1fd399 100644 --- a/lib/Cake/Test/Case/Utility/HashTest.php +++ b/lib/Cake/Test/Case/Utility/HashTest.php @@ -1368,14 +1368,17 @@ class HashTest extends CakeTestCase { public function testSortLocale() { // get the current locale $oldLocale = setlocale(LC_COLLATE, '0'); + // the de_DE.utf8 locale must be installed on the system where the test is performed setlocale(LC_COLLATE, 'de_DE.utf8'); + $items = array( array('Item' => array('entry' => 'Übergabe')), array('Item' => array('entry' => 'Ostfriesland')), array('Item' => array('entry' => 'Äpfel')), array('Item' => array('entry' => 'Apfel')), ); + $result = Hash::sort($items, '{n}.Item.entry', 'asc', 'locale'); $expected = array( array('Item' => array('entry' => 'Apfel')), @@ -1384,6 +1387,7 @@ class HashTest extends CakeTestCase { array('Item' => array('entry' => 'Übergabe')), ); $this->assertEquals($expected, $result); + // change to the original locale setlocale(LC_COLLATE, $oldLocale); } From ee319baec173d9f3f322ae04173d98d435e1ef04 Mon Sep 17 00:00:00 2001 From: Mark Sch Date: Fri, 26 Aug 2016 16:15:39 +0200 Subject: [PATCH 3/3] Backport skip --- lib/Cake/Test/Case/Utility/HashTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Cake/Test/Case/Utility/HashTest.php b/lib/Cake/Test/Case/Utility/HashTest.php index 3dd1fd399..f81316daa 100644 --- a/lib/Cake/Test/Case/Utility/HashTest.php +++ b/lib/Cake/Test/Case/Utility/HashTest.php @@ -1368,9 +1368,9 @@ class HashTest extends CakeTestCase { public function testSortLocale() { // get the current locale $oldLocale = setlocale(LC_COLLATE, '0'); - - // the de_DE.utf8 locale must be installed on the system where the test is performed - setlocale(LC_COLLATE, 'de_DE.utf8'); + + $updated = setlocale(LC_COLLATE, 'de_DE.utf8'); + $this->skipIf($updated === false, 'Could not set locale to de_DE.utf8, skipping test.'); $items = array( array('Item' => array('entry' => 'Übergabe')),