2 require_once 'CiviTest/CiviUnitTestCase.php';
3 require_once 'CiviTest/Contact.php';
4 class CRM_Dedupe_DedupeFinderTest
extends CiviUnitTestCase
{
5 function testFuzzyDupes() {
6 // make dupe checks based on based on following contact sets:
7 // FIRST - LAST - EMAIL
8 // ---------------------------------
9 // robin - hood - robin@example.com
10 // robin - hood - hood@example.com
11 // robin - dale - robin@example.com
12 // little - dale - dale@example.com
13 // will - dale - dale@example.com
14 // will - dale - will@example.com
15 // will - dale - will@example.com
17 // create a group to hold contacts, so that dupe checks don't consider any other contacts in the DB
19 'name' => 'Dupe Group',
20 'title' => 'New Test Dupe Group',
23 'visibility' => 'Public Pages',
26 // TODO: This is not an API test!!
27 $result = civicrm_api('group', 'create', $params);
28 $groupId = $result['id'];
31 // FIXME: move create params to separate function
34 'first_name' => 'robin',
35 'last_name' => 'hood',
36 'email' => 'robin@example.com',
37 'contact_type' => 'Individual',
40 'first_name' => 'robin',
41 'last_name' => 'hood',
42 'email' => 'hood@example.com',
43 'contact_type' => 'Individual',
46 'first_name' => 'robin',
47 'last_name' => 'dale',
48 'email' => 'robin@example.com',
49 'contact_type' => 'Individual',
52 'first_name' => 'little',
53 'last_name' => 'dale',
54 'email' => 'dale@example.com',
55 'contact_type' => 'Individual',
58 'first_name' => 'will',
59 'last_name' => 'dale',
60 'email' => 'dale@example.com',
61 'contact_type' => 'Individual',
64 'first_name' => 'will',
65 'last_name' => 'dale',
66 'email' => 'will@example.com',
67 'contact_type' => 'Individual',
70 'first_name' => 'will',
71 'last_name' => 'dale',
72 'email' => 'will@example.com',
73 'contact_type' => 'Individual',
78 // TODO: This is not an API test!!
79 foreach ($params as $param) {
80 $param['version'] = 3;
81 $contact = civicrm_api('contact', 'create', $param);
82 $contactIds[$count++
] = $contact['id'];
85 'contact_id' => $contact['id'],
86 'group_id' => $groupId,
89 $res = civicrm_api('group_contact', 'create', $grpParams);
92 // verify that all contacts have been created separately
93 $this->assertEquals(count($contactIds), 7, 'Check for number of contacts.');
95 $dao = new CRM_Dedupe_DAO_RuleGroup();
96 $dao->contact_type
= 'Individual';
97 $dao->level
= 'Fuzzy';
101 $foundDupes = CRM_Dedupe_Finder
::dupesInGroup($dao->id
, $groupId);
103 // -------------------------------------------------------------------------
104 // default dedupe rule: threshold = 20 => (First + Last + Email) Matches ( 1 pair )
105 // --------------------------------------------------------------------------
106 // will - dale - will@example.com
107 // will - dale - will@example.com
108 // so 1 pair for - first + last + mail
109 $this->assertEquals(count($foundDupes), 1, 'Check Individual-Fuzzy dupe rule for dupesInGroup().');
111 // delete all created contacts
112 foreach ($contactIds as $contactId) {
113 Contact
::delete($contactId);
116 $params = array('id' => $groupId, 'version' => 3);
117 civicrm_api('group', 'delete', $params);
120 function testDupesByParams() {
121 // make dupe checks based on based on following contact sets:
122 // FIRST - LAST - EMAIL
123 // ---------------------------------
124 // robin - hood - robin@example.com
125 // robin - hood - hood@example.com
126 // robin - dale - robin@example.com
127 // little - dale - dale@example.com
128 // will - dale - dale@example.com
129 // will - dale - will@example.com
130 // will - dale - will@example.com
133 // FIXME: move create params to separate function
136 'first_name' => 'robin',
137 'last_name' => 'hood',
138 'email' => 'robin@example.com',
139 'contact_type' => 'Individual',
142 'first_name' => 'robin',
143 'last_name' => 'hood',
144 'email' => 'hood@example.com',
145 'contact_type' => 'Individual',
148 'first_name' => 'robin',
149 'last_name' => 'dale',
150 'email' => 'robin@example.com',
151 'contact_type' => 'Individual',
154 'first_name' => 'little',
155 'last_name' => 'dale',
156 'email' => 'dale@example.com',
157 'contact_type' => 'Individual',
160 'first_name' => 'will',
161 'last_name' => 'dale',
162 'email' => 'dale@example.com',
163 'contact_type' => 'Individual',
166 'first_name' => 'will',
167 'last_name' => 'dale',
168 'email' => 'will@example.com',
169 'contact_type' => 'Individual',
172 'first_name' => 'will',
173 'last_name' => 'dale',
174 'email' => 'will@example.com',
175 'contact_type' => 'Individual',
180 // TODO: This is not an API test!!
181 foreach ($params as $param) {
182 $param['version'] = 3;
183 $contact = civicrm_api('contact', 'create', $param);
185 'contact_id' => $contact['id'],
186 'street_address' => 'Ambachtstraat 23',
187 'location_type_id' => 1,
190 $result = civicrm_api( 'address','create',$params );
191 $contactIds[$count++
] = $contact['id'];
194 // verify that all contacts have been created separately
195 $this->assertEquals(count($contactIds), 7, 'Check for number of contacts.');
197 $dao = new CRM_Dedupe_DAO_RuleGroup();
198 $dao->contact_type
= 'Individual';
199 $dao->used
= 'General';
200 $dao->is_default
= 1;
204 'first_name' => 'robin',
205 'last_name' => 'hood',
206 'email' => 'hood@example.com',
207 'street_address' => 'Ambachtstraat 23',
209 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
210 $dedupeParams = CRM_Dedupe_Finder
::formatParams($fields, 'Individual');
211 $ids = CRM_Dedupe_Finder
::dupesByParams($dedupeParams, 'Individual', 'General');
213 // Check with default Individual-General rule
214 $this->assertEquals(count($ids), 2, 'Check Individual-General rule for dupesByParams().');
216 // delete all created contacts
217 foreach ($contactIds as $contactId) {
218 Contact
::delete($contactId);