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