From 599a370dcae028b534030673cd0b0253fb1ab215 Mon Sep 17 00:00:00 2001 From: Cory Thompson Date: Thu, 18 Dec 2014 21:50:50 +1100 Subject: [PATCH] Fixed paginate conditions applied twice --- .../Component/PaginatorComponent.php | 3 +- .../Component/PaginatorComponentTest.php | 32 +++++++++---------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/Controller/Component/PaginatorComponent.php b/src/Controller/Component/PaginatorComponent.php index 8827f2a67ba..61eab218eda 100644 --- a/src/Controller/Component/PaginatorComponent.php +++ b/src/Controller/Component/PaginatorComponent.php @@ -159,9 +159,10 @@ public function paginate($object, array $settings = []) { if (empty($query)) { $query = $object->find($finder, $options); + } else { + $query->applyOptions($options); } - $query->applyOptions($options); $results = $query->all(); $numResults = count($results); $count = $numResults ? $query->count() : 0; diff --git a/tests/TestCase/Controller/Component/PaginatorComponentTest.php b/tests/TestCase/Controller/Component/PaginatorComponentTest.php index 47f8037de94..dbfd91b8d78 100644 --- a/tests/TestCase/Controller/Component/PaginatorComponentTest.php +++ b/tests/TestCase/Controller/Component/PaginatorComponentTest.php @@ -121,11 +121,11 @@ public function testPaginateExtraParams() { 'order' => array('PaginatorPosts.id' => 'ASC') ), ); - $table = $this->_getMockPosts(['find']); + $table = $this->_getMockPosts(['query']); $query = $this->_getMockFindQuery(); + $table->expects($this->once()) - ->method('find') - ->with('all') + ->method('query') ->will($this->returnValue($query)); $query->expects($this->once()) @@ -203,13 +203,13 @@ public function testDefaultPaginateParams() { 'maxLimit' => 10, ); - $table = $this->_getMockPosts(['find']); + $table = $this->_getMockPosts(['query']); $query = $this->_getMockFindQuery(); $table->expects($this->once()) - ->method('find') - ->with('all') + ->method('query') ->will($this->returnValue($query)); + $query->expects($this->once()) ->method('applyOptions') ->with([ @@ -233,13 +233,13 @@ public function testDefaultPaginateParamsIntoRequest() { 'maxLimit' => 10, ); - $table = $this->_getMockPosts(['find']); + $table = $this->_getMockPosts(['query']); $query = $this->_getMockFindQuery(); $table->expects($this->once()) - ->method('find') - ->with('all') + ->method('query') ->will($this->returnValue($query)); + $query->expects($this->once()) ->method('applyOptions') ->with([ @@ -418,13 +418,13 @@ public function testMergeOptionsMaxLimit() { * @return void */ public function testValidateSortInvalid() { - $table = $this->_getMockPosts(['find']); + $table = $this->_getMockPosts(['query']); $query = $this->_getMockFindQuery(); $table->expects($this->once()) - ->method('find') - ->with('all') + ->method('query') ->will($this->returnValue($query)); + $query->expects($this->once())->method('applyOptions') ->with([ 'limit' => 20, @@ -789,13 +789,13 @@ public function testPaginateCustomFindCount() { 'finder' => 'published', 'limit' => 2 ); - $table = $this->_getMockPosts(['find']); + $table = $this->_getMockPosts(['query']); $query = $this->_getMockFindQuery(); + $table->expects($this->once()) - ->method('find') - ->with('published') + ->method('query') ->will($this->returnValue($query)); - + $query->expects($this->once())->method('applyOptions') ->with(['limit' => 2, 'page' => 1, 'order' => [], 'whitelist' => ['limit', 'sort', 'page', 'direction']]); $this->Paginator->paginate($table, $settings);