Merge pull request #28731 from woocommerce/fix/28729

Restore stock_status in V3 response.
This commit is contained in:
Peter Fabian 2021-01-06 14:25:09 +01:00 committed by GitHub
commit 7640c7f1bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 543 additions and 37 deletions

View File

@ -260,13 +260,28 @@ class WC_REST_Orders_V2_Controller extends WC_REST_CRUD_Controller {
$extra_fields = array( 'meta_data', 'line_items', 'tax_lines', 'shipping_lines', 'fee_lines', 'coupon_lines', 'refunds' );
$format_decimal = array( 'discount_total', 'discount_tax', 'shipping_total', 'shipping_tax', 'shipping_total', 'shipping_tax', 'cart_tax', 'total', 'total_tax' );
$format_date = array( 'date_created', 'date_modified', 'date_completed', 'date_paid' );
// These fields are dependent on other fields.
$dependent_fields = array(
'date_created_gmt' => 'date_created',
'date_modified_gmt' => 'date_modified',
'date_completed_gmt' => 'date_completed',
'date_paid_gmt' => 'date_paid',
);
$format_line_items = array( 'line_items', 'tax_lines', 'shipping_lines', 'fee_lines', 'coupon_lines' );
// Only fetch fields that we need.
$fields = $this->get_fields_for_response( $this->request );
$fields = $this->get_fields_for_response( $this->request );
foreach ( $dependent_fields as $field_key => $dependency ) {
if ( in_array( $field_key, $fields ) && ! in_array( $dependency, $fields ) ) {
$fields[] = $dependency;
}
}
$extra_fields = array_intersect( $extra_fields, $fields );
$format_decimal = array_intersect( $format_decimal, $fields );
$format_date = array_intersect( $format_date, $fields );
$format_line_items = array_intersect( $format_line_items, $fields );
$data = $order->get_base_data();
@ -293,6 +308,7 @@ class WC_REST_Orders_V2_Controller extends WC_REST_CRUD_Controller {
$data['coupon_lines'] = $order->get_items( 'coupon' );
break;
case 'refunds':
$data['refunds'] = array();
foreach ( $order->get_refunds() as $refund ) {
$data['refunds'][] = array(
'id' => $refund->get_id(),

View File

@ -158,8 +158,9 @@ class WC_REST_Products_V2_Controller extends WC_REST_CRUD_Controller {
* @return WP_REST_Response
*/
public function prepare_object_for_response( $object, $request ) {
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->get_product_data( $object, $context, $request );
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$this->request = $request;
$data = $this->get_product_data( $object, $context, $request );
// Add variations to variable products.
if ( $object->is_type( 'variable' ) && $object->has_child() ) {

View File

@ -153,7 +153,8 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
}
$args['meta_query'] = $this->add_meta_query( // WPCS: slow query ok.
$args, array(
$args,
array(
'key' => '_sku',
'value' => $skus,
'compare' => 'IN',
@ -164,7 +165,8 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
// Filter by tax class.
if ( ! empty( $request['tax_class'] ) ) {
$args['meta_query'] = $this->add_meta_query( // WPCS: slow query ok.
$args, array(
$args,
array(
'key' => '_tax_class',
'value' => 'standard' !== $request['tax_class'] ? $request['tax_class'] : '',
)
@ -179,7 +181,8 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
// Filter product by stock_status.
if ( ! empty( $request['stock_status'] ) ) {
$args['meta_query'] = $this->add_meta_query( // WPCS: slow query ok.
$args, array(
$args,
array(
'key' => '_stock_status',
'value' => $request['stock_status'],
)
@ -332,7 +335,9 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
if ( 'variation' === $product->get_type() ) {
return new WP_Error(
"woocommerce_rest_invalid_{$this->post_type}_id", __( 'To manipulate product variations you should use the /products/<product_id>/variations/<id> endpoint.', 'woocommerce' ), array(
"woocommerce_rest_invalid_{$this->post_type}_id",
__( 'To manipulate product variations you should use the /products/<product_id>/variations/<id> endpoint.', 'woocommerce' ),
array(
'status' => 404,
)
);
@ -1376,12 +1381,12 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
*/
protected function get_product_data( $product, $context = 'view' ) {
$data = parent::get_product_data( ...func_get_args() );
// Replace in_stock with stock_status.
$pos = array_search( 'in_stock', array_keys( $data ), true );
if ( false !== $pos ) {
$array_section_1 = array_slice( $data, 0, $pos, true );
$array_section_2 = array_slice( $data, $pos + 1, null, true );
$data = $array_section_1 + array( 'stock_status' => $product->get_stock_status( $context ) ) + $array_section_2;
// Add stock_status if needed.
if ( isset( $this->request ) ) {
$fields = $this->get_fields_for_response( $this->request );
if ( in_array( 'stock_status', $fields ) ) {
$data['stock_status'] = $product->get_stock_status( $context );
}
}
return $data;
}

View File

@ -6,6 +6,108 @@
*/
class WC_REST_Order_V2_Controller_Test extends WC_REST_Unit_Test_case {
/**
* Setup our test server, endpoints, and user info.
*/
public function setUp() {
parent::setUp();
$this->endpoint = new WC_REST_Orders_V2_Controller();
$this->user = $this->factory->user->create(
array(
'role' => 'administrator',
)
);
}
/**
* Get all expected fields.
*/
public function get_expected_response_fields() {
return array(
'id',
'parent_id',
'number',
'order_key',
'created_via',
'version',
'status',
'currency',
'date_created',
'date_created_gmt',
'date_modified',
'date_modified_gmt',
'discount_total',
'discount_tax',
'shipping_total',
'shipping_tax',
'cart_tax',
'total',
'total_tax',
'prices_include_tax',
'customer_id',
'customer_ip_address',
'customer_user_agent',
'customer_note',
'billing',
'shipping',
'payment_method',
'payment_method_title',
'transaction_id',
'date_paid',
'date_paid_gmt',
'date_completed',
'date_completed_gmt',
'cart_hash',
'meta_data',
'line_items',
'tax_lines',
'shipping_lines',
'fee_lines',
'coupon_lines',
'currency_symbol',
'refunds',
);
}
/**
* Test that all expected response fields are present.
* Note: This has fields hardcoded intentionally instead of fetching from schema to test for any bugs in schema result. Add new fields manually when added to schema.
*/
public function test_orders_api_get_all_fields_v2() {
wp_set_current_user( $this->user );
$expected_response_fields = $this->get_expected_response_fields();
$order = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order( $this->user );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders/' . $order->get_id() ) );
$this->assertEquals( 200, $response->get_status() );
$response_fields = array_keys( $response->get_data() );
$this->assertEmpty( array_diff( $expected_response_fields, $response_fields ), 'These fields were expected but not present in API response: ' . print_r( array_diff( $expected_response_fields, $response_fields ), true ) );
$this->assertEmpty( array_diff( $response_fields, $expected_response_fields ), 'These fields were not expected in the API V2 response: ' . print_r( array_diff( $response_fields, $expected_response_fields ), true ) );
}
/**
* Test that all fields are returned when requested one by one.
*/
public function test_orders_get_each_field_one_by_one_v2() {
wp_set_current_user( $this->user );
$expected_response_fields = $this->get_expected_response_fields();
$order = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order( $this->user );
foreach ( $expected_response_fields as $field ) {
$request = new WP_REST_Request( 'GET', '/wc/v2/orders/' . $order->get_id() );
$request->set_param( '_fields', $field );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$response_fields = array_keys( $response->get_data() );
$this->assertContains( $field, $response_fields, "Field $field was expected but not present in order API V2 response." );
}
}
/**
* Test that `prepare_object_for_response` method works.
*/

View File

@ -0,0 +1,148 @@
<?php
/**
* class WC_REST_Products_Controller_Tests.
* Product Controller tests for V2 REST API.
*/
class WC_REST_Products_V2_Controller_Test extends WC_REST_Unit_Test_Case {
/**
* Setup our test server, endpoints, and user info.
*/
public function setUp() {
parent::setUp();
$this->endpoint = new WC_REST_Products_V2_Controller();
$this->user = $this->factory->user->create(
array(
'role' => 'administrator',
)
);
}
/**
* Get all expected fields.
*/
public function get_expected_response_fields() {
return array(
'id',
'name',
'slug',
'permalink',
'date_created',
'date_created_gmt',
'date_modified',
'date_modified_gmt',
'type',
'status',
'featured',
'catalog_visibility',
'description',
'short_description',
'sku',
'price',
'regular_price',
'sale_price',
'date_on_sale_from',
'date_on_sale_from_gmt',
'date_on_sale_to',
'date_on_sale_to_gmt',
'price_html',
'on_sale',
'purchasable',
'total_sales',
'virtual',
'downloadable',
'downloads',
'download_limit',
'download_expiry',
'external_url',
'button_text',
'tax_status',
'tax_class',
'manage_stock',
'stock_quantity',
'in_stock',
'backorders',
'backorders_allowed',
'backordered',
'sold_individually',
'weight',
'dimensions',
'shipping_required',
'shipping_taxable',
'shipping_class',
'shipping_class_id',
'reviews_allowed',
'average_rating',
'rating_count',
'related_ids',
'upsell_ids',
'cross_sell_ids',
'parent_id',
'purchase_note',
'categories',
'tags',
'images',
'attributes',
'default_attributes',
'variations',
'grouped_products',
'menu_order',
'meta_data',
);
}
/**
* Test that all expected response fields are present.
* Note: This has fields hardcoded intentionally instead of fetching from schema to test for any bugs in schema result. Add new fields manually when added to schema.
*/
public function test_product_api_get_all_fields_v2() {
wp_set_current_user( $this->user );
$expected_response_fields = $this->get_expected_response_fields();
$product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product();
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() ) );
$this->assertEquals( 200, $response->get_status() );
$response_fields = array_keys( $response->get_data() );
$this->assertEmpty( array_diff( $expected_response_fields, $response_fields ), 'These fields were expected but not present in API V2 response: ' . print_r( array_diff( $expected_response_fields, $response_fields ), true ) );
$this->assertEmpty( array_diff( $response_fields, $expected_response_fields ), 'These fields were not expected in the API V2 response: ' . print_r( array_diff( $response_fields, $expected_response_fields ), true ) );
}
/**
* Test that `get_product_data` function works without silent `request` parameter as it used to.
* TODO: Fix the underlying design issue when DI gets available.
*/
public function test_get_product_data_should_work_without_request_param() {
$product = WC_Helper_Product::create_simple_product();
$product->save();
// Workaround to call protected method.
$call_product_data_wrapper = function () use ( $product ) {
return $this->get_product_data( $product );
};
$response = $call_product_data_wrapper->call( new WC_REST_Products_Controller() );
$this->assertArrayHasKey( 'id', $response );
}
/**
* Test that all fields are returned when requested one by one.
*/
public function test_products_get_each_field_one_by_one_v2() {
wp_set_current_user( $this->user );
$expected_response_fields = $this->get_expected_response_fields();
$product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product();
foreach ( $expected_response_fields as $field ) {
$request = new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() );
$request->set_param( '_fields', $field );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$response_fields = array_keys( $response->get_data() );
$this->assertContains( $field, $response_fields, "Field $field was expected but not present in product API V2 response." );
}
}
}

View File

@ -0,0 +1,110 @@
<?php
/**
* class WC_REST_Orders_Controller_Tests.
* Orders Controller tests for V3 REST API.
*/
class WC_REST_Orders_Controller_Tests extends WC_REST_Unit_Test_Case {
/**
* Setup our test server, endpoints, and user info.
*/
public function setUp() {
parent::setUp();
$this->endpoint = new WC_REST_Orders_Controller();
$this->user = $this->factory->user->create(
array(
'role' => 'administrator',
)
);
}
/**
* Get all expected fields.
*/
public function get_expected_response_fields() {
return array(
'id',
'parent_id',
'number',
'order_key',
'created_via',
'version',
'status',
'currency',
'date_created',
'date_created_gmt',
'date_modified',
'date_modified_gmt',
'discount_total',
'discount_tax',
'shipping_total',
'shipping_tax',
'cart_tax',
'total',
'total_tax',
'prices_include_tax',
'customer_id',
'customer_ip_address',
'customer_user_agent',
'customer_note',
'billing',
'shipping',
'payment_method',
'payment_method_title',
'transaction_id',
'date_paid',
'date_paid_gmt',
'date_completed',
'date_completed_gmt',
'cart_hash',
'meta_data',
'line_items',
'tax_lines',
'shipping_lines',
'fee_lines',
'coupon_lines',
'currency_symbol',
'refunds',
);
}
/**
* Test that all expected response fields are present.
* Note: This has fields hardcoded intentionally instead of fetching from schema to test for any bugs in schema result. Add new fields manually when added to schema.
*/
public function test_orders_api_get_all_fields() {
wp_set_current_user( $this->user );
$expected_response_fields = $this->get_expected_response_fields();
$order = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order( $this->user );
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/orders/' . $order->get_id() ) );
$this->assertEquals( 200, $response->get_status() );
$response_fields = array_keys( $response->get_data() );
$this->assertEmpty( array_diff( $expected_response_fields, $response_fields ), 'These fields were expected but not present in API response: ' . print_r( array_diff( $expected_response_fields, $response_fields ), true ) );
$this->assertEmpty( array_diff( $response_fields, $expected_response_fields ), 'These fields were not expected in the API response: ' . print_r( array_diff( $response_fields, $expected_response_fields ), true ) );
}
/**
* Test that all fields are returned when requested one by one.
*/
public function test_orders_get_each_field_one_by_one() {
wp_set_current_user( $this->user );
$expected_response_fields = $this->get_expected_response_fields();
$order = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order( $this->user );
foreach ( $expected_response_fields as $field ) {
$request = new WP_REST_Request( 'GET', '/wc/v3/orders/' . $order->get_id() );
$request->set_param( '_fields', $field );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$response_fields = array_keys( $response->get_data() );
$this->assertContains( $field, $response_fields, "Field $field was expected but not present in order API response." );
}
}
}

View File

@ -1,24 +0,0 @@
<?php
/**
* class WC_REST_Products_Controller_Tests.
* Product Controller tests for V3 REST API.
*/
class WC_REST_Products_Controller_Tests extends WC_REST_Unit_Test_Case {
/**
* Test that `get_product_data` function works without silent `request` parameter as it used to.
* TODO: Fix the underlying design issue when DI gets available.
*/
public function test_get_product_data_should_work_without_request_param() {
$product = WC_Helper_Product::create_simple_product();
$product->save();
// Workaround to call protected method.
$call_product_data_wrapper = function () use ( $product ) {
return $this->get_product_data( $product );
};
$response = $call_product_data_wrapper->call( new WC_REST_Products_Controller() );
$this->assertArrayHasKey( 'id', $response );
}
}

View File

@ -0,0 +1,148 @@
<?php
/**
* class WC_REST_Products_Controller_Tests.
* Product Controller tests for V3 REST API.
*/
class WC_REST_Products_Controller_Tests extends WC_REST_Unit_Test_Case {
/**
* Setup our test server, endpoints, and user info.
*/
public function setUp() {
parent::setUp();
$this->endpoint = new WC_REST_Products_Controller();
$this->user = $this->factory->user->create(
array(
'role' => 'administrator',
)
);
}
/**
* Get all expected fields.
*/
public function get_expected_response_fields() {
return array(
'id',
'name',
'slug',
'permalink',
'date_created',
'date_created_gmt',
'date_modified',
'date_modified_gmt',
'type',
'status',
'featured',
'catalog_visibility',
'description',
'short_description',
'sku',
'price',
'regular_price',
'sale_price',
'date_on_sale_from',
'date_on_sale_from_gmt',
'date_on_sale_to',
'date_on_sale_to_gmt',
'price_html',
'on_sale',
'purchasable',
'total_sales',
'virtual',
'downloadable',
'downloads',
'download_limit',
'download_expiry',
'external_url',
'button_text',
'tax_status',
'tax_class',
'manage_stock',
'stock_quantity',
'stock_status',
'backorders',
'backorders_allowed',
'backordered',
'sold_individually',
'weight',
'dimensions',
'shipping_required',
'shipping_taxable',
'shipping_class',
'shipping_class_id',
'reviews_allowed',
'average_rating',
'rating_count',
'related_ids',
'upsell_ids',
'cross_sell_ids',
'parent_id',
'purchase_note',
'categories',
'tags',
'images',
'attributes',
'default_attributes',
'variations',
'grouped_products',
'menu_order',
'meta_data',
);
}
/**
* Test that all expected response fields are present.
* Note: This has fields hardcoded intentionally instead of fetching from schema to test for any bugs in schema result. Add new fields manually when added to schema.
*/
public function test_product_api_get_all_fields() {
wp_set_current_user( $this->user );
$expected_response_fields = $this->get_expected_response_fields();
$product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product();
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() ) );
$this->assertEquals( 200, $response->get_status() );
$response_fields = array_keys( $response->get_data() );
$this->assertEmpty( array_diff( $expected_response_fields, $response_fields ), 'These fields were expected but not present in API response: ' . print_r( array_diff( $expected_response_fields, $response_fields ), true ) );
$this->assertEmpty( array_diff( $response_fields, $expected_response_fields ), 'These fields were not expected in the API response: ' . print_r( array_diff( $response_fields, $expected_response_fields ), true ) );
}
/**
* Test that `get_product_data` function works without silent `request` parameter as it used to.
* TODO: Fix the underlying design issue when DI gets available.
*/
public function test_get_product_data_should_work_without_request_param() {
$product = WC_Helper_Product::create_simple_product();
$product->save();
// Workaround to call protected method.
$call_product_data_wrapper = function () use ( $product ) {
return $this->get_product_data( $product );
};
$response = $call_product_data_wrapper->call( new WC_REST_Products_Controller() );
$this->assertArrayHasKey( 'id', $response );
}
/**
* Test that all fields are returned when requested one by one.
*/
public function test_products_get_each_field_one_by_one() {
wp_set_current_user( $this->user );
$expected_response_fields = $this->get_expected_response_fields();
$product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product();
foreach ( $expected_response_fields as $field ) {
$request = new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() );
$request->set_param( '_fields', $field );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$response_fields = array_keys( $response->get_data() );
$this->assertContains( $field, $response_fields, "Field $field was expected but not present in product API response." );
}
}
}