Skip to content

Commit

Permalink
Allow /sites/${site}/external-media/copy/pexels to insert post meta d…
Browse files Browse the repository at this point in the history
…ata (#21659)
  • Loading branch information
mreishus authored Nov 12, 2021
1 parent b8945db commit 11cda7a
Show file tree
Hide file tree
Showing 3 changed files with 232 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,58 @@ class WPCOM_REST_API_V2_Endpoint_External_Media extends WP_REST_Controller {
* @var array
*/
public $media_schema = array(
'type' => 'object',
'required' => true,
'properties' => array(
'caption' => array(
'type' => 'string',
),
'guid' => array(
'items' => array(
'caption' => array(
'type' => 'string',
),
'name' => array(
'type' => 'string',
),
'title' => array(
'type' => 'string',
'type' => 'array',
'items' => array(
'type' => 'object',
'required' => true,
'properties' => array(
'caption' => array(
'type' => 'string',
),
'guid' => array(
'type' => 'object',
'properties' => array(
'caption' => array(
'type' => 'string',
),
'name' => array(
'type' => 'string',
),
'title' => array(
'type' => 'string',
),
'url' => array(
'format' => 'uri',
'type' => 'string',
),
),
'url' => array(
'format' => 'uri',
'type' => 'string',
),
'title' => array(
'type' => 'string',
),
'meta' => array(
'type' => 'object',
'additionalProperties' => false,
'properties' => array(
'vertical_id' => array(
'type' => 'string',
'format' => 'text-field',
),
'pexels_object' => array(
'type' => 'object',
),
'orientations' => array(
'type' => 'array',
'items' => array(
'type' => 'string',
'enum' => array( 'landscape', 'portrait', 'square' ),
),
'minItems' => 1,
'maxItems' => 3,
'uniqueItems' => true,
),
),
),
'type' => 'array',
),
'title' => array(
'type' => 'string',
),
),
);
Expand Down Expand Up @@ -467,6 +493,12 @@ public function update_attachment_meta( $id, $item ) {
'post_excerpt' => $item['caption'],
)
);

if ( ! empty( $item['meta'] ) ) {
foreach ( $item['meta'] as $meta_key => $meta_value ) {
update_post_meta( $id, $meta_key, $meta_value );
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: other

Allow /sites/$site/external-media/copy/pexels to insert post meta data
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function test_list_google_photos_unauthenticated() {
}

/**
* Tests list response with unauthenticated Google Photos.
* Tests copy response with pexels while not setting metadata.
*/
public function test_copy_image() {
$tmp_name = $this->get_temp_name( static::$image_path );
Expand Down Expand Up @@ -147,6 +147,178 @@ public function test_copy_image() {
$this->assertEmpty( $data['alt'] );
}

/**
* Tests copy response with pexels while setting metadata.
*/
public function test_copy_image_meta() {
$tmp_name = $this->get_temp_name( static::$image_path );
if ( file_exists( $tmp_name ) ) {
unlink( $tmp_name );
}

add_filter( 'pre_http_request', array( $this, 'mock_image_data' ), 10, 3 );
add_filter( 'wp_handle_sideload_prefilter', array( $this, 'copy_image' ) );
add_filter( 'wp_check_filetype_and_ext', array( $this, 'mock_extensions' ) );

$request = wp_rest_request( Requests::POST, '/wpcom/v2/external-media/copy/pexels' );
$request->set_body_params(
array(
'media' => array(
array(
'guid' => wp_json_encode(
array(
'url' => static::$image_path,
'name' => $this->image_name,
)
),
'meta' => array(
'vertical_id' => 'v1234',
'pexels_object' => array(
'information' => 'goes here',
),
'orientations' => array(
'landscape',
'square',
),
),
),
),
)
);
$response = $this->server->dispatch( $request );
$data = $response->get_data()[0];

remove_filter( 'pre_http_request', array( $this, 'mock_image_data' ) );
remove_filter( 'wp_handle_sideload_prefilter', array( $this, 'copy_image' ) );
remove_filter( 'wp_check_filetype_and_ext', array( $this, 'mock_extensions' ) );

// Check API response.
$this->assertArrayHasKey( 'id', $data );
$this->assertArrayHasKey( 'caption', $data );
$this->assertArrayHasKey( 'alt', $data );
$this->assertArrayHasKey( 'type', $data );
$this->assertArrayHasKey( 'url', $data );
$this->assertEquals( 'image', $data['type'] );
$this->assertIsInt( $data['id'] );
$this->assertEmpty( $data['caption'] );
$this->assertEmpty( $data['alt'] );

// Look inside the post_meta of the post added.
$meta = get_post_meta( $data['id'] );
$this->assertArrayHasKey( 'vertical_id', $meta );
$this->assertArrayHasKey( 'pexels_object', $meta );
$this->assertArrayHasKey( 'orientations', $meta );
$this->assertArrayNotHasKey( 'not_allowed_key', $meta );
$this->assertEquals( $meta['vertical_id'][0], 'v1234' );

$orientations = maybe_unserialize( $meta['orientations'][0] );
$this->assertEquals( $orientations[0], 'landscape' );
$this->assertEquals( $orientations[1], 'square' );

$pexels_object = maybe_unserialize( $meta['pexels_object'][0] );
$this->assertEquals( $pexels_object['information'], 'goes here' );
}

/**
* Tests copy response with pexels while setting metadata: Invalid meta keys should fail.
*/
public function test_copy_image_meta_invalid_meta_key() {
$tmp_name = $this->get_temp_name( static::$image_path );
if ( file_exists( $tmp_name ) ) {
unlink( $tmp_name );
}

add_filter( 'pre_http_request', array( $this, 'mock_image_data' ), 10, 3 );
add_filter( 'wp_handle_sideload_prefilter', array( $this, 'copy_image' ) );
add_filter( 'wp_check_filetype_and_ext', array( $this, 'mock_extensions' ) );

$request = wp_rest_request( Requests::POST, '/wpcom/v2/external-media/copy/pexels' );
$request->set_body_params(
array(
'media' => array(
array(
'guid' => wp_json_encode(
array(
'url' => static::$image_path,
'name' => $this->image_name,
)
),
'meta' => array(
'vertical_id' => 'v1234',
'pexels_object' => array(
'information' => 'goes here',
),
'orientations' => array(
'landscape',
'square',
),
'this_meta_key' => 'is_not_allowed',
),
),
),
)
);

$response = $this->server->dispatch( $request );

remove_filter( 'pre_http_request', array( $this, 'mock_image_data' ) );
remove_filter( 'wp_handle_sideload_prefilter', array( $this, 'copy_image' ) );
remove_filter( 'wp_check_filetype_and_ext', array( $this, 'mock_extensions' ) );

$this->assertEquals( $response->status, 400 );
$this->assertEquals( $response->data['data']['params']['media'], 'this_meta_key is not a valid property of Object.' );
}

/**
* Tests copy response with pexels while setting metadata: Invalid meta 'orientation' values should fail.
*/
public function test_copy_image_meta_invalid_meta_orientation() {
$tmp_name = $this->get_temp_name( static::$image_path );
if ( file_exists( $tmp_name ) ) {
unlink( $tmp_name );
}

add_filter( 'pre_http_request', array( $this, 'mock_image_data' ), 10, 3 );
add_filter( 'wp_handle_sideload_prefilter', array( $this, 'copy_image' ) );
add_filter( 'wp_check_filetype_and_ext', array( $this, 'mock_extensions' ) );

$request = wp_rest_request( Requests::POST, '/wpcom/v2/external-media/copy/pexels' );
$request->set_body_params(
array(
'media' => array(
array(
'guid' => wp_json_encode(
array(
'url' => static::$image_path,
'name' => $this->image_name,
)
),
'meta' => array(
'vertical_id' => 'v1234',
'pexels_object' => array(
'information' => 'goes here',
),
'orientations' => array(
'landscape',
'square',
'not_a_real_orientation',
),
),
),
),
)
);

$response = $this->server->dispatch( $request );

remove_filter( 'pre_http_request', array( $this, 'mock_image_data' ) );
remove_filter( 'wp_handle_sideload_prefilter', array( $this, 'copy_image' ) );
remove_filter( 'wp_check_filetype_and_ext', array( $this, 'mock_extensions' ) );

$this->assertEquals( $response->status, 400 );
$this->assertEquals( $response->data['data']['params']['media'], 'media[0][meta][orientations][2] is not one of landscape, portrait, and square.' );
}

/**
* Tests connection response for Google Photos.
*/
Expand Down

0 comments on commit 11cda7a

Please sign in to comment.