-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Add new query args to Pattern Directory Controller #3861
Changes from all commits
f827f07
1cede3c
08dd54f
e7301ef
1f4dd3d
824488d
feee687
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,15 @@ class WP_REST_Pattern_Directory_Controller_Test extends WP_Test_REST_Controller_ | |
*/ | ||
private static $controller; | ||
|
||
/** | ||
* List of URLs captured. | ||
* | ||
* @since 6.2.0 | ||
* | ||
* @var string[] | ||
*/ | ||
protected static $http_request_urls; | ||
|
||
/** | ||
* Set up class test fixtures. | ||
* | ||
|
@@ -44,9 +53,30 @@ public static function wpSetUpBeforeClass( $factory ) { | |
) | ||
); | ||
|
||
self::$http_request_urls = array(); | ||
|
||
static::$controller = new WP_REST_Pattern_Directory_Controller(); | ||
} | ||
|
||
/** | ||
* Tear down after class. | ||
* | ||
* @since 6.2.0 | ||
*/ | ||
public static function wpTearDownAfterClass() { | ||
self::delete_user( self::$contributor_id ); | ||
} | ||
|
||
/** | ||
* Clear the captured request URLs after each test. | ||
* | ||
* @since 6.2.0 | ||
*/ | ||
public function tear_down() { | ||
self::$http_request_urls = array(); | ||
parent::tear_down(); | ||
} | ||
|
||
/** | ||
* Asserts that the pattern matches the expected response schema. | ||
* | ||
|
@@ -310,6 +340,176 @@ static function( $response ) { | |
$this->assertSame( 'modified the cache', $patterns[0] ); | ||
} | ||
|
||
/** | ||
* Tests if the provided query args are passed through to the wp.org API. | ||
* | ||
* @since 6.2.0 | ||
* | ||
* @ticket 57501 | ||
* | ||
* @covers WP_REST_Pattern_Directory_Controller::get_items | ||
* | ||
* @dataProvider data_get_items_query_args | ||
* | ||
* @param string $param Query parameter name (ex, page). | ||
* @param mixed $value Query value to test. | ||
* @param bool $is_error Whether this value should error or not. | ||
* @param mixed $expected Expected value (or expected error code). | ||
*/ | ||
public function test_get_items_query_args( $param, $value, $is_error, $expected ) { | ||
wp_set_current_user( self::$contributor_id ); | ||
add_filter( 'pre_http_request', array( $this, 'mock_request_to_apiwporg_url' ), 10, 3 ); | ||
|
||
$request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' ); | ||
if ( $value ) { | ||
$request->set_query_params( array( $param => $value ) ); | ||
} | ||
|
||
$response = rest_do_request( $request ); | ||
$data = $response->get_data(); | ||
if ( $is_error ) { | ||
$this->assertSame( $expected, $data['code'], 'Response error code does not match' ); | ||
$this->assertStringContainsString( $param, $data['message'], 'Response error message does not match' ); | ||
} else { | ||
$this->assertCount( 1, self::$http_request_urls, 'The number of HTTP Request URLs is not 1' ); | ||
$this->assertStringContainsString( $param . '=' . $expected, self::$http_request_urls[0], 'The param and/or value do not match' ); | ||
} | ||
} | ||
|
||
/** | ||
* Data provider. | ||
* | ||
* return array[] | ||
*/ | ||
public function data_get_items_query_args() { | ||
return array( | ||
'per_page default' => array( | ||
'param' => 'per_page', | ||
'value' => false, | ||
'is_error' => false, | ||
'expected' => 100, | ||
), | ||
'per_page custom-1' => array( | ||
'param' => 'per_page', | ||
'value' => 5, | ||
'is_error' => false, | ||
'expected' => 5, | ||
), | ||
'per_page custom-2' => array( | ||
'param' => 'per_page', | ||
'value' => 50, | ||
'is_error' => false, | ||
'expected' => 50, | ||
), | ||
'per_page invalid-1' => array( | ||
'param' => 'per_page', | ||
'value' => 200, | ||
'is_error' => true, | ||
'expected' => 'rest_invalid_param', | ||
), | ||
'per_page invalid-2' => array( | ||
'param' => 'per_page', | ||
'value' => 'abc', | ||
'is_error' => true, | ||
'expected' => 'rest_invalid_param', | ||
), | ||
|
||
'page default' => array( | ||
'param' => 'page', | ||
'value' => false, | ||
'is_error' => false, | ||
'expected' => 1, | ||
), | ||
'page custom' => array( | ||
'param' => 'page', | ||
'value' => 5, | ||
'is_error' => false, | ||
'expected' => 5, | ||
), | ||
'page invalid' => array( | ||
'param' => 'page', | ||
'value' => 'abc', | ||
'is_error' => true, | ||
'expected' => 'rest_invalid_param', | ||
), | ||
|
||
'offset custom' => array( | ||
'param' => 'offset', | ||
'value' => 5, | ||
'is_error' => false, | ||
'expected' => 5, | ||
), | ||
'offset invalid-1' => array( | ||
'param' => 'offset', | ||
'value' => 'abc', | ||
'is_error' => true, | ||
'expected' => 'rest_invalid_param', | ||
), | ||
|
||
'order default' => array( | ||
'param' => 'order', | ||
'value' => false, | ||
'is_error' => false, | ||
'expected' => 'desc', | ||
), | ||
'order custom' => array( | ||
'param' => 'order', | ||
'value' => 'asc', | ||
'is_error' => false, | ||
'expected' => 'asc', | ||
), | ||
'order invalid-1' => array( | ||
'param' => 'order', | ||
'value' => 10, | ||
'is_error' => true, | ||
'expected' => 'rest_invalid_param', | ||
), | ||
'order invalid-2' => array( | ||
'param' => 'order', | ||
'value' => 'fake', | ||
'is_error' => true, | ||
'expected' => 'rest_invalid_param', | ||
), | ||
|
||
'orderby default' => array( | ||
'param' => 'orderby', | ||
'value' => false, | ||
'is_error' => false, | ||
'expected' => 'date', | ||
), | ||
'orderby custom-1' => array( | ||
'param' => 'orderby', | ||
'value' => 'title', | ||
'is_error' => false, | ||
'expected' => 'title', | ||
), | ||
'orderby custom-2' => array( | ||
'param' => 'orderby', | ||
'value' => 'date', | ||
'is_error' => false, | ||
'expected' => 'date', | ||
), | ||
'orderby custom-3' => array( | ||
'param' => 'orderby', | ||
'value' => 'favorite_count', | ||
'is_error' => false, | ||
'expected' => 'favorite_count', | ||
), | ||
'orderby invalid-1' => array( | ||
'param' => 'orderby', | ||
'value' => 10, | ||
'is_error' => true, | ||
'expected' => 'rest_invalid_param', | ||
), | ||
'orderby invalid-2' => array( | ||
'param' => 'orderby', | ||
'value' => 'fake', | ||
'is_error' => true, | ||
'expected' => 'rest_invalid_param', | ||
), | ||
); | ||
} | ||
|
||
/** | ||
* @doesNotPerformAssertions | ||
*/ | ||
|
@@ -573,4 +773,33 @@ static function ( $response, $parsed_args, $url ) use ( $blocked_host ) { | |
3 | ||
); | ||
} | ||
|
||
/** | ||
* Mock the request to wp.org URL to capture the URLs. | ||
* | ||
* @since 6.2.0 | ||
* | ||
* @return array faux/mocked response. | ||
*/ | ||
public function mock_request_to_apiwporg_url( $response, $args, $url ) { | ||
Comment on lines
+777
to
+784
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. In the core, some mock functions have the document and some don't. I appreciated @hellofromtonya's and @SergeyBiryukov's input on this. 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. I know. The handbook and similar test mocks needs some love to align for consistency. I agree! |
||
if ( 'api.wordpress.org' !== wp_parse_url( $url, PHP_URL_HOST ) ) { | ||
return $response; | ||
} | ||
|
||
self::$http_request_urls[] = $url; | ||
|
||
// Return a response to prevent external API request. | ||
$response = array( | ||
'headers' => array(), | ||
'response' => array( | ||
'code' => 200, | ||
'message' => 'OK', | ||
), | ||
'body' => '[]', | ||
'cookies' => array(), | ||
'filename' => null, | ||
); | ||
|
||
return $response; | ||
} | ||
} |
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.
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.
I think this is unnecessary. The naming convention of
test_
anddata_
align to know that this data provider is for the matching test.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.
Whoops forgot to share where the naming convention is identified the handbook 🤦♀️
https://make.wordpress.org/core/handbook/testing/automated-testing/writing-phpunit-tests/#repetitive-tests.