From: Aidan Saunders Date: Thu, 8 Nov 2018 12:39:06 +0000 (+0000) Subject: Fix wrong current month showing on Membership Dashboard X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=d2f262dacebf33beb8d3c0541653471e9325f579;hp=3a327615280dc2fd33750d40a634cf4a1250f516;p=civicrm-core.git Fix wrong current month showing on Membership Dashboard Remove stray dash after Last Month name Add tests for new CRM_Utils_Date::customFormatTs() and existing CRM_Utils_Date::customFormat() dev/core#511 --- diff --git a/CRM/Member/Page/DashBoard.php b/CRM/Member/Page/DashBoard.php index a0a6e4bb03..cc6d8e6a57 100644 --- a/CRM/Member/Page/DashBoard.php +++ b/CRM/Member/Page/DashBoard.php @@ -428,7 +428,7 @@ class CRM_Member_Page_DashBoard extends CRM_Core_Page { $this->assign('membershipSummary', $membershipSummary); $this->assign('totalCount', $totalCount); - $this->assign('month', CRM_Utils_Date::customFormat($monthStartTs, '%B')); + $this->assign('month', CRM_Utils_Date::customFormatTs($monthStartTs, '%B')); $this->assign('year', date('Y', $monthStartTs)); $this->assign('premonth', CRM_Utils_Date::customFormat($preMonth, '%B')); $this->assign('currentMonth', date('F')); diff --git a/CRM/Utils/Date.php b/CRM/Utils/Date.php index 74aa2e35ca..a9795c42f0 100644 --- a/CRM/Utils/Date.php +++ b/CRM/Utils/Date.php @@ -438,6 +438,23 @@ class CRM_Utils_Date { } } + /** + * Wrapper for customFormat that takes a timestamp + * + * @param int $timestamp + * Date and time in timestamp format. + * @param string $format + * The output format. + * @param array $dateParts + * An array with the desired date parts. + * + * @return string + * the $format-formatted $date + */ + public static function customFormatTs($timestamp, $format = NULL, $dateParts = NULL) { + return CRM_Utils_Date::customFormat(date("Y-m-d H:i:s", $timestamp), $format, $dateParts); + } + /** * Converts the date/datetime from MySQL format to ISO format * diff --git a/templates/CRM/Member/Page/DashBoard.tpl b/templates/CRM/Member/Page/DashBoard.tpl index 3b56964fe3..1dbf86d853 100644 --- a/templates/CRM/Member/Page/DashBoard.tpl +++ b/templates/CRM/Member/Page/DashBoard.tpl @@ -30,7 +30,7 @@ {ts}Members by Type{/ts} {if $preMonth} - {$premonth} – {ts}(Last Month){/ts} + {$premonth} {ts}(Last Month){/ts} {/if} {$month}{if $isCurrent}{ts} (MTD){/ts}{/if} diff --git a/tests/phpunit/CRM/Utils/DateTest.php b/tests/phpunit/CRM/Utils/DateTest.php index faa661c9e2..8730238fa9 100644 --- a/tests/phpunit/CRM/Utils/DateTest.php +++ b/tests/phpunit/CRM/Utils/DateTest.php @@ -173,4 +173,48 @@ class CRM_Utils_DateTest extends CiviUnitTestCase { } } + /** + * Test customFormat() function + */ + public function testCustomFormat() { + $dateTime = "2018-11-08 21:46:44"; + $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%b"), "Nov"); + $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%B"), "November"); + $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%d"), "08"); + $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%e"), " 8"); + $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%E"), "8"); + $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%f"), "th"); + $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%H"), "21"); + $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%I"), "09"); + $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%k"), "21"); + $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%l"), " 9"); + $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%m"), "11"); + $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%M"), "46"); + $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%p"), "pm"); + $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%P"), "PM"); + $this->assertEquals(CRM_Utils_Date::customFormat($dateTime, "%Y"), "2018"); + } + + /** + * Test customFormat() function + */ + public function testCustomFormatTs() { + $ts = mktime(21, 46, 44, 11, 8, 2018); + $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%b"), "Nov"); + $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%B"), "November"); + $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%d"), "08"); + $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%e"), " 8"); + $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%E"), "8"); + $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%f"), "th"); + $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%H"), "21"); + $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%I"), "09"); + $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%k"), "21"); + $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%l"), " 9"); + $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%m"), "11"); + $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%M"), "46"); + $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%p"), "pm"); + $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%P"), "PM"); + $this->assertEquals(CRM_Utils_Date::customFormatTs($ts, "%Y"), "2018"); + } + }