2017-07-23 11:05:11 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Tests for the totals class.
|
2017-07-25 14:11:32 +00:00
|
|
|
*
|
2017-07-23 11:05:11 +00:00
|
|
|
* @package WooCommerce\Tests\Discounts
|
|
|
|
*/
|
2017-07-25 14:11:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* WC_Tests_Totals
|
|
|
|
*/
|
2017-07-23 11:05:11 +00:00
|
|
|
class WC_Tests_Totals extends WC_Unit_Test_Case {
|
|
|
|
|
2017-07-25 14:11:32 +00:00
|
|
|
/**
|
|
|
|
* Totals class for getter tests.
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
2017-07-23 11:05:11 +00:00
|
|
|
protected $totals;
|
|
|
|
|
2017-07-25 14:11:32 +00:00
|
|
|
/**
|
|
|
|
* ID tracking for cleanup.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $ids = array();
|
2017-07-23 11:05:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup the cart for totals calculation.
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
2017-07-25 21:44:54 +00:00
|
|
|
$this->ids = array();
|
|
|
|
|
2017-07-28 16:43:05 +00:00
|
|
|
if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) {
|
|
|
|
define( 'WOOCOMMERCE_CHECKOUT', 1 );
|
|
|
|
}
|
|
|
|
|
2017-07-23 11:05:11 +00:00
|
|
|
$tax_rate = array(
|
|
|
|
'tax_rate_country' => '',
|
|
|
|
'tax_rate_state' => '',
|
|
|
|
'tax_rate' => '20.0000',
|
|
|
|
'tax_rate_name' => 'VAT',
|
|
|
|
'tax_rate_priority' => '1',
|
|
|
|
'tax_rate_compound' => '0',
|
|
|
|
'tax_rate_shipping' => '1',
|
|
|
|
'tax_rate_order' => '1',
|
|
|
|
'tax_rate_class' => '',
|
|
|
|
);
|
|
|
|
$tax_rate_id = WC_Tax::_insert_tax_rate( $tax_rate );
|
2017-07-28 16:43:05 +00:00
|
|
|
|
2017-07-23 11:05:11 +00:00
|
|
|
update_option( 'woocommerce_calc_taxes', 'yes' );
|
2017-07-28 16:43:05 +00:00
|
|
|
update_option( 'woocommerce_default_customer_address', 'base' );
|
|
|
|
update_option( 'woocommerce_tax_based_on', 'base' );
|
2017-07-23 11:05:11 +00:00
|
|
|
|
|
|
|
$product = WC_Helper_Product::create_simple_product();
|
|
|
|
$product2 = WC_Helper_Product::create_simple_product();
|
|
|
|
|
2017-07-25 13:05:49 +00:00
|
|
|
WC_Helper_Shipping::create_simple_flat_rate();
|
|
|
|
WC()->session->set( 'chosen_shipping_methods', array( 'flat_rate' ) );
|
|
|
|
|
2017-07-23 11:05:11 +00:00
|
|
|
$coupon = new WC_Coupon;
|
|
|
|
$coupon->set_code( 'test-coupon-10' );
|
|
|
|
$coupon->set_amount( 10 );
|
|
|
|
$coupon->set_discount_type( 'percent' );
|
|
|
|
$coupon->save();
|
|
|
|
|
2017-07-25 14:11:32 +00:00
|
|
|
$this->ids['tax_rate_ids'][] = $tax_rate_id;
|
|
|
|
$this->ids['products'][] = $product;
|
|
|
|
$this->ids['products'][] = $product2;
|
|
|
|
$this->ids['coupons'][] = $coupon;
|
2017-07-23 11:05:11 +00:00
|
|
|
|
|
|
|
WC()->cart->add_to_cart( $product->get_id(), 1 );
|
|
|
|
WC()->cart->add_to_cart( $product2->get_id(), 2 );
|
2017-07-24 16:21:08 +00:00
|
|
|
WC()->cart->add_discount( $coupon->get_code() );
|
2017-07-23 11:05:11 +00:00
|
|
|
|
2017-07-25 13:05:49 +00:00
|
|
|
add_action( 'woocommerce_cart_calculate_fees', array( $this, 'add_cart_fees_callback' ) );
|
|
|
|
|
2017-07-28 16:43:05 +00:00
|
|
|
$this->totals = new WC_Cart_Totals( WC()->cart );
|
2017-07-23 11:05:11 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 13:05:49 +00:00
|
|
|
/**
|
|
|
|
* Add fees when the fees API is called.
|
|
|
|
*/
|
|
|
|
public function add_cart_fees_callback() {
|
2017-07-25 14:11:32 +00:00
|
|
|
WC()->cart->add_fee( 'test fee', 10, true );
|
|
|
|
WC()->cart->add_fee( 'test fee 2', 20, true );
|
|
|
|
WC()->cart->add_fee( 'test fee non-taxable', 10, false );
|
2017-07-25 13:05:49 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 11:05:11 +00:00
|
|
|
/**
|
|
|
|
* Clean up after test.
|
|
|
|
*/
|
|
|
|
public function tearDown() {
|
|
|
|
WC()->cart->empty_cart();
|
2017-07-25 13:05:49 +00:00
|
|
|
WC()->session->set( 'chosen_shipping_methods', array() );
|
|
|
|
WC_Helper_Shipping::delete_simple_flat_rate();
|
2017-07-23 11:05:11 +00:00
|
|
|
update_option( 'woocommerce_calc_taxes', 'no' );
|
2017-07-25 13:05:49 +00:00
|
|
|
remove_action( 'woocommerce_cart_calculate_fees', array( $this, 'add_cart_fees_callback' ) );
|
2017-07-23 11:05:11 +00:00
|
|
|
|
2017-07-25 14:11:32 +00:00
|
|
|
foreach ( $this->ids['products'] as $product ) {
|
2017-07-23 11:05:11 +00:00
|
|
|
$product->delete( true );
|
|
|
|
}
|
|
|
|
|
2017-07-25 14:11:32 +00:00
|
|
|
foreach ( $this->ids['coupons'] as $coupon ) {
|
2017-07-23 11:05:11 +00:00
|
|
|
$coupon->delete( true );
|
2017-07-25 21:44:54 +00:00
|
|
|
wp_cache_delete( WC_Cache_Helper::get_cache_prefix( 'coupons' ) . 'coupon_id_from_code_' . $coupon->get_code(), 'coupons' );
|
2017-07-23 11:05:11 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 14:11:32 +00:00
|
|
|
foreach ( $this->ids['tax_rate_ids'] as $tax_rate_id ) {
|
2017-07-23 11:05:11 +00:00
|
|
|
WC_Tax::_delete_tax_rate( $tax_rate_id );
|
|
|
|
}
|
2017-07-28 16:43:05 +00:00
|
|
|
|
|
|
|
$this->ids = array();
|
2017-07-23 11:05:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test get and set items.
|
|
|
|
*/
|
|
|
|
public function test_get_totals() {
|
2017-07-24 16:21:08 +00:00
|
|
|
$this->assertEquals( array(
|
|
|
|
'fees_total' => 40.00,
|
|
|
|
'fees_total_tax' => 6.00,
|
|
|
|
'items_subtotal' => 30.00,
|
|
|
|
'items_subtotal_tax' => 6.00,
|
|
|
|
'items_total' => 27.00,
|
|
|
|
'items_total_tax' => 5.40,
|
2017-07-25 14:11:32 +00:00
|
|
|
'total' => 90.40,
|
2017-07-25 13:05:49 +00:00
|
|
|
'taxes' => array(
|
2017-07-25 16:25:06 +00:00
|
|
|
$this->ids['tax_rate_ids'][0] => array(
|
2017-07-25 13:05:49 +00:00
|
|
|
'tax_total' => 11.40,
|
2017-07-25 14:11:32 +00:00
|
|
|
'shipping_tax_total' => 2.00,
|
|
|
|
),
|
2017-07-25 13:05:49 +00:00
|
|
|
),
|
2017-07-24 16:21:08 +00:00
|
|
|
'tax_total' => 11.40,
|
2017-07-25 14:11:32 +00:00
|
|
|
'shipping_total' => 10,
|
|
|
|
'shipping_tax_total' => 2,
|
2017-07-24 16:21:08 +00:00
|
|
|
'discounts_total' => 3.00,
|
2017-07-25 14:11:32 +00:00
|
|
|
'discounts_tax_total' => 0.60,
|
2017-07-24 16:21:08 +00:00
|
|
|
), $this->totals->get_totals() );
|
2017-07-23 11:05:11 +00:00
|
|
|
}
|
2017-07-25 21:44:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that cart totals get updated.
|
|
|
|
*/
|
|
|
|
public function test_cart_totals() {
|
|
|
|
$cart = WC()->cart;
|
|
|
|
|
|
|
|
$this->assertEquals( 40.00, $cart->fee_total );
|
|
|
|
$this->assertEquals( 27.00, $cart->cart_contents_total );
|
|
|
|
$this->assertEquals( 90.40, $cart->total );
|
2017-08-08 08:24:03 +00:00
|
|
|
$this->assertEquals( 36.00, $cart->subtotal );
|
|
|
|
$this->assertEquals( 30.00, $cart->subtotal_ex_tax );
|
2017-07-25 21:44:54 +00:00
|
|
|
$this->assertEquals( 11.40, $cart->tax_total );
|
|
|
|
$this->assertEquals( 3.00, $cart->discount_cart );
|
|
|
|
$this->assertEquals( 0.60, $cart->discount_cart_tax );
|
|
|
|
$this->assertEquals( 40.00, $cart->fee_total );
|
|
|
|
$this->assertEquals( 10, $cart->shipping_total );
|
|
|
|
$this->assertEquals( 2, $cart->shipping_tax_total );
|
|
|
|
}
|
2017-07-23 11:05:11 +00:00
|
|
|
}
|