Skip to content

Commit

Permalink
dev/core#1838 Ensure that no fatal error is triggered if you try to a…
Browse files Browse the repository at this point in the history
…ccess a merged contact and the contact that it was merged into has been permanently deleted
  • Loading branch information
seamuslee001 committed Sep 22, 2020
1 parent 3f7ae3f commit 9a2bf23
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
15 changes: 10 additions & 5 deletions api/v3/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -1308,11 +1308,16 @@ function _civicrm_api3_contact_getmergedto($params) {
],
])['values'];
if (!empty($deleteActivity)) {
$contactID = civicrm_api3('ActivityContact', 'getvalue', [
'activity_id' => $deleteActivity[0]['activity_id.parent_id'],
'record_type_id' => 'Activity Targets',
'return' => 'contact_id',
]);
try {
$contactID = civicrm_api3('ActivityContact', 'getvalue', [
'activity_id' => $deleteActivity[0]['activity_id.parent_id'],
'record_type_id' => 'Activity Targets',
'return' => 'contact_id',
]);
}
catch (\CiviCRM_API3_Exception $e) {
Civi::log()->debug('Unable to find original kept contact record');
}
}
return $contactID;
}
Expand Down
23 changes: 23 additions & 0 deletions tests/phpunit/api/v3/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4166,6 +4166,29 @@ public function testMergedGet() {
$this->assertEquals($mergedContactIds, array_keys($result));
}

/**
* Test retrieving merged contacts.
*
* The goal here is to start with a contact deleted by merged and find out the contact that is the current version of them.
*
* @throws \CRM_Core_Exception
*/
public function testMergedGetWithPermanentlyDeletedContact() {
$this->contactIDs[] = $this->individualCreate();
$this->contactIDs[] = $this->individualCreate();
$this->contactIDs[] = $this->individualCreate();
$this->contactIDs[] = $this->individualCreate();

// First do an 'unnatural merge' - they 'like to merge into the lowest but this will mean that contact 0 merged to contact [3].
// When the batch merge runs.... the new lowest contact is contact[1]. All contacts will merge into that contact,
// including contact[3], resulting in only 3 existing at the end. For each contact the correct answer to 'who did I eventually
// wind up being should be [1]
$this->callAPISuccess('Contact', 'merge', ['to_remove_id' => $this->contactIDs[0], 'to_keep_id' => $this->contactIDs[3]]);
$this->callAPISuccess('Contact', 'delete', ['id' => $this->contactIDs[3], 'skip_undelete' => TRUE]);
$result = $this->callAPISuccess('Contact', 'getmergedto', ['sequential' => 1, 'contact_id' => $this->contactIDs[0]]);
$this->assertEmpty($result['values']);
}

/**
* Test merging 2 contacts with delete to trash off.
*
Expand Down

0 comments on commit 9a2bf23

Please sign in to comment.