Merge pull request #203 from mlutfy/CRM-11755
[civicrm-core.git] / tests / phpunit / WebTest / Import / ImportCiviSeleniumTestCase.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 'CiviTest/CiviSeleniumTestCase.php';
29 require_once 'CRM/Utils/Array.php';
30 class ImportCiviSeleniumTestCase extends CiviSeleniumTestCase {
31
32 /*
33 * Function to test csv import for each component.
34 *
35 * @params string $component component name ( Event, Contribution, Membership, Activity etc)
36 * @params array $headers csv data headers
37 * @params array $rows csv data rows
38 * @params string $contactType contact type
39 * @params string $mode import mode
40 * @params array $fieldMapper select mapper fields while import
41 * @params array $other other parameters
42 * useMappingName : to reuse mapping
43 * dateFormat : date format of data
44 * checkMapperHeaders : to override default check mapper headers
45 * saveMapping : save current mapping?
46 * saveMappingName : to override mapping name
47 *
48 */
49 function importCSVComponent($component,
50 $headers,
51 $rows,
52 $contactType = 'Individual',
53 $mode = 'Skip',
54 $fieldMapper = array(),
55 $other = array()
56 ) {
57
58 // Go to contact import page.
59 $this->open($this->sboxPath . $this->_getImportComponentUrl($component));
60
61 $this->waitForPageToLoad($this->getTimeoutMsec());
62
63 // check for upload field.
64 $this->waitForElementPresent("uploadFile");
65
66 // Create csv file of sample data.
67 $csvFile = $this->webtestCreateCSV($headers, $rows);
68
69 // Attach csv file.
70 $this->webtestAttachFile('uploadFile', $csvFile);
71
72 // First row is header.
73 $this->click('skipColumnHeader');
74
75 // select mode, default is 'Skip'.
76 if ($mode == 'Update') {
77 $this->click("CIVICRM_QFID_4_4");
78 }
79 elseif ($mode == 'No Duplicate Checking') {
80 $this->click("CIVICRM_QFID_16_6");
81 }
82
83 // select contact type, default is 'Individual'.
84 if ($component != 'Activity') {
85 $contactTypeOption = $this->_getImportComponentContactType($component, $contactType);
86 $this->click($contactTypeOption);
87 }
88
89 // Date format, default: yyyy-mm-dd OR yyyymmdd
90 if (isset($other['dateFormat'])) {
91 // default
92 $dateFormatMapper = array(
93 'yyyy-mm-dd OR yyyymmdd' => "CIVICRM_QFID_1_14",
94 'mm/dd/yy OR mm-dd-yy' => "CIVICRM_QFID_2_16",
95 'mm/dd/yyyy OR mm-dd-yyyy' => "CIVICRM_QFID_4_18",
96 'Month dd, yyyy' => "CIVICRM_QFID_8_20",
97 'dd-mon-yy OR dd/mm/yy' => "CIVICRM_QFID_16_22",
98 'dd/mm/yyyy' => "CIVICRM_QFID_32_24",
99 );
100 $this->click($dateFormatMapper[$other['dateFormat']]);
101 }
102
103 // Use already created mapping
104 $existingMapping = NULL;
105 if (isset($other['useMappingName'])) {
106 $this->select('savedMapping', "label=" . $other['useMappingName']);
107 $existingMapping = $other['useMappingName'];
108 }
109
110 // Submit form.
111 $this->click('_qf_UploadFile_upload');
112 $this->waitForPageToLoad($this->getTimeoutMsec());
113
114 // Select matching field for cvs data.
115 if (!empty($fieldMapper)) {
116 foreach ($fieldMapper as $field => $value) {
117 $this->select($field, "value={$value}");
118 }
119 }
120
121 // Check mapping data.
122 $this->_checkImportMapperData($headers,
123 $rows,
124 $existingMapping,
125 isset($other['checkMapperHeaders']) ? $other['checkMapperHeaders'] : array()
126 );
127
128 // Save mapping
129 if (isset($other['saveMapping'])) {
130 $mappingName = isset($other['saveMappingName']) ? $other['saveMappingName'] : "{$component}Import_" . substr(sha1(rand()), 0, 7);
131
132 $this->click('saveMapping');
133 $this->type('saveMappingName', $mappingName);
134 $this->type('saveMappingDesc', "Mapping for {$contactType}");
135 }
136
137 // Submit form.
138 $this->click('_qf_MapField_next');
139 $this->waitForElementPresent('_qf_Preview_next-bottom');
140
141 // Check mapping data.
142 $this->_checkImportMapperData($headers, $rows, $existingMapping, isset($other['checkMapperHeaders']) ? $other['checkMapperHeaders'] : array());
143
144 // Submit form.
145 $this->click('_qf_Preview_next-bottom');
146 $this->waitForPageToLoad($this->getTimeoutMsec());
147
148 // Visit summary page.
149 $this->waitForElementPresent("_qf_Summary_next");
150
151 // Check success message.
152 $this->assertTrue($this->isTextPresent("Import has completed successfully. The information below summarizes the results."));
153
154 // Check summary Details.
155 $importedRecords = count($rows);
156 $checkSummary = array(
157 'Total Rows' => $importedRecords,
158 'Records Imported' => $importedRecords,
159 );
160
161 foreach ($checkSummary as $label => $value) {
162 $this->verifyText("xpath=//table[@id='summary-counts']/tbody/tr/td[text()='{$label}']/following-sibling::td", preg_quote($value));
163 }
164 }
165
166 /*
167 * Function to test contact import.
168 *
169 * @params array $headers csv data headers
170 * @params array $rows csv data rows
171 * @params string $contactType contact type
172 * @params string $mode import mode
173 * @params array $fieldMapper select mapper fields while import
174 * @params array $other other parameters
175 * contactSubtype : import for selected Contact Subtype
176 * useMappingName : to reuse mapping
177 * dateFormat : date format of data
178 * checkMapperHeaders : to override default check mapper headers
179 * saveMapping : save current mapping?
180 * saveMappingName : to override mapping name
181 * createGroup : create new group?
182 * createGroupName : to override new Group name
183 * createTag : create new tag?
184 * createTagName : to override new Tag name
185 * selectGroup : select existing group for contacts
186 * selectTag : select existing tag for contacts
187 * callbackImportSummary : function to override default import summary assertions
188 *
189 * @params string $type import type (csv/sql)
190 * @todo:currently only supports csv, need to work on sql import
191 */
192 function importContacts($headers, $rows, $contactType = 'Individual', $mode = 'Skip', $fieldMapper = array(
193 ), $other = array(), $type = 'csv') {
194
195 // Go to contact import page.
196 $this->openCiviPage("import/contact", "reset=1", "uploadFile");
197
198 $originalHeaders = $headers;
199 $originalRows = $rows;
200
201 // format headers and row to import contacts with relationship data.
202 $this->_formatContactCSVdata($headers, $rows);
203
204 // Create csv file of sample data.
205 $csvFile = $this->webtestCreateCSV($headers, $rows);
206
207 // Attach csv file.
208 $this->webtestAttachFile('uploadFile', $csvFile);
209
210 // First row is header.
211 $this->click('skipColumnHeader');
212
213 // select mode, default is 'Skip'.
214 if ($mode == 'Update') {
215 $this->click("CIVICRM_QFID_4_4");
216 }
217 elseif ($mode == 'Fill') {
218 $this->click("CIVICRM_QFID_8_6");
219 }
220 elseif ($mode == 'No Duplicate Checking') {
221 $this->click("CIVICRM_QFID_16_8");
222 }
223
224 // select contact type, default is 'Individual'.
225 if ($contactType == 'Organization') {
226 $this->click("CIVICRM_QFID_4_14");
227 }
228 elseif ($contactType == 'Household') {
229 $this->click("CIVICRM_QFID_2_12");
230 }
231
232 // Select contact subtype
233 if (isset($other['contactSubtype'])) {
234 if ($contactType != 'Individual') {
235 // Because it tends to cause problems, all uses of sleep() must be justified in comments
236 // Sleep should never be used for wait for anything to load from the server
237 // FIXME: this is bad, using sleep to wait for AJAX
238 // Need to use a better way to wait for contact subtypes to repopulate
239 sleep(5);
240 }
241 $this->waitForElementPresent("subType");
242 $this->select('subType', 'label=' . $other['contactSubtype']);
243 }
244
245 if (isset($other['dedupe'])) {
246 $this->waitForElementPresent("dedupe");
247 $this->select('dedupe', 'value=' . $other['dedupe']);
248 }
249
250 // Use already created mapping
251 $existingMapping = NULL;
252 if (isset($other['useMappingName'])) {
253 $this->select('savedMapping', "label=" . $other['useMappingName']);
254 $existingMapping = $other['useMappingName'];
255 }
256
257 // Date format, default: yyyy-mm-dd OR yyyymmdd
258 if (isset($other['dateFormat'])) {
259 // default
260 $dateFormatMapper = array(
261 'yyyy-mm-dd OR yyyymmdd' => "CIVICRM_QFID_1_16",
262 'mm/dd/yy OR mm-dd-yy' => "CIVICRM_QFID_2_18",
263 'mm/dd/yyyy OR mm-dd-yyyy' => "CIVICRM_QFID_4_20",
264 'Month dd, yyyy' => "CIVICRM_QFID_8_22",
265 'dd-mon-yy OR dd/mm/yy' => "CIVICRM_QFID_16_24",
266 'dd/mm/yyyy' => "CIVICRM_QFID_32_26",
267 );
268 $this->click($dateFormatMapper[$other['dateFormat']]);
269 }
270
271 // Submit form.
272 $this->click('_qf_DataSource_upload');
273 $this->waitForPageToLoad($this->getTimeoutMsec());
274
275 if (isset($other['checkMapperHeaders'])) {
276 $checkMapperHeaders = $other['checkMapperHeaders'];
277 }
278 else {
279 $checkMapperHeaders = array(
280 1 => 'Column Names',
281 2 => 'Import Data (row 1)',
282 3 => 'Import Data (row 2)',
283 4 => 'Matching CiviCRM Field',
284 );
285 }
286
287 // Check mapping data.
288 $this->_checkImportMapperData($headers, $rows, $existingMapping, $checkMapperHeaders, 'td');
289
290 // Select matching field for cvs data.
291 if (!empty($fieldMapper)) {
292 foreach ($fieldMapper as $field => $value) {
293 $this->select($field, "value={$value}");
294 }
295 }
296
297 // Save mapping
298 if (isset($other['saveMapping'])) {
299 $mappingName = isset($other['saveMappingName']) ? $other['saveMappingName'] : 'ContactImport_' . substr(sha1(rand()), 0, 7);
300 $this->click('saveMapping');
301 $this->type('saveMappingName', $mappingName);
302 $this->type('saveMappingDesc', "Mapping for {$contactType}");
303 }
304
305 // Submit form.
306 $this->click('_qf_MapField_next');
307 $this->waitForPageToLoad($this->getTimeoutMsec());
308
309 // Check mapping data.
310 $this->_checkImportMapperData($headers, $rows, $existingMapping, $checkMapperHeaders, 'td');
311
312 // Add imported contacts in new group.
313 $groupName = NULL;
314 $existingGroups = array();
315 if (isset($other['createGroup'])) {
316 $groupName = isset($other['createGroupName']) ? $other['createGroupName'] : 'ContactImport_' . substr(sha1(rand()), 0, 7);
317
318 $this->click("css=#new-group div.crm-accordion-header");
319 $this->type('newGroupName', $groupName);
320 $this->type('newGroupDesc', "Group For {$contactType}");
321 }
322 if (isset($other['selectGroup'])) {
323 // reuse existing groups.
324 if (is_array($other['selectGroup'])) {
325 foreach ($other['selectGroup'] as $existingGroup) {
326 $this->select('groups[]', 'label=' . $existingGroup);
327 $existingGroups[] = $existingGroup;
328 }
329 }
330 else {
331 $this->select('groups[]', 'label=' . $other['selectGroup']);
332 $existingGroups[] = $other['selectGroup'];
333 }
334 }
335
336 // Assign new tag to the imported contacts.
337 $tagName = NULL;
338 $existingTags = array();
339 if (isset($other['createTag'])) {
340 $tagName = isset($other['createTagName']) ? $other['createTagName'] : "{$contactType}_" . substr(sha1(rand()), 0, 7);
341
342 $this->click("css=#new-tag div.crm-accordion-header");
343 $this->type('newTagName', $tagName);
344 $this->type('newTagDesc', "Tag for {$contactType}");
345 }
346 if (isset($other['selectTag'])) {
347 $this->click("css=#existing-tags div.crm-accordion-header");
348 // reuse existing tags.
349 if (is_array($other['selectTag'])) {
350 foreach ($other['selectTag'] as $existingTag) {
351 $this->click("xpath=//div[@id='existing-tags']//div[@class='crm-accordion-body']//label[text()='{$existingTag}']");
352 $existingTags[] = $existingTag;
353 }
354 }
355 else {
356 $this->click("xpath=//div[@id='existing-tags']//div[@class='crm-accordion-body']//label[text()='" . $other['selectTag'] . "']");
357 $existingTags[] = $other['selectTag'];
358 }
359 }
360
361 // Submit form.
362 $this->click('_qf_Preview_next');
363 $this->waitForPageToLoad($this->getTimeoutMsec());
364
365 // Check confirmation alert.
366 $this->assertTrue((bool)preg_match("/^Are you sure you want to Import now[\s\S]$/", $this->getConfirmation()));
367 $this->chooseOkOnNextConfirmation();
368 $this->waitForPageToLoad($this->getTimeoutMsec());
369
370 // Visit summary page.
371 $this->waitForElementPresent("_qf_Summary_next");
372
373 // Check success message.
374 $this->assertTrue($this->isTextPresent("Import has completed successfully. The information below summarizes the results."));
375
376 // Check summary Details.
377 $importedContacts = $totalRows = count($originalRows);
378
379 // Include relationships contacts ( if exists )
380 if (isset($originalHeaders['contact_relationships']) && is_array($originalHeaders['contact_relationships'])) {
381 foreach ($originalRows as $row) {
382 $importedContacts += count($row['contact_relationships']);
383 }
384 }
385
386 $importedContactsCount = ($importedContacts == 1) ? 'One contact' : "$importedContacts contacts";
387 $taggedContactsCount = ($importedContacts == 1) ? 'One contact is' : "$importedContacts contacts are";
388 $checkSummary = array(
389 'Total Rows' => $totalRows,
390 'Total Contacts' => $importedContacts,
391 );
392
393 if ($groupName) {
394 $checkSummary['Import to Groups'] = "{$groupName}: {$importedContactsCount} added to this new group.";
395 }
396
397 if ($tagName) {
398 $checkSummary['Tagged Imported Contacts'] = "{$tagName}: {$taggedContactsCount} tagged with this tag.";
399 }
400
401 if ($existingGroups) {
402 if (!isset($checkSummary['Import to Groups'])) {
403 $checkSummary['Import to Groups'] = '';
404 }
405 foreach ($existingGroups as $existingGroup) {
406 $checkSummary['Import to Groups'] .= "{$existingGroup}: {$importedContactsCount} added to this existing group.";
407 }
408 }
409
410 if ($existingTags) {
411 if (!isset($checkSummary['Tagged Imported Contacts'])) {
412 $checkSummary['Tagged Imported Contacts'] = '';
413 }
414 foreach ($existingTags as $existingTag) {
415 $checkSummary['Tagged Imported Contacts'] .= "{$existingTag}: {$taggedContactsCount} tagged with this tag.";
416 }
417 }
418
419 if (!empty($other['callbackImportSummary']) && is_callable(array(
420 $this, $other['callbackImportSummary']))) {
421 $callbackImportSummary = $other['callbackImportSummary'];
422 $this->$callbackImportSummary($originalHeaders, $originalRows, $checkSummary);
423 }
424 else {
425 foreach ($checkSummary as $label => $value) {
426 $this->verifyText("xpath=//table[@id='summary-counts']/tbody/tr/td[text()='{$label}']/following-sibling::td", preg_quote($value));
427 }
428 }
429 }
430
431 /*
432 * Helper function to get the import url of the component.
433 *
434 * @params string $component component name
435 *
436 * @return string import url
437 */
438 function _getImportComponentUrl($component) {
439
440 $importComponentUrl = array(
441 'Event' => 'civicrm/event/import?reset=1',
442 'Contribution' => 'civicrm/contribute/import?reset=1',
443 'Membership' => 'civicrm/member/import?reset=1',
444 'Activity' => 'civicrm/import/activity?reset=1',
445 );
446
447 return $importComponentUrl[$component];
448 }
449
450 /*
451 * Helper function to get the import url of the component.
452 *
453 * @params string $component component name
454 *
455 * @return string import url
456 */
457 function _getImportComponentContactType($component, $contactType) {
458 $importComponentMode = array(
459 'Event' => array('Individual' => 'CIVICRM_QFID_1_8',
460 'Household' => 'CIVICRM_QFID_2_10',
461 'Organization' => 'CIVICRM_QFID_4_12',
462 ),
463 'Contribution' => array(
464 'Individual' => 'CIVICRM_QFID_1_6',
465 'Household' => 'CIVICRM_QFID_2_8',
466 'Organization' => 'CIVICRM_QFID_4_10',
467 ),
468 'Membership' => array(
469 'Individual' => 'CIVICRM_QFID_1_6',
470 'Household' => 'CIVICRM_QFID_2_8',
471 'Organization' => 'CIVICRM_QFID_4_10',
472 ),
473 );
474
475 return $importComponentMode[$component][$contactType];
476 }
477
478 /*
479 * Helper function to check import mapping fields.
480 *
481 * @params array $headers field headers
482 * @params array $rows field rows
483 * @params array $checkMapperHeaders override default mapper headers
484 */
485 function _checkImportMapperData($headers, $rows, $existingMapping = NULL, $checkMapperHeaders = array(
486 ), $headerSelector = 'th') {
487
488 if (empty($checkMapperHeaders)) {
489 $checkMapperHeaders = array(
490 1 => 'Column Headers',
491 2 => 'Import Data (row 2)',
492 3 => 'Import Data (row 3)',
493 4 => 'Matching CiviCRM Field',
494 );
495 }
496
497 $rowNumber = 1;
498 if ($existingMapping) {
499 $this->verifyText("xpath=//div[@id='map-field']//table[1]/tbody/tr[{$rowNumber}]/th[1]", preg_quote("Saved Field Mapping: {$existingMapping}"));
500 $rowNumber++;
501 }
502
503 foreach ($checkMapperHeaders as $rownum => $value) {
504 $this->verifyText("xpath=//div[@id='map-field']//table[1]/tbody/tr[{$rowNumber}]/{$headerSelector}[{$rownum}]", preg_quote($value));
505 }
506 $rowNumber++;
507
508 foreach ($headers as $field => $header) {
509 $this->verifyText("xpath=//div[@id='map-field']//table[1]/tbody/tr[{$rowNumber}]/td[1]", preg_quote($header));
510 $colnum = 2;
511 foreach ($rows as $row) {
512 $this->verifyText("xpath=//div[@id='map-field']//table[1]/tbody/tr[{$rowNumber}]/td[{$colnum}]", preg_quote($row[$field]));
513 $colnum++;
514 }
515 $rowNumber++;
516 }
517 }
518
519 /*
520 * Helper function to get imported contact ids.
521 *
522 * @params array $rows fields rows
523 * @params string $contactType contact type
524 *
525 * @return array $contactIds imported contact ids
526 */
527 function _getImportedContactIds($rows, $contactType = 'Individual') {
528 $contactIds = array();
529
530 foreach ($rows as $row) {
531 $searchName = '';
532
533 // Build search name.
534 if ($contactType == 'Individual') {
535 $searchName = "{$row['last_name']}, {$row['first_name']}";
536 }
537 elseif ($contactType == 'Organization') {
538 $searchName = $row['organization_name'];
539 }
540 elseif ($contactType == 'Household') {
541 $searchName = $row['household_name'];
542 }
543
544 $this->openCiviPage("dashboard", "reset=1");
545
546 // Type search name in autocomplete.
547 $this->click("css=input#sort_name_navigation");
548 $this->type("css=input#sort_name_navigation", $searchName);
549 $this->typeKeys("css=input#sort_name_navigation", $searchName);
550
551 // Wait for result list.
552 $this->waitForElementPresent("css=div.ac_results-inner li");
553
554 // Visit contact summary page.
555 $this->click("css=div.ac_results-inner li");
556 $this->waitForPageToLoad($this->getTimeoutMsec());
557
558 // Get contact id from url.
559 $matches = array();
560 preg_match('/cid=([0-9]+)/', $this->getLocation(), $matches);
561 $contactIds[] = $matches[1];
562 }
563
564 return $contactIds;
565 }
566
567 /*
568 * Helper function to format headers and rows for contact relationship data.
569 *
570 * @params array $headers data headers
571 * @params string $rows data rows
572 */
573 function _formatContactCSVdata(&$headers, &$rows) {
574 if (!isset($headers['contact_relationships'])) {
575 return;
576 }
577
578 $relationshipHeaders = $headers['contact_relationships'];
579 unset($headers['contact_relationships']);
580
581 if (empty($relationshipHeaders) || !is_array($relationshipHeaders)) {
582 return;
583 }
584
585 foreach ($relationshipHeaders as $relationshipHeader) {
586 $headers = array_merge($headers, $relationshipHeader);
587 }
588
589 foreach ($rows as & $row) {
590 $relationshipRows = $row['contact_relationships'];
591 unset($row['contact_relationships']);
592 foreach ($relationshipRows as $relationshipRow) {
593 $row = array_merge($row, $relationshipRow);
594 }
595 }
596 }
597 }
598