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;
46 * Second organization contact.
51 protected $_relTypeID;
53 protected $_customFieldId = NULL;
61 public function setUp() {
63 $this->_cId_a
= $this->individualCreate();
64 $this->_cId_a_2
= $this->individualCreate(array(
67 'contact_type' => 'Individual',
69 $this->_cId_b
= $this->organizationCreate();
70 $this->_cId_b2
= $this->organizationCreate(array('organization_name' => ' Org 2'));
71 $this->entity
= 'Relationship';
72 //Create a relationship type.
73 $relTypeParams = array(
74 'name_a_b' => 'Relation 1 for delete',
75 'name_b_a' => 'Relation 2 for delete',
76 'description' => 'Testing relationship type',
77 'contact_type_a' => 'Individual',
78 'contact_type_b' => 'Organization',
83 $this->_relTypeID
= $this->relationshipTypeCreate($relTypeParams);
84 $this->_params
= array(
85 'contact_id_a' => $this->_cId_a
,
86 'contact_id_b' => $this->_cId_b
,
87 'relationship_type_id' => $this->_relTypeID
,
88 'start_date' => '2008-12-20',
99 public function tearDown() {
100 $this->contactDelete($this->_cId_a
);
101 $this->contactDelete($this->_cId_a_2
);
102 $this->contactDelete($this->_cId_b
);
103 $this->contactDelete($this->_cId_b2
);
104 $this->quickCleanup(array('civicrm_relationship'), TRUE);
105 $this->relationshipTypeDelete($this->_relTypeID
);
109 * Check with empty array.
111 public function testRelationshipCreateEmpty() {
112 $this->callAPIFailure('relationship', 'create', array());
116 * Test Current Employer is correctly set.
118 public function testCurrentEmployerRelationship() {
119 $employerRelationshipID = $this->callAPISuccessGetValue('RelationshipType', array(
121 'name_b_a' => "Employer Of",
123 $employerRelationship = $this->callAPISuccess('Relationship', 'create', array(
124 'contact_id_a' => $this->_cId_a
,
125 'contact_id_b' => $this->_cId_b
,
126 'relationship_type_id' => $employerRelationshipID,
128 $params = array($this->_cId_a
=> $this->_cId_b
);
129 CRM_Contact_BAO_Contact_Utils
::setCurrentEmployer($params);
131 //Check if current employer is correctly set.
132 $employer = $this->callAPISuccessGetValue('Contact', array(
133 'return' => "current_employer",
134 'id' => $this->_cId_a
,
136 $organisation = $this->callAPISuccessGetValue('Contact', array(
137 'return' => "sort_name",
138 'id' => $this->_cId_b
,
140 $this->assertEquals($employer, $organisation);
142 //Update relationship type
143 $update = $this->callAPISuccess('Relationship', 'create', array(
144 'id' => $employerRelationship['id'],
145 'relationship_type_id' => $this->_relTypeID
,
147 $employeeContact = $this->callAPISuccessGetSingle('Contact', array(
148 'return' => array("current_employer"),
149 'id' => $this->_cId_a
,
151 //current employer should be removed.
152 $this->assertEmpty($employeeContact['current_employer']);
156 * Check if required fields are not passed.
158 public function testRelationshipCreateWithoutRequired() {
160 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
161 'end_date' => array('d' => '10', 'M' => '1', 'Y' => '2009'),
165 $this->callAPIFailure('relationship', 'create', $params);
169 * Check with incorrect required fields.
171 public function testRelationshipCreateWithIncorrectData() {
174 'contact_id_a' => $this->_cId_a
,
175 'contact_id_b' => $this->_cId_b
,
176 'relationship_type_id' => 'Breaking Relationship',
179 $this->callAPIFailure('relationship', 'create', $params);
181 //contact id is not an integer
183 'contact_id_a' => 'invalid',
184 'contact_id_b' => $this->_cId_b
,
185 'relationship_type_id' => $this->_relTypeID
,
186 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
189 $this->callAPIFailure('relationship', 'create', $params);
191 // Contact id does not exist.
192 $params['contact_id_a'] = 999;
193 $this->callAPIFailure('relationship', 'create', $params);
196 $params['contact_id_a'] = $this->_cId_a
;
197 $params['start_date'] = array('d' => '1', 'M' => '1');
198 $this->callAPIFailure('relationship', 'create', $params);
202 * Check relationship creation with invalid Relationship.
204 public function testRelationshipCreateInvalidRelationship() {
205 // Both have the contact type Individual.
207 'contact_id_a' => $this->_cId_a
,
208 'contact_id_b' => $this->_cId_a
,
209 'relationship_type_id' => $this->_relTypeID
,
210 'start_date' => '2008-01-10',
214 $this->callAPIFailure('relationship', 'create', $params);
216 // both the contact of type Organization
218 'contact_id_a' => $this->_cId_b
,
219 'contact_id_b' => $this->_cId_b
,
220 'relationship_type_id' => $this->_relTypeID
,
221 'start_date' => '2008-01-10',
225 $this->callAPIFailure('relationship', 'create', $params);
229 * Check relationship already exists.
231 public function testRelationshipCreateAlreadyExists() {
233 'contact_id_a' => $this->_cId_a
,
234 'contact_id_b' => $this->_cId_b
,
235 'relationship_type_id' => $this->_relTypeID
,
236 'start_date' => '2008-12-20',
240 $relationship = $this->callAPISuccess('relationship', 'create', $params);
243 'contact_id_a' => $this->_cId_a
,
244 'contact_id_b' => $this->_cId_b
,
245 'relationship_type_id' => $this->_relTypeID
,
246 'start_date' => '2008-12-20',
249 $this->callAPIFailure('relationship', 'create', $params, 'Duplicate Relationship');
251 $params['id'] = $relationship['id'];
252 $this->callAPISuccess('relationship', 'delete', $params);
256 * Check relationship already exists.
258 public function testRelationshipCreateUpdateAlreadyExists() {
260 'contact_id_a' => $this->_cId_a
,
261 'contact_id_b' => $this->_cId_b
,
262 'relationship_type_id' => $this->_relTypeID
,
263 'start_date' => '2008-12-20',
268 $relationship = $this->callAPISuccess('relationship', 'create', $params);
270 'id' => $relationship['id'],
274 $this->callAPISuccess('relationship', 'create', $params);
275 $this->callAPISuccess('relationship', 'get', $params);
276 $params['id'] = $relationship['id'];
277 $this->callAPISuccess('relationship', 'delete', $params);
281 * Check update doesn't reset stuff badly - CRM-11789.
283 public function testRelationshipCreateUpdateDoesNotMangle() {
285 'contact_id_a' => $this->_cId_a
,
286 'contact_id_b' => $this->_cId_b
,
287 'relationship_type_id' => $this->_relTypeID
,
288 'start_date' => '2008-12-20',
290 'is_permission_a_b' => 1,
291 'description' => 'my desc',
293 $relationship = $this->callAPISuccess('relationship', 'create', $params);
295 $updateParams = array(
296 'id' => $relationship['id'],
297 'relationship_type_id' => $this->_relTypeID
,
299 $this->callAPISuccess('relationship', 'create', $updateParams);
301 //make sure the orig params didn't get changed
302 $this->getAndCheck($params, $relationship['id'], 'relationship');
307 * Check relationship creation.
309 public function testRelationshipCreate() {
311 'contact_id_a' => $this->_cId_a
,
312 'contact_id_b' => $this->_cId_b
,
313 'relationship_type_id' => $this->_relTypeID
,
314 'start_date' => '2010-10-30',
315 'end_date' => '2010-12-30',
320 $result = $this->callAPIAndDocument('relationship', 'create', $params, __FUNCTION__
, __FILE__
);
321 $this->assertNotNull($result['id']);
322 $relationParams = array(
323 'id' => $result['id'],
326 // assertDBState compares expected values in $result to actual values in the DB
327 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
328 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
329 $values = $result['values'][$result['id']];
330 foreach ($params as $key => $value) {
331 if ($key == 'note') {
334 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE));
336 $params['id'] = $result['id'];
337 $this->callAPISuccess('relationship', 'delete', $params);
341 * Ensure disabling works.
343 public function testRelationshipUpdate() {
344 $result = $this->callAPISuccess('relationship', 'create', $this->_params
);
345 $relID = $result['id'];
346 $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'description' => 'blah'));
347 $this->assertEquals($relID, $result['id']);
349 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
351 $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'is_permission_b_a' => 1));
352 $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
353 $result = $this->callAPISuccess('relationship', 'create', array('id' => $result['id'], 'is_active' => 0));
354 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
355 $this->assertEquals(0, $result['values'][$result['id']]['is_active']);
356 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
357 $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
361 * Check relationship creation.
363 public function testRelationshipCreateEmptyEndDate() {
365 'contact_id_a' => $this->_cId_a
,
366 'contact_id_b' => $this->_cId_b
,
367 'relationship_type_id' => $this->_relTypeID
,
368 'start_date' => '2010-10-30',
374 $result = $this->callAPISuccess('relationship', 'create', $params);
375 $this->assertNotNull($result['id']);
376 $relationParams = array(
377 'id' => $result['id'],
380 // assertDBState compares expected values in $result to actual values in the DB
381 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
382 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
383 $values = $result['values'][$result['id']];
384 foreach ($params as $key => $value) {
385 if ($key == 'note') {
388 if ($key == 'end_date') {
389 $this->assertTrue(empty($values[$key]));
392 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__
);
394 $params['id'] = $result['id'];
395 $this->callAPISuccess('relationship', 'delete', $params);
399 * Check relationship creation with custom data.
401 public function testRelationshipCreateEditWithCustomData() {
402 $this->createCustomGroupWithFieldsOfAllTypes();
403 //few custom Values for comparing
405 $this->getCustomFieldName('text') => 'Hello! this is custom data for relationship',
406 $this->getCustomFieldName('select_string') => 'Y',
407 $this->getCustomFieldName('select_date') => '2009-07-11 00:00:00',
408 $this->getCustomFieldName('link') => '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 = ['id' => $result['id']];
422 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
424 //Test Edit of custom field from the form.
425 $getParams = array('id' => $result['id']);
426 $updateParams = array_merge($getParams, array(
427 $this->getCustomFieldName('text') => 'Edited Text Value',
428 'relationship_type_id' => $this->_relTypeID
. '_b_a',
429 'related_contact_id' => $this->_cId_a
,
431 $reln = new CRM_Contact_Form_Relationship();
432 $reln->_action
= CRM_Core_Action
::UPDATE
;
433 $reln->_relationshipId
= $result['id'];
434 $reln->submit($updateParams);
436 $check = $this->callAPISuccess('relationship', 'get', $getParams);
437 $this->assertEquals("Edited Text Value", $check['values'][$check['id']][$this->getCustomFieldName('text')]);
439 $params['id'] = $result['id'];
440 $this->callAPISuccess('relationship', 'delete', $params);
441 $this->relationshipTypeDelete($this->_relTypeID
);
445 * Check with complete array + custom field
446 * Note that the test is written on purpose without any
447 * variables specific to participant so it can be replicated into other entities
448 * and / or moved to the automated test suite
450 public function testGetWithCustom() {
451 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
453 $params = $this->_params
;
454 $params['custom_' . $ids['custom_field_id']] = "custom string";
456 $result = $this->callAPISuccess($this->entity
, 'create', $params);
457 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
459 $getParams = array('id' => $result['id']);
460 $check = $this->callAPIAndDocument($this->entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
461 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
463 $this->customFieldDelete($ids['custom_field_id']);
464 $this->customGroupDelete($ids['custom_group_id']);
468 * Check with empty array.
470 public function testRelationshipDeleteEmpty() {
471 $this->callAPIFailure('relationship', 'delete', array(), 'Mandatory key(s) missing from params array: id');
475 * Check if required fields are not passed.
477 public function testRelationshipDeleteWithoutRequired() {
479 'start_date' => '2008-12-20',
480 'end_date' => '2009-12-20',
484 $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
488 * Check with incorrect required fields.
490 public function testRelationshipDeleteWithIncorrectData() {
492 'contact_id_a' => $this->_cId_a
,
493 'contact_id_b' => $this->_cId_b
,
494 'relationship_type_id' => 'Breaking Relationship',
497 $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
499 $params['id'] = "Invalid";
500 $this->callAPIFailure('relationship', 'delete', $params, 'id is not a valid integer');
504 * Check relationship creation.
506 public function testRelationshipDelete() {
508 'contact_id_a' => $this->_cId_a
,
509 'contact_id_b' => $this->_cId_b
,
510 'relationship_type_id' => $this->_relTypeID
,
511 'start_date' => '2008-12-20',
515 $result = $this->callAPISuccess('relationship', 'create', $params);
516 $params = array('id' => $result['id']);
517 $this->callAPIAndDocument('relationship', 'delete', $params, __FUNCTION__
, __FILE__
);
518 $this->relationshipTypeDelete($this->_relTypeID
);
521 ///////////////// civicrm_relationship_update methods
524 * Check with empty array.
526 public function testRelationshipUpdateEmpty() {
527 $this->callAPIFailure('relationship', 'create', array(),
528 'Mandatory key(s) missing from params array: contact_id_a, contact_id_b, relationship_type_id');
532 * Check if required fields are not passed.
536 * Check relationship update.
538 public function testRelationshipCreateDuplicate() {
540 'contact_id_a' => $this->_cId_a
,
541 'contact_id_b' => $this->_cId_b
,
542 'relationship_type_id' => $this->_relTypeID
,
543 'start_date' => '20081214',
544 'end_date' => '20091214',
548 $result = $this->callAPISuccess('relationship', 'create', $relParams);
550 $this->assertNotNull($result['id']);
553 'contact_id_a' => $this->_cId_a
,
554 'contact_id_b' => $this->_cId_b
,
555 'relationship_type_id' => $this->_relTypeID
,
556 'start_date' => '20081214',
557 'end_date' => '20091214',
561 $this->callAPIFailure('relationship', 'create', $params, 'Duplicate Relationship');
563 $this->callAPISuccess('relationship', 'delete', array('id' => $result['id']));
564 $this->relationshipTypeDelete($this->_relTypeID
);
568 * CRM-13725 - Two relationships of same type with same start and end date
569 * should be OK if the custom field values differ.
571 public function testRelationshipCreateDuplicateWithCustomFields() {
572 $this->createCustomGroupWithFieldsOfAllTypes();
574 $custom_params_1 = array(
575 $this->getCustomFieldName('text') => 'Hello! this is custom data for relationship',
576 $this->getCustomFieldName('select_string') => 'Y',
577 $this->getCustomFieldName('select_date') => '2009-07-11 00:00:00',
578 $this->getCustomFieldName('link') => 'http://example.com',
581 $custom_params_2 = array(
582 $this->getCustomFieldName('text') => 'Hello! this is other custom data',
583 $this->getCustomFieldName('select_string') => 'Y',
584 $this->getCustomFieldName('select_date') => '2009-07-11 00:00:00',
585 $this->getCustomFieldName('link') => 'http://example.org',
589 'contact_id_a' => $this->_cId_a
,
590 'contact_id_b' => $this->_cId_b
,
591 'relationship_type_id' => $this->_relTypeID
,
592 'start_date' => '2008-12-20',
596 $params_1 = array_merge($params, $custom_params_1);
597 $params_2 = array_merge($params, $custom_params_2);
599 $result_1 = $this->callAPISuccess('relationship', 'create', $params_1);
600 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
602 $this->assertNotNull($result_2['id']);
603 $this->assertEquals(0, $result_2['is_error']);
605 $this->relationshipTypeDelete($this->_relTypeID
);
609 * CRM-13725 - Two relationships of same type with same start and end date
610 * should be OK if the custom field values differ. In this case, the
611 * existing relationship does not have custom values, but the new one
614 public function testRelationshipCreateDuplicateWithCustomFields2() {
615 $this->createCustomGroupWithFieldsOfAllTypes();
617 $custom_params_2 = array(
618 $this->getCustomFieldName('text') => 'Hello! this is other custom data',
619 $this->getCustomFieldName('select_string') => 'Y',
620 $this->getCustomFieldName('select_date') => '2009-07-11 00:00:00',
621 $this->getCustomFieldName('link') => 'http://example.org',
625 'contact_id_a' => $this->_cId_a
,
626 'contact_id_b' => $this->_cId_b
,
627 'relationship_type_id' => $this->_relTypeID
,
628 'start_date' => '2008-12-20',
632 $params_2 = array_merge($params_1, $custom_params_2);
634 $this->callAPISuccess('relationship', 'create', $params_1);
635 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
637 $this->assertNotNull($result_2['id']);
638 $this->assertEquals(0, $result_2['is_error']);
640 $this->relationshipTypeDelete($this->_relTypeID
);
644 * CRM-13725 - Two relationships of same type with same start and end date
645 * should be OK if the custom field values differ. In this case, the
646 * existing relationship does have custom values, but the new one
649 public function testRelationshipCreateDuplicateWithCustomFields3() {
650 $this->createCustomGroupWithFieldsOfAllTypes();
653 $this->getCustomFieldName('text') => 'Hello! this is other custom data',
654 $this->getCustomFieldName('select_string') => 'Y',
655 $this->getCustomFieldName('select_date') => '2009-07-11 00:00:00',
656 $this->getCustomFieldName('link') => 'http://example.org',
660 'contact_id_a' => $this->_cId_a
,
661 'contact_id_b' => $this->_cId_b
,
662 'relationship_type_id' => $this->_relTypeID
,
663 'start_date' => '2008-12-20',
667 $params_1 = array_merge($params_2, $custom_params_1);
669 $this->callAPISuccess('relationship', 'create', $params_1);
670 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
672 $this->assertNotNull($result_2['id']);
673 $this->assertEquals(0, $result_2['is_error']);
675 $this->relationshipTypeDelete($this->_relTypeID
);
679 * Check with valid params array.
681 public function testRelationshipsGet() {
683 'contact_id_a' => $this->_cId_a
,
684 'contact_id_b' => $this->_cId_b
,
685 'relationship_type_id' => $this->_relTypeID
,
686 'start_date' => '2011-01-01',
687 'end_date' => '2013-01-01',
691 $this->callAPISuccess('relationship', 'create', $relParams);
695 'contact_id' => $this->_cId_b
,
697 $result = $this->callAPISuccess('relationship', 'get', $params);
698 $this->assertEquals($result['count'], 1);
700 'contact_id_a' => $this->_cId_a
,
702 $result = $this->callAPISuccess('relationship', 'get', $params);
703 $this->assertEquals($result['count'], 1);
704 // contact_id_a is wrong so should be no matches
706 'contact_id_a' => $this->_cId_b
,
708 $result = $this->callAPISuccess('relationship', 'get', $params);
709 $this->assertEquals($result['count'], 0);
713 * Chain Relationship.get and to Contact.get.
715 public function testRelationshipGetWithChainedCall() {
716 // Create a relationship.
717 $createResult = $this->callAPISuccess('relationship', 'create', $this->_params
);
718 $id = $createResult['id'];
720 // Try to retrieve it using chaining.
722 'relationship_type_id' => $this->_relTypeID
,
724 'api.Contact.get' => array(
725 'id' => '$value.contact_id_b',
729 $result = $this->callAPISuccess('relationship', 'get', $params);
731 $this->assertEquals(1, $result['count']);
732 $relationship = CRM_Utils_Array
::first($result['values']);
733 $this->assertEquals(1, $relationship['api.Contact.get']['count']);
734 $contact = CRM_Utils_Array
::first($relationship['api.Contact.get']['values']);
735 $this->assertEquals($this->_cId_b
, $contact['id']);
739 * Chain Contact.get to Relationship.get and again to Contact.get.
741 public function testRelationshipGetInChainedCall() {
742 // Create a relationship.
743 $this->callAPISuccess('relationship', 'create', $this->_params
);
745 // Try to retrieve it using chaining.
747 'id' => $this->_cId_a
,
748 'api.Relationship.get' => array(
749 'relationship_type_id' => $this->_relTypeID
,
750 'contact_id_a' => '$value.id',
751 'api.Contact.get' => array(
752 'id' => '$value.contact_id_b',
757 $result = $this->callAPISuccess('contact', 'get', $params);
758 $this->assertEquals(1, $result['count']);
759 $contact = CRM_Utils_Array
::first($result['values']);
760 $this->assertEquals(1, $contact['api.Relationship.get']['count']);
761 $relationship = CRM_Utils_Array
::first($contact['api.Relationship.get']['values']);
762 $this->assertEquals(1, $relationship['api.Contact.get']['count']);
763 $contact = CRM_Utils_Array
::first($relationship['api.Contact.get']['values']);
764 $this->assertEquals($this->_cId_b
, $contact['id']);
768 * Check with valid params array.
769 * (The get function will behave differently without 'contact_id' passed
771 public function testRelationshipsGetGeneric() {
773 'contact_id_a' => $this->_cId_a
,
774 'contact_id_b' => $this->_cId_b
,
775 'relationship_type_id' => $this->_relTypeID
,
776 'start_date' => '2011-01-01',
777 'end_date' => '2013-01-01',
781 $this->callAPISuccess('relationship', 'create', $relParams);
785 'contact_id_b' => $this->_cId_b
,
787 $this->callAPISuccess('relationship', 'get', $params);
791 * Test retrieving only current relationships.
793 public function testGetIsCurrent() {
795 'contact_id_a' => $this->_cId_a
,
796 'contact_id_b' => $this->_cId_b2
,
797 'relationship_type_id' => $this->_relTypeID
,
798 'start_date' => '2008-12-20',
801 $this->callAPISuccess('relationship', 'create', $rel2Params);
802 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params
);
805 'filters' => array('is_current' => 1),
807 $description = "Demonstrates is_current filter.";
808 $subfile = 'filterIsCurrent';
809 //no relationship has been created
810 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
811 $this->assertEquals($result['count'], 1);
812 $this->AssertEquals($rel1['id'], $result['id']);
814 // now try not started
815 $rel2Params['is_active'] = 1;
816 $rel2Params['start_date'] = 'tomorrow';
817 $this->callAPISuccess('relationship', 'create', $rel2Params);
818 $result = $this->callAPISuccess('relationship', 'get', $getParams);
819 $this->assertEquals($result['count'], 1);
820 $this->AssertEquals($rel1['id'], $result['id']);
823 $rel2Params['is_active'] = 1;
824 $rel2Params['start_date'] = 'last week';
825 $rel2Params['end_date'] = 'yesterday';
826 $this->callAPISuccess('relationship', 'create', $rel2Params);
830 * Test using various operators.
832 public function testGetTypeOperators() {
833 $relTypeParams = array(
834 'name_a_b' => 'Relation 3 for delete',
835 'name_b_a' => 'Relation 6 for delete',
836 'description' => 'Testing relationship type 2',
837 'contact_type_a' => 'Individual',
838 'contact_type_b' => 'Organization',
842 $relationType2 = $this->relationshipTypeCreate($relTypeParams);
843 $relTypeParams = array(
844 'name_a_b' => 'Relation 8 for delete',
845 'name_b_a' => 'Relation 9 for delete',
846 'description' => 'Testing relationship type 7',
847 'contact_type_a' => 'Individual',
848 'contact_type_b' => 'Organization',
852 $relationType3 = $this->relationshipTypeCreate($relTypeParams);
854 $relTypeParams = array(
855 'name_a_b' => 'Relation 6 for delete',
856 'name_b_a' => 'Relation 88for delete',
857 'description' => 'Testing relationship type 00',
858 'contact_type_a' => 'Individual',
859 'contact_type_b' => 'Organization',
863 $relationType4 = $this->relationshipTypeCreate($relTypeParams);
865 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params
);
866 $rel2 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
867 array('relationship_type_id' => $relationType2)));
868 $rel3 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
869 array('relationship_type_id' => $relationType3)));
870 $rel4 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
871 array('relationship_type_id' => $relationType4)));
874 'relationship_type_id' => array('IN' => array($relationType2, $relationType3)),
877 $description = "Demonstrates use of IN filter.";
878 $subfile = 'INRelationshipType';
880 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
881 $this->assertEquals($result['count'], 2);
882 $this->AssertEquals(array($rel2['id'], $rel3['id']), array_keys($result['values']));
884 $description = "Demonstrates use of NOT IN filter.";
885 $subfile = 'NotInRelationshipType';
887 'relationship_type_id' => array('NOT IN' => array($relationType2, $relationType3)),
889 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
890 $this->assertEquals($result['count'], 2);
891 $this->AssertEquals(array($rel1['id'], $rel4['id']), array_keys($result['values']));
893 $description = "Demonstrates use of BETWEEN filter.";
894 $subfile = 'BetweenRelationshipType';
896 'relationship_type_id' => array('BETWEEN' => array($relationType2, $relationType4)),
898 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
899 $this->assertEquals($result['count'], 3);
900 $this->AssertEquals(array($rel2['id'], $rel3['id'], $rel4['id']), array_keys($result['values']));
902 $description = "Demonstrates use of Not BETWEEN filter.";
903 $subfile = 'NotBetweenRelationshipType';
905 'relationship_type_id' => array('NOT BETWEEN' => array($relationType2, $relationType4)),
907 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
908 $this->assertEquals($result['count'], 1);
909 $this->AssertEquals(array($rel1['id']), array_keys($result['values']));
914 * Check with invalid relationshipType Id.
916 public function testRelationshipTypeAddInvalidId() {
917 $relTypeParams = array(
919 'name_a_b' => 'Relation 1 for delete',
920 'name_b_a' => 'Relation 2 for delete',
921 'contact_type_a' => 'Individual',
922 'contact_type_b' => 'Organization',
924 $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
925 'id is not a valid integer');
929 * Check with valid data with contact_b.
931 public function testGetRelationshipWithContactB() {
933 'contact_id_a' => $this->_cId_a
,
934 'contact_id_b' => $this->_cId_b
,
935 'relationship_type_id' => $this->_relTypeID
,
936 'start_date' => '2011-01-01',
937 'end_date' => '2013-01-01',
941 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
944 'contact_id' => $this->_cId_a
,
947 $result = $this->callAPISuccess('relationship', 'get', $contacts);
948 $this->assertGreaterThan(0, $result['count']);
950 'id' => $relationship['id'],
952 $this->callAPISuccess('relationship', 'delete', $params);
953 $this->relationshipTypeDelete($this->_relTypeID
);
957 * Check with valid data with relationshipTypes.
959 public function testGetRelationshipWithRelTypes() {
961 'contact_id_a' => $this->_cId_a
,
962 'contact_id_b' => $this->_cId_b
,
963 'relationship_type_id' => $this->_relTypeID
,
964 'start_date' => '2011-01-01',
965 'end_date' => '2013-01-01',
969 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
972 'contact_id' => $this->_cId_a
,
974 $this->callAPISuccess('relationship', 'get', $contact_a);
977 'id' => $relationship['id'],
979 $this->callAPISuccess('relationship', 'delete', $params);
980 $this->relationshipTypeDelete($this->_relTypeID
);
984 * Checks that passing in 'contact_id' + a relationship type
985 * will filter by relationship type (relationships go in both directions)
986 * as relationship api does a reciprocal check if contact_id provided
988 * We should get 1 result without or with correct relationship type id & 0 with
991 public function testGetRelationshipByTypeReciprocal() {
992 $created = $this->callAPISuccess($this->entity
, 'create', $this->_params
);
993 $result = $this->callAPISuccess($this->entity
, 'get', array(
994 'contact_id' => $this->_cId_a
,
995 'relationship_type_id' => $this->_relTypeID
,
997 $this->assertEquals(1, $result['count']);
998 $result = $this->callAPISuccess($this->entity
, 'get', array(
999 'contact_id' => $this->_cId_a
,
1000 'relationship_type_id' => $this->_relTypeID +
1,
1002 $this->assertEquals(0, $result['count']);
1003 $this->callAPISuccess($this->entity
, 'delete', array('id' => $created['id']));
1007 * Checks that passing in 'contact_id_b' + a relationship type
1008 * will filter by relationship type for contact b
1010 * We should get 1 result without or with correct relationship type id & 0 with
1013 public function testGetRelationshipByTypeDAO() {
1014 $this->_ids
['relationship'] = $this->callAPISuccess($this->entity
, 'create', array('format.only_id' => TRUE) +
1016 $this->callAPISuccess($this->entity
, 'getcount', array(
1017 'contact_id_a' => $this->_cId_a
,
1019 $result = $this->callAPISuccess($this->entity
, 'get', array(
1020 'contact_id_a' => $this->_cId_a
,
1021 'relationship_type_id' => $this->_relTypeID
,
1023 $this->assertEquals(1, $result['count']);
1024 $result = $this->callAPISuccess($this->entity
, 'get', array(
1025 'contact_id_a' => $this->_cId_a
,
1026 'relationship_type_id' => $this->_relTypeID +
1,
1028 $this->assertEquals(0, $result['count']);
1032 * Checks that passing in 'contact_id_b' + a relationship type
1033 * will filter by relationship type for contact b
1035 * We should get 1 result without or with correct relationship type id & 0 with
1038 public function testGetRelationshipByTypeArrayDAO() {
1039 $this->callAPISuccess($this->entity
, 'create', $this->_params
);
1040 $org3 = $this->organizationCreate();
1041 // lets just assume built in ones aren't being messed with!
1043 // lets just assume built in ones aren't being messed with!
1047 $this->callAPISuccess($this->entity
, 'create',
1048 array_merge($this->_params
, array(
1049 'relationship_type_id' => $relType2,
1050 'contact_id_b' => $this->_cId_b2
,
1055 $this->callAPISuccess($this->entity
, 'create',
1056 array_merge($this->_params
, array(
1057 'relationship_type_id' => $relType3,
1058 'contact_id_b' => $org3,
1062 $result = $this->callAPISuccess($this->entity
, 'get', array(
1063 'contact_id_a' => $this->_cId_a
,
1064 'relationship_type_id' => array('IN' => array($this->_relTypeID
, $relType3)),
1067 $this->assertEquals(2, $result['count']);
1068 foreach ($result['values'] as $key => $value) {
1069 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID
, $relType3)));
1074 * Checks that passing in 'contact_id_b' + a relationship type
1075 * will filter by relationship type for contact b
1077 * We should get 1 result without or with correct relationship type id & 0 with
1080 public function testGetRelationshipByTypeArrayReciprocal() {
1081 $this->callAPISuccess($this->entity
, 'create', $this->_params
);
1082 $org3 = $this->organizationCreate();
1083 // lets just assume built in ones aren't being messed with!
1088 $this->callAPISuccess($this->entity
, 'create',
1089 array_merge($this->_params
, array(
1090 'relationship_type_id' => $relType2,
1091 'contact_id_b' => $this->_cId_b2
,
1096 $this->callAPISuccess($this->entity
, 'create',
1097 array_merge($this->_params
, array(
1098 'relationship_type_id' => $relType3,
1099 'contact_id_b' => $org3,
1103 $result = $this->callAPISuccess($this->entity
, 'get', array(
1104 'contact_id' => $this->_cId_a
,
1105 'relationship_type_id' => array('IN' => array($this->_relTypeID
, $relType3)),
1108 $this->assertEquals(2, $result['count']);
1109 foreach ($result['values'] as $key => $value) {
1110 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID
, $relType3)));
1115 * Test relationship get by membership type.
1117 * Checks that passing in 'contact_id_b' + a relationship type
1118 * will filter by relationship type for contact b
1120 * We should get 1 result without or with correct relationship type id & 0 with
1123 public function testGetRelationshipByMembershipTypeDAO() {
1124 $this->callAPISuccess($this->entity
, 'create', $this->_params
);
1125 $org3 = $this->organizationCreate();
1127 // lets just assume built in ones aren't being messed with!
1129 // lets just assume built in ones aren't being messed with!
1132 $memberType = $this->membershipTypeCreate(array(
1133 'relationship_type_id' => CRM_Core_DAO
::VALUE_SEPARATOR
. $relType1 . CRM_Core_DAO
::VALUE_SEPARATOR
. $relType3 . CRM_Core_DAO
::VALUE_SEPARATOR
,
1134 'relationship_direction' => CRM_Core_DAO
::VALUE_SEPARATOR
. 'a_b' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'b_a' . CRM_Core_DAO
::VALUE_SEPARATOR
,
1138 $this->callAPISuccess($this->entity
, 'create',
1139 array_merge($this->_params
, array(
1140 'relationship_type_id' => $relType2,
1141 'contact_id_b' => $this->_cId_b2
,
1146 $this->callAPISuccess($this->entity
, 'create',
1147 array_merge($this->_params
, array(
1148 'relationship_type_id' => $relType3,
1149 'contact_id_b' => $org3,
1153 // Relationship 4 with reversal.
1154 $this->callAPISuccess($this->entity
, 'create',
1155 array_merge($this->_params
, array(
1156 'relationship_type_id' => $relType1,
1157 'contact_id_a' => $this->_cId_a
,
1158 'contact_id_b' => $this->_cId_a_2
,
1162 $result = $this->callAPISuccess($this->entity
, 'get', array(
1163 'contact_id_a' => $this->_cId_a
,
1164 'membership_type_id' => $memberType,
1166 // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1167 $this->assertEquals(1, $result['count']);
1168 foreach ($result['values'] as $key => $value) {
1169 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1)));
1174 * Checks that passing in 'contact_id_b' + a relationship type
1175 * will filter by relationship type for contact b
1177 * We should get 1 result without or with correct relationship type id & 0 with
1180 public function testGetRelationshipByMembershipTypeReciprocal() {
1181 $this->callAPISuccess($this->entity
, 'create', $this->_params
);
1182 $org3 = $this->organizationCreate();
1184 // Let's just assume built in ones aren't being messed with!
1188 $memberType = $this->membershipTypeCreate(array(
1189 'relationship_type_id' => CRM_Core_DAO
::VALUE_SEPARATOR
. $relType1 . CRM_Core_DAO
::VALUE_SEPARATOR
. $relType3 . CRM_Core_DAO
::VALUE_SEPARATOR
,
1190 'relationship_direction' => CRM_Core_DAO
::VALUE_SEPARATOR
. 'a_b' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'b_a' . CRM_Core_DAO
::VALUE_SEPARATOR
,
1194 $this->callAPISuccess($this->entity
, 'create',
1195 array_merge($this->_params
, array(
1196 'relationship_type_id' => $relType2,
1197 'contact_id_b' => $this->_cId_b2
,
1202 $this->callAPISuccess($this->entity
, 'create',
1203 array_merge($this->_params
, array(
1204 'relationship_type_id' => $relType3,
1205 'contact_id_b' => $org3,
1209 // Relationship 4 with reversal.
1210 $this->callAPISuccess($this->entity
, 'create',
1211 array_merge($this->_params
, array(
1212 'relationship_type_id' => $relType1,
1213 'contact_id_a' => $this->_cId_a
,
1214 'contact_id_b' => $this->_cId_a_2
,
1218 $result = $this->callAPISuccess($this->entity
, 'get', array(
1219 'contact_id' => $this->_cId_a
,
1220 'membership_type_id' => $memberType,
1222 // Although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1223 $this->assertEquals(2, $result['count']);
1225 foreach ($result['values'] as $key => $value) {
1226 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1, $relType3)));
1231 * Check for e-notices on enable & disable as reported in CRM-14350
1233 public function testSetActive() {
1234 $relationship = $this->callAPISuccess($this->entity
, 'create', $this->_params
);
1235 $this->callAPISuccess($this->entity
, 'create', array('id' => $relationship['id'], 'is_active' => 0));
1236 $this->callAPISuccess($this->entity
, 'create', array('id' => $relationship['id'], 'is_active' => 1));
1240 * Test creating related memberships.
1242 public function testCreateRelatedMembership() {
1243 $relatedMembershipType = $this->callAPISuccess('MembershipType', 'create', array(
1244 'name' => 'Membership with Related',
1245 'member_of_contact_id' => 1,
1246 'financial_type_id' => 1,
1247 'minimum_fee' => 0.00,
1248 'duration_unit' => 'year',
1249 'duration_interval' => 1,
1250 'period_type' => 'rolling',
1251 'relationship_type_id' => $this->_relTypeID
,
1252 'relationship_direction' => 'b_a',
1253 'visibility' => 'Public',
1256 'domain_id' => CRM_Core_Config
::domainID(),
1258 $originalMembership = $this->callAPISuccess('Membership', 'create', array(
1259 'membership_type_id' => $relatedMembershipType['id'],
1260 'contact_id' => $this->_cId_b
,
1262 $this->callAPISuccess('Relationship', 'create', array(
1263 'relationship_type_id' => $this->_relTypeID
,
1264 'contact_id_a' => $this->_cId_a
,
1265 'contact_id_b' => $this->_cId_b
,
1267 $contactAMembership = $this->callAPISuccessGetSingle('membership', array('contact_id' => $this->_cId_a
));
1268 $this->assertEquals($originalMembership['id'], $contactAMembership['owner_membership_id']);
1270 // Adding a relationship with a future start date should NOT create a membership
1271 $this->callAPISuccess('Relationship', 'create', array(
1272 'relationship_type_id' => $this->_relTypeID
,
1273 'contact_id_a' => $this->_cId_a_2
,
1274 'contact_id_b' => $this->_cId_b
,
1275 'start_date' => 'now + 1 week',
1277 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_cId_a_2
), 0);
1279 // Deleting the organization should cause the related membership to be deleted.
1280 $this->callAPISuccess('contact', 'delete', array('id' => $this->_cId_b
));
1281 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_cId_a
), 0);