Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/fix-external-media-tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

Switched external media API tests to short-circuit API calls before they're being dispatched.
Original file line number Diff line number Diff line change
Expand Up @@ -323,16 +323,16 @@ public function test_copy_image_meta_invalid_meta_orientation() {
* Tests connection response for Google Photos.
*/
public function test_connection_google_photos() {
add_filter( 'pre_http_request', array( $this, 'mock_wpcom_api_response_connection_google_photos' ), 10, 3 );
add_filter( 'rest_pre_dispatch', array( $this, 'mock_wpcom_api_response_connection_google_photos' ), 10, 3 );

$request = wp_rest_request( Requests::GET, '/wpcom/v2/external-media/connection/google_photos' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$data = json_decode( wp_remote_retrieve_body( $response->get_data() ) );

$this->assertEquals( 'google_photos', $data['ID'] );
$this->assertNotEmpty( $data['connect_URL'] );
$this->assertEquals( 'google_photos', $data->ID );
$this->assertNotEmpty( $data->connect_URL ); //phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase

remove_filter( 'pre_http_request', array( $this, 'mock_wpcom_api_response_connection_google_photos' ) );
remove_filter( 'rest_pre_dispatch', array( $this, 'mock_wpcom_api_response_connection_google_photos' ) );
}

/**
Expand All @@ -341,16 +341,16 @@ public function test_connection_google_photos() {
* @covers ::delete_connection
*/
public function test_delete_connection_google_photos() {
add_filter( 'pre_http_request', array( $this, 'mock_wpcom_api_response_delete_connection_google_photos' ), 10, 3 );
add_filter( 'rest_pre_dispatch', array( $this, 'mock_wpcom_api_response_delete_connection_google_photos' ), 10, 3 );

$request = wp_rest_request( Requests::DELETE, '/wpcom/v2/external-media/connection/google_photos' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$data = json_decode( wp_remote_retrieve_body( $response->get_data() ) );

$this->assertNotEmpty( $data['ID'] );
$this->assertTrue( $data['deleted'] );
$this->assertNotEmpty( $data->ID );
$this->assertTrue( $data->deleted );

remove_filter( 'pre_http_request', array( $this, 'mock_wpcom_api_response_delete_connection_google_photos' ) );
remove_filter( 'rest_pre_dispatch', array( $this, 'mock_wpcom_api_response_delete_connection_google_photos' ) );
}

/**
Expand All @@ -362,19 +362,19 @@ public function test_delete_connection_google_photos() {
* @param string $method Request method.
*/
public function test_connection_google_photos_with_error( $method ) {
add_filter( 'pre_http_request', array( $this, 'mock_wpcom_api_external_media_connection_response_with_error' ), 10, 3 );
add_filter( 'rest_pre_dispatch', array( $this, 'mock_wpcom_api_external_media_connection_response_with_error' ), 10, 3 );

$request = wp_rest_request( $method, '/wpcom/v2/external-media/connection/google_photos' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$data = json_decode( wp_remote_retrieve_body( $response->get_data() ) );

$this->assertNotEmpty( $data['code'] );
$this->assertSame( 'rest_not_found', $data['code'] );
$this->assertSame( 'Connection with this ID not found.', $data['message'] );
$this->assertArrayHasKey( 'status', $data['data'] );
$this->assertSame( 404, $data['data']['status'] );
$this->assertNotEmpty( $data->code );
$this->assertSame( 'rest_not_found', $data->code );
$this->assertSame( 'Connection with this ID not found.', $data->message );
$this->assertObjectHasAttribute( 'status', $data->data );
$this->assertSame( 404, $data->data->status );

remove_filter( 'pre_http_request', array( $this, 'mock_wpcom_api_external_media_connection_response_with_error' ) );
remove_filter( 'rest_pre_dispatch', array( $this, 'mock_wpcom_api_external_media_connection_response_with_error' ) );
}

/**
Expand All @@ -384,8 +384,8 @@ public function test_connection_google_photos_with_error( $method ) {
*/
public function google_photos_request_methods() {
return array(
array( Requests::GET ),
array( Requests::DELETE ),
'GET' => array( Requests::GET ),
'DELETE' => array( Requests::DELETE ),
);
}

Expand Down Expand Up @@ -478,14 +478,15 @@ public function mock_image_data( $response, $args, $url ) {
/**
* Validate the "connection" Jetpack API request for Google Photos and mock the response.
*
* @param bool $response Whether to preempt an HTTP request's return value. Default false.
* @param array $args HTTP request arguments.
* @param string $url The request URL.
* @param mixed $result Response to replace the requested version with. Can be anything
* a normal endpoint can return, or null to not hijack the request.
* @param WP_REST_Server $server Server instance.
* @param WP_REST_Request $request Request used to generate the response.
* @return array
*/
public function mock_wpcom_api_response_connection_google_photos( $response, $args, $url ) {
$this->assertEquals( WP_REST_Server::READABLE, $args['method'] );
$this->assertStringStartsWith( 'https://public-api.wordpress.com/wpcom/v2/meta/external-media/connection/google_photos', $url );
public function mock_wpcom_api_response_connection_google_photos( $result, $server, $request ) {
$this->assertEquals( WP_REST_Server::READABLE, $request->get_method() );
$this->assertStringEndsWith( '/external-media/connection/google_photos', $request->get_route() );

return array(
'headers' => array(
Expand All @@ -503,14 +504,15 @@ public function mock_wpcom_api_response_connection_google_photos( $response, $ar
/**
* Validate the "delete connection" Jetpack API request for Google Photos and mock the response.
*
* @param bool $response Whether to preempt an HTTP request's return value. Default false.
* @param array $args HTTP request arguments.
* @param string $url The request URL.
* @param mixed $result Response to replace the requested version with. Can be anything
* a normal endpoint can return, or null to not hijack the request.
* @param WP_REST_Server $server Server instance.
* @param WP_REST_Request $request Request used to generate the response.
* @return array
*/
public function mock_wpcom_api_response_delete_connection_google_photos( $response, $args, $url ) {
$this->assertEquals( WP_REST_Server::DELETABLE, $args['method'] );
$this->assertStringStartsWith( 'https://public-api.wordpress.com/wpcom/v2/meta/external-media/connection/google_photos', $url );
public function mock_wpcom_api_response_delete_connection_google_photos( $result, $server, $request ) {
$this->assertEquals( WP_REST_Server::DELETABLE, $request->get_method() );
$this->assertStringEndsWith( '/external-media/connection/google_photos', $request->get_route() );

return array(
'headers' => array(
Expand All @@ -528,13 +530,14 @@ public function mock_wpcom_api_response_delete_connection_google_photos( $respon
/**
* Validate the "delete connection" Jetpack API request for Google Photos and mock the response.
*
* @param bool $response Whether to preempt an HTTP request's return value. Default false.
* @param array $args HTTP request arguments.
* @param string $url The request URL.
* @param mixed $result Response to replace the requested version with. Can be anything
* a normal endpoint can return, or null to not hijack the request.
* @param WP_REST_Server $server Server instance.
* @param WP_REST_Request $request Request used to generate the response.
* @return array
*/
public function mock_wpcom_api_external_media_connection_response_with_error( $response, $args, $url ) {
$this->assertStringStartsWith( 'https://public-api.wordpress.com/wpcom/v2/meta/external-media/connection/google_photos', $url );
public function mock_wpcom_api_external_media_connection_response_with_error( $result, $server, $request ) {
$this->assertStringEndsWith( '/external-media/connection/google_photos', $request->get_route() );

return array(
'headers' => array(
Expand Down