Alter dedupe code to call api rather than bao->save()
authoreileen <emcnaughton@wikimedia.org>
Sun, 11 Apr 2021 21:36:30 +0000 (09:36 +1200)
committereileen <emcnaughton@wikimedia.org>
Mon, 12 Apr 2021 23:00:09 +0000 (11:00 +1200)
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
tests/phpunit/api/v3/ContactTest.php

index 5c40923ed979d5ac8479c6d3a98c09e02a2c4ec3..b6a58b886a70e0fb1ec5883e832819fb0ede7e5a 100644 (file)
@@ -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]);
         }
       }
     }
index 43942a9342527cb82fa9a5a63beea6edddfae4e1..3c6f8f3cf967496de656af2df580d57bd63e5fa1 100644 (file)
@@ -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'];