浏览代码

fix error in PaginatorHelper with scopes

Joel Montesinos 4 年之前
父节点
当前提交
3ba7a79347
共有 2 个文件被更改,包括 33 次插入7 次删除
  1. 5 7
      src/View/Helper/PaginatorHelper.php
  2. 28 0
      tests/TestCase/View/Helper/PaginatorHelperTest.php

+ 5 - 7
src/View/Helper/PaginatorHelper.php

@@ -567,20 +567,18 @@ class PaginatorHelper extends Helper
         ) {
             $options['sort'] = $options['direction'] = null;
         }
-
+        $baseUrl = $this->_config['options']['url'] ?? [];
         if (!empty($paging['scope'])) {
             $scope = $paging['scope'];
-            $currentParams = $this->_config['options']['url'];
-
-            if (isset($currentParams['?'][$scope]) && is_array($currentParams['?'][$scope])) {
-                $options += $currentParams['?'][$scope];
+            if (isset($baseUrl['?'][$scope]) && is_array($baseUrl['?'][$scope])) {
+                $options += $baseUrl['?'][$scope];
+                unset($baseUrl['?'][$scope]);
             }
             $options = [$scope => $options];
         }
 
         if (!empty($this->_config['options']['url'])) {
-            $key = implode('.', array_filter(['options.url', Hash::get($paging, 'scope', null)]));
-            $url = Hash::merge($url, Hash::get($this->_config, $key, []));
+            $url = Hash::merge($url, $baseUrl);
         }
 
         if (!isset($url['?'])) {

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

@@ -3495,4 +3495,32 @@ class PaginatorHelperTest extends TestCase
         }
         $this->View->setRequest($this->View->getRequest()->withAttribute('paging', $params));
     }
+
+    /**
+     * Test that generated URLs work without sort defined within the request
+     *
+     * @return void
+     */
+    public function testPaginationWithPassedParams()
+    {
+        $request = new ServerRequest([
+            'url' => '/articles/index/whatever/3',
+            'params' => [
+                'plugin' => null, 'controller' => 'articles', 'action' => 'index', 'pass' => ['whatever', '3']
+            ],
+            'base' => '',
+            'webroot' => '/',
+        ]);
+        Router::setRequest($request);
+
+        $this->View->setRequest($request->withAttribute('paging', [
+            'Article' => [
+                'scope' => 'article',
+            ],
+        ]));
+        $this->Paginator = new PaginatorHelper($this->View);
+        $result = $this->Paginator->generateUrl(['page' => 2]);
+        $expected = '/articles/index/whatever/3?article%5Bpage%5D=2';
+        static::assertSame($expected, $result);
+    }
 }