From: eileen Date: Tue, 13 Aug 2019 10:46:49 +0000 (+1200) Subject: Add wrapper class for importProcessor X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=0b0285b17f05054a67b2264dfc55977c8086d886;p=civicrm-core.git Add wrapper class for importProcessor With the export cleanup creating a new sensible class & migrating code to it turned out to be a good approach. This adds a class that wraps around the Importer Object, taking the mapping_field db format of fields as a format - this is also where we wound up with the Export - using that format as it is what is saed. There is a gap as I noted in https://lab.civicrm.org/dev/core/issues/1172 around the schema's adequacy. That is not such an issue for this - in that we are not dealing with Contribution import and this is really just exposing the wrapper for the unit tests at this stage - the only change to 'live' code is a little less php v4 support - we have removed a bunch of these from the constructor of the other objects already with no observed issues --- diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index a93dfc1ae4..086e35fa04 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -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; diff --git a/CRM/Import/ImportProcessor.php b/CRM/Import/ImportProcessor.php new file mode 100644 index 0000000000..ffd38c5317 --- /dev/null +++ b/CRM/Import/ImportProcessor.php @@ -0,0 +1,121 @@ +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; + } + +} diff --git a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php index ff4e11c6c9..b7fe612e49 100644 --- a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php +++ b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php @@ -333,6 +333,40 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { $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. *