REST API: Fix fatal error when making HEAD requests with _fields filter.
Some checks failed
Cleanup Pull Requests / Clean up pull requests (push) Waiting to run
Coding Standards / PHP coding standards (push) Waiting to run
Coding Standards / JavaScript coding standards (push) Waiting to run
Coding Standards / Slack Notifications (push) Blocked by required conditions
Coding Standards / Failed workflow tasks (push) Blocked by required conditions
End-to-end Tests / Test with SCRIPT_DEBUG disabled (push) Waiting to run
End-to-end Tests / Test with SCRIPT_DEBUG enabled (push) Waiting to run
End-to-end Tests / Slack Notifications (push) Blocked by required conditions
End-to-end Tests / Failed workflow tasks (push) Blocked by required conditions
JavaScript Tests / QUnit Tests (push) Waiting to run
JavaScript Tests / Slack Notifications (push) Blocked by required conditions
JavaScript Tests / Failed workflow tasks (push) Blocked by required conditions
Performance Tests / Determine Matrix (push) Waiting to run
Performance Tests / ${{ matrix.multisite && 'Multisite' || 'Single Site' }} ${{ matrix.memcached && 'Memcached' || 'Default' }} (push) Blocked by required conditions
Performance Tests / Compare (push) Blocked by required conditions
Performance Tests / Slack Notifications (push) Blocked by required conditions
Performance Tests / Failed workflow tasks (push) Blocked by required conditions
PHP Compatibility / Check PHP compatibility (push) Waiting to run
PHP Compatibility / Slack Notifications (push) Blocked by required conditions
PHP Compatibility / Failed workflow tasks (push) Blocked by required conditions
PHPUnit Tests / PHP 7.2 (push) Waiting to run
PHPUnit Tests / PHP 7.3 (push) Waiting to run
PHPUnit Tests / PHP 7.4 (push) Waiting to run
PHPUnit Tests / PHP 8.0 (push) Waiting to run
PHPUnit Tests / PHP 8.1 (push) Waiting to run
PHPUnit Tests / PHP 8.2 (push) Waiting to run
PHPUnit Tests / PHP 8.3 (push) Waiting to run
PHPUnit Tests / PHP 8.4 (push) Waiting to run
PHPUnit Tests / html-api-html5lib-tests (push) Waiting to run
PHPUnit Tests / Slack Notifications (push) Blocked by required conditions
PHPUnit Tests / Failed workflow tasks (push) Blocked by required conditions
Test Build Processes / Core running from build (push) Waiting to run
Test Build Processes / Core running from src (push) Waiting to run
Test Build Processes / Gutenberg running from build (push) Waiting to run
Test Build Processes / Gutenberg running from src (push) Waiting to run
Test Build Processes / Slack Notifications (push) Blocked by required conditions
Test Build Processes / Failed workflow tasks (push) Blocked by required conditions
Upgrade Develop Version Tests / Build (push) Waiting to run
Upgrade Develop Version Tests / Upgrade from 4.9 (push) Blocked by required conditions
Upgrade Develop Version Tests / Upgrade from 6.5 (push) Blocked by required conditions
Upgrade Develop Version Tests / Upgrade from 6.6 (push) Blocked by required conditions
Upgrade Develop Version Tests / Upgrade from 6.7 (push) Blocked by required conditions
Upgrade Develop Version Tests / Slack Notifications (push) Blocked by required conditions
Upgrade Develop Version Tests / Failed workflow tasks (push) Blocked by required conditions
Local Docker Environment / Build Test Matrix (push) Has been cancelled
Local Docker Environment / PHP ${{ matrix.php }} (push) Has been cancelled
Local Docker Environment / Slack Notifications (push) Has been cancelled
Local Docker Environment / Failed workflow tasks (push) Has been cancelled

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.


git-svn-id: https://develop.svn.wordpress.org/trunk@59970 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Timothy Jacobs 2025-03-11 14:17:41 +00:00
parent 34b98f1c54
commit 8ae3a2642b
38 changed files with 747 additions and 79 deletions

View File

