From 1e7d073abfcc3a715635caee0d8d913738669bc2 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 2 Mar 2023 11:19:02 +1300 Subject: [PATCH] Fix preferred_communication method Also fix over-aggressive test assertion --- CRM/Core/EntityTokens.php | 11 ++++++++++- tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php | 8 +++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/CRM/Core/EntityTokens.php b/CRM/Core/EntityTokens.php index 9672f08c14..9d33cf1359 100644 --- a/CRM/Core/EntityTokens.php +++ b/CRM/Core/EntityTokens.php @@ -290,10 +290,19 @@ class CRM_Core_EntityTokens extends AbstractTokenSubscriber { protected function getPseudoValue(string $realField, string $pseudoKey, $fieldValue): string { $bao = CRM_Core_DAO_AllCoreTables::getFullName($this->getMetadataForField($realField)['entity']); if ($pseudoKey === 'name') { + // There is a theoretical possibility fieldValue could be an array but + // specifically for preferred communication type - but real world usage + // hitting this is unlikely & the unexpectation is unclear so commenting, + // rather than adding handling. $fieldValue = (string) CRM_Core_PseudoConstant::getName($bao, $realField, $fieldValue); } if ($pseudoKey === 'label') { - $fieldValue = (string) CRM_Core_PseudoConstant::getLabel($bao, $realField, $fieldValue); + $newValue = []; + // Preferred communication method is an array that would resolve to (e.g) 'Phone, Email' + foreach ((array) $fieldValue as $individualValue) { + $newValue[] = CRM_Core_PseudoConstant::getLabel($bao, $realField, $individualValue); + } + $fieldValue = implode(', ', $newValue); } if ($pseudoKey === 'abbr' && $realField === 'state_province_id') { // hack alert - currently only supported for state. diff --git a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php index 868340130a..b38da39a63 100644 --- a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php +++ b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php @@ -533,7 +533,13 @@ emo $this->assertEquals($allAtOnce, implode("\n", $oneByOne)); $emptyLines = preg_grep('/:$/', $oneByOne); - $this->assertEquals([], $emptyLines, 'All tokens should have data.'); + $this->assertEquals([ + 'contact.address_primary.county_id:label:', + 'contact.contact_is_deleted', + 'contact.county', + 'contact.custom_6', + 'contact.do_not_phone' + ], $emptyLines, 'Most tokens should have data.'); } /** -- 2.25.1