// Allow hook_civicrm_merge() to add SQL statements for the merge operation.
CRM_Utils_Hook::merge('sqls', $sqls, $mainId, $otherId, $tables);
- // call the SQL queries in one transaction
- $transaction = new CRM_Core_Transaction();
foreach ($sqls as $sql) {
CRM_Core_DAO::executeQuery($sql, [], TRUE, NULL, TRUE);
}
CRM_Dedupe_Merger::addMembershipToRealtedContacts($mainId);
- $transaction->commit();
}
/**
if (empty($migrationInfo)) {
return FALSE;
}
+ // Encapsulate in a transaction to avoid half-merges.
+ $transaction = new CRM_Core_Transaction();
$contactType = $migrationInfo['main_details']['contact_type'];
$relTables = CRM_Dedupe_Merger::relTables();
CRM_Contact_BAO_Contact::createProfileContact($submitted, CRM_Core_DAO::$_nullArray, $mainId);
}
-
+ $transaction->commit();
CRM_Utils_Hook::post('merge', 'Contact', $mainId);
self::createMergeActivities($mainId, $otherId);
$sql = "SELECT entity_id, {$columnName} AS file_id FROM {$tableName} WHERE entity_id IN ({$mainId}, {$otherId})";
$dao = CRM_Core_DAO::executeQuery($sql);
while ($dao->fetch()) {
+ // @todo - this is actually broken - fix & or remove - see testMergeCustomFields
$fileIds[$dao->entity_id] = $dao->file_id;
if ($dao->entity_id == $mainId) {
CRM_Core_BAO_File::deleteFileReferences($fileIds[$mainId], $mainId, $customId);
*/
trait CRMTraits_Custom_CustomDataTrait {
+ /**
+ * Create a custom group with fields of multiple types.
+ *
+ * @param array $groupParams
+ */
+ public function createCustomGroupWithFieldsOfAllTypes($groupParams = []) {
+ $this->createCustomGroup($groupParams);
+ $this->ids['CustomField'] = $this->createCustomFieldsOfAllTypes();
+ }
+
/**
* Create a custom group.
*
];
$customField = $this->callAPISuccess('CustomField', 'create', $params);
- $ids[] = $customField['id'];
+ $ids['text'] = $customField['id'];
$optionValue[] = [
'label' => 'Red',
];
$customField = $this->callAPISuccess('custom_field', 'create', $params);
- $ids[] = $customField['id'];
+ $ids['select_string'] = $customField['id'];
$params = [
'custom_group_id' => $customGroupID,
'data_type' => 'Date',
'default_value' => '20090711',
'weight' => 3,
- 'is_required' => 1,
- 'is_searchable' => 0,
- 'is_active' => 1,
+ 'time_format' => 1,
];
$customField = $this->callAPISuccess('custom_field', 'create', $params);
- $ids[] = $customField['id'];
+ $ids['select_date'] = $customField['id'];
$params = [
'custom_group_id' => $customGroupID,
'name' => 'test_link',
];
$customField = $this->callAPISuccess('custom_field', 'create', $params);
- $ids[] = $customField['id'];
+
+ $ids['link'] = $customField['id'];
+ $fileField = $this->customFieldCreate([
+ 'custom_group_id' => $customGroupID,
+ 'data_type' => 'File',
+ 'html_type' => 'File',
+ 'default_value' => '',
+ ]);
+
+ $ids['file'] = $fileField['id'];
+ $ids['country'] = $this->customFieldCreate([
+ 'custom_group_id' => $customGroupID,
+ 'data_type' => 'Int',
+ 'html_type' => 'Select Country',
+ 'default_value' => '',
+ 'label' => 'Country',
+ 'option_type' => 0,
+ ])['id'];
+
return $ids;
}
+ /**
+ * Get the custom field name for the relevant key.
+ *
+ * e.g returns 'custom_5' where 5 is the id of the field using the key.
+ *
+ * Generally keys map to data types.
+ *
+ * @param string $key
+ *
+ * @return string
+ */
+ protected function getCustomFieldName($key) {
+ $linkField = 'custom_' . $this->ids['CustomField'][$key];
+ return $linkField;
+ }
+
}
* @group headless
*/
class api_v3_ContactTest extends CiviUnitTestCase {
+ use CRMTraits_Custom_CustomDataTrait;
+
public $DBResetRequired = FALSE;
protected $_apiversion;
protected $_entity;
protected $_contactID;
protected $_financialTypeId = 1;
+
+ /**
+ * Entity to be extended.
+ *
+ * @var string
+ */
+ protected $entity = 'Contact';
+
/**
* Test setup for every test.
*
}
+ /**
+ * Test merging 2 contacts with custom fields.
+ *
+ * @throws \Exception
+ */
+ public function testMergeCustomFields() {
+ $contact1 = $this->individualCreate();
+ /* Not sure this is quite right but it does get it into the file table
+ $file = $this->callAPISuccess('Attachment', 'create', [
+ 'name' => 'header.txt',
+ 'mime_type' => 'text/plain',
+ 'description' => 'My test description',
+ 'content' => 'My test content',
+ 'entity_table' => 'civicrm_contact',
+ 'entity_id' => $contact1,
+ ]);
+ */
+
+ $this->createCustomGroupWithFieldsOfAllTypes();
+ $fileField = $this->getCustomFieldName('file');
+ $linkField = $this->getCustomFieldName('link');
+ $dateField = $this->getCustomFieldName('select_date');
+ $selectField = $this->getCustomFieldName('select_string');
+ $countryField = $this->getCustomFieldName('country');
+
+ $countriesByName = array_flip(CRM_Core_PseudoConstant::country(FALSE, FALSE));
+ $customFieldValues = [
+ // @todo fix the fatal bug on this & uncomment - see dev/core#723
+ // $fileField => $file['id'],
+ $linkField => 'http://example.org',
+ $dateField => '2018-01-01 17:10:56',
+ $selectField => 'G',
+ // Currently broken.
+ //$countryField => $countriesByName['New Zealand'],
+ ];
+ $this->callAPISuccess('Contact', 'create', array_merge([
+ 'id' => $contact1,
+ ], $customFieldValues));
+
+ $contact2 = $this->individualCreate();
+ $this->callAPISuccess('contact', 'merge', [
+ 'to_keep_id' => $contact2,
+ 'to_remove_id' => $contact1,
+ 'auto_flip' => FALSE,
+ ]);
+ $contact = $this->callAPISuccessGetSingle('Contact', ['id' => $contact2, 'return' => array_keys($customFieldValues)]);
+ foreach ($customFieldValues as $key => $value) {
+ $this->assertEquals($value, $contact[$key]);
+ }
+ }
+
/**
* Test retrieving merged contacts.
*
* Check relationship creation with custom data.
*/
public function testRelationshipCreateEditWithCustomData() {
- $this->createCustomGroup();
- $this->_ids = $this->createCustomFieldsOfAllTypes();
+ $this->createCustomGroupWithFieldsOfAllTypes();
//few custom Values for comparing
- $custom_params = array(
- "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship',
- "custom_{$this->_ids[1]}" => 'Y',
- "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
- "custom_{$this->_ids[3]}" => 'http://example.com',
- );
-
- $params = array(
+ $custom_params = [
+ $this->getCustomFieldName('text') => 'Hello! this is custom data for relationship',
+ $this->getCustomFieldName('select_string') => 'Y',
+ $this->getCustomFieldName('select_date') => '2009-07-11 00:00:00',
+ $this->getCustomFieldName('link') => 'http://example.com',
+ ];
+
+ $params = [
'contact_id_a' => $this->_cId_a,
'contact_id_b' => $this->_cId_b,
'relationship_type_id' => $this->_relTypeID,
'start_date' => '2008-12-20',
'is_active' => 1,
- );
+ ];
$params = array_merge($params, $custom_params);
$result = $this->callAPISuccess('relationship', 'create', $params);
- $relationParams = array(
- 'id' => $result['id'],
- );
+ $relationParams = ['id' => $result['id']];
$this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
//Test Edit of custom field from the form.
$getParams = array('id' => $result['id']);
$updateParams = array_merge($getParams, array(
- "custom_{$this->_ids[0]}" => 'Edited Text Value',
+ $this->getCustomFieldName('text') => 'Edited Text Value',
'relationship_type_id' => $this->_relTypeID . '_b_a',
'related_contact_id' => $this->_cId_a,
));
$reln->submit($updateParams);
$check = $this->callAPISuccess('relationship', 'get', $getParams);
- $this->assertEquals("Edited Text Value", $check['values'][$check['id']]["custom_{$this->_ids[0]}"]);
+ $this->assertEquals("Edited Text Value", $check['values'][$check['id']][$this->getCustomFieldName('text')]);
$params['id'] = $result['id'];
$this->callAPISuccess('relationship', 'delete', $params);
* should be OK if the custom field values differ.
*/
public function testRelationshipCreateDuplicateWithCustomFields() {
- $this->createCustomGroup();
- $this->_ids = $this->createCustomFieldsOfAllTypes();
+ $this->createCustomGroupWithFieldsOfAllTypes();
$custom_params_1 = array(
- "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship',
- "custom_{$this->_ids[1]}" => 'Y',
- "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
- "custom_{$this->_ids[3]}" => 'http://example.com',
+ $this->getCustomFieldName('text') => 'Hello! this is custom data for relationship',
+ $this->getCustomFieldName('select_string') => 'Y',
+ $this->getCustomFieldName('select_date') => '2009-07-11 00:00:00',
+ $this->getCustomFieldName('link') => 'http://example.com',
);
$custom_params_2 = array(
- "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
- "custom_{$this->_ids[1]}" => 'Y',
- "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
- "custom_{$this->_ids[3]}" => 'http://example.org',
+ $this->getCustomFieldName('text') => 'Hello! this is other custom data',
+ $this->getCustomFieldName('select_string') => 'Y',
+ $this->getCustomFieldName('select_date') => '2009-07-11 00:00:00',
+ $this->getCustomFieldName('link') => 'http://example.org',
);
$params = array(
* does.
*/
public function testRelationshipCreateDuplicateWithCustomFields2() {
- $this->createCustomGroup();
- $this->_ids = $this->createCustomFieldsOfAllTypes();
+ $this->createCustomGroupWithFieldsOfAllTypes();
$custom_params_2 = array(
- "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
- "custom_{$this->_ids[1]}" => 'Y',
- "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
- "custom_{$this->_ids[3]}" => 'http://example.org',
+ $this->getCustomFieldName('text') => 'Hello! this is other custom data',
+ $this->getCustomFieldName('select_string') => 'Y',
+ $this->getCustomFieldName('select_date') => '2009-07-11 00:00:00',
+ $this->getCustomFieldName('link') => 'http://example.org',
);
- $params_1 = array(
+ $params_1 = [
'contact_id_a' => $this->_cId_a,
'contact_id_b' => $this->_cId_b,
'relationship_type_id' => $this->_relTypeID,
'start_date' => '2008-12-20',
'is_active' => 1,
- );
+ ];
$params_2 = array_merge($params_1, $custom_params_2);
* does not.
*/
public function testRelationshipCreateDuplicateWithCustomFields3() {
- $this->createCustomGroup();
- $this->_ids = $this->createCustomFieldsOfAllTypes();
-
- $custom_params_1 = array(
- "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
- "custom_{$this->_ids[1]}" => 'Y',
- "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
- "custom_{$this->_ids[3]}" => 'http://example.org',
- );
+ $this->createCustomGroupWithFieldsOfAllTypes();
+
+ $custom_params_1 = [
+ $this->getCustomFieldName('text') => 'Hello! this is other custom data',
+ $this->getCustomFieldName('select_string') => 'Y',
+ $this->getCustomFieldName('select_date') => '2009-07-11 00:00:00',
+ $this->getCustomFieldName('link') => 'http://example.org',
+ ];
$params_2 = array(
'contact_id_a' => $this->_cId_a,