[NFC] Update Pledge XML to support date picker conversion
[civicrm-core.git] / tests / phpunit / CRM / Export / BAO / ExportTest.php
CommitLineData
ef51caa8 1<?php
ef51caa8 2
3/**
4 * Class CRM_Core_DAOTest
46312f7d 5 *
acb109b7 6 * @group headless
ef51caa8 7 */
8class CRM_Export_BAO_ExportTest extends CiviUnitTestCase {
9
d989f2a4 10 use CRMTraits_Custom_CustomDataTrait;
12a36993 11 /**
12 * Contact IDs created for testing.
13 *
14 * @var array
15 */
c7224305 16 protected $contactIDs = [];
12a36993 17
18 /**
19 * Contribution IDs created for testing.
20 *
21 * @var array
22 */
c7224305 23 protected $contributionIDs = [];
24
25 /**
26 * Contribution IDs created for testing.
27 *
28 * @var array
29 */
30 protected $activityIDs = [];
12a36993 31
0a3b035f 32 /**
33 * Contribution IDs created for testing.
34 *
35 * @var array
36 */
37 protected $membershipIDs = [];
38
77544f6f
SV
39 /**
40 * Master Address ID created for testing.
41 *
42 * @var int
43 */
44 protected $masterAddressID;
45
a16a432a 46 protected $locationTypes = [];
47
68989e71 48 /**
49 * Processor generated in test.
50 *
51 * @var \CRM_Export_BAO_ExportProcessor
52 */
53 protected $processor;
54
c746d2b2 55 /**
56 * Csv output from export.
57 *
58 * @var \League\Csv\Reader
59 */
60 protected $csv;
61
68989e71 62 /**
63 * Cleanup data.
64 *
65 * @throws \Exception
66 */
4d251131 67 public function tearDown() {
68989e71 68 $this->quickCleanUpFinancialEntities();
0a3b035f 69 $this->quickCleanup([
70 'civicrm_contact',
71 'civicrm_email',
c2aec6ad 72 'civicrm_phone',
73 'civicrm_im',
74 'civicrm_website',
0a3b035f 75 'civicrm_address',
76 'civicrm_relationship',
77 'civicrm_membership',
78 'civicrm_case',
79 'civicrm_case_contact',
80 'civicrm_case_activity',
07b892fc 81 'civicrm_campaign',
0a3b035f 82 ]);
68989e71 83
a16a432a 84 if (!empty($this->locationTypes)) {
85 $this->callAPISuccess('LocationType', 'delete', ['id' => $this->locationTypes['Whare Kai']['id']]);
86 $this->callAPISuccess('LocationType', 'create', ['id' => $this->locationTypes['Main']['id'], 'name' => 'Main']);
87 }
68989e71 88 if ($this->processor && $this->processor->getTemporaryTable()) {
89 // delete the export temp table
90 CRM_Core_DAO::executeQuery("DROP TABLE IF EXISTS " . $this->processor->getTemporaryTable());
91 }
4d251131 92 parent::tearDown();
93 }
94
ef51caa8 95 /**
96 * Basic test to ensure the exportComponents function completes without error.
46312f7d 97 *
98 * @throws \CRM_Core_Exception
68989e71 99 * @throws \League\Csv\Exception
ef51caa8 100 */
12a36993 101 public function testExportComponentsNull() {
12a6fdef 102 $this->doExportTest([]);
ef51caa8 103 }
12a36993 104
105 /**
106 * Basic test to ensure the exportComponents function can export selected fields for contribution.
295549b4 107 *
108 * @throws \CRM_Core_Exception
c2aec6ad 109 * @throws \League\Csv\Exception
12a36993 110 */
111 public function testExportComponentsContribution() {
112 $this->setUpContributionExportData();
46312f7d 113 $selectedFields = [
e15ec147 114 ['contact_type' => 'Individual', 'name' => 'first_name'],
115 ['contact_type' => 'Individual', 'name' => 'last_name'],
116 ['name' => 'receive_date'],
117 ['name' => 'contribution_source'],
118 ['contact_type' => 'Individual', 'name' => 'street_address', 1],
119 ['contact_type' => 'Individual', 'name' => 'city', 1],
120 ['contact_type' => 'Individual', 'name' => 'country', 1],
121 ['contact_type' => 'Individual', 'name' => 'email', 1],
122 ['name' => 'trxn_id'],
46312f7d 123 ];
994a070c 124
295549b4 125 $this->doExportTest([
126 'ids' => $this->contributionIDs,
127 'order' => 'receive_date desc',
128 'fields' => $selectedFields,
129 'exportMode' => CRM_Export_Form_Select::CONTRIBUTE_EXPORT,
130 'componentClause' => 'civicrm_contribution.id IN ( ' . implode(',', $this->contributionIDs) . ')',
131 ]);
12a36993 132 }
c7224305 133
8535cfc6 134 /**
135 * Basic test to ensure the exportComponents function can export with soft credits enabled.
136 *
137 * @throws \CRM_Core_Exception
138 * @throws \League\Csv\Exception
139 */
140 public function testExportComponentsContributionSoftCredits() {
141 $this->setUpContributionExportData();
142 $this->callAPISuccess('ContributionSoft', 'create', ['contact_id' => $this->contactIDs[1], 'contribution_id' => $this->contributionIDs[0], 'amount' => 5]);
143 $params = [
c2aec6ad 144 ['receive_date_low', '=', '20160101000000', 0, 0],
8535cfc6 145 ['receive_date_high', '=', '20191231235959', 0, 0],
146 ['contribution_amount_low', '=', '1', 0, 0],
147 ['contribution_amount_high', '=', '10000000', 0, 0],
148 ['contribution_test', '=', '0', 0, 0],
149 ['contribution_or_softcredits', '=', 'both', 0, 0],
150 ];
151
c2aec6ad 152 $this->doExportTest([
153 'selectAll' => FALSE,
154 'ids' => $this->contributionIDs,
155 'params' => $params,
156 'order' => 'receive_date desc',
157 'exportMode' => CRM_Export_Form_Select::CONTRIBUTE_EXPORT,
158 'componentClause' => 'civicrm_contribution.id IN ( ' . implode(',', $this->contributionIDs) . ')',
159 ]);
160
161 $this->assertEquals(array_merge($this->getBasicHeaderDefinition(FALSE), $this->getContributeHeaderDefinition()), $this->csv->getHeader());
162 $this->assertCount(3, $this->csv);
163 $row = $this->csv->fetchOne(0);
164 $this->assertEquals(95, $row['Net Amount']);
165 $this->assertEquals('', $row['Soft Credit Amount']);
166 $row = $this->csv->fetchOne(1);
167 $this->assertEquals(95, $row['Net Amount']);
168 $this->assertEquals(5, $row['Soft Credit Amount']);
169 $this->assertEquals('Anderson, Anthony', $row['Soft Credit For']);
170 $this->assertEquals($this->contributionIDs[0], $row['Soft Credit For Contribution ID']);
171
8535cfc6 172 // Ideally we would use a randomised temp table name & use generic temp cleanup for cleanup - but
173 // for now just make sure we don't leave a mess.
174 CRM_Core_DAO::executeQuery('DROP TABLE IF EXISTS contribution_search_scredit_combined');
175
176 }
177
18711469 178 /**
179 * Basic test to ensure the exportComponents function can export selected fields for contribution.
65cfeed6 180 *
181 * @throws \CRM_Core_Exception
182 * @throws \League\Csv\Exception
18711469 183 */
184 public function testExportComponentsMembership() {
185 $this->setUpMembershipExportData();
65cfeed6 186 $this->doExportTest([
187 'selectAll' => TRUE,
188 'ids' => $this->membershipIDs,
189 'exportMode' => CRM_Export_Form_Select::MEMBER_EXPORT,
f03241be 190 'componentClause' => 'civicrm_membership.id IN ( ' . $this->ids['membership'] . ')',
65cfeed6 191 ]);
f03241be 192 $membership = $this->callAPISuccessGetSingle('Membership', ['id' => $this->ids['membership']]);
18711469 193
65cfeed6 194 $row = $this->csv->fetchOne();
195 $expected = [
196 'Contact ID' => $this->contactIDs[0],
197 'Contact Type' => 'Individual',
198 'Contact Subtype' => '',
199 'Do Not Email' => '',
200 'Do Not Phone' => '',
201 'Do Not Mail' => '',
202 'Do Not Sms' => '',
203 'Do Not Trade' => '',
204 'No Bulk Emails (User Opt Out)' => '',
205 'Legal Identifier' => '',
206 'External Identifier' => '',
207 'Sort Name' => 'Anderson, Anthony',
208 'Display Name' => 'Mr. Anthony Anderson II',
209 'Nickname' => '',
210 'Legal Name' => '',
211 'Image Url' => '',
212 'Preferred Communication Method' => '',
213 'Preferred Language' => 'en_US',
214 'Preferred Mail Format' => 'Both',
215 'Contact Hash' => '059023a02d27d4e7f285a40ee0e30be8',
216 'Contact Source' => '',
217 'First Name' => 'Anthony',
218 'Middle Name' => 'J.',
219 'Last Name' => 'Anderson',
220 'Individual Prefix' => 'Mr.',
221 'Individual Suffix' => 'II',
222 'Formal Title' => '',
223 'Communication Style' => 'Formal',
224 'Email Greeting ID' => '1',
225 'Postal Greeting ID' => '1',
226 'Addressee ID' => '1',
227 'Job Title' => '',
228 'Gender' => 'Female',
229 'Birth Date' => '',
230 'Deceased' => '',
231 'Deceased Date' => '',
232 'Household Name' => '',
233 'Organization Name' => '',
234 'Sic Code' => '',
235 'Unique ID (OpenID)' => '',
236 'Current Employer ID' => '',
237 'Contact is in Trash' => '',
238 'Created Date' => '2019-07-11 09:56:18',
239 'Modified Date' => '2019-07-11 09:56:19',
240 'Addressee' => 'Mr. Anthony J. Anderson II',
241 'Email Greeting' => 'Dear Anthony',
242 'Postal Greeting' => 'Dear Anthony',
243 'Current Employer' => '',
244 'Location Type' => 'Home',
245 'Street Address' => 'Ambachtstraat 23',
246 'Street Number' => '',
247 'Street Number Suffix' => '',
248 'Street Name' => '',
249 'Street Unit' => '',
250 'Supplemental Address 1' => '',
251 'Supplemental Address 2' => '',
252 'Supplemental Address 3' => '',
253 'City' => 'Brummen',
254 'Postal Code Suffix' => '',
255 'Postal Code' => '6971 BN',
256 'Latitude' => '',
257 'Longitude' => '',
258 'Address Name' => '',
259 'Master Address Belongs To' => '',
260 'County' => '',
261 'State' => '',
262 'Country' => 'Netherlands',
263 'Phone' => '',
264 'Phone Extension' => '',
265 'Phone Type' => '',
266 'Email' => 'home@example.com',
267 'On Hold' => '',
268 'Use for Bulk Mail' => '',
269 'Signature Text' => '',
270 'Signature Html' => '',
271 'IM Provider' => '',
272 'IM Screen Name' => '',
273 'OpenID' => '',
274 'World Region' => 'Europe and Central Asia',
275 'Website' => '',
276 'Membership Type' => 'General',
277 'Test' => '',
278 'Is Pay Later' => '',
f03241be 279 'Member Since' => $membership['join_date'],
280 'Membership Start Date' => $membership['start_date'],
281 'Membership Expiration Date' => $membership['end_date'],
65cfeed6 282 'Source' => 'Payment',
f03241be 283 'Membership Status' => 'New',
65cfeed6 284 'Membership ID' => '2',
285 'Primary Member ID' => '',
8f67d99a 286 'Max Related' => '',
287 'Membership Recurring Contribution' => 1,
65cfeed6 288 'Campaign ID' => '',
8f67d99a 289 'Status Override' => '',
f03241be 290 'Total Amount' => '200.00',
291 'Contribution Status' => 'Pending',
65cfeed6 292 'Date Received' => '2019-07-25 07:34:23',
f03241be 293 'Payment Method' => 'Check',
65cfeed6 294 'Transaction ID' => '',
295 ];
296 $this->assertExpectedOutput($expected, $row);
18711469 297 }
298
c7224305 299 /**
b20c1bb3 300 * Basic test to ensure the exportComponents function can export selected fields for activity
301 *
302 * @throws \CRM_Core_Exception
303 * @throws \League\Csv\Exception
c7224305 304 */
305 public function testExportComponentsActivity() {
306 $this->setUpActivityExportData();
46312f7d 307 $selectedFields = [
7d0e773c
CW
308 ['contact_type' => 'Individual', 'name' => 'display_name'],
309 ['contact_type' => 'Individual', 'relationship_type_id' => '5', 'relationship_direction' => 'a_b', 'name' => 'display_name'],
46312f7d 310 ];
c7224305 311
b20c1bb3 312 $this->doExportTest([
313 'ids' => $this->activityIDs,
314 'order' => '`activity_date_time` desc',
315 'fields' => $selectedFields,
316 'exportMode' => CRM_Export_Form_Select::ACTIVITY_EXPORT,
317 'componentClause' => 'civicrm_activity.id IN ( ' . implode(',', $this->activityIDs) . ')',
318 ]);
319 $row = $this->csv->fetchOne();
320 $this->assertEquals($this->activityIDs[0], $row['Activity ID']);
c7224305 321 }
322
12a36993 323 /**
324 * Test the function that extracts the arrays used to structure the output.
325 *
326 * The keys in the output fields array should by matched by field aliases in the sql query (with
327 * exceptions of course - currently country is one - although maybe a future refactor can change that!).
328 *
329 * We are trying to move towards simpler processing in the per row iteration as that may be
330 * repeated 100,000 times and in general we should simply be able to match the query fields to
331 * our expected rows & do a little pseudoconstant mapping.
332 */
333 public function testGetExportStructureArrays() {
334 // This is how return properties are formatted internally within the function for passing to the BAO query.
46312f7d 335 $returnProperties = [
12a36993 336 'first_name' => 1,
337 'last_name' => 1,
338 'receive_date' => 1,
339 'contribution_source' => 1,
46312f7d 340 'location' => [
341 'Home' => [
12a36993 342 'street_address' => 1,
343 'city' => 1,
344 'country' => 1,
345 'email' => 1,
346 'im-1' => 1,
347 'im_provider' => 1,
348 'phone-1' => 1,
46312f7d 349 ],
350 ],
12a36993 351 'phone' => 1,
352 'trxn_id' => 1,
353 'contribution_id' => 1,
46312f7d 354 ];
efc76c0a 355
46312f7d 356 $query = new CRM_Contact_BAO_Query([], $returnProperties, NULL,
12a36993 357 FALSE, FALSE, CRM_Contact_BAO_Query::MODE_CONTRIBUTE,
358 FALSE, TRUE, TRUE, NULL, 'AND'
359 );
360
361 list($select) = $query->query();
362 $pattern = '/as `?([^`,]*)/';
46312f7d 363 $queryFieldAliases = [];
12a36993 364 preg_match_all($pattern, $select, $queryFieldAliases, PREG_PATTERN_ORDER);
d41ab886 365 $processor = new CRM_Export_BAO_ExportProcessor(CRM_Contact_BAO_Query::MODE_CONTRIBUTE, NULL, 'AND');
adabfa40 366 $processor->setQueryFields($query->_fields);
6df77635 367 $processor->setReturnProperties($returnProperties);
12a36993 368
44fc53ab 369 list($outputFields) = $processor->getExportStructureArrays();
12a36993 370 foreach (array_keys($outputFields) as $fieldAlias) {
6df77635 371 if ($fieldAlias === 'Home-country') {
12a36993 372 $this->assertTrue(in_array($fieldAlias . '_id', $queryFieldAliases[1]), 'Country is subject to some funky translate so we make sure country id is present');
373 }
374 else {
375 $this->assertTrue(in_array($fieldAlias, $queryFieldAliases[1]), 'looking for field ' . $fieldAlias . ' in generaly the alias fields need to match the outputfields');
376 }
377 }
378
379 }
380
381 /**
382 * Set up some data for us to do testing on.
c2aec6ad 383 *
384 * @throws \CRM_Core_Exception
12a36993 385 */
386 public function setUpContributionExportData() {
387 $this->setUpContactExportData();
c2aec6ad 388 $this->contributionIDs[] = $this->contributionCreate(['contact_id' => $this->contactIDs[0], 'trxn_id' => 'null', 'invoice_id' => 'null', 'receive_date' => '2019-07-25 07:34:23']);
389 $this->contributionIDs[] = $this->contributionCreate(['contact_id' => $this->contactIDs[1], 'trxn_id' => 'null', 'invoice_id' => 'null', 'receive_date' => '2018-12-01 00:00:00']);
12a36993 390 }
391
0a3b035f 392 /**
393 * Set up some data for us to do testing on.
c2aec6ad 394 *
395 * @throws \CRM_Core_Exception
0a3b035f 396 */
397 public function setUpMembershipExportData() {
398 $this->setUpContactExportData();
18711469 399 // Create an extra so we don't get false passes due to 1
400 $this->contactMembershipCreate(['contact_id' => $this->contactIDs[0]]);
f03241be 401
402 $this->_contactID = $this->contactIDs[0];
403 $this->_invoiceID = 1234;
404 $this->_contributionPageID = NULL;
405 $this->_paymentProcessorID = $this->paymentProcessorCreate();
406 $this->setupMembershipRecurringPaymentProcessorTransaction();
407
408 $membershipID = $this->callAPISuccessGetValue('Membership', ['return' => 'id', 'contact_id' => $this->_contactID, 'options' => ['limit' => 1, 'sort' => 'id DESC']]);
409
410 $this->membershipIDs[] = $membershipID;
0a3b035f 411 }
412
413 /**
414 * Set up data to test case export.
f03241be 415 *
416 * @throws \CRM_Core_Exception
0a3b035f 417 */
418 public function setupCaseExportData() {
419 $contactID1 = $this->individualCreate();
46312f7d 420 $contactID2 = $this->individualCreate([], 1);
0a3b035f 421
46312f7d 422 $case = $this->callAPISuccess('case', 'create', [
0a3b035f 423 'case_type_id' => 1,
424 'subject' => 'blah',
425 'contact_id' => $contactID1,
46312f7d 426 ]);
0a3b035f 427 $this->callAPISuccess('CaseContact', 'create', [
428 'case_id' => $case['id'],
429 'contact_id' => $contactID2,
430 ]);
431 }
432
c7224305 433 /**
434 * Set up some data for us to do testing on.
b20c1bb3 435 *
436 * @throws \CRM_Core_Exception
c7224305 437 */
438 public function setUpActivityExportData() {
439 $this->setUpContactExportData();
46312f7d 440 $this->activityIDs[] = $this->activityCreate(['contact_id' => $this->contactIDs[0]])['id'];
c7224305 441 }
442
12a36993 443 /**
444 * Set up some data for us to do testing on.
c2aec6ad 445 *
446 * @throws \CRM_Core_Exception
12a36993 447 */
448 public function setUpContactExportData() {
bab4f15e 449 $this->contactIDs[] = $contactA = $this->individualCreate(['gender_id' => 'Female']);
4d251131 450 // Create address for contact A.
46312f7d 451 $params = [
994a070c 452 'contact_id' => $contactA,
453 'location_type_id' => 'Home',
454 'street_address' => 'Ambachtstraat 23',
455 'postal_code' => '6971 BN',
456 'country_id' => '1152',
457 'city' => 'Brummen',
458 'is_primary' => 1,
46312f7d 459 ];
994a070c 460 $result = $this->callAPISuccess('address', 'create', $params);
461 $addressId = $result['id'];
462
46312f7d 463 $this->callAPISuccess('email', 'create', [
4d251131 464 'id' => $this->callAPISuccessGetValue('Email', ['contact_id' => $params['contact_id'], 'return' => 'id']),
465 'location_type_id' => 'Home',
466 'email' => 'home@example.com',
467 'is_primary' => 1,
46312f7d 468 ]);
469 $this->callAPISuccess('email', 'create', ['contact_id' => $params['contact_id'], 'location_type_id' => 'Work', 'email' => 'work@example.com', 'is_primary' => 0]);
4d251131 470
471 $params['is_primary'] = 0;
472 $params['location_type_id'] = 'Work';
473 $this->callAPISuccess('address', 'create', $params);
474 $this->contactIDs[] = $contactB = $this->individualCreate();
475
46312f7d 476 $this->callAPISuccess('address', 'create', [
994a070c 477 'contact_id' => $contactB,
478 'location_type_id' => "Home",
479 'master_id' => $addressId,
46312f7d 480 ]);
77544f6f 481 $this->masterAddressID = $addressId;
994a070c 482
4d251131 483 }
484
485 /**
486 * Test variants of primary address exporting.
487 *
488 * @param int $isPrimaryOnly
489 *
ebebd629 490 * @dataProvider getBooleanDataProvider
491 * @throws \CRM_Core_Exception
c746d2b2 492 * @throws \League\Csv\Exception
4d251131 493 */
494 public function testExportPrimaryAddress($isPrimaryOnly) {
495 \Civi::settings()->set('searchPrimaryDetailsOnly', $isPrimaryOnly);
496 $this->setUpContactExportData();
497
e15ec147 498 $selectedFields = [
499 ['contact_type' => 'Individual', 'name' => 'email'],
500 ['contact_type' => 'Individual', 'name' => 'email', 'location_type_id' => '1'],
501 ['contact_type' => 'Individual', 'name' => 'email', 'location_type_id' => '2'],
502 ];
c746d2b2 503 $this->doExportTest([
504 'ids' => [],
505 'params' => [['email', 'LIKE', 'c', 0, 1]],
506 'fields' => $selectedFields,
507 'componentClause' => "contact_a.id IN ({$this->contactIDs[0]}, {$this->contactIDs[1]})",
508 'selectAll' => TRUE,
509 ]);
4d251131 510
c746d2b2 511 $row = $this->csv->fetchOne();
512 $this->assertEquals([
513 'Email' => 'home@example.com',
514 'Home-Email' => 'home@example.com',
515 'Work-Email' => 'work@example.com',
516 ], $row);
517 $this->assertEquals(2, count($this->csv));
4d251131 518 \Civi::settings()->set('searchPrimaryDetailsOnly', FALSE);
519 }
520
bab4f15e 521 /**
522 * Test that when exporting a pseudoField it is reset for NULL entries.
523 *
524 * ie. we have a contact WITH a gender & one without - make sure the latter one
525 * does NOT retain the gender of the former.
46312f7d 526 *
527 * @throws \CRM_Core_Exception
714ab070 528 * @throws \League\Csv\Exception
bab4f15e 529 */
530 public function testExportPseudoField() {
531 $this->setUpContactExportData();
714ab070 532 $this->callAPISuccess('OptionValue', 'create', ['option_group_id' => 'gender', 'name' => 'Really long string', 'value' => 678, 'label' => 'Really long string']);
533 $selectedFields = [['contact_type' => 'Individual', 'name' => 'gender_id']];
534 $this->callAPISuccess('Contact', 'create', ['id' => $this->contactIDs[0], 'gender_id' => 678]);
535 $this->doExportTest(['fields' => $selectedFields, 'ids' => $this->contactIDs]);
536 $row = $this->csv->fetchOne();
537 $this->assertEquals('Really long string', $row['Gender']);
bab4f15e 538 }
539
540 /**
541 * Test that when exporting a pseudoField it is reset for NULL entries.
542 *
543 * This is specific to the example in CRM-14398
07b892fc 544 *
545 * @throws \CRM_Core_Exception
546 * @throws \League\Csv\Exception
bab4f15e 547 */
548 public function testExportPseudoFieldCampaign() {
549 $this->setUpContributionExportData();
07b892fc 550 $campaign = $this->callAPISuccess('Campaign', 'create', ['title' => 'Big campaign and kinda long too']);
551 $this->callAPISuccess('Contribution', 'create', ['campaign_id' => 'Big_campaign_and_kinda_long_too', 'id' => $this->contributionIDs[0]]);
7d0e773c
CW
552 $selectedFields = [
553 ['contact_type' => 'Individual', 'name' => 'gender_id'],
554 ['contact_type' => 'Contribution', 'name' => 'contribution_campaign_title'],
07b892fc 555 ['contact_type' => 'Contribution', 'name' => 'contribution_campaign_id'],
7d0e773c 556 ];
07b892fc 557 $this->doExportTest([
558 'ids' => [$this->contactIDs[1]],
559 'exportMode' => CRM_Export_Form_Select::CONTRIBUTE_EXPORT,
560 'fields' => $selectedFields,
561 'componentClause' => "contact_a.id IN (" . implode(",", $this->contactIDs) . ")",
562 ]);
563 $row = $this->csv->fetchOne();
564 $this->assertEquals('Big campaign and kinda long too', $row['Campaign Title']);
565 $this->assertEquals($campaign['id'], $row['Campaign ID']);
bab4f15e 566 }
567
ce12a9e0 568 /**
569 * Test exporting relationships.
ea71cb07 570 *
571 * @throws \CRM_Core_Exception
572 * @throws \League\Csv\Exception
ce12a9e0 573 */
574 public function testExportRelationships() {
cedeaa5c 575 $organization1 = $this->organizationCreate(['organization_name' => 'Org 1', 'legal_name' => 'pretty legal', 'contact_source' => 'friend who took a law paper once']);
ce12a9e0 576 $organization2 = $this->organizationCreate(['organization_name' => 'Org 2', 'legal_name' => 'well dodgey']);
577 $contact1 = $this->individualCreate(['employer_id' => $organization1, 'first_name' => 'one']);
578 $contact2 = $this->individualCreate(['employer_id' => $organization2, 'first_name' => 'one']);
579 $employerRelationshipTypeID = $this->callAPISuccessGetValue('RelationshipType', ['return' => 'id', 'label_a_b' => 'Employee of']);
580 $selectedFields = [
e15ec147 581 ['contact_type' => 'Individual', 'name' => 'first_name'],
582 ['contact_type' => 'Individual', 'relationship_type_id' => $employerRelationshipTypeID, 'relationship_direction' => 'a_b', 'name' => 'organization_name'],
583 ['contact_type' => 'Individual', 'relationship_type_id' => $employerRelationshipTypeID, 'relationship_direction' => 'a_b', 'name' => 'legal_name'],
584 ['contact_type' => 'Individual', 'relationship_type_id' => $employerRelationshipTypeID, 'relationship_direction' => 'a_b', 'name' => 'contact_source'],
ce12a9e0 585 ];
ea71cb07 586 $this->doExportTest([
587 'ids' => [$contact1, $contact2],
588 'componentClause' => "contact_a.id IN ( $contact1, $contact2 )",
589 'fields' => $selectedFields,
590 ]);
5359d42b 591
ea71cb07 592 $row = $this->csv->fetchOne();
593 $this->assertEquals('one', $row['First Name']);
594 $this->assertEquals('Org 1', $row['Employee of-Organization Name']);
595 $this->assertEquals('pretty legal', $row['Employee of-Legal Name']);
596 $this->assertEquals('friend who took a law paper once', $row['Employee of-Contact Source']);
597
598 $row = $this->csv->fetchOne(1);
599 $this->assertEquals('Org 2', $row['Employee of-Organization Name']);
600 $this->assertEquals('well dodgey', $row['Employee of-Legal Name']);
ce12a9e0 601 }
602
b0eb1d74 603 /**
604 * Test exporting relationships.
605 *
606 * This is to ensure that CRM-13995 remains fixed.
ebebd629 607 *
608 * @dataProvider getBooleanDataProvider
609 *
610 * @param bool $includeHouseHold
611 *
c46c033c 612 * @throws CRM_Core_Exception
07b892fc 613 * @throws \League\Csv\Exception
b0eb1d74 614 */
ebebd629 615 public function testExportRelationshipsMergeToHousehold($includeHouseHold) {
2593f7dc 616 list($householdID, $houseHoldTypeID) = $this->setUpHousehold();
b0eb1d74 617
ebebd629 618 if ($includeHouseHold) {
619 $this->contactIDs[] = $householdID;
620 }
b0eb1d74 621 $selectedFields = [
7d0e773c
CW
622 ['contact_type' => 'Individual', 'relationship_type_id' => $houseHoldTypeID, 'relationship_direction' => 'a_b', 'name' => 'state_province', 'location_type_id' => ''],
623 ['contact_type' => 'Individual', 'relationship_type_id' => $houseHoldTypeID, 'relationship_direction' => 'a_b', 'name' => 'city', 'location_type_id' => ''],
624 ['contact_type' => 'Individual', 'name' => 'city', 'location_type_id' => ''],
625 ['contact_type' => 'Individual', 'name' => 'state_province', 'location_type_id' => ''],
626 ['contact_type' => 'Individual', 'name' => 'contact_source', 'location_type_id' => ''],
b0eb1d74 627 ];
07b892fc 628 $this->doExportTest([
629 'ids' => $this->contactIDs,
630 'fields' => $selectedFields,
631 'mergeSameHousehold' => TRUE,
632 ]);
633 $row = $this->csv->fetchOne();
634 $this->assertEquals(1, count($this->csv));
635 $this->assertEquals('Portland', $row['City']);
636 $this->assertEquals('ME', $row['State']);
637 $this->assertEquals($householdID, $row['Household ID']);
638 $this->assertEquals('household sauce', $row['Contact Source']);
b0eb1d74 639 }
640
2593f7dc 641 /**
642 * Test exporting relationships.
381f3a66 643 *
644 * @throws \CRM_Core_Exception
645 * @throws \League\Csv\Exception
2593f7dc 646 */
647 public function testExportRelationshipsMergeToHouseholdAllFields() {
2593f7dc 648 list($householdID) = $this->setUpHousehold();
381f3a66 649 $this->doExportTest(['ids' => $this->contactIDs, 'mergeSameHousehold' => TRUE]);
650 $row = $this->csv->fetchOne();
651 $this->assertCount(1, $this->csv);
652 $this->assertEquals('Unit Test household', $row['Display Name']);
653 $this->assertEquals('Portland', $row['City']);
654 $this->assertEquals('ME', $row['State']);
655 $this->assertEquals($householdID, $row['Household ID']);
656 $this->assertEquals('Unit Test household', $row['Addressee']);
657 $this->assertEquals('Dear Unit Test household', $row['Postal Greeting']);
2593f7dc 658 }
659
3663269c 660 /**
d989f2a4 661 * Test custom data exporting.
662 *
663 * @throws \CRM_Core_Exception
664 * @throws \League\Csv\Exception
3663269c 665 */
666 public function testExportCustomData() {
667 $this->setUpContactExportData();
d989f2a4 668 $this->entity = 'Contact';
669 $this->createCustomGroupWithFieldsOfAllTypes();
3663269c 670 $this->callAPISuccess('Contact', 'create', [
671 'id' => $this->contactIDs[1],
d989f2a4 672 $this->getCustomFieldName('text') => 'BlahdeBlah',
673 $this->getCustomFieldName('country') => 'LA',
3663269c 674 'api.Address.create' => ['location_type_id' => 'Billing', 'city' => 'Waipu'],
675 ]);
676 $selectedFields = [
d989f2a4 677 ['name' => 'city', 'location_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Address', 'location_type_id', 'Billing')],
678 ['name' => $this->getCustomFieldName('text')],
679 ['name' => $this->getCustomFieldName('country')],
3663269c 680 ];
681
d989f2a4 682 $this->doExportTest([
683 'fields' => $selectedFields,
684 'ids' => [$this->contactIDs[1]],
685 ]);
686 $row = $this->csv->fetchOne();
687 $this->assertEquals('BlahdeBlah', $row['Enter text here']);
688 $this->assertEquals('Waipu', $row['Billing-City']);
689 $this->assertEquals("Lao People's Democratic Republic", $row['Country']);
3663269c 690 }
691
692 /**
693 * Attempt to do a fairly full export of location data.
9d7c8793 694 *
695 * @throws \CRM_Core_Exception
696 * @throws \League\Csv\Exception
3663269c 697 */
698 public function testExportIMData() {
699 // Use default providers.
700 $providers = ['AIM', 'GTalk', 'Jabber', 'MSN', 'Skype', 'Yahoo'];
a16a432a 701 // Main sure labels are not all anglo chars.
702 $this->diversifyLocationTypes();
703
704 $locationTypes = ['Billing' => 'Billing', 'Home' => 'Home', 'Main' => 'Méin', 'Other' => 'Other', 'Whare Kai' => 'Whare Kai'];
3663269c 705
706 $this->contactIDs[] = $this->individualCreate();
707 $this->contactIDs[] = $this->individualCreate();
708 $this->contactIDs[] = $this->householdCreate();
709 $this->contactIDs[] = $this->organizationCreate();
710 foreach ($this->contactIDs as $contactID) {
711 foreach ($providers as $provider) {
a16a432a 712 foreach ($locationTypes as $locationName => $locationLabel) {
3663269c 713 $this->callAPISuccess('IM', 'create', [
714 'contact_id' => $contactID,
a16a432a 715 'location_type_id' => $locationName,
3663269c 716 'provider_id' => $provider,
a16a432a 717 'name' => $locationName . $provider . $contactID,
3663269c 718 ]);
719 }
720 }
721 }
722
723 $relationships = [
724 $this->contactIDs[1] => ['label' => 'Spouse of'],
725 $this->contactIDs[2] => ['label' => 'Household Member of'],
39b959db 726 $this->contactIDs[3] => ['label' => 'Employee of'],
3663269c 727 ];
728
729 foreach ($relationships as $contactID => $relationshipType) {
730 $relationshipTypeID = $this->callAPISuccess('RelationshipType', 'getvalue', ['label_a_b' => $relationshipType['label'], 'return' => 'id']);
731 $result = $this->callAPISuccess('Relationship', 'create', [
732 'contact_id_a' => $this->contactIDs[0],
733 'relationship_type_id' => $relationshipTypeID,
39b959db 734 'contact_id_b' => $contactID,
3663269c 735 ]);
736 $relationships[$contactID]['id'] = $result['id'];
737 $relationships[$contactID]['relationship_type_id'] = $relationshipTypeID;
738 }
739
740 $fields = [['Individual', 'contact_id']];
741 // ' ' denotes primary location type.
a16a432a 742 foreach (array_keys(array_merge($locationTypes, [' ' => ['Primary']])) as $locationType) {
3663269c 743 $fields[] = [
744 'Individual',
745 'im_provider',
746 CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_IM', 'location_type_id', $locationType),
747 ];
748 foreach ($relationships as $contactID => $relationship) {
749 $fields[] = [
750 'Individual',
751 $relationship['relationship_type_id'] . '_a_b',
752 'im_provider',
753 CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_IM', 'location_type_id', $locationType),
754 ];
755 }
756 foreach ($providers as $provider) {
757 $fields[] = [
758 'Individual',
759 'im',
760 CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_IM', 'location_type_id', $locationType),
761 CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_IM', 'provider_id', $provider),
762 ];
763 foreach ($relationships as $contactID => $relationship) {
764 $fields[] = [
765 'Individual',
766 $relationship['relationship_type_id'] . '_a_b',
767 'im',
768 CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_IM', 'location_type_id', $locationType),
769 CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_IM', 'provider_id', $provider),
770 ];
771 }
772 }
773 }
e15ec147 774
775 // @todo switch to just declaring the new format....
776 $mappedFields = [];
777 foreach ($fields as $field) {
778 $mappedFields[] = CRM_Core_BAO_Mapping::getMappingParams([], $field);
779 }
780 $this->doExportTest(['fields' => $mappedFields, 'ids' => [$this->contactIDs[0]]]);
9d7c8793 781
782 foreach ($this->csv->getRecords() as $row) {
783 $id = $row['contact_id'];
784 $this->assertEquals('AIM', $row['Billing-IM Provider']);
785 $this->assertEquals('AIM', $row['Whare Kai-IM Provider']);
786 $this->assertEquals('BillingJabber' . $id, $row['Billing-IM Screen Name-Jabber']);
787 $this->assertEquals('Whare KaiJabber' . $id, $row['Whare Kai-IM Screen Name-Jabber']);
788 $this->assertEquals('BillingSkype' . $id, $row['Billing-IM Screen Name-Skype']);
3663269c 789 foreach ($relationships as $relatedContactID => $relationship) {
9d7c8793 790 $this->assertEquals('BillingYahoo' . $relatedContactID, $row[$relationship['label'] . '-Billing-IM Screen Name-Yahoo']);
791 $this->assertEquals('Whare KaiJabber' . $relatedContactID, $row[$relationship['label'] . '-Whare Kai-IM Screen Name-Jabber']);
3663269c 792 }
793 }
3663269c 794 }
795
f797aeb8 796 /**
797 * Test phone data export.
798 *
799 * Less over the top complete than the im test.
45cee669 800 *
801 * @throws \CRM_Core_Exception
802 * @throws \League\Csv\Exception
f797aeb8 803 */
804 public function testExportPhoneData() {
805 $this->contactIDs[] = $this->individualCreate();
806 $this->contactIDs[] = $this->individualCreate();
807 $locationTypes = ['Billing' => 'Billing', 'Home' => 'Home'];
808 $phoneTypes = ['Mobile', 'Phone'];
809 foreach ($this->contactIDs as $contactID) {
810 $this->callAPISuccess('Phone', 'create', [
811 'contact_id' => $contactID,
812 'location_type_id' => 'Billing',
813 'phone_type_id' => 'Mobile',
001a515b 814 'phone' => 'Billing' . 'Mobile' . $contactID,
f797aeb8 815 'is_primary' => 1,
816 ]);
817 $this->callAPISuccess('Phone', 'create', [
818 'contact_id' => $contactID,
819 'location_type_id' => 'Home',
820 'phone_type_id' => 'Phone',
001a515b 821 'phone' => 'Home' . 'Phone' . $contactID,
f797aeb8 822 ]);
823 }
824
825 $relationships = [
39b959db 826 $this->contactIDs[1] => ['label' => 'Spouse of'],
f797aeb8 827 ];
828
829 foreach ($relationships as $contactID => $relationshipType) {
830 $relationshipTypeID = $this->callAPISuccess('RelationshipType', 'getvalue', ['label_a_b' => $relationshipType['label'], 'return' => 'id']);
831 $result = $this->callAPISuccess('Relationship', 'create', [
832 'contact_id_a' => $this->contactIDs[0],
833 'relationship_type_id' => $relationshipTypeID,
39b959db 834 'contact_id_b' => $contactID,
f797aeb8 835 ]);
836 $relationships[$contactID]['id'] = $result['id'];
837 $relationships[$contactID]['relationship_type_id'] = $relationshipTypeID;
838 }
839
840 $fields = [['Individual', 'contact_id']];
841 // ' ' denotes primary location type.
842 foreach (array_keys(array_merge($locationTypes, [' ' => ['Primary']])) as $locationType) {
843 $fields[] = [
844 'Individual',
845 'phone',
846 CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Phone', 'location_type_id', $locationType),
847 ];
848 $fields[] = [
849 'Individual',
850 'phone_type_id',
851 CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Phone', 'location_type_id', $locationType),
852 ];
853 foreach ($relationships as $contactID => $relationship) {
854 $fields[] = [
855 'Individual',
856 $relationship['relationship_type_id'] . '_a_b',
857 'phone_type_id',
858 CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Phone', 'location_type_id', $locationType),
859 ];
860 }
861 foreach ($phoneTypes as $phoneType) {
862 $fields[] = [
863 'Individual',
864 'phone',
865 CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Phone', 'location_type_id', $locationType),
866 CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Phone', 'phone_type_id', $phoneType),
867 ];
868 foreach ($relationships as $contactID => $relationship) {
869 $fields[] = [
870 'Individual',
871 $relationship['relationship_type_id'] . '_a_b',
872 'phone_type_id',
873 CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Phone', 'location_type_id', $locationType),
874 CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Phone', 'phone_type_id', $phoneType),
875 ];
876 }
877 }
878 }
e15ec147 879 // @todo switch to just declaring the new format....
880 $mappedFields = [];
881 foreach ($fields as $field) {
882 $mappedFields[] = CRM_Core_BAO_Mapping::getMappingParams([], $field);
883 }
884 $this->doExportTest(['fields' => $mappedFields, 'ids' => [$this->contactIDs[0]]]);
45cee669 885 foreach ($this->csv->getRecords() as $row) {
886 $this->assertEquals('BillingMobile3', $row['Billing-Phone-Mobile']);
887 $this->assertEquals('', $row['Billing-Phone-Phone']);
888 $this->assertEquals('Phone', $row['Spouse of-Phone Type']);
889 $this->assertEquals('Mobile', $row['Phone Type']);
890 $this->assertEquals('Mobile', $row['Billing-Phone Type']);
f797aeb8 891 }
892 }
893
a16a432a 894 /**
895 * Export City against multiple location types.
68f97b77 896 *
897 * @throws \CRM_Core_Exception
898 * @throws \League\Csv\Exception
a16a432a 899 */
900 public function testExportAddressData() {
901 $this->diversifyLocationTypes();
902
903 $locationTypes = ['Billing' => 'Billing', 'Home' => 'Home', 'Main' => 'Méin', 'Other' => 'Other', 'Whare Kai' => 'Whare Kai'];
904
905 $this->contactIDs[] = $this->individualCreate();
906 $this->contactIDs[] = $this->individualCreate();
907 $this->contactIDs[] = $this->householdCreate();
908 $this->contactIDs[] = $this->organizationCreate();
68f97b77 909 $fields = [['name' => 'contact_id']];
a16a432a 910 foreach ($this->contactIDs as $contactID) {
911 foreach ($locationTypes as $locationName => $locationLabel) {
912 $this->callAPISuccess('Address', 'create', [
913 'contact_id' => $contactID,
914 'location_type_id' => $locationName,
915 'street_address' => $locationLabel . $contactID . 'street_address',
916 'city' => $locationLabel . $contactID . 'city',
917 'postal_code' => $locationLabel . $contactID . 'postal_code',
918 ]);
68f97b77 919 $fields[] = ['contact_type' => 'Individual', 'name' => 'city', 'location_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Address', 'location_type_id', $locationName)];
920 $fields[] = ['contact_type' => 'Individual', 'name' => 'street_address', 'location_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Address', 'location_type_id', $locationName)];
921 $fields[] = ['contact_type' => 'Individual', 'name' => 'postal_code', 'location_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Address', 'location_type_id', $locationName)];
a16a432a 922 }
923 }
924
925 $relationships = [
926 $this->contactIDs[1] => ['label' => 'Spouse of'],
927 $this->contactIDs[2] => ['label' => 'Household Member of'],
39b959db 928 $this->contactIDs[3] => ['label' => 'Employee of'],
a16a432a 929 ];
930
931 foreach ($relationships as $contactID => $relationshipType) {
932 $relationshipTypeID = $this->callAPISuccess('RelationshipType', 'getvalue', ['label_a_b' => $relationshipType['label'], 'return' => 'id']);
933 $result = $this->callAPISuccess('Relationship', 'create', [
934 'contact_id_a' => $this->contactIDs[0],
935 'relationship_type_id' => $relationshipTypeID,
39b959db 936 'contact_id_b' => $contactID,
a16a432a 937 ]);
938 $relationships[$contactID]['id'] = $result['id'];
939 $relationships[$contactID]['relationship_type_id'] = $relationshipTypeID;
940 }
941
942 // ' ' denotes primary location type.
943 foreach (array_keys(array_merge($locationTypes, [' ' => ['Primary']])) as $locationType) {
944 foreach ($relationships as $contactID => $relationship) {
945 $fields[] = [
68f97b77 946 'contact_type' => 'Individual',
947 'relationship_type_id' => $relationship['relationship_type_id'],
948 'relationship_direction' => 'a_b',
949 'name' => 'city',
950 'location_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_IM', 'location_type_id', $locationType),
a16a432a 951 ];
952 }
953 }
68f97b77 954
955 $this->doExportTest(['fields' => $fields]);
956
957 foreach ($this->csv as $row) {
958 $this->assertEquals('Méin' . $row['contact_id'] . 'city', $row['Main-City']);
959 $this->assertEquals('Billing' . $row['contact_id'] . 'street_address', $row['Billing-Street Address']);
960 $this->assertEquals('Whare Kai' . $row['contact_id'] . 'postal_code', $row['Whare Kai-Postal Code']);
a16a432a 961 foreach ($relationships as $relatedContactID => $relationship) {
68f97b77 962 $value = ((int) $row['contact_id'] === $this->contactIDs[0]) ? 'Méin' . $relatedContactID . 'city' : '';
963 $this->assertEquals($value, $row[$relationship['label'] . '-Main-City'], 'checking ' . $relationship['label'] . '-Main-City');
a16a432a 964 }
965 }
966
967 $this->assertEquals([
968 'contact_id' => 'contact_id varchar(255)',
5ebd7d09 969 'billing_city' => 'billing_city varchar(64)',
970 'billing_street_address' => 'billing_street_address varchar(96)',
971 'billing_postal_code' => 'billing_postal_code varchar(64)',
972 'home_city' => 'home_city varchar(64)',
973 'home_street_address' => 'home_street_address varchar(96)',
974 'home_postal_code' => 'home_postal_code varchar(64)',
975 'main_city' => 'main_city varchar(64)',
976 'main_street_address' => 'main_street_address varchar(96)',
977 'main_postal_code' => 'main_postal_code varchar(64)',
978 'other_city' => 'other_city varchar(64)',
979 'other_street_address' => 'other_street_address varchar(96)',
980 'other_postal_code' => 'other_postal_code varchar(64)',
981 'whare_kai_city' => 'whare_kai_city varchar(64)',
982 'whare_kai_street_address' => 'whare_kai_street_address varchar(96)',
983 'whare_kai_postal_code' => 'whare_kai_postal_code varchar(64)',
984 '2_a_b_billing_city' => '2_a_b_billing_city varchar(64)',
985 '2_a_b_home_city' => '2_a_b_home_city varchar(64)',
986 '2_a_b_main_city' => '2_a_b_main_city varchar(64)',
987 '2_a_b_other_city' => '2_a_b_other_city varchar(64)',
988 '2_a_b_whare_kai_city' => '2_a_b_whare_kai_city varchar(64)',
989 '2_a_b_city' => '2_a_b_city varchar(64)',
990 '8_a_b_billing_city' => '8_a_b_billing_city varchar(64)',
991 '8_a_b_home_city' => '8_a_b_home_city varchar(64)',
992 '8_a_b_main_city' => '8_a_b_main_city varchar(64)',
993 '8_a_b_other_city' => '8_a_b_other_city varchar(64)',
994 '8_a_b_whare_kai_city' => '8_a_b_whare_kai_city varchar(64)',
995 '8_a_b_city' => '8_a_b_city varchar(64)',
996 '5_a_b_billing_city' => '5_a_b_billing_city varchar(64)',
997 '5_a_b_home_city' => '5_a_b_home_city varchar(64)',
998 '5_a_b_main_city' => '5_a_b_main_city varchar(64)',
999 '5_a_b_other_city' => '5_a_b_other_city varchar(64)',
1000 '5_a_b_whare_kai_city' => '5_a_b_whare_kai_city varchar(64)',
1001 '5_a_b_city' => '5_a_b_city varchar(64)',
68f97b77 1002 ], $this->processor->getSQLColumns());
a16a432a 1003 }
1004
4d251131 1005 /**
ffd511ee 1006 * Test master_address_id field when no merge is in play.
1007 *
1008 * @throws \CRM_Core_Exception
1009 * @throws \League\Csv\Exception
4d251131 1010 */
1011 public function testExportMasterAddress() {
1012 $this->setUpContactExportData();
1013
994a070c 1014 //export the master address for contact B
46312f7d 1015 $selectedFields = [
7d0e773c 1016 ['contact_type' => 'Individual', 'name' => 'master_id', 'location_type_id' => 1],
46312f7d 1017 ];
ffd511ee 1018 $this->doExportTest([
1019 'fields' => $selectedFields,
1020 'ids' => [$this->contactIDs[1]],
1021 ]);
1022 $row = $this->csv->fetchOne();
1023 $this->assertEquals(CRM_Contact_BAO_Contact::getMasterDisplayName($this->masterAddressID), $row['Home-Master Address Belongs To']);
994a070c 1024 }
1025
c2aec6ad 1026 /**
1027 * Test the merge same address option.
1028 *
1029 * @throws \CRM_Core_Exception
1030 * @throws \League\Csv\Exception
1031 */
1032 public function testMergeSameAddress() {
0b60a345 1033 $this->setUpContactSameAddressExportData();
1034 $this->doExportTest(['mergeSameAddress' => TRUE]);
c2aec6ad 1035 // ie 2 merged, one extra.
1036 $this->assertCount(2, $this->csv);
1037 $expected = [
1038 'Contact ID' => $this->contactIDs[0],
1039 'Contact Type' => 'Individual',
1040 'Contact Subtype' => '',
1041 'Do Not Email' => '',
1042 'Do Not Phone' => '',
1043 'Do Not Mail' => '',
1044 'Do Not Sms' => '',
1045 'Do Not Trade' => '',
1046 'No Bulk Emails (User Opt Out)' => '',
1047 'Legal Identifier' => '',
1048 'External Identifier' => '',
1049 'Sort Name' => 'Anderson, Anthony',
1050 'Display Name' => 'Mr. Anthony Anderson II',
1051 'Nickname' => '',
1052 'Legal Name' => '',
1053 'Image Url' => '',
1054 'Preferred Communication Method' => '',
1055 'Preferred Language' => 'en_US',
1056 'Preferred Mail Format' => 'Both',
1057 'Contact Hash' => 'e9bd0913cc05cc5aeae69ba04ee3be84',
1058 'Contact Source' => '',
1059 'First Name' => 'Anthony',
1060 'Middle Name' => 'J.',
1061 'Last Name' => 'Anderson',
1062 'Individual Prefix' => 'Mr.',
1063 'Individual Suffix' => 'II',
1064 'Formal Title' => '',
1065 'Communication Style' => 'Formal',
1066 'Email Greeting ID' => '1',
1067 'Postal Greeting ID' => '1',
1068 'Addressee ID' => '1',
1069 'Job Title' => '',
1070 'Gender' => 'Female',
1071 'Birth Date' => '',
1072 'Deceased' => '',
1073 'Deceased Date' => '',
1074 'Household Name' => '',
1075 'Organization Name' => '',
1076 'Sic Code' => '',
1077 'Unique ID (OpenID)' => '',
1078 'Current Employer ID' => '',
1079 'Contact is in Trash' => '',
1080 'Created Date' => '2019-07-11 10:28:15',
1081 'Modified Date' => '2019-07-11 10:28:15',
1082 'Addressee' => 'Mr. Anthony J. Anderson II, Dr. Sarah J. Smith II',
1083 'Email Greeting' => 'Dear Anthony, Sarah',
1084 'Postal Greeting' => 'Dear Anthony, Sarah',
1085 'Current Employer' => '',
1086 'Location Type' => 'Home',
1087 'Street Address' => 'Ambachtstraat 23',
1088 'Street Number' => '',
1089 'Street Number Suffix' => '',
1090 'Street Name' => '',
1091 'Street Unit' => '',
1092 'Supplemental Address 1' => '',
1093 'Supplemental Address 2' => '',
1094 'Supplemental Address 3' => '',
1095 'City' => 'Brummen',
1096 'Postal Code Suffix' => '',
1097 'Postal Code' => '6971 BN',
1098 'Latitude' => '',
1099 'Longitude' => '',
1100 'Address Name' => '',
1101 'Master Address Belongs To' => '',
1102 'County' => '',
1103 'State' => '',
1104 'Country' => 'Netherlands',
1105 'Phone' => '',
1106 'Phone Extension' => '',
1107 'Phone Type' => '',
1108 'Email' => 'home@example.com',
1109 'On Hold' => '',
1110 'Use for Bulk Mail' => '',
1111 'Signature Text' => '',
1112 'Signature Html' => '',
1113 'IM Provider' => '',
1114 'IM Screen Name' => '',
1115 'OpenID' => '',
1116 'World Region' => 'Europe and Central Asia',
1117 'Website' => '',
1118 'Group(s)' => '',
1119 'Tag(s)' => '',
1120 'Note(s)' => '',
1121 ];
1122 $this->assertExpectedOutput($expected, $this->csv->fetchOne());
1123 }
1124
0b60a345 1125 /**
1126 * Tests the options for greeting templates when choosing to merge same address.
1127 *
1128 * @throws \CRM_Core_Exception
1129 * @throws \League\Csv\Exception
1130 */
1131 public function testMergeSameAddressGreetingOptions() {
1132 $this->setUpContactSameAddressExportData();
1133 $this->callAPISuccess('OptionValue', 'create', [
1134 'option_group_id' => 'postal_greeting',
1135 'label' => '{contact.individual_suffix} {contact.last_name} and first is {contact.first_name} ',
1136 // This hard coded number makes it available for export use.
1137 'filter' => 4,
1138 ]);
1139 $this->doExportTest([
1140 'mergeSameAddress' => TRUE,
1141 'exportParams' => [
1142 'postal_greeting' => '2',
1143 'postal_greeting_other' => '',
1144 'addressee' => '2',
1145 'addressee_other' => 'random string {contact.display_name}',
1146 'mergeOption' => '1',
1147 'additional_group' => '',
1148 'mapping' => '',
1149 ],
1150 ]);
1151 $this->assertExpectedOutput([
1152 'Addressee' => 'random string Mr. Anthony Anderson II, Dr. Sarah Smith II',
1153 'Email Greeting' => 'II Anderson and first is Anthony , II Smith Sarah ',
1154 'Postal Greeting' => 'II Anderson and first is Anthony , II Smith Sarah ',
1155 ], $this->csv->fetchOne());
1156 // 3 contacts merged to 2.
1157 $this->assertCount(2, $this->csv);
1158 }
1159
6382c24c
LCA
1160 /**
1161 * Test that deceased and do not mail contacts are removed from contacts before
6d52bfe5 1162 *
1163 * @dataProvider getReasonsNotToMail
1164 *
1165 * @param array $reason
1166 * @param array $addressReason
46312f7d 1167 *
1168 * @throws \CRM_Core_Exception
efaa3367 1169 * @throws \League\Csv\Exception
6382c24c 1170 */
6d52bfe5 1171 public function testExportDeceasedDoNotMail($reason, $addressReason) {
46312f7d 1172 $contactA = $this->callAPISuccess('contact', 'create', [
6382c24c
LCA
1173 'first_name' => 'John',
1174 'last_name' => 'Doe',
1175 'contact_type' => 'Individual',
46312f7d 1176 ]);
6382c24c 1177
6d52bfe5 1178 $contactB = $this->callAPISuccess('contact', 'create', array_merge([
6382c24c
LCA
1179 'first_name' => 'Jane',
1180 'last_name' => 'Doe',
1181 'contact_type' => 'Individual',
6d52bfe5 1182 ], $reason));
6382c24c
LCA
1183
1184 //create address for contact A
6d52bfe5 1185 $this->callAPISuccess('address', 'create', [
6382c24c
LCA
1186 'contact_id' => $contactA['id'],
1187 'location_type_id' => 'Home',
1188 'street_address' => 'ABC 12',
1189 'postal_code' => '123 AB',
1190 'country_id' => '1152',
1191 'city' => 'ABC',
1192 'is_primary' => 1,
6d52bfe5 1193 ]);
6382c24c
LCA
1194
1195 //create address for contact B
6d52bfe5 1196 $this->callAPISuccess('address', 'create', array_merge([
6382c24c
LCA
1197 'contact_id' => $contactB['id'],
1198 'location_type_id' => 'Home',
1199 'street_address' => 'ABC 12',
1200 'postal_code' => '123 AB',
1201 'country_id' => '1152',
1202 'city' => 'ABC',
1203 'is_primary' => 1,
6d52bfe5 1204 ], $addressReason));
6382c24c 1205
efaa3367 1206 $this->doExportTest([
1207 'selectAll' => TRUE,
1208 'ids' => [$contactA['id'], $contactB['id']],
1209 'exportParams' => [
46312f7d 1210 'postal_mailing_export' => [
6382c24c 1211 'postal_mailing_export' => TRUE,
46312f7d 1212 ],
efaa3367 1213 'mergeSameAddress' => TRUE,
1214 ],
1215 ]);
1216 $row = $this->csv->fetchOne(0);
6382c24c 1217
efaa3367 1218 $this->assertTrue(!in_array('Stage', $this->processor->getHeaderRows()));
1219 $this->assertEquals('Dear John', $row['Email Greeting']);
1220 $this->assertCount(1, $this->csv);
6382c24c
LCA
1221 }
1222
6d52bfe5 1223 /**
1224 * Get reasons that a contact is not postalable.
46312f7d 1225 *
6d52bfe5 1226 * @return array
1227 */
1228 public function getReasonsNotToMail() {
1229 return [
1230 [['is_deceased' => 1], []],
1231 [['do_not_mail' => 1], []],
1232 [[], ['street_address' => '']],
1233 ];
1234 }
39b959db 1235
2593f7dc 1236 /**
46312f7d 1237 * Set up household for tests.
1238 *
2593f7dc 1239 * @return array
c46c033c 1240 *
1241 * @throws CRM_Core_Exception
2593f7dc 1242 */
1243 protected function setUpHousehold() {
1244 $this->setUpContactExportData();
1245 $householdID = $this->householdCreate([
cedeaa5c 1246 'source' => 'household sauce',
2593f7dc 1247 'api.Address.create' => [
1248 'city' => 'Portland',
1249 'state_province_id' => 'Maine',
39b959db
SL
1250 'location_type_id' => 'Home',
1251 ],
2593f7dc 1252 ]);
1253
1254 $relationshipTypes = $this->callAPISuccess('RelationshipType', 'get', [])['values'];
1255 $houseHoldTypeID = NULL;
1256 foreach ($relationshipTypes as $id => $relationshipType) {
1257 if ($relationshipType['name_a_b'] === 'Household Member of') {
1258 $houseHoldTypeID = $relationshipType['id'];
1259 }
1260 }
1261 $this->callAPISuccess('Relationship', 'create', [
1262 'contact_id_a' => $this->contactIDs[0],
1263 'contact_id_b' => $householdID,
1264 'relationship_type_id' => $houseHoldTypeID,
1265 ]);
1266 $this->callAPISuccess('Relationship', 'create', [
1267 'contact_id_a' => $this->contactIDs[1],
1268 'contact_id_b' => $householdID,
1269 'relationship_type_id' => $houseHoldTypeID,
1270 ]);
46312f7d 1271 return [$householdID, $houseHoldTypeID];
2593f7dc 1272 }
1273
0a3b035f 1274 /**
1275 * Ensure component is enabled.
1276 *
1277 * @param int $exportMode
1278 */
1279 public function ensureComponentIsEnabled($exportMode) {
1280 if ($exportMode === CRM_Export_Form_Select::CASE_EXPORT) {
1281 CRM_Core_BAO_ConfigSetting::enableComponent('CiviCase');
1282 }
1283 }
1284
ce14544c 1285 /**
1286 * Test our export all field metadata retrieval.
1287 *
1288 * @dataProvider additionalFieldsDataProvider
46312f7d 1289 *
ce14544c 1290 * @param int $exportMode
1291 * @param $expected
1292 */
1293 public function testAdditionalReturnProperties($exportMode, $expected) {
1294 $this->ensureComponentIsEnabled($exportMode);
1295 $processor = new CRM_Export_BAO_ExportProcessor($exportMode, NULL, 'AND');
1296 $metadata = $processor->getAdditionalReturnProperties();
1297 $this->assertEquals($expected, $metadata);
1298 }
1299
1300 /**
1301 * Test our export all field metadata retrieval.
1302 *
1303 * @dataProvider allFieldsDataProvider
46312f7d 1304 *
ce14544c 1305 * @param int $exportMode
1306 * @param $expected
1307 */
1308 public function testDefaultReturnProperties($exportMode, $expected) {
1309 $this->ensureComponentIsEnabled($exportMode);
1310 $processor = new CRM_Export_BAO_ExportProcessor($exportMode, NULL, 'AND');
1311 $metadata = $processor->getDefaultReturnProperties();
1312 $this->assertEquals($expected, $metadata);
1313 }
1314
1315 /**
1316 * Get fields returned from additionalFields function.
1317 *
1318 * @return array
1319 */
1320 public function additionalFieldsDataProvider() {
1321 return [
1322 [
1323 'anything that will then be defaulting ton contact',
1324 $this->getExtraReturnProperties(),
1325 ],
1326 [
1327 CRM_Export_Form_Select::ACTIVITY_EXPORT,
1328 array_merge($this->getExtraReturnProperties(), $this->getActivityReturnProperties()),
1329 ],
1330 [
1331 CRM_Export_Form_Select::CASE_EXPORT,
1332 array_merge($this->getExtraReturnProperties(), $this->getCaseReturnProperties()),
1333 ],
1334 [
1335 CRM_Export_Form_Select::CONTRIBUTE_EXPORT,
1336 array_merge($this->getExtraReturnProperties(), $this->getContributionReturnProperties()),
1337 ],
1338 [
1339 CRM_Export_Form_Select::EVENT_EXPORT,
1340 array_merge($this->getExtraReturnProperties(), $this->getEventReturnProperties()),
1341 ],
1342 [
1343 CRM_Export_Form_Select::MEMBER_EXPORT,
1344 array_merge($this->getExtraReturnProperties(), $this->getMembershipReturnProperties()),
1345 ],
1346 [
1347 CRM_Export_Form_Select::PLEDGE_EXPORT,
1348 array_merge($this->getExtraReturnProperties(), $this->getPledgeReturnProperties()),
1349 ],
1350
1351 ];
1352 }
1353
1354 /**
1355 * get data for testing field metadata by query mode.
1356 */
1357 public function allFieldsDataProvider() {
1358 return [
1359 [
1360 'anything that will then be defaulting ton contact',
1361 $this->getBasicReturnProperties(TRUE),
1362 ],
1363 [
1364 CRM_Export_Form_Select::ACTIVITY_EXPORT,
1365 array_merge($this->getBasicReturnProperties(FALSE), $this->getActivityReturnProperties()),
1366 ],
1367 [
1368 CRM_Export_Form_Select::CASE_EXPORT,
1369 array_merge($this->getBasicReturnProperties(FALSE), $this->getCaseReturnProperties()),
1370 ],
1371 [
1372 CRM_Export_Form_Select::CONTRIBUTE_EXPORT,
1373 array_merge($this->getBasicReturnProperties(FALSE), $this->getContributionReturnProperties()),
1374 ],
1375 [
1376 CRM_Export_Form_Select::EVENT_EXPORT,
1377 array_merge($this->getBasicReturnProperties(FALSE), $this->getEventReturnProperties()),
1378 ],
1379 [
1380 CRM_Export_Form_Select::MEMBER_EXPORT,
1381 array_merge($this->getBasicReturnProperties(FALSE), $this->getMembershipReturnProperties()),
1382 ],
1383 [
1384 CRM_Export_Form_Select::PLEDGE_EXPORT,
1385 array_merge($this->getBasicReturnProperties(FALSE), $this->getPledgeReturnProperties()),
1386 ],
1387 ];
1388 }
1389
1390 /**
1391 * Get return properties manually added in.
1392 */
1393 public function getExtraReturnProperties() {
7626754f 1394 return [];
ce14544c 1395 }
1396
1397 /**
1398 * Get basic return properties.
1399 *
704e3e9a 1400 * @param bool $isContactMode
ce14544c 1401 * Are we in contact mode or not
1402 *
1403 * @return array
1404 */
1405 protected function getBasicReturnProperties($isContactMode) {
1406 $returnProperties = [
1407 'id' => 1,
1408 'contact_type' => 1,
1409 'contact_sub_type' => 1,
1410 'do_not_email' => 1,
1411 'do_not_phone' => 1,
1412 'do_not_mail' => 1,
1413 'do_not_sms' => 1,
1414 'do_not_trade' => 1,
1415 'is_opt_out' => 1,
1416 'legal_identifier' => 1,
1417 'external_identifier' => 1,
1418 'sort_name' => 1,
1419 'display_name' => 1,
1420 'nick_name' => 1,
1421 'legal_name' => 1,
1422 'image_URL' => 1,
1423 'preferred_communication_method' => 1,
1424 'preferred_language' => 1,
1425 'preferred_mail_format' => 1,
1426 'hash' => 1,
1427 'contact_source' => 1,
1428 'first_name' => 1,
1429 'middle_name' => 1,
1430 'last_name' => 1,
1431 'prefix_id' => 1,
1432 'suffix_id' => 1,
1433 'formal_title' => 1,
1434 'communication_style_id' => 1,
1435 'email_greeting_id' => 1,
1436 'postal_greeting_id' => 1,
1437 'addressee_id' => 1,
1438 'job_title' => 1,
1439 'gender_id' => 1,
1440 'birth_date' => 1,
1441 'is_deceased' => 1,
1442 'deceased_date' => 1,
1443 'household_name' => 1,
1444 'organization_name' => 1,
1445 'sic_code' => 1,
1446 'user_unique_id' => 1,
1447 'current_employer_id' => 1,
1448 'contact_is_deleted' => 1,
1449 'created_date' => 1,
1450 'modified_date' => 1,
1451 'addressee' => 1,
1452 'email_greeting' => 1,
1453 'postal_greeting' => 1,
1454 'current_employer' => 1,
1455 'location_type' => 1,
1456 'street_address' => 1,
1457 'street_number' => 1,
1458 'street_number_suffix' => 1,
1459 'street_name' => 1,
1460 'street_unit' => 1,
1461 'supplemental_address_1' => 1,
1462 'supplemental_address_2' => 1,
1463 'supplemental_address_3' => 1,
1464 'city' => 1,
1465 'postal_code_suffix' => 1,
1466 'postal_code' => 1,
1467 'geo_code_1' => 1,
1468 'geo_code_2' => 1,
4fcfc078 1469 'manual_geo_code' => 1,
ce14544c 1470 'address_name' => 1,
1471 'master_id' => 1,
1472 'county' => 1,
1473 'state_province' => 1,
1474 'country' => 1,
1475 'phone' => 1,
1476 'phone_ext' => 1,
1477 'email' => 1,
1478 'on_hold' => 1,
1479 'is_bulkmail' => 1,
1480 'signature_text' => 1,
1481 'signature_html' => 1,
1482 'im_provider' => 1,
1483 'im' => 1,
1484 'openid' => 1,
1485 'world_region' => 1,
1486 'url' => 1,
1487 'groups' => 1,
1488 'tags' => 1,
1489 'notes' => 1,
1490 'phone_type_id' => 1,
ce14544c 1491 ];
1492 if (!$isContactMode) {
1493 unset($returnProperties['groups']);
1494 unset($returnProperties['tags']);
1495 unset($returnProperties['notes']);
1496 }
1497 return $returnProperties;
1498 }
1499
1500 /**
1501 * Get return properties for pledges.
1502 *
1503 * @return array
1504 */
1505 public function getPledgeReturnProperties() {
1506 return [
1507 'contact_type' => 1,
1508 'contact_sub_type' => 1,
1509 'sort_name' => 1,
1510 'display_name' => 1,
1511 'pledge_id' => 1,
1512 'pledge_amount' => 1,
1513 'pledge_total_paid' => 1,
1514 'pledge_create_date' => 1,
1515 'pledge_start_date' => 1,
1516 'pledge_next_pay_date' => 1,
1517 'pledge_next_pay_amount' => 1,
1518 'pledge_status' => 1,
1519 'pledge_is_test' => 1,
1520 'pledge_contribution_page_id' => 1,
1521 'pledge_financial_type' => 1,
1522 'pledge_frequency_interval' => 1,
1523 'pledge_frequency_unit' => 1,
1524 'pledge_currency' => 1,
1525 'pledge_campaign_id' => 1,
1526 'pledge_balance_amount' => 1,
1527 'pledge_payment_id' => 1,
1528 'pledge_payment_scheduled_amount' => 1,
1529 'pledge_payment_scheduled_date' => 1,
1530 'pledge_payment_paid_amount' => 1,
1531 'pledge_payment_paid_date' => 1,
1532 'pledge_payment_reminder_date' => 1,
1533 'pledge_payment_reminder_count' => 1,
1534 'pledge_payment_status' => 1,
1535 ];
1536 }
1537
1538 /**
1539 * Get membership return properties.
1540 *
1541 * @return array
1542 */
1543 public function getMembershipReturnProperties() {
1544 return [
ce14544c 1545 'contact_type' => 1,
1546 'contact_sub_type' => 1,
1547 'sort_name' => 1,
1548 'display_name' => 1,
1549 'membership_type' => 1,
1550 'member_is_test' => 1,
1551 'member_is_pay_later' => 1,
1552 'join_date' => 1,
1553 'membership_start_date' => 1,
1554 'membership_end_date' => 1,
1555 'membership_source' => 1,
1556 'membership_status' => 1,
1557 'membership_id' => 1,
1558 'owner_membership_id' => 1,
1559 'max_related' => 1,
1560 'membership_recur_id' => 1,
1561 'member_campaign_id' => 1,
1562 'member_is_override' => 1,
ce14544c 1563 ];
1564 }
1565
1566 /**
1567 * Get return properties for events.
1568 *
1569 * @return array
1570 */
1571 public function getEventReturnProperties() {
1572 return [
1573 'contact_type' => 1,
1574 'contact_sub_type' => 1,
1575 'sort_name' => 1,
1576 'display_name' => 1,
1577 'event_id' => 1,
1578 'event_title' => 1,
1579 'event_start_date' => 1,
1580 'event_end_date' => 1,
1581 'event_type' => 1,
1582 'participant_id' => 1,
1583 'participant_status' => 1,
1584 'participant_status_id' => 1,
1585 'participant_role' => 1,
1586 'participant_role_id' => 1,
1587 'participant_note' => 1,
1588 'participant_register_date' => 1,
1589 'participant_source' => 1,
1590 'participant_fee_level' => 1,
1591 'participant_is_test' => 1,
1592 'participant_is_pay_later' => 1,
1593 'participant_fee_amount' => 1,
1594 'participant_discount_name' => 1,
1595 'participant_fee_currency' => 1,
1596 'participant_registered_by_id' => 1,
1597 'participant_campaign_id' => 1,
1598 ];
1599 }
1600
1601 /**
1602 * Get return properties for activities.
1603 *
1604 * @return array
1605 */
1606 public function getActivityReturnProperties() {
1607 return [
1608 'activity_id' => 1,
1609 'contact_type' => 1,
1610 'contact_sub_type' => 1,
1611 'sort_name' => 1,
1612 'display_name' => 1,
1613 'activity_type' => 1,
1614 'activity_type_id' => 1,
1615 'activity_subject' => 1,
1616 'activity_date_time' => 1,
1617 'activity_duration' => 1,
1618 'activity_location' => 1,
1619 'activity_details' => 1,
1620 'activity_status' => 1,
1621 'activity_priority' => 1,
1622 'source_contact' => 1,
1623 'source_record_id' => 1,
1624 'activity_is_test' => 1,
1625 'activity_campaign_id' => 1,
1626 'result' => 1,
1627 'activity_engagement_level' => 1,
1628 'parent_id' => 1,
1629 ];
1630 }
1631
1632 /**
1633 * Get return properties for Case.
1634 *
1635 * @return array
1636 */
1637 public function getCaseReturnProperties() {
1638 return [
1639 'contact_type' => 1,
1640 'contact_sub_type' => 1,
1641 'sort_name' => 1,
1642 'display_name' => 1,
1643 'phone' => 1,
1644 'case_start_date' => 1,
1645 'case_end_date' => 1,
1646 'case_subject' => 1,
1647 'case_source_contact_id' => 1,
1648 'case_activity_status' => 1,
1649 'case_activity_duration' => 1,
1650 'case_activity_medium_id' => 1,
1651 'case_activity_details' => 1,
1652 'case_activity_is_auto' => 1,
1653 'contact_id' => 1,
1654 'case_id' => 1,
1655 'case_activity_subject' => 1,
1656 'case_status' => 1,
1657 'case_type' => 1,
1658 'case_role' => 1,
1659 'case_deleted' => 1,
1660 'case_recent_activity_date' => 1,
1661 'case_recent_activity_type' => 1,
1662 'case_scheduled_activity_date' => 1,
1663 ];
1664 }
1665
1666 /**
1667 * Get return properties for contribution.
1668 *
1669 * @return array
1670 */
1671 public function getContributionReturnProperties() {
1672 return [
1673 'contact_type' => 1,
1674 'contact_sub_type' => 1,
1675 'sort_name' => 1,
1676 'display_name' => 1,
1677 'financial_type' => 1,
1678 'contribution_source' => 1,
1679 'receive_date' => 1,
1680 'thankyou_date' => 1,
6e7cc0f5 1681 'contribution_cancel_date' => 1,
ce14544c 1682 'total_amount' => 1,
1683 'accounting_code' => 1,
1684 'payment_instrument' => 1,
1685 'payment_instrument_id' => 1,
1686 'contribution_check_number' => 1,
1687 'non_deductible_amount' => 1,
1688 'fee_amount' => 1,
1689 'net_amount' => 1,
1690 'trxn_id' => 1,
1691 'invoice_id' => 1,
1692 'invoice_number' => 1,
1693 'currency' => 1,
1694 'cancel_reason' => 1,
1695 'receipt_date' => 1,
ce14544c 1696 'is_test' => 1,
1697 'is_pay_later' => 1,
1698 'contribution_status' => 1,
1699 'contribution_recur_id' => 1,
1700 'amount_level' => 1,
1701 'contribution_note' => 1,
1702 'contribution_batch' => 1,
1703 'contribution_campaign_title' => 1,
1704 'contribution_campaign_id' => 1,
ce14544c 1705 'contribution_soft_credit_name' => 1,
1706 'contribution_soft_credit_amount' => 1,
1707 'contribution_soft_credit_type' => 1,
1708 'contribution_soft_credit_contact_id' => 1,
1709 'contribution_soft_credit_contribution_id' => 1,
1710 ];
1711 }
1712
0a3b035f 1713 /**
1714 * Test the column definition when 'all' fields defined.
1715 *
6c93b9a9 1716 * @param int $exportMode
1717 * @param array $expected
1718 * @param array $expectedHeaders
0a3b035f 1719 *
46312f7d 1720 * @throws \CRM_Core_Exception
fce27eeb 1721 * @throws \League\Csv\Exception
1722 *
0a3b035f 1723 * @dataProvider getSqlColumnsOutput
1724 */
6c93b9a9 1725 public function testGetSQLColumnsAndHeaders($exportMode, $expected, $expectedHeaders) {
0a3b035f 1726 $this->ensureComponentIsEnabled($exportMode);
1727 // We need some data so that we can get to the end of the export
1728 // function. Hopefully one day that won't be required to get metadata info out.
1729 // eventually aspire to call $provider->getSQLColumns straight after it
1730 // is intiated.
1731 $this->setupBaseExportData($exportMode);
fce27eeb 1732 $this->doExportTest(['selectAll' => TRUE, 'exportMode' => $exportMode, 'ids' => [1]]);
1733 $this->assertEquals($expected, $this->processor->getSQLColumns());
1734 $this->assertEquals($expectedHeaders, $this->processor->getHeaderRows());
0a3b035f 1735 }
1736
6202c5bb 1737 /**
1738 * Test exported with data entry mis-fire.
1739 *
1740 * Not fatal error if data incomplete.
1741 *
1742 * https://lab.civicrm.org/dev/core/issues/819
ee3aa985 1743 *
1744 * @throws \CRM_Core_Exception
1745 * @throws \League\Csv\Exception
6202c5bb 1746 */
1747 public function testExportIncompleteSubmission() {
1748 $this->setUpContactExportData();
ee3aa985 1749 $this->doExportTest(['fields' => [['contact_type' => 'Individual', 'name' => '']], 'ids' => [$this->contactIDs[1]]]);
6202c5bb 1750 }
1751
5506290d 1752 /**
1753 * Test exported with fields to output specified.
1754 *
1755 * @dataProvider getAllSpecifiableReturnFields
1756 *
1757 * @param int $exportMode
1758 * @param array $selectedFields
1759 * @param array $expected
ee3aa985 1760 *
1761 * @throws \CRM_Core_Exception
1762 * @throws \League\Csv\Exception
5506290d 1763 */
1764 public function testExportSpecifyFields($exportMode, $selectedFields, $expected) {
1765 $this->ensureComponentIsEnabled($exportMode);
1766 $this->setUpContributionExportData();
ee3aa985 1767 $this->doExportTest(['fields' => $selectedFields, 'ids' => [$this->contactIDs[1]], 'exportMode' => $exportMode]);
1768 $this->assertEquals($expected, $this->processor->getSQLColumns());
5506290d 1769 }
1770
c66a5741 1771 /**
1772 * Test export fields when no payment fields to be exported.
68f97b77 1773 *
1774 * @throws \CRM_Core_Exception
1775 * @throws \League\Csv\Exception
c66a5741 1776 */
1777 public function textExportParticipantSpecifyFieldsNoPayment() {
1778 $selectedFields = $this->getAllSpecifiableParticipantReturnFields();
1779 foreach ($selectedFields as $index => $field) {
1780 if (substr($field[1], 0, 22) === 'componentPaymentField_') {
420384a0 1781 unset($selectedFields[$index]);
c66a5741 1782 }
1783 }
1784
1785 $expected = $this->getAllSpecifiableParticipantReturnFields();
1786 foreach ($expected as $index => $field) {
1787 if (substr($index, 0, 22) === 'componentPaymentField_') {
420384a0 1788 unset($expected[$index]);
c66a5741 1789 }
1790 }
68f97b77 1791 $this->doExportTest(['fields' => $selectedFields, 'ids' => [$this->contactIDs[1]], 'exportMode' => CRM_Export_Form_Select::EVENT_EXPORT]);
1792 $this->assertEquals($expected, $this->processor->getSQLColumns());
c66a5741 1793 }
39b959db 1794
5506290d 1795 /**
46312f7d 1796 * Get all return fields (@return array
1797 *
1798 * @todo - still being built up.
5506290d 1799 *
5506290d 1800 */
1801 public function getAllSpecifiableReturnFields() {
1802 return [
1803 [
1804 CRM_Export_Form_Select::EVENT_EXPORT,
1805 $this->getAllSpecifiableParticipantReturnFields(),
c66a5741 1806 $this->getAllSpecifiableParticipantReturnColumns(),
5506290d 1807 ],
1808 ];
1809 }
1810
c66a5741 1811 /**
1812 * Get expected return column output for participant mode return all columns.
1813 *
1814 * @return array
1815 */
1816 public function getAllSpecifiableParticipantReturnColumns() {
1817 return [
07b892fc 1818 'participant_campaign_id' => 'participant_campaign_id varchar(16)',
c66a5741 1819 'participant_contact_id' => 'participant_contact_id varchar(16)',
1820 'componentpaymentfield_contribution_status' => 'componentpaymentfield_contribution_status text',
1821 'currency' => 'currency varchar(3)',
1822 'componentpaymentfield_received_date' => 'componentpaymentfield_received_date text',
1823 'default_role_id' => 'default_role_id varchar(16)',
1824 'participant_discount_name' => 'participant_discount_name varchar(16)',
1825 'event_id' => 'event_id varchar(16)',
1826 'event_end_date' => 'event_end_date varchar(32)',
1827 'event_start_date' => 'event_start_date varchar(32)',
1828 'template_title' => 'template_title varchar(255)',
1829 'event_title' => 'event_title varchar(255)',
1830 'participant_fee_amount' => 'participant_fee_amount varchar(32)',
1831 'participant_fee_currency' => 'participant_fee_currency varchar(3)',
1832 'fee_label' => 'fee_label varchar(255)',
1833 'participant_fee_level' => 'participant_fee_level longtext',
1834 'participant_is_pay_later' => 'participant_is_pay_later varchar(16)',
1835 'participant_id' => 'participant_id varchar(16)',
1836 'participant_note' => 'participant_note text',
1837 'participant_role_id' => 'participant_role_id varchar(128)',
1838 'participant_role' => 'participant_role varchar(255)',
1839 'participant_source' => 'participant_source varchar(128)',
1840 'participant_status_id' => 'participant_status_id varchar(16)',
1841 'participant_status' => 'participant_status varchar(255)',
1842 'participant_register_date' => 'participant_register_date varchar(32)',
1843 'participant_registered_by_id' => 'participant_registered_by_id varchar(16)',
1844 'participant_is_test' => 'participant_is_test varchar(16)',
1845 'componentpaymentfield_total_amount' => 'componentpaymentfield_total_amount text',
1846 'componentpaymentfield_transaction_id' => 'componentpaymentfield_transaction_id varchar(255)',
1847 'transferred_to_contact_id' => 'transferred_to_contact_id varchar(16)',
1848 ];
1849 }
1850
5506290d 1851 /**
1852 * @return array
1853 */
1854 public function getAllSpecifiableParticipantReturnFields() {
1855 return [
1856 0 =>
1857 [
1858 0 => 'Participant',
ee3aa985 1859 'name' => '',
5506290d 1860 ],
1861 1 =>
1862 [
1863 0 => 'Participant',
ee3aa985 1864 'name' => 'participant_campaign_id',
5506290d 1865 ],
1866 2 =>
1867 [
1868 0 => 'Participant',
ee3aa985 1869 'name' => 'participant_contact_id',
5506290d 1870 ],
1871 3 =>
1872 [
1873 0 => 'Participant',
ee3aa985 1874 'name' => 'componentPaymentField_contribution_status',
5506290d 1875 ],
1876 4 =>
1877 [
1878 0 => 'Participant',
ee3aa985 1879 'name' => 'currency',
5506290d 1880 ],
1881 5 =>
1882 [
1883 0 => 'Participant',
ee3aa985 1884 'name' => 'componentPaymentField_received_date',
5506290d 1885 ],
1886 6 =>
1887 [
1888 0 => 'Participant',
ee3aa985 1889 'name' => 'default_role_id',
5506290d 1890 ],
1891 7 =>
1892 [
1893 0 => 'Participant',
ee3aa985 1894 'name' => 'participant_discount_name',
5506290d 1895 ],
1896 8 =>
1897 [
1898 0 => 'Participant',
ee3aa985 1899 'name' => 'event_id',
5506290d 1900 ],
1901 9 =>
1902 [
1903 0 => 'Participant',
ee3aa985 1904 'name' => 'event_end_date',
5506290d 1905 ],
1906 10 =>
1907 [
1908 0 => 'Participant',
ee3aa985 1909 'name' => 'event_start_date',
5506290d 1910 ],
1911 11 =>
1912 [
1913 0 => 'Participant',
ee3aa985 1914 'name' => 'template_title',
5506290d 1915 ],
1916 12 =>
1917 [
1918 0 => 'Participant',
ee3aa985 1919 'name' => 'event_title',
5506290d 1920 ],
1921 13 =>
1922 [
1923 0 => 'Participant',
ee3aa985 1924 'name' => 'participant_fee_amount',
5506290d 1925 ],
1926 14 =>
1927 [
1928 0 => 'Participant',
ee3aa985 1929 'name' => 'participant_fee_currency',
5506290d 1930 ],
1931 15 =>
1932 [
1933 0 => 'Participant',
ee3aa985 1934 'name' => 'fee_label',
5506290d 1935 ],
1936 16 =>
1937 [
1938 0 => 'Participant',
ee3aa985 1939 'name' => 'participant_fee_level',
5506290d 1940 ],
1941 17 =>
1942 [
1943 0 => 'Participant',
ee3aa985 1944 'name' => 'participant_is_pay_later',
5506290d 1945 ],
1946 18 =>
1947 [
1948 0 => 'Participant',
ee3aa985 1949 'name' => 'participant_id',
5506290d 1950 ],
1951 19 =>
1952 [
1953 0 => 'Participant',
ee3aa985 1954 'name' => 'participant_note',
5506290d 1955 ],
1956 20 =>
1957 [
1958 0 => 'Participant',
ee3aa985 1959 'name' => 'participant_role_id',
5506290d 1960 ],
1961 21 =>
1962 [
1963 0 => 'Participant',
ee3aa985 1964 'name' => 'participant_role',
5506290d 1965 ],
1966 22 =>
1967 [
1968 0 => 'Participant',
ee3aa985 1969 'name' => 'participant_source',
5506290d 1970 ],
1971 23 =>
1972 [
1973 0 => 'Participant',
ee3aa985 1974 'name' => 'participant_status_id',
5506290d 1975 ],
1976 24 =>
1977 [
1978 0 => 'Participant',
ee3aa985 1979 'name' => 'participant_status',
5506290d 1980 ],
1981 25 =>
1982 [
1983 0 => 'Participant',
ee3aa985 1984 'name' => 'participant_status',
5506290d 1985 ],
1986 26 =>
1987 [
1988 0 => 'Participant',
ee3aa985 1989 'name' => 'participant_register_date',
5506290d 1990 ],
1991 27 =>
1992 [
1993 0 => 'Participant',
ee3aa985 1994 'name' => 'participant_registered_by_id',
5506290d 1995 ],
1996 28 =>
1997 [
1998 0 => 'Participant',
ee3aa985 1999 'name' => 'participant_is_test',
5506290d 2000 ],
2001 29 =>
2002 [
2003 0 => 'Participant',
ee3aa985 2004 'name' => 'componentPaymentField_total_amount',
5506290d 2005 ],
2006 30 =>
2007 [
2008 0 => 'Participant',
ee3aa985 2009 'name' => 'componentPaymentField_transaction_id',
5506290d 2010 ],
2011 31 =>
2012 [
2013 0 => 'Participant',
ee3aa985 2014 'name' => 'transferred_to_contact_id',
5506290d 2015 ],
2016 ];
2017 }
2018
0a3b035f 2019 /**
2020 * @param string $exportMode
2021 */
2022 public function setupBaseExportData($exportMode) {
2023 $this->createLoggedInUser();
2024 if ($exportMode === CRM_Export_Form_Select::CASE_EXPORT) {
2025 $this->setupCaseExportData();
2026 }
2027 if ($exportMode === CRM_Export_Form_Select::CONTRIBUTE_EXPORT) {
2028 $this->setUpContributionExportData();
2029 }
2030 if ($exportMode === CRM_Export_Form_Select::MEMBER_EXPORT) {
2031 $this->setUpMembershipExportData();
2032 }
2033 if ($exportMode === CRM_Export_Form_Select::ACTIVITY_EXPORT) {
2034 $this->setUpActivityExportData();
2035 }
2036 }
2037
2038 /**
2039 * Get comprehensive sql columns output.
2040 *
2041 * @return array
2042 */
2043 public function getSqlColumnsOutput() {
2044 return [
2045 [
2046 'anything that will then be defaulting ton contact',
2047 $this->getBasicSqlColumnDefinition(TRUE),
6c93b9a9 2048 $this->getBasicHeaderDefinition(TRUE),
0a3b035f 2049 ],
2050 [
2051 CRM_Export_Form_Select::ACTIVITY_EXPORT,
2052 array_merge($this->getBasicSqlColumnDefinition(FALSE), $this->getActivitySqlColumns()),
6c93b9a9 2053 array_merge($this->getBasicHeaderDefinition(FALSE), $this->getActivityHeaderDefinition()),
0a3b035f 2054 ],
2055 [
2056 CRM_Export_Form_Select::CASE_EXPORT,
2057 array_merge($this->getBasicSqlColumnDefinition(FALSE), $this->getCaseSqlColumns()),
6c93b9a9 2058 array_merge($this->getBasicHeaderDefinition(FALSE), $this->getCaseHeaderDefinition()),
0a3b035f 2059 ],
2060 [
2061 CRM_Export_Form_Select::CONTRIBUTE_EXPORT,
2062 array_merge($this->getBasicSqlColumnDefinition(FALSE), $this->getContributionSqlColumns()),
6c93b9a9 2063 array_merge($this->getBasicHeaderDefinition(FALSE), $this->getContributeHeaderDefinition()),
0a3b035f 2064 ],
2065 [
2066 CRM_Export_Form_Select::EVENT_EXPORT,
2067 array_merge($this->getBasicSqlColumnDefinition(FALSE), $this->getParticipantSqlColumns()),
6c93b9a9 2068 array_merge($this->getBasicHeaderDefinition(FALSE), $this->getParticipantHeaderDefinition()),
0a3b035f 2069 ],
2070 [
2071 CRM_Export_Form_Select::MEMBER_EXPORT,
2072 array_merge($this->getBasicSqlColumnDefinition(FALSE), $this->getMembershipSqlColumns()),
6c93b9a9 2073 array_merge($this->getBasicHeaderDefinition(FALSE), $this->getMemberHeaderDefinition()),
0a3b035f 2074 ],
2075 [
2076 CRM_Export_Form_Select::PLEDGE_EXPORT,
2077 array_merge($this->getBasicSqlColumnDefinition(FALSE), $this->getPledgeSqlColumns()),
6c93b9a9 2078 array_merge($this->getBasicHeaderDefinition(FALSE), $this->getPledgeHeaderDefinition()),
0a3b035f 2079 ],
2080
2081 ];
2082 }
2083
6c93b9a9 2084 /**
2085 * Get the header definition for exports.
2086 *
2087 * @param bool $isContactExport
2088 *
2089 * @return array
2090 */
2091 protected function getBasicHeaderDefinition($isContactExport) {
2092 $headers = [
39b959db
SL
2093 0 => 'Contact ID',
2094 1 => 'Contact Type',
2095 2 => 'Contact Subtype',
2096 3 => 'Do Not Email',
2097 4 => 'Do Not Phone',
2098 5 => 'Do Not Mail',
2099 6 => 'Do Not Sms',
2100 7 => 'Do Not Trade',
2101 8 => 'No Bulk Emails (User Opt Out)',
2102 9 => 'Legal Identifier',
2103 10 => 'External Identifier',
2104 11 => 'Sort Name',
2105 12 => 'Display Name',
2106 13 => 'Nickname',
2107 14 => 'Legal Name',
2108 15 => 'Image Url',
2109 16 => 'Preferred Communication Method',
2110 17 => 'Preferred Language',
2111 18 => 'Preferred Mail Format',
2112 19 => 'Contact Hash',
2113 20 => 'Contact Source',
2114 21 => 'First Name',
2115 22 => 'Middle Name',
2116 23 => 'Last Name',
2117 24 => 'Individual Prefix',
2118 25 => 'Individual Suffix',
2119 26 => 'Formal Title',
2120 27 => 'Communication Style',
2121 28 => 'Email Greeting ID',
2122 29 => 'Postal Greeting ID',
2123 30 => 'Addressee ID',
2124 31 => 'Job Title',
2125 32 => 'Gender',
2126 33 => 'Birth Date',
2127 34 => 'Deceased',
2128 35 => 'Deceased Date',
2129 36 => 'Household Name',
2130 37 => 'Organization Name',
2131 38 => 'Sic Code',
2132 39 => 'Unique ID (OpenID)',
2133 40 => 'Current Employer ID',
2134 41 => 'Contact is in Trash',
2135 42 => 'Created Date',
2136 43 => 'Modified Date',
2137 44 => 'Addressee',
2138 45 => 'Email Greeting',
2139 46 => 'Postal Greeting',
2140 47 => 'Current Employer',
2141 48 => 'Location Type',
2142 49 => 'Street Address',
2143 50 => 'Street Number',
2144 51 => 'Street Number Suffix',
2145 52 => 'Street Name',
2146 53 => 'Street Unit',
2147 54 => 'Supplemental Address 1',
2148 55 => 'Supplemental Address 2',
2149 56 => 'Supplemental Address 3',
2150 57 => 'City',
2151 58 => 'Postal Code Suffix',
2152 59 => 'Postal Code',
2153 60 => 'Latitude',
2154 61 => 'Longitude',
4fcfc078
SL
2155 62 => 'Is Manually Geocoded',
2156 63 => 'Address Name',
2157 64 => 'Master Address Belongs To',
2158 65 => 'County',
2159 66 => 'State',
2160 67 => 'Country',
2161 68 => 'Phone',
2162 69 => 'Phone Extension',
2163 70 => 'Phone Type',
2164 71 => 'Email',
2165 72 => 'On Hold',
2166 73 => 'Use for Bulk Mail',
2167 74 => 'Signature Text',
2168 75 => 'Signature Html',
2169 76 => 'IM Provider',
2170 77 => 'IM Screen Name',
2171 78 => 'OpenID',
2172 79 => 'World Region',
2173 80 => 'Website',
2174 81 => 'Group(s)',
2175 82 => 'Tag(s)',
2176 83 => 'Note(s)',
39b959db 2177 ];
6c93b9a9 2178 if (!$isContactExport) {
6c93b9a9 2179 unset($headers[81]);
219c47d6 2180 unset($headers[82]);
4fcfc078 2181 unset($headers[83]);
6c93b9a9 2182 }
2183 return $headers;
2184 }
2185
2186 /**
2187 * Get the definition for activity headers.
2188 *
2189 * @return array
2190 */
2191 protected function getActivityHeaderDefinition() {
2192 return [
2193 81 => 'Activity ID',
2194 82 => 'Activity Type',
2195 83 => 'Activity Type ID',
2196 84 => 'Subject',
2197 85 => 'Activity Date',
2198 86 => 'Duration',
2199 87 => 'Location',
2200 88 => 'Details',
2201 89 => 'Activity Status',
2202 90 => 'Activity Priority',
2203 91 => 'Source Contact',
2204 92 => 'source_record_id',
2205 93 => 'Test',
2206 94 => 'Campaign ID',
2207 95 => 'result',
2208 96 => 'Engagement Index',
2209 97 => 'parent_id',
2210 ];
2211 }
2212
2213 /**
2214 * Get the definition for case headers.
2215 *
2216 * @return array
2217 */
2218 protected function getCaseHeaderDefinition() {
2219 return [
2220 81 => 'contact_id',
2221 82 => 'Case ID',
2222 83 => 'case_activity_subject',
2223 84 => 'Case Subject',
2224 85 => 'Case Status',
2225 86 => 'Case Type',
2226 87 => 'Role in Case',
2227 88 => 'Case is in the Trash',
2228 89 => 'case_recent_activity_date',
2229 90 => 'case_recent_activity_type',
2230 91 => 'case_scheduled_activity_date',
2231 92 => 'Case Start Date',
2232 93 => 'Case End Date',
2233 94 => 'case_source_contact_id',
2234 95 => 'case_activity_status',
2235 96 => 'case_activity_duration',
2236 97 => 'case_activity_medium_id',
2237 98 => 'case_activity_details',
2238 99 => 'case_activity_is_auto',
2239 ];
2240 }
2241
2242 /**
2243 * Get the definition for contribute headers.
2244 *
2245 * @return array
2246 */
2247 protected function getContributeHeaderDefinition() {
2248 return [
2249 81 => 'Financial Type',
2250 82 => 'Contribution Source',
2251 83 => 'Date Received',
2252 84 => 'Thank-you Date',
6e7cc0f5 2253 85 => 'Cancelled / Refunded Date',
6c93b9a9 2254 86 => 'Total Amount',
2255 87 => 'Accounting Code',
a0090e6b 2256 88 => 'Payment Methods',
6c93b9a9 2257 89 => 'Payment Method ID',
2258 90 => 'Check Number',
2259 91 => 'Non-deductible Amount',
2260 92 => 'Fee Amount',
2261 93 => 'Net Amount',
2262 94 => 'Transaction ID',
2263 95 => 'Invoice Reference',
2264 96 => 'Invoice Number',
2265 97 => 'Currency',
927898c5 2266 98 => 'Cancellation / Refund Reason',
6c93b9a9 2267 99 => 'Receipt Date',
4fcfc078
SL
2268 100 => 'Test',
2269 101 => 'Is Pay Later',
2270 102 => 'Contribution Status',
2271 103 => 'Recurring Contribution ID',
2272 104 => 'Amount Label',
2273 105 => 'Contribution Note',
2274 106 => 'Batch Name',
2275 107 => 'Campaign Title',
2276 108 => 'Campaign ID',
2277 109 => 'Soft Credit For',
2278 110 => 'Soft Credit Amount',
2279 111 => 'Soft Credit Type',
2280 112 => 'Soft Credit For Contact ID',
2281 113 => 'Soft Credit For Contribution ID',
6c93b9a9 2282 ];
2283 }
2284
2285 /**
2286 * Get the definition for event headers.
2287 *
2288 * @return array
2289 */
2290 protected function getParticipantHeaderDefinition() {
2291 return [
2292 81 => 'Event',
2293 82 => 'Event Title',
2294 83 => 'Event Start Date',
2295 84 => 'Event End Date',
2296 85 => 'Event Type',
2297 86 => 'Participant ID',
2298 87 => 'Participant Status',
2299 88 => 'Participant Status Id',
2300 89 => 'Participant Role',
2301 90 => 'Participant Role Id',
2302 91 => 'Participant Note',
2303 92 => 'Register date',
2304 93 => 'Participant Source',
2305 94 => 'Fee level',
2306 95 => 'Test',
2307 96 => 'Is Pay Later',
2308 97 => 'Fee Amount',
2309 98 => 'Discount Name',
2310 99 => 'Fee Currency',
2311 100 => 'Registered By ID',
2312 101 => 'Campaign ID',
2313 ];
2314 }
2315
2316 /**
2317 * Get the definition for member headers.
2318 *
2319 * @return array
2320 */
2321 protected function getMemberHeaderDefinition() {
2322 return [
2323 81 => 'Membership Type',
2324 82 => 'Test',
2325 83 => 'Is Pay Later',
2326 84 => 'Member Since',
2327 85 => 'Membership Start Date',
2328 86 => 'Membership Expiration Date',
2329 87 => 'Source',
2330 88 => 'Membership Status',
2331 89 => 'Membership ID',
2332 90 => 'Primary Member ID',
8f67d99a 2333 91 => 'Max Related',
2334 92 => 'Membership Recurring Contribution',
6c93b9a9 2335 93 => 'Campaign ID',
8f67d99a 2336 94 => 'Status Override',
6c93b9a9 2337 ];
2338 }
2339
2340 /**
2341 * Get the definition for pledge headers.
2342 *
2343 * @return array
2344 */
2345 protected function getPledgeHeaderDefinition() {
2346 return [
2347 81 => 'Pledge ID',
2348 82 => 'Total Pledged',
2349 83 => 'Total Paid',
2350 84 => 'Pledge Made',
a5e468ff 2351 85 => 'Pledge Start Date',
6c93b9a9 2352 86 => 'Next Payment Date',
2353 87 => 'Next Payment Amount',
2354 88 => 'Pledge Status',
2355 89 => 'Test',
2356 90 => 'Pledge Contribution Page Id',
2357 91 => 'pledge_financial_type',
2358 92 => 'Pledge Frequency Interval',
2359 93 => 'Pledge Frequency Unit',
2360 94 => 'pledge_currency',
2361 95 => 'Campaign ID',
2362 96 => 'Balance Amount',
2363 97 => 'Payment ID',
2364 98 => 'Scheduled Amount',
2365 99 => 'Scheduled Date',
2366 100 => 'Paid Amount',
2367 101 => 'Paid Date',
2368 102 => 'Last Reminder',
2369 103 => 'Reminders Sent',
2370 104 => 'Pledge Payment Status',
2371 ];
2372 }
2373
0a3b035f 2374 /**
2375 * Get the column definition for exports.
2376 *
2377 * @param bool $isContactExport
2378 *
2379 * @return array
2380 */
2381 protected function getBasicSqlColumnDefinition($isContactExport) {
2382 $columns = [
2383 'civicrm_primary_id' => 'civicrm_primary_id varchar(16)',
2384 'contact_type' => 'contact_type varchar(64)',
2385 'contact_sub_type' => 'contact_sub_type varchar(255)',
2386 'do_not_email' => 'do_not_email varchar(16)',
2387 'do_not_phone' => 'do_not_phone varchar(16)',
2388 'do_not_mail' => 'do_not_mail varchar(16)',
2389 'do_not_sms' => 'do_not_sms varchar(16)',
2390 'do_not_trade' => 'do_not_trade varchar(16)',
2391 'is_opt_out' => 'is_opt_out varchar(16)',
2392 'legal_identifier' => 'legal_identifier varchar(32)',
2393 'external_identifier' => 'external_identifier varchar(64)',
2394 'sort_name' => 'sort_name varchar(128)',
2395 'display_name' => 'display_name varchar(128)',
2396 'nick_name' => 'nick_name varchar(128)',
2397 'legal_name' => 'legal_name varchar(128)',
2398 'image_url' => 'image_url longtext',
2399 'preferred_communication_method' => 'preferred_communication_method varchar(255)',
2400 'preferred_language' => 'preferred_language varchar(5)',
2401 'preferred_mail_format' => 'preferred_mail_format varchar(8)',
2402 'hash' => 'hash varchar(32)',
2403 'contact_source' => 'contact_source varchar(255)',
2404 'first_name' => 'first_name varchar(64)',
2405 'middle_name' => 'middle_name varchar(64)',
2406 'last_name' => 'last_name varchar(64)',
2407 'prefix_id' => 'prefix_id varchar(255)',
2408 'suffix_id' => 'suffix_id varchar(255)',
2409 'formal_title' => 'formal_title varchar(64)',
714ab070 2410 'communication_style_id' => 'communication_style_id varchar(255)',
0a3b035f 2411 'email_greeting_id' => 'email_greeting_id varchar(16)',
2412 'postal_greeting_id' => 'postal_greeting_id varchar(16)',
2413 'addressee_id' => 'addressee_id varchar(16)',
2414 'job_title' => 'job_title varchar(255)',
714ab070 2415 'gender_id' => 'gender_id varchar(255)',
0a3b035f 2416 'birth_date' => 'birth_date varchar(32)',
2417 'is_deceased' => 'is_deceased varchar(16)',
2418 'deceased_date' => 'deceased_date varchar(32)',
2419 'household_name' => 'household_name varchar(128)',
2420 'organization_name' => 'organization_name varchar(128)',
2421 'sic_code' => 'sic_code varchar(8)',
2422 'user_unique_id' => 'user_unique_id varchar(255)',
2423 'current_employer_id' => 'current_employer_id varchar(16)',
2424 'contact_is_deleted' => 'contact_is_deleted varchar(16)',
2425 'created_date' => 'created_date varchar(32)',
2426 'modified_date' => 'modified_date varchar(32)',
2427 'addressee' => 'addressee varchar(255)',
2428 'email_greeting' => 'email_greeting varchar(255)',
2429 'postal_greeting' => 'postal_greeting varchar(255)',
2430 'current_employer' => 'current_employer varchar(128)',
2431 'location_type' => 'location_type text',
2432 'street_address' => 'street_address varchar(96)',
2433 'street_number' => 'street_number varchar(16)',
2434 'street_number_suffix' => 'street_number_suffix varchar(8)',
2435 'street_name' => 'street_name varchar(64)',
2436 'street_unit' => 'street_unit varchar(16)',
2437 'supplemental_address_1' => 'supplemental_address_1 varchar(96)',
2438 'supplemental_address_2' => 'supplemental_address_2 varchar(96)',
2439 'supplemental_address_3' => 'supplemental_address_3 varchar(96)',
2440 'city' => 'city varchar(64)',
2441 'postal_code_suffix' => 'postal_code_suffix varchar(12)',
2442 'postal_code' => 'postal_code varchar(64)',
2443 'geo_code_1' => 'geo_code_1 varchar(32)',
2444 'geo_code_2' => 'geo_code_2 varchar(32)',
4fcfc078 2445 'manual_geo_code' => 'manual_geo_code varchar(16)',
0a3b035f 2446 'address_name' => 'address_name varchar(255)',
2447 'master_id' => 'master_id varchar(128)',
2448 'county' => 'county varchar(64)',
2449 'state_province' => 'state_province varchar(64)',
2450 'country' => 'country varchar(64)',
2451 'phone' => 'phone varchar(32)',
2452 'phone_ext' => 'phone_ext varchar(16)',
219c47d6 2453 'phone_type_id' => 'phone_type_id varchar(16)',
0a3b035f 2454 'email' => 'email varchar(254)',
2455 'on_hold' => 'on_hold varchar(16)',
2456 'is_bulkmail' => 'is_bulkmail varchar(16)',
2457 'signature_text' => 'signature_text longtext',
2458 'signature_html' => 'signature_html longtext',
2459 'im_provider' => 'im_provider text',
5ebd7d09 2460 'im_screen_name' => 'im_screen_name varchar(64)',
0a3b035f 2461 'openid' => 'openid varchar(255)',
2462 'world_region' => 'world_region varchar(128)',
2463 'url' => 'url varchar(128)',
2464 'groups' => 'groups text',
2465 'tags' => 'tags text',
2466 'notes' => 'notes text',
0a3b035f 2467 ];
2468 if (!$isContactExport) {
2469 unset($columns['groups']);
2470 unset($columns['tags']);
2471 unset($columns['notes']);
2472 }
2473 return $columns;
2474 }
2475
2476 /**
2477 * Get Case SQL columns.
2478 *
2479 * @return array
2480 */
2481 protected function getCaseSqlColumns() {
2482 return [
2483 'case_start_date' => 'case_start_date varchar(32)',
2484 'case_end_date' => 'case_end_date varchar(32)',
2485 'case_subject' => 'case_subject varchar(128)',
2486 'case_source_contact_id' => 'case_source_contact_id varchar(255)',
2487 'case_activity_status' => 'case_activity_status text',
2488 'case_activity_duration' => 'case_activity_duration text',
2489 'case_activity_medium_id' => 'case_activity_medium_id varchar(255)',
2490 'case_activity_details' => 'case_activity_details text',
2491 'case_activity_is_auto' => 'case_activity_is_auto text',
2492 'contact_id' => 'contact_id varchar(255)',
2493 'case_id' => 'case_id varchar(16)',
2494 'case_activity_subject' => 'case_activity_subject text',
2495 'case_status' => 'case_status text',
2496 'case_type' => 'case_type text',
2497 'case_role' => 'case_role text',
2498 'case_deleted' => 'case_deleted varchar(16)',
2499 'case_recent_activity_date' => 'case_recent_activity_date text',
2500 'case_recent_activity_type' => 'case_recent_activity_type text',
2501 'case_scheduled_activity_date' => 'case_scheduled_activity_date text',
2502 ];
2503 }
2504
2505 /**
2506 * Get activity sql columns.
2507 *
2508 * @return array
2509 */
2510 protected function getActivitySqlColumns() {
2511 return [
2512 'activity_id' => 'activity_id varchar(16)',
2513 'activity_type' => 'activity_type varchar(255)',
2514 'activity_type_id' => 'activity_type_id varchar(16)',
2515 'activity_subject' => 'activity_subject varchar(255)',
2516 'activity_date_time' => 'activity_date_time varchar(32)',
2517 'activity_duration' => 'activity_duration varchar(16)',
2518 'activity_location' => 'activity_location varchar(255)',
2519 'activity_details' => 'activity_details longtext',
2520 'activity_status' => 'activity_status varchar(255)',
2521 'activity_priority' => 'activity_priority varchar(255)',
2522 'source_contact' => 'source_contact varchar(255)',
2523 'source_record_id' => 'source_record_id varchar(255)',
2524 'activity_is_test' => 'activity_is_test varchar(16)',
07b892fc 2525 'activity_campaign_id' => 'activity_campaign_id varchar(16)',
0a3b035f 2526 'result' => 'result text',
2527 'activity_engagement_level' => 'activity_engagement_level varchar(16)',
2528 'parent_id' => 'parent_id varchar(255)',
2529 ];
2530 }
2531
2532 /**
2533 * Get participant sql columns.
2534 *
2535 * @return array
2536 */
2537 protected function getParticipantSqlColumns() {
2538 return [
2539 'event_id' => 'event_id varchar(16)',
2540 'event_title' => 'event_title varchar(255)',
2541 'event_start_date' => 'event_start_date varchar(32)',
2542 'event_end_date' => 'event_end_date varchar(32)',
2543 'event_type' => 'event_type varchar(255)',
2544 'participant_id' => 'participant_id varchar(16)',
2545 'participant_status' => 'participant_status varchar(255)',
2546 'participant_status_id' => 'participant_status_id varchar(16)',
2547 'participant_role' => 'participant_role varchar(255)',
2548 'participant_role_id' => 'participant_role_id varchar(128)',
2549 'participant_note' => 'participant_note text',
2550 'participant_register_date' => 'participant_register_date varchar(32)',
2551 'participant_source' => 'participant_source varchar(128)',
2552 'participant_fee_level' => 'participant_fee_level longtext',
2553 'participant_is_test' => 'participant_is_test varchar(16)',
2554 'participant_is_pay_later' => 'participant_is_pay_later varchar(16)',
2555 'participant_fee_amount' => 'participant_fee_amount varchar(32)',
2556 'participant_discount_name' => 'participant_discount_name varchar(16)',
2557 'participant_fee_currency' => 'participant_fee_currency varchar(3)',
2558 'participant_registered_by_id' => 'participant_registered_by_id varchar(16)',
07b892fc 2559 'participant_campaign_id' => 'participant_campaign_id varchar(16)',
0a3b035f 2560 ];
2561 }
2562
2563 /**
2564 * Get contribution sql columns.
2565 *
2566 * @return array
2567 */
2568 public function getContributionSqlColumns() {
2569 return [
2570 'civicrm_primary_id' => 'civicrm_primary_id varchar(16)',
2571 'contact_type' => 'contact_type varchar(64)',
2572 'contact_sub_type' => 'contact_sub_type varchar(255)',
2573 'do_not_email' => 'do_not_email varchar(16)',
2574 'do_not_phone' => 'do_not_phone varchar(16)',
2575 'do_not_mail' => 'do_not_mail varchar(16)',
2576 'do_not_sms' => 'do_not_sms varchar(16)',
2577 'do_not_trade' => 'do_not_trade varchar(16)',
2578 'is_opt_out' => 'is_opt_out varchar(16)',
2579 'legal_identifier' => 'legal_identifier varchar(32)',
2580 'external_identifier' => 'external_identifier varchar(64)',
2581 'sort_name' => 'sort_name varchar(128)',
2582 'display_name' => 'display_name varchar(128)',
2583 'nick_name' => 'nick_name varchar(128)',
2584 'legal_name' => 'legal_name varchar(128)',
2585 'image_url' => 'image_url longtext',
2586 'preferred_communication_method' => 'preferred_communication_method varchar(255)',
2587 'preferred_language' => 'preferred_language varchar(5)',
2588 'preferred_mail_format' => 'preferred_mail_format varchar(8)',
2589 'hash' => 'hash varchar(32)',
2590 'contact_source' => 'contact_source varchar(255)',
2591 'first_name' => 'first_name varchar(64)',
2592 'middle_name' => 'middle_name varchar(64)',
2593 'last_name' => 'last_name varchar(64)',
2594 'prefix_id' => 'prefix_id varchar(255)',
2595 'suffix_id' => 'suffix_id varchar(255)',
2596 'formal_title' => 'formal_title varchar(64)',
714ab070 2597 'communication_style_id' => 'communication_style_id varchar(255)',
0a3b035f 2598 'email_greeting_id' => 'email_greeting_id varchar(16)',
2599 'postal_greeting_id' => 'postal_greeting_id varchar(16)',
2600 'addressee_id' => 'addressee_id varchar(16)',
2601 'job_title' => 'job_title varchar(255)',
714ab070 2602 'gender_id' => 'gender_id varchar(255)',
0a3b035f 2603 'birth_date' => 'birth_date varchar(32)',
2604 'is_deceased' => 'is_deceased varchar(16)',
2605 'deceased_date' => 'deceased_date varchar(32)',
2606 'household_name' => 'household_name varchar(128)',
2607 'organization_name' => 'organization_name varchar(128)',
2608 'sic_code' => 'sic_code varchar(8)',
2609 'user_unique_id' => 'user_unique_id varchar(255)',
2610 'current_employer_id' => 'current_employer_id varchar(16)',
2611 'contact_is_deleted' => 'contact_is_deleted varchar(16)',
2612 'created_date' => 'created_date varchar(32)',
2613 'modified_date' => 'modified_date varchar(32)',
2614 'addressee' => 'addressee varchar(255)',
2615 'email_greeting' => 'email_greeting varchar(255)',
2616 'postal_greeting' => 'postal_greeting varchar(255)',
2617 'current_employer' => 'current_employer varchar(128)',
2618 'location_type' => 'location_type text',
2619 'street_address' => 'street_address varchar(96)',
2620 'street_number' => 'street_number varchar(16)',
2621 'street_number_suffix' => 'street_number_suffix varchar(8)',
2622 'street_name' => 'street_name varchar(64)',
2623 'street_unit' => 'street_unit varchar(16)',
2624 'supplemental_address_1' => 'supplemental_address_1 varchar(96)',
2625 'supplemental_address_2' => 'supplemental_address_2 varchar(96)',
2626 'supplemental_address_3' => 'supplemental_address_3 varchar(96)',
2627 'city' => 'city varchar(64)',
2628 'postal_code_suffix' => 'postal_code_suffix varchar(12)',
2629 'postal_code' => 'postal_code varchar(64)',
2630 'geo_code_1' => 'geo_code_1 varchar(32)',
2631 'geo_code_2' => 'geo_code_2 varchar(32)',
2632 'address_name' => 'address_name varchar(255)',
2633 'master_id' => 'master_id varchar(128)',
2634 'county' => 'county varchar(64)',
2635 'state_province' => 'state_province varchar(64)',
2636 'country' => 'country varchar(64)',
2637 'phone' => 'phone varchar(32)',
2638 'phone_ext' => 'phone_ext varchar(16)',
2639 'email' => 'email varchar(254)',
2640 'on_hold' => 'on_hold varchar(16)',
2641 'is_bulkmail' => 'is_bulkmail varchar(16)',
2642 'signature_text' => 'signature_text longtext',
2643 'signature_html' => 'signature_html longtext',
2644 'im_provider' => 'im_provider text',
5ebd7d09 2645 'im_screen_name' => 'im_screen_name varchar(64)',
0a3b035f 2646 'openid' => 'openid varchar(255)',
2647 'world_region' => 'world_region varchar(128)',
2648 'url' => 'url varchar(128)',
219c47d6 2649 'phone_type_id' => 'phone_type_id varchar(16)',
124f3c1b 2650 'financial_type' => 'financial_type varchar(255)',
0a3b035f 2651 'contribution_source' => 'contribution_source varchar(255)',
2652 'receive_date' => 'receive_date varchar(32)',
2653 'thankyou_date' => 'thankyou_date varchar(32)',
6e7cc0f5 2654 'contribution_cancel_date' => 'contribution_cancel_date varchar(32)',
0a3b035f 2655 'total_amount' => 'total_amount varchar(32)',
2656 'accounting_code' => 'accounting_code varchar(64)',
a0090e6b 2657 'payment_instrument' => 'payment_instrument varchar(255)',
0a3b035f 2658 'payment_instrument_id' => 'payment_instrument_id varchar(16)',
2659 'contribution_check_number' => 'contribution_check_number varchar(255)',
2660 'non_deductible_amount' => 'non_deductible_amount varchar(32)',
2661 'fee_amount' => 'fee_amount varchar(32)',
2662 'net_amount' => 'net_amount varchar(32)',
2663 'trxn_id' => 'trxn_id varchar(255)',
2664 'invoice_id' => 'invoice_id varchar(255)',
2665 'invoice_number' => 'invoice_number varchar(255)',
2666 'currency' => 'currency varchar(3)',
2667 'cancel_reason' => 'cancel_reason longtext',
2668 'receipt_date' => 'receipt_date varchar(32)',
0a3b035f 2669 'is_test' => 'is_test varchar(16)',
2670 'is_pay_later' => 'is_pay_later varchar(16)',
a0090e6b 2671 'contribution_status' => 'contribution_status varchar(255)',
0a3b035f 2672 'contribution_recur_id' => 'contribution_recur_id varchar(16)',
2673 'amount_level' => 'amount_level longtext',
2674 'contribution_note' => 'contribution_note text',
2675 'contribution_batch' => 'contribution_batch text',
2676 'contribution_campaign_title' => 'contribution_campaign_title varchar(255)',
07b892fc 2677 'contribution_campaign_id' => 'contribution_campaign_id varchar(16)',
0a3b035f 2678 'contribution_soft_credit_name' => 'contribution_soft_credit_name varchar(255)',
2679 'contribution_soft_credit_amount' => 'contribution_soft_credit_amount varchar(255)',
2680 'contribution_soft_credit_type' => 'contribution_soft_credit_type varchar(255)',
2681 'contribution_soft_credit_contact_id' => 'contribution_soft_credit_contact_id varchar(255)',
2682 'contribution_soft_credit_contribution_id' => 'contribution_soft_credit_contribution_id varchar(255)',
2683 ];
2684 }
2685
2686 /**
2687 * Get pledge sql columns.
2688 *
2689 * @return array
2690 */
2691 public function getPledgeSqlColumns() {
2692 return [
2693 'pledge_id' => 'pledge_id varchar(16)',
2694 'pledge_amount' => 'pledge_amount varchar(32)',
2695 'pledge_total_paid' => 'pledge_total_paid text',
2696 'pledge_create_date' => 'pledge_create_date varchar(32)',
a5e468ff 2697 'pledge_start_date' => 'pledge_start_date varchar(32)',
0a3b035f 2698 'pledge_next_pay_date' => 'pledge_next_pay_date text',
2699 'pledge_next_pay_amount' => 'pledge_next_pay_amount text',
2700 'pledge_status' => 'pledge_status varchar(255)',
2701 'pledge_is_test' => 'pledge_is_test varchar(16)',
2702 'pledge_contribution_page_id' => 'pledge_contribution_page_id varchar(255)',
2703 'pledge_financial_type' => 'pledge_financial_type text',
2704 'pledge_frequency_interval' => 'pledge_frequency_interval varchar(255)',
2705 'pledge_frequency_unit' => 'pledge_frequency_unit varchar(255)',
2706 'pledge_currency' => 'pledge_currency text',
07b892fc 2707 'pledge_campaign_id' => 'pledge_campaign_id varchar(16)',
0a3b035f 2708 'pledge_balance_amount' => 'pledge_balance_amount text',
2709 'pledge_payment_id' => 'pledge_payment_id varchar(16)',
2710 'pledge_payment_scheduled_amount' => 'pledge_payment_scheduled_amount varchar(32)',
2711 'pledge_payment_scheduled_date' => 'pledge_payment_scheduled_date varchar(32)',
2712 'pledge_payment_paid_amount' => 'pledge_payment_paid_amount text',
2713 'pledge_payment_paid_date' => 'pledge_payment_paid_date text',
2714 'pledge_payment_reminder_date' => 'pledge_payment_reminder_date varchar(32)',
2715 'pledge_payment_reminder_count' => 'pledge_payment_reminder_count varchar(16)',
2716 'pledge_payment_status' => 'pledge_payment_status varchar(255)',
2717 ];
2718 }
2719
2720 /**
2721 * Get membership sql columns.
2722 *
2723 * @return array
2724 */
2725 public function getMembershipSqlColumns() {
2726 return [
2727 'membership_type' => 'membership_type varchar(128)',
2728 'member_is_test' => 'member_is_test varchar(16)',
2729 'member_is_pay_later' => 'member_is_pay_later varchar(16)',
2730 'join_date' => 'join_date varchar(32)',
2731 'membership_start_date' => 'membership_start_date varchar(32)',
2732 'membership_end_date' => 'membership_end_date varchar(32)',
2733 'membership_source' => 'membership_source varchar(128)',
2734 'membership_status' => 'membership_status varchar(255)',
2735 'membership_id' => 'membership_id varchar(16)',
2736 'owner_membership_id' => 'owner_membership_id varchar(16)',
8f67d99a 2737 'max_related' => 'max_related varchar(16)',
2738 'membership_recur_id' => 'membership_recur_id varchar(16)',
07b892fc 2739 'member_campaign_id' => 'member_campaign_id varchar(16)',
8f67d99a 2740 'member_is_override' => 'member_is_override varchar(16)',
0a3b035f 2741 ];
2742 }
2743
a16a432a 2744 /**
2745 * Change our location types so we have some edge cases in the mix.
2746 *
2747 * - a space in the name
2748 * - name differs from label
2749 * - non-anglo char in the label (not valid in the name).
2750 */
2751 protected function diversifyLocationTypes() {
2752 $this->locationTypes['Main'] = $this->callAPISuccess('Location_type', 'get', [
2753 'name' => 'Main',
2754 'return' => 'id',
2755 'api.LocationType.Create' => ['display_name' => 'Méin'],
2756 ]);
2757 $this->locationTypes['Whare Kai'] = $this->callAPISuccess('Location_type', 'create', [
2758 'name' => 'Whare Kai',
2759 'display_name' => 'Whare Kai',
2760 ]);
2761 }
2762
12a6fdef 2763 /**
2764 * Test export components.
2765 *
2766 * Tests the exportComponents function with the provided parameters.
2767 *
2768 * This exportComponents will export a csv but it will also throw a prematureExitException
2769 * which we catch & grab the processor from.
2770 *
2771 * $this->processor is set to the export processor.
2772 *
2773 * @param $params
2774 *
2775 * @throws \CRM_Core_Exception
c746d2b2 2776 * @throws \League\Csv\Exception
12a6fdef 2777 */
2778 protected function doExportTest($params) {
2779 $this->startCapturingOutput();
2780 try {
0b60a345 2781 $exportMode = CRM_Utils_Array::value('exportMode', $params, CRM_Export_Form_Select::CONTACT_EXPORT);
2782 $ids = CRM_Utils_Array::value('ids', $params, ($exportMode === CRM_Export_Form_Select::CONTACT_EXPORT ? $this->contactIDs : []));
2783 $defaultClause = (empty($ids) ? NULL : "contact_a.id IN (" . implode(',', $ids) . ")");
12a6fdef 2784 CRM_Export_BAO_Export::exportComponents(
2785 CRM_Utils_Array::value('selectAll', $params, (empty($params['fields']))),
0b60a345 2786 $ids,
12a6fdef 2787 CRM_Utils_Array::value('params', $params, []),
2788 CRM_Utils_Array::value('order', $params),
e15ec147 2789 CRM_Utils_Array::value('fields', $params, []),
12a6fdef 2790 CRM_Utils_Array::value('moreReturnProperties', $params),
0b60a345 2791 $exportMode,
45cee669 2792 CRM_Utils_Array::value('componentClause', $params, $defaultClause),
12a6fdef 2793 CRM_Utils_Array::value('componentTable', $params),
2794 CRM_Utils_Array::value('mergeSameAddress', $params, FALSE),
0b60a345 2795 CRM_Utils_Array::value('mergeSameHousehold', $params, FALSE),
2796 CRM_Utils_Array::value('exportParams', $params, [])
12a6fdef 2797 );
2798 }
2799 catch (CRM_Core_Exception_PrematureExitException $e) {
2800 $this->processor = $e->errorData['processor'];
c746d2b2 2801 $this->csv = $this->captureOutputToCSV();
12a6fdef 2802 return;
2803 }
2804 $this->fail('We expected a premature exit exception');
2805 }
2806
c2aec6ad 2807 /**
2808 * Assert that the received array matches the expected, ignoring time sensitive fields.
2809 *
2810 * @param array $expected
2811 * @param array $row
2812 */
2813 protected function assertExpectedOutput(array $expected, array $row) {
2814 $variableFields = ['Created Date', 'Modified Date', 'Contact Hash'];
2815 foreach ($expected as $key => $value) {
2816 if (in_array($key, $variableFields)) {
2817 $this->assertTrue(!empty($row[$key]));
2818 }
2819 else {
2820 $this->assertEquals($value, $row[$key]);
2821 }
2822 }
2823 }
2824
978f5005 2825 /**
2826 * Test get preview function on export processor.
2827 *
2828 * @throws \CRM_Core_Exception
2829 */
2830 public function testExportGetPreview() {
2831 $this->setUpContactExportData();
2832 $fields = [
2833 ['Individual', 'first_name'],
2834 ['Individual', 'last_name'],
2835 ['Individual', 'street_address', 1],
2836 ['Individual', 'city', 1],
2837 ['Individual', 'country', 1],
2838 ['Individual', 'email', 1],
2839 ];
2840 $mappedFields = [];
2841 foreach ($fields as $field) {
2842 $mappedFields[] = CRM_Core_BAO_Mapping::getMappingParams([], $field);
2843 }
2844 $processor = new CRM_Export_BAO_ExportProcessor(FALSE, $mappedFields,
2845 'AND');
2846 $processor->setComponentClause('contact_a.id IN (' . implode(',', $this->contactIDs) . ')');
2847 $result = $processor->getPreview(2);
2848 $this->assertEquals([
2849 [
2850 'first_name' => 'Anthony',
2851 'last_name' => 'Anderson',
2852 'Home-street_address' => 'Ambachtstraat 23',
2853 'Home-city' => 'Brummen',
2854 'Home-country' => 'Netherlands',
2855 'Home-email' => 'home@example.com',
2856 ],
2857 [
2858 'first_name' => 'Anthony',
2859 'last_name' => 'Anderson',
2860 'Home-street_address' => '',
2861 'Home-city' => '',
2862 'Home-country' => '',
2863 'Home-email' => 'anthony_anderson@civicrm.org',
2864 ],
2865 ], $result);
2866 }
2867
0b60a345 2868 /**
2869 * Set up contacts which will be merged with the same address option.
2870 *
2871 * @throws \CRM_Core_Exception
2872 */
2873 protected function setUpContactSameAddressExportData() {
2874 $this->setUpContactExportData();
2875 $this->contactIDs[] = $contact3 = $this->individualCreate(['first_name' => 'Sarah', 'last_name' => 'Smith', 'prefix_id' => 'Dr.']);
2876 // Create address for contact A.
2877 $params = [
2878 'contact_id' => $contact3,
2879 'location_type_id' => 'Home',
2880 'street_address' => 'Ambachtstraat 23',
2881 'postal_code' => '6971 BN',
2882 'country_id' => '1152',
2883 'city' => 'Brummen',
2884 'is_primary' => 1,
2885 ];
2886 $this->callAPISuccess('address', 'create', $params);
2887 }
2888
ef51caa8 2889}