From 819e6707707b3b03ae8162392a097102cfb8e286 Mon Sep 17 00:00:00 2001 From: Jon goldberg Date: Wed, 28 Mar 2018 21:30:50 +1300 Subject: [PATCH] CRM-21148 - Refactor two near-identical functions into one --- CRM/Report/Form.php | 5 +- CRM/Utils/Date.php | 25 +++++--- tests/phpunit/CRM/Utils/DateTest.php | 90 ++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 12 deletions(-) create mode 100644 tests/phpunit/CRM/Utils/DateTest.php diff --git a/CRM/Report/Form.php b/CRM/Report/Form.php index 411122e90b..181cf0d7cd 100644 --- a/CRM/Report/Form.php +++ b/CRM/Report/Form.php @@ -2064,10 +2064,7 @@ class CRM_Report_Form extends CRM_Core_Form { * * @return array */ - public function getFromTo($relative, $from, $to, $fromTime = NULL, $toTime = NULL) { - if (empty($toTime)) { - $toTime = '235959'; - } + public function getFromTo($relative, $from, $to, $fromTime = NULL, $toTime = '235959') { //FIX ME not working for relative if ($relative) { list($term, $unit) = CRM_Utils_System::explode('.', $relative, 2); diff --git a/CRM/Utils/Date.php b/CRM/Utils/Date.php index 82977dfc6e..bad74c87db 100644 --- a/CRM/Utils/Date.php +++ b/CRM/Utils/Date.php @@ -794,23 +794,32 @@ class CRM_Utils_Date { * @param date $relative * Eg: term.unit. * - * @param $from - * @param $to + * @param string $from + * @param string $to + * @param string $fromTime + * @param string $toTime * * @return array * start date, end date */ - public static function getFromTo($relative, $from, $to) { + public static function getFromTo($relative, $from, $to, $fromTime = NULL, $toTime = '235959') { if ($relative) { - list($term, $unit) = explode('.', $relative); + list($term, $unit) = explode('.', $relative, 2); $dateRange = self::relativeToAbsolute($term, $unit); - $from = $dateRange['from']; - //Take only Date Part, Sometime Time part is also present in 'to' + $from = substr($dateRange['from'], 0, 8); $to = substr($dateRange['to'], 0, 8); + // relativeToAbsolute returns 8 char date strings or 14 char + // time strings. + if (strlen($dateRange['from']) === 14) { + $fromTime = substr($dateRange['from'], -6, 6); + } + if (strlen($dateRange['to']) === 14) { + $fromTime = substr($dateRange['to'], -6, 6); + } } - $from = self::processDate($from); - $to = self::processDate($to, '235959'); + $from = self::processDate($from, $fromTime); + $to = self::processDate($to, $toTime); return array($from, $to); } diff --git a/tests/phpunit/CRM/Utils/DateTest.php b/tests/phpunit/CRM/Utils/DateTest.php new file mode 100644 index 0000000000..56272d6d06 --- /dev/null +++ b/tests/phpunit/CRM/Utils/DateTest.php @@ -0,0 +1,90 @@ + + */ + +/** + * Test CRM_Utils_Date functions. + * + * @package CiviCRM + * @group headless + */ +class CRM_Utils_DateTest extends CiviUnitTestCase { + + public function setUp() { + // There are only unit tests here at present, we can skip database loading. + return TRUE; + } + + public function tearDown() { + // There are only unit tests here at present, we can skip database loading. + return TRUE; + } + + public function fromToData() { + $cases = array(); + // Absolute dates + $cases[] = array('20170901000000', '20170913235959', 0, '09/01/2017', '09/13/2017'); + // "Today" relative date filter + $date = new DateTime(); + $expectedFrom = $date->format('Ymd') . '000000'; + $expectedTo = $date->format('Ymd') . '235959'; + $cases[] = array($expectedFrom, $expectedTo, 'this.day', '', ''); + // "yesterday" relative date filter + $date = new DateTime(); + $date->sub(new DateInterval('P1D')); + $expectedFrom = $date->format('Ymd') . '000000'; + $expectedTo = $date->format('Ymd') . '235959'; + $cases[] = array($expectedFrom, $expectedTo, 'previous.day', '', ''); + return $cases; + } + + /** + * Test that getFromTo returns the correct dates. + * + * @dataProvider fromToData + * @param $expectedFrom + * @param $expectedTo + * @param $relative + * @param $from + * @param $to + */ + public function testGetFromTo($expectedFrom, $expectedTo, $relative, $from, $to) { + $obj = new CRM_Utils_Date(); + list($calculatedFrom, $calculatedTo) = $obj->getFromTo($relative, $from, $to); + $this->assertEquals($expectedFrom, $calculatedFrom); + $this->assertEquals($expectedTo, $calculatedTo); + } + +} -- 2.25.1