From de7b2b20fbe071709bbfc8f4a8cdfa87a32eb1b1 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 12 Apr 2021 09:36:30 +1200 Subject: [PATCH] Alter dedupe code to call api rather than bao->save() This achieves 2 things 1) hooks are no longer bypassed 2) it leverages core handling for is_primary rather than this somewhat unreliable attempt --- CRM/Dedupe/MergeHandler.php | 19 +++++++++++-------- tests/phpunit/api/v3/ContactTest.php | 2 +- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/CRM/Dedupe/MergeHandler.php b/CRM/Dedupe/MergeHandler.php index 5c40923ed9..b6a58b886a 100644 --- a/CRM/Dedupe/MergeHandler.php +++ b/CRM/Dedupe/MergeHandler.php @@ -377,6 +377,7 @@ class CRM_Dedupe_MergeHandler { * The use of the new hook is tested, including the fact it is called before contributions are merged, as this * is likely to be significant data in merge hooks. * + * @throws \API_Exception * @throws \CRM_Core_Exception */ public function mergeLocations(): void { @@ -410,12 +411,6 @@ class CRM_Dedupe_MergeHandler { $set_primary = $migrationInfo['location_blocks'][$name][$blkCount]['set_other_primary'] ?? NULL; if (!$changePrimary && $set_primary == "1") { $otherBlockDAO->is_primary = 1; - if ($primaryDAOId) { - $removePrimaryDAO = $this->getDAOForLocationEntity($name); - $removePrimaryDAO->id = $primaryDAOId; - $removePrimaryDAO->is_primary = 0; - $blocksDAO[$name]['update'][$primaryDAOId] = $removePrimaryDAO; - } $changePrimary = TRUE; } // Otherwise, if main contact already has primary, set it to 0. @@ -454,12 +449,20 @@ class CRM_Dedupe_MergeHandler { foreach ($blocksDAO as $blockDAOs) { if (!empty($blockDAOs['update'])) { foreach ($blockDAOs['update'] as $blockDAO) { - $blockDAO->save(); + $entity = CRM_Core_DAO_AllCoreTables::getBriefName(get_class($blockDAO)); + $values = ['checkPermissions' => FALSE]; + foreach ($blockDAO->fields() as $field) { + if (isset($blockDAO->{$field['name']})) { + $values['values'][$field['name']] = $blockDAO->{$field['name']}; + } + } + civicrm_api4($entity, 'update', $values); } } if (!empty($blockDAOs['delete'])) { foreach ($blockDAOs['delete'] as $blockDAO) { - $blockDAO->delete(); + $entity = CRM_Core_DAO_AllCoreTables::getBriefName(get_class($blockDAO)); + civicrm_api4($entity, 'delete', ['where' => [['id', '=', $blockDAO->id]], 'checkPermissions' => FALSE]); } } } diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index 43942a9342..3c6f8f3cf9 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -4040,7 +4040,7 @@ class api_v3_ContactTest extends CiviUnitTestCase { * * @throws \CRM_Core_Exception */ - public function testMergeWithBlankLocationData($isReverse) { + public function testMergeWithBlankLocationData($isReverse): void { $this->createLoggedInUser(); $this->ids['contact'][0] = $this->callAPISuccess('contact', 'create', $this->_params)['id']; $this->ids['contact'][1] = $this->callAPISuccess('contact', 'create', $this->_params)['id']; -- 2.25.1