Added basic tests for coupons and coupons/stats.

This commit is contained in:
Peter Fabian 2018-12-10 19:34:55 +01:00 committed by Joshua Flowers
parent 21ac75d21e
commit 46bf4bedc8
5 changed files with 659 additions and 0 deletions

View File

@ -0,0 +1,169 @@
<?php
/**
* Reports Coupons Stats REST API Test
*
* @package WooCommerce\Tests\API
*/
class WC_Tests_API_Reports_Coupons_Stats extends WC_REST_Unit_Test_Case {
/**
* Endpoints.
*
* @var string
*/
protected $endpoint = '/wc/v3/reports/coupons/stats';
/**
* Setup test reports products stats data.
*/
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 );
}
/**
* Test getting reports.
*/
public function test_get_reports() {
WC_Helper_Reports::reset_stats_dbs();
wp_set_current_user( $this->user );
// Populate all of the data.
// Simple product.
$product = new WC_Product_Simple();
$product->set_name( 'Test Product' );
$product->set_regular_price( 25 );
$product->save();
// Coupons.
$coupon_1_amount = 1; // by default in create_coupon.
$coupon_1 = WC_Helper_Coupon::create_coupon( 'coupon_1' );
$coupon_2_amount = 2;
$coupon_2 = WC_Helper_Coupon::create_coupon( 'coupon_2' );
$coupon_2->set_amount( $coupon_2_amount );
$coupon_2->save();
// Order without coupon.
$order = WC_Helper_Order::create_order( 1, $product );
$order->set_status( 'completed' );
$order->set_total( 100 ); // $25 x 4.
$order->save();
$time = time();
// Order with 1 coupon.
$order_1c = WC_Helper_Order::create_order( 1, $product );
$order_1c->set_status( 'completed' );
$order_1c->apply_coupon( $coupon_1 );
$order_1c->calculate_totals();
$order_1c->set_date_created( $time );
$order_1c->save();
// Order with 2 coupons.
$order_2c = WC_Helper_Order::create_order( 1, $product );
$order_2c->set_status( 'completed' );
$order_2c->apply_coupon( $coupon_1 );
$order_2c->apply_coupon( $coupon_2 );
$order_2c->calculate_totals();
$order_2c->set_date_created( $time );
$order_2c->save();
$request = new WP_REST_Request( 'GET', $this->endpoint );
$request->set_query_params(
array(
'before' => date( 'Y-m-d 23:59:59', $time ),
'after' => date( 'Y-m-d 00:00:00', $time ),
'interval' => 'day',
)
);
$response = $this->server->dispatch( $request );
$reports = $response->get_data();
$expected_reports = array(
'totals' => array(
'gross_discount' => 4,
'coupons_count' => 2,
'orders_count' => 2,
),
'intervals' => array(
array(
'interval' => date( 'Y-m-d', $time ),
'date_start' => date( 'Y-m-d 00:00:00', $time ),
'date_start_gmt' => date( 'Y-m-d 00:00:00', $time ),
'date_end' => date( 'Y-m-d 23:59:59', $time ),
'date_end_gmt' => date( 'Y-m-d 23:59:59', $time ),
'subtotals' => (object) array(
'gross_discount' => 4,
'coupons_count' => 2,
'orders_count' => 2,
),
),
),
);
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( $expected_reports, $reports );
}
/**
* Test getting reports without valid permissions.
*/
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.
*/
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'];
$this->assertEquals( 3, count( $totals ) );
$this->assertArrayHasKey( 'gross_discount', $totals );
$this->assertArrayHasKey( 'coupons_count', $totals );
$this->assertArrayHasKey( 'orders_count', $totals );
$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'];
$this->assertEquals( 3, count( $subtotals ) );
$this->assertArrayHasKey( 'gross_discount', $totals );
$this->assertArrayHasKey( 'coupons_count', $totals );
$this->assertArrayHasKey( 'orders_count', $totals );
}
}

View File

