Merge pull request #10030 from eileenmcnaughton/test
[civicrm-core.git] / tests / phpunit / api / v3 / JobTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * File for the CiviCRM APIv3 job functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Job
33 *
34 * @copyright CiviCRM LLC (c) 2004-2017
35 */
36
37 /**
38 * Class api_v3_JobTest
39 * @group headless
40 */
41 class api_v3_JobTest extends CiviUnitTestCase {
42 protected $_apiversion = 3;
43
44 public $DBResetRequired = FALSE;
45 public $_entity = 'Job';
46 public $_params = array();
47 /**
48 * Created membership type.
49 *
50 * Must be created outside the transaction due to it breaking the transaction.
51 *
52 * @var
53 */
54 public $membershipTypeID;
55
56 /**
57 * Set up for tests.
58 */
59 public function setUp() {
60 parent::setUp();
61 $this->membershipTypeID = $this->membershipTypeCreate(array('name' => 'General'));
62 $this->useTransaction(TRUE);
63 $this->_params = array(
64 'sequential' => 1,
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',
71 'is_active' => 1,
72 );
73 }
74
75 public function tearDown() {
76 parent::tearDown();
77 // The membershipType create breaks transactions so this extra cleanup is needed.
78 $this->membershipTypeDelete(array('id' => $this->membershipTypeID));
79 $this->cleanUpSetUpIDs();
80 }
81
82 /**
83 * Check with no name.
84 */
85 public function testCreateWithoutName() {
86 $params = array(
87 'is_active' => 1,
88 );
89 $this->callAPIFailure('job', 'create', $params,
90 'Mandatory key(s) missing from params array: run_frequency, name, api_entity, api_action'
91 );
92 }
93
94 /**
95 * Create job with an invalid "run_frequency" value.
96 */
97 public function testCreateWithInvalidFrequency() {
98 $params = array(
99 'sequential' => 1,
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',
106 'is_active' => 1,
107 );
108 $this->callAPIFailure('job', 'create', $params);
109 }
110
111 /**
112 * Create job.
113 */
114 public function testCreate() {
115 $result = $this->callAPIAndDocument('job', 'create', $this->_params, __FUNCTION__, __FILE__);
116 $this->assertNotNull($result['values'][0]['id']);
117
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);
122 }
123
124 /**
125 * Check if required fields are not passed.
126 */
127 public function testDeleteWithoutRequired() {
128 $params = array(
129 'name' => 'API_Test_PP',
130 'title' => 'API Test Payment Processor',
131 'class_name' => 'CRM_Core_Payment_APITest',
132 );
133
134 $result = $this->callAPIFailure('job', 'delete', $params);
135 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
136 }
137
138 /**
139 * Check with incorrect required fields.
140 */
141 public function testDeleteWithIncorrectData() {
142 $params = array(
143 'id' => 'abcd',
144 );
145 $this->callAPIFailure('job', 'delete', $params);
146 }
147
148 /**
149 * Check job delete.
150 */
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']);
156 }
157
158 /**
159 *
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']);
163 * }
164 *
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']);
168 * }
169 * /*
170 * Note that this test is about tesing the metadata / calling of the function & doesn't test the success of the called function
171 */
172 public function testCallUpdateGreetingSuccess() {
173 $this->callAPISuccess($this->_entity, 'update_greeting', array(
174 'gt' => 'postal_greeting',
175 'ct' => 'Individual',
176 ));
177 }
178
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));
183 }
184
185 /**
186 * Test the call reminder success sends more than 25 reminders & is not incorrectly limited.
187 *
188 * Note that this particular test sends the reminders to the additional recipients only
189 * as no real reminder person is configured
190 *
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
194 */
195 public function testCallSendReminderSuccessMoreThanDefaultLimit() {
196 $membershipTypeID = $this->membershipTypeCreate();
197 $this->membershipStatusCreate();
198 $createTotal = 30;
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,
206 'mapping_id' => 4,
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,
212 'limit_to' => FALSE,
213 ));
214 $this->callAPISuccess('group_contact', 'create', array(
215 'contact_id' => $contactID,
216 'status' => 'Added',
217 'group_id' => $groupID,
218 ));
219 }
220 $this->callAPISuccess('job', 'send_reminder', array());
221 $successfulCronCount = CRM_Core_DAO::singleValueQuery("SELECT count(*) FROM civicrm_action_log");
222 $this->assertEquals($successfulCronCount, $createTotal);
223 }
224
225 /**
226 * Test scheduled reminders respect limit to (since above identified addition_to handling issue).
227 *
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
230 */
231 public function testCallSendReminderLimitTo() {
232 $membershipTypeID = $this->membershipTypeCreate();
233 $this->membershipStatusCreate();
234 $createTotal = 3;
235 $groupID = $this->groupCreate(array('name' => 'Texan drawlers', 'title' => 'a...'));
236 for ($i = 1; $i <= $createTotal; $i++) {
237 $contactID = $this->individualCreate();
238 if ($i == 2) {
239 $theChosenOneID = $contactID;
240 }
241 if ($i < 3) {
242 $this->callAPISuccess('group_contact', 'create', array(
243 'contact_id' => $contactID,
244 'status' => 'Added',
245 'group_id' => $groupID,
246 ));
247 }
248 if ($i > 1) {
249 $this->callAPISuccess('membership', 'create', array(
250 'contact_id' => $contactID,
251 'membership_type_id' => $membershipTypeID,
252 'join_date' => 'now',
253 'start_date' => '+ 1 day',
254 )
255 );
256 }
257 }
258 $this->callAPISuccess('action_schedule', 'create', array(
259 'title' => " remind all Texans",
260 'subject' => "drawling renewal",
261 'entity_value' => $membershipTypeID,
262 'mapping_id' => 4,
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,
268 'limit_to' => TRUE,
269 ));
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);
275 }
276
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(
282 'return' => 'id',
283 'name_a_b' => 'Employee of',
284 ));
285 $result = $this->callAPISuccess('relationship', 'create', array(
286 'relationship_type_id' => $relationshipTypeID,
287 'contact_id_a' => $individualID,
288 'contact_id_b' => $orgID,
289 'is_active' => 1,
290 'end_date' => 'yesterday',
291 ));
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);
299 }
300
301 /**
302 * Test the batch merge function.
303 *
304 * We are just checking it returns without error here.
305 */
306 public function testBatchMerge() {
307 $this->callAPISuccess('Job', 'process_batch_merge', array());
308 }
309
310 /**
311 * Test the batch merge function actually works!
312 *
313 * @dataProvider getMergeSets
314 *
315 * @param $dataSet
316 */
317 public function testBatchMergeWorks($dataSet) {
318 foreach ($dataSet['contacts'] as $params) {
319 $this->callAPISuccess('Contact', 'create', $params);
320 }
321
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',
327 'sequential' => 1,
328 'is_deceased' => array('IN' => array(0, 1)),
329 'options' => array('sort' => 'id ASC'),
330 ));
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') {
335 $key = 'gender';
336 }
337 $this->assertEquals($value, $result['values'][$index][$key]);
338 }
339 }
340 }
341
342 /**
343 * Check that the merge carries across various related entities.
344 *
345 * Note the group combinations & expected results:
346 */
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);
378 }
379
380 /**
381 * Check that the merge carries across various related entities.
382 *
383 * Note the group combinations 'expected' results:
384 *
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
393 *
394 * The ones with **** are the ones where I think a case could be made to change the behaviour.
395 */
396 public function testBatchMergeMergesGroups() {
397 $contactID = $this->individualCreate();
398 $contact2ID = $this->individualCreate();
399 $groups = array();
400 for ($i = 0; $i < 8; $i++) {
401 $groups[] = $this->groupCreate(array(
402 'name' => 'mergeGroup' . $i,
403 'title' => 'merge group' . $i,
404 ));
405 }
406
407 $this->callAPISuccess('GroupContact', 'create', array(
408 'contact_id' => $contactID,
409 'group_id' => $groups[0],
410 ));
411 $this->callAPISuccess('GroupContact', 'create', array(
412 'contact_id' => $contactID,
413 'group_id' => $groups[1],
414 ));
415 $this->callAPISuccess('GroupContact', 'create', array(
416 'contact_id' => $contactID,
417 'group_id' => $groups[2],
418 ));
419 $this->callAPISuccess('GroupContact', 'create', array(
420 'contact_id' => $contactID,
421 'group_id' => $groups[3],
422 'status' => 'Removed',
423 ));
424 $this->callAPISuccess('GroupContact', 'create', array(
425 'contact_id' => $contactID,
426 'group_id' => $groups[4],
427 'status' => 'Removed',
428 ));
429 $this->callAPISuccess('GroupContact', 'create', array(
430 'contact_id' => $contactID,
431 'group_id' => $groups[5],
432 'status' => 'Removed',
433 ));
434 $this->callAPISuccess('GroupContact', 'create', array(
435 'contact_id' => $contact2ID,
436 'group_id' => $groups[1],
437 ));
438 $this->callAPISuccess('GroupContact', 'create', array(
439 'contact_id' => $contact2ID,
440 'group_id' => $groups[2],
441 'status' => 'Removed',
442 ));
443 $this->callAPISuccess('GroupContact', 'create', array(
444 'contact_id' => $contact2ID,
445 'group_id' => $groups[4],
446 ));
447 $this->callAPISuccess('GroupContact', 'create', array(
448 'contact_id' => $contact2ID,
449 'group_id' => $groups[5],
450 'status' => 'Removed',
451 ));
452 $this->callAPISuccess('GroupContact', 'create', array(
453 'contact_id' => $contact2ID,
454 'group_id' => $groups[6],
455 ));
456 $this->callAPISuccess('GroupContact', 'create', array(
457 'contact_id' => $contact2ID,
458 'group_id' => $groups[7],
459 'status' => 'Removed',
460 ));
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(
467 $groups[0],
468 $groups[1],
469 $groups[2],
470 $groups[4],
471 $groups[6],
472 );
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));
477
478 }
479 }
480
481 /**
482 * Test the decisions made for addresses when merging.
483 *
484 * @dataProvider getMergeLocationData
485 *
486 * Scenarios:
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).
508 *
509 * @param array $dataSet
510 */
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));
516 }
517 foreach ($dataSet['contact_2'] as $address) {
518 $this->callAPISuccess($dataSet['entity'], 'create', array_merge(array('contact_id' => $contactID2), $address));
519 }
520
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);
530 }
531 else {
532 $this->assertEquals($addresses['values'][$index][$key], $value, "mismatch on $key" . (!empty($dataSet['description']) ? " on dataset {$dataSet['description']}" : ''));
533 }
534 }
535 }
536 }
537
538 /**
539 * Test altering the address decision by hook.
540 *
541 * @dataProvider getMergeLocationData
542 *
543 * @param array $dataSet
544 */
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));
552 }
553 foreach ($dataSet['contact_2'] as $address) {
554 $this->callAPISuccess($dataSet['entity'], 'create', array_merge(array('contact_id' => $contactID2), $address));
555 }
556 $this->hookClass->setHook('civicrm_alterLocationMergeData', array($this, 'hookMostRecentDonor'));
557
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);
567 }
568 else {
569 $this->assertEquals($value, $addresses['values'][$index][$key], $dataSet['entity'] . ': Unexpected value for ' . $key . (!empty($dataSet['description']) ? " on dataset {$dataSet['description']}" : ''));
570 }
571 }
572 }
573 }
574
575 /**
576 * Test the organization will not be matched to an individual.
577 */
578 public function testBatchMergeWillNotMergeOrganizationToIndividual() {
579 $individual = $this->callAPISuccess('Contact', 'create', array(
580 'contact_type' => 'Individual',
581 'organization_name' => 'Anon',
582 'email' => 'anonymous@hacker.com',
583 ));
584 $organization = $this->callAPISuccess('Contact', 'create', array(
585 'contact_type' => 'Organization',
586 'organization_name' => 'Anon',
587 'email' => 'anonymous@hacker.com',
588 ));
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']));
594
595 }
596
597 /**
598 * Test hook allowing modification of the data calculated for merging locations.
599 *
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.
605 *
606 * This has been added to the test suite to ensure the code supports more this
607 * type of intervention.
608 *
609 * @param array $blocksDAO
610 * Array of location DAO to be saved. These are arrays in 2 keys 'update' & 'delete'.
611 * @param int $mainId
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.
617 *
618 * @return mixed
619 */
620 public function hookMostRecentDonor(&$blocksDAO, $mainId, $otherId, $migrationInfo) {
621
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),
626 ));
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;
643 }
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']]))
650 ) {
651 $blocksDAO[$blockType]['delete'][$mainBlock['id']] = _civicrm_api3_load_DAO($blockType);
652 $blocksDAO[$blockType]['delete'][$mainBlock['id']]->id = $mainBlock['id'];
653 }
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'];
661 }
662
663 }
664 $blocksDAO[$blockType]['update'][$primaryAddressID]->is_primary = 1;
665 }
666 }
667 }
668 }
669 }
670 }
671
672 /**
673 * Get address combinations for the merge test.
674 *
675 * @return array
676 */
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',
686 ))));
687 return $data;
688
689 }
690
691 /**
692 * Test the batch merge does not create duplicate emails.
693 *
694 * Test CRM-18546, a 4.7 regression whereby a merged contact gets duplicate emails.
695 */
696 public function testBatchMergeEmailHandling() {
697 for ($x = 0; $x <= 4; $x++) {
698 $id = $this->individualCreate(array('email' => 'batman@gotham.met'));
699 }
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'])),
708 ), 1);
709 $this->callAPISuccessGetCount('Email', array(
710 'email' => 'batman@gotham.met',
711 'contact_id' => array('IN' => array_keys($deletedContacts['values'])),
712 ), 4);
713 }
714
715 /**
716 * Test the batch merge respects email "on hold".
717 *
718 * Test CRM-19148, Batch merge - Email on hold data lost when there is a conflict.
719 *
720 * @dataProvider getOnHoldSets
721 *
722 * @param
723 */
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',
729 'is_primary' => 1,
730 'on_hold' => $onHold1,
731 ),
732 ));
733 $contactID2 = $this->individualCreate(array(
734 'api.email.create' => array(
735 'email' => 'batman@gotham.met',
736 'location_type_id' => 'Work',
737 'is_primary' => 1,
738 'on_hold' => $onHold2,
739 ),
740 ));
741 $result = $this->callAPISuccess('Job', 'process_batch_merge', array());
742 $this->assertEquals($merge, count($result['values']['merged']));
743 }
744
745 /**
746 * Data provider for testBatchMergeEmailOnHold: combinations of on_hold & expected outcomes.
747 */
748 public function getOnHoldSets() {
749 // Each row specifies: contact 1 on_hold, contact 2 on_hold, merge? (0 or 1),
750 $sets = array(
751 array(0, 0, 1),
752 array(0, 1, 0),
753 array(1, 0, 0),
754 array(1, 1, 1),
755 );
756 return $sets;
757 }
758
759 /**
760 * Test the batch merge does not fatal on an empty rule.
761 *
762 * @dataProvider getRuleSets
763 *
764 * @param string $contactType
765 * @param string $used
766 * @param bool $isReserved
767 * @param int $threshold
768 */
769 public function testBatchMergeEmptyRule($contactType, $used, $name, $isReserved, $threshold) {
770 $ruleGroup = $this->callAPISuccess('RuleGroup', 'create', array(
771 'contact_type' => $contactType,
772 'threshold' => $threshold,
773 'used' => $used,
774 'name' => $name,
775 'is_reserved' => $isReserved,
776 ));
777 $this->callAPISuccess('Job', 'process_batch_merge', array('rule_group_id' => $ruleGroup['id']));
778 $this->callAPISuccess('RuleGroup', 'delete', array('id' => $ruleGroup['id']));
779 }
780
781 /**
782 * Get the various rule combinations.
783 */
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);
795 }
796 }
797 return $ruleGroups;
798 }
799
800 /**
801 * Test the batch merge does not create duplicate emails.
802 *
803 * Test CRM-18546, a 4.7 regression whereby a merged contact gets duplicate emails.
804 */
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',
811 'city' => 'Gotham',
812 'postal_code' => 'Nananananana',
813 ),
814 ));
815 }
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',
821 'city' => 'Gotham',
822 'postal_code' => 'Nananananana',
823 ),
824 ));
825
826 $this->individualCreate(array(
827 'api.address.create' => array(
828 'location_type_id' => 'Home',
829 'street_address' => 'Appt 115, The Batcave',
830 'city' => 'Gotham',
831 'postal_code' => 'Batman',
832 ),
833 ));
834
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'])),
844 ), 3);
845
846 $this->callAPISuccessGetCount('Address', array(
847 'street_address' => 'Appt 115, The Batcave',
848 'contact_id' => array('IN' => array_keys($deletedContacts['values'])),
849 ), 2);
850 }
851
852 /**
853 * Test the batch merge by id range.
854 *
855 * We have 2 sets of 5 matches & set the merge only to merge the lower set.
856 */
857 public function testBatchMergeIDRange() {
858 for ($x = 0; $x <= 4; $x++) {
859 $id = $this->individualCreate(array('email' => 'batman@gotham.met'));
860 }
861 for ($x = 0; $x <= 4; $x++) {
862 $this->individualCreate(array('email' => 'robin@gotham.met'));
863 }
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'])),
873 ), 1);
874 $this->callAPISuccessGetCount('Email', array(
875 'email' => 'batman@gotham.met',
876 'contact_id' => array('IN' => array_keys($deletedContacts['values'])),
877 ), 1);
878 $this->callAPISuccessGetCount('Email', array(
879 'email' => 'robin@gotham.met',
880 'contact_id' => array('IN' => array_keys($contacts['values'])),
881 ), 5);
882
883 }
884
885 /**
886 * Test the batch merge function actually works!
887 *
888 * @dataProvider getMergeSets
889 *
890 * @param $dataSet
891 */
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);
896 }
897
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');
901 }
902
903 /**
904 * Test the batch merge function actually works!
905 *
906 * @dataProvider getMergeSets
907 *
908 * @param $dataSet
909 */
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);
914 }
915
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']));
919 }
920
921 /**
922 * Get data for batch merge.
923 */
924 public function getMergeSets() {
925 $data = array(
926 array(
927 array(
928 'mode' => 'safe',
929 'contacts' => array(
930 array(
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',
939 ),
940 ),
941 array(
942 'first_name' => 'Michael',
943 'last_name' => 'Jackson',
944 'email' => 'michael@neverland.com',
945 'contact_type' => 'Individual',
946 'contact_sub_type' => 'Student',
947 ),
948 ),
949 'skipped' => 0,
950 'merged' => 1,
951 'expected' => array(
952 array(
953 'first_name' => 'Michael',
954 'last_name' => 'Jackson',
955 'email' => 'michael@neverland.com',
956 'contact_type' => 'Individual',
957 ),
958 ),
959 ),
960 ),
961 array(
962 array(
963 'mode' => 'safe',
964 'contacts' => array(
965 array(
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',
974 ),
975 ),
976 array(
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',
985 ),
986 ),
987 ),
988 'skipped' => 1,
989 'merged' => 0,
990 'expected' => array(
991 array(
992 'first_name' => 'Michael',
993 'last_name' => 'Jackson',
994 'email' => 'michael@neverland.com',
995 'contact_type' => 'Individual',
996 'street_address' => 'big house',
997 ),
998 array(
999 'first_name' => 'Michael',
1000 'last_name' => 'Jackson',
1001 'email' => 'michael@neverland.com',
1002 'contact_type' => 'Individual',
1003 'street_address' => 'bigger house',
1004 ),
1005 ),
1006 ),
1007 ),
1008 array(
1009 array(
1010 'mode' => 'safe',
1011 'contacts' => array(
1012 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',
1021 ),
1022 ),
1023 array(
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',
1032 ),
1033 ),
1034 ),
1035 'skipped' => 1,
1036 'merged' => 0,
1037 'expected' => array(
1038 array(
1039 'first_name' => 'Michael',
1040 'last_name' => 'Jackson',
1041 'email' => 'michael@neverland.com',
1042 'contact_type' => 'Individual',
1043 ),
1044 array(
1045 'first_name' => 'Michael',
1046 'last_name' => 'Jackson',
1047 'email' => 'michael@neverland.com',
1048 'contact_type' => 'Individual',
1049 ),
1050 ),
1051 ),
1052 ),
1053 array(
1054 array(
1055 'mode' => 'safe',
1056 'contacts' => array(
1057 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',
1066 ),
1067 ),
1068 array(
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(
1075 'phone' => '23456',
1076 'location_type_id' => 'Work',
1077 ),
1078 ),
1079 ),
1080 'skipped' => 1,
1081 'merged' => 0,
1082 'expected' => array(
1083 array(
1084 'first_name' => 'Michael',
1085 'last_name' => 'Jackson',
1086 'email' => 'michael@neverland.com',
1087 'contact_type' => 'Individual',
1088 ),
1089 array(
1090 'first_name' => 'Michael',
1091 'last_name' => 'Jackson',
1092 'email' => 'michael@neverland.com',
1093 'contact_type' => 'Individual',
1094 ),
1095 ),
1096 ),
1097 ),
1098 array(
1099 array(
1100 'mode' => 'aggressive',
1101 'contacts' => array(
1102 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',
1111 ),
1112 ),
1113 array(
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',
1122 ),
1123 ),
1124 ),
1125 'skipped' => 0,
1126 'merged' => 1,
1127 'expected' => array(
1128 array(
1129 'first_name' => 'Michael',
1130 'last_name' => 'Jackson',
1131 'email' => 'michael@neverland.com',
1132 'contact_type' => 'Individual',
1133 'street_address' => 'big house',
1134 ),
1135 ),
1136 ),
1137 ),
1138 array(
1139 array(
1140 'mode' => 'safe',
1141 'contacts' => array(
1142 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',
1151 ),
1152 ),
1153 array(
1154 'first_name' => 'Michael',
1155 'last_name' => 'Jackson',
1156 'email' => 'michael@neverland.com',
1157 'contact_type' => 'Individual',
1158 'contact_sub_type' => 'Student',
1159 'is_deceased' => 1,
1160 ),
1161 ),
1162 'skipped' => 1,
1163 'merged' => 0,
1164 'expected' => array(
1165 array(
1166 'first_name' => 'Michael',
1167 'last_name' => 'Jackson',
1168 'email' => 'michael@neverland.com',
1169 'contact_type' => 'Individual',
1170 'is_deceased' => 0,
1171 ),
1172 array(
1173 'first_name' => 'Michael',
1174 'last_name' => 'Jackson',
1175 'email' => 'michael@neverland.com',
1176 'contact_type' => 'Individual',
1177 'is_deceased' => 1,
1178 ),
1179 ),
1180 ),
1181 ),
1182 array(
1183 array(
1184 'mode' => 'safe',
1185 'contacts' => array(
1186 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',
1195 ),
1196 'is_deceased' => 1,
1197 ),
1198 array(
1199 'first_name' => 'Michael',
1200 'last_name' => 'Jackson',
1201 'email' => 'michael@neverland.com',
1202 'contact_type' => 'Individual',
1203 'contact_sub_type' => 'Student',
1204 ),
1205 ),
1206 'skipped' => 1,
1207 'merged' => 0,
1208 'expected' => array(
1209 array(
1210 'first_name' => 'Michael',
1211 'last_name' => 'Jackson',
1212 'email' => 'michael@neverland.com',
1213 'contact_type' => 'Individual',
1214 'is_deceased' => 1,
1215 ),
1216 array(
1217 'first_name' => 'Michael',
1218 'last_name' => 'Jackson',
1219 'email' => 'michael@neverland.com',
1220 'contact_type' => 'Individual',
1221 'is_deceased' => 0,
1222 ),
1223 ),
1224 ),
1225 ),
1226 );
1227
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',
1235 );
1236
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',
1248 );
1249 $contact2 = $contactParams;
1250
1251 $contact2[$key] = $value;
1252 $data[$key . '_conflict'] = array(
1253 array(
1254 'mode' => 'safe',
1255 'contacts' => array($contactParams, $contact2),
1256 'skipped' => 1,
1257 'merged' => 0,
1258 'expected' => array($contactParams, $contact2),
1259 ),
1260 );
1261 }
1262
1263 return $data;
1264 }
1265
1266 /**
1267 * @param $op
1268 * @param string $objectName
1269 * @param int $id
1270 * @param array $params
1271 */
1272 public function hookPreRelationship($op, $objectName, $id, &$params) {
1273 if ($op == 'delete') {
1274 return;
1275 }
1276 if ($params['is_active']) {
1277 $params['description'] = 'Hooked';
1278 }
1279 else {
1280 $params['description'] = 'Go Go you good thing';
1281 }
1282 }
1283
1284 /**
1285 * Get the location data set.
1286 *
1287 * @param array $locationParams1
1288 * @param array $locationParams2
1289 * @param string $entity
1290 *
1291 * @return array
1292 */
1293 public function getMergeLocations($locationParams1, $locationParams2, $entity, $additionalExpected = array()) {
1294 $data = array(
1295 array(
1296 'matching_primary' => array(
1297 'entity' => $entity,
1298 'contact_1' => array(
1299 array_merge(array(
1300 'location_type_id' => 'Main',
1301 'is_primary' => 1,
1302 ), $locationParams1),
1303 array_merge(array(
1304 'location_type_id' => 'Work',
1305 'is_primary' => 0,
1306 ), $locationParams2),
1307 ),
1308 'contact_2' => array(
1309 array_merge(array(
1310 'location_type_id' => 'Main',
1311 'is_primary' => 1,
1312 ), $locationParams1),
1313 ),
1314 'expected' => array_merge($additionalExpected, array(
1315 array_merge(array(
1316 'location_type_id' => 'Main',
1317 'is_primary' => 1,
1318 ), $locationParams1),
1319 array_merge(array(
1320 'location_type_id' => 'Work',
1321 'is_primary' => 0,
1322 ), $locationParams2),
1323 )),
1324 'expected_hook' => array_merge($additionalExpected, array(
1325 array_merge(array(
1326 'location_type_id' => 'Main',
1327 'is_primary' => 1,
1328 ), $locationParams1),
1329 array_merge(array(
1330 'location_type_id' => 'Work',
1331 'is_primary' => 0,
1332 ), $locationParams2),
1333 )),
1334 ),
1335 ),
1336 array(
1337 'matching_primary_reverse' => array(
1338 'entity' => $entity,
1339 'contact_1' => array(
1340 array_merge(array(
1341 'location_type_id' => 'Main',
1342 'is_primary' => 1,
1343 ), $locationParams1),
1344 ),
1345 'contact_2' => array(
1346 array_merge(array(
1347 'location_type_id' => 'Main',
1348 'is_primary' => 1,
1349 ), $locationParams1),
1350 array_merge(array(
1351 'location_type_id' => 'Work',
1352 'is_primary' => 0,
1353 ), $locationParams2),
1354 ),
1355 'expected' => array_merge($additionalExpected, array(
1356 array_merge(array(
1357 'location_type_id' => 'Main',
1358 'is_primary' => 1,
1359 ), $locationParams1),
1360 array_merge(array(
1361 'location_type_id' => 'Work',
1362 'is_primary' => 0,
1363 ), $locationParams2),
1364 )),
1365 'expected_hook' => array_merge($additionalExpected, array(
1366 array_merge(array(
1367 'location_type_id' => 'Main',
1368 'is_primary' => 1,
1369 ), $locationParams1),
1370 array_merge(array(
1371 'location_type_id' => 'Work',
1372 'is_primary' => 0,
1373 ), $locationParams2),
1374 )),
1375 ),
1376 ),
1377 array(
1378 'only_one_has_address' => array(
1379 'entity' => $entity,
1380 'contact_1' => array(
1381 array_merge(array(
1382 'location_type_id' => 'Main',
1383 'is_primary' => 1,
1384 ), $locationParams1),
1385 array_merge(array(
1386 'location_type_id' => 'Work',
1387 'is_primary' => 0,
1388 ), $locationParams2),
1389 ),
1390 'contact_2' => array(),
1391 'expected' => array_merge($additionalExpected, array(
1392 array_merge(array(
1393 'location_type_id' => 'Main',
1394 'is_primary' => 1,
1395 ), $locationParams1),
1396 array_merge(array(
1397 'location_type_id' => 'Work',
1398 'is_primary' => 0,
1399 ), $locationParams2),
1400 )),
1401 'expected_hook' => array_merge($additionalExpected, array(
1402 array_merge(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),
1408 array_merge(array(
1409 'location_type_id' => 'Work',
1410 'is_primary' => 0,
1411 ), $locationParams2),
1412 )),
1413 ),
1414 ),
1415 array(
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(
1421 array_merge(array(
1422 'location_type_id' => 'Main',
1423 'is_primary' => 1,
1424 ), $locationParams1),
1425 array_merge(array(
1426 'location_type_id' => 'Work',
1427 'is_primary' => 0,
1428 ), $locationParams2),
1429 ),
1430 'expected' => array_merge($additionalExpected, array(
1431 array_merge(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),
1437 array_merge(array(
1438 'location_type_id' => 'Work',
1439 'is_primary' => 0,
1440 ), $locationParams2),
1441 )),
1442 'expected_hook' => array_merge($additionalExpected, array(
1443 array_merge(array(
1444 'location_type_id' => 'Main',
1445 'is_primary' => 1,
1446 ), $locationParams1),
1447 array_merge(array(
1448 'location_type_id' => 'Work',
1449 'is_primary' => 0,
1450 ), $locationParams2),
1451 )),
1452 ),
1453 ),
1454 array(
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(
1459 array_merge(array(
1460 'location_type_id' => 'Main',
1461 'is_primary' => 1,
1462 ), $locationParams1),
1463 ),
1464 'contact_2' => array(
1465 array_merge(array(
1466 'location_type_id' => 'Work',
1467 'is_primary' => 1,
1468 ), $locationParams2),
1469 ),
1470 'expected' => array_merge($additionalExpected, array(
1471 array_merge(array(
1472 'location_type_id' => 'Main',
1473 'is_primary' => 1,
1474 ), $locationParams1),
1475 array_merge(array(
1476 'location_type_id' => 'Work',
1477 'is_primary' => 0,
1478 ), $locationParams2),
1479 )),
1480 'expected_hook' => array_merge($additionalExpected, array(
1481 array_merge(array(
1482 'location_type_id' => 'Main',
1483 'is_primary' => 0,
1484 ), $locationParams1),
1485 array_merge(array(
1486 'location_type_id' => 'Work',
1487 'is_primary' => 1,
1488 ), $locationParams2),
1489 )),
1490 ),
1491 ),
1492 array(
1493 'different_primaries_with_different_location_type_reverse' => array(
1494 'entity' => $entity,
1495 'contact_1' => array(
1496 array_merge(array(
1497 'location_type_id' => 'Work',
1498 'is_primary' => 1,
1499 ), $locationParams2),
1500 ),
1501 'contact_2' => array(
1502 array_merge(array(
1503 'location_type_id' => 'Main',
1504 'is_primary' => 1,
1505 ), $locationParams1),
1506 ),
1507 'expected' => array_merge($additionalExpected, array(
1508 array_merge(array(
1509 'location_type_id' => 'Work',
1510 'is_primary' => 1,
1511 ), $locationParams2),
1512 array_merge(array(
1513 'location_type_id' => 'Main',
1514 'is_primary' => 0,
1515 ), $locationParams1),
1516 )),
1517 'expected_hook' => array_merge($additionalExpected, array(
1518 array_merge(array(
1519 'location_type_id' => 'Work',
1520 'is_primary' => 0,
1521 ), $locationParams2),
1522 array_merge(array(
1523 'location_type_id' => 'Main',
1524 'is_primary' => 1,
1525 ), $locationParams1),
1526 )),
1527 ),
1528 ),
1529 array(
1530 'different_primaries_location_match_only_one_address' => array(
1531 'entity' => $entity,
1532 'contact_1' => array(
1533 array_merge(array(
1534 'location_type_id' => 'Main',
1535 'is_primary' => 1,
1536 ), $locationParams1),
1537 array_merge(array(
1538 'location_type_id' => 'Work',
1539 'is_primary' => 0,
1540 ), $locationParams2),
1541 ),
1542 'contact_2' => array(
1543 array_merge(array(
1544 'location_type_id' => 'Work',
1545 'is_primary' => 1,
1546 ), $locationParams2),
1547
1548 ),
1549 'expected' => array_merge($additionalExpected, array(
1550 array_merge(array(
1551 'location_type_id' => 'Main',
1552 'is_primary' => 1,
1553 ), $locationParams1),
1554 array_merge(array(
1555 'location_type_id' => 'Work',
1556 'is_primary' => 0,
1557 ), $locationParams2),
1558 )),
1559 'expected_hook' => array_merge($additionalExpected, array(
1560 array_merge(array(
1561 'location_type_id' => 'Main',
1562 'is_primary' => 0,
1563 ), $locationParams1),
1564 array_merge(array(
1565 'location_type_id' => 'Work',
1566 'is_primary' => 1,
1567 ), $locationParams2),
1568 )),
1569 ),
1570 ),
1571 array(
1572 'different_primaries_location_match_only_one_address_reverse' => array(
1573 'entity' => $entity,
1574 'contact_1' => array(
1575 array_merge(array(
1576 'location_type_id' => 'Work',
1577 'is_primary' => 1,
1578 ), $locationParams2),
1579 ),
1580 'contact_2' => array(
1581 array_merge(array(
1582 'location_type_id' => 'Main',
1583 'is_primary' => 1,
1584 ), $locationParams1),
1585 array_merge(array(
1586 'location_type_id' => 'Work',
1587 'is_primary' => 0,
1588 ), $locationParams2),
1589 ),
1590 'expected' => array_merge($additionalExpected, array(
1591 array_merge(array(
1592 'location_type_id' => 'Work',
1593 'is_primary' => 1,
1594 ), $locationParams2),
1595 array_merge(array(
1596 'location_type_id' => 'Main',
1597 'is_primary' => 0,
1598 ), $locationParams1),
1599 )),
1600 'expected_hook' => array_merge($additionalExpected, array(
1601 array_merge(array(
1602 'location_type_id' => 'Work',
1603 'is_primary' => 0,
1604 ), $locationParams2),
1605 array_merge(array(
1606 'location_type_id' => 'Main',
1607 'is_primary' => 1,
1608 ), $locationParams1),
1609 )),
1610 ),
1611 ),
1612 array(
1613 'same_primaries_different_location' => array(
1614 'entity' => $entity,
1615 'contact_1' => array(
1616 array_merge(array(
1617 'location_type_id' => 'Main',
1618 'is_primary' => 1,
1619 ), $locationParams1),
1620 ),
1621 'contact_2' => array(
1622 array_merge(array(
1623 'location_type_id' => 'Work',
1624 'is_primary' => 1,
1625 ), $locationParams1),
1626
1627 ),
1628 'expected' => array_merge($additionalExpected, array(
1629 array_merge(array(
1630 'location_type_id' => 'Main',
1631 'is_primary' => 1,
1632 ), $locationParams1),
1633 array_merge(array(
1634 'location_type_id' => 'Work',
1635 'is_primary' => 0,
1636 ), $locationParams1),
1637 )),
1638 'expected_hook' => array_merge($additionalExpected, array(
1639 array_merge(array(
1640 'location_type_id' => 'Work',
1641 'is_primary' => 1,
1642 ), $locationParams1),
1643 )),
1644 ),
1645 ),
1646 array(
1647 'same_primaries_different_location_reverse' => array(
1648 'entity' => $entity,
1649 'contact_1' => array(
1650 array_merge(array(
1651 'location_type_id' => 'Work',
1652 'is_primary' => 1,
1653 ), $locationParams1),
1654 ),
1655 'contact_2' => array(
1656 array_merge(array(
1657 'location_type_id' => 'Main',
1658 'is_primary' => 1,
1659 ), $locationParams1),
1660 ),
1661 'expected' => array_merge($additionalExpected, array(
1662 array_merge(array(
1663 'location_type_id' => 'Work',
1664 'is_primary' => 1,
1665 ), $locationParams1),
1666 array_merge(array(
1667 'location_type_id' => 'Main',
1668 'is_primary' => 0,
1669 ), $locationParams1),
1670 )),
1671 'expected_hook' => array_merge($additionalExpected, array(
1672 array_merge(array(
1673 'location_type_id' => 'Main',
1674 'is_primary' => 1,
1675 ), $locationParams1),
1676 )),
1677 ),
1678 ),
1679 );
1680 return $data;
1681 }
1682
1683 }