Initial setup for unit tests.

This commit is contained in:
Peter Fabian 2018-09-20 16:20:04 +02:00
parent a27134e1a6
commit 389a2182a1
7 changed files with 980 additions and 3 deletions

View File

@ -31,7 +31,7 @@ class WC_Admin_Api_Init {
add_action( 'woocommerce_after_register_post_type', array( 'WC_Admin_Api_Init', 'order_product_lookup_store_init' ), 20 );
// Create tables.
$this->create_db_tables();
self::create_db_tables();
}
/**
@ -331,12 +331,19 @@ class WC_Admin_Api_Init {
/**
* Create database tables.
*/
protected function create_db_tables() {
public static function create_db_tables() {
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( self::get_schema() );
}
/**
* Install plugin.
*/
public static function install() {
self::create_db_tables();
}
}
new WC_Admin_Api_Init();

View File

@ -11,7 +11,7 @@
>
<testsuites>
<testsuite name="WooCommerce Admin Test Suite">
<directory prefix="test-" suffix=".php">./tests/</directory>
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>

View File

@ -0,0 +1,522 @@
<?php
/**
* Reports Interval Test
*
* @package WC Admin
* @since x.x.0
*/
class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case {
public function test_quarter() {
$datetime = new DateTime( '2017-12-31T00:00:00Z' );
$this->assertEquals( 4, WC_Admin_Reports_Interval::quarter( $datetime ) );
$datetime = new DateTime( '2018-01-01T00:00:00Z' );
$this->assertEquals( 1, WC_Admin_Reports_Interval::quarter( $datetime ) );
$datetime = new DateTime( '2018-03-31T23:59:59Z' );
$this->assertEquals( 1, WC_Admin_Reports_Interval::quarter( $datetime ) );
$datetime = new DateTime( '2018-04-01T00:00:00Z' );
$this->assertEquals( 2, WC_Admin_Reports_Interval::quarter( $datetime ) );
$datetime = new DateTime( '2018-06-30T23:59:59Z' );
$this->assertEquals( 2, WC_Admin_Reports_Interval::quarter( $datetime ) );
$datetime = new DateTime( '2018-07-01T00:00:00Z' );
$this->assertEquals( 3, WC_Admin_Reports_Interval::quarter( $datetime ) );
$datetime = new DateTime( '2018-09-30T23:59:59Z' );
$this->assertEquals( 3, WC_Admin_Reports_Interval::quarter( $datetime ) );
$datetime = new DateTime( '2018-10-01T00:00:00Z' );
$this->assertEquals( 4, WC_Admin_Reports_Interval::quarter( $datetime ) );
$datetime = new DateTime( '2018-12-31T23:59:59Z' );
$this->assertEquals( 4, WC_Admin_Reports_Interval::quarter( $datetime ) );
}
public function test_simple_week_number() {
$expected_week_no = array(
'2010-12-24' => array( 1 => 52, 2 => 52, 3 => 52, 4 => 52, 5 => 52, 6 => 52, 7 => 52, ),
'2010-12-25' => array( 1 => 52, 2 => 52, 3 => 52, 4 => 52, 5 => 52, 6 => 53, 7 => 52, ),
'2010-12-26' => array( 1 => 52, 2 => 52, 3 => 52, 4 => 52, 5 => 52, 6 => 53, 7 => 53, ),
'2010-12-27' => array( 1 => 53, 2 => 52, 3 => 52, 4 => 52, 5 => 52, 6 => 53, 7 => 53, ),
'2010-12-28' => array( 1 => 53, 2 => 53, 3 => 52, 4 => 52, 5 => 52, 6 => 53, 7 => 53, ),
'2010-12-29' => array( 1 => 53, 2 => 53, 3 => 53, 4 => 52, 5 => 52, 6 => 53, 7 => 53, ),
'2010-12-30' => array( 1 => 53, 2 => 53, 3 => 53, 4 => 53, 5 => 52, 6 => 53, 7 => 53, ),
'2010-12-31' => array( 1 => 53, 2 => 53, 3 => 53, 4 => 53, 5 => 53, 6 => 53, 7 => 53, ),
'2011-01-01' => array( 1 => 1, 2 => 1, 3 => 1, 4 => 1, 5 => 1, 6 => 1, 7 => 1, ),
'2011-01-02' => array( 1 => 1, 2 => 1, 3 => 1, 4 => 1, 5 => 1, 6 => 1, 7 => 2, ),
'2011-01-03' => array( 1 => 2, 2 => 1, 3 => 1, 4 => 1, 5 => 1, 6 => 1, 7 => 2, ),
'2011-01-04' => array( 1 => 2, 2 => 2, 3 => 1, 4 => 1, 5 => 1, 6 => 1, 7 => 2, ),
'2011-01-05' => array( 1 => 2, 2 => 2, 3 => 2, 4 => 1, 5 => 1, 6 => 1, 7 => 2, ),
'2011-01-06' => array( 1 => 2, 2 => 2, 3 => 2, 4 => 2, 5 => 1, 6 => 1, 7 => 2, ),
'2011-01-07' => array( 1 => 2, 2 => 2, 3 => 2, 4 => 2, 5 => 2, 6 => 1, 7 => 2, ),
'2011-01-08' => array( 1 => 2, 2 => 2, 3 => 2, 4 => 2, 5 => 2, 6 => 2, 7 => 2, ),
'2011-01-09' => array( 1 => 2, 2 => 2, 3 => 2, 4 => 2, 5 => 2, 6 => 2, 7 => 3, ),
'2011-01-10' => array( 1 => 3, 2 => 2, 3 => 2, 4 => 2, 5 => 2, 6 => 2, 7 => 3, ),
'2011-12-26' => array( 1 => 53, 2 => 52, 3 => 52, 4 => 52, 5 => 52, 6 => 52, 7 => 53, ),
'2011-12-27' => array( 1 => 53, 2 => 53, 3 => 52, 4 => 52, 5 => 52, 6 => 52, 7 => 53, ),
'2011-12-28' => array( 1 => 53, 2 => 53, 3 => 53, 4 => 52, 5 => 52, 6 => 52, 7 => 53, ),
'2011-12-29' => array( 1 => 53, 2 => 53, 3 => 53, 4 => 53, 5 => 52, 6 => 52, 7 => 53, ),
'2011-12-30' => array( 1 => 53, 2 => 53, 3 => 53, 4 => 53, 5 => 53, 6 => 52, 7 => 53, ),
'2011-12-31' => array( 1 => 53, 2 => 53, 3 => 53, 4 => 53, 5 => 53, 6 => 53, 7 => 53, ),
'2012-01-01' => array( 1 => 1, 2 => 1, 3 => 1, 4 => 1, 5 => 1, 6 => 1, 7 => 1, ),
'2012-01-02' => array( 1 => 2, 2 => 1, 3 => 1, 4 => 1, 5 => 1, 6 => 1, 7 => 1, ),
'2012-01-03' => array( 1 => 2, 2 => 2, 3 => 1, 4 => 1, 5 => 1, 6 => 1, 7 => 1, ),
'2012-01-04' => array( 1 => 2, 2 => 2, 3 => 2, 4 => 1, 5 => 1, 6 => 1, 7 => 1, ),
'2012-01-05' => array( 1 => 2, 2 => 2, 3 => 2, 4 => 2, 5 => 1, 6 => 1, 7 => 1, ),
'2012-01-06' => array( 1 => 2, 2 => 2, 3 => 2, 4 => 2, 5 => 2, 6 => 1, 7 => 1, ),
);
foreach ( $expected_week_no as $date => $week_numbers ) {
for ( $first_day_of_week = 1; $first_day_of_week <= 7; $first_day_of_week++ ) {
$datetime = new DateTime( $date );
$this->assertEquals( $expected_week_no[$date][$first_day_of_week], WC_Admin_Reports_Interval::simple_week_number( $datetime, $first_day_of_week ), "First day of week: $first_day_of_week; Date: $date" );
}
}
}
public function test_ISO_week_no() {
$expected_week_no = array(
'2010-12-24' => 51,
'2010-12-25' => 51,
'2010-12-26' => 51,
'2010-12-27' => 52,
'2010-12-28' => 52,
'2010-12-29' => 52,
'2010-12-30' => 52,
'2010-12-31' => 52,
'2011-01-01' => 52,
'2011-01-02' => 52,
'2011-01-03' => 1,
'2011-01-04' => 1,
'2011-01-05' => 1,
'2011-01-06' => 1,
'2011-01-07' => 1,
'2011-01-08' => 1,
'2011-01-09' => 1,
'2011-01-10' => 2,
'2011-12-26' => 52,
'2011-12-27' => 52,
'2011-12-28' => 52,
'2011-12-29' => 52,
'2011-12-30' => 52,
'2011-12-31' => 52,
'2012-01-01' => 52,
'2012-01-02' => 1,
'2012-01-03' => 1,
'2012-01-04' => 1,
'2012-01-05' => 1,
'2012-01-06' => 1,
);
foreach ( $expected_week_no as $date => $week_numbers ) {
$datetime = new DateTime( $date );
$this->assertEquals( $expected_week_no[$date], WC_Admin_Reports_Interval::week_number( $datetime, 1 ), "ISO week number for date: $date" );
}
}
public function test_intervals_between() {
// please note that all intervals are inclusive on both sides
$test_settings = array(
// 0 interval length, should just return 1.
array(
'start' => '2017-12-24T11:00:00Z',
'end' => '2017-12-24T11:00:00Z',
'week_start' => 1,
'intervals' => array(
'hour' => 1,
'day' => 1,
'week' => 1,
'month' => 1,
'quarter' => 1,
'year' => 1,
),
),
// <1 hour interval length -> should return 1 for all
array(
'start' => '2017-12-24T11:00:00Z',
'end' => '2017-12-24T11:40:00Z',
'week_start' => 1,
'intervals' => array(
'hour' => 1,
'day' => 1,
'week' => 1,
'month' => 1,
'quarter' => 1,
'year' => 1,
),
),
// 1.66 hour interval length -> 2 hours, 1 for the rest
array(
'start' => '2017-12-24T11:00:00Z',
'end' => '2017-12-24T12:40:00Z',
'week_start' => 1,
'intervals' => array(
'hour' => 2,
'day' => 1,
'week' => 1,
'month' => 1,
'quarter' => 1,
'year' => 1,
),
),
// 23:59:59 h:m:s interval -> 24 hours, 1 for the rest
array(
'start' => '2017-12-24T11:00:00Z',
'end' => '2017-12-25T10:59:59Z',
'week_start' => 1,
'intervals' => array(
'hour' => 24,
'day' => 1,
'week' => 2,
'month' => 1,
'quarter' => 1,
'year' => 1,
),
),
// 24 hour inclusive interval -> 25 hours, 2 days, 1 for the rest
array(
'start' => '2017-12-24T11:00:00Z',
'end' => '2017-12-25T11:00:00Z',
'week_start' => 1,
'intervals' => array(
'hour' => 25,
'day' => 2,
'week' => 2,
'month' => 1,
'quarter' => 1,
'year' => 1,
),
),
// 1 month interval spanning 1 month -> 720 hours, 30 days, 5 iso weeks, 1 months
array(
'start' => '2017-11-01T00:00:00Z',
'end' => '2017-11-30T23:59:59Z',
'week_start' => 1,
'intervals' => array(
'hour' => 720,
'day' => 30,
'week' => 5,
'month' => 1,
'quarter' => 1,
'year' => 1,
),
),
// 1 month interval spanning 2 months, but 1 quarter and 1 year -> 721 hours, 31 days, 5 iso weeks, 2 months
array(
'start' => '2017-11-24T11:00:00Z',
'end' => '2017-12-24T11:00:00Z',
'week_start' => 1,
'intervals' => array(
'hour' => 30 * 24 + 1, // 30 full days + 1 second from next hour
'day' => 31,
'week' => 5,
'month' => 2,
'quarter' => 1,
'year' => 1,
),
),
// 1 month + 14 hour interval spanning 2 months in 1 quarter -> 735 hours, 31 days, 6 iso weeks, 2 months
array(
'start' => '2017-11-24T11:00:00Z',
'end' => '2017-12-25T01:00:00Z',
'week_start' => 1,
'intervals' => array(
'hour' => 30 * 24 + 14 + 1, // 30 full days + 14 full hours + 1 second from next hour
'day' => 31,
'week' => 6,
'month' => 2,
'quarter' => 1,
'year' => 1,
),
),
// 1 month interval spanning 2 months and 2 quarters, 1 year -> 720 hours, 30 days, 6 iso weeks, 2 months, 2 q, 1 y
array(
'start' => '2017-09-24T11:00:00Z',
'end' => '2017-10-24T10:59:59Z',
'week_start' => 1,
'intervals' => array(
'hour' => 30 * 24,
'day' => 30, // Sept has 30 days.
'week' => 6,
'month' => 2,
'quarter' => 2,
'year' => 1,
),
),
// 1 month interval spanning 2 months and 2 quarters, 2 years -> 744 hours, 30 days, 5 iso weeks, 2 months, 2 quarters, 2 years
array(
'start' => '2017-12-24T11:00:00Z',
'end' => '2018-01-24T10:59:59Z',
'week_start' => 1,
'intervals' => array(
'hour' => 31 * 24,
'day' => 31, // Dec has 31 days.
'week' => 6,
'month' => 2,
'quarter' => 2,
'year' => 2,
),
),
// 3 months interval spanning 1 quarter, 1 year -> 2208 hours, 92 days, 14 iso weeks, 3 months, 1 quarters, 1 years
array(
'start' => '2017-10-01T00:00:00Z',
'end' => '2017-12-31T23:59:59Z',
'week_start' => 1,
'intervals' => array(
'hour' => 92 * 24, // 92 days
'day' => 92,
'week' => 14,
'month' => 3,
'quarter' => 1,
'year' => 1,
),
),
// 3 months + 1 day interval spanning 2 quarters, 1 year -> 2208 hours, 92 days, 14 iso weeks, 3 months, 2 quarters, 1 years
array(
'start' => '2017-09-30T00:00:00Z',
'end' => '2017-12-30T23:59:59Z',
'week_start' => 1,
'intervals' => array(
'hour' => 92 * 24, // 92 days
'day' => 92,
'week' => 14,
'month' => 4,
'quarter' => 2,
'year' => 1,
),
),
// 3 months + 1 day interval spanning 2 quarters, 2 years -> 2232 hours, 93 days, 14 iso weeks, 3 months, 2 quarters, 2 years
array(
'start' => '2017-10-31T00:00:00Z',
'end' => '2018-01-31T23:59:59Z',
'week_start' => 1,
'intervals' => array(
'hour' => 93 * 24, // 93 days
'day' => 93, // Jan 31d + Dec 31d + Nov 30d + Oct 1d = 93d.
'week' => 14,
'month' => 4,
'quarter' => 2,
'year' => 2,
),
),
// 9 months + 1 day interval spanning 2 quarters, 2 years.
array(
'start' => '2017-04-01T00:00:00Z',
'end' => '2018-01-01T00:00:00Z',
'week_start' => 1,
'intervals' => array(
'month' => 9 + 1,
'quarter' => 3 + 1,
'year' => 2,
),
),
// 9 months + 1 day interval spanning 2 quarters, 2 years.
array(
'start' => '2015-04-01T00:00:00Z',
'end' => '2018-01-01T00:00:00Z',
'week_start' => 1,
'intervals' => array(
'day' => 1007, // This includes leap year in 2016.
'month' => 9 + 12 + 12 + 1, // Rest of 2015 + 2016 + 2017 + 1 second in 2018.
'quarter' => 3 + 4 + 4 + 1, // Rest of 2015 + 2016 + 2017 + 1 second in 2018.
'year' => 4,
),
),
);
foreach ( $test_settings as $setting ) {
update_option( 'start_of_week', $setting['week_start'] );
foreach ( $setting['intervals'] as $interval => $exp_value ) {
$this->assertEquals( $exp_value, WC_Admin_Reports_Interval::intervals_between( $setting['start'], $setting['end'], $interval ), "FDoW: {$setting['week_start']}; Start: {$setting['start']}; End: {$setting['end']}; Interval: {$interval}" );
}
}
}
public function test_next_hour_start() {
$settings = array(
'2017-12-30T00:00:00Z' => array(
0 => '2017-12-30T01:00:00Z',
1 => '2017-12-29T23:59:59Z',
),
'2017-12-30T10:00:00Z' => array(
0 => '2017-12-30T11:00:00Z',
1 => '2017-12-30T09:59:59Z',
),
);
foreach ( $settings as $datetime_s => $setting ) {
$datetime = new DateTime( $datetime_s );
foreach ( $setting as $reversed => $exp_value ) {
$result_dt = WC_Admin_Reports_Interval::next_hour_start( $datetime, $reversed );
$this->assertEquals( $exp_value, $result_dt->format( WC_Admin_Reports_Interval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" );
}
}
}
public function test_next_day_start() {
$settings = array(
'2017-12-30T00:00:00Z' => array(
0 => '2017-12-31T00:00:00Z',
1 => '2017-12-29T23:59:59Z',
),
'2017-12-30T10:00:00Z' => array(
0 => '2017-12-31T00:00:00Z',
1 => '2017-12-29T23:59:59Z',
),
);
foreach ( $settings as $datetime_s => $setting ) {
$datetime = new DateTime( $datetime_s );
foreach ( $setting as $reversed => $exp_value ) {
$result_dt = WC_Admin_Reports_Interval::next_day_start( $datetime, $reversed );
$this->assertEquals( $exp_value, $result_dt->format( WC_Admin_Reports_Interval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" );
}
}
}
public function test_next_week_start_ISO_week() {
update_option( 'start_of_week', 1 );
$settings = array(
'2017-12-30T00:00:00Z' => array(
0 => '2018-01-01T00:00:00Z',
1 => '2017-12-24T23:59:59Z',
),
'2017-12-30T10:00:00Z' => array(
0 => '2018-01-01T00:00:00Z',
1 => '2017-12-24T23:59:59Z',
),
'2010-12-25T10:00:00Z' => array(
0 => '2010-12-27T00:00:00Z',
1 => '2010-12-19T23:59:59Z',
),
'2010-12-26T10:00:00Z' => array(
0 => '2010-12-27T00:00:00Z',
1 => '2010-12-19T23:59:59Z',
),
'2010-12-27T00:00:00Z' => array(
0 => '2011-01-03T00:00:00Z',
1 => '2010-12-26T23:59:59Z',
),
'2010-12-31T00:00:00Z' => array(
0 => '2011-01-03T00:00:00Z',
1 => '2010-12-26T23:59:59Z',
),
'2011-01-01T00:00:00Z' => array(
0 => '2011-01-03T00:00:00Z',
1 => '2010-12-26T23:59:59Z',
),
'2011-01-03T00:00:00Z' => array(
0 => '2011-01-10T00:00:00Z',
1 => '2011-01-02T23:59:59Z',
),
);
foreach ( $settings as $datetime_s => $setting ) {
$datetime = new DateTime( $datetime_s );
foreach ( $setting as $reversed => $exp_value ) {
$result_dt = WC_Admin_Reports_Interval::next_week_start( $datetime, $reversed );
$this->assertEquals( $exp_value, $result_dt->format( WC_Admin_Reports_Interval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" );
}
}
}
public function test_next_week_start_Sunday_based_week() {
update_option( 'start_of_week', 7 );
$settings = array(
'2010-12-25T10:00:00Z' => array(
0 => '2010-12-26T00:00:00Z',
1 => '2010-12-18T23:59:59Z',
),
'2010-12-26T10:00:00Z' => array(
0 => '2011-01-01T00:00:00Z',
1 => '2010-12-25T23:59:59Z',
),
'2011-01-01T00:00:00Z' => array(
0 => '2011-01-02T00:00:00Z',
1 => '2010-12-31T23:59:59Z',
),
'2011-01-02T00:00:00Z' => array(
0 => '2011-01-09T00:00:00Z',
1 => '2011-01-01T23:59:59Z',
),
);
foreach ( $settings as $datetime_s => $setting ) {
$datetime = new DateTime( $datetime_s );
foreach ( $setting as $reversed => $exp_value ) {
$result_dt = WC_Admin_Reports_Interval::next_week_start( $datetime, $reversed );
$this->assertEquals( $exp_value, $result_dt->format( WC_Admin_Reports_Interval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" );
}
}
}
public function test_next_month_start() {
$settings = array(
'2017-12-30T00:00:00Z' => array(
0 => '2018-01-01T00:00:00Z',
1 => '2017-11-30T23:59:59Z',
),
// Leap year reversed test.
'2016-03-05T10:00:00Z' => array(
0 => '2016-04-01T00:00:00Z',
1 => '2016-02-29T23:59:59Z',
),
);
foreach ( $settings as $datetime_s => $setting ) {
$datetime = new DateTime( $datetime_s );
foreach ( $setting as $reversed => $exp_value ) {
$result_dt = WC_Admin_Reports_Interval::next_month_start( $datetime, $reversed );
$this->assertEquals( $exp_value, $result_dt->format( WC_Admin_Reports_Interval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" );
}
}
}
public function test_next_quarter_start() {
$settings = array(
'2017-12-31T00:00:00Z' => array(
0 => '2018-01-01T00:00:00Z',
1 => '2017-09-30T23:59:59Z',
),
'2018-01-01T10:00:00Z' => array(
0 => '2018-04-01T00:00:00Z',
1 => '2017-12-31T23:59:59Z',
),
'2018-02-14T10:00:00Z' => array(
0 => '2018-04-01T00:00:00Z',
1 => '2017-12-31T23:59:59Z',
),
);
foreach ( $settings as $datetime_s => $setting ) {
$datetime = new DateTime( $datetime_s );
foreach ( $setting as $reversed => $exp_value ) {
$result_dt = WC_Admin_Reports_Interval::next_quarter_start( $datetime, $reversed );
$this->assertEquals( $exp_value, $result_dt->format( WC_Admin_Reports_Interval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" );
}
}
}
public function test_next_year_start() {
$settings = array(
'2017-12-31T23:59:59Z' => array(
0 => '2018-01-01T00:00:00Z',
1 => '2016-12-31T23:59:59Z',
),
'2017-01-01T00:00:00Z' => array(
0 => '2018-01-01T00:00:00Z',
1 => '2016-12-31T23:59:59Z',
),
'2017-04-23T14:53:00Z' => array(
0 => '2018-01-01T00:00:00Z',
1 => '2016-12-31T23:59:59Z',
),
);
foreach ( $settings as $datetime_s => $setting ) {
$datetime = new DateTime( $datetime_s );
foreach ( $setting as $reversed => $exp_value ) {
$result_dt = WC_Admin_Reports_Interval::next_year_start( $datetime, $reversed );
$this->assertEquals( $exp_value, $result_dt->format( WC_Admin_Reports_Interval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" );
}
}
}
}

View File

@ -0,0 +1,111 @@
<?php
/**
* Reports Orders Stats REST API Test
*
* @package WooCommerce\Tests\API
* @since 3.5.0
*/
class WC_Tests_API_Reports_Orders_Stats extends WC_REST_Unit_Test_Case {
/**
* Endpoints.
*
* @var string
*/
protected $endpoint = '/wc/v3/reports/orders/stats';
/**
* Setup test reports orders 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 ) );
$reports = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 2, count( $reports ) ); // totals and intervals.
// $this->assertEquals( array(), $reports ); // @todo update results after implement report interface.
}
/**
* 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'];
$this->assertEquals( 4, count( $totals ) );
$this->assertArrayHasKey( 'net_revenue', $totals );
$this->assertArrayHasKey( 'avg_order_value', $totals );
$this->assertArrayHasKey( 'orders_count', $totals );
$this->assertArrayHasKey( 'avg_items_per_order', $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( 4, count( $subtotals ) );
$this->assertArrayHasKey( 'net_revenue', $totals );
$this->assertArrayHasKey( 'avg_order_value', $totals );
$this->assertArrayHasKey( 'orders_count', $totals );
$this->assertArrayHasKey( 'avg_items_per_order', $totals );
}
}

View File

@ -19,14 +19,92 @@ if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) {
// Give access to tests_add_filter() function.
require_once $_tests_dir . '/includes/functions.php';
function wc_dir() {
return dirname( dirname( dirname( __FILE__ ) ) ) . '/woocommerce';
}
function load_wc() {
define( 'WC_TAX_ROUNDING_MODE', 'auto' );
define( 'WC_USE_TRANSACTIONS', false );
require_once wc_dir() . '/woocommerce.php';
}
function install_wc() {
// Clean existing install first.
define( 'WP_UNINSTALL_PLUGIN', true );
define( 'WC_REMOVE_ALL_DATA', true );
include wc_dir() . '/uninstall.php';
WC_Install::install();
// Reload capabilities after install, see https://core.trac.wordpress.org/ticket/28374.
if ( version_compare( $GLOBALS['wp_version'], '4.7', '<' ) ) {
$GLOBALS['wp_roles']->reinit();
} else {
$GLOBALS['wp_roles'] = null; // WPCS: override ok.
wp_roles();
}
echo esc_html( 'Installing WooCommerce...' . PHP_EOL );
}
function wc_test_includes() {
$wc_tests_framework_base_dir = wc_dir() . '/tests/';
// WooCommerce test classes.
// Framework.
require_once $wc_tests_framework_base_dir . '/framework/class-wc-unit-test-factory.php';
require_once $wc_tests_framework_base_dir . '/framework/class-wc-mock-session-handler.php';
require_once $wc_tests_framework_base_dir . '/framework/class-wc-mock-wc-data.php';
require_once $wc_tests_framework_base_dir . '/framework/class-wc-mock-wc-object-query.php';
require_once $wc_tests_framework_base_dir . '/framework/class-wc-mock-payment-gateway.php';
require_once $wc_tests_framework_base_dir . '/framework/class-wc-payment-token-stub.php';
require_once $wc_tests_framework_base_dir . '/framework/vendor/class-wp-test-spy-rest-server.php';
// Test cases.
require_once $wc_tests_framework_base_dir . '/includes/wp-http-testcase.php';
require_once $wc_tests_framework_base_dir . '/framework/class-wc-unit-test-case.php';
require_once $wc_tests_framework_base_dir . '/framework/class-wc-api-unit-test-case.php';
require_once $wc_tests_framework_base_dir . '/framework/class-wc-rest-unit-test-case.php';
// Helpers.
require_once $wc_tests_framework_base_dir . '/framework/helpers/class-wc-helper-product.php';
require_once $wc_tests_framework_base_dir . '/framework/helpers/class-wc-helper-coupon.php';
require_once $wc_tests_framework_base_dir . '/framework/helpers/class-wc-helper-fee.php';
require_once $wc_tests_framework_base_dir . '/framework/helpers/class-wc-helper-shipping.php';
require_once $wc_tests_framework_base_dir . '/framework/helpers/class-wc-helper-customer.php';
require_once $wc_tests_framework_base_dir . '/framework/helpers/class-wc-helper-order.php';
require_once $wc_tests_framework_base_dir . '/framework/helpers/class-wc-helper-shipping-zones.php';
require_once $wc_tests_framework_base_dir . '/framework/helpers/class-wc-helper-payment-token.php';
require_once $wc_tests_framework_base_dir . '/framework/helpers/class-wc-helper-settings.php';
}
function install_wc_admin() {
require_once dirname( dirname( __FILE__ ) ) . '/includes/class-wc-admin-api-init.php';
WC_Admin_Api_Init::install();
echo esc_html( 'Installing WC Admin...' . PHP_EOL );
}
/**
* Manually load the plugin being tested.
*/
function _manually_load_plugin() {
require dirname( dirname( dirname( __FILE__ ) ) ) . '/gutenberg/gutenberg.php';
load_wc();
require dirname( dirname( __FILE__ ) ) . '/wc-admin.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
tests_add_filter( 'setup_theme', 'install_wc' );
tests_add_filter( 'setup_theme', 'install_wc_admin' );
// Start up the WP testing environment.
require $_tests_dir . '/includes/bootstrap.php';
wc_test_includes();
// Include wc-admin helpers.
require_once dirname( __FILE__ ) . '/framework/helpers/class-wc-helper-reports.php';

View File

@ -0,0 +1,18 @@
<?php
/**
* Class WC_Helper_Reports.
*
* This helper class should ONLY be used for unit tests!.
*/
class WC_Helper_Reports {
/**
* Delete everything in the lookup tables.
*/
public static function reset_stats_dbs() {
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.
}
}

View File

@ -0,0 +1,241 @@
<?php
/**
* Reports order stats tests.
*
* @package WooCommerce\Tests\Orders
* @todo Finish up unit testing to verify bug-free order reports.
*/
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();
$order = WC_Helper_Order::create_order( 1, $product );
$order->set_status( 'completed' );
$order->set_shipping_total( 10 );
$order->set_discount_total( 20 );
$order->set_discount_tax( 0 );
$order->set_cart_tax( 5 );
$order->set_shipping_tax( 2 );
$order->set_total( 97 ); // $25x4 products + $10 shipping - $20 discount + $7 tax.
$order->save();
// /reports/revenue/stats is mapped to Orders_Data_Store.
$data_store = new WC_Admin_Reports_Orders_Data_Store();
$data_store::update( $order );
$start_time = date( 'Y-m-d H:00:00', $order->get_date_created()->getOffsetTimestamp() );
$end_time = date( 'Y-m-d H:59:59', $order->get_date_created()->getOffsetTimestamp() );
$args = array(
'interval' => 'hour',
'after' => $start_time,
'before' => $end_time,
);
$expected_stats = array(
'totals' => array(
'orders_count' => 1,
'num_items_sold' => 4,
'gross_revenue' => 97,
'coupons' => 20,
'refunds' => 0,
'taxes' => 7,
'shipping' => 10,
'net_revenue' => 80,
'avg_items_per_order' => 4,
'avg_order_value' => 97,
),
'intervals' => array(
array(
'interval' => date( 'Y-m-d H', $order->get_date_created()->getTimestamp() ),
'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,
'gross_revenue' => 97,
'coupons' => 20,
'refunds' => 0,
'taxes' => 7,
'shipping' => 10,
'net_revenue' => 80,
'avg_items_per_order' => 4,
'avg_order_value' => 97,
),
),
),
'total' => 1,
'pages' => 1,
'page_no' => 1,
);
// Test retrieving the stats from the data store.
$this->assertEquals( $expected_stats, json_decode( json_encode( $data_store->get_data( $args ) ), true ) );
// Test retrieving the stats through the query class.
$expected_stats = array(
'totals' => array(
'orders_count' => 1,
'num_items_sold' => 4,
'gross_revenue' => 97,
'coupons' => 20,
'refunds' => 0,
'taxes' => 7,
'shipping' => 10,
'net_revenue' => 80,
),
'intervals' => array(
array(
'interval' => date( 'Y-m-d H', $order->get_date_created()->getTimestamp() ),
'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,
'gross_revenue' => 97,
'coupons' => 20,
'refunds' => 0,
'taxes' => 7,
'shipping' => 10,
'net_revenue' => 80,
),
),
),
'total' => 1,
'pages' => 1,
'page_no' => 1,
);
$query = new WC_Admin_Reports_Revenue_Query( $args );
$this->assertEquals( $expected_stats, json_decode( json_encode( $query->get_data() ), true ) );
}
/**
* Test the calculations and querying works correctly for the case of multiple orders.
*/
/*public function test_populate_and_query_multiple_intervals() {
// Populate all of the data.
$product1 = new WC_Product_Simple();
$product1->set_name( 'Test Product' );
$product1->set_regular_price( 25 );
$product1->save();
$product2 = new WC_Product_Simple();
$product2->set_name( 'Test Product 2' );
$product2->set_regular_price( 10 );
$product2->save();
$order1_time = time() - ( 2 * HOUR_IN_SECONDS );
$order1 = WC_Helper_Order::create_order( 1, $product1 );
$order1->set_date_created( $order1_time );
$order1->set_status( 'completed' );
$order1->set_shipping_total( 10 );
$order1->set_discount_total( 20 );
$order1->set_discount_tax( 0 );
$order1->set_cart_tax( 5 );
$order1->set_shipping_tax( 2 );
$order1->set_total( 97 ); // $25x4 products + $10 shipping - $20 discount + $7 tax.
$order1->save();
$order2_time = time() - HOUR_IN_SECONDS + 1;
$order2 = WC_Helper_Order::create_order( 1, $product2 );
$order2->set_date_created( $order2_time );
$order2->set_status( 'processing' );
$order2->set_shipping_total( 5 );
$order2->set_discount_total( 0 );
$order2->set_discount_tax( 0 );
$order2->set_cart_tax( 3 );
$order2->set_shipping_tax( 1 );
$order2->set_total( 49 ); // $10x4 products + $5 shipping + $4 tax.
$order2->save();
// Test the calculations.
$start_time = $order1_time;
$end_time = $order2_time + HOUR_IN_SECONDS;
// Test aggregate raw summary data for both orders.
$data = WC_Order_Stats::summarize_orders( $start_time, $end_time );
$expected_data = array(
'num_orders' => 2,
'num_items_sold' => 8,
'orders_gross_total' => 146.0,
'orders_coupon_total' => 20.0,
'orders_refund_total' => 0,
'orders_tax_total' => 11.0,
'orders_shipping_total' => 15.0,
'orders_net_total' => 120.0,
);
$this->assertEquals( $expected_data, $data );
// Calculate stats for each hour and save to DB.
$data = WC_Order_Stats::summarize_orders( $order1_time, $order1_time + HOUR_IN_SECONDS );
WC_Order_Stats::update( $order1_time, $data );
$data = WC_Order_Stats::summarize_orders( $order2_time, $order2_time + HOUR_IN_SECONDS );
WC_Order_Stats::update( $order2_time, $data );
// Test querying by hourly intervals.
$stats = WC_Order_Stats::query( $start_time, $end_time );
$first_hour_stats = $stats[0];
$expected_first_hour_stats = array(
'start_time' => date( 'Y-m-d H:00:00', $order1_time ),
'num_orders' => 1,
'num_items_sold' => 4,
'orders_gross_total' => 97,
'orders_coupon_total' => 20,
'orders_refund_total' => 0,
'orders_tax_total' => 7,
'orders_shipping_total' => 10,
'orders_net_total' => 80,
);
$this->assertEquals( $expected_first_hour_stats, $first_hour_stats );
$second_hour_stats = $stats[1];
$expected_second_hour_stats = array(
'start_time' => date( 'Y-m-d H:00:00', $order2_time ),
'num_orders' => 1,
'num_items_sold' => 4,
'orders_gross_total' => 49,
'orders_coupon_total' => 0,
'orders_refund_total' => 0,
'orders_tax_total' => 4,
'orders_shipping_total' => 5,
'orders_net_total' => 40,
);
$this->assertEquals( $expected_second_hour_stats, $second_hour_stats );
// Test querying by a weekly interval.
$stats = WC_Order_Stats::query( $start_time, $end_time, array( 'interval' => 'week' ) );
$first_week_stats = $stats[0];
$expected_first_week_stats = array(
'start_time' => date( 'Y-m-d H:00:00', $order1_time ),
'num_orders' => '2',
'num_items_sold' => '8',
'orders_gross_total' => '146',
'orders_coupon_total' => '20',
'orders_refund_total' => '0',
'orders_tax_total' => '11',
'orders_shipping_total' => '15',
'orders_net_total' => '120',
);
$this->assertEquals( $expected_first_week_stats, $first_week_stats );
}*/
}