More tests

This commit is contained in:
claudiulodro 2017-07-12 14:18:51 -07:00
parent 753979ec49
commit 7be2c7dc55
1 changed files with 84 additions and 2 deletions

View File

@ -11,7 +11,7 @@ class WC_Tests_WC_Product_Query extends WC_Unit_Test_Case {
*
* @since 3.2
*/
public function test_order_query_new() {
public function test_product_query_new() {
$query = new WC_Product_Query();
$this->assertEquals( '', $query->get( 'weight' ) );
$this->assertEquals( array( 'product', 'product_variation' ), $query->get( 'type' ) );
@ -22,7 +22,7 @@ class WC_Tests_WC_Product_Query extends WC_Unit_Test_Case {
*
* @since 3.2
*/
public function test_order_query_standard() {
public function test_product_query_standard() {
$product1 = new WC_Product_Simple();
$product1->set_sku( 'sku1' );
$product1->set_regular_price( '10.00' );
@ -63,4 +63,86 @@ class WC_Tests_WC_Product_Query extends WC_Unit_Test_Case {
$results = $query->get_products();
$this->assertEquals( 1, count( $results ) );
}
/**
* Test querying by product date properties for dates stored in metadata.
*
* @since 3.2
*/
public function test_product_query_meta_date_queries() {
$now = current_time( 'mysql', true );
$now_stamp = strtotime( $now );
$now_date = date( 'Y-m-d', $now_stamp );
$past_stamp = $now_stamp - DAY_IN_SECONDS;
$past = date( 'Y-m-d', $past_stamp );
$future_stamp = $now_stamp + DAY_IN_SECONDS;
$future = date( 'Y-m-d', $future_stamp );
$product = new WC_Product_Simple();
$product->set_date_on_sale_from( $now_stamp );
$product->save();
// Check WC_DateTime support.
$query = new WC_Product_Query( array(
'date_on_sale_from' => $product->get_date_on_sale_from(),
) );
$products = $query->get_products();
$this->assertEquals( 1, count( $products ) );
// Check date support.
$query = new WC_Product_Query( array(
'date_on_sale_from' => $now_date,
) );
$this->assertEquals( 1, count( $query->get_products() ) );
$query->set( 'date_on_sale_from', $past );
$this->assertEquals( 0, count( $query->get_products() ) );
// Check timestamp support.
$query = new WC_Product_Query( array(
'date_on_sale_from' => $product->get_date_on_sale_from()->getTimestamp(),
) );
$this->assertEquals( 1, count( $query->get_products() ) );
$query->set( 'date_on_sale_from', $future_stamp );
$this->assertEquals( 0, count( $query->get_products() ) );
// Check comparison support.
$query = new WC_Product_Query( array(
'date_on_sale_from' => '>' . $past,
) );
$this->assertEquals( 1, count( $query->get_products() ) );
$query->set( 'date_on_sale_from', '<' . $past );
$this->assertEquals( 0, count( $query->get_products() ) );
$query->set( 'date_on_sale_from', '>=' . $now_date );
$this->assertEquals( 1, count( $query->get_products() ) );
// Check timestamp comparison support.
$query = new WC_Product_Query( array(
'date_on_sale_from' => '<' . $future_stamp,
) );
$this->assertEquals( 1, count( $query->get_products() ) );
$query->set( 'date_on_sale_from', '<' . $past_stamp );
$this->assertEquals( 0, count( $query->get_products() ) );
$query->set( 'date_on_sale_from', '>=' . $now_stamp );
// Check date range support.
$query = new WC_Product_Query( array(
'date_on_sale_from' => $past . '...' . $future,
) );
$this->assertEquals( 1, count( $query->get_products() ) );
$query->set( 'date_on_sale_from', $now_date . '...' . $future );
$this->assertEquals( 1, count( $query->get_products() ) );
$query->set( 'date_on_sale_from', $future . '...' . $now_date );
$this->assertEquals( 0, count( $query->get_products() ) );
// Check timestamp range support.
$query = new WC_Product_Query( array(
'date_on_sale_from' => $past_stamp . '...' . $future_stamp,
) );
$this->assertEquals( 1, count( $query->get_products() ) );
$query->set( 'date_on_sale_from', $now_stamp . '...' . $future_stamp );
$this->assertEquals( 1, count( $query->get_products() ) );
$query->set( 'date_on_sale_from', $future_stamp . '...' . $now_stamp );
$this->assertEquals( 0, count( $query->get_products() ) );
}
}