From 3ae1b2f4a8c31373fd751ff9b57488329dc18e2f Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 7 Feb 2018 21:41:31 +1300 Subject: [PATCH] CRM-17647 add tests & form cleaning of event thousand separators style fix --- CRM/Event/Form/Registration/Confirm.php | 31 ++++++++++++++++++- tests/phpunit/CRM/Batch/Form/EntryTest.php | 2 +- .../Event/Form/Registration/ConfirmTest.php | 12 +++++-- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/CRM/Event/Form/Registration/Confirm.php b/CRM/Event/Form/Registration/Confirm.php index 936444a86e..43ed2923d2 100644 --- a/CRM/Event/Form/Registration/Confirm.php +++ b/CRM/Event/Form/Registration/Confirm.php @@ -49,6 +49,15 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration { */ public $_totalAmount; + /** + * Monetary fields that may be submitted. + * + * These should get a standardised format in the beginPostProcess function. + * + * These fields are common to many forms. Some may override this. + */ + protected $submittableMoneyFields = ['total_amount', 'net_amount', 'non_deductible_amount', 'fee_amount', 'tax_amount', 'amount']; + /** * Set variables up before form is built. */ @@ -205,6 +214,7 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration { $taxAmount = 0; foreach ($this->_params as $k => $v) { + $this->cleanMoneyFields($v); if ($v == 'skip') { continue; } @@ -398,6 +408,8 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration { $now = date('YmdHis'); $this->_params = $this->get('params'); + $this->cleanMoneyFields($this->_params); + if (!empty($this->_params[0]['contact_id'])) { // unclear when this would be set & whether it could be checked in getContactID. // perhaps it relates to when cid is in the url @@ -1039,6 +1051,7 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration { } $contribParams['skipLineItem'] = 1; + $contribParams['skipCleanMoney'] = 1; // create contribution record $contribution = CRM_Contribute_BAO_Contribution::add($contribParams); // CRM-11124 @@ -1296,7 +1309,8 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration { $_REQUEST['id'] = $form->_eventId = $params['id']; $form->controller = new CRM_Event_Controller_Registration(); $form->_params = $params['params']; - $form->_amount = $form->_totalAmount = CRM_Utils_Array::value('totalAmount', $params); + // This happens in buildQuickForm so emulate here. + $form->_amount = $form->_totalAmount = CRM_Utils_Rule::cleanMoney(CRM_Utils_Array::value('totalAmount', $params)); $form->set('params', $params['params']); $form->_values['custom_pre_id'] = array(); $form->_values['custom_post_id'] = array(); @@ -1332,4 +1346,19 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration { return array(); } + /** + * Clean money fields from the form. + * + * @param array $params + */ + protected function cleanMoneyFields(&$params) { + foreach ($this->submittableMoneyFields as $moneyField) { + foreach ($params as $index => $paramField) { + if (isset($paramField[$moneyField])) { + $params[$index][$moneyField] = CRM_Utils_Rule::cleanMoney($paramField[$moneyField]); + } + } + } + } + } diff --git a/tests/phpunit/CRM/Batch/Form/EntryTest.php b/tests/phpunit/CRM/Batch/Form/EntryTest.php index fbb7a4389b..726975dd69 100644 --- a/tests/phpunit/CRM/Batch/Form/EntryTest.php +++ b/tests/phpunit/CRM/Batch/Form/EntryTest.php @@ -351,7 +351,7 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase { 'field' => array( 1 => array( 'financial_type' => 1, - 'total_amount' => $this->formatMoneyInput(1500.15), + 'total_amount' => $this->formatMoneyInput(1500.15), 'receive_date' => '2013-07-24', 'receive_date_time' => NULL, 'payment_instrument' => 1, diff --git a/tests/phpunit/CRM/Event/Form/Registration/ConfirmTest.php b/tests/phpunit/CRM/Event/Form/Registration/ConfirmTest.php index 41e4f1c1c5..e08508f132 100644 --- a/tests/phpunit/CRM/Event/Form/Registration/ConfirmTest.php +++ b/tests/phpunit/CRM/Event/Form/Registration/ConfirmTest.php @@ -81,9 +81,14 @@ class CRM_Event_Form_Registration_ConfirmTest extends CiviUnitTestCase { /** * Initial test of submit function for paid event. * + * @param string $thousandSeparator + * + * @dataProvider getThousandSeparators + * * @throws \Exception */ - public function testPaidSubmit() { + public function testPaidSubmit($thousandSeparator) { + $this->setCurrencySeparators($thousandSeparator); $paymentProcessorID = $this->processorCreate(); $params = array('is_monetary' => 1, 'financial_type_id' => 1); $event = $this->eventCreate($params); @@ -93,7 +98,7 @@ class CRM_Event_Form_Registration_ConfirmTest extends CiviUnitTestCase { 'contributeMode' => 'direct', 'registerByID' => $individualID, 'paymentProcessorObj' => CRM_Financial_BAO_PaymentProcessor::getPayment($paymentProcessorID), - 'totalAmount' => 800, + 'totalAmount' => $this->formatMoneyInput(8000.67), 'params' => array( array( 'qfKey' => 'e6eb2903eae63d4c5c6cc70bfdda8741_2801', @@ -133,7 +138,7 @@ class CRM_Event_Form_Registration_ConfirmTest extends CiviUnitTestCase { 'participant_role_id' => '1', 'currencyID' => 'USD', 'amount_level' => 'Tiny-tots (ages 5-8) - 1', - 'amount' => '800.00', + 'amount' => $this->formatMoneyInput(8000.67), 'tax_amount' => NULL, 'year' => '2019', 'month' => '1', @@ -147,6 +152,7 @@ class CRM_Event_Form_Registration_ConfirmTest extends CiviUnitTestCase { )); $this->callAPISuccessGetCount('Participant', array(), 1); $contribution = $this->callAPISuccessGetSingle('Contribution', array()); + $this->assertEquals(8000.67, $contribution['total_amount']); $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC'); $financialTrxn = $this->callAPISuccessGetSingle( 'FinancialTrxn', -- 2.25.1