@@ -810,4 +810,77 @@ class APICoreEndpointTestCase extends TestCase {
810810 $ this ->assert_equals ($ resp ->response_id , 'ENDPOINT_SORT_ORDER_FIELD_UNKNOWN_SORT_ORDER ' );
811811 $ this ->assert_equals ($ resp ->code , 400 );
812812 }
813+
814+ /**
815+ * Ensure the 'sort_flags' common control parameter must be a valid PHP sort constant.
816+ */
817+ public function test_sort_flags_must_be_php_sort_constant (): void {
818+ # Use a GET request to request a non-string sort order
819+ $ json_resp = \RESTAPI \Core \Tools \http_request (
820+ url: 'https://localhost/api/v2/firewall/aliases ' ,
821+ method: 'GET ' ,
822+ data: ['sort_by ' => 'name ' , 'sort_flags ' => 12345 ],
823+ headers: ['Content-Type ' => 'application/json ' ],
824+ username: 'admin ' ,
825+ password: 'pfsense ' ,
826+ validate_certs: false ,
827+ );
828+ # Ensure the response threw a ENDPOINT_SORT_FLAGS_FIELD_INVALID_TYPE error
829+ $ resp = json_decode ($ json_resp );
830+ $ this ->assert_equals ($ resp ->response_id , 'ENDPOINT_SORT_FLAGS_FIELD_INVALID_TYPE ' );
831+ $ this ->assert_equals ($ resp ->code , 400 );
832+
833+ # Make another request to sort order that is not known
834+ $ json_resp = \RESTAPI \Core \Tools \http_request (
835+ url: 'https://localhost/api/v2/firewall/aliases ' ,
836+ method: 'GET ' ,
837+ data: ['sort_by ' => 'name ' , 'sort_flags ' => 'unknown_sort_flags ' ],
838+ headers: ['Content-Type ' => 'application/json ' ],
839+ username: 'admin ' ,
840+ password: 'pfsense ' ,
841+ validate_certs: false ,
842+ );
843+
844+ # Ensure the response threw a ENDPOINT_SORT_FLAGS_FIELD_UNKNOWN_SORT_FLAGS error
845+ $ resp = json_decode ($ json_resp );
846+ $ this ->assert_equals ($ resp ->response_id , 'ENDPOINT_SORT_FLAGS_FIELD_UNKNOWN_SORT_FLAGS ' );
847+ $ this ->assert_equals ($ resp ->code , 400 );
848+ }
849+
850+ /**
851+ * Ensure clients can specify the 'sort_flags' common control parameter to sort the returned data.
852+ */
853+ public function test_sort_flags (): void {
854+ # Use a PUT request to create new aliases
855+ \RESTAPI \Core \Tools \http_request (
856+ url: 'https://localhost/api/v2/firewall/aliases ' ,
857+ method: 'PUT ' ,
858+ data: [
859+ ['name ' => 'alias_a ' , 'type ' => 'host ' , 'descr ' => '127.0.0.22 ' ],
860+ ['name ' => 'alias_b ' , 'type ' => 'host ' , 'descr ' => '127.0.0.2 ' ],
861+ ['name ' => 'alias_c ' , 'type ' => 'host ' , 'descr ' => '127.0.0.100 ' ],
862+ ['name ' => 'alias_d ' , 'type ' => 'host ' , 'descr ' => '127.0.0.10 ' ],
863+ ],
864+ headers: ['Content-Type ' => 'application/json ' ],
865+ username: 'admin ' ,
866+ password: 'pfsense ' ,
867+ validate_certs: false ,
868+ );
869+
870+ # Ensure we can make a successful GET request to a `many` endpoint with the 'sort_flags' control parameter
871+ $ json_resp = \RESTAPI \Core \Tools \http_request (
872+ url: 'https://localhost/api/v2/firewall/aliases ' ,
873+ method: 'GET ' ,
874+ data: ['sort_by ' => 'descr ' , 'sort_flags ' => 'SORT_NATURAL ' ],
875+ headers: ['Content-Type ' => 'application/json ' ],
876+ username: 'admin ' ,
877+ password: 'pfsense ' ,
878+ validate_certs: false ,
879+ );
880+ $ resp = json_decode ($ json_resp );
881+ $ this ->assert_equals ($ resp ->data [0 ]->name , 'alias_b ' );
882+ $ this ->assert_equals ($ resp ->data [1 ]->name , 'alias_d ' );
883+ $ this ->assert_equals ($ resp ->data [2 ]->name , 'alias_a ' );
884+ $ this ->assert_equals ($ resp ->data [3 ]->name , 'alias_c ' );
885+ }
813886}
0 commit comments