4 * Class CRM_Dedupe_DedupeMergerTest
7 class CRM_Dedupe_MergerTest
extends CiviUnitTestCase
{
10 protected $_contactIds = array();
12 public function createDupeContacts() {
13 // create a group to hold contacts, so that dupe checks don't consider any other contacts in the DB
15 'name' => 'Test Dupe Merger Group',
16 'title' => 'Test Dupe Merger Group',
19 'visibility' => 'Public Pages',
22 // TODO: This is not an API test!!
23 $result = civicrm_api('group', 'create', $params);
24 $this->_groupId
= $result['id'];
28 // make dupe checks based on based on following contact sets:
29 // FIRST - LAST - EMAIL
30 // ---------------------------------
31 // robin - hood - robin@example.com
32 // robin - hood - robin@example.com
33 // robin - hood - hood@example.com
34 // robin - dale - robin@example.com
35 // little - dale - dale@example.com
36 // little - dale - dale@example.com
37 // will - dale - dale@example.com
38 // will - dale - will@example.com
39 // will - dale - will@example.com
42 'first_name' => 'robin',
43 'last_name' => 'hood',
44 'email' => 'robin@example.com',
45 'contact_type' => 'Individual',
48 'first_name' => 'robin',
49 'last_name' => 'hood',
50 'email' => 'robin@example.com',
51 'contact_type' => 'Individual',
54 'first_name' => 'robin',
55 'last_name' => 'hood',
56 'email' => 'hood@example.com',
57 'contact_type' => 'Individual',
60 'first_name' => 'robin',
61 'last_name' => 'dale',
62 'email' => 'robin@example.com',
63 'contact_type' => 'Individual',
66 'first_name' => 'little',
67 'last_name' => 'dale',
68 'email' => 'dale@example.com',
69 'contact_type' => 'Individual',
72 'first_name' => 'little',
73 'last_name' => 'dale',
74 'email' => 'dale@example.com',
75 'contact_type' => 'Individual',
78 'first_name' => 'will',
79 'last_name' => 'dale',
80 'email' => 'dale@example.com',
81 'contact_type' => 'Individual',
84 'first_name' => 'will',
85 'last_name' => 'dale',
86 'email' => 'will@example.com',
87 'contact_type' => 'Individual',
90 'first_name' => 'will',
91 'last_name' => 'dale',
92 'email' => 'will@example.com',
93 'contact_type' => 'Individual',
98 foreach ($params as $param) {
99 $param['version'] = 3;
100 $contact = civicrm_api('contact', 'create', $param);
101 $this->_contactIds
[$count++
] = $contact['id'];
104 'contact_id' => $contact['id'],
105 'group_id' => $this->_groupId
,
108 $res = civicrm_api('group_contact', 'create', $grpParams);
112 public function deleteDupeContacts() {
113 // delete all created contacts
114 foreach ($this->_contactIds
as $contactId) {
115 Contact
::delete($contactId);
119 $params = array('id' => $this->_groupId
, 'version' => 3);
120 civicrm_api('group', 'delete', $params);
123 public function testBatchMergeSelectedDuplicates() {
124 $this->createDupeContacts();
126 // verify that all contacts have been created separately
127 $this->assertEquals(count($this->_contactIds
), 9, 'Check for number of contacts.');
129 $dao = new CRM_Dedupe_DAO_RuleGroup();
130 $dao->contact_type
= 'Individual';
131 $dao->name
= 'IndividualSupervised';
132 $dao->is_default
= 1;
135 $foundDupes = CRM_Dedupe_Finder
::dupesInGroup($dao->id
, $this->_groupId
);
137 // -------------------------------------------------------------------------
138 // Name and Email (reserved) Matches ( 3 pairs )
139 // --------------------------------------------------------------------------
140 // robin - hood - robin@example.com
141 // robin - hood - robin@example.com
142 // little - dale - dale@example.com
143 // little - dale - dale@example.com
144 // will - dale - will@example.com
145 // will - dale - will@example.com
146 // so 3 pairs for - first + last + mail
147 $this->assertEquals(count($foundDupes), 3, 'Check Individual-Supervised dupe rule for dupesInGroup().');
149 // Run dedupe finder as the browser would
150 $_SERVER['REQUEST_METHOD'] = 'GET'; //avoid invalid key error
151 $object = new CRM_Contact_Page_DedupeFind();
152 $object->set('gid', $this->_groupId
);
153 $object->set('rgid', $dao->id
);
154 $object->set('action', CRM_Core_Action
::UPDATE
);
157 // Retrieve pairs from prev next cache table
158 $select = array('pn.is_selected' => 'is_selected');
159 $cacheKeyString = "merge Individual_{$dao->id}_{$this->_groupId}";
160 $pnDupePairs = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
162 $this->assertEquals(count($foundDupes), count($pnDupePairs), 'Check number of dupe pairs in prev next cache.');
164 // mark first two pairs as selected
165 CRM_Core_DAO
::singleValueQuery("UPDATE civicrm_prevnext_cache SET is_selected = 1 WHERE id IN ({$pnDupePairs[0]['prevnext_id']}, {$pnDupePairs[1]['prevnext_id']})");
167 $pnDupePairs = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
168 $this->assertEquals($pnDupePairs[0]['is_selected'], 1, 'Check if first record in dupe pairs is marked as selected.');
169 $this->assertEquals($pnDupePairs[0]['is_selected'], 1, 'Check if second record in dupe pairs is marked as selected.');
171 // batch merge selected dupes
172 $result = CRM_Dedupe_Merger
::batchMerge($dao->id
, $this->_groupId
, 'safe', TRUE, 5, 1);
173 $this->assertEquals(count($result['merged']), 2, 'Check number of merged pairs.');
175 // retrieve pairs from prev next cache table
176 $pnDupePairs = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
177 $this->assertEquals(count($pnDupePairs), 1, 'Check number of remaining dupe pairs in prev next cache.');
179 $this->deleteDupeContacts();
182 public function testBatchMergeAllDuplicates() {
183 $this->createDupeContacts();
185 // verify that all contacts have been created separately
186 $this->assertEquals(count($this->_contactIds
), 9, 'Check for number of contacts.');
188 $dao = new CRM_Dedupe_DAO_RuleGroup();
189 $dao->contact_type
= 'Individual';
190 $dao->name
= 'IndividualSupervised';
191 $dao->is_default
= 1;
194 $foundDupes = CRM_Dedupe_Finder
::dupesInGroup($dao->id
, $this->_groupId
);
196 // -------------------------------------------------------------------------
197 // Name and Email (reserved) Matches ( 3 pairs )
198 // --------------------------------------------------------------------------
199 // robin - hood - robin@example.com
200 // robin - hood - robin@example.com
201 // little - dale - dale@example.com
202 // little - dale - dale@example.com
203 // will - dale - will@example.com
204 // will - dale - will@example.com
205 // so 3 pairs for - first + last + mail
206 $this->assertEquals(count($foundDupes), 3, 'Check Individual-Supervised dupe rule for dupesInGroup().');
208 // Run dedupe finder as the browser would
209 $_SERVER['REQUEST_METHOD'] = 'GET'; //avoid invalid key error
210 $object = new CRM_Contact_Page_DedupeFind();
211 $object->set('gid', $this->_groupId
);
212 $object->set('rgid', $dao->id
);
213 $object->set('action', CRM_Core_Action
::UPDATE
);
216 // Retrieve pairs from prev next cache table
217 $select = array('pn.is_selected' => 'is_selected');
218 $cacheKeyString = "merge Individual_{$dao->id}_{$this->_groupId}";
219 $pnDupePairs = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
221 $this->assertEquals(count($foundDupes), count($pnDupePairs), 'Check number of dupe pairs in prev next cache.');
223 // batch merge all dupes
224 $result = CRM_Dedupe_Merger
::batchMerge($dao->id
, $this->_groupId
, 'safe', TRUE, 5, 2);
225 $this->assertEquals(count($result['merged']), 3, 'Check number of merged pairs.');
227 // retrieve pairs from prev next cache table
228 $pnDupePairs = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
229 $this->assertEquals(count($pnDupePairs), 0, 'Check number of remaining dupe pairs in prev next cache.');
231 $this->deleteDupeContacts();
235 * The goal of this function is to test that all required tables are returned.
237 public function testGetCidRefs() {
238 $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'Contacts');
239 $this->assertEquals(array_merge($this->getStaticCIDRefs(), $this->getHackedInCIDRef()), CRM_Dedupe_Merger
::cidRefs());
240 $this->assertEquals(array_merge($this->getCalculatedCIDRefs(), $this->getHackedInCIDRef()), CRM_Dedupe_Merger
::cidRefs());
244 * Get the list of not-really-cid-refs that are currently hacked in.
246 * This is hacked into getCIDs function.
250 public function getHackedInCIDRef() {
252 'civicrm_entity_tag' => array(
259 * Get the list of tables that refer to the CID.
261 * This is a statically maintained (in this test list).
263 * There is also a check against an automated list but having both seems to add extra stability to me. They do
266 public function getStaticCIDRefs() {
268 'civicrm_acl_cache' => array(
271 'civicrm_acl_contact_cache' => array(
275 'civicrm_action_log' => array(
278 'civicrm_activity_contact' => array(
281 'civicrm_address' => array(
284 'civicrm_batch' => array(
288 'civicrm_campaign' => array(
290 1 => 'last_modified_id',
292 'civicrm_case_contact' => array(
295 'civicrm_contact' => array(
296 0 => 'primary_contact_id',
299 'civicrm_contribution' => array(
302 'civicrm_contribution_page' => array(
305 'civicrm_contribution_recur' => array(
308 'civicrm_contribution_soft' => array(
311 'civicrm_custom_group' => array(
314 'civicrm_dashboard_contact' => array(
317 'civicrm_dedupe_exception' => array(
321 'civicrm_domain' => array(
324 'civicrm_email' => array(
327 'civicrm_event' => array(
330 'civicrm_event_carts' => array(
333 'civicrm_financial_account' => array(
336 'civicrm_financial_item' => array(
339 'civicrm_grant' => array(
342 'civicrm_group' => array(
346 'civicrm_group_contact' => array(
349 'civicrm_group_contact_cache' => array(
352 'civicrm_group_organization' => array(
353 0 => 'organization_id',
355 'civicrm_im' => array(
358 'civicrm_log' => array(
361 'civicrm_mailing' => array(
366 'civicrm_mailing_abtest' => array(
369 'civicrm_mailing_event_queue' => array(
372 'civicrm_mailing_event_subscribe' => array(
375 'civicrm_mailing_recipients' => array(
378 'civicrm_membership' => array(
381 'civicrm_membership_log' => array(
384 'civicrm_membership_type' => array(
385 0 => 'member_of_contact_id',
387 'civicrm_note' => array(
390 'civicrm_openid' => array(
393 'civicrm_participant' => array(
395 1 => 'transferred_to_contact_id', //CRM-16761
397 'civicrm_payment_token' => array(
401 'civicrm_pcp' => array(
404 'civicrm_phone' => array(
407 'civicrm_pledge' => array(
410 'civicrm_print_label' => array(
413 'civicrm_relationship' => array(
417 'civicrm_report_instance' => array(
421 'civicrm_setting' => array(
425 'civicrm_subscription_history' => array(
428 'civicrm_survey' => array(
430 1 => 'last_modified_id',
432 'civicrm_tag' => array(
435 'civicrm_uf_group' => array(
438 'civicrm_uf_match' => array(
441 'civicrm_value_testgetcidref_1' => array(
444 'civicrm_website' => array(
451 * Get a list of CIDs that is calculated off the schema.
453 * Note this is an expensive and table locking query. Should be safe in tests though.
455 public function getCalculatedCIDRefs() {
461 FROM information_schema.key_column_usage
463 referenced_table_schema = database() AND
464 referenced_table_name = 'civicrm_contact' AND
465 referenced_column_name = 'id';
467 $dao = CRM_Core_DAO
::executeQuery($sql);
468 while ($dao->fetch()) {
469 $cidRefs[$dao->table_name
][] = $dao->column_name
;
471 // Do specific re-ordering changes to make this the same as the ref validated one.
472 // The above query orders by FK alphabetically.
473 // There might be cleverer ways to do this but it shouldn't change much.
474 $cidRefs['civicrm_contact'][0] = 'primary_contact_id';
475 $cidRefs['civicrm_contact'][1] = 'employer_id';
476 $cidRefs['civicrm_acl_contact_cache'][0] = 'user_id';
477 $cidRefs['civicrm_acl_contact_cache'][1] = 'contact_id';
478 $cidRefs['civicrm_mailing'][0] = 'created_id';
479 $cidRefs['civicrm_mailing'][1] = 'scheduled_id';
480 $cidRefs['civicrm_mailing'][2] = 'approver_id';