manually merged work on CRM-10592, #527
[civicrm-core.git] / tests / phpunit / CRM / Dedupe / DedupeFinderTest.php
1 <?php
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
16
17 // create a group to hold contacts, so that dupe checks don't consider any other contacts in the DB
18 $params = array(
19 'name' => 'Dupe Group',
20 'title' => 'New Test Dupe Group',
21 'domain_id' => 1,
22 'is_active' => 1,
23 'visibility' => 'Public Pages',
24 'version' => 3,
25 );
26 // TODO: This is not an API test!!
27 $result = civicrm_api('group', 'create', $params);
28 $groupId = $result['id'];
29
30 // contact data set
31 // FIXME: move create params to separate function
32 $params = array(
33 array(
34 'first_name' => 'robin',
35 'last_name' => 'hood',
36 'email' => 'robin@example.com',
37 'contact_type' => 'Individual',
38 ),
39 array(
40 'first_name' => 'robin',
41 'last_name' => 'hood',
42 'email' => 'hood@example.com',
43 'contact_type' => 'Individual',
44 ),
45 array(
46 'first_name' => 'robin',
47 'last_name' => 'dale',
48 'email' => 'robin@example.com',
49 'contact_type' => 'Individual',
50 ),
51 array(
52 'first_name' => 'little',
53 'last_name' => 'dale',
54 'email' => 'dale@example.com',
55 'contact_type' => 'Individual',
56 ),
57 array(
58 'first_name' => 'will',
59 'last_name' => 'dale',
60 'email' => 'dale@example.com',
61 'contact_type' => 'Individual',
62 ),
63 array(
64 'first_name' => 'will',
65 'last_name' => 'dale',
66 'email' => 'will@example.com',
67 'contact_type' => 'Individual',
68 ),
69 array(
70 'first_name' => 'will',
71 'last_name' => 'dale',
72 'email' => 'will@example.com',
73 'contact_type' => 'Individual',
74 ),
75 );
76
77 $count = 1;
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'];
83
84 $grpParams = array(
85 'contact_id' => $contact['id'],
86 'group_id' => $groupId,
87 'version' => 3,
88 );
89 $res = civicrm_api('group_contact', 'create', $grpParams);
90 }
91
92 // verify that all contacts have been created separately
93 $this->assertEquals(count($contactIds), 7, 'Check for number of contacts.');
94
95 $dao = new CRM_Dedupe_DAO_RuleGroup();
96 $dao->contact_type = 'Individual';
97 $dao->level = 'Fuzzy';
98 $dao->is_default = 1;
99 $dao->find(TRUE);
100
101 $foundDupes = CRM_Dedupe_Finder::dupesInGroup($dao->id, $groupId);
102
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().');
110
111 // delete all created contacts
112 foreach ($contactIds as $contactId) {
113 Contact::delete($contactId);
114 }
115 // delete dupe group
116 $params = array('id' => $groupId, 'version' => 3);
117 civicrm_api('group', 'delete', $params);
118 }
119
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
131
132 // contact data set
133 // FIXME: move create params to separate function
134 $params = array(
135 array(
136 'first_name' => 'robin',
137 'last_name' => 'hood',
138 'email' => 'robin@example.com',
139 'contact_type' => 'Individual',
140 ),
141 array(
142 'first_name' => 'robin',
143 'last_name' => 'hood',
144 'email' => 'hood@example.com',
145 'contact_type' => 'Individual',
146 ),
147 array(
148 'first_name' => 'robin',
149 'last_name' => 'dale',
150 'email' => 'robin@example.com',
151 'contact_type' => 'Individual',
152 ),
153 array(
154 'first_name' => 'little',
155 'last_name' => 'dale',
156 'email' => 'dale@example.com',
157 'contact_type' => 'Individual',
158 ),
159 array(
160 'first_name' => 'will',
161 'last_name' => 'dale',
162 'email' => 'dale@example.com',
163 'contact_type' => 'Individual',
164 ),
165 array(
166 'first_name' => 'will',
167 'last_name' => 'dale',
168 'email' => 'will@example.com',
169 'contact_type' => 'Individual',
170 ),
171 array(
172 'first_name' => 'will',
173 'last_name' => 'dale',
174 'email' => 'will@example.com',
175 'contact_type' => 'Individual',
176 ),
177 );
178
179 $count = 1;
180 // TODO: This is not an API test!!
181 foreach ($params as $param) {
182 $param['version'] = 3;
183 $contact = civicrm_api('contact', 'create', $param);
184 $params = array(
185 'contact_id' => $contact['id'],
186 'street_address' => 'Ambachtstraat 23',
187 'location_type_id' => 1,
188 'version' => 3,
189 );
190 $result = civicrm_api( 'address','create',$params );
191 $contactIds[$count++] = $contact['id'];
192 }
193
194 // verify that all contacts have been created separately
195 $this->assertEquals(count($contactIds), 7, 'Check for number of contacts.');
196
197 $dao = new CRM_Dedupe_DAO_RuleGroup();
198 $dao->contact_type = 'Individual';
199 $dao->used = 'General';
200 $dao->is_default = 1;
201 $dao->find(TRUE);
202
203 $fields = array(
204 'first_name' => 'robin',
205 'last_name' => 'hood',
206 'email' => 'hood@example.com',
207 'street_address' => 'Ambachtstraat 23',
208 );
209 $errorScope = CRM_Core_TemporaryErrorScope::useException();
210 $dedupeParams = CRM_Dedupe_Finder::formatParams($fields, 'Individual');
211 $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual', 'General');
212
213 // Check with default Individual-General rule
214 $this->assertEquals(count($ids), 2, 'Check Individual-General rule for dupesByParams().');
215
216 // delete all created contacts
217 foreach ($contactIds as $contactId) {
218 Contact::delete($contactId);
219 }
220 }
221 }
222
223
224