diff --git a/CRM/Price/BAO/PriceField.php b/CRM/Price/BAO/PriceField.php index 8e1829be01ad..5d299fc76f9d 100644 --- a/CRM/Price/BAO/PriceField.php +++ b/CRM/Price/BAO/PriceField.php @@ -94,8 +94,7 @@ public static function create(&$params) { } $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 @@ public static function create(&$params) { )); 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(); diff --git a/tests/phpunit/api/v3/PriceFieldTest.php b/tests/phpunit/api/v3/PriceFieldTest.php index 32d64b96233f..4798a4d890a4 100644 --- a/tests/phpunit/api/v3/PriceFieldTest.php +++ b/tests/phpunit/api/v3/PriceFieldTest.php @@ -115,4 +115,44 @@ public function testGetFieldsPriceField() { $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'])); + } + }