Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Return null or object for ImageAttachmentSchema response #9962

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
6 changes: 3 additions & 3 deletions src/StoreApi/Schemas/V1/ImageAttachmentSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function get_properties() {
* Convert a WooCommerce product into an object suitable for the response.
*
* @param int $attachment_id Image attachment ID.
* @return array|null
* @return object|null
*/
public function get_item_response( $attachment_id ) {
if ( ! $attachment_id ) {
Expand All @@ -80,12 +80,12 @@ public function get_item_response( $attachment_id ) {
$attachment = wp_get_attachment_image_src( $attachment_id, 'full' );

if ( ! is_array( $attachment ) ) {
return [];
return null;
}

$thumbnail = wp_get_attachment_image_src( $attachment_id, 'woocommerce_thumbnail' );

return [
return (object) [
'id' => (int) $attachment_id,
'src' => current( $attachment ),
'thumbnail' => current( $thumbnail ),
Expand Down
81 changes: 75 additions & 6 deletions tests/php/StoreApi/Routes/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ protected function setUp(): void {
$this->products = array(
$fixtures->get_simple_product(
array(
'name' => 'Test Product 1',
'stock_status' => 'instock',
'regular_price' => 10,
'image_id' => $fixtures->sideload_image(),
'name' => 'Test Product 1',
'stock_status' => 'instock',
'regular_price' => 10,
'image_id' => $fixtures->sideload_image(),
'gallery_image_ids' => array(),
)
),
$fixtures->get_simple_product(
Expand Down Expand Up @@ -64,6 +65,10 @@ public function test_get_item() {
$this->assertEquals( $this->products[0]->add_to_cart_text(), $data['add_to_cart']->text );
$this->assertEquals( $this->products[0]->add_to_cart_description(), $data['add_to_cart']->description );
$this->assertEquals( $this->products[0]->is_on_sale(), $data['on_sale'] );

$this->assertCount( 1, $data['images'] );
$this->assertIsObject( $data['images'][0] );
$this->assertEquals( $this->products[0]->get_image_id(), $data['images'][0]->id );
}

/**
Expand Down Expand Up @@ -94,7 +99,7 @@ public function test_get_items() {
}

/**
* Test searching by SKU
* Test searching by SKU.
*/
public function test_search_by_sku() {
$product = new \WC_Product_Simple();
Expand Down Expand Up @@ -190,6 +195,70 @@ public function test_get_item_schema() {
$validate = new ValidateSchema( $schema );

$diff = $validate->get_diff_from_object( $response->get_data() );
$this->assertEmpty( $diff, print_r( $diff, true ) );
$this->assertEmpty( $diff, print_r( $diff, true ) ); // @phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
}

/**
* Test return types when no image is available.
*/
public function test_without_image() {
$fixtures = new FixtureData();
$product = $fixtures->get_simple_product(
array(
'name' => 'Test Product 1',
'stock_status' => 'instock',
'regular_price' => 10,
'image_id' => '',
'gallery_image_ids' => array(),
)
);

$response = rest_get_server()->dispatch( new \WP_REST_Request( 'GET', '/wc/store/v1/products/' . $product->get_id() ) );
$data = $response->get_data();

$this->assertIsArray( $data['images'] );
$this->assertCount( 0, $data['images'] );

$image_id = $fixtures->sideload_image();
$product = $fixtures->get_simple_product(
array(
'name' => 'Test Product 1',
'stock_status' => 'instock',
'regular_price' => 10,
'image_id' => $image_id,
'gallery_image_ids' => array(),
)
);
wp_delete_attachment( $image_id, true );

$response = rest_get_server()->dispatch( new \WP_REST_Request( 'GET', '/wc/store/v1/products/' . $product->get_id() ) );
$data = $response->get_data();

$this->assertIsArray( $data['images'] );
$this->assertCount( 0, $data['images'] );
}

/**
* Test product category image return types.
*/
public function test_product_category_image_return_types() {
$fixtures = new FixtureData();
$image_id = $fixtures->sideload_image();
$term = wp_insert_term( 'Test Category', 'product_cat' );

update_term_meta( $term['term_id'], 'thumbnail_id', $image_id );

$response = rest_get_server()->dispatch( new \WP_REST_Request( 'GET', '/wc/store/v1/products/categories/' . $term['term_id'] ) );
$data = $response->get_data();

$this->assertIsObject( $data['image'] );
$this->assertEquals( $data['image']->id, $image_id );

delete_term_meta( $term['term_id'], 'thumbnail_id' );

$response = rest_get_server()->dispatch( new \WP_REST_Request( 'GET', '/wc/store/v1/products/categories/' . $term['term_id'] ) );
$data = $response->get_data();

$this->assertNull( $data['image'] );
}
}