Add unit test for 'wc_get_price_excluding_tax'.

The test verifies that when an order is passed as an argument,
the order customer is passed to 'WC_Tax::get_rates'.
This commit is contained in:
Nestor Soriano 2021-09-13 10:02:57 +02:00
parent 319b95b6f1
commit 98a2391747
No known key found for this signature in database
GPG Key ID: 08110F3518C12CAD
3 changed files with 100 additions and 1 deletions

View File

@ -9,6 +9,7 @@
*/
use Automattic\Jetpack\Constants;
use Automattic\WooCommerce\Proxies\LegacyProxy;
use Automattic\WooCommerce\Utilities\ArrayUtil;
use Automattic\WooCommerce\Utilities\NumberUtil;
@ -1083,7 +1084,7 @@ function wc_get_price_excluding_tax( $product, $args = array() ) {
if ( $product->is_taxable() && wc_prices_include_tax() ) {
$order = ArrayUtil::get_value_or_default( $args, 'order' );
$customer = $order ? new WC_Customer( $order->get_customer_id() ) : null;
$customer = $order ? wc_get_container()->get( LegacyProxy::class )->get_instance_of( WC_Customer::class, $order->get_customer_id() ) : null;
$tax_rates = WC_Tax::get_rates( $product->get_tax_class(), $customer );
$base_tax_rates = WC_Tax::get_base_tax_rates( $product->get_tax_class( 'unfiltered' ) );
$remove_taxes = apply_filters( 'woocommerce_adjust_non_base_location_prices', true ) ? WC_Tax::calc_tax( $line_price, $base_tax_rates, true ) : WC_Tax::calc_tax( $line_price, $tax_rates, true );

View File

@ -13,5 +13,6 @@ return array(
'get_woocommerce_currencies',
'get_woocommerce_currency_symbol',
'wc_get_shipping_method_count',
'wc_prices_include_tax',
'wc_site_is_https',
);

View File

@ -0,0 +1,97 @@
<?php
/**
* Unit tests for wc-product-functions.php.
*
* @package WooCommerce\Tests\Functions\Stock
*/
use Automattic\WooCommerce\Testing\Tools\CodeHacking\Hacks\FunctionsMockerHack;
use Automattic\WooCommerce\Testing\Tools\CodeHacking\Hacks\StaticMockerHack;
/**
* Class WC_Stock_Functions_Tests.
*/
class WC_Product_Functions_Tests extends \WC_Unit_Test_Case {
/**
* @testdox If 'wc_get_price_excluding_tax' gets an order as argument, it passes the order customer to 'WC_Tax::get_rates'.
*
* @testWith [true]
* [false]
*
* @param bool $pass_order Whether an order is passed to 'wc_get_price_excluding_tax' or not.
*/
public function test_wc_get_price_excluding_tax_passes_order_customer_to_get_rates_if_order_is_available( $pass_order ) {
$customer_passed_to_get_rates = false;
$customer_id_passed_to_wc_customer_constructor = false;
FunctionsMockerHack::add_function_mocks(
array(
'wc_prices_include_tax' => '__return_true',
)
);
StaticMockerHack::add_method_mocks(
array(
'WC_Tax' =>
array(
'get_rates' => function( $tax_class, $customer ) use ( &$customer_passed_to_get_rates ) {
$customer_passed_to_get_rates = $customer;
},
'get_base_tax_rates' => function( $tax_class ) {
return 0;
},
'calc_tax' => function( $price, $rates, $price_includes_tax = false, $deprecated = false ) {
return array( 0 );
},
),
)
);
// phpcs:disable Squiz.Commenting.FunctionComment.Missing
$product = new class() extends WC_Product {
public function get_price( $context = 'view' ) {
return 0;
}
public function is_taxable() {
return true;
}
public function get_tax_class( $context = 'view' ) {
return '';
}
};
$customer = new stdClass();
$this->register_legacy_proxy_class_mocks(
array(
'WC_Customer' => function( $customer_id ) use ( &$customer_id_passed_to_wc_customer_constructor, $customer ) {
$customer_id_passed_to_wc_customer_constructor = $customer_id;
return $customer;
},
)
);
if ( $pass_order ) {
$order = new class() {
public function get_customer_id() {
return 1;
}
};
wc_get_price_excluding_tax( $product, array( 'order' => $order ) );
$this->assertEquals( $order->get_customer_id(), $customer_id_passed_to_wc_customer_constructor );
$this->assertSame( $customer, $customer_passed_to_get_rates );
} else {
wc_get_price_excluding_tax( $product );
$this->assertFalse( $customer_id_passed_to_wc_customer_constructor );
$this->assertNull( $customer_passed_to_get_rates );
}
// phpcs:enable Squiz.Commenting.FunctionComment.Missing
}
}