Skip to content

Commit

Permalink
Merge pull request #21453 from eileenmcnaughton/member_tokens
Browse files Browse the repository at this point in the history
dev/core#2832 Add test for legacy membership tokens, add support for preferred tokens
  • Loading branch information
seamuslee001 authored Sep 13, 2021
2 parents 24ae4af + d41a5d5 commit 94dcc53
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 17 deletions.
43 changes: 26 additions & 17 deletions CRM/Utils/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -1534,21 +1534,6 @@ protected static function _buildContributionTokens() {
self::$_tokens[$key] = Civi::$statics[__CLASS__][__FUNCTION__][$key];
}

/**
* Store membership tokens on the static _tokens array.
*/
protected static function _buildMembershipTokens() {
$key = 'membership';
if (!isset(self::$_tokens[$key]) || self::$_tokens[$key] == NULL) {
$membershipTokens = [];
$tokens = CRM_Core_SelectValues::membershipTokens();
foreach ($tokens as $token => $dontCare) {
$membershipTokens[] = substr($token, (strpos($token, '.') + 1), -1);
}
self::$_tokens[$key] = $membershipTokens;
}
}

/**
* Replace tokens for an entity.
* @param string $entity
Expand Down Expand Up @@ -1705,6 +1690,14 @@ public static function replaceMultipleContributionTokens(string $separator, stri
/**
* Get replacement strings for any membership tokens (only a small number of tokens are implemnted in the first instance
* - this is used by the pdfLetter task from membership search
*
* This is called via replaceEntityTokens.
*
* In the near term it will not be called at all from core as
* the pdf letter task is updated to use the processor.
*
* @deprecated
*
* @param string $entity
* should always be "membership"
* @param string $token
Expand All @@ -1714,13 +1707,29 @@ public static function replaceMultipleContributionTokens(string $separator, stri
* @return string token replacement
*/
public static function getMembershipTokenReplacement($entity, $token, $membership) {
self::_buildMembershipTokens();
$supportedTokens = [
'id',
'status',
'status_id',
'type',
'membership_type_id',
'start_date',
'join_date',
'end_date',
'fee',
];
switch ($token) {
case 'type':
// membership_type_id would only be requested if the calling
// class is mapping it to '{membership:membership_type_id:label'}
case 'membership_type_id':
$value = $membership['membership_name'];
break;

case 'status':
// status_id would only be requested if the calling
// class is mapping it to '{membership:status_id:label'}
case 'status_id':
$statuses = CRM_Member_BAO_Membership::buildOptions('status_id');
$value = $statuses[$membership['status_id']];
break;
Expand All @@ -1741,7 +1750,7 @@ public static function getMembershipTokenReplacement($entity, $token, $membershi
break;

default:
if (in_array($token, self::$_tokens[$entity])) {
if (in_array($token, $supportedTokens)) {
$value = $membership[$token];
if (CRM_Utils_String::endsWith($token, '_date')) {
$value = CRM_Utils_Date::customFormat($value);
Expand Down
68 changes: 68 additions & 0 deletions tests/phpunit/CRM/Utils/TokenConsistencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,4 +357,72 @@ protected function getExpectedContributionRecurTokenOutPut(): string {
Check';
}

/**
* Test that membership tokens are consistently rendered.
*
* @throws \API_Exception
*/
public function testMembershipTokenConsistency(): void {
$this->createLoggedInUser();
$this->restoreMembershipTypes();
$this->createCustomGroupWithFieldOfType(['extends' => 'Membership']);
$tokens = CRM_Core_SelectValues::membershipTokens();
$this->assertEquals($this->getMembershipTokens(), $tokens);
$newStyleTokens = "\n{membership.status_id:label}\n{membership.membership_type_id:label}\n";
$tokenString = $newStyleTokens . implode("\n", array_keys($this->getMembershipTokens()));
$memberships = CRM_Utils_Token::getMembershipTokenDetails([$this->getMembershipID()]);
$messageToken = CRM_Utils_Token::getTokens($tokenString);
$tokenHtml = CRM_Utils_Token::replaceEntityTokens('membership', $memberships[$this->getMembershipID()], $tokenString, $messageToken);
$this->assertEquals($this->getExpectedMembershipTokenOutput(), $tokenHtml);
}

/**
* Get declared membership tokens.
*
* @return string[]
*/
public function getMembershipTokens(): array {
return [
'{membership.id}' => 'Membership ID',
'{membership.status}' => 'Membership Status',
'{membership.type}' => 'Membership Type',
'{membership.start_date}' => 'Membership Start Date',
'{membership.join_date}' => 'Membership Join Date',
'{membership.end_date}' => 'Membership End Date',
'{membership.fee}' => 'Membership Fee',
];
}

/**
* Get case ID.
*
* @return int
*/
protected function getMembershipID(): int {
if (!isset($this->ids['Membership'][0])) {
$this->ids['Membership'][0] = $this->contactMembershipCreate(
['contact_id' => $this->getContactID()]
);
}
return $this->ids['Membership'][0];
}

/**
* Get expected output from token parsing.
*
* @return string
*/
protected function getExpectedMembershipTokenOutput(): string {
return '
Expired
General
1
Expired
General
January 21st, 2007
January 21st, 2007
December 21st, 2007
100.00';
}

}

0 comments on commit 94dcc53

Please sign in to comment.