Skip to content

Commit

Permalink
Merge pull request #23540 from eileenmcnaughton/import_location
Browse files Browse the repository at this point in the history
Add location import testing
  • Loading branch information
colemanw authored May 23, 2022
2 parents bacaa92 + 7a8f99e commit 25d9481
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
First Name,Last Name,Birth Date,Street Address,City,Postal Code,Country,State,Email,Signature Text,IM,Website,Phone,Phone Ext,Mum’s first name,Last Name,Mum-Street Address,Mum-City,Mum-Country,Mum-State,Mum-email,Mum-signature Test,Mum-IM,Mum-website,Mum-phone,Mum-phone-ext,Mum-home-phone,Mum-home-mobile,Sister-Street Address,Sister-City,Sister-Country,Sister-State,Sister-email,Sister-signature Test,Sister-IM,Sister-website,Sister-phone,Sister-phone-ext,Team,Team Website,Team email ,Team Reference,Team Address1,Team Address 2,Team address Validity Date,Team Backup Website
Susie,Jones,2002-01-08,24 Adelaide Road,Sydney,90210,Australia,NSW,susie@example.com,Regards,susiej,https://susie.example.com,999-4445,123,Mum,Jones,The Green House,,,,mum@example.com,,Mum-IM,http://mum.example.com,911,1,88-999,99-888,,,New Zealand,,sis@example.com,,,,555-666,,Soccer Superstars,https://super.example.org,tt@example.org,T-882,PO Box 999,Marion Square,2022-03-04,http://super-t.example.org
85 changes: 83 additions & 2 deletions tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Civi\Api4\Address;
use Civi\Api4\Contact;
use Civi\Api4\ContactType;
use Civi\Api4\LocationType;
use Civi\Api4\RelationshipType;
use Civi\Api4\UserJob;

Expand Down Expand Up @@ -1144,6 +1145,86 @@ public function dateDataProvider(): array {
];
}

/**
* Test location importing, including for related contacts.
*
* @throws \CRM_Core_Exception
* @throws \API_Exception
*/
public function testImportLocations(): void {
$csv = 'individual_locations_with_related.csv';
$relationships = (array) RelationshipType::get()->addSelect('name_a_b', 'id')->addWhere('name_a_b', 'IN', [
'Child of',
'Sibling of',
'Employee of',
])->execute()->indexBy('name_a_b');

$childKey = $relationships['Child of']['id'] . '_a_b';
$siblingKey = $relationships['Sibling of']['id'] . '_a_b';
$employeeKey = $relationships['Employee of']['id'] . '_a_b';
$locations = LocationType::get()->execute()->indexBy('name');
$phoneTypeID = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Phone', 'phone_type_id', 'Phone');
$mobileTypeID = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Phone', 'phone_type_id', 'Mobile');
$skypeTypeID = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_IM', 'provider_id', 'Skype');
$mainWebsiteTypeID = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Website', 'website_type_id', 'Main');
$linkedInTypeID = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Website', 'website_type_id', 'LinkedIn');
$homeID = $locations['Home']['id'];
$workID = $locations['Work']['id'];
$mapper = [
['first_name'],
['last_name'],
['birth_date'],
['street_address', $homeID],
['city', $homeID],
['postal_code', $homeID],
['country', $homeID],
['state_province', $homeID],
// No location type ID means 'Primary'
['email'],
['signature_text'],
['im', NULL, $skypeTypeID],
['url', $mainWebsiteTypeID],
['phone', $homeID, $phoneTypeID],
['phone_ext', $homeID, $phoneTypeID],
[$childKey, 'first_name'],
[$childKey, 'last_name'],
[$childKey, 'street_address'],
[$childKey, 'city'],
[$childKey, 'country'],
[$childKey, 'state_province'],
[$childKey, 'email', $homeID],
[$childKey, 'signature_text', $homeID],
[$childKey, 'im', $homeID, $skypeTypeID],
[$childKey, 'url', $linkedInTypeID],
// Same location type, different phone typ in these phones
[$childKey, 'phone', $homeID, $phoneTypeID],
[$childKey, 'phone_ext', $homeID, $phoneTypeID],
[$childKey, 'phone', $homeID, $mobileTypeID],
[$childKey, 'phone_ext', $homeID, $mobileTypeID],
[$siblingKey, 'street_address', $homeID],
[$siblingKey, 'city', $homeID],
[$siblingKey, 'country', $homeID],
[$siblingKey, 'state_province', $homeID],
[$siblingKey, 'email', $homeID],
[$siblingKey, 'signature_text', $homeID],
[$childKey, 'im', $homeID, $skypeTypeID],
// The 2 is website_type_id (yes, small hard-coding cheat)
[$siblingKey, 'url', $linkedInTypeID],
[$siblingKey, 'phone', $workID, $phoneTypeID],
[$siblingKey, 'phone_ext', $workID, $phoneTypeID],
[$employeeKey, 'organization_name'],
[$employeeKey, 'url', $mainWebsiteTypeID],
[$employeeKey, 'email', $homeID],
[$employeeKey, 'do_not_import'],
[$employeeKey, 'street_address', $homeID],
[$employeeKey, 'supplemental_address_1', $homeID],
[$employeeKey, 'do_not_import'],
// Second website, different type.
[$employeeKey, 'url', $linkedInTypeID],
];
$this->validateCSV($csv, $mapper);
}

/**
* Test that setting duplicate action to fill doesn't blow away data
* that exists, but does fill in where it's empty.
Expand Down Expand Up @@ -1585,9 +1666,8 @@ protected function getUserJobID($submittedValues = []) {
* Any submitted values overrides.
*
* @throws \API_Exception
* @throws \CRM_Core_Exception
*/
protected function validateCSV(string $csv, array $mapper, $submittedValues): void {
protected function validateCSV(string $csv, array $mapper, array $submittedValues = []): void {
[$dataSource, $parser] = $this->getDataSourceAndParser($csv, $mapper, $submittedValues);
$parser->validateValues(array_values($dataSource->getRow()));
}
Expand All @@ -1612,6 +1692,7 @@ protected function importCSV(string $csv, array $mapper, array $submittedValues
'mapper' => $mapper,
'dataSource' => 'CRM_Import_DataSource_CSV',
'file' => ['name' => $csv],
'dateFormats' => CRM_Core_Form_Date::DATE_yyyy_mm_dd,
'onDuplicate' => CRM_Import_Parser::DUPLICATE_UPDATE,
'groups' => [],
], $submittedValues);
Expand Down

0 comments on commit 25d9481

Please sign in to comment.