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

Move exception capturing into the page layer and extend unit test to cover page function

Update status message as per Mikeý's comment
  • Loading branch information
seamuslee001 committed Sep 29, 2020
1 parent b596211 commit 50bdbb5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
8 changes: 7 additions & 1 deletion CRM/Contact/Page/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,13 @@ public static function setTitle($contactId, $isDeleted = FALSE) {
}
if ($isDeleted) {
$title = "<del>{$title}</del>";
$mergedTo = civicrm_api3('Contact', 'getmergedto', ['contact_id' => $contactId, 'api.Contact.get' => ['return' => 'display_name']]);
try {
$mergedTo = civicrm_api3('Contact', 'getmergedto', ['contact_id' => $contactId, 'api.Contact.get' => ['return' => 'display_name']]);
}
catch (CiviCRM_API3_Exception $e) {
CRM_Core_Session::singleton()->setStatus(ts('This contact was deleted during a merge operation. The contact it was merged into cannot be found and may have been deleted.'));
$mergedTo = ['count' => 0];
}
if ($mergedTo['count']) {
$mergedToContactID = $mergedTo['id'];
$mergedToDisplayName = $mergedTo['values'][$mergedToContactID]['api.Contact.get']['values'][0]['display_name'];
Expand Down
24 changes: 24 additions & 0 deletions tests/phpunit/api/v3/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4166,6 +4166,30 @@ 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]);
$this->callAPIFailure('Contact', 'getmergedto', ['sequential' => 1, 'contact_id' => $this->contactIDs[0]]);
$title = CRM_Contact_Page_View::setTitle($this->contactIDs[0], TRUE);
$this->assertContains('civicrm/profile/view&amp;reset=1&amp;gid=7&amp;id=3&amp;snippet=4', $title);
}

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

0 comments on commit 50bdbb5

Please sign in to comment.