@ -0,0 +1,126 @@
<?php
/**
* Reports Coupons REST API Test
*
* @package WooCommerce\Tests\API
*/
class WC_Tests_API_Reports_Coupons extends WC_REST_Unit_Test_Case {
/**
* Endpoints.
*
* @var string
*/
protected $endpoint = '/wc/v3/reports/coupons';
/**
* Setup test reports products data.
*/
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 );
}
/**
* Test getting basic reports.
*/
public function test_get_reports() {
wp_set_current_user( $this->user );
WC_Helper_Reports::reset_stats_dbs();
// Simple product.
$product = new WC_Product_Simple();
$product->set_name( 'Test Product' );
$product->set_regular_price( 25 );
$product->save();
// Coupons.
$coupon_1_amount = 1; // by default in create_coupon.
$coupon_1 = WC_Helper_Coupon::create_coupon( 'coupon_1' );
$coupon_2_amount = 2;
$coupon_2 = WC_Helper_Coupon::create_coupon( 'coupon_2' );
$coupon_2->set_amount( $coupon_2_amount );
$coupon_2->save();
// Order without coupon.
$order = WC_Helper_Order::create_order( 1, $product );
$order->set_status( 'completed' );
$order->set_total( 100 ); // $25 x 4.
$order->save();
// Order with 1 coupon.
$order_1c = WC_Helper_Order::create_order( 1, $product );
$order_1c->set_status( 'completed' );
$order_1c->apply_coupon( $coupon_1 );
$order_1c->calculate_totals();
$order_1c->save();
// Order with 2 coupons.
$order_2c = WC_Helper_Order::create_order( 1, $product );
$order_2c->set_status( 'completed' );
$order_2c->apply_coupon( $coupon_1 );
$order_2c->apply_coupon( $coupon_2 );
$order_2c->calculate_totals();
$order_2c->save();
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
$coupon_reports = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 2, count( $coupon_reports ) );
$this->assertEquals( $coupon_2->get_id(), $coupon_reports[0]['coupon_id'] );
$this->assertEquals( 1 * $coupon_2_amount, $coupon_reports[0]['gross_discount'] );
$this->assertEquals( 1, $coupon_reports[0]['orders_count'] );
$this->assertArrayHasKey( '_links', $coupon_reports[0] );
$this->assertArrayHasKey( 'coupon', $coupon_reports[0]['_links'] );
$this->assertEquals( $coupon_1->get_id(), $coupon_reports[1]['coupon_id'] );
$this->assertEquals( 2 * $coupon_1_amount, $coupon_reports[1]['gross_discount'] );
$this->assertEquals( 2, $coupon_reports[1]['orders_count'] );
$this->assertArrayHasKey( '_links', $coupon_reports[1] );
$this->assertArrayHasKey( 'coupon', $coupon_reports[1]['_links'] );
}
/**
* Test getting reports without valid permissions.
*/
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.
*/
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( 4, count( $properties ) );
$this->assertArrayHasKey( 'coupon_id', $properties );
$this->assertArrayHasKey( 'gross_discount', $properties );
$this->assertArrayHasKey( 'orders_count', $properties );
$this->assertArrayHasKey( 'extended_info', $properties );
}
}

View File

@ -14,5 +14,6 @@ class WC_Helper_Reports {
global $wpdb;
$wpdb->query( "DELETE FROM $wpdb->prefix" . WC_Admin_Reports_Orders_Data_Store::TABLE_NAME ); // @codingStandardsIgnoreLine.
$wpdb->query( "DELETE FROM $wpdb->prefix" . WC_Admin_Reports_Products_Data_Store::TABLE_NAME ); // @codingStandardsIgnoreLine.
$wpdb->query( "DELETE FROM $wpdb->prefix" . WC_Admin_Reports_Coupons_Data_Store::TABLE_NAME ); // @codingStandardsIgnoreLine.
}
}

View File

