Merge branch 'master' into order-crud-api

This commit is contained in:
Mike Jolley 2016-08-16 11:09:14 +01:00
commit bc75c60227
14 changed files with 218 additions and 269 deletions

View File

@ -253,8 +253,7 @@ abstract class WC_Data {
$raw_meta_data = $wpdb->get_results( $wpdb->prepare( "
SELECT " . $db_info['meta_id_field'] . ", meta_key, meta_value
FROM " . $db_info['table'] . "
WHERE " . $db_info['object_id_field'] . " = %d ORDER BY " . $db_info['meta_id_field'] . "
AND meta_key NOT LIKE 'wp\_%%'
WHERE " . $db_info['object_id_field'] . "=%d AND meta_key NOT LIKE 'wp\_%%' ORDER BY " . $db_info['meta_id_field'] . "
", $this->get_id() ) );
if ( $raw_meta_data ) {
@ -264,7 +263,7 @@ abstract class WC_Data {
}
$this->_meta_data[] = (object) array(
'key' => $meta->meta_key,
'value' => $meta->meta_value,
'value' => maybe_unserialize( $meta->meta_value ),
'meta_id' => $meta->{ $db_info['meta_id_field'] },
);
}

View File

@ -2203,7 +2203,7 @@ class WC_Admin_Post_Types {
foreach ( $existing_permissions as $existing_permission ) {
$order = wc_get_order( $existing_permission->order_id );
if ( ! empty( $order->get_id() ) ) {
if ( $order->get_id() ) {
// Remove permissions
if ( ! empty( $removed_download_ids ) ) {
foreach ( $removed_download_ids as $download_id ) {

View File

@ -153,7 +153,7 @@ class WC_Meta_Box_Order_Data {
$payment_gateways = array();
}
$payment_method = ! empty( $order->get_payment_method() ) ? $order->get_payment_method() : '';
$payment_method = $order->get_payment_method() ? $order->get_payment_method() : '';
$order_type_object = get_post_type_object( $post->post_type );
wp_nonce_field( 'woocommerce_save_data', 'woocommerce_meta_nonce' );
@ -219,7 +219,7 @@ class WC_Meta_Box_Order_Data {
<p class="form-field form-field-wide wc-customer-user">
<label for="customer_user"><?php _e( 'Customer:', 'woocommerce' ) ?> <?php
if ( ! empty( $order->get_user_id() ) ) {
if ( $order->get_user_id() ) {
$args = array( 'post_status' => 'all',
'post_type' => 'shop_order',
'_customer_user' => absint( $order->get_user_id() )
@ -233,7 +233,7 @@ class WC_Meta_Box_Order_Data {
<?php
$user_string = '';
$user_id = '';
if ( ! empty( $order->get_user_id() ) ) {
if ( $order->get_user_id() ) {
$user_id = absint( $order->get_user_id() );
$user = get_user_by( 'id', $user_id );
$user_string = esc_html( $user->display_name ) . ' (#' . absint( $user->ID ) . ' &ndash; ' . esc_html( $user->user_email ) . ')';

View File

@ -122,8 +122,8 @@ class WC_API_Coupons extends WC_API_Resource {
'individual_use' => $coupon->get_individual_use(),
'product_ids' => array_map( 'absint', (array) $coupon->get_product_ids() ),
'exclude_product_ids' => array_map( 'absint', (array) $coupon->get_excluded_product_ids() ),
'usage_limit' => ( ! empty( $coupon->get_usage_limit() ) ) ? $coupon->get_usage_limit() : null,
'usage_limit_per_user' => ( ! empty( $coupon->get_usage_limit_per_user() ) ) ? $coupon->get_usage_limit_per_user() : null,
'usage_limit' => $coupon->get_usage_limit() ? $coupon->get_usage_limit() : null,
'usage_limit_per_user' => $coupon->get_usage_limit_per_user() ? $coupon->get_usage_limit_per_user() : null,
'limit_usage_to_x_items' => (int) $coupon->get_limit_usage_to_x_items(),
'usage_count' => (int) $coupon->get_usage_count(),
'expiry_date' => $this->server->format_datetime( $coupon->get_expiry_date() ),

View File

@ -132,11 +132,11 @@ class WC_API_Coupons extends WC_API_Resource {
'individual_use' => $coupon->get_individual_use(),
'product_ids' => array_map( 'absint', (array) $coupon->get_product_ids() ),
'exclude_product_ids' => array_map( 'absint', (array) $coupon->get_excluded_product_ids() ),
'usage_limit' => ( ! empty( $coupon->get_usage_limit() ) ) ? $coupon->get_usage_limit() : null,
'usage_limit_per_user' => ( ! empty( $coupon->get_usage_limit_per_user() ) ) ? $coupon->get_usage_limit_per_user() : null,
'usage_limit' => $coupon->get_usage_limit() ? $coupon->get_usage_limit() : null,
'usage_limit_per_user' => $coupon->get_usage_limit_per_user() ? $coupon->get_usage_limit_per_user() : null,
'limit_usage_to_x_items' => (int) $coupon->get_limit_usage_to_x_items(),
'usage_count' => (int) $coupon->get_usage_count(),
'expiry_date' => ( ! empty( $coupon->get_expiry_date() ) ) ? $this->server->format_datetime( $coupon->get_expiry_date() ) : null,
'expiry_date' => $coupon->get_expiry_date() ? $this->server->format_datetime( $coupon->get_expiry_date() ) : null,
'enable_free_shipping' => $coupon->get_free_shipping(),
'product_category_ids' => array_map( 'absint', (array) $coupon->get_product_categories() ),
'exclude_product_category_ids' => array_map( 'absint', (array) $coupon->get_excluded_product_categories() ),

View File

@ -132,11 +132,11 @@ class WC_API_Coupons extends WC_API_Resource {
'individual_use' => $coupon->get_individual_use(),
'product_ids' => array_map( 'absint', (array) $coupon->get_product_ids() ),
'exclude_product_ids' => array_map( 'absint', (array) $coupon->get_excluded_product_ids() ),
'usage_limit' => ( ! empty( $coupon->get_usage_limit() ) ) ? $coupon->get_usage_limit() : null,
'usage_limit_per_user' => ( ! empty( $coupon->get_usage_limit_per_user() ) ) ? $coupon->get_usage_limit_per_user() : null,
'usage_limit' => $coupon->get_usage_limit() ? $coupon->get_usage_limit() : null,
'usage_limit_per_user' => $coupon->get_usage_limit_per_user() ? $coupon->get_usage_limit_per_user() : null,
'limit_usage_to_x_items' => (int) $coupon->get_limit_usage_to_x_items(),
'usage_count' => (int) $coupon->get_usage_count(),
'expiry_date' => ( ! empty( $coupon->get_expiry_date() ) ) ? $this->server->format_datetime( $coupon->get_expiry_date() ) : null,
'expiry_date' => $coupon->get_expiry_date() ? $this->server->format_datetime( $coupon->get_expiry_date() ) : null,
'enable_free_shipping' => $coupon->get_free_shipping(),
'product_category_ids' => array_map( 'absint', (array) $coupon->get_product_categories() ),
'exclude_product_category_ids' => array_map( 'absint', (array) $coupon->get_excluded_product_categories() ),

View File

@ -26,10 +26,6 @@ class WC_Customer extends WC_Legacy_Customer {
'first_name' => '',
'last_name' => '',
'role' => 'customer',
'last_order_id' => null, // read only
'last_order_date' => null, // read only
'orders_count' => 0, // read only
'total_spent' => 0, // read only
'username' => '', // read only on existing users
'password' => '', // write only
'date_created' => '', // read only
@ -161,23 +157,111 @@ class WC_Customer extends WC_Legacy_Customer {
$default = wc_get_customer_default_location();
// Set some defaults if some of our values are still not set.
if ( empty( $this->get_billing_country() ) ) {
if ( ! $this->get_billing_country() ) {
$this->set_billing_country( $default['country'] );
}
if ( empty( $this->get_shipping_country() ) ) {
if ( ! $this->get_shipping_country() ) {
$this->set_shipping_country( $this->get_billing_country() );
}
if ( empty( $this->get_billing_state() ) ) {
if ( ! $this->get_billing_state() ) {
$this->set_billing_state( $default['state'] );
}
if ( empty( $this->get_shipping_state() ) ) {
if ( ! $this->get_shipping_state() ) {
$this->set_shipping_state( $this->get_billing_state() );
}
}
/**
* Gets the customers last order.
* @return WC_Order|false
*/
public function get_last_order() {
global $wpdb;
$last_order = $wpdb->get_var( "SELECT posts.ID
FROM $wpdb->posts AS posts
LEFT JOIN {$wpdb->postmeta} AS meta on posts.ID = meta.post_id
WHERE meta.meta_key = '_customer_user'
AND meta.meta_value = '" . esc_sql( $this->get_id() ) . "'
AND posts.post_type = 'shop_order'
AND posts.post_status IN ( '" . implode( "','", array_map( 'esc_sql', array_keys( wc_get_order_statuses() ) ) ) . "' )
ORDER BY posts.ID DESC
" );
if ( $last_order ) {
return wc_get_order( absint( $last_order ) );
} else {
return false;
}
}
/**
* Return the number of orders this customer has.
* @since 2.7.0
* @return integer
*/
public function get_order_count() {
global $wpdb;
$count = $wpdb->get_var( "SELECT COUNT(*)
FROM $wpdb->posts as posts
LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
WHERE meta.meta_key = '_customer_user'
AND posts.post_type = 'shop_order'
AND posts.post_status IN ( '" . implode( "','", array_map( 'esc_sql', array_keys( wc_get_order_statuses() ) ) ) . "' )
AND meta_value = '" . esc_sql( $this->get_id() ) . "'
" );
return absint( $count );
}
/**
* Return how much money this customer has spent.
* @since 2.7.0
* @return float
*/
public function get_total_spent() {
global $wpdb;
$spent = $wpdb->get_var( "SELECT SUM(meta2.meta_value)
FROM $wpdb->posts as posts
LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
LEFT JOIN {$wpdb->postmeta} AS meta2 ON posts.ID = meta2.post_id
WHERE meta.meta_key = '_customer_user'
AND meta.meta_value = '" . esc_sql( $this->get_id() ) . "'
AND posts.post_type = 'shop_order'
AND posts.post_status IN ( 'wc-completed', 'wc-processing' )
AND meta2.meta_key = '_order_total'
" );
if ( ! $spent ) {
$spent = 0;
}
return wc_format_decimal( $spent );
}
/**
* Is customer outside base country (for tax purposes)?
* @return bool
*/
public function is_customer_outside_base() {
list( $country, $state ) = $this->get_taxable_address();
if ( $country ) {
$default = wc_get_base_location();
if ( $default['country'] !== $country ) {
return true;
}
if ( $default['state'] && $default['state'] !== $state ) {
return true;
}
}
return false;
}
/*
|--------------------------------------------------------------------------
| Getters
@ -239,42 +323,6 @@ class WC_Customer extends WC_Legacy_Customer {
return $this->_data['role'];
}
/**
* Return customer's last order ID.
* @since 2.7.0
* @return integer|null
*/
public function get_last_order_id() {
return ( is_null( $this->_data['last_order_id'] ) ? null : intval( $this->_data['last_order_id'] ) );
}
/**
* Return the date of the customer's last order.
* @since 2.7.0
* @return integer|null
*/
public function get_last_order_date() {
return ( is_null( $this->_data['last_order_date'] ) ? null : intval( $this->_data['last_order_date'] ) );
}
/**
* Return the number of orders this customer has.
* @since 2.7.0
* @return integer
*/
public function get_orders_count() {
return intval( $this->_data['orders_count'] );
}
/**
* Return how much money this customer has spent.
* @since 2.7.0
* @return float
*/
public function get_total_spent() {
return wc_format_decimal( $this->_data['total_spent'] );
}
/**
* Return this customer's avatar.
* @since 2.7.0
@ -566,6 +614,15 @@ class WC_Customer extends WC_Legacy_Customer {
| object.
*/
/**
* Set customer ID.
* @since 2.7.0
* @param int $value
*/
protected function set_id( $value ) {
$this->_data['id'] = absint( $value );
}
/**
* Set customer's username.
* @since 2.7.0
@ -611,42 +668,6 @@ class WC_Customer extends WC_Legacy_Customer {
$this->_data['role'] = $role;
}
/**
* Set customer's last order ID.
* @since 2.7.0
* @param integer|null $last_order_id
*/
public function set_last_order_id( $last_order_id ) {
$this->_data['last_order_id'] = $last_order_id;
}
/**
* Set the date of the customer's last order.
* @since 2.7.0
* @param string|null $last_order_date
*/
public function set_last_order_date( $last_order_date ) {
$this->_data['last_order_date'] = $last_order_date;
}
/**
* Set the number of orders this customer has.
* @since 2.7.0
* @param integer $order_count
*/
public function set_orders_count( $orders_count ) {
$this->_data['orders_count'] = $orders_count;
}
/**
* Return how much money this customer has spent.
* @since 2.7.0
* @param float $total_spent
*/
public function set_total_spent( $total_spent ) {
$this->_data['total_spent'] = $total_spent;
}
/**
* Set customer's password.
* @since 2.7.0
@ -927,31 +948,6 @@ class WC_Customer extends WC_Legacy_Customer {
$this->_data['is_paying_customer'] = (bool) $is_paying_customer;
}
/*
|--------------------------------------------------------------------------
| Other methods
|--------------------------------------------------------------------------
| Other functions for interacting with customers.
*/
/**
* Is customer outside base country (for tax purposes)?
* @return bool
*/
public function is_customer_outside_base() {
list( $country, $state ) = $this->get_taxable_address();
if ( $country ) {
$default = wc_get_base_location();
if ( $default['country'] !== $country ) {
return true;
}
if ( $default['state'] && $default['state'] !== $state ) {
return true;
}
}
return false;
}
/*
|--------------------------------------------------------------------------
| CRUD methods
@ -1011,80 +1007,35 @@ class WC_Customer extends WC_Legacy_Customer {
public function read( $id ) {
global $wpdb;
if ( $id ) {
// Only continue reading if the customer exists.
$user_object = get_userdata( $id );
if ( empty( $user_object ) || empty( $user_object->ID ) ) {
$this->_data['id'] = 0;
return;
}
$this->_data['id'] = $id;
foreach ( array_keys( $this->_data ) as $key ) {
$meta_value = get_user_meta( $id, $key, true );
if ( $meta_value && is_callable( array( $this, "set_{$key}" ) ) ) {
$this->{"set_{$key}"}( $meta_value );
}
}
$this->set_is_paying_customer( get_user_meta( $id, 'paying_customer', true ) );
$wp_user = new WP_User( $id );
$this->set_email( $wp_user->user_email );
$this->set_username( $wp_user->user_login );
$this->set_date_created( strtotime( $wp_user->user_registered ) );
$this->set_date_modified( get_user_meta( $id, 'last_update', true ) );
$this->set_role( ( ! empty ( $wp_user->roles[0] ) ? $wp_user->roles[0] : 'customer' ) );
// Get info about user's last order
$last_order = $wpdb->get_row( "SELECT id, post_date_gmt
FROM $wpdb->posts AS posts
LEFT JOIN {$wpdb->postmeta} AS meta on posts.ID = meta.post_id
WHERE meta.meta_key = '_customer_user'
AND meta.meta_value = {$id}
AND posts.post_type = 'shop_order'
AND posts.post_status IN ( '" . implode( "','", array_keys( wc_get_order_statuses() ) ) . "' )
ORDER BY posts.ID DESC
" );
$this->set_last_order_id( is_object( $last_order ) ? $last_order->id : null );
$this->set_last_order_date( is_object( $last_order ) ? strtotime( $last_order->post_date_gmt ) : null );
// WC_Customer can't use wc_get_customer_order_count because get_order_types might not be loaded by the time a customer/session is
$count = $wpdb->get_var( "SELECT COUNT(*)
FROM $wpdb->posts as posts
LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
WHERE meta.meta_key = '_customer_user'
AND posts.post_type = 'shop_order'
AND posts.post_status IN ('" . implode( "','", array_keys( wc_get_order_statuses() ) ) . "')
AND meta_value = $id
" );
$spent = $wpdb->get_var( "SELECT SUM(meta2.meta_value)
FROM $wpdb->posts as posts
LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
LEFT JOIN {$wpdb->postmeta} AS meta2 ON posts.ID = meta2.post_id
WHERE meta.meta_key = '_customer_user'
AND meta.meta_value = $id
AND posts.post_type = 'shop_order'
AND posts.post_status IN ( 'wc-completed', 'wc-processing' )
AND meta2.meta_key = '_order_total'
" );
if ( ! $spent ) {
$spent = 0;
}
$this->set_orders_count( $count );
$this->set_total_spent( $spent );
$this->read_meta_data();
// User object is required.
if ( ! $id || ! ( $user_object = get_user_by( 'id', $id ) ) || empty( $user_object->ID ) ) {
$this->set_id( 0 );
return;
}
// Only users on this site should be read.
if ( is_multisite() && ! is_user_member_of_blog( $id ) ) {
$this->set_id( 0 );
return;
}
$this->set_id( $user_object->ID );
foreach ( array_keys( $this->_data ) as $key ) {
$meta_value = get_user_meta( $id, $key, true );
if ( $meta_value && is_callable( array( $this, "set_{$key}" ) ) ) {
$this->{"set_{$key}"}( $meta_value );
}
}
$this->set_is_paying_customer( get_user_meta( $id, 'paying_customer', true ) );
$this->set_email( $user_object->user_email );
$this->set_username( $user_object->user_login );
$this->set_date_created( strtotime( $user_object->user_registered ) );
$this->set_date_modified( get_user_meta( $id, 'last_update', true ) );
$this->set_role( ( ! empty ( $user_object->roles[0] ) ? $user_object->roles[0] : 'customer' ) );
$this->read_meta_data();
unset( $this->_data['password'] ); // password is write only, never ever read it
}

View File

@ -598,7 +598,7 @@ class WC_Form_Handler {
// Load the previous order - Stop if the order does not exist
$order = wc_get_order( absint( $_GET['order_again'] ) );
if ( empty( $order->get_id() ) ) {
if ( ! $order->get_id() ) {
return;
}

View File

@ -275,7 +275,7 @@ function wc_customer_has_capability( $allcaps, $caps, $args ) {
}
$order = wc_get_order( $order_id );
if ( $user_id == $order->get_user_id() || empty( $order->get_user_id() ) ) {
if ( $user_id == $order->get_user_id() || ! $order->get_user_id() ) {
$allcaps['pay_for_order'] = true;
}
break;

View File

@ -2,7 +2,7 @@
Contributors: automattic, mikejolley, jameskoster, claudiosanches, jshreve, coderkevin, woothemes, BFTrick, iCaleb
Tags: ecommerce, e-commerce, store, sales, sell, shop, cart, checkout, downloadable, downloads, paypal, storefront
Requires at least: 4.4
Tested up to: 4.5
Tested up to: 4.6
Stable tag: 2.6.3
License: GPLv3
License URI: https://www.gnu.org/licenses/gpl-3.0.html

View File

@ -1,14 +1,12 @@
<?php
namespace WooCommerce\Tests\API;
/**
* Coupon API Tests
* @package WooCommerce\Tests\API
* @since 2.7.0
*/
class Coupons extends \WC_API_Unit_Test_Case {
class WC_Tests_API_Coupons extends WC_API_Unit_Test_Case {
/** @var \WC_API_Coupons instance */
/** @var WC_API_Coupons instance */
protected $endpoint;
/**
@ -19,7 +17,7 @@ class Coupons extends \WC_API_Unit_Test_Case {
public function setUp() {
parent::setUp();
$this->endpoint = WC()->api->WC_API_Coupons;
$this->coupon = \WC_Helper_Coupon::create_coupon();
$this->coupon = WC_Helper_Coupon::create_coupon();
}
/**
@ -196,7 +194,7 @@ class Coupons extends \WC_API_Unit_Test_Case {
$response = $this->endpoint->create_coupon( $this->get_defaults() );
$this->assertNotWPError( $response );
$this->assertArrayHasKey( 'coupon', $response );
$this->check_get_coupon_response( $response['coupon'], new \WC_Coupon( $response['coupon']['code'] ) );
$this->check_get_coupon_response( $response['coupon'], new WC_Coupon( $response['coupon']['code'] ) );
}
/**
@ -261,7 +259,7 @@ class Coupons extends \WC_API_Unit_Test_Case {
$this->assertNotWPError( $response );
$this->assertArrayHasKey( 'coupon', $response );
$this->check_get_coupon_response( $response['coupon'], new \WC_Coupon( $response['coupon']['code'] ) );
$this->check_get_coupon_response( $response['coupon'], new WC_Coupon( $response['coupon']['code'] ) );
}
/**
@ -301,8 +299,8 @@ class Coupons extends \WC_API_Unit_Test_Case {
$this->assertNotWPError( $response );
$this->assertArrayHasKey( 'coupons', $response );
$this->assertCount( 2, $response['coupons'] );
$this->check_get_coupon_response( $response['coupons'][0], new \WC_Coupon( $response['coupons'][0]['code'] ) );
$this->check_get_coupon_response( $response['coupons'][1], new \WC_Coupon( $response['coupons'][1]['code'] ) );
$this->check_get_coupon_response( $response['coupons'][0], new WC_Coupon( $response['coupons'][0]['code'] ) );
$this->check_get_coupon_response( $response['coupons'][1], new WC_Coupon( $response['coupons'][1]['code'] ) );
}
/**
@ -310,14 +308,14 @@ class Coupons extends \WC_API_Unit_Test_Case {
* @since 2.7.0
*/
public function test_edit_coupon_bulk() {
$coupon_1 = \WC_Helper_Coupon::create_coupon( 'dummycoupon-1-' . time() );
$coupon_1 = WC_Helper_Coupon::create_coupon( 'dummycoupon-1-' . time() );
$test_coupon_data = $this->get_defaults( array( 'description' => rand_str() ) );
$test_coupon_data['coupon']['id'] = $coupon_1->get_id();
$coupons = array( 'coupons' => array( $test_coupon_data['coupon'] ) );
$response = $this->endpoint->bulk( $coupons );
$this->assertNotWPError( $response );
$this->assertArrayHasKey( 'coupons', $response );
$this->check_get_coupon_response( $response['coupons'][0], new \WC_Coupon( $response['coupons'][0]['code'] ) );
$this->check_get_coupon_response( $response['coupons'][0], new WC_Coupon( $response['coupons'][0]['code'] ) );
}
}

View File

@ -1,12 +1,10 @@
<?php
namespace WooCommerce\Tests\Coupon;
/**
* Class CRUD
* @package WooCommerce\Tests\Coupon
*/
class CouponCRUD extends \WC_Unit_Test_Case {
class WC_Tests_CouponCRUD extends WC_Unit_Test_Case {
/**
* Some of our get/setters were renamed. This will return the function
@ -33,7 +31,7 @@ class CouponCRUD extends \WC_Unit_Test_Case {
*/
function test_coupon_create() {
$code = 'coupon-' . time();
$coupon = new \WC_Coupon;
$coupon = new WC_Coupon;
$coupon->set_code( $code );
$coupon->set_description( 'This is a test comment.' );
$coupon->create();
@ -47,7 +45,7 @@ class CouponCRUD extends \WC_Unit_Test_Case {
* @since 2.7.0
*/
function test_coupon_delete() {
$coupon = \WC_Helper_Coupon::create_coupon();
$coupon = WC_Helper_Coupon::create_coupon();
$coupon_id = $coupon->get_id();
$this->assertNotEquals( 0, $coupon_id );
$coupon->delete();
@ -60,7 +58,7 @@ class CouponCRUD extends \WC_Unit_Test_Case {
* @since 2.7.0
*/
function test_coupon_update() {
$coupon = \WC_Helper_Coupon::create_coupon();
$coupon = WC_Helper_Coupon::create_coupon();
$coupon_id = $coupon->get_id();
$this->assertEquals( 'dummycoupon', $coupon->get_code() );
$coupon->set_code( 'dummycoupon2' );
@ -75,14 +73,14 @@ class CouponCRUD extends \WC_Unit_Test_Case {
*/
function test_coupon_read() {
$code = 'coupon-' . time();
$coupon = new \WC_Coupon;
$coupon = new WC_Coupon;
$coupon->set_code( $code );
$coupon->set_description( 'This is a test coupon.' );
$coupon->set_usage_count( 5 );
$coupon->create();
$coupon_id = $coupon->get_id();
$coupon_read = new \WC_Coupon;
$coupon_read = new WC_Coupon;
$coupon_read->read( $coupon_id );
$this->assertEquals( 5, $coupon_read->get_usage_count() );
@ -95,7 +93,7 @@ class CouponCRUD extends \WC_Unit_Test_Case {
* @since 2.7.0
*/
function test_coupon_save() {
$coupon = \WC_Helper_Coupon::create_coupon();
$coupon = WC_Helper_Coupon::create_coupon();
$coupon_id = $coupon->get_id();
$coupon->set_code( 'dummycoupon2' );
$coupon->save();
@ -103,7 +101,7 @@ class CouponCRUD extends \WC_Unit_Test_Case {
$this->assertEquals( 'dummycoupon2', $coupon->get_code() );
$this->assertEquals( $coupon_id, $coupon->get_id() );
$new_coupon = new \WC_Coupon;
$new_coupon = new WC_Coupon;
$new_coupon->set_code( 'dummycoupon3' );
$new_coupon->save();
$new_coupon_id = $new_coupon->get_id();
@ -127,7 +125,7 @@ class CouponCRUD extends \WC_Unit_Test_Case {
);
$this->expected_doing_it_wrong = array_merge( $this->expected_doing_it_wrong, $legacy_keys );
$coupon = \WC_Helper_Coupon::create_coupon();
$coupon = WC_Helper_Coupon::create_coupon();
add_post_meta( $coupon->get_id(), 'test_coupon_field', 'testing', true );
$coupon->read( $coupon->get_id() );
@ -161,7 +159,7 @@ class CouponCRUD extends \WC_Unit_Test_Case {
*/
public function test_read_manual_coupon() {
$code = 'manual_coupon_' . time();
$coupon = new \WC_Coupon( $code );
$coupon = new WC_Coupon( $code );
$coupon->read_manual_coupon( $code, array(
'id' => true,
'type' => 'fixed_cart',
@ -193,7 +191,7 @@ class CouponCRUD extends \WC_Unit_Test_Case {
$legacy_keys = array( 'product_ids', 'exclude_product_ids', 'individual_use', 'free_shipping', 'exclude_sale_items' );
$this->expected_doing_it_wrong = array_merge( $this->expected_doing_it_wrong, $legacy_keys );
$code = 'bc_manual_coupon_' . time();
$coupon = new \WC_Coupon( $code );
$coupon = new WC_Coupon( $code );
$coupon->read_manual_coupon( $code, array(
'id' => true,
'type' => 'fixed_cart',
@ -236,7 +234,7 @@ class CouponCRUD extends \WC_Unit_Test_Case {
'customer_email' => array( 'test@woo.local' ), 'used_by' => array( 1 ),
);
$coupon = new \WC_Coupon;
$coupon = new WC_Coupon;
foreach ( $standard_getters_and_setters as $function => $value ) {
$function = $this->get_function_name( $function );
$coupon->{"set_{$function}"}( $value );
@ -249,7 +247,7 @@ class CouponCRUD extends \WC_Unit_Test_Case {
* @since 2.7.0
*/
public function test_get_custom_fields() {
$coupon = \WC_Helper_Coupon::create_coupon();
$coupon = WC_Helper_Coupon::create_coupon();
$coupon_id = $coupon->get_id();
$meta_value = time() . '-custom-value';
add_post_meta( $coupon_id, 'test_coupon_field', $meta_value, true );
@ -264,7 +262,7 @@ class CouponCRUD extends \WC_Unit_Test_Case {
* @since 2.7.0
*/
public function test_set_custom_fields() {
$coupon = \WC_Helper_Coupon::create_coupon();
$coupon = WC_Helper_Coupon::create_coupon();
$coupon_id = $coupon->get_id();
$meta_value = time() . '-custom-value';
$coupon->add_meta_data( 'my-custom-field', $meta_value, true );

View File

@ -1,11 +1,10 @@
<?php
namespace WooCommerce\Tests\Customer;
/**
* Class CustomerCRUD.
* @package WooCommerce\Tests\Customer
*/
class CustomerCRUD extends \WC_Unit_Test_Case {
class WC_Tests_CustomerCRUD extends WC_Unit_Test_Case {
/**
* Test creating a new customer.
@ -13,12 +12,12 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
*/
public function test_create_customer() {
$username = 'testusername-' . time();
$customer = new \WC_Customer();
$customer = new WC_Customer();
$customer->set_username( 'testusername-' . time() );
$customer->set_password( 'test123' );
$customer->set_email( 'test@woo.local' );
$customer->create();
$wp_user = new \WP_User( $customer->get_id() );
$wp_user = new WP_User( $customer->get_id() );
$this->assertEquals( $username, $customer->get_username() );
$this->assertNotEquals( 0, $customer->get_id() );
@ -30,7 +29,7 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
* @since 2.7.0
*/
public function test_update_customer() {
$customer = \WC_Helper_Customer::create_customer();
$customer = WC_Helper_Customer::create_customer();
$customer_id = $customer->get_id();
$this->assertEquals( 'test@woo.local', $customer->get_email() );
$this->assertEquals( 'Apt 1', $customer->get_billing_address_2() );
@ -39,7 +38,7 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
$customer->set_billing_address_2( 'Apt 5' );
$customer->update();
$customer = new \WC_Customer( $customer_id ); // so we can read fresh copies from the DB
$customer = new WC_Customer( $customer_id ); // so we can read fresh copies from the DB
$this->assertEquals( 'test@wc.local', $customer->get_email() );
$this->assertEquals( 'Justin', $customer->get_first_name() );
$this->assertEquals( 'Apt 5', $customer->get_billing_address_2() );
@ -52,14 +51,14 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
*/
public function test_save_customer() {
// test save() -> Create
$customer = new \WC_Customer();
$customer = new WC_Customer();
$customer->set_username( 'testusername-' . time() );
$customer->set_password( 'test123' );
$customer->set_email( 'test@woo.local' );
$this->assertEquals( 0, $customer->get_id() );
$customer->save();
$customer_id = $customer->get_id();
$wp_user = new \WP_User( $customer->get_id() );
$wp_user = new WP_User( $customer->get_id() );
$this->assertNotEquals( 0, $customer->get_id() );
@ -68,7 +67,7 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
$customer->set_email( 'test@wc.local' );
$customer->save();
$customer = new \WC_Customer( $customer_id );
$customer = new WC_Customer( $customer_id );
$this->assertEquals( 'test@wc.local', $customer->get_email() );
}
@ -77,10 +76,14 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
* @since 2.7.0
*/
public function test_delete_customer() {
$customer = \WC_Helper_Customer::create_customer();
$customer = WC_Helper_Customer::create_customer();
$customer_id = $customer->get_id();
$this->assertNotEquals( 0, $customer->get_id() );
$customer->delete();
$user_object = get_user_by( 'id', $customer_id );
$this->assertFalse( $user_object );
$customer->read( $customer_id );
$this->assertEquals( 0, $customer->get_id() );
}
@ -91,7 +94,7 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
*/
public function test_read_customer() {
$username = 'user-' . time();
$customer = new \WC_Customer();
$customer = new WC_Customer();
$customer->set_username( $username );
$customer->set_email( 'test@woo.local' );
$customer->set_password( 'hunter2' );
@ -99,7 +102,7 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
$customer->set_last_name( 'Bob' );
$customer->create();
$customer_id = $customer->get_id();
$customer_read = new \WC_Customer();
$customer_read = new WC_Customer();
$customer_read->read( $customer_id );
$this->assertEquals( $customer_id, $customer_read->get_id() );
@ -132,7 +135,7 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
$this->expected_doing_it_wrong = array_merge( $this->expected_doing_it_wrong, $legacy_keys );
$customer = \WC_Helper_Customer::create_customer();
$customer = WC_Helper_Customer::create_customer();
$this->assertEquals( $customer->get_id(), $customer->id );
$this->assertEquals( $customer->get_billing_country(), $customer->country );
@ -172,8 +175,7 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
$time = time();
$standard_getters_and_setters = array(
'username' => 'test', 'email' => 'test@woo.local', 'first_name' => 'Bob', 'last_name' => 'tester',
'role' => 'customer', 'last_order_id' => 5, 'last_order_date' => $time, 'orders_count' => 2,
'total_spent' => 10.57, 'date_created' => $time, 'date_modified' => $time, 'billing_postcode' => 11010,
'role' => 'customer', 'date_created' => $time, 'date_modified' => $time, 'billing_postcode' => 11010,
'billing_city' => 'New York', 'billing_address' => '123 Main St.', 'billing_address_1' => '123 Main St.', 'billing_address_2' => 'Apt 2', 'billing_state' => 'NY',
'billing_country' => 'US', 'shipping_state' => 'NY', 'shipping_postcode' => 11011, 'shipping_city' =>
'New York', 'shipping_address' => '123 Main St.', 'shipping_address_1' => '123 Main St.',
@ -181,7 +183,7 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
'is_paying_customer' => true
);
$customer = new \WC_Customer;
$customer = new WC_Customer;
foreach ( $standard_getters_and_setters as $function => $value ) {
$customer->{"set_{$function}"}( $value );
$this->assertEquals( $value, $customer->{"get_{$function}"}(), $function );
@ -193,12 +195,13 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
* @since 2.7.0
*/
public function test_customer_get_last_order_info() {
$customer = \WC_Helper_Customer::create_customer();
$customer = WC_Helper_Customer::create_customer();
$customer_id = $customer->get_id();
$order = \WC_Helper_Order::create_order( $customer_id );
$order = WC_Helper_Order::create_order( $customer_id );
$customer->read( $customer_id );
$this->assertEquals( $order->get_id(), $customer->get_last_order_id() );
$this->assertEquals( $order->get_date_created(), $customer->get_last_order_date() );
$last_order = $customer->get_last_order();
$this->assertEquals( $order->get_id(), $last_order ? $last_order->get_id() : 0 );
$this->assertEquals( $order->get_date_created(), $last_order ? $last_order->get_date_created() : 0 );
$order->delete();
}
@ -206,14 +209,14 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
* Test getting a customer's order count from DB.
* @since 2.7.0
*/
public function test_customer_get_orders_count_read() {
$customer = \WC_Helper_Customer::create_customer();
public function test_customer_get_order_count_read() {
$customer = WC_Helper_Customer::create_customer();
$customer_id = $customer->get_id();
\WC_Helper_Order::create_order( $customer_id );
\WC_Helper_Order::create_order( $customer_id );
\WC_Helper_Order::create_order( $customer_id );
WC_Helper_Order::create_order( $customer_id );
WC_Helper_Order::create_order( $customer_id );
WC_Helper_Order::create_order( $customer_id );
$customer->read( $customer_id );
$this->assertEquals( 3, $customer->get_orders_count() );
$this->assertEquals( 3, $customer->get_order_count() );
}
/**
@ -221,9 +224,9 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
* @since 2.7.0
*/
public function test_customer_get_total_spent_read() {
$customer = \WC_Helper_Customer::create_customer();
$customer = WC_Helper_Customer::create_customer();
$customer_id = $customer->get_id();
$order = \WC_Helper_Order::create_order( $customer_id );
$order = WC_Helper_Order::create_order( $customer_id );
$customer->read( $customer_id );
$this->assertEquals( 0, $customer->get_total_spent() );
$order->update_status( 'wc-completed' );
@ -237,7 +240,7 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
* @since 2.7.0
*/
public function test_customer_get_avatar_url() {
$customer = \WC_Helper_Customer::create_customer();
$customer = WC_Helper_Customer::create_customer();
$this->assertContains( 'gravatar.com/avatar', $customer->get_avatar_url() );
$this->assertContains( md5( 'test@woo.local' ), $customer->get_avatar_url() );
}
@ -247,9 +250,9 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
* @since 2.7.0
*/
public function test_customer_get_date_created_read() {
$customer = \WC_Helper_Customer::create_customer();
$customer = WC_Helper_Customer::create_customer();
$customer_id = $customer->get_id();
$user = new \WP_User( $customer_id );
$user = new WP_User( $customer_id );
$this->assertEquals( strtotime( $user->data->user_registered ), $customer->get_date_created() );
}
@ -258,7 +261,7 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
* @since 2.7.0
*/
public function test_customer_get_date_modified_read() {
$customer = \WC_Helper_Customer::create_customer();
$customer = WC_Helper_Customer::create_customer();
$customer_id = $customer->get_id();
$last = get_user_meta( $customer_id, 'last_update', true );
sleep(1);
@ -275,7 +278,7 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
* @since 2.7.0
*/
public function test_customer_get_taxable_address() {
$customer = \WC_Helper_Customer::create_customer();
$customer = WC_Helper_Customer::create_customer();
$customer_id = $customer->get_id();
$customer->set_shipping_postcode( '11111' );
$customer->set_shipping_city( 'Test' );
@ -309,7 +312,7 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
* @since 2.7.0
*/
public function test_customer_get_downloadable_products() {
$customer = \WC_Helper_Customer::create_customer();
$customer = WC_Helper_Customer::create_customer();
$customer_id = $customer->get_id();
$this->assertEquals( wc_get_customer_available_downloads( $customer_id ), $customer->get_downloadable_products() );
}
@ -319,7 +322,7 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
* @since 2.7.0
*/
public function test_customer_password() {
$customer = \WC_Helper_Customer::create_customer();
$customer = WC_Helper_Customer::create_customer();
$customer_id = $customer->get_id();
$user = get_user_by( 'id', $customer_id );
@ -337,7 +340,7 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
* @since 2.7.0
*/
public function test_customer_set_address_to_base() {
$customer = \WC_Helper_Customer::create_customer();
$customer = WC_Helper_Customer::create_customer();
$customer->set_billing_address_to_base();
$base = wc_get_customer_default_location();
$this->assertEquals( $base['country'], $customer->get_billing_country() );
@ -351,7 +354,7 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
* @since 2.7.0
*/
public function test_customer_set_shipping_address_to_base() {
$customer = \WC_Helper_Customer::create_customer();
$customer = WC_Helper_Customer::create_customer();
$customer->set_shipping_address_to_base();
$base = wc_get_customer_default_location();
$this->assertEquals( $base['country'], $customer->get_shipping_country() );
@ -365,7 +368,7 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
* @since 2.7.0
*/
public function test_customer_set_location() {
$customer = \WC_Helper_Customer::create_customer();
$customer = WC_Helper_Customer::create_customer();
$customer->set_billing_location( 'US', 'OH', '12345', 'Cleveland' );
$this->assertEquals( 'US', $customer->get_billing_country() );
$this->assertEquals( 'OH', $customer->get_billing_state() );
@ -378,7 +381,7 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
* @since 2.7.0
*/
public function test_customer_set_shipping_location() {
$customer = \WC_Helper_Customer::create_customer();
$customer = WC_Helper_Customer::create_customer();
$customer->set_shipping_location( 'US', 'OH', '12345', 'Cleveland' );
$this->assertEquals( 'US', $customer->get_shipping_country() );
$this->assertEquals( 'OH', $customer->get_shipping_state() );
@ -391,7 +394,7 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
* @since 2.7.0
*/
public function test_customer_is_customer_outside_base() {
$customer = \WC_Helper_Customer::create_customer();
$customer = WC_Helper_Customer::create_customer();
$this->assertTrue( $customer->is_customer_outside_base() );
update_option( 'woocommerce_tax_based_on', 'base' );
$customer->set_billing_address_to_base();
@ -403,8 +406,8 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
* @since 2.7.0
*/
public function test_customer_sessions() {
$customer = \WC_Helper_Customer::create_customer();
$session = \WC_Helper_Customer::create_mock_customer(); // set into session....
$customer = WC_Helper_Customer::create_customer();
$session = WC_Helper_Customer::create_mock_customer(); // set into session....
$this->assertEquals( '19123', $session->get_billing_postcode() );
$this->assertEquals( '123 South Street', $session->get_billing_address() );
@ -413,11 +416,11 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
$session->set_billing_address( '124 South Street' );
$session->save_to_session();
$session = new \WC_Customer( 0, true );
$session = new WC_Customer( 0, true );
$session->load_session();
$this->assertEquals( '124 South Street', $session->get_billing_address() );
$session = new \WC_Customer( 0, true );
$session = new WC_Customer( 0, true );
$session->load_session();
$session->set_billing_postcode( '32191' );
$session->save();
@ -432,7 +435,7 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
* @since 2.7.0
*/
public function test_get_meta() {
$customer = \WC_Helper_Customer::create_customer();
$customer = WC_Helper_Customer::create_customer();
$customer_id = $customer->get_id();
$meta_value = time() . '-custom-value';
add_user_meta( $customer_id, 'test_field', $meta_value, true );
@ -446,7 +449,7 @@ class CustomerCRUD extends \WC_Unit_Test_Case {
* @since 2.7.0
*/
public function test_set_meta() {
$customer = \WC_Helper_Customer::create_customer();
$customer = WC_Helper_Customer::create_customer();
$customer_id = $customer->get_id();
$meta_value = time() . '-custom-value';
$customer->add_meta_data( 'my-field', $meta_value, true );

View File

@ -7,7 +7,7 @@
* Author: WooThemes
* Author URI: https://woothemes.com
* Requires at least: 4.4
* Tested up to: 4.5
* Tested up to: 4.6
*
* Text Domain: woocommerce
* Domain Path: /i18n/languages/