3 * +--------------------------------------------------------------------+
4 * | CiviCRM version 5 |
5 * +--------------------------------------------------------------------+
6 * | Copyright CiviCRM LLC (c) 2004-2018 |
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
{
33 protected $_apiversion = 3;
42 * Second organization contact.
47 protected $_relTypeID;
48 protected $_ids = array();
49 protected $_customGroupId = NULL;
50 protected $_customFieldId = NULL;
58 public function setUp() {
60 $this->_cId_a
= $this->individualCreate();
61 $this->_cId_a_2
= $this->individualCreate(array(
64 'contact_type' => 'Individual',
66 $this->_cId_b
= $this->organizationCreate();
67 $this->_cId_b2
= $this->organizationCreate(array('organization_name' => ' Org 2'));
68 $this->_entity
= 'relationship';
69 //Create a relationship type.
70 $relTypeParams = array(
71 'name_a_b' => 'Relation 1 for delete',
72 'name_b_a' => 'Relation 2 for delete',
73 'description' => 'Testing relationship type',
74 'contact_type_a' => 'Individual',
75 'contact_type_b' => 'Organization',
80 $this->_relTypeID
= $this->relationshipTypeCreate($relTypeParams);
81 $this->_params
= array(
82 'contact_id_a' => $this->_cId_a
,
83 'contact_id_b' => $this->_cId_b
,
84 'relationship_type_id' => $this->_relTypeID
,
85 'start_date' => '2008-12-20',
96 public function tearDown() {
97 $this->contactDelete($this->_cId_a
);
98 $this->contactDelete($this->_cId_a_2
);
99 $this->contactDelete($this->_cId_b
);
100 $this->contactDelete($this->_cId_b2
);
101 $this->quickCleanup(array('civicrm_relationship'), TRUE);
102 $this->relationshipTypeDelete($this->_relTypeID
);
106 * Check with empty array.
108 public function testRelationshipCreateEmpty() {
109 $this->callAPIFailure('relationship', 'create', array());
113 * Test Current Employer is correctly set.
115 public function testCurrentEmployerRelationship() {
116 $employerRelationshipID = $this->callAPISuccessGetValue('RelationshipType', array(
118 'name_b_a' => "Employer Of",
120 $employerRelationship = $this->callAPISuccess('Relationship', 'create', array(
121 'contact_id_a' => $this->_cId_a
,
122 'contact_id_b' => $this->_cId_b
,
123 'relationship_type_id' => $employerRelationshipID,
125 $params = array($this->_cId_a
=> $this->_cId_b
);
126 CRM_Contact_BAO_Contact_Utils
::setCurrentEmployer($params);
128 //Check if current employer is correctly set.
129 $employer = $this->callAPISuccessGetValue('Contact', array(
130 'return' => "current_employer",
131 'id' => $this->_cId_a
,
133 $organisation = $this->callAPISuccessGetValue('Contact', array(
134 'return' => "sort_name",
135 'id' => $this->_cId_b
,
137 $this->assertEquals($employer, $organisation);
139 //Update relationship type
140 $update = $this->callAPISuccess('Relationship', 'create', array(
141 'id' => $employerRelationship['id'],
142 'relationship_type_id' => $this->_relTypeID
,
144 $employeeContact = $this->callAPISuccessGetSingle('Contact', array(
145 'return' => array("current_employer"),
146 'id' => $this->_cId_a
,
148 //current employer should be removed.
149 $this->assertEmpty($employeeContact['current_employer']);
153 * Check if required fields are not passed.
155 public function testRelationshipCreateWithoutRequired() {
157 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
158 'end_date' => array('d' => '10', 'M' => '1', 'Y' => '2009'),
162 $this->callAPIFailure('relationship', 'create', $params);
166 * Check with incorrect required fields.
168 public function testRelationshipCreateWithIncorrectData() {
171 'contact_id_a' => $this->_cId_a
,
172 'contact_id_b' => $this->_cId_b
,
173 'relationship_type_id' => 'Breaking Relationship',
176 $this->callAPIFailure('relationship', 'create', $params);
178 //contact id is not an integer
180 'contact_id_a' => 'invalid',
181 'contact_id_b' => $this->_cId_b
,
182 'relationship_type_id' => $this->_relTypeID
,
183 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
186 $this->callAPIFailure('relationship', 'create', $params);
188 // Contact id does not exist.
189 $params['contact_id_a'] = 999;
190 $this->callAPIFailure('relationship', 'create', $params);
193 $params['contact_id_a'] = $this->_cId_a
;
194 $params['start_date'] = array('d' => '1', 'M' => '1');
195 $this->callAPIFailure('relationship', 'create', $params);
199 * Check relationship creation with invalid Relationship.
201 public function testRelationshipCreateInvalidRelationship() {
202 // Both have the contact type Individual.
204 'contact_id_a' => $this->_cId_a
,
205 'contact_id_b' => $this->_cId_a
,
206 'relationship_type_id' => $this->_relTypeID
,
207 'start_date' => '2008-01-10',
211 $this->callAPIFailure('relationship', 'create', $params);
213 // both the contact of type Organization
215 'contact_id_a' => $this->_cId_b
,
216 'contact_id_b' => $this->_cId_b
,
217 'relationship_type_id' => $this->_relTypeID
,
218 'start_date' => '2008-01-10',
222 $this->callAPIFailure('relationship', 'create', $params);
226 * Check relationship already exists.
228 public function testRelationshipCreateAlreadyExists() {
230 'contact_id_a' => $this->_cId_a
,
231 'contact_id_b' => $this->_cId_b
,
232 'relationship_type_id' => $this->_relTypeID
,
233 'start_date' => '2008-12-20',
237 $relationship = $this->callAPISuccess('relationship', 'create', $params);
240 'contact_id_a' => $this->_cId_a
,
241 'contact_id_b' => $this->_cId_b
,
242 'relationship_type_id' => $this->_relTypeID
,
243 'start_date' => '2008-12-20',
246 $this->callAPIFailure('relationship', 'create', $params, 'Duplicate Relationship');
248 $params['id'] = $relationship['id'];
249 $this->callAPISuccess('relationship', 'delete', $params);
253 * Check relationship already exists.
255 public function testRelationshipCreateUpdateAlreadyExists() {
257 'contact_id_a' => $this->_cId_a
,
258 'contact_id_b' => $this->_cId_b
,
259 'relationship_type_id' => $this->_relTypeID
,
260 'start_date' => '2008-12-20',
265 $relationship = $this->callAPISuccess('relationship', 'create', $params);
267 'id' => $relationship['id'],
271 $this->callAPISuccess('relationship', 'create', $params);
272 $this->callAPISuccess('relationship', 'get', $params);
273 $params['id'] = $relationship['id'];
274 $this->callAPISuccess('relationship', 'delete', $params);
278 * Check update doesn't reset stuff badly - CRM-11789.
280 public function testRelationshipCreateUpdateDoesNotMangle() {
282 'contact_id_a' => $this->_cId_a
,
283 'contact_id_b' => $this->_cId_b
,
284 'relationship_type_id' => $this->_relTypeID
,
285 'start_date' => '2008-12-20',
287 'is_permission_a_b' => 1,
288 'description' => 'my desc',
290 $relationship = $this->callAPISuccess('relationship', 'create', $params);
292 $updateParams = array(
293 'id' => $relationship['id'],
294 'relationship_type_id' => $this->_relTypeID
,
296 $this->callAPISuccess('relationship', 'create', $updateParams);
298 //make sure the orig params didn't get changed
299 $this->getAndCheck($params, $relationship['id'], 'relationship');
305 * Check relationship creation.
307 public function testRelationshipCreate() {
309 'contact_id_a' => $this->_cId_a
,
310 'contact_id_b' => $this->_cId_b
,
311 'relationship_type_id' => $this->_relTypeID
,
312 'start_date' => '2010-10-30',
313 'end_date' => '2010-12-30',
318 $result = $this->callAPIAndDocument('relationship', 'create', $params, __FUNCTION__
, __FILE__
);
319 $this->assertNotNull($result['id']);
320 $relationParams = array(
321 'id' => $result['id'],
324 // assertDBState compares expected values in $result to actual values in the DB
325 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
326 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
327 $values = $result['values'][$result['id']];
328 foreach ($params as $key => $value) {
329 if ($key == 'note') {
332 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE));
334 $params['id'] = $result['id'];
335 $this->callAPISuccess('relationship', 'delete', $params);
339 * Ensure disabling works.
341 public function testRelationshipUpdate() {
342 $result = $this->callAPISuccess('relationship', 'create', $this->_params
);
343 $relID = $result['id'];
344 $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'description' => 'blah'));
345 $this->assertEquals($relID, $result['id']);
347 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
349 $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'is_permission_b_a' => 1));
350 $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
351 $result = $this->callAPISuccess('relationship', 'create', array('id' => $result['id'], 'is_active' => 0));
352 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
353 $this->assertEquals(0, $result['values'][$result['id']]['is_active']);
354 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
355 $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
359 * Check relationship creation.
361 public function testRelationshipCreateEmptyEndDate() {
363 'contact_id_a' => $this->_cId_a
,
364 'contact_id_b' => $this->_cId_b
,
365 'relationship_type_id' => $this->_relTypeID
,
366 'start_date' => '2010-10-30',
372 $result = $this->callAPISuccess('relationship', 'create', $params);
373 $this->assertNotNull($result['id']);
374 $relationParams = array(
375 'id' => $result['id'],
378 // assertDBState compares expected values in $result to actual values in the DB
379 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
380 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
381 $values = $result['values'][$result['id']];
382 foreach ($params as $key => $value) {
383 if ($key == 'note') {
386 if ($key == 'end_date') {
387 $this->assertTrue(empty($values[$key]));
390 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__
);
392 $params['id'] = $result['id'];
393 $this->callAPISuccess('relationship', 'delete', $params);
397 * Check relationship creation with custom data.
399 public function testRelationshipCreateEditWithCustomData() {
400 $this->createCustomGroup();
401 $this->_ids
= $this->createCustomField();
402 //few custom Values for comparing
403 $custom_params = array(
404 "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship',
405 "custom_{$this->_ids[1]}" => 'Y',
406 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
407 "custom_{$this->_ids[3]}" => 'http://example.com',
411 'contact_id_a' => $this->_cId_a
,
412 'contact_id_b' => $this->_cId_b
,
413 'relationship_type_id' => $this->_relTypeID
,
414 'start_date' => '2008-12-20',
417 $params = array_merge($params, $custom_params);
418 $result = $this->callAPISuccess('relationship', 'create', $params);
420 $relationParams = array(
421 'id' => $result['id'],
423 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
425 //Test Edit of custom field from the form.
426 $getParams = array('id' => $result['id']);
427 $updateParams = array_merge($getParams, array(
428 "custom_{$this->_ids[0]}" => 'Edited Text Value',
429 'relationship_type_id' => $this->_relTypeID
. '_b_a',
430 'related_contact_id' => $this->_cId_a
,
432 $reln = new CRM_Contact_Form_Relationship();
433 $reln->_action
= CRM_Core_Action
::UPDATE
;
434 $reln->_relationshipId
= $result['id'];
435 $reln->submit($updateParams);
437 $check = $this->callAPISuccess('relationship', 'get', $getParams);
438 $this->assertEquals("Edited Text Value", $check['values'][$check['id']]["custom_{$this->_ids[0]}"]);
440 $params['id'] = $result['id'];
441 $this->callAPISuccess('relationship', 'delete', $params);
442 $this->relationshipTypeDelete($this->_relTypeID
);
446 * Check with complete array + custom field
447 * Note that the test is written on purpose without any
448 * variables specific to participant so it can be replicated into other entities
449 * and / or moved to the automated test suite
451 public function testGetWithCustom() {
452 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
454 $params = $this->_params
;
455 $params['custom_' . $ids['custom_field_id']] = "custom string";
457 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
458 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
460 $getParams = array('id' => $result['id']);
461 $check = $this->callAPIAndDocument($this->_entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
462 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
464 $this->customFieldDelete($ids['custom_field_id']);
465 $this->customGroupDelete($ids['custom_group_id']);
471 public function createCustomGroup() {
473 'title' => 'Custom Group',
474 'extends' => array('Relationship'),
480 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
481 $this->_customGroupId
= $customGroup['id'];
482 return $customGroup['id'];
488 public function createCustomField() {
491 'custom_group_id' => $this->_customGroupId
,
492 'label' => 'Enter text about relationship',
493 'html_type' => 'Text',
494 'data_type' => 'String',
495 'default_value' => 'xyz',
498 'is_searchable' => 0,
502 $customField = $this->callAPISuccess('CustomField', 'create', $params);
503 $ids[] = $customField['id'];
505 $optionValue[] = array(
511 $optionValue[] = array(
517 $optionValue[] = array(
525 'label' => 'Pick Color',
526 'html_type' => 'Select',
527 'data_type' => 'String',
530 'is_searchable' => 0,
532 'option_values' => $optionValue,
533 'custom_group_id' => $this->_customGroupId
,
536 $customField = $this->callAPISuccess('custom_field', 'create', $params);
537 $ids[] = $customField['id'];
540 'custom_group_id' => $this->_customGroupId
,
541 'name' => 'test_date',
542 'label' => 'test_date',
543 'html_type' => 'Select Date',
544 'data_type' => 'Date',
545 'default_value' => '20090711',
548 'is_searchable' => 0,
552 $customField = $this->callAPISuccess('custom_field', 'create', $params);
554 $ids[] = $customField['id'];
556 'custom_group_id' => $this->_customGroupId
,
557 'name' => 'test_link',
558 'label' => 'test_link',
559 'html_type' => 'Link',
560 'data_type' => 'Link',
561 'default_value' => 'http://civicrm.org',
564 'is_searchable' => 0,
568 $customField = $this->callAPISuccess('custom_field', 'create', $params);
569 $ids[] = $customField['id'];
574 * Check with empty array.
576 public function testRelationshipDeleteEmpty() {
577 $this->callAPIFailure('relationship', 'delete', array(), 'Mandatory key(s) missing from params array: id');
581 * Check if required fields are not passed.
583 public function testRelationshipDeleteWithoutRequired() {
585 'start_date' => '2008-12-20',
586 'end_date' => '2009-12-20',
590 $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
594 * Check with incorrect required fields.
596 public function testRelationshipDeleteWithIncorrectData() {
598 'contact_id_a' => $this->_cId_a
,
599 'contact_id_b' => $this->_cId_b
,
600 'relationship_type_id' => 'Breaking Relationship',
603 $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
605 $params['id'] = "Invalid";
606 $this->callAPIFailure('relationship', 'delete', $params, 'id is not a valid integer');
610 * Check relationship creation.
612 public function testRelationshipDelete() {
614 'contact_id_a' => $this->_cId_a
,
615 'contact_id_b' => $this->_cId_b
,
616 'relationship_type_id' => $this->_relTypeID
,
617 'start_date' => '2008-12-20',
621 $result = $this->callAPISuccess('relationship', 'create', $params);
622 $params = array('id' => $result['id']);
623 $this->callAPIAndDocument('relationship', 'delete', $params, __FUNCTION__
, __FILE__
);
624 $this->relationshipTypeDelete($this->_relTypeID
);
627 ///////////////// civicrm_relationship_update methods
630 * Check with empty array.
632 public function testRelationshipUpdateEmpty() {
633 $this->callAPIFailure('relationship', 'create', array(),
634 'Mandatory key(s) missing from params array: contact_id_a, contact_id_b, relationship_type_id');
638 * Check if required fields are not passed.
642 * Check relationship update.
644 public function testRelationshipCreateDuplicate() {
646 'contact_id_a' => $this->_cId_a
,
647 'contact_id_b' => $this->_cId_b
,
648 'relationship_type_id' => $this->_relTypeID
,
649 'start_date' => '20081214',
650 'end_date' => '20091214',
654 $result = $this->callAPISuccess('relationship', 'create', $relParams);
656 $this->assertNotNull($result['id']);
659 'contact_id_a' => $this->_cId_a
,
660 'contact_id_b' => $this->_cId_b
,
661 'relationship_type_id' => $this->_relTypeID
,
662 'start_date' => '20081214',
663 'end_date' => '20091214',
667 $this->callAPIFailure('relationship', 'create', $params, 'Duplicate Relationship');
669 $this->callAPISuccess('relationship', 'delete', array('id' => $result['id']));
670 $this->relationshipTypeDelete($this->_relTypeID
);
674 * CRM-13725 - Two relationships of same type with same start and end date
675 * should be OK if the custom field values differ.
677 public function testRelationshipCreateDuplicateWithCustomFields() {
678 $this->createCustomGroup();
679 $this->_ids
= $this->createCustomField();
681 $custom_params_1 = array(
682 "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship',
683 "custom_{$this->_ids[1]}" => 'Y',
684 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
685 "custom_{$this->_ids[3]}" => 'http://example.com',
688 $custom_params_2 = array(
689 "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
690 "custom_{$this->_ids[1]}" => 'Y',
691 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
692 "custom_{$this->_ids[3]}" => 'http://example.org',
696 'contact_id_a' => $this->_cId_a
,
697 'contact_id_b' => $this->_cId_b
,
698 'relationship_type_id' => $this->_relTypeID
,
699 'start_date' => '2008-12-20',
703 $params_1 = array_merge($params, $custom_params_1);
704 $params_2 = array_merge($params, $custom_params_2);
706 $result_1 = $this->callAPISuccess('relationship', 'create', $params_1);
707 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
709 $this->assertNotNull($result_2['id']);
710 $this->assertEquals(0, $result_2['is_error']);
712 $this->relationshipTypeDelete($this->_relTypeID
);
716 * CRM-13725 - Two relationships of same type with same start and end date
717 * should be OK if the custom field values differ. In this case, the
718 * existing relationship does not have custom values, but the new one
721 public function testRelationshipCreateDuplicateWithCustomFields2() {
722 $this->createCustomGroup();
723 $this->_ids
= $this->createCustomField();
725 $custom_params_2 = array(
726 "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
727 "custom_{$this->_ids[1]}" => 'Y',
728 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
729 "custom_{$this->_ids[3]}" => 'http://example.org',
733 'contact_id_a' => $this->_cId_a
,
734 'contact_id_b' => $this->_cId_b
,
735 'relationship_type_id' => $this->_relTypeID
,
736 'start_date' => '2008-12-20',
740 $params_2 = array_merge($params_1, $custom_params_2);
742 $this->callAPISuccess('relationship', 'create', $params_1);
743 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
745 $this->assertNotNull($result_2['id']);
746 $this->assertEquals(0, $result_2['is_error']);
748 $this->relationshipTypeDelete($this->_relTypeID
);
752 * CRM-13725 - Two relationships of same type with same start and end date
753 * should be OK if the custom field values differ. In this case, the
754 * existing relationship does have custom values, but the new one
757 public function testRelationshipCreateDuplicateWithCustomFields3() {
758 $this->createCustomGroup();
759 $this->_ids
= $this->createCustomField();
761 $custom_params_1 = array(
762 "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
763 "custom_{$this->_ids[1]}" => 'Y',
764 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
765 "custom_{$this->_ids[3]}" => 'http://example.org',
769 'contact_id_a' => $this->_cId_a
,
770 'contact_id_b' => $this->_cId_b
,
771 'relationship_type_id' => $this->_relTypeID
,
772 'start_date' => '2008-12-20',
776 $params_1 = array_merge($params_2, $custom_params_1);
778 $this->callAPISuccess('relationship', 'create', $params_1);
779 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
781 $this->assertNotNull($result_2['id']);
782 $this->assertEquals(0, $result_2['is_error']);
784 $this->relationshipTypeDelete($this->_relTypeID
);
788 * Check with valid params array.
790 public function testRelationshipsGet() {
792 'contact_id_a' => $this->_cId_a
,
793 'contact_id_b' => $this->_cId_b
,
794 'relationship_type_id' => $this->_relTypeID
,
795 'start_date' => '2011-01-01',
796 'end_date' => '2013-01-01',
800 $this->callAPISuccess('relationship', 'create', $relParams);
804 'contact_id' => $this->_cId_b
,
806 $result = $this->callAPISuccess('relationship', 'get', $params);
807 $this->assertEquals($result['count'], 1);
809 'contact_id_a' => $this->_cId_a
,
811 $result = $this->callAPISuccess('relationship', 'get', $params);
812 $this->assertEquals($result['count'], 1);
813 // contact_id_a is wrong so should be no matches
815 'contact_id_a' => $this->_cId_b
,
817 $result = $this->callAPISuccess('relationship', 'get', $params);
818 $this->assertEquals($result['count'], 0);
822 * Chain Relationship.get and to Contact.get.
824 public function testRelationshipGetWithChainedCall() {
825 // Create a relationship.
826 $createResult = $this->callAPISuccess('relationship', 'create', $this->_params
);
827 $id = $createResult['id'];
829 // Try to retrieve it using chaining.
831 'relationship_type_id' => $this->_relTypeID
,
833 'api.Contact.get' => array(
834 'id' => '$value.contact_id_b',
838 $result = $this->callAPISuccess('relationship', 'get', $params);
840 $this->assertEquals(1, $result['count']);
841 $relationship = CRM_Utils_Array
::first($result['values']);
842 $this->assertEquals(1, $relationship['api.Contact.get']['count']);
843 $contact = CRM_Utils_Array
::first($relationship['api.Contact.get']['values']);
844 $this->assertEquals($this->_cId_b
, $contact['id']);
848 * Chain Contact.get to Relationship.get and again to Contact.get.
850 public function testRelationshipGetInChainedCall() {
851 // Create a relationship.
852 $this->callAPISuccess('relationship', 'create', $this->_params
);
854 // Try to retrieve it using chaining.
856 'id' => $this->_cId_a
,
857 'api.Relationship.get' => array(
858 'relationship_type_id' => $this->_relTypeID
,
859 'contact_id_a' => '$value.id',
860 'api.Contact.get' => array(
861 'id' => '$value.contact_id_b',
866 $result = $this->callAPISuccess('contact', 'get', $params);
867 $this->assertEquals(1, $result['count']);
868 $contact = CRM_Utils_Array
::first($result['values']);
869 $this->assertEquals(1, $contact['api.Relationship.get']['count']);
870 $relationship = CRM_Utils_Array
::first($contact['api.Relationship.get']['values']);
871 $this->assertEquals(1, $relationship['api.Contact.get']['count']);
872 $contact = CRM_Utils_Array
::first($relationship['api.Contact.get']['values']);
873 $this->assertEquals($this->_cId_b
, $contact['id']);
877 * Check with valid params array.
878 * (The get function will behave differently without 'contact_id' passed
880 public function testRelationshipsGetGeneric() {
882 'contact_id_a' => $this->_cId_a
,
883 'contact_id_b' => $this->_cId_b
,
884 'relationship_type_id' => $this->_relTypeID
,
885 'start_date' => '2011-01-01',
886 'end_date' => '2013-01-01',
890 $this->callAPISuccess('relationship', 'create', $relParams);
894 'contact_id_b' => $this->_cId_b
,
896 $this->callAPISuccess('relationship', 'get', $params);
900 * Test retrieving only current relationships.
902 public function testGetIsCurrent() {
904 'contact_id_a' => $this->_cId_a
,
905 'contact_id_b' => $this->_cId_b2
,
906 'relationship_type_id' => $this->_relTypeID
,
907 'start_date' => '2008-12-20',
910 $this->callAPISuccess('relationship', 'create', $rel2Params);
911 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params
);
914 'filters' => array('is_current' => 1),
916 $description = "Demonstrates is_current filter.";
917 $subfile = 'filterIsCurrent';
918 //no relationship has been created
919 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
920 $this->assertEquals($result['count'], 1);
921 $this->AssertEquals($rel1['id'], $result['id']);
923 // now try not started
924 $rel2Params['is_active'] = 1;
925 $rel2Params['start_date'] = 'tomorrow';
926 $this->callAPISuccess('relationship', 'create', $rel2Params);
927 $result = $this->callAPISuccess('relationship', 'get', $getParams);
928 $this->assertEquals($result['count'], 1);
929 $this->AssertEquals($rel1['id'], $result['id']);
932 $rel2Params['is_active'] = 1;
933 $rel2Params['start_date'] = 'last week';
934 $rel2Params['end_date'] = 'yesterday';
935 $this->callAPISuccess('relationship', 'create', $rel2Params);
939 * Test using various operators.
941 public function testGetTypeOperators() {
942 $relTypeParams = array(
943 'name_a_b' => 'Relation 3 for delete',
944 'name_b_a' => 'Relation 6 for delete',
945 'description' => 'Testing relationship type 2',
946 'contact_type_a' => 'Individual',
947 'contact_type_b' => 'Organization',
951 $relationType2 = $this->relationshipTypeCreate($relTypeParams);
952 $relTypeParams = array(
953 'name_a_b' => 'Relation 8 for delete',
954 'name_b_a' => 'Relation 9 for delete',
955 'description' => 'Testing relationship type 7',
956 'contact_type_a' => 'Individual',
957 'contact_type_b' => 'Organization',
961 $relationType3 = $this->relationshipTypeCreate($relTypeParams);
963 $relTypeParams = array(
964 'name_a_b' => 'Relation 6 for delete',
965 'name_b_a' => 'Relation 88for delete',
966 'description' => 'Testing relationship type 00',
967 'contact_type_a' => 'Individual',
968 'contact_type_b' => 'Organization',
972 $relationType4 = $this->relationshipTypeCreate($relTypeParams);
974 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params
);
975 $rel2 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
976 array('relationship_type_id' => $relationType2)));
977 $rel3 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
978 array('relationship_type_id' => $relationType3)));
979 $rel4 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
980 array('relationship_type_id' => $relationType4)));
983 'relationship_type_id' => array('IN' => array($relationType2, $relationType3)),
986 $description = "Demonstrates use of IN filter.";
987 $subfile = 'INRelationshipType';
989 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
990 $this->assertEquals($result['count'], 2);
991 $this->AssertEquals(array($rel2['id'], $rel3['id']), array_keys($result['values']));
993 $description = "Demonstrates use of NOT IN filter.";
994 $subfile = 'NotInRelationshipType';
996 'relationship_type_id' => array('NOT IN' => array($relationType2, $relationType3)),
998 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
999 $this->assertEquals($result['count'], 2);
1000 $this->AssertEquals(array($rel1['id'], $rel4['id']), array_keys($result['values']));
1002 $description = "Demonstrates use of BETWEEN filter.";
1003 $subfile = 'BetweenRelationshipType';
1005 'relationship_type_id' => array('BETWEEN' => array($relationType2, $relationType4)),
1007 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
1008 $this->assertEquals($result['count'], 3);
1009 $this->AssertEquals(array($rel2['id'], $rel3['id'], $rel4['id']), array_keys($result['values']));
1011 $description = "Demonstrates use of Not BETWEEN filter.";
1012 $subfile = 'NotBetweenRelationshipType';
1014 'relationship_type_id' => array('NOT BETWEEN' => array($relationType2, $relationType4)),
1016 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
1017 $this->assertEquals($result['count'], 1);
1018 $this->AssertEquals(array($rel1['id']), array_keys($result['values']));
1023 * Check with invalid relationshipType Id.
1025 public function testRelationshipTypeAddInvalidId() {
1026 $relTypeParams = array(
1028 'name_a_b' => 'Relation 1 for delete',
1029 'name_b_a' => 'Relation 2 for delete',
1030 'contact_type_a' => 'Individual',
1031 'contact_type_b' => 'Organization',
1033 $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
1034 'id is not a valid integer');
1038 * Check with valid data with contact_b.
1040 public function testGetRelationshipWithContactB() {
1042 'contact_id_a' => $this->_cId_a
,
1043 'contact_id_b' => $this->_cId_b
,
1044 'relationship_type_id' => $this->_relTypeID
,
1045 'start_date' => '2011-01-01',
1046 'end_date' => '2013-01-01',
1050 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
1053 'contact_id' => $this->_cId_a
,
1056 $result = $this->callAPISuccess('relationship', 'get', $contacts);
1057 $this->assertGreaterThan(0, $result['count']);
1059 'id' => $relationship['id'],
1061 $this->callAPISuccess('relationship', 'delete', $params);
1062 $this->relationshipTypeDelete($this->_relTypeID
);
1066 * Check with valid data with relationshipTypes.
1068 public function testGetRelationshipWithRelTypes() {
1070 'contact_id_a' => $this->_cId_a
,
1071 'contact_id_b' => $this->_cId_b
,
1072 'relationship_type_id' => $this->_relTypeID
,
1073 'start_date' => '2011-01-01',
1074 'end_date' => '2013-01-01',
1078 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
1081 'contact_id' => $this->_cId_a
,
1083 $this->callAPISuccess('relationship', 'get', $contact_a);
1086 'id' => $relationship['id'],
1088 $this->callAPISuccess('relationship', 'delete', $params);
1089 $this->relationshipTypeDelete($this->_relTypeID
);
1093 * Checks that passing in 'contact_id' + a relationship type
1094 * will filter by relationship type (relationships go in both directions)
1095 * as relationship api does a reciprocal check if contact_id provided
1097 * We should get 1 result without or with correct relationship type id & 0 with
1100 public function testGetRelationshipByTypeReciprocal() {
1101 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1102 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1103 'contact_id' => $this->_cId_a
,
1104 'relationship_type_id' => $this->_relTypeID
,
1106 $this->assertEquals(1, $result['count']);
1107 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1108 'contact_id' => $this->_cId_a
,
1109 'relationship_type_id' => $this->_relTypeID +
1,
1111 $this->assertEquals(0, $result['count']);
1112 $this->callAPISuccess($this->_entity
, 'delete', array('id' => $created['id']));
1116 * Checks that passing in 'contact_id_b' + a relationship type
1117 * will filter by relationship type for contact b
1119 * We should get 1 result without or with correct relationship type id & 0 with
1122 public function testGetRelationshipByTypeDAO() {
1123 $this->_ids
['relationship'] = $this->callAPISuccess($this->_entity
, 'create', array('format.only_id' => TRUE) +
1125 $this->callAPISuccess($this->_entity
, 'getcount', array(
1126 'contact_id_a' => $this->_cId_a
,
1129 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1130 'contact_id_a' => $this->_cId_a
,
1131 'relationship_type_id' => $this->_relTypeID
,
1133 $this->assertEquals(1, $result['count']);
1134 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1135 'contact_id_a' => $this->_cId_a
,
1136 'relationship_type_id' => $this->_relTypeID +
1,
1138 $this->assertEquals(0, $result['count']);
1142 * Checks that passing in 'contact_id_b' + a relationship type
1143 * will filter by relationship type for contact b
1145 * We should get 1 result without or with correct relationship type id & 0 with
1148 public function testGetRelationshipByTypeArrayDAO() {
1149 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1150 $org3 = $this->organizationCreate();
1151 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1152 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1155 $this->callAPISuccess($this->_entity
, 'create',
1156 array_merge($this->_params
, array(
1157 'relationship_type_id' => $relType2,
1158 'contact_id_b' => $this->_cId_b2
,
1163 $this->callAPISuccess($this->_entity
, 'create',
1164 array_merge($this->_params
, array(
1165 'relationship_type_id' => $relType3,
1166 'contact_id_b' => $org3,
1170 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1171 'contact_id_a' => $this->_cId_a
,
1172 'relationship_type_id' => array('IN' => array($this->_relTypeID
, $relType3)),
1175 $this->assertEquals(2, $result['count']);
1176 foreach ($result['values'] as $key => $value) {
1177 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID
, $relType3)));
1182 * Checks that passing in 'contact_id_b' + a relationship type
1183 * will filter by relationship type for contact b
1185 * We should get 1 result without or with correct relationship type id & 0 with
1188 public function testGetRelationshipByTypeArrayReciprocal() {
1189 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1190 $org3 = $this->organizationCreate();
1191 // lets just assume built in ones aren't being messed with!
1196 $this->callAPISuccess($this->_entity
, 'create',
1197 array_merge($this->_params
, array(
1198 'relationship_type_id' => $relType2,
1199 'contact_id_b' => $this->_cId_b2
,
1204 $this->callAPISuccess($this->_entity
, 'create',
1205 array_merge($this->_params
, array(
1206 'relationship_type_id' => $relType3,
1207 'contact_id_b' => $org3,
1211 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1212 'contact_id' => $this->_cId_a
,
1213 'relationship_type_id' => array('IN' => array($this->_relTypeID
, $relType3)),
1216 $this->assertEquals(2, $result['count']);
1217 foreach ($result['values'] as $key => $value) {
1218 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID
, $relType3)));
1223 * Test relationship get by membership type.
1225 * Checks that passing in 'contact_id_b' + a relationship type
1226 * will filter by relationship type for contact b
1228 * We should get 1 result without or with correct relationship type id & 0 with
1231 public function testGetRelationshipByMembershipTypeDAO() {
1232 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1233 $org3 = $this->organizationCreate();
1235 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1236 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1238 $memberType = $this->membershipTypeCreate(array(
1239 'relationship_type_id' => CRM_Core_DAO
::VALUE_SEPARATOR
. $relType1 . CRM_Core_DAO
::VALUE_SEPARATOR
. $relType3 . CRM_Core_DAO
::VALUE_SEPARATOR
,
1240 'relationship_direction' => CRM_Core_DAO
::VALUE_SEPARATOR
. 'a_b' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'b_a' . CRM_Core_DAO
::VALUE_SEPARATOR
,
1244 $this->callAPISuccess($this->_entity
, 'create',
1245 array_merge($this->_params
, array(
1246 'relationship_type_id' => $relType2,
1247 'contact_id_b' => $this->_cId_b2
,
1252 $this->callAPISuccess($this->_entity
, 'create',
1253 array_merge($this->_params
, array(
1254 'relationship_type_id' => $relType3,
1255 'contact_id_b' => $org3,
1259 // Relationship 4 with reversal.
1260 $this->callAPISuccess($this->_entity
, 'create',
1261 array_merge($this->_params
, array(
1262 'relationship_type_id' => $relType1,
1263 'contact_id_a' => $this->_cId_a
,
1264 'contact_id_b' => $this->_cId_a_2
,
1268 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1269 'contact_id_a' => $this->_cId_a
,
1270 'membership_type_id' => $memberType,
1272 // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1273 $this->assertEquals(1, $result['count']);
1274 foreach ($result['values'] as $key => $value) {
1275 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1)));
1280 * Checks that passing in 'contact_id_b' + a relationship type
1281 * will filter by relationship type for contact b
1283 * We should get 1 result without or with correct relationship type id & 0 with
1286 public function testGetRelationshipByMembershipTypeReciprocal() {
1287 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1288 $org3 = $this->organizationCreate();
1290 // Let's just assume built in ones aren't being messed with!
1294 $memberType = $this->membershipTypeCreate(array(
1295 'relationship_type_id' => CRM_Core_DAO
::VALUE_SEPARATOR
. $relType1 . CRM_Core_DAO
::VALUE_SEPARATOR
. $relType3 . CRM_Core_DAO
::VALUE_SEPARATOR
,
1296 'relationship_direction' => CRM_Core_DAO
::VALUE_SEPARATOR
. 'a_b' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'b_a' . CRM_Core_DAO
::VALUE_SEPARATOR
,
1300 $this->callAPISuccess($this->_entity
, 'create',
1301 array_merge($this->_params
, array(
1302 'relationship_type_id' => $relType2,
1303 'contact_id_b' => $this->_cId_b2
,
1308 $this->callAPISuccess($this->_entity
, 'create',
1309 array_merge($this->_params
, array(
1310 'relationship_type_id' => $relType3,
1311 'contact_id_b' => $org3,
1315 // Relationship 4 with reversal.
1316 $this->callAPISuccess($this->_entity
, 'create',
1317 array_merge($this->_params
, array(
1318 'relationship_type_id' => $relType1,
1319 'contact_id_a' => $this->_cId_a
,
1320 'contact_id_b' => $this->_cId_a_2
,
1324 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1325 'contact_id' => $this->_cId_a
,
1326 'membership_type_id' => $memberType,
1328 // Although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1329 $this->assertEquals(2, $result['count']);
1331 foreach ($result['values'] as $key => $value) {
1332 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1, $relType3)));
1337 * Check for e-notices on enable & disable as reported in CRM-14350
1339 public function testSetActive() {
1340 $relationship = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1341 $this->callAPISuccess($this->_entity
, 'create', array('id' => $relationship['id'], 'is_active' => 0));
1342 $this->callAPISuccess($this->_entity
, 'create', array('id' => $relationship['id'], 'is_active' => 1));
1346 * Test creating related memberships.
1348 public function testCreateRelatedMembership() {
1349 $relatedMembershipType = $this->callAPISuccess('MembershipType', 'create', array(
1350 'name' => 'Membership with Related',
1351 'member_of_contact_id' => 1,
1352 'financial_type_id' => 1,
1353 'minimum_fee' => 0.00,
1354 'duration_unit' => 'year',
1355 'duration_interval' => 1,
1356 'period_type' => 'rolling',
1357 'relationship_type_id' => $this->_relTypeID
,
1358 'relationship_direction' => 'b_a',
1359 'visibility' => 'Public',
1362 'domain_id' => CRM_Core_Config
::domainID(),
1364 $originalMembership = $this->callAPISuccess('Membership', 'create', array(
1365 'membership_type_id' => $relatedMembershipType['id'],
1366 'contact_id' => $this->_cId_b
,
1368 $this->callAPISuccess('Relationship', 'create', array(
1369 'relationship_type_id' => $this->_relTypeID
,
1370 'contact_id_a' => $this->_cId_a
,
1371 'contact_id_b' => $this->_cId_b
,
1373 $contactAMembership = $this->callAPISuccessGetSingle('membership', array('contact_id' => $this->_cId_a
));
1374 $this->assertEquals($originalMembership['id'], $contactAMembership['owner_membership_id']);
1376 // Adding a relationship with a future start date should NOT create a membership
1377 $this->callAPISuccess('Relationship', 'create', array(
1378 'relationship_type_id' => $this->_relTypeID
,
1379 'contact_id_a' => $this->_cId_a_2
,
1380 'contact_id_b' => $this->_cId_b
,
1381 'start_date' => 'now + 1 week',
1383 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_cId_a_2
), 0);
1385 // Deleting the organization should cause the related membership to be deleted.
1386 $this->callAPISuccess('contact', 'delete', array('id' => $this->_cId_b
));
1387 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_cId_a
), 0);