Skip to content

Commit 3e1cb75

Browse files
committed
Add tests for non-numeric and zero-priced cart lines
1 parent fad66dc commit 3e1cb75

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

tests/php/test-class-wc-connect-taxjar-integration.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public function tear_down() {
8787
remove_all_filters( 'woocommerce_tax_line_item_location' );
8888
remove_all_filters( 'woocommerce_services_override_tax_rate' );
8989
remove_all_filters( 'woocommerce_product_is_taxable' );
90+
remove_all_filters( 'woocommerce_product_get_price' );
9091

9192
delete_option( 'woocommerce_calc_taxes' );
9293

@@ -445,6 +446,44 @@ public function test_get_line_items_structure() {
445446
$this->assertEquals( 2, $item['quantity'] );
446447
}
447448

449+
/**
450+
* A pricing filter can empty a price after the item is already in the cart.
451+
* wc_format_decimal() passes that through, and a non-numeric unit price is fatal
452+
* in the totals arithmetic, so the line is skipped.
453+
*/
454+
public function test_get_line_items_skips_line_with_non_numeric_price() {
455+
$this->product = WC_Helper_Product::create_simple_product();
456+
$this->product->save();
457+
458+
WC()->cart->add_to_cart( $this->product->get_id(), 2 );
459+
460+
add_filter( 'woocommerce_product_get_price', '__return_empty_string' );
461+
$line_items = $this->invoke_protected_method( 'get_line_items', array( WC()->cart ) );
462+
remove_filter( 'woocommerce_product_get_price', '__return_empty_string' );
463+
464+
$this->assertSame( array(), $line_items );
465+
}
466+
467+
/**
468+
* A zero price is legitimate and must keep reaching TaxJar, so the guard has to be
469+
* is_numeric() and not a truthiness check. See
470+
* test_zero_amount_response_persists_real_itemized_rates() for what the response to
471+
* a $0 line carries.
472+
*/
473+
public function test_get_line_items_keeps_line_priced_zero() {
474+
$this->product = WC_Helper_Product::create_simple_product();
475+
$this->product->set_regular_price( 0 );
476+
$this->product->set_price( 0 );
477+
$this->product->save();
478+
479+
WC()->cart->add_to_cart( $this->product->get_id(), 2 );
480+
481+
$line_items = $this->invoke_protected_method( 'get_line_items', array( WC()->cart ) );
482+
483+
$this->assertCount( 1, $line_items );
484+
$this->assertSame( '0', reset( $line_items )['unit_price'] );
485+
}
486+
448487
// -------------------------------------------------------------------------
449488
// group_items_by_location() tests
450489
// -------------------------------------------------------------------------

0 commit comments

Comments
 (0)