Skip to content

Commit

Permalink
Product Collection: Added priceRange PHPUnit Tests (woocommerce#45363)
Browse files Browse the repository at this point in the history
  • Loading branch information
ObliviousHarmony authored Mar 11, 2024
1 parent 0b6259e commit 1e7b8c4
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: dev
Comment: Just adding some PHPUnit tests.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Automattic\WooCommerce\Tests\Blocks\BlockTypes;

use Automattic\WooCommerce\Tests\Blocks\Mocks\ProductCollectionMock;
use WC_Helper_Product;
use WC_Tax;
use WP_Query;

/**
* Tests for the ProductCollection block type
Expand Down Expand Up @@ -87,6 +90,7 @@ private function build_request( $params = array() ) {
'woocommerceOnSale' => false,
'woocommerceAttributes' => array(),
'woocommerceStockStatus' => array(),
'priceRange' => array(),
)
);

Expand Down Expand Up @@ -624,6 +628,10 @@ public function test_updating_rest_query_with_attributes() {
),
),
'woocommerceStockStatus' => array( 'instock', 'outofstock' ),
'priceRange' => array(
'min' => 1,
'max' => 100,
),
);
$request = $this->build_request( $params );

Expand Down Expand Up @@ -666,5 +674,203 @@ public function test_updating_rest_query_with_attributes() {
),
$updated_query['tax_query'],
);

$this->assertEquals(
array(
'min' => 1,
'max' => 100,
),
$updated_query['priceRange'],
);
}

/**
* Test that price range queries are set so they can be picked up in the `posts_clauses` filter.
*/
public function test_price_range_queries() {
$parsed_block = $this->get_base_parsed_block();
$parsed_block['attrs']['query']['priceRange'] = array(
'min' => 1,
'max' => 100,
);

$merged_query = $this->initialize_merged_query( $parsed_block );

$this->assertEquals(
array(
'min' => 1,
'max' => 100,
),
$merged_query['priceRange'],
);
}

/**
* Tests that empty price range clauses are not added to the query.
*/
public function test_price_range_clauses_empty() {
$parsed_block = $this->get_base_parsed_block();
$parsed_block['attrs']['query']['priceRange'] = array(
'min' => 0,
'max' => 0,
);

$merged_query = $this->initialize_merged_query( $parsed_block );

$this->assertEquals(
array(
'min' => 0,
'max' => 0,
),
$merged_query['priceRange'],
);

$query = new WP_Query( $merged_query );

$this->assertStringNotContainsString( 'wc_product_meta_lookup.min_price', $query->request );
$this->assertStringNotContainsString( 'wc_product_meta_lookup.max_price', $query->request );
}

/**
* Tests that the minimum in a price range is added if set.
*/
public function test_price_range_clauses_min_price() {
$parsed_block = $this->get_base_parsed_block();
$parsed_block['attrs']['query']['priceRange'] = array(
'min' => 1,
);

$merged_query = $this->initialize_merged_query( $parsed_block );

$this->assertEquals(
array(
'min' => 1,
),
$merged_query['priceRange'],
);

$query = new WP_Query( $merged_query );

$this->assertStringContainsString( 'wc_product_meta_lookup.min_price >= 1.', $query->request );
}

/**
* Tests that the maximum in a price range is added if set.
*/
public function test_price_range_clauses_max_price() {
$parsed_block = $this->get_base_parsed_block();
$parsed_block['attrs']['query']['priceRange'] = array(
'max' => 1,
);

$merged_query = $this->initialize_merged_query( $parsed_block );

$this->assertEquals(
array(
'max' => 1,
),
$merged_query['priceRange'],
);

$query = new WP_Query( $merged_query );

$this->assertStringContainsString( 'wc_product_meta_lookup.max_price <= 1.', $query->request );
}

/**
* Tests that the both the minimum and maximum in a price range is added if set.
*/
public function test_price_range_clauses_min_max_price() {
$parsed_block = $this->get_base_parsed_block();
$parsed_block['attrs']['query']['priceRange'] = array(
'min' => 1,
'max' => 2,
);

$merged_query = $this->initialize_merged_query( $parsed_block );

$this->assertEquals(
array(
'min' => 1,
'max' => 2,
),
$merged_query['priceRange'],
);

$query = new WP_Query( $merged_query );

$this->assertStringContainsString( 'wc_product_meta_lookup.min_price >= 1.', $query->request );
$this->assertStringContainsString( 'wc_product_meta_lookup.max_price <= 2.', $query->request );
}

/**
* Tests that the both the minimum and maximum in a price range is added if set.
*/
public function test_price_range_clauses_min_max_price_tax_exclusive() {
update_option( 'woocommerce_prices_include_tax', 'yes' );
update_option( 'woocommerce_tax_display_shop', 'excl' );

$parsed_block = $this->get_base_parsed_block();
$parsed_block['attrs']['query']['priceRange'] = array(
'min' => 1,
'max' => 2,
);

$merged_query = $this->initialize_merged_query( $parsed_block );

$this->assertEquals(
array(
'min' => 1,
'max' => 2,
),
$merged_query['priceRange'],
);

$query = new WP_Query( $merged_query );

delete_option( 'woocommerce_tax_display_shop' );
delete_option( 'woocommerce_prices_include_tax' );

$this->assertStringContainsString( 'wc_product_meta_lookup.min_price >= 1.', $query->request );
$this->assertStringContainsString( 'wc_product_meta_lookup.max_price <= 2.', $query->request );
}

/**
* Tests that the both the minimum and maximum in a price range with taxes inclusive is added if set.
*/
public function test_price_range_clauses_min_max_price_tax_inclusive() {
update_option( 'woocommerce_prices_include_tax', 'yes' );
update_option( 'woocommerce_tax_display_shop', 'incl' );
WC_Tax::create_tax_class( 'collection-test' );

$product = WC_Helper_Product::create_simple_product();
$product->set_tax_class( 'collection-test' );
$product->save();

$parsed_block = $this->get_base_parsed_block();
$parsed_block['attrs']['query']['priceRange'] = array(
'min' => 1,
'max' => 2,
);

$merged_query = $this->initialize_merged_query( $parsed_block );

$this->assertEquals(
array(
'min' => 1,
'max' => 2,
),
$merged_query['priceRange'],
);

$query = new WP_Query( $merged_query );

delete_option( 'woocommerce_tax_display_shop' );
delete_option( 'woocommerce_prices_include_tax' );
$product->delete();
WC_Tax::delete_tax_class_by( 'slug', 'collection-test' );

$this->assertStringContainsString( "( wc_product_meta_lookup.tax_class = 'collection-test' AND wc_product_meta_lookup.`min_price` >= 1.", $query->request );
$this->assertStringContainsString( "( wc_product_meta_lookup.tax_class = 'collection-test' AND wc_product_meta_lookup.`max_price` <= 2.", $query->request );
}
}

0 comments on commit 1e7b8c4

Please sign in to comment.