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 );
|
|
|
|
$past = date( DATE_ISO8601, $now_stamp - 1000 );
|
|
|
|
$future = date( DATE_ISO8601, $now_stamp + 1000 );
|
|
|
|
|
|
|
|
$order = new WC_Order();
|
|
|
|
$order->set_date_completed( $now_stamp );
|
|
|
|
$order->save();
|
|
|
|
|
|
|
|
// 'date_created_after' should get orders created after the specified time.
|
|
|
|
$query = new WC_Order_Query( array(
|
|
|
|
'date_created_after' => $past
|
|
|
|
) );
|
|
|
|
$orders = $query->get_orders();
|
|
|
|
$this->assertEquals( 1, count( $orders ) );
|
|
|
|
|
|
|
|
// 'date_created_after' should exclude orders created before the specified time.
|
|
|
|
$query->set( 'date_created_after', $future );
|
|
|
|
$orders = $query->get_orders();
|
|
|
|
$this->assertEquals( 0, count( $orders ) );
|
|
|
|
|
|
|
|
// 'date_created_before' should get orders created before the specified time.
|
|
|
|
$query = new WC_Order_Query( array(
|
|
|
|
'date_created_before' => $future
|
|
|
|
) );
|
|
|
|
$orders = $query->get_orders();
|
|
|
|
$this->assertEquals( 1, count( $orders ) );
|
|
|
|
|
|
|
|
// 'date_created_before' should get orders created before the specified time.
|
|
|
|
$query->set( 'date_created_before', $future );
|
|
|
|
$orders = $query->get_orders();
|
|
|
|
$this->assertEquals( 1, count( $orders ) );
|
|
|
|
|
|
|
|
// Test with some metadata-stored date params.
|
|
|
|
$query = new WC_Order_Query( array(
|
|
|
|
'date_completed_after' => $past
|
|
|
|
) );
|
|
|
|
$orders = $query->get_orders();
|
|
|
|
$this->assertEquals( 1, count( $orders ) );
|
|
|
|
|
|
|
|
$query->set( 'date_completed_after', $future );
|
|
|
|
$orders = $query->get_orders();
|
|
|
|
$this->assertEquals( 0, count( $orders ) );
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|