Add support for CiviCRM date formats in crmDate
authorEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 16 Sep 2021 22:22:33 +0000 (10:22 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 17 Sep 2021 05:23:39 +0000 (17:23 +1200)
CRM/Core/Smarty/plugins/modifier.crmDate.php
tests/phpunit/CRM/Core/TokenSmartyTest.php

index bef94f9d971514bbf2d84013c1a285473de9bb8b..2a175bcfc0132a7c3ad1a6b53d690143fc019a5d 100644 (file)
  * @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) {
index 8704c42b53db43d1ed8ceea44e45561a8b36de17..328e312bd4dc6c7eee11c7e1d7adb7b0a0424223 100644 (file)
@@ -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;