From 2667017c613f67e6582b5edac6f9f33c46fe5d8f Mon Sep 17 00:00:00 2001 From: demeritcowboy Date: Mon, 22 Jun 2020 14:18:55 -0400 Subject: [PATCH] don't change name field when updating option value --- CRM/Custom/Form/Option.php | 6 +- tests/phpunit/CRM/Custom/Form/OptionTest.php | 69 ++++++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 tests/phpunit/CRM/Custom/Form/OptionTest.php diff --git a/CRM/Custom/Form/Option.php b/CRM/Custom/Form/Option.php index 5a0c3d5eb3..004e1d51a3 100644 --- a/CRM/Custom/Form/Option.php +++ b/CRM/Custom/Form/Option.php @@ -387,11 +387,10 @@ SELECT count(*) // set values for custom field properties and save $customOption = new CRM_Core_DAO_OptionValue(); $customOption->label = $params['label']; - $customOption->name = CRM_Utils_String::titleToVar($params['label']); $customOption->weight = $params['weight']; $customOption->description = $params['description']; $customOption->value = $params['value']; - $customOption->is_active = CRM_Utils_Array::value('is_active', $params, FALSE); + $customOption->is_active = $params['is_active'] ?? FALSE; $oldWeight = NULL; if ($this->_id) { @@ -399,6 +398,9 @@ SELECT count(*) CRM_Core_BAO_CustomOption::updateCustomValues($params); $oldWeight = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id, 'weight', 'id'); } + else { + $customOption->name = CRM_Utils_String::titleToVar($params['label']); + } $fieldValues = ['option_group_id' => $this->_optionGroupID]; $customOption->weight diff --git a/tests/phpunit/CRM/Custom/Form/OptionTest.php b/tests/phpunit/CRM/Custom/Form/OptionTest.php new file mode 100644 index 0000000000..33f08c89a0 --- /dev/null +++ b/tests/phpunit/CRM/Custom/Form/OptionTest.php @@ -0,0 +1,69 @@ +customGroupCreate(['extends' => 'Contact', 'title' => 'contact stuff']); + $customField = $this->customFieldOptionValueCreate($customGroup, 'myCustomField'); + $fid = $customField['id']; + $option_group_id = $customField['values'][$fid]['option_group_id']; + $optionValue = $this->callAPISuccess('OptionValue', 'get', [ + 'option_group_id' => $option_group_id, + 'sequential' => 1, + ])['values'][0]; + + // Run the form + $form = new CRM_Custom_Form_Option(); + $form->controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_Option', 'Custom Option'); + + $form->set('id', $optionValue['id']); + $form->set('fid', $customField['id']); + $form->set('gid', $customGroup['id']); + + ob_start(); + $form->controller->_actions['display']->perform($form, 'display'); + $contents = ob_get_contents(); + ob_end_clean(); + // We could check for something in $contents, but we don't really care + // what the form looks like here. + + // Submit the form + // + // This might not work if postProcess does something like access certain + // properties that here won't have been rebuilt from the full http post + // etc process. But at the moment it doesn't. + $container = &$form->controller->container(); + $container['values']['Option'] = [ + 'label' => 'Label changed', + 'value' => $optionValue['value'], + 'description' => '', + 'weight' => $optionValue['value'], + 'is_active' => '1', + // unchecked checkboxes don't submit any actual value + // 'default_value' => $optionValue['is_default'], + 'optionId' => $optionValue['id'], + 'fieldId' => $fid, + ]; + $form->mainProcess(); + + $newOptionValue = $this->callAPISuccess('OptionValue', 'get', [ + 'id' => $optionValue['id'], + ])['values'][$optionValue['id']]; + $this->assertEquals($optionValue['name'], $newOptionValue['name']); + $this->assertEquals($optionValue['value'], $newOptionValue['value']); + $this->assertEquals('Label changed', $newOptionValue['label']); + } + +} -- 2.25.1