2017-04-24 23:28:13 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test WC_Order_Query
|
|
|
|
* @package WooCommerce\Tests\Order
|
|
|
|
*/
|
|
|
|
class WC_Tests_WC_Order_Query extends WC_Unit_Test_Case {
|
|
|
|
|
2017-04-26 17:49:19 +00:00
|
|
|
/**
|
|
|
|
* Test basic methods on a new WC_Order_Query.
|
|
|
|
*
|
|
|
|
* @since 3.1
|
|
|
|
*/
|
|
|
|
public function test_order_query_new() {
|
2017-04-24 23:28:13 +00:00
|
|
|
$query = new WC_Order_Query();
|
|
|
|
$this->assertEquals( '', $query->get( 'total' ) );
|
|
|
|
$this->assertEquals( wc_get_order_types( 'view-orders' ), $query->get( 'type' ) );
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:49:19 +00:00
|
|
|
/**
|
|
|
|
* Test querying by order properties.
|
|
|
|
*
|
|
|
|
* @since 3.1
|
|
|
|
*/
|
|
|
|
public function test_order_query_standard() {
|
2017-04-25 21:52:17 +00:00
|
|
|
$order1 = new WC_Order();
|
|
|
|
$order1->set_prices_include_tax( true );
|
|
|
|
$order1->set_total( '100.00' );
|
|
|
|
$order1->save();
|
|
|
|
|
|
|
|
$order2 = new WC_Order();
|
|
|
|
$order2->set_prices_include_tax( false );
|
|
|
|
$order2->set_total( '100.00' );
|
|
|
|
$order2->save();
|
|
|
|
|
2017-04-26 17:49:19 +00:00
|
|
|
// Just get some orders.
|
2017-04-25 21:52:17 +00:00
|
|
|
$query = new WC_Order_Query();
|
|
|
|
$results = $query->get_orders();
|
|
|
|
$this->assertEquals( 2, count( $results ) );
|
|
|
|
|
2017-04-26 17:49:19 +00:00
|
|
|
// Get orders with a specific property..
|
2017-04-25 21:52:17 +00:00
|
|
|
$query->set( 'prices_include_tax', 'no' );
|
|
|
|
$results = $query->get_orders();
|
2017-04-26 17:49:19 +00:00
|
|
|
$this->assertEquals( 1, count( $results ) );
|
|
|
|
$this->assertEquals( $results[0]->get_id(), $order2->get_id() );
|
2017-04-25 21:52:17 +00:00
|
|
|
|
2017-04-26 17:49:19 +00:00
|
|
|
// Get orders with two specific properties.
|
|
|
|
$query->set( 'total', '100.00' );
|
|
|
|
$results = $query->get_orders();
|
2017-04-25 21:52:17 +00:00
|
|
|
$this->assertEquals( 1, count( $results ) );
|
|
|
|
$this->assertEquals( $results[0]->get_id(), $order2->get_id() );
|
|
|
|
|
2017-04-26 17:49:19 +00:00
|
|
|
// Get multiple orders that have a the same specific property.
|
2017-04-25 21:52:17 +00:00
|
|
|
$query->set( 'prices_include_tax', '' );
|
|
|
|
$results = $query->get_orders();
|
|
|
|
$this->assertEquals( 2, count( $results ) );
|
2017-04-26 17:49:19 +00:00
|
|
|
|
|
|
|
// Limit to one result.
|
2017-04-27 21:46:00 +00:00
|
|
|
$query->set( 'limit', 1 );
|
2017-04-26 17:49:19 +00:00
|
|
|
$results = $query->get_orders();
|
|
|
|
$this->assertEquals( 1, count( $results ) );
|
|
|
|
}
|
|
|
|
|
2017-04-27 21:46:00 +00:00
|
|
|
/**
|
|
|
|
* Test querying by order date properties.
|
|
|
|
*
|
|
|
|
* @since 3.1
|
|
|
|
*/
|
|
|
|
public function test_order_query_date_queries() {
|
|
|
|
$now = current_time( 'mysql', true );
|
|
|
|
$now_stamp = strtotime( $now );
|
2017-05-09 21:49:45 +00:00
|
|
|
$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 );
|
2017-04-27 21:46:00 +00:00
|
|
|
|
|
|
|
$order = new WC_Order();
|
|
|
|
$order->set_date_completed( $now_stamp );
|
|
|
|
$order->save();
|
|
|
|
|
2017-05-09 21:49:45 +00:00
|
|
|
// Check WC_DateTime support.
|
2017-04-27 21:46:00 +00:00
|
|
|
$query = new WC_Order_Query( array(
|
2017-05-09 18:37:45 +00:00
|
|
|
'date_created' => $order->get_date_created()
|
2017-04-27 21:46:00 +00:00
|
|
|
) );
|
|
|
|
$orders = $query->get_orders();
|
|
|
|
$this->assertEquals( 1, count( $orders ) );
|
|
|
|
|
2017-05-09 21:49:45 +00:00
|
|
|
// Check date support.
|
|
|
|
$query = new WC_Order_Query( array(
|
|
|
|
'date_created' => $now_date
|
|
|
|
) );
|
|
|
|
$this->assertEquals( 1, count( $query->get_orders() ) );
|
|
|
|
$query->set( 'date_created', $past );
|
|
|
|
$this->assertEquals( 0, count( $query->get_orders() ) );
|
|
|
|
|
|
|
|
// Check timestamp support.
|
|
|
|
$query = new WC_Order_Query( array(
|
|
|
|
'date_created' => $order->get_date_created()->getTimestamp()
|
|
|
|
) );
|
|
|
|
$this->assertEquals( 1, count( $query->get_orders() ) );
|
|
|
|
$query->set( 'date_created', $future_stamp );
|
|
|
|
$this->assertEquals( 0, count( $query->get_orders() ) );
|
|
|
|
|
|
|
|
// Check comparison support.
|
|
|
|
$query = new WC_Order_Query( array(
|
|
|
|
'date_created' => '>' . $past
|
|
|
|
) );
|
|
|
|
$this->assertEquals( 1, count( $query->get_orders() ) );
|
|
|
|
$query->set( 'date_created', '<' . $past );
|
|
|
|
$this->assertEquals( 0, count( $query->get_orders() ) );
|
|
|
|
$query->set( 'date_created', '>=' . $now_date );
|
|
|
|
$this->assertEquals( 1, count( $query->get_orders() ) );
|
|
|
|
|
|
|
|
// Check timestamp comparison support.
|
|
|
|
$query = new WC_Order_Query( array(
|
|
|
|
'date_created' => '<' . $future_stamp
|
|
|
|
) );
|
|
|
|
$this->assertEquals( 1, count( $query->get_orders() ) );
|
|
|
|
$query->set( 'date_created', '<' . $past_stamp );
|
|
|
|
$this->assertEquals( 0, count( $query->get_orders() ) );
|
|
|
|
$query->set( 'date_created', '>=' . $now_stamp );
|
|
|
|
|
|
|
|
// Check date range support.
|
|
|
|
$query = new WC_Order_Query( array(
|
|
|
|
'date_created' => $past . '...' . $future
|
|
|
|
) );
|
|
|
|
$this->assertEquals( 1, count( $query->get_orders() ) );
|
2017-05-10 20:20:07 +00:00
|
|
|
$query->set( 'date_created', $past . '...' . $now_date );
|
2017-05-09 21:49:45 +00:00
|
|
|
$this->assertEquals( 1, count( $query->get_orders() ) );
|
|
|
|
$query->set( 'date_created', $future . '...' . $now_date );
|
|
|
|
$this->assertEquals( 0, count( $query->get_orders() ) );
|
|
|
|
|
|
|
|
// Check timestamp range support.
|
|
|
|
$query = new WC_Order_Query( array(
|
|
|
|
'date_created' => $past_stamp . '...' . $future_stamp
|
|
|
|
) );
|
|
|
|
$this->assertEquals( 1, count( $query->get_orders() ) );
|
|
|
|
$query->set( 'date_created', $now_stamp . '...' . $future_stamp );
|
|
|
|
$this->assertEquals( 1, count( $query->get_orders() ) );
|
|
|
|
$query->set( 'date_created', $future_stamp . '...' . $now_stamp );
|
|
|
|
$this->assertEquals( 0, count( $query->get_orders() ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test querying by order date properties for dates stored in metadata.
|
|
|
|
*
|
|
|
|
* @since 3.1
|
|
|
|
*/
|
|
|
|
public function test_order_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 );
|
|
|
|
|
|
|
|
$order = new WC_Order();
|
|
|
|
$order->set_date_completed( $now_stamp );
|
|
|
|
$order->save();
|
2017-04-27 21:46:00 +00:00
|
|
|
|
2017-05-09 21:49:45 +00:00
|
|
|
// Check WC_DateTime support.
|
2017-04-27 21:46:00 +00:00
|
|
|
$query = new WC_Order_Query( array(
|
2017-05-09 21:49:45 +00:00
|
|
|
'date_completed' => $order->get_date_completed()
|
2017-04-27 21:46:00 +00:00
|
|
|
) );
|
|
|
|
$orders = $query->get_orders();
|
|
|
|
$this->assertEquals( 1, count( $orders ) );
|
|
|
|
|
2017-05-09 21:49:45 +00:00
|
|
|
// Check date support.
|
|
|
|
$query = new WC_Order_Query( array(
|
|
|
|
'date_completed' => $now_date
|
|
|
|
) );
|
|
|
|
$this->assertEquals( 1, count( $query->get_orders() ) );
|
|
|
|
$query->set( 'date_completed', $past );
|
|
|
|
$this->assertEquals( 0, count( $query->get_orders() ) );
|
|
|
|
|
|
|
|
// Check timestamp support.
|
|
|
|
$query = new WC_Order_Query( array(
|
|
|
|
'date_completed' => $order->get_date_completed()->getTimestamp()
|
|
|
|
) );
|
|
|
|
$this->assertEquals( 1, count( $query->get_orders() ) );
|
|
|
|
$query->set( 'date_completed', $future_stamp );
|
|
|
|
$this->assertEquals( 0, count( $query->get_orders() ) );
|
|
|
|
|
|
|
|
// Check comparison support.
|
2017-04-27 21:46:00 +00:00
|
|
|
$query = new WC_Order_Query( array(
|
2017-05-09 18:37:45 +00:00
|
|
|
'date_completed' => '>' . $past
|
2017-04-27 21:46:00 +00:00
|
|
|
) );
|
2017-05-09 21:49:45 +00:00
|
|
|
//$this->assertEquals( 1, count( $query->get_orders() ) );
|
|
|
|
$query->set( 'date_completed', '<' . $past );
|
|
|
|
//$this->assertEquals( 0, count( $query->get_orders() ) );
|
|
|
|
$query->set( 'date_completed', '>=' . $now_date );
|
|
|
|
$this->assertEquals( 1, count( $query->get_orders() ) );
|
2017-04-27 21:46:00 +00:00
|
|
|
|
2017-05-09 21:49:45 +00:00
|
|
|
// Check timestamp comparison support.
|
|
|
|
$query = new WC_Order_Query( array(
|
|
|
|
'date_completed' => '<' . $future_stamp
|
|
|
|
) );
|
|
|
|
$this->assertEquals( 1, count( $query->get_orders() ) );
|
|
|
|
$query->set( 'date_completed', '<' . $past_stamp );
|
|
|
|
$this->assertEquals( 0, count( $query->get_orders() ) );
|
|
|
|
$query->set( 'date_completed', '>=' . $now_stamp );
|
2017-05-09 18:37:45 +00:00
|
|
|
|
2017-05-09 21:49:45 +00:00
|
|
|
// Check date range support.
|
|
|
|
$query = new WC_Order_Query( array(
|
|
|
|
'date_completed' => $past . '...' . $future
|
|
|
|
) );
|
|
|
|
$this->assertEquals( 1, count( $query->get_orders() ) );
|
|
|
|
$query->set( 'date_completed', $now_date . '...' . $future );
|
|
|
|
$this->assertEquals( 1, count( $query->get_orders() ) );
|
|
|
|
$query->set( 'date_completed', $future . '...' . $now_date );
|
|
|
|
$this->assertEquals( 0, count( $query->get_orders() ) );
|
|
|
|
|
|
|
|
// Check timestamp range support.
|
|
|
|
$query = new WC_Order_Query( array(
|
|
|
|
'date_completed' => $past_stamp . '...' . $future_stamp
|
|
|
|
) );
|
|
|
|
$this->assertEquals( 1, count( $query->get_orders() ) );
|
|
|
|
$query->set( 'date_completed', $now_stamp . '...' . $future_stamp );
|
|
|
|
$this->assertEquals( 1, count( $query->get_orders() ) );
|
|
|
|
$query->set( 'date_completed', $future_stamp . '...' . $now_stamp );
|
|
|
|
$this->assertEquals( 0, count( $query->get_orders() ) );
|
2017-04-27 21:46:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-26 17:49:19 +00:00
|
|
|
/**
|
|
|
|
* Test the query var mapping customer_id => customer_user.
|
|
|
|
*
|
|
|
|
* @since 3.1
|
|
|
|
*/
|
|
|
|
public function test_order_query_key_mapping() {
|
|
|
|
$user_id = wp_insert_user( array(
|
|
|
|
'user_login' => 'testname',
|
|
|
|
'user_pass' => 'testpass',
|
|
|
|
'user_email' => 'email@testmail.com'
|
|
|
|
) );
|
|
|
|
|
|
|
|
$order = new WC_Order();
|
|
|
|
$order->set_customer_id( $user_id );
|
|
|
|
$order->save();
|
|
|
|
|
|
|
|
$query = new WC_Order_Query( array(
|
|
|
|
'customer_id' => $user_id
|
|
|
|
) );
|
|
|
|
$results = $query->get_orders();
|
|
|
|
|
|
|
|
$this->assertEquals( 1, count( $results ) );
|
2017-04-25 21:52:17 +00:00
|
|
|
}
|
2017-04-24 23:28:13 +00:00
|
|
|
}
|