Browse Source

Merge pull request #11757 from cakephp/default-pagination-direction

Ensure paging params are properly set based on "order" in settings.
Mark Story 8 years ago
parent
commit
fe7c3a82d6

+ 7 - 0
src/Datasource/Paginator.php

@@ -373,6 +373,13 @@ class Paginator implements PaginatorInterface
             }
         }
 
+        if ($options['sort'] === null
+            && count($options['order']) === 1
+            && !is_numeric(key($options['order']))
+        ) {
+            $options['sort'] = key($options['order']);
+        }
+
         $options['order'] = $this->_prefix($object, $options['order'], $inWhitelist);
 
         return $options;

+ 5 - 5
tests/TestCase/Controller/Component/PaginatorComponentTest.php

@@ -192,7 +192,7 @@ class PaginatorComponentTest extends TestCase
                 'page' => 1,
                 'whitelist' => ['limit', 'sort', 'page', 'direction'],
                 'scope' => null,
-                'sort' => null,
+                'sort' => 'PaginatorPosts.id',
             ]);
         $this->Paginator->paginate($table, $settings);
     }
@@ -328,7 +328,7 @@ class PaginatorComponentTest extends TestCase
                 'order' => ['PaginatorPosts.id' => 'DESC'],
                 'whitelist' => ['limit', 'sort', 'page', 'direction'],
                 'scope' => null,
-                'sort' => null,
+                'sort' => 'PaginatorPosts.id',
             ]);
 
         $this->Paginator->paginate($table, $settings);
@@ -361,7 +361,7 @@ class PaginatorComponentTest extends TestCase
                 'order' => ['PaginatorPosts.id' => 'DESC'],
                 'whitelist' => ['limit', 'sort', 'page', 'direction'],
                 'scope' => null,
-                'sort' => null,
+                'sort' => 'PaginatorPosts.id',
             ]);
 
         $this->Paginator->paginate($table, $settings);
@@ -1275,7 +1275,7 @@ class PaginatorComponentTest extends TestCase
                 'page' => 1,
                 'whitelist' => ['limit', 'sort', 'page', 'direction'],
                 'scope' => null,
-                'sort' => null,
+                'sort' => 'PaginatorPosts.id',
             ]);
         $this->Paginator->paginate($query, $settings);
     }
@@ -1336,7 +1336,7 @@ class PaginatorComponentTest extends TestCase
                 'page' => 1,
                 'whitelist' => ['limit', 'sort', 'page', 'direction'],
                 'scope' => null,
-                'sort' => null,
+                'sort' => 'PaginatorPosts.id',
             ]);
         $this->Paginator->paginate($query, $settings);
     }

+ 44 - 5
tests/TestCase/Datasource/PaginatorTest.php

@@ -129,7 +129,7 @@ class PaginatorTest extends TestCase
                 'page' => 1,
                 'whitelist' => ['limit', 'sort', 'page', 'direction'],
                 'scope' => null,
-                'sort' => null,
+                'sort' => 'PaginatorPosts.id',
             ]);
         $this->Paginator->paginate($table, $params, $settings);
     }
@@ -241,7 +241,7 @@ class PaginatorTest extends TestCase
                 'order' => ['PaginatorPosts.id' => 'DESC'],
                 'whitelist' => ['limit', 'sort', 'page', 'direction'],
                 'scope' => null,
-                'sort' => null,
+                'sort' => 'PaginatorPosts.id',
             ]);
 
         $this->Paginator->paginate($table, [], $settings);
@@ -274,7 +274,7 @@ class PaginatorTest extends TestCase
                 'order' => ['PaginatorPosts.id' => 'DESC'],
                 'whitelist' => ['limit', 'sort', 'page', 'direction'],
                 'scope' => null,
-                'sort' => null,
+                'sort' => 'PaginatorPosts.id',
             ]);
 
         $this->Paginator->paginate($table, [], $settings);
@@ -634,6 +634,45 @@ class PaginatorTest extends TestCase
     }
 
     /**
+     * Test that "sort" and "direction" in paging params is properly set based
+     * on initial value of "order" in paging settings.
+     *
+     * @return void
+     */
+    public function testValidaSortInitialSortAndDirection()
+    {
+        $table = $this->_getMockPosts(['query']);
+        $query = $this->_getMockFindQuery();
+
+        $table->expects($this->once())
+            ->method('query')
+            ->will($this->returnValue($query));
+
+        $query->expects($this->once())->method('applyOptions')
+            ->with([
+                'limit' => 20,
+                'page' => 1,
+                'order' => ['PaginatorPosts.id' => 'asc'],
+                'whitelist' => ['limit', 'sort', 'page', 'direction'],
+                'sort' => 'id',
+                'scope' => null,
+                'sortWhitelist' => ['id'],
+            ]);
+
+        $options = [
+            'order' => [
+                'id' => 'asc',
+            ],
+            'sortWhitelist' => ['id'],
+        ];
+        $this->Paginator->paginate($table, [], $options);
+        $pagingParams = $this->Paginator->getPagingParams();
+
+        $this->assertEquals('id', $pagingParams['PaginatorPosts']['sort']);
+        $this->assertEquals('asc', $pagingParams['PaginatorPosts']['direction']);
+    }
+
+    /**
      * testValidateSortRetainsOriginalSortValue
      *
      * @return void
@@ -1196,7 +1235,7 @@ class PaginatorTest extends TestCase
                 'page' => 1,
                 'whitelist' => ['limit', 'sort', 'page', 'direction'],
                 'scope' => null,
-                'sort' => null,
+                'sort' => 'PaginatorPosts.id',
             ]);
         $this->Paginator->paginate($query, $params, $settings);
     }
@@ -1257,7 +1296,7 @@ class PaginatorTest extends TestCase
                 'page' => 1,
                 'whitelist' => ['limit', 'sort', 'page', 'direction'],
                 'scope' => null,
-                'sort' => null,
+                'sort' => 'PaginatorPosts.id',
             ]);
         $this->Paginator->paginate($query, $params, $settings);
     }