Fix preferred_communication method
authorEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 1 Mar 2023 22:19:02 +0000 (11:19 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 1 Mar 2023 22:19:02 +0000 (11:19 +1300)
Also fix over-aggressive test assertion

CRM/Core/EntityTokens.php
tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php

index 9672f08c1407003dd05564242698f893a31d3d36..9d33cf13599b1ee82740aa4f88f573e14d23586f 100644 (file)
@@ -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.
index 868340130a82936b8d7d482ef2d1c3caf3879fd7..b38da39a63a8feaee728d9814e88f31a40491177 100644 (file)
@@ -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.');
   }
 
   /**