diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index ad97146add13..b5043a135cae 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -456,6 +456,16 @@ public function summary(&$values) { return CRM_Import_Parser::VALID; } + /** + * Get Array of all the fields that could potentially be part + * import process + * + * @return array + */ + public function getAllFields() { + return $this->_fields; + } + /** * Handle the values in import mode. * diff --git a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php index 55af3f8cd98b..9fc3da8a92b1 100644 --- a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php +++ b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php @@ -46,6 +46,55 @@ public function setUp() { parent::setUp(); } + /** + * Test that import parser will add contact with employee of relationship. + * + * @throws \Exception + */ + public function testImportParserWtihEmployeeOfRelationship() { + $this->organizationCreate(array( + "organization_name" => "Agileware", + "legal_name" => "Agileware", + )); + $contactImportValues = array( + "first_name" => "Alok", + "last_name" => "Patel", + "Employee of" => "Agileware", + ); + + $fields = array_keys($contactImportValues); + $values = array_values($contactImportValues); + $parser = new CRM_Contact_Import_Parser_Contact($fields, NULL); + $parser->_contactType = 'Individual'; + $parser->init(); + $this->mapRelationshipFields($fields, $parser->getAllFields()); + + $parser = new CRM_Contact_Import_Parser_Contact($fields, NULL, NULL, NULL, array( + NULL, + NULL, + $fields[2], + ), array( + NULL, + NULL, + "Organization", + ), array( + NULL, + NULL, + "organization_name", + ), NULL, NULL, NULL, NULL, NULL); + + $parser->_contactType = 'Individual'; + $parser->_onDuplicate = CRM_Import_Parser::DUPLICATE_UPDATE; + $parser->init(); + + $this->assertEquals(CRM_Import_Parser::VALID, $parser->import(CRM_Import_Parser::DUPLICATE_UPDATE, $values), 'Return code from parser import was not as expected'); + $this->callAPISuccess("Contact", "get", array( + "first_name" => "Alok", + "last_name" => "Patel", + "organization_name" => "Agileware", + )); + } + /** * Test that import parser will not fail when same external_identifier found of deleted contact. * @@ -513,6 +562,19 @@ protected function runImport($originalValues, $onDuplicateAction, $expectedResul $this->assertEquals($expectedResult, $parser->import($onDuplicateAction, $values), 'Return code from parser import was not as expected'); } + /** + * @param array $fields Array of fields to be imported + * @param array $allfields Array of all fields which can be part of import + */ + private function mapRelationshipFields(&$fields, $allfields) { + foreach ($allfields as $key => $fieldtocheck) { + $elementIndex = array_search($fieldtocheck->_title, $fields); + if ($elementIndex !== FALSE) { + $fields[$elementIndex] = $key; + } + } + } + /** * Set up the underlying contact. *