CRM-17718 update repeattransaction api action to give precedence to contribution_recu...
authoreileenmcnaughton <eileen@fuzion.co.nz>
Wed, 16 Dec 2015 16:00:34 +0000 (16:00 +0000)
committereileen <emcnaughton@wikimedia.org>
Wed, 23 Dec 2015 20:44:04 +0000 (09:44 +1300)
CRM/Contribute/BAO/Contribution.php
api/v3/Contribution.php
tests/phpunit/api/v3/ContributionTest.php

index 46a830b7c1891fd4b52ca7ed614578ea7de3b50e..2e64f674edebf45ff5a0df21fd20973a3fdb4e36 100644 (file)
@@ -2040,6 +2040,16 @@ LEFT JOIN  civicrm_contribution contribution ON ( componentPayment.contribution_
         'contribution_recur_id' => $contributionParams['contribution_recur_id'],
         'options' => array('limit' => 1),
       ));
+      if (!empty($contributionParams['contribution_recur_id'])) {
+        $recurringContribution = civicrm_api3('ContributionRecur', 'getsingle', array(
+          'id' => $contributionParams['contribution_recur_id'],
+        ));
+        if (!empty($recurringContribution['campaign_id'])) {
+          // CRM-17718 the campaign id on the contribution recur record should get precedence.
+          $contributionParams['campaign_id'] = $recurringContribution['campaign_id'];
+        }
+      }
+
       $contributionParams['skipLineItem'] = TRUE;
       $contributionParams['status_id'] = 'Pending';
       $contributionParams['financial_type_id'] = $templateContribution['financial_type_id'];
index dc1a9861ab93a6a767aa4ae4c24432e8e3e778ee..4ee8872a01f96d793e413b54156e4997307ccb1c 100644 (file)
@@ -648,6 +648,16 @@ function _civicrm_api3_contribution_repeattransaction_spec(&$params) {
     'name' => 'trxn_id',
     'type' => CRM_Utils_Type::T_STRING,
   );
+  $params['campaign_id'] = array(
+    'title' => 'Campaign ID',
+    'name' => 'campaign_id',
+    'type' => CRM_Utils_Type::T_INT,
+    'pseudoconstant' => array(
+      'table' => 'civicrm_campaign',
+      'keyColumn' => 'id',
+      'labelColumn' => 'title',
+    ),
+  );
   $params['payment_processor_id'] = array(
     'description' => ts('Payment processor ID, will be loaded from contribution_recur if not provided'),
     'title' => 'Payment processor ID',
index 664f4f99d1c06aed5879493d7758e112a5d2b75f..2c82baaeda4e5c605ff40c0009656ae609d4dff4 100644 (file)
@@ -1451,8 +1451,6 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
    */
   public function testRepeatTransactionAlteredAmount() {
     $paymentProcessorID = $this->paymentProcessorCreate();
-    $campaignID = $this->campaignCreate();
-    $campaignID2 = $this->campaignCreate();
     $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
       'contact_id' => $this->_individualId,
       'installments' => '12',
@@ -1468,7 +1466,6 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
         $this->_params,
         array(
           'contribution_recur_id' => $contributionRecur['id'],
-          'campaign_id' => $campaignID,
         ))
     );
 
@@ -1478,7 +1475,6 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
       'trxn_id' => uniqid(),
       'total_amount' => '400',
       'fee_amount' => 50,
-      'campaign_id' => $campaignID2,
     ));
     $lineItemParams = array(
       'entity_id' => $originalContribution['id'],
@@ -1497,7 +1493,6 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
     );
     $this->callAPISuccessGetSingle('contribution', array(
       'total_amount' => 400,
-      'campaign_id' => $campaignID2,
       'fee_amount' => 50,
       'net_amount' => 350,
     ));
@@ -1517,8 +1512,86 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
     unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
     unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
     $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
+  }
 
-    $this->quickCleanUpFinancialEntities();
+  /**
+   * CRM-16397 test appropriate action if total amount has changed for single line items.
+   */
+  public function testRepeatTransactionPassedInCampaign() {
+    $paymentProcessorID = $this->paymentProcessorCreate();
+    $campaignID = $this->campaignCreate();
+    $campaignID2 = $this->campaignCreate();
+    $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
+      'contact_id' => $this->_individualId,
+      'installments' => '12',
+      'frequency_interval' => '1',
+      'amount' => '100',
+      'contribution_status_id' => 1,
+      'start_date' => '2012-01-01 00:00:00',
+      'currency' => 'USD',
+      'frequency_unit' => 'month',
+      'payment_processor_id' => $paymentProcessorID,
+    ));
+    $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
+      $this->_params,
+      array(
+        'contribution_recur_id' => $contributionRecur['id'],
+        'campaign_id' => $campaignID,
+      ))
+    );
+
+    $this->callAPISuccess('contribution', 'repeattransaction', array(
+      'original_contribution_id' => $originalContribution['id'],
+      'contribution_status_id' => 'Completed',
+      'trxn_id' => uniqid(),
+      'campaign_id' => $campaignID2,
+    ));
+
+    $this->callAPISuccessGetSingle('contribution', array(
+      'total_amount' => 100,
+      'campaign_id' => $campaignID2,
+    ));
+  }
+
+  /**
+   * CRM-17718 campaign stored on contribution recur gets priority.
+   *
+   * This reflects the fact we permit people to update them.
+   */
+  public function testRepeatTransactionUpdatedCampaign() {
+    $paymentProcessorID = $this->paymentProcessorCreate();
+    $campaignID = $this->campaignCreate();
+    $campaignID2 = $this->campaignCreate();
+    $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
+      'contact_id' => $this->_individualId,
+      'installments' => '12',
+      'frequency_interval' => '1',
+      'amount' => '100',
+      'contribution_status_id' => 1,
+      'start_date' => '2012-01-01 00:00:00',
+      'currency' => 'USD',
+      'frequency_unit' => 'month',
+      'payment_processor_id' => $paymentProcessorID,
+      'campaign_id' => $campaignID,
+    ));
+    $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
+      $this->_params,
+      array(
+        'contribution_recur_id' => $contributionRecur['id'],
+        'campaign_id' => $campaignID2,
+      ))
+    );
+
+    $this->callAPISuccess('contribution', 'repeattransaction', array(
+      'original_contribution_id' => $originalContribution['id'],
+      'contribution_status_id' => 'Completed',
+      'trxn_id' => uniqid(),
+    ));
+
+    $this->callAPISuccessGetSingle('contribution', array(
+      'total_amount' => 100,
+      'campaign_id' => $campaignID,
+    ));
   }
 
   /**