Skip to content

Commit

Permalink
Use Processor to determine the phone_type_id & im_provider_id
Browse files Browse the repository at this point in the history
What we are doing here is actually very simple - despite the code being kinda insane - we are just getting
the  fields from the database for  each column in the mapping.

The fields are all loaded into the processor already & those functions are tested. This switches to  using them
and to passing around less variables...

Follow ons will do the same for other fields (website_type_id, location_type_id but I will add more tests as I do that
  • Loading branch information
eileenmcnaughton committed Aug 23, 2019
1 parent 7b1b7f8 commit cd41fa5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 19 deletions.
19 changes: 7 additions & 12 deletions CRM/Contact/Import/Form/MapField.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,6 @@ public function buildQuickForm() {
//get loaded Mapping Fields
$mappingName = CRM_Utils_Array::value(1, $mappingName);
$mappingLocation = CRM_Utils_Array::value(1, $mappingLocation);
$mappingPhoneType = CRM_Utils_Array::value(1, $mappingPhoneType);
$mappingImProvider = CRM_Utils_Array::value(1, $mappingImProvider);
$mappingRelation = CRM_Utils_Array::value(1, $mappingRelation);
$mappingWebsiteType = CRM_Utils_Array::value(1, $mappingWebsiteType);

Expand Down Expand Up @@ -409,7 +407,7 @@ public function buildQuickForm() {
$sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', [1 => $i]), NULL);

if ($this->get('savedMapping')) {
list($defaults, $js) = $this->loadSavedMapping($processor, $mappingName, $i, $mappingRelation, $mappingWebsiteType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $defaults, $js, $hasColumnNames, $dataPatterns, $columnPatterns);
list($defaults, $js) = $this->loadSavedMapping($processor, $mappingName, $i, $mappingRelation, $mappingWebsiteType, $mappingLocation, $defaults, $js, $hasColumnNames, $dataPatterns, $columnPatterns);
}
else {
$js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n";
Expand Down Expand Up @@ -854,17 +852,16 @@ protected function saveMappingField($mapperKeys, array $saveMapping, string $cTy
* @param $mappingRelation
* @param $mappingWebsiteType
* @param $mappingLocation
* @param $mappingPhoneType
* @param $mappingImProvider
* @param array $defaults
* @param string $js
* @param bool $hasColumnNames
* @param array $dataPatterns
* @param array $columnPatterns
*
* @return array
* @throws \CiviCRM_API3_Exception
*/
public function loadSavedMapping($processor, $mappingName, $i, $mappingRelation, $mappingWebsiteType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $defaults, $js, $hasColumnNames, $dataPatterns, $columnPatterns) {
public function loadSavedMapping($processor, $mappingName, $i, $mappingRelation, $mappingWebsiteType, $mappingLocation, $defaults, $js, $hasColumnNames, $dataPatterns, $columnPatterns) {
$jsSet = FALSE;
$formName = $processor->getFormName();
if (isset($mappingName[$i])) {
Expand Down Expand Up @@ -904,9 +901,8 @@ public function loadSavedMapping($processor, $mappingName, $i, $mappingRelation,
$contactDetails = strtolower(str_replace(" ", "_", $mappingName[$i]));
$websiteTypeId = isset($mappingWebsiteType[$i]) ? $mappingWebsiteType[$i] : NULL;
$locationId = isset($mappingLocation[$i]) ? $mappingLocation[$i] : 0;
$phoneType = isset($mappingPhoneType[$i]) ? $mappingPhoneType[$i] : NULL;
//get provider id from saved mappings
$imProvider = isset($mappingImProvider[$i]) ? $mappingImProvider[$i] : NULL;
$phoneType = $processor->getPhoneTypeID($i);
$imProvider = $processor->getIMProviderID($i);

if ($websiteTypeId) {
$defaults["mapper[$i]"] = [$relation, $contactDetails, $websiteTypeId];
Expand Down Expand Up @@ -947,9 +943,8 @@ public function loadSavedMapping($processor, $mappingName, $i, $mappingRelation,
$mappingHeader = array_keys((array) $this->_mapperFields, $mappingName[$i]);
$websiteTypeId = isset($mappingWebsiteType[$i]) ? $mappingWebsiteType[$i] : NULL;
$locationId = isset($mappingLocation[$i]) ? $mappingLocation[$i] : 0;
$phoneType = isset($mappingPhoneType[$i]) ? $mappingPhoneType[$i] : NULL;
// get IM service provider id
$imProvider = isset($mappingImProvider[$i]) ? $mappingImProvider[$i] : NULL;
$phoneType = $processor->getPhoneTypeID($i);
$imProvider = $processor->getIMProviderID($i);

if ($websiteTypeId) {
$defaults["mapper[$i]"] = [$mappingHeader[0], $websiteTypeId];
Expand Down
2 changes: 1 addition & 1 deletion CRM/Import/ImportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ public function getMetadataTitles() {
protected function rekeyBySortedColumnNumbers(array $mappingFields) {
$this->mappingFields = CRM_Utils_Array::rekey($mappingFields, 'column_number');
ksort($this->mappingFields);
return array_values($this->mappingFields);
return $this->mappingFields;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ protected function loadSavedMapping($form, $mappingID, $columnNumber) {
$processor->setFormName('document.forms.MapField');
$processor->setMetadata($this->getContactImportMetadata());

$return = $form->loadSavedMapping($processor, $mappingName, $columnNumber, $mappingRelation, $mappingWebsiteType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $defaults, $js, $hasColumnNames, $dataPatterns, $columnPatterns);
$return = $form->loadSavedMapping($processor, $mappingName, $columnNumber, $mappingRelation, $mappingWebsiteType, $mappingLocation, $defaults, $js, $hasColumnNames, $dataPatterns, $columnPatterns);
return ['defaults' => $return[0], 'js' => $return[1]];
}

Expand Down
11 changes: 6 additions & 5 deletions tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,10 @@ public function testGenderLabel() {
public function testPrefixLabel() {
$this->callAPISuccess('OptionValue', 'create', ['option_group_id' => 'individual_prefix', 'name' => 'new_one', 'label' => 'special', 'value' => 70]);
$mapping = [
['name' => 'first_name', 'column_number' => 1],
['name' => 'last_name', 'column_number' => 2],
['name' => 'email', 'column_number' => 3, 'location_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Email', 'location_type_id', 'Home')],
['name' => 'prefix_id', 'column_number' => 5],
['name' => 'first_name', 'column_number' => 0],
['name' => 'last_name', 'column_number' => 1],
['name' => 'email', 'column_number' => 2, 'location_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Email', 'location_type_id', 'Home')],
['name' => 'prefix_id', 'column_number' => 3],
['name' => 'suffix_id', 'column_number' => 4],
];
$processor = new CRM_Import_ImportProcessor();
Expand All @@ -359,12 +359,13 @@ public function testPrefixLabel() {
'Bill',
'Gates',
'[email protected]',
'III',
'special',
'III',
];
$importer->import(CRM_Import_Parser::DUPLICATE_NOCHECK, $contactValues);

$contact = $this->callAPISuccessGetSingle('Contact', ['first_name' => 'Bill', 'prefix_id' => 'new_one', 'suffix_id' => 'III']);
$this->assertEquals('special Bill Gates III', $contact['display_name']);
}

/**
Expand Down

0 comments on commit cd41fa5

Please sign in to comment.