@@ -934,13 +934,77 @@ public function testValidateSortMultiple()
934
934
]
935
935
];
936
936
$ result = $ this ->Paginator ->validateSort ($ model , $ options );
937
+
937
938
$ expected = [
938
939
'model.author_id ' => 'asc ' ,
939
940
'model.title ' => 'asc '
940
941
];
941
942
942
943
$ this ->assertEquals ($ expected , $ result ['order ' ]);
943
944
}
945
+
946
+ /**
947
+ * test that multiple sort works in combination with query.
948
+ *
949
+ * @return void
950
+ */
951
+ public function testValidateSortMultipleWithQuery ()
952
+ {
953
+ $ this ->loadFixtures ('Posts ' );
954
+ $ table = TableRegistry::get ('PaginatorPosts ' );
955
+
956
+ $ titleExtractor = function ($ result ) {
957
+ $ ids = [];
958
+ foreach ($ result as $ record ) {
959
+ $ ids [] = $ record ->title ;
960
+ }
961
+ return $ ids ;
962
+ };
963
+
964
+ // Define two columns as default sort order
965
+ $ settings = [
966
+ 'order ' => [
967
+ 'author_id ' => 'asc ' ,
968
+ 'title ' => 'asc '
969
+ ]
970
+ ];
971
+
972
+ // Test with empty query
973
+ $ this ->request ->query = [];
974
+ $ result = $ this ->Paginator ->paginate ($ table , $ settings );
975
+ $ this ->assertCount (3 , $ result , '3 rows should come back ' );
976
+ $ this ->assertEquals (['First Post ' , 'Third Post ' , 'Second Post ' ], $ titleExtractor ($ result ));
977
+ $ this ->assertEquals (
978
+ ['PaginatorPosts.author_id ' => 'asc ' , 'PaginatorPosts.title ' => 'asc ' ],
979
+ $ this ->request ->params ['paging ' ]['PaginatorPosts ' ]['totalOrder ' ]
980
+ );
981
+
982
+ // Test overwriting a sort field defined in the settings
983
+ $ this ->request ->query = [
984
+ 'sort ' => 'author_id ' ,
985
+ 'direction ' => 'desc '
986
+ ];
987
+ $ result = $ this ->Paginator ->paginate ($ table , $ settings );
988
+ $ this ->assertCount (3 , $ result , '3 rows should come back ' );
989
+ $ this ->assertEquals (['Second Post ' , 'First Post ' , 'Third Post ' ], $ titleExtractor ($ result ));
990
+ $ this ->assertEquals (
991
+ ['PaginatorPosts.author_id ' => 'desc ' , 'PaginatorPosts.title ' => 'asc ' ],
992
+ $ this ->request ->params ['paging ' ]['PaginatorPosts ' ]['totalOrder ' ]
993
+ );
994
+
995
+ // Test sorting by a field not defined in the settings
996
+ $ this ->request ->query = [
997
+ 'sort ' => 'id ' ,
998
+ 'direction ' => 'asc '
999
+ ];
1000
+ $ result = $ this ->Paginator ->paginate ($ table , $ settings );
1001
+ $ this ->assertCount (3 , $ result , '3 rows should come back ' );
1002
+ $ this ->assertEquals (['First Post ' , 'Second Post ' , 'Third Post ' ], $ titleExtractor ($ result ));
1003
+ $ this ->assertEquals (
1004
+ ['PaginatorPosts.id ' => 'asc ' , 'PaginatorPosts.author_id ' => 'asc ' , 'PaginatorPosts.title ' => 'asc ' ],
1005
+ $ this ->request ->params ['paging ' ]['PaginatorPosts ' ]['totalOrder ' ]
1006
+ );
1007
+ }
944
1008
945
1009
/**
946
1010
* Tests that order strings can used by Paginator
0 commit comments