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