Permalink
Browse files

Applying patch from 'Daren Thomas' Fixes issues where sorting on virt…

…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 4915645fb14b1f56ee3041cbe1ea9688eca02c97
Showing with 52 additions and 1 deletion.
  1. +6 −1 cake/libs/view/helpers/paginator.php
  2. +46 −0 cake/tests/cases/libs/view/helpers/paginator.test.php
@@ -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';
@@ -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
*

0 comments on commit 4915645

Please sign in to comment.