2019-04-11 10:50:12 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Reports Import REST API Test
|
|
|
|
*
|
2020-08-11 19:18:47 +00:00
|
|
|
* @package WooCommerce\Admin\Tests\API
|
2019-04-11 10:50:12 +00:00
|
|
|
*/
|
|
|
|
|
2019-08-12 21:52:09 +00:00
|
|
|
use Automattic\WooCommerce\Admin\ReportsSync;
|
2019-08-01 22:06:00 +00:00
|
|
|
|
2019-04-11 10:50:12 +00:00
|
|
|
/**
|
|
|
|
* Reports Import REST API Test Class
|
|
|
|
*
|
2020-08-11 19:18:47 +00:00
|
|
|
* @package WooCommerce\Admin\Tests\API
|
2019-04-11 10:50:12 +00:00
|
|
|
*/
|
|
|
|
class WC_Tests_API_Reports_Import extends WC_REST_Unit_Test_Case {
|
|
|
|
/**
|
|
|
|
* Endpoint.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2019-11-12 18:15:55 +00:00
|
|
|
protected $endpoint = '/wc-analytics/reports/import';
|
2019-04-11 10:50:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup test reports products data.
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->user = $this->factory->user->create(
|
|
|
|
array(
|
|
|
|
'role' => 'administrator',
|
|
|
|
)
|
|
|
|
);
|
2019-04-30 15:46:14 +00:00
|
|
|
|
|
|
|
$this->customer = $this->factory->user->create(
|
|
|
|
array(
|
|
|
|
'first_name' => 'Steve',
|
|
|
|
'last_name' => 'User',
|
|
|
|
'role' => 'customer',
|
|
|
|
)
|
|
|
|
);
|
2019-04-11 10:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test route registration.
|
|
|
|
*/
|
|
|
|
public function test_register_routes() {
|
|
|
|
$routes = $this->server->get_routes();
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( $this->endpoint, $routes );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asserts the report item schema is correct.
|
|
|
|
*
|
|
|
|
* @param array $schema Item to check schema.
|
|
|
|
*/
|
|
|
|
public function assert_report_item_schema( $schema ) {
|
|
|
|
$this->assertArrayHasKey( 'status', $schema );
|
|
|
|
$this->assertArrayHasKey( 'message', $schema );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test reports schema.
|
|
|
|
*/
|
|
|
|
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'];
|
|
|
|
|
|
|
|
$this->assertCount( 2, $properties );
|
|
|
|
$this->assert_report_item_schema( $properties );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting reports without valid permissions.
|
|
|
|
*/
|
|
|
|
public function test_get_reports_without_permission() {
|
|
|
|
wp_set_current_user( 0 );
|
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'POST', $this->endpoint ) );
|
|
|
|
$this->assertEquals( 401, $response->get_status() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the import paramaters.
|
|
|
|
*/
|
|
|
|
public function test_import_params() {
|
|
|
|
global $wpdb;
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
|
|
|
// Populate all of the data.
|
|
|
|
$product = new WC_Product_Simple();
|
|
|
|
$product->set_name( 'Test Product' );
|
|
|
|
$product->set_regular_price( 25 );
|
|
|
|
$product->save();
|
|
|
|
|
2019-06-20 03:22:47 +00:00
|
|
|
$order_1 = WC_Helper_Order::create_order( $this->customer, $product );
|
2019-04-11 10:50:12 +00:00
|
|
|
$order_1->set_status( 'completed' );
|
|
|
|
$order_1->set_date_created( time() - ( 3 * DAY_IN_SECONDS ) );
|
|
|
|
$order_1->save();
|
2019-06-20 03:22:47 +00:00
|
|
|
$order_2 = WC_Helper_Order::create_order( $this->customer, $product );
|
2019-04-11 10:50:12 +00:00
|
|
|
$order_2->set_total( 100 );
|
|
|
|
$order_2->set_status( 'completed' );
|
|
|
|
$order_2->save();
|
|
|
|
|
|
|
|
// Delete order stats so we can test import API.
|
2020-03-12 13:46:13 +00:00
|
|
|
WC_Helper_Queue::cancel_all_pending();
|
2019-04-11 10:50:12 +00:00
|
|
|
$wpdb->query( "DELETE FROM {$wpdb->prefix}wc_order_stats" );
|
|
|
|
|
|
|
|
// Use the days param to only process orders in the last day.
|
|
|
|
$request = new WP_REST_Request( 'POST', $this->endpoint );
|
|
|
|
$request->set_query_params( array( 'days' => '1' ) );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$report = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( 'success', $report['status'] );
|
|
|
|
|
|
|
|
WC_Helper_Queue::run_all_pending();
|
|
|
|
|
2019-11-12 18:15:55 +00:00
|
|
|
$request = new WP_REST_Request( 'GET', '/wc-analytics/reports/customers' );
|
2019-04-30 15:46:14 +00:00
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
2019-06-20 03:22:47 +00:00
|
|
|
$this->assertCount( 1, $reports );
|
2019-04-30 15:46:14 +00:00
|
|
|
$this->assertEquals( $this->customer, $reports[0]['user_id'] );
|
|
|
|
|
2019-11-12 18:15:55 +00:00
|
|
|
$request = new WP_REST_Request( 'GET', '/wc-analytics/reports/orders' );
|
2019-04-11 10:50:12 +00:00
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertCount( 1, $reports );
|
|
|
|
$this->assertEquals( $order_2->get_id(), $reports[0]['order_id'] );
|
|
|
|
|
2019-04-30 15:46:14 +00:00
|
|
|
// Use the skip existing params to skip processing customers/orders.
|
2019-04-11 10:50:12 +00:00
|
|
|
// Compare against order status to make sure previously imported order was skipped.
|
|
|
|
$order_2->set_status( 'processing' );
|
|
|
|
$order_2->save();
|
|
|
|
|
2019-04-30 15:46:14 +00:00
|
|
|
// Compare against name to make sure previously imported customer was skipped.
|
|
|
|
wp_update_user(
|
|
|
|
array(
|
|
|
|
'ID' => $this->customer,
|
|
|
|
'first_name' => 'Changed',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2019-04-11 10:50:12 +00:00
|
|
|
// Delete scheduled actions to avoid default order processing.
|
2020-03-12 13:46:13 +00:00
|
|
|
WC_Helper_Queue::cancel_all_pending();
|
2019-04-11 10:50:12 +00:00
|
|
|
|
|
|
|
$request = new WP_REST_Request( 'POST', $this->endpoint );
|
|
|
|
$request->set_query_params( array( 'skip_existing' => '1' ) );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$report = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( 'success', $report['status'] );
|
|
|
|
|
|
|
|
WC_Helper_Queue::run_all_pending();
|
|
|
|
|
2019-11-12 18:15:55 +00:00
|
|
|
$request = new WP_REST_Request( 'GET', '/wc-analytics/reports/customers' );
|
2019-04-30 15:46:14 +00:00
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
2019-06-20 03:22:47 +00:00
|
|
|
$this->assertCount( 1, $reports );
|
2019-04-30 15:46:14 +00:00
|
|
|
$this->assertEquals( 'Steve User', $reports[0]['name'] );
|
|
|
|
|
2019-11-12 18:15:55 +00:00
|
|
|
$request = new WP_REST_Request( 'GET', '/wc-analytics/reports/orders' );
|
2019-04-12 06:30:48 +00:00
|
|
|
$request->set_query_params( array( 'per_page' => 5 ) );
|
2019-04-11 10:50:12 +00:00
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertCount( 2, $reports );
|
|
|
|
$this->assertEquals( 'completed', $reports[0]['status'] );
|
|
|
|
}
|
|
|
|
|
2019-04-12 08:42:25 +00:00
|
|
|
/**
|
|
|
|
* Test cancelling import actions.
|
|
|
|
*/
|
|
|
|
public function test_cancel_import() {
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
|
|
|
// Populate all of the data.
|
|
|
|
$product = new WC_Product_Simple();
|
|
|
|
$product->set_name( 'Test Product' );
|
|
|
|
$product->set_regular_price( 25 );
|
|
|
|
$product->save();
|
|
|
|
|
|
|
|
$order = WC_Helper_Order::create_order( 1, $product );
|
|
|
|
$order->set_status( 'completed' );
|
|
|
|
$order->set_date_created( time() - ( 3 * DAY_IN_SECONDS ) );
|
|
|
|
$order->save();
|
|
|
|
|
2019-04-30 16:38:07 +00:00
|
|
|
// Verify there are actions to cancel.
|
|
|
|
$pending_actions = WC_Helper_Queue::get_all_pending();
|
2022-03-10 22:39:59 +00:00
|
|
|
$pending_hooks = array_map(
|
|
|
|
function ( $action ) {
|
|
|
|
return $action->get_hook();
|
|
|
|
},
|
|
|
|
$pending_actions
|
|
|
|
);
|
|
|
|
$this->assertContains( 'wc-admin_import_orders', $pending_hooks );
|
|
|
|
$this->assertContains( 'wc-admin_import_customers', $pending_hooks );
|
2019-04-30 16:38:07 +00:00
|
|
|
|
2019-04-12 08:42:25 +00:00
|
|
|
// Cancel outstanding actions.
|
|
|
|
$request = new WP_REST_Request( 'POST', $this->endpoint . '/cancel' );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$report = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( 'success', $report['status'] );
|
|
|
|
|
2019-04-30 16:38:07 +00:00
|
|
|
// Verify there are no pending actions.
|
|
|
|
$pending_actions = WC_Helper_Queue::get_all_pending();
|
2022-03-10 22:39:59 +00:00
|
|
|
$pending_hooks = array_map(
|
|
|
|
function ( $action ) {
|
|
|
|
return $action->get_hook();
|
|
|
|
},
|
|
|
|
$pending_actions
|
|
|
|
);
|
|
|
|
$this->assertNotContains( 'wc-admin_import_orders', $pending_hooks );
|
|
|
|
$this->assertNotContains( 'wc-admin_import_customers', $pending_hooks );
|
2019-04-12 08:42:25 +00:00
|
|
|
}
|
2019-04-15 08:21:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test import deletion.
|
|
|
|
*/
|
|
|
|
public function test_delete_stats() {
|
|
|
|
global $wpdb;
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
|
|
|
// Populate all of the data.
|
|
|
|
$product = new WC_Product_Simple();
|
|
|
|
$product->set_name( 'Test Product' );
|
|
|
|
$product->set_regular_price( 25 );
|
|
|
|
$product->save();
|
|
|
|
|
|
|
|
for ( $i = 0; $i < 25; $i++ ) {
|
|
|
|
$order = WC_Helper_Order::create_order( 1, $product );
|
|
|
|
$order->set_status( 'completed' );
|
|
|
|
$order->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that stats exist before deleting.
|
|
|
|
WC_Helper_Queue::run_all_pending();
|
|
|
|
|
2019-11-12 18:15:55 +00:00
|
|
|
$request = new WP_REST_Request( 'GET', '/wc-analytics/reports/orders' );
|
2019-04-15 08:21:14 +00:00
|
|
|
$request->set_query_params( array( 'per_page' => 25 ) );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertCount( 25, $reports );
|
|
|
|
|
2019-11-12 18:15:55 +00:00
|
|
|
$request = new WP_REST_Request( 'GET', '/wc-analytics/reports/customers' );
|
2019-04-15 08:21:14 +00:00
|
|
|
$request->set_query_params( array( 'per_page' => 25 ) );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertCount( 1, $reports );
|
|
|
|
|
|
|
|
// Delete all stats.
|
|
|
|
$request = new WP_REST_Request( 'POST', $this->endpoint . '/delete' );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$report = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( 'success', $report['status'] );
|
|
|
|
|
|
|
|
WC_Helper_Queue::run_all_pending();
|
|
|
|
|
|
|
|
// Check that stats have been deleted.
|
2019-11-12 18:15:55 +00:00
|
|
|
$request = new WP_REST_Request( 'GET', '/wc-analytics/reports/orders' );
|
2019-04-15 08:21:14 +00:00
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertCount( 0, $reports );
|
|
|
|
|
2019-11-12 18:15:55 +00:00
|
|
|
$request = new WP_REST_Request( 'GET', '/wc-analytics/reports/customers' );
|
2019-04-15 08:21:14 +00:00
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertCount( 0, $reports );
|
|
|
|
}
|
2019-05-13 02:30:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test import status and totals query.
|
|
|
|
*/
|
|
|
|
public function test_import_status() {
|
|
|
|
// Delete any pending actions that weren't fully run.
|
2019-08-12 21:52:09 +00:00
|
|
|
ReportsSync::clear_queued_actions();
|
2019-05-13 02:30:07 +00:00
|
|
|
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
|
|
|
// Populate all of the data.
|
|
|
|
$product = new WC_Product_Simple();
|
|
|
|
$product->set_name( 'Test Product' );
|
|
|
|
$product->set_regular_price( 25 );
|
|
|
|
$product->save();
|
|
|
|
|
2019-05-20 18:05:14 +00:00
|
|
|
// Create 5 completed orders.
|
2019-05-13 02:30:07 +00:00
|
|
|
for ( $i = 0; $i < 5; $i++ ) {
|
2019-06-20 03:22:47 +00:00
|
|
|
$order = WC_Helper_Order::create_order( $this->customer, $product );
|
2019-05-13 02:30:07 +00:00
|
|
|
$order->set_status( 'completed' );
|
|
|
|
$order->set_date_created( time() - ( ( $i + 1 ) * DAY_IN_SECONDS ) );
|
|
|
|
$order->save();
|
|
|
|
}
|
|
|
|
|
2019-05-20 18:05:14 +00:00
|
|
|
// Trash one test order - excludes it from totals.
|
|
|
|
$order->set_status( 'trash' );
|
|
|
|
$order->save();
|
|
|
|
|
|
|
|
// Create 1 draft order - to be excluded from totals.
|
2019-06-20 03:22:47 +00:00
|
|
|
$order = WC_Helper_Order::create_order( $this->customer, $product );
|
2019-05-20 18:05:14 +00:00
|
|
|
$order->set_date_created( time() - ( 5 * DAY_IN_SECONDS ) );
|
|
|
|
$order->save();
|
|
|
|
wp_update_post(
|
|
|
|
array(
|
|
|
|
'ID' => $order->get_id(),
|
|
|
|
'post_status' => 'auto-draft',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2019-05-13 02:30:07 +00:00
|
|
|
// Test totals and total params.
|
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint . '/totals' );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$report = $response->get_data();
|
|
|
|
$user_query = new WP_User_Query(
|
|
|
|
array(
|
2019-06-11 12:47:53 +00:00
|
|
|
'fields' => 'ID',
|
|
|
|
'number' => 1,
|
|
|
|
'role__in' => array( 'customer' ),
|
2019-05-13 02:30:07 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
2019-05-20 18:05:14 +00:00
|
|
|
$this->assertEquals( 4, $report['orders'] );
|
2019-05-13 02:30:07 +00:00
|
|
|
$this->assertEquals( $user_query->get_total(), $report['customers'] );
|
|
|
|
|
|
|
|
// Test totals with days param.
|
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint . '/totals' );
|
|
|
|
$request->set_query_params( array( 'days' => 2 ) );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$report = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( 2, $report['orders'] );
|
|
|
|
|
2019-06-13 15:32:58 +00:00
|
|
|
// Cancel any pending imports, import, and check import status.
|
|
|
|
$request = new WP_REST_Request( 'POST', $this->endpoint . '/cancel' );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$report = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( 'success', $report['status'] );
|
|
|
|
|
2019-05-13 02:30:07 +00:00
|
|
|
$request = new WP_REST_Request( 'POST', $this->endpoint );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$report = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( 'success', $report['status'] );
|
|
|
|
|
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint . '/status' );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$report = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( true, $report['is_importing'] );
|
2020-01-02 16:00:37 +00:00
|
|
|
$this->assertEquals( 0, $report['customers']['imported'] );
|
|
|
|
$this->assertEquals( 1, $report['customers']['total'] );
|
|
|
|
$this->assertEquals( 0, $report['orders']['imported'] );
|
|
|
|
$this->assertEquals( 4, $report['orders']['total'] );
|
2019-05-13 02:30:07 +00:00
|
|
|
|
2019-08-21 23:39:09 +00:00
|
|
|
WC_Helper_Queue::run_all_pending();
|
2019-05-13 02:30:07 +00:00
|
|
|
|
|
|
|
// Test import status after processing.
|
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint . '/status' );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$report = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( false, $report['is_importing'] );
|
2020-01-02 16:00:37 +00:00
|
|
|
$this->assertEquals( 1, $report['customers']['imported'] );
|
|
|
|
$this->assertEquals( 1, $report['customers']['total'] );
|
|
|
|
$this->assertEquals( 4, $report['orders']['imported'] );
|
|
|
|
$this->assertEquals( 4, $report['orders']['total'] );
|
2019-05-13 02:30:07 +00:00
|
|
|
|
|
|
|
// Test totals with skip existing param.
|
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint . '/totals' );
|
|
|
|
$request->set_query_params( array( 'skip_existing' => 1 ) );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$report = $response->get_data();
|
|
|
|
|
2019-06-11 12:47:53 +00:00
|
|
|
$this->assertEquals( 0, $report['customers'] );
|
2019-05-13 02:30:07 +00:00
|
|
|
$this->assertEquals( 0, $report['orders'] );
|
|
|
|
}
|
2019-04-11 10:50:12 +00:00
|
|
|
}
|