CRM-18106 create activity against contact deleted by merge
authoreileen <emcnaughton@wikimedia.org>
Thu, 3 Mar 2016 00:59:10 +0000 (13:59 +1300)
committereileen <emcnaughton@wikimedia.org>
Wed, 9 Mar 2016 07:17:10 +0000 (20:17 +1300)
CRM/Dedupe/Merger.php
tests/phpunit/api/v3/ContactTest.php

index d2e0b6f72e760b608cfcf5e13956285a05f2e814..de2e18dca1bc24fa951e5e1f591ec05c258f7567 100644 (file)
@@ -1832,27 +1832,19 @@ INNER JOIN  civicrm_membership membership2 ON membership1.membership_type_id = m
       }
 
       CRM_Contact_BAO_Contact::createProfileContact($submitted, CRM_Core_DAO::$_nullArray, $mainId);
-      unset($submitted);
     }
 
     CRM_Utils_Hook::post('merge', 'Contact', $mainId, CRM_Core_DAO::$_nullObject);
-
-    // Create activity for merge.
-    $messageActivity = ts('Contact ID %1 has been merged and deleted.', array(1 => $otherId));
-    civicrm_api3('activity', 'create', array(
-      'subject' => $messageActivity,
-      'source_contact_id' => CRM_Core_Session::singleton()->getLoggedInContactID(),
-      'target_contact_id' => $mainId,
-      'activity_type_id' => 'Contact Merged',
-      'status_id' => 'Completed',
-    ));
+    self::createMergeActivities($mainId, $otherId);
 
     return TRUE;
   }
 
   /**
+   * Get fields in the contact table suitable for merging.
+   *
    * @return array
-   *   Array of field names which will be compared, so everything except ID.
+   *   Array of field names to be potentially merged.
    */
   public static function getContactFields() {
     $contactFields = CRM_Contact_DAO_Contact::fields();
@@ -1929,4 +1921,32 @@ INNER JOIN  civicrm_membership membership2 ON membership1.membership_type_id = m
     }
   }
 
+  /**
+   * Create activities tracking the merge on affected contacts.
+   *
+   * @param int $mainId
+   * @param int $otherId
+   *
+   * @throws \CiviCRM_API3_Exception
+   */
+  public static function createMergeActivities($mainId, $otherId) {
+    $params = array(
+      1 => $otherId,
+      2 => $mainId,
+    );
+    $activity = civicrm_api3('activity', 'create', array(
+      'subject' => ts('Contact ID %1 has been merged and deleted.', $params),
+      'target_contact_id' => $mainId,
+      'activity_type_id' => 'Contact Merged',
+      'status_id' => 'Completed',
+    ));
+    civicrm_api3('activity', 'create', array(
+      'subject' => ts('Contact ID %1 has been merged into Contact ID %2 and deleted.', $params),
+      'target_contact_id' => $otherId,
+      'activity_type_id' => 'Contact Deleted by Merge',
+      'parent_id' => $activity['id'],
+      'status_id' => 'Completed',
+    ));
+  }
+
 }
index 4e9599f02256de5ab2b2dccd186205b7749789dd..21bf13ed4c2e70365ea3019a26ee0ca8b4a06e96 100644 (file)
@@ -2872,6 +2872,16 @@ class api_v3_ContactTest extends CiviUnitTestCase {
       'activity_type_id' => 'Contact Merged',
     ));
     $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($activity['activity_date_time'])));
+    $activity2 = $this->callAPISuccess('Activity', 'getsingle', array(
+      'target_contact_id' => $otherContact['id'],
+      'activity_type_id' => 'Contact Deleted by Merge',
+    ));
+    $this->assertEquals($activity['id'], $activity2['parent_id']);
+    $this->assertEquals('Normal', civicrm_api3('option_value', 'getvalue', array(
+      'value' => $activity['priority_id'],
+      'return' => 'label',
+      'option_group_id' => 'priority',
+    )));
 
   }