diff --git a/plugins/woocommerce-admin/tests/api/reports-import.php b/plugins/woocommerce-admin/tests/api/reports-import.php index 0621fb61c61..9076e169e84 100644 --- a/plugins/woocommerce-admin/tests/api/reports-import.php +++ b/plugins/woocommerce-admin/tests/api/reports-import.php @@ -218,4 +218,71 @@ class WC_Tests_API_Reports_Import extends WC_REST_Unit_Test_Case { $pending_actions = WC_Helper_Queue::get_all_pending(); $this->assertCount( 0, $pending_actions ); } + + /** + * 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(); + + $request = new WP_REST_Request( 'GET', '/wc/v4/reports/orders' ); + $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 ); + + $request = new WP_REST_Request( 'GET', '/wc/v4/reports/customers' ); + $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'] ); + + // Run pending three times to process batches and dependent actions. + WC_Helper_Queue::run_all_pending(); + WC_Helper_Queue::run_all_pending(); + WC_Helper_Queue::run_all_pending(); + + // Check that stats have been deleted. + $request = new WP_REST_Request( 'GET', '/wc/v4/reports/orders' ); + $response = $this->server->dispatch( $request ); + $reports = $response->get_data(); + + $this->assertEquals( 200, $response->get_status() ); + $this->assertCount( 0, $reports ); + + $request = new WP_REST_Request( 'GET', '/wc/v4/reports/customers' ); + $response = $this->server->dispatch( $request ); + $reports = $response->get_data(); + + $this->assertEquals( 200, $response->get_status() ); + $this->assertCount( 0, $reports ); + } }