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
22 * Class api_v3_JobTest
26 class api_v3_JobTest
extends CiviUnitTestCase
{
28 public $DBResetRequired = FALSE;
30 public $_entity = 'Job';
35 * Created membership type.
37 * Must be created outside the transaction due to it breaking the transaction.
41 public $membershipTypeID;
46 public function setUp() {
48 $this->membershipTypeID
= $this->membershipTypeCreate(['name' => 'General']);
49 $this->useTransaction(TRUE);
52 'name' => 'API_Test_Job',
53 'description' => 'A long description written by hand in cursive',
54 'run_frequency' => 'Daily',
55 'api_entity' => 'ApiTestEntity',
56 'api_action' => 'apitestaction',
57 'parameters' => 'Semi-formal explanation of runtime job parameters',
62 public function tearDown() {
64 // The membershipType create breaks transactions so this extra cleanup is needed.
65 $this->membershipTypeDelete(['id' => $this->membershipTypeID
]);
66 $this->cleanUpSetUpIDs();
67 $this->quickCleanup(['civicrm_contact', 'civicrm_address', 'civicrm_email', 'civicrm_website', 'civicrm_phone'], TRUE);
74 public function testCreateWithoutName() {
78 $this->callAPIFailure('job', 'create', $params,
79 'Mandatory key(s) missing from params array: run_frequency, name, api_entity, api_action'
84 * Create job with an invalid "run_frequency" value.
86 public function testCreateWithInvalidFrequency() {
89 'name' => 'API_Test_Job',
90 'description' => 'A long description written by hand in cursive',
91 'run_frequency' => 'Fortnightly',
92 'api_entity' => 'ApiTestEntity',
93 'api_action' => 'apitestaction',
94 'parameters' => 'Semi-formal explanation of runtime job parameters',
97 $this->callAPIFailure('job', 'create', $params);
103 public function testCreate() {
104 $result = $this->callAPIAndDocument('job', 'create', $this->_params
, __FUNCTION__
, __FILE__
);
105 $this->assertNotNull($result['values'][0]['id']);
107 // mutate $params to match expected return value
108 unset($this->_params
['sequential']);
109 //assertDBState compares expected values in $result to actual values in the DB
110 $this->assertDBState('CRM_Core_DAO_Job', $result['id'], $this->_params
);
116 public function testClone() {
117 $createResult = $this->callAPISuccess('job', 'create', $this->_params
);
118 $params = ['id' => $createResult['id']];
119 $cloneResult = $this->callAPIAndDocument('job', 'clone', $params, __FUNCTION__
, __FILE__
);
120 $clonedJob = $cloneResult['values'][$cloneResult['id']];
121 $this->assertEquals($this->_params
['name'] . ' - Copy', $clonedJob['name']);
122 $this->assertEquals($this->_params
['description'], $clonedJob['description']);
123 $this->assertEquals($this->_params
['parameters'], $clonedJob['parameters']);
124 $this->assertEquals($this->_params
['is_active'], $clonedJob['is_active']);
125 $this->assertArrayNotHasKey('last_run', $clonedJob);
126 $this->assertArrayNotHasKey('scheduled_run_date', $clonedJob);
130 * Check if required fields are not passed.
132 public function testDeleteWithoutRequired() {
134 'name' => 'API_Test_PP',
135 'title' => 'API Test Payment Processor',
136 'class_name' => 'CRM_Core_Payment_APITest',
139 $result = $this->callAPIFailure('job', 'delete', $params);
140 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
144 * Check with incorrect required fields.
146 public function testDeleteWithIncorrectData() {
150 $this->callAPIFailure('job', 'delete', $params);
156 public function testDelete() {
157 $createResult = $this->callAPISuccess('job', 'create', $this->_params
);
158 $params = ['id' => $createResult['id']];
159 $this->callAPIAndDocument('job', 'delete', $params, __FUNCTION__
, __FILE__
);
160 $this->assertAPIDeleted($this->_entity
, $createResult['id']);
165 * public function testCallUpdateGreetingMissingParams() {
166 * $result = $this->callAPISuccess($this->_entity, 'update_greeting', array('gt' => 1));
167 * $this->assertEquals('Mandatory key(s) missing from params array: ct', $result['error_message']);
170 * public function testCallUpdateGreetingIncorrectParams() {
171 * $result = $this->callAPISuccess($this->_entity, 'update_greeting', array('gt' => 1, 'ct' => 'djkfhdskjfhds'));
172 * $this->assertEquals('ct `djkfhdskjfhds` is not valid.', $result['error_message']);
175 * Note that this test is about tesing the metadata / calling of the function & doesn't test the success of the called function
177 public function testCallUpdateGreetingSuccess() {
178 $this->callAPISuccess($this->_entity
, 'update_greeting', [
179 'gt' => 'postal_greeting',
180 'ct' => 'Individual',
184 public function testCallUpdateGreetingCommaSeparatedParamsSuccess() {
185 $gt = 'postal_greeting,email_greeting,addressee';
186 $ct = 'Individual,Household';
187 $this->callAPISuccess($this->_entity
, 'update_greeting', ['gt' => $gt, 'ct' => $ct]);
191 * Test the call reminder success sends more than 25 reminders & is not incorrectly limited.
193 * Note that this particular test sends the reminders to the additional recipients only
194 * as no real reminder person is configured
196 * Also note that this is testing a 'job' api so is in this class rather than scheduled_reminder - which
197 * seems a cleaner place to build up a collection of scheduled reminder testing functions. However, it seems
198 * that the api itself would need to be moved to the scheduled_reminder fn to do that with the job wrapper being respected for legacy functions
200 public function testCallSendReminderSuccessMoreThanDefaultLimit() {
201 $membershipTypeID = $this->membershipTypeCreate();
202 $this->membershipStatusCreate();
204 for ($i = 1; $i <= $createTotal; $i++
) {
205 $contactID = $this->individualCreate();
206 $groupID = $this->groupCreate(['name' => $i, 'title' => $i]);
207 $this->callAPISuccess('action_schedule', 'create', [
208 'title' => " job $i",
209 'subject' => "job $i",
210 'entity_value' => $membershipTypeID,
212 'start_action_date' => 'membership_join_date',
213 'start_action_offset' => 0,
214 'start_action_condition' => 'before',
215 'start_action_unit' => 'hour',
216 'group_id' => $groupID,
219 $this->callAPISuccess('group_contact', 'create', [
220 'contact_id' => $contactID,
222 'group_id' => $groupID,
225 $this->callAPISuccess('job', 'send_reminder', []);
226 $successfulCronCount = CRM_Core_DAO
::singleValueQuery("SELECT count(*) FROM civicrm_action_log");
227 $this->assertEquals($successfulCronCount, $createTotal);
231 * Test scheduled reminders respect limit to (since above identified addition_to handling issue).
233 * We create 3 contacts - 1 is in our group, 1 has our membership & the chosen one has both
234 * & check that only the chosen one got the reminder
236 public function testCallSendReminderLimitToSMS() {
237 $membershipTypeID = $this->membershipTypeCreate();
238 $this->membershipStatusCreate();
240 $groupID = $this->groupCreate(['name' => 'Texan drawlers', 'title' => 'a...']);
241 for ($i = 1; $i <= $createTotal; $i++
) {
242 $contactID = $this->individualCreate();
243 $this->callAPISuccess('Phone', 'create', [
244 'contact_id' => $contactID,
245 'phone' => '555 123 1234',
246 'phone_type_id' => 'Mobile',
247 'location_type_id' => 'Billing',
250 $theChosenOneID = $contactID;
253 $this->callAPISuccess('group_contact', 'create', [
254 'contact_id' => $contactID,
256 'group_id' => $groupID,
260 $this->callAPISuccess('membership', 'create', [
261 'contact_id' => $contactID,
262 'membership_type_id' => $membershipTypeID,
263 'join_date' => 'now',
264 'start_date' => '+ 1 day',
268 $this->setupForSmsTests();
269 $provider = civicrm_api3('SmsProvider', 'create', [
270 'name' => "CiviTestSMSProvider",
276 "api_params" => "a=1",
281 $this->callAPISuccess('action_schedule', 'create', [
282 'title' => " remind all Texans",
283 'subject' => "drawling renewal",
284 'entity_value' => $membershipTypeID,
286 'start_action_date' => 'membership_start_date',
287 'start_action_offset' => 1,
288 'start_action_condition' => 'before',
289 'start_action_unit' => 'day',
290 'group_id' => $groupID,
292 'sms_provider_id' => $provider['id'],
293 'mode' => 'User_Preference',
295 $this->callAPISuccess('job', 'send_reminder', []);
296 $successfulCronCount = CRM_Core_DAO
::singleValueQuery("SELECT count(*) FROM civicrm_action_log");
297 $this->assertEquals($successfulCronCount, 1);
298 $sentToID = CRM_Core_DAO
::singleValueQuery("SELECT contact_id FROM civicrm_action_log");
299 $this->assertEquals($sentToID, $theChosenOneID);
300 $this->assertEquals(0, CRM_Core_DAO
::singleValueQuery("SELECT is_error FROM civicrm_action_log"));
301 $this->setupForSmsTests(TRUE);
304 public function testCallDisableExpiredRelationships() {
305 $individualID = $this->individualCreate();
306 $orgID = $this->organizationCreate();
307 CRM_Utils_Hook_UnitTests
::singleton()->setHook('civicrm_pre', [$this, 'hookPreRelationship']);
308 $relationshipTypeID = $this->callAPISuccess('relationship_type', 'getvalue', [
310 'name_a_b' => 'Employee of',
312 $result = $this->callAPISuccess('relationship', 'create', [
313 'relationship_type_id' => $relationshipTypeID,
314 'contact_id_a' => $individualID,
315 'contact_id_b' => $orgID,
317 'end_date' => 'yesterday',
319 $relationshipID = $result['id'];
320 $this->assertEquals('Hooked', $result['values'][$relationshipID]['description']);
321 $this->callAPISuccess($this->_entity
, 'disable_expired_relationships', []);
322 $result = $this->callAPISuccess('relationship', 'get', []);
323 $this->assertEquals('Go Go you good thing', $result['values'][$relationshipID]['description']);
324 $this->contactDelete($individualID);
325 $this->contactDelete($orgID);
329 * Event templates should not send reminders to additional contacts.
331 public function testTemplateRemindAddlContacts() {
332 $contactId = $this->individualCreate();
333 $groupId = $this->groupCreate(['name' => 'Additional Contacts', 'title' => 'Additional Contacts']);
334 $this->callAPISuccess('GroupContact', 'create', [
335 'contact_id' => $contactId,
336 'group_id' => $groupId,
338 $event = $this->eventCreate(['is_template' => 1, 'template_title' => "I'm a template", 'title' => NULL]);
339 $eventId = $event['id'];
341 $actionSchedule = $this->callAPISuccess('action_schedule', 'create', [
342 'title' => "Do not send me",
343 'subject' => "I am a reminder attached to a template.",
344 'entity_value' => $eventId,
346 'start_action_date' => 'start_date',
347 'start_action_offset' => 1,
348 'start_action_condition' => 'before',
349 'start_action_unit' => 'day',
350 'group_id' => $groupId,
355 $this->callAPISuccess('job', 'send_reminder', []);
356 $successfulCronCount = CRM_Core_DAO
::singleValueQuery("SELECT count(*) FROM civicrm_action_log");
357 $this->assertEquals(0, $successfulCronCount);
361 * Test scheduled reminders respect limit to (since above identified addition_to handling issue).
363 * We create 3 contacts - 1 is in our group, 1 has our membership & the chosen one has both
364 * & check that only the chosen one got the reminder
366 * Also check no hard fail on cron job with running a reminder that has a deleted SMS provider
368 public function testCallSendReminderLimitToSMSWithDeletedProviderr() {
369 $membershipTypeID = $this->membershipTypeCreate();
370 $this->membershipStatusCreate();
372 $groupID = $this->groupCreate(['name' => 'Texan drawlers', 'title' => 'a...']);
373 for ($i = 1; $i <= $createTotal; $i++
) {
374 $contactID = $this->individualCreate();
375 $this->callAPISuccess('Phone', 'create', [
376 'contact_id' => $contactID,
377 'phone' => '555 123 1234',
378 'phone_type_id' => 'Mobile',
379 'location_type_id' => 'Billing',
382 $theChosenOneID = $contactID;
385 $this->callAPISuccess('group_contact', 'create', [
386 'contact_id' => $contactID,
388 'group_id' => $groupID,
392 $this->callAPISuccess('membership', 'create', [
393 'contact_id' => $contactID,
394 'membership_type_id' => $membershipTypeID,
395 'join_date' => 'now',
396 'start_date' => '+ 1 day',
400 $this->setupForSmsTests();
401 $provider = civicrm_api3('SmsProvider', 'create', [
402 'name' => "CiviTestSMSProvider",
408 "api_params" => "a=1",
413 $this->callAPISuccess('action_schedule', 'create', [
414 'title' => " remind all Texans",
415 'subject' => "drawling renewal",
416 'entity_value' => $membershipTypeID,
418 'start_action_date' => 'membership_start_date',
419 'start_action_offset' => 1,
420 'start_action_condition' => 'before',
421 'start_action_unit' => 'day',
422 'group_id' => $groupID,
424 'sms_provider_id' => $provider['id'],
427 $this->callAPISuccess('SmsProvider', 'delete', ['id' => $provider['id']]);
428 $this->callAPISuccess('job', 'send_reminder', []);
429 $cronCount = CRM_Core_DAO
::singleValueQuery("SELECT count(*) FROM civicrm_action_log");
430 $this->assertEquals($cronCount, 1);
431 $sentToID = CRM_Core_DAO
::singleValueQuery("SELECT contact_id FROM civicrm_action_log");
432 $this->assertEquals($sentToID, $theChosenOneID);
433 $cronlog = CRM_Core_DAO
::executeQuery("SELECT * FROM civicrm_action_log")->fetchAll()[0];
434 $this->assertEquals(1, $cronlog['is_error']);
435 $this->assertEquals('SMS reminder cannot be sent because the SMS provider has been deleted.', $cronlog['message']);
436 $this->setupForSmsTests(TRUE);
440 * Test the batch merge function.
442 * We are just checking it returns without error here.
444 public function testBatchMerge() {
445 $this->callAPISuccess('Job', 'process_batch_merge', []);
449 * Test the batch merge function actually works!
451 * @dataProvider getMergeSets
455 public function testBatchMergeWorks($dataSet) {
456 foreach ($dataSet['contacts'] as $params) {
457 $this->callAPISuccess('Contact', 'create', $params);
460 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['mode' => $dataSet['mode']]);
461 $this->assertEquals($dataSet['skipped'], count($result['values']['skipped']), 'Failed to skip the right number:' . $dataSet['skipped']);
462 $this->assertEquals($dataSet['merged'], count($result['values']['merged']));
463 $result = $this->callAPISuccess('Contact', 'get', [
464 'contact_sub_type' => 'Student',
466 'is_deceased' => ['IN' => [0, 1]],
467 'options' => ['sort' => 'id ASC'],
469 $this->assertEquals(count($dataSet['expected']), $result['count']);
470 foreach ($dataSet['expected'] as $index => $contact) {
471 foreach ($contact as $key => $value) {
472 if ($key == 'gender_id') {
475 $this->assertEquals($value, $result['values'][$index][$key]);
481 * Check that the merge carries across various related entities.
483 * Note the group combinations & expected results:
485 public function testBatchMergeWithAssets() {
486 $contactID = $this->individualCreate();
487 $contact2ID = $this->individualCreate();
488 $this->contributionCreate(['contact_id' => $contactID]);
489 $this->contributionCreate(['contact_id' => $contact2ID, 'invoice_id' => '2', 'trxn_id' => 2]);
490 $this->contactMembershipCreate(['contact_id' => $contactID]);
491 $this->contactMembershipCreate(['contact_id' => $contact2ID]);
492 $this->activityCreate(['source_contact_id' => $contactID, 'target_contact_id' => $contactID, 'assignee_contact_id' => $contactID]);
493 $this->activityCreate(['source_contact_id' => $contact2ID, 'target_contact_id' => $contact2ID, 'assignee_contact_id' => $contact2ID]);
494 $this->tagCreate(['name' => 'Tall']);
495 $this->tagCreate(['name' => 'Short']);
496 $this->entityTagAdd(['contact_id' => $contactID, 'tag_id' => 'Tall']);
497 $this->entityTagAdd(['contact_id' => $contact2ID, 'tag_id' => 'Short']);
498 $this->entityTagAdd(['contact_id' => $contact2ID, 'tag_id' => 'Tall']);
499 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['mode' => 'safe']);
500 $this->assertEquals(0, count($result['values']['skipped']));
501 $this->assertEquals(1, count($result['values']['merged']));
502 $this->callAPISuccessGetCount('Contribution', ['contact_id' => $contactID], 2);
503 $this->callAPISuccessGetCount('Contribution', ['contact_id' => $contact2ID], 0);
504 $this->callAPISuccessGetCount('FinancialItem', ['contact_id' => $contactID], 2);
505 $this->callAPISuccessGetCount('FinancialItem', ['contact_id' => $contact2ID], 0);
506 $this->callAPISuccessGetCount('Membership', ['contact_id' => $contactID], 2);
507 $this->callAPISuccessGetCount('Membership', ['contact_id' => $contact2ID], 0);
508 $this->callAPISuccessGetCount('EntityTag', ['contact_id' => $contactID], 2);
509 $this->callAPISuccessGetCount('EntityTag', ['contact_id' => $contact2ID], 0);
510 // 14 activities is one for each contribution (2), two (source + target) for each membership (+(2x2) = 6)
511 // 3 for each of the added activities as there are 3 roles (+6 = 12
512 // 2 for the (source & target) contact merged activity (+2 = 14)
513 $this->callAPISuccessGetCount('ActivityContact', ['contact_id' => $contactID], 14);
514 // 2 for the connection to the deleted by merge activity (source & target)
515 $this->callAPISuccessGetCount('ActivityContact', ['contact_id' => $contact2ID], 2);
519 * Check that the merge carries across various related entities.
521 * Note the group combinations 'expected' results:
523 * Group 0 Added null Added
524 * Group 1 Added Added Added
525 * Group 2 Added Removed **** Added
526 * Group 3 Removed null **** null
527 * Group 4 Removed Added **** Added
528 * Group 5 Removed Removed **** null
529 * Group 6 null Added Added
530 * Group 7 null Removed **** null
532 * The ones with **** are the ones where I think a case could be made to change the behaviour.
534 public function testBatchMergeMergesGroups() {
535 $contactID = $this->individualCreate();
536 $contact2ID = $this->individualCreate();
538 for ($i = 0; $i < 8; $i++
) {
539 $groups[] = $this->groupCreate([
540 'name' => 'mergeGroup' . $i,
541 'title' => 'merge group' . $i,
545 $this->callAPISuccess('GroupContact', 'create', [
546 'contact_id' => $contactID,
547 'group_id' => $groups[0],
549 $this->callAPISuccess('GroupContact', 'create', [
550 'contact_id' => $contactID,
551 'group_id' => $groups[1],
553 $this->callAPISuccess('GroupContact', 'create', [
554 'contact_id' => $contactID,
555 'group_id' => $groups[2],
557 $this->callAPISuccess('GroupContact', 'create', [
558 'contact_id' => $contactID,
559 'group_id' => $groups[3],
560 'status' => 'Removed',
562 $this->callAPISuccess('GroupContact', 'create', [
563 'contact_id' => $contactID,
564 'group_id' => $groups[4],
565 'status' => 'Removed',
567 $this->callAPISuccess('GroupContact', 'create', [
568 'contact_id' => $contactID,
569 'group_id' => $groups[5],
570 'status' => 'Removed',
572 $this->callAPISuccess('GroupContact', 'create', [
573 'contact_id' => $contact2ID,
574 'group_id' => $groups[1],
576 $this->callAPISuccess('GroupContact', 'create', [
577 'contact_id' => $contact2ID,
578 'group_id' => $groups[2],
579 'status' => 'Removed',
581 $this->callAPISuccess('GroupContact', 'create', [
582 'contact_id' => $contact2ID,
583 'group_id' => $groups[4],
585 $this->callAPISuccess('GroupContact', 'create', [
586 'contact_id' => $contact2ID,
587 'group_id' => $groups[5],
588 'status' => 'Removed',
590 $this->callAPISuccess('GroupContact', 'create', [
591 'contact_id' => $contact2ID,
592 'group_id' => $groups[6],
594 $this->callAPISuccess('GroupContact', 'create', [
595 'contact_id' => $contact2ID,
596 'group_id' => $groups[7],
597 'status' => 'Removed',
599 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['mode' => 'safe']);
600 $this->assertEquals(0, count($result['values']['skipped']));
601 $this->assertEquals(1, count($result['values']['merged']));
602 $groupResult = $this->callAPISuccess('GroupContact', 'get', []);
603 $this->assertEquals(5, $groupResult['count']);
611 foreach ($groupResult['values'] as $groupValues) {
612 $this->assertEquals($contactID, $groupValues['contact_id']);
613 $this->assertEquals('Added', $groupValues['status']);
614 $this->assertTrue(in_array($groupValues['group_id'], $expectedGroups));
620 * Test the decisions made for addresses when merging.
622 * @dataProvider getMergeLocationData
625 * (the ones with **** could be disputed as whether it is the best outcome).
626 * 'matching_primary' - Primary matches, including location_type_id. One contact has an additional address.
627 * - result - primary is the shared one. Additional address is retained.
628 * 'matching_primary_reverse' - Primary matches, including location_type_id. Keep both. (opposite order)
629 * - result - primary is the shared one. Additional address is retained.
630 * 'only_one_has_address' - Only one contact has addresses (retain)
631 * - the (only) address is retained
632 * 'only_one_has_address_reverse'
633 * - the (only) address is retained
634 * 'different_primaries_with_different_location_type' Primaries are different but do not clash due to diff type
635 * - result - both addresses kept. The one from the kept (lowest ID) contact is primary
636 * 'different_primaries_with_different_location_type_reverse' Primaries are different but do not clash due to diff type
637 * - result - both addresses kept. The one from the kept (lowest ID) contact is primary
638 * 'different_primaries_location_match_only_one_address' per previous but a second address matches the primary but is not primary
639 * - result - both addresses kept. The one from the kept (lowest ID) contact is primary
640 * 'different_primaries_location_match_only_one_address_reverse' per previous but a second address matches the primary but is not primary
641 * - result - both addresses kept. The one from the kept (lowest ID) contact is primary
642 * 'same_primaries_different_location' Primary addresses are the same but have different location type IDs
643 * - result primary kept with the lowest ID. Other address retained too (to preserve location type info).
644 * 'same_primaries_different_location_reverse' Primary addresses are the same but have different location type IDs
645 * - result primary kept with the lowest ID. Other address retained too (to preserve location type info).
647 * @param array $dataSet
649 public function testBatchMergesAddresses($dataSet) {
650 $contactID1 = $this->individualCreate();
651 $contactID2 = $this->individualCreate();
652 foreach ($dataSet['contact_1'] as $address) {
653 $this->callAPISuccess($dataSet['entity'], 'create', array_merge(['contact_id' => $contactID1], $address));
655 foreach ($dataSet['contact_2'] as $address) {
656 $this->callAPISuccess($dataSet['entity'], 'create', array_merge(['contact_id' => $contactID2], $address));
659 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['mode' => 'safe']);
660 $this->assertEquals(1, count($result['values']['merged']));
661 $addresses = $this->callAPISuccess($dataSet['entity'], 'get', ['contact_id' => $contactID1, 'sequential' => 1]);
662 $this->assertEquals(count($dataSet['expected']), $addresses['count'], "Did not get the expected result for " . $dataSet['entity'] . (!empty($dataSet['description']) ?
" on dataset {$dataSet['description']}" : ''));
663 $locationTypes = $this->callAPISuccess($dataSet['entity'], 'getoptions', ['field' => 'location_type_id']);
664 foreach ($dataSet['expected'] as $index => $expectedAddress) {
665 foreach ($expectedAddress as $key => $value) {
666 if ($key == 'location_type_id') {
667 $this->assertEquals($locationTypes['values'][$addresses['values'][$index][$key]], $value);
670 $this->assertEquals($addresses['values'][$index][$key], $value, "mismatch on $key" . (!empty($dataSet['description']) ?
" on dataset {$dataSet['description']}" : ''));
677 * Test altering the address decision by hook.
679 * @dataProvider getMergeLocationData
681 * @param array $dataSet
683 public function testBatchMergesAddressesHook($dataSet) {
684 $contactID1 = $this->individualCreate();
685 $contactID2 = $this->individualCreate();
686 $this->contributionCreate(['contact_id' => $contactID1, 'receive_date' => '2010-01-01', 'invoice_id' => 1, 'trxn_id' => 1]);
687 $this->contributionCreate(['contact_id' => $contactID2, 'receive_date' => '2012-01-01', 'invoice_id' => 2, 'trxn_id' => 2]);
688 foreach ($dataSet['contact_1'] as $address) {
689 $this->callAPISuccess($dataSet['entity'], 'create', array_merge(['contact_id' => $contactID1], $address));
691 foreach ($dataSet['contact_2'] as $address) {
692 $this->callAPISuccess($dataSet['entity'], 'create', array_merge(['contact_id' => $contactID2], $address));
694 $this->hookClass
->setHook('civicrm_alterLocationMergeData', [$this, 'hookMostRecentDonor']);
696 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['mode' => 'safe']);
697 $this->assertEquals(1, count($result['values']['merged']));
698 $addresses = $this->callAPISuccess($dataSet['entity'], 'get', ['contact_id' => $contactID1, 'sequential' => 1]);
699 $this->assertEquals(count($dataSet['expected_hook']), $addresses['count']);
700 $locationTypes = $this->callAPISuccess($dataSet['entity'], 'getoptions', ['field' => 'location_type_id']);
701 foreach ($dataSet['expected_hook'] as $index => $expectedAddress) {
702 foreach ($expectedAddress as $key => $value) {
703 if ($key == 'location_type_id') {
704 $this->assertEquals($locationTypes['values'][$addresses['values'][$index][$key]], $value);
707 $this->assertEquals($value, $addresses['values'][$index][$key], $dataSet['entity'] . ': Unexpected value for ' . $key . (!empty($dataSet['description']) ?
" on dataset {$dataSet['description']}" : ''));
714 * Test the organization will not be matched to an individual.
716 public function testBatchMergeWillNotMergeOrganizationToIndividual() {
717 $individual = $this->callAPISuccess('Contact', 'create', [
718 'contact_type' => 'Individual',
719 'organization_name' => 'Anon',
720 'email' => 'anonymous@hacker.com',
722 $organization = $this->callAPISuccess('Contact', 'create', [
723 'contact_type' => 'Organization',
724 'organization_name' => 'Anon',
725 'email' => 'anonymous@hacker.com',
727 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['mode' => 'aggressive']);
728 $this->assertEquals(0, count($result['values']['skipped']));
729 $this->assertEquals(0, count($result['values']['merged']));
730 $this->callAPISuccessGetSingle('Contact', ['id' => $individual['id']]);
731 $this->callAPISuccessGetSingle('Contact', ['id' => $organization['id']]);
736 * Test hook allowing modification of the data calculated for merging locations.
738 * We are testing a nuanced real life situation where the address data of the
739 * most recent donor gets priority - resulting in the primary address being set
740 * to the primary address of the most recent donor and address data on a per
741 * location type basis also being set to the most recent donor. Hook also excludes
742 * a fully matching address with a different location.
744 * This has been added to the test suite to ensure the code supports more this
745 * type of intervention.
747 * @param array $blocksDAO
748 * Array of location DAO to be saved. These are arrays in 2 keys 'update' & 'delete'.
750 * Contact_id of the contact that survives the merge.
751 * @param int $otherId
752 * Contact_id of the contact that will be absorbed and deleted.
753 * @param array $migrationInfo
754 * Calculated migration info, informational only.
758 public function hookMostRecentDonor(&$blocksDAO, $mainId, $otherId, $migrationInfo) {
760 $lastDonorID = $this->callAPISuccessGetValue('Contribution', [
761 'return' => 'contact_id',
762 'contact_id' => ['IN' => [$mainId, $otherId]],
763 'options' => ['sort' => 'receive_date DESC', 'limit' => 1],
765 // Since the last donor is not the main ID we are prioritising info from the last donor.
766 // In the test this should always be true - but keep the check in case
767 // something changes that we need to detect.
768 if ($lastDonorID != $mainId) {
769 foreach ($migrationInfo['other_details']['location_blocks'] as $blockType => $blocks) {
770 foreach ($blocks as $block) {
771 if ($block['is_primary']) {
772 $primaryAddressID = $block['id'];
773 if (!empty($migrationInfo['main_details']['location_blocks'][$blockType])) {
774 foreach ($migrationInfo['main_details']['location_blocks'][$blockType] as $mainBlock) {
775 if (empty($blocksDAO[$blockType]['update'][$block['id']]) && $mainBlock['location_type_id'] == $block['location_type_id']) {
776 // This was an address match - we just need to check the is_primary
777 // is true on the matching kept address.
778 $primaryAddressID = $mainBlock['id'];
779 $blocksDAO[$blockType]['update'][$primaryAddressID] = _civicrm_api3_load_DAO($blockType);
780 $blocksDAO[$blockType]['update'][$primaryAddressID]->id
= $primaryAddressID;
782 $mainLocationTypeID = $mainBlock['location_type_id'];
783 // We also want to be more ruthless about removing matching addresses.
784 unset($mainBlock['location_type_id']);
785 if (CRM_Dedupe_Merger
::locationIsSame($block, $mainBlock)
786 && (!isset($blocksDAO[$blockType]['update']) ||
!isset($blocksDAO[$blockType]['update'][$mainBlock['id']]))
787 && (!isset($blocksDAO[$blockType]['delete']) ||
!isset($blocksDAO[$blockType]['delete'][$mainBlock['id']]))
789 $blocksDAO[$blockType]['delete'][$mainBlock['id']] = _civicrm_api3_load_DAO($blockType);
790 $blocksDAO[$blockType]['delete'][$mainBlock['id']]->id
= $mainBlock['id'];
792 // Arguably the right way to handle this is just to set is_primary for the primary
793 // and for the merge fn to call something like BAO::add & hooks to work etc.
794 // if that happens though this should keep working...
795 elseif ($mainBlock['is_primary'] && $mainLocationTypeID != $block['location_type_id']) {
796 $blocksDAO['address']['update'][$mainBlock['id']] = _civicrm_api3_load_DAO($blockType);
797 $blocksDAO['address']['update'][$mainBlock['id']]->is_primary
= 0;
798 $blocksDAO['address']['update'][$mainBlock['id']]->id
= $mainBlock['id'];
802 $blocksDAO[$blockType]['update'][$primaryAddressID]->is_primary
= 1;
811 * Get address combinations for the merge test.
815 public function getMergeLocationData() {
816 $address1 = ['street_address' => 'Buckingham Palace', 'city' => 'London'];
817 $address2 = ['street_address' => 'The Doghouse', 'supplemental_address_1' => 'under the blanket'];
818 $data = $this->getMergeLocations($address1, $address2, 'Address');
819 $data = array_merge($data, $this->getMergeLocations(['phone' => '12345', 'phone_type_id' => 1], ['phone' => '678910', 'phone_type_id' => 1], 'Phone'));
820 $data = array_merge($data, $this->getMergeLocations(['phone' => '12345'], ['phone' => '678910'], 'Phone'));
821 $data = array_merge($data, $this->getMergeLocations(['email' => 'mini@me.com'], ['email' => 'mini@me.org'], 'Email', [
823 'email' => 'anthony_anderson@civicrm.org',
824 'location_type_id' => 'Home',
832 * Test the batch merge does not create duplicate emails.
834 * Test CRM-18546, a 4.7 regression whereby a merged contact gets duplicate emails.
836 public function testBatchMergeEmailHandling() {
837 for ($x = 0; $x <= 4; $x++
) {
838 $id = $this->individualCreate(['email' => 'batman@gotham.met']);
840 $result = $this->callAPISuccess('Job', 'process_batch_merge', []);
841 $this->assertEquals(4, count($result['values']['merged']));
842 $this->callAPISuccessGetCount('Contact', ['email' => 'batman@gotham.met'], 1);
843 $contacts = $this->callAPISuccess('Contact', 'get', ['is_deleted' => 0]);
844 $deletedContacts = $this->callAPISuccess('Contact', 'get', ['is_deleted' => 1]);
845 $this->callAPISuccessGetCount('Email', [
846 'email' => 'batman@gotham.met',
847 'contact_id' => ['IN' => array_keys($contacts['values'])],
849 $this->callAPISuccessGetCount('Email', [
850 'email' => 'batman@gotham.met',
851 'contact_id' => ['IN' => array_keys($deletedContacts['values'])],
856 * Test the batch merge respects email "on hold".
858 * Test CRM-19148, Batch merge - Email on hold data lost when there is a conflict.
860 * @dataProvider getOnHoldSets
862 * @param bool $onHold1
863 * @param bool $onHold2
865 * @param string $conflictText
867 * @throws \CRM_Core_Exception
869 public function testBatchMergeEmailOnHold($onHold1, $onHold2, $merge, $conflictText) {
870 $this->individualCreate([
871 'api.email.create' => [
872 'email' => 'batman@gotham.met',
873 'location_type_id' => 'Work',
875 'on_hold' => $onHold1,
878 $this->individualCreate([
879 'api.email.create' => [
880 'email' => 'batman@gotham.met',
881 'location_type_id' => 'Work',
883 'on_hold' => $onHold2,
886 $result = $this->callAPISuccess('Job', 'process_batch_merge', []);
887 $this->assertCount($merge, $result['values']['merged']);
889 $defaultRuleGroupID = $this->callAPISuccessGetValue('RuleGroup', [
890 'contact_type' => 'Individual',
891 'used' => 'Unsupervised',
893 'options' => ['limit' => 1],
896 $duplicates = $this->callAPISuccess('Dedupe', 'getduplicates', ['rule_group_id' => $defaultRuleGroupID]);
897 $this->assertEquals($conflictText, $duplicates['values'][0]['conflicts']);
902 * Data provider for testBatchMergeEmailOnHold: combinations of on_hold & expected outcomes.
904 public function getOnHoldSets() {
905 // Each row specifies: contact 1 on_hold, contact 2 on_hold, merge? (0 or 1),
908 [0, 1, 0, "Email 2 (Work): 'batman@gotham.met' vs. 'batman@gotham.met\n(On Hold)'"],
909 [1, 0, 0, "Email 2 (Work): 'batman@gotham.met\n(On Hold)' vs. 'batman@gotham.met'"],
916 * Test the batch merge does not fatal on an empty rule.
918 * @dataProvider getRuleSets
920 * @param string $contactType
921 * @param string $used
922 * @param string $name
923 * @param bool $isReserved
924 * @param int $threshold
926 public function testBatchMergeEmptyRule($contactType, $used, $name, $isReserved, $threshold) {
927 $ruleGroup = $this->callAPISuccess('RuleGroup', 'create', [
928 'contact_type' => $contactType,
929 'threshold' => $threshold,
932 'is_reserved' => $isReserved,
934 $this->callAPISuccess('Job', 'process_batch_merge', ['rule_group_id' => $ruleGroup['id']]);
935 $this->callAPISuccess('RuleGroup', 'delete', ['id' => $ruleGroup['id']]);
939 * Get the various rule combinations.
941 public function getRuleSets() {
942 $contactTypes = ['Individual', 'Organization', 'Household'];
943 $useds = ['Unsupervised', 'General', 'Supervised'];
945 foreach ($contactTypes as $contactType) {
946 foreach ($useds as $used) {
947 $ruleGroups[] = [$contactType, $used, 'Bob', FALSE, 0];
948 $ruleGroups[] = [$contactType, $used, 'Bob', FALSE, 10];
949 $ruleGroups[] = [$contactType, $used, 'Bob', TRUE, 10];
950 $ruleGroups[] = [$contactType, $used, $contactType . $used, FALSE, 10];
951 $ruleGroups[] = [$contactType, $used, $contactType . $used, TRUE, 10];
958 * Test the batch merge does not create duplicate emails.
960 * Test CRM-18546, a 4.7 regression whereby a merged contact gets duplicate emails.
962 public function testBatchMergeMatchingAddress() {
963 for ($x = 0; $x <= 2; $x++
) {
964 $this->individualCreate([
965 'api.address.create' => [
966 'location_type_id' => 'Home',
967 'street_address' => 'Appt 115, The Batcave',
969 'postal_code' => 'Nananananana',
973 // Different location type, still merge, identical.
974 $this->individualCreate([
975 'api.address.create' => [
976 'location_type_id' => 'Main',
977 'street_address' => 'Appt 115, The Batcave',
979 'postal_code' => 'Nananananana',
983 $this->individualCreate([
984 'api.address.create' => [
985 'location_type_id' => 'Home',
986 'street_address' => 'Appt 115, The Batcave',
988 'postal_code' => 'Batman',
992 $result = $this->callAPISuccess('Job', 'process_batch_merge', []);
993 $this->assertEquals(3, count($result['values']['merged']));
994 $this->assertEquals(1, count($result['values']['skipped']));
995 $this->callAPISuccessGetCount('Contact', ['street_address' => 'Appt 115, The Batcave'], 2);
996 $contacts = $this->callAPISuccess('Contact', 'get', ['is_deleted' => 0]);
997 $deletedContacts = $this->callAPISuccess('Contact', 'get', ['is_deleted' => 1]);
998 $this->callAPISuccessGetCount('Address', [
999 'street_address' => 'Appt 115, The Batcave',
1000 'contact_id' => ['IN' => array_keys($contacts['values'])],
1003 $this->callAPISuccessGetCount('Address', [
1004 'street_address' => 'Appt 115, The Batcave',
1005 'contact_id' => ['IN' => array_keys($deletedContacts['values'])],
1010 * Test the batch merge by id range.
1012 * We have 2 sets of 5 matches & set the merge only to merge the lower set.
1014 public function testBatchMergeIDRange() {
1015 for ($x = 0; $x <= 4; $x++
) {
1016 $id = $this->individualCreate(['email' => 'batman@gotham.met']);
1018 for ($x = 0; $x <= 4; $x++
) {
1019 $this->individualCreate(['email' => 'robin@gotham.met']);
1021 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['criteria' => ['contact' => ['id' => ['<' => $id]]]]);
1022 $this->assertEquals(4, count($result['values']['merged']));
1023 $this->callAPISuccessGetCount('Contact', ['email' => 'batman@gotham.met'], 1);
1024 $this->callAPISuccessGetCount('Contact', ['email' => 'robin@gotham.met'], 5);
1025 $contacts = $this->callAPISuccess('Contact', 'get', ['is_deleted' => 0]);
1026 $deletedContacts = $this->callAPISuccess('Contact', 'get', ['is_deleted' => 0]);
1027 $this->callAPISuccessGetCount('Email', [
1028 'email' => 'batman@gotham.met',
1029 'contact_id' => ['IN' => array_keys($contacts['values'])],
1031 $this->callAPISuccessGetCount('Email', [
1032 'email' => 'batman@gotham.met',
1033 'contact_id' => ['IN' => array_keys($deletedContacts['values'])],
1035 $this->callAPISuccessGetCount('Email', [
1036 'email' => 'robin@gotham.met',
1037 'contact_id' => ['IN' => array_keys($contacts['values'])],
1043 * Test the batch merge copes with view only custom data field.
1045 public function testBatchMergeCustomDataViewOnlyField() {
1046 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviCRM', 'edit my contact'];
1047 $mouseParams = ['first_name' => 'Mickey', 'last_name' => 'Mouse', 'email' => 'tha_mouse@mouse.com'];
1048 $this->individualCreate($mouseParams);
1050 $customGroup = $this->CustomGroupCreate();
1051 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id'], 'is_view' => 1]);
1052 $this->individualCreate(array_merge($mouseParams, ['custom_' . $customField['id'] => 'blah']));
1054 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0, 'mode' => 'safe']);
1055 $this->assertEquals(1, count($result['values']['merged']));
1056 $mouseParams['return'] = 'custom_' . $customField['id'];
1057 $mouse = $this->callAPISuccess('Contact', 'getsingle', $mouseParams);
1058 $this->assertEquals('blah', $mouse['custom_' . $customField['id']]);
1060 $this->customFieldDelete($customField['id']);
1061 $this->customGroupDelete($customGroup['id']);
1065 * Test the batch merge retains 0 as a valid custom field value.
1067 * Note that we set 0 on 2 fields with one on each contact to ensure that
1068 * both merged & mergee fields are respected.
1070 * @throws \CRM_Core_Exception
1072 public function testBatchMergeCustomDataZeroValueField() {
1073 $customGroup = $this->customGroupCreate();
1074 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id'], 'default_value' => NULL]);
1076 $mouseParams = ['first_name' => 'Mickey', 'last_name' => 'Mouse', 'email' => 'tha_mouse@mouse.com'];
1077 $this->individualCreate(array_merge($mouseParams, ['custom_' . $customField['id'] => '']));
1078 $this->individualCreate(array_merge($mouseParams, ['custom_' . $customField['id'] => 0]));
1080 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0, 'mode' => 'safe']);
1081 $this->assertEquals(1, count($result['values']['merged']));
1082 $mouseParams['return'] = 'custom_' . $customField['id'];
1083 $mouse = $this->callAPISuccess('Contact', 'getsingle', $mouseParams);
1084 $this->assertEquals(0, $mouse['custom_' . $customField['id']]);
1086 $this->individualCreate(array_merge($mouseParams, ['custom_' . $customField['id'] => NULL]));
1087 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0, 'mode' => 'safe']);
1088 $this->assertEquals(1, count($result['values']['merged']));
1089 $mouseParams['return'] = 'custom_' . $customField['id'];
1090 $mouse = $this->callAPISuccess('Contact', 'getsingle', $mouseParams);
1091 $this->assertEquals(0, $mouse['custom_' . $customField['id']]);
1093 $this->customFieldDelete($customField['id']);
1094 $this->customGroupDelete($customGroup['id']);
1098 * Test the batch merge treats 0 vs 1 as a conflict.
1100 * @throws \CRM_Core_Exception
1102 public function testBatchMergeCustomDataZeroValueFieldWithConflict() {
1103 $customGroup = $this->customGroupCreate();
1104 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id'], 'default_value' => NULL]);
1106 $mouseParams = ['first_name' => 'Mickey', 'last_name' => 'Mouse', 'email' => 'tha_mouse@mouse.com'];
1107 $mouse1 = $this->individualCreate(array_merge($mouseParams, ['custom_' . $customField['id'] => 0]));
1108 $mouse2 = $this->individualCreate(array_merge($mouseParams, ['custom_' . $customField['id'] => 1]));
1110 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0, 'mode' => 'safe']);
1111 $this->assertEquals(0, count($result['values']['merged']));
1113 // Reverse which mouse has the zero to test we still get a conflict.
1114 $this->individualCreate(array_merge($mouseParams, ['id' => $mouse1, 'custom_' . $customField['id'] => 1]));
1115 $this->individualCreate(array_merge($mouseParams, ['id' => $mouse2, 'custom_' . $customField['id'] => 0]));
1116 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0, 'mode' => 'safe']);
1117 $this->assertEquals(0, count($result['values']['merged']));
1119 $this->customFieldDelete($customField['id']);
1120 $this->customGroupDelete($customGroup['id']);
1124 * Test the batch merge function actually works!
1126 * @dataProvider getMergeSets
1130 public function testBatchMergeWorksCheckPermissionsTrue($dataSet) {
1131 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviCRM', 'administer CiviCRM', 'merge duplicate contacts', 'force merge duplicate contacts'];
1132 foreach ($dataSet['contacts'] as $params) {
1133 $this->callAPISuccess('Contact', 'create', $params);
1136 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 1, 'mode' => $dataSet['mode']]);
1137 $this->assertEquals(0, count($result['values']['merged']), 'User does not have permission to any contacts, so no merging');
1138 $this->assertEquals(0, count($result['values']['skipped']), 'User does not have permission to any contacts, so no skip visibility');
1142 * Test the batch merge function actually works!
1144 * @dataProvider getMergeSets
1148 public function testBatchMergeWorksCheckPermissionsFalse($dataSet) {
1149 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviCRM', 'edit my contact'];
1150 foreach ($dataSet['contacts'] as $params) {
1151 $this->callAPISuccess('Contact', 'create', $params);
1154 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0, 'mode' => $dataSet['mode']]);
1155 $this->assertEquals($dataSet['skipped'], count($result['values']['skipped']), 'Failed to skip the right number:' . $dataSet['skipped']);
1156 $this->assertEquals($dataSet['merged'], count($result['values']['merged']));
1160 * Get data for batch merge.
1162 public function getMergeSets() {
1169 'first_name' => 'Michael',
1170 'last_name' => 'Jackson',
1171 'email' => 'michael@neverland.com',
1172 'contact_type' => 'Individual',
1173 'contact_sub_type' => 'Student',
1174 'api.Address.create' => [
1175 'street_address' => 'big house',
1176 'location_type_id' => 'Home',
1180 'first_name' => 'Michael',
1181 'last_name' => 'Jackson',
1182 'email' => 'michael@neverland.com',
1183 'contact_type' => 'Individual',
1184 'contact_sub_type' => 'Student',
1191 'first_name' => 'Michael',
1192 'last_name' => 'Jackson',
1193 'email' => 'michael@neverland.com',
1194 'contact_type' => 'Individual',
1204 'first_name' => 'Michael',
1205 'last_name' => 'Jackson',
1206 'email' => 'michael@neverland.com',
1207 'contact_type' => 'Individual',
1208 'contact_sub_type' => 'Student',
1209 'api.Address.create' => [
1210 'street_address' => 'big house',
1211 'location_type_id' => 'Home',
1215 'first_name' => 'Michael',
1216 'last_name' => 'Jackson',
1217 'email' => 'michael@neverland.com',
1218 'contact_type' => 'Individual',
1219 'contact_sub_type' => 'Student',
1220 'api.Address.create' => [
1221 'street_address' => 'bigger house',
1222 'location_type_id' => 'Home',
1230 'first_name' => 'Michael',
1231 'last_name' => 'Jackson',
1232 'email' => 'michael@neverland.com',
1233 'contact_type' => 'Individual',
1234 'street_address' => 'big house',
1237 'first_name' => 'Michael',
1238 'last_name' => 'Jackson',
1239 'email' => 'michael@neverland.com',
1240 'contact_type' => 'Individual',
1241 'street_address' => 'bigger house',
1251 'first_name' => 'Michael',
1252 'last_name' => 'Jackson',
1253 'email' => 'michael@neverland.com',
1254 'contact_type' => 'Individual',
1255 'contact_sub_type' => 'Student',
1256 'api.Email.create' => [
1257 'email' => 'big.slog@work.co.nz',
1258 'location_type_id' => 'Work',
1262 'first_name' => 'Michael',
1263 'last_name' => 'Jackson',
1264 'email' => 'michael@neverland.com',
1265 'contact_type' => 'Individual',
1266 'contact_sub_type' => 'Student',
1267 'api.Email.create' => [
1268 'email' => 'big.slog@work.com',
1269 'location_type_id' => 'Work',
1277 'first_name' => 'Michael',
1278 'last_name' => 'Jackson',
1279 'email' => 'michael@neverland.com',
1280 'contact_type' => 'Individual',
1283 'first_name' => 'Michael',
1284 'last_name' => 'Jackson',
1285 'email' => 'michael@neverland.com',
1286 'contact_type' => 'Individual',
1296 'first_name' => 'Michael',
1297 'last_name' => 'Jackson',
1298 'email' => 'michael@neverland.com',
1299 'contact_type' => 'Individual',
1300 'contact_sub_type' => 'Student',
1301 'api.Phone.create' => [
1302 'phone' => '123456',
1303 'location_type_id' => 'Work',
1307 'first_name' => 'Michael',
1308 'last_name' => 'Jackson',
1309 'email' => 'michael@neverland.com',
1310 'contact_type' => 'Individual',
1311 'contact_sub_type' => 'Student',
1312 'api.Phone.create' => [
1314 'location_type_id' => 'Work',
1322 'first_name' => 'Michael',
1323 'last_name' => 'Jackson',
1324 'email' => 'michael@neverland.com',
1325 'contact_type' => 'Individual',
1328 'first_name' => 'Michael',
1329 'last_name' => 'Jackson',
1330 'email' => 'michael@neverland.com',
1331 'contact_type' => 'Individual',
1338 'mode' => 'aggressive',
1341 'first_name' => 'Michael',
1342 'last_name' => 'Jackson',
1343 'email' => 'michael@neverland.com',
1344 'contact_type' => 'Individual',
1345 'contact_sub_type' => 'Student',
1346 'api.Address.create' => [
1347 'street_address' => 'big house',
1348 'location_type_id' => 'Home',
1352 'first_name' => 'Michael',
1353 'last_name' => 'Jackson',
1354 'email' => 'michael@neverland.com',
1355 'contact_type' => 'Individual',
1356 'contact_sub_type' => 'Student',
1357 'api.Address.create' => [
1358 'street_address' => 'bigger house',
1359 'location_type_id' => 'Home',
1367 'first_name' => 'Michael',
1368 'last_name' => 'Jackson',
1369 'email' => 'michael@neverland.com',
1370 'contact_type' => 'Individual',
1371 'street_address' => 'big house',
1381 'first_name' => 'Michael',
1382 'last_name' => 'Jackson',
1383 'email' => 'michael@neverland.com',
1384 'contact_type' => 'Individual',
1385 'contact_sub_type' => 'Student',
1386 'api.Address.create' => [
1387 'street_address' => 'big house',
1388 'location_type_id' => 'Home',
1392 'first_name' => 'Michael',
1393 'last_name' => 'Jackson',
1394 'email' => 'michael@neverland.com',
1395 'contact_type' => 'Individual',
1396 'contact_sub_type' => 'Student',
1404 'first_name' => 'Michael',
1405 'last_name' => 'Jackson',
1406 'email' => 'michael@neverland.com',
1407 'contact_type' => 'Individual',
1411 'first_name' => 'Michael',
1412 'last_name' => 'Jackson',
1413 'email' => 'michael@neverland.com',
1414 'contact_type' => 'Individual',
1425 'first_name' => 'Michael',
1426 'last_name' => 'Jackson',
1427 'email' => 'michael@neverland.com',
1428 'contact_type' => 'Individual',
1429 'contact_sub_type' => 'Student',
1430 'api.Address.create' => [
1431 'street_address' => 'big house',
1432 'location_type_id' => 'Home',
1437 'first_name' => 'Michael',
1438 'last_name' => 'Jackson',
1439 'email' => 'michael@neverland.com',
1440 'contact_type' => 'Individual',
1441 'contact_sub_type' => 'Student',
1448 'first_name' => 'Michael',
1449 'last_name' => 'Jackson',
1450 'email' => 'michael@neverland.com',
1451 'contact_type' => 'Individual',
1455 'first_name' => 'Michael',
1456 'last_name' => 'Jackson',
1457 'email' => 'michael@neverland.com',
1458 'contact_type' => 'Individual',
1467 'first_name' => 'Dianna',
1468 'last_name' => 'McAndrew',
1469 'middle_name' => 'Prancer',
1470 'birth_date' => '2015-12-25',
1471 'gender_id' => 'Female',
1472 'job_title' => 'Thriller',
1475 foreach ($conflictPairs as $key => $value) {
1477 'first_name' => 'Michael',
1478 'middle_name' => 'Dancer',
1479 'last_name' => 'Jackson',
1480 'birth_date' => '2015-02-25',
1481 'email' => 'michael@neverland.com',
1482 'contact_type' => 'Individual',
1483 'contact_sub_type' => ['Student'],
1484 'gender_id' => 'Male',
1485 'job_title' => 'Entertainer',
1487 $contact2 = $contactParams;
1489 $contact2[$key] = $value;
1490 $data[$key . '_conflict'] = [
1493 'contacts' => [$contactParams, $contact2],
1496 'expected' => [$contactParams, $contact2],
1506 * @param string $objectName
1508 * @param array $params
1510 public function hookPreRelationship($op, $objectName, $id, &$params) {
1511 if ($op === 'delete') {
1514 if ($params['is_active']) {
1515 $params['description'] = 'Hooked';
1518 $params['description'] = 'Go Go you good thing';
1523 * Get the location data set.
1525 * @param array $locationParams1
1526 * @param array $locationParams2
1527 * @param string $entity
1528 * @param array $additionalExpected
1532 public function getMergeLocations($locationParams1, $locationParams2, $entity, $additionalExpected = []) {
1535 'matching_primary' => [
1536 'entity' => $entity,
1539 'location_type_id' => 'Main',
1541 ], $locationParams1),
1543 'location_type_id' => 'Work',
1545 ], $locationParams2),
1549 'location_type_id' => 'Main',
1551 ], $locationParams1),
1553 'expected' => array_merge($additionalExpected, [
1555 'location_type_id' => 'Main',
1557 ], $locationParams1),
1559 'location_type_id' => 'Work',
1561 ], $locationParams2),
1563 'expected_hook' => array_merge($additionalExpected, [
1565 'location_type_id' => 'Main',
1567 ], $locationParams1),
1569 'location_type_id' => 'Work',
1571 ], $locationParams2),
1576 'matching_primary_reverse' => [
1577 'entity' => $entity,
1580 'location_type_id' => 'Main',
1582 ], $locationParams1),
1586 'location_type_id' => 'Main',
1588 ], $locationParams1),
1590 'location_type_id' => 'Work',
1592 ], $locationParams2),
1594 'expected' => array_merge($additionalExpected, [
1596 'location_type_id' => 'Main',
1598 ], $locationParams1),
1600 'location_type_id' => 'Work',
1602 ], $locationParams2),
1604 'expected_hook' => array_merge($additionalExpected, [
1606 'location_type_id' => 'Main',
1608 ], $locationParams1),
1610 'location_type_id' => 'Work',
1612 ], $locationParams2),
1617 'only_one_has_address' => [
1618 'entity' => $entity,
1621 'location_type_id' => 'Main',
1623 ], $locationParams1),
1625 'location_type_id' => 'Work',
1627 ], $locationParams2),
1630 'expected' => array_merge($additionalExpected, [
1632 'location_type_id' => 'Main',
1634 ], $locationParams1),
1636 'location_type_id' => 'Work',
1638 ], $locationParams2),
1640 'expected_hook' => array_merge($additionalExpected, [
1642 'location_type_id' => 'Main',
1643 // When dealing with email we don't have a clean slate - the existing
1644 // primary will be primary.
1645 'is_primary' => ($entity == 'Email' ?
0 : 1),
1646 ], $locationParams1),
1648 'location_type_id' => 'Work',
1650 ], $locationParams2),
1655 'only_one_has_address_reverse' => [
1656 'description' => 'The destination contact does not have an address. secondary contact should be merged in.',
1657 'entity' => $entity,
1661 'location_type_id' => 'Main',
1663 ], $locationParams1),
1665 'location_type_id' => 'Work',
1667 ], $locationParams2),
1669 'expected' => array_merge($additionalExpected, [
1671 'location_type_id' => 'Main',
1672 // When dealing with email we don't have a clean slate - the existing
1673 // primary will be primary.
1674 'is_primary' => ($entity == 'Email' ?
0 : 1),
1675 ], $locationParams1),
1677 'location_type_id' => 'Work',
1679 ], $locationParams2),
1681 'expected_hook' => array_merge($additionalExpected, [
1683 'location_type_id' => 'Main',
1685 ], $locationParams1),
1687 'location_type_id' => 'Work',
1689 ], $locationParams2),
1694 'different_primaries_with_different_location_type' => [
1695 'description' => 'Primaries are different with different location. Keep both addresses. Set primary to be that of lower id',
1696 'entity' => $entity,
1699 'location_type_id' => 'Main',
1701 ], $locationParams1),
1705 'location_type_id' => 'Work',
1707 ], $locationParams2),
1709 'expected' => array_merge($additionalExpected, [
1711 'location_type_id' => 'Main',
1713 ], $locationParams1),
1715 'location_type_id' => 'Work',
1717 ], $locationParams2),
1719 'expected_hook' => array_merge($additionalExpected, [
1721 'location_type_id' => 'Main',
1723 ], $locationParams1),
1725 'location_type_id' => 'Work',
1727 ], $locationParams2),
1732 'different_primaries_with_different_location_type_reverse' => [
1733 'entity' => $entity,
1736 'location_type_id' => 'Work',
1738 ], $locationParams2),
1742 'location_type_id' => 'Main',
1744 ], $locationParams1),
1746 'expected' => array_merge($additionalExpected, [
1748 'location_type_id' => 'Work',
1750 ], $locationParams2),
1752 'location_type_id' => 'Main',
1754 ], $locationParams1),
1756 'expected_hook' => array_merge($additionalExpected, [
1758 'location_type_id' => 'Work',
1760 ], $locationParams2),
1762 'location_type_id' => 'Main',
1764 ], $locationParams1),
1769 'different_primaries_location_match_only_one_address' => [
1770 'entity' => $entity,
1773 'location_type_id' => 'Main',
1775 ], $locationParams1),
1777 'location_type_id' => 'Work',
1779 ], $locationParams2),
1783 'location_type_id' => 'Work',
1785 ], $locationParams2),
1788 'expected' => array_merge($additionalExpected, [
1790 'location_type_id' => 'Main',
1792 ], $locationParams1),
1794 'location_type_id' => 'Work',
1796 ], $locationParams2),
1798 'expected_hook' => array_merge($additionalExpected, [
1800 'location_type_id' => 'Main',
1802 ], $locationParams1),
1804 'location_type_id' => 'Work',
1806 ], $locationParams2),
1811 'different_primaries_location_match_only_one_address_reverse' => [
1812 'entity' => $entity,
1815 'location_type_id' => 'Work',
1817 ], $locationParams2),
1821 'location_type_id' => 'Main',
1823 ], $locationParams1),
1825 'location_type_id' => 'Work',
1827 ], $locationParams2),
1829 'expected' => array_merge($additionalExpected, [
1831 'location_type_id' => 'Work',
1833 ], $locationParams2),
1835 'location_type_id' => 'Main',
1837 ], $locationParams1),
1839 'expected_hook' => array_merge($additionalExpected, [
1841 'location_type_id' => 'Work',
1843 ], $locationParams2),
1845 'location_type_id' => 'Main',
1847 ], $locationParams1),
1852 'same_primaries_different_location' => [
1853 'entity' => $entity,
1856 'location_type_id' => 'Main',
1858 ], $locationParams1),
1862 'location_type_id' => 'Work',
1864 ], $locationParams1),
1867 'expected' => array_merge($additionalExpected, [
1869 'location_type_id' => 'Main',
1871 ], $locationParams1),
1873 'location_type_id' => 'Work',
1875 ], $locationParams1),
1877 'expected_hook' => array_merge($additionalExpected, [
1879 'location_type_id' => 'Work',
1881 ], $locationParams1),
1886 'same_primaries_different_location_reverse' => [
1887 'entity' => $entity,
1890 'location_type_id' => 'Work',
1892 ], $locationParams1),
1896 'location_type_id' => 'Main',
1898 ], $locationParams1),
1900 'expected' => array_merge($additionalExpected, [
1902 'location_type_id' => 'Work',
1904 ], $locationParams1),
1906 'location_type_id' => 'Main',
1908 ], $locationParams1),
1910 'expected_hook' => array_merge($additionalExpected, [
1912 'location_type_id' => 'Main',
1914 ], $locationParams1),
1923 * Test processing membership for deceased contacts.
1925 public function testProcessMembershipDeceased() {
1926 $this->callAPISuccess('Job', 'process_membership', []);
1927 $deadManWalkingID = $this->individualCreate();
1928 $membershipID = $this->contactMembershipCreate(['contact_id' => $deadManWalkingID]);
1929 $this->callAPISuccess('Contact', 'create', ['id' => $deadManWalkingID, 'is_deceased' => 1]);
1930 $this->callAPISuccess('Job', 'process_membership', []);
1931 $membership = $this->callAPISuccessGetSingle('Membership', ['id' => $membershipID]);
1932 $deceasedStatusId = CRM_Core_PseudoConstant
::getKey('CRM_Member_BAO_Membership', 'status_id', 'Deceased');
1933 $this->assertEquals($deceasedStatusId, $membership['status_id']);
1937 * Test we get an error is deceased status is disabled.
1939 * @throws \CRM_Core_Exception
1941 public function testProcessMembershipNoDeceasedStatus() {
1942 $deceasedStatusId = CRM_Core_PseudoConstant
::getKey('CRM_Member_BAO_Membership', 'status_id', 'Deceased');
1943 $this->callAPISuccess('MembershipStatus', 'create', ['is_active' => 0, 'id' => $deceasedStatusId]);
1944 CRM_Core_PseudoConstant
::flush();
1946 $deadManWalkingID = $this->individualCreate();
1947 $this->contactMembershipCreate(['contact_id' => $deadManWalkingID]);
1948 $this->callAPISuccess('Contact', 'create', ['id' => $deadManWalkingID, 'is_deceased' => 1]);
1949 $this->callAPIFailure('Job', 'process_membership', []);
1951 $this->callAPISuccess('MembershipStatus', 'create', ['is_active' => 1, 'id' => $deceasedStatusId]);
1955 * Test processing membership: check that status is updated when it should be
1956 * and left alone when it shouldn't.
1958 * @throws \CRM_Core_Exception
1959 * @throws \CiviCRM_API3_Exception
1961 public function testProcessMembershipUpdateStatus() {
1962 $this->ids
['MembershipType'] = $this->membershipTypeCreate();
1964 // Create admin-only membership status and get all statuses.
1965 $membershipStatusIdAdmin = $this->callAPISuccess('membership_status', 'create', ['name' => 'Admin', 'is_admin' => 1])['id'];
1967 // Create membership with incorrect statuses for the given dates and also some (pending, cancelled, admin override) which should not be updated.
1970 'start_date' => 'now',
1971 'end_date' => '+ 1 year',
1972 'initial_status' => 'Current',
1973 'expected_processed_status' => 'New',
1976 'start_date' => '- 6 month',
1977 'end_date' => '+ 6 month',
1978 'initial_status' => 'New',
1979 'expected_processed_status' => 'Current',
1982 'start_date' => '- 53 week',
1983 'end_date' => '-1 week',
1984 'initial_status' => 'Current',
1985 'expected_processed_status' => 'Grace',
1988 'start_date' => '- 16 month',
1989 'end_date' => '- 4 month',
1990 'initial_status' => 'Grace',
1991 'expected_processed_status' => 'Expired',
1994 'start_date' => 'now',
1995 'end_date' => '+ 1 year',
1996 'initial_status' => 'Pending',
1997 'expected_processed_status' => 'Pending',
2000 'start_date' => '- 6 month',
2001 'end_date' => '+ 6 month',
2002 'initial_status' => 'Cancelled',
2003 'expected_processed_status' => 'Cancelled',
2006 'start_date' => '- 16 month',
2007 'end_date' => '- 4 month',
2008 'initial_status' => 'Current',
2009 'is_override' => TRUE,
2010 'expected_processed_status' => 'Current',
2013 // @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.
2014 // 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.
2015 'start_date' => '- 16 month',
2016 'end_date' => '- 4 month',
2017 'initial_status' => 'Admin',
2018 'is_override' => TRUE,
2019 'expected_processed_status' => 'Admin',
2022 foreach ($memberships as $index => $membership) {
2023 $memberships[$index]['id'] = $this->createMembershipNeedingStatusProcessing($membership['start_date'], $membership['end_date'], $membership['initial_status'], $membership['is_override'] ??
FALSE);
2027 * Create membership type with inheritence and check processing of secondary memberships.
2029 $employerRelationshipId = $this->callAPISuccessGetValue('RelationshipType', [
2031 'name_b_a' => 'Employer Of',
2033 // Create membership type: inherited through employment.
2034 $membershipOrgId = $this->organizationCreate();
2036 'name' => 'Corporate Membership',
2037 'duration_unit' => 'year',
2038 'duration_interval' => 1,
2039 'period_type' => 'rolling',
2040 'member_of_contact_id' => $membershipOrgId,
2042 'financial_type_id' => 1,
2043 'relationship_type_id' => $employerRelationshipId,
2044 'relationship_direction' => 'b_a',
2047 $membershipTypeId = $this->callAPISuccess('membership_type', 'create', $params)['id'];
2049 // Create employer and first employee
2050 $employerId = $this->organizationCreate([], 1);
2051 $memberContactId = $this->individualCreate(['employer_id' => $employerId], 0);
2053 // Create inherited membership with incorrect status but dates implying status Expired.
2055 'contact_id' => $employerId,
2056 'membership_type_id' => $membershipTypeId,
2057 'source' => 'Test suite',
2058 'join_date' => date('Y-m-d', strtotime('now - 16 month')),
2059 'start_date' => date('Y-m-d', strtotime('now - 16 month')),
2060 'end_date' => date('Y-m-d', strtotime('now - 4 month')),
2061 // Intentionally incorrect status.
2062 'status_id' => 'Grace',
2063 // Don't calculate status.
2064 'skipStatusCal' => 1,
2066 $organizationMembershipID = $this->contactMembershipCreate($params);
2068 // Check that the employee inherited the membership and status.
2069 $expiredInheritedRelationship = $this->callAPISuccessGetSingle('membership', [
2070 'contact_id' => $memberContactId,
2071 'membership_type_id' => $membershipTypeId,
2073 $this->assertEquals($organizationMembershipID, $expiredInheritedRelationship['owner_membership_id']);
2074 $this->assertMembershipStatus('Grace', (int) $expiredInheritedRelationship['status_id']);
2076 // Reset static $relatedContactIds array in createRelatedMemberships(),
2077 // to avoid bug where inherited membership gets deleted.
2079 CRM_Member_BAO_Membership
::createRelatedMemberships($var, $var, TRUE);
2080 // Check that after running process_membership job, statuses are correct.
2081 $this->callAPISuccess('Job', 'process_membership', []);
2083 foreach ($memberships as $expectation) {
2084 $membership = $this->callAPISuccessGetSingle('membership', ['id' => $expectation['id']]);
2085 $this->assertMembershipStatus($expectation['expected_processed_status'], (int) $membership['status_id']);
2088 // Inherit Expired - should get updated.
2089 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $expiredInheritedRelationship['id']]);
2090 $this->assertMembershipStatus('Expired', $membership['status_id']);
2094 * Test procesing membership where is_override is set to 0 rather than NULL
2096 * @throws \CRM_Core_Exception
2098 public function testProcessMembershipIsOverrideNotNullNot1either() {
2099 $membershipTypeId = $this->membershipTypeCreate();
2101 // Create admin-only membership status and get all statuses.
2102 $result = $this->callAPISuccess('membership_status', 'create', ['name' => 'Admin', 'is_admin' => 1, 'sequential' => 1]);
2103 $membershipStatusIdAdmin = $result['values'][0]['id'];
2104 $memStatus = CRM_Member_PseudoConstant
::membershipStatus();
2106 // Default params, which we'll expand on below.
2108 'membership_type_id' => $membershipTypeId,
2109 // Don't calculate status.
2110 'skipStatusCal' => 1,
2115 // Create membership with incorrect status but dates implying status Current.
2116 $params['contact_id'] = $this->individualCreate();
2117 $params['join_date'] = date('Y-m-d', strtotime('now - 6 month'));
2118 $params['start_date'] = date('Y-m-d', strtotime('now - 6 month'));
2119 $params['end_date'] = date('Y-m-d', strtotime('now + 6 month'));
2120 // Intentionally incorrect status.
2121 $params['status_id'] = 'New';
2122 $resultCurrent = $this->callAPISuccess('Membership', 'create', $params);
2123 // Ensure that is_override is set to 0 by doing through DB given API not seem to accept id
2124 CRM_Core_DAO
::executeQuery("Update civicrm_membership SET is_override = 0 WHERE id = %1", [1 => [$resultCurrent['id'], 'Positive']]);
2125 $this->assertEquals(array_search('New', $memStatus), $resultCurrent['values'][0]['status_id']);
2126 $jobResult = $this->callAPISuccess('Job', 'process_membership', []);
2127 $this->assertEquals('Processed 1 membership records. Updated 1 records.', $jobResult['values']);
2128 $this->assertEquals(array_search('Current', $memStatus), $this->callAPISuccess('Membership', 'get', ['id' => $resultCurrent['id']])['values'][$resultCurrent['id']]['status_id']);
2132 * @param string $expectedStatusName
2133 * @param int $actualStatusID
2135 protected function assertMembershipStatus(string $expectedStatusName, int $actualStatusID) {
2136 $this->assertEquals($expectedStatusName, CRM_Core_PseudoConstant
::getName('CRM_Member_BAO_Membership', 'status_id', $actualStatusID));
2140 * @param string $startDate
2141 * Date in strtotime format - e.g 'now', '+1 day'
2142 * @param string $endDate
2143 * Date in strtotime format - e.g 'now', '+1 day'
2144 * @param string $status
2146 * @param bool $isAdminOverride
2147 * Is administratively overridden (if so the status is fixed).
2151 * @throws \CRM_Core_Exception
2153 protected function createMembershipNeedingStatusProcessing(string $startDate, string $endDate, string $status, bool $isAdminOverride = FALSE): int {
2155 'membership_type_id' => $this->ids
['MembershipType'],
2156 // Don't calculate status.
2157 'skipStatusCal' => 1,
2161 $params['contact_id'] = $this->individualCreate();
2162 $params['join_date'] = date('Y-m-d', strtotime($startDate));
2163 $params['start_date'] = date('Y-m-d', strtotime($startDate));
2164 $params['end_date'] = date('Y-m-d', strtotime($endDate));
2165 $params['sequential'] = TRUE;
2166 $params['is_override'] = $isAdminOverride;
2167 // Intentionally incorrect status.
2168 $params['status_id'] = $status;
2169 $resultNew = $this->callAPISuccess('Membership', 'create', $params);
2170 $this->assertMembershipStatus($status, (int) $resultNew['values'][0]['status_id']);
2171 return (int) $resultNew['id'];