@ -0,0 +1,97 @@
<?php
/**
* Reports coupons stats tests.
*
* @package WooCommerce\Tests\Coupons-stats
*/
class WC_Tests_Reports_Coupons_Stats extends WC_Unit_Test_Case {
/**
* Test the for the basic cases.
*/
public function test_populate_and_query() {
WC_Helper_Reports::reset_stats_dbs();
// Simple product.
$product = new WC_Product_Simple();
$product->set_name( 'Test Product' );
$product->set_regular_price( 25 );
$product->save();
// Coupons.
$coupon_1_amount = 3; // by default in create_coupon.
$coupon_1 = WC_Helper_Coupon::create_coupon( 'coupon_1' );
$coupon_1->set_amount( $coupon_1_amount );
$coupon_1->save();
$coupon_2_amount = 7;
$coupon_2 = WC_Helper_Coupon::create_coupon( 'coupon_2' );
$coupon_2->set_amount( $coupon_2_amount );
$coupon_2->save();
// Order without coupon.
$order = WC_Helper_Order::create_order( 1, $product );
$order->set_status( 'completed' );
$order->set_total( 100 ); // $25 x 4.
$order->save();
// Order with 1 coupon.
$order_1c = WC_Helper_Order::create_order( 1, $product );
$order_1c->set_status( 'completed' );
$order_1c->apply_coupon( $coupon_1 );
$order_1c->calculate_totals();
$order_1c->save();
// Order with 2 coupons.
$order_2c = WC_Helper_Order::create_order( 1, $product );
$order_2c->set_status( 'completed' );
$order_2c->apply_coupon( $coupon_1 );
$order_2c->apply_coupon( $coupon_2 );
$order_2c->calculate_totals();
$order_2c->save();
$data_store = new WC_Admin_Reports_Coupons_Stats_Data_Store();
$start_time = date( 'Y-m-d 00:00:00', $order->get_date_created()->getOffsetTimestamp() );
$end_time = date( 'Y-m-d 23:59:59', $order->get_date_created()->getOffsetTimestamp() );
$args = array(
'after' => $start_time,
'before' => $end_time,
'interval' => 'day',
);
// Test retrieving the stats through the data store.
$start_datetime = new DateTime( $start_time );
$end_datetime = new DateTime( $end_time );
$data = $data_store->get_data( $args );
$expected_data = (object) array(
'total' => 1,
'pages' => 1,
'page_no' => 1,
'totals' => (object) array(
'gross_discount' => 2 * $coupon_1_amount + $coupon_2_amount,
'coupons_count' => 2,
'orders_count' => 2,
),
'intervals' => array(
array(
'interval' => $start_datetime->format( 'Y-m-d' ),
'date_start' => $start_datetime->format( 'Y-m-d H:i:s' ),
'date_start_gmt' => $start_datetime->format( 'Y-m-d H:i:s' ),
'date_end' => $end_datetime->format( 'Y-m-d H:i:s' ),
'date_end_gmt' => $end_datetime->format( 'Y-m-d H:i:s' ),
'subtotals' => (object) array(
'gross_discount' => 2 * $coupon_1_amount + $coupon_2_amount,
'coupons_count' => 2,
'orders_count' => 2,
),
),
),
);
$this->assertEquals( $expected_data, $data );
// Test retrieving the stats through the query class.
$query = new WC_Admin_Reports_Coupons_Stats_Query( $args );
$this->assertEquals( $expected_data, $query->get_data() );
}
}

View File

