Browse Source

Use a unit test instead of an integration test for sort directions.

We don't need to hit the database to test sort condition generation.
Mark Story 8 years ago
parent
commit
8991d7d6ce

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

@@ -944,70 +944,6 @@ class PaginatorComponentTest extends TestCase
     }
 
     /**
-     * test that multiple sort works in combination with query.
-     *
-     * @return void
-     */
-    public function testValidateSortMultipleWithQuery()
-    {
-        $this->loadFixtures('Posts');
-        $table = TableRegistry::get('PaginatorPosts');
-
-        $titleExtractor = function ($result) {
-            $ids = [];
-            foreach ($result as $record) {
-                $ids[] = $record->title;
-            }
-
-            return $ids;
-        };
-
-        // Define two columns as default sort order
-        $settings = [
-            'order' => [
-                'author_id' => 'asc',
-                'title' => 'asc'
-            ]
-        ];
-
-        // Test with empty query
-        $this->request->query = [];
-        $result = $this->Paginator->paginate($table, $settings);
-        $this->assertCount(3, $result, '3 rows should come back');
-        $this->assertEquals(['First Post', 'Third Post', 'Second Post'], $titleExtractor($result));
-        $this->assertEquals(
-            ['PaginatorPosts.author_id' => 'asc', 'PaginatorPosts.title' => 'asc'],
-            $this->request->params['paging']['PaginatorPosts']['completeSort']
-        );
-
-        // Test overwriting a sort field defined in the settings
-        $this->request->query = [
-            'sort' => 'author_id',
-            'direction' => 'desc'
-        ];
-        $result = $this->Paginator->paginate($table, $settings);
-        $this->assertCount(3, $result, '3 rows should come back');
-        $this->assertEquals(['Second Post', 'First Post', 'Third Post'], $titleExtractor($result));
-        $this->assertEquals(
-            ['PaginatorPosts.author_id' => 'desc', 'PaginatorPosts.title' => 'asc'],
-            $this->request->params['paging']['PaginatorPosts']['completeSort']
-        );
-
-        // Test sorting by a field not defined in the settings
-        $this->request->query = [
-            'sort' => 'id',
-            'direction' => 'asc'
-        ];
-        $result = $this->Paginator->paginate($table, $settings);
-        $this->assertCount(3, $result, '3 rows should come back');
-        $this->assertEquals(['First Post', 'Second Post', 'Third Post'], $titleExtractor($result));
-        $this->assertEquals(
-            ['PaginatorPosts.id' => 'asc', 'PaginatorPosts.author_id' => 'asc', 'PaginatorPosts.title' => 'asc'],
-            $this->request->params['paging']['PaginatorPosts']['completeSort']
-        );
-    }
-
-    /**
      * Tests that order strings can used by Paginator
      *
      * @return void

+ 64 - 46
tests/TestCase/Datasource/PaginatorTest.php

@@ -683,11 +683,7 @@ class PaginatorTest extends TestCase
      */
     public function testValidateSortWhitelistFailure()
     {
-        $model = $this->getMockBuilder('Cake\Datasource\RepositoryInterface')->getMock();
-        $model->expects($this->any())
-            ->method('alias')
-            ->will($this->returnValue('model'));
-        $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
+        $model = $this->mockAliasHasFieldModel();
 
         $options = [
             'sort' => 'body',
@@ -706,13 +702,7 @@ class PaginatorTest extends TestCase
      */
     public function testValidateSortWhitelistTrusted()
     {
-        $model = $this->getMockBuilder('Cake\Datasource\RepositoryInterface')->getMock();
-        $model->expects($this->any())
-            ->method('alias')
-            ->will($this->returnValue('model'));
-        $model->expects($this->once())
-            ->method('hasField')
-            ->will($this->returnValue(true));
+        $model = $this->mockAliasHasFieldModel();
 
         $options = [
             'sort' => 'body',
@@ -736,12 +726,7 @@ class PaginatorTest extends TestCase
      */
     public function testValidateSortWhitelistEmpty()
     {
-        $model = $this->getMockBuilder('Cake\Datasource\RepositoryInterface')->getMock();
-        $model->expects($this->any())
-            ->method('alias')
-            ->will($this->returnValue('model'));
-        $model->expects($this->any())->method('hasField')
-            ->will($this->returnValue(true));
+        $model = $this->mockAliasHasFieldModel();
 
         $options = [
             'order' => [
@@ -793,13 +778,7 @@ class PaginatorTest extends TestCase
      */
     public function testValidateSortWhitelistMultiple()
     {
-        $model = $this->getMockBuilder('Cake\Datasource\RepositoryInterface')->getMock();
-        $model->expects($this->any())
-            ->method('alias')
-            ->will($this->returnValue('model'));
-        $model->expects($this->once())
-            ->method('hasField')
-            ->will($this->returnValue(true));
+        $model = $this->mockAliasHasFieldModel();
 
         $options = [
             'order' => [
@@ -817,6 +796,19 @@ class PaginatorTest extends TestCase
         $this->assertEquals($expected, $result['order']);
     }
 
+    protected function mockAliasHasFieldModel()
+    {
+        $model = $this->getMockBuilder('Cake\Datasource\RepositoryInterface')->getMock();
+        $model->expects($this->any())
+            ->method('alias')
+            ->will($this->returnValue('model'));
+        $model->expects($this->any())
+            ->method('hasField')
+            ->will($this->returnValue(true));
+
+        return $model;
+    }
+
     /**
      * test that multiple sort works.
      *
@@ -824,11 +816,7 @@ class PaginatorTest extends TestCase
      */
     public function testValidateSortMultiple()
     {
-        $model = $this->getMockBuilder('Cake\Datasource\RepositoryInterface')->getMock();
-        $model->expects($this->any())
-            ->method('alias')
-            ->will($this->returnValue('model'));
-        $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
+        $model = $this->mockAliasHasFieldModel();
 
         $options = [
             'order' => [
@@ -846,17 +834,56 @@ class PaginatorTest extends TestCase
     }
 
     /**
+     * test that multiple sort adds in query data.
+     *
+     * @return void
+     */
+    public function testValidateSortMultipleWithQuery()
+    {
+        $model = $this->mockAliasHasFieldModel();
+
+        $options = [
+            'sort' => 'created',
+            'direction' => 'desc',
+            'order' => [
+                'author_id' => 'asc',
+                'title' => 'asc'
+            ]
+        ];
+        $result = $this->Paginator->validateSort($model, $options);
+
+        $expected = [
+            'model.created' => 'desc',
+            'model.author_id' => 'asc',
+            'model.title' => 'asc'
+        ];
+        $this->assertEquals($expected, $result['order']);
+
+        $options = [
+            'sort' => 'title',
+            'direction' => 'desc',
+            'order' => [
+                'author_id' => 'asc',
+                'title' => 'asc'
+            ]
+        ];
+        $result = $this->Paginator->validateSort($model, $options);
+
+        $expected = [
+            'model.title' => 'desc',
+            'model.author_id' => 'asc',
+        ];
+        $this->assertEquals($expected, $result['order']);
+    }
+
+    /**
      * Tests that order strings can used by Paginator
      *
      * @return void
      */
     public function testValidateSortWithString()
     {
-        $model = $this->getMockBuilder('Cake\Datasource\RepositoryInterface')->getMock();
-        $model->expects($this->any())
-            ->method('alias')
-            ->will($this->returnValue('model'));
-        $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
+        $model = $this->mockAliasHasFieldModel();
 
         $options = [
             'order' => 'model.author_id DESC'
@@ -874,12 +901,7 @@ class PaginatorTest extends TestCase
      */
     public function testValidateSortNoSort()
     {
-        $model = $this->getMockBuilder('Cake\Datasource\RepositoryInterface')->getMock();
-        $model->expects($this->any())
-            ->method('alias')
-            ->will($this->returnValue('model'));
-        $model->expects($this->any())->method('hasField')
-            ->will($this->returnValue(true));
+        $model = $this->mockAliasHasFieldModel();
 
         $options = [
             'direction' => 'asc',
@@ -896,11 +918,7 @@ class PaginatorTest extends TestCase
      */
     public function testValidateSortInvalidAlias()
     {
-        $model = $this->getMockBuilder('Cake\Datasource\RepositoryInterface')->getMock();
-        $model->expects($this->any())
-            ->method('alias')
-            ->will($this->returnValue('model'));
-        $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
+        $model = $this->mockAliasHasFieldModel();
 
         $options = ['sort' => 'Derp.id'];
         $result = $this->Paginator->validateSort($model, $options);