Merge pull request #7313 from JKingsnorth/CRM-17625
[civicrm-core.git] / tests / phpunit / WebTest / Contact / MergeContactsTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 'CiviTest/CiviSeleniumTestCase.php';
28
29 /**
30 * Class WebTest_Contact_MergeContactsTest
31 */
32 class WebTest_Contact_MergeContactsTest extends CiviSeleniumTestCase {
33
34 protected function setUp() {
35 parent::setUp();
36 }
37
38 public function testIndividualAdd() {
39 $this->markTestSkipped('Skipping for now as it works fine locally.');
40 $this->webtestLogin();
41
42 $this->openCiviPage("contact/add", "reset=1&ct=Individual");
43
44 // add contact1
45 //select prefix
46 $prefix = 'Mr.';
47 $this->click("prefix_id");
48 $this->select("prefix_id", "label=$prefix");
49
50 //fill in first name
51 $firstName = substr(sha1(rand()), 0, 7);
52 $this->type('first_name', $firstName);
53
54 //fill in last name
55 $lastName = substr(sha1(rand()), 0, 7);
56 $this->type('last_name', $lastName);
57
58 //fill in email id
59 $this->type('email_1_email', "{$firstName}.{$lastName}@example.com");
60
61 //fill in billing email id
62 $this->click('addEmail');
63 $this->waitForElementPresent('email_2_email');
64 $this->type('email_2_email', "$firstName.$lastName@billing.com");
65 $this->select('email_2_location_type_id', 'value=5');
66
67 // Clicking save.
68 $this->click("_qf_Contact_upload_view");
69 $this->waitForPageToLoad($this->getTimeoutMsec());
70 $this->waitForText('crm-notification-container', "Contact Saved");
71
72 // Add Contact to a group
73 $group = 'Newsletter Subscribers';
74 $this->click('css=li#tab_group a');
75 $this->waitForElementPresent('_qf_GroupContact_next');
76 $this->select('group_id', "label=$group");
77 $this->click('_qf_GroupContact_next');
78 $this->waitForElementPresent('link=Delete');
79 $this->waitForText('crm-notification-container', "Added to Group");
80
81 // Add Tags to the contact
82 $tag = 'Government Entity';
83 $this->click("css=li#tab_tag a");
84 $this->waitForElementPresent('tagtree');
85 $this->click("xpath=//div[@id='tagtree']/ul//li/input/../span/label[text()='$tag']");
86 $this->click("css=#tab_summary a");
87 $this->assertElementContainsText('css=.crm-summary-block #tags', $tag);
88
89 // Add an activity
90 $subject = "This is subject of test activity being added through activity tab of contact summary screen.";
91 $this->addActivity($firstName, $lastName, $subject);
92
93 // contact2: duplicate of contact1.
94 $this->openCiviPage("contact/add", "reset=1&ct=Individual");
95
96 //fill in first name
97 $this->type("first_name", $firstName);
98
99 //fill in last name
100 $this->type("last_name", $lastName);
101
102 //fill in email
103 $this->type("email_1_email", "{$firstName}.{$lastName}@example.com");
104
105 // Clicking save.
106 $this->click("_qf_Contact_refresh_dedupe");
107 $this->waitForPageToLoad($this->getTimeoutMsec());
108
109 $this->waitForText('crm-notification-container', "One matching contact was found. You can View or Edit the existing contact.");
110 $this->click("_qf_Contact_upload_duplicate");
111 $this->waitForPageToLoad($this->getTimeoutMsec());
112 $this->waitForText('crm-notification-container', "Contact Saved");
113
114 // Add second pair of dupes so we can test Merge and Goto Next Pair
115 $fname2 = 'Janet';
116 $lname2 = 'Rogers' . substr(sha1(rand()), 0, 7);
117 $email2 = "{$fname2}.{$lname2}@example.org";
118 $this->webtestAddContact($fname2, $lname2, $email2);
119
120 // Can not use helper for 2nd contact since it is a dupe
121 $this->openCiviPage("contact/add", "reset=1&ct=Individual");
122 $this->type("first_name", $fname2);
123 $this->type("last_name", $lname2);
124 $this->type("email_1_email", $email2);
125 $this->click("_qf_Contact_refresh_dedupe");
126 $this->waitForPageToLoad($this->getTimeoutMsec());
127 $this->waitForText('crm-notification-container', "One matching contact was found. You can View or Edit the existing contact.");
128 $this->click("_qf_Contact_upload_duplicate");
129 $this->waitForPageToLoad($this->getTimeoutMsec());
130 $this->waitForText('crm-notification-container', "Contact Saved");
131
132 // Find and Merge Contacts with Supervised Rule
133 $this->openCiviPage("contact/dedupefind", "reset=1&rgid=1&action=renew");
134
135 // reload the page
136 $this->openCiviPage("contact/dedupefind", "reset=1&rgid=1&action=update");
137
138 // Select the contacts to be merged
139 $this->waitForElementPresent('dupePairs_length');
140 $this->select("name=dupePairs_length", "value=100");
141 $this->waitForElementPresent("xpath=//table[@id='dupePairs']/tbody//tr/td[5]/a[text()='$firstName $lastName']/../../td[8]/a[text()='merge']");
142 $this->click("xpath=//table[@id='dupePairs']/tbody//tr/td[5]/a[text()='$firstName $lastName']/../../td[8]/a[text()='merge']");
143 $this->waitForElementPresent('_qf_Merge_cancel-bottom');
144
145 $this->clickLink("css=div.crm-contact-merge-form-block div.action-link a", '_qf_Merge_cancel-bottom');
146
147 // Move the activities, groups, etc to the main contact and merge using Merge and Goto Next Pair
148 $this->check('move_prefix_id');
149 $this->check('move_location_email_2');
150 $this->check('move_rel_table_activities');
151 $this->check('move_rel_table_groups');
152 $this->check('move_rel_table_tags');
153 $this->clickLink('_qf_Merge_next-bottom', '_qf_Merge_cancel-bottom');
154 $this->assertTrue($this->isTextPresent('Contacts Merged'), "Contacts Merged text was not found after merge.");
155
156 // Check that we are viewing the next Merge Pair (our 2nd contact, since the merge list is ordered by contact_id)
157 $this->assertTrue($this->isTextPresent("{$fname2} {$lname2}"), "Redirect for Goto Next Pair after merge did not work.");
158
159 // Ensure that the duplicate contact has been deleted
160 $this->openCiviPage("contact/search/advanced", "reset=1", '_qf_Advanced_refresh');
161 $this->type('sort_name', $firstName);
162 $this->click('deleted_contacts');
163 $this->click('_qf_Advanced_refresh');
164 $this->waitForPageToLoad($this->getTimeoutMsec());
165 $this->assertTrue($this->isTextPresent('1 Contact'), "Deletion of duplicate contact during merge was not successful. Dupe contact not found when searching trash.");
166
167 // Search for the main contact
168 $this->openCiviPage("contact/search/advanced", "reset=1", '_qf_Advanced_refresh');
169 $this->type('sort_name', $firstName);
170 $this->click('_qf_Advanced_refresh');
171 $this->waitForElementPresent("xpath=//form[@id='Advanced']/div[3]/div/div[2]/table/tbody/tr");
172
173 $this->click("//form[@id='Advanced']/div[3]/div/div[2]/table/tbody/tr/td[11]/span[1]/a[text()='View']");
174 $this->waitForPageToLoad($this->getTimeoutMsec());
175
176 // Verify prefix merged
177 // $this->verifyText( "xpath=//div[@class='left-corner']/h2", preg_quote( "$prefix $firstName $lastName" ) );
178
179 // Verify billing email merged
180 $this->isElementPresent("xpath=//div[@class='contact_details']/div[1][@class='contact_panel']/div[1][@class='contactCardLeft']/table/tbody/tr[4]/td[2]/span/a[text()='$firstName.$lastName@billing.com']");
181
182 // Verify activity merged
183 $this->click("css=li#tab_activity a");
184 $this->waitForAjaxContent();
185 $this->waitForElementPresent("xpath=//div[@class='dataTables_wrapper no-footer']/table/tbody/tr");
186 $this->verifyText("xpath=//div[@class='dataTables_wrapper no-footer']/table/tbody/tr/td[5]/a", preg_quote("$lastName, $firstName"));
187
188 // Verify group merged
189 $this->click("css=li#tab_group a");
190 $this->waitForElementPresent("GroupContact");
191 $this->waitForElementPresent("xpath=//form[@id='GroupContact']//div[@class='view-content view-contact-groups']//div/table/tbody/tr/td/a");
192 $this->verifyText("xpath=//form[@id='GroupContact']//div[@class='view-content view-contact-groups']//div/table/tbody/tr/td/a",
193 preg_quote("$group")
194 );
195
196 // Verify tag merged
197 $this->click("css=li#tab_tag a");
198 $this->waitForElementPresent('check_5');
199 $this->assertChecked("check_3");
200 }
201
202 public function testMergeContactSubType() {
203 $this->webtestLogin();
204 $this->openCiviPage("contact/add", "reset=1&ct=Individual");
205 $this->waitForElementPresent('_qf_Contact_cancel-bottom');
206 //fill in first name
207 $firstName = "Anderson" . substr(sha1(rand()), 0, 4);
208 $this->type('first_name', $firstName);
209
210 //fill in last name
211 $lastName = substr(sha1(rand()), 0, 4);
212 $this->type('last_name', $lastName);
213
214 //fill in email id
215 $this->waitForElementPresent('email_1_email');
216 $this->type('email_1_email', "{$firstName}.{$lastName}@example.com");
217 $this->waitForElementPresent('contact_sub_type');
218 $this->select('contact_sub_type', "Parent");
219
220 // Clicking save.
221 $this->click("_qf_Contact_upload_view");
222 $this->waitForPageToLoad($this->getTimeoutMsec());
223 $this->waitForText('crm-notification-container', "Contact Saved");
224
225 // contact2: contact with same email id as contact 1.
226 $this->openCiviPage("contact/add", "reset=1&ct=Individual");
227 $this->waitForElementPresent('_qf_Contact_cancel-bottom');
228
229 $fName = "John" . substr(sha1(rand()), 0, 4);
230 $this->type('first_name', $fName);
231 $lName = substr(sha1(rand()), 0, 4);
232 $this->type('last_name', $lName);
233 $this->type('email_1_email', "{$firstName}.{$lastName}@example.com");
234 $this->waitForElementPresent('contact_sub_type');
235 $this->multiselect2('contact_sub_type', array("Student", "Staff"));
236 $this->click("_qf_Contact_upload_view");
237 $this->waitForText('crm-notification-container', "Contact Saved");
238 $this->openCiviPage("contact/deduperules", "reset=1");
239 $this->click("xpath=//*[@id='option12']/tbody/tr[3]/td[3]/span/a[1][contains(text(),'Use Rule')]");
240 $this->waitForElementPresent('_qf_DedupeFind_submit-bottom');
241 $this->click("_qf_DedupeFind_next-bottom");
242
243 $this->waitForElementPresent("xpath=//table[@id='dupePairs']/tbody//tr/td[3]/a[text()='{$firstName} {$lastName}']/../../td[8]/a[2][text()='merge']");
244 $this->waitForElementPresent("xpath=//form[@id='DedupeFind']//a/span[contains(text(),'Done')]");
245 $this->isElementPresent("xpath=//table[@id='dupePairs']/tbody//tr//td/a[text()='$firstName $lastName']/../../td[5]/a[text()='{$fName} {$lName}']/../../td[8]/a[text()='merge']");
246 $this->click("xpath=//table[@id='dupePairs']/tbody//tr/td[3]/a[text()='{$firstName} {$lastName}']/../../td[8]/a[2][text()='merge']");
247 $this->waitForElementPresent('_qf_Merge_cancel-bottom');
248 $this->click('toggleSelect');
249 $this->click('_qf_Merge_next-bottom');
250 $this->waitForPageToLoad($this->getTimeoutMsec());
251 $this->assertTrue($this->isTextPresent("Staff, Student"));
252 }
253
254 /**
255 * @param string $firstName
256 * @param string $lastName
257 * @param $subject
258 */
259 public function addActivity($firstName, $lastName, $subject) {
260 $withContact = substr(sha1(rand()), 0, 7);
261 $this->webtestAddContact($withContact, "Anderson", $withContact . "@anderson.name");
262
263 $this->click("css=li#tab_activity a");
264
265 // waiting for the activity dropdown to show up
266 $this->waitForElementPresent("other_activity");
267
268 // Select the activity type from the activity dropdown
269 $this->select("other_activity", "label=Meeting");
270
271 // waitForPageToLoad is not always reliable. Below, we're waiting for the submit
272 // button at the end of this page to show up, to make sure it's fully loaded.
273 $this->waitForElementPresent("_qf_Activity_upload");
274
275 // ...and verifying if the page contains properly formatted display name for chosen contact.
276 $this->assertTrue($this->isTextPresent("Anderson, " . $withContact), "Contact not found in line " . __LINE__);
277
278 // Now we're filling the "Assigned To" field.
279 // Typing contact's name into the field (using typeKeys(), not type()!)...
280 $this->select2("assignee_contact_id", $firstName, TRUE, FALSE);
281
282 // ...and verifying if the page contains properly formatted display name for chosen contact.
283 $this->assertTrue($this->isTextPresent("$lastName, " . $firstName), "Contact not found in line " . __LINE__);
284
285 // Since we're here, let's check if screen help is being displayed properly
286 $this->assertTrue($this->isTextPresent("A copy of this activity will be emailed to each Assignee."));
287
288 // Putting the contents into subject field - assigning the text to variable, it'll come in handy later
289 // For simple input fields we can use field id as selector
290 $this->type("subject", $subject);
291 $this->type("location", "Some location needs to be put in this field.");
292
293 // Choosing the Date.
294 // Please note that we don't want to put in fixed date, since
295 // we want this test to work in the future and not fail because
296 // of date being set in the past. Therefore, using helper webtestFillDateTime function.
297 $this->webtestFillDateTime('activity_date_time', '+1 month 11:10PM');
298
299 // Setting duration.
300 $this->type("duration", "30");
301
302 // Putting in details.
303 $this->type("details", "Really brief details information.");
304
305 // Making sure that status is set to Scheduled (using value, not label).
306 $this->select("status_id", "value=1");
307
308 // Setting priority.
309 $this->select("priority_id", "value=1");
310
311 // Clicking save.
312 $this->click("_qf_Activity_upload");
313 $this->waitForElementPresent("crm-notification-container");
314
315 // Is status message correct?
316 $this->waitForText('crm-notification-container', "Activity '$subject' has been saved.", "Status message didn't show up after saving!");
317 }
318
319 public function testMergeTest() {
320 $this->webtestLogin();
321
322 $this->openCiviPage("contact/add", "reset=1&ct=Individual");
323
324 // add contact1
325 //fill in first name
326 $firstName = substr(sha1(rand()), 0, 7);
327 $this->type('first_name', $firstName);
328
329 //fill in last name
330 $lastName = substr(sha1(rand()), 0, 7);
331 $this->type('last_name', $lastName);
332
333 //fill in email id
334 $this->type('email_1_email', "{$firstName}.{$lastName}@example.com");
335
336 //fill in billing email id
337 $this->click('addEmail');
338 $this->waitForElementPresent('email_2_email');
339 $this->type('email_2_email', "$firstName.$lastName@billing.com");
340 $this->select('email_2_location_type_id', 'value=5');
341
342 //fill in home phone no
343 $this->type('phone_1_phone', "9876543210");
344
345 //fill in billing phone id
346 $this->click('addPhone');
347 $this->waitForElementPresent('phone_2_phone');
348 $this->type('phone_2_phone', "9876543120");
349 $this->select('phone_2_location_type_id', 'value=5');
350
351 // Clicking save.
352 $this->click("_qf_Contact_upload_view");
353 $this->waitForPageToLoad($this->getTimeoutMsec());
354 $this->waitForText('crm-notification-container', "Contact Saved");
355
356 // contact2: duplicate of contact1.
357 $this->openCiviPage("contact/add", "reset=1&ct=Individual");
358
359 //fill in first name
360 $this->type("first_name", $firstName);
361
362 //fill in last name
363 $this->type("last_name", $lastName);
364
365 //fill in email
366 $this->type("email_1_email", "{$firstName}.{$lastName}@example.com");
367
368 //fill in home phone no
369 $this->type('phone_1_phone', "9876543211");
370
371 // Clicking save.
372 $this->click("_qf_Contact_refresh_dedupe");
373 $this->waitForPageToLoad($this->getTimeoutMsec());
374
375 $this->waitForText('crm-notification-container', "One matching contact was found. You can View or Edit the existing contact.");
376 $this->click("_qf_Contact_upload_duplicate");
377 $this->waitForPageToLoad($this->getTimeoutMsec());
378 $this->waitForText('crm-notification-container', "Contact Saved");
379
380 // Find and Merge Contacts with Supervised Rule
381 $this->openCiviPage("contact/dedupefind", "reset=1&rgid=1&action=renew");
382
383 // Select the contacts to be merged
384 $this->waitForElementPresent('dupePairs_length');
385 $this->waitForElementPresent("xpath=//a[text()='$firstName $lastName']");
386 $this->click("xpath=//a[text()='$firstName $lastName']/../../td[8]/a[text()='merge']");
387 $this->waitForElementPresent('_qf_Merge_cancel-bottom');
388 $this->clickLink("css=div.crm-contact-merge-form-block div.action-link a", "xpath=//form[@id='Merge']/div[2]/table/tbody/tr[3]/td[4]/span[text()='(overwrite)']", FALSE);
389 $this->waitForElementPresent("xpath=//form[@id='Merge']/div[2]/table/tbody/tr[5]/td[4]/span[text()='(add)']");
390 $this->waitForElementPresent('_qf_Merge_cancel-bottom');
391
392 $this->check("move_location_email_1");
393 $this->check("location[email][1][operation]");
394 $this->check("move_location_email_2");
395 $this->check("move_location_phone_1");
396 $this->check("location[phone][1][operation]");
397 $this->check("move_location_phone_2");
398 $this->click("_qf_Merge_next-bottom");
399 $this->waitForPageToLoad($this->getTimeoutMsec());
400
401 $this->assertTrue($this->isTextPresent('Contacts Merged'));
402 $this->assertTrue($this->isElementPresent("xpath=//div[@id='email-block']/div/div/div[2]/div[1][contains(text(), 'Home')]"));
403 $this->assertTrue($this->isElementPresent("xpath=//div[@id='email-block']/div/div/div[2]/div[2]/a[contains(text(), '{$firstName}.{$lastName}@example.com')]"));
404 $this->assertTrue($this->isElementPresent("xpath=//div[@id='email-block']/div/div/div[3]/div[1][contains(text(), 'Home')]"));
405 $this->assertTrue($this->isElementPresent("xpath=//div[@id='email-block']/div/div/div[3]/div[2]/a[contains(text(), '{$firstName}.{$lastName}@example.com')]"));
406 $this->assertTrue($this->isElementPresent("xpath=//div[@id='email-block']/div/div/div[4]/div[1][contains(text(), 'Billing')]"));
407 $this->assertTrue($this->isElementPresent("xpath=//div[@id='email-block']/div/div/div[4]/div[2]/a[contains(text(), '$firstName.$lastName@billing.com')]"));
408 $this->assertTrue($this->isElementPresent("xpath=//div[@id='phone-block']/div/div/div[2]/div[1][contains(text(), 'Home')]"));
409 $this->assertTrue($this->isElementPresent("xpath=//div[@id='phone-block']/div/div/div[2]/div[2][contains(text(), '9876543211')]"));
410 $this->assertTrue($this->isElementPresent("xpath=//div[@id='phone-block']/div/div/div[3]/div[1][contains(text(), 'Home')]"));
411 $this->assertTrue($this->isElementPresent("xpath=//div[@id='phone-block']/div/div/div[3]/div[2][contains(text(), '9876543210')]"));
412 $this->assertTrue($this->isElementPresent("xpath=//div[@id='phone-block']/div/div/div[4]/div[1][contains(text(), 'Billing')]"));
413 $this->assertTrue($this->isElementPresent("xpath=//div[@id='phone-block']/div/div/div[4]/div[2][contains(text(), '9876543120')]"));
414
415 //Merge with the feature of (add)
416 $this->openCiviPage("contact/add", "reset=1&ct=Individual");
417
418 // add contact1
419 //fill in first name
420 $firstName1 = substr(sha1(rand()), 0, 7);
421 $this->type('first_name', $firstName1);
422
423 //fill in last name
424 $lastName1 = substr(sha1(rand()), 0, 7);
425 $this->type('last_name', $lastName1);
426
427 //fill in billing email id
428 $this->waitForElementPresent('email_1_email');
429 $this->type('email_1_email', "$firstName1.$lastName1@example.com");
430 $this->select('email_1_location_type_id', 'value=5');
431
432 $this->click('addEmail');
433 $this->waitForElementPresent('email_2_email');
434 $this->type('email_2_email', "$firstName.$lastName@home.com");
435 $this->select('email_2_location_type_id', 'value=1');
436
437 //fill in home phone no
438 $this->type('phone_1_phone', "9876543210");
439
440 // Clicking save.
441 $this->click("_qf_Contact_upload_view");
442 $this->waitForPageToLoad($this->getTimeoutMsec());
443 $this->waitForText('crm-notification-container', "Contact Saved");
444
445 // contact2: duplicate of contact1.
446 $this->openCiviPage("contact/add", "reset=1&ct=Individual");
447
448 //fill in first name
449 $this->type("first_name", $firstName1);
450
451 //fill in last name
452 $this->type("last_name", $lastName1);
453
454 //fill in email
455 $this->type("email_1_email", "{$firstName1}.{$lastName1}@example.com");
456
457 //fill in billing phone no
458 $this->type('phone_1_phone', "9876543120");
459 $this->select('phone_1_location_type_id', 'value=5');
460
461 // Clicking save.
462 $this->click("_qf_Contact_refresh_dedupe");
463 $this->waitForPageToLoad($this->getTimeoutMsec());
464 $this->waitForText('crm-notification-container', "One matching contact was found. You can View or Edit the existing contact.");
465 $this->click("_qf_Contact_upload_duplicate");
466 $this->waitForPageToLoad($this->getTimeoutMsec());
467 $this->waitForText('crm-notification-container', "Contact Saved");
468
469 // Find and Merge Contacts with Supervised Rule
470 $this->openCiviPage("contact/dedupefind", "reset=1&rgid=1&action=renew");
471
472 // Select the contacts to be merged
473 $this->waitForElementPresent('dupePairs_length');
474 $this->waitForElementPresent("xpath=//a[text()='$firstName1 $lastName1']");
475 $this->click("xpath=//a[text()='$firstName1 $lastName1']/../../td[8]/a[text()='merge']");
476 $this->waitForElementPresent('_qf_Merge_cancel-bottom');
477 $this->clickLink("css=div.crm-contact-merge-form-block div.action-link a", "xpath=//form[@id='Merge']/div[2]/table/tbody/tr[4]/td[4]/span[text()='(overwrite)']");
478 $this->waitForElementPresent("xpath=//form[@id='Merge']/div[2]/table/tbody/tr[3]/td[4]/span[text()='(add)']");
479 $this->waitForElementPresent("xpath=//form[@id='Merge']/div[2]/table/tbody/tr[4]/td[4]/span[text()='(overwrite)']");
480 $this->select('location_email_1_locTypeId', 'value=3');
481 $this->select('location_phone_1_locTypeId', 'value=1');
482 $this->assertFalse($this->isElementPresent("xpath=//form[@id='Merge']/div[2]/table/tbody/tr[2]/td[4]/span[text()='(overwrite)']"));
483 $this->assertFalse($this->isElementPresent("xpath=//form[@id='Merge']/div[2]/table/tbody/tr[4]/td[4]/span[text()='(overwrite)']"));
484 $this->assertTrue($this->isElementPresent("xpath=//form[@id='Merge']/div[2]/table/tbody/tr[3]/td[4]/span[text()='(add)']"));
485 $this->waitForElementPresent('_qf_Merge_cancel-bottom');
486
487 $this->check("move_location_email_1");
488 $this->check("move_location_phone_1");
489 $this->click("_qf_Merge_next-bottom");
490 $this->waitForPageToLoad($this->getTimeoutMsec());
491
492 $this->assertTrue($this->isTextPresent('Contacts Merged'));
493 $this->assertTrue($this->isElementPresent("xpath=//div[@id='email-block']/div/div/div[2]/div[1][contains(text(), 'Home')]"));
494 $this->assertTrue($this->isElementPresent("xpath=//div[@id='email-block']/div/div/div[2]/div[2]/a[contains(text(), '{$firstName1}.{$lastName1}@example.com')]"));
495 $this->assertTrue($this->isElementPresent("xpath=//div[@id='email-block']/div/div/div[3]/div[1][contains(text(), 'Main')]"));
496 $this->assertTrue($this->isElementPresent("xpath=//div[@id='email-block']/div/div/div[3]/div[2]/a[contains(text(), '{$firstName1}.{$lastName1}@example.com')]"));
497 $this->assertTrue($this->isElementPresent("xpath=//div[@id='phone-block']/div/div/div[2]/div[1][contains(text(), 'Billing')]"));
498 $this->assertTrue($this->isElementPresent("xpath=//div[@id='phone-block']/div/div/div[2]/div[2][contains(text(), '9876543120')]"));
499 $this->assertTrue($this->isElementPresent("xpath=//div[@id='phone-block']/div/div/div[3]/div[1][contains(text(), 'Home')]"));
500 $this->assertTrue($this->isElementPresent("xpath=//div[@id='phone-block']/div/div/div[3]/div[2][contains(text(), '9876543210')]"));
501 }
502
503 public function testBatchMerge() {
504 $this->webtestLogin();
505
506 // add contact1 and its duplicate
507 //first name
508 $firstName = "Kerry" . substr(sha1(rand()), 0, 7);
509 //last name
510 $lastName = "King" . substr(sha1(rand()), 0, 7);
511 $this->_createContacts($firstName, $lastName);
512
513 //add contact2 and its duplicate
514 //These are the contacts with conflicts in communication preference.these contacts will be skipped during merge.
515 $this->openCiviPage("contact/add", "reset=1&ct=Individual");
516
517 //fill in first name
518 $firstName1 = "Kurt" . substr(sha1(rand()), 0, 7);
519 $this->type('first_name', $firstName1);
520
521 //fill in last name
522 $lastName1 = "Cobain" . substr(sha1(rand()), 0, 7);
523 $this->type('last_name', $lastName1);
524
525 //fill in email id
526 $this->type('email_1_email', "{$firstName1}.{$lastName1}@example.com");
527
528 //fill in billing email id
529 $this->click('addEmail');
530 $this->waitForElementPresent('email_2_email');
531 $this->type('email_2_email', "$firstName1.$lastName1@billing.com");
532 $this->select('email_2_location_type_id', 'value=5');
533
534 //fill in home phone no
535 $this->type('phone_1_phone', "9876543210");
536
537 //fill in billing phone id
538 $this->click('addPhone');
539 $this->waitForElementPresent('phone_2_phone');
540 $this->type('phone_2_phone', "9876543120");
541 $this->select('phone_2_location_type_id', 'value=5');
542
543 //select communication preference
544 $this->check("privacy[do_not_phone]");
545
546 //Clicking save.
547 $this->click("_qf_Contact_upload_view");
548 $this->waitForPageToLoad($this->getTimeoutMsec());
549 $this->waitForText('crm-notification-container', "Contact Saved");
550
551 //duplicate of contact2.
552 $this->openCiviPage("contact/add", "reset=1&ct=Individual");
553
554 //fill in first name
555 $this->type("first_name", $firstName1);
556
557 //fill in last name
558 $this->type("last_name", $lastName1);
559
560 //fill in email
561 $this->type("email_1_email", "{$firstName1}.{$lastName1}@example.com");
562
563 //fill in home phone no
564 $this->type('phone_1_phone', "9876543211");
565
566 //select communication preference
567 $this->check("preferred_communication_method[1]");
568
569 // Clicking save.
570 $this->click("_qf_Contact_refresh_dedupe");
571 $this->waitForPageToLoad($this->getTimeoutMsec());
572
573 $this->waitForText('crm-notification-container', "One matching contact was found. You can View or Edit the existing contact.");
574 $this->click("_qf_Contact_upload_duplicate");
575 $this->waitForPageToLoad($this->getTimeoutMsec());
576 $this->waitForText('crm-notification-container', "Contact Saved");
577
578 // add contact3 and its duplicate
579 //fill in first name
580 $firstName2 = "David" . substr(sha1(rand()), 0, 7);
581 //fill in last name
582 $lastName2 = "Gilmour" . substr(sha1(rand()), 0, 7);
583 $this->_createContacts($firstName2, $lastName2);
584
585 // add contact4 and its duplicate
586 //fill in first name
587 $firstName3 = "Dave" . substr(sha1(rand()), 0, 7);
588 //fill in last name
589 $lastName3 = "Mustaine" . substr(sha1(rand()), 0, 7);
590 $this->_createContacts($firstName3, $lastName3);
591
592 // Find and Merge Contacts with Supervised Rule
593 $this->openCiviPage("contact/dedupefind", "reset=1&rgid=1&action=renew", "dupePairs");
594
595 $this->waitForElementPresent('dupePairs_length');
596 $this->select("name=dupePairs_length", "value=100");
597 $totalContacts = $this->getXpathCount("//table[@id='dupePairs']/tbody/tr");
598 $this->click("//form[@id='DedupeFind']//a/span[contains(text(),' Batch Merge All Duplicates')]");
599
600 // Check confirmation alert.
601 $this->assertTrue(
602 (bool) preg_match("/^This will run the batch merge process on the listed duplicates. The operation will run in safe mode - only records with no direct data conflicts will be merged. Click OK to proceed if you are sure you wish to run this operation./",
603 $this->getConfirmation()
604 )
605 );
606 $this->chooseOkOnNextConfirmation();
607 $this->waitForPageToLoad($this->getTimeoutMsec());
608 $this->waitForElementPresent('civicrm-footer');
609 $this->waitForElementPresent("crm-main-content-wrapper");
610
611 // If we are still on the dedupe table page, count unmerged contacts
612 if ($this->isElementPresent("//table[@class='pagerDisplay']")) {
613 // Wait for datatable to load
614 $this->waitForElementPresent("//table[@class='pagerDisplay']/tbody/tr");
615 $unMergedContacts = $this->getXpathCount("//table[@class='pagerDisplay']/tbody/tr");
616 }
617 else {
618 $unMergedContacts = 0;
619 }
620
621 $mergedContacts = $totalContacts - $unMergedContacts;
622
623 //check the existence of merged contacts
624 $contactEmails = array(
625 1 => "{$firstName}.{$lastName}@example.com",
626 2 => "{$firstName2}.{$lastName2}@example.com",
627 3 => "{$firstName3}.{$lastName3}@example.com",
628 );
629
630 foreach ($contactEmails as $key => $value) {
631 $this->click('sort_name_navigation');
632 $this->type('css=input#sort_name_navigation', $value);
633 $this->typeKeys('css=input#sort_name_navigation', $value);
634 // Wait for result list.
635 $this->waitForElementPresent("css=ul.ui-autocomplete li.ui-menu-item");
636
637 // Visit contact summary page.
638 $this->clickLink("css=ul.ui-autocomplete li.ui-menu-item", 'civicrm-footer');
639 }
640 }
641
642 /**
643 * Helper FN.
644 * @param null $firstName
645 * @param null $lastName
646 * @param null $organizationName
647 * @param string $contactType
648 * @return array
649 */
650 public function _createContacts($firstName = NULL, $lastName = NULL, $organizationName = NULL, $contactType = 'Individual') {
651 if ($contactType == 'Individual') {
652 // add contact
653 $this->openCiviPage("contact/add", "reset=1&ct=Individual");
654 //fill in first name
655 $this->type('first_name', $firstName);
656
657 //fill in last name
658 $this->type('last_name', $lastName);
659
660 //fill in email id
661 $this->type('email_1_email', "{$firstName}.{$lastName}@example.com");
662
663 //fill in billing email id
664 $this->click('addEmail');
665 $this->waitForElementPresent('email_2_email');
666 $this->type('email_2_email', "$firstName.$lastName@billing.com");
667 $this->select('email_2_location_type_id', 'value=5');
668
669 // Clicking save.
670 $this->click("_qf_Contact_upload_view");
671 $this->waitForPageToLoad($this->getTimeoutMsec());
672 $this->waitForText('crm-notification-container', "Contact Saved");
673
674 //duplicate of above contact.
675 $this->openCiviPage("contact/add", "reset=1&ct=Individual");
676
677 //fill in first name
678 $this->type("first_name", $firstName);
679
680 //fill in last name
681 $this->type("last_name", $lastName);
682
683 //fill in email
684 $this->type("email_1_email", "{$firstName}.{$lastName}@example.com");
685
686 // Clicking save.
687 $this->click("_qf_Contact_refresh_dedupe");
688 $this->waitForPageToLoad($this->getTimeoutMsec());
689
690 $this->waitForText('crm-notification-container', "One matching contact was found. You can View or Edit the existing contact.");
691 $this->click("_qf_Contact_upload_duplicate");
692 $this->waitForPageToLoad($this->getTimeoutMsec());
693 $this->waitForText('crm-notification-container', "Contact Saved");
694 }
695 elseif ($contactType == 'Organization') {
696 // add contact
697 $this->openCiviPage("contact/add", "reset=1&ct=Organization");
698 //fill in Organization name
699 $this->type('organization_name', $organizationName);
700
701 //fill in email id
702 $this->type('email_1_email', "{$organizationName}@org.com");
703 // Clicking save.
704 $this->click("_qf_Contact_upload_view-bottom");
705 $this->waitForPageToLoad($this->getTimeoutMsec());
706 $this->waitForText('crm-notification-container', "Contact Saved");
707 $mainId = explode("Contact ID:", trim($this->getText("xpath=//div[@id='crm-record-log']/span[@class='col1']")));
708 $mainId = trim($mainId[1]);
709
710 //Duplicate of above contact.
711 $this->openCiviPage("contact/add", "reset=1&ct=Organization");
712
713 //fill in Organization name
714 $this->type('organization_name', $organizationName);
715
716 //fill in email id
717 $this->type('email_1_email', "{$organizationName}@org.com");
718
719 // Clicking save.
720 $this->click("_qf_Contact_upload_view-bottom");
721 $this->waitForPageToLoad($this->getTimeoutMsec());
722
723 $this->waitForText('crm-notification-container', "One matching contact was found. You can View or Edit the existing contact.");
724 $this->click("_qf_Contact_upload_duplicate");
725 $this->waitForPageToLoad($this->getTimeoutMsec());
726 $this->waitForText('crm-notification-container', "Contact Saved");
727 $duplicateId = explode("Contact ID:", trim($this->getText("xpath=//div[@id='crm-record-log']/span[@class='col1']")));
728 $duplicateId = trim($duplicateId[1]);
729
730 return array(
731 'mainId' => $mainId,
732 'duplicateId' => $duplicateId,
733 );
734 }
735 }
736
737 /**
738 * Helper FN.
739 * to create new membership type
740 * @param $membershipOrganization
741 */
742 public function addMembershipType($membershipOrganization) {
743 $this->openCiviPage("admin/member/membershipType", "reset=1&action=browse");
744 $this->click("link=Add Membership Type");
745 $this->waitForElementPresent('_qf_MembershipType_cancel-bottom');
746
747 $this->type('name', "Membership Type $membershipOrganization");
748 $this->select2('member_of_contact_id', $membershipOrganization);
749
750 $this->type('minimum_fee', '1');
751 $this->select('financial_type_id', 'value=2');
752 $this->type('duration_interval', 1);
753 $this->select('duration_unit', "label=year");
754
755 $this->select('period_type', "label=Fixed");
756 $this->waitForElementPresent('fixed_period_rollover_day[d]');
757
758 // fixed period start set to April 1
759 $this->select('fixed_period_start_day[M]', 'value=4');
760 // rollover date set to Jan 31
761 $this->select('fixed_period_rollover_day[M]', 'value=1');
762
763 // Employer of relationship
764 $this->select('relationship_type_id', 'value=5_b_a');
765 $this->click('_qf_MembershipType_upload-bottom');
766 $this->waitForElementPresent('link=Add Membership Type');
767 $this->waitForText("crm-notification-container", "The membership type 'Membership Type $membershipOrganization' has been saved.");
768 }
769
770 /**
771 * Test for CRM-12695 fix
772 */
773 public function testMergeOrganizations() {
774 $this->webtestLogin();
775
776 // build organisation name
777 $orgnaizationName = 'org_' . substr(sha1(rand()), 0, 7);
778
779 $contactIds = array();
780 // create organization and its duplicate
781 $contactIds = $this->_createContacts(NULL, NULL, $orgnaizationName, 'Organization');
782
783 /*** Add Membership Type - start ***/
784 $this->addMembershipType($orgnaizationName);
785 /*** Add Membership Type - end ***/
786
787 //create a New Individual to be related to main organization
788 $firstName = substr(sha1(rand()), 0, 7);
789 $this->webtestAddContact($firstName, "Anderson", "$firstName@anderson.name");
790 $sortName = "Anderson, $firstName";
791
792 // go to main organization contact to add membership
793 $this->openCiviPage("contact/view", "reset=1&cid={$contactIds['mainId']}");
794 // click through to the membership view screen
795 $this->click("css=li#tab_member a");
796
797 $this->waitForElementPresent("link=Add Membership");
798 $this->click("link=Add Membership");
799
800 $this->waitForElementPresent("_qf_Membership_cancel-bottom");
801
802 // fill in Membership Organization and Type
803 $this->select("membership_type_id[0]", "label={$orgnaizationName}");
804 $this->waitForElementPresent("membership_type_id[1]");
805 // Wait for membership type select to reload
806 $this->waitForTextPresent("Membership Type $orgnaizationName");
807 $this->select("membership_type_id[1]", "label=Membership Type $orgnaizationName");
808
809 $sourceText = "Membership-Organization Duplicate Merge Webtest";
810 // fill in Source
811 $this->type("source", $sourceText);
812
813 // Let Join Date stay default
814
815 // fill in Start Date
816 $this->webtestFillDate('start_date');
817
818 // Clicking save.
819 $this->click("_qf_Membership_upload");
820 $this->waitForElementPresent('crm-notification-container');
821 // page was loaded
822 $this->waitForTextPresent($sourceText);
823 // Is status message correct?
824 $this->waitForText('crm-notification-container', "membership for $orgnaizationName has been added.");
825
826 // add relationship "Employer of"
827 // click through to the relationship view screen
828 $this->click("css=li#tab_rel a");
829
830 // wait for add Relationship link
831 $this->waitForElementPresent('link=Add Relationship');
832 $this->click('link=Add Relationship');
833
834 //choose the created relationship type
835 $this->waitForElementPresent("relationship_type_id");
836 $this->select('relationship_type_id', "value=5_b_a");
837
838 //fill in the individual
839 $this->waitForElementPresent('related_contact_id');
840 $this->select2('related_contact_id', $sortName, TRUE, FALSE);
841
842 $this->waitForElementPresent("_qf_Relationship_upload");
843
844 //fill in the relationship start date
845 //$this->webtestFillDate('start_date', '-2 year');
846 //$this->webtestFillDate('end_date', '+1 year');
847
848 $description = "Well here is some description !!!!";
849 $this->type("description", $description);
850
851 //save the relationship
852 $this->click("_qf_Relationship_upload");
853 $this->isTextPresent("Current Relationships");
854
855 //check the status message
856 $this->waitForText('crm-notification-container', "Relationship created.");
857 $this->waitForElementPresent("xpath=//div[@class='dataTables_wrapper no-footer']/table/tbody/tr/td[9]/span/a[text()='View']");
858 $this->waitForAjaxContent();
859 $this->click("xpath=//a[text()='$sortName']");
860 $this->waitForAjaxContent();
861
862 // Check if Membership for the individual is created
863 $this->waitForElementPresent("xpath=//li[@id='tab_member']/a/em");
864 $this->verifyText("xpath=//li[@id='tab_member']/a/em", 1);
865
866 //create a New Individual to be related to duplicate organization
867 $firstNameOther = substr(sha1(rand()), 0, 7);
868 $this->webtestAddContact($firstNameOther, "Harmison", "$firstNameOther@harmison.name");
869 $sortNameOther = "Harmison, $firstNameOther";
870
871 // go to main organization contact to add membership
872 $this->openCiviPage("contact/view", "reset=1&cid={$contactIds['duplicateId']}");
873
874 // add relationship "Employer of"
875 // click through to the relationship view screen
876 $this->click("css=li#tab_rel a");
877
878 // wait for add Relationship link
879 $this->waitForElementPresent('link=Add Relationship');
880 $this->click('link=Add Relationship');
881
882 //choose the created relationship type
883 $this->waitForElementPresent("relationship_type_id");
884 $this->select('relationship_type_id', "value=5_b_a");
885
886 //fill in the individual
887 $this->waitForElementPresent('related_contact_id');
888 $this->select2('related_contact_id', $sortNameOther, TRUE, FALSE);
889
890 $this->waitForElementPresent("_qf_Relationship_upload");
891
892 //fill in the relationship start date
893 $this->webtestFillDate('start_date', '-2 year');
894 $this->webtestFillDate('end_date', '+1 year');
895
896 $description = "Well here is some description !!!!";
897 $this->type("description", $description);
898
899 //save the relationship
900 //$this->click("_qf_Relationship_upload");
901 $this->click("_qf_Relationship_upload");
902 $this->isTextPresent("Current Relationships");
903
904 //check the status message
905 $this->isTextPresent("Relationship created.");
906
907 $this->waitForElementPresent("xpath=//div[@class='dataTables_wrapper no-footer']/table/tbody/tr/td[9]/span/a[text()='View']");
908
909 // go directly to contact merge page.
910 $this->openCiviPage("contact/merge", "reset=1&cid={$contactIds['mainId']}&oid={$contactIds['duplicateId']}&action=update&rgid=2");
911
912 $this->waitForElementPresent('_qf_Merge_cancel-bottom');
913 $this->click('_qf_Merge_next-bottom');
914 $this->waitForPageToLoad($this->getTimeoutMsec());
915
916 // click through to the relationship view screen
917 $this->click("css=li#tab_rel a");
918
919 // wait for add Relationship link
920 $this->waitForElementPresent("xpath=//div[@class='dataTables_wrapper no-footer']/table/tbody//tr//td//a[text()='$sortName']");
921 // go to duplicate organization's related contact
922 // to check if membership is added to that contact
923 $this->click("xpath=//div[@class='dataTables_wrapper no-footer']/table/tbody//tr//td//a[text()='$sortName']");
924 $this->waitForPageToLoad($this->getTimeoutMsec());
925
926 // Check if Membership for the individual is created
927 $this->waitForElementPresent("xpath=//li[@id='tab_member']/a/em");
928 $this->verifyText("xpath=//li[@id='tab_member']/a/em", 0);
929 }
930
931 /**
932 * Test for CRM-15658 fix
933 */
934 public function testMergeEmailAndAddress() {
935 $this->webtestLogin();
936 $this->openCiviPage("contact/add", "reset=1&ct=Individual");
937 $firstName = substr(sha1(rand()), 0, 7);
938 $this->type('first_name', $firstName);
939
940 //fill in last name
941 $lastName = substr(sha1(rand()), 0, 7);
942 $this->type('last_name', $lastName);
943
944 //fill in email id
945 $this->type('email_1_email', "{$firstName}.{$lastName}@example.com");
946
947 //address section
948 $this->click("addressBlock");
949 $this->waitForElementPresent("address_1_street_address");
950 $this->type("address_1_street_address", "902C El Camino Way SW");
951 $this->type("address_1_city", "Dumfries");
952 $this->type("address_1_postal_code", "1234");
953
954 // Clicking save.
955 $this->click("_qf_Contact_upload_view");
956 $this->waitForPageToLoad($this->getTimeoutMsec());
957 $this->waitForText('crm-notification-container', "Contact Saved");
958
959 //duplicate contact with same email id
960 $this->openCiviPage("contact/add", "reset=1&ct=Individual");
961 $firstName2 = substr(sha1(rand()), 0, 7);
962 $this->type('first_name', $firstName2);
963
964 //fill in last name
965 $lastName2 = substr(sha1(rand()), 0, 7);
966 $this->type('last_name', $lastName2);
967
968 //fill in email id
969 $this->type('email_1_email', "{$firstName}.{$lastName}@example.com");
970
971 //address section
972 $this->click("addressBlock");
973 $this->waitForElementPresent("address_1_street_address");
974 $this->type("address_1_street_address", "2782Y Dowlen Path W");
975 $this->type("address_1_city", "Birmingham");
976 $this->type("address_1_postal_code", "3456");
977
978 // Clicking save.
979 $this->click("_qf_Contact_upload_view");
980 $this->waitForPageToLoad($this->getTimeoutMsec());
981 $this->waitForText('crm-notification-container', "Contact Saved");
982
983 $this->openCiviPage("contact/dedupefind", "reset=1&action=update&rgid=4");
984 $this->click("//a/span[contains(text(),'Refresh Duplicates')]");
985 $this->assertTrue((bool) preg_match("/This will refresh the duplicates list. Click OK to proceed./", $this->getConfirmation()));
986 $this->chooseOkOnNextConfirmation();
987 $this->waitForPageToLoad($this->getTimeoutMsec());
988 $this->waitForElementPresent("xpath=//table[@id='dupePairs']/tbody//tr/td[3]/a[text()='$firstName $lastName']/../../td[8]//a[text()='merge']");
989 $this->click("xpath=//table[@id='dupePairs']/tbody//tr/td[3]/a[text()='$firstName $lastName']/../../td[8]//a[text()='merge']");
990 $this->waitForPageToLoad($this->getTimeoutMsec());
991
992 //merge without specifying any criteria
993 $this->click("_qf_Merge_next-bottom");
994 $this->waitForPageToLoad($this->getTimeoutMsec());
995
996 $this->assertTrue($this->isTextPresent('Contacts Merged'));
997 $this->assertTrue($this->isElementPresent("xpath=//div[@id='email-block']/div/div/div[2]/div[1][contains(text(), 'Home')]"));
998 $this->verifyElementNotPresent("xpath=//div[@id='email-block']/div/div/div[3]/div[1][contains(text(), 'Home')]");
999 $this->assertTrue($this->isElementPresent("xpath=//div[@id='email-block']/div/div/div[2]/div[2]/a[contains(text(), '{$firstName}.{$lastName}@example.com')]"));
1000 $this->verifyElementNotPresent("xpath=//div[@id='email-block']/div/div/div[3]/div[2]/a[contains(text(), '{$firstName}.{$lastName}@example.com')]");
1001
1002 $this->assertElementContainsText('address-block-1', "902C El Camino Way SW");
1003 $this->assertElementContainsText('address-block-1', "Dumfries");
1004 $this->assertElementContainsText('address-block-1', "1234");
1005
1006 $this->assertElementNotContainsText("address-block-2", "2782Y Dowlen Path W");
1007 $this->assertElementNotContainsText("address-block-2", "Birmingham");
1008 $this->assertElementNotContainsText("address-block-2", "3456");
1009 }
1010
1011 }