CRM-17647 add tests & form cleaning of event thousand separators
authoreileen <emcnaughton@wikimedia.org>
Wed, 7 Feb 2018 08:41:31 +0000 (21:41 +1300)
committereileen <emcnaughton@wikimedia.org>
Thu, 8 Feb 2018 05:18:09 +0000 (18:18 +1300)
style fix

CRM/Event/Form/Registration/Confirm.php
tests/phpunit/CRM/Batch/Form/EntryTest.php
tests/phpunit/CRM/Event/Form/Registration/ConfirmTest.php

index 936444a86e37a5b3cab02a02cb4ed849bfd0abcb..43ed2923d26874acc4f48814bde32646ab212c4f 100644 (file)
@@ -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]);
+        }
+      }
+    }
+  }
+
 }
index fbb7a4389bf3b4f6332a11bba409d0c56919f1b6..726975dd695a159d2525447ab3514243a4cf3876 100644 (file)
@@ -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,
index 41e4f1c1c5271d32de8efc0df05bd76bdbdc42a7..e08508f13210c2d6f1a1b8d997ada04903d587c6 100644 (file)
@@ -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' => '\ 1Tiny-tots (ages 5-8) - 1\ 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',