Skip to content

Commit

Permalink
CRM-21175 fix fatal error on multiple custom field import.
Browse files Browse the repository at this point in the history
Additional test is tangental....
  • Loading branch information
eileenmcnaughton committed Sep 12, 2017
1 parent 76f6dea commit 412585f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
3 changes: 1 addition & 2 deletions CRM/Contact/Import/Parser/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -1147,8 +1147,7 @@ public function fini() {
* @param null $relationships
*/
public static function isErrorInCustomData($params, &$errorMessage, $csType = NULL, $relationships = NULL) {
$session = CRM_Core_Session::singleton();
$dateType = $session->get("dateTypes");
$dateType = CRM_Core_Session::singleton()->get("dateTypes");

if (!empty($params['contact_sub_type'])) {
$csType = CRM_Utils_Array::value('contact_sub_type', $params);
Expand Down
34 changes: 34 additions & 0 deletions CRM/Core/BAO/Mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,40 @@ public static function getMappings($mappingType) {
return $mapping;
}

/**
* Get the mappings array, creating if it does not exist.
*
* @param string $mappingType
* Mapping type name.
*
* @return array
* Array of mapping names, keyed by id.
*
* @throws \CiviCRM_API3_Exception
*/
public static function getCreateMappingValues($mappingType) {
try {
return CRM_Core_BAO_Mapping::getMappings($mappingType);
}
catch (CiviCRM_API3_Exception $e) {
// Having a valid mapping_type_id is now enforced. However, rather than error let's
// add it. This is required for Multi value which could be done by upgrade script, but
// it feels like there could be other instances so this is safer.
$errorParams = $e->getExtraParams();
if ($errorParams['error_field'] === 'mapping_type_id') {
$mappingValues = civicrm_api3('Mapping', 'getoptions', array('field' => 'mapping_type_id'));
civicrm_api3('OptionValue', 'create', array(
'option_group_id' => 'mapping_type',
'label' => $mappingType,
'value' => max(array_keys($mappingValues['values'])) + 1,
'is_reserved' => 1,
));
return CRM_Core_BAO_Mapping::getMappings($mappingType);
}
throw $e;
}
}

/**
* Get the mapping fields.
*
Expand Down
3 changes: 1 addition & 2 deletions CRM/Import/Form/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ public function buildQuickForm() {

$this->add('text', 'fieldSeparator', ts('Import Field Separator'), array('size' => 2), TRUE);
$this->setDefaults(array('fieldSeparator' => $config->fieldSeparator));
$mappingArray = CRM_Core_BAO_Mapping::getCreateMappingValues('Import ' . static::IMPORT_ENTITY);

//get the saved mapping details
$mappingArray = CRM_Core_BAO_Mapping::getMappings('Import ' . static::IMPORT_ENTITY);
$this->assign('savedMapping', $mappingArray);
$this->add('select', 'savedMapping', ts('Mapping Option'), array('' => ts('- select -')) + $mappingArray);

Expand Down
14 changes: 14 additions & 0 deletions tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,20 @@ public function testImportPrimaryAddressUpdate() {
$this->callAPISuccessGetSingle('Contact', $contactValues);
}

/**
* Test the determination of whether a custom field is valid.
*/
public function testCustomFieldValidation() {
$errorMessage = array();
$customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'ABC'));
$customField = $this->customFieldOptionValueCreate($customGroup, 'fieldABC', array('html_type' => 'Multi-Select'));
$params = array(
'custom_' . $customField['id'] => 'Label1|Label2',
);
CRM_Contact_Import_Parser_Contact::isErrorInCustomData($params, $errorMessage);
$this->assertEquals(array(), $errorMessage);
}

/**
* Run the import parser.
*
Expand Down
6 changes: 4 additions & 2 deletions tests/phpunit/CiviTest/CiviUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2431,10 +2431,12 @@ public function tidyExampleResult(&$result) {
* @param array $customGroup
* @param string $name
* Name of custom field.
* @param array $extraParams
* Additional parameters to pass through.
*
* @return array|int
*/
public function customFieldOptionValueCreate($customGroup, $name) {
public function customFieldOptionValueCreate($customGroup, $name, $extraParams = array()) {
$fieldParams = array(
'custom_group_id' => $customGroup['id'],
'name' => 'test_custom_group',
Expand All @@ -2461,7 +2463,7 @@ public function customFieldOptionValueCreate($customGroup, $name) {
'option_status' => 1,
);

$params = array_merge($fieldParams, $optionGroup, $optionValue);
$params = array_merge($fieldParams, $optionGroup, $optionValue, $extraParams);

return $this->callAPISuccess('custom_field', 'create', $params);
}
Expand Down

0 comments on commit 412585f

Please sign in to comment.