From 2a10847103973f0a199ffce345fbf877f46ae69a Mon Sep 17 00:00:00 2001 From: ADmad Date: Fri, 30 Oct 2015 01:09:09 +0530 Subject: [PATCH] Fix setting paging data for plugin models. Refs #303 --- src/Listener/ApiPaginationListener.php | 2 +- .../Listener/ApiPaginationListenerTest.php | 74 +++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/src/Listener/ApiPaginationListener.php b/src/Listener/ApiPaginationListener.php index b57f8f049..1b2a08f70 100644 --- a/src/Listener/ApiPaginationListener.php +++ b/src/Listener/ApiPaginationListener.php @@ -47,7 +47,7 @@ public function beforeRender(Event $event) } $controller = $this->_controller(); - $modelClass = $controller->modelClass; + list(, $modelClass) = pluginSplit($controller->modelClass); if (!array_key_exists($modelClass, $request->paging)) { return; diff --git a/tests/TestCase/Listener/ApiPaginationListenerTest.php b/tests/TestCase/Listener/ApiPaginationListenerTest.php index 5f79190e9..bad8c62ff 100644 --- a/tests/TestCase/Listener/ApiPaginationListenerTest.php +++ b/tests/TestCase/Listener/ApiPaginationListenerTest.php @@ -178,4 +178,78 @@ public function testBeforeRenderWithPaginationData() $Instance->beforeRender(new \Cake\Event\Event('something')); } + + /** + * Test with pagination data for plugin model + * + * @return void + */ + public function testBeforeRenderWithPaginationDataForPluginModel() + { + $Request = $this + ->getMockBuilder('\Cake\Network\Request') + ->setMethods(null) + ->getMock(); + $Request->paging = [ + 'MyModel' => [ + 'pageCount' => 10, + 'page' => 2, + 'nextPage' => true, + 'prevPage' => true, + 'count' => 100, + 'limit' => 10 + ] + ]; + + $expected = [ + 'page_count' => 10, + 'current_page' => 2, + 'has_next_page' => true, + 'has_prev_page' => true, + 'count' => 100, + 'limit' => 10 + ]; + + $Controller = $this + ->getMockBuilder('\Cake\Controller\Controller') + ->disableOriginalConstructor() + ->setMethods(['set']) + ->getMock(); + $Controller + ->expects($this->once()) + ->method('set') + ->with('pagination', $expected); + + $Action = $this + ->getMockBuilder('\Crud\Action\BaseAction') + ->disableOriginalConstructor() + ->setMethods(['config']) + ->getMock(); + $Action + ->expects($this->once()) + ->method('config') + ->with('serialize.pagination', 'pagination'); + + $Instance = $this + ->getMockBuilder('\Crud\Listener\ApiPaginationListener') + ->disableOriginalConstructor() + ->setMethods(['_request', '_controller', '_action']) + ->getMock(); + $Instance + ->expects($this->once()) + ->method('_request') + ->will($this->returnValue($Request)); + $Instance + ->expects($this->once()) + ->method('_controller') + ->will($this->returnValue($Controller)); + $Instance + ->expects($this->once()) + ->method('_action') + ->will($this->returnValue($Action)); + + $Controller->modelClass = 'MyPlugin.MyModel'; + + $Instance->beforeRender(new \Cake\Event\Event('something')); + } }