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