Merge pull request #197 from pratik-joshi/CRM-12130
[civicrm-core.git] / tests / phpunit / WebTest / Import / 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/Import/ImportCiviSeleniumTestCase.php';
29 class WebTest_Import_ContactTest extends ImportCiviSeleniumTestCase {
30
31 protected $captureScreenshotOnFailure = TRUE;
32 protected $screenshotPath = '/var/www/api.dev.civicrm.org/public/sc';
33 protected $screenshotUrl = 'http://api.dev.civicrm.org/sc/';
34
35 protected function setUp() {
36 parent::setUp();
37 }
38
39 /*
40 * Test contact import for Individuals.
41 */
42 function testIndividualImport() {
43 $this->webtestLogin();
44
45 // Get sample import data.
46 list($headers, $rows) = $this->_individualCSVData();
47
48 // Import and check Individual contacts in Skip mode.
49 $other = array(
50 'saveMapping' => TRUE,
51 'createGroup' => TRUE,
52 'createTag' => TRUE,
53 );
54
55 $this->importContacts($headers, $rows, 'Individual', 'Skip', array(), $other);
56
57 // Get imported contact Ids
58 $importedContactIds = $this->_getImportedContactIds($rows);
59
60 // Build update mode import headers
61 $updateHeaders = array(
62 'contact_id' => 'Internal Contact ID',
63 'first_name' => 'First Name',
64 'last_name' => 'Last Name',
65 );
66
67 // Create update mode import rows
68 $updateRows = array();
69 foreach ($importedContactIds as $cid) {
70 $updateRows[$cid] = array(
71 'contact_id' => $cid,
72 'first_name' => substr(sha1(rand()), 0, 7),
73 'last_name' => 'Anderson' . substr(sha1(rand()), 0, 7),
74 );
75 }
76
77 // Import and check Individual contacts in Update mode.
78 $this->importContacts($updateHeaders, $updateRows, 'Individual', 'Update');
79
80 // Visit contacts to check updated data.
81 foreach ($updateRows as $updatedRow) {
82 $this->openCiviPage("contact/view", "reset=1&cid={$updatedRow['contact_id']}");
83
84 $displayName = "{$updatedRow['first_name']} {$updatedRow['last_name']}";
85 $this->assertTrue($this->isTextPresent("$displayName"), "Contact did not update!");
86 }
87
88 // Headers that should not updated.
89 $fillHeaders = $updateHeaders;
90
91 // Headers that should fill.
92 $fillHeaders['gender'] = 'Gender';
93 $fillHeaders['dob'] = 'Birth Date';
94
95 $fillRows = array();
96 foreach ($importedContactIds as $cid) {
97 $fillRows[$cid] = array(
98 'contact_id' => $cid,
99 // should not update
100 'first_name' => substr(sha1(rand()), 0, 7),
101 // should not update
102 'last_name' => 'Anderson' . substr(sha1(rand()), 0, 7),
103 'gender' => 'Male',
104 'dob' => '1986-04-16',
105 );
106 }
107
108 // Import and check Individual contacts in Update mode.
109 $this->importContacts($fillHeaders, $fillRows, 'Individual', 'Fill');
110
111 // Visit contacts to check filled data.
112 foreach ($fillRows as $cid => $fillRow) {
113 $this->openCiviPage("contact/view", "reset=1&cid={$fillRow['contact_id']}");
114
115 // Check old display name.
116 $displayName = "{$updateRows[$cid]['first_name']} {$updateRows[$cid]['last_name']}";
117 $this->assertTrue($this->isTextPresent("$displayName"), "Contact display name should not update in fill mode!");
118
119 $this->verifyText("css=div#contact-summary div.crm-contact-gender_display", preg_quote($fillRow['gender']));
120 }
121
122 // Recreate same conacts using 'No Duplicate Checking'
123 $this->importContacts($headers, $rows, 'Individual', 'No Duplicate Checking');
124 }
125
126 /*
127 * Test contact import for Organization.
128 */
129 function testOrganizationImport() {
130
131 $this->webtestLogin();
132
133 // Get sample import data.
134 list($headers, $rows) = $this->_organizationCSVData();
135
136 // Import and check Organization contacts
137 $other = array(
138 'saveMapping' => TRUE,
139 'createGroup' => TRUE,
140 'createTag' => TRUE,
141 );
142
143 $this->importContacts($headers, $rows, 'Organization', 'Skip', array(), $other);
144
145 // Get imported contact Ids
146 $importedContactIds = $this->_getImportedContactIds($rows, 'Organization');
147
148 // Build update mode import headers
149 $updateHeaders = array(
150 'contact_id' => 'Internal Contact ID',
151 'organization_name' => 'Organization Name',
152 );
153
154 // Create update mode import rows
155 $updateRows = array();
156 foreach ($importedContactIds as $cid) {
157 $updateRows[$cid] = array(
158 'contact_id' => $cid,
159 'organization_name' => 'UpdatedOrg ' . substr(sha1(rand()), 0, 7),
160 );
161 }
162
163 // Import and check Individual contacts in Update mode.
164 $this->importContacts($updateHeaders, $updateRows, 'Organization', 'Update');
165
166 // Visit contacts to check updated data.
167 foreach ($updateRows as $updatedRow) {
168 $organizationName = $updatedRow['organization_name'];
169 $this->openCiviPage("contact/view", "reset=1&cid={$updatedRow['contact_id']}");
170
171 $this->assertTrue($this->isTextPresent("$organizationName"), "Contact did not update!");
172 }
173
174 // Headers that should not updated.
175 $fillHeaders = $updateHeaders;
176
177 // Headers that should fill.
178 $fillHeaders['legal_name'] = 'Legal Name';
179
180 $fillRows = array();
181 foreach ($importedContactIds as $cid) {
182 $fillRows[$cid] = array(
183 'contact_id' => $cid,
184 // should not update
185 'organization_name' => 'UpdateOrg ' . substr(sha1(rand()), 0, 7),
186 'legal_name' => 'org ' . substr(sha1(rand()), 0, 7),
187 );
188 }
189
190 // Import and check Individual contacts in Update mode.
191 $this->importContacts($fillHeaders, $fillRows, 'Organization', 'Fill');
192
193 // Visit contacts to check filled data.
194 foreach ($fillRows as $cid => $fillRow) {
195 $this->openCiviPage("contact/view", "reset=1&cid={$fillRow['contact_id']}");
196
197 // Check old Organization name.
198 $organizationName = $updateRows[$cid]['organization_name'];
199 $this->assertTrue($this->isTextPresent("$organizationName"), "Contact should not update in fill mode!");
200 $this->verifyText("xpath=//div[@id='crm-contactinfo-content']/div/div[3]/div[2]", preg_quote($fillRow['legal_name']));
201 }
202
203 // Recreate same conacts using 'No Duplicate Checking'
204 $this->importContacts($headers, $rows, 'Organization', 'No Duplicate Checking');
205 }
206
207 /*
208 * Test contact import for Household.
209 */
210 function testHouseholdImport() {
211
212 $this->webtestLogin();
213
214 // Get sample import data.
215 list($headers, $rows) = $this->_householdCSVData();
216
217 // Import and check Household contacts
218 $other = array(
219 'saveMapping' => TRUE,
220 'createGroup' => TRUE,
221 'createTag' => TRUE,
222 );
223
224 $this->importContacts($headers, $rows, 'Household', 'Skip', array(), $other);
225
226 // Get imported contact Ids
227 $importedContactIds = $this->_getImportedContactIds($rows, 'Household');
228
229 // Build update mode import headers
230 $updateHeaders = array(
231 'contact_id' => 'Internal Contact ID',
232 'household_name' => 'Household Name',
233 );
234
235 // Create update mode import rows
236 $updateRows = array();
237 foreach ($importedContactIds as $cid) {
238 $updateRows[$cid] = array(
239 'contact_id' => $cid,
240 'household_name' => 'UpdatedHousehold ' . substr(sha1(rand()), 0, 7),
241 );
242 }
243
244 // Import and check Individual contacts in Update mode.
245 $this->importContacts($updateHeaders, $updateRows, 'Household', 'Update');
246
247 // Visit contacts to check updated data.
248 foreach ($updateRows as $updatedRow) {
249 $householdName = $updatedRow['household_name'];
250 $this->openCiviPage("contact/view", "reset=1&cid={$updatedRow['contact_id']}");
251
252 $this->assertTrue($this->isTextPresent("$householdName"), "Contact did not update!");
253 }
254
255 // Headers that should not updated.
256 $fillHeaders = $updateHeaders;
257
258 // Headers that should fill.
259 $fillHeaders['nick_name'] = 'Nick Name';
260
261 $fillRows = array();
262 foreach ($importedContactIds as $cid) {
263 $fillRows[$cid] = array(
264 'contact_id' => $cid,
265 // should not update
266 'household_name' => 'UpdatedHousehold ' . substr(sha1(rand()), 0, 7),
267 'nick_name' => 'Household ' . substr(sha1(rand()), 0, 7),
268 );
269 }
270
271 // Import and check Individual contacts in Update mode.
272 $this->importContacts($fillHeaders, $fillRows, 'Household', 'Fill');
273
274 // Visit contacts to check filled data.
275 foreach ($fillRows as $cid => $fillRow) {
276 $this->openCiviPage("contact/view", "reset=1&cid={$fillRow['contact_id']}");
277
278 // Check old Household name.
279 $householdName = $updateRows[$cid]['household_name'];
280 $this->assertTrue($this->isTextPresent("$householdName"), "Contact should not update in fill mode!");
281 $this->verifyText("xpath=//div[@id='crm-contactinfo-content']/div/div[2]/div[2]", preg_quote($fillRow['nick_name']));
282 }
283
284 // Recreate same conacts using 'No Duplicate Checking'
285 $this->importContacts($headers, $rows, 'Household', 'No Duplicate Checking');
286 }
287
288 /*
289 * Helper function to provide data for contact import for Individuals.
290 */
291 function _individualCSVData() {
292 $headers = array(
293 'first_name' => 'First Name',
294 'middle_name' => 'Middle Name',
295 'last_name' => 'Last Name',
296 'email' => 'Email',
297 'phone' => 'Phone',
298 'address_1' => 'Additional Address 1',
299 'address_2' => 'Additional Address 2',
300 'city' => 'City',
301 'state' => 'State',
302 'country' => 'Country',
303 );
304
305 $rows = array(
306 array('first_name' => substr(sha1(rand()), 0, 7),
307 'middle_name' => substr(sha1(rand()), 0, 7),
308 'last_name' => 'Anderson',
309 'email' => substr(sha1(rand()), 0, 7) . '@example.com',
310 'phone' => '6949912154',
311 'address_1' => 'Add 1',
312 'address_2' => 'Add 2',
313 'city' => 'Watson',
314 'state' => 'NY',
315 'country' => 'United States',
316 ),
317 array('first_name' => substr(sha1(rand()), 0, 7),
318 'middle_name' => substr(sha1(rand()), 0, 7),
319 'last_name' => 'Summerson',
320 'email' => substr(sha1(rand()), 0, 7) . '@example.com',
321 'phone' => '6944412154',
322 'address_1' => 'Add 1',
323 'address_2' => 'Add 2',
324 'city' => 'Watson',
325 'state' => 'NY',
326 'country' => 'United States',
327 ),
328 );
329
330 return array($headers, $rows);
331 }
332
333 /*
334 * Helper function to provide data for contact import for Organizations.
335 */
336 function _organizationCSVData() {
337 $headers = array(
338 'organization_name' => 'Organization Name',
339 'email' => 'Email',
340 'phone' => 'Phone',
341 'address_1' => 'Additional Address 1',
342 'address_2' => 'Additional Address 2',
343 'city' => 'City',
344 'state' => 'State',
345 'country' => 'Country',
346 );
347
348 $rows = array(
349 array('organization_name' => 'org_' . substr(sha1(rand()), 0, 7),
350 'email' => substr(sha1(rand()), 0, 7) . '@example.org',
351 'phone' => '9949912154',
352 'address_1' => 'Add 1',
353 'address_2' => 'Add 2',
354 'city' => 'Watson',
355 'state' => 'NY',
356 'country' => 'United States',
357 ),
358 array('organization_name' => 'org_' . substr(sha1(rand()), 0, 7),
359 'email' => substr(sha1(rand()), 0, 7) . '@example.org',
360 'phone' => '6949412154',
361 'address_1' => 'Add 1',
362 'address_2' => 'Add 2',
363 'city' => 'Watson',
364 'state' => 'NY',
365 'country' => 'United States',
366 ),
367 );
368
369 return array($headers, $rows);
370 }
371
372 /*
373 * Helper function to provide data for contact import for Household.
374 */
375 function _householdCSVData() {
376 $headers = array(
377 'household_name' => 'Household Name',
378 'email' => 'Email',
379 'phone' => 'Phone',
380 'address_1' => 'Additional Address 1',
381 'address_2' => 'Additional Address 2',
382 'city' => 'City',
383 'state' => 'State',
384 'country' => 'Country',
385 );
386
387 $rows = array(
388 array('household_name' => 'household_' . substr(sha1(rand()), 0, 7),
389 'email' => substr(sha1(rand()), 0, 7) . '@example.org',
390 'phone' => '3949912154',
391 'address_1' => 'Add 1',
392 'address_2' => 'Add 2',
393 'city' => 'Watson',
394 'state' => 'NY',
395 'country' => 'United States',
396 ),
397 array('household_name' => 'household_' . substr(sha1(rand()), 0, 7),
398 'email' => substr(sha1(rand()), 0, 7) . '@example.org',
399 'phone' => '5949412154',
400 'address_1' => 'Add 1',
401 'address_2' => 'Add 2',
402 'city' => 'Watson',
403 'state' => 'NY',
404 'country' => 'United States',
405 ),
406 );
407
408 return array($headers, $rows);
409 }
410 }
411