Add unit test for net_amount when fee_amount is set
authoreileen <emcnaughton@wikimedia.org>
Mon, 29 Jul 2019 07:00:39 +0000 (19:00 +1200)
committereileen <emcnaughton@wikimedia.org>
Mon, 29 Jul 2019 09:43:21 +0000 (21:43 +1200)
This just improves testing & ensures fee_amount is used to set net amount.

I got some odd results on financial_trxn & financial_item but just adding checks for
what I found atm

CRM/Event/Form/Registration/Confirm.php
Civi/Payment/System.php
tests/phpunit/CRM/Event/Form/Registration/ConfirmTest.php

index 55e2e91e690ee405fa8db193578ea2097cb3b7b3..1aa29c43e0ee0ce0d310c2d0e533281f0ee3ec2a 100644 (file)
@@ -1002,7 +1002,6 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration {
     if (!$pending && $result) {
       $contribParams += [
         'fee_amount' => CRM_Utils_Array::value('fee_amount', $result),
-        'net_amount' => CRM_Utils_Array::value('net_amount', $result, $params['amount']),
         'trxn_id' => $result['trxn_id'],
         'receipt_date' => $receiptDate,
       ];
index 44047cdc4107483ede61c25604a291a3d50d745d..d7b6f0e2c0524a10dc56b0db03a0ae9593165f50 100644 (file)
@@ -105,6 +105,7 @@ class System {
    * @param int $id
    *
    * @return \CRM_Core_Payment|NULL
+   *
    * @throws \CiviCRM_API3_Exception
    */
   public function getById($id) {
index 5960691e872092117e8166dea3fce90c55af48fb..4fa5ca7e8f723947a3311508f1840db70ec02c16 100644 (file)
@@ -90,6 +90,9 @@ class CRM_Event_Form_Registration_ConfirmTest extends CiviUnitTestCase {
   public function testPaidSubmit($thousandSeparator) {
     $this->setCurrencySeparators($thousandSeparator);
     $paymentProcessorID = $this->processorCreate();
+    /* @var \CRM_Core_Payment_Dummy $processor */
+    $processor = Civi\Payment\System::singleton()->getById($paymentProcessorID);
+    $processor->setDoDirectPaymentResult(['fee_amount' => 1.67]);
     $params = ['is_monetary' => 1, 'financial_type_id' => 1];
     $event = $this->eventCreate($params);
     $individualID = $this->individualCreate();
@@ -153,6 +156,8 @@ class CRM_Event_Form_Registration_ConfirmTest extends CiviUnitTestCase {
     $this->callAPISuccessGetCount('Participant', [], 1);
     $contribution = $this->callAPISuccessGetSingle('Contribution', []);
     $this->assertEquals(8000.67, $contribution['total_amount']);
+    $this->assertEquals(1.67, $contribution['fee_amount']);
+    $this->assertEquals(7999, $contribution['net_amount']);
     $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
     $financialTrxn = $this->callAPISuccessGetSingle(
       'FinancialTrxn',
@@ -164,6 +169,35 @@ class CRM_Event_Form_Registration_ConfirmTest extends CiviUnitTestCase {
     $this->assertEquals(CRM_Utils_Array::value('payment_processor_id', $financialTrxn), $paymentProcessorID);
     $this->assertEquals(CRM_Utils_Array::value('card_type_id.label', $financialTrxn), 'Visa');
     $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), 1111);
+
+    // This looks like it's missing an item for the main contribution - but just locking in current behaviour.
+    $financialItems = $this->callAPISuccess('FinancialItem', 'get', [
+      'return' => ['description', 'financial_account_id', 'amount', 'contact_id', 'currency', 'status_id', 'entity_table', 'entity_id'],
+      'sequential' => 1,
+    ])['values'];
+
+    $entityFinancialTrxns = $this->callAPISuccess('EntityFinancialTrxn', 'get', ['sequential' => 1])['values'];
+
+    $this->assertAPIArrayComparison([
+      'entity_table' => 'civicrm_contribution',
+      'entity_id' => $contribution['id'],
+      'financial_trxn_id' => $financialTrxn['id'],
+      'amount' => '8000.67',
+    ], $entityFinancialTrxns[0], ['id']);
+
+    $this->assertAPIArrayComparison([
+      'entity_table' => 'civicrm_contribution',
+      'entity_id' => $contribution['id'],
+      'financial_trxn_id' => $financialTrxn['id'] + 1,
+      'amount' => '1.67',
+    ], $entityFinancialTrxns[1], ['id']);
+
+    $this->assertAPIArrayComparison([
+      'entity_table' => 'civicrm_financial_item',
+      'entity_id' => $financialItems[0]['id'],
+      'financial_trxn_id' => $financialTrxn['id'] + 1,
+      'amount' => '1.67',
+    ], $entityFinancialTrxns[2], ['id', 'entity_id']);
   }
 
   /**