Merge pull request #9155 from JMAConsulting/CRM-19438
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionTest.php
index 12bc23a3c005fa41619ce59741f4a1dee4b455d7..d3d1ca43caef1a2efd7b64c48abdb1f8ca76f769 100644 (file)
@@ -1058,6 +1058,31 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
     $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId);
   }
 
+  /**
+   * Function tests that financial records are updated when Payment Instrument is changed when amount is negative.
+   */
+  public function testCreateUpdateNegativeContributionPaymentInstrument() {
+    $instrumentId = $this->_addPaymentInstrument();
+    $contribParams = array(
+      'contact_id' => $this->_individualId,
+      'total_amount' => -100.00,
+      'financial_type_id' => $this->_financialTypeId,
+      'payment_instrument_id' => 4,
+      'contribution_status_id' => 1,
+
+    );
+    $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
+
+    $newParams = array_merge($contribParams, array(
+        'id' => $contribution['id'],
+        'payment_instrument_id' => $instrumentId,
+      )
+    );
+    $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
+    $this->assertAPISuccess($contribution);
+    $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId, array('total_amount' => '-100.00'));
+  }
+
   /**
    * Function tests that financial records are added when Contribution is Refunded.
    */
@@ -2568,9 +2593,36 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
         'Event',
       )
     );
+
+    $this->checkCreditCardDetails($mut, $contribution['id']);
     $mut->stop();
   }
 
+  /**
+   * Check credit card details in sent mail via API
+   *
+   * @param $mut obj CiviMailUtils instance
+   * @param int $contributionID Contribution ID
+   *
+   */
+  public function checkCreditCardDetails($mut, $contributionID) {
+    $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
+    $this->callAPISuccess('contribution', 'sendconfirmation', array(
+        'id' => $contributionID,
+        'receipt_from_email' => 'api@civicrm.org',
+        'payment_processor_id' => $this->paymentProcessorID,
+      )
+    );
+    $mut->checkMailLog(array(
+        'Credit Card Information', // credit card header
+        'Billing Name and Address', // billing header
+        'anthony_anderson@civicrm.org', // billing name
+      ), array(
+        'Event',
+      )
+    );
+  }
+
   /**
    * Test sending a mail via the API.
    */
@@ -2849,10 +2901,18 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
       $trxnParams1 = array(
         'id' => $trxn['financial_trxn_id'],
       );
-      $compareParams = array(
-        'total_amount' => -100,
-        'status_id' => 1,
-      );
+      if (empty($extraParams)) {
+        $compareParams = array(
+          'total_amount' => -100,
+          'status_id' => 1,
+        );
+      }
+      else {
+        $compareParams = array(
+          'total_amount' => 100,
+          'status_id' => 1,
+        );
+      }
       if ($context == 'paymentInstrument') {
         $compareParams += array(
           'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount(4),