From 34795e7ae24c47900a671925d23f536b7027cab2 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 23 Sep 2021 23:25:28 +1200 Subject: [PATCH] Extend token date handling to 'most' date fields Note I hit an error locally in tests on variant of this - so throwing against jenkins as my first stop --- CRM/Core/EntityTokens.php | 8 ++++++- Civi/Token/TokenProcessor.php | 4 ++++ Civi/Token/TokenRow.php | 2 +- .../Form/Task/PDFLetterCommonTest.php | 24 +++++++++---------- .../Contribute/ActionMapping/ByTypeTest.php | 6 ++--- .../phpunit/CRM/Event/Form/Task/BadgeTest.php | 2 +- .../CRM/Utils/TokenConsistencyTest.php | 7 ++++-- 7 files changed, 33 insertions(+), 20 deletions(-) diff --git a/CRM/Core/EntityTokens.php b/CRM/Core/EntityTokens.php index cc9350bb1374..290136046878 100644 --- a/CRM/Core/EntityTokens.php +++ b/CRM/Core/EntityTokens.php @@ -63,7 +63,13 @@ public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL) \CRM_Utils_Money::format($fieldValue, $this->getCurrency($row))); } if ($this->isDateField($field)) { - return $row->format('text/plain')->tokens($entity, $field, \CRM_Utils_Date::customFormat($fieldValue)); + try { + return $row->format('text/plain') + ->tokens($entity, $field, new DateTime($fieldValue)); + } + catch (Exception $e) { + Civi::log()->info('invalid date token'); + } } $row->format('text/plain')->tokens($entity, $field, (string) $fieldValue); } diff --git a/Civi/Token/TokenProcessor.php b/Civi/Token/TokenProcessor.php index 00b6d1732745..f2e32add9168 100644 --- a/Civi/Token/TokenProcessor.php +++ b/Civi/Token/TokenProcessor.php @@ -452,6 +452,10 @@ private function filterTokenValue($value, ?array $filter, TokenRow $row) { if ($value instanceof \DateTime && $filter === NULL) { $filter = ['crmDate']; + if ($value->format('His') === '000000') { + // if time is 'midnight' default to just date. + $filter[1] = 'Full'; + } } switch ($filter[0] ?? NULL) { diff --git a/Civi/Token/TokenRow.php b/Civi/Token/TokenRow.php index 46aa8a889de8..41b646274986 100644 --- a/Civi/Token/TokenRow.php +++ b/Civi/Token/TokenRow.php @@ -272,7 +272,7 @@ public function fill($format = NULL) { $htmlTokens[$entity][$field] = \CRM_Utils_String::purifyHTML($value); } else { - $htmlTokens[$entity][$field] = htmlentities($value); + $htmlTokens[$entity][$field] = is_object($value) ? $value : htmlentities($value); } } } diff --git a/tests/phpunit/CRM/Activity/Form/Task/PDFLetterCommonTest.php b/tests/phpunit/CRM/Activity/Form/Task/PDFLetterCommonTest.php index 2903c9a8955a..878aa70ba366 100644 --- a/tests/phpunit/CRM/Activity/Form/Task/PDFLetterCommonTest.php +++ b/tests/phpunit/CRM/Activity/Form/Task/PDFLetterCommonTest.php @@ -29,7 +29,7 @@ public function testCreateDocumentBasicTokens(): void { $activity = $this->activityCreate(['campaign_id' => $this->campaignCreate()]); $data = [ ['Subject: {activity.subject}', 'Subject: Discussion on warm beer'], - ['Date: {activity.activity_date_time}', 'Date: ' . CRM_Utils_Date::customFormat(date('Ymd')) . ' 12:00 AM'], + ['Date: {activity.activity_date_time}', 'Date: ' . CRM_Utils_Date::customFormat(date('Ymd'))], ['Duration: {activity.duration}', 'Duration: 90'], ['Location: {activity.location}', 'Location: Baker Street'], ['Details: {activity.details}', 'Details: Lets schedule a meeting'], @@ -127,17 +127,17 @@ public function testCreateDocumentSpecialTokens(): void { $this->markTestIncomplete('special tokens not yet merged - see https://github.com/civicrm/civicrm-core/pull/12012'); $activity = $this->activityCreate(); $data = [ - ['Source First Name: {activity.source_first_name}', "Source First Name: Anthony"], - ['Target N First Name: {activity.target_N_first_name}', "Target N First Name: Julia"], - ['Target 0 First Name: {activity.target_0_first_name}', "Target 0 First Name: Julia"], - ['Target 1 First Name: {activity.target_1_first_name}', "Target 1 First Name: Julia"], - ['Target 2 First Name: {activity.target_2_first_name}', "Target 2 First Name: "], - ['Assignee N First Name: {activity.target_N_first_name}', "Assignee N First Name: Julia"], - ['Assignee 0 First Name: {activity.target_0_first_name}', "Assignee 0 First Name: Julia"], - ['Assignee 1 First Name: {activity.target_1_first_name}', "Assignee 1 First Name: Julia"], - ['Assignee 2 First Name: {activity.target_2_first_name}', "Assignee 2 First Name: "], - ['Assignee Count: {activity.assignees_count}', "Assignee Count: 1"], - ['Target Count: {activity.targets_count}', "Target Count: 1"], + ['Source First Name: {activity.source_first_name}', 'Source First Name: Anthony'], + ['Target N First Name: {activity.target_N_first_name}', 'Target N First Name: Julia'], + ['Target 0 First Name: {activity.target_0_first_name}', 'Target 0 First Name: Julia'], + ['Target 1 First Name: {activity.target_1_first_name}', 'Target 1 First Name: Julia'], + ['Target 2 First Name: {activity.target_2_first_name}', 'Target 2 First Name: '], + ['Assignee N First Name: {activity.target_N_first_name}', 'Assignee N First Name: Julia'], + ['Assignee 0 First Name: {activity.target_0_first_name}', 'Assignee 0 First Name: Julia'], + ['Assignee 1 First Name: {activity.target_1_first_name}', 'Assignee 1 First Name: Julia'], + ['Assignee 2 First Name: {activity.target_2_first_name}', 'Assignee 2 First Name: '], + ['Assignee Count: {activity.assignees_count}', 'Assignee Count: 1'], + ['Target Count: {activity.targets_count}', 'Target Count: 1'], ]; $html_message = "\n" . implode("\n", CRM_Utils_Array::collect('0', $data)) . "\n"; $form = $this->getFormObject('CRM_Activity_Form_Task_PDF'); diff --git a/tests/phpunit/CRM/Contribute/ActionMapping/ByTypeTest.php b/tests/phpunit/CRM/Contribute/ActionMapping/ByTypeTest.php index 2140dd8ed46d..73df1631cebe 100644 --- a/tests/phpunit/CRM/Contribute/ActionMapping/ByTypeTest.php +++ b/tests/phpunit/CRM/Contribute/ActionMapping/ByTypeTest.php @@ -297,13 +297,13 @@ public function testTokenRendering(): void { $this->callAPISuccess('job', 'send_reminder', []); $expected = [ 'first name = Alice', - 'receive_date = February 1st, 2015 12:00 AM', + 'receive_date = February 1st, 2015', 'contribution status id = 1', 'new style status = Completed', 'new style label = Completed Label**', 'id ' . $this->ids['Contribution']['alice'], 'id - not valid for action schedule', - 'cancel date August 9th, 2021 12:00 AM', + 'cancel date August 9th, 2021', 'source SSF', 'financial type id = 1', 'financial type name = Donation', @@ -349,7 +349,7 @@ public function testTokenRendering(): void { TRUE ); $expected = [ - 'receive_date = February 1st, 2015 12:00 AM', + 'receive_date = February 1st, 2015', 'new style status = Completed', 'contribution status id = 1', 'id ' . $this->ids['Contribution']['alice'], diff --git a/tests/phpunit/CRM/Event/Form/Task/BadgeTest.php b/tests/phpunit/CRM/Event/Form/Task/BadgeTest.php index 8e0d4db5b25b..92f986580b26 100644 --- a/tests/phpunit/CRM/Event/Form/Task/BadgeTest.php +++ b/tests/phpunit/CRM/Event/Form/Task/BadgeTest.php @@ -91,7 +91,7 @@ protected function getAvailableTokens(): array { '{event.start_date}' => 'October 21st', '{participant.status_id}' => 2, '{participant.role_id}' => 1, - '{participant.register_date}' => 'February 19th, 2007 12:00 AM', + '{participant.register_date}' => 'February 19th, 2007', '{participant.source}' => 'Wimbeldon', '{participant.fee_level}' => 'low', '{participant.fee_amount}' => NULL, diff --git a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php index 96055889902d..de91019e27a1 100644 --- a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php +++ b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php @@ -288,6 +288,7 @@ protected function getContributionRecurID(): int { 'start_date' => '2021-07-23 15:39:20', 'end_date' => '2021-07-26 18:07:20', 'cancel_date' => '2021-08-19 09:12:45', + 'next_sched_contribution_date' => '2021-09-08', 'cancel_reason' => 'Because', 'amount' => 5990.99, 'currency' => 'EUR', @@ -340,9 +341,9 @@ protected function getExpectedContributionRecurTokenOutPut(): string { 2 Yes 15 - +September 8th, 2021 0 -January 3rd, 2020 12:00 AM +January 3rd, 2020 Yes 1 2 @@ -602,6 +603,8 @@ public function getDomainTokens(): array { /** * Test that domain tokens are consistently rendered. + * + * @throws \API_Exception */ public function testEventTokenConsistency(): void { $mut = new CiviMailUtils($this);