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