Browse Source

Merge pull request #11873 from cakephp/ad-sort-page-1

Paginator sort links should go to page 1 when changing order
Mark Story 8 years ago
parent
commit
027da9679d

+ 1 - 1
src/View/Helper/PaginatorHelper.php

@@ -471,7 +471,7 @@ class PaginatorHelper extends Helper
         }
 
         $url = array_merge(
-            ['sort' => $key, 'direction' => $dir],
+            ['sort' => $key, 'direction' => $dir, 'page' => 1],
             $url,
             ['order' => null]
         );

+ 55 - 2
tests/TestCase/View/Helper/PaginatorHelperTest.php

@@ -811,6 +811,59 @@ class PaginatorHelperTest extends TestCase
     }
 
     /**
+     * Verify that sort links always result in a url that is page 1 (page not
+     * present in the url)
+     *
+     * @param string $field
+     * @param array $options
+     * @param string $expected
+     * @dataProvider urlGenerationResetsToPage1Provider
+     */
+    public function testUrlGenerationResetsToPage1($field, $options, $expected)
+    {
+        $this->Paginator->request = $this->Paginator->request
+            ->withParam('paging.Article.page', 2)
+            ->withParam('paging.Article.sort', 'name')
+            ->withParam('paging.Article.direction', 'asc');
+        $result = $this->Paginator->sort($field, null, ['url' => $options]);
+        $this->assertSame($expected, $result);
+    }
+
+    /**
+     * Returns data sets of:
+     *  * the name of the field being sorted on
+     *  * url paramters to pass to paginator sort
+     *  * expected result as a string
+     *
+     * @return array
+     */
+    public function urlGenerationResetsToPage1Provider()
+    {
+        return [
+            'Sorting the field currently sorted asc, asc' => [
+                'name',
+                ['sort' => 'name', 'direction' => 'asc'],
+                '<a class="asc" href="/index?sort=name&amp;direction=asc">Name</a>'
+            ],
+            'Sorting the field currently sorted asc, desc' => [
+                'name',
+                ['sort' => 'name', 'direction' => 'desc'],
+                '<a class="asc" href="/index?sort=name&amp;direction=desc">Name</a>'
+            ],
+            'Sorting other asc' => [
+                'other',
+                ['sort' => 'other', 'direction' => 'asc'],
+                '<a href="/index?sort=other&amp;direction=asc">Other</a>'
+            ],
+            'Sorting other desc' => [
+                'other',
+                ['sort' => 'other', 'direction' => 'desc'],
+                '<a href="/index?sort=other&amp;direction=desc">Other</a>'
+            ]
+        ];
+    }
+
+    /**
      * test URL generation with prefix routes
      *
      * @return void
@@ -842,7 +895,7 @@ class PaginatorHelperTest extends TestCase
 
         $result = $this->Paginator->sort('name', null, ['url' => $options]);
         $expected = [
-            'a' => ['href' => '/members/posts/index?page=2&amp;sort=name&amp;direction=asc'],
+            'a' => ['href' => '/members/posts/index?sort=name&amp;direction=asc'],
             'Name',
             '/a'
         ];
@@ -951,7 +1004,7 @@ class PaginatorHelperTest extends TestCase
 
         $result = $this->Paginator->sort('name');
         $expected = [
-            'a' => ['href' => '/posts/index?article%5Bpage%5D=3&amp;article%5Bsort%5D=name&amp;article%5Bdirection%5D=asc'],
+            'a' => ['href' => '/posts/index?article%5Bsort%5D=name&amp;article%5Bdirection%5D=asc'],
             'Name',
             '/a'
         ];