2019-01-09 21:08:39 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Reports Performance indicators REST API Tests
|
|
|
|
*
|
2020-08-11 19:18:47 +00:00
|
|
|
* @package WooCommerce\Admin\Tests\API.
|
2019-01-09 21:08:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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',
|
|
|
|
)
|
|
|
|
);
|
2020-05-13 11:52:32 +00:00
|
|
|
|
|
|
|
// Mock the Jetpack endpoints and permissions.
|
|
|
|
$wp_user = get_userdata( $this->user );
|
|
|
|
$wp_user->add_cap( 'view_stats' );
|
2020-07-06 20:11:45 +00:00
|
|
|
$this->mock_jetpack_modules();
|
|
|
|
|
2020-05-13 11:52:32 +00:00
|
|
|
add_filter( 'rest_post_dispatch', array( $this, 'mock_rest_responses' ), 10, 3 );
|
2019-01-09 21:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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() {
|
|
|
|
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();
|
2021-09-23 10:01:52 +00:00
|
|
|
$prod_download->set_file( plugin_dir_url( $this->wc_core_dir ) . 'woocommerce/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(
|
2020-05-13 11:52:32 +00:00
|
|
|
'before' => gmdate( 'Y-m-d 23:59:59', $time ),
|
|
|
|
'after' => gmdate( 'Y-m-d H:00:00', $time - ( 7 * DAY_IN_SECONDS ) ),
|
|
|
|
'stats' => 'orders/orders_count,downloads/download_count,test/bogus_stat,jetpack/stats/views',
|
2019-01-09 21:08:39 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
2020-05-13 11:52:32 +00:00
|
|
|
$this->assertEquals( 3, count( $reports ) );
|
2019-01-09 21:08:39 +00:00
|
|
|
|
|
|
|
$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'] );
|
2020-05-13 11:52:32 +00:00
|
|
|
|
|
|
|
$this->assertEquals( 'jetpack/stats/views', $reports[2]['stat'] );
|
|
|
|
$this->assertEquals( 'Views', $reports[2]['label'] );
|
|
|
|
$this->assertEquals( 10, $reports[2]['value'] );
|
|
|
|
$this->assertEquals( 'views', $reports[2]['chart'] );
|
|
|
|
$this->assertEquals( get_rest_url( null, '/jetpack/v4/module/stats/data' ), $response->data[2]['_links']['api'][0]['href'] );
|
2019-01-09 21:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting indicators with an empty request.
|
|
|
|
*/
|
|
|
|
public function test_get_indicators_empty_request() {
|
|
|
|
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(
|
2020-05-13 11:52:32 +00:00
|
|
|
'before' => gmdate( 'Y-m-d 23:59:59', $time ),
|
|
|
|
'after' => gmdate( '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 );
|
|
|
|
}
|
2020-05-13 11:52:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the ability to aggregate Jetpack stats based on before and after dates.
|
|
|
|
*/
|
|
|
|
public function test_jetpack_stats_query_args() {
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
|
|
|
$request->set_query_params(
|
|
|
|
array(
|
|
|
|
'before' => '2020-01-05 23:59:59',
|
|
|
|
'after' => '2020-01-01 00:00:00',
|
|
|
|
'stats' => 'jetpack/stats/views',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( 1, count( $reports ) );
|
|
|
|
|
|
|
|
$this->assertEquals( 'jetpack/stats/views', $reports[0]['stat'] );
|
|
|
|
$this->assertEquals( 'Views', $reports[0]['label'] );
|
|
|
|
$this->assertEquals( 18, $reports[0]['value'] );
|
|
|
|
$this->assertEquals( 'views', $reports[0]['chart'] );
|
|
|
|
$this->assertEquals( get_rest_url( null, '/jetpack/v4/module/stats/data' ), $response->data[0]['_links']['api'][0]['href'] );
|
|
|
|
|
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
|
|
|
$request->set_query_params(
|
|
|
|
array(
|
|
|
|
'before' => '2020-01-02 23:59:59',
|
|
|
|
'after' => '2020-01-01 00:00:00',
|
|
|
|
'stats' => 'jetpack/stats/views',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( 1, count( $reports ) );
|
|
|
|
|
|
|
|
$this->assertEquals( 'jetpack/stats/views', $reports[0]['stat'] );
|
|
|
|
$this->assertEquals( 'Views', $reports[0]['label'] );
|
|
|
|
$this->assertEquals( 4, $reports[0]['value'] );
|
|
|
|
$this->assertEquals( 'views', $reports[0]['chart'] );
|
|
|
|
$this->assertEquals( get_rest_url( null, '/jetpack/v4/module/stats/data' ), $response->data[0]['_links']['api'][0]['href'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the ability to aggregate Jetpack stats based on default arguments.
|
|
|
|
*/
|
|
|
|
public function test_jetpack_stats_default_query_args() {
|
|
|
|
wp_set_current_user( $this->user );
|
|
|
|
|
|
|
|
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
|
|
|
$request->set_query_params(
|
|
|
|
array(
|
|
|
|
'stats' => 'jetpack/stats/views',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$reports = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( 1, count( $reports ) );
|
|
|
|
|
|
|
|
$this->assertEquals( 'jetpack/stats/views', $reports[0]['stat'] );
|
|
|
|
$this->assertEquals( 'Views', $reports[0]['label'] );
|
|
|
|
$this->assertEquals( 10, $reports[0]['value'] );
|
|
|
|
$this->assertEquals( 'views', $reports[0]['chart'] );
|
|
|
|
$this->assertEquals( get_rest_url( null, '/jetpack/v4/module/stats/data' ), $response->data[0]['_links']['api'][0]['href'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mock the Jetpack REST API responses since we're not really connected.
|
|
|
|
*
|
|
|
|
* @param WP_Rest_Response $response Response from the server.
|
|
|
|
* @param WP_Rest_Server $rest_server WP Rest Server.
|
|
|
|
* @param WP_REST_Request $request Request made to the server.
|
|
|
|
*
|
|
|
|
* @return WP_Rest_Response
|
|
|
|
*/
|
|
|
|
public function mock_rest_responses( $response, $rest_server, $request ) {
|
|
|
|
if ( 'GET' === $request->get_method() && '/jetpack/v4/module/stats/data' === $request->get_route() ) {
|
|
|
|
$general = new \stdClass();
|
|
|
|
$general->visits = new \stdClass();
|
|
|
|
$general->visits->fields = array(
|
|
|
|
'date',
|
|
|
|
'views',
|
|
|
|
'visits',
|
|
|
|
);
|
|
|
|
$general->visits->data = array(
|
|
|
|
array(
|
|
|
|
'2020-01-01',
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'2020-01-02',
|
|
|
|
3,
|
|
|
|
0,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'2020-01-03',
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'2020-01-04',
|
|
|
|
8,
|
|
|
|
0,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'2020-01-05',
|
|
|
|
5,
|
|
|
|
0,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
gmdate( 'Y-m-d' ),
|
|
|
|
10,
|
|
|
|
0,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$response->set_status( 200 );
|
|
|
|
$response->set_data(
|
|
|
|
array( 'general' => $general )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
2020-07-06 20:11:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Mock the Jetpack stats module as active.
|
|
|
|
*/
|
|
|
|
public function mock_jetpack_modules() {
|
|
|
|
$api_init = \Automattic\WooCommerce\Admin\API\Init::instance();
|
|
|
|
$controller_name = 'Automattic\WooCommerce\Admin\API\Reports\PerformanceIndicators\Controller';
|
|
|
|
|
|
|
|
$api_init->$controller_name->set_active_jetpack_modules( array( 'stats' ) );
|
|
|
|
}
|
2019-01-09 21:08:39 +00:00
|
|
|
}
|