add missing comments - tests directory
[civicrm-core.git] / tests / phpunit / WebTest / Import / DuplicateMatchingTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License along with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
25 */
26
27 require_once 'WebTest/Import/ImportCiviSeleniumTestCase.php';
28 class WebTest_Import_DuplicateMatchingTest extends ImportCiviSeleniumTestCase {
29
30 protected function setUp() {
31 parent::setUp();
32 }
33
34 /*
35 * Test contact import for Individuals Duplicate Matching.
36 */
37 function testIndividualDuplicateMatchingImport() {
38 $this->webtestLogin();
39
40 $this->openCiviPage("contact/add", "reset=1&ct=Individual", 'first_name');
41
42 $email = substr(sha1(rand()), 0, 7) . '@example.com';
43
44 // fill in first name
45 $firstName = substr(sha1(rand()), 0, 7);
46 $this->type('first_name', $firstName);
47
48 // fill in last name
49 $lastName = substr(sha1(rand()), 0, 7);
50 $this->type('last_name', $lastName);
51
52 //fill in email
53 $this->type('email_1_email', $email);
54
55 // Clicking save.
56 $this->click('_qf_Contact_upload_view');
57 $this->waitForPageToLoad($this->getTimeoutMsec());
58 $this->waitForText('crm-notification-container', "Contact Saved");
59
60 $existingContact = array(
61 'first_name' => $firstName,
62 'last_name' => $lastName,
63 'email' => $email,
64 );
65
66 // Get sample import data.
67 list($headers, $rows) = $this->_individualDuplicateMatchingCSVData($existingContact);
68
69 // Import and check Individual contacts in Skip mode.
70 $other = array('callbackImportSummary' => 'checkDuplicateContacts');
71 $this->importContacts($headers, $rows, 'Individual', 'Skip', array(), $other);
72
73 // Get imported contact Ids
74 $importedContactIds = $this->_getImportedContactIds($rows);
75
76 // Build update mode import headers
77 $updateHeaders = array(
78 'email' => 'Email',
79 'first_name' => 'First Name',
80 'last_name' => 'Last Name',
81 );
82
83 // Create update mode import rows
84 $updateRows = array();
85 $contact = current($rows);
86 foreach ($importedContactIds as $cid) {
87 $updateRows[$cid] = array(
88 'email' => $contact['email'],
89 'first_name' => substr(sha1(rand()), 0, 7),
90 'last_name' => 'Anderson' . substr(sha1(rand()), 0, 7),
91 );
92 $contact = next($rows);
93 }
94
95 // Import and check Individual contacts in Update mode.
96 $this->importContacts($updateHeaders, $updateRows, 'Individual', 'Update');
97
98 // Headers that should not updated.
99 $fillHeaders = $updateHeaders;
100
101 // Headers that should fill.
102 $fillHeaders['gender'] = 'Gender';
103 $fillHeaders['dob'] = 'Birth Date';
104
105 $fillRows = array();
106 foreach ($importedContactIds as $cid) {
107 $fillRows[$cid] = array(
108 'email' => $updateRows[$cid]['email'],
109 // should not update
110 'first_name' => substr(sha1(rand()), 0, 7),
111 // should not update
112 'last_name' => 'Anderson' . substr(sha1(rand()), 0, 7),
113 'gender' => 'Male',
114 'dob' => '1986-04-16',
115 );
116 }
117
118 // Import and check Individual contacts in Fill mode.
119 $this->importContacts($fillHeaders, $fillRows, 'Individual', 'Fill');
120
121 foreach ($importedContactIds as $cid) {
122 $this->openCiviPage("contact/view", "reset=1&cid={$cid}");
123
124 // Check old display name.
125 $displayName = "{$updateRows[$cid]['first_name']} {$updateRows[$cid]['last_name']}";
126 $this->assertTrue($this->isTextPresent("$displayName"), 'Contact display name should not update in fill mode!');
127
128 $this->verifyText('css=div.crm-contact-gender_display', preg_quote($fillRows[$cid]['gender']));
129 }
130
131 // Recreate same conacts using 'No Duplicate Checking'
132 $this->importContacts($headers, $rows, 'Individual', 'No Duplicate Checking');
133 }
134
135 /*
136 * Test contact import for Organization Duplicate Matching.
137 */
138 function testOrganizationDuplicateMatchingImport() {
139 $this->webtestLogin();
140
141 //create oranization
142 $this->openCiviPage("contact/add", "reset=1&ct=Organization", 'organization_name');
143
144 // get value for organization contact
145 $organizationName = 'org_' . substr(sha1(rand()), 0, 7);
146 $organizationEmail = substr(sha1(rand()), 0, 7) . '@example.org';
147
148 $this->click('organization_name');
149
150 //fill in first name
151 $this->type('organization_name', $organizationName);
152
153 //fill in Email
154 $this->type('email_1_email', $organizationEmail);
155
156 // Clicking save.
157 $this->click('_qf_Contact_upload_view');
158 $this->waitForPageToLoad($this->getTimeoutMsec());
159
160 // Reset Organization strict dedupe rule for Organization name
161 // and Organization email (default)
162 $this->webtestStrictDedupeRuleDefault('Organization');
163
164 $organizationFields = array(
165 'organization_name' => $organizationName,
166 'email' => $organizationEmail,
167 );
168 // Get sample import data.
169 list($headers, $rows) = $this->_organizationDuplicateMatchingCSVData($organizationFields);
170
171 // Import and check Individual contacts in Skip mode.
172 $other = array('callbackImportSummary' => 'checkDuplicateContacts');
173 $this->importContacts($headers, $rows, 'Organization', 'Skip', array(), $other);
174
175 // Get imported contact Ids
176 $importedContactIds = $this->_getImportedContactIds($rows, 'Organization');
177
178 // Build update mode import headers
179 $updateHeaders = array(
180 'email' => 'Email',
181 'organization_name' => 'Organization Name',
182 );
183
184 // Create update mode import rows
185 $updateRows = array();
186 $contact = current($rows);
187 foreach ($importedContactIds as $cid) {
188 $updateRows[$cid] = array(
189 'email' => $contact['email'],
190 'organization_name' => 'UpdatedOrg ' . substr(sha1(rand()), 0, 7),
191 );
192 $contact = next($rows);
193 }
194
195 // Import and check Individual contacts in Update mode.
196 $this->importContacts($updateHeaders, $updateRows, 'Organization', 'Update');
197
198 // Headers that should not updated.
199 $fillHeaders = $updateHeaders;
200
201 // Headers that should fill.
202 $fillHeaders['legal_name'] = 'Legal Name';
203
204 $fillRows = array();
205 foreach ($importedContactIds as $cid) {
206 $fillRows[$cid] = array(
207 'email' => $updateRows[$cid]['email'],
208 // should not update
209 'organization_name' => 'UpdateOrg ' . substr(sha1(rand()), 0, 7),
210 'legal_name' => 'org ' . substr(sha1(rand()), 0, 7),
211 );
212 }
213
214 // Import and check Individual contacts in Fill mode.
215 $this->importContacts($fillHeaders, $fillRows, 'Organization', 'Fill');
216
217 foreach ($importedContactIds as $cid) {
218 $this->openCiviPage("contact/view", "reset=1&cid={$cid}");
219
220 // Check old Organization name.
221 $organizationName = $updateRows[$cid]['organization_name'];
222 $this->assertTrue($this->isTextPresent("$organizationName"), 'Contact should not update in fill mode!');
223 $this->verifyText("xpath=//div[@id='crm-contactinfo-content']/div/div[3]/div[2]", preg_quote($fillRows[$cid]['legal_name']));
224 }
225
226 // Recreate same conacts using 'No Duplicate Checking'
227 $this->importContacts($headers, $rows, 'Organization', 'No Duplicate Checking');
228 }
229
230 /*
231 * Test contact import for Household Duplicate Matching.
232 */
233 function testHouseholdDuplicateMatchingImport() {
234 $this->webtestLogin();
235
236 // create household
237 $this->openCiviPage("contact/add", "reset=1&ct=Household", 'household_name');
238
239 // get values for household contact
240 $householdName = 'household_' . substr(sha1(rand()), 0, 7);
241 $householdEmail = substr(sha1(rand()), 0, 7) . '@example.com';
242
243 //fill in household name
244 $this->type('household_name', $householdName);
245
246 //fill in Email
247 $this->type('email_1_email', $householdEmail);
248
249 // Clicking save.
250 $this->click('_qf_Contact_upload_view');
251 $this->waitForPageToLoad($this->getTimeoutMsec());
252
253 // Reset Household strict dedupe rule for Household name
254 // and Household email (default)
255 $this->webtestStrictDedupeRuleDefault('Household');
256
257 $this->waitForPageToLoad($this->getTimeoutMsec());
258
259 // Store household contact value in array
260 $householdFields = array(
261 'household_name' => $householdName,
262 'email' => $householdEmail,
263 );
264
265 // Get sample import data.
266 list($headers, $rows) = $this->_householdDuplicateMatchingCSVData($householdFields);
267
268 // Import and check Individual contacts in Skip mode.
269 $other = array('callbackImportSummary' => 'checkDuplicateContacts');
270 $this->importContacts($headers, $rows, 'Household', 'Skip', array(), $other);
271
272 // Get imported contact Ids
273 $importedContactIds = $this->_getImportedContactIds($rows, 'Household');
274
275 // Build update mode import headers
276 $updateHeaders = array(
277 'email' => 'Email',
278 'household_name' => 'Household Name',
279 );
280
281 // Create update mode import rows
282 $updateRows = array();
283 $contact = current($rows);
284 foreach ($importedContactIds as $cid) {
285 $updateRows[$cid] = array(
286 'email' => $contact['email'],
287 'household_name' => 'UpdatedHousehold ' . substr(sha1(rand()), 0, 7),
288 );
289 $contact = next($rows);
290 }
291
292 $this->importContacts($updateHeaders, $updateRows, 'Household', 'Update');
293
294 // Headers that should not updated.
295 $fillHeaders = $updateHeaders;
296
297 // Headers that should fill.
298 $fillHeaders['nick_name'] = 'Nick Name';
299
300 $fillRows = array();
301 foreach ($importedContactIds as $cid) {
302 $fillRows[$cid] = array(
303 'email' => $updateRows[$cid]['email'],
304 // should not update
305 'household_name' => 'UpdatedHousehold ' . substr(sha1(rand()), 0, 7),
306 'nick_name' => 'Household ' . substr(sha1(rand()), 0, 7),
307 );
308 }
309
310 // Import and check Individual contacts in Fill mode.
311 $this->importContacts($fillHeaders, $fillRows, 'Household', 'Fill');
312
313 foreach ($importedContactIds as $cid) {
314 $this->openCiviPage("contact/view", "reset=1&cid={$cid}");
315
316 // Check old Household name.
317 $householdName = $updateRows[$cid]['household_name'];
318 $this->assertTrue($this->isTextPresent("$householdName"), 'Contact should not update in fill mode!');
319 $this->verifyText("xpath=//div[@id='crm-contactinfo-content']/div/div[2]/div[2]", preg_quote($fillRows[$cid]['nick_name']));
320 }
321
322 // Recreate same conacts using 'No Duplicate Checking'
323 $this->importContacts($headers, $rows, 'Household', 'No Duplicate Checking');
324 }
325
326 /**
327 * @param $originalHeaders
328 * @param $originalRows
329 * @param $checkSummary
330 */
331 function checkDuplicateContacts($originalHeaders, $originalRows, $checkSummary) {
332 $this->assertTrue($this->isTextPresent('CiviCRM has detected one record which is a duplicate of existing CiviCRM contact record. These records have not been imported.'));
333
334 $checkSummary = array(
335 'Total Rows' => '2',
336 'Duplicate Rows' => '1',
337 'Total Contacts' => '1',
338 );
339
340 foreach ($checkSummary as $label => $value) {
341 $this->verifyText("xpath=//table[@id='summary-counts']/tbody/tr/td[text()='{$label}']/following-sibling::td", preg_quote($value));
342 }
343 }
344
345 /*
346 * Helper function to provide data for contact import for
347 * Individual Duplicate Matching.
348 */
349 /**
350 * @param $individualFields
351 *
352 * @return array
353 */
354 function _individualDuplicateMatchingCSVData($individualFields) {
355 $headers = array(
356 'first_name' => 'First Name',
357 'middle_name' => 'Middle Name',
358 'last_name' => 'Last Name',
359 'email' => 'Email',
360 'phone' => 'Phone',
361 'address_1' => 'Additional Address 1',
362 'address_2' => 'Additional Address 2',
363 'city' => 'City',
364 'state' => 'State',
365 'country' => 'Country',
366 );
367
368 $rows = array(
369 array('first_name' => isset($individualFields['first_name']) ? $individualFields['first_name'] : substr(sha1(rand()), 0, 7),
370 'middle_name' => isset($individualFields['middle_name']) ? $individualFields['middle_name'] : substr(sha1(rand()), 0, 7),
371 'last_name' => isset($individualFields['last_name']) ? $individualFields['last_name'] : 'Anderson',
372 'email' => isset($individualFields['email']) ? $individualFields['email'] : substr(sha1(rand()), 0, 7) . '@example.com',
373 'phone' => '6949912154',
374 'address_1' => 'Add 1',
375 'address_2' => 'Add 2',
376 'city' => 'Watson',
377 'state' => 'NY',
378 'country' => 'United States',
379 ),
380 array('first_name' => substr(sha1(rand()), 0, 7),
381 'middle_name' => substr(sha1(rand()), 0, 7),
382 'last_name' => 'Summerson',
383 'email' => substr(sha1(rand()), 0, 7) . '@example.com',
384 'phone' => '6944412154',
385 'address_1' => 'Add 1',
386 'address_2' => 'Add 2',
387 'city' => 'Watson',
388 'state' => 'NY',
389 'country' => 'United States',
390 ),
391 );
392
393 return array($headers, $rows);
394 }
395
396 /*
397 * Helper function to provide data for contact import for
398 * Organizations Duplicate Matching.
399 */
400 /**
401 * @param $organizationFields
402 *
403 * @return array
404 */
405 function _organizationDuplicateMatchingCSVData($organizationFields) {
406 $headers = array(
407 'organization_name' => 'Organization Name',
408 'email' => 'Email',
409 'phone' => 'Phone',
410 'address_1' => 'Additional Address 1',
411 'address_2' => 'Additional Address 2',
412 'city' => 'City',
413 'state' => 'State',
414 'country' => 'Country',
415 );
416 $rows = array(
417 array('organization_name' => isset($organizationFields['organization_name']) ? $organizationFields['organization_name'] : 'org_' . substr(sha1(rand()), 0, 7),
418 'email' => isset($organizationFields['email']) ? $organizationFields['email'] : substr(sha1(rand()), 0, 7) . 'example.org',
419 'phone' => '9949912154',
420 'address_1' => 'Add 1',
421 'address_2' => 'Add 2',
422 'city' => 'Watson',
423 'state' => 'NY',
424 'country' => 'United States',
425 ),
426 array('organization_name' => 'org_' . substr(sha1(rand()), 0, 7),
427 'email' => substr(sha1(rand()), 0, 7) . '@example.org',
428 'phone' => '6949412154',
429 'address_1' => 'Add 1',
430 'address_2' => 'Add 2',
431 'city' => 'Watson',
432 'state' => 'NY',
433 'country' => 'United States',
434 ),
435 );
436
437 return array($headers, $rows);
438 }
439
440 /*
441 * Helper function to provide data for contact import for Household
442 * Duplicate Matching.
443 */
444 /**
445 * @param $householdFields
446 *
447 * @return array
448 */
449 function _householdDuplicateMatchingCSVData($householdFields) {
450 $headers = array(
451 'household_name' => 'Household Name',
452 'email' => 'Email',
453 'phone' => 'Phone',
454 'address_1' => 'Additional Address 1',
455 'address_2' => 'Additional Address 2',
456 'city' => 'City',
457 'state' => 'State',
458 'country' => 'Country',
459 );
460
461 $rows = array(
462 array('household_name' => isset($householdFields['household_name']) ? $householdFields['household_name'] : 'household_' . substr(sha1(rand()), 0, 7),
463 'email' => isset($householdFields['email']) ? $householdFields['email'] : substr(sha1(rand()), 0, 7) . '@example.com',
464 'phone' => '3949912154',
465 'address_1' => 'Add 1',
466 'address_2' => 'Add 2',
467 'city' => 'Watson',
468 'state' => 'NY',
469 'country' => 'United States',
470 ),
471 array('household_name' => 'household_' . substr(sha1(rand()), 0, 7),
472 'email' => substr(sha1(rand()), 0, 7) . '@example.org',
473 'phone' => '5949412154',
474 'address_1' => 'Add 1',
475 'address_2' => 'Add 2',
476 'city' => 'Watson',
477 'state' => 'NY',
478 'country' => 'United States',
479 ),
480 );
481
482 return array($headers, $rows);
483 }
484 }
485