Commit f96d3b60d0d for woocommerce
commit f96d3b60d0dcaf283d32b1184c91dbd129efb505
Author: Ján Mikláš <neosinner@gmail.com>
Date: Fri Nov 27 12:31:13 2026 +0100
Share the attribute filter test catalog (#71523)
* Share the attribute filter test catalog
* Add an attribute filter test changelog entry
diff --git a/plugins/woocommerce/changelog/dev-share-attribute-filter-catalog b/plugins/woocommerce/changelog/dev-share-attribute-filter-catalog
new file mode 100644
index 00000000000..b7ecfbe3dec
--- /dev/null
+++ b/plugins/woocommerce/changelog/dev-share-attribute-filter-catalog
@@ -0,0 +1,3 @@
+Significance: patch
+Type: dev
+Comment: Share the catalog in the non-variation product attribute filter cases.
diff --git a/plugins/woocommerce/tests/php/src/Internal/ProductAttributesLookup/FiltererTest.php b/plugins/woocommerce/tests/php/src/Internal/ProductAttributesLookup/FiltererTest.php
index ef9b765fcfa..190b4b0bd03 100644
--- a/plugins/woocommerce/tests/php/src/Internal/ProductAttributesLookup/FiltererTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/ProductAttributesLookup/FiltererTest.php
@@ -19,6 +19,23 @@ use Automattic\WooCommerce\Enums\ProductStatus;
*/
class FiltererTest extends \WC_Unit_Test_Case {
+ private const FIXTURE_COLOR = 'Filterer Color';
+ private const FIXTURE_FEATURES = 'Filterer Features';
+
+ /**
+ * Attribute IDs owned by the shared fixture.
+ *
+ * @var int[]
+ */
+ private static $fixture_attribute_ids = array();
+
+ /**
+ * ID of the variable product shared by the non-variation attribute tests.
+ *
+ * @var int
+ */
+ private static $fixture_product_id = 0;
+
/**
* Product IDs owned by the current test.
*
@@ -37,11 +54,9 @@ class FiltererTest extends \WC_Unit_Test_Case {
/**
* Runs before all the tests in the class.
*/
- public static function setUpBeforeClass(): void {
+ public static function wpSetUpBeforeClass(): void {
global $wpdb, $wp_post_types;
- parent::setUpBeforeClass();
-
foreach ( wc_get_attribute_taxonomy_ids() as $attribute_name => $attribute_id ) {
$taxonomy_name = wc_attribute_taxonomy_name( wc_sanitize_taxonomy_name( $attribute_name ) );
$terms = get_terms(
@@ -74,10 +89,63 @@ class FiltererTest extends \WC_Unit_Test_Case {
"
);
- $wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}wc_product_attributes_lookup" );
+ $wpdb->query( "DELETE FROM {$wpdb->prefix}wc_product_attributes_lookup" );
// This is required too for WC_Query to act on the main query.
$wp_post_types['product']->has_archive = true;
+
+ $color = ProductHelper::create_attribute( self::FIXTURE_COLOR, array( 'Blue', 'Red' ) );
+ $features = ProductHelper::create_attribute( self::FIXTURE_FEATURES, array( 'Washable', 'Ironable', 'Elastic' ) );
+
+ self::$fixture_attribute_ids = array( $color['attribute_id'], $features['attribute_id'] );
+
+ $builder = new self();
+
+ self::$fixture_product_id = self::with_direct_product_attribute_lookup_updates(
+ static function () use ( $builder ): int {
+ $product = $builder->create_variable_product(
+ array(
+ 'variation_attributes' => array(
+ self::FIXTURE_COLOR => array( 'Blue', 'Red' ),
+ ),
+ 'non_variation_attributes' => array(
+ self::FIXTURE_FEATURES => array( 'Washable', 'Ironable' ),
+ ),
+ 'variations' => array(
+ array(
+ 'in_stock' => true,
+ 'defining_attributes' => array(
+ self::FIXTURE_COLOR => 'Blue',
+ ),
+ ),
+ array(
+ 'in_stock' => true,
+ 'defining_attributes' => array(
+ self::FIXTURE_COLOR => 'Red',
+ ),
+ ),
+ ),
+ )
+ );
+ return $product['id'];
+ }
+ );
+ }
+
+ /**
+ * Remove the test-owned catalog and taxonomies after the class finishes.
+ */
+ public static function wpTearDownAfterClass(): void {
+ $product = wc_get_product( self::$fixture_product_id );
+ if ( $product ) {
+ $product->delete( true );
+ }
+ self::$fixture_product_id = 0;
+
+ foreach ( self::$fixture_attribute_ids as $attribute_id ) {
+ wc_delete_attribute( $attribute_id );
+ }
+ self::$fixture_attribute_ids = array();
}
/**
@@ -925,44 +993,18 @@ class FiltererTest extends \WC_Unit_Test_Case {
* @param bool $using_lookup_table Are we using the lookup table?.
*/
private function base_test_filtering_variable_product_in_stock_for_non_variation_defining_attributes( $attributes, $filter_type, $expected_to_be_visible, $using_lookup_table ) {
- $this->create_product_attribute( 'Color', array( 'Blue', 'Red' ) );
- $this->create_product_attribute( 'Features', array( 'Washable', 'Ironable', 'Elastic' ) );
+ $this->product_ids = array( self::$fixture_product_id );
- $product = $this->create_variable_product(
- array(
- 'variation_attributes' => array(
- 'Color' => array( 'Blue', 'Red' ),
- ),
- 'non_variation_attributes' => array(
- 'Features' => array( 'Washable', 'Ironable' ),
- ),
- 'variations' => array(
- array(
- 'in_stock' => true,
- 'defining_attributes' => array(
- 'Color' => 'Blue',
- ),
- ),
- array(
- 'in_stock' => true,
- 'defining_attributes' => array(
- 'Color' => 'Red',
- ),
- ),
- ),
- )
- );
-
- $filtered_product_ids = $this->do_product_request( array( 'Features' => $attributes ), array( 'Features' => $filter_type ) );
+ $filtered_product_ids = $this->do_product_request( array( self::FIXTURE_FEATURES => $attributes ), array( self::FIXTURE_FEATURES => $filter_type ) );
if ( $expected_to_be_visible ) {
- $this->assertEquals( array( $product['id'] ), $filtered_product_ids );
+ $this->assertEquals( array( self::$fixture_product_id ), $filtered_product_ids );
} else {
$this->assertEmpty( $filtered_product_ids );
}
$expected_to_be_included_in_count = 'or' === $filter_type || $expected_to_be_visible;
- $this->assert_counters( 'Features', $expected_to_be_included_in_count ? array( 'Washable', 'Ironable' ) : array(), $filter_type );
+ $this->assert_counters( self::FIXTURE_FEATURES, $expected_to_be_included_in_count ? array( 'Washable', 'Ironable' ) : array(), $filter_type );
}
/**