Skip to content

Commit

Permalink
Fix setting paging data for plugin models.
Browse files Browse the repository at this point in the history
Refs #303
  • Loading branch information
ADmad committed Oct 29, 2015
1 parent f234ff2 commit 2a10847
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Listener/ApiPaginationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
74 changes: 74 additions & 0 deletions tests/TestCase/Listener/ApiPaginationListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}

0 comments on commit 2a10847

Please sign in to comment.