From b539161b2dd9974dc6461e466b3a5d3f8e6b52cd Mon Sep 17 00:00:00 2001 From: Lucas Ferreira Date: Fri, 26 May 2017 15:05:18 -0300 Subject: [PATCH 1/3] - Some fix into Paginator component for order / sort classic sintax --- lib/Cake/Controller/Component/PaginatorComponent.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Cake/Controller/Component/PaginatorComponent.php b/lib/Cake/Controller/Component/PaginatorComponent.php index 6e34fa6d7..85f8ab6b0 100644 --- a/lib/Cake/Controller/Component/PaginatorComponent.php +++ b/lib/Cake/Controller/Component/PaginatorComponent.php @@ -382,13 +382,13 @@ class PaginatorComponent extends Component { } return $options; } - if (!empty($options['order']) && is_array($options['order'])) { $order = array(); foreach ($options['order'] as $key => $value) { if (is_int($key)) { - $key = $value; - $value = 'asc'; + $field = explode(' ', $value); + $key = $field[0]; + $value = count($field) === 2 ? trim($field[1]) : 'asc'; } $field = $key; $alias = $object->alias; From ee1980b8f5eb56a82830c14eb1d327853628cd13 Mon Sep 17 00:00:00 2001 From: Lucas Ferreira Date: Fri, 26 May 2017 18:36:50 -0300 Subject: [PATCH 2/3] - Tests for array order syntax fix --- .../Component/PaginatorComponentTest.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php b/lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php index 087b92029..c888b291d 100644 --- a/lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php @@ -320,12 +320,20 @@ class PaginatorComponentTest extends CakeTestCase { $Controller->PaginatorControllerPost->order = null; + /* ORDER ARRAY FIELD => SORT MODE */ $Controller->Paginator->settings = array( 'order' => array('PaginatorControllerComment.id' => 'ASC') ); $results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerComment'), '{n}.PaginatorControllerComment.id'); $this->assertEquals(array(1, 2, 3, 4, 5, 6), $results); + /* ORDER ARRAY "FIELD SORT" MODE */ + $Controller->Paginator->settings = array( + 'order' => array('PaginatorControllerComment.id DESC') + ); + $results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerComment'), '{n}.PaginatorControllerComment.id'); + $this->assertEquals(array(6, 5, 4, 3, 2, 1), $results); + $Controller->Paginator->settings = array( 'order' => array('PaginatorControllerPost.id' => 'ASC') ); @@ -619,6 +627,7 @@ class PaginatorComponentTest extends CakeTestCase { $result = $Controller->Paginator->validateSort($Controller->PaginatorControllerPost, array()); $this->assertArrayNotHasKey('order', $result); + /* DEFAULT */ $Controller->PaginatorControllerPost->order = array( 'PaginatorControllerPost.id', 'PaginatorControllerPost.created' => 'asc' @@ -629,6 +638,18 @@ class PaginatorComponentTest extends CakeTestCase { 'PaginatorControllerPost.created' => 'asc' ); $this->assertEquals($expected, $result['order']); + + /* CLASSIC */ + $Controller->PaginatorControllerPost->order = array( + 'PaginatorControllerPost.id ASC', + 'PaginatorControllerPost.created DESC' + ); + $result = $Controller->Paginator->validateSort($Controller->PaginatorControllerPost, array()); + $expected = array( + 'PaginatorControllerPost.id' => 'ASC', + 'PaginatorControllerPost.created' => 'DESC' + ); + $this->assertEquals($expected, $result['order']); } /** From 32581991931d600a039798d6b9fc7b4ee47410bd Mon Sep 17 00:00:00 2001 From: Lucas Ferreira Date: Wed, 31 May 2017 08:33:41 -0300 Subject: [PATCH 3/3] Remove personal comments for pull request --- .../Test/Case/Controller/Component/PaginatorComponentTest.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php b/lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php index c888b291d..9da2a1fe4 100644 --- a/lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php @@ -320,14 +320,12 @@ class PaginatorComponentTest extends CakeTestCase { $Controller->PaginatorControllerPost->order = null; - /* ORDER ARRAY FIELD => SORT MODE */ $Controller->Paginator->settings = array( 'order' => array('PaginatorControllerComment.id' => 'ASC') ); $results = Hash::extract($Controller->Paginator->paginate('PaginatorControllerComment'), '{n}.PaginatorControllerComment.id'); $this->assertEquals(array(1, 2, 3, 4, 5, 6), $results); - /* ORDER ARRAY "FIELD SORT" MODE */ $Controller->Paginator->settings = array( 'order' => array('PaginatorControllerComment.id DESC') ); @@ -627,7 +625,6 @@ class PaginatorComponentTest extends CakeTestCase { $result = $Controller->Paginator->validateSort($Controller->PaginatorControllerPost, array()); $this->assertArrayNotHasKey('order', $result); - /* DEFAULT */ $Controller->PaginatorControllerPost->order = array( 'PaginatorControllerPost.id', 'PaginatorControllerPost.created' => 'asc' @@ -639,7 +636,6 @@ class PaginatorComponentTest extends CakeTestCase { ); $this->assertEquals($expected, $result['order']); - /* CLASSIC */ $Controller->PaginatorControllerPost->order = array( 'PaginatorControllerPost.id ASC', 'PaginatorControllerPost.created DESC'