2020-12-28 08:40:22 +00:00
< ? php
/**
* class WC_REST_Order_V2_Controller_Test .
* Orders controller test .
*/
class WC_REST_Order_V2_Controller_Test extends WC_REST_Unit_Test_case {
2021-01-06 09:55:17 +00:00
/**
* 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. " );
}
}
2020-12-28 08:40:22 +00:00
/**
* Test that `prepare_object_for_response` method works .
*/
public function test_prepare_object_for_response () {
$order = WC_Helper_Order :: create_order ();
$order -> save ();
$response = ( new WC_REST_Orders_V2_Controller () ) -> prepare_object_for_response ( $order , new WP_REST_Request () );
$this -> assertArrayHasKey ( 'id' , $response -> data );
$this -> assertEquals ( $order -> get_id (), $response -> data [ 'id' ] );
}
}