From e1defd06401eb18fd46cacba13b5b2a8ab8fb0f9 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Thu, 6 Apr 2017 10:58:29 +1000 Subject: [PATCH] CRM-19741 Add back in fix and add in unit tests to lock in fix --- CRM/Price/BAO/PriceField.php | 18 +++++++---- tests/phpunit/api/v3/PriceFieldTest.php | 40 +++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/CRM/Price/BAO/PriceField.php b/CRM/Price/BAO/PriceField.php index 8e1829be01..6c6070d59e 100644 --- a/CRM/Price/BAO/PriceField.php +++ b/CRM/Price/BAO/PriceField.php @@ -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(); diff --git a/tests/phpunit/api/v3/PriceFieldTest.php b/tests/phpunit/api/v3/PriceFieldTest.php index 32d64b9623..4798a4d890 100644 --- a/tests/phpunit/api/v3/PriceFieldTest.php +++ b/tests/phpunit/api/v3/PriceFieldTest.php @@ -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'])); + } + } -- 2.25.1