2 require_once 'CiviTest/CiviUnitTestCase.php';
3 require_once 'CiviTest/Contact.php';
6 * Class CRM_Dedupe_DedupeFinderTest
8 class CRM_Dedupe_DedupeFinderTest
extends CiviUnitTestCase
{
9 function testFuzzyDupes() {
10 // make dupe checks based on based on following contact sets:
11 // FIRST - LAST - EMAIL
12 // ---------------------------------
13 // robin - hood - robin@example.com
14 // robin - hood - hood@example.com
15 // robin - dale - robin@example.com
16 // little - dale - dale@example.com
17 // will - dale - dale@example.com
18 // will - dale - will@example.com
19 // will - dale - will@example.com
21 // create a group to hold contacts, so that dupe checks don't consider any other contacts in the DB
23 'name' => 'Dupe Group',
24 'title' => 'New Test Dupe Group',
27 'visibility' => 'Public Pages',
30 // TODO: This is not an API test!!
31 $result = civicrm_api('group', 'create', $params);
32 $groupId = $result['id'];
35 // FIXME: move create params to separate function
38 'first_name' => 'robin',
39 'last_name' => 'hood',
40 'email' => 'robin@example.com',
41 'contact_type' => 'Individual',
44 'first_name' => 'robin',
45 'last_name' => 'hood',
46 'email' => 'hood@example.com',
47 'contact_type' => 'Individual',
50 'first_name' => 'robin',
51 'last_name' => 'dale',
52 'email' => 'robin@example.com',
53 'contact_type' => 'Individual',
56 'first_name' => 'little',
57 'last_name' => 'dale',
58 'email' => 'dale@example.com',
59 'contact_type' => 'Individual',
62 'first_name' => 'will',
63 'last_name' => 'dale',
64 'email' => 'dale@example.com',
65 'contact_type' => 'Individual',
68 'first_name' => 'will',
69 'last_name' => 'dale',
70 'email' => 'will@example.com',
71 'contact_type' => 'Individual',
74 'first_name' => 'will',
75 'last_name' => 'dale',
76 'email' => 'will@example.com',
77 'contact_type' => 'Individual',
82 // TODO: This is not an API test!!
83 foreach ($params as $param) {
84 $param['version'] = 3;
85 $contact = civicrm_api('contact', 'create', $param);
86 $contactIds[$count++
] = $contact['id'];
89 'contact_id' => $contact['id'],
90 'group_id' => $groupId,
93 $res = civicrm_api('group_contact', 'create', $grpParams);
96 // verify that all contacts have been created separately
97 $this->assertEquals(count($contactIds), 7, 'Check for number of contacts.');
99 $dao = new CRM_Dedupe_DAO_RuleGroup();
100 $dao->contact_type
= 'Individual';
101 $dao->level
= 'Fuzzy';
102 $dao->is_default
= 1;
105 $foundDupes = CRM_Dedupe_Finder
::dupesInGroup($dao->id
, $groupId);
107 // -------------------------------------------------------------------------
108 // default dedupe rule: threshold = 20 => (First + Last + Email) Matches ( 1 pair )
109 // --------------------------------------------------------------------------
110 // will - dale - will@example.com
111 // will - dale - will@example.com
112 // so 1 pair for - first + last + mail
113 $this->assertEquals(count($foundDupes), 1, 'Check Individual-Fuzzy dupe rule for dupesInGroup().');
115 // delete all created contacts
116 foreach ($contactIds as $contactId) {
117 Contact
::delete($contactId);
120 $params = array('id' => $groupId, 'version' => 3);
121 civicrm_api('group', 'delete', $params);
124 function testDupesByParams() {
125 // make dupe checks based on based on following contact sets:
126 // FIRST - LAST - EMAIL
127 // ---------------------------------
128 // robin - hood - robin@example.com
129 // robin - hood - hood@example.com
130 // robin - dale - robin@example.com
131 // little - dale - dale@example.com
132 // will - dale - dale@example.com
133 // will - dale - will@example.com
134 // will - dale - will@example.com
137 // FIXME: move create params to separate function
140 'first_name' => 'robin',
141 'last_name' => 'hood',
142 'email' => 'robin@example.com',
143 'contact_type' => 'Individual',
146 'first_name' => 'robin',
147 'last_name' => 'hood',
148 'email' => 'hood@example.com',
149 'contact_type' => 'Individual',
152 'first_name' => 'robin',
153 'last_name' => 'dale',
154 'email' => 'robin@example.com',
155 'contact_type' => 'Individual',
158 'first_name' => 'little',
159 'last_name' => 'dale',
160 'email' => 'dale@example.com',
161 'contact_type' => 'Individual',
164 'first_name' => 'will',
165 'last_name' => 'dale',
166 'email' => 'dale@example.com',
167 'contact_type' => 'Individual',
170 'first_name' => 'will',
171 'last_name' => 'dale',
172 'email' => 'will@example.com',
173 'contact_type' => 'Individual',
176 'first_name' => 'will',
177 'last_name' => 'dale',
178 'email' => 'will@example.com',
179 'contact_type' => 'Individual',
184 // TODO: This is not an API test!!
185 foreach ($params as $param) {
186 $param['version'] = 3;
187 $contact = civicrm_api('contact', 'create', $param);
189 'contact_id' => $contact['id'],
190 'street_address' => 'Ambachtstraat 23',
191 'location_type_id' => 1,
194 $result = civicrm_api( 'address','create',$params );
195 $contactIds[$count++
] = $contact['id'];
198 // verify that all contacts have been created separately
199 $this->assertEquals(count($contactIds), 7, 'Check for number of contacts.');
201 $dao = new CRM_Dedupe_DAO_RuleGroup();
202 $dao->contact_type
= 'Individual';
203 $dao->used
= 'General';
204 $dao->is_default
= 1;
208 'first_name' => 'robin',
209 'last_name' => 'hood',
210 'email' => 'hood@example.com',
211 'street_address' => 'Ambachtstraat 23',
213 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
214 $dedupeParams = CRM_Dedupe_Finder
::formatParams($fields, 'Individual');
215 $ids = CRM_Dedupe_Finder
::dupesByParams($dedupeParams, 'Individual', 'General');
217 // Check with default Individual-General rule
218 $this->assertEquals(count($ids), 2, 'Check Individual-General rule for dupesByParams().');
220 // delete all created contacts
221 foreach ($contactIds as $contactId) {
222 Contact
::delete($contactId);