tests/phpunit - Declare `@group headless`
[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 'version' => 3,
30 );
31 // TODO: This is not an API test!!
32 $result = civicrm_api('group', 'create', $params);
33 $groupId = $result['id'];
34
35 // contact data set
36 // FIXME: move create params to separate function
37 $params = array(
38 array(
39 'first_name' => 'robin',
40 'last_name' => 'hood',
41 'email' => 'robin@example.com',
42 'contact_type' => 'Individual',
43 ),
44 array(
45 'first_name' => 'robin',
46 'last_name' => 'hood',
47 'email' => 'hood@example.com',
48 'contact_type' => 'Individual',
49 ),
50 array(
51 'first_name' => 'robin',
52 'last_name' => 'dale',
53 'email' => 'robin@example.com',
54 'contact_type' => 'Individual',
55 ),
56 array(
57 'first_name' => 'little',
58 'last_name' => 'dale',
59 'email' => 'dale@example.com',
60 'contact_type' => 'Individual',
61 ),
62 array(
63 'first_name' => 'will',
64 'last_name' => 'dale',
65 'email' => 'dale@example.com',
66 'contact_type' => 'Individual',
67 ),
68 array(
69 'first_name' => 'will',
70 'last_name' => 'dale',
71 'email' => 'will@example.com',
72 'contact_type' => 'Individual',
73 ),
74 array(
75 'first_name' => 'will',
76 'last_name' => 'dale',
77 'email' => 'will@example.com',
78 'contact_type' => 'Individual',
79 ),
80 );
81
82 $count = 1;
83 // TODO: This is not an API test!!
84 foreach ($params as $param) {
85 $param['version'] = 3;
86 $contact = civicrm_api('contact', 'create', $param);
87 $contactIds[$count++] = $contact['id'];
88
89 $grpParams = array(
90 'contact_id' => $contact['id'],
91 'group_id' => $groupId,
92 'version' => 3,
93 );
94 $res = civicrm_api('group_contact', 'create', $grpParams);
95 }
96
97 // verify that all contacts have been created separately
98 $this->assertEquals(count($contactIds), 7, 'Check for number of contacts.');
99
100 $dao = new CRM_Dedupe_DAO_RuleGroup();
101 $dao->contact_type = 'Individual';
102 $dao->level = 'Fuzzy';
103 $dao->is_default = 1;
104 $dao->find(TRUE);
105
106 $foundDupes = CRM_Dedupe_Finder::dupesInGroup($dao->id, $groupId);
107
108 // -------------------------------------------------------------------------
109 // default dedupe rule: threshold = 20 => (First + Last + Email) Matches ( 1 pair )
110 // --------------------------------------------------------------------------
111 // will - dale - will@example.com
112 // will - dale - will@example.com
113 // so 1 pair for - first + last + mail
114 $this->assertEquals(count($foundDupes), 1, 'Check Individual-Fuzzy dupe rule for dupesInGroup().');
115
116 // delete all created contacts
117 foreach ($contactIds as $contactId) {
118 Contact::delete($contactId);
119 }
120 // delete dupe group
121 $params = array('id' => $groupId, 'version' => 3);
122 civicrm_api('group', 'delete', $params);
123 }
124
125 public function testDupesByParams() {
126 // make dupe checks based on based on following contact sets:
127 // FIRST - LAST - EMAIL
128 // ---------------------------------
129 // robin - hood - robin@example.com
130 // robin - hood - hood@example.com
131 // robin - dale - robin@example.com
132 // little - dale - dale@example.com
133 // will - dale - dale@example.com
134 // will - dale - will@example.com
135 // will - dale - will@example.com
136
137 // contact data set
138 // FIXME: move create params to separate function
139 $params = array(
140 array(
141 'first_name' => 'robin',
142 'last_name' => 'hood',
143 'email' => 'robin@example.com',
144 'contact_type' => 'Individual',
145 ),
146 array(
147 'first_name' => 'robin',
148 'last_name' => 'hood',
149 'email' => 'hood@example.com',
150 'contact_type' => 'Individual',
151 ),
152 array(
153 'first_name' => 'robin',
154 'last_name' => 'dale',
155 'email' => 'robin@example.com',
156 'contact_type' => 'Individual',
157 ),
158 array(
159 'first_name' => 'little',
160 'last_name' => 'dale',
161 'email' => 'dale@example.com',
162 'contact_type' => 'Individual',
163 ),
164 array(
165 'first_name' => 'will',
166 'last_name' => 'dale',
167 'email' => 'dale@example.com',
168 'contact_type' => 'Individual',
169 ),
170 array(
171 'first_name' => 'will',
172 'last_name' => 'dale',
173 'email' => 'will@example.com',
174 'contact_type' => 'Individual',
175 ),
176 array(
177 'first_name' => 'will',
178 'last_name' => 'dale',
179 'email' => 'will@example.com',
180 'contact_type' => 'Individual',
181 ),
182 );
183
184 $count = 1;
185 // TODO: This is not an API test!!
186 foreach ($params as $param) {
187 $param['version'] = 3;
188 $contact = civicrm_api('contact', 'create', $param);
189 $params = array(
190 'contact_id' => $contact['id'],
191 'street_address' => 'Ambachtstraat 23',
192 'location_type_id' => 1,
193 'version' => 3,
194 );
195 $result = civicrm_api('address', 'create', $params);
196 $contactIds[$count++] = $contact['id'];
197 }
198
199 // verify that all contacts have been created separately
200 $this->assertEquals(count($contactIds), 7, 'Check for number of contacts.');
201
202 $dao = new CRM_Dedupe_DAO_RuleGroup();
203 $dao->contact_type = 'Individual';
204 $dao->used = 'General';
205 $dao->is_default = 1;
206 $dao->find(TRUE);
207
208 $fields = array(
209 'first_name' => 'robin',
210 'last_name' => 'hood',
211 'email' => 'hood@example.com',
212 'street_address' => 'Ambachtstraat 23',
213 );
214 $errorScope = CRM_Core_TemporaryErrorScope::useException();
215 $dedupeParams = CRM_Dedupe_Finder::formatParams($fields, 'Individual');
216 $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual', 'General');
217
218 // Check with default Individual-General rule
219 $this->assertEquals(count($ids), 2, 'Check Individual-General rule for dupesByParams().');
220
221 // delete all created contacts
222 foreach ($contactIds as $contactId) {
223 Contact::delete($contactId);
224 }
225 }
226
227 }