Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Grafikart committed Jan 31, 2014
1 parent 673638a commit 7bdfa54
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/View/Helper/PaginatorHelper.php
Expand Up @@ -440,7 +440,7 @@ public function url($options = array(), $model = null) {
if (!empty($url['page']) && $url['page'] == 1) {
$url['page'] = null;
}
if (isset($paging['sortDefault']) && isset($paging['directionDefault']) && $url['sort'] == $paging['sortDefault'] && $url['direction'] == $paging['directionDefault'] ) {
if (isset($paging['sortDefault']) && isset($paging['directionDefault']) && $url['sort'] === $paging['sortDefault'] && $url['direction'] === $paging['directionDefault'] ) {
$url['sort'] = $url['direction'] = null;
}
return parent::url($url);
Expand Down
30 changes: 30 additions & 0 deletions tests/TestCase/Controller/Component/PaginatorComponentTest.php
Expand Up @@ -185,6 +185,36 @@ public function testDefaultPaginateParams() {
$this->Paginator->paginate($table, $settings);
}

/**
* test that default sort and default direction are injectect into request
*
* @return void
*/
public function testDefaultPaginateParamsIntoRequest() {
$settings = array(
'order' => ['PaginatorPosts.id' => 'DESC'],
'maxLimit' => 10,
);

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

$table->expects($this->once())
->method('find')
->with('all', [
'conditions' => [],
'fields' => null,
'limit' => 10,
'page' => 1,
'order' => ['PaginatorPosts.id' => 'DESC']
])
->will($this->returnValue($query));

$this->Paginator->paginate($table, $settings);
$this->assertEquals('PaginatorPosts.id', $this->request->params['paging']['PaginatorPosts']['sortDefault']);
$this->assertEquals('DESC', $this->request->params['paging']['PaginatorPosts']['directionDefault']);
}

/**
* test that option merging prefers specific models
*
Expand Down
29 changes: 29 additions & 0 deletions tests/TestCase/View/Helper/PaginatorHelperTest.php
Expand Up @@ -734,6 +734,35 @@ public function testPassedArgsMergingWithUrlOptions() {
$this->assertTags($result, $expected);
}

/**
* Test that url generated doesn't include default sort & direction
*
* @return void
*/
public function testDefaultSortRemovedFromUrl() {
Router::setRequestInfo(array(
array('plugin' => null, 'controller' => 'articles', 'action' => 'index'),
array('base' => '/', 'here' => '/articles/', 'webroot' => '/')
));
$this->Paginator->request->params['paging'] = array(
'Article' => array(
'page' => 1, 'current' => 3, 'count' => 13,
'prevPage' => false, 'nextPage' => true, 'pageCount' => 8,
'sort' => 'Article.title', 'direction' => 'ASC',
'sortDefault' => 'Article.title', 'directionDefault' => 'ASC'
)
);
$result = $this->Paginator->next('Next');
$expected = array(
'li' => array('class' => 'next'),
'a' => array('rel' => 'next', 'href' => '/articles/index?page=2'),
'Next',
'/a',
'/li'
);
$this->assertTags($result, $expected);
}

/**
* Test the prev() method.
*
Expand Down

0 comments on commit 7bdfa54

Please sign in to comment.