Skip to content

Commit

Permalink
Fix repeattransaction api to use custom data from the template contri…
Browse files Browse the repository at this point in the history
…bution

This ensures (& tests) that custom data is copied from the template transaction - generally the most recent transaction
  • Loading branch information
eileenmcnaughton committed Jul 27, 2020
1 parent 9170f16 commit 5efb434
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -2636,7 +2636,7 @@ protected static function repeatTransaction(&$contribution, &$input, $contributi

$createContribution = civicrm_api3('Contribution', 'create', $contributionParams);
$contribution->id = $createContribution['id'];
CRM_Contribute_BAO_ContributionRecur::copyCustomValues($contributionParams['contribution_recur_id'], $contribution->id);
$contribution->copyCustomFields($templateContribution['id'], $contribution->id);
self::handleMembershipIDOverride($contribution->id, $input);
return TRUE;
}
Expand Down
2 changes: 2 additions & 0 deletions CRM/Contribute/BAO/ContributionRecur.php
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,8 @@ public static function sendRecurringStartOrEndNotification($ids, $recur, $isFirs
/**
* Copy custom data of the initial contribution into its recurring contributions.
*
* @deprecated
*
* @param int $recurId
* @param int $targetContributionId
*/
Expand Down
41 changes: 40 additions & 1 deletion tests/phpunit/api/v3/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
+--------------------------------------------------------------------+
*/

use Civi\Api4\Contribution;

/**
* Test APIv3 civicrm_contribute_* functions
*
Expand Down Expand Up @@ -107,7 +109,7 @@ public function setUp() {
*/
public function tearDown() {
$this->quickCleanUpFinancialEntities();
$this->quickCleanup(['civicrm_uf_match']);
$this->quickCleanup(['civicrm_uf_match'], TRUE);
$financialAccounts = $this->callAPISuccess('FinancialAccount', 'get', []);
foreach ($financialAccounts['values'] as $financialAccount) {
if ($financialAccount['name'] === 'Test Tax financial account ' || $financialAccount['name'] === 'Test taxable financial Type') {
Expand Down Expand Up @@ -2326,6 +2328,43 @@ public function testRepeatTransaction() {
], 'online');
}

/**
* Test custom data is copied over.
*
* @throws \API_Exception
* @throws \CRM_Core_Exception
*/
public function testRepeatTransactionWithCustomData() {
$this->createCustomGroupWithFieldOfType(['extends' => 'Contribution', 'name' => 'Repeat'], 'text');
$originalContribution = $this->setUpRepeatTransaction([], 'single', [$this->getCustomFieldName('text') => 'first']);
$this->callAPISuccess('contribution', 'repeattransaction', [
'original_contribution_id' => $originalContribution['id'],
'contribution_status_id' => 'Completed',
'trxn_id' => 'my_trxn',
]);

$contribution = Contribution::get()
->addWhere('trxn_id', '=', 'my_trxn')
->addSelect('Custom_Group.Enter_text_here')
->addSelect('id')
->execute()->first();
$this->assertEquals('first', $contribution['Custom_Group.Enter_text_here']);

Contribution::update()->setValues(['Custom_Group.Enter_text_here' => 'second'])->addWhere('id', '=', $contribution['id'])->execute();

$this->callAPISuccess('contribution', 'repeattransaction', [
'original_contribution_id' => $originalContribution['id'],
'contribution_status_id' => 'Completed',
'trxn_id' => 'number_3',
]);

$contribution = Contribution::get()
->addWhere('trxn_id', '=', 'number_3')
->setSelect(['id', 'Custom_Group.Enter_text_here'])
->execute()->first();
$this->assertEquals('second', $contribution['Custom_Group.Enter_text_here']);
}

/**
* Test repeat contribution successfully creates line items (plural).
*
Expand Down

0 comments on commit 5efb434

Please sign in to comment.