Skip to content

Commit

Permalink
Fix token subscriber to format the display of the custom tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Mar 16, 2021
1 parent 5119782 commit 37ed8dc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
11 changes: 5 additions & 6 deletions Civi/Token/TokenCompatSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public function onEvaluate(TokenValueEvent $e) {
$e->getTokenProcessor()->context['hookTokenCategories'] = $categories;

$messageTokens = $e->getTokenProcessor()->getMessageTokens();
$returnProperties = array_fill_keys($messageTokens['contact'] ?? [], 1);
$returnProperties = array_merge(\CRM_Contact_BAO_Query::defaultReturnProperties(), $returnProperties);

foreach ($e->getRows() as $row) {
if (empty($row->context['contactId'])) {
Expand All @@ -58,23 +60,20 @@ 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, $returnProperties ?? NULL);
//CRM-4524
$contact = reset($contact);
if (!$contact || is_a($contact, 'CRM_Core_Error')) {
// FIXME: Need to differentiate errors which kill the batch vs the individual row.
\Civi::log()->debug("Failed to generate token data. Invalid contact ID: " . $row->context['contactId']);
\Civi::log()->debug('Failed to generate token data. Invalid contact ID: ' . $row->context['contactId']);
continue;
}

//update value of custom field token
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));
}
}
}
Expand Down
25 changes: 15 additions & 10 deletions tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 37ed8dc

Please sign in to comment.