Fixed the unit tests broken by the requirement that a valid address must be set to calculate shipping rates

This commit is contained in:
Christopher Allford 2019-12-06 08:48:13 -08:00 committed by vedanshujain
parent a7daebc6fd
commit ac22406763
3 changed files with 130 additions and 44 deletions

View File

@ -172,6 +172,11 @@ class WC_Tests_Cart extends WC_Unit_Test_Case {
update_option( 'woocommerce_calc_taxes', 'yes' );
update_option( 'woocommerce_tax_round_at_subtotal', 'yes' );
// Set an address so that shipping can be calculated.
add_filter( 'woocommerce_customer_get_shipping_country', array( $this, 'force_customer_us_country' ) );
add_filter( 'woocommerce_customer_get_shipping_state', array( $this, 'force_customer_us_state' ) );
add_filter( 'woocommerce_customer_get_shipping_postcode', array( $this, 'force_customer_us_postcode' ) );
$tax_rate = array(
'tax_rate_country' => '',
'tax_rate_state' => '',
@ -567,7 +572,8 @@ class WC_Tests_Cart extends WC_Unit_Test_Case {
$full_coupon->set_amount( 100 );
$full_coupon->save();
add_filter( 'woocommerce_customer_get_shipping_country', array( $this, 'force_customer_gb_shipping' ) );
add_filter( 'woocommerce_customer_get_shipping_country', array( $this, 'force_customer_gb_country' ) );
add_filter( 'woocommerce_customer_get_shipping_postcode', array( $this, 'force_customer_gb_postcode' ) );
WC()->cart->add_to_cart( $product->get_id(), 1 );
// Test in store location with no coupon.
@ -604,8 +610,11 @@ class WC_Tests_Cart extends WC_Unit_Test_Case {
WC()->cart->remove_coupons();
WC()->cart->empty_cart();
remove_filter( 'woocommerce_customer_get_shipping_country', array( $this, 'force_customer_gb_shipping' ) );
add_filter( 'woocommerce_customer_get_shipping_country', array( $this, 'force_customer_us_shipping' ) );
remove_filter( 'woocommerce_customer_get_shipping_country', array( $this, 'force_customer_gb_country' ) );
remove_filter( 'woocommerce_customer_get_shipping_postcode', array( $this, 'force_customer_gb_postcode' ) );
add_filter( 'woocommerce_customer_get_shipping_country', array( $this, 'force_customer_us_country' ) );
add_filter( 'woocommerce_customer_get_shipping_state', array( $this, 'force_customer_us_state' ) );
add_filter( 'woocommerce_customer_get_shipping_postcode', array( $this, 'force_customer_us_postcode' ) );
WC()->cart->add_to_cart( $product->get_id(), 1 );
// Test out of store location with no coupon.
@ -710,7 +719,8 @@ class WC_Tests_Cart extends WC_Unit_Test_Case {
$full_coupon->set_amount( 100 );
$full_coupon->save();
add_filter( 'woocommerce_customer_get_shipping_country', array( $this, 'force_customer_gb_shipping' ) );
add_filter( 'woocommerce_customer_get_shipping_country', array( $this, 'force_customer_gb_country' ) );
add_filter( 'woocommerce_customer_get_shipping_postcode', array( $this, 'force_customer_gb_postcode' ) );
WC()->cart->add_to_cart( $product->get_id(), 1 );
// Test in store location with no coupon.
@ -747,8 +757,11 @@ class WC_Tests_Cart extends WC_Unit_Test_Case {
WC()->cart->remove_coupons();
WC()->cart->empty_cart();
remove_filter( 'woocommerce_customer_get_shipping_country', array( $this, 'force_customer_gb_shipping' ) );
add_filter( 'woocommerce_customer_get_shipping_country', array( $this, 'force_customer_us_shipping' ) );
remove_filter( 'woocommerce_customer_get_shipping_country', array( $this, 'force_customer_gb_country' ) );
remove_filter( 'woocommerce_customer_get_shipping_postcode', array( $this, 'force_customer_gb_postcode' ) );
add_filter( 'woocommerce_customer_get_shipping_country', array( $this, 'force_customer_us_country' ) );
add_filter( 'woocommerce_customer_get_shipping_state', array( $this, 'force_customer_us_state' ) );
add_filter( 'woocommerce_customer_get_shipping_postcode', array( $this, 'force_customer_us_postcode' ) );
WC()->cart->add_to_cart( $product->get_id(), 1 );
// Test out of store location with no coupon.
@ -851,7 +864,9 @@ class WC_Tests_Cart extends WC_Unit_Test_Case {
$full_coupon->set_amount( 100 );
$full_coupon->save();
add_filter( 'woocommerce_customer_get_shipping_country', array( $this, 'force_customer_us_shipping' ) );
add_filter( 'woocommerce_customer_get_shipping_country', array( $this, 'force_customer_us_country' ) );
add_filter( 'woocommerce_customer_get_shipping_state', array( $this, 'force_customer_us_state' ) );
add_filter( 'woocommerce_customer_get_shipping_postcode', array( $this, 'force_customer_us_postcode' ) );
WC()->cart->add_to_cart( $product->get_id(), 1 );
// Test out of store location with no coupon.
@ -895,10 +910,21 @@ class WC_Tests_Cart extends WC_Unit_Test_Case {
* @param string $country Country code.
* @return string
*/
public function force_customer_gb_shipping( $country ) {
public function force_customer_gb_country( $country ) {
return 'GB';
}
/**
* Helper that can be hooked to a filter to force the customer's shipping postal code to be ANN NAA.
*
* @since 3.9.0
* @param string $postcode Postal code..
* @return string
*/
public function force_customer_gb_postcode( $postcode ) {
return 'ANN NAA';
}
/**
* Helper that can be hooked to a filter to force the customer's shipping country to be US.
*
@ -906,10 +932,32 @@ class WC_Tests_Cart extends WC_Unit_Test_Case {
* @param string $country Country code.
* @return string
*/
public function force_customer_us_shipping( $country ) {
public function force_customer_us_country( $country ) {
return 'US';
}
/**
* Helper that can be hooked to a filter to force the customer's shipping state to be NY.
*
* @since 3.9.0
* @param string $state State code.
* @return string
*/
public function force_customer_us_state( $state ) {
return 'NY';
}
/**
* Helper that can be hooked to a filter to force the customer's shipping postal code to be 12345.
*
* @since 3.9.0
* @param string $postcode Postal code.
* @return string
*/
public function force_customer_us_postcode( $postcode ) {
return '12345';
}
/**
* Test a rounding issue on prices that are entered inclusive tax and shipping is used.
* See: #17970.
@ -932,6 +980,11 @@ class WC_Tests_Cart extends WC_Unit_Test_Case {
update_option( 'woocommerce_prices_include_tax', 'yes' );
update_option( 'woocommerce_calc_taxes', 'yes' );
// Set an address so that shipping can be calculated.
add_filter( 'woocommerce_customer_get_shipping_country', array( $this, 'force_customer_us_country' ) );
add_filter( 'woocommerce_customer_get_shipping_state', array( $this, 'force_customer_us_state' ) );
add_filter( 'woocommerce_customer_get_shipping_postcode', array( $this, 'force_customer_us_postcode' ) );
// 19% tax.
$tax_rate = array(
'tax_rate_country' => '',
@ -1487,6 +1540,11 @@ class WC_Tests_Cart extends WC_Unit_Test_Case {
// Add product to cart.
WC()->cart->add_to_cart( $product->get_id(), 1 );
// Set an address so that shipping can be calculated.
add_filter( 'woocommerce_customer_get_shipping_country', array( $this, 'force_customer_us_country' ) );
add_filter( 'woocommerce_customer_get_shipping_state', array( $this, 'force_customer_us_state' ) );
add_filter( 'woocommerce_customer_get_shipping_postcode', array( $this, 'force_customer_us_postcode' ) );
// Set the flat_rate shipping method.
WC()->session->set( 'chosen_shipping_methods', array( 'flat_rate' ) );
WC()->cart->calculate_totals();

View File

@ -1,4 +1,9 @@
<?php
/**
* Coupon tests.
*
* @package WooCommerce\Tests\Coupon
*/
/**
* Class Coupon.
@ -7,6 +12,21 @@
*/
class WC_Tests_Coupon extends WC_Unit_Test_Case {
/**
* Sets up the test class.
*/
public function setUp() {
parent::setUp();
// Set a valid address for the customer so shipping rates will calculate.
WC()->customer->set_shipping_country( 'US' );
WC()->customer->set_shipping_state( 'NY' );
WC()->customer->set_shipping_postcode( '12345' );
}
/**
* Cleans up after the test class.
*/
public function tearDown() {
WC()->cart->empty_cart();
WC()->cart->remove_coupons();
@ -53,7 +73,7 @@ class WC_Tests_Coupon extends WC_Unit_Test_Case {
$test_coupon = new WC_Coupon( (string) $coupon_2->get_id() );
$this->assertEquals( $coupon_2->get_id(), $test_coupon->get_id() );
// Test getting a coupon by coupon object
// Test getting a coupon by coupon object.
$test_coupon = new WC_Coupon( $coupon_1 );
$this->assertEquals( $test_coupon->get_id(), $coupon_1->get_id() );
}
@ -65,13 +85,13 @@ class WC_Tests_Coupon extends WC_Unit_Test_Case {
*/
public function test_add_discount() {
// Create coupon
// Create coupon.
$coupon = WC_Helper_Coupon::create_coupon();
// Add coupon, test return statement
// Add coupon, test return statement.
$this->assertTrue( WC()->cart->add_discount( $coupon->get_code() ) );
// Test if total amount of coupons is 1
// Test if total amount of coupons is 1.
$this->assertEquals( 1, count( WC()->cart->get_applied_coupons() ) );
}
@ -82,16 +102,16 @@ class WC_Tests_Coupon extends WC_Unit_Test_Case {
*/
public function test_add_discount_duplicate() {
// Create coupon
// Create coupon.
$coupon = WC_Helper_Coupon::create_coupon();
// Add coupon
// Add coupon.
$this->assertTrue( WC()->cart->add_discount( $coupon->get_code() ) );
// Add coupon again, test return statement
// Add coupon again, test return statement.
$this->assertFalse( WC()->cart->add_discount( $coupon->get_code() ) );
// Test if total amount of coupons is 1
// Test if total amount of coupons is 1.
$this->assertEquals( 1, count( WC()->cart->get_applied_coupons() ) );
}
@ -102,30 +122,30 @@ class WC_Tests_Coupon extends WC_Unit_Test_Case {
*/
public function test_fixed_cart_discount() {
// Create product
// Create product.
$product = WC_Helper_Product::create_simple_product();
$product->set_regular_price( 10 );
$product->save();
// Create coupon
// Create coupon.
$coupon = WC_Helper_Coupon::create_coupon();
update_post_meta( $coupon->get_id(), 'discount_type', 'fixed_cart' );
update_post_meta( $coupon->get_id(), 'coupon_amount', '5' );
// Create a flat rate method
// Create a flat rate method.
WC_Helper_Shipping::create_simple_flat_rate();
// Add product to cart
// Add product to cart.
WC()->cart->add_to_cart( $product->get_id(), 1 );
// Add coupon
// Add coupon.
WC()->cart->add_discount( $coupon->get_code() );
// Set the flat_rate shipping method
// Set the flat_rate shipping method.
WC()->session->set( 'chosen_shipping_methods', array( 'flat_rate' ) );
WC()->cart->calculate_totals();
// Test if the cart total amount is equal 15
// Test if the cart total amount is equal 15.
$this->assertEquals( 15, WC()->cart->total );
}
@ -136,33 +156,33 @@ class WC_Tests_Coupon extends WC_Unit_Test_Case {
*/
public function test_fixed_product_discount() {
// Create product
// Create product.
$product = WC_Helper_Product::create_simple_product();
$product->set_regular_price( 10 );
$product->save();
// Create coupon
// Create coupon.
$coupon = WC_Helper_Coupon::create_coupon();
update_post_meta( $coupon->get_id(), 'discount_type', 'fixed_product' );
update_post_meta( $coupon->get_id(), 'coupon_amount', '5' );
// Create a flat rate method - $10
// Create a flat rate method - $10.
WC_Helper_Shipping::create_simple_flat_rate();
// Add fee - $10
// Add fee - $10.
WC_Helper_Fee::add_cart_fee();
// Add product to cart
// Add product to cart.
WC()->cart->add_to_cart( $product->get_id(), 1 );
// Add coupon
// Add coupon.
WC()->cart->add_discount( $coupon->get_code() );
// Set the flat_rate shipping method
// Set the flat_rate shipping method.
WC()->session->set( 'chosen_shipping_methods', array( 'flat_rate' ) );
WC()->cart->calculate_totals();
// Test if the cart total amount is equal 25
// Test if the cart total amount is equal 25.
$this->assertEquals( 25, WC()->cart->total );
}
@ -173,33 +193,33 @@ class WC_Tests_Coupon extends WC_Unit_Test_Case {
*/
public function test_percent_discount() {
// Create product
// Create product.
$product = WC_Helper_Product::create_simple_product();
$product->set_regular_price( 10 );
$product->save();
// Create coupon
// Create coupon.
$coupon = WC_Helper_Coupon::create_coupon();
update_post_meta( $coupon->get_id(), 'discount_type', 'percent' );
update_post_meta( $coupon->get_id(), 'coupon_amount', '5' );
// Create a flat rate method
// Create a flat rate method.
WC_Helper_Shipping::create_simple_flat_rate();
// Add fee
// Add fee.
WC_Helper_Fee::add_cart_fee();
// Add product to cart
// Add product to cart.
WC()->cart->add_to_cart( $product->get_id(), 1 );
// Add coupon
// Add coupon.
WC()->cart->add_discount( $coupon->get_code() );
// Set the flat_rate shipping method
// Set the flat_rate shipping method.
WC()->session->set( 'chosen_shipping_methods', array( 'flat_rate' ) );
WC()->cart->calculate_totals();
// Test if the cart total amount is equal 29.5
// Test if the cart total amount is equal 29.5.
$this->assertEquals( 29.5, WC()->cart->total );
}
@ -229,12 +249,12 @@ class WC_Tests_Coupon extends WC_Unit_Test_Case {
* Test an item limit for percent discounts.
*/
public function test_percent_discount_item_limit() {
// Create product
// Create product.
$product = WC_Helper_Product::create_simple_product();
update_post_meta( $product->get_id(), '_price', '10' );
update_post_meta( $product->get_id(), '_regular_price', '10' );
// Create coupon
// Create coupon.
$coupon = WC_Helper_Coupon::create_coupon(
'dummycoupon',
array(
@ -258,16 +278,19 @@ class WC_Tests_Coupon extends WC_Unit_Test_Case {
$this->assertEquals( 19.5, WC()->cart->total );
}
/**
* Test the coupon's item limit.
*/
public function test_custom_discount_item_limit() {
// Register custom discount type.
WC_Helper_Coupon::register_custom_type( __FUNCTION__ );
// Create product
// Create product.
$product = WC_Helper_Product::create_simple_product();
update_post_meta( $product->get_id(), '_price', '10' );
update_post_meta( $product->get_id(), '_regular_price', '10' );
// Create coupon
// Create coupon.
$coupon = WC_Helper_Coupon::create_coupon(
'dummycoupon',
array(

View File

@ -30,6 +30,11 @@ class WC_Tests_Totals extends WC_Unit_Test_Case {
public function setUp() {
parent::setUp();
// Set a valid address for the customer so shipping rates will calculate.
WC()->customer->set_shipping_country( 'US' );
WC()->customer->set_shipping_state( 'NY' );
WC()->customer->set_shipping_postcode( '12345' );
$this->ids = array();
$tax_rate = array(