From 820e19742b18f62317f3fe2b9bdb7785b0447f90 Mon Sep 17 00:00:00 2001 From: Samuel Vanhove Date: Thu, 11 Jun 2020 14:50:36 -0400 Subject: [PATCH] dev/core#1670 copy custom fields from master to shared address --- CRM/Core/BAO/Address.php | 20 +++++++-- tests/phpunit/CRM/Core/BAO/AddressTest.php | 51 ++++++++++++++++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/CRM/Core/BAO/Address.php b/CRM/Core/BAO/Address.php index af3dbd233d..1b59159cbc 100644 --- a/CRM/Core/BAO/Address.php +++ b/CRM/Core/BAO/Address.php @@ -157,9 +157,22 @@ class CRM_Core_BAO_Address extends CRM_Core_DAO_Address { self::fixSharedAddress($params); } - $address->copyValues($params); - - $address->save(); + if (empty($params['id']) && isset($params['master_id']) && !CRM_Utils_System::isNull($params['master_id'])) { + // copy from master to ensure we have custom fields + // but keep all params data that might have been updated above + $fieldsFix = [ + 'replace' => $params, + ]; + $address = CRM_Core_DAO::copyGeneric('CRM_Core_DAO_Address', [ + 'id' => $params['master_id']], + NULL, + $fieldsFix + ); + } + else { + $address->copyValues($params); + $address->save(); + } if ($address->id) { if (isset($params['custom'])) { @@ -1067,6 +1080,7 @@ SELECT is_primary, $addressDAO->copyValues($params); $addressDAO->id = $dao->id; $addressDAO->save(); + $addressDAO->copyCustomFields($addressId, $addressDAO->id); } } diff --git a/tests/phpunit/CRM/Core/BAO/AddressTest.php b/tests/phpunit/CRM/Core/BAO/AddressTest.php index 110a0a5f40..d6793ac99d 100644 --- a/tests/phpunit/CRM/Core/BAO/AddressTest.php +++ b/tests/phpunit/CRM/Core/BAO/AddressTest.php @@ -31,6 +31,8 @@ */ class CRM_Core_BAO_AddressTest extends CiviUnitTestCase { + use CRMTraits_Custom_CustomDataTrait; + public function setUp() { parent::setUp(); @@ -603,4 +605,53 @@ class CRM_Core_BAO_AddressTest extends CiviUnitTestCase { $this->assertEmpty($updatedAddressA->master_id); } + /** + * dev/core#1670 - Ensure that the custom fields on adresses are copied + * to inherited address + * 1. test the creation of the shared address with custom field + * 2. test the update of the custom field in the master + */ + public function testSharedAddressCustomField() { + + $this->createCustomGroupWithFieldOfType(['extends' => 'Address'], 'text'); + $customField = $this->getCustomFieldName('text'); + + $contactIdA = $this->individualCreate([], 0); + $contactIdB = $this->individualCreate([], 1); + + $addressParamsA = [ + 'street_address' => '123 Fake St.', + 'location_type_id' => '1', + 'is_primary' => '1', + 'contact_id' => $contactIdA, + $customField => 'this is a custom text field', + ]; + + $addAddressA = CRM_Core_BAO_Address::add($addressParamsA, FALSE); + + // without having the custom field, we should still copy the values from master + $addressParamsB = [ + 'street_address' => '123 Fake St.', + 'location_type_id' => '1', + 'is_primary' => '1', + 'master_id' => $addAddressA->id, + 'contact_id' => $contactIdB, + ]; + $addAddressB = CRM_Core_BAO_Address::add($addressParamsB, FALSE); + + // 1. check if the custom fields values have been copied from master to shared address + $address = $this->callAPISuccessGetSingle('Address', ['id' => $addAddressB->id, 'return' => $this->getCustomFieldName('text')]); + $this->assertEquals($addressParamsA[$customField], $address[$customField]); + + // 2. now, we update addressA custom field to see if it goes into addressB + $addressParamsA['id'] = $addAddressA->id; + $addressParamsA[$customField] = 'updated custom text field'; + $addAddressA = CRM_Core_BAO_Address::add($addressParamsA, FALSE); + + $address = $this->callAPISuccessGetSingle('Address', ['id' => $addAddressB->id, 'return' => $this->getCustomFieldName('text')]); + $this->assertEquals($addressParamsA[$customField], $address[$customField]); + + } + + } -- 2.25.1