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