From e0e7dfdf7d2ec76d181cf156044ddab80ba93065 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 13 Apr 2023 12:16:51 +1200 Subject: [PATCH] More better fix on population on cached organization name --- CRM/Contact/BAO/Contact/Utils.php | 5 ++++- tests/phpunit/CRM/Dedupe/MergerTest.php | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/CRM/Contact/BAO/Contact/Utils.php b/CRM/Contact/BAO/Contact/Utils.php index d76ea928e6..49f652be1f 100644 --- a/CRM/Contact/BAO/Contact/Utils.php +++ b/CRM/Contact/BAO/Contact/Utils.php @@ -284,13 +284,16 @@ WHERE id IN ( $idString ) ['relationship_type_id', '=', $relationshipTypeID], ['is_active', 'IN', [0, 1]], ]) - ->setSelect(['id', 'is_active', 'start_date', 'end_date', 'contact_id_a.employer_id']) + ->setSelect(['id', 'is_active', 'start_date', 'end_date', 'contact_id_a.employer_id', 'contact_id_a.organization_name', 'contact_id_b.organization_name']) ->addOrderBy('is_active', 'DESC') ->setLimit(1) ->execute()->first(); if (!empty($existingRelationship)) { if ($existingRelationship['is_active']) { + if ($existingRelationship['contact_id_a.organization_name'] !== $existingRelationship['contact_id_b.organization_name']){ + self::setCurrentEmployer([$contactID => $employerID]); + } // My work here is done. return; } diff --git a/tests/phpunit/CRM/Dedupe/MergerTest.php b/tests/phpunit/CRM/Dedupe/MergerTest.php index c821f75d18..18e80a8262 100644 --- a/tests/phpunit/CRM/Dedupe/MergerTest.php +++ b/tests/phpunit/CRM/Dedupe/MergerTest.php @@ -1467,6 +1467,27 @@ WHERE * @throws \CRM_Core_Exception */ public function testMergeWithEmployer(): void { + $organizationToRemoveID = $this->organizationCreate(['organization_name' => 'remove']); + $organizationToKeepID = $this->organizationCreate(['organization_name' => 'keep']); + $individualToKeepID = $this->createContactWithEmployerRelationship([ + 'contact_id_b' => $organizationToRemoveID, + ]); + $individualToRemoveID = $this->createContactWithEmployerRelationship([ + 'contact_id_b' => $organizationToKeepID, + ]); + $employerName = Contact::get()->addSelect('organization_name')->addWhere('id', '=', $individualToKeepID)->execute()->first()['organization_name']; + $this->assertEquals('remove', $employerName); + $this->mergeContacts($individualToKeepID, $individualToRemoveID, ['move_employer_id' => $organizationToKeepID, 'move_rel_table_relationships' => TRUE]); + $employerName = Contact::get()->addSelect('organization_name')->addWhere('id', '=', $individualToKeepID)->execute()->first()['organization_name']; + $this->assertEquals('keep', $employerName); + } + + /** + * Test that organization name is updated for employees of merged organizations.. + * + * @throws \CRM_Core_Exception + */ + public function testMergeWithEmployee(): void { $organizationToRemoveID = $this->organizationCreate(['organization_name' => 'remove']); $organizationToKeepID = $this->organizationCreate(['organization_name' => 'keep']); $individualID = $this->createContactWithEmployerRelationship([ -- 2.25.1