diff --git a/plugins/woocommerce-blocks/tests/php/Helpers/FixtureData.php b/plugins/woocommerce-blocks/tests/php/Helpers/FixtureData.php index 9976e04fc2e..df4d7a7d6b3 100644 --- a/plugins/woocommerce-blocks/tests/php/Helpers/FixtureData.php +++ b/plugins/woocommerce-blocks/tests/php/Helpers/FixtureData.php @@ -208,6 +208,53 @@ class FixtureData { return new \WC_Coupon( $coupon->get_id() ); } + /** + * Create a new product taxonomy and term. + * + * @param \WC_Product $product The product to add the term to. + * @param string $taxonomy_name The name of the taxonomy. + * @param string $term_name The name of the term. + * @param string $term_slug The slug of the term. + * @param int $term_parent The parent of the term. + * @param string $term_description The description of the term. + * + * @return array|int[]|\WP_Error|\WP_Taxonomy + */ + public function get_taxonomy_and_term( \WC_Product $product, $taxonomy_name, $term_name, $term_slug = '', $term_parent = 0, $term_description = '' ) { + $taxonomy = register_taxonomy( $taxonomy_name, array( 'product' ), array( 'hierarchical' => true ) ); + + if ( is_wp_error( $taxonomy ) ) { + return $taxonomy; + } + + $term = wp_insert_term( + $term_name, + $taxonomy_name, + array( + 'slug' => $term_slug, + 'parent' => $term_parent, + 'description' => $term_description, + ) + ); + + if ( ! is_wp_error( $term ) && ! empty( $term['term_id'] ) ) { + global $wpdb; + $wpdb->insert( + $wpdb->prefix . 'wc_product_attributes_lookup', + array( + 'product_id' => $product->get_id(), + 'product_or_parent_id' => $product->get_parent_id(), + 'taxonomy' => $taxonomy_name, + 'term_id' => $term['term_id'], + 'is_variation_attribute' => true, + ), + array( '%d', '%d', '%s', '%d', '%d' ) + ); + } + + return $term; + } + /** * Upload a sample image and return it's ID. * diff --git a/plugins/woocommerce-blocks/tests/php/StoreApi/Routes/ProductCollectionData.php b/plugins/woocommerce-blocks/tests/php/StoreApi/Routes/ProductCollectionData.php index 255042787fb..80dd569e9a2 100644 --- a/plugins/woocommerce-blocks/tests/php/StoreApi/Routes/ProductCollectionData.php +++ b/plugins/woocommerce-blocks/tests/php/StoreApi/Routes/ProductCollectionData.php @@ -82,6 +82,7 @@ class ProductCollectionData extends ControllerTestCase { $fixtures->get_product_attribute( 'size', array( 'small', 'medium', 'large' ) ), ) ); + $fixtures->get_taxonomy_and_term( $product, 'pa_size', 'large', 'large' ); $request = new \WP_REST_Request( 'GET', '/wc/store/v1/products/collection-data' ); $request->set_param( @@ -91,8 +92,20 @@ class ProductCollectionData extends ControllerTestCase { 'taxonomy' => 'pa_size', 'query_type' => 'and', ), + ), + ); + + $request->set_param( + 'attributes', + array( + array( + 'attribute' => 'pa_size', + 'operator' => 'in', + 'slug' => array( 'large' ), + ), ) ); + $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); @@ -100,8 +113,9 @@ class ProductCollectionData extends ControllerTestCase { $this->assertEquals( null, $data['price_range'] ); $this->assertEquals( null, $data['rating_counts'] ); - $this->assertObjectHasAttribute( 'term', $data['attribute_counts'][0] ); - $this->assertObjectHasAttribute( 'count', $data['attribute_counts'][0] ); + $this->assertIsArray( $data ); + $this->assertTrue( property_exists( $data['attribute_counts'][0], 'term' ) ); + $this->assertTrue( property_exists( $data['attribute_counts'][0], 'count' ) ); } /**