Skip to content

Commit

Permalink
Merge pull request #11757 from cakephp/default-pagination-direction
Browse files Browse the repository at this point in the history
Ensure paging params are properly set based on "order" in settings.
  • Loading branch information
markstory committed Feb 25, 2018
2 parents 2d98f8b + b6dbeb5 commit fe7c3a8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
7 changes: 7 additions & 0 deletions src/Datasource/Paginator.php
Expand Up @@ -373,6 +373,13 @@ public function validateSort(RepositoryInterface $object, array $options)
}
}

if ($options['sort'] === null
&& count($options['order']) === 1
&& !is_numeric(key($options['order']))
) {
$options['sort'] = key($options['order']);
}

$options['order'] = $this->_prefix($object, $options['order'], $inWhitelist);

return $options;
Expand Down
10 changes: 5 additions & 5 deletions tests/TestCase/Controller/Component/PaginatorComponentTest.php
Expand Up @@ -192,7 +192,7 @@ public function testPaginateExtraParams()
'page' => 1,
'whitelist' => ['limit', 'sort', 'page', 'direction'],
'scope' => null,
'sort' => null,
'sort' => 'PaginatorPosts.id',
]);
$this->Paginator->paginate($table, $settings);
}
Expand Down Expand Up @@ -328,7 +328,7 @@ public function testDefaultPaginateParams()
'order' => ['PaginatorPosts.id' => 'DESC'],
'whitelist' => ['limit', 'sort', 'page', 'direction'],
'scope' => null,
'sort' => null,
'sort' => 'PaginatorPosts.id',
]);

$this->Paginator->paginate($table, $settings);
Expand Down Expand Up @@ -361,7 +361,7 @@ public function testDefaultPaginateParamsIntoRequest()
'order' => ['PaginatorPosts.id' => 'DESC'],
'whitelist' => ['limit', 'sort', 'page', 'direction'],
'scope' => null,
'sort' => null,
'sort' => 'PaginatorPosts.id',
]);

$this->Paginator->paginate($table, $settings);
Expand Down Expand Up @@ -1275,7 +1275,7 @@ public function testPaginateQuery()
'page' => 1,
'whitelist' => ['limit', 'sort', 'page', 'direction'],
'scope' => null,
'sort' => null,
'sort' => 'PaginatorPosts.id',
]);
$this->Paginator->paginate($query, $settings);
}
Expand Down Expand Up @@ -1336,7 +1336,7 @@ public function testPaginateQueryWithLimit()
'page' => 1,
'whitelist' => ['limit', 'sort', 'page', 'direction'],
'scope' => null,
'sort' => null,
'sort' => 'PaginatorPosts.id',
]);
$this->Paginator->paginate($query, $settings);
}
Expand Down
49 changes: 44 additions & 5 deletions tests/TestCase/Datasource/PaginatorTest.php
Expand Up @@ -129,7 +129,7 @@ public function testPaginateExtraParams()
'page' => 1,
'whitelist' => ['limit', 'sort', 'page', 'direction'],
'scope' => null,
'sort' => null,
'sort' => 'PaginatorPosts.id',
]);
$this->Paginator->paginate($table, $params, $settings);
}
Expand Down Expand Up @@ -241,7 +241,7 @@ public function testDefaultPaginateParams()
'order' => ['PaginatorPosts.id' => 'DESC'],
'whitelist' => ['limit', 'sort', 'page', 'direction'],
'scope' => null,
'sort' => null,
'sort' => 'PaginatorPosts.id',
]);

$this->Paginator->paginate($table, [], $settings);
Expand Down Expand Up @@ -274,7 +274,7 @@ public function testDefaultPaginateParamsIntoRequest()
'order' => ['PaginatorPosts.id' => 'DESC'],
'whitelist' => ['limit', 'sort', 'page', 'direction'],
'scope' => null,
'sort' => null,
'sort' => 'PaginatorPosts.id',
]);

$this->Paginator->paginate($table, [], $settings);
Expand Down Expand Up @@ -633,6 +633,45 @@ public function testValidateSortInvalidDirection()
$this->assertEquals('asc', $result['order']['model.something']);
}

/**
* Test that "sort" and "direction" in paging params is properly set based
* on initial value of "order" in paging settings.
*
* @return void
*/
public function testValidaSortInitialSortAndDirection()
{
$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' => ['PaginatorPosts.id' => 'asc'],
'whitelist' => ['limit', 'sort', 'page', 'direction'],
'sort' => 'id',
'scope' => null,
'sortWhitelist' => ['id'],
]);

$options = [
'order' => [
'id' => 'asc',
],
'sortWhitelist' => ['id'],
];
$this->Paginator->paginate($table, [], $options);
$pagingParams = $this->Paginator->getPagingParams();

$this->assertEquals('id', $pagingParams['PaginatorPosts']['sort']);
$this->assertEquals('asc', $pagingParams['PaginatorPosts']['direction']);
}

/**
* testValidateSortRetainsOriginalSortValue
*
Expand Down Expand Up @@ -1196,7 +1235,7 @@ public function testPaginateQuery()
'page' => 1,
'whitelist' => ['limit', 'sort', 'page', 'direction'],
'scope' => null,
'sort' => null,
'sort' => 'PaginatorPosts.id',
]);
$this->Paginator->paginate($query, $params, $settings);
}
Expand Down Expand Up @@ -1257,7 +1296,7 @@ public function testPaginateQueryWithLimit()
'page' => 1,
'whitelist' => ['limit', 'sort', 'page', 'direction'],
'scope' => null,
'sort' => null,
'sort' => 'PaginatorPosts.id',
]);
$this->Paginator->paginate($query, $params, $settings);
}
Expand Down

0 comments on commit fe7c3a8

Please sign in to comment.