Browse Source

Fix regression about other aliases to be sorted.

Mark Scherer 10 years ago
parent
commit
449a857164

+ 3 - 1
src/Controller/Component/PaginatorComponent.php

@@ -357,7 +357,9 @@ class PaginatorComponent extends Component
                 }
                 $correctAlias = ($tableAlias === $alias);
 
-                if ($correctAlias && (!$validate || $object->hasField($field))) {
+                if (!$correctAlias && !$validate) {
+                    $tableOrder[$alias . '.' . $field] = $value;
+                } elseif ($correctAlias && (!$validate || $object->hasField($field))) {
                     $tableOrder[$tableAlias . '.' . $field] = $value;
                 }
             }

+ 29 - 0
tests/TestCase/Controller/Component/PaginatorComponentTest.php

@@ -569,6 +569,35 @@ class PaginatorComponentTest extends TestCase
     }
 
     /**
+     * test that multiple fields in the whitelist are not validated and properly aliased.
+     *
+     * @return void
+     */
+    public function testValidateSortWhitelistMultiple()
+    {
+        $model = $this->getMock('Cake\ORM\Table');
+        $model->expects($this->any())
+            ->method('alias')
+            ->will($this->returnValue('model'));
+        $model->expects($this->never())->method('hasField');
+
+        $options = [
+            'order' => [
+                'body' => 'asc',
+                'foo.bar' => 'asc'
+            ],
+            'sortWhitelist' => ['body', 'foo.bar']
+        ];
+        $result = $this->Paginator->validateSort($model, $options);
+
+        $expected = [
+            'model.body' => 'asc',
+            'foo.bar' => 'asc'
+        ];
+        $this->assertEquals($expected, $result['order']);
+    }
+
+    /**
      * test that multiple sort works.
      *
      * @return void