mirrored from git://develop.git.wordpress.org/
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't prepare the response body for HEAD requests in WP_REST_Taxonomies_Controller #7925
Closed
anton-vlasenko
wants to merge
6
commits into
WordPress:trunk
from
anton-vlasenko:add/short-circuit-head-requests-in-taxonomies-controller
+119
−12
Closed
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e1692a4
Don't prepare response for a single taxonomy.
anton-vlasenko 8bec4d1
Short-circuit the get_items method.
anton-vlasenko 362cdb1
Add more unit tests.
anton-vlasenko 8378c83
Add more unit tests.
anton-vlasenko 4c12f48
Fix the tests.
anton-vlasenko f119b28
Fix code style.
anton-vlasenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,24 @@ public function test_get_items() { | |
$this->assertSame( 'tags', $data['post_tag']['rest_base'] ); | ||
} | ||
|
||
/** | ||
* @ticket 56481 | ||
*/ | ||
public function test_get_items_with_head_request_should_not_prepare_taxonomy_data() { | ||
$request = new WP_REST_Request( 'HEAD', '/wp/v2/taxonomies' ); | ||
$hook_name = 'rest_prepare_taxonomy'; | ||
$filter = new MockAction(); | ||
$callback = array( $filter, 'filter' ); | ||
add_filter( $hook_name, $callback ); | ||
$response = rest_get_server()->dispatch( $request ); | ||
remove_filter( $hook_name, $callback ); | ||
Comment on lines
+67
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: I know this is "just a test", but my inner monk wants to sort this. so that this is better readable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in f119b28. |
||
|
||
$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.' ); | ||
} | ||
|
||
public function test_get_items_context_edit() { | ||
wp_set_current_user( self::$contributor_id ); | ||
$request = new WP_REST_Request( 'GET', '/wp/v2/taxonomies' ); | ||
|
@@ -79,26 +97,54 @@ public function test_get_items_context_edit() { | |
$this->assertSame( 'tags', $data['post_tag']['rest_base'] ); | ||
} | ||
|
||
public function test_get_items_invalid_permission_for_context() { | ||
|
||
/** | ||
* @dataProvider data_readable_http_methods | ||
* @ticket 56481 | ||
* | ||
* @param string $method HTTP method to use. | ||
*/ | ||
public function test_get_items_invalid_permission_for_context( $method ) { | ||
wp_set_current_user( 0 ); | ||
$request = new WP_REST_Request( 'GET', '/wp/v2/taxonomies' ); | ||
$request = new WP_REST_Request( $method, '/wp/v2/taxonomies' ); | ||
$request->set_param( 'context', 'edit' ); | ||
$response = rest_get_server()->dispatch( $request ); | ||
$this->assertErrorResponse( 'rest_cannot_view', $response, 401 ); | ||
} | ||
|
||
/** | ||
* Data provider intended to provide HTTP method names for testing GET and HEAD requests. | ||
* | ||
* @return array | ||
*/ | ||
public static function data_readable_http_methods() { | ||
return array( | ||
'GET request' => array( 'GET' ), | ||
'HEAD request' => array( 'HEAD' ), | ||
); | ||
} | ||
|
||
public function test_get_taxonomies_for_type() { | ||
$request = new WP_REST_Request( 'GET', '/wp/v2/taxonomies' ); | ||
$request->set_param( 'type', 'post' ); | ||
$response = rest_get_server()->dispatch( $request ); | ||
$this->check_taxonomies_for_type_response( 'post', $response ); | ||
} | ||
|
||
public function test_get_taxonomies_for_invalid_type() { | ||
$request = new WP_REST_Request( 'GET', '/wp/v2/taxonomies' ); | ||
/** | ||
* @dataProvider data_readable_http_methods | ||
* @ticket 56481 | ||
* | ||
* @param string $method HTTP method to use. | ||
*/ | ||
public function test_get_taxonomies_for_invalid_type( $method ) { | ||
$request = new WP_REST_Request( $method, '/wp/v2/taxonomies' ); | ||
$request->set_param( 'type', 'wingding' ); | ||
$response = rest_get_server()->dispatch( $request ); | ||
$this->assertSame( 200, $response->get_status() ); | ||
if ( 'HEAD' === $method ) { | ||
return; | ||
} | ||
$data = $response->get_data(); | ||
$this->assertSame( '{}', json_encode( $data ) ); | ||
} | ||
|
@@ -109,6 +155,24 @@ public function test_get_item() { | |
$this->check_taxonomy_object_response( 'view', $response ); | ||
} | ||
|
||
/** | ||
* @ticket 56481 | ||
*/ | ||
public function test_get_item_with_head_request_should_not_prepare_taxonomy_data() { | ||
$request = new WP_REST_Request( 'HEAD', '/wp/v2/taxonomies/category' ); | ||
$hook_name = 'rest_prepare_taxonomy'; | ||
$filter = new MockAction(); | ||
$callback = array( $filter, 'filter' ); | ||
add_filter( $hook_name, $callback ); | ||
$response = rest_get_server()->dispatch( $request ); | ||
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.' ); | ||
} | ||
|
||
public function test_get_item_edit_context() { | ||
$editor_id = self::factory()->user->create( array( 'role' => 'editor' ) ); | ||
wp_set_current_user( $editor_id ); | ||
|
@@ -118,33 +182,57 @@ public function test_get_item_edit_context() { | |
$this->check_taxonomy_object_response( 'edit', $response ); | ||
} | ||
|
||
public function test_get_item_invalid_permission_for_context() { | ||
/** | ||
* @dataProvider data_readable_http_methods | ||
* @ticket 56481 | ||
* | ||
* @param string $method HTTP method to use. | ||
*/ | ||
public function test_get_item_invalid_permission_for_context( $method ) { | ||
wp_set_current_user( 0 ); | ||
$request = new WP_REST_Request( 'GET', '/wp/v2/taxonomies/category' ); | ||
$request = new WP_REST_Request( $method, '/wp/v2/taxonomies/category' ); | ||
$request->set_param( 'context', 'edit' ); | ||
$response = rest_get_server()->dispatch( $request ); | ||
$this->assertErrorResponse( 'rest_forbidden_context', $response, 401 ); | ||
} | ||
|
||
public function test_get_invalid_taxonomy() { | ||
$request = new WP_REST_Request( 'GET', '/wp/v2/taxonomies/invalid' ); | ||
/** | ||
* @dataProvider data_readable_http_methods | ||
* @ticket 56481 | ||
* | ||
* @param string $method HTTP method to use. | ||
*/ | ||
public function test_get_invalid_taxonomy( $method ) { | ||
$request = new WP_REST_Request( $method, '/wp/v2/taxonomies/invalid' ); | ||
$response = rest_get_server()->dispatch( $request ); | ||
$this->assertErrorResponse( 'rest_taxonomy_invalid', $response, 404 ); | ||
} | ||
|
||
public function test_get_non_public_taxonomy_not_authenticated() { | ||
/** | ||
* @dataProvider data_readable_http_methods | ||
* @ticket 56481 | ||
* | ||
* @param string $method HTTP method to use. | ||
*/ | ||
public function test_get_non_public_taxonomy_not_authenticated( $method ) { | ||
register_taxonomy( 'api-private', 'post', array( 'public' => false ) ); | ||
|
||
$request = new WP_REST_Request( 'GET', '/wp/v2/taxonomies/api-private' ); | ||
$request = new WP_REST_Request( $method, '/wp/v2/taxonomies/api-private' ); | ||
$response = rest_get_server()->dispatch( $request ); | ||
$this->assertErrorResponse( 'rest_forbidden', $response, 401 ); | ||
} | ||
|
||
public function test_get_non_public_taxonomy_no_permission() { | ||
/** | ||
* @dataProvider data_readable_http_methods | ||
* @ticket 56481 | ||
* | ||
* @param string $method HTTP method to use. | ||
*/ | ||
public function test_get_non_public_taxonomy_no_permission( $method ) { | ||
wp_set_current_user( self::$contributor_id ); | ||
register_taxonomy( 'api-private', 'post', array( 'public' => false ) ); | ||
|
||
$request = new WP_REST_Request( 'GET', '/wp/v2/taxonomies/api-private' ); | ||
$request = new WP_REST_Request( $method, '/wp/v2/taxonomies/api-private' ); | ||
$response = rest_get_server()->dispatch( $request ); | ||
$this->assertErrorResponse( 'rest_forbidden', $response, 403 ); | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MInimum required PHP version allows the use of type hints, and for bool it is already in use at other places, so imho we should introduce this new function with a type hint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review.
There is currently no agreement on using return type declarations in WordPress Core, so I’d refrain from using them for now. I’m fine with using them as soon as the Community reaches a consensus.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The HTML API for example uses it, so there are already some examples for return types in core. But I get your point, so waiting for a consensus is fine with me.