Towards CRM-20155 remove duplicate calls to dedupe functions.
[civicrm-core.git] / tests / phpunit / CRM / Dedupe / DedupeFinderTest.php
1 <?php
2
3 /**
4 * Class CRM_Dedupe_DedupeFinderTest
5 * @group headless
6 */
7 class CRM_Dedupe_DedupeFinderTest extends CiviUnitTestCase {
8 public function testFuzzyDupes() {
9 // make dupe checks based on based on following contact sets:
10 // FIRST - LAST - EMAIL
11 // ---------------------------------
12 // robin - hood - robin@example.com
13 // robin - hood - hood@example.com
14 // robin - dale - robin@example.com
15 // little - dale - dale@example.com
16 // will - dale - dale@example.com
17 // will - dale - will@example.com
18 // will - dale - will@example.com
19
20 // create a group to hold contacts, so that dupe checks don't consider any other contacts in the DB
21 $params = array(
22 'name' => 'Dupe Group',
23 'title' => 'New Test Dupe Group',
24 'domain_id' => 1,
25 'is_active' => 1,
26 'visibility' => 'Public Pages',
27 );
28
29 $result = $this->callAPISuccess('group', 'create', $params);
30 $groupId = $result['id'];
31
32 // contact data set
33 // FIXME: move create params to separate function
34 $params = array(
35 array(
36 'first_name' => 'robin',
37 'last_name' => 'hood',
38 'email' => 'robin@example.com',
39 'contact_type' => 'Individual',
40 ),
41 array(
42 'first_name' => 'robin',
43 'last_name' => 'hood',
44 'email' => 'hood@example.com',
45 'contact_type' => 'Individual',
46 ),
47 array(
48 'first_name' => 'robin',
49 'last_name' => 'dale',
50 'email' => 'robin@example.com',
51 'contact_type' => 'Individual',
52 ),
53 array(
54 'first_name' => 'little',
55 'last_name' => 'dale',
56 'email' => 'dale@example.com',
57 'contact_type' => 'Individual',
58 ),
59 array(
60 'first_name' => 'will',
61 'last_name' => 'dale',
62 'email' => 'dale@example.com',
63 'contact_type' => 'Individual',
64 ),
65 array(
66 'first_name' => 'will',
67 'last_name' => 'dale',
68 'email' => 'will@example.com',
69 'contact_type' => 'Individual',
70 ),
71 array(
72 'first_name' => 'will',
73 'last_name' => 'dale',
74 'email' => 'will@example.com',
75 'contact_type' => 'Individual',
76 ),
77 );
78
79 $count = 1;
80 foreach ($params as $param) {
81 $contact = $this->callAPISuccess('contact', 'create', $param);
82 $contactIds[$count++] = $contact['id'];
83
84 $grpParams = array(
85 'contact_id' => $contact['id'],
86 'group_id' => $groupId,
87 );
88 $this->callAPISuccess('group_contact', 'create', $grpParams);
89 }
90
91 // verify that all contacts have been created separately
92 $this->assertEquals(count($contactIds), 7, 'Check for number of contacts.');
93
94 $dao = new CRM_Dedupe_DAO_RuleGroup();
95 $dao->contact_type = 'Individual';
96 $dao->level = 'Fuzzy';
97 $dao->is_default = 1;
98 $dao->find(TRUE);
99
100 $foundDupes = CRM_Dedupe_Finder::dupesInGroup($dao->id, $groupId);
101
102 // -------------------------------------------------------------------------
103 // default dedupe rule: threshold = 20 => (First + Last + Email) Matches ( 1 pair )
104 // --------------------------------------------------------------------------
105 // will - dale - will@example.com
106 // will - dale - will@example.com
107 // so 1 pair for - first + last + mail
108 $this->assertEquals(count($foundDupes), 1, 'Check Individual-Fuzzy dupe rule for dupesInGroup().');
109
110 foreach ($contactIds as $contactId) {
111 $this->contactDelete($contactId);
112 }
113 // delete dupe group
114 $params = array('id' => $groupId, 'version' => 3);
115 civicrm_api('group', 'delete', $params);
116 }
117
118 public function testDupesByParams() {
119 // make dupe checks based on based on following contact sets:
120 // FIRST - LAST - EMAIL
121 // ---------------------------------
122 // robin - hood - robin@example.com
123 // robin - hood - hood@example.com
124 // robin - dale - robin@example.com
125 // little - dale - dale@example.com
126 // will - dale - dale@example.com
127 // will - dale - will@example.com
128 // will - dale - will@example.com
129
130 // contact data set
131 // FIXME: move create params to separate function
132 $params = array(
133 array(
134 'first_name' => 'robin',
135 'last_name' => 'hood',
136 'email' => 'robin@example.com',
137 'contact_type' => 'Individual',
138 ),
139 array(
140 'first_name' => 'robin',
141 'last_name' => 'hood',
142 'email' => 'hood@example.com',
143 'contact_type' => 'Individual',
144 ),
145 array(
146 'first_name' => 'robin',
147 'last_name' => 'dale',
148 'email' => 'robin@example.com',
149 'contact_type' => 'Individual',
150 ),
151 array(
152 'first_name' => 'little',
153 'last_name' => 'dale',
154 'email' => 'dale@example.com',
155 'contact_type' => 'Individual',
156 ),
157 array(
158 'first_name' => 'will',
159 'last_name' => 'dale',
160 'email' => 'dale@example.com',
161 'contact_type' => 'Individual',
162 ),
163 array(
164 'first_name' => 'will',
165 'last_name' => 'dale',
166 'email' => 'will@example.com',
167 'contact_type' => 'Individual',
168 ),
169 array(
170 'first_name' => 'will',
171 'last_name' => 'dale',
172 'email' => 'will@example.com',
173 'contact_type' => 'Individual',
174 ),
175 );
176
177 $count = 1;
178
179 foreach ($params as $param) {
180 $contact = $this->callAPISuccess('contact', 'create', $param);
181 $params = array(
182 'contact_id' => $contact['id'],
183 'street_address' => 'Ambachtstraat 23',
184 'location_type_id' => 1,
185 );
186 $this->callAPISuccess('address', 'create', $params);
187 $contactIds[$count++] = $contact['id'];
188 }
189
190 // verify that all contacts have been created separately
191 $this->assertEquals(count($contactIds), 7, 'Check for number of contacts.');
192
193 $dao = new CRM_Dedupe_DAO_RuleGroup();
194 $dao->contact_type = 'Individual';
195 $dao->used = 'General';
196 $dao->is_default = 1;
197 $dao->find(TRUE);
198
199 $fields = array(
200 'first_name' => 'robin',
201 'last_name' => 'hood',
202 'email' => 'hood@example.com',
203 'street_address' => 'Ambachtstraat 23',
204 );
205 CRM_Core_TemporaryErrorScope::useException();
206 $ids = CRM_Contact_BAO_Contact::getDuplicateContacts($fields, 'Individual', 'General');
207
208 // Check with default Individual-General rule
209 $this->assertEquals(count($ids), 2, 'Check Individual-General rule for dupesByParams().');
210
211 // delete all created contacts
212 foreach ($contactIds as $contactId) {
213 $this->contactDelete($contactId);
214 }
215 }
216
217 }