diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 191e0164701..41d00277b16 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -100,6 +100,7 @@ class WC_Unit_Tests_Bootstrap { require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-coupon.php' ); require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-fee.php' ); require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-shipping.php' ); + require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-customer.php' ); } /** diff --git a/tests/framework/helpers/class-wc-helper-customer.php b/tests/framework/helpers/class-wc-helper-customer.php new file mode 100644 index 00000000000..c7c698f2a71 --- /dev/null +++ b/tests/framework/helpers/class-wc-helper-customer.php @@ -0,0 +1,90 @@ +session->get( 'customer' ); + } + + /** + * Get the store's default shipping method. + * + * @return string + */ + + public static function get_default_shipping_method() { + get_option( 'woocommerce_default_shipping_method' ); + } + + + /** + * Get the user's chosen shipping method. + * + * @return array + */ + + public static function get_chosen_shipping_methods() { + return WC()->session->get( 'chosen_shipping_methods' ); + } + + /** + * Get the "Tax Based On" WooCommerce option. + * + * @return string base or billing + */ + + public static function get_tax_based_on() { + return get_option( 'woocommerce_tax_based_on' ); + } + + /** + * Set the the current customer's billing details in the session + * + * @param string $default_shipping_method Shipping Method slug + */ + + public static function set_customer_details( $customer_details ) { + WC()->session->set( 'customer', $customer_details ); + } + + /** + * Set the store's default shipping method. + * + * @param string $default_shipping_method Shipping Method slug + */ + + public static function set_default_shipping_method( $default_shipping_method ) { + update_option( 'woocommerce_default_shipping_method', $default_shipping_method ); + } + + /** + * Set the user's chosen shipping method. + * + * @param string $chosen_shipping_method Shipping Method slug + */ + + public static function set_chosen_shipping_methods( $chosen_shipping_methods ) { + WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods ); + } + + /** + * Set the "Tax Based On" WooCommerce option. + * + * @param string $default_shipping_method Shipping Method slug + */ + + public static function set_tax_based_on( $default_shipping_method ) { + update_option( 'woocommerce_tax_based_on', $default_shipping_method ); + } +} diff --git a/tests/unit-tests/customer.php b/tests/unit-tests/customer.php index 7df36f1e853..1befe3299a3 100644 --- a/tests/unit-tests/customer.php +++ b/tests/unit-tests/customer.php @@ -23,37 +23,69 @@ class WC_Tests_Customer extends WC_Unit_Test_Case { 'calculated_shipping' => false ); - //Set up the address assertions for the two taxable addresses - $base_store_address = array( "GB", "", "", "" ); $customer_address = array( "US", "PA", "19123", "Philadelphia" ); - //Initialize the session variables + //Initialize the session variables for the dummy customer. + + // Get the original settings for the session and the options + $original_default_shipping_method = WC_Helper_Customer::get_default_shipping_method(); + $original_chosen_shipping_methods = WC_Helper_Customer::get_chosen_shipping_methods(); + $original_tax_based_on = WC_Helper_Customer::get_tax_based_on(); + $original_customer_details = WC_Helper_Customer::get_customer_details(); - WC()->session->set( 'customer', $customer_data ); + WC_Helper_Customer::set_customer_details( $customer_data ); //Create a dummy customer to use for testing! $customer = new WC_Customer(); - // Create dummy product, and add it to the cart + // Create dummy product, and add the product to the cart. $product = WC_Helper_Product::create_simple_product(); + WC()->cart->add_to_cart( $product->id, 1 ); - // Customer is going with the Local Pickup option, and the store calculates tax based on the store base location. - $this->helper_setup_shipping_tax_env( 'local_pickup', 'billing' ); - $this->assertEquals( $customer->get_taxable_address() , $base_store_address); - - // Customer is going with the Local Pickup option, and the store calculates tax based on the customer's billing address. - $this->helper_setup_shipping_tax_env( 'local_pickup', 'billing' ); - $this->assertEquals( $customer->get_taxable_address() , $base_store_address); - - // Customer is going with the Free Shipping option, and the store calculates tax based on the customer's billing address. - $this->helper_setup_shipping_tax_env( 'free_shipping', 'billing' ); - $this->assertEquals( $customer->get_taxable_address() , $customer_address); + //Run through these tests twice - with two different selections for the store's default shipping options - // Customer is going with the Free Shipping option, and the store calculates tax based on the store base location. - $this->helper_setup_shipping_tax_env( 'free_shipping', 'base' ); - $this->assertEquals( $customer->get_taxable_address() , $base_store_address); + foreach( array('local_pickup') as $default_shipping_option ) { + + WC_Helper_Customer::get_default_shipping_method( $default_shipping_option ); + + // Customer is going with the Local Pickup option, and the store calculates tax based on the store location. + + WC_Helper_Customer::set_chosen_shipping_methods( array( 'local_pickup' ) ); + WC_Helper_Customer::set_tax_based_on( 'base' ); + + $this->assertEquals( $customer->get_taxable_address(), $base_store_address ); + + // Customer is going with the Local Pickup option, and the store calculates tax based on the customer's billing address. + + WC_Helper_Customer::set_chosen_shipping_methods( array( 'local_pickup' ) ); + WC_Helper_Customer::set_tax_based_on( 'billing' ); + + $this->assertEquals( $customer->get_taxable_address(), $base_store_address ); + + // Customer is going with the Free Shipping option, and the store calculates tax based on the customer's billing address. + + WC_Helper_Customer::set_chosen_shipping_methods( array( 'free_shipping' ) ); + WC_Helper_Customer::set_tax_based_on( 'billing' ); + + $this->assertEquals( $customer->get_taxable_address(), $customer_address ); + + // Customer is going with the Free Shipping option, and the store calculates tax based on the store base location. + + WC_Helper_Customer::set_chosen_shipping_methods( array( 'free_shipping' ) ); + WC_Helper_Customer::set_tax_based_on( 'base' ); + + $this->assertEquals( $customer->get_taxable_address(), $base_store_address ); + + } + + //Reset the session and the options. + + WC_Helper_Customer::set_default_shipping_method( $original_default_shipping_method ); + WC_Helper_Customer::set_chosen_shipping_methods( $original_chosen_shipping_methods ); + WC_Helper_Customer::set_tax_based_on( $original_tax_based_on ); + WC_Helper_Customer::set_customer_details( $original_customer_details ); } /** * Test the is_customer_outside_base method @@ -79,47 +111,63 @@ class WC_Tests_Customer extends WC_Unit_Test_Case { //Initialize the session variables for the dummy customer. - WC()->session->set( 'customer', $customer_data ); + // Get the original settings for the session and the options + $original_default_shipping_method = WC_Helper_Customer::get_default_shipping_method(); + $original_chosen_shipping_methods = WC_Helper_Customer::get_chosen_shipping_methods(); + $original_tax_based_on = WC_Helper_Customer::get_tax_based_on(); + $original_customer_details = WC_Helper_Customer::get_customer_details(); + + WC_Helper_Customer::set_customer_details( $customer_data ); //Create a dummy customer to use for testing! $customer = new WC_Customer(); // Create dummy product, and add the product to the cart. $product = WC_Helper_Product::create_simple_product(); + WC()->cart->add_to_cart( $product->id, 1 ); - // Customer is going with the Local Pickup option, and the store calculates tax based on the store base location. - $this->helper_setup_shipping_tax_env( 'local_pickup', 'billing' ); - $this->assertEquals( $customer->is_customer_outside_base(), false ); - - // Customer is going with the Local Pickup option, and the store calculates tax based on the customer's billing address. - $this->helper_setup_shipping_tax_env( 'local_pickup', 'billing' ); - $this->assertEquals( $customer->is_customer_outside_base(), false ); - - // Customer is going with the Free Shipping option, and the store calculates tax based on the customer's billing address. - $this->helper_setup_shipping_tax_env( 'free_shipping', 'billing' ); - $this->assertEquals( $customer->is_customer_outside_base(), true ); - - // Customer is going with the Free Shipping option, and the store calculates tax based on the store base location. - $this->helper_setup_shipping_tax_env( 'free_shipping', 'base' ); - $this->assertEquals( $customer->is_customer_outside_base(), false ); - } + //Run through these tests twice - with two different selections for the store's default shipping options - /** - * Helper function for creating the customer and setting up the tax enviroment based on desired params. - * - * @param string $shipping_method Shipping Method slug - * @param string $tax_based_on either 'base' or 'billing.' base refers to tax computed based on the shop location, 'billing' computes tax based on the customer's billing address. - * @return void - */ + foreach( array('local_pickup', 'free_shipping') as $default_shipping_option ) { - private function helper_setup_shipping_tax_env($shipping_method, $tax_based_on) { + WC_Helper_Customer::get_default_shipping_method( $default_shipping_option ); - //Shipping Methods - update_option( 'woocommerce_default_shipping_method', $shipping_method ); - WC()->session->set( 'chosen_shipping_methods', array( $shipping_method ) ); + // Customer is going with the Local Pickup option, and the store calculates tax based on the store location. - //Tax "Based-on" Settings - update_option( 'woocommerce_tax_based_on', $tax_based_on ); + WC_Helper_Customer::set_chosen_shipping_methods( array( 'local_pickup' ) ); + WC_Helper_Customer::set_tax_based_on( 'base' ); + + $this->assertEquals( $customer->is_customer_outside_base(), false ); + + // Customer is going with the Local Pickup option, and the store calculates tax based on the customer's billing address. + + WC_Helper_Customer::set_chosen_shipping_methods( array( 'local_pickup' ) ); + WC_Helper_Customer::set_tax_based_on( 'billing' ); + + $this->assertEquals( $customer->is_customer_outside_base(), false ); + + // Customer is going with the Free Shipping option, and the store calculates tax based on the customer's billing address. + + WC_Helper_Customer::set_chosen_shipping_methods( array( 'free_shipping' ) ); + WC_Helper_Customer::set_tax_based_on( 'billing' ); + + $this->assertEquals( $customer->is_customer_outside_base(), true ); + + // Customer is going with the Free Shipping option, and the store calculates tax based on the store base location. + + WC_Helper_Customer::set_chosen_shipping_methods( array( 'free_shipping' ) ); + WC_Helper_Customer::set_tax_based_on( 'base' ); + + $this->assertEquals( $customer->is_customer_outside_base(), false ); + + } + + //Reset the session and the options. + + WC_Helper_Customer::set_default_shipping_method( $original_default_shipping_method ); + WC_Helper_Customer::set_chosen_shipping_methods( $original_chosen_shipping_methods ); + WC_Helper_Customer::set_tax_based_on( $original_tax_based_on ); + WC_Helper_Customer::set_customer_details( $original_customer_details ); } } \ No newline at end of file