3 * +--------------------------------------------------------------------+
4 * | CiviCRM version 5 |
5 * +--------------------------------------------------------------------+
6 * | Copyright CiviCRM LLC (c) 2004-2019 |
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 * Class contains api test cases for "civicrm_relationship"
32 class api_v3_RelationshipTest
extends CiviUnitTestCase
{
34 use CRMTraits_Custom_CustomDataTrait
;
36 protected $_apiversion = 3;
45 * Second organization contact.
50 protected $_relTypeID;
52 protected $_customFieldId = NULL;
60 public function setUp() {
62 $this->_cId_a
= $this->individualCreate();
63 $this->_cId_a_2
= $this->individualCreate(array(
66 'contact_type' => 'Individual',
68 $this->_cId_b
= $this->organizationCreate();
69 $this->_cId_b2
= $this->organizationCreate(array('organization_name' => ' Org 2'));
70 $this->entity
= 'Relationship';
71 //Create a relationship type.
72 $relTypeParams = array(
73 'name_a_b' => 'Relation 1 for delete',
74 'name_b_a' => 'Relation 2 for delete',
75 'description' => 'Testing relationship type',
76 'contact_type_a' => 'Individual',
77 'contact_type_b' => 'Organization',
82 $this->_relTypeID
= $this->relationshipTypeCreate($relTypeParams);
83 $this->_params
= array(
84 'contact_id_a' => $this->_cId_a
,
85 'contact_id_b' => $this->_cId_b
,
86 'relationship_type_id' => $this->_relTypeID
,
87 'start_date' => '2008-12-20',
98 public function tearDown() {
99 $this->contactDelete($this->_cId_a
);
100 $this->contactDelete($this->_cId_a_2
);
101 $this->contactDelete($this->_cId_b
);
102 $this->contactDelete($this->_cId_b2
);
103 $this->quickCleanup(array('civicrm_relationship'), TRUE);
104 $this->relationshipTypeDelete($this->_relTypeID
);
108 * Check with empty array.
110 public function testRelationshipCreateEmpty() {
111 $this->callAPIFailure('relationship', 'create', array());
115 * Test Current Employer is correctly set.
117 public function testCurrentEmployerRelationship() {
118 $employerRelationshipID = $this->callAPISuccessGetValue('RelationshipType', array(
120 'name_b_a' => "Employer Of",
122 $employerRelationship = $this->callAPISuccess('Relationship', 'create', array(
123 'contact_id_a' => $this->_cId_a
,
124 'contact_id_b' => $this->_cId_b
,
125 'relationship_type_id' => $employerRelationshipID,
127 $params = array($this->_cId_a
=> $this->_cId_b
);
128 CRM_Contact_BAO_Contact_Utils
::setCurrentEmployer($params);
130 //Check if current employer is correctly set.
131 $employer = $this->callAPISuccessGetValue('Contact', array(
132 'return' => "current_employer",
133 'id' => $this->_cId_a
,
135 $organisation = $this->callAPISuccessGetValue('Contact', array(
136 'return' => "sort_name",
137 'id' => $this->_cId_b
,
139 $this->assertEquals($employer, $organisation);
141 //Update relationship type
142 $update = $this->callAPISuccess('Relationship', 'create', array(
143 'id' => $employerRelationship['id'],
144 'relationship_type_id' => $this->_relTypeID
,
146 $employeeContact = $this->callAPISuccessGetSingle('Contact', array(
147 'return' => array("current_employer"),
148 'id' => $this->_cId_a
,
150 //current employer should be removed.
151 $this->assertEmpty($employeeContact['current_employer']);
155 * Check if required fields are not passed.
157 public function testRelationshipCreateWithoutRequired() {
159 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
160 'end_date' => array('d' => '10', 'M' => '1', 'Y' => '2009'),
164 $this->callAPIFailure('relationship', 'create', $params);
168 * Check with incorrect required fields.
170 public function testRelationshipCreateWithIncorrectData() {
173 'contact_id_a' => $this->_cId_a
,
174 'contact_id_b' => $this->_cId_b
,
175 'relationship_type_id' => 'Breaking Relationship',
178 $this->callAPIFailure('relationship', 'create', $params);
180 //contact id is not an integer
182 'contact_id_a' => 'invalid',
183 'contact_id_b' => $this->_cId_b
,
184 'relationship_type_id' => $this->_relTypeID
,
185 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
188 $this->callAPIFailure('relationship', 'create', $params);
190 // Contact id does not exist.
191 $params['contact_id_a'] = 999;
192 $this->callAPIFailure('relationship', 'create', $params);
195 $params['contact_id_a'] = $this->_cId_a
;
196 $params['start_date'] = array('d' => '1', 'M' => '1');
197 $this->callAPIFailure('relationship', 'create', $params);
201 * Check relationship creation with invalid Relationship.
203 public function testRelationshipCreateInvalidRelationship() {
204 // Both have the contact type Individual.
206 'contact_id_a' => $this->_cId_a
,
207 'contact_id_b' => $this->_cId_a
,
208 'relationship_type_id' => $this->_relTypeID
,
209 'start_date' => '2008-01-10',
213 $this->callAPIFailure('relationship', 'create', $params);
215 // both the contact of type Organization
217 'contact_id_a' => $this->_cId_b
,
218 'contact_id_b' => $this->_cId_b
,
219 'relationship_type_id' => $this->_relTypeID
,
220 'start_date' => '2008-01-10',
224 $this->callAPIFailure('relationship', 'create', $params);
228 * Check relationship already exists.
230 public function testRelationshipCreateAlreadyExists() {
232 'contact_id_a' => $this->_cId_a
,
233 'contact_id_b' => $this->_cId_b
,
234 'relationship_type_id' => $this->_relTypeID
,
235 'start_date' => '2008-12-20',
239 $relationship = $this->callAPISuccess('relationship', 'create', $params);
242 'contact_id_a' => $this->_cId_a
,
243 'contact_id_b' => $this->_cId_b
,
244 'relationship_type_id' => $this->_relTypeID
,
245 'start_date' => '2008-12-20',
248 $this->callAPIFailure('relationship', 'create', $params, 'Duplicate Relationship');
250 $params['id'] = $relationship['id'];
251 $this->callAPISuccess('relationship', 'delete', $params);
255 * Check relationship already exists.
257 public function testRelationshipCreateUpdateAlreadyExists() {
259 'contact_id_a' => $this->_cId_a
,
260 'contact_id_b' => $this->_cId_b
,
261 'relationship_type_id' => $this->_relTypeID
,
262 'start_date' => '2008-12-20',
267 $relationship = $this->callAPISuccess('relationship', 'create', $params);
269 'id' => $relationship['id'],
273 $this->callAPISuccess('relationship', 'create', $params);
274 $this->callAPISuccess('relationship', 'get', $params);
275 $params['id'] = $relationship['id'];
276 $this->callAPISuccess('relationship', 'delete', $params);
280 * Check update doesn't reset stuff badly - CRM-11789.
282 public function testRelationshipCreateUpdateDoesNotMangle() {
284 'contact_id_a' => $this->_cId_a
,
285 'contact_id_b' => $this->_cId_b
,
286 'relationship_type_id' => $this->_relTypeID
,
287 'start_date' => '2008-12-20',
289 'is_permission_a_b' => 1,
290 'description' => 'my desc',
292 $relationship = $this->callAPISuccess('relationship', 'create', $params);
294 $updateParams = array(
295 'id' => $relationship['id'],
296 'relationship_type_id' => $this->_relTypeID
,
298 $this->callAPISuccess('relationship', 'create', $updateParams);
300 //make sure the orig params didn't get changed
301 $this->getAndCheck($params, $relationship['id'], 'relationship');
306 * Check relationship creation.
308 public function testRelationshipCreate() {
310 'contact_id_a' => $this->_cId_a
,
311 'contact_id_b' => $this->_cId_b
,
312 'relationship_type_id' => $this->_relTypeID
,
313 'start_date' => '2010-10-30',
314 'end_date' => '2010-12-30',
319 $result = $this->callAPIAndDocument('relationship', 'create', $params, __FUNCTION__
, __FILE__
);
320 $this->assertNotNull($result['id']);
321 $relationParams = array(
322 'id' => $result['id'],
325 // assertDBState compares expected values in $result to actual values in the DB
326 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
327 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
328 $values = $result['values'][$result['id']];
329 foreach ($params as $key => $value) {
330 if ($key == 'note') {
333 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE));
335 $params['id'] = $result['id'];
336 $this->callAPISuccess('relationship', 'delete', $params);
340 * Ensure disabling works.
342 public function testRelationshipUpdate() {
343 $result = $this->callAPISuccess('relationship', 'create', $this->_params
);
344 $relID = $result['id'];
345 $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'description' => 'blah'));
346 $this->assertEquals($relID, $result['id']);
348 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
350 $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'is_permission_b_a' => 1));
351 $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
352 $result = $this->callAPISuccess('relationship', 'create', array('id' => $result['id'], 'is_active' => 0));
353 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
354 $this->assertEquals(0, $result['values'][$result['id']]['is_active']);
355 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
356 $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
360 * Check relationship creation.
362 public function testRelationshipCreateEmptyEndDate() {
364 'contact_id_a' => $this->_cId_a
,
365 'contact_id_b' => $this->_cId_b
,
366 'relationship_type_id' => $this->_relTypeID
,
367 'start_date' => '2010-10-30',
373 $result = $this->callAPISuccess('relationship', 'create', $params);
374 $this->assertNotNull($result['id']);
375 $relationParams = array(
376 'id' => $result['id'],
379 // assertDBState compares expected values in $result to actual values in the DB
380 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
381 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
382 $values = $result['values'][$result['id']];
383 foreach ($params as $key => $value) {
384 if ($key == 'note') {
387 if ($key == 'end_date') {
388 $this->assertTrue(empty($values[$key]));
391 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__
);
393 $params['id'] = $result['id'];
394 $this->callAPISuccess('relationship', 'delete', $params);
398 * Check relationship creation with custom data.
400 public function testRelationshipCreateEditWithCustomData() {
401 $this->createCustomGroup();
402 $this->_ids
= $this->createCustomFieldsOfAllTypes();
403 //few custom Values for comparing
404 $custom_params = array(
405 "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship',
406 "custom_{$this->_ids[1]}" => 'Y',
407 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
408 "custom_{$this->_ids[3]}" => 'http://example.com',
412 'contact_id_a' => $this->_cId_a
,
413 'contact_id_b' => $this->_cId_b
,
414 'relationship_type_id' => $this->_relTypeID
,
415 'start_date' => '2008-12-20',
418 $params = array_merge($params, $custom_params);
419 $result = $this->callAPISuccess('relationship', 'create', $params);
421 $relationParams = array(
422 'id' => $result['id'],
424 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
426 //Test Edit of custom field from the form.
427 $getParams = array('id' => $result['id']);
428 $updateParams = array_merge($getParams, array(
429 "custom_{$this->_ids[0]}" => 'Edited Text Value',
430 'relationship_type_id' => $this->_relTypeID
. '_b_a',
431 'related_contact_id' => $this->_cId_a
,
433 $reln = new CRM_Contact_Form_Relationship();
434 $reln->_action
= CRM_Core_Action
::UPDATE
;
435 $reln->_relationshipId
= $result['id'];
436 $reln->submit($updateParams);
438 $check = $this->callAPISuccess('relationship', 'get', $getParams);
439 $this->assertEquals("Edited Text Value", $check['values'][$check['id']]["custom_{$this->_ids[0]}"]);
441 $params['id'] = $result['id'];
442 $this->callAPISuccess('relationship', 'delete', $params);
443 $this->relationshipTypeDelete($this->_relTypeID
);
447 * Check with complete array + custom field
448 * Note that the test is written on purpose without any
449 * variables specific to participant so it can be replicated into other entities
450 * and / or moved to the automated test suite
452 public function testGetWithCustom() {
453 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
455 $params = $this->_params
;
456 $params['custom_' . $ids['custom_field_id']] = "custom string";
458 $result = $this->callAPISuccess($this->entity
, 'create', $params);
459 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
461 $getParams = array('id' => $result['id']);
462 $check = $this->callAPIAndDocument($this->entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
463 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
465 $this->customFieldDelete($ids['custom_field_id']);
466 $this->customGroupDelete($ids['custom_group_id']);
470 * Check with empty array.
472 public function testRelationshipDeleteEmpty() {
473 $this->callAPIFailure('relationship', 'delete', array(), 'Mandatory key(s) missing from params array: id');
477 * Check if required fields are not passed.
479 public function testRelationshipDeleteWithoutRequired() {
481 'start_date' => '2008-12-20',
482 'end_date' => '2009-12-20',
486 $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
490 * Check with incorrect required fields.
492 public function testRelationshipDeleteWithIncorrectData() {
494 'contact_id_a' => $this->_cId_a
,
495 'contact_id_b' => $this->_cId_b
,
496 'relationship_type_id' => 'Breaking Relationship',
499 $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
501 $params['id'] = "Invalid";
502 $this->callAPIFailure('relationship', 'delete', $params, 'id is not a valid integer');
506 * Check relationship creation.
508 public function testRelationshipDelete() {
510 'contact_id_a' => $this->_cId_a
,
511 'contact_id_b' => $this->_cId_b
,
512 'relationship_type_id' => $this->_relTypeID
,
513 'start_date' => '2008-12-20',
517 $result = $this->callAPISuccess('relationship', 'create', $params);
518 $params = array('id' => $result['id']);
519 $this->callAPIAndDocument('relationship', 'delete', $params, __FUNCTION__
, __FILE__
);
520 $this->relationshipTypeDelete($this->_relTypeID
);
523 ///////////////// civicrm_relationship_update methods
526 * Check with empty array.
528 public function testRelationshipUpdateEmpty() {
529 $this->callAPIFailure('relationship', 'create', array(),
530 'Mandatory key(s) missing from params array: contact_id_a, contact_id_b, relationship_type_id');
534 * Check if required fields are not passed.
538 * Check relationship update.
540 public function testRelationshipCreateDuplicate() {
542 'contact_id_a' => $this->_cId_a
,
543 'contact_id_b' => $this->_cId_b
,
544 'relationship_type_id' => $this->_relTypeID
,
545 'start_date' => '20081214',
546 'end_date' => '20091214',
550 $result = $this->callAPISuccess('relationship', 'create', $relParams);
552 $this->assertNotNull($result['id']);
555 'contact_id_a' => $this->_cId_a
,
556 'contact_id_b' => $this->_cId_b
,
557 'relationship_type_id' => $this->_relTypeID
,
558 'start_date' => '20081214',
559 'end_date' => '20091214',
563 $this->callAPIFailure('relationship', 'create', $params, 'Duplicate Relationship');
565 $this->callAPISuccess('relationship', 'delete', array('id' => $result['id']));
566 $this->relationshipTypeDelete($this->_relTypeID
);
570 * CRM-13725 - Two relationships of same type with same start and end date
571 * should be OK if the custom field values differ.
573 public function testRelationshipCreateDuplicateWithCustomFields() {
574 $this->createCustomGroup();
575 $this->_ids
= $this->createCustomFieldsOfAllTypes();
577 $custom_params_1 = array(
578 "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship',
579 "custom_{$this->_ids[1]}" => 'Y',
580 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
581 "custom_{$this->_ids[3]}" => 'http://example.com',
584 $custom_params_2 = array(
585 "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
586 "custom_{$this->_ids[1]}" => 'Y',
587 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
588 "custom_{$this->_ids[3]}" => 'http://example.org',
592 'contact_id_a' => $this->_cId_a
,
593 'contact_id_b' => $this->_cId_b
,
594 'relationship_type_id' => $this->_relTypeID
,
595 'start_date' => '2008-12-20',
599 $params_1 = array_merge($params, $custom_params_1);
600 $params_2 = array_merge($params, $custom_params_2);
602 $result_1 = $this->callAPISuccess('relationship', 'create', $params_1);
603 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
605 $this->assertNotNull($result_2['id']);
606 $this->assertEquals(0, $result_2['is_error']);
608 $this->relationshipTypeDelete($this->_relTypeID
);
612 * CRM-13725 - Two relationships of same type with same start and end date
613 * should be OK if the custom field values differ. In this case, the
614 * existing relationship does not have custom values, but the new one
617 public function testRelationshipCreateDuplicateWithCustomFields2() {
618 $this->createCustomGroup();
619 $this->_ids
= $this->createCustomFieldsOfAllTypes();
621 $custom_params_2 = array(
622 "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
623 "custom_{$this->_ids[1]}" => 'Y',
624 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
625 "custom_{$this->_ids[3]}" => 'http://example.org',
629 'contact_id_a' => $this->_cId_a
,
630 'contact_id_b' => $this->_cId_b
,
631 'relationship_type_id' => $this->_relTypeID
,
632 'start_date' => '2008-12-20',
636 $params_2 = array_merge($params_1, $custom_params_2);
638 $this->callAPISuccess('relationship', 'create', $params_1);
639 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
641 $this->assertNotNull($result_2['id']);
642 $this->assertEquals(0, $result_2['is_error']);
644 $this->relationshipTypeDelete($this->_relTypeID
);
648 * CRM-13725 - Two relationships of same type with same start and end date
649 * should be OK if the custom field values differ. In this case, the
650 * existing relationship does have custom values, but the new one
653 public function testRelationshipCreateDuplicateWithCustomFields3() {
654 $this->createCustomGroup();
655 $this->_ids
= $this->createCustomFieldsOfAllTypes();
657 $custom_params_1 = array(
658 "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
659 "custom_{$this->_ids[1]}" => 'Y',
660 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
661 "custom_{$this->_ids[3]}" => 'http://example.org',
665 'contact_id_a' => $this->_cId_a
,
666 'contact_id_b' => $this->_cId_b
,
667 'relationship_type_id' => $this->_relTypeID
,
668 'start_date' => '2008-12-20',
672 $params_1 = array_merge($params_2, $custom_params_1);
674 $this->callAPISuccess('relationship', 'create', $params_1);
675 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
677 $this->assertNotNull($result_2['id']);
678 $this->assertEquals(0, $result_2['is_error']);
680 $this->relationshipTypeDelete($this->_relTypeID
);
684 * Check with valid params array.
686 public function testRelationshipsGet() {
688 'contact_id_a' => $this->_cId_a
,
689 'contact_id_b' => $this->_cId_b
,
690 'relationship_type_id' => $this->_relTypeID
,
691 'start_date' => '2011-01-01',
692 'end_date' => '2013-01-01',
696 $this->callAPISuccess('relationship', 'create', $relParams);
700 'contact_id' => $this->_cId_b
,
702 $result = $this->callAPISuccess('relationship', 'get', $params);
703 $this->assertEquals($result['count'], 1);
705 'contact_id_a' => $this->_cId_a
,
707 $result = $this->callAPISuccess('relationship', 'get', $params);
708 $this->assertEquals($result['count'], 1);
709 // contact_id_a is wrong so should be no matches
711 'contact_id_a' => $this->_cId_b
,
713 $result = $this->callAPISuccess('relationship', 'get', $params);
714 $this->assertEquals($result['count'], 0);
718 * Chain Relationship.get and to Contact.get.
720 public function testRelationshipGetWithChainedCall() {
721 // Create a relationship.
722 $createResult = $this->callAPISuccess('relationship', 'create', $this->_params
);
723 $id = $createResult['id'];
725 // Try to retrieve it using chaining.
727 'relationship_type_id' => $this->_relTypeID
,
729 'api.Contact.get' => array(
730 'id' => '$value.contact_id_b',
734 $result = $this->callAPISuccess('relationship', 'get', $params);
736 $this->assertEquals(1, $result['count']);
737 $relationship = CRM_Utils_Array
::first($result['values']);
738 $this->assertEquals(1, $relationship['api.Contact.get']['count']);
739 $contact = CRM_Utils_Array
::first($relationship['api.Contact.get']['values']);
740 $this->assertEquals($this->_cId_b
, $contact['id']);
744 * Chain Contact.get to Relationship.get and again to Contact.get.
746 public function testRelationshipGetInChainedCall() {
747 // Create a relationship.
748 $this->callAPISuccess('relationship', 'create', $this->_params
);
750 // Try to retrieve it using chaining.
752 'id' => $this->_cId_a
,
753 'api.Relationship.get' => array(
754 'relationship_type_id' => $this->_relTypeID
,
755 'contact_id_a' => '$value.id',
756 'api.Contact.get' => array(
757 'id' => '$value.contact_id_b',
762 $result = $this->callAPISuccess('contact', 'get', $params);
763 $this->assertEquals(1, $result['count']);
764 $contact = CRM_Utils_Array
::first($result['values']);
765 $this->assertEquals(1, $contact['api.Relationship.get']['count']);
766 $relationship = CRM_Utils_Array
::first($contact['api.Relationship.get']['values']);
767 $this->assertEquals(1, $relationship['api.Contact.get']['count']);
768 $contact = CRM_Utils_Array
::first($relationship['api.Contact.get']['values']);
769 $this->assertEquals($this->_cId_b
, $contact['id']);
773 * Check with valid params array.
774 * (The get function will behave differently without 'contact_id' passed
776 public function testRelationshipsGetGeneric() {
778 'contact_id_a' => $this->_cId_a
,
779 'contact_id_b' => $this->_cId_b
,
780 'relationship_type_id' => $this->_relTypeID
,
781 'start_date' => '2011-01-01',
782 'end_date' => '2013-01-01',
786 $this->callAPISuccess('relationship', 'create', $relParams);
790 'contact_id_b' => $this->_cId_b
,
792 $this->callAPISuccess('relationship', 'get', $params);
796 * Test retrieving only current relationships.
798 public function testGetIsCurrent() {
800 'contact_id_a' => $this->_cId_a
,
801 'contact_id_b' => $this->_cId_b2
,
802 'relationship_type_id' => $this->_relTypeID
,
803 'start_date' => '2008-12-20',
806 $this->callAPISuccess('relationship', 'create', $rel2Params);
807 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params
);
810 'filters' => array('is_current' => 1),
812 $description = "Demonstrates is_current filter.";
813 $subfile = 'filterIsCurrent';
814 //no relationship has been created
815 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
816 $this->assertEquals($result['count'], 1);
817 $this->AssertEquals($rel1['id'], $result['id']);
819 // now try not started
820 $rel2Params['is_active'] = 1;
821 $rel2Params['start_date'] = 'tomorrow';
822 $this->callAPISuccess('relationship', 'create', $rel2Params);
823 $result = $this->callAPISuccess('relationship', 'get', $getParams);
824 $this->assertEquals($result['count'], 1);
825 $this->AssertEquals($rel1['id'], $result['id']);
828 $rel2Params['is_active'] = 1;
829 $rel2Params['start_date'] = 'last week';
830 $rel2Params['end_date'] = 'yesterday';
831 $this->callAPISuccess('relationship', 'create', $rel2Params);
835 * Test using various operators.
837 public function testGetTypeOperators() {
838 $relTypeParams = array(
839 'name_a_b' => 'Relation 3 for delete',
840 'name_b_a' => 'Relation 6 for delete',
841 'description' => 'Testing relationship type 2',
842 'contact_type_a' => 'Individual',
843 'contact_type_b' => 'Organization',
847 $relationType2 = $this->relationshipTypeCreate($relTypeParams);
848 $relTypeParams = array(
849 'name_a_b' => 'Relation 8 for delete',
850 'name_b_a' => 'Relation 9 for delete',
851 'description' => 'Testing relationship type 7',
852 'contact_type_a' => 'Individual',
853 'contact_type_b' => 'Organization',
857 $relationType3 = $this->relationshipTypeCreate($relTypeParams);
859 $relTypeParams = array(
860 'name_a_b' => 'Relation 6 for delete',
861 'name_b_a' => 'Relation 88for delete',
862 'description' => 'Testing relationship type 00',
863 'contact_type_a' => 'Individual',
864 'contact_type_b' => 'Organization',
868 $relationType4 = $this->relationshipTypeCreate($relTypeParams);
870 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params
);
871 $rel2 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
872 array('relationship_type_id' => $relationType2)));
873 $rel3 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
874 array('relationship_type_id' => $relationType3)));
875 $rel4 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
876 array('relationship_type_id' => $relationType4)));
879 'relationship_type_id' => array('IN' => array($relationType2, $relationType3)),
882 $description = "Demonstrates use of IN filter.";
883 $subfile = 'INRelationshipType';
885 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
886 $this->assertEquals($result['count'], 2);
887 $this->AssertEquals(array($rel2['id'], $rel3['id']), array_keys($result['values']));
889 $description = "Demonstrates use of NOT IN filter.";
890 $subfile = 'NotInRelationshipType';
892 'relationship_type_id' => array('NOT IN' => array($relationType2, $relationType3)),
894 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
895 $this->assertEquals($result['count'], 2);
896 $this->AssertEquals(array($rel1['id'], $rel4['id']), array_keys($result['values']));
898 $description = "Demonstrates use of BETWEEN filter.";
899 $subfile = 'BetweenRelationshipType';
901 'relationship_type_id' => array('BETWEEN' => array($relationType2, $relationType4)),
903 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
904 $this->assertEquals($result['count'], 3);
905 $this->AssertEquals(array($rel2['id'], $rel3['id'], $rel4['id']), array_keys($result['values']));
907 $description = "Demonstrates use of Not BETWEEN filter.";
908 $subfile = 'NotBetweenRelationshipType';
910 'relationship_type_id' => array('NOT BETWEEN' => array($relationType2, $relationType4)),
912 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
913 $this->assertEquals($result['count'], 1);
914 $this->AssertEquals(array($rel1['id']), array_keys($result['values']));
919 * Check with invalid relationshipType Id.
921 public function testRelationshipTypeAddInvalidId() {
922 $relTypeParams = array(
924 'name_a_b' => 'Relation 1 for delete',
925 'name_b_a' => 'Relation 2 for delete',
926 'contact_type_a' => 'Individual',
927 'contact_type_b' => 'Organization',
929 $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
930 'id is not a valid integer');
934 * Check with valid data with contact_b.
936 public function testGetRelationshipWithContactB() {
938 'contact_id_a' => $this->_cId_a
,
939 'contact_id_b' => $this->_cId_b
,
940 'relationship_type_id' => $this->_relTypeID
,
941 'start_date' => '2011-01-01',
942 'end_date' => '2013-01-01',
946 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
949 'contact_id' => $this->_cId_a
,
952 $result = $this->callAPISuccess('relationship', 'get', $contacts);
953 $this->assertGreaterThan(0, $result['count']);
955 'id' => $relationship['id'],
957 $this->callAPISuccess('relationship', 'delete', $params);
958 $this->relationshipTypeDelete($this->_relTypeID
);
962 * Check with valid data with relationshipTypes.
964 public function testGetRelationshipWithRelTypes() {
966 'contact_id_a' => $this->_cId_a
,
967 'contact_id_b' => $this->_cId_b
,
968 'relationship_type_id' => $this->_relTypeID
,
969 'start_date' => '2011-01-01',
970 'end_date' => '2013-01-01',
974 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
977 'contact_id' => $this->_cId_a
,
979 $this->callAPISuccess('relationship', 'get', $contact_a);
982 'id' => $relationship['id'],
984 $this->callAPISuccess('relationship', 'delete', $params);
985 $this->relationshipTypeDelete($this->_relTypeID
);
989 * Checks that passing in 'contact_id' + a relationship type
990 * will filter by relationship type (relationships go in both directions)
991 * as relationship api does a reciprocal check if contact_id provided
993 * We should get 1 result without or with correct relationship type id & 0 with
996 public function testGetRelationshipByTypeReciprocal() {
997 $created = $this->callAPISuccess($this->entity
, 'create', $this->_params
);
998 $result = $this->callAPISuccess($this->entity
, 'get', array(
999 'contact_id' => $this->_cId_a
,
1000 'relationship_type_id' => $this->_relTypeID
,
1002 $this->assertEquals(1, $result['count']);
1003 $result = $this->callAPISuccess($this->entity
, 'get', array(
1004 'contact_id' => $this->_cId_a
,
1005 'relationship_type_id' => $this->_relTypeID +
1,
1007 $this->assertEquals(0, $result['count']);
1008 $this->callAPISuccess($this->entity
, 'delete', array('id' => $created['id']));
1012 * Checks that passing in 'contact_id_b' + a relationship type
1013 * will filter by relationship type for contact b
1015 * We should get 1 result without or with correct relationship type id & 0 with
1018 public function testGetRelationshipByTypeDAO() {
1019 $this->_ids
['relationship'] = $this->callAPISuccess($this->entity
, 'create', array('format.only_id' => TRUE) +
1021 $this->callAPISuccess($this->entity
, 'getcount', array(
1022 'contact_id_a' => $this->_cId_a
,
1024 $result = $this->callAPISuccess($this->entity
, 'get', array(
1025 'contact_id_a' => $this->_cId_a
,
1026 'relationship_type_id' => $this->_relTypeID
,
1028 $this->assertEquals(1, $result['count']);
1029 $result = $this->callAPISuccess($this->entity
, 'get', array(
1030 'contact_id_a' => $this->_cId_a
,
1031 'relationship_type_id' => $this->_relTypeID +
1,
1033 $this->assertEquals(0, $result['count']);
1037 * Checks that passing in 'contact_id_b' + a relationship type
1038 * will filter by relationship type for contact b
1040 * We should get 1 result without or with correct relationship type id & 0 with
1043 public function testGetRelationshipByTypeArrayDAO() {
1044 $this->callAPISuccess($this->entity
, 'create', $this->_params
);
1045 $org3 = $this->organizationCreate();
1046 // lets just assume built in ones aren't being messed with!
1048 // lets just assume built in ones aren't being messed with!
1052 $this->callAPISuccess($this->entity
, 'create',
1053 array_merge($this->_params
, array(
1054 'relationship_type_id' => $relType2,
1055 'contact_id_b' => $this->_cId_b2
,
1060 $this->callAPISuccess($this->entity
, 'create',
1061 array_merge($this->_params
, array(
1062 'relationship_type_id' => $relType3,
1063 'contact_id_b' => $org3,
1067 $result = $this->callAPISuccess($this->entity
, 'get', array(
1068 'contact_id_a' => $this->_cId_a
,
1069 'relationship_type_id' => array('IN' => array($this->_relTypeID
, $relType3)),
1072 $this->assertEquals(2, $result['count']);
1073 foreach ($result['values'] as $key => $value) {
1074 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID
, $relType3)));
1079 * Checks that passing in 'contact_id_b' + a relationship type
1080 * will filter by relationship type for contact b
1082 * We should get 1 result without or with correct relationship type id & 0 with
1085 public function testGetRelationshipByTypeArrayReciprocal() {
1086 $this->callAPISuccess($this->entity
, 'create', $this->_params
);
1087 $org3 = $this->organizationCreate();
1088 // lets just assume built in ones aren't being messed with!
1093 $this->callAPISuccess($this->entity
, 'create',
1094 array_merge($this->_params
, array(
1095 'relationship_type_id' => $relType2,
1096 'contact_id_b' => $this->_cId_b2
,
1101 $this->callAPISuccess($this->entity
, 'create',
1102 array_merge($this->_params
, array(
1103 'relationship_type_id' => $relType3,
1104 'contact_id_b' => $org3,
1108 $result = $this->callAPISuccess($this->entity
, 'get', array(
1109 'contact_id' => $this->_cId_a
,
1110 'relationship_type_id' => array('IN' => array($this->_relTypeID
, $relType3)),
1113 $this->assertEquals(2, $result['count']);
1114 foreach ($result['values'] as $key => $value) {
1115 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID
, $relType3)));
1120 * Test relationship get by membership type.
1122 * Checks that passing in 'contact_id_b' + a relationship type
1123 * will filter by relationship type for contact b
1125 * We should get 1 result without or with correct relationship type id & 0 with
1128 public function testGetRelationshipByMembershipTypeDAO() {
1129 $this->callAPISuccess($this->entity
, 'create', $this->_params
);
1130 $org3 = $this->organizationCreate();
1132 // lets just assume built in ones aren't being messed with!
1134 // lets just assume built in ones aren't being messed with!
1137 $memberType = $this->membershipTypeCreate(array(
1138 'relationship_type_id' => CRM_Core_DAO
::VALUE_SEPARATOR
. $relType1 . CRM_Core_DAO
::VALUE_SEPARATOR
. $relType3 . CRM_Core_DAO
::VALUE_SEPARATOR
,
1139 'relationship_direction' => CRM_Core_DAO
::VALUE_SEPARATOR
. 'a_b' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'b_a' . CRM_Core_DAO
::VALUE_SEPARATOR
,
1143 $this->callAPISuccess($this->entity
, 'create',
1144 array_merge($this->_params
, array(
1145 'relationship_type_id' => $relType2,
1146 'contact_id_b' => $this->_cId_b2
,
1151 $this->callAPISuccess($this->entity
, 'create',
1152 array_merge($this->_params
, array(
1153 'relationship_type_id' => $relType3,
1154 'contact_id_b' => $org3,
1158 // Relationship 4 with reversal.
1159 $this->callAPISuccess($this->entity
, 'create',
1160 array_merge($this->_params
, array(
1161 'relationship_type_id' => $relType1,
1162 'contact_id_a' => $this->_cId_a
,
1163 'contact_id_b' => $this->_cId_a_2
,
1167 $result = $this->callAPISuccess($this->entity
, 'get', array(
1168 'contact_id_a' => $this->_cId_a
,
1169 'membership_type_id' => $memberType,
1171 // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1172 $this->assertEquals(1, $result['count']);
1173 foreach ($result['values'] as $key => $value) {
1174 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1)));
1179 * Checks that passing in 'contact_id_b' + a relationship type
1180 * will filter by relationship type for contact b
1182 * We should get 1 result without or with correct relationship type id & 0 with
1185 public function testGetRelationshipByMembershipTypeReciprocal() {
1186 $this->callAPISuccess($this->entity
, 'create', $this->_params
);
1187 $org3 = $this->organizationCreate();
1189 // Let's just assume built in ones aren't being messed with!
1193 $memberType = $this->membershipTypeCreate(array(
1194 'relationship_type_id' => CRM_Core_DAO
::VALUE_SEPARATOR
. $relType1 . CRM_Core_DAO
::VALUE_SEPARATOR
. $relType3 . CRM_Core_DAO
::VALUE_SEPARATOR
,
1195 'relationship_direction' => CRM_Core_DAO
::VALUE_SEPARATOR
. 'a_b' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'b_a' . CRM_Core_DAO
::VALUE_SEPARATOR
,
1199 $this->callAPISuccess($this->entity
, 'create',
1200 array_merge($this->_params
, array(
1201 'relationship_type_id' => $relType2,
1202 'contact_id_b' => $this->_cId_b2
,
1207 $this->callAPISuccess($this->entity
, 'create',
1208 array_merge($this->_params
, array(
1209 'relationship_type_id' => $relType3,
1210 'contact_id_b' => $org3,
1214 // Relationship 4 with reversal.
1215 $this->callAPISuccess($this->entity
, 'create',
1216 array_merge($this->_params
, array(
1217 'relationship_type_id' => $relType1,
1218 'contact_id_a' => $this->_cId_a
,
1219 'contact_id_b' => $this->_cId_a_2
,
1223 $result = $this->callAPISuccess($this->entity
, 'get', array(
1224 'contact_id' => $this->_cId_a
,
1225 'membership_type_id' => $memberType,
1227 // Although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1228 $this->assertEquals(2, $result['count']);
1230 foreach ($result['values'] as $key => $value) {
1231 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1, $relType3)));
1236 * Check for e-notices on enable & disable as reported in CRM-14350
1238 public function testSetActive() {
1239 $relationship = $this->callAPISuccess($this->entity
, 'create', $this->_params
);
1240 $this->callAPISuccess($this->entity
, 'create', array('id' => $relationship['id'], 'is_active' => 0));
1241 $this->callAPISuccess($this->entity
, 'create', array('id' => $relationship['id'], 'is_active' => 1));
1245 * Test creating related memberships.
1247 public function testCreateRelatedMembership() {
1248 $relatedMembershipType = $this->callAPISuccess('MembershipType', 'create', array(
1249 'name' => 'Membership with Related',
1250 'member_of_contact_id' => 1,
1251 'financial_type_id' => 1,
1252 'minimum_fee' => 0.00,
1253 'duration_unit' => 'year',
1254 'duration_interval' => 1,
1255 'period_type' => 'rolling',
1256 'relationship_type_id' => $this->_relTypeID
,
1257 'relationship_direction' => 'b_a',
1258 'visibility' => 'Public',
1261 'domain_id' => CRM_Core_Config
::domainID(),
1263 $originalMembership = $this->callAPISuccess('Membership', 'create', array(
1264 'membership_type_id' => $relatedMembershipType['id'],
1265 'contact_id' => $this->_cId_b
,
1267 $this->callAPISuccess('Relationship', 'create', array(
1268 'relationship_type_id' => $this->_relTypeID
,
1269 'contact_id_a' => $this->_cId_a
,
1270 'contact_id_b' => $this->_cId_b
,
1272 $contactAMembership = $this->callAPISuccessGetSingle('membership', array('contact_id' => $this->_cId_a
));
1273 $this->assertEquals($originalMembership['id'], $contactAMembership['owner_membership_id']);
1275 // Adding a relationship with a future start date should NOT create a membership
1276 $this->callAPISuccess('Relationship', 'create', array(
1277 'relationship_type_id' => $this->_relTypeID
,
1278 'contact_id_a' => $this->_cId_a_2
,
1279 'contact_id_b' => $this->_cId_b
,
1280 'start_date' => 'now + 1 week',
1282 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_cId_a_2
), 0);
1284 // Deleting the organization should cause the related membership to be deleted.
1285 $this->callAPISuccess('contact', 'delete', array('id' => $this->_cId_b
));
1286 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_cId_a
), 0);