Browse Source

Fix paginator limit if query string exists

mscherer 6 years ago
parent
commit
7718399b6c

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

@@ -1268,7 +1268,8 @@ class PaginatorHelper extends Helper
         $out .= $this->Form->control('limit', $options + [
                 'type' => 'select',
                 'label' => __('View'),
-                'value' => $default,
+                'default' => $default,
+                'value' => $this->_View->getRequest()->getQuery('limit'),
                 'options' => $limits,
                 'onChange' => 'this.form.submit()'
             ]);

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

@@ -3174,4 +3174,59 @@ class PaginatorHelperTest extends TestCase
         ];
         $this->assertHtml($expected, $out);
     }
+    
+    /**
+     * test the limitControl() method with defaults and query
+     *
+     * @return void
+     */
+    public function testLimitControlQuery()
+    {
+        $out = $this->Paginator->limitControl([], 50);
+        $expected = [
+            ['form' => ['method' => 'get', 'accept-charset' => 'utf-8', 'action' => '/']],
+            ['div' => ['class' => 'input select']],
+            ['label' => ['for' => 'limit']],
+            'View',
+            '/label',
+            ['select' => ['name' => 'limit', 'id' => 'limit', 'onChange' => 'this.form.submit()']],
+            ['option' => ['value' => '20']],
+            '20',
+            '/option',
+            ['option' => ['value' => '50', 'selected' => 'selected']],
+            '50',
+            '/option',
+            ['option' => ['value' => '100']],
+            '100',
+            '/option',
+            '/select',
+            '/div',
+            '/form'
+        ];
+        $this->assertHtml($expected, $out);
+        
+        $this->View->setRequest($this->View->getRequest()->withQueryParams(['limit' => '100']));
+        $out = $this->Paginator->limitControl([], 50);
+        $expected = [
+            ['form' => ['method' => 'get', 'accept-charset' => 'utf-8', 'action' => '/']],
+            ['div' => ['class' => 'input select']],
+            ['label' => ['for' => 'limit']],
+            'View',
+            '/label',
+            ['select' => ['name' => 'limit', 'id' => 'limit', 'onChange' => 'this.form.submit()']],
+            ['option' => ['value' => '20']],
+            '20',
+            '/option',
+            ['option' => ['value' => '50']],
+            '50',
+            '/option',
+            ['option' => ['value' => '100', 'selected' => 'selected']],
+            '100',
+            '/option',
+            '/select',
+            '/div',
+            '/form'
+        ];
+        $this->assertHtml($expected, $out);
+    }
 }