Skip to content

Commit

Permalink
Merge pull request #21756 from eileenmcnaughton/tt
Browse files Browse the repository at this point in the history
Fix token regression - re-add support for empty tokens
  • Loading branch information
eileenmcnaughton authored Oct 8, 2021
2 parents 19369a2 + 0bb1824 commit 617345f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Civi/Token/TokenCompatSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public function onRender(TokenRenderEvent $e): void {
return '';
});

// This removes the pattern used in greetings of having bits of text that
// depend on the tokens around them - ie '{first_name}{ }{last_name}
// has an extra construct '{ }' which will resolve as a space if the
// tokens on either side are resolved to 'something'
$e->string = preg_replace('/\\\\|\{(\s*)?\}/', ' ', $e->string);

if ($useSmarty) {
$smartyVars = [];
foreach ($e->context['smartyTokenAlias'] ?? [] as $smartyName => $tokenName) {
Expand Down
38 changes: 38 additions & 0 deletions tests/phpunit/CRM/Utils/TokenConsistencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,44 @@ public function getUnadvertisedTokens(): array {
];
}

/**
* Test tokens in 2 ways to ensure consistent handling.
*
* 1) as part of the greeting processing
* 2) via the token processor.
*
*/
public function testOddTokens(): void {

$variants = [
[
'string' => '{contact.individual_prefix}{ }{contact.first_name}{ }{contact.middle_name}{ }{contact.last_name}{ }{contact.individual_suffix}',
'expected' => 'Mr. Anthony Anderson II',
],
[
'string' => '{contact.prefix_id:label}{ }{contact.first_name}{ }{contact.middle_name}{ }{contact.last_name}{ }{contact.suffix_id:label}',
'expected' => 'Mr. Anthony Anderson II',
],
];
$tokenProcessor = new TokenProcessor(\Civi::dispatcher(), [
'smarty' => FALSE,
'schema' => ['contactId'],
]);
$contactID = $this->individualCreate(['middle_name' => '']);
$tokenProcessor->addRow(['contactId' => $contactID]);
foreach ($variants as $index => $variant) {
$tokenProcessor->addMessage($index, $variant['string'], 'text/plain');
}
$tokenProcessor->evaluate();
$result = $tokenProcessor->getRow(0);
foreach ($variants as $index => $variant) {
$greetingString = $variant['string'];
CRM_Utils_Token::replaceGreetingTokens($greetingString, $this->callAPISuccessGetSingle('Contact', ['id' => $contactID]), $contactID);
$this->assertEquals($variant['expected'], $greetingString, 'replaceGreetingTokens() should render expected output');
$this->assertEquals($variant['expected'], $result->render($index), 'TokenProcessor should render expected output');
}
}

/**
* Get the contribution recur tokens keyed by the token.
*
Expand Down

0 comments on commit 617345f

Please sign in to comment.