Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-20790: Unit test to ensure relationships are created on import. #11843

Merged
merged 1 commit into from
Mar 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CRM/Contact/Import/Parser/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
62 changes: 62 additions & 0 deletions tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down