Skip to content

Commit

Permalink
Applying patch from 'Daren Thomas' fixes issues where sorting on virt…
Browse files Browse the repository at this point in the history
…ualFields did not work properly when using aliased sort link keys. Tests added. Fixes #680
  • Loading branch information
markstory committed May 17, 2010
1 parent 353c600 commit 4915645
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cake/libs/view/helpers/paginator.php
Expand Up @@ -302,7 +302,12 @@ function sort($title, $key = null, $options = array()) {
unset($options['direction']);

$sortKey = $this->sortKey($options['model']);
$isSorted = ($sortKey === $key || $sortKey === $this->defaultModel() . '.' . $key);
$defaultModel = $this->defaultModel();
$isSorted = (
$sortKey === $key ||
$sortKey === $defaultModel . '.' . $key ||
$key === $defaultModel . '.' . $sortKey
);

if ($isSorted) {
$dir = $this->sortDir($options['model']) === 'asc' ? 'desc' : 'asc';
Expand Down
46 changes: 46 additions & 0 deletions cake/tests/cases/libs/view/helpers/paginator.test.php
Expand Up @@ -219,6 +219,52 @@ function testSortLinks() {
$this->assertPattern('/\/accounts\/index\/param\/page:1\/sort:title\/direction:desc" class="foo asc">Title<\/a>$/', $result);
}

/**
* test that sort() works with virtual field order options.
*
* @return void
*/
function testSortLinkWithVirtualField() {
Router::setRequestInfo(array(
array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array(), 'form' => array(), 'url' => array('url' => 'accounts/')),
array('base' => '', 'here' => '/accounts/', 'webroot' => '/')
));
$this->Paginator->params['paging']['Article']['options']['order'] = array('full_name' => 'asc');

$result = $this->Paginator->sort('Article.full_name');
$expected = array(
'a' => array('href' => '/accounts/index/page:1/sort:Article.full_name/direction:desc', 'class' => 'asc'),
'Article.full Name',
'/a'
);
$this->assertTags($result, $expected);

$result = $this->Paginator->sort('full_name');
$expected = array(
'a' => array('href' => '/accounts/index/page:1/sort:full_name/direction:desc', 'class' => 'asc'),
'Full Name',
'/a'
);
$this->assertTags($result, $expected);

$this->Paginator->params['paging']['Article']['options']['order'] = array('full_name' => 'desc');
$result = $this->Paginator->sort('Article.full_name');
$expected = array(
'a' => array('href' => '/accounts/index/page:1/sort:Article.full_name/direction:asc', 'class' => 'desc'),
'Article.full Name',
'/a'
);
$this->assertTags($result, $expected);

$result = $this->Paginator->sort('full_name');
$expected = array(
'a' => array('href' => '/accounts/index/page:1/sort:full_name/direction:asc', 'class' => 'desc'),
'Full Name',
'/a'
);
$this->assertTags($result, $expected);
}

/**
* testSortLinksUsingDirectionOption method
*
Expand Down

0 comments on commit 4915645

Please sign in to comment.