From 5f881728e52950854f99fbbf55107cf3fd9dbf89 Mon Sep 17 00:00:00 2001 From: anitaamurthy Date: Wed, 5 Jan 2022 18:38:48 +0530 Subject: [PATCH] Added new unit test class for coupons v3 controller --- ...class-wc-rest-coupons-controller-tests.php | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-coupons-controller-tests.php diff --git a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-coupons-controller-tests.php b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-coupons-controller-tests.php new file mode 100644 index 00000000000..21f45dd7457 --- /dev/null +++ b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-coupons-controller-tests.php @@ -0,0 +1,96 @@ +endpoint = new WC_REST_Coupons_Controller(); + $this->user = $this->factory->user->create( + array( + 'role' => 'administrator', + ) + ); + } + + /** + * Get all expected fields. + */ + public function get_expected_response_fields() { + return array( + 'id', + 'code', + 'amount', + 'status', + 'date_created', + 'date_created_gmt', + 'date_modified', + 'date_modified_gmt', + 'discount_type', + 'description', + 'date_expires', + 'date_expires_gmt', + 'usage_count', + 'individual_use', + 'product_ids', + 'excluded_product_ids', + 'usage_limit', + 'usage_limit_per_user', + 'limit_usage_to_x_items', + 'free_shipping', + 'product_categories', + 'excluded_product_categories', + 'exclude_sale_items', + 'minimum_amount', + 'maximum_amount', + 'email_restrictions', + 'used_by', + '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_coupon_api_get_all_fields() { + wp_set_current_user( $this->user ); + $expected_response_fields = $this->get_expected_response_fields(); + + $coupon = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CouponHelper::create_coupon(); + $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/coupons/' . $coupon->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_coupons_get_each_field_one_by_one() { + wp_set_current_user( $this->user ); + $expected_response_fields = $this->get_expected_response_fields(); + $coupon = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CouponHelper::create_coupon(); + + foreach ( $expected_response_fields as $field ) { + $request = new WP_REST_Request( 'GET', '/wc/v3/coupons/' . $coupon->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 coupon API response." ); + } + } +}