From: Eileen McNaughton Date: Tue, 5 Dec 2023 06:25:39 +0000 (+1300) Subject: dev/core#4447 fix inability to import to master_id X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=58d4e726b6354378c5c6a528efcdc9a0664469c3;p=civicrm-core.git dev/core#4447 fix inability to import to master_id --- diff --git a/CRM/Core/BAO/Address.php b/CRM/Core/BAO/Address.php index d73e543dc8..1489b5ca67 100644 --- a/CRM/Core/BAO/Address.php +++ b/CRM/Core/BAO/Address.php @@ -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; + } } /** diff --git a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php index 44c49dd8b0..c77a995285 100644 --- a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php +++ b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php @@ -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.