Skip to content

Commit

Permalink
Merge pull request #15028 from eileenmcnaughton/import_processor
Browse files Browse the repository at this point in the history
[Test] [Ref] [Import]Add wrapper class for importProcessor
seamuslee001 authored Aug 15, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 25318b0 + 0b0285b commit 51d349a
Showing 3 changed files with 157 additions and 2 deletions.
4 changes: 2 additions & 2 deletions CRM/Contact/Import/Parser/Contact.php
Original file line number Diff line number Diff line change
@@ -122,11 +122,11 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser {
* @param array $mapperRelatedContactWebsiteType
*/
public function __construct(
&$mapperKeys, $mapperLocType = [], $mapperPhoneType = [], $mapperImProvider = [], $mapperRelated = [], $mapperRelatedContactType = [], $mapperRelatedContactDetails = [], $mapperRelatedContactLocType = [], $mapperRelatedContactPhoneType = [], $mapperRelatedContactImProvider = [],
$mapperKeys, $mapperLocType = [], $mapperPhoneType = [], $mapperImProvider = [], $mapperRelated = [], $mapperRelatedContactType = [], $mapperRelatedContactDetails = [], $mapperRelatedContactLocType = [], $mapperRelatedContactPhoneType = [], $mapperRelatedContactImProvider = [],
$mapperWebsiteType = [], $mapperRelatedContactWebsiteType = []
) {
parent::__construct();
$this->_mapperKeys = &$mapperKeys;
$this->_mapperKeys = $mapperKeys;
$this->_mapperLocType = &$mapperLocType;
$this->_mapperPhoneType = &$mapperPhoneType;
$this->_mapperWebsiteType = $mapperWebsiteType;
121 changes: 121 additions & 0 deletions CRM/Import/ImportProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

/**
* Class CRM_Import_ImportProcessor.
*
* Import processor class. This is intended to provide a sanitising wrapper around
* the form-oriented import classes. In particular it is intended to provide a clear translation
* between the saved mapping field format and the quick form & parser formats.
*
* In the first instance this is only being used in unit tests but the intent is to migrate
* to it on a trajectory similar to the ExportProcessor so it is not in the tests.
*/
class CRM_Import_ImportProcessor {

/**
* An array of fields in the format used in the table civicrm_mapping_field.
*
* @var array
*/
protected $mappingFields = [];

/**
* Get contact type being imported.
*
* @var string
*/
protected $contactType;

/**
* @return string
*/
public function getContactType(): string {
return $this->contactType;
}

/**
* @param string $contactType
*/
public function setContactType(string $contactType) {
$this->contactType = $contactType;
}

/**
* @return array
*/
public function getMappingFields(): array {
return $this->mappingFields;
}

/**
* @param array $mappingFields
*/
public function setMappingFields(array $mappingFields) {
$this->mappingFields = CRM_Utils_Array::rekey($mappingFields, 'column_number');
ksort($this->mappingFields);
$this->mappingFields = array_values($this->mappingFields);
}

/**
* Get the names of the mapped fields.
*/
public function getFieldNames() {
return CRM_Utils_Array::collect('name', $this->getMappingFields());
}

/**
* Get the location types of the mapped fields.
*/
public function getFieldLocationTypes() {
return CRM_Utils_Array::collect('location_type_id', $this->getMappingFields());
}

/**
* Get the phone types of the mapped fields.
*/
public function getFieldPhoneTypes() {
return CRM_Utils_Array::collect('phone_type_id', $this->getMappingFields());
}

/**
* Get the names of the im_provider fields.
*/
public function getFieldIMProviderTypes() {
return CRM_Utils_Array::collect('im_provider_id', $this->getMappingFields());
}

/**
* Get the names of the website fields.
*/
public function getFieldWebsiteTypes() {
return CRM_Utils_Array::collect('im_provider_id', $this->getMappingFields());
}

/**
* Get an instance of the importer object.
*
* @return CRM_Contact_Import_Parser_Contact
*/
public function getImporterObject() {
$importer = new CRM_Contact_Import_Parser_Contact(
$this->getFieldNames(),
$this->getFieldLocationTypes(),
$this->getFieldPhoneTypes(),
$this->getFieldIMProviderTypes(),
// @todo - figure out related mappings.
// $mapperRelated = [], $mapperRelatedContactType = [], $mapperRelatedContactDetails = [], $mapperRelatedContactLocType = [], $mapperRelatedContactPhoneType = [], $mapperRelatedContactImProvider = [],
[],
[],
[],
[],
[],
[],
$this->getFieldWebsiteTypes()
// $mapperRelatedContactWebsiteType = []
);
$importer->init();
$importer->_contactType = $this->getContactType();
return $importer;
}

}
34 changes: 34 additions & 0 deletions tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php
Original file line number Diff line number Diff line change
@@ -333,6 +333,40 @@ public function testGenderLabel() {
$this->callAPISuccessGetSingle('Contact', $contactValues);
}

/**
* Test prefix & suffix work when you specify the label.
*
* There is an expectation that you can import by label here.
*
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
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' => 'suffix_id', 'column_number' => 4],
];
$processor = new CRM_Import_ImportProcessor();
$processor->setMappingFields($mapping);
$processor->setContactType('Individual');
$importer = $processor->getImporterObject();

$contactValues = [
'Bill',
'Gates',
'bill.gates@microsoft.com',
'III',
'special',
];
$importer->import(CRM_Import_Parser::DUPLICATE_NOCHECK, $contactValues);

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

/**
* Test that labels work for importing custom data.
*

0 comments on commit 51d349a

Please sign in to comment.