From 1fdc24daa91e3a18dc2f466edda1bcb47152655d Mon Sep 17 00:00:00 2001 From: vedanshujain Date: Wed, 6 Jan 2021 14:19:26 +0530 Subject: [PATCH] Add unit test for verifying that all expected fields are present. --- .../class-wc-rest-orders-controller-tests.php | 84 ++++++++++++ ...class-wc-rest-producs-controller-tests.php | 24 ---- ...lass-wc-rest-products-controller-tests.php | 122 ++++++++++++++++++ 3 files changed, 206 insertions(+), 24 deletions(-) create mode 100644 tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-orders-controller-tests.php delete mode 100644 tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-producs-controller-tests.php create mode 100644 tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-products-controller-tests.php diff --git a/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-orders-controller-tests.php b/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-orders-controller-tests.php new file mode 100644 index 00000000000..21c5814eeb6 --- /dev/null +++ b/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-orders-controller-tests.php @@ -0,0 +1,84 @@ +endpoint = new WC_REST_Orders_Controller(); + $this->user = $this->factory->user->create( + array( + 'role' => 'administrator', + ) + ); + } + + /** + * 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 = 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', + ); + + $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 ) ); + } +} diff --git a/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-producs-controller-tests.php b/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-producs-controller-tests.php deleted file mode 100644 index 41c35720bd3..00000000000 --- a/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-producs-controller-tests.php +++ /dev/null @@ -1,24 +0,0 @@ -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 ); - } -} diff --git a/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-products-controller-tests.php b/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-products-controller-tests.php new file mode 100644 index 00000000000..327c100c663 --- /dev/null +++ b/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-products-controller-tests.php @@ -0,0 +1,122 @@ +endpoint = new WC_REST_Products_Controller(); + $this->user = $this->factory->user->create( + array( + 'role' => 'administrator', + ) + ); + } + + /** + * 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 = 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', + ); + + $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 ); + } +}