2019-01-09 21:08:39 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Reports Performance indicators REST API Tests
|
|
|
|
*
|
|
|
|
* @package WooCommerce Admin\Tests\API.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WC_Tests_API_Reports_Performance_Indicators
|
|
|
|
*/
|
|
|
|
class WC_Tests_API_Reports_Performance_Indicators extends WC_REST_Unit_Test_Case {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Endpoints.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2019-11-12 18:15:55 +00:00
|
|
|
protected $endpoint = '/wc-analytics/reports/performance-indicators';
|
2019-01-09 21:08:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->user = $this->factory->user->create(
|
|
|
|
array(
|
|
|
|
'role' => 'administrator',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test route registration.
|
|
|
|
*/
|
|
|
|
public function test_register_routes() {
|
|
|
|
$routes = $this->server->get_routes();
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( $this->endpoint, $routes );
|
2019-01-17 14:08:59 +00:00
|
|
|
$this->assertArrayHasKey( $this->endpoint . '/allowed', $routes );
|
2019-01-09 21:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting indicators.
|
|
|
|
*/
|
|
|
|
public function test_get_indicators() {
|
|
|
|
global $wpdb;
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
WC_Helper_Reports::reset_stats_dbs();
|
|
|
|
|
|
|
|
// Populate all of the data. We'll create an order and a download.
|
|
|
|
$prod_download = new WC_Product_Download();
|
2019-06-26 19:55:57 +00:00
|
|
|
$prod_download->set_file( plugin_dir_url( __FILE__ ) . 'assets/images/help.png' );
|
2019-06-27 20:46:56 +00:00
|
|
|
$prod_download->set_id( '1' );
|
2019-01-09 21:08:39 +00:00
|
|
|
|
|
|
|
$product = new WC_Product_Simple();
|
|
|
|
$product->set_name( 'Test Product' );
|
|
|
|
$product->set_downloadable( 'yes' );
|
|
|
|
$product->set_downloads( array( $prod_download ) );
|
|
|
|
$product->set_regular_price( 25 );
|
|
|
|
$product->save();
|
|
|
|
|
|
|
|
$order = WC_Helper_Order::create_order( 1, $product );
|
|
|
|
$order->set_status( 'completed' );
|
|
|
|
$order->set_total( 25 );
|
|
|
|
$order->save();
|
|
|
|
|
|
|
|
$download = new WC_Customer_Download();
|
|
|
|
$download->set_user_id( $this->user );
|
|
|
|
$download->set_order_id( $order->get_id() );
|
|
|
|
$download->set_product_id( $product->get_id() );
|
|
|
|
$download->set_download_id( $prod_download->get_id() );
|
|
|
|
$download->save();
|
|
|
|
|
|
|
|
$object = new WC_Customer_Download_Log();
|
|
|
|
$object->set_permission_id( $download->get_id() );
|
|
|
|
$object->set_user_id( $this->user );
|
|
|
|
$object->set_user_ip_address( '1.2.3.4' );
|
|
|
|
$object->save();
|
|
|
|
|
|
|
|
$object = new WC_Customer_Download_Log();
|
|
|
|
$object->set_permission_id( $download->get_id() );
|
|
|
|
$object->set_user_id( $this->user );
|
|
|
|
$object->set_user_ip_address( '1.2.3.4' );
|
|
|
|
$object->save();
|
|
|
|
|
2019-01-30 16:59:45 +00:00
|
|
|
WC_Helper_Queue::run_all_pending();
|
|
|
|
|
2019-02-14 23:08:25 +00:00
|
|
|
$time = time();
|
2019-01-09 21:08:39 +00:00
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
|
|
|
$request->set_query_params(
|
|
|
|
array(
|
2019-02-14 23:08:25 +00:00
|
|
|
'before' => date( 'Y-m-d 23:59:59', $time ),
|
|
|
|
'after' => date( 'Y-m-d H:00:00', $time - ( 7 * DAY_IN_SECONDS ) ),
|
|
|
|
'stats' => 'orders/orders_count,downloads/download_count,test/bogus_stat',
|
2019-01-09 21:08:39 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( 2, count( $reports ) );
|
|
|
|
|
|
|
|
$this->assertEquals( 'orders/orders_count', $reports[0]['stat'] );
|
2019-08-26 22:39:15 +00:00
|
|
|
$this->assertEquals( 'Orders', $reports[0]['label'] );
|
2019-01-09 21:08:39 +00:00
|
|
|
$this->assertEquals( 1, $reports[0]['value'] );
|
2019-01-17 14:08:59 +00:00
|
|
|
$this->assertEquals( 'orders_count', $reports[0]['chart'] );
|
|
|
|
$this->assertEquals( '/analytics/orders', $response->data[0]['_links']['report'][0]['href'] );
|
2019-01-09 21:08:39 +00:00
|
|
|
|
|
|
|
$this->assertEquals( 'downloads/download_count', $reports[1]['stat'] );
|
2019-08-26 22:39:15 +00:00
|
|
|
$this->assertEquals( 'Downloads', $reports[1]['label'] );
|
2019-01-09 21:08:39 +00:00
|
|
|
$this->assertEquals( 2, $reports[1]['value'] );
|
2019-01-17 14:08:59 +00:00
|
|
|
$this->assertEquals( 'download_count', $reports[1]['chart'] );
|
|
|
|
$this->assertEquals( '/analytics/downloads', $response->data[1]['_links']['report'][0]['href'] );
|
2019-01-09 21:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting indicators with an empty request.
|
|
|
|
*/
|
|
|
|
public function test_get_indicators_empty_request() {
|
|
|
|
global $wpdb;
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
WC_Helper_Reports::reset_stats_dbs();
|
|
|
|
|
2019-03-13 17:14:02 +00:00
|
|
|
$time = time();
|
2019-01-09 21:08:39 +00:00
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
|
|
|
$request->set_query_params(
|
|
|
|
array(
|
2019-02-14 23:08:25 +00:00
|
|
|
'before' => date( 'Y-m-d 23:59:59', $time ),
|
|
|
|
'after' => date( 'Y-m-d H:00:00', $time - ( 7 * DAY_IN_SECONDS ) ),
|
2019-01-09 21:08:39 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 500, $response->get_status() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting without valid permissions.
|
|
|
|
*/
|
|
|
|
public function test_get_indicators_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 schema.
|
|
|
|
*/
|
|
|
|
public function test_indicators_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-01-17 14:08:59 +00:00
|
|
|
$this->assertEquals( 5, count( $properties ) );
|
2019-01-09 21:08:39 +00:00
|
|
|
$this->assertArrayHasKey( 'stat', $properties );
|
2019-01-17 14:08:59 +00:00
|
|
|
$this->assertArrayHasKey( 'chart', $properties );
|
2019-01-14 19:39:47 +00:00
|
|
|
$this->assertArrayHasKey( 'label', $properties );
|
2019-01-17 14:08:59 +00:00
|
|
|
$this->assertArrayHasKey( 'format', $properties );
|
2019-01-09 21:08:39 +00:00
|
|
|
$this->assertArrayHasKey( 'value', $properties );
|
|
|
|
}
|
2019-01-21 17:28:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test schema for /allowed indicators endpoint.
|
|
|
|
*/
|
|
|
|
public function test_indicators_schema_allowed() {
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
|
|
|
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint . '/allowed' );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
$properties = $data['schema']['properties'];
|
|
|
|
|
|
|
|
$this->assertEquals( 3, count( $properties ) );
|
|
|
|
$this->assertArrayHasKey( 'stat', $properties );
|
|
|
|
$this->assertArrayHasKey( 'chart', $properties );
|
|
|
|
$this->assertArrayHasKey( 'label', $properties );
|
|
|
|
}
|
2019-01-09 21:08:39 +00:00
|
|
|
}
|