dev/core#4447 fix inability to import to master_id
authorEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 5 Dec 2023 06:25:39 +0000 (19:25 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 6 Dec 2023 21:24:11 +0000 (10:24 +1300)
CRM/Core/BAO/Address.php
tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php

index d73e543dc89e3fc298151ee585bf2951d872d8a2..1489b5ca670f8a6dadaeac12f8e1b1a24c0030d4 100644 (file)
@@ -301,7 +301,6 @@ class CRM_Core_BAO_Address extends CRM_Core_DAO_Address implements Civi\Core\Hoo
         'contact_id',
         'is_billing',
         'display',
-        'master_id',
       ])) {
         continue;
       }
@@ -899,6 +898,8 @@ SELECT is_primary,
    * Fix the shared address if address is already shared
    * or if address will be shared with itself.
    *
+   * Add in the details from the master address.
+   *
    * @param array $params
    *   Associated array of address params.
    */
@@ -914,6 +915,12 @@ SELECT is_primary,
       $params['master_id'] = NULL;
       CRM_Core_Session::setStatus(ts("You can't connect an address to itself"), '', 'warning');
     }
+    if ($params['master_id']) {
+      $masterAddressParams = Address::get(FALSE)
+        ->addWhere('id', '=', $params['master_id'])->execute()->first();
+      unset($masterAddressParams['id'], $masterAddressParams['is_primary'], $masterAddressParams['is_billing'], $masterAddressParams['contact_id']);
+      $params += $masterAddressParams;
+    }
   }
 
   /**
index 44c49dd8b0b392d45ff9137c4af7f0a4842dc64b..c77a995285ebf31a7f853fe8e26a75f8083aa787 100644 (file)
@@ -309,6 +309,22 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase {
     $this->assertEquals(['Parent'], $contact['contact_sub_type']);
   }
 
+  /**
+   * @throws \CRM_Core_Exception
+   */
+  public function testImportMasterAddress(): void {
+    $this->individualCreate(['external_identifier' => 'billy', 'first_name' => 'William'], 'billy-the-kid');
+    $address = $this->callAPISuccess('Address', 'create', ['street_address' => 'out yonder', 'contact_id' => $this->ids['Contact']['billy-the-kid']]);
+    $this->individualCreate(['external_identifier' => '', 'first_name' => 'Daddy Bill'], 'billy-the-dad');
+    $this->runImport([
+      'id' => $this->ids['Contact']['billy-the-dad'],
+      'master_id' => $address['id'],
+    ], CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID);
+    $newAddress = $this->callAPISuccessGetSingle('Address', ['contact_id' => $this->ids['Contact']['billy-the-dad']]);
+    $this->assertEquals($address['id'], $newAddress['master_id']);
+    $this->assertEquals('out yonder', $newAddress['street_address']);
+  }
+
   /**
    * Test updating an existing contact with external_identifier match but
    * subtype mismatch.