Skip to content

Commit

Permalink
Genericise 'ending' date filter and add tests
Browse files Browse the repository at this point in the history
Currently we have hard coded variants of 'ending_2.year' for 'Last 2 years including today' and
for month, week and quarter we support last month ending today etc. This adds tests
for the units already existing and also adds support (but no UI entries) for
ending_n.month
ending_n.week
ending_n.year
ending_n.day
ending_n.quarter

There is a complexity in that the current month entries are
ending_2.month = 'Last 60 days ending today'
ending.quarter = 'Last 90 days ending today'

I think it was done this way primarily to get the language right but as the period gets longer
(my interest is in supporting 18 months) the mismatch between 30 days & a month becomes more &
more of an issue.

I haven't focussed on any code rationalisation as I wanted to keep this to
additional code not changes to current & discuss / agree if this approach (ie. for the 'n'
entries we are respectful of actual month lengths & we leave it to future option value
enterers to worry about the wording.

I actually feel a pretty good case could be made for replacing the default
entries so instead of

ending_2.month = Last 60 days including today

we have

ending_60.day = Last 60 days including today

(I would probably do that on new installs only if we go that way)
  • Loading branch information
eileenmcnaughton committed Aug 17, 2018
1 parent 0114e4a commit ad7c965
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 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.month' => '- 29 days',
'ending.year' => '- 1 year + 1 day',
'ending.quarter' => '- 90 days + 1 day',
'ending_2.month' => '- 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

0 comments on commit ad7c965

Please sign in to comment.