From c1797e865254db8a7d52ac20eb987b5b9ca3aec7 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 29 Jul 2019 19:00:39 +1200 Subject: [PATCH] Add unit test for net_amount when fee_amount is set 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 | 1 - Civi/Payment/System.php | 1 + .../Event/Form/Registration/ConfirmTest.php | 34 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/CRM/Event/Form/Registration/Confirm.php b/CRM/Event/Form/Registration/Confirm.php index 55e2e91e69..1aa29c43e0 100644 --- a/CRM/Event/Form/Registration/Confirm.php +++ b/CRM/Event/Form/Registration/Confirm.php @@ -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, ]; diff --git a/Civi/Payment/System.php b/Civi/Payment/System.php index 44047cdc41..d7b6f0e2c0 100644 --- a/Civi/Payment/System.php +++ b/Civi/Payment/System.php @@ -105,6 +105,7 @@ class System { * @param int $id * * @return \CRM_Core_Payment|NULL + * * @throws \CiviCRM_API3_Exception */ public function getById($id) { diff --git a/tests/phpunit/CRM/Event/Form/Registration/ConfirmTest.php b/tests/phpunit/CRM/Event/Form/Registration/ConfirmTest.php index 5960691e87..4fa5ca7e8f 100644 --- a/tests/phpunit/CRM/Event/Form/Registration/ConfirmTest.php +++ b/tests/phpunit/CRM/Event/Form/Registration/ConfirmTest.php @@ -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']); } /** -- 2.25.1