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