Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-08-28-20-20-34
[civicrm-core.git] / tests / phpunit / WebTest / Export / ContactTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 class WebTest_Export_ContactTest extends ExportCiviSeleniumTestCase {
29
30 protected function setUp() {
31 parent::setUp();
32 }
33
34 /**
35 * Test Contact Export.
36 */
37 function testContactExport() {
38 $this->webtestLogin();
39
40 // Create new group
41 $parentGroupName = 'Parentgroup_' . substr(sha1(rand()), 0, 7);
42 $this->addContactGroup($parentGroupName);
43
44 // Create new group and select the previously selected group as parent group for this new group.
45 $childGroupName = 'Childgroup_' . substr(sha1(rand()), 0, 7);
46 $this->addContactGroup($childGroupName, $parentGroupName);
47
48 // Adding Parent group contact
49 // We're using Quick Add block on the main page for this.
50 $firstName = 'a' . substr(sha1(rand()), 0, 7);
51 $this->webtestAddContact($firstName, "Smith", "$firstName.smith@example.org");
52
53 $sortName = "Smith, $firstName";
54 $displayName = "$firstName Smith";
55
56 // Add contact to parent group
57 // visit group tab.
58 $this->click("css=li#tab_group a");
59 $this->waitForElementPresent("group_id");
60
61 // Add to group.
62 $this->select("group_id", "label=$parentGroupName");
63 $this->click("_qf_GroupContact_next");
64 $this->waitForPageToLoad($this->getTimeoutMsec());
65
66 // Adding child group contact
67 // We're using Quick Add block on the main page for this.
68 $childName = 'b' . substr(sha1(rand()), 0, 7);
69 $this->webtestAddContact($childName, "John", "$childName.john@example.org");
70
71 $childSortName = "John, $childName";
72 $childDisplayName = "$childName John";
73
74 // Add contact to child group
75 // visit group tab.
76 $this->click("css=li#tab_group a");
77 $this->waitForElementPresent("group_id");
78
79 // Add to child group.
80 $this->select("group_id", "label=regexp:$childGroupName");
81 $this->click("_qf_GroupContact_next");
82 $this->waitForPageToLoad($this->getTimeoutMsec());
83
84 // Visit contact search page.
85 $this->openCiviPage("contact/search", "reset=1");
86
87 // Select contact type as Indiividual.
88 $this->select("contact_type", "value=Individual");
89
90 // Select group.
91 $this->select("group", "label=$parentGroupName");
92
93 // Click to search.
94 $this->click("_qf_Basic_refresh");
95 $this->waitForPageToLoad($this->getTimeoutMsec());
96
97 // Is contact present in search result?
98 $this->assertElementContainsText('css=div.crm-search-results', $sortName, "Contact did not found in search result!");
99
100 // Is contact present in search result?
101 $this->assertElementContainsText('css=div.crm-search-results', $childSortName, "Contact did not found in search result!");
102
103 // select to export all the contasct from search result.
104 $this->click("CIVICRM_QFID_ts_all_4");
105
106 // Select the task action to export.
107 $this->click("task");
108 $this->select("task", "label=Export Contacts");
109 $this->click("Go");
110 $this->waitForPageToLoad($this->getTimeoutMsec());
111
112 $csvFile = $this->downloadCSV("_qf_Select_next-bottom");
113
114 // Build header row for assertion.
115 require_once 'CRM/Contact/BAO/Contact.php';
116 $expotableFields = CRM_Contact_BAO_Contact::exportableFields('All', FALSE, TRUE);
117
118 $checkHeaders = array();
119 foreach ($expotableFields as $key => $field) {
120 // Exclude custom fields.
121 if ($key && (substr($key, 0, 6) == 'custom')) {
122 continue;
123 }
124 $checkHeaders[] = $field['title'];
125 }
126
127 // All other rows to be check.
128 $checkRows = array(
129 1 => array(
130 'First Name' => $firstName,
131 'Last Name' => 'Smith',
132 'Email' => "$firstName.smith@example.org",
133 'Sort Name' => $sortName,
134 'Display Name' => $displayName,
135 ),
136 2 => array(
137 'First Name' => $childName,
138 'Last Name' => 'John',
139 'Email' => "$childName.john@example.org",
140 'Sort Name' => $childSortName,
141 'Display Name' => $childDisplayName,
142 ),
143 );
144
145 // Read CSV and fire assertions.
146 $this->reviewCSV($csvFile, $checkHeaders, $checkRows, 2);
147 }
148
149 function testMergeHousehold() {
150 $this->webtestLogin();
151
152 // Create new group
153 $groupName = 'TestGroup_' . substr(sha1(rand()), 0, 7);
154 $this->addContactGroup($groupName);
155
156 // Adding Parent group contact
157 // We're using Quick Add block on the main page for this.
158 $houseHold = 'H' . substr(sha1(rand()), 0, 5) . ' House';
159
160 $this->openCiviPage("contact/add", "reset=1&ct=Household");
161 $this->click('household_name');
162 $this->type('household_name', $houseHold);
163
164 //address section
165 $this->click("addressBlock");
166 $this->waitForElementPresent("address_1_street_address");
167
168 // fill in address
169 $this->click("//div[@id='addressBlockId']/div[1]");
170 $this->type("address_1_street_address", "121A Sherman St. Apt. 12");
171 $this->type("address_1_city", "Dumfries");
172 $this->type("address_1_postal_code", "1234");
173 $this->assertElementContainsText('address_1', "- select - United States");
174 $this->select("address_1_state_province_id", "value=1019");
175
176 $this->click('_qf_Contact_upload_view');
177 $this->waitForPageToLoad($this->getTimeoutMsec());
178
179 // Add contact to group
180 // visit group tab.
181 $this->click("css=li#tab_group a");
182 $this->waitForElementPresent("group_id");
183
184 // Add to group.
185 $this->select("group_id", "label=$groupName");
186 $this->click("_qf_GroupContact_next");
187 $this->waitForPageToLoad($this->getTimeoutMsec());
188
189 $firstName1 = 'aa' . substr(sha1(rand()), 0, 5);
190 $this->webtestAddContact($firstName1, "Smith", "{$firstName1}.smith@example.org");
191
192 $sortName1 = "Smith, {$firstName1}";
193 $displayName1 = "{$firstName1} Smith";
194
195 // Add contact to parent group
196 // visit group tab.
197 $this->click("css=li#tab_group a");
198 $this->waitForElementPresent("group_id");
199
200 // Add to group.
201 $this->select("group_id", "label=$groupName");
202 $this->click("_qf_GroupContact_next");
203 $this->waitForPageToLoad($this->getTimeoutMsec());
204
205 $firstName2 = 'bb' . substr(sha1(rand()), 0, 5);
206
207 $this->openCiviPage("contact/add", "reset=1&ct=Individual", "_qf_Contact_upload_view-bottom");
208 $this->type('first_name', $firstName2);
209 $this->type('last_name', "Smith");
210 $this->type('email_1_email', "{$firstName2}.smith@example.org");
211
212 //address section
213 $this->click("addressBlock");
214 $this->waitForElementPresent("address_1_street_address");
215
216 $this->click("//div[@id='addressBlockId']/div[1]");
217
218 $this->click("address[1][use_shared_address]");
219 $this->waitForElementPresent("contact_1");
220 $this->webtestFillAutocomplete($houseHold);
221 $this->waitForTextPresent("121A Sherman");
222
223 $this->click('_qf_Contact_upload_view-bottom');
224 $this->waitForPageToLoad($this->getTimeoutMsec());
225
226 $sortName2 = "Smith, {$firstName2}";
227 $displayName2 = "{$firstName2} Smith";
228
229 // Add contact to parent group
230 // visit group tab.
231 $this->click("css=li#tab_group a");
232 $this->waitForElementPresent("group_id");
233
234 // Add to group.
235 $this->select("group_id", "label=$groupName");
236 $this->click("_qf_GroupContact_next");
237 $this->waitForPageToLoad($this->getTimeoutMsec());
238
239 $this->openCiviPage("contact/search", "reset=1", NULL);
240
241 // Select group.
242 $this->select("group", "label=$groupName");
243
244 // Click to search.
245 $this->click("_qf_Basic_refresh");
246 $this->waitForPageToLoad($this->getTimeoutMsec());
247
248 // Is contact present in search result?
249 $this->assertElementContainsText('css=div.crm-search-results', $sortName1, "Contact did not found in search result!");
250
251 // Is contact present in search result?
252 $this->assertElementContainsText('css=div.crm-search-results', $sortName2, "Contact did not found in search result!");
253
254 // Is contact present in search result?
255 $this->assertElementContainsText('css=div.crm-search-results', $houseHold, "Contact did not found in search result!");
256
257 // select to export all the contasct from search result.
258 $this->click("CIVICRM_QFID_ts_all_4");
259
260 // Select the task action to export.
261 $this->click("task");
262 $this->select("task", "label=Export Contacts");
263 $this->click("Go");
264 $this->waitForPageToLoad($this->getTimeoutMsec());
265
266 $this->click("CIVICRM_QFID_2_10");
267
268 $csvFile = $this->downloadCSV("_qf_Select_next-bottom");
269
270 // Build header row for assertion.
271 require_once 'CRM/Contact/BAO/Contact.php';
272 $expotableFields = CRM_Contact_BAO_Contact::exportableFields('All', FALSE, TRUE);
273
274 $checkHeaders = array();
275 foreach ($expotableFields as $key => $field) {
276 // Exclude custom fields.
277 if ($key && (substr($key, 0, 6) == 'custom')) {
278 continue;
279 }
280 $checkHeaders[] = $field['title'];
281 }
282
283 // All other rows to be check.
284 $checkRows = array(
285 1 => array(
286 'Contact Type' => 'Household',
287 'Household Name' => $houseHold,
288 ),
289 2 => array(
290 'Contact Type' => 'Individual',
291 'First Name' => $firstName1,
292 'Email' => "{$firstName1}.smith@example.org",
293 'Sort Name' => $sortName1,
294 'Display Name' => $displayName1,
295 ),
296 );
297
298 // Read CSV and fire assertions.
299 $this->reviewCSV($csvFile, $checkHeaders, $checkRows, 2);
300 }
301
302 function addContactGroup($groupName = 'New Group', $parentGroupName = "- select -") {
303 $this->openCiviPage("group/add", "reset=1", "_qf_Edit_upload");
304
305 // Fill group name.
306 $this->type("title", $groupName);
307
308 // Fill description.
309 $this->type("description", "Adding new group.");
310
311 // Check Access Control.
312 $this->click("group_type[1]");
313
314 // Check Mailing List.
315 $this->click("group_type[2]");
316
317 // Select Visibility as Public Pages.
318 $this->select("visibility", "value=Public Pages");
319
320 // Select parent group.
321 $this->select("parents", "label=$parentGroupName");
322
323 // Clicking save.
324 $this->click("_qf_Edit_upload");
325 $this->waitForPageToLoad($this->getTimeoutMsec());
326
327 // Is status message correct?
328 $this->waitForText('crm-notification-container', "The Group '$groupName' has been saved.");
329 }
330 }