Add tests for searching for multiple term IDs (#43089)
* correct copy/paste error * test searching by multiple tag IDs * test searching by multiple category IDs * add the changelog file
This commit is contained in:
parent
a8dab20997
commit
a81cfff412
|
@ -0,0 +1,4 @@
|
||||||
|
Significance: patch
|
||||||
|
Type: dev
|
||||||
|
|
||||||
|
Add tests for searching for multiple term ids
|
|
@ -234,6 +234,7 @@ class WC_Tests_Product_Functions extends WC_Unit_Test_Case {
|
||||||
$cat1 = wp_insert_term( 'Cat One', 'product_cat' );
|
$cat1 = wp_insert_term( 'Cat One', 'product_cat' );
|
||||||
$cat1_term = get_term_by( 'id', $cat1['term_id'], 'product_cat' );
|
$cat1_term = get_term_by( 'id', $cat1['term_id'], 'product_cat' );
|
||||||
$cat2 = wp_insert_term( 'Cat Two', 'product_cat' );
|
$cat2 = wp_insert_term( 'Cat Two', 'product_cat' );
|
||||||
|
$cat3 = wp_insert_term( 'Cat Three', 'product_cat' );
|
||||||
|
|
||||||
$product1 = WC_Helper_Product::create_simple_product();
|
$product1 = WC_Helper_Product::create_simple_product();
|
||||||
$product1->set_name( 'Product 1' );
|
$product1->set_name( 'Product 1' );
|
||||||
|
@ -242,7 +243,7 @@ class WC_Tests_Product_Functions extends WC_Unit_Test_Case {
|
||||||
|
|
||||||
$product2 = WC_Helper_Product::create_simple_product();
|
$product2 = WC_Helper_Product::create_simple_product();
|
||||||
$product2->set_name( 'Product 2' );
|
$product2->set_name( 'Product 2' );
|
||||||
$product2->set_category_ids( array( $cat2['term_id'] ) );
|
$product2->set_category_ids( array( $cat2['term_id'], $cat3['term_id'] ) );
|
||||||
$product2->save();
|
$product2->save();
|
||||||
|
|
||||||
$product3 = WC_Helper_Product::create_simple_product();
|
$product3 = WC_Helper_Product::create_simple_product();
|
||||||
|
@ -266,6 +267,15 @@ class WC_Tests_Product_Functions extends WC_Unit_Test_Case {
|
||||||
);
|
);
|
||||||
$this->assertCount( 1, $products );
|
$this->assertCount( 1, $products );
|
||||||
$this->assertEquals( $product2->get_id(), $products[0]->get_id() );
|
$this->assertEquals( $product2->get_id(), $products[0]->get_id() );
|
||||||
|
|
||||||
|
// Search by multiple category IDs.
|
||||||
|
$products = wc_get_products(
|
||||||
|
array(
|
||||||
|
'product_category_id' => array( $cat2['term_id'], $cat3['term_id'] ),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$this->assertCount( 1, $products );
|
||||||
|
$this->assertEquals( $product2->get_id(), $products[0]->get_id() );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -275,6 +285,7 @@ class WC_Tests_Product_Functions extends WC_Unit_Test_Case {
|
||||||
$tag1 = wp_insert_term( 'Tag One', 'product_tag' );
|
$tag1 = wp_insert_term( 'Tag One', 'product_tag' );
|
||||||
$tag1_term = get_term_by( 'id', $tag1['term_id'], 'product_tag' );
|
$tag1_term = get_term_by( 'id', $tag1['term_id'], 'product_tag' );
|
||||||
$tag2 = wp_insert_term( 'Tag Two', 'product_tag' );
|
$tag2 = wp_insert_term( 'Tag Two', 'product_tag' );
|
||||||
|
$tag3 = wp_insert_term( 'Tag Three', 'product_tag' );
|
||||||
|
|
||||||
$product1 = WC_Helper_Product::create_simple_product();
|
$product1 = WC_Helper_Product::create_simple_product();
|
||||||
$product1->set_name( 'Product 1' );
|
$product1->set_name( 'Product 1' );
|
||||||
|
@ -283,14 +294,14 @@ class WC_Tests_Product_Functions extends WC_Unit_Test_Case {
|
||||||
|
|
||||||
$product2 = WC_Helper_Product::create_simple_product();
|
$product2 = WC_Helper_Product::create_simple_product();
|
||||||
$product2->set_name( 'Product 2' );
|
$product2->set_name( 'Product 2' );
|
||||||
$product2->set_tag_ids( array( $tag2['term_id'] ) );
|
$product2->set_tag_ids( array( $tag2['term_id'], $tag3['term_id'] ) );
|
||||||
$product2->save();
|
$product2->save();
|
||||||
|
|
||||||
$product3 = WC_Helper_Product::create_simple_product();
|
$product3 = WC_Helper_Product::create_simple_product();
|
||||||
$product3->set_name( 'Product 3' );
|
$product3->set_name( 'Product 3' );
|
||||||
$product3->save();
|
$product3->save();
|
||||||
|
|
||||||
// Search by category slug.
|
// Search by tag slug.
|
||||||
$products = wc_get_products(
|
$products = wc_get_products(
|
||||||
array(
|
array(
|
||||||
'tag' => $tag1_term->slug,
|
'tag' => $tag1_term->slug,
|
||||||
|
@ -299,7 +310,7 @@ class WC_Tests_Product_Functions extends WC_Unit_Test_Case {
|
||||||
$this->assertCount( 1, $products );
|
$this->assertCount( 1, $products );
|
||||||
$this->assertEquals( $product1->get_id(), $products[0]->get_id() );
|
$this->assertEquals( $product1->get_id(), $products[0]->get_id() );
|
||||||
|
|
||||||
// Search by category ID.
|
// Search by tag ID.
|
||||||
$products = wc_get_products(
|
$products = wc_get_products(
|
||||||
array(
|
array(
|
||||||
'product_tag_id' => $tag2['term_id'],
|
'product_tag_id' => $tag2['term_id'],
|
||||||
|
@ -307,6 +318,15 @@ class WC_Tests_Product_Functions extends WC_Unit_Test_Case {
|
||||||
);
|
);
|
||||||
$this->assertCount( 1, $products );
|
$this->assertCount( 1, $products );
|
||||||
$this->assertEquals( $product2->get_id(), $products[0]->get_id() );
|
$this->assertEquals( $product2->get_id(), $products[0]->get_id() );
|
||||||
|
|
||||||
|
// Search by multiple tag IDs.
|
||||||
|
$products = wc_get_products(
|
||||||
|
array(
|
||||||
|
'product_tag_id' => array( $tag2['term_id'], $tag3['term_id'] ),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$this->assertCount( 1, $products );
|
||||||
|
$this->assertEquals( $product2->get_id(), $products[0]->get_id() );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue