Skip to content

Commit

Permalink
Reconcile remaining fields between scheduled reminders and legacy tokens
Browse files Browse the repository at this point in the history
This adds the last fields that were in legacy tokens but not scheduled reminders and now
the same code is deriving the list for each. I wound up UI testing rather than unit
testing that custom fields are still advertised without this line
as the test uses rollback & adding custom fields would break that.
  • Loading branch information
eileenmcnaughton committed Aug 11, 2021
1 parent 43a25f5 commit b5e0905
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
9 changes: 8 additions & 1 deletion CRM/Contribute/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected function getApiEntityName(): string {
* @return array
*/
protected function getExposedFields(): array {
return [
$fields = [
'contribution_page_id',
'source',
'id',
Expand All @@ -75,7 +75,14 @@ protected function getExposedFields(): array {
'contribution_status_id',
'financial_type_id',
'payment_instrument_id',
'cancel_reason',
'amount_level',
'check_number',
];
if (CRM_Campaign_BAO_Campaign::isCampaignEnable()) {
$fields[] = 'campaign_id';
}
return $fields;
}

/**
Expand Down
9 changes: 1 addition & 8 deletions CRM/Core/SelectValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,14 +568,7 @@ public static function contributionTokens(): array {
foreach ($processor->getAllTokens() as $token => $title) {
$tokens['{contribution.' . $token . '}'] = $title;
}
return array_merge($tokens, [
'{contribution.cancel_reason}' => ts('Contribution Cancel Reason'),
'{contribution.amount_level}' => ts('Amount Level'),
'{contribution.check_number}' => ts('Check Number'),
'{contribution.campaign}' => ts('Contribution Campaign'),
// @todo - we shouldn't need to include custom fields here -
// remove, with test.
], CRM_Utils_Token::getCustomFieldTokens('Contribution', TRUE));
return $tokens;
}

/**
Expand Down
31 changes: 30 additions & 1 deletion tests/phpunit/CRM/Contribute/ActionMapping/ByTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
+--------------------------------------------------------------------+
*/

use Civi\Api4\Contribution;

/**
* Class CRM_Contribute_ActionMapping_ByTypeTest
* @group ActionSchedule
Expand Down Expand Up @@ -151,6 +153,8 @@ public function createTestCases() {
* Create a contribution record for Alice with type "Member Dues".
*/
public function addAliceDues(): void {
$this->enableCiviCampaign();
$campaignID = $this->campaignCreate();
$this->ids['Contribution']['alice'] = $this->callAPISuccess('Contribution', 'create', [
'contact_id' => $this->contacts['alice']['id'],
'receive_date' => date('Ymd', strtotime($this->targetDate)),
Expand All @@ -164,6 +168,7 @@ public function addAliceDues(): void {
// Having a cancel date is a bit artificial here but we can test it....
'cancel_date' => '2021-08-09',
'contribution_status_id' => 1,
'campaign_id' => $campaignID,
'soft_credit' => [
'1' => [
'contact_id' => $this->contacts['carol']['id'],
Expand Down Expand Up @@ -281,7 +286,10 @@ public function testTokenRendering(): void {
non_deductible_amount = {contribution.non_deductible_amount}
total_amount = {contribution.total_amount}
net_amount = {contribution.net_amount}
fee_amount = {contribution.fee_amount}';
fee_amount = {contribution.fee_amount}
campaign_id = {contribution.campaign_id}
campaign name = {contribution.campaign_id:name}
campaign label = {contribution.campaign_id:label}';

$this->schedule->save();
$this->callAPISuccess('job', 'send_reminder', []);
Expand All @@ -305,6 +313,9 @@ public function testTokenRendering(): void {
'total_amount = € 100.00',
'net_amount = € 95.00',
'fee_amount = € 5.00',
'campaign_id = 1',
'campaign name = big_campaign',
'campaign label = Campaign',
];
$this->mut->checkMailLog($expected);

Expand Down Expand Up @@ -337,6 +348,9 @@ public function testTokenRendering(): void {
'total_amount = € 100.00',
'net_amount = € 95.00',
'fee_amount = € 5.00',
'campaign_id = 1',
'campaign name = big_campaign',
'campaign label = Campaign',
];
foreach ($expected as $string) {
$this->assertStringContainsString($string, $contributionDetails[$this->contacts['alice']['id']]['html']);
Expand All @@ -354,6 +368,21 @@ public function testTokenRendering(): void {
'contribution_status_id:label',
];
$processor = new CRM_Contribute_Tokens();
$legacyTokens = [];
$realLegacyTokens = [];
foreach (CRM_Core_SelectValues::contributionTokens() as $token => $label) {
$legacyTokens[substr($token, 14, -1)] = $label;
if (strpos($token, ':') === FALSE) {
$realLegacyTokens[substr($token, 14, -1)] = $label;
}
}
$fields = (array) Contribution::getFields()->addSelect('name', 'title')->execute()->indexBy('name');
$allFields = [];
foreach ($fields as $field) {
$allFields[$field['name']] = $field['title'];
}
// $this->assertEquals($realLegacyTokens, $allFields);
$this->assertEquals($legacyTokens, $processor->tokenNames);
foreach ($tokens as $token) {
$this->assertEquals(CRM_Core_SelectValues::contributionTokens()['{contribution.' . $token . '}'], $processor->tokenNames[$token]);
}
Expand Down

0 comments on commit b5e0905

Please sign in to comment.