Make WordPress Core

Changeset 59970


Ignore:
Timestamp:
03/11/2025 02:17:41 PM (12 days ago)
Author:
TimothyBlynJacobs
Message:

REST API: Fix fatal error when making HEAD requests with _fields filter.

In [59889] the REST API controllers were adjusted to perform less work when responding to HEAD requests. The WP_REST_Response body would now be null, which caused issues with filters that expected the response body to be an array.

This commit sets the response body to be an empty array when preparing the response instead. The body will still be discarded, but this provides better backward comppatibility with code that assumes an array will be used.

See #56481.
Props antonvlasenko, timothyblynjacobs, mamaduka, wildworks.

Location:
trunk
Files:
38 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php

    r59899 r59970  
    308308        if ( $request->is_method( 'HEAD' ) ) {
    309309            // Return early as this handler doesn't add any response headers.
    310             return new WP_REST_Response();
     310            return new WP_REST_Response( array() );
    311311        }
    312312        $response  = array();
     
    456456        if ( $request->is_method( 'HEAD' ) ) {
    457457            /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php */
    458             return apply_filters( 'rest_prepare_autosave', new WP_REST_Response(), $post, $request );
     458            return apply_filters( 'rest_prepare_autosave', new WP_REST_Response( array() ), $post, $request );
    459459        }
    460460        $response = $this->revisions_controller->prepare_item_for_response( $post, $request );
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-pattern-categories-controller.php

    r59899 r59970  
    8484        if ( $request->is_method( 'HEAD' ) ) {
    8585            // Return early as this handler doesn't add any response headers.
    86             return new WP_REST_Response();
     86            return new WP_REST_Response( array() );
    8787        }
    8888
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php

    r59899 r59970  
    134134        if ( $request->is_method( 'HEAD' ) ) {
    135135            // Return early as this handler doesn't add any response headers.
    136             return new WP_REST_Response();
     136            return new WP_REST_Response( array() );
    137137        }
    138138
     
    259259        if ( $request->is_method( 'HEAD' ) ) {
    260260            /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php */
    261             return apply_filters( 'rest_prepare_block_type', new WP_REST_Response(), $block_type, $request );
     261            return apply_filters( 'rest_prepare_block_type', new WP_REST_Response( array() ), $block_type, $request );
    262262        }
    263263
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    r59899 r59970  
    314314        }
    315315
    316         $response = $is_head_request ? new WP_REST_Response() : rest_ensure_response( $comments );
     316        $response = $is_head_request ? new WP_REST_Response( array() ) : rest_ensure_response( $comments );
    317317        $response->header( 'X-WP-Total', $total_comments );
    318318        $response->header( 'X-WP-TotalPages', $max_pages );
     
    10551055        if ( $request->is_method( 'HEAD' ) ) {
    10561056            /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php */
    1057             return apply_filters( 'rest_prepare_comment', new WP_REST_Response(), $comment, $request );
     1057            return apply_filters( 'rest_prepare_comment', new WP_REST_Response( array() ), $comment, $request );
    10581058        }
    10591059
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-font-collections-controller.php

    r59899 r59970  
    114114        }
    115115
    116         $response = $is_head_request ? new WP_REST_Response() : rest_ensure_response( $items );
     116        $response = $is_head_request ? new WP_REST_Response( array() ) : rest_ensure_response( $items );
    117117
    118118        $response->header( 'X-WP-Total', (int) $total_items );
     
    194194            if ( $request->is_method( 'HEAD' ) ) {
    195195                /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-font-collections-controller.php */
    196                 return apply_filters( 'rest_prepare_font_collection', new WP_REST_Response(), $item, $request );
     196                return apply_filters( 'rest_prepare_font_collection', new WP_REST_Response( array() ), $item, $request );
    197197            }
    198198
     
    210210        if ( $request->is_method( 'HEAD' ) ) {
    211211            /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-font-collections-controller.php */
    212             return apply_filters( 'rest_prepare_font_collection', new WP_REST_Response(), $item, $request );
     212            return apply_filters( 'rest_prepare_font_collection', new WP_REST_Response( array() ), $item, $request );
    213213        }
    214214
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php

    r59899 r59970  
    249249            $response = rest_ensure_response( $response );
    250250        } else {
    251             $response = new WP_REST_Response();
     251            $response = new WP_REST_Response( array() );
    252252        }
    253253
     
    292292        // Don't prepare the response body for HEAD requests.
    293293        if ( $request->is_method( 'HEAD' ) ) {
    294             return new WP_REST_Response();
     294            return new WP_REST_Response( array() );
    295295        }
    296296
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php

    r59899 r59970  
    164164        if ( $request->is_method( 'HEAD' ) ) {
    165165            // Return early as this handler doesn't add any response headers.
    166             return new WP_REST_Response();
     166            return new WP_REST_Response( array() );
    167167        }
    168168
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php

    r59899 r59970  
    112112        if ( $request->is_method( 'HEAD' ) ) {
    113113            // Return early as this handler doesn't add any response headers.
    114             return new WP_REST_Response();
     114            return new WP_REST_Response( array() );
    115115        }
    116116
     
    187187        if ( $request->is_method( 'HEAD' ) ) {
    188188            /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php */
    189             return apply_filters( 'rest_prepare_post_type', new WP_REST_Response(), $post_type, $request );
     189            return apply_filters( 'rest_prepare_post_type', new WP_REST_Response( array() ), $post_type, $request );
    190190        }
    191191
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r59899 r59970  
    500500        }
    501501
    502         $response = $is_head_request ? new WP_REST_Response() : rest_ensure_response( $posts );
     502        $response = $is_head_request ? new WP_REST_Response( array() ) : rest_ensure_response( $posts );
    503503
    504504        $response->header( 'X-WP-Total', (int) $total_posts );
     
    18481848        if ( $request->is_method( 'HEAD' ) ) {
    18491849            /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
    1850             return apply_filters( "rest_prepare_{$this->post_type}", new WP_REST_Response(), $post, $request );
     1850            return apply_filters( "rest_prepare_{$this->post_type}", new WP_REST_Response( array() ), $post, $request );
    18511851        }
    18521852
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php

    r59899 r59970  
    356356            $response = rest_ensure_response( $response );
    357357        } else {
    358             $response = new WP_REST_Response();
     358            $response = new WP_REST_Response( array() );
    359359        }
    360360
     
    592592        if ( $request->is_method( 'HEAD' ) ) {
    593593            /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php */
    594             return apply_filters( 'rest_prepare_revision', new WP_REST_Response(), $post, $request );
     594            return apply_filters( 'rest_prepare_revision', new WP_REST_Response( array() ), $post, $request );
    595595        }
    596596
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php

    r59899 r59970  
    166166        }
    167167
    168         $response = $is_head_request ? new WP_REST_Response() : rest_ensure_response( $results );
     168        $response = $is_head_request ? new WP_REST_Response( array() ) : rest_ensure_response( $results );
    169169        $response->header( 'X-WP-Total', $total );
    170170        $response->header( 'X-WP-TotalPages', $max_pages );
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-sidebars-controller.php

    r59899 r59970  
    122122        if ( $request->is_method( 'HEAD' ) ) {
    123123            // Return early as this handler doesn't add any response headers.
    124             return new WP_REST_Response();
     124            return new WP_REST_Response( array() );
    125125        }
    126126
     
    330330        if ( $request->is_method( 'HEAD' ) ) {
    331331            /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-sidebars-controller.php */
    332             return apply_filters( 'rest_prepare_sidebar', new WP_REST_Response(), $raw_sidebar, $request );
     332            return apply_filters( 'rest_prepare_sidebar', new WP_REST_Response( array() ), $raw_sidebar, $request );
    333333        }
    334334
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php

    r59899 r59970  
    116116        if ( $request->is_method( 'HEAD' ) ) {
    117117            // Return early as this handler doesn't add any response headers.
    118             return new WP_REST_Response();
     118            return new WP_REST_Response( array() );
    119119        }
    120120
     
    218218        if ( $request->is_method( 'HEAD' ) ) {
    219219            /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php */
    220             return apply_filters( 'rest_prepare_taxonomy', new WP_REST_Response(), $taxonomy, $request );
     220            return apply_filters( 'rest_prepare_taxonomy', new WP_REST_Response( array() ), $taxonomy, $request );
    221221        }
    222222
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php

    r59899 r59970  
    272272        if ( $request->is_method( 'HEAD' ) ) {
    273273            // Return early as this handler doesn't add any response headers.
    274             return new WP_REST_Response();
     274            return new WP_REST_Response( array() );
    275275        }
    276276
     
    676676        // Don't prepare the response body for HEAD requests.
    677677        if ( $request->is_method( 'HEAD' ) ) {
    678             return new WP_REST_Response();
     678            return new WP_REST_Response( array() );
    679679        }
    680680
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php

    r59899 r59970  
    371371        }
    372372
    373         $response = $is_head_request ? new WP_REST_Response() : rest_ensure_response( $response );
     373        $response = $is_head_request ? new WP_REST_Response( array() ) : rest_ensure_response( $response );
    374374
    375375        // Store pagination values for headers.
     
    900900        if ( $request->is_method( 'HEAD' ) ) {
    901901            /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
    902             return apply_filters( "rest_prepare_{$this->taxonomy}", new WP_REST_Response(), $item, $request );
     902            return apply_filters( "rest_prepare_{$this->taxonomy}", new WP_REST_Response( array() ), $item, $request );
    903903        }
    904904
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

    r59899 r59970  
    385385        }
    386386
    387         $response = $is_head_request ? new WP_REST_Response() : rest_ensure_response( $users );
     387        $response = $is_head_request ? new WP_REST_Response( array() ) : rest_ensure_response( $users );
    388388
    389389        // Store pagination values for headers then unset for count query.
     
    10331033        if ( $request->is_method( 'HEAD' ) ) {
    10341034            /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php */
    1035             return apply_filters( 'rest_prepare_user', new WP_REST_Response(), $user, $request );
     1035            return apply_filters( 'rest_prepare_user', new WP_REST_Response( array() ), $user, $request );
    10361036        }
    10371037
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php

    r59899 r59970  
    148148        if ( $request->is_method( 'HEAD' ) ) {
    149149            // Return early as this handler doesn't add any response headers.
    150             return new WP_REST_Response();
     150            return new WP_REST_Response( array() );
    151151        }
    152152
     
    307307        if ( $request->is_method( 'HEAD' ) ) {
    308308            /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php */
    309             return apply_filters( 'rest_prepare_widget_type', new WP_REST_Response(), $widget_type, $request );
     309            return apply_filters( 'rest_prepare_widget_type', new WP_REST_Response( array() ), $widget_type, $request );
    310310        }
    311311
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php

    r59899 r59970  
    139139        if ( $request->is_method( 'HEAD' ) ) {
    140140            // Return early as this handler doesn't add any response headers.
    141             return new WP_REST_Response();
     141            return new WP_REST_Response( array() );
    142142        }
    143143
     
    687687        if ( $request->is_method( 'HEAD' ) ) {
    688688            /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php */
    689             return apply_filters( 'rest_prepare_widget', new WP_REST_Response(), $widget, $request );
     689            return apply_filters( 'rest_prepare_widget', new WP_REST_Response( array() ), $widget, $request );
    690690        }
    691691
  • trunk/tests/phpunit/tests/fonts/font-library/wpRestFontCollectionsController.php

    r59899 r59970  
    109109        }
    110110
    111         $this->assertNull( $content, 'The response should be empty.' );
     111        $this->assertSame( array(), $content, 'The response should be empty.' );
    112112        $headers = $response->get_headers();
    113113        $this->assertArrayHasKey( 'X-WP-Total', $headers, 'The "X-WP-Total" header should be present in the response.' );
     
    173173            return null;
    174174        }
    175         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     175        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
    176176    }
    177177
  • trunk/tests/phpunit/tests/rest-api/rest-autosaves-controller.php

    r59899 r59970  
    197197        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
    198198        $this->assertSame( 0, $filter->get_call_count(), 'The "' . $hook_name . '" filter was called when it should not be for HEAD requests.' );
    199         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     199        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
    200200    }
    201201
     
    315315            return null;
    316316        }
    317         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     317        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
    318318    }
    319319
     
    890890        $this->assertSame( $autosave['id'], $data['id'], 'Original autosave was not returned' );
    891891    }
     892
     893    /**
     894     * @dataProvider data_head_request_with_specified_fields_returns_success_response
     895     * @ticket 56481
     896     *
     897     * @param string $path The path to test.
     898     */
     899    public function test_head_request_with_specified_fields_returns_success_response( $path ) {
     900        wp_set_current_user( self::$editor_id );
     901        $request = new WP_REST_Request( 'HEAD', sprintf( $path, self::$post_id, self::$autosave_post_id ) );
     902        $request->set_param( '_fields', 'id' );
     903        $server   = rest_get_server();
     904        $response = $server->dispatch( $request );
     905        add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
     906        $response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
     907        remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
     908
     909        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
     910    }
     911
     912    /**
     913     * Data provider intended to provide paths for testing HEAD requests.
     914     *
     915     * @return array
     916     */
     917    public static function data_head_request_with_specified_fields_returns_success_response() {
     918        return array(
     919            'get_item request'  => array( '/wp/v2/posts/%d/autosaves/%d' ),
     920            'get_items request' => array( '/wp/v2/posts/%d' ),
     921        );
     922    }
    892923}
  • trunk/tests/phpunit/tests/rest-api/rest-block-type-controller.php

    r59899 r59970  
    637637            return null;
    638638        }
    639         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     639        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
    640640    }
    641641
     
    660660        $response = rest_get_server()->dispatch( $request );
    661661        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
    662         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     662        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     663    }
     664
     665    /**
     666     * @dataProvider data_head_request_with_specified_fields_returns_success_response
     667     * @ticket 56481
     668     *
     669     * @param string $path The path to test.
     670     */
     671    public function test_head_request_with_specified_fields_returns_success_response( $path ) {
     672        wp_set_current_user( self::$admin_id );
     673        $request = new WP_REST_Request( 'HEAD', $path );
     674        $request->set_param( '_fields', 'title' );
     675        $server   = rest_get_server();
     676        $response = $server->dispatch( $request );
     677        add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
     678        $response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
     679        remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
     680
     681        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
     682    }
     683
     684    /**
     685     * Data provider intended to provide paths for testing HEAD requests.
     686     *
     687     * @return array
     688     */
     689    public static function data_head_request_with_specified_fields_returns_success_response() {
     690        return array(
     691            'get_item request'  => array( '/wp/v2/block-types/fake/test' ),
     692            'get_items request' => array( '/wp/v2/block-types' ),
     693        );
    663694    }
    664695
  • trunk/tests/phpunit/tests/rest-api/rest-categories-controller.php

    r59899 r59970  
    13611361            return null;
    13621362        }
    1363         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     1363        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     1364    }
     1365
     1366    /**
     1367     * @dataProvider data_head_request_with_specified_fields_returns_success_response
     1368     * @ticket 56481
     1369     *
     1370     * @param string $path The path to test.
     1371     */
     1372    public function test_head_request_with_specified_fields_returns_success_response( $path ) {
     1373        $request = new WP_REST_Request( 'HEAD', $path );
     1374        $request->set_param( '_fields', 'id' );
     1375        $server   = rest_get_server();
     1376        $response = $server->dispatch( $request );
     1377        add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
     1378        $response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
     1379        remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
     1380
     1381        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
     1382    }
     1383
     1384    /**
     1385     * Data provider intended to provide paths for testing HEAD requests.
     1386     *
     1387     * @return array
     1388     */
     1389    public static function data_head_request_with_specified_fields_returns_success_response() {
     1390        return array(
     1391            'get_item request'  => array( '/wp/v2/categories/1' ),
     1392            'get_items request' => array( '/wp/v2/categories' ),
     1393        );
    13641394    }
    13651395}
  • trunk/tests/phpunit/tests/rest-api/rest-comments-controller.php

    r59899 r59970  
    35683568            return null;
    35693569        }
    3570         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     3570        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     3571    }
     3572
     3573    /**
     3574     * @dataProvider data_head_request_with_specified_fields_returns_success_response
     3575     * @ticket 56481
     3576     *
     3577     * @param string $path The path to test.
     3578     */
     3579    public function test_head_request_with_specified_fields_returns_success_response( $path ) {
     3580        $request = new WP_REST_Request( 'HEAD', sprintf( $path, self::$approved_id ) );
     3581        $request->set_param( '_fields', 'id' );
     3582        $server   = rest_get_server();
     3583        $response = $server->dispatch( $request );
     3584        add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
     3585        $response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
     3586        remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
     3587
     3588        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
     3589    }
     3590
     3591    /**
     3592     * Data provider intended to provide paths for testing HEAD requests.
     3593     *
     3594     * @return array
     3595     */
     3596    public static function data_head_request_with_specified_fields_returns_success_response() {
     3597        return array(
     3598            'get_item request'  => array( '/wp/v2/comments/%d' ),
     3599            'get_items request' => array( '/wp/v2/comments' ),
     3600        );
    35713601    }
    35723602}
  • trunk/tests/phpunit/tests/rest-api/rest-global-styles-revisions-controller.php

    r59899 r59970  
    337337        $response = rest_get_server()->dispatch( $request );
    338338        $this->assertSame( 200, $response->get_status(), 'Response status is 200.' );
    339         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     339        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     340    }
     341
     342    /**
     343     * @dataProvider data_head_request_with_specified_fields_returns_success_response
     344     * @ticket 56481
     345     *
     346     * @param string $path The path to test.
     347     */
     348    public function test_head_request_with_specified_fields_returns_success_response( $path ) {
     349        wp_set_current_user( self::$admin_id );
     350        $request = new WP_REST_Request( 'GET', sprintf( $path, self::$global_styles_id, $this->revision_1_id ) );
     351        $request->set_param( '_fields', 'id' );
     352        $server   = rest_get_server();
     353        $response = $server->dispatch( $request );
     354        add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
     355        $response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
     356        remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
     357
     358        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
     359    }
     360
     361    /**
     362     * Data provider intended to provide paths for testing HEAD requests.
     363     *
     364     * @return array
     365     */
     366    public static function data_head_request_with_specified_fields_returns_success_response() {
     367        return array(
     368            'get_item request'  => array( '/wp/v2/global-styles/%d/revisions/%d' ),
     369            'get_items request' => array( '/wp/v2/global-styles/%d/revisions' ),
     370        );
    340371    }
    341372
     
    367398        $response = rest_get_server()->dispatch( $request );
    368399        $this->assertSame( 200, $response->get_status(), 'Response status is 200.' );
    369         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     400        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
    370401    }
    371402
  • trunk/tests/phpunit/tests/rest-api/rest-pattern-directory-controller.php

    r59899 r59970  
    168168
    169169        $this->assertSame( 0, $filter->get_call_count(), 'The "' . $hook_name . '" filter was called when it should not be for HEAD requests.' );
    170         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     170        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     171    }
     172
     173    /**
     174     * @ticket 56481
     175     */
     176    public function test_get_items_head_request_with_specified_fields_returns_success_response() {
     177        wp_set_current_user( self::$contributor_id );
     178        self::mock_successful_response( 'browse-all', true );
     179        $request = new WP_REST_Request( 'HEAD', '/wp/v2/pattern-directory/patterns' );
     180        $request->set_param( '_fields', 'id' );
     181        $server   = rest_get_server();
     182        $response = $server->dispatch( $request );
     183        add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
     184        $response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
     185        remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
     186
     187        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
    171188    }
    172189
  • trunk/tests/phpunit/tests/rest-api/rest-post-types-controller.php

    r59899 r59970  
    112112            return null;
    113113        }
    114         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     114        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     115    }
     116
     117    /**
     118     * @dataProvider data_head_request_with_specified_fields_returns_success_response
     119     * @ticket 56481
     120     *
     121     * @param string $path The path to test.
     122     */
     123    public function test_head_request_with_specified_fields_returns_success_response( $path ) {
     124        $request = new WP_REST_Request( 'HEAD', $path );
     125        $request->set_param( '_fields', 'slug' );
     126        $server   = rest_get_server();
     127        $response = $server->dispatch( $request );
     128        add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
     129        $response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
     130        remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
     131
     132        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
     133    }
     134
     135    /**
     136     * Data provider intended to provide paths for testing HEAD requests.
     137     *
     138     * @return array
     139     */
     140    public static function data_head_request_with_specified_fields_returns_success_response() {
     141        return array(
     142            'get_item request'  => array( '/wp/v2/types/post' ),
     143            'get_items request' => array( '/wp/v2/types' ),
     144        );
    115145    }
    116146
     
    325355        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
    326356        $this->assertSame( 0, $filter->get_call_count(), 'The "' . $hook_name . '" filter was called when it should not be for HEAD requests.' );
    327         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     357        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
    328358    }
    329359
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r59899 r59970  
    296296        $this->assertSame( 0, $filter->get_call_count(), 'The "' . $hook_name . '" filter was called when it should not be for HEAD requests.' );
    297297        $this->assertArrayHasKey( 'Link', $headers, 'The "Link" header should be present in the response.' );
    298         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     298        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
    299299    }
    300300
     
    321321
    322322        if ( $request->is_method( 'HEAD' ) ) {
    323             $this->assertNull( $response->get_data(), 'Failed asserting that response data is null for HEAD request.' );
     323            $this->assertSame( array(), $response->get_data(), 'Failed asserting that response data is null for HEAD request.' );
    324324        } else {
    325325            $this->assertSame( array(), $response->get_data(), 'Failed asserting that response data is an empty array for GET request.' );
     
    352352
    353353        } else {
    354             $this->assertNull( $response->get_data(), 'Failed asserting that response data is null for HEAD request.' );
     354            $this->assertSame( array(), $response->get_data(), 'Failed asserting that response data is null for HEAD request.' );
    355355            $headers = $response->get_headers();
    356356            $this->assertSame( $total_posts, $headers['X-WP-Total'] );
     
    367367            $this->assertSameSets( array( self::$editor_id, self::$author_id ), wp_list_pluck( $data, 'author' ) );
    368368        } else {
    369             $this->assertNull( $data, 'Failed asserting that response data is null for HEAD request.' );
     369            $this->assertSame( array(), $data, 'Failed asserting that response data is null for HEAD request.' );
    370370            $headers = $response->get_headers();
    371371            $this->assertSame( 2, $headers['X-WP-Total'], 'Failed asserting that X-WP-Total header is 2.' );
     
    382382            $this->assertSame( self::$editor_id, $data[0]['author'] );
    383383        } else {
    384             $this->assertNull( $data, 'Failed asserting that response data is null for HEAD request.' );
     384            $this->assertSame( array(), $data, 'Failed asserting that response data is null for HEAD request.' );
    385385            $headers = $response->get_headers();
    386386            $this->assertSame( 1, $headers['X-WP-Total'], 'Failed asserting that X-WP-Total header is 1.' );
     
    408408            $this->assertCount( $total_posts, $response->get_data() );
    409409        } else {
    410             $this->assertNull( $response->get_data(), 'Failed asserting that response data is null for HEAD request.' );
     410            $this->assertSame( array(), $response->get_data(), 'Failed asserting that response data is null for HEAD request.' );
    411411            $headers = $response->get_headers();
    412412            $this->assertSame( $total_posts, $headers['X-WP-Total'], 'Failed asserting that the number of posts is correct.' );
     
    425425            $this->assertNotEquals( self::$author_id, $data[0]['author'] );
    426426        } else {
    427             $this->assertNull( $response->get_data(), 'Failed asserting that response data is null for HEAD request.' );
     427            $this->assertSame( array(), $response->get_data(), 'Failed asserting that response data is null for HEAD request.' );
    428428            $headers = $response->get_headers();
    429429            $this->assertSame( $total_posts - 2, $headers['X-WP-Total'], 'Failed asserting that the number of posts is correct.' );
     
    442442            $this->assertNotEquals( self::$editor_id, $data[1]['author'] );
    443443        } else {
    444             $this->assertNull( $response->get_data(), 'Failed asserting that response data is null for HEAD request.' );
     444            $this->assertSame( array(), $response->get_data(), 'Failed asserting that response data is null for HEAD request.' );
    445445            $headers = $response->get_headers();
    446446            $this->assertSame( $total_posts - 1, $headers['X-WP-Total'], 'Failed asserting that the number of posts is correct.' );
     
    484484            $this->assertSame( $id2, $data[0]['id'] );
    485485        } else {
    486             $this->assertNull( $data, 'Failed asserting that response data is null for HEAD request.' );
     486            $this->assertSame( array(), $data, 'Failed asserting that response data is null for HEAD request.' );
    487487            $headers = $response->get_headers();
    488488            $this->assertSame( 2, $headers['X-WP-Total'], 'Failed asserting that the number of posts is correct.' );
     
    499499            $this->assertSame( $id1, $data[0]['id'] );
    500500        } else {
    501             $this->assertNull( $data, 'Failed asserting that response data is null for HEAD request.' );
     501            $this->assertSame( array(), $data, 'Failed asserting that response data is null for HEAD request.' );
    502502            $headers = $response->get_headers();
    503503            $this->assertSame( 2, $headers['X-WP-Total'], 'Failed asserting that the number of posts is correct.' );
     
    21942194            return null;
    21952195        }
    2196         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     2196        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     2197    }
     2198
     2199    /**
     2200     * @dataProvider data_head_request_with_specified_fields_returns_success_response
     2201     * @ticket 56481
     2202     *
     2203     * @param string $path The path to test.
     2204     */
     2205    public function test_head_request_with_specified_fields_returns_success_response( $path ) {
     2206        $request = new WP_REST_Request( 'HEAD', sprintf( $path, self::$post_id ) );
     2207        $request->set_param( '_fields', 'id' );
     2208        $server   = rest_get_server();
     2209        $response = $server->dispatch( $request );
     2210        add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
     2211        $response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
     2212        remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
     2213
     2214        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
     2215    }
     2216
     2217    /**
     2218     * Data provider intended to provide paths for testing HEAD requests.
     2219     *
     2220     * @return array
     2221     */
     2222    public static function data_head_request_with_specified_fields_returns_success_response() {
     2223        return array(
     2224            'get_item request'  => array( '/wp/v2/posts/%d' ),
     2225            'get_items request' => array( '/wp/v2/posts' ),
     2226        );
    21972227    }
    21982228
  • trunk/tests/phpunit/tests/rest-api/rest-revisions-controller.php

    r59899 r59970  
    183183        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
    184184        $this->assertSame( 0, $filter->get_call_count(), 'The "' . $hook_name . '" filter was called when it should not be for HEAD requests.' );
    185         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     185        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
    186186    }
    187187
     
    301301            return null;
    302302        }
    303         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     303        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     304    }
     305
     306    /**
     307     * @dataProvider data_head_request_with_specified_fields_returns_success_response
     308     * @ticket 56481
     309     *
     310     * @param string $path The path to test.
     311     */
     312    public function test_head_request_with_specified_fields_returns_success_response( $path ) {
     313        wp_set_current_user( self::$editor_id );
     314        $request = new WP_REST_Request( 'HEAD', sprintf( $path, self::$post_id, $this->revision_id1 ) );
     315        $request->set_param( '_fields', 'id' );
     316        $server   = rest_get_server();
     317        $response = $server->dispatch( $request );
     318        add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
     319        $response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
     320        remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
     321
     322        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
     323    }
     324
     325    /**
     326     * Data provider intended to provide paths for testing HEAD requests.
     327     *
     328     * @return array
     329     */
     330    public static function data_head_request_with_specified_fields_returns_success_response() {
     331        return array(
     332
     333            'get_item request'  => array( '/wp/v2/posts/%d/revisions/%d' ),
     334            'get_items request' => array( '/wp/v2/posts/%d/revisions' ),
     335        );
    304336    }
    305337
  • trunk/tests/phpunit/tests/rest-api/rest-sidebars-controller.php

    r59899 r59970  
    172172        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
    173173        $this->assertSame( 0, $filter->get_call_count(), 'The "' . $hook_name . '" filter was called when it should not be for HEAD requests.' );
    174         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     174        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
    175175    }
    176176
     
    568568            return null;
    569569        }
    570         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     570        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     571    }
     572
     573    /**
     574     * @dataProvider data_head_request_with_specified_fields_returns_success_response
     575     * @ticket 56481
     576     *
     577     * @param string $path The path to test.
     578     */
     579    public function test_head_request_with_specified_fields_returns_success_response( $path ) {
     580        $this->setup_sidebar(
     581            'sidebar-1',
     582            array(
     583                'name' => 'Test sidebar',
     584            )
     585        );
     586
     587        $request = new WP_REST_Request( 'HEAD', $path );
     588        // This endpoint doesn't seem to support _fields param, but we need to set it to reproduce the fatal error.
     589        $request->set_param( '_fields', 'name' );
     590        $server   = rest_get_server();
     591        $response = $server->dispatch( $request );
     592        add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
     593        $response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
     594        remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
     595
     596        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
     597    }
     598
     599    /**
     600     * Data provider intended to provide paths for testing HEAD requests.
     601     *
     602     * @return array
     603     */
     604    public static function data_head_request_with_specified_fields_returns_success_response() {
     605        return array(
     606
     607            'get_item request'  => array( '/wp/v2/sidebars/sidebar-1' ),
     608            'get_items request' => array( '/wp/v2/sidebars' ),
     609        );
    571610    }
    572611
  • trunk/tests/phpunit/tests/rest-api/rest-tags-controller.php

    r59899 r59970  
    16151615            return null;
    16161616        }
    1617         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     1617        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     1618    }
     1619
     1620    /**
     1621     * @dataProvider data_head_request_with_specified_fields_returns_success_response
     1622     * @ticket 56481
     1623     *
     1624     * @param string $path The path to test.
     1625     */
     1626    public function test_head_request_with_specified_fields_returns_success_response( $path ) {
     1627        $tag_id  = self::factory()->tag->create();
     1628        $request = new WP_REST_Request( 'HEAD', sprintf( $path, $tag_id ) );
     1629        $request->set_param( '_fields', 'id' );
     1630        $server   = rest_get_server();
     1631        $response = $server->dispatch( $request );
     1632        add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
     1633        $response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
     1634        remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
     1635
     1636        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
     1637    }
     1638
     1639    /**
     1640     * Data provider intended to provide paths for testing HEAD requests.
     1641     *
     1642     * @return array
     1643     */
     1644    public static function data_head_request_with_specified_fields_returns_success_response() {
     1645        return array(
     1646            'get_item request'  => array( '/wp/v2/tags/%d' ),
     1647            'get_items request' => array( '/wp/v2/tags' ),
     1648        );
    16181649    }
    16191650}
  • trunk/tests/phpunit/tests/rest-api/rest-taxonomies-controller.php

    r59899 r59970  
    7474        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
    7575        $this->assertSame( 0, $filter->get_call_count(), 'The "' . $hook_name . '" filter was called when it should not be for HEAD requests.' );
    76         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     76        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
    7777    }
    7878
     
    186186            return null;
    187187        }
    188         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     188        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     189    }
     190
     191    /**
     192     * @dataProvider data_head_request_with_specified_fields_returns_success_response
     193     * @ticket 56481
     194     *
     195     * @param string $path The path to test.
     196     */
     197    public function test_head_request_with_specified_fields_returns_success_response( $path ) {
     198        $request = new WP_REST_Request( 'HEAD', $path );
     199        $request->set_param( '_fields', 'name' );
     200        $server   = rest_get_server();
     201        $response = $server->dispatch( $request );
     202        add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
     203        $response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
     204        remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
     205
     206        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
     207    }
     208
     209    /**
     210     * Data provider intended to provide paths for testing HEAD requests.
     211     *
     212     * @return array
     213     */
     214    public static function data_head_request_with_specified_fields_returns_success_response() {
     215        return array(
     216            'get_item request'  => array( '/wp/v2/taxonomies/category' ),
     217            'get_items request' => array( '/wp/v2/taxonomies' ),
     218        );
    189219    }
    190220
  • trunk/tests/phpunit/tests/rest-api/rest-users-controller.php

    r59899 r59970  
    255255
    256256        if ( 'HEAD' === $method ) {
    257             $this->assertNull( $response->get_data(), 'Expected null response data for HEAD request, but received non-null data.' );
     257            $this->assertSame( array(), $response->get_data(), 'Expected null response data for HEAD request, but received non-null data.' );
    258258            return null;
    259259        }
     
    32523252            return null;
    32533253        }
    3254         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     3254        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
    32553255    }
    32563256
     
    32733273        $this->assertSame( 200, $response->get_status() );
    32743274        if ( $is_head_request ) {
    3275             $this->assertNull( $response->get_data() );
     3275            $this->assertSame( array(), $response->get_data() );
    32763276        } else {
    32773277            $this->assertNotEmpty( $response->get_data() );
     
    33053305        // Assert that the SQL query only fetches the id column.
    33063306        $this->assertMatchesRegularExpression( $pattern, $query->request, 'The SQL query does not match the expected string.' );
     3307    }
     3308
     3309    /**
     3310     * @dataProvider data_head_request_with_specified_fields_returns_success_response
     3311     * @ticket 56481
     3312     *
     3313     * @param string $path The path to test.
     3314     */
     3315    public function test_head_request_with_specified_fields_returns_success_response( $path ) {
     3316        $user_id = self::factory()->user->create();
     3317        wp_set_current_user( self::$user );
     3318
     3319        $request = new WP_REST_Request( 'HEAD', sprintf( $path, $user_id ) );
     3320        $request->set_param( '_fields', 'id' );
     3321        $server   = rest_get_server();
     3322        $response = $server->dispatch( $request );
     3323        add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
     3324        $response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
     3325        remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
     3326
     3327        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
     3328    }
     3329
     3330    /**
     3331     * Data provider intended to provide paths for testing HEAD requests.
     3332     *
     3333     * @return array
     3334     */
     3335    public static function data_head_request_with_specified_fields_returns_success_response() {
     3336        return array(
     3337            'get_item request'  => array( '/wp/v2/users/%d' ),
     3338            'get_items request' => array( '/wp/v2/users' ),
     3339        );
    33073340    }
    33083341
  • trunk/tests/phpunit/tests/rest-api/rest-widget-types-controller.php

    r59899 r59970  
    130130        $response = rest_get_server()->dispatch( $request );
    131131        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
    132         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     132        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
    133133    }
    134134
     
    228228            return null;
    229229        }
    230         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     230        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
    231231    }
    232232
     
    240240            'GET request'  => array( 'GET' ),
    241241            'HEAD request' => array( 'HEAD' ),
     242        );
     243    }
     244
     245    /**
     246     * @dataProvider data_head_request_with_specified_fields_returns_success_response
     247     * @ticket 56481
     248     *
     249     * @param string $path The path to test.
     250     */
     251    public function test_head_request_with_specified_fields_returns_success_response( $path ) {
     252        wp_set_current_user( self::$admin_id );
     253        $request = new WP_REST_Request( 'HEAD', $path );
     254        $request->set_param( '_fields', 'id' );
     255        $server   = rest_get_server();
     256        $response = $server->dispatch( $request );
     257        add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
     258        $response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
     259        remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
     260
     261        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
     262    }
     263
     264    /**
     265     * Data provider intended to provide paths for testing HEAD requests.
     266     *
     267     * @return array
     268     */
     269    public static function data_head_request_with_specified_fields_returns_success_response() {
     270        return array(
     271            'get_item request'  => array( '/wp/v2/widget-types/calendar' ),
     272            'get_items request' => array( '/wp/v2/widget-types' ),
    242273        );
    243274    }
  • trunk/tests/phpunit/tests/rest-api/rest-widgets-controller.php

    r59899 r59970  
    473473        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
    474474        $this->assertSame( 0, $filter->get_call_count(), 'The "' . $hook_name . '" filter was called when it should not be for HEAD requests.' );
    475         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     475        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
    476476    }
    477477
     
    645645            return null;
    646646        }
    647         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     647        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     648    }
     649
     650    /**
     651     * @dataProvider data_head_request_with_specified_fields_returns_success_response
     652     * @ticket 56481
     653     *
     654     * @param string $path The path to test.
     655     */
     656    public function test_head_request_with_specified_fields_returns_success_response( $path ) {
     657        add_filter( 'pre_http_request', array( $this, 'mocked_rss_response' ) );
     658        global $wp_widget_factory;
     659
     660        $wp_widget_factory->widgets['WP_Widget_RSS']->widget_options['show_instance_in_rest'] = false;
     661
     662        $block_content = '<!-- wp:paragraph --><p>Block test</p><!-- /wp:paragraph -->';
     663
     664        $this->setup_widget(
     665            'rss',
     666            1,
     667            array(
     668                'title' => 'RSS test',
     669                'url'   => 'https://wordpress.org/news/feed',
     670            )
     671        );
     672        $this->setup_widget(
     673            'block',
     674            1,
     675            array(
     676                'content' => $block_content,
     677            )
     678        );
     679        $this->setup_sidebar(
     680            'sidebar-1',
     681            array(
     682                'name' => 'Test sidebar',
     683            ),
     684            array( 'block-1', 'rss-1', 'testwidget' )
     685        );
     686
     687        $request = new WP_REST_Request( 'HEAD', $path );
     688        $request->set_param( '_fields', 'id' );
     689        $server   = rest_get_server();
     690        $response = $server->dispatch( $request );
     691        add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
     692        $response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
     693        remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
     694        remove_filter( 'pre_http_request', array( $this, 'mocked_rss_response' ) );
     695
     696        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
     697    }
     698
     699    /**
     700     * Data provider intended to provide paths for testing HEAD requests.
     701     *
     702     * @return array
     703     */
     704    public static function data_head_request_with_specified_fields_returns_success_response() {
     705        return array(
     706            'get_item request'  => array( '/wp/v2/widgets/block-1' ),
     707            'get_items request' => array( '/wp/v2/widgets' ),
     708        );
    648709    }
    649710
  • trunk/tests/phpunit/tests/rest-api/wpRestBlockPatternCategoriesController.php

    r59899 r59970  
    132132        $response = rest_get_server()->dispatch( $request );
    133133        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
    134         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     134        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     135    }
     136
     137    /**
     138     * @ticket 56481
     139     *
     140     * @param string $path The path to test.
     141     */
     142    public function test_head_request_with_specified_fields_returns_success_response() {
     143        wp_set_current_user( self::$admin_id );
     144        $request = new WP_REST_Request( 'HEAD', static::REQUEST_ROUTE );
     145        $request->set_param( '_fields', 'name' );
     146        $server   = rest_get_server();
     147        $response = $server->dispatch( $request );
     148        add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
     149        $response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
     150        remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
     151
     152        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
    135153    }
    136154
  • trunk/tests/phpunit/tests/rest-api/wpRestTemplateAutosavesController.php

    r59899 r59970  
    317317        $response = rest_get_server()->dispatch( $request );
    318318        $this->assertSame( 200, $response->get_status(), 'Response status is 200.' );
    319         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     319        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
    320320    }
    321321
     
    471471        $response = rest_get_server()->dispatch( $request );
    472472        $this->assertSame( 200, $response->get_status(), 'Response status is 200.' );
    473         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     473        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
    474474    }
    475475
     
    484484            'template parts' => array( 'template_part_post', 'template-parts', self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME ),
    485485        );
     486    }
     487
     488    /**
     489     * @dataProvider data_get_item_with_data_provider
     490     * @covers       WP_REST_Template_Autosaves_Controller::get_item
     491     * @ticket 56922
     492     *
     493     * @param string $parent_post_property_name A class property name that contains the parent post object.
     494     * @param string $rest_base Base part of the REST API endpoint to test.
     495     * @param string $template_id Template ID to use in the test.
     496     */
     497    public function test_get_item_head_request_with_specified_fields_returns_success_response( $parent_post_property_name, $rest_base, $template_id ) {
     498        wp_set_current_user( self::$admin_id );
     499
     500        $parent_post = self::$$parent_post_property_name;
     501
     502        $autosave_post_id = wp_create_post_autosave(
     503            array(
     504                'post_content' => 'Autosave content.',
     505                'post_ID'      => $parent_post->ID,
     506                'post_type'    => $parent_post->post_type,
     507            )
     508        );
     509
     510        $request = new WP_REST_Request(
     511            'HEAD',
     512            '/wp/v2/' . $rest_base . '/' . $template_id . '/autosaves/' . $autosave_post_id
     513        );
     514        $request->set_param( '_fields', 'id' );
     515        $server   = rest_get_server();
     516        $response = $server->dispatch( $request );
     517
     518        add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
     519        $response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
     520        remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
     521
     522        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
     523    }
     524
     525    /**
     526     * @dataProvider data_get_items_with_data_provider
     527     * @covers       WP_REST_Template_Autosaves_Controller::get_items
     528     * @ticket 56922
     529     *
     530     * @param string $parent_post_property_name A class property name that contains the parent post object.
     531     * @param string $rest_base Base part of the REST API endpoint to test.
     532     * @param string $template_id Template ID to use in the test.
     533     */
     534    public function test_get_items_head_request_with_specified_fields_returns_success_response( $parent_post_property_name, $rest_base, $template_id ) {
     535        wp_set_current_user( self::$admin_id );
     536        // Cannot access this property in the data provider because it is not initialized at the time of execution.
     537        $parent_post = self::$$parent_post_property_name;
     538        wp_create_post_autosave(
     539            array(
     540                'post_content' => 'Autosave content.',
     541                'post_ID'      => $parent_post->ID,
     542                'post_type'    => $parent_post->post_type,
     543            )
     544        );
     545
     546        $request = new WP_REST_Request(
     547            'HEAD',
     548            '/wp/v2/' . $rest_base . '/' . $template_id . '/autosaves'
     549        );
     550        $request->set_param( '_fields', 'id' );
     551        $server   = rest_get_server();
     552        $response = $server->dispatch( $request );
     553
     554        add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
     555        $response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
     556        remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
     557
     558        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
    486559    }
    487560
  • trunk/tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php

    r59899 r59970  
    438438        $response = rest_get_server()->dispatch( $request );
    439439        $this->assertSame( 200, $response->get_status(), 'Response status is 200.' );
    440         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     440        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
    441441    }
    442442
     
    628628        $response    = rest_get_server()->dispatch( $request );
    629629        $this->assertSame( 200, $response->get_status(), 'Response status is 200.' );
    630         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     630        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
    631631    }
    632632
     
    641641            'template parts' => array( 'template_part_post', 'template-parts', self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME ),
    642642        );
     643    }
     644
     645    /**
     646     * @dataProvider data_get_item_with_data_provider
     647     * @covers       WP_REST_Template_Revisions_Controller::get_item
     648     * @ticket 56922
     649     *
     650     * @param string $parent_post_property_name A class property name that contains the parent post object.
     651     * @param string $rest_base Base part of the REST API endpoint to test.
     652     * @param string $template_id Template ID to use in the test.
     653     */
     654    public function test_get_item_head_request_with_specified_fields_returns_success_response( $parent_post_property_name, $rest_base, $template_id ) {
     655        wp_set_current_user( self::$admin_id );
     656
     657        $parent_post = self::$$parent_post_property_name;
     658
     659        $revisions   = wp_get_post_revisions( $parent_post, array( 'fields' => 'ids' ) );
     660        $revision_id = array_shift( $revisions );
     661
     662        $request = new WP_REST_Request(
     663            'HEAD',
     664            '/wp/v2/' . $rest_base . '/' . $template_id . '/revisions/' . $revision_id
     665        );
     666        $request->set_param( '_fields', 'id' );
     667        $server   = rest_get_server();
     668        $response = $server->dispatch( $request );
     669        add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
     670        $response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
     671        remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
     672
     673        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
     674    }
     675
     676    /**
     677     * @dataProvider data_get_items_with_data_provider
     678     * @covers       WP_REST_Template_Revisions_Controller::get_items
     679     * @ticket 56922
     680     *
     681     * @param string $parent_post_property_name A class property name that contains the parent post object.
     682     * @param string $rest_base Base part of the REST API endpoint to test.
     683     * @param string $template_id Template ID to use in the test.
     684     */
     685    public function test_get_items_head_request_with_specified_fields_returns_success_response( $parent_post_property_name, $rest_base, $template_id ) {
     686        wp_set_current_user( self::$admin_id );
     687        $parent_post = self::$$parent_post_property_name;
     688
     689        $request = new WP_REST_Request(
     690            'HEAD',
     691            '/wp/v2/' . $rest_base . '/' . $template_id . '/revisions'
     692        );
     693
     694        $request->set_param( '_fields', 'id' );
     695        $server   = rest_get_server();
     696        $response = $server->dispatch( $request );
     697        add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
     698        $response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
     699        remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
     700
     701        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
    643702    }
    644703
  • trunk/tests/phpunit/tests/rest-api/wpRestTemplatesController.php

    r59899 r59970  
    186186        $response = rest_get_server()->dispatch( $request );
    187187        $this->assertSame( 200, $response->get_status(), 'Response status is 200.' );
    188         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     188        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     189    }
     190
     191    /**
     192     * @dataProvider data_head_request_with_specified_fields_returns_success_response
     193     * @ticket 56481
     194     *
     195     * @param string $path The path to test.
     196     */
     197    public function test_head_request_with_specified_fields_returns_success_response( $path ) {
     198        wp_set_current_user( self::$admin_id );
     199        $request = new WP_REST_Request( 'HEAD', $path );
     200        $request->set_param( '_fields', 'id' );
     201        $server   = rest_get_server();
     202        $response = $server->dispatch( $request );
     203        add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
     204        $response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
     205        remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
     206
     207        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
     208    }
     209
     210    /**
     211     * Data provider intended to provide paths for testing HEAD requests.
     212     *
     213     * @return array
     214     */
     215    public static function data_head_request_with_specified_fields_returns_success_response() {
     216        return array(
     217            'get_item request'  => array( '/wp/v2/templates/default//my_template' ),
     218            'get_items request' => array( '/wp/v2/templates' ),
     219        );
    189220    }
    190221
     
    292323        $response = rest_get_server()->dispatch( $request );
    293324        $this->assertSame( 200, $response->get_status(), 'Response status is 200.' );
    294         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     325        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
    295326    }
    296327
Note: See TracChangeset for help on using the changeset viewer.