From 5f95ae6806d884ebef1be7ddb626fdbd487f9472 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 17 Sep 2021 10:22:33 +1200 Subject: [PATCH] Add support for CiviCRM date formats in crmDate --- CRM/Core/Smarty/plugins/modifier.crmDate.php | 26 ++++++++++++-- tests/phpunit/CRM/Core/TokenSmartyTest.php | 38 +++++++++++++++++--- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/CRM/Core/Smarty/plugins/modifier.crmDate.php b/CRM/Core/Smarty/plugins/modifier.crmDate.php index bef94f9d97..2a175bcfc0 100644 --- a/CRM/Core/Smarty/plugins/modifier.crmDate.php +++ b/CRM/Core/Smarty/plugins/modifier.crmDate.php @@ -21,14 +21,36 @@ * @param string $dateString * Date which needs to converted to human readable format. * - * @param null $dateFormat + * @param string|null $dateFormat + * A string per https://www.php.net/manual/en/function.strftime.php or + * one of our configured formats name - eg + * - dateformatDatetime + * - dateformatFull + * - dateformatPartial + * - dateformatTime + * - dateformatYear + * - dateformatFinancialBatch + * - dateformatshortdate + * * @param bool $onlyTime * * @return string * human readable date format | invalid date message */ -function smarty_modifier_crmDate($dateString, $dateFormat = NULL, $onlyTime = FALSE) { +function smarty_modifier_crmDate($dateString, ?string $dateFormat = NULL, bool $onlyTime = FALSE): string { if ($dateString) { + $configuredFormats = [ + 'Datetime', + 'Full', + 'Partial', + 'Time', + 'Year', + 'FinancialBatch', + 'shortdate', + ]; + if (in_array($dateFormat, $configuredFormats, TRUE)) { + $dateFormat = Civi::settings()->get('dateformat' . $dateFormat); + } // this check needs to be type sensitive // CRM-3689, CRM-2441 if ($dateFormat === 0) { diff --git a/tests/phpunit/CRM/Core/TokenSmartyTest.php b/tests/phpunit/CRM/Core/TokenSmartyTest.php index 8704c42b53..328e312bd4 100644 --- a/tests/phpunit/CRM/Core/TokenSmartyTest.php +++ b/tests/phpunit/CRM/Core/TokenSmartyTest.php @@ -56,7 +56,7 @@ class CRM_Core_TokenSmartyTest extends CiviUnitTestCase { /** * A template that specifically opts out of Smarty. */ - public function testDisableSmarty() { + public function testDisableSmarty(): void { $rendered = CRM_Core_TokenSmarty::render( ['msg_subject' => 'First name is {contact.first_name}. ExtraFoo is {$extra.foo}.'], ['contactId' => $this->contactId, 'smarty' => FALSE], @@ -65,10 +65,40 @@ class CRM_Core_TokenSmartyTest extends CiviUnitTestCase { $this->assertEquals('First name is Bob. ExtraFoo is {$extra.foo}.', $rendered['msg_subject']); } + /** + * Test smarty rendered dates using the setting short name. + * + * @param string $format + * @param string $expected + * + * @dataProvider getDateFormats + */ + public function testSmartySettingDates(string $format, string $expected = ''): void { + $date = '2010-09-19 13:34:45'; + CRM_Core_Smarty::singleton()->assign('date', $date); + $string = '{$date|crmDate:' . $format . '}'; + $this->assertEquals($expected, CRM_Utils_String::parseOneOffStringThroughSmarty($string)); + } + + /** + * Get date formats to test. + */ + public function getDateFormats(): array { + return [ + ['Full', 'September 19th, 2010'], + ['Datetime', 'September 19th, 2010 1:34 PM'], + ['Partial', 'September 2010'], + ['Time', ' 1:34 PM'], + ['Year', '2010'], + ['FinancialBatch', '09/19/2010'], + ['shortdate', '09/19/2010'], + ]; + } + /** * Someone malicious gives cutesy expressions (via token-content) that tries to provoke extra evaluation. */ - public function testCutesyTokenData() { + public function testCutesyTokenData(): void { $cutesyContactId = $this->individualCreate([ 'first_name' => '{$extra.foo}{contact.last_name}', 'last_name' => 'Roberts', @@ -84,7 +114,7 @@ class CRM_Core_TokenSmartyTest extends CiviUnitTestCase { /** * Someone malicious gives cutesy expressions (via Smarty-content) that tries to provoke extra evaluation. */ - public function testCutesySmartyData() { + public function testCutesySmartyData(): void { $rendered = CRM_Core_TokenSmarty::render( ['msg_subject' => 'First name is {contact.first_name}. ExtraFoo is {$extra.foo}.'], ['contactId' => $this->contactId], @@ -96,7 +126,7 @@ class CRM_Core_TokenSmartyTest extends CiviUnitTestCase { /** * The same tokens are used in multiple parts of the template - without redundant evaluation. */ - public function testDataLoadCount() { + public function testDataLoadCount(): void { // Define a token `{counter.i}` which increments whenever tokens are evaluated. Civi::dispatcher()->addListener('civi.token.eval', function (\Civi\Token\Event\TokenValueEvent $e) { static $i; -- 2.25.1