@ -307,7 +307,7 @@ class WP_REST_Autosaves_Controller extends WP_REST_Revisions_Controller {
if ( $request->is_method( 'HEAD' ) ) {
// Return early as this handler doesn't add any response headers.
return new WP_REST_Response();
return new WP_REST_Response( array() );
}
$response = array();
$parent_id = $parent->ID;
@ -455,7 +455,7 @@ class WP_REST_Autosaves_Controller extends WP_REST_Revisions_Controller {
// Don't prepare the response body for HEAD requests.
if ( $request->is_method( 'HEAD' ) ) {
/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php */
return apply_filters( 'rest_prepare_autosave', new WP_REST_Response(), $post, $request );
return apply_filters( 'rest_prepare_autosave', new WP_REST_Response( array() ), $post, $request );
}
$response = $this->revisions_controller->prepare_item_for_response( $post, $request );
$fields = $this->get_fields_for_response( $request );

View File

@ -83,7 +83,7 @@ class WP_REST_Block_Pattern_Categories_Controller extends WP_REST_Controller {
public function get_items( $request ) {
if ( $request->is_method( 'HEAD' ) ) {
// Return early as this handler doesn't add any response headers.
return new WP_REST_Response();
return new WP_REST_Response( array() );
}
$response = array();

View File

@ -133,7 +133,7 @@ class WP_REST_Block_Types_Controller extends WP_REST_Controller {
public function get_items( $request ) {
if ( $request->is_method( 'HEAD' ) ) {
// Return early as this handler doesn't add any response headers.
return new WP_REST_Response();
return new WP_REST_Response( array() );
}
$data = array();
@ -258,7 +258,7 @@ class WP_REST_Block_Types_Controller extends WP_REST_Controller {
// Don't prepare the response body for HEAD requests.
if ( $request->is_method( 'HEAD' ) ) {
/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php */
return apply_filters( 'rest_prepare_block_type', new WP_REST_Response(), $block_type, $request );
return apply_filters( 'rest_prepare_block_type', new WP_REST_Response( array() ), $block_type, $request );
}
$fields = $this->get_fields_for_response( $request );

View File

@ -313,7 +313,7 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
$max_pages = (int) ceil( $total_comments / $request['per_page'] );
}
$response = $is_head_request ? new WP_REST_Response() : rest_ensure_response( $comments );
$response = $is_head_request ? new WP_REST_Response( array() ) : rest_ensure_response( $comments );
$response->header( 'X-WP-Total', $total_comments );
$response->header( 'X-WP-TotalPages', $max_pages );
@ -1054,7 +1054,7 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
// Don't prepare the response body for HEAD requests.
if ( $request->is_method( 'HEAD' ) ) {
/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php */
return apply_filters( 'rest_prepare_comment', new WP_REST_Response(), $comment, $request );
return apply_filters( 'rest_prepare_comment', new WP_REST_Response( array() ), $comment, $request );
}
$fields = $this->get_fields_for_response( $request );

View File

@ -113,7 +113,7 @@ class WP_REST_Font_Collections_Controller extends WP_REST_Controller {
$items[] = $item;
}
$response = $is_head_request ? new WP_REST_Response() : rest_ensure_response( $items );
$response = $is_head_request ? new WP_REST_Response( array() ) : rest_ensure_response( $items );
$response->header( 'X-WP-Total', (int) $total_items );
$response->header( 'X-WP-TotalPages', $max_pages );
@ -193,7 +193,7 @@ class WP_REST_Font_Collections_Controller extends WP_REST_Controller {
*/
if ( $request->is_method( 'HEAD' ) ) {
/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-font-collections-controller.php */
return apply_filters( 'rest_prepare_font_collection', new WP_REST_Response(), $item, $request );
return apply_filters( 'rest_prepare_font_collection', new WP_REST_Response( array() ), $item, $request );
}
foreach ( $data_fields as $field ) {
@ -209,7 +209,7 @@ class WP_REST_Font_Collections_Controller extends WP_REST_Controller {
*/
if ( $request->is_method( 'HEAD' ) ) {
/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-font-collections-controller.php */
return apply_filters( 'rest_prepare_font_collection', new WP_REST_Response(), $item, $request );
return apply_filters( 'rest_prepare_font_collection', new WP_REST_Response( array() ), $item, $request );
}
$response = rest_ensure_response( $data );

View File

@ -248,7 +248,7 @@ class WP_REST_Global_Styles_Revisions_Controller extends WP_REST_Revisions_Contr
$response = rest_ensure_response( $response );
} else {
$response = new WP_REST_Response();
$response = new WP_REST_Response( array() );
}
$response->header( 'X-WP-Total', (int) $total_revisions );
@ -291,7 +291,7 @@ class WP_REST_Global_Styles_Revisions_Controller extends WP_REST_Revisions_Contr
public function prepare_item_for_response( $post, $request ) {
// Don't prepare the response body for HEAD requests.
if ( $request->is_method( 'HEAD' ) ) {
return new WP_REST_Response();
return new WP_REST_Response( array() );
}
$parent = $this->get_parent( $request['parent'] );

View File

@ -163,7 +163,7 @@ class WP_REST_Pattern_Directory_Controller extends WP_REST_Controller {
if ( $request->is_method( 'HEAD' ) ) {
// Return early as this handler doesn't add any response headers.
return new WP_REST_Response();
return new WP_REST_Response( array() );
}
$response = array();

View File

@ -111,7 +111,7 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller {
public function get_items( $request ) {
if ( $request->is_method( 'HEAD' ) ) {
// Return early as this handler doesn't add any response headers.
return new WP_REST_Response();
return new WP_REST_Response( array() );
}
$data = array();
@ -186,7 +186,7 @@ class WP_REST_Post_Types_Controller extends WP_REST_Controller {
// Don't prepare the response body for HEAD requests.
if ( $request->is_method( 'HEAD' ) ) {
/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php */
return apply_filters( 'rest_prepare_post_type', new WP_REST_Response(), $post_type, $request );
return apply_filters( 'rest_prepare_post_type', new WP_REST_Response( array() ), $post_type, $request );
}
$taxonomies = wp_list_filter( get_object_taxonomies( $post_type->name, 'objects' ), array( 'show_in_rest' => true ) );

View File

@ -499,7 +499,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
);
}
$response = $is_head_request ? new WP_REST_Response() : rest_ensure_response( $posts );
$response = $is_head_request ? new WP_REST_Response( array() ) : rest_ensure_response( $posts );
$response->header( 'X-WP-Total', (int) $total_posts );
$response->header( 'X-WP-TotalPages', (int) $max_pages );
@ -1847,7 +1847,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
// Don't prepare the response body for HEAD requests.
if ( $request->is_method( 'HEAD' ) ) {
/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
return apply_filters( "rest_prepare_{$this->post_type}", new WP_REST_Response(), $post, $request );
return apply_filters( "rest_prepare_{$this->post_type}", new WP_REST_Response( array() ), $post, $request );
}
$fields = $this->get_fields_for_response( $request );

View File

@ -355,7 +355,7 @@ class WP_REST_Revisions_Controller extends WP_REST_Controller {
$response = rest_ensure_response( $response );
} else {
$response = new WP_REST_Response();
$response = new WP_REST_Response( array() );
}
$response->header( 'X-WP-Total', (int) $total_revisions );
@ -591,7 +591,7 @@ class WP_REST_Revisions_Controller extends WP_REST_Controller {
// Don't prepare the response body for HEAD requests.
if ( $request->is_method( 'HEAD' ) ) {
/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php */
return apply_filters( 'rest_prepare_revision', new WP_REST_Response(), $post, $request );
return apply_filters( 'rest_prepare_revision', new WP_REST_Response( array() ), $post, $request );
}
$fields = $this->get_fields_for_response( $request );

View File

@ -165,7 +165,7 @@ class WP_REST_Search_Controller extends WP_REST_Controller {
);
}
$response = $is_head_request ? new WP_REST_Response() : rest_ensure_response( $results );
$response = $is_head_request ? new WP_REST_Response( array() ) : rest_ensure_response( $results );
$response->header( 'X-WP-Total', $total );
$response->header( 'X-WP-TotalPages', $max_pages );

View File

@ -121,7 +121,7 @@ class WP_REST_Sidebars_Controller extends WP_REST_Controller {
public function get_items( $request ) {
if ( $request->is_method( 'HEAD' ) ) {
// Return early as this handler doesn't add any response headers.
return new WP_REST_Response();
return new WP_REST_Response( array() );
}
$this->retrieve_widgets();
@ -329,7 +329,7 @@ class WP_REST_Sidebars_Controller extends WP_REST_Controller {
// Don't prepare the response body for HEAD requests.
if ( $request->is_method( 'HEAD' ) ) {
/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-sidebars-controller.php */
return apply_filters( 'rest_prepare_sidebar', new WP_REST_Response(), $raw_sidebar, $request );
return apply_filters( 'rest_prepare_sidebar', new WP_REST_Response( array() ), $raw_sidebar, $request );
}
$id = $raw_sidebar['id'];

View File

@ -115,7 +115,7 @@ class WP_REST_Taxonomies_Controller extends WP_REST_Controller {
public function get_items( $request ) {
if ( $request->is_method( 'HEAD' ) ) {
// Return early as this handler doesn't add any response headers.
return new WP_REST_Response();
return new WP_REST_Response( array() );
}
// Retrieve the list of registered collection query parameters.
@ -217,7 +217,7 @@ class WP_REST_Taxonomies_Controller extends WP_REST_Controller {
// Don't prepare the response body for HEAD requests.
if ( $request->is_method( 'HEAD' ) ) {
/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php */
return apply_filters( 'rest_prepare_taxonomy', new WP_REST_Response(), $taxonomy, $request );
return apply_filters( 'rest_prepare_taxonomy', new WP_REST_Response( array() ), $taxonomy, $request );
}
$base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;

View File

@ -271,7 +271,7 @@ class WP_REST_Templates_Controller extends WP_REST_Controller {
public function get_items( $request ) {
if ( $request->is_method( 'HEAD' ) ) {
// Return early as this handler doesn't add any response headers.
return new WP_REST_Response();
return new WP_REST_Response( array() );
}
$query = array();
@ -675,7 +675,7 @@ class WP_REST_Templates_Controller extends WP_REST_Controller {
public function prepare_item_for_response( $item, $request ) {
// Don't prepare the response body for HEAD requests.
if ( $request->is_method( 'HEAD' ) ) {
return new WP_REST_Response();
return new WP_REST_Response( array() );
}
/*

View File

@ -370,7 +370,7 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
}
}
$response = $is_head_request ? new WP_REST_Response() : rest_ensure_response( $response );
$response = $is_head_request ? new WP_REST_Response( array() ) : rest_ensure_response( $response );
// Store pagination values for headers.
$per_page = (int) $prepared_args['number'];
@ -899,7 +899,7 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
// Don't prepare the response body for HEAD requests.
if ( $request->is_method( 'HEAD' ) ) {
/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
return apply_filters( "rest_prepare_{$this->taxonomy}", new WP_REST_Response(), $item, $request );
return apply_filters( "rest_prepare_{$this->taxonomy}", new WP_REST_Response( array() ), $item, $request );
}
$fields = $this->get_fields_for_response( $request );

View File

@ -384,7 +384,7 @@ class WP_REST_Users_Controller extends WP_REST_Controller {
}
}
$response = $is_head_request ? new WP_REST_Response() : rest_ensure_response( $users );
$response = $is_head_request ? new WP_REST_Response( array() ) : rest_ensure_response( $users );
// Store pagination values for headers then unset for count query.
$per_page = (int) $prepared_args['number'];
@ -1032,7 +1032,7 @@ class WP_REST_Users_Controller extends WP_REST_Controller {
// Don't prepare the response body for HEAD requests.
if ( $request->is_method( 'HEAD' ) ) {
/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php */
return apply_filters( 'rest_prepare_user', new WP_REST_Response(), $user, $request );
return apply_filters( 'rest_prepare_user', new WP_REST_Response( array() ), $user, $request );
}
$fields = $this->get_fields_for_response( $request );

View File

@ -147,7 +147,7 @@ class WP_REST_Widget_Types_Controller extends WP_REST_Controller {
public function get_items( $request ) {
if ( $request->is_method( 'HEAD' ) ) {
// Return early as this handler doesn't add any response headers.
return new WP_REST_Response();
return new WP_REST_Response( array() );
}
$data = array();
@ -306,7 +306,7 @@ class WP_REST_Widget_Types_Controller extends WP_REST_Controller {
// Don't prepare the response body for HEAD requests.
if ( $request->is_method( 'HEAD' ) ) {
/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php */
return apply_filters( 'rest_prepare_widget_type', new WP_REST_Response(), $widget_type, $request );
return apply_filters( 'rest_prepare_widget_type', new WP_REST_Response( array() ), $widget_type, $request );
}
$fields = $this->get_fields_for_response( $request );

View File

@ -138,7 +138,7 @@ class WP_REST_Widgets_Controller extends WP_REST_Controller {
public function get_items( $request ) {
if ( $request->is_method( 'HEAD' ) ) {
// Return early as this handler doesn't add any response headers.
return new WP_REST_Response();
return new WP_REST_Response( array() );
}
$this->retrieve_widgets();
@ -686,7 +686,7 @@ class WP_REST_Widgets_Controller extends WP_REST_Controller {
// Don't prepare the response body for HEAD requests.
if ( $request->is_method( 'HEAD' ) ) {
/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php */
return apply_filters( 'rest_prepare_widget', new WP_REST_Response(), $widget, $request );
return apply_filters( 'rest_prepare_widget', new WP_REST_Response( array() ), $widget, $request );
}
$parsed_id = wp_parse_widget_id( $widget_id );

View File

@ -108,7 +108,7 @@ class Tests_REST_WpRestFontCollectionsController extends WP_Test_REST_Controller
return null;
}
$this->assertNull( $content, 'The response should be empty.' );
$this->assertSame( array(), $content, 'The response should be empty.' );
$headers = $response->get_headers();
$this->assertArrayHasKey( 'X-WP-Total', $headers, 'The "X-WP-Total" header should be present in the response.' );
// Includes non-valid collections.
@ -172,7 +172,7 @@ class Tests_REST_WpRestFontCollectionsController extends WP_Test_REST_Controller
if ( 'HEAD' !== $method ) {
return null;
}
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**

View File

@ -196,7 +196,7 @@ class WP_Test_REST_Autosaves_Controller extends WP_Test_REST_Post_Type_Controlle
$this->assertNotWPError( $response );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
$this->assertSame( 0, $filter->get_call_count(), 'The "' . $hook_name . '" filter was called when it should not be for HEAD requests.' );
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
@ -314,7 +314,7 @@ class WP_Test_REST_Autosaves_Controller extends WP_Test_REST_Post_Type_Controlle
if ( 'HEAD' !== $method ) {
return null;
}
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
public function test_get_item_embed_context() {
@ -889,4 +889,35 @@ class WP_Test_REST_Autosaves_Controller extends WP_Test_REST_Post_Type_Controlle
$this->assertSame( 200, $response->get_status() );
$this->assertSame( $autosave['id'], $data['id'], 'Original autosave was not returned' );
}
/**
* @dataProvider data_head_request_with_specified_fields_returns_success_response
* @ticket 56481
*
* @param string $path The path to test.
*/
public function test_head_request_with_specified_fields_returns_success_response( $path ) {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'HEAD', sprintf( $path, self::$post_id, self::$autosave_post_id ) );
$request->set_param( '_fields', 'id' );
$server = rest_get_server();
$response = $server->dispatch( $request );
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
}
/**
* Data provider intended to provide paths for testing HEAD requests.
*
* @return array
*/
public static function data_head_request_with_specified_fields_returns_success_response() {
return array(
'get_item request' => array( '/wp/v2/posts/%d/autosaves/%d' ),
'get_items request' => array( '/wp/v2/posts/%d' ),
);
}
}

View File

@ -636,7 +636,7 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase {
if ( 'HEAD' !== $method ) {
return null;
}
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
@ -659,7 +659,38 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase {
$request = new WP_REST_Request( 'HEAD', '/wp/v2/block-types' );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
* @dataProvider data_head_request_with_specified_fields_returns_success_response
* @ticket 56481
*
* @param string $path The path to test.
*/
public function test_head_request_with_specified_fields_returns_success_response( $path ) {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'HEAD', $path );
$request->set_param( '_fields', 'title' );
$server = rest_get_server();
$response = $server->dispatch( $request );
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
}
/**
* Data provider intended to provide paths for testing HEAD requests.
*
* @return array
*/
public static function data_head_request_with_specified_fields_returns_success_response() {
return array(
'get_item request' => array( '/wp/v2/block-types/fake/test' ),
'get_items request' => array( '/wp/v2/block-types' ),
);
}
/**

View File

@ -1360,6 +1360,36 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas
if ( 'HEAD' !== $method ) {
return null;
}
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
* @dataProvider data_head_request_with_specified_fields_returns_success_response
* @ticket 56481
*
* @param string $path The path to test.
*/
public function test_head_request_with_specified_fields_returns_success_response( $path ) {
$request = new WP_REST_Request( 'HEAD', $path );
$request->set_param( '_fields', 'id' );
$server = rest_get_server();
$response = $server->dispatch( $request );
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
}
/**
* Data provider intended to provide paths for testing HEAD requests.
*
* @return array
*/
public static function data_head_request_with_specified_fields_returns_success_response() {
return array(
'get_item request' => array( '/wp/v2/categories/1' ),
'get_items request' => array( '/wp/v2/categories' ),
);
}
}

View File

@ -3567,6 +3567,36 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase
if ( 'HEAD' !== $method ) {
return null;
}
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
* @dataProvider data_head_request_with_specified_fields_returns_success_response
* @ticket 56481
*
* @param string $path The path to test.
*/
public function test_head_request_with_specified_fields_returns_success_response( $path ) {
$request = new WP_REST_Request( 'HEAD', sprintf( $path, self::$approved_id ) );
$request->set_param( '_fields', 'id' );
$server = rest_get_server();
$response = $server->dispatch( $request );
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
}
/**
* Data provider intended to provide paths for testing HEAD requests.
*
* @return array
*/
public static function data_head_request_with_specified_fields_returns_success_response() {
return array(
'get_item request' => array( '/wp/v2/comments/%d' ),
'get_items request' => array( '/wp/v2/comments' ),
);
}
}

View File

@ -336,7 +336,38 @@ class WP_REST_Global_Styles_Revisions_Controller_Test extends WP_Test_REST_Contr
$request = new WP_REST_Request( 'HEAD', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status(), 'Response status is 200.' );
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
* @dataProvider data_head_request_with_specified_fields_returns_success_response
* @ticket 56481
*
* @param string $path The path to test.
*/
public function test_head_request_with_specified_fields_returns_success_response( $path ) {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', sprintf( $path, self::$global_styles_id, $this->revision_1_id ) );
$request->set_param( '_fields', 'id' );
$server = rest_get_server();
$response = $server->dispatch( $request );
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
}
/**
* Data provider intended to provide paths for testing HEAD requests.
*
* @return array
*/
public static function data_head_request_with_specified_fields_returns_success_response() {
return array(
'get_item request' => array( '/wp/v2/global-styles/%d/revisions/%d' ),
'get_items request' => array( '/wp/v2/global-styles/%d/revisions' ),
);
}
/**
@ -366,7 +397,7 @@ class WP_REST_Global_Styles_Revisions_Controller_Test extends WP_Test_REST_Contr
$request = new WP_REST_Request( 'HEAD', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions/' . $this->revision_1_id );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status(), 'Response status is 200.' );
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**

View File

@ -167,7 +167,24 @@ class WP_REST_Pattern_Directory_Controller_Test extends WP_Test_REST_Controller_
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
$this->assertSame( 0, $filter->get_call_count(), 'The "' . $hook_name . '" filter was called when it should not be for HEAD requests.' );
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
* @ticket 56481
*/
public function test_get_items_head_request_with_specified_fields_returns_success_response() {
wp_set_current_user( self::$contributor_id );
self::mock_successful_response( 'browse-all', true );
$request = new WP_REST_Request( 'HEAD', '/wp/v2/pattern-directory/patterns' );
$request->set_param( '_fields', 'id' );
$server = rest_get_server();
$response = $server->dispatch( $request );
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
}
/**

View File

@ -111,7 +111,37 @@ class WP_Test_REST_Post_Types_Controller extends WP_Test_REST_Controller_Testcas
if ( 'HEAD' !== $method ) {
return null;
}
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
* @dataProvider data_head_request_with_specified_fields_returns_success_response
* @ticket 56481
*
* @param string $path The path to test.
*/
public function test_head_request_with_specified_fields_returns_success_response( $path ) {
$request = new WP_REST_Request( 'HEAD', $path );
$request->set_param( '_fields', 'slug' );
$server = rest_get_server();
$response = $server->dispatch( $request );
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
}
/**
* Data provider intended to provide paths for testing HEAD requests.
*
* @return array
*/
public static function data_head_request_with_specified_fields_returns_success_response() {
return array(
'get_item request' => array( '/wp/v2/types/post' ),
'get_items request' => array( '/wp/v2/types' ),
);
}
/**
@ -324,7 +354,7 @@ class WP_Test_REST_Post_Types_Controller extends WP_Test_REST_Controller_Testcas
remove_filter( $hook_name, $callback );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
$this->assertSame( 0, $filter->get_call_count(), 'The "' . $hook_name . '" filter was called when it should not be for HEAD requests.' );
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
protected function check_post_type_obj( $context, $post_type_obj, $data, $links ) {

View File

@ -295,7 +295,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$headers = $response->get_headers();
$this->assertSame( 0, $filter->get_call_count(), 'The "' . $hook_name . '" filter was called when it should not be for HEAD requests.' );
$this->assertArrayHasKey( 'Link', $headers, 'The "Link" header should be present in the response.' );
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
@ -320,7 +320,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$response = rest_get_server()->dispatch( $request );
if ( $request->is_method( 'HEAD' ) ) {
$this->assertNull( $response->get_data(), 'Failed asserting that response data is null for HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'Failed asserting that response data is null for HEAD request.' );
} else {
$this->assertSame( array(), $response->get_data(), 'Failed asserting that response data is an empty array for GET request.' );
}
@ -351,7 +351,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$this->assertCount( $total_posts, $response->get_data() );
} else {
$this->assertNull( $response->get_data(), 'Failed asserting that response data is null for HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'Failed asserting that response data is null for HEAD request.' );
$headers = $response->get_headers();
$this->assertSame( $total_posts, $headers['X-WP-Total'] );
}
@ -366,7 +366,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$this->assertCount( 2, $data );
$this->assertSameSets( array( self::$editor_id, self::$author_id ), wp_list_pluck( $data, 'author' ) );
} else {
$this->assertNull( $data, 'Failed asserting that response data is null for HEAD request.' );
$this->assertSame( array(), $data, 'Failed asserting that response data is null for HEAD request.' );
$headers = $response->get_headers();
$this->assertSame( 2, $headers['X-WP-Total'], 'Failed asserting that X-WP-Total header is 2.' );
}
@ -381,7 +381,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$this->assertCount( 1, $data );
$this->assertSame( self::$editor_id, $data[0]['author'] );
} else {
$this->assertNull( $data, 'Failed asserting that response data is null for HEAD request.' );
$this->assertSame( array(), $data, 'Failed asserting that response data is null for HEAD request.' );
$headers = $response->get_headers();
$this->assertSame( 1, $headers['X-WP-Total'], 'Failed asserting that X-WP-Total header is 1.' );
}
@ -407,7 +407,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
if ( $request->is_method( 'get' ) ) {
$this->assertCount( $total_posts, $response->get_data() );
} else {
$this->assertNull( $response->get_data(), 'Failed asserting that response data is null for HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'Failed asserting that response data is null for HEAD request.' );
$headers = $response->get_headers();
$this->assertSame( $total_posts, $headers['X-WP-Total'], 'Failed asserting that the number of posts is correct.' );
}
@ -424,7 +424,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$this->assertNotEquals( self::$editor_id, $data[0]['author'] );
$this->assertNotEquals( self::$author_id, $data[0]['author'] );
} else {
$this->assertNull( $response->get_data(), 'Failed asserting that response data is null for HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'Failed asserting that response data is null for HEAD request.' );
$headers = $response->get_headers();
$this->assertSame( $total_posts - 2, $headers['X-WP-Total'], 'Failed asserting that the number of posts is correct.' );
}
@ -441,7 +441,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$this->assertNotEquals( self::$editor_id, $data[0]['author'] );
$this->assertNotEquals( self::$editor_id, $data[1]['author'] );
} else {
$this->assertNull( $response->get_data(), 'Failed asserting that response data is null for HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'Failed asserting that response data is null for HEAD request.' );
$headers = $response->get_headers();
$this->assertSame( $total_posts - 1, $headers['X-WP-Total'], 'Failed asserting that the number of posts is correct.' );
}
@ -483,7 +483,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$this->assertCount( 2, $data );
$this->assertSame( $id2, $data[0]['id'] );
} else {
$this->assertNull( $data, 'Failed asserting that response data is null for HEAD request.' );
$this->assertSame( array(), $data, 'Failed asserting that response data is null for HEAD request.' );
$headers = $response->get_headers();
$this->assertSame( 2, $headers['X-WP-Total'], 'Failed asserting that the number of posts is correct.' );
}
@ -498,7 +498,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$this->assertCount( 2, $data );
$this->assertSame( $id1, $data[0]['id'] );
} else {
$this->assertNull( $data, 'Failed asserting that response data is null for HEAD request.' );
$this->assertSame( array(), $data, 'Failed asserting that response data is null for HEAD request.' );
$headers = $response->get_headers();
$this->assertSame( 2, $headers['X-WP-Total'], 'Failed asserting that the number of posts is correct.' );
}
@ -2193,7 +2193,37 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
if ( 'HEAD' !== $method ) {
return null;
}
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
* @dataProvider data_head_request_with_specified_fields_returns_success_response
* @ticket 56481
*
* @param string $path The path to test.
*/
public function test_head_request_with_specified_fields_returns_success_response( $path ) {
$request = new WP_REST_Request( 'HEAD', sprintf( $path, self::$post_id ) );
$request->set_param( '_fields', 'id' );
$server = rest_get_server();
$response = $server->dispatch( $request );
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
}
/**
* Data provider intended to provide paths for testing HEAD requests.
*
* @return array
*/
public static function data_head_request_with_specified_fields_returns_success_response() {
return array(
'get_item request' => array( '/wp/v2/posts/%d' ),
'get_items request' => array( '/wp/v2/posts' ),
);
}
public function test_get_item_links() {

View File

@ -182,7 +182,7 @@ class WP_Test_REST_Revisions_Controller extends WP_Test_REST_Controller_Testcase
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
$this->assertSame( 0, $filter->get_call_count(), 'The "' . $hook_name . '" filter was called when it should not be for HEAD requests.' );
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
@ -300,7 +300,39 @@ class WP_Test_REST_Revisions_Controller extends WP_Test_REST_Controller_Testcase
if ( 'GET' === $method ) {
return null;
}
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
* @dataProvider data_head_request_with_specified_fields_returns_success_response
* @ticket 56481
*
* @param string $path The path to test.
*/
public function test_head_request_with_specified_fields_returns_success_response( $path ) {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'HEAD', sprintf( $path, self::$post_id, $this->revision_id1 ) );
$request->set_param( '_fields', 'id' );
$server = rest_get_server();
$response = $server->dispatch( $request );
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
}
/**
* Data provider intended to provide paths for testing HEAD requests.
*
* @return array
*/
public static function data_head_request_with_specified_fields_returns_success_response() {
return array(
'get_item request' => array( '/wp/v2/posts/%d/revisions/%d' ),
'get_items request' => array( '/wp/v2/posts/%d/revisions' ),
);
}
public function test_get_item_embed_context() {

View File

@ -171,7 +171,7 @@ class WP_Test_REST_Sidebars_Controller extends WP_Test_REST_Controller_Testcase
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
$this->assertSame( 0, $filter->get_call_count(), 'The "' . $hook_name . '" filter was called when it should not be for HEAD requests.' );
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
@ -567,7 +567,46 @@ class WP_Test_REST_Sidebars_Controller extends WP_Test_REST_Controller_Testcase
if ( 'HEAD' !== $method ) {
return null;
}
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
* @dataProvider data_head_request_with_specified_fields_returns_success_response
* @ticket 56481
*
* @param string $path The path to test.
*/
public function test_head_request_with_specified_fields_returns_success_response( $path ) {
$this->setup_sidebar(
'sidebar-1',
array(
'name' => 'Test sidebar',
)
);
$request = new WP_REST_Request( 'HEAD', $path );
// This endpoint doesn't seem to support _fields param, but we need to set it to reproduce the fatal error.
$request->set_param( '_fields', 'name' );
$server = rest_get_server();
$response = $server->dispatch( $request );
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
}
/**
* Data provider intended to provide paths for testing HEAD requests.
*
* @return array
*/
public static function data_head_request_with_specified_fields_returns_success_response() {
return array(
'get_item request' => array( '/wp/v2/sidebars/sidebar-1' ),
'get_items request' => array( '/wp/v2/sidebars' ),
);
}
/**

View File

@ -1614,6 +1614,37 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase {
if ( 'HEAD' !== $method ) {
return null;
}
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
* @dataProvider data_head_request_with_specified_fields_returns_success_response
* @ticket 56481
*
* @param string $path The path to test.
*/
public function test_head_request_with_specified_fields_returns_success_response( $path ) {
$tag_id = self::factory()->tag->create();
$request = new WP_REST_Request( 'HEAD', sprintf( $path, $tag_id ) );
$request->set_param( '_fields', 'id' );
$server = rest_get_server();
$response = $server->dispatch( $request );
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
}
/**
* Data provider intended to provide paths for testing HEAD requests.
*
* @return array
*/
public static function data_head_request_with_specified_fields_returns_success_response() {
return array(
'get_item request' => array( '/wp/v2/tags/%d' ),
'get_items request' => array( '/wp/v2/tags' ),
);
}
}

View File

@ -73,7 +73,7 @@ class WP_Test_REST_Taxonomies_Controller extends WP_Test_REST_Controller_Testcas
remove_filter( $hook_name, $callback );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
$this->assertSame( 0, $filter->get_call_count(), 'The "' . $hook_name . '" filter was called when it should not be for HEAD requests.' );
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
public function test_get_items_context_edit() {
@ -185,7 +185,37 @@ class WP_Test_REST_Taxonomies_Controller extends WP_Test_REST_Controller_Testcas
if ( 'HEAD' !== $method ) {
return null;
}
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
* @dataProvider data_head_request_with_specified_fields_returns_success_response
* @ticket 56481
*
* @param string $path The path to test.
*/
public function test_head_request_with_specified_fields_returns_success_response( $path ) {
$request = new WP_REST_Request( 'HEAD', $path );
$request->set_param( '_fields', 'name' );
$server = rest_get_server();
$response = $server->dispatch( $request );
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
}
/**
* Data provider intended to provide paths for testing HEAD requests.
*
* @return array
*/
public static function data_head_request_with_specified_fields_returns_success_response() {
return array(
'get_item request' => array( '/wp/v2/taxonomies/category' ),
'get_items request' => array( '/wp/v2/taxonomies' ),
);
}
public function test_get_item_edit_context() {

View File

@ -254,7 +254,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase {
);
if ( 'HEAD' === $method ) {
$this->assertNull( $response->get_data(), 'Expected null response data for HEAD request, but received non-null data.' );
$this->assertSame( array(), $response->get_data(), 'Expected null response data for HEAD request, but received non-null data.' );
return null;
}
@ -3251,7 +3251,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase {
if ( 'HEAD' !== $method ) {
return null;
}
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
@ -3272,7 +3272,7 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase {
$this->assertSame( 200, $response->get_status() );
if ( $is_head_request ) {
$this->assertNull( $response->get_data() );
$this->assertSame( array(), $response->get_data() );
} else {
$this->assertNotEmpty( $response->get_data() );
}
@ -3306,6 +3306,39 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase {
$this->assertMatchesRegularExpression( $pattern, $query->request, 'The SQL query does not match the expected string.' );
}
/**
* @dataProvider data_head_request_with_specified_fields_returns_success_response
* @ticket 56481
*
* @param string $path The path to test.
*/
public function test_head_request_with_specified_fields_returns_success_response( $path ) {
$user_id = self::factory()->user->create();
wp_set_current_user( self::$user );
$request = new WP_REST_Request( 'HEAD', sprintf( $path, $user_id ) );
$request->set_param( '_fields', 'id' );
$server = rest_get_server();
$response = $server->dispatch( $request );
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
}
/**
* Data provider intended to provide paths for testing HEAD requests.
*
* @return array
*/
public static function data_head_request_with_specified_fields_returns_success_response() {
return array(
'get_item request' => array( '/wp/v2/users/%d' ),
'get_items request' => array( '/wp/v2/users' ),
);
}
protected function check_user_data( $user, $data, $context, $links ) {
$this->assertSame( $user->ID, $data['id'] );
$this->assertSame( $user->display_name, $data['name'] );

View File

@ -129,7 +129,7 @@ class WP_Test_REST_Widget_Types_Controller extends WP_Test_REST_Controller_Testc
$request = new WP_REST_Request( 'HEAD', '/wp/v2/widget-types' );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
@ -227,7 +227,7 @@ class WP_Test_REST_Widget_Types_Controller extends WP_Test_REST_Controller_Testc
if ( 'HEAD' !== $method ) {
return null;
}
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
@ -242,6 +242,37 @@ class WP_Test_REST_Widget_Types_Controller extends WP_Test_REST_Controller_Testc
);
}
/**
* @dataProvider data_head_request_with_specified_fields_returns_success_response
* @ticket 56481
*
* @param string $path The path to test.
*/
public function test_head_request_with_specified_fields_returns_success_response( $path ) {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'HEAD', $path );
$request->set_param( '_fields', 'id' );
$server = rest_get_server();
$response = $server->dispatch( $request );
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
}
/**
* Data provider intended to provide paths for testing HEAD requests.
*
* @return array
*/
public static function data_head_request_with_specified_fields_returns_success_response() {
return array(
'get_item request' => array( '/wp/v2/widget-types/calendar' ),
'get_items request' => array( '/wp/v2/widget-types' ),
);
}
/**
* @ticket 41683
*/

View File

@ -472,7 +472,7 @@ class WP_Test_REST_Widgets_Controller extends WP_Test_REST_Controller_Testcase {
$this->assertNotWPError( $response );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
$this->assertSame( 0, $filter->get_call_count(), 'The "' . $hook_name . '" filter was called when it should not be for HEAD requests.' );
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
public function mocked_rss_response() {
@ -644,7 +644,68 @@ class WP_Test_REST_Widgets_Controller extends WP_Test_REST_Controller_Testcase {
if ( 'HEAD' !== $method ) {
return null;
}
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
* @dataProvider data_head_request_with_specified_fields_returns_success_response
* @ticket 56481
*
* @param string $path The path to test.
*/
public function test_head_request_with_specified_fields_returns_success_response( $path ) {
add_filter( 'pre_http_request', array( $this, 'mocked_rss_response' ) );
global $wp_widget_factory;
$wp_widget_factory->widgets['WP_Widget_RSS']->widget_options['show_instance_in_rest'] = false;
$block_content = '<!-- wp:paragraph --><p>Block test</p><!-- /wp:paragraph -->';
$this->setup_widget(
'rss',
1,
array(
'title' => 'RSS test',
'url' => 'https://wordpress.org/news/feed',
)
);
$this->setup_widget(
'block',
1,
array(
'content' => $block_content,
)
);
$this->setup_sidebar(
'sidebar-1',
array(
'name' => 'Test sidebar',
),
array( 'block-1', 'rss-1', 'testwidget' )
);
$request = new WP_REST_Request( 'HEAD', $path );
$request->set_param( '_fields', 'id' );
$server = rest_get_server();
$response = $server->dispatch( $request );
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
remove_filter( 'pre_http_request', array( $this, 'mocked_rss_response' ) );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
}
/**
* Data provider intended to provide paths for testing HEAD requests.
*
* @return array
*/
public static function data_head_request_with_specified_fields_returns_success_response() {
return array(
'get_item request' => array( '/wp/v2/widgets/block-1' ),
'get_items request' => array( '/wp/v2/widgets' ),
);
}
/**

View File

@ -131,7 +131,25 @@ class Tests_REST_WpRestBlockPatternCategoriesController extends WP_Test_REST_Con
$request = new WP_REST_Request( 'HEAD', static::REQUEST_ROUTE );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
* @ticket 56481
*
* @param string $path The path to test.
*/
public function test_head_request_with_specified_fields_returns_success_response() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'HEAD', static::REQUEST_ROUTE );
$request->set_param( '_fields', 'name' );
$server = rest_get_server();
$response = $server->dispatch( $request );
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
}
/**

View File

@ -316,7 +316,7 @@ class Tests_REST_wpRestTemplateAutosavesController extends WP_Test_REST_Controll
);
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status(), 'Response status is 200.' );
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
@ -470,7 +470,7 @@ class Tests_REST_wpRestTemplateAutosavesController extends WP_Test_REST_Controll
$request = new WP_REST_Request( 'HEAD', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/autosaves/' . $autosave_post_id );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status(), 'Response status is 200.' );
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
@ -485,6 +485,79 @@ class Tests_REST_wpRestTemplateAutosavesController extends WP_Test_REST_Controll
);
}
/**
* @dataProvider data_get_item_with_data_provider
* @covers WP_REST_Template_Autosaves_Controller::get_item
* @ticket 56922
*
* @param string $parent_post_property_name A class property name that contains the parent post object.
* @param string $rest_base Base part of the REST API endpoint to test.
* @param string $template_id Template ID to use in the test.
*/
public function test_get_item_head_request_with_specified_fields_returns_success_response( $parent_post_property_name, $rest_base, $template_id ) {
wp_set_current_user( self::$admin_id );
$parent_post = self::$$parent_post_property_name;
$autosave_post_id = wp_create_post_autosave(
array(
'post_content' => 'Autosave content.',
'post_ID' => $parent_post->ID,
'post_type' => $parent_post->post_type,
)
);
$request = new WP_REST_Request(
'HEAD',
'/wp/v2/' . $rest_base . '/' . $template_id . '/autosaves/' . $autosave_post_id
);
$request->set_param( '_fields', 'id' );
$server = rest_get_server();
$response = $server->dispatch( $request );
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
}
/**
* @dataProvider data_get_items_with_data_provider
* @covers WP_REST_Template_Autosaves_Controller::get_items
* @ticket 56922
*
* @param string $parent_post_property_name A class property name that contains the parent post object.
* @param string $rest_base Base part of the REST API endpoint to test.
* @param string $template_id Template ID to use in the test.
*/
public function test_get_items_head_request_with_specified_fields_returns_success_response( $parent_post_property_name, $rest_base, $template_id ) {
wp_set_current_user( self::$admin_id );
// Cannot access this property in the data provider because it is not initialized at the time of execution.
$parent_post = self::$$parent_post_property_name;
wp_create_post_autosave(
array(
'post_content' => 'Autosave content.',
'post_ID' => $parent_post->ID,
'post_type' => $parent_post->post_type,
)
);
$request = new WP_REST_Request(
'HEAD',
'/wp/v2/' . $rest_base . '/' . $template_id . '/autosaves'
);
$request->set_param( '_fields', 'id' );
$server = rest_get_server();
$response = $server->dispatch( $request );
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
}
/**
* @coversNothing
* @ticket 56922

View File

@ -437,7 +437,7 @@ class Tests_REST_wpRestTemplateRevisionsController extends WP_Test_REST_Controll
);
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status(), 'Response status is 200.' );
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
@ -627,7 +627,7 @@ class Tests_REST_wpRestTemplateRevisionsController extends WP_Test_REST_Controll
$request = new WP_REST_Request( 'HEAD', '/wp/v2/templates/' . self::TEST_THEME . '/' . self::TEMPLATE_NAME . '/revisions/' . $revision_id );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status(), 'Response status is 200.' );
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
@ -642,6 +642,65 @@ class Tests_REST_wpRestTemplateRevisionsController extends WP_Test_REST_Controll
);
}
/**
* @dataProvider data_get_item_with_data_provider
* @covers WP_REST_Template_Revisions_Controller::get_item
* @ticket 56922
*
* @param string $parent_post_property_name A class property name that contains the parent post object.
* @param string $rest_base Base part of the REST API endpoint to test.
* @param string $template_id Template ID to use in the test.
*/
public function test_get_item_head_request_with_specified_fields_returns_success_response( $parent_post_property_name, $rest_base, $template_id ) {
wp_set_current_user( self::$admin_id );
$parent_post = self::$$parent_post_property_name;
$revisions = wp_get_post_revisions( $parent_post, array( 'fields' => 'ids' ) );
$revision_id = array_shift( $revisions );
$request = new WP_REST_Request(
'HEAD',
'/wp/v2/' . $rest_base . '/' . $template_id . '/revisions/' . $revision_id
);
$request->set_param( '_fields', 'id' );
$server = rest_get_server();
$response = $server->dispatch( $request );
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
}
/**
* @dataProvider data_get_items_with_data_provider
* @covers WP_REST_Template_Revisions_Controller::get_items
* @ticket 56922
*
* @param string $parent_post_property_name A class property name that contains the parent post object.
* @param string $rest_base Base part of the REST API endpoint to test.
* @param string $template_id Template ID to use in the test.
*/
public function test_get_items_head_request_with_specified_fields_returns_success_response( $parent_post_property_name, $rest_base, $template_id ) {
wp_set_current_user( self::$admin_id );
$parent_post = self::$$parent_post_property_name;
$request = new WP_REST_Request(
'HEAD',
'/wp/v2/' . $rest_base . '/' . $template_id . '/revisions'
);
$request->set_param( '_fields', 'id' );
$server = rest_get_server();
$response = $server->dispatch( $request );
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
}
/**
* @dataProvider data_get_item_not_found
* @covers WP_REST_Template_Revisions_Controller::get_item

View File

@ -185,7 +185,38 @@ class Tests_REST_WpRestTemplatesController extends WP_Test_REST_Controller_Testc
$request = new WP_REST_Request( 'HEAD', '/wp/v2/templates' );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status(), 'Response status is 200.' );
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**
* @dataProvider data_head_request_with_specified_fields_returns_success_response
* @ticket 56481
*
* @param string $path The path to test.
*/
public function test_head_request_with_specified_fields_returns_success_response( $path ) {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'HEAD', $path );
$request->set_param( '_fields', 'id' );
$server = rest_get_server();
$response = $server->dispatch( $request );
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
$response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
$this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
}
/**
* Data provider intended to provide paths for testing HEAD requests.
*
* @return array
*/
public static function data_head_request_with_specified_fields_returns_success_response() {
return array(
'get_item request' => array( '/wp/v2/templates/default//my_template' ),
'get_items request' => array( '/wp/v2/templates' ),
);
}
/**
@ -291,7 +322,7 @@ class Tests_REST_WpRestTemplatesController extends WP_Test_REST_Controller_Testc
$request = new WP_REST_Request( 'HEAD', '/wp/v2/templates/default//my_template' );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status(), 'Response status is 200.' );
$this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
$this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
}
/**