Merge branch 'fix/api/229-unit-test' of https://github.com/shiki/woocommerce into fix/api/229

This commit is contained in:
vedanshujain 2020-10-14 13:53:03 +05:30
commit 8e12de5d3c
2 changed files with 44 additions and 24 deletions

View File

@ -199,32 +199,42 @@ class WC_REST_Orders_V2_Controller extends WC_REST_CRUD_Controller {
// Expand meta_data to include user-friendly values. // Expand meta_data to include user-friendly values.
$formatted_meta_data = $item->get_formatted_meta_data( null, true ); $formatted_meta_data = $item->get_formatted_meta_data( null, true );
$data['meta_data'] = array_map( $data['meta_data'] = array_map(
array( $this, 'clean_formatted_order_item_meta_data' ), array( $this, 'merge_meta_item_with_formatted_meta_display_attributes' ),
$formatted_meta_data, $data['meta_data'],
array_keys( $formatted_meta_data ) array_fill( 0, count( $data['meta_data'] ), $formatted_meta_data )
); );
return $data; return $data;
} }
/** /**
* Sanitizes an object from the array returned by {@link WC_Order_Item::get_formatted_meta_data} and includes * Merge the `$formatted_meta_data` `display_key` and `display_value` attribute values into the corresponding
* the {@link WC_Meta_Data} `id` in the resulting array. * {@link WC_Meta_Data}. Returns the merged array.
*
* @param WC_Meta_Data $meta_item An object from {@link WC_Order_Item::get_meta_data()}.
* @param Object[] $formatted_meta_data An object result from {@link WC_Order_Item::get_formatted_meta_data}.
* The keys are the IDs of {@link WC_Meta_Data}.
* *
* @param Object $meta_data An object result from {@link WC_Order_Item::get_formatted_meta_data}.
* This is expected to have the properties `key`, `value`, `display_key`, and `display_value`.
* @param integer $meta_data_id The id of the {@link WC_Meta_Data}.
* @return array * @return array
*/ */
private function clean_formatted_order_item_meta_data( $meta_data, $meta_data_id ) { private function merge_meta_item_with_formatted_meta_display_attributes( $meta_item, $formatted_meta_data ) {
return array( $result = array(
'id' => $meta_data_id, 'id' => $meta_item->id,
'key' => $meta_data->key, 'key' => $meta_item->key,
'value' => $meta_data->value, 'value' => $meta_item->value,
'display_key' => wc_clean( $meta_data->display_key ), 'display_key' => null,
'display_value' => wc_clean( $meta_data->display_value ), 'display_value' => null,
); );
if ( array_key_exists( $meta_item->id, $formatted_meta_data ) ) {
$formatted_meta_item = $formatted_meta_data[ $meta_item->id ];
$result['display_key'] = wc_clean( $formatted_meta_item->display_key );
$result['display_value'] = wc_clean( $formatted_meta_item->display_value );
}
return $result;
} }
/** /**

View File

@ -155,7 +155,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
$site_level_attribute_id = wc_create_attribute( array( 'name' => 'Site Level Type' ) ); $site_level_attribute_id = wc_create_attribute( array( 'name' => 'Site Level Type' ) );
$site_level_attribute_slug = wc_attribute_taxonomy_name_by_id( $site_level_attribute_id ); $site_level_attribute_slug = wc_attribute_taxonomy_name_by_id( $site_level_attribute_id );
// Register the attribute so that wp_insert_term will be successful // Register the attribute so that wp_insert_term will be successful.
register_taxonomy( $site_level_attribute_slug, array( 'product' ), array() ); register_taxonomy( $site_level_attribute_slug, array( 'product' ), array() );
$site_level_term_insertion_result = wp_insert_term( 'Site Level Value - Wood', $site_level_attribute_slug ); $site_level_term_insertion_result = wp_insert_term( 'Site Level Value - Wood', $site_level_attribute_slug );
@ -166,9 +166,9 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
$line_item = new WC_Order_Item_Product(); $line_item = new WC_Order_Item_Product();
$line_item->set_product( $variation ); $line_item->set_product( $variation );
$line_item->set_props( array( $line_item->set_props(
'variation' => array( "attribute_{$site_level_attribute_slug}" => $site_level_term->slug ) array( 'variation' => array( "attribute_{$site_level_attribute_slug}" => $site_level_term->slug ) )
) ); );
$order = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order(); $order = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order();
$order->add_item( $line_item ); $order->add_item( $line_item );
@ -316,8 +316,9 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
$this->assertEquals( $order->get_shipping_country(), $data['shipping']['country'] ); $this->assertEquals( $order->get_shipping_country(), $data['shipping']['country'] );
$this->assertEquals( 1, count( $data['line_items'] ) ); $this->assertEquals( 1, count( $data['line_items'] ) );
$this->assertEquals( 1, count( $data['shipping_lines'] ) ); $this->assertEquals( 1, count( $data['shipping_lines'] ) );
$shipping = current( $order->get_items( 'shipping' ) ); $shipping = current( $order->get_items( 'shipping' ) );
$expected = array( $expected_shipping_line = array(
'id' => $shipping->get_id(), 'id' => $shipping->get_id(),
'method_title' => $shipping->get_method_title(), 'method_title' => $shipping->get_method_title(),
'method_id' => $shipping->get_method_id(), 'method_id' => $shipping->get_method_id(),
@ -325,9 +326,18 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
'total' => wc_format_decimal( $shipping->get_total(), '' ), 'total' => wc_format_decimal( $shipping->get_total(), '' ),
'total_tax' => wc_format_decimal( $shipping->get_total_tax(), '' ), 'total_tax' => wc_format_decimal( $shipping->get_total_tax(), '' ),
'taxes' => array(), 'taxes' => array(),
'meta_data' => $shipping->get_meta_data(),
); );
$this->assertEquals( $expected, $data['shipping_lines'][0] ); foreach ( $expected_shipping_line as $key => $value ) {
$this->assertEquals( $value, $data['shipping_lines'][0][ $key ] );
}
$actual_shipping_line_meta_data = $data['shipping_lines'][0]['meta_data'];
$this->assertCount( 3, $actual_shipping_line_meta_data );
foreach ( $shipping->get_meta_data() as $index => $expected_meta_item ) {
$this->assertEquals( $expected_meta_item->id, $actual_shipping_line_meta_data[ $index ]['id'] );
$this->assertEquals( $expected_meta_item->key, $actual_shipping_line_meta_data[ $index ]['key'] );
$this->assertEquals( $expected_meta_item->value, $actual_shipping_line_meta_data[ $index ]['value'] );
}
} }
/** /**
@ -870,6 +880,6 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
$meta_data_item_properties = $line_item_properties['meta_data']['items']['properties']; $meta_data_item_properties = $line_item_properties['meta_data']['items']['properties'];
$this->assertEquals( 5, count( $meta_data_item_properties ) ); $this->assertEquals( 5, count( $meta_data_item_properties ) );
$this->assertEquals( [ 'id', 'key', 'value', 'display_key', 'display_value' ], array_keys( $meta_data_item_properties ) ); $this->assertEquals( array( 'id', 'key', 'value', 'display_key', 'display_value' ), array_keys( $meta_data_item_properties ) );
} }
} }