3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * File for the CiviCRM APIv3 job functions
15 * @package CiviCRM_APIv3
18 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 use Civi\Api4\Contact
;
24 * Class api_v3_JobTest
28 class api_v3_JobTest
extends CiviUnitTestCase
{
30 public $DBResetRequired = FALSE;
32 public $_entity = 'Job';
35 * Created membership type.
37 * Must be created outside the transaction due to it breaking the transaction.
41 public $membershipTypeID;
44 * Report instance used in mail_report tests.
47 private $report_instance;
52 * @throws \CRM_Core_Exception
54 public function setUp(): void
{
56 $this->membershipTypeID
= $this->membershipTypeCreate(['name' => 'General']);
57 $this->useTransaction();
60 'name' => 'API_Test_Job',
61 'description' => 'A long description written by hand in cursive',
62 'run_frequency' => 'Daily',
63 'api_entity' => 'ApiTestEntity',
64 'api_action' => 'api_test_action',
65 'parameters' => 'Semi-formal explanation of runtime job parameters',
68 $this->report_instance
= $this->createReportInstance();
74 * @throws \CRM_Core_Exception
76 public function tearDown(): void
{
78 // The membershipType create breaks transactions so this extra cleanup is needed.
79 $this->membershipTypeDelete(['id' => $this->membershipTypeID
]);
80 $this->cleanUpSetUpIDs();
81 $this->quickCleanUpFinancialEntities();
82 $this->quickCleanup(['civicrm_contact', 'civicrm_address', 'civicrm_email', 'civicrm_website', 'civicrm_phone'], TRUE);
89 public function testCreateWithoutName(): void
{
93 $this->callAPIFailure('job', 'create', $params,
94 'Mandatory key(s) missing from params array: run_frequency, name, api_entity, api_action'
99 * Create job with an invalid "run_frequency" value.
101 public function testCreateWithInvalidFrequency(): void
{
104 'name' => 'API_Test_Job',
105 'description' => 'A long description written by hand in cursive',
106 'run_frequency' => 'Fortnightly',
107 'api_entity' => 'ApiTestEntity',
108 'api_action' => 'api_test_action',
109 'parameters' => 'Semi-formal explanation of runtime job parameters',
112 $this->callAPIFailure('job', 'create', $params);
118 public function testCreate(): void
{
119 $result = $this->callAPIAndDocument('job', 'create', $this->_params
, __FUNCTION__
, __FILE__
);
120 $this->assertNotNull($result['values'][0]['id']);
122 // mutate $params to match expected return value
123 unset($this->_params
['sequential']);
124 //assertDBState compares expected values in $result to actual values in the DB
125 $this->assertDBState('CRM_Core_DAO_Job', $result['id'], $this->_params
);
131 * @throws \CRM_Core_Exception
133 public function testClone(): void
{
134 $createResult = $this->callAPISuccess('job', 'create', $this->_params
);
135 $params = ['id' => $createResult['id']];
136 $cloneResult = $this->callAPIAndDocument('job', 'clone', $params, __FUNCTION__
, __FILE__
);
137 $clonedJob = $cloneResult['values'][$cloneResult['id']];
138 $this->assertEquals($this->_params
['name'] . ' - Copy', $clonedJob['name']);
139 $this->assertEquals($this->_params
['description'], $clonedJob['description']);
140 $this->assertEquals($this->_params
['parameters'], $clonedJob['parameters']);
141 $this->assertEquals($this->_params
['is_active'], $clonedJob['is_active']);
142 $this->assertArrayNotHasKey('last_run', $clonedJob);
143 $this->assertArrayNotHasKey('scheduled_run_date', $clonedJob);
147 * Check if required fields are not passed.
149 public function testDeleteWithoutRequired(): void
{
151 'name' => 'API_Test_PP',
152 'title' => 'API Test Payment Processor',
153 'class_name' => 'CRM_Core_Payment_APITest',
156 $result = $this->callAPIFailure('job', 'delete', $params);
157 $this->assertEquals('Mandatory key(s) missing from params array: id', $result['error_message']);
161 * Check with incorrect required fields.
163 public function testDeleteWithIncorrectData(): void
{
167 $this->callAPIFailure('job', 'delete', $params);
173 * @throws \CRM_Core_Exception
175 public function testDelete(): void
{
176 $createResult = $this->callAPISuccess('job', 'create', $this->_params
);
177 $params = ['id' => $createResult['id']];
178 $this->callAPIAndDocument('job', 'delete', $params, __FUNCTION__
, __FILE__
);
179 $this->assertAPIDeleted($this->_entity
, $createResult['id']);
183 * Test greeting update job.
185 * Note that this test is about testing the metadata / calling of the
186 * function & doesn't test the success of the called function
188 * @throws \API_Exception
190 public function testCallUpdateGreetingSuccess(): void
{
191 $contactID = $this->individualCreate();
192 // Clear out the postal greeting
193 CRM_Core_DAO
::executeQuery('UPDATE civicrm_contact SET postal_greeting_display = NULL WHERE id = ' . $contactID);
194 $this->callAPISuccess($this->_entity
, 'update_greeting', [
195 'gt' => 'postal_greeting',
196 'ct' => 'Individual',
198 $this->assertEquals('Dear Anthony', Contact
::get()
199 ->addWhere('id', '=', $contactID)
200 ->addSelect('postal_greeting_display')
201 ->execute()->first()['postal_greeting_display']
206 * Test greeting update handles comma separated params.
208 * @throws \CRM_Core_Exception
210 public function testCallUpdateGreetingCommaSeparatedParamsSuccess(): void
{
211 $gt = 'postal_greeting,email_greeting,addressee';
212 $ct = 'Individual,Household';
213 $this->callAPISuccess($this->_entity
, 'update_greeting', ['gt' => $gt, 'ct' => $ct]);
217 * Test the call reminder success sends more than 25 reminders & is not
218 * incorrectly limited.
220 * Note that this particular test sends the reminders to the additional
221 * recipients only as no real reminder person is configured
223 * Also note that this is testing a 'job' api so is in this class rather than
224 * scheduled_reminder - which seems a cleaner place to build up a collection
225 * of scheduled reminder testing functions. However, it seems that the api
226 * itself would need to be moved to the scheduled_reminder fn to do that
227 * with the job wrapper being respected for legacy functions
229 * @throws \CRM_Core_Exception
230 * @throws \CiviCRM_API3_Exception
232 public function testCallSendReminderSuccessMoreThanDefaultLimit(): void
{
233 $membershipTypeID = $this->membershipTypeCreate();
234 $this->membershipStatusCreate();
236 for ($i = 1; $i <= $createTotal; $i++
) {
237 $contactID = $this->individualCreate();
238 $groupID = $this->groupCreate(['name' => $i, 'title' => $i]);
239 $this->callAPISuccess('action_schedule', 'create', [
240 'title' => " job $i",
241 'subject' => "job $i",
242 'entity_value' => $membershipTypeID,
244 'start_action_date' => 'membership_join_date',
245 'start_action_offset' => 0,
246 'start_action_condition' => 'before',
247 'start_action_unit' => 'hour',
248 'group_id' => $groupID,
251 $this->callAPISuccess('group_contact', 'create', [
252 'contact_id' => $contactID,
254 'group_id' => $groupID,
257 $this->callAPISuccess('job', 'send_reminder', []);
258 $successfulCronCount = CRM_Core_DAO
::singleValueQuery("SELECT count(*) FROM civicrm_action_log");
259 $this->assertEquals($successfulCronCount, $createTotal);
263 * Test scheduled reminders respect limit to (since above identified addition_to handling issue).
265 * We create 3 contacts - 1 is in our group, 1 has our membership & the chosen one has both
266 * & check that only the chosen one got the reminder
268 * @throws \CRM_Core_Exception
269 * @throws \CiviCRM_API3_Exception
271 public function testCallSendReminderLimitToSMS(): void
{
272 [$membershipTypeID, $groupID, $theChosenOneID, $provider] = $this->setUpMembershipSMSReminders();
273 $this->callAPISuccess('action_schedule', 'create', [
274 'title' => ' remind all Texans',
275 'subject' => 'drawling renewal',
276 'entity_value' => $membershipTypeID,
278 'start_action_date' => 'membership_start_date',
279 'start_action_offset' => 1,
280 'start_action_condition' => 'before',
281 'start_action_unit' => 'day',
282 'group_id' => $groupID,
284 'sms_provider_id' => $provider['id'],
285 'mode' => 'User_Preference',
287 $this->callAPISuccess('job', 'send_reminder', []);
288 $successfulCronCount = CRM_Core_DAO
::singleValueQuery("SELECT count(*) FROM civicrm_action_log");
289 $this->assertEquals(1, $successfulCronCount);
290 $sentToID = CRM_Core_DAO
::singleValueQuery("SELECT contact_id FROM civicrm_action_log");
291 $this->assertEquals($sentToID, $theChosenOneID);
292 $this->assertEquals(0, CRM_Core_DAO
::singleValueQuery("SELECT is_error FROM civicrm_action_log"));
293 $this->setupForSmsTests(TRUE);
297 * Test disabling expired relationships.
299 * @throws \CRM_Core_Exception
300 * @throws \CiviCRM_API3_Exception
302 public function testCallDisableExpiredRelationships(): void
{
303 $individualID = $this->individualCreate();
304 $orgID = $this->organizationCreate();
305 CRM_Utils_Hook_UnitTests
::singleton()->setHook('civicrm_pre', [$this, 'hookPreRelationship']);
306 $relationshipTypeID = $this->callAPISuccess('relationship_type', 'getvalue', [
308 'name_a_b' => 'Employee of',
310 $result = $this->callAPISuccess('relationship', 'create', [
311 'relationship_type_id' => $relationshipTypeID,
312 'contact_id_a' => $individualID,
313 'contact_id_b' => $orgID,
315 'end_date' => 'yesterday',
317 $relationshipID = $result['id'];
318 $this->assertEquals('Hooked', $result['values'][$relationshipID]['description']);
319 $this->callAPISuccess($this->_entity
, 'disable_expired_relationships', []);
320 $result = $this->callAPISuccess('relationship', 'get', []);
321 $this->assertEquals('Go Go you good thing', $result['values'][$relationshipID]['description']);
322 $this->contactDelete($individualID);
323 $this->contactDelete($orgID);
327 * Event templates should not send reminders to additional contacts.
329 * @throws \CRM_Core_Exception
330 * @throws \CiviCRM_API3_Exception
332 public function testTemplateRemindAdditionalContacts(): void
{
333 $contactId = $this->individualCreate();
334 $groupId = $this->groupCreate(['name' => 'Additional Contacts', 'title' => 'Additional Contacts']);
335 $this->callAPISuccess('GroupContact', 'create', [
336 'contact_id' => $contactId,
337 'group_id' => $groupId,
339 $event = $this->eventCreate(['is_template' => 1, 'template_title' => "I'm a template", 'title' => NULL]);
340 $eventId = $event['id'];
342 $this->callAPISuccess('action_schedule', 'create', [
343 'title' => 'Do not send me',
344 'subject' => 'I am a reminder attached to a template.',
345 'entity_value' => $eventId,
347 'start_action_date' => 'start_date',
348 'start_action_offset' => 1,
349 'start_action_condition' => 'before',
350 'start_action_unit' => 'day',
351 'group_id' => $groupId,
356 $this->callAPISuccess('job', 'send_reminder', []);
357 $successfulCronCount = CRM_Core_DAO
::singleValueQuery('SELECT count(*) FROM civicrm_action_log');
358 $this->assertEquals(0, $successfulCronCount);
362 * Deleted events should not send reminders to additional contacts.
364 * @throws \CRM_Core_Exception
365 * @throws \CiviCRM_API3_Exception
367 public function testDeletedEventRemindAdditionalContacts(): void
{
368 $contactId = $this->individualCreate();
369 $groupId = $this->groupCreate(['name' => 'Additional Contacts', 'title' => 'Additional Contacts']);
370 $this->callAPISuccess('GroupContact', 'create', [
371 'contact_id' => $contactId,
372 'group_id' => $groupId,
374 $event = $this->eventCreate(['title' => 'delete this event']);
375 $eventId = $event['id'];
377 $this->callAPISuccess('action_schedule', 'create', [
378 'title' => 'Do not send me',
379 'subject' => 'I am a reminder attached to a (soon to be) deleted event.',
380 'entity_value' => $eventId,
381 'mapping_id' => CRM_Event_ActionMapping
::EVENT_NAME_MAPPING_ID
,
382 'start_action_date' => 'start_date',
383 'start_action_offset' => 1,
384 'start_action_condition' => 'before',
385 'start_action_unit' => 'day',
386 'group_id' => $groupId,
390 $this->callAPISuccess('event', 'delete', ['id' => $eventId]);
392 $this->callAPISuccess('job', 'send_reminder', []);
393 $successfulCronCount = CRM_Core_DAO
::singleValueQuery('SELECT count(*) FROM civicrm_action_log');
394 $this->assertEquals(0, $successfulCronCount);
398 * Test scheduled reminders respect limit to (since above identified addition_to handling issue).
400 * We create 3 contacts - 1 is in our group, 1 has our membership & the chosen one has both
401 * & check that only the chosen one got the reminder
403 * Also check no hard fail on cron job with running a reminder that has a deleted SMS provider
405 * @throws \CRM_Core_Exception
406 * @throws \CiviCRM_API3_Exception
408 public function testCallSendReminderLimitToSMSWithDeletedProvider(): void
{
409 [$membershipTypeID, $groupID, $theChosenOneID, $provider] = $this->setUpMembershipSMSReminders();
410 $this->callAPISuccess('action_schedule', 'create', [
411 'title' => ' remind all Texans',
412 'subject' => 'drawling renewal',
413 'entity_value' => $membershipTypeID,
415 'start_action_date' => 'membership_start_date',
416 'start_action_offset' => 1,
417 'start_action_condition' => 'before',
418 'start_action_unit' => 'day',
419 'group_id' => $groupID,
421 'sms_provider_id' => $provider['id'],
424 $this->callAPISuccess('SmsProvider', 'delete', ['id' => $provider['id']]);
425 $this->callAPISuccess('job', 'send_reminder', []);
426 $cronCount = CRM_Core_DAO
::singleValueQuery("SELECT count(*) FROM civicrm_action_log");
427 $this->assertEquals(1, $cronCount);
428 $sentToID = CRM_Core_DAO
::singleValueQuery("SELECT contact_id FROM civicrm_action_log");
429 $this->assertEquals($sentToID, $theChosenOneID);
430 $cronLog = CRM_Core_DAO
::executeQuery("SELECT * FROM civicrm_action_log")->fetchAll()[0];
431 $this->assertEquals(1, $cronLog['is_error']);
432 $this->assertEquals('SMS reminder cannot be sent because the SMS provider has been deleted.', $cronLog['message']);
433 $this->setupForSmsTests(TRUE);
437 * Test the batch merge function.
439 * We are just checking it returns without error here.
441 * @throws \CRM_Core_Exception
443 public function testBatchMerge(): void
{
444 $this->callAPISuccess('Job', 'process_batch_merge', []);
448 * Test the batch merge function actually works!
450 * @dataProvider getMergeSets
454 * @throws \CRM_Core_Exception
456 public function testBatchMergeWorks($dataSet): void
{
457 foreach ($dataSet['contacts'] as $params) {
458 $this->callAPISuccess('Contact', 'create', $params);
461 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['mode' => $dataSet['mode']]);
462 $this->assertCount($dataSet['skipped'], $result['values']['skipped'], 'Failed to skip the right number:' . $dataSet['skipped']);
463 $this->assertCount($dataSet['merged'], $result['values']['merged']);
464 $result = $this->callAPISuccess('Contact', 'get', [
465 'contact_sub_type' => 'Student',
467 'is_deceased' => ['IN' => [0, 1]],
468 'options' => ['sort' => 'id ASC'],
470 $this->assertEquals(count($dataSet['expected']), $result['count']);
471 foreach ($dataSet['expected'] as $index => $contact) {
472 foreach ($contact as $key => $value) {
473 if ($key === 'gender_id') {
476 $this->assertEquals($value, $result['values'][$index][$key]);
482 * Check that the merge carries across various related entities.
484 * Note the group combinations & expected results:
486 * @throws \CRM_Core_Exception
487 * @throws \CiviCRM_API3_Exception
489 public function testBatchMergeWithAssets(): void
{
490 $contactID = $this->individualCreate();
491 $contact2ID = $this->individualCreate();
492 $this->contributionCreate(['contact_id' => $contactID]);
493 $this->contributionCreate(['contact_id' => $contact2ID, 'invoice_id' => '2', 'trxn_id' => 2]);
494 $this->contactMembershipCreate(['contact_id' => $contactID]);
495 $this->contactMembershipCreate(['contact_id' => $contact2ID]);
496 $this->activityCreate(['source_contact_id' => $contactID, 'target_contact_id' => $contactID, 'assignee_contact_id' => $contactID]);
497 $this->activityCreate(['source_contact_id' => $contact2ID, 'target_contact_id' => $contact2ID, 'assignee_contact_id' => $contact2ID]);
498 $this->tagCreate(['name' => 'Tall']);
499 $this->tagCreate(['name' => 'Short']);
500 $this->entityTagAdd(['contact_id' => $contactID, 'tag_id' => 'Tall']);
501 $this->entityTagAdd(['contact_id' => $contact2ID, 'tag_id' => 'Short']);
502 $this->entityTagAdd(['contact_id' => $contact2ID, 'tag_id' => 'Tall']);
503 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['mode' => 'safe']);
504 $this->assertCount(0, $result['values']['skipped']);
505 $this->assertCount(1, $result['values']['merged']);
506 $this->callAPISuccessGetCount('Contribution', ['contact_id' => $contactID], 2);
507 $this->callAPISuccessGetCount('Contribution', ['contact_id' => $contact2ID], 0);
508 $this->callAPISuccessGetCount('FinancialItem', ['contact_id' => $contactID], 2);
509 $this->callAPISuccessGetCount('FinancialItem', ['contact_id' => $contact2ID], 0);
510 $this->callAPISuccessGetCount('Membership', ['contact_id' => $contactID], 2);
511 $this->callAPISuccessGetCount('Membership', ['contact_id' => $contact2ID], 0);
512 $this->callAPISuccessGetCount('EntityTag', ['contact_id' => $contactID], 2);
513 $this->callAPISuccessGetCount('EntityTag', ['contact_id' => $contact2ID], 0);
514 // 14 activities is one for each contribution (2), two (source + target) for each membership (+(2x2) = 6)
515 // 3 for each of the added activities as there are 3 roles (+6 = 12
516 // 2 for the (source & target) contact merged activity (+2 = 14)
517 $this->callAPISuccessGetCount('ActivityContact', ['contact_id' => $contactID], 14);
518 // 2 for the connection to the deleted by merge activity (source & target)
519 $this->callAPISuccessGetCount('ActivityContact', ['contact_id' => $contact2ID], 2);
523 * Test that non-contact entity tags are untouched in merge.
525 * @throws \CRM_Core_Exception
526 * @throws \CiviCRM_API3_Exception
528 public function testContributionEntityTag(): void
{
529 $this->callAPISuccess('OptionValue', 'create', ['option_group_id' => 'tag_used_for', 'value' => 'civicrm_contribution', 'label' => 'Contribution']);
530 $tagID = $this->tagCreate(['name' => 'Big', 'used_for' => 'civicrm_contribution'])['id'];
531 $contact1 = $this->individualCreate();
532 $contact2 = $this->individualCreate();
533 $contributionID = NULL;
534 while ($contributionID !== $contact2) {
535 $contributionID = (int) $this->callAPISuccess('Contribution', 'create', ['contact_id' => $contact1, 'total_amount' => 5, 'financial_type_id' => 'Donation'])['id'];
537 $entityTagParams = ['entity_id' => $contributionID, 'entity_table' => 'civicrm_contribution', 'tag_id' => $tagID];
538 $this->callAPISuccess('EntityTag', 'create', $entityTagParams);
539 $this->callAPISuccessGetSingle('EntityTag', $entityTagParams);
540 $this->callAPISuccess('Job', 'process_batch_merge', ['mode' => 'safe']);
541 $this->callAPISuccessGetSingle('EntityTag', $entityTagParams);
545 * Check that the merge carries across various related entities.
547 * Note the group combinations 'expected' results:
549 * Group 0 Added null Added
550 * Group 1 Added Added Added
551 * Group 2 Added Removed **** Added
552 * Group 3 Removed null **** null
553 * Group 4 Removed Added **** Added
554 * Group 5 Removed Removed **** null
555 * Group 6 null Added Added
556 * Group 7 null Removed **** null
558 * The ones with **** are the ones where I think a case could be made to change the behaviour.
560 * @throws \CRM_Core_Exception
561 * @throws \CiviCRM_API3_Exception
563 public function testBatchMergeMergesGroups(): void
{
564 $contactID = $this->individualCreate();
565 $contact2ID = $this->individualCreate();
567 for ($i = 0; $i < 8; $i++
) {
568 $groups[] = $this->groupCreate([
569 'name' => 'mergeGroup' . $i,
570 'title' => 'merge group' . $i,
574 $this->callAPISuccess('GroupContact', 'create', [
575 'contact_id' => $contactID,
576 'group_id' => $groups[0],
578 $this->callAPISuccess('GroupContact', 'create', [
579 'contact_id' => $contactID,
580 'group_id' => $groups[1],
582 $this->callAPISuccess('GroupContact', 'create', [
583 'contact_id' => $contactID,
584 'group_id' => $groups[2],
586 $this->callAPISuccess('GroupContact', 'create', [
587 'contact_id' => $contactID,
588 'group_id' => $groups[3],
589 'status' => 'Removed',
591 $this->callAPISuccess('GroupContact', 'create', [
592 'contact_id' => $contactID,
593 'group_id' => $groups[4],
594 'status' => 'Removed',
596 $this->callAPISuccess('GroupContact', 'create', [
597 'contact_id' => $contactID,
598 'group_id' => $groups[5],
599 'status' => 'Removed',
601 $this->callAPISuccess('GroupContact', 'create', [
602 'contact_id' => $contact2ID,
603 'group_id' => $groups[1],
605 $this->callAPISuccess('GroupContact', 'create', [
606 'contact_id' => $contact2ID,
607 'group_id' => $groups[2],
608 'status' => 'Removed',
610 $this->callAPISuccess('GroupContact', 'create', [
611 'contact_id' => $contact2ID,
612 'group_id' => $groups[4],
614 $this->callAPISuccess('GroupContact', 'create', [
615 'contact_id' => $contact2ID,
616 'group_id' => $groups[5],
617 'status' => 'Removed',
619 $this->callAPISuccess('GroupContact', 'create', [
620 'contact_id' => $contact2ID,
621 'group_id' => $groups[6],
623 $this->callAPISuccess('GroupContact', 'create', [
624 'contact_id' => $contact2ID,
625 'group_id' => $groups[7],
626 'status' => 'Removed',
628 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['mode' => 'safe']);
629 $this->assertCount(0, $result['values']['skipped']);
630 $this->assertCount(1, $result['values']['merged']);
631 $groupResult = $this->callAPISuccess('GroupContact', 'get', []);
632 $this->assertEquals(5, $groupResult['count']);
640 foreach ($groupResult['values'] as $groupValues) {
641 $this->assertEquals($contactID, $groupValues['contact_id']);
642 $this->assertEquals('Added', $groupValues['status']);
643 $this->assertContains($groupValues['group_id'], $expectedGroups);
649 * Test that we handle cache entries without clashes.
651 * @throws \CRM_Core_Exception
652 * @throws \CiviCRM_API3_Exception
654 public function testMergeCaches(): void
{
655 $contactID = $this->individualCreate();
656 $contact2ID = $this->individualCreate();
657 $groupID = $this->groupCreate();
658 $this->callAPISuccess('GroupContact', 'create', ['group_id' => $groupID, 'contact_id' => $contactID]);
659 $this->callAPISuccess('GroupContact', 'create', ['group_id' => $groupID, 'contact_id' => $contact2ID]);
660 CRM_Core_DAO
::executeQuery("INSERT INTO civicrm_group_contact_cache(group_id, contact_id) VALUES
661 ($groupID, $contactID),
662 ($groupID, $contact2ID)
664 $this->callAPISuccess('Job', 'process_batch_merge', ['mode' => 'safe']);
668 * Test that we handle cache entries without clashes.
670 * @throws \CiviCRM_API3_Exception|\CRM_Core_Exception
672 public function testMergeSharedActivity(): void
{
673 $contactID = $this->individualCreate();
674 $contact2ID = $this->individualCreate();
675 $this->activityCreate(['target_contact_id' => [$contactID, $contact2ID]]);
676 $this->callAPISuccess('Job', 'process_batch_merge', ['mode' => 'safe']);
680 * Test the decisions made for addresses when merging.
682 * @dataProvider getMergeLocationData
685 * (the ones with **** could be disputed as whether it is the best outcome).
686 * 'matching_primary' - Primary matches, including location_type_id. One contact has an additional address.
687 * - result - primary is the shared one. Additional address is retained.
688 * 'matching_primary_reverse' - Primary matches, including location_type_id. Keep both. (opposite order)
689 * - result - primary is the shared one. Additional address is retained.
690 * 'only_one_has_address' - Only one contact has addresses (retain)
691 * - the (only) address is retained
692 * 'only_one_has_address_reverse'
693 * - the (only) address is retained
694 * 'different_primaries_with_different_location_type' Primaries are different but do not clash due to diff type
695 * - result - both addresses kept. The one from the kept (lowest ID) contact is primary
696 * 'different_primaries_with_different_location_type_reverse' Primaries are different but do not clash due to diff type
697 * - result - both addresses kept. The one from the kept (lowest ID) contact is primary
698 * 'different_primaries_location_match_only_one_address' per previous but a second address matches the primary but is not primary
699 * - result - both addresses kept. The one from the kept (lowest ID) contact is primary
700 * 'different_primaries_location_match_only_one_address_reverse' per previous but a second address matches the primary but is not primary
701 * - result - both addresses kept. The one from the kept (lowest ID) contact is primary
702 * 'same_primaries_different_location' Primary addresses are the same but have different location type IDs
703 * - result primary kept with the lowest ID. Other address retained too (to preserve location type info).
704 * 'same_primaries_different_location_reverse' Primary addresses are the same but have different location type IDs
705 * - result primary kept with the lowest ID. Other address retained too (to preserve location type info).
707 * @param array $dataSet
709 * @throws \CRM_Core_Exception
710 * @throws \CiviCRM_API3_Exception
712 public function testBatchMergesAddresses(array $dataSet): void
{
713 $contactID1 = $this->individualCreate();
714 $contactID2 = $this->individualCreate();
715 foreach ($dataSet['contact_1'] as $address) {
716 $this->callAPISuccess($dataSet['entity'], 'create', array_merge(['contact_id' => $contactID1], $address));
718 foreach ($dataSet['contact_2'] as $address) {
719 $this->callAPISuccess($dataSet['entity'], 'create', array_merge(['contact_id' => $contactID2], $address));
722 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['mode' => 'safe']);
723 $this->assertCount(1, $result['values']['merged']);
724 $addresses = $this->callAPISuccess($dataSet['entity'], 'get', ['contact_id' => $contactID1, 'sequential' => 1]);
725 $this->assertEquals(count($dataSet['expected']), $addresses['count'], 'Did not get the expected result for ' . $dataSet['entity'] . (!empty($dataSet['description']) ?
" on dataset {$dataSet['description']}" : ''));
726 $locationTypes = $this->callAPISuccess($dataSet['entity'], 'getoptions', ['field' => 'location_type_id']);
727 foreach ($dataSet['expected'] as $index => $expectedAddress) {
728 foreach ($expectedAddress as $key => $value) {
729 if ($key === 'location_type_id') {
730 $this->assertEquals($locationTypes['values'][$addresses['values'][$index][$key]], $value);
733 $this->assertEquals($addresses['values'][$index][$key], $value, "mismatch on $key" . (!empty($dataSet['description']) ?
" on dataset {$dataSet['description']}" : ''));
740 * Test altering the address decision by hook.
742 * @dataProvider getMergeLocationData
744 * @param array $dataSet
746 * @throws \CRM_Core_Exception
747 * @throws \CiviCRM_API3_Exception
749 public function testBatchMergesAddressesHook(array $dataSet): void
{
750 $contactID1 = $this->individualCreate();
751 $contactID2 = $this->individualCreate();
752 $this->contributionCreate(['contact_id' => $contactID1, 'receive_date' => '2010-01-01', 'invoice_id' => 1, 'trxn_id' => 1]);
753 $this->contributionCreate(['contact_id' => $contactID2, 'receive_date' => '2012-01-01', 'invoice_id' => 2, 'trxn_id' => 2]);
754 foreach ($dataSet['contact_1'] as $address) {
755 $this->callAPISuccess($dataSet['entity'], 'create', array_merge(['contact_id' => $contactID1], $address));
757 foreach ($dataSet['contact_2'] as $address) {
758 $this->callAPISuccess($dataSet['entity'], 'create', array_merge(['contact_id' => $contactID2], $address));
760 $this->hookClass
->setHook('civicrm_alterLocationMergeData', [$this, 'hookMostRecentDonor']);
762 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['mode' => 'safe']);
763 $this->assertCount(1, $result['values']['merged']);
764 $addresses = $this->callAPISuccess($dataSet['entity'], 'get', ['contact_id' => $contactID1, 'sequential' => 1]);
765 $this->assertEquals(count($dataSet['expected_hook']), $addresses['count']);
766 $locationTypes = $this->callAPISuccess($dataSet['entity'], 'getoptions', ['field' => 'location_type_id']);
767 foreach ($dataSet['expected_hook'] as $index => $expectedAddress) {
768 foreach ($expectedAddress as $key => $value) {
769 if ($key === 'location_type_id') {
770 $this->assertEquals($locationTypes['values'][$addresses['values'][$index][$key]], $value);
773 $this->assertEquals($value, $addresses['values'][$index][$key], $dataSet['entity'] . ': Unexpected value for ' . $key . (!empty($dataSet['description']) ?
" on dataset {$dataSet['description']}" : ''));
780 * Test the organization will not be matched to an individual.
782 * @throws \CRM_Core_Exception
784 public function testBatchMergeWillNotMergeOrganizationToIndividual(): void
{
785 $individual = $this->callAPISuccess('Contact', 'create', [
786 'contact_type' => 'Individual',
787 'organization_name' => 'Anon',
788 'email' => 'anonymous@hacker.com',
790 $organization = $this->callAPISuccess('Contact', 'create', [
791 'contact_type' => 'Organization',
792 'organization_name' => 'Anon',
793 'email' => 'anonymous@hacker.com',
795 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['mode' => 'aggressive']);
796 $this->assertCount(0, $result['values']['skipped']);
797 $this->assertCount(0, $result['values']['merged']);
798 $this->callAPISuccessGetSingle('Contact', ['id' => $individual['id']]);
799 $this->callAPISuccessGetSingle('Contact', ['id' => $organization['id']]);
804 * Test hook allowing modification of the data calculated for merging locations.
806 * We are testing a nuanced real life situation where the address data of the
807 * most recent donor gets priority - resulting in the primary address being set
808 * to the primary address of the most recent donor and address data on a per
809 * location type basis also being set to the most recent donor. Hook also excludes
810 * a fully matching address with a different location.
812 * This has been added to the test suite to ensure the code supports more this
813 * type of intervention.
815 * @param array $blocksDAO
816 * Array of location DAO to be saved. These are arrays in 2 keys 'update' & 'delete'.
818 * Contact_id of the contact that survives the merge.
819 * @param int $otherId
820 * Contact_id of the contact that will be absorbed and deleted.
821 * @param array $migrationInfo
822 * Calculated migration info, informational only.
824 * @throws \CRM_Core_Exception
826 public function hookMostRecentDonor(array &$blocksDAO, int $mainId, int $otherId, array $migrationInfo): void
{
828 $lastDonorID = $this->callAPISuccessGetValue('Contribution', [
829 'return' => 'contact_id',
830 'contact_id' => ['IN' => [$mainId, $otherId]],
831 'options' => ['sort' => 'receive_date DESC', 'limit' => 1],
833 // Since the last donor is not the main ID we are prioritising info from the last donor.
834 // In the test this should always be true - but keep the check in case
835 // something changes that we need to detect.
836 if ($lastDonorID !== $mainId) {
837 foreach ($migrationInfo['other_details']['location_blocks'] as $blockType => $blocks) {
838 foreach ($blocks as $block) {
839 if ($block['is_primary']) {
840 $primaryAddressID = $block['id'];
841 if (!empty($migrationInfo['main_details']['location_blocks'][$blockType])) {
842 foreach ($migrationInfo['main_details']['location_blocks'][$blockType] as $mainBlock) {
843 if (empty($blocksDAO[$blockType]['update'][$block['id']]) && $mainBlock['location_type_id'] === $block['location_type_id']) {
844 // This was an address match - we just need to check the is_primary
845 // is true on the matching kept address.
846 $primaryAddressID = $mainBlock['id'];
847 $blocksDAO[$blockType]['update'][$primaryAddressID] = _civicrm_api3_load_DAO($blockType);
848 $blocksDAO[$blockType]['update'][$primaryAddressID]->id
= $primaryAddressID;
850 $mainLocationTypeID = $mainBlock['location_type_id'];
851 // We also want to be more ruthless about removing matching addresses.
852 unset($mainBlock['location_type_id']);
853 if (CRM_Dedupe_Merger
::locationIsSame($block, $mainBlock)
854 && (!isset($blocksDAO[$blockType]['update'][$mainBlock['id']]))
855 && (!isset($blocksDAO[$blockType]['delete'][$mainBlock['id']]))
857 $blocksDAO[$blockType]['delete'][$mainBlock['id']] = _civicrm_api3_load_DAO($blockType);
858 $blocksDAO[$blockType]['delete'][$mainBlock['id']]->id
= $mainBlock['id'];
860 // Arguably the right way to handle this is just to set is_primary for the primary
861 // and for the merge fn to call something like BAO::add & hooks to work etc.
862 // if that happens though this should keep working...
863 elseif ($mainBlock['is_primary'] && $mainLocationTypeID !== $block['location_type_id']) {
864 $blocksDAO['address']['update'][$mainBlock['id']] = _civicrm_api3_load_DAO($blockType);
865 $blocksDAO['address']['update'][$mainBlock['id']]->is_primary
= 0;
866 $blocksDAO['address']['update'][$mainBlock['id']]->id
= $mainBlock['id'];
870 $blocksDAO[$blockType]['update'][$primaryAddressID]->is_primary
= 1;
879 * Get address combinations for the merge test.
883 public function getMergeLocationData(): array {
884 $address1 = ['street_address' => 'Buckingham Palace', 'city' => 'London'];
885 $address2 = ['street_address' => 'The Doghouse', 'supplemental_address_1' => 'under the blanket'];
886 $data = $this->getMergeLocations($address1, $address2, 'Address');
887 $data = array_merge($data, $this->getMergeLocations(['phone' => '12345', 'phone_type_id' => 1], ['phone' => '678910', 'phone_type_id' => 1], 'Phone'));
888 $data = array_merge($data, $this->getMergeLocations(['phone' => '12345'], ['phone' => '678910'], 'Phone'));
889 $data = array_merge($data, $this->getMergeLocations(['email' => 'mini@me.com'], ['email' => 'mini@me.org'], 'Email', [
891 'email' => 'anthony_anderson@civicrm.org',
892 'location_type_id' => 'Home',
900 * Test weird characters don't mess with merge & cause a fatal.
902 * @throws \CRM_Core_Exception
903 * @throws \CiviCRM_API3_Exception
905 public function testNoErrorOnOdd(): void
{
906 $this->individualCreate();
907 $this->individualCreate(['first_name' => 'Gerrit%0a%2e%0a']);
908 $this->callAPISuccess('Job', 'process_batch_merge', []);
910 $this->individualCreate();
911 $this->individualCreate(['first_name' => '[foo\\bar\'baz']);
912 $this->callAPISuccess('Job', 'process_batch_merge', []);
913 $this->callAPISuccessGetSingle('Contact', ['first_name' => '[foo\\bar\'baz']);
917 * Test the batch merge does not create duplicate emails.
919 * Test CRM-18546, a 4.7 regression whereby a merged contact gets duplicate
922 * @throws \CRM_Core_Exception
923 * @throws \CiviCRM_API3_Exception
925 public function testBatchMergeEmailHandling(): void
{
926 for ($x = 0; $x <= 4; $x++
) {
927 $this->individualCreate(['email' => 'batman@gotham.met']);
929 $result = $this->callAPISuccess('Job', 'process_batch_merge', []);
930 $this->assertCount(4, $result['values']['merged']);
931 $this->callAPISuccessGetCount('Contact', ['email' => 'batman@gotham.met'], 1);
932 $contacts = $this->callAPISuccess('Contact', 'get', ['is_deleted' => 0]);
933 $deletedContacts = $this->callAPISuccess('Contact', 'get', ['is_deleted' => 1]);
934 $this->callAPISuccessGetCount('Email', [
935 'email' => 'batman@gotham.met',
936 'contact_id' => ['IN' => array_keys($contacts['values'])],
938 $this->callAPISuccessGetCount('Email', [
939 'email' => 'batman@gotham.met',
940 'contact_id' => ['IN' => array_keys($deletedContacts['values'])],
945 * Test the batch merge respects email "on hold".
947 * Test CRM-19148, Batch merge - Email on hold data lost when there is a conflict.
949 * @dataProvider getOnHoldSets
951 * @param bool $onHold1
952 * @param bool $onHold2
954 * @param string|null $conflictText
956 * @throws \CRM_Core_Exception
957 * @throws \CiviCRM_API3_Exception
959 public function testBatchMergeEmailOnHold($onHold1, $onHold2, bool $merge, ?
string $conflictText): void
{
960 $this->individualCreate([
961 'api.email.create' => [
962 'email' => 'batman@gotham.met',
963 'location_type_id' => 'Work',
965 'on_hold' => $onHold1,
968 $this->individualCreate([
969 'api.email.create' => [
970 'email' => 'batman@gotham.met',
971 'location_type_id' => 'Work',
973 'on_hold' => $onHold2,
976 $result = $this->callAPISuccess('Job', 'process_batch_merge', []);
977 $this->assertCount($merge, $result['values']['merged']);
979 $defaultRuleGroupID = $this->callAPISuccessGetValue('RuleGroup', [
980 'contact_type' => 'Individual',
981 'used' => 'Unsupervised',
983 'options' => ['limit' => 1],
986 $duplicates = $this->callAPISuccess('Dedupe', 'getduplicates', ['rule_group_id' => $defaultRuleGroupID]);
987 $this->assertEquals($conflictText, $duplicates['values'][0]['conflicts']);
992 * Data provider for testBatchMergeEmailOnHold: combinations of on_hold & expected outcomes.
994 public function getOnHoldSets(): array {
995 // Each row specifies: contact 1 on_hold, contact 2 on_hold, merge? (0 or 1),
998 [0, 1, FALSE, "Email 2 (Work): 'batman@gotham.met' vs. 'batman@gotham.met\n(On Hold)'"],
999 [1, 0, FALSE, "Email 2 (Work): 'batman@gotham.met\n(On Hold)' vs. 'batman@gotham.met'"],
1005 * Test the batch merge does not fatal on an empty rule.
1007 * @dataProvider getRuleSets
1009 * @param string $contactType
1010 * @param string $used
1011 * @param string $name
1012 * @param bool $isReserved
1013 * @param int $threshold
1015 * @throws \CRM_Core_Exception
1017 public function testBatchMergeEmptyRule(string $contactType, string $used, string $name, bool $isReserved, int $threshold): void
{
1018 $ruleGroup = $this->callAPISuccess('RuleGroup', 'create', [
1019 'contact_type' => $contactType,
1020 'threshold' => $threshold,
1023 'is_reserved' => $isReserved,
1025 $this->callAPISuccess('Job', 'process_batch_merge', ['rule_group_id' => $ruleGroup['id']]);
1026 $this->callAPISuccess('RuleGroup', 'delete', ['id' => $ruleGroup['id']]);
1030 * Get the various rule combinations.
1032 public function getRuleSets(): array {
1033 $contactTypes = ['Individual', 'Organization', 'Household'];
1034 $ruleTypes = ['Unsupervised', 'General', 'Supervised'];
1036 foreach ($contactTypes as $contactType) {
1037 foreach ($ruleTypes as $used) {
1038 $ruleGroups[] = [$contactType, $used, 'Bob', FALSE, 0];
1039 $ruleGroups[] = [$contactType, $used, 'Bob', FALSE, 10];
1040 $ruleGroups[] = [$contactType, $used, 'Bob', TRUE, 10];
1041 $ruleGroups[] = [$contactType, $used, $contactType . $used, FALSE, 10];
1042 $ruleGroups[] = [$contactType, $used, $contactType . $used, TRUE, 10];
1049 * Test the batch merge does not create duplicate emails.
1051 * Test CRM-18546, a 4.7 regression whereby a merged contact gets duplicate
1054 * @throws \CRM_Core_Exception
1055 * @throws \CiviCRM_API3_Exception
1057 public function testBatchMergeMatchingAddress(): void
{
1058 for ($x = 0; $x <= 2; $x++
) {
1059 $this->individualCreate([
1060 'api.address.create' => [
1061 'location_type_id' => 'Home',
1062 'street_address' => 'Appt 115, The Batcave',
1064 'postal_code' => 'Nananananana',
1068 // Different location type, still merge, identical.
1069 $this->individualCreate([
1070 'api.address.create' => [
1071 'location_type_id' => 'Main',
1072 'street_address' => 'Appt 115, The Batcave',
1074 'postal_code' => 'Nananananana',
1078 $this->individualCreate([
1079 'api.address.create' => [
1080 'location_type_id' => 'Home',
1081 'street_address' => 'Appt 115, The Batcave',
1083 'postal_code' => 'Batman',
1087 $result = $this->callAPISuccess('Job', 'process_batch_merge', []);
1088 $this->assertCount(3, $result['values']['merged']);
1089 $this->assertCount(1, $result['values']['skipped']);
1090 $this->callAPISuccessGetCount('Contact', ['street_address' => 'Appt 115, The Batcave'], 2);
1091 $contacts = $this->callAPISuccess('Contact', 'get', ['is_deleted' => 0]);
1092 $deletedContacts = $this->callAPISuccess('Contact', 'get', ['is_deleted' => 1]);
1093 $this->callAPISuccessGetCount('Address', [
1094 'street_address' => 'Appt 115, The Batcave',
1095 'contact_id' => ['IN' => array_keys($contacts['values'])],
1098 $this->callAPISuccessGetCount('Address', [
1099 'street_address' => 'Appt 115, The Batcave',
1100 'contact_id' => ['IN' => array_keys($deletedContacts['values'])],
1105 * Test the batch merge by id range.
1107 * We have 2 sets of 5 matches & set the merge only to merge the lower set.
1109 * @throws \CRM_Core_Exception
1110 * @throws \CiviCRM_API3_Exception
1112 public function testBatchMergeIDRange(): void
{
1113 for ($x = 0; $x <= 4; $x++
) {
1114 $id = $this->individualCreate(['email' => 'batman@gotham.met']);
1116 for ($x = 0; $x <= 4; $x++
) {
1117 $this->individualCreate(['email' => 'robin@gotham.met']);
1119 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['criteria' => ['contact' => ['id' => ['<' => $id]]]]);
1120 $this->assertCount(4, $result['values']['merged']);
1121 $this->callAPISuccessGetCount('Contact', ['email' => 'batman@gotham.met'], 1);
1122 $this->callAPISuccessGetCount('Contact', ['email' => 'robin@gotham.met'], 5);
1123 $contacts = $this->callAPISuccess('Contact', 'get', ['is_deleted' => 0]);
1124 $deletedContacts = $this->callAPISuccess('Contact', 'get', ['is_deleted' => 0]);
1125 $this->callAPISuccessGetCount('Email', [
1126 'email' => 'batman@gotham.met',
1127 'contact_id' => ['IN' => array_keys($contacts['values'])],
1129 $this->callAPISuccessGetCount('Email', [
1130 'email' => 'batman@gotham.met',
1131 'contact_id' => ['IN' => array_keys($deletedContacts['values'])],
1133 $this->callAPISuccessGetCount('Email', [
1134 'email' => 'robin@gotham.met',
1135 'contact_id' => ['IN' => array_keys($contacts['values'])],
1141 * Test the batch merge copes with view only custom data field.
1143 * @throws \CRM_Core_Exception
1144 * @throws \CiviCRM_API3_Exception
1146 public function testBatchMergeCustomDataViewOnlyField(): void
{
1147 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviCRM', 'edit my contact'];
1148 $mouseParams = ['first_name' => 'Mickey', 'last_name' => 'Mouse', 'email' => 'tha_mouse@mouse.com'];
1149 $this->individualCreate($mouseParams);
1151 $customGroup = $this->customGroupCreate();
1152 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id'], 'is_view' => 1]);
1153 $this->individualCreate(array_merge($mouseParams, ['custom_' . $customField['id'] => 'blah']));
1155 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0, 'mode' => 'safe']);
1156 $this->assertCount(1, $result['values']['merged']);
1157 $mouseParams['return'] = 'custom_' . $customField['id'];
1158 $mouse = $this->callAPISuccess('Contact', 'getsingle', $mouseParams);
1159 $this->assertEquals('blah', $mouse['custom_' . $customField['id']]);
1163 * Test the batch merge retains 0 as a valid custom field value.
1165 * Note that we set 0 on 2 fields with one on each contact to ensure that
1166 * both merged & mergee fields are respected.
1168 * @throws \CRM_Core_Exception
1169 * @throws \CiviCRM_API3_Exception
1171 public function testBatchMergeCustomDataZeroValueField(): void
{
1172 $customGroup = $this->customGroupCreate();
1173 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id'], 'default_value' => NULL]);
1175 $mouseParams = ['first_name' => 'Mickey', 'last_name' => 'Mouse', 'email' => 'tha_mouse@mouse.com'];
1176 $this->individualCreate(array_merge($mouseParams, ['custom_' . $customField['id'] => '']));
1177 $this->individualCreate(array_merge($mouseParams, ['custom_' . $customField['id'] => 0]));
1179 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0, 'mode' => 'safe']);
1180 $this->assertCount(1, $result['values']['merged']);
1181 $mouseParams['return'] = 'custom_' . $customField['id'];
1182 $mouse = $this->callAPISuccess('Contact', 'getsingle', $mouseParams);
1183 $this->assertEquals(0, $mouse['custom_' . $customField['id']]);
1185 $this->individualCreate(array_merge($mouseParams, ['custom_' . $customField['id'] => NULL]));
1186 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0, 'mode' => 'safe']);
1187 $this->assertCount(1, $result['values']['merged']);
1188 $mouseParams['return'] = 'custom_' . $customField['id'];
1189 $mouse = $this->callAPISuccess('Contact', 'getsingle', $mouseParams);
1190 $this->assertEquals(0, $mouse['custom_' . $customField['id']]);
1194 * Test the batch merge treats 0 vs 1 as a conflict.
1196 * @throws \CRM_Core_Exception
1197 * @throws \CiviCRM_API3_Exception
1199 public function testBatchMergeCustomDataZeroValueFieldWithConflict(): void
{
1200 $customGroup = $this->customGroupCreate();
1201 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id'], 'default_value' => NULL]);
1203 $mouseParams = ['first_name' => 'Mickey', 'last_name' => 'Mouse', 'email' => 'tha_mouse@mouse.com'];
1204 $mouse1 = $this->individualCreate(array_merge($mouseParams, ['custom_' . $customField['id'] => 0]));
1205 $mouse2 = $this->individualCreate(array_merge($mouseParams, ['custom_' . $customField['id'] => 1]));
1207 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0, 'mode' => 'safe']);
1208 $this->assertCount(0, $result['values']['merged']);
1210 // Reverse which mouse has the zero to test we still get a conflict.
1211 $this->individualCreate(array_merge($mouseParams, ['id' => $mouse1, 'custom_' . $customField['id'] => 1]));
1212 $this->individualCreate(array_merge($mouseParams, ['id' => $mouse2, 'custom_' . $customField['id'] => 0]));
1213 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0, 'mode' => 'safe']);
1214 $this->assertCount(0, $result['values']['merged']);
1218 * Test the batch merge function actually works!
1220 * @dataProvider getMergeSets
1222 * @param array $dataSet
1224 * @throws \CRM_Core_Exception
1226 public function testBatchMergeWorksCheckPermissionsTrue(array $dataSet): void
{
1227 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviCRM', 'administer CiviCRM', 'merge duplicate contacts', 'force merge duplicate contacts'];
1228 foreach ($dataSet['contacts'] as $params) {
1229 $this->callAPISuccess('Contact', 'create', $params);
1232 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 1, 'mode' => $dataSet['mode']]);
1233 $this->assertCount(0, $result['values']['merged'], 'User does not have permission to any contacts, so no merging');
1234 $this->assertCount(0, $result['values']['skipped'], 'User does not have permission to any contacts, so no skip visibility');
1238 * Test the batch merge function actually works!
1240 * @dataProvider getMergeSets
1242 * @param array $dataSet
1244 * @throws \CRM_Core_Exception
1246 public function testBatchMergeWorksCheckPermissionsFalse(array $dataSet): void
{
1247 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviCRM', 'edit my contact'];
1248 foreach ($dataSet['contacts'] as $params) {
1249 $this->callAPISuccess('Contact', 'create', $params);
1252 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0, 'mode' => $dataSet['mode']]);
1253 $this->assertCount($dataSet['skipped'], $result['values']['skipped'], 'Failed to skip the right number:' . $dataSet['skipped']);
1254 $this->assertCount($dataSet['merged'], $result['values']['merged']);
1258 * Get data for batch merge.
1260 public function getMergeSets(): array {
1267 'first_name' => 'Michael',
1268 'last_name' => 'Jackson',
1269 'email' => 'michael@neverland.com',
1270 'contact_type' => 'Individual',
1271 'contact_sub_type' => 'Student',
1272 'api.Address.create' => [
1273 'street_address' => 'big house',
1274 'location_type_id' => 'Home',
1278 'first_name' => 'Michael',
1279 'last_name' => 'Jackson',
1280 'email' => 'michael@neverland.com',
1281 'contact_type' => 'Individual',
1282 'contact_sub_type' => 'Student',
1289 'first_name' => 'Michael',
1290 'last_name' => 'Jackson',
1291 'email' => 'michael@neverland.com',
1292 'contact_type' => 'Individual',
1302 'first_name' => 'Michael',
1303 'last_name' => 'Jackson',
1304 'email' => 'michael@neverland.com',
1305 'contact_type' => 'Individual',
1306 'contact_sub_type' => 'Student',
1307 'api.Address.create' => [
1308 'street_address' => 'big house',
1309 'location_type_id' => 'Home',
1313 'first_name' => 'Michael',
1314 'last_name' => 'Jackson',
1315 'email' => 'michael@neverland.com',
1316 'contact_type' => 'Individual',
1317 'contact_sub_type' => 'Student',
1318 'api.Address.create' => [
1319 'street_address' => 'bigger house',
1320 'location_type_id' => 'Home',
1328 'first_name' => 'Michael',
1329 'last_name' => 'Jackson',
1330 'email' => 'michael@neverland.com',
1331 'contact_type' => 'Individual',
1332 'street_address' => 'big house',
1335 'first_name' => 'Michael',
1336 'last_name' => 'Jackson',
1337 'email' => 'michael@neverland.com',
1338 'contact_type' => 'Individual',
1339 'street_address' => 'bigger house',
1349 'first_name' => 'Michael',
1350 'last_name' => 'Jackson',
1351 'email' => 'michael@neverland.com',
1352 'contact_type' => 'Individual',
1353 'contact_sub_type' => 'Student',
1354 'api.Email.create' => [
1355 'email' => 'big.slog@work.co.nz',
1356 'location_type_id' => 'Work',
1360 'first_name' => 'Michael',
1361 'last_name' => 'Jackson',
1362 'email' => 'michael@neverland.com',
1363 'contact_type' => 'Individual',
1364 'contact_sub_type' => 'Student',
1365 'api.Email.create' => [
1366 'email' => 'big.slog@work.com',
1367 'location_type_id' => 'Work',
1375 'first_name' => 'Michael',
1376 'last_name' => 'Jackson',
1377 'email' => 'michael@neverland.com',
1378 'contact_type' => 'Individual',
1381 'first_name' => 'Michael',
1382 'last_name' => 'Jackson',
1383 'email' => 'michael@neverland.com',
1384 'contact_type' => 'Individual',
1394 'first_name' => 'Michael',
1395 'last_name' => 'Jackson',
1396 'email' => 'michael@neverland.com',
1397 'contact_type' => 'Individual',
1398 'contact_sub_type' => 'Student',
1399 'api.Phone.create' => [
1400 'phone' => '123456',
1401 'location_type_id' => 'Work',
1405 'first_name' => 'Michael',
1406 'last_name' => 'Jackson',
1407 'email' => 'michael@neverland.com',
1408 'contact_type' => 'Individual',
1409 'contact_sub_type' => 'Student',
1410 'api.Phone.create' => [
1412 'location_type_id' => 'Work',
1420 'first_name' => 'Michael',
1421 'last_name' => 'Jackson',
1422 'email' => 'michael@neverland.com',
1423 'contact_type' => 'Individual',
1426 'first_name' => 'Michael',
1427 'last_name' => 'Jackson',
1428 'email' => 'michael@neverland.com',
1429 'contact_type' => 'Individual',
1436 'mode' => 'aggressive',
1439 'first_name' => 'Michael',
1440 'last_name' => 'Jackson',
1441 'email' => 'michael@neverland.com',
1442 'contact_type' => 'Individual',
1443 'contact_sub_type' => 'Student',
1444 'api.Address.create' => [
1445 'street_address' => 'big house',
1446 'location_type_id' => 'Home',
1450 'first_name' => 'Michael',
1451 'last_name' => 'Jackson',
1452 'email' => 'michael@neverland.com',
1453 'contact_type' => 'Individual',
1454 'contact_sub_type' => 'Student',
1455 'api.Address.create' => [
1456 'street_address' => 'bigger house',
1457 'location_type_id' => 'Home',
1465 'first_name' => 'Michael',
1466 'last_name' => 'Jackson',
1467 'email' => 'michael@neverland.com',
1468 'contact_type' => 'Individual',
1469 'street_address' => 'big house',
1479 'first_name' => 'Michael',
1480 'last_name' => 'Jackson',
1481 'email' => 'michael@neverland.com',
1482 'contact_type' => 'Individual',
1483 'contact_sub_type' => 'Student',
1484 'api.Address.create' => [
1485 'street_address' => 'big house',
1486 'location_type_id' => 'Home',
1490 'first_name' => 'Michael',
1491 'last_name' => 'Jackson',
1492 'email' => 'michael@neverland.com',
1493 'contact_type' => 'Individual',
1494 'contact_sub_type' => 'Student',
1502 'first_name' => 'Michael',
1503 'last_name' => 'Jackson',
1504 'email' => 'michael@neverland.com',
1505 'contact_type' => 'Individual',
1509 'first_name' => 'Michael',
1510 'last_name' => 'Jackson',
1511 'email' => 'michael@neverland.com',
1512 'contact_type' => 'Individual',
1523 'first_name' => 'Michael',
1524 'last_name' => 'Jackson',
1525 'email' => 'michael@neverland.com',
1526 'contact_type' => 'Individual',
1527 'contact_sub_type' => 'Student',
1528 'api.Address.create' => [
1529 'street_address' => 'big house',
1530 'location_type_id' => 'Home',
1535 'first_name' => 'Michael',
1536 'last_name' => 'Jackson',
1537 'email' => 'michael@neverland.com',
1538 'contact_type' => 'Individual',
1539 'contact_sub_type' => 'Student',
1546 'first_name' => 'Michael',
1547 'last_name' => 'Jackson',
1548 'email' => 'michael@neverland.com',
1549 'contact_type' => 'Individual',
1553 'first_name' => 'Michael',
1554 'last_name' => 'Jackson',
1555 'email' => 'michael@neverland.com',
1556 'contact_type' => 'Individual',
1565 'first_name' => 'Dianna',
1566 'last_name' => 'McAndrew',
1567 'middle_name' => 'Prancer',
1568 'birth_date' => '2015-12-25',
1569 'gender_id' => 'Female',
1570 'job_title' => 'Thriller',
1573 foreach ($conflictPairs as $key => $value) {
1575 'first_name' => 'Michael',
1576 'middle_name' => 'Dancer',
1577 'last_name' => 'Jackson',
1578 'birth_date' => '2015-02-25',
1579 'email' => 'michael@neverland.com',
1580 'contact_type' => 'Individual',
1581 'contact_sub_type' => ['Student'],
1582 'gender_id' => 'Male',
1583 'job_title' => 'Entertainer',
1585 $contact2 = $contactParams;
1587 $contact2[$key] = $value;
1588 $data[$key . '_conflict'] = [
1591 'contacts' => [$contactParams, $contact2],
1594 'expected' => [$contactParams, $contact2],
1603 * Implements pre hook on relationships.
1606 * @param string $objectName
1608 * @param array $params
1610 * @noinspection PhpUnusedParameterInspection
1612 public function hookPreRelationship(string $op, string $objectName, $id, array &$params): void
{
1613 if ($op === 'delete') {
1616 if ($params['is_active']) {
1617 $params['description'] = 'Hooked';
1620 $params['description'] = 'Go Go you good thing';
1625 * Get the location data set.
1627 * @param array $locationParams1
1628 * @param array $locationParams2
1629 * @param string $entity
1630 * @param array $additionalExpected
1634 public function getMergeLocations(array $locationParams1, array $locationParams2, string $entity, $additionalExpected = []): array {
1637 'matching_primary' => [
1638 'entity' => $entity,
1641 'location_type_id' => 'Main',
1643 ], $locationParams1),
1645 'location_type_id' => 'Work',
1647 ], $locationParams2),
1651 'location_type_id' => 'Main',
1653 ], $locationParams1),
1655 'expected' => array_merge($additionalExpected, [
1657 'location_type_id' => 'Main',
1659 ], $locationParams1),
1661 'location_type_id' => 'Work',
1663 ], $locationParams2),
1665 'expected_hook' => array_merge($additionalExpected, [
1667 'location_type_id' => 'Main',
1669 ], $locationParams1),
1671 'location_type_id' => 'Work',
1673 ], $locationParams2),
1678 'matching_primary_reverse' => [
1679 'entity' => $entity,
1682 'location_type_id' => 'Main',
1684 ], $locationParams1),
1688 'location_type_id' => 'Main',
1690 ], $locationParams1),
1692 'location_type_id' => 'Work',
1694 ], $locationParams2),
1696 'expected' => array_merge($additionalExpected, [
1698 'location_type_id' => 'Main',
1700 ], $locationParams1),
1702 'location_type_id' => 'Work',
1704 ], $locationParams2),
1706 'expected_hook' => array_merge($additionalExpected, [
1708 'location_type_id' => 'Main',
1710 ], $locationParams1),
1712 'location_type_id' => 'Work',
1714 ], $locationParams2),
1719 'only_one_has_address' => [
1720 'entity' => $entity,
1723 'location_type_id' => 'Main',
1725 ], $locationParams1),
1727 'location_type_id' => 'Work',
1729 ], $locationParams2),
1732 'expected' => array_merge($additionalExpected, [
1734 'location_type_id' => 'Main',
1736 ], $locationParams1),
1738 'location_type_id' => 'Work',
1740 ], $locationParams2),
1742 'expected_hook' => array_merge($additionalExpected, [
1744 'location_type_id' => 'Main',
1745 // When dealing with email we don't have a clean slate - the existing
1746 // primary will be primary.
1747 'is_primary' => ($entity === 'Email' ?
0 : 1),
1748 ], $locationParams1),
1750 'location_type_id' => 'Work',
1752 ], $locationParams2),
1757 'only_one_has_address_reverse' => [
1758 'description' => 'The destination contact does not have an address. secondary contact should be merged in.',
1759 'entity' => $entity,
1763 'location_type_id' => 'Main',
1765 ], $locationParams1),
1767 'location_type_id' => 'Work',
1769 ], $locationParams2),
1771 'expected' => array_merge($additionalExpected, [
1773 'location_type_id' => 'Main',
1774 // When dealing with email we don't have a clean slate - the existing
1775 // primary will be primary.
1776 'is_primary' => ($entity === 'Email' ?
0 : 1),
1777 ], $locationParams1),
1779 'location_type_id' => 'Work',
1781 ], $locationParams2),
1783 'expected_hook' => array_merge($additionalExpected, [
1785 'location_type_id' => 'Main',
1787 ], $locationParams1),
1789 'location_type_id' => 'Work',
1791 ], $locationParams2),
1796 'different_primaries_with_different_location_type' => [
1797 'description' => 'Primaries are different with different location. Keep both addresses. Set primary to be that of lower id',
1798 'entity' => $entity,
1801 'location_type_id' => 'Main',
1803 ], $locationParams1),
1807 'location_type_id' => 'Work',
1809 ], $locationParams2),
1811 'expected' => array_merge($additionalExpected, [
1813 'location_type_id' => 'Main',
1815 ], $locationParams1),
1817 'location_type_id' => 'Work',
1819 ], $locationParams2),
1821 'expected_hook' => array_merge($additionalExpected, [
1823 'location_type_id' => 'Main',
1825 ], $locationParams1),
1827 'location_type_id' => 'Work',
1829 ], $locationParams2),
1834 'different_primaries_with_different_location_type_reverse' => [
1835 'entity' => $entity,
1838 'location_type_id' => 'Work',
1840 ], $locationParams2),
1844 'location_type_id' => 'Main',
1846 ], $locationParams1),
1848 'expected' => array_merge($additionalExpected, [
1850 'location_type_id' => 'Work',
1852 ], $locationParams2),
1854 'location_type_id' => 'Main',
1856 ], $locationParams1),
1858 'expected_hook' => array_merge($additionalExpected, [
1860 'location_type_id' => 'Work',
1862 ], $locationParams2),
1864 'location_type_id' => 'Main',
1866 ], $locationParams1),
1871 'different_primaries_location_match_only_one_address' => [
1872 'entity' => $entity,
1875 'location_type_id' => 'Main',
1877 ], $locationParams1),
1879 'location_type_id' => 'Work',
1881 ], $locationParams2),
1885 'location_type_id' => 'Work',
1887 ], $locationParams2),
1890 'expected' => array_merge($additionalExpected, [
1892 'location_type_id' => 'Main',
1894 ], $locationParams1),
1896 'location_type_id' => 'Work',
1898 ], $locationParams2),
1900 'expected_hook' => array_merge($additionalExpected, [
1902 'location_type_id' => 'Main',
1904 ], $locationParams1),
1906 'location_type_id' => 'Work',
1908 ], $locationParams2),
1913 'different_primaries_location_match_only_one_address_reverse' => [
1914 'entity' => $entity,
1917 'location_type_id' => 'Work',
1919 ], $locationParams2),
1923 'location_type_id' => 'Main',
1925 ], $locationParams1),
1927 'location_type_id' => 'Work',
1929 ], $locationParams2),
1931 'expected' => array_merge($additionalExpected, [
1933 'location_type_id' => 'Work',
1935 ], $locationParams2),
1937 'location_type_id' => 'Main',
1939 ], $locationParams1),
1941 'expected_hook' => array_merge($additionalExpected, [
1943 'location_type_id' => 'Work',
1945 ], $locationParams2),
1947 'location_type_id' => 'Main',
1949 ], $locationParams1),
1954 'same_primaries_different_location' => [
1955 'entity' => $entity,
1958 'location_type_id' => 'Main',
1960 ], $locationParams1),
1964 'location_type_id' => 'Work',
1966 ], $locationParams1),
1969 'expected' => array_merge($additionalExpected, [
1971 'location_type_id' => 'Main',
1973 ], $locationParams1),
1975 'location_type_id' => 'Work',
1977 ], $locationParams1),
1979 'expected_hook' => array_merge($additionalExpected, [
1981 'location_type_id' => 'Work',
1983 ], $locationParams1),
1988 'same_primaries_different_location_reverse' => [
1989 'entity' => $entity,
1992 'location_type_id' => 'Work',
1994 ], $locationParams1),
1998 'location_type_id' => 'Main',
2000 ], $locationParams1),
2002 'expected' => array_merge($additionalExpected, [
2004 'location_type_id' => 'Work',
2006 ], $locationParams1),
2008 'location_type_id' => 'Main',
2010 ], $locationParams1),
2012 'expected_hook' => array_merge($additionalExpected, [
2014 'location_type_id' => 'Main',
2016 ], $locationParams1),
2024 * Test processing membership for deceased contacts.
2026 * @throws \CRM_Core_Exception
2027 * @throws \CiviCRM_API3_Exception
2029 public function testProcessMembershipDeceased(): void
{
2030 $this->callAPISuccess('Job', 'process_membership', []);
2031 $deadManWalkingID = $this->individualCreate();
2032 $membershipID = $this->contactMembershipCreate(['contact_id' => $deadManWalkingID]);
2033 $this->callAPISuccess('Contact', 'create', ['id' => $deadManWalkingID, 'is_deceased' => 1]);
2034 $this->callAPISuccess('Job', 'process_membership', []);
2035 $membership = $this->callAPISuccessGetSingle('Membership', ['id' => $membershipID]);
2036 $deceasedStatusId = CRM_Core_PseudoConstant
::getKey('CRM_Member_BAO_Membership', 'status_id', 'Deceased');
2037 $this->assertEquals($deceasedStatusId, $membership['status_id']);
2041 * Test we get an error is deceased status is disabled.
2043 * @throws \CRM_Core_Exception
2044 * @throws \CiviCRM_API3_Exception
2046 public function testProcessMembershipNoDeceasedStatus(): void
{
2047 $deceasedStatusId = CRM_Core_PseudoConstant
::getKey('CRM_Member_BAO_Membership', 'status_id', 'Deceased');
2048 $this->callAPISuccess('MembershipStatus', 'create', ['is_active' => 0, 'id' => $deceasedStatusId]);
2049 CRM_Core_PseudoConstant
::flush();
2051 $deadManWalkingID = $this->individualCreate();
2052 $this->contactMembershipCreate(['contact_id' => $deadManWalkingID]);
2053 $this->callAPISuccess('Contact', 'create', ['id' => $deadManWalkingID, 'is_deceased' => 1]);
2054 $this->callAPIFailure('Job', 'process_membership', []);
2056 $this->callAPISuccess('MembershipStatus', 'create', ['is_active' => 1, 'id' => $deceasedStatusId]);
2060 * Test processing membership: check that status is updated when it should be
2061 * and left alone when it shouldn't.
2063 * @throws \CRM_Core_Exception
2064 * @throws \CiviCRM_API3_Exception
2066 public function testProcessMembershipUpdateStatus(): void
{
2067 $this->ids
['MembershipType'] = $this->membershipTypeCreate();
2069 // Create admin-only membership status and get all statuses.
2070 $this->callAPISuccess('membership_status', 'create', ['name' => 'Admin', 'is_admin' => 1])['id'];
2072 // Create membership with incorrect statuses for the given dates and also some (pending, cancelled, admin override) which should not be updated.
2075 'start_date' => 'now',
2076 'end_date' => '+ 1 year',
2077 'initial_status' => 'Current',
2078 'expected_processed_status' => 'New',
2081 'start_date' => '- 6 month',
2082 'end_date' => '+ 6 month',
2083 'initial_status' => 'New',
2084 'expected_processed_status' => 'Current',
2087 'start_date' => '- 53 week',
2088 'end_date' => '-1 week',
2089 'initial_status' => 'Current',
2090 'expected_processed_status' => 'Grace',
2093 'start_date' => '- 16 month',
2094 'end_date' => '- 4 month',
2095 'initial_status' => 'Grace',
2096 'expected_processed_status' => 'Expired',
2099 'start_date' => 'now',
2100 'end_date' => '+ 1 year',
2101 'initial_status' => 'Pending',
2102 'expected_processed_status' => 'Pending',
2105 'start_date' => '- 6 month',
2106 'end_date' => '+ 6 month',
2107 'initial_status' => 'Cancelled',
2108 'expected_processed_status' => 'Cancelled',
2111 'start_date' => '- 16 month',
2112 'end_date' => '- 4 month',
2113 'initial_status' => 'Current',
2114 'is_override' => TRUE,
2115 'expected_processed_status' => 'Current',
2118 // @todo this looks like it's covering something up. If we pass isAdminOverride it is the same as the line above. Without it the test fails.
2119 // this smells of something that should work (or someone thought should work & so put in a test) doesn't & test has been adjusted to cope.
2120 'start_date' => '- 16 month',
2121 'end_date' => '- 4 month',
2122 'initial_status' => 'Admin',
2123 'is_override' => TRUE,
2124 'expected_processed_status' => 'Admin',
2127 foreach ($memberships as $index => $membership) {
2128 $memberships[$index]['id'] = $this->createMembershipNeedingStatusProcessing($membership['start_date'], $membership['end_date'], $membership['initial_status'], $membership['is_override'] ??
FALSE);
2132 * Create membership type with inheritance and check processing of secondary memberships.
2134 $employerRelationshipId = $this->callAPISuccessGetValue('RelationshipType', [
2136 'name_b_a' => 'Employer Of',
2138 // Create membership type: inherited through employment.
2139 $membershipOrgId = $this->organizationCreate();
2141 'name' => 'Corporate Membership',
2142 'duration_unit' => 'year',
2143 'duration_interval' => 1,
2144 'period_type' => 'rolling',
2145 'member_of_contact_id' => $membershipOrgId,
2147 'financial_type_id' => 1,
2148 'relationship_type_id' => $employerRelationshipId,
2149 'relationship_direction' => 'b_a',
2152 $membershipTypeId = $this->callAPISuccess('membership_type', 'create', $params)['id'];
2154 // Create employer and first employee
2155 $employerId = $this->organizationCreate([], 1);
2156 $memberContactId = $this->individualCreate(['employer_id' => $employerId]);
2158 // Create inherited membership with incorrect status but dates implying status Expired.
2160 'contact_id' => $employerId,
2161 'membership_type_id' => $membershipTypeId,
2162 'source' => 'Test suite',
2163 'join_date' => date('Y-m-d', strtotime('now - 16 month')),
2164 'start_date' => date('Y-m-d', strtotime('now - 16 month')),
2165 'end_date' => date('Y-m-d', strtotime('now - 4 month')),
2166 // Intentionally incorrect status.
2167 'status_id' => 'Grace',
2168 // Don't calculate status.
2169 'skipStatusCal' => 1,
2171 $organizationMembershipID = $this->contactMembershipCreate($params);
2173 // Check that the employee inherited the membership and status.
2174 $expiredInheritedRelationship = $this->callAPISuccessGetSingle('membership', [
2175 'contact_id' => $memberContactId,
2176 'membership_type_id' => $membershipTypeId,
2178 $this->assertEquals($organizationMembershipID, $expiredInheritedRelationship['owner_membership_id']);
2179 $this->assertMembershipStatus('Grace', (int) $expiredInheritedRelationship['status_id']);
2181 // Check that after running process_membership job, statuses are correct.
2182 $this->callAPISuccess('Job', 'process_membership', []);
2184 foreach ($memberships as $expectation) {
2185 $membership = $this->callAPISuccessGetSingle('membership', ['id' => $expectation['id']]);
2186 $this->assertMembershipStatus($expectation['expected_processed_status'], (int) $membership['status_id']);
2189 // Inherit Expired - should get updated.
2190 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $expiredInheritedRelationship['id']]);
2191 $this->assertMembershipStatus('Expired', $membership['status_id']);
2195 * Test processing membership where is_override is set to 0 rather than NULL
2197 * @throws \CRM_Core_Exception|\CiviCRM_API3_Exception
2199 public function testProcessMembershipIsOverrideNotNullNot1either(): void
{
2200 $membershipTypeId = $this->membershipTypeCreate();
2202 // Create admin-only membership status and get all statuses.
2203 $this->callAPISuccess('membership_status', 'create', ['name' => 'Admin', 'is_admin' => 1, 'sequential' => 1]);
2204 $memStatus = CRM_Member_PseudoConstant
::membershipStatus();
2206 // Default params, which we'll expand on below.
2208 'membership_type_id' => $membershipTypeId,
2209 // Don't calculate status.
2210 'skipStatusCal' => 1,
2215 // Create membership with incorrect status but dates implying status Current.
2216 $params['contact_id'] = $this->individualCreate();
2217 $params['join_date'] = date('Y-m-d', strtotime('now - 6 month'));
2218 $params['start_date'] = date('Y-m-d', strtotime('now - 6 month'));
2219 $params['end_date'] = date('Y-m-d', strtotime('now + 6 month'));
2220 // Intentionally incorrect status.
2221 $params['status_id'] = 'New';
2222 $resultCurrent = $this->callAPISuccess('Membership', 'create', $params);
2223 // Ensure that is_override is set to 0 by doing through DB given API not seem to accept id
2224 CRM_Core_DAO
::executeQuery("Update civicrm_membership SET is_override = 0 WHERE id = %1", [1 => [$resultCurrent['id'], 'Positive']]);
2225 $this->assertEquals(array_search('New', $memStatus, TRUE), $resultCurrent['values'][0]['status_id']);
2226 $jobResult = $this->callAPISuccess('Job', 'process_membership', []);
2227 $this->assertEquals('Processed 1 membership records. Updated 1 records.', $jobResult['values']);
2228 $this->assertEquals(array_search('Current', $memStatus, TRUE), $this->callAPISuccess('Membership', 'get', ['id' => $resultCurrent['id']])['values'][$resultCurrent['id']]['status_id']);
2232 * @param string $expectedStatusName
2233 * @param int $actualStatusID
2235 protected function assertMembershipStatus(string $expectedStatusName, int $actualStatusID): void
{
2236 $this->assertEquals($expectedStatusName, CRM_Core_PseudoConstant
::getName('CRM_Member_BAO_Membership', 'status_id', $actualStatusID));
2240 * @param string $startDate
2241 * Date in strtotime format - e.g 'now', '+1 day'
2242 * @param string $endDate
2243 * Date in strtotime format - e.g 'now', '+1 day'
2244 * @param string $status
2246 * @param bool $isAdminOverride
2247 * Is administratively overridden (if so the status is fixed).
2251 * @throws \CRM_Core_Exception
2252 * @throws \CiviCRM_API3_Exception
2254 protected function createMembershipNeedingStatusProcessing(string $startDate, string $endDate, string $status, bool $isAdminOverride = FALSE): int {
2256 'membership_type_id' => $this->ids
['MembershipType'],
2257 // Don't calculate status.
2258 'skipStatusCal' => 1,
2262 $params['contact_id'] = $this->individualCreate();
2263 $params['join_date'] = date('Y-m-d', strtotime($startDate));
2264 $params['start_date'] = date('Y-m-d', strtotime($startDate));
2265 $params['end_date'] = date('Y-m-d', strtotime($endDate));
2266 $params['sequential'] = TRUE;
2267 $params['is_override'] = $isAdminOverride;
2268 // Intentionally incorrect status.
2269 $params['status_id'] = $status;
2270 $resultNew = $this->callAPISuccess('Membership', 'create', $params);
2271 $this->assertMembershipStatus($status, (int) $resultNew['values'][0]['status_id']);
2272 return (int) $resultNew['id'];
2276 * Shared set up for SMS reminder tests.
2280 * @throws \CRM_Core_Exception
2281 * @throws \CiviCRM_API3_Exception
2283 protected function setUpMembershipSMSReminders(): array {
2284 $membershipTypeID = $this->membershipTypeCreate();
2285 $this->membershipStatusCreate();
2287 $groupID = $this->groupCreate(['name' => 'Texan drawlers', 'title' => 'a...']);
2288 for ($i = 1; $i <= $createTotal; $i++
) {
2289 $contactID = $this->individualCreate();
2290 $this->callAPISuccess('Phone', 'create', [
2291 'contact_id' => $contactID,
2292 'phone' => '555 123 1234',
2293 'phone_type_id' => 'Mobile',
2294 'location_type_id' => 'Billing',
2297 $theChosenOneID = $contactID;
2300 $this->callAPISuccess('group_contact', 'create', [
2301 'contact_id' => $contactID,
2302 'status' => 'Added',
2303 'group_id' => $groupID,
2307 $this->callAPISuccess('membership', 'create', [
2308 'contact_id' => $contactID,
2309 'membership_type_id' => $membershipTypeID,
2310 'join_date' => 'now',
2311 'start_date' => '+ 1 day',
2315 $this->setupForSmsTests();
2316 $provider = civicrm_api3('SmsProvider', 'create', [
2317 'name' => 'CiviTestSMSProvider',
2322 'api_params' => 'a=1',
2323 'is_default' => '1',
2327 return [$membershipTypeID, $groupID, $theChosenOneID, $provider];
2331 * Test that the mail_report job sends an email for 'print' format.
2333 * We're not testing that the report itself is correct since in 'print'
2334 * format it's a little difficult to parse out, so we're just testing that
2335 * the email was sent and it more or less looks like an email we'd expect.
2337 * @throws \CRM_Core_Exception
2338 * @throws \CRM_Core_Exception
2340 public function testMailReportForPrint(): void
{
2341 $mut = new CiviMailUtils($this, TRUE);
2344 if (empty($_SERVER['QUERY_STRING'])) {
2345 $_SERVER['QUERY_STRING'] = 'reset=1';
2348 $this->callAPISuccess('job', 'mail_report', [
2349 'instanceId' => $this->report_instance
['id'],
2350 'format' => 'print',
2354 $message = $mut->getMostRecentEmail('ezc');
2356 $this->assertEquals('This is the email subject', $message->subject
);
2357 $this->assertEquals('reportperson@example.com', $message->to
[0]->email
);
2359 $parts = $message->fetchParts(NULL, TRUE);
2360 $this->assertCount(2, $parts);
2361 $this->assertStringContainsString('test report', $parts[1]->text
);
2363 $mut->clearMessages();
2368 * Test that the mail_report job sends an email for 'pdf' format.
2370 * We're not testing that the report itself is correct since in 'pdf'
2371 * format it's a little difficult to parse out, so we're just testing that
2372 * the email was sent and it more or less looks like an email we'd expect.
2374 * @throws \CRM_Core_Exception
2376 public function testMailReportForPdf(): void
{
2377 $mut = new CiviMailUtils($this, TRUE);
2380 if (empty($_SERVER['QUERY_STRING'])) {
2381 $_SERVER['QUERY_STRING'] = 'reset=1';
2385 $this->callAPISuccess('job', 'mail_report', [
2386 'instanceId' => $this->report_instance
['id'],
2391 $message = $mut->getMostRecentEmail('ezc');
2393 $this->assertEquals('This is the email subject', $message->subject
);
2394 $this->assertEquals('reportperson@example.com', $message->to
[0]->email
);
2396 $parts = $message->fetchParts(NULL, TRUE);
2397 $this->assertCount(3, $parts);
2398 $this->assertStringContainsString('<title>CiviCRM Report</title>', $parts[1]->text
);
2399 $this->assertEquals(ezcMailFilePart
::CONTENT_TYPE_APPLICATION
, $parts[2]->contentType
);
2400 $this->assertEquals('pdf', $parts[2]->mimeType
);
2401 $this->assertEquals(ezcMailFilePart
::DISPLAY_ATTACHMENT
, $parts[2]->dispositionType
);
2402 $this->assertGreaterThan(0, filesize($parts[2]->fileName
));
2404 $mut->clearMessages();
2409 * Test that the mail_report job sends an email for 'csv' format.
2411 * As with the print and pdf we're not super-concerned about report
2412 * functionality itself - we're more concerned with the mailing part,
2413 * but since it's csv we can easily check the output.
2415 * @throws \CRM_Core_Exception
2417 public function testMailReportForCsv(): void
{
2418 // Create many contacts, in particular so that the report would be more
2419 // than a one-pager.
2420 for ($i = 0; $i < 110; $i++
) {
2421 $this->individualCreate([], $i, TRUE);
2424 $mut = new CiviMailUtils($this, TRUE);
2427 if (empty($_SERVER['QUERY_STRING'])) {
2428 $_SERVER['QUERY_STRING'] = 'reset=1';
2431 $this->callAPISuccess('job', 'mail_report', [
2432 'instanceId' => $this->report_instance
['id'],
2437 $message = $mut->getMostRecentEmail('ezc');
2439 $this->assertEquals('This is the email subject', $message->subject
);
2440 $this->assertEquals('reportperson@example.com', $message->to
[0]->email
);
2442 $parts = $message->fetchParts(NULL, TRUE);
2443 $this->assertCount(3, $parts);
2444 $this->assertStringContainsString('<title>CiviCRM Report</title>', $parts[1]->text
);
2445 $this->assertEquals('csv', $parts[2]->subType
);
2447 // Pull all the contacts to get our expected output.
2448 $contacts = $this->callAPISuccess('Contact', 'get', [
2449 'return' => 'sort_name',
2452 'sort' => 'sort_name',
2456 foreach ($contacts['values'] as $contact) {
2457 $rows[] = ['civicrm_contact_sort_name' => $contact['sort_name']];
2459 // need this for makeCsv()
2460 $fakeForm = new CRM_Report_Form();
2461 $fakeForm->_columnHeaders
= [
2462 'civicrm_contact_sort_name' => [
2463 'title' => 'Contact Name',
2468 $this->assertEquals(
2469 CRM_Report_Utils_Report
::makeCsv($fakeForm, $rows),
2473 $mut->clearMessages();
2478 * Helper to create a report instance of the contact summary report.
2480 private function createReportInstance() {
2481 return $this->callAPISuccess('ReportInstance', 'create', [
2482 'report_id' => 'contact/summary',
2483 'title' => 'test report',
2488 'street_address' => '1',
2490 'country_id' => '1',
2492 'sort_name_op' => 'has',
2493 'sort_name_value' => '',
2494 'source_op' => 'has',
2495 'source_value' => '',
2500 'country_id_op' => 'in',
2501 'country_id_value' => [],
2502 'state_province_id_op' => 'in',
2503 'state_province_id_value' => [],
2507 'tagid_value' => [],
2508 'description' => 'Provides a list of address and telephone information for constituent records in your system.',
2509 'email_subject' => 'This is the email subject',
2510 'email_to' => 'reportperson@example.com',
2512 'permission' => 'view all contacts',
2517 // Email params need to be repeated outside form_values for some reason
2518 'email_subject' => 'This is the email subject',
2519 'email_to' => 'reportperson@example.com',