Polish and cleanup

This commit is contained in:
claudiulodro 2017-04-26 10:49:19 -07:00
parent efa1dbbda5
commit 9f0f0cd246
6 changed files with 150 additions and 35 deletions

View File

@ -35,7 +35,7 @@ class WC_Order_Query extends WC_Object_Query {
'cart_tax' => '',
'total' => '',
'total_tax' => '',
'customer_user' => '',
'customer_id' => '',
'order_key' => '',
'billing_first_name' => '',
'billing_last_name' => '',

View File

@ -194,6 +194,13 @@ class WC_Data_Store_WP {
return $props_to_update;
}
/**
* Get valid WP_Query args from a WC_Object_Query's query variables.
*
* @since 3.1.0
* @param array $query_vars query vars from a WC_Object_Query
* @return array
*/
public function get_wp_query_args( $query_vars ) {
$skipped_values = array( '', array(), null );
@ -213,35 +220,22 @@ class WC_Data_Store_WP {
'compare' => '=',
);
} else {
switch ( $key ) {
case 'parent':
$wp_query_args['post_parent'] = $value;
break;
case 'parent__in':
$wp_query_args['post_parent__in'] = $value;
break;
case 'parent__not_in':
$wp_query_args['post_parent__not_in'] = $value;
break;
case 'in':
$wp_query_args['post__in'] = $value;
break;
case 'not_in':
$wp_query_args['post__not_in'] = $value;
break;
case 'password':
$wp_query_args['post_password'] = $value;
break;
case 'status':
$wp_query_args['post_status'] = $value;
break;
case 'per_page':
$wp_query_args['posts_per_page'] = $value;
break;
case 'type':
$wp_query_args['post_type'] = $value;
default:
$wp_query_args[ $key ] = $value;
$key_mapping = array (
'parent' => 'post_parent',
'parent__in' => 'post_parent__in',
'parent__not_in' => 'post_parent__not_in',
'in' => 'post__in',
'not_in' => 'post__not_in',
'password' => 'post_password',
'status' => 'post_status',
'per_page' => 'posts_per_page',
'type' => 'post_type',
);
if ( isset( $key_mapping[ $key ] ) ) {
$wp_query_args[ $key_mapping[ $key ] ] = $value;
} else {
$wp_query_args[ $key ] = $value;
}
}
}

View File

@ -635,6 +635,36 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
return get_post_type( $order_id );
}
/**
* Get valid WP_Query args from a WC_Order_Query's query variables.
*
* @since 3.1.0
* @param array $query_vars query vars from a WC_Order_Query
* @return array
*/
public function get_wp_query_args( $query_vars ) {
$key_mapping = array(
'customer_id' => 'customer_user',
);
foreach( $key_mapping as $query_key => $db_key ) {
if ( isset( $query_vars[ $query_key ] ) ) {
$query_vars[ $db_key ] = $query_vars[ $query_key ];
unset( $query_vars[ $query_key ] );
}
}
return parent::get_wp_query_args( $query_vars );
}
/**
* Query for Orders matching specific criteria.
*
* @since 3.1.0
* @param array $query_vars query vars from a WC_Order_Query
* @return array of WC_Order objects
*/
public function query( $query_vars ) {
$args = $this->get_wp_query_args( $query_vars );
$query = new WP_Query( $args );

View File

@ -67,6 +67,11 @@ class WC_Tests_Data_Store extends WC_Unit_Test_Case {
$this->assertEquals( 'WC_Dummy_Data_Store_CPT', $store->get_current_class_name() );
}
/**
* Test converting WP_Object_Query query vars to WP_Query args.
*
* @since 3.1.0
*/
function test_get_wp_query_args() {
$store = new WC_Data_Store_WP();
$query = new WC_Mock_WC_Object_Query( array(

View File

@ -3,9 +3,15 @@
/**
* WC_Object_Query tests
* @package WooCommerce\Tests\CRUD
* @since 3.1.0
*/
class WC_Tests_WC_Object_Query extends WC_Unit_Test_Case {
/**
* Test the default query var values.
*
* @since 3.1.0
*/
function test_default_query() {
$query = new WC_Mock_WC_Object_Query();
@ -14,6 +20,11 @@ class WC_Tests_WC_Object_Query extends WC_Unit_Test_Case {
$this->assertEquals( 'date', $query->get( 'orderby' ) );
}
/**
* Test setting/getting query vars.
*
* @since 3.1.0
*/
function test_query_with_args() {
$args = array(
'per_page' => 15,

View File

@ -6,13 +6,23 @@
*/
class WC_Tests_WC_Order_Query extends WC_Unit_Test_Case {
public function test_new_order_query() {
/**
* Test basic methods on a new WC_Order_Query.
*
* @since 3.1
*/
public function test_order_query_new() {
$query = new WC_Order_Query();
$this->assertEquals( '', $query->get( 'total' ) );
$this->assertEquals( wc_get_order_types( 'view-orders' ), $query->get( 'type' ) );
}
public function test_standard_order_query() {
/**
* Test querying by order properties.
*
* @since 3.1
*/
public function test_order_query_standard() {
$order1 = new WC_Order();
$order1->set_prices_include_tax( true );
$order1->set_total( '100.00' );
@ -23,20 +33,85 @@ class WC_Tests_WC_Order_Query extends WC_Unit_Test_Case {
$order2->set_total( '100.00' );
$order2->save();
// Just get some orders.
$query = new WC_Order_Query();
$results = $query->get_orders();
$this->assertEquals( 2, count( $results ) );
// Get orders with a specific property..
$query->set( 'prices_include_tax', 'no' );
$results = $query->get_orders();
$this->assertEquals( 1, count( $results ) );
$this->assertEquals( $results[0]->get_id(), $order2->get_id() );
$query->set( 'prices_include_tax', '' );
// Get orders with two specific properties.
$query->set( 'total', '100.00' );
$results = $query->get_orders();
$this->assertEquals( 1, count( $results ) );
$this->assertEquals( $results[0]->get_id(), $order2->get_id() );
// Get multiple orders that have a the same specific property.
$query->set( 'prices_include_tax', '' );
$results = $query->get_orders();
$this->assertEquals( 2, count( $results ) );
// Limit to one result.
$query->set( 'per_page', 1 );
$results = $query->get_orders();
$this->assertEquals( 1, count( $results ) );
}
/**
* Test querying with custom meta queries.
*
* @since 3.1
*/
public function test_order_query_meta() {
$order = new WC_Order();
$order->set_billing_first_name( 'Eduardo' );
$order->save();
update_post_meta( $order->get_id(), 'testkey', 'testvalue' );
// Get orders with a custom meta query.
$query = new WC_Order_Query( array(
'meta_query' => array(
array(
'key' => 'testkey',
'value' => 'testvalue',
'compare' => '='
)
)
) );
$results = $query->get_orders();
$this->assertEquals( 1, count( $results ) );
// Get orders with a custom meta query and a specific property.
$query->set( 'billing_first_name', 'Eduardo' );
$results = $query->get_orders();
$this->assertEquals( 1, count( $results ) );
}
/**
* 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 ) );
}
}