Product Collection: Added `timeFrame` PHPUnit Tests (#45365)

This commit is contained in:
Christopher Allford 2024-03-15 16:30:46 -07:00 committed by GitHub
parent 7ded649fa7
commit f5f2bb035a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 70 additions and 2 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: dev
Comment: This just adds PHPUnit tests.

View File

@ -90,6 +90,7 @@ class ProductCollection extends \WP_UnitTestCase {
'woocommerceOnSale' => false,
'woocommerceAttributes' => array(),
'woocommerceStockStatus' => array(),
'timeFrame' => array(),
'priceRange' => array(),
)
);
@ -431,6 +432,54 @@ class ProductCollection extends \WP_UnitTestCase {
set_query_var( 'filter_stock_status', '' );
}
/**
* Test merging time range queries.
*/
public function test_merging_time_frame_before_queries() {
$time_frame_date = gmdate( 'Y-m-d H:i:s' );
$parsed_block = $this->get_base_parsed_block();
$parsed_block['attrs']['query']['timeFrame'] = array(
'operator' => 'not-in',
'value' => $time_frame_date,
);
$merged_query = $this->initialize_merged_query( $parsed_block );
$this->assertContainsEquals(
array(
'column' => 'post_date_gmt',
'before' => $time_frame_date,
'inclusive' => true,
),
$merged_query['date_query'],
);
}
/**
* Test merging time range queries.
*/
public function test_merging_time_frame_after_queries() {
$time_frame_date = gmdate( 'Y-m-d H:i:s' );
$parsed_block = $this->get_base_parsed_block();
$parsed_block['attrs']['query']['timeFrame'] = array(
'operator' => 'in',
'value' => $time_frame_date,
);
$merged_query = $this->initialize_merged_query( $parsed_block );
$this->assertContainsEquals(
array(
'column' => 'post_date_gmt',
'after' => $time_frame_date,
'inclusive' => true,
),
$merged_query['date_query'],
);
}
/**
* Test merging filter by stock status queries.
*/
@ -617,8 +666,9 @@ 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();
$params = array(
$args = array();
$time_frame_date = gmdate( 'Y-m-d H:i:s' );
$params = array(
'featured' => 'true',
'woocommerceOnSale' => 'true',
'woocommerceAttributes' => array(
@ -628,11 +678,16 @@ class ProductCollection extends \WP_UnitTestCase {
),
),
'woocommerceStockStatus' => array( 'instock', 'outofstock' ),
'timeFrame' => array(
'operator' => 'in',
'value' => $time_frame_date,
),
'priceRange' => array(
'min' => 1,
'max' => 100,
),
);
$request = $this->build_request( $params );
$updated_query = $this->block_instance->update_rest_query_in_editor( $args, $request );
@ -665,6 +720,15 @@ class ProductCollection extends \WP_UnitTestCase {
$updated_query['tax_query'],
);
$this->assertContains(
array(
'column' => 'post_date_gmt',
'after' => $time_frame_date,
'inclusive' => true,
),
$updated_query['date_query'],
);
$this->assertContains(
array(
'field' => 'term_id',