2016-03-08 21:44:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class CustomerCRUD.
|
|
|
|
* @package WooCommerce\Tests\Customer
|
|
|
|
*/
|
2016-08-16 09:50:24 +00:00
|
|
|
class WC_Tests_CustomerCRUD extends WC_Unit_Test_Case {
|
2016-03-08 21:44:28 +00:00
|
|
|
|
2016-03-11 18:28:34 +00:00
|
|
|
/**
|
|
|
|
* Test creating a new customer.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-03-11 18:28:34 +00:00
|
|
|
*/
|
|
|
|
public function test_create_customer() {
|
|
|
|
$username = 'testusername-' . time();
|
2016-08-16 09:50:24 +00:00
|
|
|
$customer = new WC_Customer();
|
2019-11-16 16:09:59 +00:00
|
|
|
$customer->set_username( $username );
|
2016-03-11 18:28:34 +00:00
|
|
|
$customer->set_password( 'test123' );
|
|
|
|
$customer->set_email( 'test@woo.local' );
|
2016-11-14 18:18:08 +00:00
|
|
|
$customer->save();
|
2016-08-16 09:50:24 +00:00
|
|
|
$wp_user = new WP_User( $customer->get_id() );
|
2016-03-11 18:28:34 +00:00
|
|
|
|
|
|
|
$this->assertEquals( $username, $customer->get_username() );
|
|
|
|
$this->assertNotEquals( 0, $customer->get_id() );
|
2017-03-10 04:53:06 +00:00
|
|
|
$this->assertEquals( strtotime( $wp_user->user_registered ), $customer->get_date_created()->getOffsetTimestamp() );
|
2016-03-11 18:28:34 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 21:51:32 +00:00
|
|
|
/**
|
|
|
|
* Test updating a customer.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-03-14 21:51:32 +00:00
|
|
|
*/
|
|
|
|
public function test_update_customer() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$customer = WC_Helper_Customer::create_customer();
|
2016-03-14 21:51:32 +00:00
|
|
|
$customer_id = $customer->get_id();
|
|
|
|
$this->assertEquals( 'test@woo.local', $customer->get_email() );
|
2016-03-17 19:39:29 +00:00
|
|
|
$this->assertEquals( 'Apt 1', $customer->get_billing_address_2() );
|
2016-03-14 21:51:32 +00:00
|
|
|
$customer->set_email( 'test@wc.local' );
|
2016-03-17 19:03:23 +00:00
|
|
|
$customer->set_first_name( 'Justin' );
|
2016-03-17 19:39:29 +00:00
|
|
|
$customer->set_billing_address_2( 'Apt 5' );
|
2016-11-14 18:18:08 +00:00
|
|
|
$customer->save();
|
2016-03-14 21:51:32 +00:00
|
|
|
|
2016-08-16 09:50:24 +00:00
|
|
|
$customer = new WC_Customer( $customer_id ); // so we can read fresh copies from the DB
|
2016-03-14 21:51:32 +00:00
|
|
|
$this->assertEquals( 'test@wc.local', $customer->get_email() );
|
2016-03-17 19:03:23 +00:00
|
|
|
$this->assertEquals( 'Justin', $customer->get_first_name() );
|
2016-03-17 19:39:29 +00:00
|
|
|
$this->assertEquals( 'Apt 5', $customer->get_billing_address_2() );
|
2016-03-14 21:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test saving a customer.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-03-14 21:51:32 +00:00
|
|
|
*/
|
|
|
|
public function test_save_customer() {
|
|
|
|
// test save() -> Create
|
2016-08-16 09:50:24 +00:00
|
|
|
$customer = new WC_Customer();
|
2016-03-14 21:51:32 +00:00
|
|
|
$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();
|
2019-05-01 22:05:00 +00:00
|
|
|
$wp_user = new WP_User( $customer->get_id() );
|
2016-03-14 21:51:32 +00:00
|
|
|
|
|
|
|
$this->assertNotEquals( 0, $customer->get_id() );
|
|
|
|
|
|
|
|
// test save() -> Update
|
|
|
|
$this->assertEquals( 'test@woo.local', $customer->get_email() );
|
|
|
|
$customer->set_email( 'test@wc.local' );
|
|
|
|
$customer->save();
|
|
|
|
|
2016-08-16 09:50:24 +00:00
|
|
|
$customer = new WC_Customer( $customer_id );
|
2016-03-14 21:51:32 +00:00
|
|
|
$this->assertEquals( 'test@wc.local', $customer->get_email() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test deleting a customer.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-03-14 21:51:32 +00:00
|
|
|
*/
|
|
|
|
public function test_delete_customer() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$customer = WC_Helper_Customer::create_customer();
|
2016-03-14 21:51:32 +00:00
|
|
|
$customer_id = $customer->get_id();
|
|
|
|
$this->assertNotEquals( 0, $customer->get_id() );
|
|
|
|
$customer->delete();
|
|
|
|
$this->assertEquals( 0, $customer->get_id() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test reading a customer.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-03-14 21:51:32 +00:00
|
|
|
*/
|
|
|
|
public function test_read_customer() {
|
|
|
|
$username = 'user-' . time();
|
2016-08-16 09:50:24 +00:00
|
|
|
$customer = new WC_Customer();
|
2016-03-14 21:51:32 +00:00
|
|
|
$customer->set_username( $username );
|
|
|
|
$customer->set_email( 'test@woo.local' );
|
|
|
|
$customer->set_password( 'hunter2' );
|
2016-11-14 18:18:08 +00:00
|
|
|
$customer->set_first_name( 'Billy' );
|
2016-03-14 21:51:32 +00:00
|
|
|
$customer->set_last_name( 'Bob' );
|
2017-05-23 23:12:58 +00:00
|
|
|
$customer->set_display_name( 'Billy Bob' );
|
2016-11-14 18:18:08 +00:00
|
|
|
$customer->save();
|
2019-05-01 22:05:00 +00:00
|
|
|
$customer_id = $customer->get_id();
|
2016-11-14 18:18:08 +00:00
|
|
|
$customer_read = new WC_Customer( $customer_id );
|
2016-03-14 21:51:32 +00:00
|
|
|
|
|
|
|
$this->assertEquals( $customer_id, $customer_read->get_id() );
|
|
|
|
$this->assertEquals( 'test@woo.local', $customer_read->get_email() );
|
2016-11-14 18:18:08 +00:00
|
|
|
$this->assertEquals( 'Billy', $customer_read->get_first_name() );
|
2016-03-14 21:51:32 +00:00
|
|
|
$this->assertEquals( 'Bob', $customer_read->get_last_name() );
|
2017-05-23 23:12:58 +00:00
|
|
|
$this->assertEquals( 'Billy Bob', $customer_read->get_display_name() );
|
2016-03-14 21:51:32 +00:00
|
|
|
$this->assertEquals( $username, $customer_read->get_username() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests backwards compat / legacy handling.
|
2016-03-15 18:37:42 +00:00
|
|
|
* @expectedDeprecated WC_Customer::get_default_country
|
|
|
|
* @expectedDeprecated WC_Customer::get_default_state
|
|
|
|
* @expectedDeprecated WC_Customer::is_paying_customer
|
|
|
|
* @expectedDeprecated WC_Customer::calculated_shipping
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-03-14 21:51:32 +00:00
|
|
|
*/
|
|
|
|
public function test_customer_backwards_compat() {
|
2016-03-15 18:37:42 +00:00
|
|
|
// Properties.
|
|
|
|
// Accessing properties directly will throw some wanted deprected notices
|
|
|
|
// So we need to let PHPUnit know we are expecting them and it's fine to continue
|
|
|
|
$legacy_keys = array(
|
2016-08-27 03:04:10 +00:00
|
|
|
'id',
|
|
|
|
'country',
|
|
|
|
'state',
|
|
|
|
'postcode',
|
|
|
|
'city',
|
|
|
|
'address',
|
|
|
|
'address_1',
|
|
|
|
'address_2',
|
|
|
|
'shipping_country',
|
|
|
|
'shipping_state',
|
|
|
|
'shipping_postcode',
|
|
|
|
'shipping_city',
|
|
|
|
'shipping_address',
|
|
|
|
'shipping_address_1',
|
|
|
|
'shipping_address_2',
|
|
|
|
'is_vat_exempt',
|
|
|
|
'calculated_shipping',
|
2016-03-15 18:37:42 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->expected_doing_it_wrong = array_merge( $this->expected_doing_it_wrong, $legacy_keys );
|
2016-03-14 21:51:32 +00:00
|
|
|
|
2016-08-16 09:50:24 +00:00
|
|
|
$customer = WC_Helper_Customer::create_customer();
|
2016-03-14 21:51:32 +00:00
|
|
|
|
2016-03-15 18:37:42 +00:00
|
|
|
$this->assertEquals( $customer->get_id(), $customer->id );
|
2016-03-17 19:39:29 +00:00
|
|
|
$this->assertEquals( $customer->get_billing_country(), $customer->country );
|
|
|
|
$this->assertEquals( $customer->get_billing_state(), $customer->state );
|
|
|
|
$this->assertEquals( $customer->get_billing_postcode(), $customer->postcode );
|
|
|
|
$this->assertEquals( $customer->get_billing_city(), $customer->city );
|
|
|
|
$this->assertEquals( $customer->get_billing_address(), $customer->address );
|
|
|
|
$this->assertEquals( $customer->get_billing_address(), $customer->address_1 );
|
|
|
|
$this->assertEquals( $customer->get_billing_address_2(), $customer->address_2 );
|
2016-03-15 18:37:42 +00:00
|
|
|
$this->assertEquals( $customer->get_shipping_country(), $customer->shipping_country );
|
|
|
|
$this->assertEquals( $customer->get_shipping_state(), $customer->shipping_state );
|
|
|
|
$this->assertEquals( $customer->get_shipping_postcode(), $customer->shipping_postcode );
|
|
|
|
$this->assertEquals( $customer->get_shipping_city(), $customer->shipping_city );
|
|
|
|
$this->assertEquals( $customer->get_shipping_address(), $customer->shipping_address );
|
|
|
|
$this->assertEquals( $customer->get_shipping_address(), $customer->shipping_address_1 );
|
|
|
|
$this->assertEquals( $customer->get_shipping_address_2(), $customer->shipping_address_2 );
|
|
|
|
$this->assertEquals( $customer->get_is_vat_exempt(), $customer->is_vat_exempt );
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->assertEquals( $customer->has_calculated_shipping(), $customer->calculated_shipping );
|
2016-03-15 18:37:42 +00:00
|
|
|
|
|
|
|
// Functions
|
|
|
|
$this->assertEquals( $customer->get_is_vat_exempt(), $customer->is_vat_exempt() );
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->assertEquals( $customer->has_calculated_shipping(), $customer->has_calculated_shipping() );
|
2016-03-15 18:37:42 +00:00
|
|
|
$default = wc_get_customer_default_location();
|
|
|
|
$this->assertEquals( $default['country'], $customer->get_default_country() );
|
|
|
|
$this->assertEquals( $default['state'], $customer->get_default_state() );
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->assertFalse( $customer->has_calculated_shipping() );
|
2016-03-15 18:37:42 +00:00
|
|
|
$customer->calculated_shipping( true );
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->assertTrue( $customer->has_calculated_shipping() );
|
2016-03-15 18:37:42 +00:00
|
|
|
$this->assertEquals( $customer->get_is_paying_customer(), $customer->is_paying_customer() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test generic getters & setters
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-03-15 18:37:42 +00:00
|
|
|
*/
|
|
|
|
public function test_customer_setters_and_getters() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$time = time();
|
2017-03-10 04:53:06 +00:00
|
|
|
$setters = array(
|
2019-05-01 22:05:00 +00:00
|
|
|
'username' => 'test',
|
|
|
|
'email' => 'test@woo.local',
|
|
|
|
'first_name' => 'Bob',
|
|
|
|
'last_name' => 'tester',
|
|
|
|
'display_name' => 'Bob Tester',
|
|
|
|
'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.',
|
|
|
|
'shipping_address_2' => 'Apt 2',
|
|
|
|
'is_vat_exempt' => true,
|
2016-08-27 03:35:20 +00:00
|
|
|
'calculated_shipping' => true,
|
2019-05-01 22:05:00 +00:00
|
|
|
'is_paying_customer' => true,
|
2016-03-15 18:37:42 +00:00
|
|
|
);
|
|
|
|
|
2019-05-01 22:05:00 +00:00
|
|
|
$customer = new WC_Customer();
|
2017-03-10 04:53:06 +00:00
|
|
|
|
|
|
|
foreach ( $setters as $method => $value ) {
|
|
|
|
$customer->{"set_{$method}"}( $value );
|
|
|
|
}
|
|
|
|
|
|
|
|
$getters = array();
|
|
|
|
|
|
|
|
foreach ( $setters as $method => $value ) {
|
|
|
|
$getters[ $method ] = $customer->{"get_{$method}"}();
|
2016-03-15 18:37:42 +00:00
|
|
|
}
|
2017-03-10 04:53:06 +00:00
|
|
|
|
|
|
|
// Get timestamps from date_created and date_modified.
|
2019-05-01 22:05:00 +00:00
|
|
|
$getters['date_created'] = $getters['date_created']->getOffsetTimestamp();
|
2017-03-10 04:53:06 +00:00
|
|
|
$getters['date_modified'] = $getters['date_modified']->getOffsetTimestamp();
|
|
|
|
|
|
|
|
$this->assertEquals( $setters, $getters );
|
2016-03-15 18:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting a customer's last order ID and date
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-03-15 18:37:42 +00:00
|
|
|
*/
|
|
|
|
public function test_customer_get_last_order_info() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$customer = WC_Helper_Customer::create_customer();
|
2016-03-15 18:37:42 +00:00
|
|
|
$customer_id = $customer->get_id();
|
2019-05-01 22:05:00 +00:00
|
|
|
$order = WC_Helper_Order::create_order( $customer_id );
|
|
|
|
$customer = new WC_Customer( $customer_id );
|
|
|
|
$last_order = $customer->get_last_order();
|
2016-08-16 09:04:52 +00:00
|
|
|
$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 );
|
2016-08-15 10:15:03 +00:00
|
|
|
$order->delete();
|
2016-03-14 21:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-15 18:37:42 +00:00
|
|
|
* Test getting a customer's order count from DB.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-03-14 21:51:32 +00:00
|
|
|
*/
|
2016-08-16 09:04:52 +00:00
|
|
|
public function test_customer_get_order_count_read() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$customer = WC_Helper_Customer::create_customer();
|
2016-03-15 18:37:42 +00:00
|
|
|
$customer_id = $customer->get_id();
|
2016-08-16 09:50:24 +00:00
|
|
|
WC_Helper_Order::create_order( $customer_id );
|
|
|
|
WC_Helper_Order::create_order( $customer_id );
|
|
|
|
WC_Helper_Order::create_order( $customer_id );
|
2016-11-14 18:18:08 +00:00
|
|
|
$customer = new WC_Customer( $customer_id );
|
2016-08-16 09:04:52 +00:00
|
|
|
$this->assertEquals( 3, $customer->get_order_count() );
|
2016-03-14 21:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-15 18:37:42 +00:00
|
|
|
* Test getting a customer's total amount spent from DB.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-03-14 21:51:32 +00:00
|
|
|
*/
|
2016-03-15 18:37:42 +00:00
|
|
|
public function test_customer_get_total_spent_read() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$customer = WC_Helper_Customer::create_customer();
|
2016-03-15 18:37:42 +00:00
|
|
|
$customer_id = $customer->get_id();
|
2019-05-01 22:05:00 +00:00
|
|
|
$order = WC_Helper_Order::create_order( $customer_id );
|
|
|
|
$customer = new WC_Customer( $customer_id );
|
2016-03-15 18:37:42 +00:00
|
|
|
$this->assertEquals( 0, $customer->get_total_spent() );
|
|
|
|
$order->update_status( 'wc-completed' );
|
2016-11-14 18:18:08 +00:00
|
|
|
$customer = new WC_Customer( $customer_id );
|
2017-08-10 10:38:09 +00:00
|
|
|
$this->assertEquals( 50, $customer->get_total_spent() );
|
2016-08-15 10:15:03 +00:00
|
|
|
$order->delete();
|
2016-03-14 21:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting a customer's avatar URL.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-03-14 21:51:32 +00:00
|
|
|
*/
|
|
|
|
public function test_customer_get_avatar_url() {
|
2016-08-16 09:50:24 +00:00
|
|
|
$customer = WC_Helper_Customer::create_customer();
|
2016-03-15 18:37:42 +00:00
|
|
|
$this->assertContains( 'gravatar.com/avatar', $customer->get_avatar_url() );
|
|
|
|
$this->assertContains( md5( 'test@woo.local' ), $customer->get_avatar_url() );
|
2016-03-14 21:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-15 18:37:42 +00:00
|
|
|
* Test getting a customer's creation date from DB.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-03-14 21:51:32 +00:00
|
|
|
*/
|
2016-03-15 18:37:42 +00:00
|
|
|
public function test_customer_get_date_created_read() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$customer = WC_Helper_Customer::create_customer();
|
2016-03-15 18:37:42 +00:00
|
|
|
$customer_id = $customer->get_id();
|
2019-05-01 22:05:00 +00:00
|
|
|
$user = new WP_User( $customer_id );
|
2017-03-10 04:53:06 +00:00
|
|
|
$this->assertEquals( strtotime( $user->data->user_registered ), $customer->get_date_created()->getOffsetTimestamp() );
|
2016-03-14 21:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-15 18:37:42 +00:00
|
|
|
* Test getting a customer's modification date from DB.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-03-14 21:51:32 +00:00
|
|
|
*/
|
2016-03-15 18:37:42 +00:00
|
|
|
public function test_customer_get_date_modified_read() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$customer = WC_Helper_Customer::create_customer();
|
2016-03-15 18:37:42 +00:00
|
|
|
$customer_id = $customer->get_id();
|
2019-05-01 22:05:00 +00:00
|
|
|
$last = get_user_meta( $customer_id, 'last_update', true );
|
2016-09-02 01:51:31 +00:00
|
|
|
sleep( 1 );
|
2017-03-10 04:53:06 +00:00
|
|
|
$this->assertEquals( $last, $customer->get_date_modified()->getOffsetTimestamp() );
|
2016-03-17 19:39:29 +00:00
|
|
|
$customer->set_billing_address( '1234 Some St.' );
|
2016-03-15 18:37:42 +00:00
|
|
|
$customer->save();
|
|
|
|
$update = get_user_meta( $customer_id, 'last_update', true );
|
2017-03-10 04:53:06 +00:00
|
|
|
$this->assertEquals( $update, $customer->get_date_modified()->getOffsetTimestamp() );
|
2016-03-15 18:37:42 +00:00
|
|
|
$this->assertNotEquals( $update, $last );
|
2016-03-14 21:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting a customer's taxable address.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-03-14 21:51:32 +00:00
|
|
|
*/
|
|
|
|
public function test_customer_get_taxable_address() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$customer = WC_Helper_Customer::create_customer();
|
2016-03-15 18:37:42 +00:00
|
|
|
$customer_id = $customer->get_id();
|
|
|
|
$customer->set_shipping_postcode( '11111' );
|
|
|
|
$customer->set_shipping_city( 'Test' );
|
|
|
|
$customer->save();
|
2016-11-14 18:18:08 +00:00
|
|
|
$customer = new WC_Customer( $customer_id );
|
2016-03-14 21:51:32 +00:00
|
|
|
|
2016-03-15 18:37:42 +00:00
|
|
|
update_option( 'woocommerce_tax_based_on', 'shipping' );
|
|
|
|
$taxable = $customer->get_taxable_address();
|
|
|
|
$this->assertEquals( 'US', $taxable[0] );
|
|
|
|
$this->assertEquals( 'PA', $taxable[1] );
|
|
|
|
$this->assertEquals( '11111', $taxable[2] );
|
|
|
|
$this->assertEquals( 'Test', $taxable[3] );
|
2016-03-14 21:51:32 +00:00
|
|
|
|
2016-03-15 18:37:42 +00:00
|
|
|
update_option( 'woocommerce_tax_based_on', 'billing' );
|
|
|
|
$taxable = $customer->get_taxable_address();
|
|
|
|
$this->assertEquals( 'US', $taxable[0] );
|
|
|
|
$this->assertEquals( 'PA', $taxable[1] );
|
|
|
|
$this->assertEquals( '19123', $taxable[2] );
|
|
|
|
$this->assertEquals( 'Philadelphia', $taxable[3] );
|
2016-03-14 21:51:32 +00:00
|
|
|
|
2016-03-15 18:37:42 +00:00
|
|
|
update_option( 'woocommerce_tax_based_on', 'base' );
|
|
|
|
$taxable = $customer->get_taxable_address();
|
|
|
|
$this->assertEquals( WC()->countries->get_base_country(), $taxable[0] );
|
|
|
|
$this->assertEquals( WC()->countries->get_base_state(), $taxable[1] );
|
|
|
|
$this->assertEquals( WC()->countries->get_base_postcode(), $taxable[2] );
|
|
|
|
$this->assertEquals( WC()->countries->get_base_city(), $taxable[3] );
|
2016-03-14 21:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-15 18:37:42 +00:00
|
|
|
* Test getting a customer's downloadable products.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-03-14 21:51:32 +00:00
|
|
|
*/
|
2016-03-15 18:37:42 +00:00
|
|
|
public function test_customer_get_downloadable_products() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$customer = WC_Helper_Customer::create_customer();
|
2016-03-15 18:37:42 +00:00
|
|
|
$customer_id = $customer->get_id();
|
|
|
|
$this->assertEquals( wc_get_customer_available_downloads( $customer_id ), $customer->get_downloadable_products() );
|
2016-03-14 21:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-15 18:37:42 +00:00
|
|
|
* Test setting a password on update - making sure it actually changes the users password.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-03-14 21:51:32 +00:00
|
|
|
*/
|
2016-03-15 18:37:42 +00:00
|
|
|
public function test_customer_password() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$customer = WC_Helper_Customer::create_customer();
|
2016-03-15 18:37:42 +00:00
|
|
|
$customer_id = $customer->get_id();
|
2016-03-14 21:51:32 +00:00
|
|
|
|
2016-03-15 18:37:42 +00:00
|
|
|
$user = get_user_by( 'id', $customer_id );
|
|
|
|
$this->assertTrue( wp_check_password( 'hunter2', $user->data->user_pass, $user->ID ) );
|
2016-03-14 21:51:32 +00:00
|
|
|
|
2016-03-15 18:37:42 +00:00
|
|
|
$customer->set_password( 'hunter3' );
|
|
|
|
$customer->save();
|
2016-03-14 21:51:32 +00:00
|
|
|
|
2016-03-15 18:37:42 +00:00
|
|
|
$user = get_user_by( 'id', $customer_id );
|
|
|
|
$this->assertTrue( wp_check_password( 'hunter3', $user->data->user_pass, $user->ID ) );
|
2016-03-14 21:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test setting a customer's address to the base address.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-03-14 21:51:32 +00:00
|
|
|
*/
|
|
|
|
public function test_customer_set_address_to_base() {
|
2016-08-16 09:50:24 +00:00
|
|
|
$customer = WC_Helper_Customer::create_customer();
|
2016-03-17 19:39:29 +00:00
|
|
|
$customer->set_billing_address_to_base();
|
2016-03-15 18:37:42 +00:00
|
|
|
$base = wc_get_customer_default_location();
|
2016-03-17 19:39:29 +00:00
|
|
|
$this->assertEquals( $base['country'], $customer->get_billing_country() );
|
|
|
|
$this->assertEquals( $base['state'], $customer->get_billing_state() );
|
|
|
|
$this->assertEmpty( $customer->get_billing_postcode() );
|
|
|
|
$this->assertEmpty( $customer->get_billing_city() );
|
2016-03-14 21:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test setting a customer's shipping address to the base address.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-03-14 21:51:32 +00:00
|
|
|
*/
|
2016-03-15 18:37:42 +00:00
|
|
|
public function test_customer_set_shipping_address_to_base() {
|
2016-08-16 09:50:24 +00:00
|
|
|
$customer = WC_Helper_Customer::create_customer();
|
2016-03-15 18:37:42 +00:00
|
|
|
$customer->set_shipping_address_to_base();
|
|
|
|
$base = wc_get_customer_default_location();
|
|
|
|
$this->assertEquals( $base['country'], $customer->get_shipping_country() );
|
|
|
|
$this->assertEquals( $base['state'], $customer->get_shipping_state() );
|
|
|
|
$this->assertEmpty( $customer->get_shipping_postcode() );
|
|
|
|
$this->assertEmpty( $customer->get_shipping_city() );
|
2016-03-14 21:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test setting a customer's location (multiple address fields at once)
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-03-14 21:51:32 +00:00
|
|
|
*/
|
|
|
|
public function test_customer_set_location() {
|
2016-08-16 09:50:24 +00:00
|
|
|
$customer = WC_Helper_Customer::create_customer();
|
2016-03-17 19:39:29 +00:00
|
|
|
$customer->set_billing_location( 'US', 'OH', '12345', 'Cleveland' );
|
|
|
|
$this->assertEquals( 'US', $customer->get_billing_country() );
|
|
|
|
$this->assertEquals( 'OH', $customer->get_billing_state() );
|
|
|
|
$this->assertEquals( '12345', $customer->get_billing_postcode() );
|
|
|
|
$this->assertEquals( 'Cleveland', $customer->get_billing_city() );
|
2016-03-14 21:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test setting a customer's shipping location (multiple address fields at once)
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-03-14 21:51:32 +00:00
|
|
|
*/
|
|
|
|
public function test_customer_set_shipping_location() {
|
2016-08-16 09:50:24 +00:00
|
|
|
$customer = WC_Helper_Customer::create_customer();
|
2016-03-15 18:37:42 +00:00
|
|
|
$customer->set_shipping_location( 'US', 'OH', '12345', 'Cleveland' );
|
|
|
|
$this->assertEquals( 'US', $customer->get_shipping_country() );
|
|
|
|
$this->assertEquals( 'OH', $customer->get_shipping_state() );
|
|
|
|
$this->assertEquals( '12345', $customer->get_shipping_postcode() );
|
|
|
|
$this->assertEquals( 'Cleveland', $customer->get_shipping_city() );
|
2016-03-14 21:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test is_customer_outside_base.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-03-14 21:51:32 +00:00
|
|
|
*/
|
|
|
|
public function test_customer_is_customer_outside_base() {
|
2016-08-16 09:50:24 +00:00
|
|
|
$customer = WC_Helper_Customer::create_customer();
|
2016-03-15 18:37:42 +00:00
|
|
|
$this->assertTrue( $customer->is_customer_outside_base() );
|
|
|
|
update_option( 'woocommerce_tax_based_on', 'base' );
|
2016-03-17 19:39:29 +00:00
|
|
|
$customer->set_billing_address_to_base();
|
2016-03-15 18:37:42 +00:00
|
|
|
$this->assertFalse( $customer->is_customer_outside_base() );
|
2016-03-14 21:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test WC_Customer's session handling code.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-03-14 21:51:32 +00:00
|
|
|
*/
|
|
|
|
public function test_customer_sessions() {
|
2017-12-21 19:00:55 +00:00
|
|
|
$session = WC_Helper_Customer::create_mock_customer(); // set into session....
|
2016-03-15 18:37:42 +00:00
|
|
|
|
2016-03-17 19:39:29 +00:00
|
|
|
$this->assertEquals( '19123', $session->get_billing_postcode() );
|
|
|
|
$this->assertEquals( '123 South Street', $session->get_billing_address() );
|
|
|
|
$this->assertEquals( 'Philadelphia', $session->get_billing_city() );
|
2016-03-15 18:37:42 +00:00
|
|
|
|
2016-03-17 19:39:29 +00:00
|
|
|
$session->set_billing_address( '124 South Street' );
|
2016-11-14 18:18:08 +00:00
|
|
|
$session->save();
|
2016-03-14 21:51:32 +00:00
|
|
|
|
2016-08-16 09:50:24 +00:00
|
|
|
$session = new WC_Customer( 0, true );
|
2016-03-17 19:39:29 +00:00
|
|
|
$this->assertEquals( '124 South Street', $session->get_billing_address() );
|
2016-08-04 18:39:34 +00:00
|
|
|
|
2016-08-16 09:50:24 +00:00
|
|
|
$session = new WC_Customer( 0, true );
|
2016-08-04 18:39:34 +00:00
|
|
|
$session->set_billing_postcode( '32191' );
|
|
|
|
$session->save();
|
|
|
|
|
|
|
|
// should still be session ID, not a created row, since we are working with guests/sessions
|
2016-08-15 15:53:48 +00:00
|
|
|
$this->assertFalse( $session->get_id() > 0 );
|
2019-05-01 22:05:00 +00:00
|
|
|
$this->assertEquals( '32191', $session->get_billing_postcode() );
|
2016-03-14 21:51:32 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 19:03:23 +00:00
|
|
|
/**
|
|
|
|
* Test getting meta.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-03-17 19:03:23 +00:00
|
|
|
*/
|
|
|
|
public function test_get_meta() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$customer = WC_Helper_Customer::create_customer();
|
|
|
|
$customer_id = $customer->get_id();
|
|
|
|
$meta_value = time() . '-custom-value';
|
2016-03-17 19:03:23 +00:00
|
|
|
add_user_meta( $customer_id, 'test_field', $meta_value, true );
|
2016-11-14 18:18:08 +00:00
|
|
|
$customer = new WC_Customer( $customer_id );
|
2019-05-01 22:05:00 +00:00
|
|
|
$fields = $customer->get_meta_data();
|
2016-09-02 01:33:57 +00:00
|
|
|
$this->assertEquals( $meta_value, $customer->get_meta( 'test_field' ) );
|
2016-03-17 19:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test setting meta.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-03-17 19:03:23 +00:00
|
|
|
*/
|
|
|
|
public function test_set_meta() {
|
2019-05-01 22:05:00 +00:00
|
|
|
$customer = WC_Helper_Customer::create_customer();
|
|
|
|
$customer_id = $customer->get_id();
|
|
|
|
$meta_value = time() . '-custom-value';
|
2016-03-17 19:03:23 +00:00
|
|
|
$customer->add_meta_data( 'my-field', $meta_value, true );
|
|
|
|
$customer->save();
|
2016-11-14 18:18:08 +00:00
|
|
|
$customer = new WC_Customer( $customer_id );
|
2016-03-17 19:03:23 +00:00
|
|
|
$this->assertEquals( $meta_value, $customer->get_meta( 'my-field' ) );
|
|
|
|
}
|
2016-03-08 21:44:28 +00:00
|
|
|
}
|