Merge pull request #723 from Jippi/improve-custom-pagination

Better custom find for pagination
This commit is contained in:
José Lorenzo Rodríguez 2012-07-19 12:46:22 -07:00
commit 2ccbf7819b
2 changed files with 60 additions and 0 deletions

View file

@ -152,6 +152,12 @@ class PaginatorComponent extends Component {
$extra = array_diff_key($options, compact(
'conditions', 'fields', 'order', 'limit', 'page', 'recursive'
));
if (!empty($extra['findType'])) {
$type = $extra['findType'];
unset($extra['findType']);
}
if ($type !== 'all') {
$extra['type'] = $type;
}

View file

@ -710,6 +710,28 @@ class PaginatorComponentTest extends CakeTestCase {
$this->assertEquals($expected, $result);
}
/**
* test mergeOptions with customFind key
*
* @return void
*/
public function testMergeOptionsCustomFindKey() {
$this->request->params['named'] = array(
'page' => 10,
'limit' => 10
);
$this->Paginator->settings = array(
'page' => 1,
'limit' => 20,
'maxLimit' => 100,
'paramType' => 'named',
'findType' => 'myCustomFind'
);
$result = $this->Paginator->mergeOptions('Post');
$expected = array('page' => 10, 'limit' => 10, 'maxLimit' => 100, 'paramType' => 'named', 'findType' => 'myCustomFind');
$this->assertEquals($expected, $result);
}
/**
* test merging options from the querystring.
*
@ -1078,6 +1100,38 @@ class PaginatorComponentTest extends CakeTestCase {
$this->assertTrue($result['nextPage']);
$this->assertFalse($result['prevPage']);
}
/**
* test paginate() and custom find with customFind key, to make sure the correct count is returned.
*
* @return void
*/
public function testPaginateCustomFindWithCustomFindKey() {
$Controller =& new Controller($this->request);
$Controller->uses = array('PaginatorCustomPost');
$Controller->constructClasses();
$data = array('author_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
$Controller->PaginatorCustomPost->create($data);
$result = $Controller->PaginatorCustomPost->save();
$this->assertTrue(!empty($result));
$Controller->paginate = array(
'conditions' => array('PaginatorCustomPost.published' => 'Y'),
'findType' => 'list',
'limit' => 2
);
$result = $Controller->paginate();
$expected = array(
1 => 'First Post',
2 => 'Second Post',
);
$this->assertEquals($expected, $result);
$result = $Controller->params['paging']['PaginatorCustomPost'];
$this->assertEquals(2, $result['current']);
$this->assertEquals(3, $result['count']);
$this->assertEquals(2, $result['pageCount']);
$this->assertTrue($result['nextPage']);
$this->assertFalse($result['prevPage']);
}
/**
* test paginate() and custom find with fields array, to make sure the correct count is returned.