From 8faf3ec85ade6fa630a7a85f321ac33eec0b3aca Mon Sep 17 00:00:00 2001 From: claudiosmweb Date: Thu, 30 Oct 2014 16:22:14 -0200 Subject: [PATCH] Unit Tests: Created test_fixed_product_discount and test_percent_product_discount methods --- .../framework/helpers/class-wc-helper-fee.php | 1 + tests/unit-tests/coupon.php | 100 ++++++++++++++++++ 2 files changed, 101 insertions(+) diff --git a/tests/framework/helpers/class-wc-helper-fee.php b/tests/framework/helpers/class-wc-helper-fee.php index 71ee69a90f9..2a111bd3569 100644 --- a/tests/framework/helpers/class-wc-helper-fee.php +++ b/tests/framework/helpers/class-wc-helper-fee.php @@ -22,6 +22,7 @@ class WC_Helper_Fee { /** * Add a cart simple fee without taxes + * Note: need to be added before add any product in the cart * * @since 2.3 */ diff --git a/tests/unit-tests/coupon.php b/tests/unit-tests/coupon.php index 17da62204cb..a94e6e8ad1c 100644 --- a/tests/unit-tests/coupon.php +++ b/tests/unit-tests/coupon.php @@ -141,4 +141,104 @@ class WC_Tests_Coupon extends WC_Unit_Test_Case { // Delete product WC_Helper_Product::delete_product( $product->id ); } + + /** + * Test fixed product discount method + * + * @since 2.3 + */ + public function test_fixed_product_discount() { + + // Create product + $product = WC_Helper_Product::create_simple_product(); + update_post_meta( $product->id, '_price', '10' ); + update_post_meta( $product->id, '_regular_price', '10' ); + + // Create coupon + $coupon = WC_Helper_Coupon::create_coupon(); + update_post_meta( $coupon->id, 'discount_type', 'fixed_product' ); + update_post_meta( $coupon->id, 'coupon_amount', '5' ); + + // We need this to have the calculate_totals() method calculate totals + if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) { + define( 'WOOCOMMERCE_CHECKOUT', true ); + } + + // Add fee + WC_Helper_Fee::add_cart_fee(); + + // Add product to cart + WC()->cart->add_to_cart( $product->id, 1 ); + + // Add coupon + WC()->cart->add_discount( $coupon->code ); + + // Test if the cart total amount is equal 19.5 + $this->assertEquals( 15, WC()->cart->total ); + + // Clearing WC notices + wc_clear_notices(); + + // Clean up the cart + WC()->cart->empty_cart(); + + // Remove fee + WC_Helper_Fee::remove_cart_fee(); + + // Delete coupon + WC_Helper_Coupon::delete_coupon( $coupon->id ); + + // Delete product + WC_Helper_Product::delete_product( $product->id ); + } + + /** + * Test percent product discount method + * + * @since 2.3 + */ + public function test_percent_product_discount() { + + // Create product + $product = WC_Helper_Product::create_simple_product(); + update_post_meta( $product->id, '_price', '10' ); + update_post_meta( $product->id, '_regular_price', '10' ); + + // Create coupon + $coupon = WC_Helper_Coupon::create_coupon(); + update_post_meta( $coupon->id, 'discount_type', 'percent_product' ); + update_post_meta( $coupon->id, 'coupon_amount', '5' ); + + // We need this to have the calculate_totals() method calculate totals + if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) { + define( 'WOOCOMMERCE_CHECKOUT', true ); + } + + // Add fee + WC_Helper_Fee::add_cart_fee(); + + // Add product to cart + WC()->cart->add_to_cart( $product->id, 1 ); + + // Add coupon + WC()->cart->add_discount( $coupon->code ); + + // Test if the cart total amount is equal 19.5 + $this->assertEquals( 19.5, WC()->cart->total ); + + // Clearing WC notices + wc_clear_notices(); + + // Clean up the cart + WC()->cart->empty_cart(); + + // Remove fee + WC_Helper_Fee::remove_cart_fee(); + + // Delete coupon + WC_Helper_Coupon::delete_coupon( $coupon->id ); + + // Delete product + WC_Helper_Product::delete_product( $product->id ); + } }