Make getTemplateContribution match is_test
authorAidan Saunders <aidan.saunders@squiffle.uk>
Wed, 9 Oct 2019 08:28:16 +0000 (09:28 +0100)
committerAidan Saunders <aidan.saunders@squiffle.uk>
Wed, 9 Oct 2019 08:28:16 +0000 (09:28 +0100)
- 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
tests/phpunit/CRM/Contribute/BAO/ContributionRecurTest.php

index fd9e484ee554697b54875cb95e5ef7292dd29e21..f7532dca213fa6473590432c80174687fec7bb87 100644 (file)
@@ -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();
index 0b120f16f43bb59a76a414886b7a1ea357bf473f..dfc79ba856c485d4ba47ed847a42039294d07d38 100644 (file)
@@ -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']);
+  }
+
 }