Skip to content

Commit

Permalink
dev/core#2242 Ensure that when a custom field is deleted any associat…
Browse files Browse the repository at this point in the history
…ed mapping field records are also deleted to avoid DB errors when doing exports
  • Loading branch information
seamuslee001 committed Dec 13, 2020
1 parent 5c7ad4a commit 751dec0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CRM/Core/BAO/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -1005,12 +1005,12 @@ public static function deleteField($field) {
//not delete the option group and related option values
self::checkOptionGroup($field->option_group_id);
}

// next drop the column from the custom value table
self::createField($field, 'delete');

$field->delete();
CRM_Core_BAO_UFField::delUFField($field->id);
CRM_Core_BAO_Mapping::removeFieldFromMapping('custom_' . $field->id);
CRM_Utils_Weight::correctDuplicateWeights('CRM_Core_DAO_CustomField');

CRM_Utils_Hook::post('delete', 'CustomField', $field->id, $field);
Expand Down
10 changes: 10 additions & 0 deletions CRM/Core/BAO/Mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -1213,4 +1213,14 @@ public static function saveMappingFields($params, $mappingId) {
}
}

/**
* Remove references to a specific field from save Mappings
* @param string $fieldName
*/
public static function removeFieldFromMapping($fieldName): void {
$mappingField = new CRM_Core_DAO_MappingField();
$mappingField->name = $fieldName;
$mappingField->delete();
}

}
28 changes: 28 additions & 0 deletions tests/phpunit/api/v3/CustomFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,34 @@ public function testCustomFieldDelete() {
$this->assertAPISuccess($result);
}

/**
* Check That any associated Mapping Field Entries are also removed.
*/
public function testCustomFieldDeleteWithMappingField() {
$customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'test_group']);
$customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
$this->assertNotNull($customField['id']);
$mapping = $this->callAPISuccess('Mapping', 'create', [
'name' => 'test mapping',
'mapping_type_id' => 'Export Contact',
]);
$mappingField = $this->callAPISuccess('MappingField', 'create', [
'mapping_id' => $mapping['id'],
'name' => 'custom_' . $customField['id'],
'grouping' => 1,
'column_number' => 0,
]);
$mappingFieldCheck = $this->callAPISuccess('MappingField', 'get', ['mapping_id' => $mapping['id']]);
$this->assertCount(1, $mappingFieldCheck['values']);
$params = [
'id' => $customField['id'],
];
$this->callAPISuccess('custom_field', 'delete', $params);
$mappingFieldCheck = $this->callAPISuccess('MappingField', 'get', ['mapping_id' => $mapping['id']]);
$this->assertCount(0, $mappingFieldCheck['values']);
$this->callAPISuccess('Mapping', 'delete', ['id' => $mapping['id']]);
}

/**
* Check for Option Value.
*/
Expand Down

0 comments on commit 751dec0

Please sign in to comment.