From 65fe267f32e5534312fcf6e2edb725e25aa41341 Mon Sep 17 00:00:00 2001 From: Christopher Allford <6451942+ObliviousHarmony@users.noreply.github.com> Date: Wed, 6 Mar 2024 14:24:10 -0800 Subject: [PATCH] Product Collection: Added `featured` PHPUnit Tests (#45359) --- .../45359-add-featured-collection-tests | 4 + .../Blocks/BlockTypes/ProductCollection.php | 74 +++++++++++++++---- 2 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 plugins/woocommerce/changelog/45359-add-featured-collection-tests diff --git a/plugins/woocommerce/changelog/45359-add-featured-collection-tests b/plugins/woocommerce/changelog/45359-add-featured-collection-tests new file mode 100644 index 00000000000..1b2051796a8 --- /dev/null +++ b/plugins/woocommerce/changelog/45359-add-featured-collection-tests @@ -0,0 +1,4 @@ +Significance: patch +Type: dev +Comment: This PR adds some unit tests and does not need a changelog. + diff --git a/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/ProductCollection.php b/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/ProductCollection.php index 3a4a796ffdc..6d34d7e1a67 100644 --- a/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/ProductCollection.php +++ b/plugins/woocommerce/tests/php/src/Blocks/BlockTypes/ProductCollection.php @@ -1,4 +1,5 @@ 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(