Product Collection: Added `featured` PHPUnit Tests (#45359)

This commit is contained in:
Christopher Allford 2024-03-06 14:24:10 -08:00 committed by GitHub
parent 8163b83bf7
commit 65fe267f32
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 62 additions and 16 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: dev
Comment: This PR adds some unit tests and does not need a changelog.

View File

@ -1,4 +1,5 @@
<?php
namespace Automattic\WooCommerce\Tests\Blocks\BlockTypes;
use Automattic\WooCommerce\Tests\Blocks\Mocks\ProductCollectionMock;
@ -75,21 +76,50 @@ class ProductCollection extends \WP_UnitTestCase {
/**
* Build a simplified request for testing.
*
* @param bool $woocommerce_on_sale WooCommerce on sale.
* @param array $woocommerce_attributes WooCommerce attributes.
* @param array $woocommerce_stock_status WooCommerce stock status.
* @param array $params The parameters to set on the request.
* @return WP_REST_Request
*/
private function build_request( $woocommerce_on_sale = 'false', $woocommerce_attributes = array(), $woocommerce_stock_status = array() ) {
private function build_request( $params = array() ) {
$params = wp_parse_args(
$params,
array(
'featured' => false,
'woocommerceOnSale' => false,
'woocommerceAttributes' => array(),
'woocommerceStockStatus' => array(),
)
);
$params['isProductCollectionBlock'] = true;
$request = new \WP_REST_Request( 'GET', '/wp/v2/product' );
$request->set_param( 'woocommerceOnSale', $woocommerce_on_sale );
$request->set_param( 'woocommerceAttributes', $woocommerce_attributes );
$request->set_param( 'woocommerceStockStatus', $woocommerce_stock_status );
$request->set_param( 'isProductCollectionBlock', true );
foreach ( $params as $param => $value ) {
$request->set_param( $param, $value );
}
return $request;
}
/**
* Test merging featured queries.
*/
public function test_merging_featured_queries() {
$parsed_block = $this->get_base_parsed_block();
$parsed_block['attrs']['query']['featured'] = true;
$merged_query = $this->initialize_merged_query( $parsed_block );
$this->assertContainsEquals(
array(
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN',
'taxonomy' => 'product_visibility',
),
$merged_query['tax_query']
);
}
/**
* Test merging on sale queries.
*/
@ -583,16 +613,19 @@ class ProductCollection extends \WP_UnitTestCase {
$product_visibility_terms = wc_get_product_visibility_term_ids();
$product_visibility_not_in = array( is_search() ? $product_visibility_terms['exclude-from-search'] : $product_visibility_terms['exclude-from-catalog'] );
$args = array();
$on_sale = 'true';
$attributes = array(
array(
'taxonomy' => 'pa_test',
'termId' => 1,
$args = array();
$params = array(
'featured' => 'true',
'woocommerceOnSale' => 'true',
'woocommerceAttributes' => array(
array(
'taxonomy' => 'pa_test',
'termId' => 1,
),
),
'woocommerceStockStatus' => array( 'instock', 'outofstock' ),
);
$stock_status = array( 'instock', 'outofstock' );
$request = $this->build_request( $on_sale, $attributes, $stock_status );
$request = $this->build_request( $params );
$updated_query = $this->block_instance->update_rest_query_in_editor( $args, $request );
@ -614,6 +647,15 @@ class ProductCollection extends \WP_UnitTestCase {
),
$updated_query['tax_query'],
);
$this->assertContains(
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN',
),
$updated_query['tax_query'],
);
$this->assertContains(
array(