Skip to content

Commit

Permalink
Merge pull request #12682 from eileenmcnaughton/date_extension
Browse files Browse the repository at this point in the history
Genericise 'ending' date filter and add tests
  • Loading branch information
eileenmcnaughton authored Nov 8, 2018
2 parents f6a3ccd + ad336d6 commit 0938511
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
54 changes: 54 additions & 0 deletions CRM/Utils/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,17 @@ public static function relativeToAbsolute($relativeTerm, $unit) {
$to['M'] = $now['mon'];
$to['Y'] = $now['year'] + 1;
break;

default:
if ($relativeTermPrefix === 'ending') {
$to['d'] = $now['mday'];
$to['M'] = $now['mon'];
$to['Y'] = $now['year'];
$to['H'] = 23;
$to['i'] = $to['s'] = 59;
$from = self::intervalAdd('year', -$relativeTermSuffix, $to);
$from = self::intervalAdd('second', 1, $from);
}
}
break;

Expand Down Expand Up @@ -1433,6 +1444,17 @@ public static function relativeToAbsolute($relativeTerm, $unit) {
$to = self::intervalAdd('day', 90, $from);
$to = self::intervalAdd('second', -1, $to);
break;

default:
if ($relativeTermPrefix === 'ending') {
$to['d'] = $now['mday'];
$to['M'] = $now['mon'];
$to['Y'] = $now['year'];
$to['H'] = 23;
$to['i'] = $to['s'] = 59;
$from = self::intervalAdd('month', -($relativeTermSuffix * 3), $to);
$from = self::intervalAdd('second', 1, $from);
}
}
break;

Expand Down Expand Up @@ -1602,6 +1624,17 @@ public static function relativeToAbsolute($relativeTerm, $unit) {
$to = self::intervalAdd('day', 60, $from);
$to = self::intervalAdd('second', -1, $to);
break;

default:
if ($relativeTermPrefix === 'ending') {
$to['d'] = $now['mday'];
$to['M'] = $now['mon'];
$to['Y'] = $now['year'];
$to['H'] = 23;
$to['i'] = $to['s'] = 59;
$from = self::intervalAdd($unit, -$relativeTermSuffix, $to);
$from = self::intervalAdd('second', 1, $from);
}
}
break;

Expand Down Expand Up @@ -1719,6 +1752,17 @@ public static function relativeToAbsolute($relativeTerm, $unit) {
$to = self::intervalAdd('day', 7, $from);
$to = self::intervalAdd('second', -1, $to);
break;

default:
if ($relativeTermPrefix === 'ending') {
$to['d'] = $now['mday'];
$to['M'] = $now['mon'];
$to['Y'] = $now['year'];
$to['H'] = 23;
$to['i'] = $to['s'] = 59;
$from = self::intervalAdd($unit, -$relativeTermSuffix, $to);
$from = self::intervalAdd('second', 1, $from);
}
}
break;

Expand Down Expand Up @@ -1782,6 +1826,16 @@ public static function relativeToAbsolute($relativeTerm, $unit) {
$from['Y'] = $to['Y'];
break;

default:
if ($relativeTermPrefix === 'ending') {
$to['d'] = $now['mday'];
$to['M'] = $now['mon'];
$to['Y'] = $now['year'];
$to['H'] = 23;
$to['i'] = $to['s'] = 59;
$from = self::intervalAdd($unit, -$relativeTermSuffix, $to);
$from = self::intervalAdd('second', 1, $from);
}
}
break;
}
Expand Down
37 changes: 37 additions & 0 deletions tests/phpunit/CRM/Utils/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,43 @@ public function testRelativeToAbsoluteYear() {
}
}

