From: Aidan Saunders Date: Wed, 9 Oct 2019 08:49:13 +0000 (+0100) Subject: Change getTemplateContribution to prefer is_template contrib X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=f306dbebd5332e974144280eb6032fb7848d8d4b;p=civicrm-core.git Change getTemplateContribution to prefer is_template contrib Add test --- diff --git a/CRM/Contribute/BAO/ContributionRecur.php b/CRM/Contribute/BAO/ContributionRecur.php index f7532dca21..7f80887281 100644 --- a/CRM/Contribute/BAO/ContributionRecur.php +++ b/CRM/Contribute/BAO/ContributionRecur.php @@ -433,12 +433,23 @@ INNER JOIN civicrm_contribution con ON ( con.id = mp.contribution_id ) 'return' => "is_test", 'id' => $id, ]); + // First look for new-style template contribution with is_template=1 $templateContributions = \Civi\Api4\Contribution::get() ->addWhere('contribution_recur_id', '=', $id) + ->addWhere('is_template', '=', 1) ->addWhere('is_test', '=', $is_test) ->addOrderBy('id', 'DESC') ->setLimit(1) ->execute(); + if (!$templateContributions->count()) { + // Fall back to old style template contributions + $templateContributions = \Civi\Api4\Contribution::get() + ->addWhere('contribution_recur_id', '=', $id) + ->addWhere('is_test', '=', $is_test) + ->addOrderBy('id', 'DESC') + ->setLimit(1) + ->execute(); + } if ($templateContributions->count()) { $templateContribution = $templateContributions->first(); $result = array_merge($templateContribution, $overrides); diff --git a/tests/phpunit/CRM/Contribute/BAO/ContributionRecurTest.php b/tests/phpunit/CRM/Contribute/BAO/ContributionRecurTest.php index dfc79ba856..bd8370cbce 100644 --- a/tests/phpunit/CRM/Contribute/BAO/ContributionRecurTest.php +++ b/tests/phpunit/CRM/Contribute/BAO/ContributionRecurTest.php @@ -200,4 +200,38 @@ class CRM_Contribute_BAO_ContributionRecurTest extends CiviUnitTestCase { $this->assertEquals($firstContrib['id'], $fetchedTemplate['id']); } + /** + * Test that is_template contribution is used where available + * + */ + public function testGetTemplateContributionNewTemplate() { + $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $this->_params); + // Create the template + $templateContrib = $this->callAPISuccess('Contribution', 'create', [ + 'contribution_recur_id' => $contributionRecur['id'], + 'total_amount' => '3.00', + 'financial_type_id' => 1, + 'payment_instrument_id' => 1, + 'currency' => 'USD', + 'contact_id' => $this->individualCreate(), + 'contribution_status_id' => 1, + 'receive_date' => 'yesterday', + 'is_template' => 1, + ]); + // Create another normal contrib + $this->callAPISuccess('Contribution', 'create', [ + 'contribution_recur_id' => $contributionRecur['id'], + 'total_amount' => '3.00', + 'financial_type_id' => 1, + 'payment_instrument_id' => 1, + 'currency' => 'USD', + 'contact_id' => $this->individualCreate(), + 'contribution_status_id' => 1, + 'receive_date' => 'yesterday', + ]); + $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']); + } + }