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 weird characters don't mess with merge & cause a fatal.
834 * @throws \CRM_Core_Exception
836 public function testNoErrorOnOdd() {
837 $this->individualCreate();
838 $this->individualCreate(['first_name' => 'Gerrit%0a%2e%0a']);
839 $this->callAPISuccess('Job', 'process_batch_merge', []);
841 $this->individualCreate();
842 $this->individualCreate(['first_name' => '[foo\\bar\'baz']);
843 $this->callAPISuccess('Job', 'process_batch_merge', []);
844 $this->callAPISuccessGetSingle('Contact', ['first_name' => '[foo\\bar\'baz']);
848 * Test the batch merge does not create duplicate emails.
850 * Test CRM-18546, a 4.7 regression whereby a merged contact gets duplicate emails.
852 public function testBatchMergeEmailHandling() {
853 for ($x = 0; $x <= 4; $x++
) {
854 $id = $this->individualCreate(['email' => 'batman@gotham.met']);
856 $result = $this->callAPISuccess('Job', 'process_batch_merge', []);
857 $this->assertEquals(4, count($result['values']['merged']));
858 $this->callAPISuccessGetCount('Contact', ['email' => 'batman@gotham.met'], 1);
859 $contacts = $this->callAPISuccess('Contact', 'get', ['is_deleted' => 0]);
860 $deletedContacts = $this->callAPISuccess('Contact', 'get', ['is_deleted' => 1]);
861 $this->callAPISuccessGetCount('Email', [
862 'email' => 'batman@gotham.met',
863 'contact_id' => ['IN' => array_keys($contacts['values'])],
865 $this->callAPISuccessGetCount('Email', [
866 'email' => 'batman@gotham.met',
867 'contact_id' => ['IN' => array_keys($deletedContacts['values'])],
872 * Test the batch merge respects email "on hold".
874 * Test CRM-19148, Batch merge - Email on hold data lost when there is a conflict.
876 * @dataProvider getOnHoldSets
878 * @param bool $onHold1
879 * @param bool $onHold2
881 * @param string $conflictText
883 * @throws \CRM_Core_Exception
885 public function testBatchMergeEmailOnHold($onHold1, $onHold2, $merge, $conflictText) {
886 $this->individualCreate([
887 'api.email.create' => [
888 'email' => 'batman@gotham.met',
889 'location_type_id' => 'Work',
891 'on_hold' => $onHold1,
894 $this->individualCreate([
895 'api.email.create' => [
896 'email' => 'batman@gotham.met',
897 'location_type_id' => 'Work',
899 'on_hold' => $onHold2,
902 $result = $this->callAPISuccess('Job', 'process_batch_merge', []);
903 $this->assertCount($merge, $result['values']['merged']);
905 $defaultRuleGroupID = $this->callAPISuccessGetValue('RuleGroup', [
906 'contact_type' => 'Individual',
907 'used' => 'Unsupervised',
909 'options' => ['limit' => 1],
912 $duplicates = $this->callAPISuccess('Dedupe', 'getduplicates', ['rule_group_id' => $defaultRuleGroupID]);
913 $this->assertEquals($conflictText, $duplicates['values'][0]['conflicts']);
918 * Data provider for testBatchMergeEmailOnHold: combinations of on_hold & expected outcomes.
920 public function getOnHoldSets() {
921 // Each row specifies: contact 1 on_hold, contact 2 on_hold, merge? (0 or 1),
924 [0, 1, 0, "Email 2 (Work): 'batman@gotham.met' vs. 'batman@gotham.met\n(On Hold)'"],
925 [1, 0, 0, "Email 2 (Work): 'batman@gotham.met\n(On Hold)' vs. 'batman@gotham.met'"],
932 * Test the batch merge does not fatal on an empty rule.
934 * @dataProvider getRuleSets
936 * @param string $contactType
937 * @param string $used
938 * @param string $name
939 * @param bool $isReserved
940 * @param int $threshold
942 public function testBatchMergeEmptyRule($contactType, $used, $name, $isReserved, $threshold) {
943 $ruleGroup = $this->callAPISuccess('RuleGroup', 'create', [
944 'contact_type' => $contactType,
945 'threshold' => $threshold,
948 'is_reserved' => $isReserved,
950 $this->callAPISuccess('Job', 'process_batch_merge', ['rule_group_id' => $ruleGroup['id']]);
951 $this->callAPISuccess('RuleGroup', 'delete', ['id' => $ruleGroup['id']]);
955 * Get the various rule combinations.
957 public function getRuleSets() {
958 $contactTypes = ['Individual', 'Organization', 'Household'];
959 $useds = ['Unsupervised', 'General', 'Supervised'];
961 foreach ($contactTypes as $contactType) {
962 foreach ($useds as $used) {
963 $ruleGroups[] = [$contactType, $used, 'Bob', FALSE, 0];
964 $ruleGroups[] = [$contactType, $used, 'Bob', FALSE, 10];
965 $ruleGroups[] = [$contactType, $used, 'Bob', TRUE, 10];
966 $ruleGroups[] = [$contactType, $used, $contactType . $used, FALSE, 10];
967 $ruleGroups[] = [$contactType, $used, $contactType . $used, TRUE, 10];
974 * Test the batch merge does not create duplicate emails.
976 * Test CRM-18546, a 4.7 regression whereby a merged contact gets duplicate emails.
978 public function testBatchMergeMatchingAddress() {
979 for ($x = 0; $x <= 2; $x++
) {
980 $this->individualCreate([
981 'api.address.create' => [
982 'location_type_id' => 'Home',
983 'street_address' => 'Appt 115, The Batcave',
985 'postal_code' => 'Nananananana',
989 // Different location type, still merge, identical.
990 $this->individualCreate([
991 'api.address.create' => [
992 'location_type_id' => 'Main',
993 'street_address' => 'Appt 115, The Batcave',
995 'postal_code' => 'Nananananana',
999 $this->individualCreate([
1000 'api.address.create' => [
1001 'location_type_id' => 'Home',
1002 'street_address' => 'Appt 115, The Batcave',
1004 'postal_code' => 'Batman',
1008 $result = $this->callAPISuccess('Job', 'process_batch_merge', []);
1009 $this->assertEquals(3, count($result['values']['merged']));
1010 $this->assertEquals(1, count($result['values']['skipped']));
1011 $this->callAPISuccessGetCount('Contact', ['street_address' => 'Appt 115, The Batcave'], 2);
1012 $contacts = $this->callAPISuccess('Contact', 'get', ['is_deleted' => 0]);
1013 $deletedContacts = $this->callAPISuccess('Contact', 'get', ['is_deleted' => 1]);
1014 $this->callAPISuccessGetCount('Address', [
1015 'street_address' => 'Appt 115, The Batcave',
1016 'contact_id' => ['IN' => array_keys($contacts['values'])],
1019 $this->callAPISuccessGetCount('Address', [
1020 'street_address' => 'Appt 115, The Batcave',
1021 'contact_id' => ['IN' => array_keys($deletedContacts['values'])],
1026 * Test the batch merge by id range.
1028 * We have 2 sets of 5 matches & set the merge only to merge the lower set.
1030 public function testBatchMergeIDRange() {
1031 for ($x = 0; $x <= 4; $x++
) {
1032 $id = $this->individualCreate(['email' => 'batman@gotham.met']);
1034 for ($x = 0; $x <= 4; $x++
) {
1035 $this->individualCreate(['email' => 'robin@gotham.met']);
1037 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['criteria' => ['contact' => ['id' => ['<' => $id]]]]);
1038 $this->assertEquals(4, count($result['values']['merged']));
1039 $this->callAPISuccessGetCount('Contact', ['email' => 'batman@gotham.met'], 1);
1040 $this->callAPISuccessGetCount('Contact', ['email' => 'robin@gotham.met'], 5);
1041 $contacts = $this->callAPISuccess('Contact', 'get', ['is_deleted' => 0]);
1042 $deletedContacts = $this->callAPISuccess('Contact', 'get', ['is_deleted' => 0]);
1043 $this->callAPISuccessGetCount('Email', [
1044 'email' => 'batman@gotham.met',
1045 'contact_id' => ['IN' => array_keys($contacts['values'])],
1047 $this->callAPISuccessGetCount('Email', [
1048 'email' => 'batman@gotham.met',
1049 'contact_id' => ['IN' => array_keys($deletedContacts['values'])],
1051 $this->callAPISuccessGetCount('Email', [
1052 'email' => 'robin@gotham.met',
1053 'contact_id' => ['IN' => array_keys($contacts['values'])],
1059 * Test the batch merge copes with view only custom data field.
1061 public function testBatchMergeCustomDataViewOnlyField() {
1062 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviCRM', 'edit my contact'];
1063 $mouseParams = ['first_name' => 'Mickey', 'last_name' => 'Mouse', 'email' => 'tha_mouse@mouse.com'];
1064 $this->individualCreate($mouseParams);
1066 $customGroup = $this->CustomGroupCreate();
1067 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id'], 'is_view' => 1]);
1068 $this->individualCreate(array_merge($mouseParams, ['custom_' . $customField['id'] => 'blah']));
1070 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0, 'mode' => 'safe']);
1071 $this->assertEquals(1, count($result['values']['merged']));
1072 $mouseParams['return'] = 'custom_' . $customField['id'];
1073 $mouse = $this->callAPISuccess('Contact', 'getsingle', $mouseParams);
1074 $this->assertEquals('blah', $mouse['custom_' . $customField['id']]);
1076 $this->customFieldDelete($customField['id']);
1077 $this->customGroupDelete($customGroup['id']);
1081 * Test the batch merge retains 0 as a valid custom field value.
1083 * Note that we set 0 on 2 fields with one on each contact to ensure that
1084 * both merged & mergee fields are respected.
1086 * @throws \CRM_Core_Exception
1088 public function testBatchMergeCustomDataZeroValueField() {
1089 $customGroup = $this->customGroupCreate();
1090 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id'], 'default_value' => NULL]);
1092 $mouseParams = ['first_name' => 'Mickey', 'last_name' => 'Mouse', 'email' => 'tha_mouse@mouse.com'];
1093 $this->individualCreate(array_merge($mouseParams, ['custom_' . $customField['id'] => '']));
1094 $this->individualCreate(array_merge($mouseParams, ['custom_' . $customField['id'] => 0]));
1096 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0, 'mode' => 'safe']);
1097 $this->assertEquals(1, count($result['values']['merged']));
1098 $mouseParams['return'] = 'custom_' . $customField['id'];
1099 $mouse = $this->callAPISuccess('Contact', 'getsingle', $mouseParams);
1100 $this->assertEquals(0, $mouse['custom_' . $customField['id']]);
1102 $this->individualCreate(array_merge($mouseParams, ['custom_' . $customField['id'] => NULL]));
1103 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0, 'mode' => 'safe']);
1104 $this->assertEquals(1, count($result['values']['merged']));
1105 $mouseParams['return'] = 'custom_' . $customField['id'];
1106 $mouse = $this->callAPISuccess('Contact', 'getsingle', $mouseParams);
1107 $this->assertEquals(0, $mouse['custom_' . $customField['id']]);
1109 $this->customFieldDelete($customField['id']);
1110 $this->customGroupDelete($customGroup['id']);
1114 * Test the batch merge treats 0 vs 1 as a conflict.
1116 * @throws \CRM_Core_Exception
1118 public function testBatchMergeCustomDataZeroValueFieldWithConflict() {
1119 $customGroup = $this->customGroupCreate();
1120 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id'], 'default_value' => NULL]);
1122 $mouseParams = ['first_name' => 'Mickey', 'last_name' => 'Mouse', 'email' => 'tha_mouse@mouse.com'];
1123 $mouse1 = $this->individualCreate(array_merge($mouseParams, ['custom_' . $customField['id'] => 0]));
1124 $mouse2 = $this->individualCreate(array_merge($mouseParams, ['custom_' . $customField['id'] => 1]));
1126 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0, 'mode' => 'safe']);
1127 $this->assertEquals(0, count($result['values']['merged']));
1129 // Reverse which mouse has the zero to test we still get a conflict.
1130 $this->individualCreate(array_merge($mouseParams, ['id' => $mouse1, 'custom_' . $customField['id'] => 1]));
1131 $this->individualCreate(array_merge($mouseParams, ['id' => $mouse2, 'custom_' . $customField['id'] => 0]));
1132 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0, 'mode' => 'safe']);
1133 $this->assertEquals(0, count($result['values']['merged']));
1135 $this->customFieldDelete($customField['id']);
1136 $this->customGroupDelete($customGroup['id']);
1140 * Test the batch merge function actually works!
1142 * @dataProvider getMergeSets
1146 public function testBatchMergeWorksCheckPermissionsTrue($dataSet) {
1147 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviCRM', 'administer CiviCRM', 'merge duplicate contacts', 'force merge duplicate contacts'];
1148 foreach ($dataSet['contacts'] as $params) {
1149 $this->callAPISuccess('Contact', 'create', $params);
1152 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 1, 'mode' => $dataSet['mode']]);
1153 $this->assertEquals(0, count($result['values']['merged']), 'User does not have permission to any contacts, so no merging');
1154 $this->assertEquals(0, count($result['values']['skipped']), 'User does not have permission to any contacts, so no skip visibility');
1158 * Test the batch merge function actually works!
1160 * @dataProvider getMergeSets
1164 public function testBatchMergeWorksCheckPermissionsFalse($dataSet) {
1165 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviCRM', 'edit my contact'];
1166 foreach ($dataSet['contacts'] as $params) {
1167 $this->callAPISuccess('Contact', 'create', $params);
1170 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0, 'mode' => $dataSet['mode']]);
1171 $this->assertEquals($dataSet['skipped'], count($result['values']['skipped']), 'Failed to skip the right number:' . $dataSet['skipped']);
1172 $this->assertEquals($dataSet['merged'], count($result['values']['merged']));
1176 * Get data for batch merge.
1178 public function getMergeSets() {
1185 'first_name' => 'Michael',
1186 'last_name' => 'Jackson',
1187 'email' => 'michael@neverland.com',
1188 'contact_type' => 'Individual',
1189 'contact_sub_type' => 'Student',
1190 'api.Address.create' => [
1191 'street_address' => 'big house',
1192 'location_type_id' => 'Home',
1196 'first_name' => 'Michael',
1197 'last_name' => 'Jackson',
1198 'email' => 'michael@neverland.com',
1199 'contact_type' => 'Individual',
1200 'contact_sub_type' => 'Student',
1207 'first_name' => 'Michael',
1208 'last_name' => 'Jackson',
1209 'email' => 'michael@neverland.com',
1210 'contact_type' => 'Individual',
1220 'first_name' => 'Michael',
1221 'last_name' => 'Jackson',
1222 'email' => 'michael@neverland.com',
1223 'contact_type' => 'Individual',
1224 'contact_sub_type' => 'Student',
1225 'api.Address.create' => [
1226 'street_address' => 'big house',
1227 'location_type_id' => 'Home',
1231 'first_name' => 'Michael',
1232 'last_name' => 'Jackson',
1233 'email' => 'michael@neverland.com',
1234 'contact_type' => 'Individual',
1235 'contact_sub_type' => 'Student',
1236 'api.Address.create' => [
1237 'street_address' => 'bigger house',
1238 'location_type_id' => 'Home',
1246 'first_name' => 'Michael',
1247 'last_name' => 'Jackson',
1248 'email' => 'michael@neverland.com',
1249 'contact_type' => 'Individual',
1250 'street_address' => 'big house',
1253 'first_name' => 'Michael',
1254 'last_name' => 'Jackson',
1255 'email' => 'michael@neverland.com',
1256 'contact_type' => 'Individual',
1257 'street_address' => 'bigger house',
1267 'first_name' => 'Michael',
1268 'last_name' => 'Jackson',
1269 'email' => 'michael@neverland.com',
1270 'contact_type' => 'Individual',
1271 'contact_sub_type' => 'Student',
1272 'api.Email.create' => [
1273 'email' => 'big.slog@work.co.nz',
1274 'location_type_id' => 'Work',
1278 'first_name' => 'Michael',
1279 'last_name' => 'Jackson',
1280 'email' => 'michael@neverland.com',
1281 'contact_type' => 'Individual',
1282 'contact_sub_type' => 'Student',
1283 'api.Email.create' => [
1284 'email' => 'big.slog@work.com',
1285 'location_type_id' => 'Work',
1293 'first_name' => 'Michael',
1294 'last_name' => 'Jackson',
1295 'email' => 'michael@neverland.com',
1296 'contact_type' => 'Individual',
1299 'first_name' => 'Michael',
1300 'last_name' => 'Jackson',
1301 'email' => 'michael@neverland.com',
1302 'contact_type' => 'Individual',
1312 'first_name' => 'Michael',
1313 'last_name' => 'Jackson',
1314 'email' => 'michael@neverland.com',
1315 'contact_type' => 'Individual',
1316 'contact_sub_type' => 'Student',
1317 'api.Phone.create' => [
1318 'phone' => '123456',
1319 'location_type_id' => 'Work',
1323 'first_name' => 'Michael',
1324 'last_name' => 'Jackson',
1325 'email' => 'michael@neverland.com',
1326 'contact_type' => 'Individual',
1327 'contact_sub_type' => 'Student',
1328 'api.Phone.create' => [
1330 'location_type_id' => 'Work',
1338 'first_name' => 'Michael',
1339 'last_name' => 'Jackson',
1340 'email' => 'michael@neverland.com',
1341 'contact_type' => 'Individual',
1344 'first_name' => 'Michael',
1345 'last_name' => 'Jackson',
1346 'email' => 'michael@neverland.com',
1347 'contact_type' => 'Individual',
1354 'mode' => 'aggressive',
1357 'first_name' => 'Michael',
1358 'last_name' => 'Jackson',
1359 'email' => 'michael@neverland.com',
1360 'contact_type' => 'Individual',
1361 'contact_sub_type' => 'Student',
1362 'api.Address.create' => [
1363 'street_address' => 'big house',
1364 'location_type_id' => 'Home',
1368 'first_name' => 'Michael',
1369 'last_name' => 'Jackson',
1370 'email' => 'michael@neverland.com',
1371 'contact_type' => 'Individual',
1372 'contact_sub_type' => 'Student',
1373 'api.Address.create' => [
1374 'street_address' => 'bigger house',
1375 'location_type_id' => 'Home',
1383 'first_name' => 'Michael',
1384 'last_name' => 'Jackson',
1385 'email' => 'michael@neverland.com',
1386 'contact_type' => 'Individual',
1387 'street_address' => 'big house',
1397 'first_name' => 'Michael',
1398 'last_name' => 'Jackson',
1399 'email' => 'michael@neverland.com',
1400 'contact_type' => 'Individual',
1401 'contact_sub_type' => 'Student',
1402 'api.Address.create' => [
1403 'street_address' => 'big house',
1404 'location_type_id' => 'Home',
1408 'first_name' => 'Michael',
1409 'last_name' => 'Jackson',
1410 'email' => 'michael@neverland.com',
1411 'contact_type' => 'Individual',
1412 'contact_sub_type' => 'Student',
1420 'first_name' => 'Michael',
1421 'last_name' => 'Jackson',
1422 'email' => 'michael@neverland.com',
1423 'contact_type' => 'Individual',
1427 'first_name' => 'Michael',
1428 'last_name' => 'Jackson',
1429 'email' => 'michael@neverland.com',
1430 'contact_type' => 'Individual',
1441 'first_name' => 'Michael',
1442 'last_name' => 'Jackson',
1443 'email' => 'michael@neverland.com',
1444 'contact_type' => 'Individual',
1445 'contact_sub_type' => 'Student',
1446 'api.Address.create' => [
1447 'street_address' => 'big house',
1448 'location_type_id' => 'Home',
1453 'first_name' => 'Michael',
1454 'last_name' => 'Jackson',
1455 'email' => 'michael@neverland.com',
1456 'contact_type' => 'Individual',
1457 'contact_sub_type' => 'Student',
1464 'first_name' => 'Michael',
1465 'last_name' => 'Jackson',
1466 'email' => 'michael@neverland.com',
1467 'contact_type' => 'Individual',
1471 'first_name' => 'Michael',
1472 'last_name' => 'Jackson',
1473 'email' => 'michael@neverland.com',
1474 'contact_type' => 'Individual',
1483 'first_name' => 'Dianna',
1484 'last_name' => 'McAndrew',
1485 'middle_name' => 'Prancer',
1486 'birth_date' => '2015-12-25',
1487 'gender_id' => 'Female',
1488 'job_title' => 'Thriller',
1491 foreach ($conflictPairs as $key => $value) {
1493 'first_name' => 'Michael',
1494 'middle_name' => 'Dancer',
1495 'last_name' => 'Jackson',
1496 'birth_date' => '2015-02-25',
1497 'email' => 'michael@neverland.com',
1498 'contact_type' => 'Individual',
1499 'contact_sub_type' => ['Student'],
1500 'gender_id' => 'Male',
1501 'job_title' => 'Entertainer',
1503 $contact2 = $contactParams;
1505 $contact2[$key] = $value;
1506 $data[$key . '_conflict'] = [
1509 'contacts' => [$contactParams, $contact2],
1512 'expected' => [$contactParams, $contact2],
1522 * @param string $objectName
1524 * @param array $params
1526 public function hookPreRelationship($op, $objectName, $id, &$params) {
1527 if ($op === 'delete') {
1530 if ($params['is_active']) {
1531 $params['description'] = 'Hooked';
1534 $params['description'] = 'Go Go you good thing';
1539 * Get the location data set.
1541 * @param array $locationParams1
1542 * @param array $locationParams2
1543 * @param string $entity
1544 * @param array $additionalExpected
1548 public function getMergeLocations($locationParams1, $locationParams2, $entity, $additionalExpected = []) {
1551 'matching_primary' => [
1552 'entity' => $entity,
1555 'location_type_id' => 'Main',
1557 ], $locationParams1),
1559 'location_type_id' => 'Work',
1561 ], $locationParams2),
1565 'location_type_id' => 'Main',
1567 ], $locationParams1),
1569 'expected' => array_merge($additionalExpected, [
1571 'location_type_id' => 'Main',
1573 ], $locationParams1),
1575 'location_type_id' => 'Work',
1577 ], $locationParams2),
1579 'expected_hook' => array_merge($additionalExpected, [
1581 'location_type_id' => 'Main',
1583 ], $locationParams1),
1585 'location_type_id' => 'Work',
1587 ], $locationParams2),
1592 'matching_primary_reverse' => [
1593 'entity' => $entity,
1596 'location_type_id' => 'Main',
1598 ], $locationParams1),
1602 'location_type_id' => 'Main',
1604 ], $locationParams1),
1606 'location_type_id' => 'Work',
1608 ], $locationParams2),
1610 'expected' => array_merge($additionalExpected, [
1612 'location_type_id' => 'Main',
1614 ], $locationParams1),
1616 'location_type_id' => 'Work',
1618 ], $locationParams2),
1620 'expected_hook' => array_merge($additionalExpected, [
1622 'location_type_id' => 'Main',
1624 ], $locationParams1),
1626 'location_type_id' => 'Work',
1628 ], $locationParams2),
1633 'only_one_has_address' => [
1634 'entity' => $entity,
1637 'location_type_id' => 'Main',
1639 ], $locationParams1),
1641 'location_type_id' => 'Work',
1643 ], $locationParams2),
1646 'expected' => array_merge($additionalExpected, [
1648 'location_type_id' => 'Main',
1650 ], $locationParams1),
1652 'location_type_id' => 'Work',
1654 ], $locationParams2),
1656 'expected_hook' => array_merge($additionalExpected, [
1658 'location_type_id' => 'Main',
1659 // When dealing with email we don't have a clean slate - the existing
1660 // primary will be primary.
1661 'is_primary' => ($entity == 'Email' ?
0 : 1),
1662 ], $locationParams1),
1664 'location_type_id' => 'Work',
1666 ], $locationParams2),
1671 'only_one_has_address_reverse' => [
1672 'description' => 'The destination contact does not have an address. secondary contact should be merged in.',
1673 'entity' => $entity,
1677 'location_type_id' => 'Main',
1679 ], $locationParams1),
1681 'location_type_id' => 'Work',
1683 ], $locationParams2),
1685 'expected' => array_merge($additionalExpected, [
1687 'location_type_id' => 'Main',
1688 // When dealing with email we don't have a clean slate - the existing
1689 // primary will be primary.
1690 'is_primary' => ($entity == 'Email' ?
0 : 1),
1691 ], $locationParams1),
1693 'location_type_id' => 'Work',
1695 ], $locationParams2),
1697 'expected_hook' => array_merge($additionalExpected, [
1699 'location_type_id' => 'Main',
1701 ], $locationParams1),
1703 'location_type_id' => 'Work',
1705 ], $locationParams2),
1710 'different_primaries_with_different_location_type' => [
1711 'description' => 'Primaries are different with different location. Keep both addresses. Set primary to be that of lower id',
1712 'entity' => $entity,
1715 'location_type_id' => 'Main',
1717 ], $locationParams1),
1721 'location_type_id' => 'Work',
1723 ], $locationParams2),
1725 'expected' => array_merge($additionalExpected, [
1727 'location_type_id' => 'Main',
1729 ], $locationParams1),
1731 'location_type_id' => 'Work',
1733 ], $locationParams2),
1735 'expected_hook' => array_merge($additionalExpected, [
1737 'location_type_id' => 'Main',
1739 ], $locationParams1),
1741 'location_type_id' => 'Work',
1743 ], $locationParams2),
1748 'different_primaries_with_different_location_type_reverse' => [
1749 'entity' => $entity,
1752 'location_type_id' => 'Work',
1754 ], $locationParams2),
1758 'location_type_id' => 'Main',
1760 ], $locationParams1),
1762 'expected' => array_merge($additionalExpected, [
1764 'location_type_id' => 'Work',
1766 ], $locationParams2),
1768 'location_type_id' => 'Main',
1770 ], $locationParams1),
1772 'expected_hook' => array_merge($additionalExpected, [
1774 'location_type_id' => 'Work',
1776 ], $locationParams2),
1778 'location_type_id' => 'Main',
1780 ], $locationParams1),
1785 'different_primaries_location_match_only_one_address' => [
1786 'entity' => $entity,
1789 'location_type_id' => 'Main',
1791 ], $locationParams1),
1793 'location_type_id' => 'Work',
1795 ], $locationParams2),
1799 'location_type_id' => 'Work',
1801 ], $locationParams2),
1804 'expected' => array_merge($additionalExpected, [
1806 'location_type_id' => 'Main',
1808 ], $locationParams1),
1810 'location_type_id' => 'Work',
1812 ], $locationParams2),
1814 'expected_hook' => array_merge($additionalExpected, [
1816 'location_type_id' => 'Main',
1818 ], $locationParams1),
1820 'location_type_id' => 'Work',
1822 ], $locationParams2),
1827 'different_primaries_location_match_only_one_address_reverse' => [
1828 'entity' => $entity,
1831 'location_type_id' => 'Work',
1833 ], $locationParams2),
1837 'location_type_id' => 'Main',
1839 ], $locationParams1),
1841 'location_type_id' => 'Work',
1843 ], $locationParams2),
1845 'expected' => array_merge($additionalExpected, [
1847 'location_type_id' => 'Work',
1849 ], $locationParams2),
1851 'location_type_id' => 'Main',
1853 ], $locationParams1),
1855 'expected_hook' => array_merge($additionalExpected, [
1857 'location_type_id' => 'Work',
1859 ], $locationParams2),
1861 'location_type_id' => 'Main',
1863 ], $locationParams1),
1868 'same_primaries_different_location' => [
1869 'entity' => $entity,
1872 'location_type_id' => 'Main',
1874 ], $locationParams1),
1878 'location_type_id' => 'Work',
1880 ], $locationParams1),
1883 'expected' => array_merge($additionalExpected, [
1885 'location_type_id' => 'Main',
1887 ], $locationParams1),
1889 'location_type_id' => 'Work',
1891 ], $locationParams1),
1893 'expected_hook' => array_merge($additionalExpected, [
1895 'location_type_id' => 'Work',
1897 ], $locationParams1),
1902 'same_primaries_different_location_reverse' => [
1903 'entity' => $entity,
1906 'location_type_id' => 'Work',
1908 ], $locationParams1),
1912 'location_type_id' => 'Main',
1914 ], $locationParams1),
1916 'expected' => array_merge($additionalExpected, [
1918 'location_type_id' => 'Work',
1920 ], $locationParams1),
1922 'location_type_id' => 'Main',
1924 ], $locationParams1),
1926 'expected_hook' => array_merge($additionalExpected, [
1928 'location_type_id' => 'Main',
1930 ], $locationParams1),
1939 * Test processing membership for deceased contacts.
1941 public function testProcessMembershipDeceased() {
1942 $this->callAPISuccess('Job', 'process_membership', []);
1943 $deadManWalkingID = $this->individualCreate();
1944 $membershipID = $this->contactMembershipCreate(['contact_id' => $deadManWalkingID]);
1945 $this->callAPISuccess('Contact', 'create', ['id' => $deadManWalkingID, 'is_deceased' => 1]);
1946 $this->callAPISuccess('Job', 'process_membership', []);
1947 $membership = $this->callAPISuccessGetSingle('Membership', ['id' => $membershipID]);
1948 $deceasedStatusId = CRM_Core_PseudoConstant
::getKey('CRM_Member_BAO_Membership', 'status_id', 'Deceased');
1949 $this->assertEquals($deceasedStatusId, $membership['status_id']);
1953 * Test we get an error is deceased status is disabled.
1955 * @throws \CRM_Core_Exception
1957 public function testProcessMembershipNoDeceasedStatus() {
1958 $deceasedStatusId = CRM_Core_PseudoConstant
::getKey('CRM_Member_BAO_Membership', 'status_id', 'Deceased');
1959 $this->callAPISuccess('MembershipStatus', 'create', ['is_active' => 0, 'id' => $deceasedStatusId]);
1960 CRM_Core_PseudoConstant
::flush();
1962 $deadManWalkingID = $this->individualCreate();
1963 $this->contactMembershipCreate(['contact_id' => $deadManWalkingID]);
1964 $this->callAPISuccess('Contact', 'create', ['id' => $deadManWalkingID, 'is_deceased' => 1]);
1965 $this->callAPIFailure('Job', 'process_membership', []);
1967 $this->callAPISuccess('MembershipStatus', 'create', ['is_active' => 1, 'id' => $deceasedStatusId]);
1971 * Test processing membership: check that status is updated when it should be
1972 * and left alone when it shouldn't.
1974 * @throws \CRM_Core_Exception
1975 * @throws \CiviCRM_API3_Exception
1977 public function testProcessMembershipUpdateStatus() {
1978 $this->ids
['MembershipType'] = $this->membershipTypeCreate();
1980 // Create admin-only membership status and get all statuses.
1981 $membershipStatusIdAdmin = $this->callAPISuccess('membership_status', 'create', ['name' => 'Admin', 'is_admin' => 1])['id'];
1983 // Create membership with incorrect statuses for the given dates and also some (pending, cancelled, admin override) which should not be updated.
1986 'start_date' => 'now',
1987 'end_date' => '+ 1 year',
1988 'initial_status' => 'Current',
1989 'expected_processed_status' => 'New',
1992 'start_date' => '- 6 month',
1993 'end_date' => '+ 6 month',
1994 'initial_status' => 'New',
1995 'expected_processed_status' => 'Current',
1998 'start_date' => '- 53 week',
1999 'end_date' => '-1 week',
2000 'initial_status' => 'Current',
2001 'expected_processed_status' => 'Grace',
2004 'start_date' => '- 16 month',
2005 'end_date' => '- 4 month',
2006 'initial_status' => 'Grace',
2007 'expected_processed_status' => 'Expired',
2010 'start_date' => 'now',
2011 'end_date' => '+ 1 year',
2012 'initial_status' => 'Pending',
2013 'expected_processed_status' => 'Pending',
2016 'start_date' => '- 6 month',
2017 'end_date' => '+ 6 month',
2018 'initial_status' => 'Cancelled',
2019 'expected_processed_status' => 'Cancelled',
2022 'start_date' => '- 16 month',
2023 'end_date' => '- 4 month',
2024 'initial_status' => 'Current',
2025 'is_override' => TRUE,
2026 'expected_processed_status' => 'Current',
2029 // @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.
2030 // 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.
2031 'start_date' => '- 16 month',
2032 'end_date' => '- 4 month',
2033 'initial_status' => 'Admin',
2034 'is_override' => TRUE,
2035 'expected_processed_status' => 'Admin',
2038 foreach ($memberships as $index => $membership) {
2039 $memberships[$index]['id'] = $this->createMembershipNeedingStatusProcessing($membership['start_date'], $membership['end_date'], $membership['initial_status'], $membership['is_override'] ??
FALSE);
2043 * Create membership type with inheritence and check processing of secondary memberships.
2045 $employerRelationshipId = $this->callAPISuccessGetValue('RelationshipType', [
2047 'name_b_a' => 'Employer Of',
2049 // Create membership type: inherited through employment.
2050 $membershipOrgId = $this->organizationCreate();
2052 'name' => 'Corporate Membership',
2053 'duration_unit' => 'year',
2054 'duration_interval' => 1,
2055 'period_type' => 'rolling',
2056 'member_of_contact_id' => $membershipOrgId,
2058 'financial_type_id' => 1,
2059 'relationship_type_id' => $employerRelationshipId,
2060 'relationship_direction' => 'b_a',
2063 $membershipTypeId = $this->callAPISuccess('membership_type', 'create', $params)['id'];
2065 // Create employer and first employee
2066 $employerId = $this->organizationCreate([], 1);
2067 $memberContactId = $this->individualCreate(['employer_id' => $employerId], 0);
2069 // Create inherited membership with incorrect status but dates implying status Expired.
2071 'contact_id' => $employerId,
2072 'membership_type_id' => $membershipTypeId,
2073 'source' => 'Test suite',
2074 'join_date' => date('Y-m-d', strtotime('now - 16 month')),
2075 'start_date' => date('Y-m-d', strtotime('now - 16 month')),
2076 'end_date' => date('Y-m-d', strtotime('now - 4 month')),
2077 // Intentionally incorrect status.
2078 'status_id' => 'Grace',
2079 // Don't calculate status.
2080 'skipStatusCal' => 1,
2082 $organizationMembershipID = $this->contactMembershipCreate($params);
2084 // Check that the employee inherited the membership and status.
2085 $expiredInheritedRelationship = $this->callAPISuccessGetSingle('membership', [
2086 'contact_id' => $memberContactId,
2087 'membership_type_id' => $membershipTypeId,
2089 $this->assertEquals($organizationMembershipID, $expiredInheritedRelationship['owner_membership_id']);
2090 $this->assertMembershipStatus('Grace', (int) $expiredInheritedRelationship['status_id']);
2092 // Reset static $relatedContactIds array in createRelatedMemberships(),
2093 // to avoid bug where inherited membership gets deleted.
2095 CRM_Member_BAO_Membership
::createRelatedMemberships($var, $var, TRUE);
2096 // Check that after running process_membership job, statuses are correct.
2097 $this->callAPISuccess('Job', 'process_membership', []);
2099 foreach ($memberships as $expectation) {
2100 $membership = $this->callAPISuccessGetSingle('membership', ['id' => $expectation['id']]);
2101 $this->assertMembershipStatus($expectation['expected_processed_status'], (int) $membership['status_id']);
2104 // Inherit Expired - should get updated.
2105 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $expiredInheritedRelationship['id']]);
2106 $this->assertMembershipStatus('Expired', $membership['status_id']);
2110 * Test procesing membership where is_override is set to 0 rather than NULL
2112 * @throws \CRM_Core_Exception
2114 public function testProcessMembershipIsOverrideNotNullNot1either() {
2115 $membershipTypeId = $this->membershipTypeCreate();
2117 // Create admin-only membership status and get all statuses.
2118 $result = $this->callAPISuccess('membership_status', 'create', ['name' => 'Admin', 'is_admin' => 1, 'sequential' => 1]);
2119 $membershipStatusIdAdmin = $result['values'][0]['id'];
2120 $memStatus = CRM_Member_PseudoConstant
::membershipStatus();
2122 // Default params, which we'll expand on below.
2124 'membership_type_id' => $membershipTypeId,
2125 // Don't calculate status.
2126 'skipStatusCal' => 1,
2131 // Create membership with incorrect status but dates implying status Current.
2132 $params['contact_id'] = $this->individualCreate();
2133 $params['join_date'] = date('Y-m-d', strtotime('now - 6 month'));
2134 $params['start_date'] = date('Y-m-d', strtotime('now - 6 month'));
2135 $params['end_date'] = date('Y-m-d', strtotime('now + 6 month'));
2136 // Intentionally incorrect status.
2137 $params['status_id'] = 'New';
2138 $resultCurrent = $this->callAPISuccess('Membership', 'create', $params);
2139 // Ensure that is_override is set to 0 by doing through DB given API not seem to accept id
2140 CRM_Core_DAO
::executeQuery("Update civicrm_membership SET is_override = 0 WHERE id = %1", [1 => [$resultCurrent['id'], 'Positive']]);
2141 $this->assertEquals(array_search('New', $memStatus), $resultCurrent['values'][0]['status_id']);
2142 $jobResult = $this->callAPISuccess('Job', 'process_membership', []);
2143 $this->assertEquals('Processed 1 membership records. Updated 1 records.', $jobResult['values']);
2144 $this->assertEquals(array_search('Current', $memStatus), $this->callAPISuccess('Membership', 'get', ['id' => $resultCurrent['id']])['values'][$resultCurrent['id']]['status_id']);
2148 * @param string $expectedStatusName
2149 * @param int $actualStatusID
2151 protected function assertMembershipStatus(string $expectedStatusName, int $actualStatusID) {
2152 $this->assertEquals($expectedStatusName, CRM_Core_PseudoConstant
::getName('CRM_Member_BAO_Membership', 'status_id', $actualStatusID));
2156 * @param string $startDate
2157 * Date in strtotime format - e.g 'now', '+1 day'
2158 * @param string $endDate
2159 * Date in strtotime format - e.g 'now', '+1 day'
2160 * @param string $status
2162 * @param bool $isAdminOverride
2163 * Is administratively overridden (if so the status is fixed).
2167 * @throws \CRM_Core_Exception
2169 protected function createMembershipNeedingStatusProcessing(string $startDate, string $endDate, string $status, bool $isAdminOverride = FALSE): int {
2171 'membership_type_id' => $this->ids
['MembershipType'],
2172 // Don't calculate status.
2173 'skipStatusCal' => 1,
2177 $params['contact_id'] = $this->individualCreate();
2178 $params['join_date'] = date('Y-m-d', strtotime($startDate));
2179 $params['start_date'] = date('Y-m-d', strtotime($startDate));
2180 $params['end_date'] = date('Y-m-d', strtotime($endDate));
2181 $params['sequential'] = TRUE;
2182 $params['is_override'] = $isAdminOverride;
2183 // Intentionally incorrect status.
2184 $params['status_id'] = $status;
2185 $resultNew = $this->callAPISuccess('Membership', 'create', $params);
2186 $this->assertMembershipStatus($status, (int) $resultNew['values'][0]['status_id']);
2187 return (int) $resultNew['id'];