CRM-19741 Add back in fix and add in unit tests to lock in fix
authorSeamus Lee <seamuslee001@gmail.com>
Thu, 6 Apr 2017 00:58:29 +0000 (10:58 +1000)
committerSeamus Lee <seamuslee001@gmail.com>
Thu, 6 Apr 2017 04:32:46 +0000 (14:32 +1000)
CRM/Price/BAO/PriceField.php
tests/phpunit/api/v3/PriceFieldTest.php

index 8e1829be01ada38546873ad281992e93fb23084e..6c6070d59e44d8c39d118f7a374aec90cbfa4256 100644 (file)
@@ -94,8 +94,7 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
     }
     $optionsIds = array();
     $maxIndex = CRM_Price_Form_Field::NUM_OPTION;
-    $priceField2 = civicrm_api3('price_field', 'getsingle', array('id' => $priceField->id));
-    if ($priceField2['html_type'] == 'Text') {
+    if ($priceField->html_type == 'Text') {
       $maxIndex = 1;
       $fieldOptions = civicrm_api3('price_field_value', 'get', array(
         'price_field_id' => $priceField->id,
@@ -103,6 +102,13 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
       ));
       foreach ($fieldOptions['values'] as $option) {
         $optionsIds['id'] = $option['id'];
+        // CRM-19741 If we are dealing with price fields that are Text only set the field value label to match
+        if (!empty($params['id']) && $priceField->label != $option['label']) {
+          $fieldValue = new CRM_Price_DAO_PriceFieldValue();
+          $fieldValue->label = $priceField->label;
+          $fieldValue->id = $option['id'];
+          $fieldValue->save();
+        }
       }
     }
     $defaultArray = array();
@@ -170,12 +176,12 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
           throw new CRM_Core_Exception($e->getMessage());
         }
       }
-      elseif (!empty($optionIds)) {
-        $optionsLoad = civicrm_api3('price_field_value', 'get', array('id' => $optionIds['id']));
-        $options = $optionsLoad['values'][$option['id']];
+      elseif (!empty($optionsIds)) {
+        $optionsLoad = civicrm_api3('price_field_value', 'get', array('id' => $optionsIds['id']));
+        $options = $optionsLoad['values'][$optionsIds['id']];
         $options['is_active'] = CRM_Utils_Array::value('is_active', $params, 1);
         try {
-          CRM_Price_BAO_PriceFieldValue::create($options, $optionIds);
+          CRM_Price_BAO_PriceFieldValue::create($options, $optionsIds);
         }
         catch (Exception $e) {
           $transaction->rollback();
index 32d64b96233f321a8fb6dd7a39d3c79ef51ba894..4798a4d890a47ad676b43baed69c74659a0f613d 100644 (file)
@@ -115,4 +115,44 @@ class api_v3_PriceFieldTest extends CiviUnitTestCase {
     $this->assertEquals(1, $result['values']['options_per_line']['type']);
   }
 
+  /**
+   * CRM-19741
+   * Test updating the label of a texte price field and ensure price field value label is also updated
+   */
+  public function testUpdatePriceFieldLabel() {
+    $field = $this->callAPISuccess($this->_entity, 'create', $this->_params);
+    $this->callAPISuccess('price_field_value', 'create', array(
+      'price_field_id' => $field['id'],
+      'name' => 'rye grass',
+      'label' => 'juicy and healthy',
+      'amount' => 1,
+      'financial_type_id' => 1,
+    ));
+    $priceField = $this->callAPISuccess($this->_entity, 'create', array('id' => $field['id'], 'label' => 'Rose Variety'));
+    $priceFieldValue = $this->callAPISuccess('price_field_value', 'get', array('price_field_id' => $field['id']));
+    $this->assertEquals($priceField['values'][$priceField['id']]['label'], $priceFieldValue['values'][$priceFieldValue['id']]['label']);
+    $this->callAPISuccess('price_field_value', 'delete', array('id' => $priceFieldValue['id']));
+    $this->callAPISuccess($this->_entity, 'delete', array('id' => $field['id']));
+  }
+
+  /**
+   * CRM-19741
+   * Confirm value label only updates if fiedl type is html.
+   */
+  public function testUpdatePriceFieldLabelNotUpdateField() {
+    $field = $this->callAPISuccess($this->_entity, 'create', array_merge($this->_params, array('html_type' => 'Radio')));
+    $this->callAPISuccess('price_field_value', 'create', array(
+      'price_field_id' => $field['id'],
+      'name' => 'rye grass',
+      'label' => 'juicy and healthy',
+      'amount' => 1,
+      'financial_type_id' => 1,
+    ));
+    $priceField = $this->callAPISuccess($this->_entity, 'create', array('id' => $field['id'], 'label' => 'Rose Variety'));
+    $priceFieldValue = $this->callAPISuccess('price_field_value', 'get', array('price_field_id' => $field['id']));
+    $this->assertEquals('juicy and healthy', $priceFieldValue['values'][$priceFieldValue['id']]['label']);
+    $this->callAPISuccess('price_field_value', 'delete', array('id' => $priceFieldValue['id']));
+    $this->callAPISuccess($this->_entity, 'delete', array('id' => $field['id']));
+  }
+
 }