From f4c27e04bcad304bf9a3127e778dfce1c82f9ed6 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 8 Jan 2012 21:55:51 -0500 Subject: [PATCH] Fix errors generated when option['order'] is undefined. Fixes #2447 --- .../Component/PaginatorComponent.php | 2 +- .../Component/PaginatorComponentTest.php | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Controller/Component/PaginatorComponent.php b/lib/Cake/Controller/Component/PaginatorComponent.php index ecc1f2ec4..cc82ab259 100644 --- a/lib/Cake/Controller/Component/PaginatorComponent.php +++ b/lib/Cake/Controller/Component/PaginatorComponent.php @@ -333,7 +333,7 @@ class PaginatorComponent extends Component { $options['order'] = array($options['sort'] => $direction); } - if (!empty($whitelist)) { + if (!empty($whitelist) && isset($options['order']) && is_array($options['order'])) { $field = key($options['order']); if (!in_array($field, $whitelist)) { $options['order'] = null; diff --git a/lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php b/lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php index fe6c961a3..814fa6b7d 100644 --- a/lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php @@ -776,6 +776,26 @@ class PaginatorComponentTest extends CakeTestCase { $this->assertEquals($expected, $result['order']); } +/** + * Test that no sort doesn't trigger an error. + * + * @return void + */ + public function testValidateSortNoSort() { + $model = $this->getMock('Model'); + $model->alias = 'model'; + $model->expects($this->any())->method('hasField')->will($this->returnValue(true)); + + $options = array('direction' => 'asc'); + $result = $this->Paginator->validateSort($model, $options, array('title', 'id')); + $this->assertFalse(isset($result['order'])); + + $options = array('order' => 'invalid desc'); + $result = $this->Paginator->validateSort($model, $options, array('title', 'id')); + + $this->assertEquals($options['order'], $result['order']); + } + /** * test that maxLimit is respected *