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);
124 * Test the batch merge.
126 public function testBatchMergeSelectedDuplicates() {
127 $this->createDupeContacts();
129 // verify that all contacts have been created separately
130 $this->assertEquals(count($this->_contactIds
), 9, 'Check for number of contacts.');
132 $dao = new CRM_Dedupe_DAO_RuleGroup();
133 $dao->contact_type
= 'Individual';
134 $dao->name
= 'IndividualSupervised';
135 $dao->is_default
= 1;
138 $foundDupes = CRM_Dedupe_Finder
::dupesInGroup($dao->id
, $this->_groupId
);
140 // -------------------------------------------------------------------------
141 // Name and Email (reserved) Matches ( 3 pairs )
142 // --------------------------------------------------------------------------
143 // robin - hood - robin@example.com
144 // robin - hood - robin@example.com
145 // little - dale - dale@example.com
146 // little - dale - dale@example.com
147 // will - dale - will@example.com
148 // will - dale - will@example.com
149 // so 3 pairs for - first + last + mail
150 $this->assertEquals(count($foundDupes), 3, 'Check Individual-Supervised dupe rule for dupesInGroup().');
152 // Run dedupe finder as the browser would
153 $_SERVER['REQUEST_METHOD'] = 'GET'; //avoid invalid key error
154 $object = new CRM_Contact_Page_DedupeFind();
155 $object->set('gid', $this->_groupId
);
156 $object->set('rgid', $dao->id
);
157 $object->set('action', CRM_Core_Action
::UPDATE
);
160 // Retrieve pairs from prev next cache table
161 $select = array('pn.is_selected' => 'is_selected');
162 $cacheKeyString = "merge Individual_{$dao->id}_{$this->_groupId}";
163 $pnDupePairs = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
165 $this->assertEquals(count($foundDupes), count($pnDupePairs), 'Check number of dupe pairs in prev next cache.');
167 // mark first two pairs as selected
168 CRM_Core_DAO
::singleValueQuery("UPDATE civicrm_prevnext_cache SET is_selected = 1 WHERE id IN ({$pnDupePairs[0]['prevnext_id']}, {$pnDupePairs[1]['prevnext_id']})");
170 $pnDupePairs = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
171 $this->assertEquals($pnDupePairs[0]['is_selected'], 1, 'Check if first record in dupe pairs is marked as selected.');
172 $this->assertEquals($pnDupePairs[0]['is_selected'], 1, 'Check if second record in dupe pairs is marked as selected.');
174 // batch merge selected dupes
175 $result = CRM_Dedupe_Merger
::batchMerge($dao->id
, $this->_groupId
, 'safe', TRUE, 5, 1);
176 $this->assertEquals(count($result['merged']), 2, 'Check number of merged pairs.');
178 // retrieve pairs from prev next cache table
179 $pnDupePairs = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
180 $this->assertEquals(count($pnDupePairs), 1, 'Check number of remaining dupe pairs in prev next cache.');
182 $this->deleteDupeContacts();
186 * Test the batch merge.
188 public function testBatchMergeAllDuplicates() {
189 $this->createDupeContacts();
191 // verify that all contacts have been created separately
192 $this->assertEquals(count($this->_contactIds
), 9, 'Check for number of contacts.');
194 $dao = new CRM_Dedupe_DAO_RuleGroup();
195 $dao->contact_type
= 'Individual';
196 $dao->name
= 'IndividualSupervised';
197 $dao->is_default
= 1;
200 $foundDupes = CRM_Dedupe_Finder
::dupesInGroup($dao->id
, $this->_groupId
);
202 // -------------------------------------------------------------------------
203 // Name and Email (reserved) Matches ( 3 pairs )
204 // --------------------------------------------------------------------------
205 // robin - hood - robin@example.com
206 // robin - hood - robin@example.com
207 // little - dale - dale@example.com
208 // little - dale - dale@example.com
209 // will - dale - will@example.com
210 // will - dale - will@example.com
211 // so 3 pairs for - first + last + mail
212 $this->assertEquals(count($foundDupes), 3, 'Check Individual-Supervised dupe rule for dupesInGroup().');
214 // Run dedupe finder as the browser would
215 $_SERVER['REQUEST_METHOD'] = 'GET'; //avoid invalid key error
216 $object = new CRM_Contact_Page_DedupeFind();
217 $object->set('gid', $this->_groupId
);
218 $object->set('rgid', $dao->id
);
219 $object->set('action', CRM_Core_Action
::UPDATE
);
222 // Retrieve pairs from prev next cache table
223 $select = array('pn.is_selected' => 'is_selected');
224 $cacheKeyString = "merge Individual_{$dao->id}_{$this->_groupId}";
225 $pnDupePairs = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
227 $this->assertEquals(count($foundDupes), count($pnDupePairs), 'Check number of dupe pairs in prev next cache.');
229 // batch merge all dupes
230 $result = CRM_Dedupe_Merger
::batchMerge($dao->id
, $this->_groupId
, 'safe', TRUE, 5, 2);
231 $this->assertEquals(count($result['merged']), 3, 'Check number of merged pairs.');
233 // retrieve pairs from prev next cache table
234 $pnDupePairs = CRM_Core_BAO_PrevNextCache
::retrieve($cacheKeyString, NULL, NULL, 0, 0, $select);
235 $this->assertEquals(count($pnDupePairs), 0, 'Check number of remaining dupe pairs in prev next cache.');
237 $this->deleteDupeContacts();
241 * The goal of this function is to test that all required tables are returned.
243 public function testGetCidRefs() {
244 $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'Contacts');
245 $this->assertEquals(array_merge($this->getStaticCIDRefs(), $this->getHackedInCIDRef()), CRM_Dedupe_Merger
::cidRefs());
246 $this->assertEquals(array_merge($this->getCalculatedCIDRefs(), $this->getHackedInCIDRef()), CRM_Dedupe_Merger
::cidRefs());
250 * Get the list of not-really-cid-refs that are currently hacked in.
252 * This is hacked into getCIDs function.
256 public function getHackedInCIDRef() {
258 'civicrm_entity_tag' => array(
265 * Get the list of tables that refer to the CID.
267 * This is a statically maintained (in this test list).
269 * There is also a check against an automated list but having both seems to add extra stability to me. They do
272 public function getStaticCIDRefs() {
274 'civicrm_acl_cache' => array(
277 'civicrm_acl_contact_cache' => array(
281 'civicrm_action_log' => array(
284 'civicrm_activity_contact' => array(
287 'civicrm_address' => array(
290 'civicrm_batch' => array(
294 'civicrm_campaign' => array(
296 1 => 'last_modified_id',
298 'civicrm_case_contact' => array(
301 'civicrm_contact' => array(
302 0 => 'primary_contact_id',
305 'civicrm_contribution' => array(
308 'civicrm_contribution_page' => array(
311 'civicrm_contribution_recur' => array(
314 'civicrm_contribution_soft' => array(
317 'civicrm_custom_group' => array(
320 'civicrm_dashboard_contact' => array(
323 'civicrm_dedupe_exception' => array(
327 'civicrm_domain' => array(
330 'civicrm_email' => array(
333 'civicrm_event' => array(
336 'civicrm_event_carts' => array(
339 'civicrm_financial_account' => array(
342 'civicrm_financial_item' => array(
345 'civicrm_grant' => array(
348 'civicrm_group' => array(
352 'civicrm_group_contact' => array(
355 'civicrm_group_contact_cache' => array(
358 'civicrm_group_organization' => array(
359 0 => 'organization_id',
361 'civicrm_im' => array(
364 'civicrm_log' => array(
367 'civicrm_mailing' => array(
372 'civicrm_mailing_abtest' => array(
375 'civicrm_mailing_event_queue' => array(
378 'civicrm_mailing_event_subscribe' => array(
381 'civicrm_mailing_recipients' => array(
384 'civicrm_membership' => array(
387 'civicrm_membership_log' => array(
390 'civicrm_membership_type' => array(
391 0 => 'member_of_contact_id',
393 'civicrm_note' => array(
396 'civicrm_openid' => array(
399 'civicrm_participant' => array(
401 1 => 'transferred_to_contact_id', //CRM-16761
403 'civicrm_payment_token' => array(
407 'civicrm_pcp' => array(
410 'civicrm_phone' => array(
413 'civicrm_pledge' => array(
416 'civicrm_print_label' => array(
419 'civicrm_relationship' => array(
423 'civicrm_report_instance' => array(
427 'civicrm_setting' => array(
431 'civicrm_subscription_history' => array(
434 'civicrm_survey' => array(
436 1 => 'last_modified_id',
438 'civicrm_tag' => array(
441 'civicrm_uf_group' => array(
444 'civicrm_uf_match' => array(
447 'civicrm_value_testgetcidref_1' => array(
450 'civicrm_website' => array(
457 * Get a list of CIDs that is calculated off the schema.
459 * Note this is an expensive and table locking query. Should be safe in tests though.
461 public function getCalculatedCIDRefs() {
467 FROM information_schema.key_column_usage
469 referenced_table_schema = database() AND
470 referenced_table_name = 'civicrm_contact' AND
471 referenced_column_name = 'id';
473 $dao = CRM_Core_DAO
::executeQuery($sql);
474 while ($dao->fetch()) {
475 $cidRefs[$dao->table_name
][] = $dao->column_name
;
477 // Do specific re-ordering changes to make this the same as the ref validated one.
478 // The above query orders by FK alphabetically.
479 // There might be cleverer ways to do this but it shouldn't change much.
480 $cidRefs['civicrm_contact'][0] = 'primary_contact_id';
481 $cidRefs['civicrm_contact'][1] = 'employer_id';
482 $cidRefs['civicrm_acl_contact_cache'][0] = 'user_id';
483 $cidRefs['civicrm_acl_contact_cache'][1] = 'contact_id';
484 $cidRefs['civicrm_mailing'][0] = 'created_id';
485 $cidRefs['civicrm_mailing'][1] = 'scheduled_id';
486 $cidRefs['civicrm_mailing'][2] = 'approver_id';