Filter data count mismatch > Fix PHP unit tests (https://github.com/woocommerce/woocommerce-blocks/pull/8984)

* Introduce the new get_attribute_and_meta_counts method.

* Ensure that if no term_slug or term_id is found for counting, the default list with all terms (with count equal zero) is returned instead.

* update conditional for the slug, the empty state for requests without filter attributes and the condition query for 'and'

* Introduce the get_terms_list method.

* Remove the legacy get_attribute_counts method and update its calls to rely on the new get_attribute_and_meta_counts method instead.

* Update the query to ensure that if a parent product has multiple identical attributes, they are counted once.

* Update to start relying on the get_product_by_metas method for counting product metas

* Add a new where_clause to only include product metas and attributes in the macro query if they are not empty.

* Add wpdb->prepare to the macro query and the get_terms_list method.

* Replace the raw atomic query for fetching the filtered terms with the new get_product_by_filtered_terms method.

* Update the request params for the get_attribute_and_meta_counts method.

* Update the request params for the product metas (min and max price).

* Update the query and returned value on get_terms_list.

* Update the validation for returning the default counts when no values are filtered.

* Update the query on get_terms_list to use ->prefix

* Update the  variable for the query to rely on the filtered one. Update the min_price and max_price format on get_product_by_metas.

* Ensure the get_product_by_filtered_terms method is triggered for each one of the filtered terms and update the macro query to include those term ids on the WHERE clause.

* Make adjustments for the 'and' condition to work as expected.

* Ensure the queryState.attributes is properly added as a param to the API request to correctly fetch the attribute count data.

* Ensure the get_product_by_metas method is only triggered when at least one of the metas in the request is not empty.

* Join type update: for the 'and' (all) filter condition, items with the count zero are not displayed.

* wpdb prepare the where clauses

* Update the get_product_by_filtered_terms query wpdb prepare params

* update the get_product_by_metas method's where clause preparation.

* Update the where clause preparation for get_attribute_and_meta_counts so we don't rely on interpolated variables anymore.

* Adjust the get_attribute_and_meta_counts method for usage alongside the rating filter.

* Adjust the query for fetching the attribute counts for filtered ratings.

* Add support for the filter by stock.

* Ensure the product attribute counts are correct if the parent product receives a rating.

* Ensure product_or_parent_id is used only when the filter by rating is used, not affecting price or stock filters.

* Add the missing else condition.

* Enable caching.

* Address CR

* Update query for average rating.

* remove file accidentally commited.

* When multiple ratings are selected, make sure the where clause is updated accordingly for each one of them.

* Start updating the stock_status logic to account for when multiple options are selected by the user.

* Ensure the counts are properly updated when more than one stock status is selected.

* Ditch the is_array condition for the average_rating counts as  is always an array.

* Deprecate the second param attributes for the get_attribute_counts method.

* Add the filtered_attribute to the transient_key

* Bypass cache if WP_DEBUG is enabled.

* Update formatting for macro query.

* Fix mixed tabs spaces on query

* Fix PHP unit tests for the new attribute counts.

* Update spacing/formatting for SQL queries.

* Minor: update indentation for the main SQL query

---------

Co-authored-by: roykho <roykho77@gmail.com>
This commit is contained in:
Patricia Hillebrandt 2023-04-11 12:33:18 -03:00 committed by GitHub
parent 3c0463ada1
commit 8ae444b201
2 changed files with 63 additions and 2 deletions

View File

@ -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.
*

View File

@ -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' ) );
}
/**