From 0b2f1f291501520a3d53f24d2af2f35eeb279ee1 Mon Sep 17 00:00:00 2001 From: Aidan Saunders Date: Wed, 9 Oct 2019 09:28:16 +0100 Subject: [PATCH] Make getTemplateContribution match is_test - non-test recurs should only pick up non-test templates - test recurs should only pick up test templates Add test --- CRM/Contribute/BAO/ContributionRecur.php | 7 +- .../Contribute/BAO/ContributionRecurTest.php | 72 ++++++++++++++++++- 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/CRM/Contribute/BAO/ContributionRecur.php b/CRM/Contribute/BAO/ContributionRecur.php index fd9e484ee5..f7532dca21 100644 --- a/CRM/Contribute/BAO/ContributionRecur.php +++ b/CRM/Contribute/BAO/ContributionRecur.php @@ -428,9 +428,14 @@ INNER JOIN civicrm_contribution con ON ( con.id = mp.contribution_id ) * @throws \CiviCRM_API3_Exception */ public static function getTemplateContribution($id, $overrides = []) { + // use api3 because api4 doesn't handle ContributionRecur yet... + $is_test = civicrm_api3('ContributionRecur', 'getvalue', [ + 'return' => "is_test", + 'id' => $id, + ]); $templateContributions = \Civi\Api4\Contribution::get() ->addWhere('contribution_recur_id', '=', $id) - ->addWhere('is_test', 'IN', [0, 1]) + ->addWhere('is_test', '=', $is_test) ->addOrderBy('id', 'DESC') ->setLimit(1) ->execute(); diff --git a/tests/phpunit/CRM/Contribute/BAO/ContributionRecurTest.php b/tests/phpunit/CRM/Contribute/BAO/ContributionRecurTest.php index 0b120f16f4..dfc79ba856 100644 --- a/tests/phpunit/CRM/Contribute/BAO/ContributionRecurTest.php +++ b/tests/phpunit/CRM/Contribute/BAO/ContributionRecurTest.php @@ -94,7 +94,7 @@ class CRM_Contribute_BAO_ContributionRecurTest extends CiviUnitTestCase { } /** - * Test checking if contribution recurr object can allow for changes to financial types. + * Test checking if contribution recur object can allow for changes to financial types. * */ public function testSupportFinancialTypeChange() { @@ -130,4 +130,74 @@ class CRM_Contribute_BAO_ContributionRecurTest extends CiviUnitTestCase { $this->assertEquals('XAU', $dao->currency, 'Edit clobbered recur currency'); } + /** + * Check test contributions aren't picked up as template for non-test recurs + * + */ + public function testGetTemplateContributionMatchTest1() { + $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $this->_params); + // Create a first contrib + $firstContrib = $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', + ]); + // Create a test contrib - should not be picked up as template for non-test recur + $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_test' => 1, + ]); + $fetchedTemplate = CRM_Contribute_BAO_ContributionRecur::getTemplateContribution($contributionRecur['id']); + $this->assertEquals($firstContrib['id'], $fetchedTemplate['id']); + } + + /** + * Check non-test contributions aren't picked up as template for test recurs + * + */ + public function testGetTemplateContributionMatchTest() { + $params = $this->_params; + $params['is_test'] = 1; + $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $params); + // Create a first test contrib + $firstContrib = $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_test' => 1, + ]); + // Create a non-test contrib - should not be picked up as template for non-test recur + // This shouldn't occur - a live contrib against a test recur, but that's not the point... + $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_test' => 0, + ]); + $fetchedTemplate = CRM_Contribute_BAO_ContributionRecur::getTemplateContribution($contributionRecur['id']); + $this->assertEquals($firstContrib['id'], $fetchedTemplate['id']); + } + } -- 2.25.1