4 * Class CRM_Dedupe_DedupeMergerTest
7 class CRM_Dedupe_MergerTest
extends CiviUnitTestCase
{
10 protected $_contactIds = array();
12 public function tearDown() {
13 $this->quickCleanup(array('civicrm_contact', 'civicrm_group_contact', 'civicrm_group'));
17 public function createDupeContacts() {
18 // create a group to hold contacts, so that dupe checks don't consider any other contacts in the DB
20 'name' => 'Test Dupe Merger Group',
21 'title' => 'Test Dupe Merger Group',
24 'visibility' => 'Public Pages',
27 // TODO: This is not an API test!!
28 $result = civicrm_api('group', 'create', $params);
29 $this->_groupId
= $result['id'];
33 // make dupe checks based on based on following contact sets:
34 // FIRST - LAST - EMAIL
35 // ---------------------------------
36 // robin - hood - robin@example.com
37 // robin - hood - robin@example.com
38 // robin - hood - hood@example.com
39 // robin - dale - robin@example.com
40 // little - dale - dale@example.com
41 // little - dale - dale@example.com
42 // will - dale - dale@example.com
43 // will - dale - will@example.com
44 // will - dale - will@example.com
47 'first_name' => 'robin',
48 'last_name' => 'hood',
49 'email' => 'robin@example.com',
50 'contact_type' => 'Individual',
53 'first_name' => 'robin',
54 'last_name' => 'hood',
55 'email' => 'robin@example.com',
56 'contact_type' => 'Individual',
59 'first_name' => 'robin',
60 'last_name' => 'hood',
61 'email' => 'hood@example.com',
62 'contact_type' => 'Individual',
65 'first_name' => 'robin',
66 'last_name' => 'dale',
67 'email' => 'robin@example.com',
68 'contact_type' => 'Individual',
71 'first_name' => 'little',
72 'last_name' => 'dale',
73 'email' => 'dale@example.com',
74 'contact_type' => 'Individual',
77 'first_name' => 'little',
78 'last_name' => 'dale',
79 'email' => 'dale@example.com',
80 'contact_type' => 'Individual',
83 'first_name' => 'will',
84 'last_name' => 'dale',
85 'email' => 'dale@example.com',
86 'contact_type' => 'Individual',
89 'first_name' => 'will',
90 'last_name' => 'dale',
91 'email' => 'will@example.com',
92 'contact_type' => 'Individual',
95 'first_name' => 'will',
96 'last_name' => 'dale',
97 'email' => 'will@example.com',
98 'contact_type' => 'Individual',
103 foreach ($params as $param) {
104 $param['version'] = 3;
105 $contact = civicrm_api('contact', 'create', $param);
106 $this->_contactIds
[$count++
] = $contact['id'];
109 'contact_id' => $contact['id'],
110 'group_id' => $this->_groupId
,
113 $res = civicrm_api('group_contact', 'create', $grpParams);
118 * Delete all created contacts.
120 public function deleteDupeContacts() {
121 foreach ($this->_contactIds
as $contactId) {
122 $this->contactDelete($contactId);
126 $params = array('id' => $this->_groupId
, 'version' => 3);
127 civicrm_api('group', 'delete', $params);
131 * Test the batch merge.
133 public function testBatchMergeSelectedDuplicates() {
134 $this->createDupeContacts();
136 // verify that all contacts have been created separately
137 $this->assertEquals(count($this->_contactIds
), 9, 'Check for number of contacts.');
139 $dao = new CRM_Dedupe_DAO_RuleGroup();
140 $dao->contact_type
= 'Individual';
141 $dao->name
= 'IndividualSupervised';
142 $dao->is_default
= 1;
145 $foundDupes = CRM_Dedupe_Finder
::dupesInGroup($dao->id
, $this->_groupId
);
147 // -------------------------------------------------------------------------
148 // Name and Email (reserved) Matches ( 3 pairs )
149 // --------------------------------------------------------------------------
150 // robin - hood - robin@example.com
151 // robin - hood - robin@example.com
152 // little - dale - dale@example.com
153 // little - dale - dale@example.com
154 // will - dale - will@example.com
155 // will - dale - will@example.com
156 // so 3 pairs for - first + last + mail
157 $this->assertEquals(count($foundDupes), 3, 'Check Individual-Supervised dupe rule for dupesInGroup().');
159 // Run dedupe finder as the browser would
160 $_SERVER['REQUEST_METHOD'] = 'GET'; //avoid invalid key error
161 $object = new CRM_Contact_Page_DedupeFind();
162 $object->set('gid', $this->_groupId
);
163 $object->set('rgid', $dao->id
);
164 $object->set('action', CRM_Core_Action
::UPDATE
);
165 $object->setEmbedded(TRUE);
168 // Retrieve pairs from prev next cache table
169 $select = array('pn.is_selected' => 'is_selected');
170 $cacheKeyString = "merge Individual_{$dao->id}_{$this->_groupId}";
171 $pnDupePairs = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
173 $this->assertEquals(count($foundDupes), count($pnDupePairs), 'Check number of dupe pairs in prev next cache.');
175 // mark first two pairs as selected
176 CRM_Core_DAO
::singleValueQuery("UPDATE civicrm_prevnext_cache SET is_selected = 1 WHERE id IN ({$pnDupePairs[0]['prevnext_id']}, {$pnDupePairs[1]['prevnext_id']})");
178 $pnDupePairs = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
179 $this->assertEquals($pnDupePairs[0]['is_selected'], 1, 'Check if first record in dupe pairs is marked as selected.');
180 $this->assertEquals($pnDupePairs[0]['is_selected'], 1, 'Check if second record in dupe pairs is marked as selected.');
182 // batch merge selected dupes
183 $result = CRM_Dedupe_Merger
::batchMerge($dao->id
, $this->_groupId
, 'safe', TRUE, 5, 1);
184 $this->assertEquals(count($result['merged']), 2, 'Check number of merged pairs.');
186 // retrieve pairs from prev next cache table
187 $pnDupePairs = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
188 $this->assertEquals(count($pnDupePairs), 1, 'Check number of remaining dupe pairs in prev next cache.');
190 $this->deleteDupeContacts();
194 * Test the batch merge.
196 public function testBatchMergeAllDuplicates() {
197 $this->createDupeContacts();
199 // verify that all contacts have been created separately
200 $this->assertEquals(count($this->_contactIds
), 9, 'Check for number of contacts.');
202 $dao = new CRM_Dedupe_DAO_RuleGroup();
203 $dao->contact_type
= 'Individual';
204 $dao->name
= 'IndividualSupervised';
205 $dao->is_default
= 1;
208 $foundDupes = CRM_Dedupe_Finder
::dupesInGroup($dao->id
, $this->_groupId
);
210 // -------------------------------------------------------------------------
211 // Name and Email (reserved) Matches ( 3 pairs )
212 // --------------------------------------------------------------------------
213 // robin - hood - robin@example.com
214 // robin - hood - robin@example.com
215 // little - dale - dale@example.com
216 // little - dale - dale@example.com
217 // will - dale - will@example.com
218 // will - dale - will@example.com
219 // so 3 pairs for - first + last + mail
220 $this->assertEquals(count($foundDupes), 3, 'Check Individual-Supervised dupe rule for dupesInGroup().');
222 // Run dedupe finder as the browser would
223 $_SERVER['REQUEST_METHOD'] = 'GET'; //avoid invalid key error
224 $object = new CRM_Contact_Page_DedupeFind();
225 $object->set('gid', $this->_groupId
);
226 $object->set('rgid', $dao->id
);
227 $object->set('action', CRM_Core_Action
::UPDATE
);
228 $object->setEmbedded(TRUE);
231 // Retrieve pairs from prev next cache table
232 $select = array('pn.is_selected' => 'is_selected');
233 $cacheKeyString = "merge Individual_{$dao->id}_{$this->_groupId}";
234 $pnDupePairs = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
236 $this->assertEquals(count($foundDupes), count($pnDupePairs), 'Check number of dupe pairs in prev next cache.');
238 // batch merge all dupes
239 $result = CRM_Dedupe_Merger
::batchMerge($dao->id
, $this->_groupId
, 'safe', TRUE, 5, 2);
240 $this->assertEquals(count($result['merged']), 3, 'Check number of merged pairs.');
242 // retrieve pairs from prev next cache table
243 $pnDupePairs = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
244 $this->assertEquals(count($pnDupePairs), 0, 'Check number of remaining dupe pairs in prev next cache.');
246 $this->deleteDupeContacts();
250 * The goal of this function is to test that all required tables are returned.
252 public function testGetCidRefs() {
253 $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'Contacts');
254 $this->assertEquals(array_merge($this->getStaticCIDRefs(), $this->getHackedInCIDRef()), CRM_Dedupe_Merger
::cidRefs());
255 $this->assertEquals(array_merge($this->getCalculatedCIDRefs(), $this->getHackedInCIDRef()), CRM_Dedupe_Merger
::cidRefs());
259 * Get the list of not-really-cid-refs that are currently hacked in.
261 * This is hacked into getCIDs function.
265 public function getHackedInCIDRef() {
267 'civicrm_entity_tag' => array(
274 * Test function that gets duplicate pairs.
276 * It turns out there are 2 code paths retrieving this data so my initial focus is on ensuring
279 public function testGetMatches() {
280 $this->setupMatchData();
281 $pairs = CRM_Dedupe_Merger
::getDuplicatePairs(
289 $this->assertEquals(array(
291 'srcID' => $this->contacts
[0]['id'],
292 'srcName' => 'Mr. Mickey Mouse II',
293 'dstID' => $this->contacts
[1]['id'],
294 'dstName' => 'Mr. Mickey Mouse II',
299 'srcID' => $this->contacts
[2]['id'],
300 'srcName' => 'Mr. Minnie Mouse II',
301 'dstID' => $this->contacts
[3]['id'],
302 'dstName' => 'Mr. Minnie Mouse II',
310 * Test function that gets duplicate pairs.
312 * It turns out there are 2 code paths retrieving this data so my initial focus is on ensuring
315 public function testGetMatchesInGroup() {
316 $this->setupMatchData();
318 $groupID = $this->groupCreate(array('title' => 'she-mice'));
320 $this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupID, 'contact_id' => $this->contacts
[3]['id']));
322 $pairs = CRM_Dedupe_Merger
::getDuplicatePairs(
330 $this->assertEquals(array(
332 'srcID' => $this->contacts
[3]['id'],
333 'srcName' => 'Mr. Minnie Mouse II',
334 'dstID' => $this->contacts
[2]['id'],
335 'dstName' => 'Mr. Minnie Mouse II',
343 * Set up some contacts for our matching.
345 public function setupMatchData() {
348 'first_name' => 'Mickey',
349 'last_name' => 'Mouse',
350 'email' => 'mickey@mouse.com',
353 'first_name' => 'Mickey',
354 'last_name' => 'Mouse',
355 'email' => 'mickey@mouse.com',
358 'first_name' => 'Minnie',
359 'last_name' => 'Mouse',
360 'email' => 'mickey@mouse.com',
363 'first_name' => 'Minnie',
364 'last_name' => 'Mouse',
365 'email' => 'mickey@mouse.com',
368 foreach ($fixtures as $fixture) {
369 $contactID = $this->individualCreate($fixture);
370 $this->contacts
[] = array_merge($fixture, array('id' => $contactID));
376 * Get the list of tables that refer to the CID.
378 * This is a statically maintained (in this test list).
380 * There is also a check against an automated list but having both seems to add extra stability to me. They do
383 public function getStaticCIDRefs() {
385 'civicrm_acl_cache' => array(
388 'civicrm_acl_contact_cache' => array(
392 'civicrm_action_log' => array(
395 'civicrm_activity_contact' => array(
398 'civicrm_address' => array(
401 'civicrm_batch' => array(
405 'civicrm_campaign' => array(
407 1 => 'last_modified_id',
409 'civicrm_case_contact' => array(
412 'civicrm_contact' => array(
413 0 => 'primary_contact_id',
416 'civicrm_contribution' => array(
419 'civicrm_contribution_page' => array(
422 'civicrm_contribution_recur' => array(
425 'civicrm_contribution_soft' => array(
428 'civicrm_custom_group' => array(
431 'civicrm_dashboard_contact' => array(
434 'civicrm_dedupe_exception' => array(
438 'civicrm_domain' => array(
441 'civicrm_email' => array(
444 'civicrm_event' => array(
447 'civicrm_event_carts' => array(
450 'civicrm_financial_account' => array(
453 'civicrm_financial_item' => array(
456 'civicrm_grant' => array(
459 'civicrm_group' => array(
463 'civicrm_group_contact' => array(
466 'civicrm_group_contact_cache' => array(
469 'civicrm_group_organization' => array(
470 0 => 'organization_id',
472 'civicrm_im' => array(
475 'civicrm_log' => array(
478 'civicrm_mailing' => array(
483 'civicrm_mailing_abtest' => array(
486 'civicrm_mailing_event_queue' => array(
489 'civicrm_mailing_event_subscribe' => array(
492 'civicrm_mailing_recipients' => array(
495 'civicrm_membership' => array(
498 'civicrm_membership_log' => array(
501 'civicrm_membership_type' => array(
502 0 => 'member_of_contact_id',
504 'civicrm_note' => array(
507 'civicrm_openid' => array(
510 'civicrm_participant' => array(
512 1 => 'transferred_to_contact_id', //CRM-16761
514 'civicrm_payment_token' => array(
518 'civicrm_pcp' => array(
521 'civicrm_phone' => array(
524 'civicrm_pledge' => array(
527 'civicrm_print_label' => array(
530 'civicrm_relationship' => array(
534 'civicrm_report_instance' => array(
538 'civicrm_setting' => array(
542 'civicrm_subscription_history' => array(
545 'civicrm_survey' => array(
547 1 => 'last_modified_id',
549 'civicrm_tag' => array(
552 'civicrm_uf_group' => array(
555 'civicrm_uf_match' => array(
558 'civicrm_value_testgetcidref_1' => array(
561 'civicrm_website' => array(
568 * Get a list of CIDs that is calculated off the schema.
570 * Note this is an expensive and table locking query. Should be safe in tests though.
572 public function getCalculatedCIDRefs() {
578 FROM information_schema.key_column_usage
580 referenced_table_schema = database() AND
581 referenced_table_name = 'civicrm_contact' AND
582 referenced_column_name = 'id';
584 $dao = CRM_Core_DAO
::executeQuery($sql);
585 while ($dao->fetch()) {
586 $cidRefs[$dao->table_name
][] = $dao->column_name
;
588 // Do specific re-ordering changes to make this the same as the ref validated one.
589 // The above query orders by FK alphabetically.
590 // There might be cleverer ways to do this but it shouldn't change much.
591 $cidRefs['civicrm_contact'][0] = 'primary_contact_id';
592 $cidRefs['civicrm_contact'][1] = 'employer_id';
593 $cidRefs['civicrm_acl_contact_cache'][0] = 'user_id';
594 $cidRefs['civicrm_acl_contact_cache'][1] = 'contact_id';
595 $cidRefs['civicrm_mailing'][0] = 'created_id';
596 $cidRefs['civicrm_mailing'][1] = 'scheduled_id';
597 $cidRefs['civicrm_mailing'][2] = 'approver_id';