/**
* Test relativeToAbsolute function on a range of year options.
*
* Go backwards one year at a time through the sequence.
*/
public function testRelativeEnding() {
$relativeDateValues = [
'ending.week' => '- 6 days',
'ending_30.day' => '- 29 days',
'ending.year' => '- 1 year + 1 day',
'ending_90.day' => '- 89 days',
'ending_60.day' => '- 59 days',
'ending_2.year' => '- 2 years + 1 day',
'ending_3.year' => '- 3 years + 1 day',
'ending_18.year' => '- 18 years + 1 day',
'ending_18.quarter' => '- 54 months + 1 day',
'ending_18.week' => '- 18 weeks + 1 day',
'ending_18.month' => '- 18 months + 1 day',
'ending_18.day' => '- 17 days',
];

foreach ($relativeDateValues as $key => $value) {
$parts = explode('.', $key);
$date = CRM_Utils_Date::relativeToAbsolute($parts[0], $parts[1]);
$this->assertEquals([
'from' => date('Ymd000000', strtotime($value)),
'to' => date('Ymd235959'),
], $date, 'relative term is ' . $key);
}

$date = CRM_Utils_Date::relativeToAbsolute('ending', 'month');
$this->assertEquals([
'from' => date('Ymd000000', strtotime('- 29 days')),
'to' => date('Ymd235959'),
], $date, 'relative term is ending.week');
}

/**
* Test relativeToAbsolute function on a range of year options.
*
Expand Down
6 changes: 3 additions & 3 deletions xml/templates/civicrm_data.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -986,9 +986,9 @@ VALUES
(@option_group_id_date_filter, '{ts escape="sql"}Previous fiscal year{/ts}', 'previous.fiscal_year', 'previous.fiscal_year', NULL, NULL, NULL,11, NULL, 0, 0, 1, NULL, NULL, NULL),
(@option_group_id_date_filter, '{ts escape="sql"}Previous calendar year{/ts}', 'previous.year', 'previous.year', NULL, NULL, NULL,12, NULL, 0, 0, 1, NULL, NULL, NULL),
(@option_group_id_date_filter, '{ts escape="sql"}Last 7 days including today{/ts}', 'ending.week', 'ending.week', NULL, NULL, NULL,13, NULL, 0, 0, 1, NULL, NULL, NULL),
(@option_group_id_date_filter, '{ts escape="sql"}Last 30 days including today{/ts}', 'ending.month', 'ending.month', NULL, NULL, NULL,14, NULL, 0, 0, 1, NULL, NULL, NULL),
(@option_group_id_date_filter, '{ts escape="sql"}Last 60 days including today{/ts}', 'ending_2.month', 'ending_2.month', NULL, NULL, NULL,15, NULL, 0, 0, 1, NULL, NULL, NULL),
(@option_group_id_date_filter, '{ts escape="sql"}Last 90 days including today{/ts}', 'ending.quarter', 'ending.quarter', NULL, NULL, NULL,16, NULL, 0, 0, 1, NULL, NULL, NULL),
(@option_group_id_date_filter, '{ts escape="sql"}Last 30 days including today{/ts}', 'ending_30.day', 'ending.month', NULL, NULL, NULL,14, NULL, 0, 0, 1, NULL, NULL, NULL),
(@option_group_id_date_filter, '{ts escape="sql"}Last 60 days including today{/ts}', 'ending_60.day', 'ending_2.month', NULL, NULL, NULL,15, NULL, 0, 0, 1, NULL, NULL, NULL),
(@option_group_id_date_filter, '{ts escape="sql"}Last 90 days including today{/ts}', 'ending_90.day', 'ending.quarter', NULL, NULL, NULL,16, NULL, 0, 0, 1, NULL, NULL, NULL),
(@option_group_id_date_filter, '{ts escape="sql"}Last 12 months including today{/ts}', 'ending.year', 'ending.year', NULL, NULL, NULL,17, NULL, 0, 0, 1, NULL, NULL, NULL),
(@option_group_id_date_filter, '{ts escape="sql"}Last 2 years including today{/ts}', 'ending_2.year', 'ending_2.year', NULL, NULL, NULL,18, NULL, 0, 0, 1, NULL, NULL, NULL),
(@option_group_id_date_filter, '{ts escape="sql"}Last 3 years including today{/ts}', 'ending_3.year', 'ending_3.year', NULL, NULL, NULL,19, NULL, 0, 0, 1, NULL, NULL, NULL),
Expand Down

0 comments on commit 0938511

Please sign in to comment.