* @group headless
*/
class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase {
- protected $_tablesToTruncate = ['civicrm_address', 'civicrm_phone', 'civicrm_email'];
+ use CRMTraits_Custom_CustomDataTrait;
+
+ /**
+ * Main entity for the class.
+ *
+ * @var string
+ */
+ protected $entity = 'Contact';
/**
* Setup function.
parent::setUp();
}
+ /**
+ * Tear down after test.
+ *
+ * @throws \CRM_Core_Exception
+ */
+ public function tearDown() {
+ $this->quickCleanup(['civicrm_address', 'civicrm_phone', 'civicrm_email'], TRUE);
+ parent::tearDown();
+ }
+
/**
* Test that import parser will add contact with employee of relationship.
*
$this->callAPISuccessGetSingle('Contact', $contactValues);
}
+ /**
+ * Test that labels work for importing custom data.
+ *
+ * @throws \CRM_Core_Exception
+ */
+ public function testCustomDataLabel() {
+ $this->createCustomGroupWithFieldOfType([], 'select');
+ $contactValues = [
+ 'first_name' => 'Bill',
+ 'last_name' => 'Gates',
+ 'email' => 'bill.gates@microsoft.com',
+ 'nick_name' => 'Billy-boy',
+ $this->getCustomFieldName('select') => 'Yellow',
+ ];
+ $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, [NULL, NULL, 'Primary', NULL, NULL]);
+ $contact = $this->callAPISuccessGetSingle('Contact', array_merge($contactValues, ['return' => $this->getCustomFieldName('select')]));
+ $this->assertEquals('Y', $contact[$this->getCustomFieldName('select')]);
+ }
+
+ /**
+ * Test that names work for importing custom data.
+ *
+ * @throws \CRM_Core_Exception
+ */
+ public function testCustomDataName() {
+ $this->createCustomGroupWithFieldOfType([], 'select');
+ $contactValues = [
+ 'first_name' => 'Bill',
+ 'last_name' => 'Gates',
+ 'email' => 'bill.gates@microsoft.com',
+ 'nick_name' => 'Billy-boy',
+ $this->getCustomFieldName('select') => 'Y',
+ ];
+ $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, [NULL, NULL, 'Primary', NULL, NULL]);
+ $contact = $this->callAPISuccessGetSingle('Contact', array_merge($contactValues, ['return' => $this->getCustomFieldName('select')]));
+ $this->assertEquals('Y', $contact[$this->getCustomFieldName('select')]);
+ }
+
/**
* Test that the import parser adds the address to the primary location.
*
* @throws \CRM_Core_Exception
*/
public function createCustomGroupWithFieldOfType($groupParams = [], $customFieldType = 'text', $identifier = '') {
- if ($customFieldType !== 'text') {
+ $supported = ['text', 'select'];
+ if (!in_array($customFieldType, $supported)) {
throw new CRM_Core_Exception('we have not yet extracted other custom field types from createCustomFieldsOfAllTypes, Use consistent syntax when you do');
}
$groupParams['title'] = empty($groupParams['title']) ? $identifier . 'Group with field ' . $customFieldType : $groupParams['title'];
$this->createCustomGroup($groupParams);
- $customField = $this->createTextCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['title']]]);
+ switch ($customFieldType) {
+ case 'text':
+ $customField = $this->createTextCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['title']]]);
+ break;
+
+ case 'select':
+ $customField = $this->createSelectCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['title']]]);
+ break;
+ }
$this->ids['CustomField'][$identifier . $customFieldType] = $customField['id'];
}
$customField = $this->createTextCustomField(['custom_group_id' => $customGroupID]);
$ids['text'] = $customField['id'];
- $optionValue[] = [
- 'label' => 'Red',
- 'value' => 'R',
- 'weight' => 1,
- 'is_active' => 1,
- ];
- $optionValue[] = [
- 'label' => 'Yellow',
- 'value' => 'Y',
- 'weight' => 2,
- 'is_active' => 1,
- ];
- $optionValue[] = [
- 'label' => 'Green',
- 'value' => 'G',
- 'weight' => 3,
- 'is_active' => 1,
- ];
-
- $params = [
- 'label' => 'Pick Color',
- 'html_type' => 'Select',
- 'data_type' => 'String',
- 'weight' => 2,
- 'is_required' => 1,
- 'is_searchable' => 0,
- 'is_active' => 1,
- 'option_values' => $optionValue,
- 'custom_group_id' => $customGroupID,
- ];
-
- $customField = $this->callAPISuccess('custom_field', 'create', $params);
+ $customField = $this->createSelectCustomField(['custom_group_id' => $customGroupID]);
$ids['select_string'] = $customField['id'];
$params = [
return $this->callAPISuccess('CustomField', 'create', $params)['values'][0];
}
+ /**
+ * Create custom select field.
+ *
+ * @param array $params
+ * Parameter overrides, must include custom_group_id.
+ *
+ * @return array
+ */
+ protected function createSelectCustomField(array $params): array {
+ $optionValue = [
+ [
+ 'label' => 'Red',
+ 'value' => 'R',
+ 'weight' => 1,
+ 'is_active' => 1,
+ ],
+ [
+ 'label' => 'Yellow',
+ 'value' => 'Y',
+ 'weight' => 2,
+ 'is_active' => 1,
+ ],
+ [
+ 'label' => 'Green',
+ 'value' => 'G',
+ 'weight' => 3,
+ 'is_active' => 1,
+ ],
+ ];
+
+ $params = array_merge([
+ 'label' => 'Pick Color',
+ 'html_type' => 'Select',
+ 'data_type' => 'String',
+ 'weight' => 2,
+ 'is_required' => 1,
+ 'is_searchable' => 0,
+ 'is_active' => 1,
+ 'option_values' => $optionValue,
+ ], $params);
+
+ $customField = $this->callAPISuccess('custom_field', 'create', $params);
+ return $customField['values'][$customField['id']];
+ }
+
}