From: Jitendra Purohit Date: Wed, 15 Jul 2020 10:12:30 +0000 (+0530) Subject: Fix code to not overwrite source contribution - unit test for 17455 X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=c80d977f0ff5ffe6ba39aee57f6a13fb02434021;p=civicrm-core.git Fix code to not overwrite source contribution - unit test for 17455 --- diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 831f5e86aa..84da2280dc 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -2575,12 +2575,11 @@ LEFT JOIN civicrm_contribution contribution ON ( componentPayment.contribution_ * @param CRM_Contribute_BAO_Contribution $contribution * @param array $input * @param array $contributionParams - * @param int $paymentProcessorID * - * @return bool + * @return bool|array * @throws CiviCRM_API3_Exception */ - protected static function repeatTransaction(&$contribution, &$input, $contributionParams, $paymentProcessorID) { + protected static function repeatTransaction(&$contribution, &$input, $contributionParams) { if (!empty($contribution->id)) { return FALSE; } @@ -2638,7 +2637,7 @@ LEFT JOIN civicrm_contribution contribution ON ( componentPayment.contribution_ $contribution->id = $createContribution['id']; $contribution->copyCustomFields($templateContribution['id'], $contribution->id); self::handleMembershipIDOverride($contribution->id, $input); - return TRUE; + return $createContribution; } } @@ -4486,7 +4485,7 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac } $changeDate = CRM_Utils_Array::value('trxn_date', $input, date('YmdHis')); - self::repeatTransaction($contribution, $input, $contributionParams, $paymentProcessorId); + $contributionResult = self::repeatTransaction($contribution, $input, $contributionParams); $contributionParams['financial_type_id'] = $contribution->financial_type_id; $values = []; @@ -4534,7 +4533,9 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac // CRM-19309 - if you update the contribution here with financial_type_id it can/will mess with $lineItem // unsetting it here does NOT cause any other contribution test to fail! unset($contributionParams['financial_type_id']); - $contributionResult = civicrm_api3('Contribution', 'create', $contributionParams); + if (!$contributionResult) { + $contributionResult = civicrm_api3('Contribution', 'create', $contributionParams); + } // Add new soft credit against current $contribution. if (!empty($objects['contributionRecur']) && $objects['contributionRecur']->id) { diff --git a/tests/phpunit/CRM/Contribute/BAO/ContributionRecurTest.php b/tests/phpunit/CRM/Contribute/BAO/ContributionRecurTest.php index f5c9d4fe34..d2bf0d85e9 100644 --- a/tests/phpunit/CRM/Contribute/BAO/ContributionRecurTest.php +++ b/tests/phpunit/CRM/Contribute/BAO/ContributionRecurTest.php @@ -225,6 +225,7 @@ class CRM_Contribute_BAO_ContributionRecurTest extends CiviUnitTestCase { 'contribution_recur_id' => $contributionRecur['id'], 'total_amount' => '3.00', 'financial_type_id' => 1, + 'source' => 'Template Contribution', 'payment_instrument_id' => 1, 'currency' => 'USD', 'contact_id' => $this->individualCreate(), @@ -237,6 +238,7 @@ class CRM_Contribute_BAO_ContributionRecurTest extends CiviUnitTestCase { 'contribution_recur_id' => $contributionRecur['id'], 'total_amount' => '3.00', 'financial_type_id' => 1, + 'source' => 'Non-template Contribution', 'payment_instrument_id' => 1, 'currency' => 'USD', 'contact_id' => $this->individualCreate(), @@ -246,6 +248,12 @@ class CRM_Contribute_BAO_ContributionRecurTest extends CiviUnitTestCase { $fetchedTemplate = CRM_Contribute_BAO_ContributionRecur::getTemplateContribution($contributionRecur['id']); // Fetched template should be the is_template, not the latest contrib $this->assertEquals($fetchedTemplate['id'], $templateContrib['id']); + + $repeatContribution = $this->callAPISuccess('Contribution', 'repeattransaction', [ + 'contribution_status_id' => 'Completed', + 'contribution_recur_id' => $contributionRecur['id'], + ]); + $this->assertEquals('Template Contribution', $repeatContribution['values'][$repeatContribution['id']]['source']); } /**