Skip to content

Commit

Permalink
dev/core#4156 Fix failure to update organization_name on employees du…
Browse files Browse the repository at this point in the history
…ring merge
  • Loading branch information
eileenmcnaughton committed Mar 10, 2023
1 parent c16f4e2 commit 1acc404
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CRM/Dedupe/Merger.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
+--------------------------------------------------------------------+
*/

use Civi\Api4\Contact;
use Civi\Api4\CustomGroup;

/**
Expand Down Expand Up @@ -685,7 +686,14 @@ protected static function updateContact(int $contactID, $params): void {
// This parameter causes blank fields to be be emptied out.
// We can probably remove.
$params['updateBlankLocInfo'] = TRUE;
if ($params['contact_type'] === 'Organization' && !isset($params['organization_name'])) {
// Ensuring this is set addresses https://lab.civicrm.org/dev/core/-/issues/4156
// but not that RM_Dedupe_MergerTest::testMergeWithEmployer covers this scenario
// so refactoring of this is safe.
$params['organization_name'] = Contact::get(FALSE)->addWhere('id', '=', $contactID)->addSelect('organization_name')->execute()->first()['organization_name'];
}
$data = self::formatProfileContactParams($params, $contactID);

CRM_Contact_BAO_Contact::create($data);
}

Expand Down
25 changes: 24 additions & 1 deletion tests/phpunit/CRM/Dedupe/MergerTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Civi\Api4\Contact;

/**
* Class CRM_Dedupe_DedupeMergerTest
*
Expand Down Expand Up @@ -1453,7 +1455,28 @@ public function testMergeWithDeclaredSearchJoin(): void {
CRM_Core_DAO_AllCoreTables::flush();
$contact1 = $this->individualCreate();
$contact2 = $this->individualCreate(['api.Im.create' => ['name' => 'chat_handle']]);
$this->callAPISuccess('Contact', 'merge', ['to_keep_id' => $contact1, 'to_remove_id' => $contact2]);
$this->callAPISuccess('Contact', 'merge', [
'to_keep_id' => $contact1,
'to_remove_id' => $contact2,
]);
}

/**
* Test that organization name is updated for employees of merged organizations..
*
* @throws \CRM_Core_Exception
*/
public function testMergeWithEmployer(): void {
$organizationToRemoveID = $this->organizationCreate(['organization_name' => 'remove']);
$organizationToKeepID = $this->organizationCreate(['organization_name' => 'keep']);
$individualID = $this->createContactWithEmployerRelationship([
'contact_id_b' => $organizationToRemoveID,
]);
$employerName = Contact::get()->addSelect('organization_name')->addWhere('id', '=', $individualID)->execute()->first()['organization_name'];
$this->assertEquals('remove', $employerName);
$this->callAPISuccess('Contact', 'merge', ['to_keep_id' => $organizationToKeepID, 'to_remove_id' => $organizationToRemoveID, 'mode' => 'aggressive']);
$employerName = Contact::get()->addSelect('organization_name')->addWhere('id', '=', $individualID)->execute()->first()['organization_name'];
$this->assertEquals('keep', $employerName);
}

/**
Expand Down

0 comments on commit 1acc404

Please sign in to comment.