Browse Source

Merge pull request #9849 from gorogoroyasu/issue9848-cakephp-paginatorcomponent-maxlimit

#9848 PaginatorComponent.php does not cap limit to maxLimit
Mark Story 9 years ago
parent
commit
f188cf83db

+ 18 - 12
src/Controller/Component/PaginatorComponent.php

@@ -287,25 +287,31 @@ class PaginatorComponent extends Component
     }
 
     /**
-     * Get the default settings for a $model. If there are no settings for a specific model, the general settings
+     * Get the settings for a $model. If there are no settings for a specific model, the general settings
      * will be used.
      *
-     * @param string $alias Model name to get default settings for.
-     * @param array $defaults The defaults to use for combining settings.
-     * @return array An array of pagination defaults for a model, or the general settings.
+     * @param string $alias Model name to get settings for.
+     * @param array $settings The settings which is used for combining.
+     * @return array An array of pagination settings for a model, or the general settings.
      */
-    public function getDefaults($alias, $defaults)
+    public function getDefaults($alias, $settings)
     {
-        if (isset($defaults[$alias])) {
-            $defaults = $defaults[$alias];
+        if (isset($settings[$alias])) {
+            $settings = $settings[$alias];
         }
-        if (isset($defaults['limit']) &&
-            (empty($defaults['maxLimit']) || $defaults['limit'] > $defaults['maxLimit'])
-        ) {
-            $defaults['maxLimit'] = $defaults['limit'];
+
+        $defaults = $this->config();
+        $maxLimit = isset($settings['maxLimit']) ? $settings['maxLimit'] : $defaults['maxLimit'];
+        $limit = isset($settings['limit']) ? $settings['limit'] : $defaults['limit'];
+
+        if ($limit > $maxLimit) {
+            $limit = $maxLimit;
         }
 
-        return $defaults + $this->config();
+        $settings['maxLimit'] = $maxLimit;
+        $settings['limit'] = $limit;
+
+        return $settings + $defaults;
     }
 
     /**

+ 51 - 3
tests/TestCase/Controller/Component/PaginatorComponentTest.php

@@ -480,8 +480,8 @@ class PaginatorComponentTest extends TestCase
         $result = $this->Paginator->mergeOptions('Post', $settings);
         $expected = [
             'page' => 1,
-            'limit' => 200,
-            'maxLimit' => 200,
+            'limit' => 100,
+            'maxLimit' => 100,
             'paramType' => 'named',
             'whitelist' => ['limit', 'sort', 'page', 'direction']
         ];
@@ -494,7 +494,7 @@ class PaginatorComponentTest extends TestCase
         $result = $this->Paginator->mergeOptions('Post', $settings);
         $expected = [
             'page' => 1,
-            'limit' => 20,
+            'limit' => 10,
             'maxLimit' => 10,
             'paramType' => 'named',
             'whitelist' => ['limit', 'sort', 'page', 'direction']
@@ -503,6 +503,54 @@ class PaginatorComponentTest extends TestCase
     }
 
     /**
+     * test getDefaults with limit > maxLimit in code.
+     *
+     * @return void
+     */
+    public function testGetDefaultMaxLimit()
+    {
+        $settings = [
+            'page' => 1,
+            'limit' => 2,
+            'maxLimit' => 10,
+            'order' => [
+                'Users.username' => 'asc'
+            ],
+        ];
+        $result = $this->Paginator->mergeOptions('Post', $settings);
+        $expected = [
+            'page' => 1,
+            'limit' => 2,
+            'maxLimit' => 10,
+            'order' => [
+                'Users.username' => 'asc'
+            ],
+            'whitelist' => ['limit', 'sort', 'page', 'direction']
+        ];
+        $this->assertEquals($expected, $result);
+
+        $settings = [
+            'page' => 1,
+            'limit' => 100,
+            'maxLimit' => 10,
+            'order' => [
+                'Users.username' => 'asc'
+            ],
+        ];
+        $result = $this->Paginator->mergeOptions('Post', $settings);
+        $expected = [
+            'page' => 1,
+            'limit' => 10,
+            'maxLimit' => 10,
+            'order' => [
+                'Users.username' => 'asc'
+            ],
+            'whitelist' => ['limit', 'sort', 'page', 'direction']
+        ];
+        $this->assertEquals($expected, $result);
+    }
+
+    /**
      * Integration test to ensure that validateSort is being used by paginate()
      *
      * @return void