2019-01-24 00:48:22 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Reports Customers Stats REST API Test
|
|
|
|
*
|
2020-08-11 19:18:47 +00:00
|
|
|
* @package WooCommerce\Admin\Tests\API
|
2019-01-24 00:48:22 +00:00
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reports Customers Stats REST API Test Class
|
|
|
|
*
|
2020-08-11 19:18:47 +00:00
|
|
|
* @package WooCommerce\Admin\Tests\API
|
2019-01-24 00:48:22 +00:00
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
class WC_Tests_API_Reports_Customers_Stats extends WC_REST_Unit_Test_Case {
|
|
|
|
/**
|
|
|
|
* Endpoint.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2019-11-12 18:15:55 +00:00
|
|
|
protected $endpoint = '/wc-analytics/reports/customers/stats';
|
2019-01-24 00:48:22 +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
|
|
|
|
*/
|
2019-02-14 23:17:05 +00:00
|
|
|
public function test_register_routes() {
|
2019-01-24 00:48:22 +00:00
|
|
|
$routes = $this->server->get_routes();
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( $this->endpoint, $routes );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-02-14 03:31:27 +00:00
|
|
|
$this->assertCount( 1, $properties );
|
2019-01-24 00:48:22 +00:00
|
|
|
$this->assertArrayHasKey( 'totals', $properties );
|
|
|
|
$this->assertCount( 4, $properties['totals']['properties'] );
|
|
|
|
$this->assertArrayHasKey( 'customers_count', $properties['totals']['properties'] );
|
|
|
|
$this->assertArrayHasKey( 'avg_orders_count', $properties['totals']['properties'] );
|
|
|
|
$this->assertArrayHasKey( 'avg_total_spend', $properties['totals']['properties'] );
|
|
|
|
$this->assertArrayHasKey( 'avg_avg_order_value', $properties['totals']['properties'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting reports.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_get_reports() {
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
WC_Helper_Reports::reset_stats_dbs();
|
|
|
|
|
|
|
|
$test_customers = array();
|
|
|
|
|
|
|
|
// Create 10 test customers.
|
|
|
|
for ( $i = 1; $i <= 10; $i++ ) {
|
|
|
|
$test_customers[] = WC_Helper_Customer::create_customer( "customer{$i}", 'password', "customer{$i}@example.com" );
|
|
|
|
}
|
|
|
|
// One differing name.
|
|
|
|
$test_customers[2]->set_first_name( 'Jeff' );
|
|
|
|
$test_customers[2]->save();
|
|
|
|
|
|
|
|
// 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 some test orders.
|
|
|
|
$order = WC_Helper_Order::create_order( $test_customers[0]->get_id(), $product );
|
|
|
|
$order->set_status( 'completed' );
|
|
|
|
$order->set_total( 100 );
|
|
|
|
$order->save();
|
|
|
|
|
|
|
|
$order = WC_Helper_Order::create_order( $test_customers[0]->get_id(), $product );
|
|
|
|
$order->set_status( 'completed' );
|
|
|
|
$order->set_total( 234 );
|
|
|
|
$order->save();
|
|
|
|
|
|
|
|
$order = WC_Helper_Order::create_order( $test_customers[1]->get_id(), $product );
|
|
|
|
$order->set_status( 'completed' );
|
|
|
|
$order->set_total( 55 );
|
|
|
|
$order->save();
|
|
|
|
|
|
|
|
$order = WC_Helper_Order::create_order( $test_customers[2]->get_id(), $product );
|
|
|
|
$order->set_status( 'completed' );
|
|
|
|
$order->set_total( 9.12 );
|
|
|
|
$order->save();
|
|
|
|
|
2019-01-30 16:59:45 +00:00
|
|
|
WC_Helper_Queue::run_all_pending();
|
|
|
|
|
2019-01-24 00:48:22 +00:00
|
|
|
$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->assertEquals( 10, $reports['totals']->customers_count );
|
|
|
|
$this->assertEquals( 1.333, round( $reports['totals']->avg_orders_count, 3 ) );
|
|
|
|
$this->assertEquals( 132.707, round( $reports['totals']->avg_total_spend, 3 ) );
|
|
|
|
$this->assertEquals( 77.04, $reports['totals']->avg_avg_order_value );
|
|
|
|
|
|
|
|
// Test name parameter (case with no matches).
|
|
|
|
$request->set_query_params(
|
|
|
|
array(
|
2019-02-12 19:06:22 +00:00
|
|
|
'search' => 'Nota Customername',
|
2019-01-24 00:48:22 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( 0, $reports['totals']->customers_count );
|
|
|
|
$this->assertEquals( 0, $reports['totals']->avg_orders_count );
|
|
|
|
$this->assertEquals( 0, $reports['totals']->avg_total_spend );
|
|
|
|
$this->assertEquals( 0, $reports['totals']->avg_avg_order_value );
|
|
|
|
|
|
|
|
// Test name and last_order parameters.
|
|
|
|
$request->set_query_params(
|
|
|
|
array(
|
2019-02-14 23:17:05 +00:00
|
|
|
'search' => 'Jeff',
|
2020-08-11 19:18:47 +00:00
|
|
|
'last_order_after' => gmdate( 'Y-m-d' ) . 'T00:00:00Z',
|
2019-01-24 00:48:22 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( 1, $reports['totals']->customers_count );
|
|
|
|
$this->assertEquals( 1, $reports['totals']->avg_orders_count );
|
|
|
|
$this->assertEquals( 9.12, $reports['totals']->avg_total_spend );
|
|
|
|
$this->assertEquals( 9.12, $reports['totals']->avg_avg_order_value );
|
|
|
|
}
|
|
|
|
}
|