woocommerce/plugins/woocommerce-admin/tests/reports/class-wc-tests-reports-reve...

158 lines
4.7 KiB
PHP
Raw Normal View History

2018-09-20 14:20:04 +00:00
<?php
/**
* Reports order stats tests.
*
* @package WooCommerce\Tests\Orders
* @todo Finish up unit testing to verify bug-free order reports.
*/
2018-12-11 11:41:35 +00:00
use \Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore as OrdersStatsDataStore;
2019-08-06 19:11:33 +00:00
use \Automattic\WooCommerce\Admin\API\Reports\Revenue\Query as RevenueQuery;
2018-12-11 11:41:35 +00:00
/**
* Class WC_Admin_Tests_Reports_Revenue_Stats
*/
2018-09-20 14:20:04 +00:00
class WC_Admin_Tests_Reports_Revenue_Stats extends WC_Unit_Test_Case {
/**
* Test the calculations and querying works correctly for the base case of 1 order.
*
* @since 3.5.0
*/
public function test_populate_and_query() {
global $wpdb;
WC_Helper_Reports::reset_stats_dbs();
// Populate all of the data.
$product = new WC_Product_Simple();
$product->set_name( 'Test Product' );
$product->set_regular_price( 25 );
$product->save();
$coupon = WC_Helper_Coupon::create_coupon( 'test-coupon' );
$coupon->set_amount( 20 );
$coupon->save();
2018-09-20 14:20:04 +00:00
$order = WC_Helper_Order::create_order( 1, $product );
$order->set_status( 'completed' );
$order->set_shipping_total( 10 );
$order->apply_coupon( $coupon );
2018-09-20 14:20:04 +00:00
$order->set_cart_tax( 5 );
$order->set_shipping_tax( 2 );
$order->set_total( 97 ); // $25x4 products + $10 shipping - $20 discount + $7 tax.
$order->save();
WC_Helper_Queue::run_all_pending();
2018-09-20 14:20:04 +00:00
// /reports/revenue/stats is mapped to Orders_Data_Store.
$data_store = new OrdersStatsDataStore();
2018-09-20 14:20:04 +00:00
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
$start_time = gmdate( 'Y-m-d H:00:00', $order->get_date_created()->getOffsetTimestamp() );
$end_time = gmdate( 'Y-m-d H:59:59', $order->get_date_created()->getOffsetTimestamp() );
2018-09-20 14:20:04 +00:00
$args = array(
2018-09-20 14:20:04 +00:00
'interval' => 'hour',
'after' => $start_time,
'before' => $end_time,
);
$expected_stats = array(
'totals' => array(
'orders_count' => 1,
'num_items_sold' => 4,
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
'total_sales' => 97,
'gross_sales' => 100,
'coupons' => 20,
'coupons_count' => 1,
'refunds' => 0,
'taxes' => 7,
'shipping' => 10,
'net_revenue' => 80,
'avg_items_per_order' => 4,
'avg_order_value' => 80,
'num_returning_customers' => 0,
'num_new_customers' => 1,
'products' => 1,
'segments' => array(),
2018-09-20 14:20:04 +00:00
),
'intervals' => array(
array(
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
'interval' => gmdate( 'Y-m-d H', $order->get_date_created()->getTimestamp() ),
2018-09-20 14:20:04 +00:00
'date_start' => $start_time,
'date_start_gmt' => $start_time,
'date_end' => $end_time,
'date_end_gmt' => $end_time,
'subtotals' => array(
2018-11-02 11:24:21 +00:00
'orders_count' => 1,
'num_items_sold' => 4,
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
'total_sales' => 97,
'gross_sales' => 100,
2018-11-02 11:24:21 +00:00
'coupons' => 20,
'coupons_count' => 1,
2018-11-02 11:24:21 +00:00
'refunds' => 0,
'taxes' => 7,
'shipping' => 10,
'net_revenue' => 80,
'avg_items_per_order' => 4,
'avg_order_value' => 80,
2018-11-02 11:24:21 +00:00
'num_returning_customers' => 0,
'num_new_customers' => 1,
'segments' => array(),
2018-09-20 14:20:04 +00:00
),
),
),
'total' => 1,
'pages' => 1,
'page_no' => 1,
2018-09-20 14:20:04 +00:00
);
// Test retrieving the stats from the data store.
$this->assertEquals( $expected_stats, json_decode( wp_json_encode( $data_store->get_data( $args ) ), true ) );
2018-09-20 14:20:04 +00:00
// Test retrieving the stats through the query class.
$expected_stats = array(
'totals' => array(
'orders_count' => 1,
'num_items_sold' => 4,
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
'total_sales' => 97,
'gross_sales' => 100,
'coupons' => 20,
'coupons_count' => 1,
'refunds' => 0,
'taxes' => 7,
'shipping' => 10,
'net_revenue' => 80,
2018-11-02 11:24:21 +00:00
'products' => '1',
'segments' => array(),
2018-09-20 14:20:04 +00:00
),
'intervals' => array(
array(
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
'interval' => gmdate( 'Y-m-d H', $order->get_date_created()->getTimestamp() ),
2018-09-20 14:20:04 +00:00
'date_start' => $start_time,
'date_start_gmt' => $start_time,
'date_end' => $end_time,
'date_end_gmt' => $end_time,
'subtotals' => array(
'orders_count' => 1,
'num_items_sold' => 4,
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
'total_sales' => 97,
'gross_sales' => 100,
'coupons' => 20,
'coupons_count' => 1,
'refunds' => 0,
'taxes' => 7,
'shipping' => 10,
'net_revenue' => 80,
'segments' => array(),
2018-09-20 14:20:04 +00:00
),
),
),
'total' => 1,
'pages' => 1,
'page_no' => 1,
2018-09-20 14:20:04 +00:00
);
2019-08-06 19:11:33 +00:00
$query = new RevenueQuery( $args );
$this->assertEquals( $expected_stats, json_decode( wp_json_encode( $query->get_data() ), true ) );
2018-09-20 14:20:04 +00:00
}
}