Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test cover for the MessageTemplate::renderMessageTemplate function #19551

Merged
merged 1 commit into from
Feb 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions CRM/Core/BAO/MessageTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ protected static function resolveDomainTokens(array $mailContent, array $tokens,
$domain = CRM_Core_BAO_Domain::getDomain();
$mailContent['subject'] = CRM_Utils_Token::replaceDomainTokens($mailContent['subject'], $domain, FALSE, $tokens['subject'], $escapeSmarty);
$mailContent['text'] = CRM_Utils_Token::replaceDomainTokens($mailContent['text'], $domain, FALSE, $tokens['text'], $escapeSmarty);
$mailContent['html'] = CRM_Utils_Token::replaceDomainTokens($mailContent['html'], $domain, TRUE, $tokens, $escapeSmarty);
$mailContent['html'] = CRM_Utils_Token::replaceDomainTokens($mailContent['html'], $domain, TRUE, $tokens['html'], $escapeSmarty);
return $mailContent;
}

Expand Down Expand Up @@ -687,6 +687,9 @@ protected static function parseThroughSmarty(array $mailContent, $tplParams): ar
/**
* Render the message template, resolving tokens and smarty tokens.
*
* As with all BAO methods this should not be called directly outside
* of tested core code and is highly likely to change.
*
* @param array $mailContent
* @param bool $disableSmarty
* @param int $contactID
Expand All @@ -695,7 +698,7 @@ protected static function parseThroughSmarty(array $mailContent, $tplParams): ar
* @return array
* @throws \CRM_Core_Exception
*/
protected static function renderMessageTemplate(array $mailContent, $disableSmarty, $contactID, $smartyAssigns): array {
public static function renderMessageTemplate(array $mailContent, $disableSmarty, $contactID, $smartyAssigns): array {
$tokens = self::getTokensToResolve($mailContent);

// When using Smarty we need to pass the $escapeSmarty parameter.
Expand Down
28 changes: 14 additions & 14 deletions CRM/Utils/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,14 @@ function ($matches) use ($domain, $html, $escapeSmarty) {
}

/**
* @param $token
* @param string $token
* @param CRM_Core_BAO_Domain $domain
* @param bool $html
* @param bool $escapeSmarty
*
* @return mixed|null|string
* @return null|string
*/
public static function getDomainTokenReplacement($token, $domain, $html = FALSE, $escapeSmarty = FALSE) {
public static function getDomainTokenReplacement($token, $domain, $html = FALSE, $escapeSmarty = FALSE): ?string {
// check if the token we were passed is valid
// we have to do this because this function is
// called only when we find a token in the string
Expand All @@ -266,32 +266,32 @@ public static function getDomainTokenReplacement($token, $domain, $html = FALSE,
if (!in_array($token, self::$_tokens['domain'])) {
$value = "{domain.$token}";
}
elseif ($token == 'address') {
static $addressCache = [];
elseif ($token === 'address') {
$cacheKey = __CLASS__ . 'address_token_cache' . CRM_Core_Config::domainID();
$addressCache = Civi::cache()->has($cacheKey) ? Civi::cache()->get($cacheKey) : [];

$cache_key = $html ? 'address-html' : 'address-text';
if (array_key_exists($cache_key, $addressCache)) {
return $addressCache[$cache_key];
$fieldKey = $html ? 'address-html' : 'address-text';
if (array_key_exists($fieldKey, $addressCache)) {
return $addressCache[$fieldKey];
}

$value = NULL;
// Construct the address token

if (!empty($loc[$token])) {
if ($html) {
$value = $loc[$token][1]['display'];
$value = str_replace("\n", '<br />', $value);
$value = str_replace("\n", '<br />', $loc[$token][1]['display']);
}
else {
$value = $loc[$token][1]['display_text'];
}
$addressCache[$cache_key] = $value;
Civi::cache()->set($cacheKey, $addressCache);
}
}
elseif ($token == 'name' || $token == 'id' || $token == 'description') {
elseif ($token === 'name' || $token === 'id' || $token === 'description') {
$value = $domain->$token;
}
elseif ($token == 'phone' || $token == 'email') {
elseif ($token === 'phone' || $token === 'email') {
// Construct the phone and email tokens

$value = NULL;
Expand Down Expand Up @@ -1234,7 +1234,7 @@ public static function getTokenDetails(
if (!empty($contactDetails[$contactID]['preferred_communication_method'])
) {
$communicationPreferences = [];
foreach ($contactDetails[$contactID]['preferred_communication_method'] as $val) {
foreach ((array) $contactDetails[$contactID]['preferred_communication_method'] as $val) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensuring it is an array fixed a test fail & makes sense when iterating

if ($val) {
$communicationPreferences[$val] = CRM_Core_PseudoConstant::getLabel('CRM_Contact_DAO_Contact', 'preferred_communication_method', $val);
}
Expand Down
Loading