3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
38 * Class api_v3_JobTest
41 class api_v3_JobTest
extends CiviUnitTestCase
{
42 protected $_apiversion = 3;
44 public $DBResetRequired = FALSE;
45 public $_entity = 'Job';
46 public $_params = array();
48 * Created membership type.
50 * Must be created outside the transaction due to it breaking the transaction.
54 public $membershipTypeID;
59 public function setUp() {
61 $this->membershipTypeID
= $this->membershipTypeCreate(array('name' => 'General'));
62 $this->useTransaction(TRUE);
63 $this->_params
= array(
65 'name' => 'API_Test_Job',
66 'description' => 'A long description written by hand in cursive',
67 'run_frequency' => 'Daily',
68 'api_entity' => 'ApiTestEntity',
69 'api_action' => 'apitestaction',
70 'parameters' => 'Semi-formal explanation of runtime job parameters',
75 public function tearDown() {
77 // The membershipType create breaks transactions so this extra cleanup is needed.
78 $this->membershipTypeDelete(array('id' => $this->membershipTypeID
));
79 $this->cleanUpSetUpIDs();
85 public function testCreateWithoutName() {
89 $this->callAPIFailure('job', 'create', $params,
90 'Mandatory key(s) missing from params array: run_frequency, name, api_entity, api_action'
95 * Create job with an invalid "run_frequency" value.
97 public function testCreateWithInvalidFrequency() {
100 'name' => 'API_Test_Job',
101 'description' => 'A long description written by hand in cursive',
102 'run_frequency' => 'Fortnightly',
103 'api_entity' => 'ApiTestEntity',
104 'api_action' => 'apitestaction',
105 'parameters' => 'Semi-formal explanation of runtime job parameters',
108 $this->callAPIFailure('job', 'create', $params);
114 public function testCreate() {
115 $result = $this->callAPIAndDocument('job', 'create', $this->_params
, __FUNCTION__
, __FILE__
);
116 $this->assertNotNull($result['values'][0]['id']);
118 // mutate $params to match expected return value
119 unset($this->_params
['sequential']);
120 //assertDBState compares expected values in $result to actual values in the DB
121 $this->assertDBState('CRM_Core_DAO_Job', $result['id'], $this->_params
);
125 * Check if required fields are not passed.
127 public function testDeleteWithoutRequired() {
129 'name' => 'API_Test_PP',
130 'title' => 'API Test Payment Processor',
131 'class_name' => 'CRM_Core_Payment_APITest',
134 $result = $this->callAPIFailure('job', 'delete', $params);
135 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
139 * Check with incorrect required fields.
141 public function testDeleteWithIncorrectData() {
145 $this->callAPIFailure('job', 'delete', $params);
151 public function testDelete() {
152 $createResult = $this->callAPISuccess('job', 'create', $this->_params
);
153 $params = array('id' => $createResult['id']);
154 $this->callAPIAndDocument('job', 'delete', $params, __FUNCTION__
, __FILE__
);
155 $this->assertAPIDeleted($this->_entity
, $createResult['id']);
160 * public function testCallUpdateGreetingMissingParams() {
161 * $result = $this->callAPISuccess($this->_entity, 'update_greeting', array('gt' => 1));
162 * $this->assertEquals('Mandatory key(s) missing from params array: ct', $result['error_message']);
165 * public function testCallUpdateGreetingIncorrectParams() {
166 * $result = $this->callAPISuccess($this->_entity, 'update_greeting', array('gt' => 1, 'ct' => 'djkfhdskjfhds'));
167 * $this->assertEquals('ct `djkfhdskjfhds` is not valid.', $result['error_message']);
170 * Note that this test is about tesing the metadata / calling of the function & doesn't test the success of the called function
172 public function testCallUpdateGreetingSuccess() {
173 $this->callAPISuccess($this->_entity
, 'update_greeting', array(
174 'gt' => 'postal_greeting',
175 'ct' => 'Individual',
179 public function testCallUpdateGreetingCommaSeparatedParamsSuccess() {
180 $gt = 'postal_greeting,email_greeting,addressee';
181 $ct = 'Individual,Household';
182 $this->callAPISuccess($this->_entity
, 'update_greeting', array('gt' => $gt, 'ct' => $ct));
186 * Test the call reminder success sends more than 25 reminders & is not incorrectly limited.
188 * Note that this particular test sends the reminders to the additional recipients only
189 * as no real reminder person is configured
191 * Also note that this is testing a 'job' api so is in this class rather than scheduled_reminder - which
192 * seems a cleaner place to build up a collection of scheduled reminder testing functions. However, it seems
193 * 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
195 public function testCallSendReminderSuccessMoreThanDefaultLimit() {
196 $membershipTypeID = $this->membershipTypeCreate();
197 $this->membershipStatusCreate();
199 for ($i = 1; $i <= $createTotal; $i++
) {
200 $contactID = $this->individualCreate();
201 $groupID = $this->groupCreate(array('name' => $i, 'title' => $i));
202 $this->callAPISuccess('action_schedule', 'create', array(
203 'title' => " job $i",
204 'subject' => "job $i",
205 'entity_value' => $membershipTypeID,
207 'start_action_date' => 'membership_join_date',
208 'start_action_offset' => 0,
209 'start_action_condition' => 'before',
210 'start_action_unit' => 'hour',
211 'group_id' => $groupID,
214 $this->callAPISuccess('group_contact', 'create', array(
215 'contact_id' => $contactID,
217 'group_id' => $groupID,
220 $this->callAPISuccess('job', 'send_reminder', array());
221 $successfulCronCount = CRM_Core_DAO
::singleValueQuery("SELECT count(*) FROM civicrm_action_log");
222 $this->assertEquals($successfulCronCount, $createTotal);
226 * Test scheduled reminders respect limit to (since above identified addition_to handling issue).
228 * We create 3 contacts - 1 is in our group, 1 has our membership & the chosen one has both
229 * & check that only the chosen one got the reminder
231 public function testCallSendReminderLimitTo() {
232 $membershipTypeID = $this->membershipTypeCreate();
233 $this->membershipStatusCreate();
235 $groupID = $this->groupCreate(array('name' => 'Texan drawlers', 'title' => 'a...'));
236 for ($i = 1; $i <= $createTotal; $i++
) {
237 $contactID = $this->individualCreate();
239 $theChosenOneID = $contactID;
242 $this->callAPISuccess('group_contact', 'create', array(
243 'contact_id' => $contactID,
245 'group_id' => $groupID,
249 $this->callAPISuccess('membership', 'create', array(
250 'contact_id' => $contactID,
251 'membership_type_id' => $membershipTypeID,
252 'join_date' => 'now',
253 'start_date' => '+ 1 day',
258 $this->callAPISuccess('action_schedule', 'create', array(
259 'title' => " remind all Texans",
260 'subject' => "drawling renewal",
261 'entity_value' => $membershipTypeID,
263 'start_action_date' => 'membership_start_date',
264 'start_action_offset' => 1,
265 'start_action_condition' => 'before',
266 'start_action_unit' => 'day',
267 'group_id' => $groupID,
270 $this->callAPISuccess('job', 'send_reminder', array());
271 $successfulCronCount = CRM_Core_DAO
::singleValueQuery("SELECT count(*) FROM civicrm_action_log");
272 $this->assertEquals($successfulCronCount, 1);
273 $sentToID = CRM_Core_DAO
::singleValueQuery("SELECT contact_id FROM civicrm_action_log");
274 $this->assertEquals($sentToID, $theChosenOneID);
277 public function testCallDisableExpiredRelationships() {
278 $individualID = $this->individualCreate();
279 $orgID = $this->organizationCreate();
280 CRM_Utils_Hook_UnitTests
::singleton()->setHook('civicrm_pre', array($this, 'hookPreRelationship'));
281 $relationshipTypeID = $this->callAPISuccess('relationship_type', 'getvalue', array(
283 'name_a_b' => 'Employee of',
285 $result = $this->callAPISuccess('relationship', 'create', array(
286 'relationship_type_id' => $relationshipTypeID,
287 'contact_id_a' => $individualID,
288 'contact_id_b' => $orgID,
290 'end_date' => 'yesterday',
292 $relationshipID = $result['id'];
293 $this->assertEquals('Hooked', $result['values'][$relationshipID]['description']);
294 $this->callAPISuccess($this->_entity
, 'disable_expired_relationships', array());
295 $result = $this->callAPISuccess('relationship', 'get', array());
296 $this->assertEquals('Go Go you good thing', $result['values'][$relationshipID]['description']);
297 $this->contactDelete($individualID);
298 $this->contactDelete($orgID);
302 * Test the batch merge function.
304 * We are just checking it returns without error here.
306 public function testBatchMerge() {
307 $this->callAPISuccess('Job', 'process_batch_merge', array());
311 * Test the batch merge function actually works!
313 * @dataProvider getMergeSets
317 public function testBatchMergeWorks($dataSet) {
318 foreach ($dataSet['contacts'] as $params) {
319 $this->callAPISuccess('Contact', 'create', $params);
322 $result = $this->callAPISuccess('Job', 'process_batch_merge', array('mode' => $dataSet['mode']));
323 $this->assertEquals($dataSet['skipped'], count($result['values']['skipped']), 'Failed to skip the right number:' . $dataSet['skipped']);
324 $this->assertEquals($dataSet['merged'], count($result['values']['merged']));
325 $result = $this->callAPISuccess('Contact', 'get', array(
326 'contact_sub_type' => 'Student',
328 'is_deceased' => array('IN' => array(0, 1)),
329 'options' => array('sort' => 'id ASC'),
331 $this->assertEquals(count($dataSet['expected']), $result['count']);
332 foreach ($dataSet['expected'] as $index => $contact) {
333 foreach ($contact as $key => $value) {
334 if ($key == 'gender_id') {
337 $this->assertEquals($value, $result['values'][$index][$key]);
343 * Check that the merge carries across various related entities.
345 * Note the group combinations & expected results:
347 public function testBatchMergeWithAssets() {
348 $contactID = $this->individualCreate();
349 $contact2ID = $this->individualCreate();
350 $this->contributionCreate(array('contact_id' => $contactID));
351 $this->contributionCreate(array('contact_id' => $contact2ID, 'invoice_id' => '2', 'trxn_id' => 2));
352 $this->contactMembershipCreate(array('contact_id' => $contactID));
353 $this->contactMembershipCreate(array('contact_id' => $contact2ID));
354 $this->activityCreate(array('source_contact_id' => $contactID, 'target_contact_id' => $contactID, 'assignee_contact_id' => $contactID));
355 $this->activityCreate(array('source_contact_id' => $contact2ID, 'target_contact_id' => $contact2ID, 'assignee_contact_id' => $contact2ID));
356 $this->tagCreate(array('name' => 'Tall'));
357 $this->tagCreate(array('name' => 'Short'));
358 $this->entityTagAdd(array('contact_id' => $contactID, 'tag_id' => 'Tall'));
359 $this->entityTagAdd(array('contact_id' => $contact2ID, 'tag_id' => 'Short'));
360 $this->entityTagAdd(array('contact_id' => $contact2ID, 'tag_id' => 'Tall'));
361 $result = $this->callAPISuccess('Job', 'process_batch_merge', array('mode' => 'safe'));
362 $this->assertEquals(0, count($result['values']['skipped']));
363 $this->assertEquals(1, count($result['values']['merged']));
364 $this->callAPISuccessGetCount('Contribution', array('contact_id' => $contactID), 2);
365 $this->callAPISuccessGetCount('Contribution', array('contact_id' => $contact2ID), 0);
366 $this->callAPISuccessGetCount('FinancialItem', array('contact_id' => $contactID), 2);
367 $this->callAPISuccessGetCount('FinancialItem', array('contact_id' => $contact2ID), 0);
368 $this->callAPISuccessGetCount('Membership', array('contact_id' => $contactID), 2);
369 $this->callAPISuccessGetCount('Membership', array('contact_id' => $contact2ID), 0);
370 $this->callAPISuccessGetCount('EntityTag', array('contact_id' => $contactID), 2);
371 $this->callAPISuccessGetCount('EntityTag', array('contact_id' => $contact2ID), 0);
372 // 12 activities is one for each contribution (2), one for each membership (+2 = 4)
373 // 3 for each of the added activities as there are 3 roles (+6 = 10
374 // 2 for the (source & target) contact merged activity (+2 = 12)
375 $this->callAPISuccessGetCount('ActivityContact', array('contact_id' => $contactID), 12);
376 // 2 for the connection to the deleted by merge activity (source & target)
377 $this->callAPISuccessGetCount('ActivityContact', array('contact_id' => $contact2ID), 2);
381 * Check that the merge carries across various related entities.
383 * Note the group combinations 'expected' results:
385 * Group 0 Added null Added
386 * Group 1 Added Added Added
387 * Group 2 Added Removed **** Added
388 * Group 3 Removed null **** null
389 * Group 4 Removed Added **** Added
390 * Group 5 Removed Removed **** null
391 * Group 6 null Added Added
392 * Group 7 null Removed **** null
394 * The ones with **** are the ones where I think a case could be made to change the behaviour.
396 public function testBatchMergeMergesGroups() {
397 $contactID = $this->individualCreate();
398 $contact2ID = $this->individualCreate();
400 for ($i = 0; $i < 8; $i++
) {
401 $groups[] = $this->groupCreate(array(
402 'name' => 'mergeGroup' . $i,
403 'title' => 'merge group' . $i,
407 $this->callAPISuccess('GroupContact', 'create', array(
408 'contact_id' => $contactID,
409 'group_id' => $groups[0],
411 $this->callAPISuccess('GroupContact', 'create', array(
412 'contact_id' => $contactID,
413 'group_id' => $groups[1],
415 $this->callAPISuccess('GroupContact', 'create', array(
416 'contact_id' => $contactID,
417 'group_id' => $groups[2],
419 $this->callAPISuccess('GroupContact', 'create', array(
420 'contact_id' => $contactID,
421 'group_id' => $groups[3],
422 'status' => 'Removed',
424 $this->callAPISuccess('GroupContact', 'create', array(
425 'contact_id' => $contactID,
426 'group_id' => $groups[4],
427 'status' => 'Removed',
429 $this->callAPISuccess('GroupContact', 'create', array(
430 'contact_id' => $contactID,
431 'group_id' => $groups[5],
432 'status' => 'Removed',
434 $this->callAPISuccess('GroupContact', 'create', array(
435 'contact_id' => $contact2ID,
436 'group_id' => $groups[1],
438 $this->callAPISuccess('GroupContact', 'create', array(
439 'contact_id' => $contact2ID,
440 'group_id' => $groups[2],
441 'status' => 'Removed',
443 $this->callAPISuccess('GroupContact', 'create', array(
444 'contact_id' => $contact2ID,
445 'group_id' => $groups[4],
447 $this->callAPISuccess('GroupContact', 'create', array(
448 'contact_id' => $contact2ID,
449 'group_id' => $groups[5],
450 'status' => 'Removed',
452 $this->callAPISuccess('GroupContact', 'create', array(
453 'contact_id' => $contact2ID,
454 'group_id' => $groups[6],
456 $this->callAPISuccess('GroupContact', 'create', array(
457 'contact_id' => $contact2ID,
458 'group_id' => $groups[7],
459 'status' => 'Removed',
461 $result = $this->callAPISuccess('Job', 'process_batch_merge', array('mode' => 'safe'));
462 $this->assertEquals(0, count($result['values']['skipped']));
463 $this->assertEquals(1, count($result['values']['merged']));
464 $groupResult = $this->callAPISuccess('GroupContact', 'get', array());
465 $this->assertEquals(5, $groupResult['count']);
466 $expectedGroups = array(
473 foreach ($groupResult['values'] as $groupValues) {
474 $this->assertEquals($contactID, $groupValues['contact_id']);
475 $this->assertEquals('Added', $groupValues['status']);
476 $this->assertTrue(in_array($groupValues['group_id'], $expectedGroups));
482 * Test the decisions made for addresses when merging.
484 * @dataProvider getMergeLocationData
487 * (the ones with **** could be disputed as whether it is the best outcome).
488 * 'matching_primary' - Primary matches, including location_type_id. One contact has an additional address.
489 * - result - primary is the shared one. Additional address is retained.
490 * 'matching_primary_reverse' - Primary matches, including location_type_id. Keep both. (opposite order)
491 * - result - primary is the shared one. Additional address is retained.
492 * 'only_one_has_address' - Only one contact has addresses (retain)
493 * - the (only) address is retained
494 * 'only_one_has_address_reverse'
495 * - the (only) address is retained
496 * 'different_primaries_with_different_location_type' Primaries are different but do not clash due to diff type
497 * - result - both addresses kept. The one from the kept (lowest ID) contact is primary
498 * 'different_primaries_with_different_location_type_reverse' Primaries are different but do not clash due to diff type
499 * - result - both addresses kept. The one from the kept (lowest ID) contact is primary
500 * 'different_primaries_location_match_only_one_address' per previous but a second address matches the primary but is not primary
501 * - result - both addresses kept. The one from the kept (lowest ID) contact is primary
502 * 'different_primaries_location_match_only_one_address_reverse' per previous but a second address matches the primary but is not primary
503 * - result - both addresses kept. The one from the kept (lowest ID) contact is primary
504 * 'same_primaries_different_location' Primary addresses are the same but have different location type IDs
505 * - result primary kept with the lowest ID. Other address retained too (to preserve location type info).
506 * 'same_primaries_different_location_reverse' Primary addresses are the same but have different location type IDs
507 * - result primary kept with the lowest ID. Other address retained too (to preserve location type info).
509 * @param array $dataSet
511 public function testBatchMergesAddresses($dataSet) {
512 $contactID1 = $this->individualCreate();
513 $contactID2 = $this->individualCreate();
514 foreach ($dataSet['contact_1'] as $address) {
515 $this->callAPISuccess($dataSet['entity'], 'create', array_merge(array('contact_id' => $contactID1), $address));
517 foreach ($dataSet['contact_2'] as $address) {
518 $this->callAPISuccess($dataSet['entity'], 'create', array_merge(array('contact_id' => $contactID2), $address));
521 $result = $this->callAPISuccess('Job', 'process_batch_merge', array('mode' => 'safe'));
522 $this->assertEquals(1, count($result['values']['merged']));
523 $addresses = $this->callAPISuccess($dataSet['entity'], 'get', array('contact_id' => $contactID1, 'sequential' => 1));
524 $this->assertEquals(count($dataSet['expected']), $addresses['count'], "Did not get the expected result for " . $dataSet['entity'] . (!empty($dataSet['description']) ?
" on dataset {$dataSet['description']}" : ''));
525 $locationTypes = $this->callAPISuccess($dataSet['entity'], 'getoptions', array('field' => 'location_type_id'));
526 foreach ($dataSet['expected'] as $index => $expectedAddress) {
527 foreach ($expectedAddress as $key => $value) {
528 if ($key == 'location_type_id') {
529 $this->assertEquals($locationTypes['values'][$addresses['values'][$index][$key]], $value);
532 $this->assertEquals($addresses['values'][$index][$key], $value, "mismatch on $key" . (!empty($dataSet['description']) ?
" on dataset {$dataSet['description']}" : ''));
539 * Test altering the address decision by hook.
541 * @dataProvider getMergeLocationData
543 * @param array $dataSet
545 public function testBatchMergesAddressesHook($dataSet) {
546 $contactID1 = $this->individualCreate();
547 $contactID2 = $this->individualCreate();
548 $this->contributionCreate(array('contact_id' => $contactID1, 'receive_date' => '2010-01-01', 'invoice_id' => 1, 'trxn_id' => 1));
549 $this->contributionCreate(array('contact_id' => $contactID2, 'receive_date' => '2012-01-01', 'invoice_id' => 2, 'trxn_id' => 2));
550 foreach ($dataSet['contact_1'] as $address) {
551 $this->callAPISuccess($dataSet['entity'], 'create', array_merge(array('contact_id' => $contactID1), $address));
553 foreach ($dataSet['contact_2'] as $address) {
554 $this->callAPISuccess($dataSet['entity'], 'create', array_merge(array('contact_id' => $contactID2), $address));
556 $this->hookClass
->setHook('civicrm_alterLocationMergeData', array($this, 'hookMostRecentDonor'));
558 $result = $this->callAPISuccess('Job', 'process_batch_merge', array('mode' => 'safe'));
559 $this->assertEquals(1, count($result['values']['merged']));
560 $addresses = $this->callAPISuccess($dataSet['entity'], 'get', array('contact_id' => $contactID1, 'sequential' => 1));
561 $this->assertEquals(count($dataSet['expected_hook']), $addresses['count']);
562 $locationTypes = $this->callAPISuccess($dataSet['entity'], 'getoptions', array('field' => 'location_type_id'));
563 foreach ($dataSet['expected_hook'] as $index => $expectedAddress) {
564 foreach ($expectedAddress as $key => $value) {
565 if ($key == 'location_type_id') {
566 $this->assertEquals($locationTypes['values'][$addresses['values'][$index][$key]], $value);
569 $this->assertEquals($value, $addresses['values'][$index][$key], $dataSet['entity'] . ': Unexpected value for ' . $key . (!empty($dataSet['description']) ?
" on dataset {$dataSet['description']}" : ''));
576 * Test the organization will not be matched to an individual.
578 public function testBatchMergeWillNotMergeOrganizationToIndividual() {
579 $individual = $this->callAPISuccess('Contact', 'create', array(
580 'contact_type' => 'Individual',
581 'organization_name' => 'Anon',
582 'email' => 'anonymous@hacker.com',
584 $organization = $this->callAPISuccess('Contact', 'create', array(
585 'contact_type' => 'Organization',
586 'organization_name' => 'Anon',
587 'email' => 'anonymous@hacker.com',
589 $result = $this->callAPISuccess('Job', 'process_batch_merge', array('mode' => 'aggressive'));
590 $this->assertEquals(0, count($result['values']['skipped']));
591 $this->assertEquals(0, count($result['values']['merged']));
592 $this->callAPISuccessGetSingle('Contact', array('id' => $individual['id']));
593 $this->callAPISuccessGetSingle('Contact', array('id' => $organization['id']));
598 * Test hook allowing modification of the data calculated for merging locations.
600 * We are testing a nuanced real life situation where the address data of the
601 * most recent donor gets priority - resulting in the primary address being set
602 * to the primary address of the most recent donor and address data on a per
603 * location type basis also being set to the most recent donor. Hook also excludes
604 * a fully matching address with a different location.
606 * This has been added to the test suite to ensure the code supports more this
607 * type of intervention.
609 * @param array $blocksDAO
610 * Array of location DAO to be saved. These are arrays in 2 keys 'update' & 'delete'.
612 * Contact_id of the contact that survives the merge.
613 * @param int $otherId
614 * Contact_id of the contact that will be absorbed and deleted.
615 * @param array $migrationInfo
616 * Calculated migration info, informational only.
620 public function hookMostRecentDonor(&$blocksDAO, $mainId, $otherId, $migrationInfo) {
622 $lastDonorID = $this->callAPISuccessGetValue('Contribution', array(
623 'return' => 'contact_id',
624 'contact_id' => array('IN' => array($mainId, $otherId)),
625 'options' => array('sort' => 'receive_date DESC', 'limit' => 1),
627 // Since the last donor is not the main ID we are prioritising info from the last donor.
628 // In the test this should always be true - but keep the check in case
629 // something changes that we need to detect.
630 if ($lastDonorID != $mainId) {
631 foreach ($migrationInfo['other_details']['location_blocks'] as $blockType => $blocks) {
632 foreach ($blocks as $block) {
633 if ($block['is_primary']) {
634 $primaryAddressID = $block['id'];
635 if (!empty($migrationInfo['main_details']['location_blocks'][$blockType])) {
636 foreach ($migrationInfo['main_details']['location_blocks'][$blockType] as $mainBlock) {
637 if (empty($blocksDAO[$blockType]['update'][$block['id']]) && $mainBlock['location_type_id'] == $block['location_type_id']) {
638 // This was an address match - we just need to check the is_primary
639 // is true on the matching kept address.
640 $primaryAddressID = $mainBlock['id'];
641 $blocksDAO[$blockType]['update'][$primaryAddressID] = _civicrm_api3_load_DAO($blockType);
642 $blocksDAO[$blockType]['update'][$primaryAddressID]->id
= $primaryAddressID;
644 $mainLocationTypeID = $mainBlock['location_type_id'];
645 // We also want to be more ruthless about removing matching addresses.
646 unset($mainBlock['location_type_id']);
647 if (CRM_Dedupe_Merger
::locationIsSame($block, $mainBlock)
648 && (!isset($blocksDAO[$blockType]['update']) ||
!isset($blocksDAO[$blockType]['update'][$mainBlock['id']]))
649 && (!isset($blocksDAO[$blockType]['delete']) ||
!isset($blocksDAO[$blockType]['delete'][$mainBlock['id']]))
651 $blocksDAO[$blockType]['delete'][$mainBlock['id']] = _civicrm_api3_load_DAO($blockType);
652 $blocksDAO[$blockType]['delete'][$mainBlock['id']]->id
= $mainBlock['id'];
654 // Arguably the right way to handle this is just to set is_primary for the primary
655 // and for the merge fn to call something like BAO::add & hooks to work etc.
656 // if that happens though this should keep working...
657 elseif ($mainBlock['is_primary'] && $mainLocationTypeID != $block['location_type_id']) {
658 $blocksDAO['address']['update'][$mainBlock['id']] = _civicrm_api3_load_DAO($blockType);
659 $blocksDAO['address']['update'][$mainBlock['id']]->is_primary
= 0;
660 $blocksDAO['address']['update'][$mainBlock['id']]->id
= $mainBlock['id'];
664 $blocksDAO[$blockType]['update'][$primaryAddressID]->is_primary
= 1;
673 * Get address combinations for the merge test.
677 public function getMergeLocationData() {
678 $address1 = array('street_address' => 'Buckingham Palace', 'city' => 'London');
679 $address2 = array('street_address' => 'The Doghouse', 'supplemental_address_1' => 'under the blanket');
680 $data = $this->getMergeLocations($address1, $address2, 'Address');
681 $data = array_merge($data, $this->getMergeLocations(array('phone' => '12345', 'phone_type_id' => 1), array('phone' => '678910', 'phone_type_id' => 1), 'Phone'));
682 $data = array_merge($data, $this->getMergeLocations(array('phone' => '12345'), array('phone' => '678910'), 'Phone'));
683 $data = array_merge($data, $this->getMergeLocations(array('email' => 'mini@me.com'), array('email' => 'mini@me.org'), 'Email', array(array(
684 'email' => 'anthony_anderson@civicrm.org',
685 'location_type_id' => 'Home',
692 * Test the batch merge does not create duplicate emails.
694 * Test CRM-18546, a 4.7 regression whereby a merged contact gets duplicate emails.
696 public function testBatchMergeEmailHandling() {
697 for ($x = 0; $x <= 4; $x++
) {
698 $id = $this->individualCreate(array('email' => 'batman@gotham.met'));
700 $result = $this->callAPISuccess('Job', 'process_batch_merge', array());
701 $this->assertEquals(4, count($result['values']['merged']));
702 $this->callAPISuccessGetCount('Contact', array('email' => 'batman@gotham.met'), 1);
703 $contacts = $this->callAPISuccess('Contact', 'get', array('is_deleted' => 0));
704 $deletedContacts = $this->callAPISuccess('Contact', 'get', array('is_deleted' => 1));
705 $this->callAPISuccessGetCount('Email', array(
706 'email' => 'batman@gotham.met',
707 'contact_id' => array('IN' => array_keys($contacts['values'])),
709 $this->callAPISuccessGetCount('Email', array(
710 'email' => 'batman@gotham.met',
711 'contact_id' => array('IN' => array_keys($deletedContacts['values'])),
716 * Test the batch merge respects email "on hold".
718 * Test CRM-19148, Batch merge - Email on hold data lost when there is a conflict.
720 * @dataProvider getOnHoldSets
724 public function testBatchMergeEmailOnHold($onHold1, $onHold2, $merge) {
725 $contactID1 = $this->individualCreate(array(
726 'api.email.create' => array(
727 'email' => 'batman@gotham.met',
728 'location_type_id' => 'Work',
730 'on_hold' => $onHold1,
733 $contactID2 = $this->individualCreate(array(
734 'api.email.create' => array(
735 'email' => 'batman@gotham.met',
736 'location_type_id' => 'Work',
738 'on_hold' => $onHold2,
741 $result = $this->callAPISuccess('Job', 'process_batch_merge', array());
742 $this->assertEquals($merge, count($result['values']['merged']));
746 * Data provider for testBatchMergeEmailOnHold: combinations of on_hold & expected outcomes.
748 public function getOnHoldSets() {
749 // Each row specifies: contact 1 on_hold, contact 2 on_hold, merge? (0 or 1),
760 * Test the batch merge does not fatal on an empty rule.
762 * @dataProvider getRuleSets
764 * @param string $contactType
765 * @param string $used
766 * @param bool $isReserved
767 * @param int $threshold
769 public function testBatchMergeEmptyRule($contactType, $used, $name, $isReserved, $threshold) {
770 $ruleGroup = $this->callAPISuccess('RuleGroup', 'create', array(
771 'contact_type' => $contactType,
772 'threshold' => $threshold,
775 'is_reserved' => $isReserved,
777 $this->callAPISuccess('Job', 'process_batch_merge', array('rule_group_id' => $ruleGroup['id']));
778 $this->callAPISuccess('RuleGroup', 'delete', array('id' => $ruleGroup['id']));
782 * Get the various rule combinations.
784 public function getRuleSets() {
785 $contactTypes = array('Individual', 'Organization', 'Household');
786 $useds = array('Unsupervised', 'General', 'Supervised');
787 $ruleGroups = array();
788 foreach ($contactTypes as $contactType) {
789 foreach ($useds as $used) {
790 $ruleGroups[] = array($contactType, $used, 'Bob', FALSE, 0);
791 $ruleGroups[] = array($contactType, $used, 'Bob', FALSE, 10);
792 $ruleGroups[] = array($contactType, $used, 'Bob', TRUE, 10);
793 $ruleGroups[] = array($contactType, $used, $contactType . $used, FALSE, 10);
794 $ruleGroups[] = array($contactType, $used, $contactType . $used, TRUE, 10);
801 * Test the batch merge does not create duplicate emails.
803 * Test CRM-18546, a 4.7 regression whereby a merged contact gets duplicate emails.
805 public function testBatchMergeMatchingAddress() {
806 for ($x = 0; $x <= 2; $x++
) {
807 $this->individualCreate(array(
808 'api.address.create' => array(
809 'location_type_id' => 'Home',
810 'street_address' => 'Appt 115, The Batcave',
812 'postal_code' => 'Nananananana',
816 // Different location type, still merge, identical.
817 $this->individualCreate(array(
818 'api.address.create' => array(
819 'location_type_id' => 'Main',
820 'street_address' => 'Appt 115, The Batcave',
822 'postal_code' => 'Nananananana',
826 $this->individualCreate(array(
827 'api.address.create' => array(
828 'location_type_id' => 'Home',
829 'street_address' => 'Appt 115, The Batcave',
831 'postal_code' => 'Batman',
835 $result = $this->callAPISuccess('Job', 'process_batch_merge', array());
836 $this->assertEquals(3, count($result['values']['merged']));
837 $this->assertEquals(1, count($result['values']['skipped']));
838 $this->callAPISuccessGetCount('Contact', array('street_address' => 'Appt 115, The Batcave'), 2);
839 $contacts = $this->callAPISuccess('Contact', 'get', array('is_deleted' => 0));
840 $deletedContacts = $this->callAPISuccess('Contact', 'get', array('is_deleted' => 1));
841 $this->callAPISuccessGetCount('Address', array(
842 'street_address' => 'Appt 115, The Batcave',
843 'contact_id' => array('IN' => array_keys($contacts['values'])),
846 $this->callAPISuccessGetCount('Address', array(
847 'street_address' => 'Appt 115, The Batcave',
848 'contact_id' => array('IN' => array_keys($deletedContacts['values'])),
853 * Test the batch merge by id range.
855 * We have 2 sets of 5 matches & set the merge only to merge the lower set.
857 public function testBatchMergeIDRange() {
858 for ($x = 0; $x <= 4; $x++
) {
859 $id = $this->individualCreate(array('email' => 'batman@gotham.met'));
861 for ($x = 0; $x <= 4; $x++
) {
862 $this->individualCreate(array('email' => 'robin@gotham.met'));
864 $result = $this->callAPISuccess('Job', 'process_batch_merge', array('criteria' => array('contact' => array('id' => array('<' => $id)))));
865 $this->assertEquals(4, count($result['values']['merged']));
866 $this->callAPISuccessGetCount('Contact', array('email' => 'batman@gotham.met'), 1);
867 $this->callAPISuccessGetCount('Contact', array('email' => 'robin@gotham.met'), 5);
868 $contacts = $this->callAPISuccess('Contact', 'get', array('is_deleted' => 0));
869 $deletedContacts = $this->callAPISuccess('Contact', 'get', array('is_deleted' => 0));
870 $this->callAPISuccessGetCount('Email', array(
871 'email' => 'batman@gotham.met',
872 'contact_id' => array('IN' => array_keys($contacts['values'])),
874 $this->callAPISuccessGetCount('Email', array(
875 'email' => 'batman@gotham.met',
876 'contact_id' => array('IN' => array_keys($deletedContacts['values'])),
878 $this->callAPISuccessGetCount('Email', array(
879 'email' => 'robin@gotham.met',
880 'contact_id' => array('IN' => array_keys($contacts['values'])),
886 * Test the batch merge function actually works!
888 * @dataProvider getMergeSets
892 public function testBatchMergeWorksCheckPermissionsTrue($dataSet) {
893 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM', 'administer CiviCRM');
894 foreach ($dataSet['contacts'] as $params) {
895 $this->callAPISuccess('Contact', 'create', $params);
898 $result = $this->callAPISuccess('Job', 'process_batch_merge', array('check_permissions' => 1, 'mode' => $dataSet['mode']));
899 $this->assertEquals(0, count($result['values']['merged']), 'User does not have permission to any contacts, so no merging');
900 $this->assertEquals(0, count($result['values']['skipped']), 'User does not have permission to any contacts, so no skip visibility');
904 * Test the batch merge function actually works!
906 * @dataProvider getMergeSets
910 public function testBatchMergeWorksCheckPermissionsFalse($dataSet) {
911 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= array('access CiviCRM', 'edit my contact');
912 foreach ($dataSet['contacts'] as $params) {
913 $this->callAPISuccess('Contact', 'create', $params);
916 $result = $this->callAPISuccess('Job', 'process_batch_merge', array('check_permissions' => 0, 'mode' => $dataSet['mode']));
917 $this->assertEquals($dataSet['skipped'], count($result['values']['skipped']), 'Failed to skip the right number:' . $dataSet['skipped']);
918 $this->assertEquals($dataSet['merged'], count($result['values']['merged']));
922 * Get data for batch merge.
924 public function getMergeSets() {
931 'first_name' => 'Michael',
932 'last_name' => 'Jackson',
933 'email' => 'michael@neverland.com',
934 'contact_type' => 'Individual',
935 'contact_sub_type' => 'Student',
936 'api.Address.create' => array(
937 'street_address' => 'big house',
938 'location_type_id' => 'Home',
942 'first_name' => 'Michael',
943 'last_name' => 'Jackson',
944 'email' => 'michael@neverland.com',
945 'contact_type' => 'Individual',
946 'contact_sub_type' => 'Student',
953 'first_name' => 'Michael',
954 'last_name' => 'Jackson',
955 'email' => 'michael@neverland.com',
956 'contact_type' => 'Individual',
966 'first_name' => 'Michael',
967 'last_name' => 'Jackson',
968 'email' => 'michael@neverland.com',
969 'contact_type' => 'Individual',
970 'contact_sub_type' => 'Student',
971 'api.Address.create' => array(
972 'street_address' => 'big house',
973 'location_type_id' => 'Home',
977 'first_name' => 'Michael',
978 'last_name' => 'Jackson',
979 'email' => 'michael@neverland.com',
980 'contact_type' => 'Individual',
981 'contact_sub_type' => 'Student',
982 'api.Address.create' => array(
983 'street_address' => 'bigger house',
984 'location_type_id' => 'Home',
992 'first_name' => 'Michael',
993 'last_name' => 'Jackson',
994 'email' => 'michael@neverland.com',
995 'contact_type' => 'Individual',
996 'street_address' => 'big house',
999 'first_name' => 'Michael',
1000 'last_name' => 'Jackson',
1001 'email' => 'michael@neverland.com',
1002 'contact_type' => 'Individual',
1003 'street_address' => 'bigger house',
1011 'contacts' => array(
1013 'first_name' => 'Michael',
1014 'last_name' => 'Jackson',
1015 'email' => 'michael@neverland.com',
1016 'contact_type' => 'Individual',
1017 'contact_sub_type' => 'Student',
1018 'api.Email.create' => array(
1019 'email' => 'big.slog@work.co.nz',
1020 'location_type_id' => 'Work',
1024 'first_name' => 'Michael',
1025 'last_name' => 'Jackson',
1026 'email' => 'michael@neverland.com',
1027 'contact_type' => 'Individual',
1028 'contact_sub_type' => 'Student',
1029 'api.Email.create' => array(
1030 'email' => 'big.slog@work.com',
1031 'location_type_id' => 'Work',
1037 'expected' => array(
1039 'first_name' => 'Michael',
1040 'last_name' => 'Jackson',
1041 'email' => 'michael@neverland.com',
1042 'contact_type' => 'Individual',
1045 'first_name' => 'Michael',
1046 'last_name' => 'Jackson',
1047 'email' => 'michael@neverland.com',
1048 'contact_type' => 'Individual',
1056 'contacts' => array(
1058 'first_name' => 'Michael',
1059 'last_name' => 'Jackson',
1060 'email' => 'michael@neverland.com',
1061 'contact_type' => 'Individual',
1062 'contact_sub_type' => 'Student',
1063 'api.Phone.create' => array(
1064 'phone' => '123456',
1065 'location_type_id' => 'Work',
1069 'first_name' => 'Michael',
1070 'last_name' => 'Jackson',
1071 'email' => 'michael@neverland.com',
1072 'contact_type' => 'Individual',
1073 'contact_sub_type' => 'Student',
1074 'api.Phone.create' => array(
1076 'location_type_id' => 'Work',
1082 'expected' => array(
1084 'first_name' => 'Michael',
1085 'last_name' => 'Jackson',
1086 'email' => 'michael@neverland.com',
1087 'contact_type' => 'Individual',
1090 'first_name' => 'Michael',
1091 'last_name' => 'Jackson',
1092 'email' => 'michael@neverland.com',
1093 'contact_type' => 'Individual',
1100 'mode' => 'aggressive',
1101 'contacts' => array(
1103 'first_name' => 'Michael',
1104 'last_name' => 'Jackson',
1105 'email' => 'michael@neverland.com',
1106 'contact_type' => 'Individual',
1107 'contact_sub_type' => 'Student',
1108 'api.Address.create' => array(
1109 'street_address' => 'big house',
1110 'location_type_id' => 'Home',
1114 'first_name' => 'Michael',
1115 'last_name' => 'Jackson',
1116 'email' => 'michael@neverland.com',
1117 'contact_type' => 'Individual',
1118 'contact_sub_type' => 'Student',
1119 'api.Address.create' => array(
1120 'street_address' => 'bigger house',
1121 'location_type_id' => 'Home',
1127 'expected' => array(
1129 'first_name' => 'Michael',
1130 'last_name' => 'Jackson',
1131 'email' => 'michael@neverland.com',
1132 'contact_type' => 'Individual',
1133 'street_address' => 'big house',
1141 'contacts' => array(
1143 'first_name' => 'Michael',
1144 'last_name' => 'Jackson',
1145 'email' => 'michael@neverland.com',
1146 'contact_type' => 'Individual',
1147 'contact_sub_type' => 'Student',
1148 'api.Address.create' => array(
1149 'street_address' => 'big house',
1150 'location_type_id' => 'Home',
1154 'first_name' => 'Michael',
1155 'last_name' => 'Jackson',
1156 'email' => 'michael@neverland.com',
1157 'contact_type' => 'Individual',
1158 'contact_sub_type' => 'Student',
1164 'expected' => array(
1166 'first_name' => 'Michael',
1167 'last_name' => 'Jackson',
1168 'email' => 'michael@neverland.com',
1169 'contact_type' => 'Individual',
1173 'first_name' => 'Michael',
1174 'last_name' => 'Jackson',
1175 'email' => 'michael@neverland.com',
1176 'contact_type' => 'Individual',
1185 'contacts' => array(
1187 'first_name' => 'Michael',
1188 'last_name' => 'Jackson',
1189 'email' => 'michael@neverland.com',
1190 'contact_type' => 'Individual',
1191 'contact_sub_type' => 'Student',
1192 'api.Address.create' => array(
1193 'street_address' => 'big house',
1194 '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',
1208 'expected' => array(
1210 'first_name' => 'Michael',
1211 'last_name' => 'Jackson',
1212 'email' => 'michael@neverland.com',
1213 'contact_type' => 'Individual',
1217 'first_name' => 'Michael',
1218 'last_name' => 'Jackson',
1219 'email' => 'michael@neverland.com',
1220 'contact_type' => 'Individual',
1228 $conflictPairs = array(
1229 'first_name' => 'Dianna',
1230 'last_name' => 'McAndrew',
1231 'middle_name' => 'Prancer',
1232 'birth_date' => '2015-12-25',
1233 'gender_id' => 'Female',
1234 'job_title' => 'Thriller',
1237 foreach ($conflictPairs as $key => $value) {
1238 $contactParams = array(
1239 'first_name' => 'Michael',
1240 'middle_name' => 'Dancer',
1241 'last_name' => 'Jackson',
1242 'birth_date' => '2015-02-25',
1243 'email' => 'michael@neverland.com',
1244 'contact_type' => 'Individual',
1245 'contact_sub_type' => array('Student'),
1246 'gender_id' => 'Male',
1247 'job_title' => 'Entertainer',
1249 $contact2 = $contactParams;
1251 $contact2[$key] = $value;
1252 $data[$key . '_conflict'] = array(
1255 'contacts' => array($contactParams, $contact2),
1258 'expected' => array($contactParams, $contact2),
1268 * @param string $objectName
1270 * @param array $params
1272 public function hookPreRelationship($op, $objectName, $id, &$params) {
1273 if ($op == 'delete') {
1276 if ($params['is_active']) {
1277 $params['description'] = 'Hooked';
1280 $params['description'] = 'Go Go you good thing';
1285 * Get the location data set.
1287 * @param array $locationParams1
1288 * @param array $locationParams2
1289 * @param string $entity
1293 public function getMergeLocations($locationParams1, $locationParams2, $entity, $additionalExpected = array()) {
1296 'matching_primary' => array(
1297 'entity' => $entity,
1298 'contact_1' => array(
1300 'location_type_id' => 'Main',
1302 ), $locationParams1),
1304 'location_type_id' => 'Work',
1306 ), $locationParams2),
1308 'contact_2' => array(
1310 'location_type_id' => 'Main',
1312 ), $locationParams1),
1314 'expected' => array_merge($additionalExpected, array(
1316 'location_type_id' => 'Main',
1318 ), $locationParams1),
1320 'location_type_id' => 'Work',
1322 ), $locationParams2),
1324 'expected_hook' => array_merge($additionalExpected, array(
1326 'location_type_id' => 'Main',
1328 ), $locationParams1),
1330 'location_type_id' => 'Work',
1332 ), $locationParams2),
1337 'matching_primary_reverse' => array(
1338 'entity' => $entity,
1339 'contact_1' => array(
1341 'location_type_id' => 'Main',
1343 ), $locationParams1),
1345 'contact_2' => array(
1347 'location_type_id' => 'Main',
1349 ), $locationParams1),
1351 'location_type_id' => 'Work',
1353 ), $locationParams2),
1355 'expected' => array_merge($additionalExpected, array(
1357 'location_type_id' => 'Main',
1359 ), $locationParams1),
1361 'location_type_id' => 'Work',
1363 ), $locationParams2),
1365 'expected_hook' => array_merge($additionalExpected, array(
1367 'location_type_id' => 'Main',
1369 ), $locationParams1),
1371 'location_type_id' => 'Work',
1373 ), $locationParams2),
1378 'only_one_has_address' => array(
1379 'entity' => $entity,
1380 'contact_1' => array(
1382 'location_type_id' => 'Main',
1384 ), $locationParams1),
1386 'location_type_id' => 'Work',
1388 ), $locationParams2),
1390 'contact_2' => array(),
1391 'expected' => array_merge($additionalExpected, array(
1393 'location_type_id' => 'Main',
1395 ), $locationParams1),
1397 'location_type_id' => 'Work',
1399 ), $locationParams2),
1401 'expected_hook' => array_merge($additionalExpected, array(
1403 'location_type_id' => 'Main',
1404 // When dealing with email we don't have a clean slate - the existing
1405 // primary will be primary.
1406 'is_primary' => ($entity == 'Email' ?
0 : 1),
1407 ), $locationParams1),
1409 'location_type_id' => 'Work',
1411 ), $locationParams2),
1416 'only_one_has_address_reverse' => array(
1417 'description' => 'The destination contact does not have an address. secondary contact should be merged in.',
1418 'entity' => $entity,
1419 'contact_1' => array(),
1420 'contact_2' => array(
1422 'location_type_id' => 'Main',
1424 ), $locationParams1),
1426 'location_type_id' => 'Work',
1428 ), $locationParams2),
1430 'expected' => array_merge($additionalExpected, array(
1432 'location_type_id' => 'Main',
1433 // When dealing with email we don't have a clean slate - the existing
1434 // primary will be primary.
1435 'is_primary' => ($entity == 'Email' ?
0 : 1),
1436 ), $locationParams1),
1438 'location_type_id' => 'Work',
1440 ), $locationParams2),
1442 'expected_hook' => array_merge($additionalExpected, array(
1444 'location_type_id' => 'Main',
1446 ), $locationParams1),
1448 'location_type_id' => 'Work',
1450 ), $locationParams2),
1455 'different_primaries_with_different_location_type' => array(
1456 'description' => 'Primaries are different with different location. Keep both addresses. Set primary to be that of lower id',
1457 'entity' => $entity,
1458 'contact_1' => array(
1460 'location_type_id' => 'Main',
1462 ), $locationParams1),
1464 'contact_2' => array(
1466 'location_type_id' => 'Work',
1468 ), $locationParams2),
1470 'expected' => array_merge($additionalExpected, array(
1472 'location_type_id' => 'Main',
1474 ), $locationParams1),
1476 'location_type_id' => 'Work',
1478 ), $locationParams2),
1480 'expected_hook' => array_merge($additionalExpected, array(
1482 'location_type_id' => 'Main',
1484 ), $locationParams1),
1486 'location_type_id' => 'Work',
1488 ), $locationParams2),
1493 'different_primaries_with_different_location_type_reverse' => array(
1494 'entity' => $entity,
1495 'contact_1' => array(
1497 'location_type_id' => 'Work',
1499 ), $locationParams2),
1501 'contact_2' => array(
1503 'location_type_id' => 'Main',
1505 ), $locationParams1),
1507 'expected' => array_merge($additionalExpected, array(
1509 'location_type_id' => 'Work',
1511 ), $locationParams2),
1513 'location_type_id' => 'Main',
1515 ), $locationParams1),
1517 'expected_hook' => array_merge($additionalExpected, array(
1519 'location_type_id' => 'Work',
1521 ), $locationParams2),
1523 'location_type_id' => 'Main',
1525 ), $locationParams1),
1530 'different_primaries_location_match_only_one_address' => array(
1531 'entity' => $entity,
1532 'contact_1' => array(
1534 'location_type_id' => 'Main',
1536 ), $locationParams1),
1538 'location_type_id' => 'Work',
1540 ), $locationParams2),
1542 'contact_2' => array(
1544 'location_type_id' => 'Work',
1546 ), $locationParams2),
1549 'expected' => array_merge($additionalExpected, array(
1551 'location_type_id' => 'Main',
1553 ), $locationParams1),
1555 'location_type_id' => 'Work',
1557 ), $locationParams2),
1559 'expected_hook' => array_merge($additionalExpected, array(
1561 'location_type_id' => 'Main',
1563 ), $locationParams1),
1565 'location_type_id' => 'Work',
1567 ), $locationParams2),
1572 'different_primaries_location_match_only_one_address_reverse' => array(
1573 'entity' => $entity,
1574 'contact_1' => array(
1576 'location_type_id' => 'Work',
1578 ), $locationParams2),
1580 'contact_2' => array(
1582 'location_type_id' => 'Main',
1584 ), $locationParams1),
1586 'location_type_id' => 'Work',
1588 ), $locationParams2),
1590 'expected' => array_merge($additionalExpected, array(
1592 'location_type_id' => 'Work',
1594 ), $locationParams2),
1596 'location_type_id' => 'Main',
1598 ), $locationParams1),
1600 'expected_hook' => array_merge($additionalExpected, array(
1602 'location_type_id' => 'Work',
1604 ), $locationParams2),
1606 'location_type_id' => 'Main',
1608 ), $locationParams1),
1613 'same_primaries_different_location' => array(
1614 'entity' => $entity,
1615 'contact_1' => array(
1617 'location_type_id' => 'Main',
1619 ), $locationParams1),
1621 'contact_2' => array(
1623 'location_type_id' => 'Work',
1625 ), $locationParams1),
1628 'expected' => array_merge($additionalExpected, array(
1630 'location_type_id' => 'Main',
1632 ), $locationParams1),
1634 'location_type_id' => 'Work',
1636 ), $locationParams1),
1638 'expected_hook' => array_merge($additionalExpected, array(
1640 'location_type_id' => 'Work',
1642 ), $locationParams1),
1647 'same_primaries_different_location_reverse' => array(
1648 'entity' => $entity,
1649 'contact_1' => array(
1651 'location_type_id' => 'Work',
1653 ), $locationParams1),
1655 'contact_2' => array(
1657 'location_type_id' => 'Main',
1659 ), $locationParams1),
1661 'expected' => array_merge($additionalExpected, array(
1663 'location_type_id' => 'Work',
1665 ), $locationParams1),
1667 'location_type_id' => 'Main',
1669 ), $locationParams1),
1671 'expected_hook' => array_merge($additionalExpected, array(
1673 'location_type_id' => 'Main',
1675 ), $locationParams1),