More better fix on population on cached organization name
authorEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 13 Apr 2023 00:16:51 +0000 (12:16 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 13 Apr 2023 00:16:51 +0000 (12:16 +1200)
CRM/Contact/BAO/Contact/Utils.php
tests/phpunit/CRM/Dedupe/MergerTest.php

index d76ea928e66541825f6011794b02c6c864b6b3f5..49f652be1fa5966e54960e4786b7102c5c924a78 100644 (file)
@@ -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;
       }
index c821f75d183d362136efc3750f5a82645a3ae4b8..18e80a8262326b57599103b5af333b91fe39fb19 100644 (file)
@@ -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([