Merge pull request #4014 from jitendrapurohit/WebtestFix4.5.0
[civicrm-core.git] / tests / phpunit / WebTest / Export / ContactTest.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/Export/ExportCiviSeleniumTestCase.php';
28
29 /**
30 * Class WebTest_Export_ContactTest
31 */
32 class WebTest_Export_ContactTest extends ExportCiviSeleniumTestCase {
33
34 protected function setUp() {
35 parent::setUp();
36 }
37
38 function testPrefixGenderSuffix(){
39 $this->webtestLogin();
40
41 // Create new group
42 $parentGroupName = 'TestSuffixPrefixGender_' . substr(sha1(rand()), 0, 7);
43 $this->addContactGroup($parentGroupName);
44
45 // Adding Parent group contact
46 // We're using Quick Add block on the main page for this.
47 $firstContactName = 'TestExport' . substr(sha1(rand()), 0, 7);
48
49 list($emailContactFirst,$prefixLabelContactFrst,$suffixLabelContactFrst,$genderLabelContactFrst) = WebTest_Export_ContactTest::webtestAddContactWithGenderPrefixSuffix($firstContactName, "Smith", "$firstContactName.smith@example.org", Null);
50
51 $sortFirstName = "Smith, $firstContactName";
52 $displayFirstName = "$firstContactName Smith";
53
54 // Add contact to parent group
55 // visit group tab.
56 $this->click("css=li#tab_group a");
57 $this->waitForElementPresent("group_id");
58
59 // Add to group.
60 $this->select("group_id", "label=$parentGroupName");
61 $this->click("_qf_GroupContact_next");
62 $this->waitForElementPresent("//*[@id='GroupContact']");
63
64 $secondContactName = 'TestExport2' . substr(sha1(rand()), 0, 7);
65 list($emailContactSecond,$prefixLabelContactScnd,$suffixLabelContactScnd,$genderLabelContactScnd) = WebTest_Export_ContactTest::webtestAddContactWithGenderPrefixSuffix($secondContactName, "John", "$secondContactName.john@example.org", Null);
66
67 $sortSecondName = "John, $secondContactName";
68 $displaySecondName = "$secondContactName John";
69
70 // Add contact to parent group
71 // visit group tab.
72 $this->click("css=li#tab_group a");
73 $this->waitForElementPresent("group_id");
74
75 // Add to group.
76 $this->select("group_id", "label=$parentGroupName");
77 $this->click("_qf_GroupContact_next");
78 $this->waitForElementPresent("//*[@id='GroupContact']");
79
80 $this->openCiviPage("contact/search", "reset=1");
81
82 // Select contact type as Indiividual.
83 $this->select("contact_type", "value=Individual");
84
85 // Select group.
86 $this->select("group", "label=$parentGroupName");
87
88 // Click to search.
89 $this->click("_qf_Basic_refresh");
90 $this->waitForPageToLoad($this->getTimeoutMsec());
91
92 // Is contact present in search result?
93 $this->assertElementContainsText('css=div.crm-search-results', $sortFirstName, "Contact did not found in search result!");
94
95 // Is contact present in search result?
96 $this->assertElementContainsText('css=div.crm-search-results', $sortSecondName, "Contact did not found in search result!");
97
98 // select to export all the contasct from search result.
99 $this->click("CIVICRM_QFID_ts_all_4");
100
101 // Select the task action to export.
102 $this->click("task");
103 $this->select("task", "label=Export Contacts");
104 $this->waitForPageToLoad($this->getTimeoutMsec());
105
106 $csvFile = $this->downloadCSV("_qf_Select_next-bottom");
107
108 // Build header row for assertion.
109 require_once 'CRM/Contact/BAO/Contact.php';
110 $expotableFields = CRM_Contact_BAO_Contact::exportableFields('All', FALSE, TRUE);
111
112 $checkHeaders = array();
113 foreach ($expotableFields as $key => $field) {
114 // Exclude custom fields.
115 if ($key && (substr($key, 0, 6) == 'custom')) {
116 continue;
117 }
118 $checkHeaders[] = $field['title'];
119 }
120
121 // All other rows to be check.
122 $checkRows = array(
123 1 => array(
124 'First Name' => $firstContactName,
125 'Last Name' => 'Smith',
126 'Email' => ''.strtolower($emailContactFirst).'',
127 'Sort Name' => $sortFirstName,
128 'Display Name' => $prefixLabelContactFrst.' '.$displayFirstName.' '.$suffixLabelContactFrst,
129 'Individual Prefix' => $prefixLabelContactFrst,
130 'Individual Suffix' => $suffixLabelContactFrst,
131 'Gender' => $genderLabelContactFrst,
132 ),
133 2 => array(
134 'First Name' => $secondContactName,
135 'Last Name' => 'John',
136 'Email' => ''.strtolower($emailContactSecond).'',
137 'Sort Name' => $sortSecondName,
138 'Display Name' => $prefixLabelContactScnd.' '.$displaySecondName.' '.$suffixLabelContactScnd,
139 'Individual Prefix' => $prefixLabelContactScnd,
140 'Individual Suffix' => $suffixLabelContactScnd,
141 'Gender' => $genderLabelContactScnd,
142 ),
143 );
144
145 // Read CSV and fire assertions.
146 $this->reviewCSV($csvFile, $checkHeaders, $checkRows, 2);
147 }
148
149 /**
150 * Test Contact Export.
151 */
152 function testContactExport() {
153 $this->webtestLogin();
154
155 // Create new group
156 $parentGroupName = 'Parentgroup_' . substr(sha1(rand()), 0, 7);
157 $this->addContactGroup($parentGroupName);
158
159 // Create new group and select the previously selected group as parent group for this new group.
160 $childGroupName = 'Childgroup_' . substr(sha1(rand()), 0, 7);
161 $this->addContactGroup($childGroupName, $parentGroupName);
162
163 // Adding Parent group contact
164 // We're using Quick Add block on the main page for this.
165 $firstName = 'a' . substr(sha1(rand()), 0, 7);
166 $this->webtestAddContact($firstName, "Smith", "$firstName.smith@example.org");
167
168 $sortName = "Smith, $firstName";
169 $displayName = "$firstName Smith";
170
171 // Add contact to parent group
172 // visit group tab.
173 $this->click("css=li#tab_group a");
174 $this->waitForElementPresent("group_id");
175
176 // Add to group.
177 $this->select("group_id", "label=$parentGroupName");
178 $this->click("_qf_GroupContact_next");
179 $this->waitForElementPresent("//*[@id='GroupContact']");
180
181 // Adding child group contact
182 // We're using Quick Add block on the main page for this.
183 $childName = 'b' . substr(sha1(rand()), 0, 7);
184 $this->webtestAddContact($childName, "John", "$childName.john@example.org");
185
186 $childSortName = "John, $childName";
187 $childDisplayName = "$childName John";
188
189 // Add contact to child group
190 // visit group tab.
191 $this->click("css=li#tab_group a");
192 $this->waitForElementPresent("group_id");
193
194 // Add to child group.
195 $this->select("group_id", "label=regexp:$childGroupName");
196 $this->click("_qf_GroupContact_next");
197 $this->waitForElementPresent("//*[@id='GroupContact']");
198
199 // Visit contact search page.
200 $this->openCiviPage("contact/search", "reset=1");
201
202 // Select contact type as Indiividual.
203 $this->select("contact_type", "value=Individual");
204
205 // Select group.
206 $this->select("group", "label=$parentGroupName");
207
208 // Click to search.
209 $this->click("_qf_Basic_refresh");
210 $this->waitForPageToLoad($this->getTimeoutMsec());
211
212 // Is contact present in search result?
213 $this->assertElementContainsText('css=div.crm-search-results', $sortName, "Contact did not found in search result!");
214
215 // Is contact present in search result?
216 $this->assertElementContainsText('css=div.crm-search-results', $childSortName, "Contact did not found in search result!");
217
218 // select to export all the contasct from search result.
219 $this->click("CIVICRM_QFID_ts_all_4");
220
221 // Select the task action to export.
222 $this->click("task");
223 $this->select("task", "label=Export Contacts");
224 $this->waitForPageToLoad($this->getTimeoutMsec());
225
226 $csvFile = $this->downloadCSV("_qf_Select_next-bottom");
227
228 // Build header row for assertion.
229 require_once 'CRM/Contact/BAO/Contact.php';
230 $expotableFields = CRM_Contact_BAO_Contact::exportableFields('All', FALSE, TRUE);
231
232 $checkHeaders = array();
233 foreach ($expotableFields as $key => $field) {
234 // Exclude custom fields.
235 if ($key && (substr($key, 0, 6) == 'custom')) {
236 continue;
237 }
238 $checkHeaders[] = $field['title'];
239 }
240
241 // All other rows to be check.
242 $checkRows = array(
243 1 => array(
244 'First Name' => $firstName,
245 'Last Name' => 'Smith',
246 'Email' => "$firstName.smith@example.org",
247 'Sort Name' => $sortName,
248 'Display Name' => $displayName,
249 ),
250 2 => array(
251 'First Name' => $childName,
252 'Last Name' => 'John',
253 'Email' => "$childName.john@example.org",
254 'Sort Name' => $childSortName,
255 'Display Name' => $childDisplayName,
256 ),
257 );
258
259 // Read CSV and fire assertions.
260 $this->reviewCSV($csvFile, $checkHeaders, $checkRows, 2);
261 }
262
263 function testMergeHousehold() {
264 $this->webtestLogin();
265
266 // Create new group
267 $groupName = 'TestGroup_' . substr(sha1(rand()), 0, 7);
268 $this->addContactGroup($groupName);
269
270 // Adding Parent group contact
271 // We're using Quick Add block on the main page for this.
272 $houseHold = 'H' . substr(sha1(rand()), 0, 5) . ' House';
273
274 $this->openCiviPage("contact/add", "reset=1&ct=Household");
275 $this->click('household_name');
276 $this->type('household_name', $houseHold);
277
278 //address section
279 $this->click("addressBlock");
280 $this->waitForElementPresent("address_1_street_address");
281
282 // fill in address
283 $this->click("//div[@id='addressBlockId']/div[1]");
284 $this->type("address_1_street_address", "121A Sherman St. Apt. 12");
285 $this->type("address_1_city", "Dumfries");
286 $this->type("address_1_postal_code", "1234");
287 $this->assertElementContainsText('address_1', "- select - United States");
288 $this->select("address_1_state_province_id", "value=1019");
289
290 $this->click('_qf_Contact_upload_view');
291 $this->waitForPageToLoad($this->getTimeoutMsec());
292
293 // Add contact to group
294 // visit group tab.
295 $this->click("css=li#tab_group a");
296 $this->waitForElementPresent("group_id");
297
298 // Add to group.
299 $this->select("group_id", "label=$groupName");
300 $this->click("_qf_GroupContact_next");
301 $this->waitForElementPresent("//*[@id='GroupContact']");
302
303 $firstName1 = 'aa' . substr(sha1(rand()), 0, 5);
304 $this->webtestAddContact($firstName1, "Smith", "{$firstName1}.smith@example.org");
305
306 $sortName1 = "Smith, {$firstName1}";
307 $displayName1 = "{$firstName1} Smith";
308
309 // Add contact to parent group
310 // visit group tab.
311 $this->click("css=li#tab_group a");
312 $this->waitForElementPresent("group_id");
313
314 // Add to group.
315 $this->select("group_id", "label=$groupName");
316 $this->click("_qf_GroupContact_next");
317 $this->waitForElementPresent("//*[@id='GroupContact']");
318
319 $firstName2 = 'bb' . substr(sha1(rand()), 0, 5);
320
321 $this->openCiviPage("contact/add", "reset=1&ct=Individual", "_qf_Contact_upload_view-bottom");
322 $this->type('first_name', $firstName2);
323 $this->type('last_name', "Smith");
324 $this->type('email_1_email', "{$firstName2}.smith@example.org");
325
326 //address section
327 $this->click("addressBlock");
328 $this->waitForElementPresent("address_1_street_address");
329
330 $this->click("//div[@id='addressBlockId']/div[1]");
331
332 $this->click("address[1][use_shared_address]");
333 $this->waitForElementPresent("address_1_master_contact_id");
334 $this->select2('address_1_master_contact_id', $houseHold);
335 $this->waitForTextPresent("121A Sherman");
336
337 $this->click('_qf_Contact_upload_view-bottom');
338 $this->waitForPageToLoad($this->getTimeoutMsec());
339
340 $sortName2 = "Smith, {$firstName2}";
341 $displayName2 = "{$firstName2} Smith";
342
343 // Add contact to parent group
344 // visit group tab.
345 $this->click("css=li#tab_group a");
346 $this->waitForElementPresent("group_id");
347
348 // Add to group.
349 $this->select("group_id", "label=$groupName");
350 $this->click("_qf_GroupContact_next");
351 $this->waitForElementPresent("//*[@id='GroupContact']");
352
353 $this->openCiviPage("contact/search", "reset=1", NULL);
354
355 // Select group.
356 $this->select("group", "label=$groupName");
357
358 // Click to search.
359 $this->click("_qf_Basic_refresh");
360 $this->waitForPageToLoad($this->getTimeoutMsec());
361
362 // Is contact present in search result?
363 $this->assertElementContainsText('css=div.crm-search-results', $sortName1, "Contact did not found in search result!");
364
365 // Is contact present in search result?
366 $this->assertElementContainsText('css=div.crm-search-results', $sortName2, "Contact did not found in search result!");
367
368 // Is contact present in search result?
369 $this->assertElementContainsText('css=div.crm-search-results', $houseHold, "Contact did not found in search result!");
370
371 // select to export all the contasct from search result.
372 $this->click("CIVICRM_QFID_ts_all_4");
373
374 // Select the task action to export.
375 $this->click("task");
376 $this->select("task", "label=Export Contacts");
377 $this->waitForPageToLoad($this->getTimeoutMsec());
378
379 $this->click("CIVICRM_QFID_2_10");
380
381 $csvFile = $this->downloadCSV("_qf_Select_next-bottom");
382
383 // Build header row for assertion.
384 require_once 'CRM/Contact/BAO/Contact.php';
385 $expotableFields = CRM_Contact_BAO_Contact::exportableFields('All', FALSE, TRUE);
386
387 $checkHeaders = array();
388 foreach ($expotableFields as $key => $field) {
389 // Exclude custom fields.
390 if ($key && (substr($key, 0, 6) == 'custom')) {
391 continue;
392 }
393 $checkHeaders[] = $field['title'];
394 }
395
396 // All other rows to be check.
397 $checkRows = array(
398 1 => array(
399 'Contact Type' => 'Household',
400 'Household Name' => $houseHold,
401 ),
402 2 => array(
403 'Contact Type' => 'Individual',
404 'First Name' => $firstName1,
405 'Email' => "{$firstName1}.smith@example.org",
406 'Sort Name' => $sortName1,
407 'Display Name' => $displayName1,
408 ),
409 );
410
411 // Read CSV and fire assertions.
412 $this->reviewCSV($csvFile, $checkHeaders, $checkRows, 2);
413 }
414
415 /**
416 * @param string $groupName
417 * @param string $parentGroupName
418 */
419 function addContactGroup($groupName = 'New Group', $parentGroupName = "- select group -") {
420 $this->openCiviPage("group/add", "reset=1", "_qf_Edit_upload");
421
422 // Fill group name.
423 $this->type("title", $groupName);
424
425 // Fill description.
426 $this->type("description", "Adding new group.");
427
428 // Check Access Control.
429 $this->click("group_type[1]");
430
431 // Check Mailing List.
432 $this->click("group_type[2]");
433
434 // Select Visibility as Public Pages.
435 $this->select("visibility", "value=Public Pages");
436
437 // Select parent group.
438 $this->select("parents", "label=$parentGroupName");
439
440 // Clicking save.
441 $this->click("_qf_Edit_upload");
442 $this->waitForPageToLoad($this->getTimeoutMsec());
443
444 // Is status message correct?
445 $this->waitForText('crm-notification-container', "The Group '$groupName' has been saved.");
446 }
447
448 /**
449 * @param string $fname
450 * @param string $lname
451 * @param null $email
452 * @param null $contactSubtype
453 *
454 * @return array
455 */
456 function webtestAddContactWithGenderPrefixSuffix($fname = 'Anthony', $lname = 'Anderson', $email = NULL, $contactSubtype = NULL) {
457 $url = $this->sboxPath . 'civicrm/contact/add?reset=1&ct=Individual';
458 if ($contactSubtype) {
459 $url = $url . "&cst={$contactSubtype}";
460 }
461 $this->open($url);
462 $this->waitForElementPresent('_qf_Contact_upload_view-bottom');
463
464 $this->type('first_name', $fname);
465 $this->type('last_name', $lname);
466 if ($email === TRUE) {
467 $email = substr(sha1(rand()), 0, 7) . '@example.org';
468 }
469 if ($email) {
470 $this->type('email_1_email', $email);
471 }
472 $genderLabelArray = array(
473 1 => 'Female',
474 2 => 'Male',
475 3 => 'Transgender'
476 );
477 $prefix = rand(1, 4);
478 $suffix = rand(1, 8);
479 $gender = rand(1, 3);
480 $genderLabel = "civicrm_gender_".$genderLabelArray[$gender]."_$gender";
481 $this->select("prefix_id", "value=$prefix");
482 $this->select("suffix_id", "value=$suffix");
483 $this->click("demographics");
484 $this->waitForElementPresent("civicrm_gender_Female_1");
485 $this->click($genderLabel,"value=$gender");
486 $this->waitForElementPresent('_qf_Contact_upload_view-bottom');
487 $this->click('_qf_Contact_upload_view-bottom');
488 $this->waitForPageToLoad($this->getTimeoutMsec());
489 $prefixLabel = WebTest_Export_ContactTest::getOptionLabel('individual_prefix',$prefix);
490 $suffixLabel = WebTest_Export_ContactTest::getOptionLabel('individual_suffix',$suffix);
491 $genderLabel = WebTest_Export_ContactTest::getOptionLabel('gender',$gender);
492 return array($email,$prefixLabel,$suffixLabel,$genderLabel);
493 }
494
495 /**
496 * @param $optionGroupName
497 * @param $optionValue
498 *
499 * @return array|int
500 */
501 function getOptionLabel($optionGroupName,$optionValue){
502 $params = array(
503 'version' => 3,
504 'sequential' => 1,
505 'option_group_name' => $optionGroupName,
506 'value' => $optionValue,
507 'return' => 'label'
508 );
509 $optionLabel = $this->webtest_civicrm_api("OptionValue", "getvalue",$params);
510 return $optionLabel;
511 }
512 }