From c834dfb98bbe0f280ff9de79f69041c671b37537 Mon Sep 17 00:00:00 2001 From: vedanshujain Date: Tue, 23 Feb 2021 16:53:16 +0530 Subject: [PATCH] Add test for when entered price has more precision than displayed price. --- .../includes/class-wc-cart-totals-test.php | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/php/includes/class-wc-cart-totals-test.php b/tests/php/includes/class-wc-cart-totals-test.php index 13c7d091e87..2b8e2e816da 100644 --- a/tests/php/includes/class-wc-cart-totals-test.php +++ b/tests/php/includes/class-wc-cart-totals-test.php @@ -71,10 +71,49 @@ class WC_Cart_Totals_Tests extends WC_Unit_Test_Case { update_option( 'woocommerce_price_num_decimals', 0 ); 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( '425', wc_format_decimal( WC()->cart->get_discount_tax(), 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 ) ); + } }