* File for the CRM_Contact_Imports_Parser_ContactTest class.
*/
+use Civi\Api4\UserJob;
+
/**
* Test contact import parser.
*
* @throws \CRM_Core_Exception
*/
public function tearDown(): void {
- $this->quickCleanup(['civicrm_address', 'civicrm_phone', 'civicrm_email'], TRUE);
+ $this->quickCleanup(['civicrm_address', 'civicrm_phone', 'civicrm_email', 'civicrm_user_job'], TRUE);
parent::tearDown();
}
'legal_name' => 'Agileware',
]);
$contactImportValues = [
- "first_name" => "Alok",
- "last_name" => "Patel",
- "Employee of" => "Agileware",
+ '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, []);
- $parser->_contactType = 'Individual';
+ $parser->setUserJobID($this->getUserJobID());
$parser->init();
$this->mapRelationshipFields($fields, $parser->getAllFields());
], [
NULL,
NULL,
- "Organization",
+ 'Organization',
], [
NULL,
NULL,
- "organization_name",
+ 'organization_name',
], [], [], [], [], []);
-
- $parser->_contactType = 'Individual';
+ $parser->setUserJobID($this->getUserJobID());
$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", [
- "first_name" => "Alok",
- "last_name" => "Patel",
- "organization_name" => "Agileware",
+ 'first_name' => 'Alok',
+ 'last_name' => 'Patel',
+ 'organization_name' => 'Agileware',
]);
}
*
* There is an expectation that you can import by label here.
*
+ * @throws \API_Exception
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
['name' => 'prefix_id', 'column_number' => 3],
['name' => 'suffix_id', 'column_number' => 4],
];
+
$processor = new CRM_Import_ImportProcessor();
$processor->setMappingFields($mapping);
$processor->setContactType('Individual');
+ $processor->setUserJobID($this->getUserJobID());
$importer = $processor->getImporterObject();
$contactValues = [
/**
* Test importing 2 phones of different types.
*
+ * @throws \API_Exception
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
- public function testImportTwoPhonesDifferentTypes() {
+ public function testImportTwoPhonesDifferentTypes(): void {
$processor = new CRM_Import_ImportProcessor();
- $processor->setContactType('Individual');
+ $processor->setUserJobID($this->getUserJobID());
$processor->setMappingFields(
[
['name' => 'first_name'],
$this->assertEquals(array_search('United States', $countries), $addresses['values'][1]['country_id']);
}
+ /**
+ * Test importing fields with various options.
+ *
+ * Ensure we can import multiple preferred_communication_methods, single
+ * gender, and single preferred language using both labels and values.
+ *
+ * @throws \API_Exception
+ * @throws \CRM_Core_Exception
+ * @throws \CiviCRM_API3_Exception
+ */
+ public function testImportFieldsWithVariousOptions(): void {
+ $processor = new CRM_Import_ImportProcessor();
+ $processor->setUserJobID($this->getUserJobID());
+ $processor->setMappingFields(
+ [
+ ['name' => 'first_name'],
+ ['name' => 'last_name'],
+ ['name' => 'preferred_communication_method'],
+ ['name' => 'gender_id'],
+ ['name' => 'preferred_language'],
+ ]
+ );
+ $importer = $processor->getImporterObject();
+ $fields = ['Ima', 'Texter', 'SMS,Phone', 'Female', 'Danish'];
+ $importer->import(CRM_Import_Parser::DUPLICATE_NOCHECK, $fields);
+ $contact = $this->callAPISuccessGetSingle('Contact', ['last_name' => 'Texter']);
+
+ $this->assertEquals([4, 1], $contact['preferred_communication_method'], "Import multiple preferred communication methods using labels.");
+ $this->assertEquals(1, $contact['gender_id'], "Import gender with label.");
+ $this->assertEquals('da_DK', $contact['preferred_language'], "Import preferred language with label.");
+
+ $importer = $processor->getImporterObject();
+ $fields = ['Ima', 'Texter', "4,1", "1", "da_DK"];
+ $importer->import(CRM_Import_Parser::DUPLICATE_NOCHECK, $fields);
+ $contact = $this->callAPISuccessGetSingle('Contact', ['last_name' => 'Texter']);
+
+ $this->assertEquals([4, 1], $contact['preferred_communication_method'], "Import multiple preferred communication methods using values.");
+ $this->assertEquals(1, $contact['gender_id'], "Import gender with id.");
+ $this->assertEquals('da_DK', $contact['preferred_language'], "Import preferred language with value.");
+ }
+
/**
* Run the import parser.
*
}
$values = array_values($originalValues);
$parser = new CRM_Contact_Import_Parser_Contact($fields, $mapperLocType);
- $parser->_contactType = 'Individual';
+ $parser->setUserJobID($this->getUserJobID());
$parser->_dedupeRuleGroupID = $ruleGroupId;
$parser->_onDuplicate = $onDuplicateAction;
$parser->init();
}
}
+ /**
+ * Test mapping fields within the Parser class.
+ *
+ * @throws \API_Exception
+ * @throws \Civi\API\Exception\UnauthorizedException
+ */
+ public function testMapFields(): void {
+ $parser = new CRM_Contact_Import_Parser_Contact(
+ // Array of field names
+ ['first_name', 'phone', NULL, 'im', NULL],
+ // Array of location types, ie columns 2 & 4 have types.
+ [NULL, 1, NULL, 1, NULL],
+ // Array of phone types
+ [NULL, 1, NULL, NULL, NULL],
+ // Array of im provider types
+ [NULL, NULL, NULL, 1, NULL],
+ // Array of filled in relationship values.
+ [NULL, NULL, '5_a_b', NULL, '5_a_b'],
+ // Array of the contact type to map to - note this can be determined from ^^
+ [NULL, NULL, 'Organization', NULL, 'Organization'],
+ // Related contact field names
+ [NULL, NULL, 'url', NULL, 'phone'],
+ // Related contact location types
+ [NULL, NULL, NULL, NULL, 1],
+ // Related contact phone types
+ [NULL, NULL, NULL, NULL, 1],
+ // Related contact im provider types
+ [NULL, NULL, NULL, NULL, NULL],
+ // Website types
+ [NULL, NULL, NULL, NULL, NULL],
+ // Related contact website types
+ [NULL, NULL, 1, NULL, NULL]
+ );
+ $parser->setUserJobID($this->getUserJobID([
+ 'mapper' => [
+ ['first_name'],
+ ['phone', 1, 1],
+ ['5_a_b', 'url', 1],
+ ['im', 1, 1],
+ ['5_a_b', 'phone', 1, 1],
+ ],
+ ]));
+ $parser->init();
+ $params = $parser->getMappedRow(
+ ['Bob', '123', 'https://example.org', 'my-handle', '456']
+ );
+ $this->assertEquals([
+ 'first_name' => 'Bob',
+ 'phone' => [
+ [
+ 'phone' => '123',
+ 'location_type_id' => 1,
+ 'phone_type_id' => 1,
+ ],
+ ],
+ '5_a_b' => [
+ 'contact_type' => 'Organization',
+ 'url' =>
+ [
+
+ [
+ 'url' => 'https://example.org',
+ 'website_type_id' => 1,
+ ],
+ ],
+ 'phone' =>
+ [
+ [
+ 'phone' => '456',
+ 'location_type_id' => 1,
+ 'phone_type_id' => 1,
+ ],
+ ],
+ ],
+ 'im' =>
+ [
+
+ [
+ 'im' => 'my-handle',
+ 'location_type_id' => 1,
+ 'provider_id' => 1,
+ ],
+ ],
+ 'contact_type' => 'Individual',
+ ], $params);
+ }
+
/**
* Set up the underlying contact.
*
return [$originalValues, $result];
}
+ /**
+ * @return mixed
+ * @throws \API_Exception
+ * @throws \Civi\API\Exception\UnauthorizedException
+ */
+ protected function getUserJobID($submittedValues = []) {
+ return UserJob::create()->setValues([
+ 'metadata' => [
+ 'submitted_values' => array_merge([
+ 'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL,
+ 'contactSubType' => '',
+ ], $submittedValues),
+ ],
+ 'status_id:name' => 'draft',
+ 'type_id:name' => 'contact_import',
+ ])->execute()->first()['id'];
+ }
+
}