woocommerce/plugins/woocommerce-admin/tests/api/reports-revenue-stats.php

138 lines
4.1 KiB
PHP
Raw Normal View History

2018-09-20 14:20:50 +00:00
<?php
/**
* Reports Revenue Stats REST API Test
*
* @package WooCommerce\Admin\Tests\API
2018-09-20 14:20:50 +00:00
* @since 3.5.0
*/
/**
* Class WC_Tests_API_Reports_Revenue_Stats
*/
2018-09-20 14:20:50 +00:00
class WC_Tests_API_Reports_Revenue_Stats extends WC_REST_Unit_Test_Case {
/**
* Endpoints.
*
* @var string
*/
protected $endpoint = '/wc-analytics/reports/revenue/stats';
2018-09-20 14:20:50 +00:00
/**
* Orders
*
* @var array
*/
2018-09-20 14:20:50 +00:00
protected $orders = array();
/**
* Setup test reports revenue 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
*/
public function test_register_routes() {
$routes = $this->server->get_routes();
$this->assertArrayHasKey( $this->endpoint, $routes );
}
/**
* Test getting reports.
*
* @since 3.5.0
*/
public function test_get_reports() {
wp_set_current_user( $this->user );
// @todo update after report interface is done.
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 2, count( $data ) ); // @todo Update results after implement report interface.
// $this->assertEquals( array(), $reports ); // @todo Update results after implement report interface.
2018-09-20 14:20:50 +00:00
}
/**
* 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 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'];
$this->assertEquals( 2, count( $properties ) );
$this->assertArrayHasKey( 'totals', $properties );
$this->assertArrayHasKey( 'intervals', $properties );
$totals = $properties['totals']['properties'];
Correcting and clarifying analytics terms and calculations (https://github.com/woocommerce/woocommerce-admin/pull/3104) * Relabel Net Revenue to Net Sales, revert previous refund work on Gross revenue and rename to total sales. Update the orer of all the things * Add gross sales calculation to revenue stats endpoint. * Restore coupon_total when updating order stats. * Wire up gross sales to revenue report. * Fix revenue report refunds calculation when there are no refunds. * update net sales labels and cases in order, product and category tables * Subtract refunded shipping and taxes from gross sales. * pluses to minuses to fix the gross revenue and refund totals when refunding * Add gross_sales to revenue stats orderby enum. * Change refund labels to Returns * Remove usage of defunct coupon_total column. * Store refunded amount in stats table. * Rename "gross_total" column to "total_sales". * Net total for refund orders can be used instead of a new column. * Rename gross_revenue to total_sales. * Coalesce coupons total in order stats query. SUM()ing all nulls gives null, not zero. * Use segmentation selections to backfill missing data. Fo when report columns and segmentation columns don't match. * Remove errant gross_sales from expected interval test data. * Fix gross sales tests for revenue/stats. * Move missing segment fills back to their original locations. * Fix remaining tests failing because of gross sales. * Fix db upgrade function rename of gross_total column. * Fix linter errors.
2019-11-22 15:06:14 +00:00
$this->assertEquals( 12, count( $totals ) );
$this->assertArrayHasKey( 'gross_sales', $totals );
$this->assertArrayHasKey( 'total_sales', $totals );
2018-09-20 14:20:50 +00:00
$this->assertArrayHasKey( 'net_revenue', $totals );
$this->assertArrayHasKey( 'coupons', $totals );
$this->assertArrayHasKey( 'coupons_count', $totals );
2018-09-20 14:20:50 +00:00
$this->assertArrayHasKey( 'shipping', $totals );
$this->assertArrayHasKey( 'taxes', $totals );
$this->assertArrayHasKey( 'refunds', $totals );
$this->assertArrayHasKey( 'orders_count', $totals );
$this->assertArrayHasKey( 'num_items_sold', $totals );
$this->assertArrayHasKey( 'products', $totals );
$this->assertArrayHasKey( 'segments', $totals );
2018-09-20 14:20:50 +00:00
$intervals = $properties['intervals']['items']['properties'];
$this->assertEquals( 6, count( $intervals ) );
$this->assertArrayHasKey( 'interval', $intervals );
$this->assertArrayHasKey( 'date_start', $intervals );
$this->assertArrayHasKey( 'date_start_gmt', $intervals );
$this->assertArrayHasKey( 'date_end', $intervals );
$this->assertArrayHasKey( 'date_end_gmt', $intervals );
$this->assertArrayHasKey( 'subtotals', $intervals );
$subtotals = $properties['intervals']['items']['properties']['subtotals']['properties'];
Correcting and clarifying analytics terms and calculations (https://github.com/woocommerce/woocommerce-admin/pull/3104) * Relabel Net Revenue to Net Sales, revert previous refund work on Gross revenue and rename to total sales. Update the orer of all the things * Add gross sales calculation to revenue stats endpoint. * Restore coupon_total when updating order stats. * Wire up gross sales to revenue report. * Fix revenue report refunds calculation when there are no refunds. * update net sales labels and cases in order, product and category tables * Subtract refunded shipping and taxes from gross sales. * pluses to minuses to fix the gross revenue and refund totals when refunding * Add gross_sales to revenue stats orderby enum. * Change refund labels to Returns * Remove usage of defunct coupon_total column. * Store refunded amount in stats table. * Rename "gross_total" column to "total_sales". * Net total for refund orders can be used instead of a new column. * Rename gross_revenue to total_sales. * Coalesce coupons total in order stats query. SUM()ing all nulls gives null, not zero. * Use segmentation selections to backfill missing data. Fo when report columns and segmentation columns don't match. * Remove errant gross_sales from expected interval test data. * Fix gross sales tests for revenue/stats. * Move missing segment fills back to their original locations. * Fix remaining tests failing because of gross sales. * Fix db upgrade function rename of gross_total column. * Fix linter errors.
2019-11-22 15:06:14 +00:00
$this->assertEquals( 11, count( $subtotals ) );
$this->assertArrayHasKey( 'gross_sales', $subtotals );
$this->assertArrayHasKey( 'total_sales', $subtotals );
2018-09-20 14:20:50 +00:00
$this->assertArrayHasKey( 'net_revenue', $subtotals );
$this->assertArrayHasKey( 'coupons', $subtotals );
$this->assertArrayHasKey( 'coupons_count', $subtotals );
2018-09-20 14:20:50 +00:00
$this->assertArrayHasKey( 'shipping', $subtotals );
$this->assertArrayHasKey( 'taxes', $subtotals );
$this->assertArrayHasKey( 'refunds', $subtotals );
$this->assertArrayHasKey( 'orders_count', $subtotals );
$this->assertArrayHasKey( 'num_items_sold', $subtotals );
$this->assertArrayHasKey( 'segments', $subtotals );
2018-09-20 14:20:50 +00:00
}
}