Skip to content

Commit

Permalink
Change getTemplateContribution to prefer is_template contrib
Browse files Browse the repository at this point in the history
Add test
  • Loading branch information
aydun committed Oct 9, 2019
1 parent 0b2f1f2 commit b91a542
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
11 changes: 11 additions & 0 deletions CRM/Contribute/BAO/ContributionRecur.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,12 +433,23 @@ public static function getTemplateContribution($id, $overrides = []) {
'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);
Expand Down
34 changes: 34 additions & 0 deletions tests/phpunit/CRM/Contribute/BAO/ContributionRecurTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,38 @@ public function testGetTemplateContributionMatchTest() {
$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']);
}

}

0 comments on commit b91a542

Please sign in to comment.