diff --git a/Civi/Token/TokenCompatSubscriber.php b/Civi/Token/TokenCompatSubscriber.php index bdfaa5994eb5..faac3e7ea494 100644 --- a/Civi/Token/TokenCompatSubscriber.php +++ b/Civi/Token/TokenCompatSubscriber.php @@ -58,7 +58,7 @@ public function onEvaluate(TokenValueEvent $e) { $params = [ ['contact_id', '=', $contactId, 0, 0], ]; - [$contact] = \CRM_Contact_BAO_Query::apiQuery($params); + [$contact] = \CRM_Contact_BAO_Query::apiQuery($params, array_fill_keys($messageTokens['contact'], 1)); //CRM-4524 $contact = reset($contact); if (!$contact || is_a($contact, 'CRM_Core_Error')) { @@ -71,10 +71,7 @@ public function onEvaluate(TokenValueEvent $e) { if (!empty($messageTokens['contact'])) { foreach ($messageTokens['contact'] as $token) { if (\CRM_Core_BAO_CustomField::getKeyID($token)) { - $contact[$token] = civicrm_api3('Contact', 'getvalue', [ - 'return' => $token, - 'id' => $contactId, - ]); + $contact[$token] = \CRM_Core_BAO_CustomField::displayValue($contact[$token], \CRM_Core_BAO_CustomField::getKeyID($token)); } } } diff --git a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php index 38d9c9945030..ad36d68f7065 100644 --- a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php +++ b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php @@ -182,10 +182,10 @@ public function testContactTokens(): void { public function testContactTokensRenderedByTokenProcessor(): void { $this->createCustomGroupWithFieldsOfAllTypes([]); $tokenData = $this->getAllContactTokens(); - // @todo - these 2 still need fixing/ syncing. - unset($tokenData['preferred_communication_method'], $tokenData['addressee']); + // @todo - these still need fixing/ syncing. + unset($tokenData['preferred_communication_method'], $tokenData['addressee'], $tokenData['email_greeting'], $tokenData['postal_greeting']); $address = $this->setupContactFromTokeData($tokenData); - $tokenString =''; + $tokenString = ''; foreach (array_keys($tokenData) as $key) { $tokenString .= "{$key}:{contact.{$key}}\n"; } @@ -198,8 +198,11 @@ public function testContactTokensRenderedByTokenProcessor(): void { $rendered = (string) $row->render('html'); } $expected = $this->getExpectedContactOutput($address['id'], $tokenData, $rendered); - // @todo - fix these 2 & stop stripping them out. - $expected = str_replace(["preferred_communication_method:\n", "addressee:Mr. Robert Frank Smith II\n"], '', $expected); + // @todo - fix these & stop stripping them out. + $expected = str_replace([ + "preferred_communication_method:\n", + "addressee:Mr. Robert Frank Smith II\n", + ], '', $expected); $this->assertEquals($expected, $rendered); } @@ -364,23 +367,25 @@ protected function setupContactFromTokeData(array $tokenData) { 'is_primary' => TRUE, 'name' => $tokenData['im'], 'provider_id' => $tokenData['im_provider'], - 'contact_id' => $tokenData['contact_id'] + 'contact_id' => $tokenData['contact_id'], ]); $this->callAPISuccess('OpenID', 'create', array_merge($tokenData, [ 'is_primary' => TRUE, 'contact_id' => $tokenData['contact_id'], - 'openid' => $tokenData['openid'] + 'openid' => $tokenData['openid'], ])); return $address; } /** - * @param array|null $contact - * @param $id + * Get the expected rendered string. + * + * @param int $id * @param array $tokenData - * @param bool $checksum + * @param string $actualOutput * * @return string + * @throws \API_Exception */ protected function getExpectedContactOutput($id, array $tokenData, string $actualOutput): string { $checksum = substr($actualOutput, (strpos($actualOutput, 'cs=') + 3), 47);