@@ -745,14 +745,10 @@ public function test_output_validation(): void {
745745 /**
746746 * Data provider for test_output_is_prepared_for_json_clients().
747747 *
748- * Each result is requested without global parameters, with `_fields` and `_embed`
749- * alone and together, and with a link that a `rest_post_dispatch` filter adds,
750- * alone and embedded.
751- *
752- * @return array<string, array{0: string, 1: array<string, string>, 2: bool, 3: string}>
748+ * @return array<string, array{0: string, 1: string}>
753749 */
754750 public static function data_output_is_prepared_for_json_clients (): array {
755- $ results = array (
751+ return array (
756752 'root empty object ' => array ( 'test/empty-object-output ' , '{} ' ),
757753 'nested empty object ' => array ( 'test/nested-empty-object-output ' , '{"settings":{}} ' ),
758754 'numeric collection ' => array ( 'test/empty-object-list-output ' , '[{}] ' ),
@@ -762,93 +758,68 @@ public static function data_output_is_prepared_for_json_clients(): array {
762758 'list of strings ' => array ( 'test/string-list-output ' , '["a","b"] ' ),
763759 'string ' => array ( 'test/string-output ' , '"value" ' ),
764760 );
765-
766- // No result has the `other` field, so filtering would leave none of the result.
767- $ requests = array (
768- 'no global parameters ' => array ( array (), false ),
769- '_fields ' => array ( array ( '_fields ' => 'other ' ), false ),
770- '_embed ' => array ( array ( '_embed ' => '1 ' ), false ),
771- '_fields and _embed ' => array (
772- array (
773- '_fields ' => 'other ' ,
774- '_embed ' => '1 ' ,
775- ),
776- false ,
777- ),
778- 'response link ' => array ( array (), true ),
779- 'response link with _embed ' => array ( array ( '_embed ' => '1 ' ), true ),
780- );
781-
782- $ data = array ();
783- foreach ( $ results as $ result_label => $ result ) {
784- foreach ( $ requests as $ request_label => $ request ) {
785- $ data [ "{$ result_label }, {$ request_label }" ] = array ( $ result [0 ], $ request [0 ], $ request [1 ], $ result [1 ] );
786- }
787- }
788-
789- return $ data ;
790761 }
791762
792763 /**
793- * Tests that ability results are prepared for JSON clients, and that the global
794- * `_fields` and `_embed` parameters and response links leave them unchanged.
764+ * Tests that ability results are prepared for JSON clients.
795765 *
796766 * @ticket 66267
797767 * @dataProvider data_output_is_prepared_for_json_clients
798768 *
799- * @param string $ability_name The ability name.
800- * @param array<string, string> $query_params The global parameters sent with the request.
801- * @param bool $add_link Whether a `rest_post_dispatch` filter adds an embeddable link.
802- * @param string $expected The expected encoded result.
769+ * @param string $ability_name The ability name.
770+ * @param string $expected The expected encoded result.
803771 */
804- public function test_output_is_prepared_for_json_clients ( string $ ability_name , array $ query_params , bool $ add_link , string $ expected ): void {
805- $ request = new WP_REST_Request ( 'POST ' , "/wp-abilities/v1/abilities/ {$ ability_name }/run " );
806- $ request ->set_query_params ( $ query_params );
807-
808- if ( $ add_link ) {
809- add_filter (
810- 'rest_post_dispatch ' ,
811- static function ( $ response ) {
812- $ response ->add_link ( 'alternate ' , rest_url ( '/wp-abilities/v1/abilities ' ), array ( 'embeddable ' => true ) );
813- return $ response ;
814- }
815- );
816- }
817-
772+ public function test_output_is_prepared_for_json_clients ( string $ ability_name , string $ expected ): void {
773+ $ request = new WP_REST_Request ( 'POST ' , "/wp-abilities/v1/abilities/ {$ ability_name }/run " );
818774 $ response = $ this ->server ->dispatch ( $ request );
819775
820776 $ this ->assertSame ( 200 , $ response ->get_status (), 'The ability should run successfully. ' );
821-
822- add_filter ( 'rest_post_dispatch ' , 'rest_filter_response_fields ' , 10 , 3 );
823- $ response = apply_filters ( 'rest_post_dispatch ' , $ response , $ this ->server , $ request );
824- $ embed = $ request ->has_param ( '_embed ' ) ? rest_parse_embed_param ( $ request ['_embed ' ] ) : false ;
825- $ data = $ this ->server ->response_to_data ( $ response , $ embed );
826-
827- $ this ->assertSame ( $ expected , wp_json_encode ( $ data ), 'The result should be encoded as expected. ' );
777+ $ this ->assertSame ( $ expected , wp_json_encode ( $ response ->get_data () ), 'The result should be encoded as expected. ' );
828778 }
829779
830780 /**
831- * Data provider for test_links_in_the_result_are_not_embedded().
781+ * Tests that the global `_fields` and `_embed` parameters and response links leave
782+ * ability results unchanged.
832783 *
833- * @return array<string, array{0: string}>
784+ * @ticket 66267
834785 */
835- public static function data_links_in_the_result_are_not_embedded (): array {
836- return array (
837- 'every link ' => array ( '1 ' ),
838- 'the link relation ' => array ( 'author ' ),
786+ public function test_global_parameters_and_response_links_leave_output_unchanged (): void {
787+ $ request = new WP_REST_Request ( 'POST ' , '/wp-abilities/v1/abilities/test/nested-empty-object-output/run ' );
788+
789+ // The result has no `other` field, so filtering would leave none of the result.
790+ $ request ->set_query_params (
791+ array (
792+ '_fields ' => 'other ' ,
793+ '_embed ' => '1 ' ,
794+ )
839795 );
796+
797+ add_filter (
798+ 'rest_post_dispatch ' ,
799+ static function ( $ response ) {
800+ $ response ->add_link ( 'alternate ' , rest_url ( '/wp-abilities/v1/abilities ' ), array ( 'embeddable ' => true ) );
801+ return $ response ;
802+ }
803+ );
804+
805+ $ response = $ this ->server ->dispatch ( $ request );
806+
807+ $ this ->assertSame ( 200 , $ response ->get_status (), 'The ability should run successfully. ' );
808+
809+ add_filter ( 'rest_post_dispatch ' , 'rest_filter_response_fields ' , 10 , 3 );
810+ $ response = apply_filters ( 'rest_post_dispatch ' , $ response , $ this ->server , $ request );
811+ $ data = $ this ->server ->response_to_data ( $ response , rest_parse_embed_param ( $ request ['_embed ' ] ) );
812+
813+ $ this ->assertSame ( '{"settings":{}} ' , wp_json_encode ( $ data ), 'The result should be left unchanged. ' );
840814 }
841815
842816 /**
843817 * Tests that the global `_embed` parameter leaves the `_links` returned by an ability
844818 * unchanged, without requesting the linked resources.
845819 *
846820 * @ticket 66267
847- * @dataProvider data_links_in_the_result_are_not_embedded
848- *
849- * @param string $embed The `_embed` parameter sent with the request.
850821 */
851- public function test_links_in_the_result_are_not_embedded ( string $ embed ): void {
822+ public function test_links_in_the_result_are_not_embedded (): void {
852823 $ result = array (
853824 'id ' => 1 ,
854825 '_links ' => array (
@@ -880,7 +851,7 @@ public function test_links_in_the_result_are_not_embedded( string $embed ): void
880851 );
881852
882853 $ request = new WP_REST_Request ( 'POST ' , '/wp-abilities/v1/abilities/test/links-output/run ' );
883- $ request ->set_query_params ( array ( '_embed ' => $ embed ) );
854+ $ request ->set_query_params ( array ( '_embed ' => ' 1 ' ) );
884855
885856 $ response = $ this ->server ->dispatch ( $ request );
886857
@@ -894,42 +865,24 @@ public function test_links_in_the_result_are_not_embedded( string $embed ): void
894865 $ this ->assertSame ( 0 , $ dispatch ->get_call_count (), 'No linked resource should be requested. ' );
895866 }
896867
897- /**
898- * Data provider for test_preloaded_output_is_prepared_for_json_clients().
899- *
900- * @return array<string, array{0: array<string, string>, 1: mixed, 2: string, 3: string}>
901- */
902- public static function data_preloaded_output_is_prepared_for_json_clients (): array {
903- return array (
904- 'empty object ' => array ( array ( 'type ' => 'object ' ), array (), '' , '{} ' ),
905- 'empty object, _fields and _embed ' => array ( array ( 'type ' => 'object ' ), array (), '?_fields=other&_embed=1 ' , '{} ' ),
906- 'string ' => array ( array ( 'type ' => 'string ' ), 'value ' , '' , '"value" ' ),
907- 'string, _fields and _embed ' => array ( array ( 'type ' => 'string ' ), 'value ' , '?_fields=other&_embed=1 ' , '"value" ' ),
908- );
909- }
910-
911868 /**
912869 * Tests that preloading a read-only ability run keeps the result prepared for
913870 * JSON clients, also with the global `_fields` and `_embed` parameters.
914871 *
915872 * @ticket 66267
916- * @dataProvider data_preloaded_output_is_prepared_for_json_clients
917- *
918- * @param array<string, string> $output_schema The ability output schema.
919- * @param mixed $result The ability result.
920- * @param string $query The query string of the preloaded path.
921- * @param string $expected The expected encoded preloaded body.
922873 */
923- public function test_preloaded_output_is_prepared_for_json_clients ( array $ output_schema , $ result , string $ query , string $ expected ): void {
874+ public function test_preloaded_output_is_prepared_for_json_clients (): void {
924875 $ this ->register_test_ability (
925876 'test/readonly-output ' ,
926877 array (
927878 'label ' => 'Read-only Output ' ,
928- 'description ' => 'Returns a value from a read-only ability. ' ,
879+ 'description ' => 'Returns an empty object from a read-only ability. ' ,
929880 'category ' => 'general ' ,
930- 'output_schema ' => $ output_schema ,
931- 'execute_callback ' => static function () use ( $ result ) {
932- return $ result ;
881+ 'output_schema ' => array (
882+ 'type ' => 'object ' ,
883+ ),
884+ 'execute_callback ' => static function () {
885+ return array ();
933886 },
934887 'permission_callback ' => '__return_true ' ,
935888 'meta ' => array (
@@ -941,11 +894,11 @@ public function test_preloaded_output_is_prepared_for_json_clients( array $outpu
941894 )
942895 );
943896
944- $ path = '/wp-abilities/v1/abilities/test/readonly-output/run ' . $ query ;
897+ $ path = '/wp-abilities/v1/abilities/test/readonly-output/run?_fields=other&_embed=1 ' ;
945898 $ preload = rest_preload_api_request ( array (), $ path );
946899
947900 $ this ->assertArrayHasKey ( $ path , $ preload , 'The ability run should be preloaded. ' );
948- $ this ->assertSame ( $ expected , wp_json_encode ( $ preload [ $ path ]['body ' ] ), 'The preloaded result should be encoded as expected . ' );
901+ $ this ->assertSame ( ' {} ' , wp_json_encode ( $ preload [ $ path ]['body ' ] ), 'The preloaded result should be encoded as an empty object . ' );
949902 }
950903
951904 /**
0 commit comments