Add test for when entered price has more precision than displayed price.

This commit is contained in:
vedanshujain 2021-02-23 16:53:16 +05:30
parent 2930057847
commit c834dfb98b
1 changed files with 39 additions and 0 deletions

View File

@ -71,10 +71,49 @@ class WC_Cart_Totals_Tests extends WC_Unit_Test_Case {
update_option( 'woocommerce_price_num_decimals', 0 ); update_option( 'woocommerce_price_num_decimals', 0 );
WC()->cart->calculate_totals(); WC()->cart->calculate_totals();
update_option( 'woocommerce_price_num_decimals', $decimal_precision );
$this->assertEquals( '1575', wc_format_decimal( WC()->cart->get_discount_total(), 0 ) ); $this->assertEquals( '1575', wc_format_decimal( WC()->cart->get_discount_total(), 0 ) );
$this->assertEquals( '425', wc_format_decimal( WC()->cart->get_discount_tax(), 0 ) ); $this->assertEquals( '425', wc_format_decimal( WC()->cart->get_discount_tax(), 0 ) );
$this->assertEquals( '11070', wc_format_decimal( WC()->cart->get_total(), 0 ) ); $this->assertEquals( '11070', wc_format_decimal( WC()->cart->get_total(), 0 ) );
} }
/**
* Tests whether subtotal is properly rounded, when prices entered have higher precision than displayed.
*
* @link https://github.com/woocommerce/woocommerce/issues/28292.
*/
public function test_subtotal_rounding_with_changing_precision() {
update_option( 'woocommerce_prices_include_tax', 'yes' );
update_option( 'woocommerce_calc_taxes', 'yes' );
update_option( 'woocommerce_tax_round_at_subtotal', 'yes' );
$decimal_precision = wc_get_price_decimals();
update_option( 'woocommerce_price_num_decimals', 0 );
WC()->cart->empty_cart();
$tax_rate = array(
'tax_rate_country' => '',
'tax_rate_state' => '',
'tax_rate' => '23.0000',
'tax_rate_name' => 'TAX23',
'tax_rate_priority' => '1',
'tax_rate_compound' => '0',
'tax_rate_shipping' => '0',
'tax_rate_order' => '1',
);
WC_Tax::_insert_tax_rate( $tax_rate );
$product_301_90909 = WC_Helper_Product::create_simple_product( true, array( 'regular_price' => 301.90909 ) );
WC()->cart->add_to_cart( $product_301_90909->get_id() );
WC()->cart->calculate_totals();
update_option( 'woocommerce_price_num_decimals', $decimal_precision );
// Notice how subtotal + tax does not equate to total here.
// This is feature of round at subtotal property, where since we are not rounding, displayed components of price may not add up to displayed total price.
$this->assertEquals( '245', wc_format_decimal( WC()->cart->get_subtotal(), 0 ) );
$this->assertEquals( '302', wc_format_decimal( WC()->cart->get_total(), 0 ) );
$this->assertEquals( '56', wc_format_decimal( WC()->cart->get_total_tax(), 0 ) );
}
} }