woocommerce/plugins/woocommerce-admin/tests/api/reports-interval.php

928 lines
23 KiB
PHP
Raw Normal View History

2018-09-20 14:20:04 +00:00
<?php
/**
* Reports Interval Test
*
* @package WooCommerce\Admin\Tests\API
2018-09-20 14:20:04 +00:00
* @since x.x.0
*/
2018-12-12 18:58:26 +00:00
use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
2018-12-12 18:58:26 +00:00
/**
* Class WC_Tests_Reports_Interval_Stats
*/
2018-09-20 14:20:04 +00:00
class WC_Tests_Reports_Interval_Stats extends WC_Unit_Test_Case {
/**
* Local timezone used throughout the tests.
*
* @var DateTimeZone
*/
protected static $local_tz;
/**
* Set current local timezone.
*/
public static function setUpBeforeClass() {
self::$local_tz = new DateTimeZone( wc_timezone_string() );
}
2018-09-24 15:24:08 +00:00
/**
* Test quarter function.
*/
2018-09-20 14:20:04 +00:00
public function test_quarter() {
$datetime = new DateTime( '2017-12-31T00:00:00', self::$local_tz );
$this->assertEquals( 4, TimeInterval::quarter( $datetime ) );
2018-09-20 14:20:04 +00:00
$datetime = new DateTime( '2018-01-01T00:00:00', self::$local_tz );
$this->assertEquals( 1, TimeInterval::quarter( $datetime ) );
2018-09-20 14:20:04 +00:00
$datetime = new DateTime( '2018-03-31T23:59:59', self::$local_tz );
$this->assertEquals( 1, TimeInterval::quarter( $datetime ) );
2018-09-20 14:20:04 +00:00
$datetime = new DateTime( '2018-04-01T00:00:00', self::$local_tz );
$this->assertEquals( 2, TimeInterval::quarter( $datetime ) );
2018-09-20 14:20:04 +00:00
$datetime = new DateTime( '2018-06-30T23:59:59', self::$local_tz );
$this->assertEquals( 2, TimeInterval::quarter( $datetime ) );
2018-09-20 14:20:04 +00:00
$datetime = new DateTime( '2018-07-01T00:00:00', self::$local_tz );
$this->assertEquals( 3, TimeInterval::quarter( $datetime ) );
2018-09-20 14:20:04 +00:00
$datetime = new DateTime( '2018-09-30T23:59:59', self::$local_tz );
$this->assertEquals( 3, TimeInterval::quarter( $datetime ) );
2018-09-20 14:20:04 +00:00
$datetime = new DateTime( '2018-10-01T00:00:00', self::$local_tz );
$this->assertEquals( 4, TimeInterval::quarter( $datetime ) );
2018-09-20 14:20:04 +00:00
$datetime = new DateTime( '2018-12-31T23:59:59', self::$local_tz );
$this->assertEquals( 4, TimeInterval::quarter( $datetime ) );
2018-09-20 14:20:04 +00:00
}
2018-09-24 15:24:08 +00:00
/**
* Test simple week number function.
*/
2018-09-20 14:20:04 +00:00
public function test_simple_week_number() {
$expected_week_no = array(
2018-09-24 15:24:08 +00:00
'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,
),
2018-09-20 14:20:04 +00:00
);
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, self::$local_tz );
$this->assertEquals( $expected_week_no[ $date ][ $first_day_of_week ], TimeInterval::simple_week_number( $datetime, $first_day_of_week ), "First day of week: $first_day_of_week; Date: $date" );
2018-09-20 14:20:04 +00:00
}
}
}
2018-09-24 15:24:08 +00:00
/**
* Testing ISO week number function.
*/
2018-09-20 14:20:04 +00:00
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, self::$local_tz );
$this->assertEquals( $expected_week_no[ $date ], TimeInterval::week_number( $datetime, 1 ), "ISO week number for date: $date" );
2018-09-20 14:20:04 +00:00
}
}
2018-09-24 15:24:08 +00:00
/**
* Test function counting intervals between two datetimes.
*/
2018-09-20 14:20:04 +00:00
public function test_intervals_between() {
2018-09-24 15:24:08 +00:00
// Please note that all intervals are inclusive on both sides.
2018-09-20 14:20:04 +00:00
$test_settings = array(
// 0 interval length, should just return 1.
array(
'start' => '2017-12-24T11:00:00',
'end' => '2017-12-24T11:00:00',
2018-09-20 14:20:04 +00:00
'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:00',
'end' => '2017-12-24T11:40:00',
2018-09-20 14:20:04 +00:00
'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:00',
'end' => '2017-12-24T12:40:00',
2018-09-20 14:20:04 +00:00
'week_start' => 1,
'intervals' => array(
'hour' => 2,
'day' => 1,
'week' => 1,
'month' => 1,
'quarter' => 1,
'year' => 1,
),
),
// 8.0186111 hours interval length -> 10 hours, 2 days, 1 for the rest
array(
'start' => '2019-01-16T16:59:00',
'end' => '2019-01-17T01:00:07',
'week_start' => 1,
'intervals' => array(
'hour' => 10,
'day' => 2,
'week' => 1,
'month' => 1,
'quarter' => 1,
'year' => 1,
),
),
// 23:59:59 h:m:s interval -> 24 hours, 2 days, 1 for the rest
2018-09-20 14:20:04 +00:00
array(
'start' => '2017-12-24T11:00:00',
'end' => '2017-12-25T10:59:59',
2018-09-20 14:20:04 +00:00
'week_start' => 1,
'intervals' => array(
'hour' => 24,
'day' => 2,
2018-09-20 14:20:04 +00:00
'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:00',
'end' => '2017-12-25T11:00:00',
2018-09-20 14:20:04 +00:00
'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:00',
'end' => '2017-11-30T23:59:59',
2018-09-20 14:20:04 +00:00
'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:00',
'end' => '2017-12-24T11:00:00',
2018-09-20 14:20:04 +00:00
'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, 32 days, 6 iso weeks, 2 months
2018-09-20 14:20:04 +00:00
array(
'start' => '2017-11-24T11:00:00',
'end' => '2017-12-25T01:00:00',
2018-09-20 14:20:04 +00:00
'week_start' => 1,
'intervals' => array(
'hour' => 30 * 24 + 13 + 2, // 30 full days + 13 full hours on Nov 24 + 2 hours on Dec 25
'day' => 32,
2018-09-20 14:20:04 +00:00
'week' => 6,
'month' => 2,
'quarter' => 1,
'year' => 1,
),
),
// 1 month interval spanning 2 months and 2 quarters, 1 year -> 720 hours, 31 days, 6 iso weeks, 2 months, 2 q, 1 y
2018-09-20 14:20:04 +00:00
array(
'start' => '2017-09-24T11:00:00',
'end' => '2017-10-24T10:59:59',
2018-09-20 14:20:04 +00:00
'week_start' => 1,
'intervals' => array(
'hour' => 30 * 24,
'day' => 31, // Sept has 30 days, but need to include interval for half of Sept 24 and interval for half of Oct 24.
2018-09-20 14:20:04 +00:00
'week' => 6,
'month' => 2,
'quarter' => 2,
'year' => 1,
),
),
// 1 month interval spanning 2 months and 2 quarters, 2 years -> 744 hours, 32 days, 6 iso weeks, 2 months, 2 quarters, 2 years
2018-09-20 14:20:04 +00:00
array(
'start' => '2017-12-24T11:00:00',
'end' => '2018-01-24T10:59:59',
2018-09-20 14:20:04 +00:00
'week_start' => 1,
'intervals' => array(
'hour' => 31 * 24,
'day' => 32, // Dec has 31 days, plus 1 interval for half day at the end.
2018-09-20 14:20:04 +00:00
'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:00',
'end' => '2017-12-31T23:59:59',
2018-09-20 14:20:04 +00:00
'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:00',
'end' => '2017-12-30T23:59:59',
2018-09-20 14:20:04 +00:00
'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:00',
'end' => '2018-01-31T23:59:59',
2018-09-20 14:20:04 +00:00
'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:00',
'end' => '2018-01-01T00:00:00',
2018-09-20 14:20:04 +00:00
'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:00',
'end' => '2018-01-01T00:00:00',
2018-09-20 14:20:04 +00:00
'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 ) {
$start_datetime = new DateTime( $setting['start'], self::$local_tz );
$end_datetime = new DateTime( $setting['end'], self::$local_tz );
$this->assertEquals( $exp_value, TimeInterval::intervals_between( $start_datetime, $end_datetime, $interval ), "First Day of Week: {$setting['week_start']}; Start: {$setting['start']}; End: {$setting['end']}; Interval: {$interval}" );
2018-09-20 14:20:04 +00:00
}
}
}
2018-09-24 15:24:08 +00:00
/**
* Test function that returns beginning of next hour.
*/
2018-09-20 14:20:04 +00:00
public function test_next_hour_start() {
$settings = array(
'2017-12-30T00:00:00' => array(
0 => '2017-12-30T01:00:00',
1 => '2017-12-29T23:59:59',
2018-09-20 14:20:04 +00:00
),
'2017-12-30T10:00:00' => array(
0 => '2017-12-30T11:00:00',
1 => '2017-12-30T09:59:59',
2018-09-20 14:20:04 +00:00
),
);
foreach ( $settings as $datetime_s => $setting ) {
$datetime = new DateTime( $datetime_s, self::$local_tz );
2018-09-20 14:20:04 +00:00
foreach ( $setting as $reversed => $exp_value ) {
$result_dt = TimeInterval::next_hour_start( $datetime, $reversed );
$this->assertEquals( $exp_value, $result_dt->format( TimeInterval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" );
2018-09-20 14:20:04 +00:00
}
}
}
2018-09-24 15:24:08 +00:00
/**
* Test function that returns beginning of next day.
*/
2018-09-20 14:20:04 +00:00
public function test_next_day_start() {
$settings = array(
'2017-12-30T00:00:00' => array(
0 => '2017-12-31T00:00:00',
1 => '2017-12-29T23:59:59',
2018-09-20 14:20:04 +00:00
),
'2017-12-30T10:00:00' => array(
0 => '2017-12-31T00:00:00',
1 => '2017-12-29T23:59:59',
2018-09-20 14:20:04 +00:00
),
);
foreach ( $settings as $datetime_s => $setting ) {
$datetime = new DateTime( $datetime_s, self::$local_tz );
2018-09-20 14:20:04 +00:00
foreach ( $setting as $reversed => $exp_value ) {
$result_dt = TimeInterval::next_day_start( $datetime, $reversed );
$this->assertEquals( $exp_value, $result_dt->format( TimeInterval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" );
2018-09-20 14:20:04 +00:00
}
}
}
2018-09-24 15:24:08 +00:00
/**
* Test function that returns beginning of next week, for weeks starting on Monday.
*/
2018-09-20 14:20:04 +00:00
public function test_next_week_start_ISO_week() {
update_option( 'start_of_week', 1 );
$settings = array(
'2017-12-30T00:00:00' => array(
0 => '2018-01-01T00:00:00',
1 => '2017-12-24T23:59:59',
2018-09-20 14:20:04 +00:00
),
'2017-12-30T10:00:00' => array(
0 => '2018-01-01T00:00:00',
1 => '2017-12-24T23:59:59',
2018-09-20 14:20:04 +00:00
),
'2010-12-25T10:00:00' => array(
0 => '2010-12-27T00:00:00',
1 => '2010-12-19T23:59:59',
2018-09-20 14:20:04 +00:00
),
'2010-12-26T10:00:00' => array(
0 => '2010-12-27T00:00:00',
1 => '2010-12-19T23:59:59',
2018-09-20 14:20:04 +00:00
),
'2010-12-27T00:00:00' => array(
0 => '2011-01-03T00:00:00',
1 => '2010-12-26T23:59:59',
2018-09-20 14:20:04 +00:00
),
'2010-12-31T00:00:00' => array(
0 => '2011-01-03T00:00:00',
1 => '2010-12-26T23:59:59',
2018-09-20 14:20:04 +00:00
),
'2011-01-01T00:00:00' => array(
0 => '2011-01-03T00:00:00',
1 => '2010-12-26T23:59:59',
2018-09-20 14:20:04 +00:00
),
'2011-01-03T00:00:00' => array(
0 => '2011-01-10T00:00:00',
1 => '2011-01-02T23:59:59',
2018-09-20 14:20:04 +00:00
),
);
foreach ( $settings as $datetime_s => $setting ) {
$datetime = new DateTime( $datetime_s, self::$local_tz );
2018-09-20 14:20:04 +00:00
foreach ( $setting as $reversed => $exp_value ) {
$result_dt = TimeInterval::next_week_start( $datetime, $reversed );
$this->assertEquals( $exp_value, $result_dt->format( TimeInterval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" );
2018-09-20 14:20:04 +00:00
}
}
}
2018-09-24 15:24:08 +00:00
/**
* Test function that returns beginning of next week, for weeks starting on Sunday.
*/
2018-09-20 14:20:04 +00:00
public function test_next_week_start_Sunday_based_week() {
update_option( 'start_of_week', 7 );
$settings = array(
'2010-12-25T10:00:00' => array(
0 => '2010-12-26T00:00:00',
1 => '2010-12-18T23:59:59',
2018-09-20 14:20:04 +00:00
),
'2010-12-26T10:00:00' => array(
0 => '2011-01-01T00:00:00',
1 => '2010-12-25T23:59:59',
2018-09-20 14:20:04 +00:00
),
'2011-01-01T00:00:00' => array(
0 => '2011-01-02T00:00:00',
1 => '2010-12-31T23:59:59',
2018-09-20 14:20:04 +00:00
),
'2011-01-02T00:00:00' => array(
0 => '2011-01-09T00:00:00',
1 => '2011-01-01T23:59:59',
2018-09-20 14:20:04 +00:00
),
);
foreach ( $settings as $datetime_s => $setting ) {
$datetime = new DateTime( $datetime_s, self::$local_tz );
2018-09-20 14:20:04 +00:00
foreach ( $setting as $reversed => $exp_value ) {
$result_dt = TimeInterval::next_week_start( $datetime, $reversed );
$this->assertEquals( $exp_value, $result_dt->format( TimeInterval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" );
2018-09-20 14:20:04 +00:00
}
}
}
2018-09-24 15:24:08 +00:00
/**
* Test function that returns beginning of next month.
*/
2018-09-20 14:20:04 +00:00
public function test_next_month_start() {
$settings = array(
'2017-12-30T00:00:00' => array(
0 => '2018-01-01T00:00:00',
1 => '2017-11-30T23:59:59',
2018-09-20 14:20:04 +00:00
),
// Leap year reversed test.
'2016-03-05T10:00:00' => array(
0 => '2016-04-01T00:00:00',
1 => '2016-02-29T23:59:59',
2018-09-20 14:20:04 +00:00
),
);
foreach ( $settings as $datetime_s => $setting ) {
$datetime = new DateTime( $datetime_s, self::$local_tz );
2018-09-20 14:20:04 +00:00
foreach ( $setting as $reversed => $exp_value ) {
$result_dt = TimeInterval::next_month_start( $datetime, $reversed );
$this->assertEquals( $exp_value, $result_dt->format( TimeInterval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" );
2018-09-20 14:20:04 +00:00
}
}
}
2018-09-24 15:24:08 +00:00
/**
* Test function that returns beginning of next quarter.
*/
2018-09-20 14:20:04 +00:00
public function test_next_quarter_start() {
$settings = array(
'2017-12-31T00:00:00' => array(
0 => '2018-01-01T00:00:00',
1 => '2017-09-30T23:59:59',
2018-09-20 14:20:04 +00:00
),
'2018-01-01T10:00:00' => array(
0 => '2018-04-01T00:00:00',
1 => '2017-12-31T23:59:59',
2018-09-20 14:20:04 +00:00
),
'2018-02-14T10:00:00' => array(
0 => '2018-04-01T00:00:00',
1 => '2017-12-31T23:59:59',
2018-09-20 14:20:04 +00:00
),
'2018-04-14T10:00:00' => array(
0 => '2018-07-01T00:00:00',
1 => '2018-03-31T23:59:59',
),
'2018-07-14T10:00:00' => array(
0 => '2018-10-01T00:00:00',
1 => '2018-06-30T23:59:59',
),
2018-09-20 14:20:04 +00:00
);
foreach ( $settings as $datetime_s => $setting ) {
$datetime = new DateTime( $datetime_s, self::$local_tz );
2018-09-20 14:20:04 +00:00
foreach ( $setting as $reversed => $exp_value ) {
$result_dt = TimeInterval::next_quarter_start( $datetime, $reversed );
$this->assertEquals( $exp_value, $result_dt->format( TimeInterval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" );
2018-09-20 14:20:04 +00:00
}
}
}
2018-09-24 15:24:08 +00:00
/**
* Test function that returns beginning of next year.
*/
2018-09-20 14:20:04 +00:00
public function test_next_year_start() {
$settings = array(
'2017-12-31T23:59:59' => array(
0 => '2018-01-01T00:00:00',
1 => '2016-12-31T23:59:59',
2018-09-20 14:20:04 +00:00
),
'2017-01-01T00:00:00' => array(
0 => '2018-01-01T00:00:00',
1 => '2016-12-31T23:59:59',
2018-09-20 14:20:04 +00:00
),
'2017-04-23T14:53:00' => array(
0 => '2018-01-01T00:00:00',
1 => '2016-12-31T23:59:59',
2018-09-20 14:20:04 +00:00
),
);
foreach ( $settings as $datetime_s => $setting ) {
$datetime = new DateTime( $datetime_s, self::$local_tz );
2018-09-20 14:20:04 +00:00
foreach ( $setting as $reversed => $exp_value ) {
$result_dt = TimeInterval::next_year_start( $datetime, $reversed );
$this->assertEquals( $exp_value, $result_dt->format( TimeInterval::$iso_datetime_format ), __FUNCTION__ . ": DT: $datetime_s; R: $reversed" );
2018-09-20 14:20:04 +00:00
}
}
}
/**
* Test function that normalizes *_between query parameters to *_min & *_max.
*/
2019-01-28 21:23:44 +00:00
public function test_normalize_between_params() {
$request = array(
'a_between' => 'malformed', // won't be normalized (not an array).
'b_between' => array( 1, 5 ), // results in min=1, max=5.
'c_between' => array( 4, 2 ), // results in min=2, max=4.
'd_between' => array( 7 ), // won't be normalized (only 1 item).
'f_between' => array( 10, 12 ), // not in params, skipped.
);
$params = array( 'a', 'b', 'c', 'd' );
$result = TimeInterval::normalize_between_params( $request, $params, false );
$expected = array(
'b_min' => 1,
'b_max' => 5,
'c_min' => 2,
'c_max' => 4,
);
$this->assertEquals( $result, $expected );
}
/**
2019-01-28 22:01:25 +00:00
* Test function that normalizes *_between query parameters for dates to *_after & *_before.
*/
public function test_normalize_between_date_params() {
$request = array(
'a_between' => 'malformed', // won't be normalized (not an array).
'b_between' => array( 1, 5 ), // results in after=1, before=5.
'c_between' => array( 4, 2 ), // results in after=2, before=4.
'd_between' => array( 7 ), // won't be normalized (only 1 item).
'f_between' => array( 10, 12 ), // not in params, skipped.
);
$params = array( 'a', 'b', 'c', 'd' );
$result = TimeInterval::normalize_between_params( $request, $params, true );
2019-01-28 22:01:25 +00:00
$expected = array(
2019-02-14 23:12:15 +00:00
'b_after' => 1,
2019-01-28 22:01:25 +00:00
'b_before' => 5,
2019-02-14 23:12:15 +00:00
'c_after' => 2,
2019-01-28 22:01:25 +00:00
'c_before' => 4,
);
$this->assertEquals( $result, $expected );
}
/**
* Test function that validates *_between query parameters for numeric values.
*/
public function test_rest_validate_between_numeric_arg() {
$this->assertWPError(
TimeInterval::rest_validate_between_numeric_arg( 'not array', null, 'param' ),
'param is not a numerically indexed array.'
);
$this->assertWPError(
TimeInterval::rest_validate_between_numeric_arg( array( 1 ), null, 'param' ),
'param must contain 2 numbers.'
);
$this->assertTrue(
TimeInterval::rest_validate_between_numeric_arg( array( 1, 2 ), null, 'param' )
);
}
2019-01-28 22:01:25 +00:00
/**
* Test function that validates *_between query parameters for date values.
*/
public function rest_validate_between_date_arg() {
$this->assertWPError(
TimeInterval::rest_validate_between_date_arg( 'not array', null, 'param' ),
2019-01-28 22:01:25 +00:00
'param is not a numerically indexed array.'
);
$this->assertWPError(
TimeInterval::rest_validate_between_date_arg( array( '2019-01-01T00:00:00' ), null, 'param' ),
2019-01-28 22:01:25 +00:00
'param must contain 2 valid dates.'
);
$this->assertWPError(
TimeInterval::rest_validate_between_date_arg( array( 'not a valid date' ), null, 'param' ),
2019-01-28 22:01:25 +00:00
'param must contain 2 valid dates.'
);
$this->assertTrue(
TimeInterval::rest_validate_between_date_arg( array( '2019-01-01T00:00:00', '2019-01-15T00:00:00' ), null, 'param' )
2019-01-28 22:01:25 +00:00
);
}
2018-09-20 14:20:04 +00:00
}