Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev/core#1405 Strip any spaces from Option Group name parameter and r… #15937

Merged
merged 1 commit into from
Dec 2, 2019

Conversation

seamuslee001
Copy link
Contributor

@seamuslee001 seamuslee001 commented Nov 23, 2019

…emove the name field from front end form as will be auto generated

Overview

This removes the name field from the form to add a new option group as really it should be a calculated field. It also Ensures that option values are fixed on upgrade

Before

Option group names can be created with a space in it causing errors when trying to add new option valuse

After

All option group names are lower case and for new option groups created primarily on the basis of the title of the option group.

Agileware Ref: CIVICRM-1373

ping @eileenmcnaughton @colemanw @jusfreeman

@civibot
Copy link

civibot bot commented Nov 23, 2019

(Standard links)

@civibot civibot bot added the master label Nov 23, 2019
@seamuslee001 seamuslee001 force-pushed the dev_core_1405 branch 2 times, most recently from 5bd1bff to 5fbf3d3 Compare November 23, 2019 02:50
@jusfreeman
Copy link
Contributor

Fantastic, thanks @seamuslee001 will test and report back here.

@jusfreeman
Copy link
Contributor

Fixes this issue, https://lab.civicrm.org/dev/core/issues/1405

@seamuslee001 thanks also for including the Agileware Ref made it much easier to find the related issues. Legend!

Copy link
Contributor

@jusfreeman jusfreeman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @seamuslee001 - I think the main thing here is that the option group name should be lower case, not preserve mixed case from the title.

As a side comment, I really don't understand why CRM_Utils_String::titleToVar doesn't do the case conversion as that would save work here and possibly elsewhere too.

public static function formRule($fields, $files, $self) {
$errors = [];
if ($self->_id) {
$name = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGrou', $self->_id, 'name');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo here.

Should be:
$name = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $self->_id, 'name');

@@ -76,6 +76,12 @@ public static function add(&$params, $ids = []) {
CRM_Core_Error::deprecatedFunctionWarning('no $ids array');
$params['id'] = $ids['optionGroup'];
}
if (empty($params['name']) && empty($params['id'])) {
$params['name'] = CRM_Utils_String::titleToVar($params['title']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a strtolower here, otherwise Option Group Title: "Didgeridoo Kakapo" is saved as Option Group Name: "Didgeridoo_Kakapo" but should be "didgeridoo_kakapo"

$params['name'] = CRM_Utils_String::titleToVar(strtolower($params['title']));

$params['name'] = CRM_Utils_String::titleToVar($params['title']);
}
elseif (!empty($params['name']) && strpos($params['name'], ' ')) {
$params['name'] = CRM_Utils_String::titleToVar($params['name']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also convert to lowercase for any existing mixed case Option Group Names.

$params['name'] = CRM_Utils_String::titleToVar(strtolower($params['name']));

$name = CRM_Utils_String::titleToVar(strtolower($fields['title']));
}
if (!CRM_Core_DAO::objectExists($name, 'CRM_Core_DAO_OptionGroup', $self->_id)) {
$errors['title'] = ts('Option Group name already exists in the database. Option Group Names need to be unique');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include the name in the error message so that user knows what value is conflicting, otherwise it's not clear. Shorter message too.

$errors['title'] = ts('Option Group name: ' . $name .' already exists. Option Group Names need to be unique');

foreach ($optionGroups as $optionGroup) {
$name = trim($optionGroup['name']);
if (strpos($name, ' ') !== FALSE) {
$fixedName = CRM_Utils_String::titleToVar($name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Convert to lowercase here as well during the upgrade.

$fixedName = CRM_Utils_String::titleToVar(strtolower($name));

Probably should at least log what option group names have been changed so that at least CiviCRM developers are forewarned after the upgrade that potentially any custom integration referring to the option group by name will now be broken. Just a suggestion.

*/
public function testFixOptionGroupName() {
$name = 'This is a test Name';
$fixedName = CRM_Utils_String::titleToVar($name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a strtolower here too.

}
elseif (!empty($params['name']) && strpos($params['name'], ' ')) {
$params['name'] = CRM_Utils_String::titleToVar($params['name']);
}
$optionGroup = new CRM_Core_DAO_OptionGroup();
$optionGroup->copyValues($params);;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo with double ;;

@@ -44,7 +44,7 @@ public function testEnsureOptionGroupExistsNewValue() {

CRM_Core_BAO_OptionGroup::ensureOptionGroupExists(['name' => 'Bombed Again']);
$optionGroups = $this->callAPISuccess('OptionValue', 'getoptions', ['field' => 'option_group_id'])['values'];
$this->assertTrue(in_array('Bombed Again', $optionGroups));
$this->assertTrue(in_array('Bombed_Again', $optionGroups));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be "bombed_again" - lower case.

Copy link
Contributor

@jusfreeman jusfreeman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes requested

@mattwire
Copy link
Contributor

@seamuslee001 Just wanted to check that this won't stop you manually setting name via API / xml option group import?

@seamuslee001
Copy link
Contributor Author

I don't think so but it would make it without spaces tho.

@mattwire
Copy link
Contributor

I don't think so but it would make it without spaces tho.

Ok, that seems fine to me then

…emove the name field from front end form as will be auto generated

Ensure that option group names are lower csae

Ensure that option group names are created as lower case and fix as per Justin's review
@seamuslee001
Copy link
Contributor Author

ok @jusfreeman have made those changes now

Copy link
Contributor

@jusfreeman jusfreeman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seamuslee001 looks good to me!

@jusfreeman
Copy link
Contributor

#merge-on-pass

@seamuslee001
Copy link
Contributor Author

Merging as per review from @jusfreeman

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants