2018-12-20 22:35:30 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Reports Customers REST API Test
|
|
|
|
*
|
2020-08-11 19:18:47 +00:00
|
|
|
* @package WooCommerce\Admin\Tests\API
|
2018-12-20 22:35:30 +00:00
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
|
2019-08-05 18:14:25 +00:00
|
|
|
use \Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore as CustomersDataStore;
|
|
|
|
|
2018-12-20 22:35:30 +00:00
|
|
|
/**
|
|
|
|
* Reports Customers REST API Test Class
|
|
|
|
*
|
2020-08-11 19:18:47 +00:00
|
|
|
* @package WooCommerce\Admin\Tests\API
|
2018-12-20 22:35:30 +00:00
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
class WC_Tests_API_Reports_Customers extends WC_REST_Unit_Test_Case {
|
|
|
|
/**
|
|
|
|
* Endpoint.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2019-11-12 18:15:55 +00:00
|
|
|
protected $endpoint = '/wc-analytics/reports/customers';
|
2018-12-20 22:35:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup test reports products data.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->user = $this->factory->user->create(
|
|
|
|
array(
|
|
|
|
'role' => 'administrator',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test route registration.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_register_routes() {
|
|
|
|
$routes = $this->server->get_routes();
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( $this->endpoint, $routes );
|
|
|
|
}
|
|
|
|
|
2018-12-20 23:19:40 +00:00
|
|
|
/**
|
|
|
|
* Asserts the report item schema is correct.
|
|
|
|
*
|
|
|
|
* @param array $schema Item to check schema.
|
|
|
|
*/
|
|
|
|
public function assert_report_item_schema( $schema ) {
|
2019-02-12 19:08:03 +00:00
|
|
|
$this->assertArrayHasKey( 'id', $schema );
|
2018-12-20 23:19:40 +00:00
|
|
|
$this->assertArrayHasKey( 'user_id', $schema );
|
|
|
|
$this->assertArrayHasKey( 'name', $schema );
|
|
|
|
$this->assertArrayHasKey( 'username', $schema );
|
|
|
|
$this->assertArrayHasKey( 'country', $schema );
|
|
|
|
$this->assertArrayHasKey( 'city', $schema );
|
2019-06-19 22:11:24 +00:00
|
|
|
$this->assertArrayHasKey( 'state', $schema );
|
2018-12-20 23:19:40 +00:00
|
|
|
$this->assertArrayHasKey( 'postcode', $schema );
|
|
|
|
$this->assertArrayHasKey( 'date_registered', $schema );
|
|
|
|
$this->assertArrayHasKey( 'date_registered_gmt', $schema );
|
|
|
|
$this->assertArrayHasKey( 'date_last_active', $schema );
|
|
|
|
$this->assertArrayHasKey( 'date_last_active_gmt', $schema );
|
|
|
|
$this->assertArrayHasKey( 'orders_count', $schema );
|
|
|
|
$this->assertArrayHasKey( 'total_spend', $schema );
|
|
|
|
$this->assertArrayHasKey( 'avg_order_value', $schema );
|
|
|
|
}
|
|
|
|
|
2018-12-20 22:35:30 +00:00
|
|
|
/**
|
|
|
|
* Test reports schema.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_reports_schema() {
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
|
|
|
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
$properties = $data['schema']['properties'];
|
|
|
|
|
2019-06-20 19:24:14 +00:00
|
|
|
$this->assertCount( 15, $properties );
|
2018-12-20 23:19:40 +00:00
|
|
|
$this->assert_report_item_schema( $properties );
|
2018-12-20 22:35:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting reports without valid permissions.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_get_reports_without_permission() {
|
|
|
|
wp_set_current_user( 0 );
|
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
|
|
|
$this->assertEquals( 401, $response->get_status() );
|
|
|
|
}
|
|
|
|
|
2019-01-04 01:26:08 +00:00
|
|
|
/**
|
|
|
|
* Test calling update_registered_customer() with a bad user id.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_update_registered_customer_with_bad_user_id() {
|
2019-08-05 18:14:25 +00:00
|
|
|
$result = CustomersDataStore::update_registered_customer( 2 );
|
2019-01-04 01:26:08 +00:00
|
|
|
$this->assertFalse( $result );
|
|
|
|
}
|
|
|
|
|
2019-02-20 02:30:00 +00:00
|
|
|
/**
|
|
|
|
* Test creation of various user roles
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_user_creation() {
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
$admin_id = wp_insert_user(
|
|
|
|
array(
|
|
|
|
'user_login' => 'testadmin',
|
|
|
|
'user_pass' => null,
|
|
|
|
'role' => 'administrator',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Admin user without orders should not be shown.
|
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
|
|
|
$request->set_query_params( array( 'per_page' => 10 ) );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
$headers = $response->get_headers();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertCount( 0, $reports );
|
|
|
|
|
|
|
|
// Creating an order with admin should return the admin.
|
|
|
|
$product = new WC_Product_Simple();
|
|
|
|
$product->set_name( 'Test Product' );
|
|
|
|
$product->set_regular_price( 25 );
|
|
|
|
$product->save();
|
|
|
|
|
|
|
|
$order = WC_Helper_Order::create_order( $admin_id, $product );
|
|
|
|
$order->set_status( 'processing' );
|
|
|
|
$order->set_total( 100 );
|
|
|
|
$order->save();
|
|
|
|
|
|
|
|
WC_Helper_Queue::run_all_pending();
|
|
|
|
|
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
|
|
|
$request->set_query_params( array( 'per_page' => 10 ) );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
$headers = $response->get_headers();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertCount( 1, $reports );
|
|
|
|
$this->assertEquals( $admin_id, $reports[0]['user_id'] );
|
|
|
|
|
|
|
|
// Creating a customer should show up regardless of orders.
|
|
|
|
$customer = WC_Helper_Customer::create_customer( 'customer', 'password', 'customer@example.com' );
|
|
|
|
|
2019-08-21 16:34:17 +00:00
|
|
|
WC_Helper_Queue::run_all_pending();
|
|
|
|
|
2019-02-20 02:30:00 +00:00
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
|
|
|
$request->set_query_params(
|
|
|
|
array(
|
|
|
|
'per_page' => 10,
|
|
|
|
'order' => 'asc',
|
|
|
|
'orderby' => 'username',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
$headers = $response->get_headers();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertCount( 2, $reports );
|
|
|
|
$this->assertEquals( $customer->get_id(), $reports[0]['user_id'] );
|
|
|
|
$this->assertEquals( $admin_id, $reports[1]['user_id'] );
|
|
|
|
}
|
|
|
|
|
2018-12-20 22:35:30 +00:00
|
|
|
/**
|
|
|
|
* Test getting reports.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_get_reports() {
|
2019-02-19 19:35:29 +00:00
|
|
|
global $wpdb;
|
|
|
|
|
2018-12-20 22:35:30 +00:00
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
WC_Helper_Reports::reset_stats_dbs();
|
|
|
|
|
|
|
|
$test_customers = array();
|
|
|
|
|
2019-02-19 19:35:29 +00:00
|
|
|
$customer_names = array( 'Alice', 'Betty', 'Catherine', 'Dan', 'Eric', 'Fred', 'Greg', 'Henry', 'Ivan', 'Justin' );
|
|
|
|
|
2018-12-20 22:35:30 +00:00
|
|
|
// Create 10 test customers.
|
|
|
|
for ( $i = 1; $i <= 10; $i++ ) {
|
2019-03-13 17:14:02 +00:00
|
|
|
$name = $customer_names[ $i - 1 ];
|
|
|
|
$email = 'customer+' . strtolower( $name ) . '@example.com';
|
2019-02-19 19:35:29 +00:00
|
|
|
$customer = WC_Helper_Customer::create_customer( "customer{$i}", 'password', $email );
|
|
|
|
$customer->set_first_name( $name );
|
|
|
|
$customer->save();
|
|
|
|
$test_customers[] = $customer;
|
2018-12-20 22:35:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a test product for use in an order.
|
|
|
|
$product = new WC_Product_Simple();
|
|
|
|
$product->set_name( 'Test Product' );
|
|
|
|
$product->set_regular_price( 25 );
|
|
|
|
$product->save();
|
|
|
|
|
|
|
|
// Place an order for the first test customer.
|
|
|
|
$order = WC_Helper_Order::create_order( $test_customers[0]->get_id(), $product );
|
|
|
|
$order->set_status( 'processing' );
|
|
|
|
$order->set_total( 100 );
|
|
|
|
$order->save();
|
|
|
|
|
2019-01-30 16:59:45 +00:00
|
|
|
WC_Helper_Queue::run_all_pending();
|
|
|
|
|
2019-02-14 23:14:43 +00:00
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
2018-12-20 22:35:30 +00:00
|
|
|
$request->set_query_params(
|
|
|
|
array(
|
|
|
|
'per_page' => 5,
|
|
|
|
'order' => 'asc',
|
|
|
|
'orderby' => 'username',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
$headers = $response->get_headers();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
2018-12-21 01:56:36 +00:00
|
|
|
$this->assertCount( 5, $reports );
|
2018-12-20 22:35:30 +00:00
|
|
|
$this->assertArrayHasKey( 'X-WP-Total', $headers );
|
|
|
|
$this->assertEquals( 10, $headers['X-WP-Total'] );
|
|
|
|
$this->assertArrayHasKey( 'X-WP-TotalPages', $headers );
|
|
|
|
$this->assertEquals( 2, $headers['X-WP-TotalPages'] );
|
|
|
|
$this->assertEquals( $test_customers[0]->get_id(), $reports[0]['user_id'] );
|
|
|
|
$this->assertEquals( 1, $reports[0]['orders_count'] );
|
|
|
|
$this->assertEquals( 100, $reports[0]['total_spend'] );
|
2018-12-20 23:19:40 +00:00
|
|
|
$this->assert_report_item_schema( $reports[0] );
|
2018-12-21 01:56:36 +00:00
|
|
|
|
|
|
|
// Test name parameter (case with no matches).
|
|
|
|
$request->set_query_params(
|
|
|
|
array(
|
2019-02-12 19:06:22 +00:00
|
|
|
'search' => 'Nota Customername',
|
2018-12-21 01:56:36 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertCount( 0, $reports );
|
|
|
|
|
2019-02-19 19:35:29 +00:00
|
|
|
// Test name parameter (partial match).
|
|
|
|
$request->set_query_params(
|
|
|
|
array(
|
|
|
|
'search' => 're',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertCount( 2, $reports );
|
|
|
|
|
|
|
|
// Test email search.
|
|
|
|
$request->set_query_params(
|
|
|
|
array(
|
|
|
|
'search' => 'customer+justin',
|
|
|
|
'searchby' => 'email',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertCount( 1, $reports );
|
|
|
|
|
|
|
|
// Test username search.
|
|
|
|
$request->set_query_params(
|
|
|
|
array(
|
|
|
|
'search' => 'customer1',
|
|
|
|
'searchby' => 'username',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
// customer1 and customer10.
|
|
|
|
$this->assertCount( 2, $reports );
|
|
|
|
|
2018-12-21 01:56:36 +00:00
|
|
|
// Test name and last_order parameters.
|
|
|
|
$request->set_query_params(
|
|
|
|
array(
|
2019-02-19 19:35:29 +00:00
|
|
|
'search' => 'Alice',
|
2020-08-11 19:18:47 +00:00
|
|
|
'last_order_after' => gmdate( 'Y-m-d' ) . 'T00:00:00Z',
|
2018-12-21 01:56:36 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertCount( 1, $reports );
|
2019-02-19 19:35:29 +00:00
|
|
|
|
2018-12-21 01:56:36 +00:00
|
|
|
$this->assertEquals( $test_customers[0]->get_id(), $reports[0]['user_id'] );
|
|
|
|
$this->assertEquals( 1, $reports[0]['orders_count'] );
|
|
|
|
$this->assertEquals( 100, $reports[0]['total_spend'] );
|
2019-02-19 19:35:29 +00:00
|
|
|
|
|
|
|
$customer_id = $wpdb->get_col(
|
|
|
|
$wpdb->prepare(
|
|
|
|
"SELECT customer_id FROM {$wpdb->prefix}wc_customer_lookup WHERE user_id = %d",
|
|
|
|
$reports[0]['user_id']
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test customers param.
|
|
|
|
$request->set_query_params(
|
|
|
|
array(
|
|
|
|
'customers' => $customer_id,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertCount( 1, $reports );
|
|
|
|
$this->assertEquals( $test_customers[0]->get_id(), $reports[0]['user_id'] );
|
2018-12-20 22:35:30 +00:00
|
|
|
}
|
2019-04-09 03:39:39 +00:00
|
|
|
|
|
|
|
/**
|
2019-06-20 03:22:47 +00:00
|
|
|
* Test customer user profile name priority.
|
2019-04-09 03:39:39 +00:00
|
|
|
*/
|
2019-06-20 03:22:47 +00:00
|
|
|
public function test_customer_user_profile_name() {
|
2019-04-09 03:39:39 +00:00
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
|
|
|
$customer = wp_insert_user(
|
|
|
|
array(
|
2019-06-20 03:22:47 +00:00
|
|
|
'first_name' => 'Tyrion',
|
|
|
|
'last_name' => 'Lanister',
|
2019-04-09 03:39:39 +00:00
|
|
|
'user_login' => 'daenerys',
|
|
|
|
'user_pass' => null,
|
|
|
|
'role' => 'customer',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$order = WC_Helper_Order::create_order( $customer );
|
2019-06-20 03:22:47 +00:00
|
|
|
$order->set_billing_first_name( 'Jon' );
|
|
|
|
$order->set_billing_last_name( 'Snow' );
|
2019-04-09 03:39:39 +00:00
|
|
|
$order->set_status( 'completed' );
|
|
|
|
$order->set_total( 100 );
|
|
|
|
$order->save();
|
|
|
|
|
|
|
|
WC_Helper_Queue::run_all_pending();
|
|
|
|
|
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
$headers = $response->get_headers();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertCount( 1, $reports );
|
2019-06-20 03:22:47 +00:00
|
|
|
$this->assertEquals( 'Tyrion Lanister', $reports[0]['name'] );
|
|
|
|
}
|
2019-04-09 03:39:39 +00:00
|
|
|
|
2019-06-20 03:22:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test customer billing name priority.
|
|
|
|
*/
|
|
|
|
public function test_customer_billing_name() {
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
|
|
|
// Test shipping name and empty billing name on a guest account.
|
|
|
|
$order = WC_Helper_Order::create_order( 0 );
|
2019-04-09 03:39:39 +00:00
|
|
|
$order->set_billing_first_name( 'Jon' );
|
|
|
|
$order->set_billing_last_name( 'Snow' );
|
2019-06-20 03:22:47 +00:00
|
|
|
$order->set_shipping_first_name( 'IgnoredFirstName' );
|
|
|
|
$order->set_shipping_last_name( 'IgnoredLastName' );
|
|
|
|
$order->set_status( 'completed' );
|
|
|
|
$order->set_total( 100 );
|
2019-04-09 03:39:39 +00:00
|
|
|
$order->save();
|
|
|
|
|
|
|
|
WC_Helper_Queue::run_all_pending();
|
|
|
|
|
2019-06-20 03:22:47 +00:00
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
2019-04-09 03:39:39 +00:00
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
$headers = $response->get_headers();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertCount( 1, $reports );
|
|
|
|
$this->assertEquals( 'Jon Snow', $reports[0]['name'] );
|
2019-06-20 03:22:47 +00:00
|
|
|
}
|
2019-04-09 03:39:39 +00:00
|
|
|
|
2019-06-20 03:22:47 +00:00
|
|
|
/**
|
|
|
|
* Test customer shipping name priority.
|
|
|
|
*/
|
|
|
|
public function test_customer_shipping_name() {
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
|
|
|
// Test shipping name and empty billing name on a guest account.
|
|
|
|
$order = WC_Helper_Order::create_order( 0 );
|
|
|
|
$order->set_billing_first_name( '' );
|
|
|
|
$order->set_billing_last_name( '' );
|
|
|
|
$order->set_shipping_first_name( 'Daenerys' );
|
|
|
|
$order->set_shipping_last_name( 'Targaryen' );
|
|
|
|
$order->set_status( 'completed' );
|
|
|
|
$order->set_total( 100 );
|
|
|
|
$order->save();
|
2019-04-09 03:39:39 +00:00
|
|
|
|
|
|
|
WC_Helper_Queue::run_all_pending();
|
|
|
|
|
2019-06-20 03:22:47 +00:00
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
2019-04-09 03:39:39 +00:00
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
$headers = $response->get_headers();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertCount( 1, $reports );
|
2019-06-20 03:22:47 +00:00
|
|
|
$this->assertEquals( 'Daenerys Targaryen', $reports[0]['name'] );
|
2019-04-09 03:39:39 +00:00
|
|
|
}
|
2019-06-20 02:39:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that bad order params don't cause PHP errors when retrieving customers.
|
|
|
|
*/
|
|
|
|
public function test_customer_retrieval_from_order_bad_order() {
|
2019-08-05 18:14:25 +00:00
|
|
|
$this->assertFalse( CustomersDataStore::get_existing_customer_id_from_order( false ) );
|
|
|
|
$this->assertFalse( CustomersDataStore::get_or_create_customer_from_order( false ) );
|
2019-06-20 02:39:48 +00:00
|
|
|
}
|
2020-08-13 13:23:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test user deletion.
|
|
|
|
*/
|
|
|
|
public function test_user_deletion() {
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
|
|
|
// Creating a customer should show up regardless of orders.
|
|
|
|
$customer = WC_Helper_Customer::create_customer( 'deleteme', 'password', 'deleteme@example.com' );
|
|
|
|
|
|
|
|
WC_Helper_Queue::run_all_pending();
|
|
|
|
|
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
|
|
|
$request->set_query_params(
|
|
|
|
array(
|
|
|
|
'per_page' => 1,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$customers = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertCount( 1, $customers );
|
|
|
|
$this->assertEquals( $customer->get_id(), $customers[0]['user_id'] );
|
|
|
|
|
|
|
|
// Delete the user associated with the customer.
|
|
|
|
wp_delete_user( $customer->get_id() );
|
|
|
|
|
|
|
|
WC_Helper_Queue::run_all_pending();
|
|
|
|
|
|
|
|
// Verify they are gone.
|
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
|
|
|
$request->set_query_params(
|
|
|
|
array(
|
|
|
|
'per_page' => 1,
|
|
|
|
'customers' => array( $customer->get_id() ),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$customers = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertCount( 0, $customers );
|
|
|
|
}
|
2020-12-03 12:55:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test sync order update with customer info.
|
|
|
|
*/
|
|
|
|
public function test_sync_order_customer() {
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
|
|
|
$order = WC_Helper_Order::create_order( 0 );
|
|
|
|
$order->set_status( 'completed' );
|
|
|
|
$order->set_total( 100 );
|
|
|
|
$order->save();
|
|
|
|
|
|
|
|
WC_Helper_Queue::run_all_pending();
|
|
|
|
|
|
|
|
// update order info.
|
|
|
|
$order->set_billing_city( 'Random' );
|
|
|
|
$order->set_billing_state( 'FL' );
|
|
|
|
$order->set_billing_postcode( '54321' );
|
|
|
|
$order->save();
|
|
|
|
|
|
|
|
WC_Helper_Queue::run_all_pending();
|
|
|
|
|
|
|
|
$result = CustomersDataStore::sync_order_customer( $order->get_id() );
|
|
|
|
|
|
|
|
WC_Helper_Queue::run_all_pending();
|
|
|
|
|
|
|
|
$this->assertTrue( $result );
|
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertTrue( 'Random' === $reports[0]['city'] );
|
|
|
|
$this->assertTrue( 'FL' === $reports[0]['state'] );
|
|
|
|
$this->assertTrue( '54321' === $reports[0]['postcode'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test sync order update with customer latest order info.
|
|
|
|
*/
|
|
|
|
public function test_sync_latest_order_customer() {
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
|
|
|
$order = WC_Helper_Order::create_order( 0 );
|
|
|
|
$order->set_status( 'completed' );
|
|
|
|
$order->set_total( 100 );
|
|
|
|
$order->save();
|
|
|
|
$order2 = WC_Helper_Order::create_order( 0 );
|
|
|
|
$order2->set_status( 'completed' );
|
|
|
|
$order2->set_total( 100 );
|
|
|
|
$order2->save();
|
|
|
|
|
|
|
|
WC_Helper_Queue::run_all_pending();
|
|
|
|
|
|
|
|
$customer_id = CustomersDataStore::get_existing_customer_id_from_order( $order );
|
|
|
|
$customer2_id = CustomersDataStore::get_existing_customer_id_from_order( $order2 );
|
|
|
|
$this->assertEquals( $customer_id, $customer2_id );
|
|
|
|
// update order info.
|
|
|
|
$order->set_billing_city( 'Random' );
|
|
|
|
$order->set_billing_state( 'FL' );
|
|
|
|
$order->set_billing_postcode( '54321' );
|
|
|
|
$order->save();
|
|
|
|
|
|
|
|
WC_Helper_Queue::run_all_pending();
|
|
|
|
|
|
|
|
$result = CustomersDataStore::sync_order_customer( $order->get_id() );
|
|
|
|
|
|
|
|
WC_Helper_Queue::run_all_pending();
|
|
|
|
|
|
|
|
// Didn't update anything.
|
|
|
|
$this->assertEquals( -1, $result );
|
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertFalse( 'Random' === $reports[0]['city'] );
|
|
|
|
$this->assertFalse( 'FL' === $reports[0]['state'] );
|
|
|
|
$this->assertFalse( '54321' === $reports[0]['postcode'] );
|
|
|
|
}
|
2018-12-20 22:35:30 +00:00
|
|
|
}
|