INFRA-132 - Drupal.Classes.ClassDeclaration
[civicrm-core.git] / tests / phpunit / WebTest / Export / ContactTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
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 27require_once 'WebTest/Export/ExportCiviSeleniumTestCase.php';
e9479dcf
EM
28
29/**
30 * Class WebTest_Export_ContactTest
31 */
6a488035
TO
32class WebTest_Export_ContactTest extends ExportCiviSeleniumTestCase {
33
34 protected function setUp() {
35 parent::setUp();
36 }
37
9b873358 38 public function testPrefixGenderSuffix() {
e0bbb596
AS
39 $this->webtestLogin();
40
41 // Create new group
42 $parentGroupName = 'TestSuffixPrefixGender_' . substr(sha1(rand()), 0, 7);
beea38fc 43 $this->WebtestAddGroup($parentGroupName);
e0bbb596
AS
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);
542e6ea1 48
6c6e6187 49 list($emailContactFirst, $prefixLabelContactFrst, $suffixLabelContactFrst, $genderLabelContactFrst) = WebTest_Export_ContactTest::webtestAddContactWithGenderPrefixSuffix($firstContactName, "Smith", "$firstContactName.smith@example.org", NULL);
e0bbb596
AS
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");
beea38fc 62 $this->waitForAjaxContent();
e0bbb596 63
e0bbb596 64 $secondContactName = 'TestExport2' . substr(sha1(rand()), 0, 7);
6c6e6187 65 list($emailContactSecond, $prefixLabelContactScnd, $suffixLabelContactScnd, $genderLabelContactScnd) = WebTest_Export_ContactTest::webtestAddContactWithGenderPrefixSuffix($secondContactName, "John", "$secondContactName.john@example.org", NULL);
e0bbb596
AS
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");
beea38fc 78 $this->waitForAjaxContent();
e0bbb596
AS
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.
beea38fc 89 $this->clickLink("_qf_Basic_refresh");
e0bbb596
AS
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");
e0bbb596
AS
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(
e0bbb596
AS
123 'First Name' => $secondContactName,
124 'Last Name' => 'John',
92fcb95f 125 'Email' => '' . strtolower($emailContactSecond) . '',
e0bbb596 126 'Sort Name' => $sortSecondName,
92fcb95f 127 'Display Name' => $prefixLabelContactScnd . ' ' . $displaySecondName . ' ' . $suffixLabelContactScnd,
542e6ea1
AS
128 'Individual Prefix' => $prefixLabelContactScnd,
129 'Individual Suffix' => $suffixLabelContactScnd,
130 'Gender' => $genderLabelContactScnd,
e0bbb596 131 ),
fc0a5af6 132 2 => array(
133 'First Name' => $firstContactName,
134 'Last Name' => 'Smith',
135 'Email' => '' . strtolower($emailContactFirst) . '',
136 'Sort Name' => $sortFirstName,
137 'Display Name' => $prefixLabelContactFrst . ' ' . $displayFirstName . ' ' . $suffixLabelContactFrst,
138 'Individual Prefix' => $prefixLabelContactFrst,
139 'Individual Suffix' => $suffixLabelContactFrst,
140 'Gender' => $genderLabelContactFrst,
141 ),
e0bbb596
AS
142 );
143
144 // Read CSV and fire assertions.
145 $this->reviewCSV($csvFile, $checkHeaders, $checkRows, 2);
146 }
147
6a488035
TO
148 /**
149 * Test Contact Export.
150 */
00be9182 151 public function testContactExport() {
6a488035
TO
152 $this->webtestLogin();
153
154 // Create new group
155 $parentGroupName = 'Parentgroup_' . substr(sha1(rand()), 0, 7);
beea38fc 156 $this->WebtestAddGroup($parentGroupName);
6a488035
TO
157
158 // Create new group and select the previously selected group as parent group for this new group.
159 $childGroupName = 'Childgroup_' . substr(sha1(rand()), 0, 7);
beea38fc 160 $this->WebtestAddGroup($childGroupName, $parentGroupName);
6a488035
TO
161
162 // Adding Parent group contact
163 // We're using Quick Add block on the main page for this.
164 $firstName = 'a' . substr(sha1(rand()), 0, 7);
165 $this->webtestAddContact($firstName, "Smith", "$firstName.smith@example.org");
166
167 $sortName = "Smith, $firstName";
168 $displayName = "$firstName Smith";
169
170 // Add contact to parent group
171 // visit group tab.
172 $this->click("css=li#tab_group a");
173 $this->waitForElementPresent("group_id");
174
175 // Add to group.
176 $this->select("group_id", "label=$parentGroupName");
177 $this->click("_qf_GroupContact_next");
beea38fc 178 $this->waitForAjaxContent();
6a488035
TO
179
180 // Adding child group contact
181 // We're using Quick Add block on the main page for this.
182 $childName = 'b' . substr(sha1(rand()), 0, 7);
183 $this->webtestAddContact($childName, "John", "$childName.john@example.org");
184
185 $childSortName = "John, $childName";
186 $childDisplayName = "$childName John";
187
188 // Add contact to child group
189 // visit group tab.
190 $this->click("css=li#tab_group a");
191 $this->waitForElementPresent("group_id");
192
193 // Add to child group.
194 $this->select("group_id", "label=regexp:$childGroupName");
195 $this->click("_qf_GroupContact_next");
beea38fc 196 $this->waitForAjaxContent();
6a488035
TO
197
198 // Visit contact search page.
cb5b38f1 199 $this->openCiviPage("contact/search", "reset=1");
6a488035
TO
200
201 // Select contact type as Indiividual.
202 $this->select("contact_type", "value=Individual");
203
204 // Select group.
205 $this->select("group", "label=$parentGroupName");
206
207 // Click to search.
beea38fc 208 $this->clickLink("_qf_Basic_refresh");
6a488035
TO
209
210 // Is contact present in search result?
a5a7f3f2 211 $this->assertElementContainsText('css=div.crm-search-results', $sortName, "Contact did not found in search result!");
76e86fd8 212
6a488035 213 // Is contact present in search result?
a5a7f3f2 214 $this->assertElementContainsText('css=div.crm-search-results', $childSortName, "Contact did not found in search result!");
76e86fd8 215
6a488035
TO
216 // select to export all the contasct from search result.
217 $this->click("CIVICRM_QFID_ts_all_4");
218
219 // Select the task action to export.
220 $this->click("task");
221 $this->select("task", "label=Export Contacts");
6a488035
TO
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(
6a488035
TO
242 'First Name' => $childName,
243 'Last Name' => 'John',
244 'Email' => "$childName.john@example.org",
245 'Sort Name' => $childSortName,
246 'Display Name' => $childDisplayName,
247 ),
fc0a5af6 248 2 => array(
249 'First Name' => $firstName,
250 'Last Name' => 'Smith',
251 'Email' => "$firstName.smith@example.org",
252 'Sort Name' => $sortName,
253 'Display Name' => $displayName,
254 ),
6a488035
TO
255 );
256
257 // Read CSV and fire assertions.
258 $this->reviewCSV($csvFile, $checkHeaders, $checkRows, 2);
259 }
260
00be9182 261 public function testMergeHousehold() {
6a488035
TO
262 $this->webtestLogin();
263
264 // Create new group
265 $groupName = 'TestGroup_' . substr(sha1(rand()), 0, 7);
beea38fc 266 $this->WebtestAddGroup($groupName);
6a488035
TO
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");
fc0a5af6 285 $this->select("address_1_country_id", "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");
beea38fc 299 $this->waitForAjaxContent();
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");
beea38fc 315 $this->waitForAjaxContent();
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");
beea38fc 349 $this->waitForAjaxContent();
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.
beea38fc 357 $this->clickLink("_qf_Basic_refresh");
6a488035
TO
358
359 // Is contact present in search result?
a5a7f3f2 360 $this->assertElementContainsText('css=div.crm-search-results', $sortName1, "Contact did not found in search result!");
76e86fd8 361
6a488035 362 // Is contact present in search result?
a5a7f3f2 363 $this->assertElementContainsText('css=div.crm-search-results', $sortName2, "Contact did not found in search result!");
76e86fd8 364
6a488035 365 // Is contact present in search result?
a5a7f3f2 366 $this->assertElementContainsText('css=div.crm-search-results', $houseHold, "Contact did not found in search result!");
76e86fd8 367
6a488035
TO
368 // select to export all the contasct from search result.
369 $this->click("CIVICRM_QFID_ts_all_4");
370
371 // Select the task action to export.
372 $this->click("task");
373 $this->select("task", "label=Export Contacts");
6a488035
TO
374 $this->waitForPageToLoad($this->getTimeoutMsec());
375
376 $this->click("CIVICRM_QFID_2_10");
377
378 $csvFile = $this->downloadCSV("_qf_Select_next-bottom");
379
380 // Build header row for assertion.
381 require_once 'CRM/Contact/BAO/Contact.php';
382 $expotableFields = CRM_Contact_BAO_Contact::exportableFields('All', FALSE, TRUE);
383
384 $checkHeaders = array();
385 foreach ($expotableFields as $key => $field) {
386 // Exclude custom fields.
387 if ($key && (substr($key, 0, 6) == 'custom')) {
388 continue;
389 }
390 $checkHeaders[] = $field['title'];
391 }
392
393 // All other rows to be check.
394 $checkRows = array(
395 1 => array(
396 'Contact Type' => 'Household',
397 'Household Name' => $houseHold,
398 ),
399 2 => array(
400 'Contact Type' => 'Individual',
401 'First Name' => $firstName1,
402 'Email' => "{$firstName1}.smith@example.org",
403 'Sort Name' => $sortName1,
404 'Display Name' => $displayName1,
405 ),
406 );
407
408 // Read CSV and fire assertions.
409 $this->reviewCSV($csvFile, $checkHeaders, $checkRows, 2);
410 }
411
4cbe18b8
EM
412 /**
413 * @param string $fname
414 * @param string $lname
415 * @param null $email
416 * @param null $contactSubtype
417 *
418 * @return array
419 */
00be9182 420 public function webtestAddContactWithGenderPrefixSuffix($fname = 'Anthony', $lname = 'Anderson', $email = NULL, $contactSubtype = NULL) {
e0bbb596
AS
421 $url = $this->sboxPath . 'civicrm/contact/add?reset=1&ct=Individual';
422 if ($contactSubtype) {
423 $url = $url . "&cst={$contactSubtype}";
424 }
425 $this->open($url);
426 $this->waitForElementPresent('_qf_Contact_upload_view-bottom');
427
428 $this->type('first_name', $fname);
429 $this->type('last_name', $lname);
430 if ($email === TRUE) {
431 $email = substr(sha1(rand()), 0, 7) . '@example.org';
432 }
433 if ($email) {
434 $this->type('email_1_email', $email);
435 }
436 $genderLabelArray = array(
437 1 => 'Female',
438 2 => 'Male',
21dfd5f5 439 3 => 'Transgender',
e0bbb596 440 );
542e6ea1
AS
441 $prefix = rand(1, 4);
442 $suffix = rand(1, 8);
443 $gender = rand(1, 3);
92fcb95f 444 $genderLabel = "civicrm_gender_" . $genderLabelArray[$gender] . "_$gender";
e0bbb596
AS
445 $this->select("prefix_id", "value=$prefix");
446 $this->select("suffix_id", "value=$suffix");
447 $this->click("demographics");
448 $this->waitForElementPresent("civicrm_gender_Female_1");
6c6e6187 449 $this->click($genderLabel, "value=$gender");
e0bbb596
AS
450 $this->waitForElementPresent('_qf_Contact_upload_view-bottom');
451 $this->click('_qf_Contact_upload_view-bottom');
452 $this->waitForPageToLoad($this->getTimeoutMsec());
6c6e6187
TO
453 $prefixLabel = WebTest_Export_ContactTest::getOptionLabel('individual_prefix', $prefix);
454 $suffixLabel = WebTest_Export_ContactTest::getOptionLabel('individual_suffix', $suffix);
455 $genderLabel = WebTest_Export_ContactTest::getOptionLabel('gender', $gender);
456 return array($email, $prefixLabel, $suffixLabel, $genderLabel);
542e6ea1
AS
457 }
458
4cbe18b8 459 /**
100fef9d 460 * @param string $optionGroupName
4cbe18b8
EM
461 * @param $optionValue
462 *
463 * @return array|int
464 */
9b873358 465 public function getOptionLabel($optionGroupName, $optionValue) {
542e6ea1
AS
466 $params = array(
467 'version' => 3,
468 'sequential' => 1,
469 'option_group_name' => $optionGroupName,
470 'value' => $optionValue,
21dfd5f5 471 'return' => 'label',
542e6ea1 472 );
6c6e6187 473 $optionLabel = $this->webtest_civicrm_api("OptionValue", "getvalue", $params);
542e6ea1 474 return $optionLabel;
e0bbb596 475 }
96025800 476
42daf119 477}