Merge pull request #8338 from JKingsnorth/CRM-18526
[civicrm-core.git] / tests / phpunit / CRM / Dedupe / DedupeFinderTest.php
1 <?php
2
3 require_once 'CiviTest/Contact.php';
4
5 /**
6 * Class CRM_Dedupe_DedupeFinderTest
7 * @group headless
8 */
9 class CRM_Dedupe_DedupeFinderTest extends CiviUnitTestCase {
10 public function testFuzzyDupes() {
11 // make dupe checks based on based on following contact sets:
12 // FIRST - LAST - EMAIL
13 // ---------------------------------
14 // robin - hood - robin@example.com
15 // robin - hood - hood@example.com
16 // robin - dale - robin@example.com
17 // little - dale - dale@example.com
18 // will - dale - dale@example.com
19 // will - dale - will@example.com
20 // will - dale - will@example.com
21
22 // create a group to hold contacts, so that dupe checks don't consider any other contacts in the DB
23 $params = array(
24 'name' => 'Dupe Group',
25 'title' => 'New Test Dupe Group',
26 'domain_id' => 1,
27 'is_active' => 1,
28 'visibility' => 'Public Pages',
29 );
30
31 $result = $this->callAPISuccess('group', 'create', $params);
32 $groupId = $result['id'];
33
34 // contact data set
35 // FIXME: move create params to separate function
36 $params = array(
37 array(
38 'first_name' => 'robin',
39 'last_name' => 'hood',
40 'email' => 'robin@example.com',
41 'contact_type' => 'Individual',
42 ),
43 array(
44 'first_name' => 'robin',
45 'last_name' => 'hood',
46 'email' => 'hood@example.com',
47 'contact_type' => 'Individual',
48 ),
49 array(
50 'first_name' => 'robin',
51 'last_name' => 'dale',
52 'email' => 'robin@example.com',
53 'contact_type' => 'Individual',
54 ),
55 array(
56 'first_name' => 'little',
57 'last_name' => 'dale',
58 'email' => 'dale@example.com',
59 'contact_type' => 'Individual',
60 ),
61 array(
62 'first_name' => 'will',
63 'last_name' => 'dale',
64 'email' => 'dale@example.com',
65 'contact_type' => 'Individual',
66 ),
67 array(
68 'first_name' => 'will',
69 'last_name' => 'dale',
70 'email' => 'will@example.com',
71 'contact_type' => 'Individual',
72 ),
73 array(
74 'first_name' => 'will',
75 'last_name' => 'dale',
76 'email' => 'will@example.com',
77 'contact_type' => 'Individual',
78 ),
79 );
80
81 $count = 1;
82 foreach ($params as $param) {
83 $contact = $this->callAPISuccess('contact', 'create', $param);
84 $contactIds[$count++] = $contact['id'];
85
86 $grpParams = array(
87 'contact_id' => $contact['id'],
88 'group_id' => $groupId,
89 );
90 $this->callAPISuccess('group_contact', 'create', $grpParams);
91 }
92
93 // verify that all contacts have been created separately
94 $this->assertEquals(count($contactIds), 7, 'Check for number of contacts.');
95
96 $dao = new CRM_Dedupe_DAO_RuleGroup();
97 $dao->contact_type = 'Individual';
98 $dao->level = 'Fuzzy';
99 $dao->is_default = 1;
100 $dao->find(TRUE);
101
102 $foundDupes = CRM_Dedupe_Finder::dupesInGroup($dao->id, $groupId);
103
104 // -------------------------------------------------------------------------
105 // default dedupe rule: threshold = 20 => (First + Last + Email) Matches ( 1 pair )
106 // --------------------------------------------------------------------------
107 // will - dale - will@example.com
108 // will - dale - will@example.com
109 // so 1 pair for - first + last + mail
110 $this->assertEquals(count($foundDupes), 1, 'Check Individual-Fuzzy dupe rule for dupesInGroup().');
111
112 foreach ($contactIds as $contactId) {
113 $this->contactDelete($contactId);
114 }
115 // delete dupe group
116 $params = array('id' => $groupId, 'version' => 3);
117 civicrm_api('group', 'delete', $params);
118 }
119
120 public 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
181 foreach ($params as $param) {
182 $contact = $this->callAPISuccess('contact', 'create', $param);
183 $params = array(
184 'contact_id' => $contact['id'],
185 'street_address' => 'Ambachtstraat 23',
186 'location_type_id' => 1,
187 );
188 $this->callAPISuccess('address', 'create', $params);
189 $contactIds[$count++] = $contact['id'];
190 }
191
192 // verify that all contacts have been created separately
193 $this->assertEquals(count($contactIds), 7, 'Check for number of contacts.');
194
195 $dao = new CRM_Dedupe_DAO_RuleGroup();
196 $dao->contact_type = 'Individual';
197 $dao->used = 'General';
198 $dao->is_default = 1;
199 $dao->find(TRUE);
200
201 $fields = array(
202 'first_name' => 'robin',
203 'last_name' => 'hood',
204 'email' => 'hood@example.com',
205 'street_address' => 'Ambachtstraat 23',
206 );
207 CRM_Core_TemporaryErrorScope::useException();
208 $dedupeParams = CRM_Dedupe_Finder::formatParams($fields, 'Individual');
209 $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual', 'General');
210
211 // Check with default Individual-General rule
212 $this->assertEquals(count($ids), 2, 'Check Individual-General rule for dupesByParams().');
213
214 // delete all created contacts
215 foreach ($contactIds as $contactId) {
216 $this->contactDelete($contactId);
217 }
218 }
219
220 }