Skip to content

Commit

Permalink
Add unit test cover for the MessageTemplate::renderMessageTemplate fu…
Browse files Browse the repository at this point in the history
…nction

This provides cover for the 4 methods
- domain tokens
- contact tokens
- smarty assigns within the function
- smarty assigns before the function
  • Loading branch information
eileenmcnaughton committed Feb 6, 2021
1 parent 8af3035 commit 41e9e1b
Show file tree
Hide file tree
Showing 3 changed files with 345 additions and 15 deletions.
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
14 changes: 7 additions & 7 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,7 +266,7 @@ public static function getDomainTokenReplacement($token, $domain, $html = FALSE,
if (!in_array($token, self::$_tokens['domain'])) {
$value = "{domain.$token}";
}
elseif ($token == 'address') {
elseif ($token === 'address') {
static $addressCache = [];

$cache_key = $html ? 'address-html' : 'address-text';
Expand All @@ -288,10 +288,10 @@ public static function getDomainTokenReplacement($token, $domain, $html = FALSE,
$addressCache[$cache_key] = $value;
}
}
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) {
if ($val) {
$communicationPreferences[$val] = CRM_Core_PseudoConstant::getLabel('CRM_Contact_DAO_Contact', 'preferred_communication_method', $val);
}
Expand Down
Loading

0 comments on commit 41e9e1b

Please sign in to comment.