Skip to content

Commit

Permalink
Fix scoped query parameters not being merged into pagination URLs
Browse files Browse the repository at this point in the history
Existing query parameters in the active scope should be present in
generated pagination URLs. This fixes pagination URLs 'dropping' scoped
parameters.

Refs #9210
  • Loading branch information
markstory committed Aug 12, 2016
1 parent 193b498 commit 37e8ef0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/View/Helper/PaginatorHelper.php
Expand Up @@ -505,9 +505,16 @@ public function generateUrl(array $options = [], $model = null, $full = false)
$url['sort'] = $url['direction'] = null;
}
if (!empty($paging['scope'])) {
$url = [$paging['scope'] => $url] + $this->_config['options']['url'];
if (empty($url[$paging['scope']]['page'])) {
unset($url[$paging['scope']]['page']);
$scope = $paging['scope'];
$currentParams = $this->_config['options']['url'];
// Merge existing query parameters in the scope.
if (isset($currentParams['?'][$scope]) && is_array($currentParams['?'][$scope])) {
$url += $currentParams['?'][$scope];
unset($currentParams['?'][$scope]);
}
$url = [$scope => $url] + $currentParams;
if (empty($url[$scope]['page'])) {
unset($url[$scope]['page']);
}
}

Expand Down
32 changes: 32 additions & 0 deletions tests/TestCase/View/Helper/PaginatorHelperTest.php
Expand Up @@ -826,6 +826,38 @@ public function testGenerateUrlMultiplePagination()
$this->assertEquals($expected, $result);
}

/**
* test generateUrl with multiple pagination and query string values
*
* @return void
*/
public function testGenerateUrlMultiplePaginationQueryStringData()
{
Router::setRequestInfo([
['controller' => 'posts', 'action' => 'index', 'plugin' => null],
['base' => '', 'here' => 'posts/index', 'webroot' => '/']
]);
$this->View->request->params['paging']['Article']['scope'] = 'article';
$this->View->request->params['paging']['Article']['page'] = 3;
$this->View->request->params['paging']['Article']['prevPage'] = true;
$this->View->request->query = [
'article' => [
'puppy' => 'no'
]
];
// Need to run __construct to update _config['url']
$paginator = new PaginatorHelper($this->View);
$paginator->options(['model' => 'Article']);

$result = $paginator->generateUrl(['sort' => 'name']);
$expected = '/posts/index?article%5Bpage%5D=3&article%5Bsort%5D=name&article%5Bpuppy%5D=no';
$this->assertEquals($expected, $result);

$result = $paginator->generateUrl([]);
$expected = '/posts/index?article%5Bpage%5D=3&article%5Bpuppy%5D=no';
$this->assertEquals($expected, $result);
}

/**
* testOptions method
*
Expand Down

0 comments on commit 37e8ef0

Please sign in to comment.