Skip to content

Commit

Permalink
Initial test on loadSavedMapping function.
Browse files Browse the repository at this point in the history
To facilitate this I
1) made it public - in the end it will go anyway
2) removed a 'pass-through' variable from the return array - it is passed in & returned unchanged
3) made it cope without a notice when a class property is not defined - that will go too soon
  • Loading branch information
eileenmcnaughton committed Aug 16, 2019
1 parent 4009859 commit 1ab5cdf
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
8 changes: 4 additions & 4 deletions CRM/Contact/Import/Form/MapField.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ public function buildQuickForm() {
$sel = &$this->addElement('hierselect', "mapper[$i]", ts('Mapper for Field %1', [1 => $i]), NULL);

if ($this->get('savedMapping')) {
list($mappingName, $defaults, $js) = $this->loadSavedMapping($mappingName, $i, $mappingRelation, $mappingWebsiteType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $defaults, $formName, $js, $hasColumnNames, $dataPatterns, $columnPatterns);
list($defaults, $js) = $this->loadSavedMapping($mappingName, $i, $mappingRelation, $mappingWebsiteType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $defaults, $formName, $js, $hasColumnNames, $dataPatterns, $columnPatterns);
}
else {
$js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n";
Expand Down Expand Up @@ -856,7 +856,7 @@ protected function saveMappingField($mapperKeys, array $saveMapping, string $cTy
*
* @return array
*/
protected function loadSavedMapping($mappingName, $i, $mappingRelation, $mappingWebsiteType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $defaults, $formName, $js, $hasColumnNames, $dataPatterns, $columnPatterns) {
public function loadSavedMapping($mappingName, $i, $mappingRelation, $mappingWebsiteType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $defaults, $formName, $js, $hasColumnNames, $dataPatterns, $columnPatterns) {
$jsSet = FALSE;
if (isset($mappingName[$i])) {
if ($mappingName[$i] != ts('- do not import -')) {
Expand Down Expand Up @@ -935,7 +935,7 @@ protected function loadSavedMapping($mappingName, $i, $mappingRelation, $mapping
$jsSet = TRUE;
}
else {
$mappingHeader = array_keys($this->_mapperFields, $mappingName[$i]);
$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;
Expand Down Expand Up @@ -989,7 +989,7 @@ protected function loadSavedMapping($mappingName, $i, $mappingRelation, $mapping
$defaults["mapper[$i]"] = [$this->defaultFromData($dataPatterns, $i)];
}
}
return [$mappingName, $defaults, $js];
return [$defaults, $js];
}

}
73 changes: 73 additions & 0 deletions tests/phpunit/CRM/Contact/Import/Form/MapFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,77 @@ public function getFormObject($class, $formValues = [], $pageName = '') {
return $form;
}

/**
* Test the function that loads saved field mappings.
*
* @dataProvider mapFieldDataProvider
*
* @param array $fieldSpec
* @param string $expectedJS
*
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function testLoadSavedMapping($fieldSpec, $expectedJS) {
$form = $this->getFormObject('CRM_Contact_Import_Form_MapField');
/* @var CRM_Contact_Import_Form_MapField $form */
$form->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL);

$mapping = $this->callAPISuccess('Mapping', 'create', ['name' => 'my test']);
$this->callAPISuccess('MappingField', 'create', array_merge(['mapping_id' => $mapping], $fieldSpec));
$result = $this->loadSavedMapping($form, $mapping['id'], 1);
$this->assertEquals($expectedJS, $result['js']);
}

/**
* Get data for map field tests.
*/
public function mapFieldDataProvider() {
return [
[
['name' => 'First Name', 'contact_type' => 'Individual', 'column_number' => 1],
'document.forms.MapField[\'mapper[1][1]\'].style.display = \'none\';
document.forms.MapField[\'mapper[1][2]\'].style.display = \'none\';
document.forms.MapField[\'mapper[1][3]\'].style.display = \'none\';
',
],
];
}

/**
* Wrapper for loadSavedMapping.
*
* This signature of the function we are calling is funky as a new extraction & will be refined.
*
* @param \CRM_Contact_Import_Form_MapField $form
*
* @param int $mappingID
* @param int $columnNumber
*
* @return array
*
* @throws \CiviCRM_API3_Exception
*/
protected function loadSavedMapping($form, $mappingID, $columnNumber) {
list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $mappingRelation, $mappingOperator, $mappingValue, $mappingWebsiteType) = CRM_Core_BAO_Mapping::getMappingFields($mappingID, TRUE);

//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);
$defaults = [];
$formName = 'document.forms.MapField';
$js = '';
$hasColumnNames = TRUE;
// @todo - can use metadata trait once https://github.com/civicrm/civicrm-core/pull/15018 is merged.
$dataPatterns = [];
$columnPatterns = [];

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

}

0 comments on commit 1ab5cdf

Please sign in to comment.