3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * File for the CiviCRM APIv3 job functions
31 * @package CiviCRM_APIv3
34 * @copyright CiviCRM LLC (c) 2004-2020
38 * Class api_v3_JobTest
42 class api_v3_JobTest
extends CiviUnitTestCase
{
44 public $DBResetRequired = FALSE;
46 public $_entity = 'Job';
51 * Created membership type.
53 * Must be created outside the transaction due to it breaking the transaction.
57 public $membershipTypeID;
62 public function setUp() {
64 $this->membershipTypeID
= $this->membershipTypeCreate(['name' => 'General']);
65 $this->useTransaction(TRUE);
68 'name' => 'API_Test_Job',
69 'description' => 'A long description written by hand in cursive',
70 'run_frequency' => 'Daily',
71 'api_entity' => 'ApiTestEntity',
72 'api_action' => 'apitestaction',
73 'parameters' => 'Semi-formal explanation of runtime job parameters',
78 public function tearDown() {
80 // The membershipType create breaks transactions so this extra cleanup is needed.
81 $this->membershipTypeDelete(['id' => $this->membershipTypeID
]);
82 $this->cleanUpSetUpIDs();
83 $this->quickCleanup(['civicrm_contact', 'civicrm_address', 'civicrm_email', 'civicrm_website', 'civicrm_phone'], TRUE);
90 public function testCreateWithoutName() {
94 $this->callAPIFailure('job', 'create', $params,
95 'Mandatory key(s) missing from params array: run_frequency, name, api_entity, api_action'
100 * Create job with an invalid "run_frequency" value.
102 public function testCreateWithInvalidFrequency() {
105 'name' => 'API_Test_Job',
106 'description' => 'A long description written by hand in cursive',
107 'run_frequency' => 'Fortnightly',
108 'api_entity' => 'ApiTestEntity',
109 'api_action' => 'apitestaction',
110 'parameters' => 'Semi-formal explanation of runtime job parameters',
113 $this->callAPIFailure('job', 'create', $params);
119 public function testCreate() {
120 $result = $this->callAPIAndDocument('job', 'create', $this->_params
, __FUNCTION__
, __FILE__
);
121 $this->assertNotNull($result['values'][0]['id']);
123 // mutate $params to match expected return value
124 unset($this->_params
['sequential']);
125 //assertDBState compares expected values in $result to actual values in the DB
126 $this->assertDBState('CRM_Core_DAO_Job', $result['id'], $this->_params
);
132 public function testClone() {
133 $createResult = $this->callAPISuccess('job', 'create', $this->_params
);
134 $params = ['id' => $createResult['id']];
135 $cloneResult = $this->callAPIAndDocument('job', 'clone', $params, __FUNCTION__
, __FILE__
);
136 $clonedJob = $cloneResult['values'][$cloneResult['id']];
137 $this->assertEquals($this->_params
['name'] . ' - Copy', $clonedJob['name']);
138 $this->assertEquals($this->_params
['description'], $clonedJob['description']);
139 $this->assertEquals($this->_params
['parameters'], $clonedJob['parameters']);
140 $this->assertEquals($this->_params
['is_active'], $clonedJob['is_active']);
141 $this->assertArrayNotHasKey('last_run', $clonedJob);
142 $this->assertArrayNotHasKey('scheduled_run_date', $clonedJob);
146 * Check if required fields are not passed.
148 public function testDeleteWithoutRequired() {
150 'name' => 'API_Test_PP',
151 'title' => 'API Test Payment Processor',
152 'class_name' => 'CRM_Core_Payment_APITest',
155 $result = $this->callAPIFailure('job', 'delete', $params);
156 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
160 * Check with incorrect required fields.
162 public function testDeleteWithIncorrectData() {
166 $this->callAPIFailure('job', 'delete', $params);
172 public function testDelete() {
173 $createResult = $this->callAPISuccess('job', 'create', $this->_params
);
174 $params = ['id' => $createResult['id']];
175 $this->callAPIAndDocument('job', 'delete', $params, __FUNCTION__
, __FILE__
);
176 $this->assertAPIDeleted($this->_entity
, $createResult['id']);
181 * public function testCallUpdateGreetingMissingParams() {
182 * $result = $this->callAPISuccess($this->_entity, 'update_greeting', array('gt' => 1));
183 * $this->assertEquals('Mandatory key(s) missing from params array: ct', $result['error_message']);
186 * public function testCallUpdateGreetingIncorrectParams() {
187 * $result = $this->callAPISuccess($this->_entity, 'update_greeting', array('gt' => 1, 'ct' => 'djkfhdskjfhds'));
188 * $this->assertEquals('ct `djkfhdskjfhds` is not valid.', $result['error_message']);
191 * Note that this test is about tesing the metadata / calling of the function & doesn't test the success of the called function
193 public function testCallUpdateGreetingSuccess() {
194 $this->callAPISuccess($this->_entity
, 'update_greeting', [
195 'gt' => 'postal_greeting',
196 'ct' => 'Individual',
200 public function testCallUpdateGreetingCommaSeparatedParamsSuccess() {
201 $gt = 'postal_greeting,email_greeting,addressee';
202 $ct = 'Individual,Household';
203 $this->callAPISuccess($this->_entity
, 'update_greeting', ['gt' => $gt, 'ct' => $ct]);
207 * Test the call reminder success sends more than 25 reminders & is not incorrectly limited.
209 * Note that this particular test sends the reminders to the additional recipients only
210 * as no real reminder person is configured
212 * Also note that this is testing a 'job' api so is in this class rather than scheduled_reminder - which
213 * seems a cleaner place to build up a collection of scheduled reminder testing functions. However, it seems
214 * 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
216 public function testCallSendReminderSuccessMoreThanDefaultLimit() {
217 $membershipTypeID = $this->membershipTypeCreate();
218 $this->membershipStatusCreate();
220 for ($i = 1; $i <= $createTotal; $i++
) {
221 $contactID = $this->individualCreate();
222 $groupID = $this->groupCreate(['name' => $i, 'title' => $i]);
223 $this->callAPISuccess('action_schedule', 'create', [
224 'title' => " job $i",
225 'subject' => "job $i",
226 'entity_value' => $membershipTypeID,
228 'start_action_date' => 'membership_join_date',
229 'start_action_offset' => 0,
230 'start_action_condition' => 'before',
231 'start_action_unit' => 'hour',
232 'group_id' => $groupID,
235 $this->callAPISuccess('group_contact', 'create', [
236 'contact_id' => $contactID,
238 'group_id' => $groupID,
241 $this->callAPISuccess('job', 'send_reminder', []);
242 $successfulCronCount = CRM_Core_DAO
::singleValueQuery("SELECT count(*) FROM civicrm_action_log");
243 $this->assertEquals($successfulCronCount, $createTotal);
247 * Test scheduled reminders respect limit to (since above identified addition_to handling issue).
249 * We create 3 contacts - 1 is in our group, 1 has our membership & the chosen one has both
250 * & check that only the chosen one got the reminder
252 public function testCallSendReminderLimitToSMS() {
253 $membershipTypeID = $this->membershipTypeCreate();
254 $this->membershipStatusCreate();
256 $groupID = $this->groupCreate(['name' => 'Texan drawlers', 'title' => 'a...']);
257 for ($i = 1; $i <= $createTotal; $i++
) {
258 $contactID = $this->individualCreate();
259 $this->callAPISuccess('Phone', 'create', [
260 'contact_id' => $contactID,
261 'phone' => '555 123 1234',
262 'phone_type_id' => 'Mobile',
263 'location_type_id' => 'Billing',
266 $theChosenOneID = $contactID;
269 $this->callAPISuccess('group_contact', 'create', [
270 'contact_id' => $contactID,
272 'group_id' => $groupID,
276 $this->callAPISuccess('membership', 'create', [
277 'contact_id' => $contactID,
278 'membership_type_id' => $membershipTypeID,
279 'join_date' => 'now',
280 'start_date' => '+ 1 day',
284 $this->setupForSmsTests();
285 $provider = civicrm_api3('SmsProvider', 'create', [
286 'name' => "CiviTestSMSProvider",
292 "api_params" => "a=1",
297 $this->callAPISuccess('action_schedule', 'create', [
298 'title' => " remind all Texans",
299 'subject' => "drawling renewal",
300 'entity_value' => $membershipTypeID,
302 'start_action_date' => 'membership_start_date',
303 'start_action_offset' => 1,
304 'start_action_condition' => 'before',
305 'start_action_unit' => 'day',
306 'group_id' => $groupID,
308 'sms_provider_id' => $provider['id'],
309 'mode' => 'User_Preference',
311 $this->callAPISuccess('job', 'send_reminder', []);
312 $successfulCronCount = CRM_Core_DAO
::singleValueQuery("SELECT count(*) FROM civicrm_action_log");
313 $this->assertEquals($successfulCronCount, 1);
314 $sentToID = CRM_Core_DAO
::singleValueQuery("SELECT contact_id FROM civicrm_action_log");
315 $this->assertEquals($sentToID, $theChosenOneID);
316 $this->assertEquals(0, CRM_Core_DAO
::singleValueQuery("SELECT is_error FROM civicrm_action_log"));
317 $this->setupForSmsTests(TRUE);
320 public function testCallDisableExpiredRelationships() {
321 $individualID = $this->individualCreate();
322 $orgID = $this->organizationCreate();
323 CRM_Utils_Hook_UnitTests
::singleton()->setHook('civicrm_pre', [$this, 'hookPreRelationship']);
324 $relationshipTypeID = $this->callAPISuccess('relationship_type', 'getvalue', [
326 'name_a_b' => 'Employee of',
328 $result = $this->callAPISuccess('relationship', 'create', [
329 'relationship_type_id' => $relationshipTypeID,
330 'contact_id_a' => $individualID,
331 'contact_id_b' => $orgID,
333 'end_date' => 'yesterday',
335 $relationshipID = $result['id'];
336 $this->assertEquals('Hooked', $result['values'][$relationshipID]['description']);
337 $this->callAPISuccess($this->_entity
, 'disable_expired_relationships', []);
338 $result = $this->callAPISuccess('relationship', 'get', []);
339 $this->assertEquals('Go Go you good thing', $result['values'][$relationshipID]['description']);
340 $this->contactDelete($individualID);
341 $this->contactDelete($orgID);
345 * Test scheduled reminders respect limit to (since above identified addition_to handling issue).
347 * We create 3 contacts - 1 is in our group, 1 has our membership & the chosen one has both
348 * & check that only the chosen one got the reminder
350 * Also check no hard fail on cron job with running a reminder that has a deleted SMS provider
352 public function testCallSendReminderLimitToSMSWithDeletedProviderr() {
353 $membershipTypeID = $this->membershipTypeCreate();
354 $this->membershipStatusCreate();
356 $groupID = $this->groupCreate(['name' => 'Texan drawlers', 'title' => 'a...']);
357 for ($i = 1; $i <= $createTotal; $i++
) {
358 $contactID = $this->individualCreate();
359 $this->callAPISuccess('Phone', 'create', [
360 'contact_id' => $contactID,
361 'phone' => '555 123 1234',
362 'phone_type_id' => 'Mobile',
363 'location_type_id' => 'Billing',
366 $theChosenOneID = $contactID;
369 $this->callAPISuccess('group_contact', 'create', [
370 'contact_id' => $contactID,
372 'group_id' => $groupID,
376 $this->callAPISuccess('membership', 'create', [
377 'contact_id' => $contactID,
378 'membership_type_id' => $membershipTypeID,
379 'join_date' => 'now',
380 'start_date' => '+ 1 day',
384 $this->setupForSmsTests();
385 $provider = civicrm_api3('SmsProvider', 'create', [
386 'name' => "CiviTestSMSProvider",
392 "api_params" => "a=1",
397 $this->callAPISuccess('action_schedule', 'create', [
398 'title' => " remind all Texans",
399 'subject' => "drawling renewal",
400 'entity_value' => $membershipTypeID,
402 'start_action_date' => 'membership_start_date',
403 'start_action_offset' => 1,
404 'start_action_condition' => 'before',
405 'start_action_unit' => 'day',
406 'group_id' => $groupID,
408 'sms_provider_id' => $provider['id'],
411 $this->callAPISuccess('SmsProvider', 'delete', ['id' => $provider['id']]);
412 $this->callAPISuccess('job', 'send_reminder', []);
413 $cronCount = CRM_Core_DAO
::singleValueQuery("SELECT count(*) FROM civicrm_action_log");
414 $this->assertEquals($cronCount, 1);
415 $sentToID = CRM_Core_DAO
::singleValueQuery("SELECT contact_id FROM civicrm_action_log");
416 $this->assertEquals($sentToID, $theChosenOneID);
417 $cronlog = CRM_Core_DAO
::executeQuery("SELECT * FROM civicrm_action_log")->fetchAll()[0];
418 $this->assertEquals(1, $cronlog['is_error']);
419 $this->assertEquals('SMS reminder cannot be sent because the SMS provider has been deleted.', $cronlog['message']);
420 $this->setupForSmsTests(TRUE);
424 * Test the batch merge function.
426 * We are just checking it returns without error here.
428 public function testBatchMerge() {
429 $this->callAPISuccess('Job', 'process_batch_merge', []);
433 * Test the batch merge function actually works!
435 * @dataProvider getMergeSets
439 public function testBatchMergeWorks($dataSet) {
440 foreach ($dataSet['contacts'] as $params) {
441 $this->callAPISuccess('Contact', 'create', $params);
444 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['mode' => $dataSet['mode']]);
445 $this->assertEquals($dataSet['skipped'], count($result['values']['skipped']), 'Failed to skip the right number:' . $dataSet['skipped']);
446 $this->assertEquals($dataSet['merged'], count($result['values']['merged']));
447 $result = $this->callAPISuccess('Contact', 'get', [
448 'contact_sub_type' => 'Student',
450 'is_deceased' => ['IN' => [0, 1]],
451 'options' => ['sort' => 'id ASC'],
453 $this->assertEquals(count($dataSet['expected']), $result['count']);
454 foreach ($dataSet['expected'] as $index => $contact) {
455 foreach ($contact as $key => $value) {
456 if ($key == 'gender_id') {
459 $this->assertEquals($value, $result['values'][$index][$key]);
465 * Check that the merge carries across various related entities.
467 * Note the group combinations & expected results:
469 public function testBatchMergeWithAssets() {
470 $contactID = $this->individualCreate();
471 $contact2ID = $this->individualCreate();
472 $this->contributionCreate(['contact_id' => $contactID]);
473 $this->contributionCreate(['contact_id' => $contact2ID, 'invoice_id' => '2', 'trxn_id' => 2]);
474 $this->contactMembershipCreate(['contact_id' => $contactID]);
475 $this->contactMembershipCreate(['contact_id' => $contact2ID]);
476 $this->activityCreate(['source_contact_id' => $contactID, 'target_contact_id' => $contactID, 'assignee_contact_id' => $contactID]);
477 $this->activityCreate(['source_contact_id' => $contact2ID, 'target_contact_id' => $contact2ID, 'assignee_contact_id' => $contact2ID]);
478 $this->tagCreate(['name' => 'Tall']);
479 $this->tagCreate(['name' => 'Short']);
480 $this->entityTagAdd(['contact_id' => $contactID, 'tag_id' => 'Tall']);
481 $this->entityTagAdd(['contact_id' => $contact2ID, 'tag_id' => 'Short']);
482 $this->entityTagAdd(['contact_id' => $contact2ID, 'tag_id' => 'Tall']);
483 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['mode' => 'safe']);
484 $this->assertEquals(0, count($result['values']['skipped']));
485 $this->assertEquals(1, count($result['values']['merged']));
486 $this->callAPISuccessGetCount('Contribution', ['contact_id' => $contactID], 2);
487 $this->callAPISuccessGetCount('Contribution', ['contact_id' => $contact2ID], 0);
488 $this->callAPISuccessGetCount('FinancialItem', ['contact_id' => $contactID], 2);
489 $this->callAPISuccessGetCount('FinancialItem', ['contact_id' => $contact2ID], 0);
490 $this->callAPISuccessGetCount('Membership', ['contact_id' => $contactID], 2);
491 $this->callAPISuccessGetCount('Membership', ['contact_id' => $contact2ID], 0);
492 $this->callAPISuccessGetCount('EntityTag', ['contact_id' => $contactID], 2);
493 $this->callAPISuccessGetCount('EntityTag', ['contact_id' => $contact2ID], 0);
494 // 14 activities is one for each contribution (2), two (source + target) for each membership (+(2x2) = 6)
495 // 3 for each of the added activities as there are 3 roles (+6 = 12
496 // 2 for the (source & target) contact merged activity (+2 = 14)
497 $this->callAPISuccessGetCount('ActivityContact', ['contact_id' => $contactID], 14);
498 // 2 for the connection to the deleted by merge activity (source & target)
499 $this->callAPISuccessGetCount('ActivityContact', ['contact_id' => $contact2ID], 2);
503 * Check that the merge carries across various related entities.
505 * Note the group combinations 'expected' results:
507 * Group 0 Added null Added
508 * Group 1 Added Added Added
509 * Group 2 Added Removed **** Added
510 * Group 3 Removed null **** null
511 * Group 4 Removed Added **** Added
512 * Group 5 Removed Removed **** null
513 * Group 6 null Added Added
514 * Group 7 null Removed **** null
516 * The ones with **** are the ones where I think a case could be made to change the behaviour.
518 public function testBatchMergeMergesGroups() {
519 $contactID = $this->individualCreate();
520 $contact2ID = $this->individualCreate();
522 for ($i = 0; $i < 8; $i++
) {
523 $groups[] = $this->groupCreate([
524 'name' => 'mergeGroup' . $i,
525 'title' => 'merge group' . $i,
529 $this->callAPISuccess('GroupContact', 'create', [
530 'contact_id' => $contactID,
531 'group_id' => $groups[0],
533 $this->callAPISuccess('GroupContact', 'create', [
534 'contact_id' => $contactID,
535 'group_id' => $groups[1],
537 $this->callAPISuccess('GroupContact', 'create', [
538 'contact_id' => $contactID,
539 'group_id' => $groups[2],
541 $this->callAPISuccess('GroupContact', 'create', [
542 'contact_id' => $contactID,
543 'group_id' => $groups[3],
544 'status' => 'Removed',
546 $this->callAPISuccess('GroupContact', 'create', [
547 'contact_id' => $contactID,
548 'group_id' => $groups[4],
549 'status' => 'Removed',
551 $this->callAPISuccess('GroupContact', 'create', [
552 'contact_id' => $contactID,
553 'group_id' => $groups[5],
554 'status' => 'Removed',
556 $this->callAPISuccess('GroupContact', 'create', [
557 'contact_id' => $contact2ID,
558 'group_id' => $groups[1],
560 $this->callAPISuccess('GroupContact', 'create', [
561 'contact_id' => $contact2ID,
562 'group_id' => $groups[2],
563 'status' => 'Removed',
565 $this->callAPISuccess('GroupContact', 'create', [
566 'contact_id' => $contact2ID,
567 'group_id' => $groups[4],
569 $this->callAPISuccess('GroupContact', 'create', [
570 'contact_id' => $contact2ID,
571 'group_id' => $groups[5],
572 'status' => 'Removed',
574 $this->callAPISuccess('GroupContact', 'create', [
575 'contact_id' => $contact2ID,
576 'group_id' => $groups[6],
578 $this->callAPISuccess('GroupContact', 'create', [
579 'contact_id' => $contact2ID,
580 'group_id' => $groups[7],
581 'status' => 'Removed',
583 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['mode' => 'safe']);
584 $this->assertEquals(0, count($result['values']['skipped']));
585 $this->assertEquals(1, count($result['values']['merged']));
586 $groupResult = $this->callAPISuccess('GroupContact', 'get', []);
587 $this->assertEquals(5, $groupResult['count']);
595 foreach ($groupResult['values'] as $groupValues) {
596 $this->assertEquals($contactID, $groupValues['contact_id']);
597 $this->assertEquals('Added', $groupValues['status']);
598 $this->assertTrue(in_array($groupValues['group_id'], $expectedGroups));
604 * Test the decisions made for addresses when merging.
606 * @dataProvider getMergeLocationData
609 * (the ones with **** could be disputed as whether it is the best outcome).
610 * 'matching_primary' - Primary matches, including location_type_id. One contact has an additional address.
611 * - result - primary is the shared one. Additional address is retained.
612 * 'matching_primary_reverse' - Primary matches, including location_type_id. Keep both. (opposite order)
613 * - result - primary is the shared one. Additional address is retained.
614 * 'only_one_has_address' - Only one contact has addresses (retain)
615 * - the (only) address is retained
616 * 'only_one_has_address_reverse'
617 * - the (only) address is retained
618 * 'different_primaries_with_different_location_type' Primaries are different but do not clash due to diff type
619 * - result - both addresses kept. The one from the kept (lowest ID) contact is primary
620 * 'different_primaries_with_different_location_type_reverse' Primaries are different but do not clash due to diff type
621 * - result - both addresses kept. The one from the kept (lowest ID) contact is primary
622 * 'different_primaries_location_match_only_one_address' per previous but a second address matches the primary but is not primary
623 * - result - both addresses kept. The one from the kept (lowest ID) contact is primary
624 * 'different_primaries_location_match_only_one_address_reverse' per previous but a second address matches the primary but is not primary
625 * - result - both addresses kept. The one from the kept (lowest ID) contact is primary
626 * 'same_primaries_different_location' Primary addresses are the same but have different location type IDs
627 * - result primary kept with the lowest ID. Other address retained too (to preserve location type info).
628 * 'same_primaries_different_location_reverse' Primary addresses are the same but have different location type IDs
629 * - result primary kept with the lowest ID. Other address retained too (to preserve location type info).
631 * @param array $dataSet
633 public function testBatchMergesAddresses($dataSet) {
634 $contactID1 = $this->individualCreate();
635 $contactID2 = $this->individualCreate();
636 foreach ($dataSet['contact_1'] as $address) {
637 $this->callAPISuccess($dataSet['entity'], 'create', array_merge(['contact_id' => $contactID1], $address));
639 foreach ($dataSet['contact_2'] as $address) {
640 $this->callAPISuccess($dataSet['entity'], 'create', array_merge(['contact_id' => $contactID2], $address));
643 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['mode' => 'safe']);
644 $this->assertEquals(1, count($result['values']['merged']));
645 $addresses = $this->callAPISuccess($dataSet['entity'], 'get', ['contact_id' => $contactID1, 'sequential' => 1]);
646 $this->assertEquals(count($dataSet['expected']), $addresses['count'], "Did not get the expected result for " . $dataSet['entity'] . (!empty($dataSet['description']) ?
" on dataset {$dataSet['description']}" : ''));
647 $locationTypes = $this->callAPISuccess($dataSet['entity'], 'getoptions', ['field' => 'location_type_id']);
648 foreach ($dataSet['expected'] as $index => $expectedAddress) {
649 foreach ($expectedAddress as $key => $value) {
650 if ($key == 'location_type_id') {
651 $this->assertEquals($locationTypes['values'][$addresses['values'][$index][$key]], $value);
654 $this->assertEquals($addresses['values'][$index][$key], $value, "mismatch on $key" . (!empty($dataSet['description']) ?
" on dataset {$dataSet['description']}" : ''));
661 * Test altering the address decision by hook.
663 * @dataProvider getMergeLocationData
665 * @param array $dataSet
667 public function testBatchMergesAddressesHook($dataSet) {
668 $contactID1 = $this->individualCreate();
669 $contactID2 = $this->individualCreate();
670 $this->contributionCreate(['contact_id' => $contactID1, 'receive_date' => '2010-01-01', 'invoice_id' => 1, 'trxn_id' => 1]);
671 $this->contributionCreate(['contact_id' => $contactID2, 'receive_date' => '2012-01-01', 'invoice_id' => 2, 'trxn_id' => 2]);
672 foreach ($dataSet['contact_1'] as $address) {
673 $this->callAPISuccess($dataSet['entity'], 'create', array_merge(['contact_id' => $contactID1], $address));
675 foreach ($dataSet['contact_2'] as $address) {
676 $this->callAPISuccess($dataSet['entity'], 'create', array_merge(['contact_id' => $contactID2], $address));
678 $this->hookClass
->setHook('civicrm_alterLocationMergeData', [$this, 'hookMostRecentDonor']);
680 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['mode' => 'safe']);
681 $this->assertEquals(1, count($result['values']['merged']));
682 $addresses = $this->callAPISuccess($dataSet['entity'], 'get', ['contact_id' => $contactID1, 'sequential' => 1]);
683 $this->assertEquals(count($dataSet['expected_hook']), $addresses['count']);
684 $locationTypes = $this->callAPISuccess($dataSet['entity'], 'getoptions', ['field' => 'location_type_id']);
685 foreach ($dataSet['expected_hook'] as $index => $expectedAddress) {
686 foreach ($expectedAddress as $key => $value) {
687 if ($key == 'location_type_id') {
688 $this->assertEquals($locationTypes['values'][$addresses['values'][$index][$key]], $value);
691 $this->assertEquals($value, $addresses['values'][$index][$key], $dataSet['entity'] . ': Unexpected value for ' . $key . (!empty($dataSet['description']) ?
" on dataset {$dataSet['description']}" : ''));
698 * Test the organization will not be matched to an individual.
700 public function testBatchMergeWillNotMergeOrganizationToIndividual() {
701 $individual = $this->callAPISuccess('Contact', 'create', [
702 'contact_type' => 'Individual',
703 'organization_name' => 'Anon',
704 'email' => 'anonymous@hacker.com',
706 $organization = $this->callAPISuccess('Contact', 'create', [
707 'contact_type' => 'Organization',
708 'organization_name' => 'Anon',
709 'email' => 'anonymous@hacker.com',
711 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['mode' => 'aggressive']);
712 $this->assertEquals(0, count($result['values']['skipped']));
713 $this->assertEquals(0, count($result['values']['merged']));
714 $this->callAPISuccessGetSingle('Contact', ['id' => $individual['id']]);
715 $this->callAPISuccessGetSingle('Contact', ['id' => $organization['id']]);
720 * Test hook allowing modification of the data calculated for merging locations.
722 * We are testing a nuanced real life situation where the address data of the
723 * most recent donor gets priority - resulting in the primary address being set
724 * to the primary address of the most recent donor and address data on a per
725 * location type basis also being set to the most recent donor. Hook also excludes
726 * a fully matching address with a different location.
728 * This has been added to the test suite to ensure the code supports more this
729 * type of intervention.
731 * @param array $blocksDAO
732 * Array of location DAO to be saved. These are arrays in 2 keys 'update' & 'delete'.
734 * Contact_id of the contact that survives the merge.
735 * @param int $otherId
736 * Contact_id of the contact that will be absorbed and deleted.
737 * @param array $migrationInfo
738 * Calculated migration info, informational only.
742 public function hookMostRecentDonor(&$blocksDAO, $mainId, $otherId, $migrationInfo) {
744 $lastDonorID = $this->callAPISuccessGetValue('Contribution', [
745 'return' => 'contact_id',
746 'contact_id' => ['IN' => [$mainId, $otherId]],
747 'options' => ['sort' => 'receive_date DESC', 'limit' => 1],
749 // Since the last donor is not the main ID we are prioritising info from the last donor.
750 // In the test this should always be true - but keep the check in case
751 // something changes that we need to detect.
752 if ($lastDonorID != $mainId) {
753 foreach ($migrationInfo['other_details']['location_blocks'] as $blockType => $blocks) {
754 foreach ($blocks as $block) {
755 if ($block['is_primary']) {
756 $primaryAddressID = $block['id'];
757 if (!empty($migrationInfo['main_details']['location_blocks'][$blockType])) {
758 foreach ($migrationInfo['main_details']['location_blocks'][$blockType] as $mainBlock) {
759 if (empty($blocksDAO[$blockType]['update'][$block['id']]) && $mainBlock['location_type_id'] == $block['location_type_id']) {
760 // This was an address match - we just need to check the is_primary
761 // is true on the matching kept address.
762 $primaryAddressID = $mainBlock['id'];
763 $blocksDAO[$blockType]['update'][$primaryAddressID] = _civicrm_api3_load_DAO($blockType);
764 $blocksDAO[$blockType]['update'][$primaryAddressID]->id
= $primaryAddressID;
766 $mainLocationTypeID = $mainBlock['location_type_id'];
767 // We also want to be more ruthless about removing matching addresses.
768 unset($mainBlock['location_type_id']);
769 if (CRM_Dedupe_Merger
::locationIsSame($block, $mainBlock)
770 && (!isset($blocksDAO[$blockType]['update']) ||
!isset($blocksDAO[$blockType]['update'][$mainBlock['id']]))
771 && (!isset($blocksDAO[$blockType]['delete']) ||
!isset($blocksDAO[$blockType]['delete'][$mainBlock['id']]))
773 $blocksDAO[$blockType]['delete'][$mainBlock['id']] = _civicrm_api3_load_DAO($blockType);
774 $blocksDAO[$blockType]['delete'][$mainBlock['id']]->id
= $mainBlock['id'];
776 // Arguably the right way to handle this is just to set is_primary for the primary
777 // and for the merge fn to call something like BAO::add & hooks to work etc.
778 // if that happens though this should keep working...
779 elseif ($mainBlock['is_primary'] && $mainLocationTypeID != $block['location_type_id']) {
780 $blocksDAO['address']['update'][$mainBlock['id']] = _civicrm_api3_load_DAO($blockType);
781 $blocksDAO['address']['update'][$mainBlock['id']]->is_primary
= 0;
782 $blocksDAO['address']['update'][$mainBlock['id']]->id
= $mainBlock['id'];
786 $blocksDAO[$blockType]['update'][$primaryAddressID]->is_primary
= 1;
795 * Get address combinations for the merge test.
799 public function getMergeLocationData() {
800 $address1 = ['street_address' => 'Buckingham Palace', 'city' => 'London'];
801 $address2 = ['street_address' => 'The Doghouse', 'supplemental_address_1' => 'under the blanket'];
802 $data = $this->getMergeLocations($address1, $address2, 'Address');
803 $data = array_merge($data, $this->getMergeLocations(['phone' => '12345', 'phone_type_id' => 1], ['phone' => '678910', 'phone_type_id' => 1], 'Phone'));
804 $data = array_merge($data, $this->getMergeLocations(['phone' => '12345'], ['phone' => '678910'], 'Phone'));
805 $data = array_merge($data, $this->getMergeLocations(['email' => 'mini@me.com'], ['email' => 'mini@me.org'], 'Email', [
807 'email' => 'anthony_anderson@civicrm.org',
808 'location_type_id' => 'Home',
816 * Test the batch merge does not create duplicate emails.
818 * Test CRM-18546, a 4.7 regression whereby a merged contact gets duplicate emails.
820 public function testBatchMergeEmailHandling() {
821 for ($x = 0; $x <= 4; $x++
) {
822 $id = $this->individualCreate(['email' => 'batman@gotham.met']);
824 $result = $this->callAPISuccess('Job', 'process_batch_merge', []);
825 $this->assertEquals(4, count($result['values']['merged']));
826 $this->callAPISuccessGetCount('Contact', ['email' => 'batman@gotham.met'], 1);
827 $contacts = $this->callAPISuccess('Contact', 'get', ['is_deleted' => 0]);
828 $deletedContacts = $this->callAPISuccess('Contact', 'get', ['is_deleted' => 1]);
829 $this->callAPISuccessGetCount('Email', [
830 'email' => 'batman@gotham.met',
831 'contact_id' => ['IN' => array_keys($contacts['values'])],
833 $this->callAPISuccessGetCount('Email', [
834 'email' => 'batman@gotham.met',
835 'contact_id' => ['IN' => array_keys($deletedContacts['values'])],
840 * Test the batch merge respects email "on hold".
842 * Test CRM-19148, Batch merge - Email on hold data lost when there is a conflict.
844 * @dataProvider getOnHoldSets
846 * @param bool $onHold1
847 * @param bool $onHold2
849 * @param string $conflictText
851 * @throws \CRM_Core_Exception
853 public function testBatchMergeEmailOnHold($onHold1, $onHold2, $merge, $conflictText) {
854 $this->individualCreate([
855 'api.email.create' => [
856 'email' => 'batman@gotham.met',
857 'location_type_id' => 'Work',
859 'on_hold' => $onHold1,
862 $this->individualCreate([
863 'api.email.create' => [
864 'email' => 'batman@gotham.met',
865 'location_type_id' => 'Work',
867 'on_hold' => $onHold2,
870 $result = $this->callAPISuccess('Job', 'process_batch_merge', []);
871 $this->assertCount($merge, $result['values']['merged']);
873 $defaultRuleGroupID = $this->callAPISuccessGetValue('RuleGroup', [
874 'contact_type' => 'Individual',
875 'used' => 'Unsupervised',
877 'options' => ['limit' => 1],
880 $duplicates = $this->callAPISuccess('Dedupe', 'getduplicates', ['rule_group_id' => $defaultRuleGroupID]);
881 $this->assertEquals($conflictText, $duplicates['values'][0]['conflicts']);
886 * Data provider for testBatchMergeEmailOnHold: combinations of on_hold & expected outcomes.
888 public function getOnHoldSets() {
889 // Each row specifies: contact 1 on_hold, contact 2 on_hold, merge? (0 or 1),
892 [0, 1, 0, "Email 2 (Work): 'batman@gotham.met' vs. 'batman@gotham.met\n(On Hold)'"],
893 [1, 0, 0, "Email 2 (Work): 'batman@gotham.met\n(On Hold)' vs. 'batman@gotham.met'"],
900 * Test the batch merge does not fatal on an empty rule.
902 * @dataProvider getRuleSets
904 * @param string $contactType
905 * @param string $used
906 * @param string $name
907 * @param bool $isReserved
908 * @param int $threshold
910 public function testBatchMergeEmptyRule($contactType, $used, $name, $isReserved, $threshold) {
911 $ruleGroup = $this->callAPISuccess('RuleGroup', 'create', [
912 'contact_type' => $contactType,
913 'threshold' => $threshold,
916 'is_reserved' => $isReserved,
918 $this->callAPISuccess('Job', 'process_batch_merge', ['rule_group_id' => $ruleGroup['id']]);
919 $this->callAPISuccess('RuleGroup', 'delete', ['id' => $ruleGroup['id']]);
923 * Get the various rule combinations.
925 public function getRuleSets() {
926 $contactTypes = ['Individual', 'Organization', 'Household'];
927 $useds = ['Unsupervised', 'General', 'Supervised'];
929 foreach ($contactTypes as $contactType) {
930 foreach ($useds as $used) {
931 $ruleGroups[] = [$contactType, $used, 'Bob', FALSE, 0];
932 $ruleGroups[] = [$contactType, $used, 'Bob', FALSE, 10];
933 $ruleGroups[] = [$contactType, $used, 'Bob', TRUE, 10];
934 $ruleGroups[] = [$contactType, $used, $contactType . $used, FALSE, 10];
935 $ruleGroups[] = [$contactType, $used, $contactType . $used, TRUE, 10];
942 * Test the batch merge does not create duplicate emails.
944 * Test CRM-18546, a 4.7 regression whereby a merged contact gets duplicate emails.
946 public function testBatchMergeMatchingAddress() {
947 for ($x = 0; $x <= 2; $x++
) {
948 $this->individualCreate([
949 'api.address.create' => [
950 'location_type_id' => 'Home',
951 'street_address' => 'Appt 115, The Batcave',
953 'postal_code' => 'Nananananana',
957 // Different location type, still merge, identical.
958 $this->individualCreate([
959 'api.address.create' => [
960 'location_type_id' => 'Main',
961 'street_address' => 'Appt 115, The Batcave',
963 'postal_code' => 'Nananananana',
967 $this->individualCreate([
968 'api.address.create' => [
969 'location_type_id' => 'Home',
970 'street_address' => 'Appt 115, The Batcave',
972 'postal_code' => 'Batman',
976 $result = $this->callAPISuccess('Job', 'process_batch_merge', []);
977 $this->assertEquals(3, count($result['values']['merged']));
978 $this->assertEquals(1, count($result['values']['skipped']));
979 $this->callAPISuccessGetCount('Contact', ['street_address' => 'Appt 115, The Batcave'], 2);
980 $contacts = $this->callAPISuccess('Contact', 'get', ['is_deleted' => 0]);
981 $deletedContacts = $this->callAPISuccess('Contact', 'get', ['is_deleted' => 1]);
982 $this->callAPISuccessGetCount('Address', [
983 'street_address' => 'Appt 115, The Batcave',
984 'contact_id' => ['IN' => array_keys($contacts['values'])],
987 $this->callAPISuccessGetCount('Address', [
988 'street_address' => 'Appt 115, The Batcave',
989 'contact_id' => ['IN' => array_keys($deletedContacts['values'])],
994 * Test the batch merge by id range.
996 * We have 2 sets of 5 matches & set the merge only to merge the lower set.
998 public function testBatchMergeIDRange() {
999 for ($x = 0; $x <= 4; $x++
) {
1000 $id = $this->individualCreate(['email' => 'batman@gotham.met']);
1002 for ($x = 0; $x <= 4; $x++
) {
1003 $this->individualCreate(['email' => 'robin@gotham.met']);
1005 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['criteria' => ['contact' => ['id' => ['<' => $id]]]]);
1006 $this->assertEquals(4, count($result['values']['merged']));
1007 $this->callAPISuccessGetCount('Contact', ['email' => 'batman@gotham.met'], 1);
1008 $this->callAPISuccessGetCount('Contact', ['email' => 'robin@gotham.met'], 5);
1009 $contacts = $this->callAPISuccess('Contact', 'get', ['is_deleted' => 0]);
1010 $deletedContacts = $this->callAPISuccess('Contact', 'get', ['is_deleted' => 0]);
1011 $this->callAPISuccessGetCount('Email', [
1012 'email' => 'batman@gotham.met',
1013 'contact_id' => ['IN' => array_keys($contacts['values'])],
1015 $this->callAPISuccessGetCount('Email', [
1016 'email' => 'batman@gotham.met',
1017 'contact_id' => ['IN' => array_keys($deletedContacts['values'])],
1019 $this->callAPISuccessGetCount('Email', [
1020 'email' => 'robin@gotham.met',
1021 'contact_id' => ['IN' => array_keys($contacts['values'])],
1027 * Test the batch merge copes with view only custom data field.
1029 public function testBatchMergeCustomDataViewOnlyField() {
1030 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviCRM', 'edit my contact'];
1031 $mouseParams = ['first_name' => 'Mickey', 'last_name' => 'Mouse', 'email' => 'tha_mouse@mouse.com'];
1032 $this->individualCreate($mouseParams);
1034 $customGroup = $this->CustomGroupCreate();
1035 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id'], 'is_view' => 1]);
1036 $this->individualCreate(array_merge($mouseParams, ['custom_' . $customField['id'] => 'blah']));
1038 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0, 'mode' => 'safe']);
1039 $this->assertEquals(1, count($result['values']['merged']));
1040 $mouseParams['return'] = 'custom_' . $customField['id'];
1041 $mouse = $this->callAPISuccess('Contact', 'getsingle', $mouseParams);
1042 $this->assertEquals('blah', $mouse['custom_' . $customField['id']]);
1044 $this->customFieldDelete($customField['id']);
1045 $this->customGroupDelete($customGroup['id']);
1049 * Test the batch merge retains 0 as a valid custom field value.
1051 * Note that we set 0 on 2 fields with one on each contact to ensure that
1052 * both merged & mergee fields are respected.
1054 * @throws \CRM_Core_Exception
1056 public function testBatchMergeCustomDataZeroValueField() {
1057 $customGroup = $this->customGroupCreate();
1058 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id'], 'default_value' => NULL]);
1060 $mouseParams = ['first_name' => 'Mickey', 'last_name' => 'Mouse', 'email' => 'tha_mouse@mouse.com'];
1061 $this->individualCreate(array_merge($mouseParams, ['custom_' . $customField['id'] => '']));
1062 $this->individualCreate(array_merge($mouseParams, ['custom_' . $customField['id'] => 0]));
1064 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0, 'mode' => 'safe']);
1065 $this->assertEquals(1, count($result['values']['merged']));
1066 $mouseParams['return'] = 'custom_' . $customField['id'];
1067 $mouse = $this->callAPISuccess('Contact', 'getsingle', $mouseParams);
1068 $this->assertEquals(0, $mouse['custom_' . $customField['id']]);
1070 $this->individualCreate(array_merge($mouseParams, ['custom_' . $customField['id'] => NULL]));
1071 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0, 'mode' => 'safe']);
1072 $this->assertEquals(1, count($result['values']['merged']));
1073 $mouseParams['return'] = 'custom_' . $customField['id'];
1074 $mouse = $this->callAPISuccess('Contact', 'getsingle', $mouseParams);
1075 $this->assertEquals(0, $mouse['custom_' . $customField['id']]);
1077 $this->customFieldDelete($customField['id']);
1078 $this->customGroupDelete($customGroup['id']);
1082 * Test the batch merge treats 0 vs 1 as a conflict.
1084 * @throws \CRM_Core_Exception
1086 public function testBatchMergeCustomDataZeroValueFieldWithConflict() {
1087 $customGroup = $this->customGroupCreate();
1088 $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id'], 'default_value' => NULL]);
1090 $mouseParams = ['first_name' => 'Mickey', 'last_name' => 'Mouse', 'email' => 'tha_mouse@mouse.com'];
1091 $mouse1 = $this->individualCreate(array_merge($mouseParams, ['custom_' . $customField['id'] => 0]));
1092 $mouse2 = $this->individualCreate(array_merge($mouseParams, ['custom_' . $customField['id'] => 1]));
1094 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0, 'mode' => 'safe']);
1095 $this->assertEquals(0, count($result['values']['merged']));
1097 // Reverse which mouse has the zero to test we still get a conflict.
1098 $this->individualCreate(array_merge($mouseParams, ['id' => $mouse1, 'custom_' . $customField['id'] => 1]));
1099 $this->individualCreate(array_merge($mouseParams, ['id' => $mouse2, 'custom_' . $customField['id'] => 0]));
1100 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0, 'mode' => 'safe']);
1101 $this->assertEquals(0, count($result['values']['merged']));
1103 $this->customFieldDelete($customField['id']);
1104 $this->customGroupDelete($customGroup['id']);
1108 * Test the batch merge function actually works!
1110 * @dataProvider getMergeSets
1114 public function testBatchMergeWorksCheckPermissionsTrue($dataSet) {
1115 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviCRM', 'administer CiviCRM', 'merge duplicate contacts', 'force merge duplicate contacts'];
1116 foreach ($dataSet['contacts'] as $params) {
1117 $this->callAPISuccess('Contact', 'create', $params);
1120 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 1, 'mode' => $dataSet['mode']]);
1121 $this->assertEquals(0, count($result['values']['merged']), 'User does not have permission to any contacts, so no merging');
1122 $this->assertEquals(0, count($result['values']['skipped']), 'User does not have permission to any contacts, so no skip visibility');
1126 * Test the batch merge function actually works!
1128 * @dataProvider getMergeSets
1132 public function testBatchMergeWorksCheckPermissionsFalse($dataSet) {
1133 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviCRM', 'edit my contact'];
1134 foreach ($dataSet['contacts'] as $params) {
1135 $this->callAPISuccess('Contact', 'create', $params);
1138 $result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0, 'mode' => $dataSet['mode']]);
1139 $this->assertEquals($dataSet['skipped'], count($result['values']['skipped']), 'Failed to skip the right number:' . $dataSet['skipped']);
1140 $this->assertEquals($dataSet['merged'], count($result['values']['merged']));
1144 * Get data for batch merge.
1146 public function getMergeSets() {
1153 'first_name' => 'Michael',
1154 'last_name' => 'Jackson',
1155 'email' => 'michael@neverland.com',
1156 'contact_type' => 'Individual',
1157 'contact_sub_type' => 'Student',
1158 'api.Address.create' => [
1159 'street_address' => 'big house',
1160 'location_type_id' => 'Home',
1164 'first_name' => 'Michael',
1165 'last_name' => 'Jackson',
1166 'email' => 'michael@neverland.com',
1167 'contact_type' => 'Individual',
1168 'contact_sub_type' => 'Student',
1175 'first_name' => 'Michael',
1176 'last_name' => 'Jackson',
1177 'email' => 'michael@neverland.com',
1178 'contact_type' => 'Individual',
1188 'first_name' => 'Michael',
1189 'last_name' => 'Jackson',
1190 'email' => 'michael@neverland.com',
1191 'contact_type' => 'Individual',
1192 'contact_sub_type' => 'Student',
1193 'api.Address.create' => [
1194 'street_address' => 'big house',
1195 'location_type_id' => 'Home',
1199 'first_name' => 'Michael',
1200 'last_name' => 'Jackson',
1201 'email' => 'michael@neverland.com',
1202 'contact_type' => 'Individual',
1203 'contact_sub_type' => 'Student',
1204 'api.Address.create' => [
1205 'street_address' => 'bigger house',
1206 'location_type_id' => 'Home',
1214 'first_name' => 'Michael',
1215 'last_name' => 'Jackson',
1216 'email' => 'michael@neverland.com',
1217 'contact_type' => 'Individual',
1218 'street_address' => 'big house',
1221 'first_name' => 'Michael',
1222 'last_name' => 'Jackson',
1223 'email' => 'michael@neverland.com',
1224 'contact_type' => 'Individual',
1225 'street_address' => 'bigger house',
1235 'first_name' => 'Michael',
1236 'last_name' => 'Jackson',
1237 'email' => 'michael@neverland.com',
1238 'contact_type' => 'Individual',
1239 'contact_sub_type' => 'Student',
1240 'api.Email.create' => [
1241 'email' => 'big.slog@work.co.nz',
1242 'location_type_id' => 'Work',
1246 'first_name' => 'Michael',
1247 'last_name' => 'Jackson',
1248 'email' => 'michael@neverland.com',
1249 'contact_type' => 'Individual',
1250 'contact_sub_type' => 'Student',
1251 'api.Email.create' => [
1252 'email' => 'big.slog@work.com',
1253 'location_type_id' => 'Work',
1261 'first_name' => 'Michael',
1262 'last_name' => 'Jackson',
1263 'email' => 'michael@neverland.com',
1264 'contact_type' => 'Individual',
1267 'first_name' => 'Michael',
1268 'last_name' => 'Jackson',
1269 'email' => 'michael@neverland.com',
1270 'contact_type' => 'Individual',
1280 'first_name' => 'Michael',
1281 'last_name' => 'Jackson',
1282 'email' => 'michael@neverland.com',
1283 'contact_type' => 'Individual',
1284 'contact_sub_type' => 'Student',
1285 'api.Phone.create' => [
1286 'phone' => '123456',
1287 'location_type_id' => 'Work',
1291 'first_name' => 'Michael',
1292 'last_name' => 'Jackson',
1293 'email' => 'michael@neverland.com',
1294 'contact_type' => 'Individual',
1295 'contact_sub_type' => 'Student',
1296 'api.Phone.create' => [
1298 'location_type_id' => 'Work',
1306 'first_name' => 'Michael',
1307 'last_name' => 'Jackson',
1308 'email' => 'michael@neverland.com',
1309 'contact_type' => 'Individual',
1312 'first_name' => 'Michael',
1313 'last_name' => 'Jackson',
1314 'email' => 'michael@neverland.com',
1315 'contact_type' => 'Individual',
1322 'mode' => 'aggressive',
1325 'first_name' => 'Michael',
1326 'last_name' => 'Jackson',
1327 'email' => 'michael@neverland.com',
1328 'contact_type' => 'Individual',
1329 'contact_sub_type' => 'Student',
1330 'api.Address.create' => [
1331 'street_address' => 'big house',
1332 'location_type_id' => 'Home',
1336 'first_name' => 'Michael',
1337 'last_name' => 'Jackson',
1338 'email' => 'michael@neverland.com',
1339 'contact_type' => 'Individual',
1340 'contact_sub_type' => 'Student',
1341 'api.Address.create' => [
1342 'street_address' => 'bigger house',
1343 'location_type_id' => 'Home',
1351 'first_name' => 'Michael',
1352 'last_name' => 'Jackson',
1353 'email' => 'michael@neverland.com',
1354 'contact_type' => 'Individual',
1355 'street_address' => 'big house',
1365 'first_name' => 'Michael',
1366 'last_name' => 'Jackson',
1367 'email' => 'michael@neverland.com',
1368 'contact_type' => 'Individual',
1369 'contact_sub_type' => 'Student',
1370 'api.Address.create' => [
1371 'street_address' => 'big house',
1372 'location_type_id' => 'Home',
1376 'first_name' => 'Michael',
1377 'last_name' => 'Jackson',
1378 'email' => 'michael@neverland.com',
1379 'contact_type' => 'Individual',
1380 'contact_sub_type' => 'Student',
1388 'first_name' => 'Michael',
1389 'last_name' => 'Jackson',
1390 'email' => 'michael@neverland.com',
1391 'contact_type' => 'Individual',
1395 'first_name' => 'Michael',
1396 'last_name' => 'Jackson',
1397 'email' => 'michael@neverland.com',
1398 'contact_type' => 'Individual',
1409 'first_name' => 'Michael',
1410 'last_name' => 'Jackson',
1411 'email' => 'michael@neverland.com',
1412 'contact_type' => 'Individual',
1413 'contact_sub_type' => 'Student',
1414 'api.Address.create' => [
1415 'street_address' => 'big house',
1416 'location_type_id' => 'Home',
1421 'first_name' => 'Michael',
1422 'last_name' => 'Jackson',
1423 'email' => 'michael@neverland.com',
1424 'contact_type' => 'Individual',
1425 'contact_sub_type' => 'Student',
1432 'first_name' => 'Michael',
1433 'last_name' => 'Jackson',
1434 'email' => 'michael@neverland.com',
1435 'contact_type' => 'Individual',
1439 'first_name' => 'Michael',
1440 'last_name' => 'Jackson',
1441 'email' => 'michael@neverland.com',
1442 'contact_type' => 'Individual',
1451 'first_name' => 'Dianna',
1452 'last_name' => 'McAndrew',
1453 'middle_name' => 'Prancer',
1454 'birth_date' => '2015-12-25',
1455 'gender_id' => 'Female',
1456 'job_title' => 'Thriller',
1459 foreach ($conflictPairs as $key => $value) {
1461 'first_name' => 'Michael',
1462 'middle_name' => 'Dancer',
1463 'last_name' => 'Jackson',
1464 'birth_date' => '2015-02-25',
1465 'email' => 'michael@neverland.com',
1466 'contact_type' => 'Individual',
1467 'contact_sub_type' => ['Student'],
1468 'gender_id' => 'Male',
1469 'job_title' => 'Entertainer',
1471 $contact2 = $contactParams;
1473 $contact2[$key] = $value;
1474 $data[$key . '_conflict'] = [
1477 'contacts' => [$contactParams, $contact2],
1480 'expected' => [$contactParams, $contact2],
1490 * @param string $objectName
1492 * @param array $params
1494 public function hookPreRelationship($op, $objectName, $id, &$params) {
1495 if ($op === 'delete') {
1498 if ($params['is_active']) {
1499 $params['description'] = 'Hooked';
1502 $params['description'] = 'Go Go you good thing';
1507 * Get the location data set.
1509 * @param array $locationParams1
1510 * @param array $locationParams2
1511 * @param string $entity
1512 * @param array $additionalExpected
1516 public function getMergeLocations($locationParams1, $locationParams2, $entity, $additionalExpected = []) {
1519 'matching_primary' => [
1520 'entity' => $entity,
1523 'location_type_id' => 'Main',
1525 ], $locationParams1),
1527 'location_type_id' => 'Work',
1529 ], $locationParams2),
1533 'location_type_id' => 'Main',
1535 ], $locationParams1),
1537 'expected' => array_merge($additionalExpected, [
1539 'location_type_id' => 'Main',
1541 ], $locationParams1),
1543 'location_type_id' => 'Work',
1545 ], $locationParams2),
1547 'expected_hook' => array_merge($additionalExpected, [
1549 'location_type_id' => 'Main',
1551 ], $locationParams1),
1553 'location_type_id' => 'Work',
1555 ], $locationParams2),
1560 'matching_primary_reverse' => [
1561 'entity' => $entity,
1564 'location_type_id' => 'Main',
1566 ], $locationParams1),
1570 'location_type_id' => 'Main',
1572 ], $locationParams1),
1574 'location_type_id' => 'Work',
1576 ], $locationParams2),
1578 'expected' => array_merge($additionalExpected, [
1580 'location_type_id' => 'Main',
1582 ], $locationParams1),
1584 'location_type_id' => 'Work',
1586 ], $locationParams2),
1588 'expected_hook' => array_merge($additionalExpected, [
1590 'location_type_id' => 'Main',
1592 ], $locationParams1),
1594 'location_type_id' => 'Work',
1596 ], $locationParams2),
1601 'only_one_has_address' => [
1602 'entity' => $entity,
1605 'location_type_id' => 'Main',
1607 ], $locationParams1),
1609 'location_type_id' => 'Work',
1611 ], $locationParams2),
1614 'expected' => array_merge($additionalExpected, [
1616 'location_type_id' => 'Main',
1618 ], $locationParams1),
1620 'location_type_id' => 'Work',
1622 ], $locationParams2),
1624 'expected_hook' => array_merge($additionalExpected, [
1626 'location_type_id' => 'Main',
1627 // When dealing with email we don't have a clean slate - the existing
1628 // primary will be primary.
1629 'is_primary' => ($entity == 'Email' ?
0 : 1),
1630 ], $locationParams1),
1632 'location_type_id' => 'Work',
1634 ], $locationParams2),
1639 'only_one_has_address_reverse' => [
1640 'description' => 'The destination contact does not have an address. secondary contact should be merged in.',
1641 'entity' => $entity,
1645 'location_type_id' => 'Main',
1647 ], $locationParams1),
1649 'location_type_id' => 'Work',
1651 ], $locationParams2),
1653 'expected' => array_merge($additionalExpected, [
1655 'location_type_id' => 'Main',
1656 // When dealing with email we don't have a clean slate - the existing
1657 // primary will be primary.
1658 'is_primary' => ($entity == 'Email' ?
0 : 1),
1659 ], $locationParams1),
1661 'location_type_id' => 'Work',
1663 ], $locationParams2),
1665 'expected_hook' => array_merge($additionalExpected, [
1667 'location_type_id' => 'Main',
1669 ], $locationParams1),
1671 'location_type_id' => 'Work',
1673 ], $locationParams2),
1678 'different_primaries_with_different_location_type' => [
1679 'description' => 'Primaries are different with different location. Keep both addresses. Set primary to be that of lower id',
1680 'entity' => $entity,
1683 'location_type_id' => 'Main',
1685 ], $locationParams1),
1689 'location_type_id' => 'Work',
1691 ], $locationParams2),
1693 'expected' => array_merge($additionalExpected, [
1695 'location_type_id' => 'Main',
1697 ], $locationParams1),
1699 'location_type_id' => 'Work',
1701 ], $locationParams2),
1703 'expected_hook' => array_merge($additionalExpected, [
1705 'location_type_id' => 'Main',
1707 ], $locationParams1),
1709 'location_type_id' => 'Work',
1711 ], $locationParams2),
1716 'different_primaries_with_different_location_type_reverse' => [
1717 'entity' => $entity,
1720 'location_type_id' => 'Work',
1722 ], $locationParams2),
1726 'location_type_id' => 'Main',
1728 ], $locationParams1),
1730 'expected' => array_merge($additionalExpected, [
1732 'location_type_id' => 'Work',
1734 ], $locationParams2),
1736 'location_type_id' => 'Main',
1738 ], $locationParams1),
1740 'expected_hook' => array_merge($additionalExpected, [
1742 'location_type_id' => 'Work',
1744 ], $locationParams2),
1746 'location_type_id' => 'Main',
1748 ], $locationParams1),
1753 'different_primaries_location_match_only_one_address' => [
1754 'entity' => $entity,
1757 'location_type_id' => 'Main',
1759 ], $locationParams1),
1761 'location_type_id' => 'Work',
1763 ], $locationParams2),
1767 'location_type_id' => 'Work',
1769 ], $locationParams2),
1772 'expected' => array_merge($additionalExpected, [
1774 'location_type_id' => 'Main',
1776 ], $locationParams1),
1778 'location_type_id' => 'Work',
1780 ], $locationParams2),
1782 'expected_hook' => array_merge($additionalExpected, [
1784 'location_type_id' => 'Main',
1786 ], $locationParams1),
1788 'location_type_id' => 'Work',
1790 ], $locationParams2),
1795 'different_primaries_location_match_only_one_address_reverse' => [
1796 'entity' => $entity,
1799 'location_type_id' => 'Work',
1801 ], $locationParams2),
1805 'location_type_id' => 'Main',
1807 ], $locationParams1),
1809 'location_type_id' => 'Work',
1811 ], $locationParams2),
1813 'expected' => array_merge($additionalExpected, [
1815 'location_type_id' => 'Work',
1817 ], $locationParams2),
1819 'location_type_id' => 'Main',
1821 ], $locationParams1),
1823 'expected_hook' => array_merge($additionalExpected, [
1825 'location_type_id' => 'Work',
1827 ], $locationParams2),
1829 'location_type_id' => 'Main',
1831 ], $locationParams1),
1836 'same_primaries_different_location' => [
1837 'entity' => $entity,
1840 'location_type_id' => 'Main',
1842 ], $locationParams1),
1846 'location_type_id' => 'Work',
1848 ], $locationParams1),
1851 'expected' => array_merge($additionalExpected, [
1853 'location_type_id' => 'Main',
1855 ], $locationParams1),
1857 'location_type_id' => 'Work',
1859 ], $locationParams1),
1861 'expected_hook' => array_merge($additionalExpected, [
1863 'location_type_id' => 'Work',
1865 ], $locationParams1),
1870 'same_primaries_different_location_reverse' => [
1871 'entity' => $entity,
1874 'location_type_id' => 'Work',
1876 ], $locationParams1),
1880 'location_type_id' => 'Main',
1882 ], $locationParams1),
1884 'expected' => array_merge($additionalExpected, [
1886 'location_type_id' => 'Work',
1888 ], $locationParams1),
1890 'location_type_id' => 'Main',
1892 ], $locationParams1),
1894 'expected_hook' => array_merge($additionalExpected, [
1896 'location_type_id' => 'Main',
1898 ], $locationParams1),
1907 * Test processing membership for deceased contacts.
1909 public function testProcessMembershipDeceased() {
1910 $this->callAPISuccess('Job', 'process_membership', []);
1911 $deadManWalkingID = $this->individualCreate();
1912 $membershipID = $this->contactMembershipCreate(['contact_id' => $deadManWalkingID]);
1913 $this->callAPISuccess('Contact', 'create', ['id' => $deadManWalkingID, 'is_deceased' => 1]);
1914 $this->callAPISuccess('Job', 'process_membership', []);
1915 $membership = $this->callAPISuccessGetSingle('Membership', ['id' => $membershipID]);
1916 $deceasedStatusId = CRM_Core_PseudoConstant
::getKey('CRM_Member_BAO_Membership', 'status_id', 'Deceased');
1917 $this->assertEquals($deceasedStatusId, $membership['status_id']);
1921 * Test we get an error is deceased status is disabled.
1923 public function testProcessMembershipNoDeceasedStatus() {
1924 $deceasedStatusId = CRM_Core_PseudoConstant
::getKey('CRM_Member_BAO_Membership', 'status_id', 'Deceased');
1925 $this->callAPISuccess('MembershipStatus', 'create', ['is_active' => 0, 'id' => $deceasedStatusId]);
1926 CRM_Core_PseudoConstant
::flush();
1928 $deadManWalkingID = $this->individualCreate();
1929 $this->contactMembershipCreate(['contact_id' => $deadManWalkingID]);
1930 $this->callAPISuccess('Contact', 'create', ['id' => $deadManWalkingID, 'is_deceased' => 1]);
1931 $this->callAPIFailure('Job', 'process_membership', []);
1933 $this->callAPISuccess('MembershipStatus', 'create', ['is_active' => 1, 'id' => $deceasedStatusId]);
1937 * Test processing membership: check that status is updated when it should be
1938 * and left alone when it shouldn't.
1940 public function testProcessMembershipUpdateStatus() {
1941 $membershipTypeId = $this->membershipTypeCreate();
1943 // Create admin-only membership status and get all statuses.
1944 $result = $this->callAPISuccess('membership_status', 'create', ['name' => 'Admin', 'is_admin' => 1, 'sequential' => 1]);
1945 $membershipStatusIdAdmin = $result['values'][0]['id'];
1946 $memStatus = CRM_Member_PseudoConstant
::membershipStatus();
1948 // Default params, which we'll expand on below.
1950 'membership_type_id' => $membershipTypeId,
1951 // Don't calculate status.
1952 'skipStatusCal' => 1,
1958 * We create various memberships with wrong status, then check that the
1959 * process_membership job sets the correct status for each.
1960 * Also some memberships that should not be updated.
1963 // Create membership with incorrect status but dates implying status New.
1964 $params['contact_id'] = $this->individualCreate();
1965 $params['join_date'] = date('Y-m-d');
1966 $params['start_date'] = date('Y-m-d');
1967 $params['end_date'] = date('Y-m-d', strtotime('now + 1 year'));
1968 // Intentionally incorrect status.
1969 $params['status_id'] = 'Current';
1970 $resultNew = $this->callAPISuccess('Membership', 'create', $params);
1971 $this->assertEquals(array_search('Current', $memStatus), $resultNew['values'][0]['status_id']);
1973 // Create membership with incorrect status but dates implying status Current.
1974 $params['contact_id'] = $this->individualCreate();
1975 $params['join_date'] = date('Y-m-d', strtotime('now - 6 month'));
1976 $params['start_date'] = date('Y-m-d', strtotime('now - 6 month'));
1977 $params['end_date'] = date('Y-m-d', strtotime('now + 6 month'));
1978 // Intentionally incorrect status.
1979 $params['status_id'] = 'New';
1980 $resultCurrent = $this->callAPISuccess('Membership', 'create', $params);
1981 $this->assertEquals(array_search('New', $memStatus), $resultCurrent['values'][0]['status_id']);
1983 // Create membership with incorrect status but dates implying status Grace.
1984 $params['contact_id'] = $this->individualCreate();
1985 $params['join_date'] = date('Y-m-d', strtotime('now - 53 week'));
1986 $params['start_date'] = date('Y-m-d', strtotime('now - 53 week'));
1987 $params['end_date'] = date('Y-m-d', strtotime('now - 1 week'));
1988 // Intentionally incorrect status.
1989 $params['status_id'] = 'Current';
1990 $resultGrace = $this->callAPISuccess('Membership', 'create', $params);
1991 $this->assertEquals(array_search('Current', $memStatus), $resultGrace['values'][0]['status_id']);
1993 // Create membership with incorrect status but dates implying status Expired.
1994 $params['contact_id'] = $this->individualCreate();
1995 $params['join_date'] = date('Y-m-d', strtotime('now - 16 month'));
1996 $params['start_date'] = date('Y-m-d', strtotime('now - 16 month'));
1997 $params['end_date'] = date('Y-m-d', strtotime('now - 4 month'));
1998 // Intentionally incorrect status.
1999 $params['status_id'] = 'Grace';
2000 $resultExpired = $this->callAPISuccess('Membership', 'create', $params);
2001 $this->assertEquals(array_search('Grace', $memStatus), $resultExpired['values'][0]['status_id']);
2003 // Create Pending membership with dates implying New: should not get updated.
2004 $params['contact_id'] = $this->individualCreate();
2005 $params['join_date'] = date('Y-m-d');
2006 $params['start_date'] = date('Y-m-d');
2007 $params['end_date'] = date('Y-m-d', strtotime('now + 1 year'));
2008 $params['status_id'] = 'Pending';
2009 $resultPending = $this->callAPISuccess('Membership', 'create', $params);
2010 $this->assertEquals(array_search('Pending', $memStatus), $resultPending['values'][0]['status_id']);
2012 // Create Cancelled membership with dates implying Current: should not get updated.
2013 $params['contact_id'] = $this->individualCreate();
2014 $params['join_date'] = date('Y-m-d', strtotime('now - 6 month'));
2015 $params['start_date'] = date('Y-m-d', strtotime('now - 6 month'));
2016 $params['end_date'] = date('Y-m-d', strtotime('now + 6 month'));
2017 $params['status_id'] = 'Cancelled';
2018 $resultCancelled = $this->callAPISuccess('Membership', 'create', $params);
2019 $this->assertEquals(array_search('Cancelled', $memStatus), $resultCancelled['values'][0]['status_id']);
2021 // Create status-overridden membership with dates implying Expired: should not get updated.
2022 $params['contact_id'] = $this->individualCreate();
2023 $params['join_date'] = date('Y-m-d', strtotime('now - 16 month'));
2024 $params['start_date'] = date('Y-m-d', strtotime('now - 16 month'));
2025 $params['end_date'] = date('Y-m-d', strtotime('now - 4 month'));
2026 $params['status_id'] = 'Current';
2027 $params['is_override'] = 1;
2028 $resultOverride = $this->callAPISuccess('Membership', 'create', $params);
2029 $this->assertEquals(array_search('Current', $memStatus), $resultOverride['values'][0]['status_id']);
2031 // Create membership with admin-only status but dates implying Expired: should not get updated.
2032 $params['contact_id'] = $this->individualCreate();
2033 $params['join_date'] = date('Y-m-d', strtotime('now - 16 month'));
2034 $params['start_date'] = date('Y-m-d', strtotime('now - 16 month'));
2035 $params['end_date'] = date('Y-m-d', strtotime('now - 4 month'));
2036 $params['status_id'] = $membershipStatusIdAdmin;
2037 $params['is_override'] = 1;
2038 $resultAdmin = $this->callAPISuccess('Membership', 'create', $params);
2039 $this->assertEquals($membershipStatusIdAdmin, $resultAdmin['values'][0]['status_id']);
2042 * Create membership type with inheritence and check processing of secondary memberships.
2044 $employerRelationshipId = $this->callAPISuccessGetValue('RelationshipType', [
2046 'name_b_a' => "Employer Of",
2048 // Create membership type: inherited through employment.
2049 $membershipOrgId = $this->organizationCreate();
2051 'name' => 'Corporate Membership',
2052 'duration_unit' => 'year',
2053 'duration_interval' => 1,
2054 'period_type' => 'rolling',
2055 'member_of_contact_id' => $membershipOrgId,
2057 'financial_type_id' => 1,
2058 'relationship_type_id' => $employerRelationshipId,
2059 'relationship_direction' => 'b_a',
2062 $result = $this->callAPISuccess('membership_type', 'create', $params);
2063 $membershipTypeId = $result['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.
2086 'contact_id' => $memberContactId,
2087 'membership_type_id' => $membershipTypeId,
2090 $resultInheritExpired = $this->callAPISuccess('membership', 'get', $params);
2091 $this->assertEquals(1, $resultInheritExpired['count']);
2092 $this->assertEquals($organizationMembershipID, $resultInheritExpired['values'][0]['owner_membership_id']);
2093 $this->assertEquals(array_search('Grace', $memStatus), $resultInheritExpired['values'][0]['status_id']);
2095 // Reset static $relatedContactIds array in createRelatedMemberships(),
2096 // to avoid bug where inherited membership gets deleted.
2098 CRM_Member_BAO_Membership
::createRelatedMemberships($var, $var, TRUE);
2100 // Check that after running process_membership job, statuses are correct.
2101 $this->callAPISuccess('Job', 'process_membership', []);
2103 // New - should get updated.
2104 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $resultNew['values'][0]['id']]);
2105 $this->assertEquals(array_search('New', $memStatus), $membership['status_id']);
2107 // Current - should get updated.
2108 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $resultCurrent['values'][0]['id']]);
2109 $this->assertEquals(array_search('Current', $memStatus), $membership['status_id']);
2111 // Grace - should get updated.
2112 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $resultGrace['values'][0]['id']]);
2113 $this->assertEquals(array_search('Grace', $memStatus), $membership['status_id']);
2115 // Expired - should get updated.
2116 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $resultExpired['values'][0]['id']]);
2117 $this->assertEquals(array_search('Expired', $memStatus), $membership['status_id']);
2119 // Pending - should not get updated.
2120 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $resultPending['values'][0]['id']]);
2121 $this->assertEquals(array_search('Pending', $memStatus), $membership['status_id']);
2123 // Cancelled - should not get updated.
2124 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $resultCancelled['values'][0]['id']]);
2125 $this->assertEquals(array_search('Cancelled', $memStatus), $membership['status_id']);
2127 // Override - should not get updated.
2128 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $resultOverride['values'][0]['id']]);
2129 $this->assertEquals(array_search('Current', $memStatus), $membership['status_id']);
2131 // Admin - should not get updated.
2132 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $resultAdmin['values'][0]['id']]);
2133 $this->assertEquals($membershipStatusIdAdmin, $membership['status_id']);
2135 // Inherit Expired - should get updated.
2136 $membership = $this->callAPISuccess('membership', 'getsingle', ['id' => $resultInheritExpired['values'][0]['id']]);
2137 $this->assertEquals(array_search('Expired', $memStatus), $membership['status_id']);
2141 * Test procesing membership where is_override is set to 0 rather than NULL
2143 * @throws \CRM_Core_Exception
2145 public function testProcessMembershipIsOverrideNotNullNot1either() {
2146 $membershipTypeId = $this->membershipTypeCreate();
2148 // Create admin-only membership status and get all statuses.
2149 $result = $this->callAPISuccess('membership_status', 'create', ['name' => 'Admin', 'is_admin' => 1, 'sequential' => 1]);
2150 $membershipStatusIdAdmin = $result['values'][0]['id'];
2151 $memStatus = CRM_Member_PseudoConstant
::membershipStatus();
2153 // Default params, which we'll expand on below.
2155 'membership_type_id' => $membershipTypeId,
2156 // Don't calculate status.
2157 'skipStatusCal' => 1,
2162 // Create membership with incorrect status but dates implying status Current.
2163 $params['contact_id'] = $this->individualCreate();
2164 $params['join_date'] = date('Y-m-d', strtotime('now - 6 month'));
2165 $params['start_date'] = date('Y-m-d', strtotime('now - 6 month'));
2166 $params['end_date'] = date('Y-m-d', strtotime('now + 6 month'));
2167 // Intentionally incorrect status.
2168 $params['status_id'] = 'New';
2169 $resultCurrent = $this->callAPISuccess('Membership', 'create', $params);
2170 // Ensure that is_override is set to 0 by doing through DB given API not seem to accept id
2171 CRM_Core_DAO
::executeQuery("Update civicrm_membership SET is_override = 0 WHERE id = %1", [1 => [$resultCurrent['id'], 'Positive']]);
2172 $this->assertEquals(array_search('New', $memStatus), $resultCurrent['values'][0]['status_id']);
2173 $jobResult = $this->callAPISuccess('Job', 'process_membership', []);
2174 $this->assertEquals('Processed 1 membership records. Updated 1 records.', $jobResult['values']);
2175 $this->assertEquals(array_search('Current', $memStatus), $this->callAPISuccess('Membership', 'get', ['id' => $resultCurrent['id']])['values'][$resultCurrent['id']]['status_id']);