Skip to content

Commit

Permalink
Merge pull request #12714 from cakephp/bugfix/pagination-default-mult…
Browse files Browse the repository at this point in the history
…i-sort

Fix paginator defaulting for multi-default-sort.
  • Loading branch information
markstory committed Nov 11, 2018
2 parents 21c8fce + d252e0b commit d080e1d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Datasource/Paginator.php
Expand Up @@ -221,7 +221,7 @@ public function paginate($object, array $params = [], array $settings = [])
'nextPage' => $count > ($page * $limit),
'pageCount' => $pageCount,
'sort' => $options['sort'],
'direction' => current($order),
'direction' => isset($options['sort']) ? current($order) : null,
'limit' => $defaults['limit'] != $limit ? $limit : null,
'sortDefault' => $sortDefault,
'directionDefault' => $directionDefault,
Expand Down
1 change: 1 addition & 0 deletions src/View/Helper/PaginatorHelper.php
Expand Up @@ -436,6 +436,7 @@ public function sort($key, $title = null, array $options = [])

$title = __(Inflector::humanize(preg_replace('/_id$/', '', $title)));
}

$defaultDir = isset($options['direction']) ? strtolower($options['direction']) : 'asc';
unset($options['direction']);

Expand Down
55 changes: 52 additions & 3 deletions tests/TestCase/Datasource/PaginatorTest.php
Expand Up @@ -24,7 +24,6 @@

class PaginatorTest extends TestCase
{

/**
* fixtures property
*
Expand All @@ -42,6 +41,11 @@ class PaginatorTest extends TestCase
*/
public $autoFixtures = false;

/**
* @var \Cake\Datasource\Paginator
*/
public $Paginator;

/**
* setup
*
Expand Down Expand Up @@ -247,6 +251,43 @@ public function testDefaultPaginateParams()
$this->Paginator->paginate($table, [], $settings);
}

/**
* Tests that flat default pagination parameters work for multi order.
*
* @return void
*/
public function testDefaultPaginateParamsMultiOrder()
{
$settings = [
'order' => ['PaginatorPosts.id' => 'DESC', 'PaginatorPosts.title' => 'ASC'],
];

$table = $this->_getMockPosts(['query']);
$query = $this->_getMockFindQuery();

$table->expects($this->once())
->method('query')
->will($this->returnValue($query));

$query->expects($this->once())
->method('applyOptions')
->with([
'limit' => 20,
'page' => 1,
'order' => $settings['order'],
'whitelist' => ['limit', 'sort', 'page', 'direction'],
'scope' => null,
'sort' => null,
]);

$this->Paginator->paginate($table, [], $settings);

$pagingParams = $this->Paginator->getPagingParams();
$this->assertNull($pagingParams['PaginatorPosts']['direction']);
$this->assertFalse($pagingParams['PaginatorPosts']['sortDefault']);
$this->assertFalse($pagingParams['PaginatorPosts']['directionDefault']);
}

/**
* test that default sort and default direction are injected into request
*
Expand Down Expand Up @@ -878,6 +919,9 @@ public function testValidateSortWhitelistMultiple()
$this->assertEquals($expected, $result['order']);
}

/**
* @return \Cake\Datasource\RepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected function getMockRepository()
{
$model = $this->getMockBuilder('Cake\Datasource\RepositoryInterface')
Expand All @@ -890,6 +934,9 @@ protected function getMockRepository()
return $model;
}

/**
* @return \Cake\Datasource\RepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected function mockAliasHasFieldModel()
{
$model = $this->getMockRepository();
Expand Down Expand Up @@ -1335,7 +1382,7 @@ public function testPaginateQueryWithLimit()
* Helper method for making mocks.
*
* @param array $methods
* @return \Cake\ORM\Table
* @return \Cake\ORM\Table|\PHPUnit\Framework\MockObject\MockObject
*/
protected function _getMockPosts($methods = [])
{
Expand All @@ -1359,10 +1406,12 @@ protected function _getMockPosts($methods = [])
/**
* Helper method for mocking queries.
*
* @return \Cake\ORM\Query
* @param string|null $table
* @return \Cake\ORM\Query|\PHPUnit\Framework\MockObject\MockObject
*/
protected function _getMockFindQuery($table = null)
{
/** @var \Cake\ORM\Query|\PHPUnit\Framework\MockObject\MockObject $query */
$query = $this->getMockBuilder('Cake\ORM\Query')
->setMethods(['total', 'all', 'count', 'applyOptions'])
->disableOriginalConstructor()
Expand Down

0 comments on commit d080e1d

Please sign in to comment.