[REF] Refactor price field form to allow for unit testing of the form
authorSeamus Lee <seamuslee001@gmail.com>
Wed, 9 Sep 2020 03:08:52 +0000 (13:08 +1000)
committerSeamus Lee <seamuslee001@gmail.com>
Wed, 9 Sep 2020 06:04:42 +0000 (16:04 +1000)
Set the assert equals to be an actual float number

CRM/Price/Form/Field.php
tests/phpunit/CRM/Price/Form/FieldTest.php

index 023066db68b5d84d45d967c9136f389ecbcec976..de3b607e3b98748ac8fff89ab86c0984a16a57fc 100644 (file)
@@ -77,6 +77,13 @@ class CRM_Price_Form_Field extends CRM_Core_Form {
    */
   protected $_useForMember;
 
+  /**
+   * Set the price Set Id (only used in tests)
+   */
+  public function setPriceSetId($priceSetId) {
+    $this->_sid = $priceSetId;
+  }
+
   /**
    * Set variables up before form is built.
    */
@@ -641,6 +648,25 @@ class CRM_Price_Form_Field extends CRM_Core_Form {
   public function postProcess() {
     // store the submitted values in an array
     $params = $this->controller->exportValues('Field');
+    $params['id'] = $this->getEntityId();
+    $priceField = $this->submit($params);
+    if (!is_a($priceField, 'CRM_Core_Error')) {
+      // Required by extensions implementing the postProcess hook (to get the ID of new entities)
+      $this->setEntityId($priceField->id);
+      CRM_Core_Session::setStatus(ts('Price Field \'%1\' has been saved.', [1 => $priceField->label]), ts('Saved'), 'success');
+    }
+    $buttonName = $this->controller->getButtonName();
+    $session = CRM_Core_Session::singleton();
+    if ($buttonName == $this->getButtonName('next', 'new')) {
+      CRM_Core_Session::setStatus(ts(' You can add another price set field.'), '', 'info');
+      $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=add&sid=' . $this->_sid));
+    }
+    else {
+      $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid));
+    }
+  }
+
+  public function submit($params) {
     $params['price'] = CRM_Utils_Rule::cleanMoney($params['price']);
     foreach ($params['option_amount'] as $key => $amount) {
       $params['option_amount'][$key] = CRM_Utils_Rule::cleanMoney($amount);
@@ -686,26 +712,10 @@ class CRM_Price_Form_Field extends CRM_Core_Form {
       $params['option_visibility_id'] = [1 => CRM_Utils_Array::value('visibility_id', $params)];
     }
 
-    $params['id'] = $this->getEntityId();
-
     $params['membership_num_terms'] = (!empty($params['membership_type_id'])) ? CRM_Utils_Array::value('membership_num_terms', $params, 1) : NULL;
 
     $priceField = CRM_Price_BAO_PriceField::create($params);
-
-    if (!is_a($priceField, 'CRM_Core_Error')) {
-      // Required by extensions implementing the postProcess hook (to get the ID of new entities)
-      $this->setEntityId($priceField->id);
-      CRM_Core_Session::setStatus(ts('Price Field \'%1\' has been saved.', [1 => $priceField->label]), ts('Saved'), 'success');
-    }
-    $buttonName = $this->controller->getButtonName();
-    $session = CRM_Core_Session::singleton();
-    if ($buttonName == $this->getButtonName('next', 'new')) {
-      CRM_Core_Session::setStatus(ts(' You can add another price set field.'), '', 'info');
-      $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=add&sid=' . $this->_sid));
-    }
-    else {
-      $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/price/field', 'reset=1&action=browse&sid=' . $this->_sid));
-    }
+    return $priceField;
   }
 
 }
index f7ba89f11d17f74fcbcb53c0ceb0c0d3252ab195..e4c71d881900cd4ae0420da09f700a2a67961eda 100644 (file)
@@ -57,6 +57,35 @@ class CRM_Price_Form_FieldTest extends CiviUnitTestCase {
     $this->assertTrue(array_key_exists('visibility_id', $validationResult));
   }
 
+  /**
+   * Test submitting a large float value is stored correctly in the db.
+   *
+   * @param string $thousandSeparator
+   *   punctuation used to refer to thousands.
+   *
+   * @dataProvider getThousandSeparators
+   */
+  public function testLargeFloatOptionValue($thousandSeparator) {
+    $this->setCurrencySeparators($thousandSeparator);
+    $thousands = Civi::settings()->get('monetaryThousandSeparator');
+    $decimal = Civi::settings()->get('monetaryDecimalPoint');
+    $paramsSet['title'] = 'Price Set' . substr(sha1(rand()), 0, 7);
+    $paramsSet['name'] = CRM_Utils_String::titleToVar($paramsSet['title']);
+    $paramsSet['is_active'] = TRUE;
+    $paramsSet['financial_type_id'] = 'Event Fee';
+    $paramsSet['extends'] = 1;
+    $priceSet = $this->callAPISuccess('price_set', 'create', $paramsSet);
+    $form = new CRM_Price_Form_Field();
+    $form->_action = CRM_Core_Action::ADD;
+    $form->setPriceSetId($priceSet['id']);
+    $this->publicFieldParams['option_label'][1] = 'Large Float';
+    $this->publicFieldParams['option_amount'][1] = '123' . $thousands . '456' . $thousands . '789' . $decimal . '987654321';
+    $this->publicFieldParams['option_visibility_id'][1] = $this->visibilityOptionsKeys['public'];
+    $priceField = $form->submit($this->publicFieldParams);
+    $priceOptions = $this->callAPISuccess('PriceFieldValue', 'get', ['price_field_id' => $priceField->id]);
+    $this->assertEquals(123456789.987654321, $priceOptions['values'][$priceOptions['id']]['amount']);
+  }
+
   private function initializeFieldParameters($params) {
     $defaultParams = [
       'label' => 'Price Field',
@@ -68,6 +97,7 @@ class CRM_Price_Form_FieldTest extends CiviUnitTestCase {
       'is_enter_qty' => 1,
       'financial_type_id' => $this->getFinancialTypeId('Event Fee'),
       'visibility_id' => $this->visibilityOptionsKeys['public'],
+      'price' => 10,
     ];
 
     for ($index = 1; $index <= CRM_Price_Form_Field::NUM_OPTION; $index++) {