From ad336d61afe346478f78fc232faa9f9c33bdee93 Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 17 Aug 2018 14:48:35 +1200 Subject: [PATCH] Genericise 'ending' date filter and add tests 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 (This makes that change on new installs only) --- CRM/Utils/Date.php | 54 ++++++++++++++++++++++++++++ tests/phpunit/CRM/Utils/DateTest.php | 37 +++++++++++++++++++ xml/templates/civicrm_data.tpl | 6 ++-- 3 files changed, 94 insertions(+), 3 deletions(-) diff --git a/CRM/Utils/Date.php b/CRM/Utils/Date.php index 74aa2e35ca..b2316eca75 100644 --- a/CRM/Utils/Date.php +++ b/CRM/Utils/Date.php @@ -1209,6 +1209,17 @@ class CRM_Utils_Date { $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; @@ -1433,6 +1444,17 @@ class CRM_Utils_Date { $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; @@ -1602,6 +1624,17 @@ class CRM_Utils_Date { $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; @@ -1719,6 +1752,17 @@ class CRM_Utils_Date { $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; @@ -1782,6 +1826,16 @@ class CRM_Utils_Date { $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; } diff --git a/tests/phpunit/CRM/Utils/DateTest.php b/tests/phpunit/CRM/Utils/DateTest.php index faa661c9e2..d930c360c7 100644 --- a/tests/phpunit/CRM/Utils/DateTest.php +++ b/tests/phpunit/CRM/Utils/DateTest.php @@ -128,6 +128,43 @@ class CRM_Utils_DateTest extends CiviUnitTestCase { } } + /** + * 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. * diff --git a/xml/templates/civicrm_data.tpl b/xml/templates/civicrm_data.tpl index d20de95e1c..5c256e5143 100644 --- a/xml/templates/civicrm_data.tpl +++ b/xml/templates/civicrm_data.tpl @@ -984,9 +984,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), -- 2.25.1