@ -0,0 +1,266 @@
<?php
/**
* Reports coupons tests.
*
* @package WooCommerce\Tests\Coupons
*/
class WC_Tests_Reports_Coupons extends WC_Unit_Test_Case {
/**
* Test the calculations and querying works correctly for the base case of 1 product.
*/
public function test_populate_and_query() {
WC_Helper_Reports::reset_stats_dbs();
// Simple product.
$product = new WC_Product_Simple();
$product->set_name( 'Test Product' );
$product->set_regular_price( 25 );
$product->save();
// Coupons.
$coupon_1_amount = 3; // by default in create_coupon.
$coupon_1 = WC_Helper_Coupon::create_coupon( 'coupon_1' );
$coupon_1->set_amount( $coupon_1_amount );
$coupon_1->save();
$coupon_2_amount = 7;
$coupon_2 = WC_Helper_Coupon::create_coupon( 'coupon_2' );
$coupon_2->set_amount( $coupon_2_amount );
$coupon_2->save();
// Order without coupon.
$order = WC_Helper_Order::create_order( 1, $product );
$order->set_status( 'completed' );
$order->set_total( 100 ); // $25 x 4.
$order->save();
// Order with 1 coupon.
$order_1c = WC_Helper_Order::create_order( 1, $product );
$order_1c->set_status( 'completed' );
$order_1c->apply_coupon( $coupon_1 );
$order_1c->calculate_totals();
$order_1c->save();
// Order with 2 coupons.
$order_2c = WC_Helper_Order::create_order( 1, $product );
$order_2c->set_status( 'completed' );
$order_2c->apply_coupon( $coupon_1 );
$order_2c->apply_coupon( $coupon_2 );
$order_2c->calculate_totals();
$order_2c->save();
$data_store = new WC_Admin_Reports_Coupons_Data_Store();
$start_time = date( 'Y-m-d 00:00:00', $order->get_date_created()->getOffsetTimestamp() );
$end_time = date( 'Y-m-d 23:59:59', $order->get_date_created()->getOffsetTimestamp() );
$args = array(
'after' => $start_time,
'before' => $end_time,
);
// Test retrieving the stats through the data store.
$coupon_1_response = array(
'coupon_id' => $coupon_1->get_id(),
'gross_discount' => floatval( $coupon_1_amount * 2 ),
'orders_count' => 2,
'extended_info' => new ArrayObject(),
);
$coupon_2_response = array(
'coupon_id' => $coupon_2->get_id(),
'gross_discount' => floatval( $coupon_2_amount ),
'orders_count' => 1,
'extended_info' => new ArrayObject(),
);
// Order by coupon id DESC is the default.
$data = $data_store->get_data( $args );
$expected_data = (object) array(
'total' => 2,
'pages' => 1,
'page_no' => 1,
'data' => array(
0 => $coupon_2_response,
1 => $coupon_1_response,
),
);
$this->assertEquals( $expected_data, $data );
// Test retrieving the stats through the query class.
$query = new WC_Admin_Reports_Coupons_Query( $args );
$this->assertEquals( $expected_data, $query->get_data() );
// Test order by orders_count DESC.
$args = array(
'after' => $start_time,
'before' => $end_time,
'orderby' => 'orders_count',
'order' => 'desc',
);
$data = $data_store->get_data( $args );
$expected_data = (object) array(
'total' => 2,
'pages' => 1,
'page_no' => 1,
'data' => array(
0 => $coupon_1_response,
1 => $coupon_2_response,
),
);
$this->assertEquals( $expected_data, $data );
// Test order by gross_discount ASC.
$args = array(
'after' => $start_time,
'before' => $end_time,
'orderby' => 'gross_discount',
'order' => 'asc',
);
$data = $data_store->get_data( $args );
$expected_data = (object) array(
'total' => 2,
'pages' => 1,
'page_no' => 1,
'data' => array(
0 => $coupon_1_response,
1 => $coupon_2_response,
),
);
$this->assertEquals( $expected_data, $data );
// Test filtering by coupon id: coupon_1.
$args = array(
'after' => $start_time,
'before' => $end_time,
'code' => array( 'coupon_1' ),
);
$data = $data_store->get_data( $args );
$expected_data = (object) array(
'total' => 1,
'pages' => 1,
'page_no' => 1,
'data' => array(
0 => $coupon_1_response,
),
);
$this->assertEquals( $expected_data, $data );
// Test filtering by coupon id: coupon_1 & coupon_2.
$args = array(
'after' => $start_time,
'before' => $end_time,
'code' => array( 'coupon_1', 'coupon_2' ),
);
$data = $data_store->get_data( $args );
$expected_data = (object) array(
'total' => 2,
'pages' => 1,
'page_no' => 1,
'data' => array(
0 => $coupon_2_response,
1 => $coupon_1_response,
),
);
$this->assertEquals( $expected_data, $data );
// Test extended info.
$gmt_timezone = new DateTimeZone( 'UTC' );
$c1_date_created = $coupon_1->get_date_created();
if ( null === $c1_date_created ) {
$c1_date_created = '';
$c1_date_created_gmt = '';
} else {
$c1_date_created_gmt = new DateTime( $c1_date_created );
$c1_date_created_gmt->setTimezone( $gmt_timezone );
$c1_date_created = $c1_date_created->format( WC_Admin_Reports_Interval::$iso_datetime_format );
$c1_date_created_gmt = $c1_date_created_gmt->format( WC_Admin_Reports_Interval::$iso_datetime_format );
}
$c1_date_expires = $coupon_1->get_date_expires();
if ( null === $c1_date_expires ) {
$c1_date_expires = '';
$c1_date_expires_gmt = '';
} else {
$c1_date_expires_gmt = new DateTime( $c1_date_expires );
$c1_date_expires_gmt->setTimezone( $gmt_timezone );
$c1_date_expires = $c1_date_expires->format( WC_Admin_Reports_Interval::$iso_datetime_format );
$c1_date_expires_gmt = $c1_date_expires_gmt->format( WC_Admin_Reports_Interval::$iso_datetime_format );
}
$coupon_1_response = array(
'coupon_id' => $coupon_1->get_id(),
'gross_discount' => $coupon_1_amount * 2,
'orders_count' => 2,
'extended_info' => array(
'code' => $coupon_1->get_code(),
'date_created' => $c1_date_created,
'date_created_gmt' => $c1_date_created_gmt,
'date_expires' => $c1_date_expires,
'date_expires_gmt' => $c1_date_expires_gmt,
'discount_type' => $coupon_1->get_discount_type(),
),
);
$c2_date_created = $coupon_2->get_date_created();
if ( null === $c2_date_created ) {
$c2_date_created = '';
$c2_date_created_gmt = '';
} else {
$c2_date_created_gmt = new DateTime( $c2_date_created );
$c2_date_created_gmt->setTimezone( $gmt_timezone );
$c2_date_created = $c2_date_created->format( WC_Admin_Reports_Interval::$iso_datetime_format );
$c2_date_created_gmt = $c2_date_created_gmt->format( WC_Admin_Reports_Interval::$iso_datetime_format );
}
$c2_date_expires = $coupon_2->get_date_expires();
if ( null === $c2_date_expires ) {
$c2_date_expires = '';
$c2_date_expires_gmt = '';
} else {
$c2_date_expires_gmt = new DateTime( $c2_date_expires );
$c2_date_expires_gmt->setTimezone( $gmt_timezone );
$c2_date_expires = $c2_date_expires->format( WC_Admin_Reports_Interval::$iso_datetime_format );
$c2_date_expires_gmt = $c2_date_expires_gmt->format( WC_Admin_Reports_Interval::$iso_datetime_format );
}
$coupon_2_response = array(
'coupon_id' => $coupon_2->get_id(),
'gross_discount' => $coupon_2_amount,
'orders_count' => 1,
'extended_info' => array(
'code' => $coupon_2->get_code(),
'date_created' => $c2_date_created,
'date_created_gmt' => $c2_date_created_gmt,
'date_expires' => $c2_date_expires,
'date_expires_gmt' => $c2_date_expires_gmt,
'discount_type' => $coupon_2->get_discount_type(),
),
);
$args = array(
'after' => $start_time,
'before' => $end_time,
'extended_info' => true,
);
$data = $data_store->get_data( $args );
$expected_data = (object) array(
'total' => 2,
'pages' => 1,
'page_no' => 1,
'data' => array(
0 => $coupon_2_response,
1 => $coupon_1_response,
),
);
$this->assertEquals( $expected_data, $data );
}
}