3 * +--------------------------------------------------------------------+
4 * | CiviCRM version 4.7 |
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 * Check if required fields are not passed.
115 public function testRelationshipCreateWithoutRequired() {
117 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
118 'end_date' => array('d' => '10', 'M' => '1', 'Y' => '2009'),
122 $this->callAPIFailure('relationship', 'create', $params);
126 * Check with incorrect required fields.
128 public function testRelationshipCreateWithIncorrectData() {
131 'contact_id_a' => $this->_cId_a
,
132 'contact_id_b' => $this->_cId_b
,
133 'relationship_type_id' => 'Breaking Relationship',
136 $this->callAPIFailure('relationship', 'create', $params);
138 //contact id is not an integer
140 'contact_id_a' => 'invalid',
141 'contact_id_b' => $this->_cId_b
,
142 'relationship_type_id' => $this->_relTypeID
,
143 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
146 $this->callAPIFailure('relationship', 'create', $params);
148 // Contact id does not exist.
149 $params['contact_id_a'] = 999;
150 $this->callAPIFailure('relationship', 'create', $params);
153 $params['contact_id_a'] = $this->_cId_a
;
154 $params['start_date'] = array('d' => '1', 'M' => '1');
155 $this->callAPIFailure('relationship', 'create', $params);
159 * Check relationship creation with invalid Relationship.
161 public function testRelationshipCreateInvalidRelationship() {
162 // Both have the contact type Individual.
164 'contact_id_a' => $this->_cId_a
,
165 'contact_id_b' => $this->_cId_a
,
166 'relationship_type_id' => $this->_relTypeID
,
167 'start_date' => '2008-01-10',
171 $this->callAPIFailure('relationship', 'create', $params);
173 // both the contact of type Organization
175 'contact_id_a' => $this->_cId_b
,
176 'contact_id_b' => $this->_cId_b
,
177 'relationship_type_id' => $this->_relTypeID
,
178 'start_date' => '2008-01-10',
182 $this->callAPIFailure('relationship', 'create', $params);
186 * Check relationship already exists.
188 public function testRelationshipCreateAlreadyExists() {
190 'contact_id_a' => $this->_cId_a
,
191 'contact_id_b' => $this->_cId_b
,
192 'relationship_type_id' => $this->_relTypeID
,
193 'start_date' => '2008-12-20',
197 $relationship = $this->callAPISuccess('relationship', 'create', $params);
200 'contact_id_a' => $this->_cId_a
,
201 'contact_id_b' => $this->_cId_b
,
202 'relationship_type_id' => $this->_relTypeID
,
203 'start_date' => '2008-12-20',
206 $this->callAPIFailure('relationship', 'create', $params, 'Duplicate Relationship');
208 $params['id'] = $relationship['id'];
209 $this->callAPISuccess('relationship', 'delete', $params);
213 * Check relationship already exists.
215 public function testRelationshipCreateUpdateAlreadyExists() {
217 'contact_id_a' => $this->_cId_a
,
218 'contact_id_b' => $this->_cId_b
,
219 'relationship_type_id' => $this->_relTypeID
,
220 'start_date' => '2008-12-20',
225 $relationship = $this->callAPISuccess('relationship', 'create', $params);
227 'id' => $relationship['id'],
231 $this->callAPISuccess('relationship', 'create', $params);
232 $this->callAPISuccess('relationship', 'get', $params);
233 $params['id'] = $relationship['id'];
234 $this->callAPISuccess('relationship', 'delete', $params);
238 * Check update doesn't reset stuff badly - CRM-11789.
240 public function testRelationshipCreateUpdateDoesNotMangle() {
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',
247 'is_permission_a_b' => 1,
248 'description' => 'my desc',
250 $relationship = $this->callAPISuccess('relationship', 'create', $params);
252 $updateParams = array(
253 'id' => $relationship['id'],
254 'relationship_type_id' => $this->_relTypeID
,
256 $this->callAPISuccess('relationship', 'create', $updateParams);
258 //make sure the orig params didn't get changed
259 $this->getAndCheck($params, $relationship['id'], 'relationship');
265 * Check relationship creation.
267 public function testRelationshipCreate() {
269 'contact_id_a' => $this->_cId_a
,
270 'contact_id_b' => $this->_cId_b
,
271 'relationship_type_id' => $this->_relTypeID
,
272 'start_date' => '2010-10-30',
273 'end_date' => '2010-12-30',
278 $result = $this->callAPIAndDocument('relationship', 'create', $params, __FUNCTION__
, __FILE__
);
279 $this->assertNotNull($result['id']);
280 $relationParams = array(
281 'id' => $result['id'],
284 // assertDBState compares expected values in $result to actual values in the DB
285 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
286 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
287 $values = $result['values'][$result['id']];
288 foreach ($params as $key => $value) {
289 if ($key == 'note') {
292 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE));
294 $params['id'] = $result['id'];
295 $this->callAPISuccess('relationship', 'delete', $params);
299 * Ensure disabling works.
301 public function testRelationshipUpdate() {
302 $result = $this->callAPISuccess('relationship', 'create', $this->_params
);
303 $relID = $result['id'];
304 $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'description' => 'blah'));
305 $this->assertEquals($relID, $result['id']);
307 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
309 $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'is_permission_b_a' => 1));
310 $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
311 $result = $this->callAPISuccess('relationship', 'create', array('id' => $result['id'], 'is_active' => 0));
312 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
313 $this->assertEquals(0, $result['values'][$result['id']]['is_active']);
314 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
315 $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
319 * Check relationship creation.
321 public function testRelationshipCreateEmptyEndDate() {
323 'contact_id_a' => $this->_cId_a
,
324 'contact_id_b' => $this->_cId_b
,
325 'relationship_type_id' => $this->_relTypeID
,
326 'start_date' => '2010-10-30',
332 $result = $this->callAPISuccess('relationship', 'create', $params);
333 $this->assertNotNull($result['id']);
334 $relationParams = array(
335 'id' => $result['id'],
338 // assertDBState compares expected values in $result to actual values in the DB
339 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
340 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
341 $values = $result['values'][$result['id']];
342 foreach ($params as $key => $value) {
343 if ($key == 'note') {
346 if ($key == 'end_date') {
347 $this->assertTrue(empty($values[$key]));
350 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__
);
352 $params['id'] = $result['id'];
353 $this->callAPISuccess('relationship', 'delete', $params);
357 * Check relationship creation with custom data.
359 public function testRelationshipCreateEditWithCustomData() {
360 $this->createCustomGroup();
361 $this->_ids
= $this->createCustomField();
362 //few custom Values for comparing
363 $custom_params = array(
364 "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship',
365 "custom_{$this->_ids[1]}" => 'Y',
366 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
367 "custom_{$this->_ids[3]}" => 'http://example.com',
371 'contact_id_a' => $this->_cId_a
,
372 'contact_id_b' => $this->_cId_b
,
373 'relationship_type_id' => $this->_relTypeID
,
374 'start_date' => '2008-12-20',
377 $params = array_merge($params, $custom_params);
378 $result = $this->callAPISuccess('relationship', 'create', $params);
380 $relationParams = array(
381 'id' => $result['id'],
383 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
385 //Test Edit of custom field from the form.
386 $getParams = array('id' => $result['id']);
387 $updateParams = array_merge($getParams, array(
388 "custom_{$this->_ids[0]}" => 'Edited Text Value',
389 'relationship_type_id' => $this->_relTypeID
. '_b_a',
390 'related_contact_id' => $this->_cId_a
,
392 $reln = new CRM_Contact_Form_Relationship();
393 $reln->_action
= CRM_Core_Action
::UPDATE
;
394 $reln->_relationshipId
= $result['id'];
395 $reln->submit($updateParams);
397 $check = $this->callAPISuccess('relationship', 'get', $getParams);
398 $this->assertEquals("Edited Text Value", $check['values'][$check['id']]["custom_{$this->_ids[0]}"]);
400 $params['id'] = $result['id'];
401 $this->callAPISuccess('relationship', 'delete', $params);
402 $this->relationshipTypeDelete($this->_relTypeID
);
406 * Check with complete array + custom field
407 * Note that the test is written on purpose without any
408 * variables specific to participant so it can be replicated into other entities
409 * and / or moved to the automated test suite
411 public function testGetWithCustom() {
412 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
414 $params = $this->_params
;
415 $params['custom_' . $ids['custom_field_id']] = "custom string";
417 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
418 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
420 $getParams = array('id' => $result['id']);
421 $check = $this->callAPIAndDocument($this->_entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
422 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
424 $this->customFieldDelete($ids['custom_field_id']);
425 $this->customGroupDelete($ids['custom_group_id']);
431 public function createCustomGroup() {
433 'title' => 'Custom Group',
434 'extends' => array('Relationship'),
440 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
441 $this->_customGroupId
= $customGroup['id'];
442 return $customGroup['id'];
448 public function createCustomField() {
451 'custom_group_id' => $this->_customGroupId
,
452 'label' => 'Enter text about relationship',
453 'html_type' => 'Text',
454 'data_type' => 'String',
455 'default_value' => 'xyz',
458 'is_searchable' => 0,
462 $customField = $this->callAPISuccess('CustomField', 'create', $params);
463 $ids[] = $customField['id'];
465 $optionValue[] = array(
471 $optionValue[] = array(
477 $optionValue[] = array(
485 'label' => 'Pick Color',
486 'html_type' => 'Select',
487 'data_type' => 'String',
490 'is_searchable' => 0,
492 'option_values' => $optionValue,
493 'custom_group_id' => $this->_customGroupId
,
496 $customField = $this->callAPISuccess('custom_field', 'create', $params);
497 $ids[] = $customField['id'];
500 'custom_group_id' => $this->_customGroupId
,
501 'name' => 'test_date',
502 'label' => 'test_date',
503 'html_type' => 'Select Date',
504 'data_type' => 'Date',
505 'default_value' => '20090711',
508 'is_searchable' => 0,
512 $customField = $this->callAPISuccess('custom_field', 'create', $params);
514 $ids[] = $customField['id'];
516 'custom_group_id' => $this->_customGroupId
,
517 'name' => 'test_link',
518 'label' => 'test_link',
519 'html_type' => 'Link',
520 'data_type' => 'Link',
521 'default_value' => 'http://civicrm.org',
524 'is_searchable' => 0,
528 $customField = $this->callAPISuccess('custom_field', 'create', $params);
529 $ids[] = $customField['id'];
534 * Check with empty array.
536 public function testRelationshipDeleteEmpty() {
537 $this->callAPIFailure('relationship', 'delete', array(), 'Mandatory key(s) missing from params array: id');
541 * Check if required fields are not passed.
543 public function testRelationshipDeleteWithoutRequired() {
545 'start_date' => '2008-12-20',
546 'end_date' => '2009-12-20',
550 $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
554 * Check with incorrect required fields.
556 public function testRelationshipDeleteWithIncorrectData() {
558 'contact_id_a' => $this->_cId_a
,
559 'contact_id_b' => $this->_cId_b
,
560 'relationship_type_id' => 'Breaking Relationship',
563 $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
565 $params['id'] = "Invalid";
566 $this->callAPIFailure('relationship', 'delete', $params, 'id is not a valid integer');
570 * Check relationship creation.
572 public function testRelationshipDelete() {
574 'contact_id_a' => $this->_cId_a
,
575 'contact_id_b' => $this->_cId_b
,
576 'relationship_type_id' => $this->_relTypeID
,
577 'start_date' => '2008-12-20',
581 $result = $this->callAPISuccess('relationship', 'create', $params);
582 $params = array('id' => $result['id']);
583 $this->callAPIAndDocument('relationship', 'delete', $params, __FUNCTION__
, __FILE__
);
584 $this->relationshipTypeDelete($this->_relTypeID
);
587 ///////////////// civicrm_relationship_update methods
590 * Check with empty array.
592 public function testRelationshipUpdateEmpty() {
593 $this->callAPIFailure('relationship', 'create', array(),
594 'Mandatory key(s) missing from params array: contact_id_a, contact_id_b, relationship_type_id');
598 * Check if required fields are not passed.
602 * Check relationship update.
604 public function testRelationshipCreateDuplicate() {
606 'contact_id_a' => $this->_cId_a
,
607 'contact_id_b' => $this->_cId_b
,
608 'relationship_type_id' => $this->_relTypeID
,
609 'start_date' => '20081214',
610 'end_date' => '20091214',
614 $result = $this->callAPISuccess('relationship', 'create', $relParams);
616 $this->assertNotNull($result['id']);
619 'contact_id_a' => $this->_cId_a
,
620 'contact_id_b' => $this->_cId_b
,
621 'relationship_type_id' => $this->_relTypeID
,
622 'start_date' => '20081214',
623 'end_date' => '20091214',
627 $this->callAPIFailure('relationship', 'create', $params, 'Duplicate Relationship');
629 $this->callAPISuccess('relationship', 'delete', array('id' => $result['id']));
630 $this->relationshipTypeDelete($this->_relTypeID
);
634 * CRM-13725 - Two relationships of same type with same start and end date
635 * should be OK if the custom field values differ.
637 public function testRelationshipCreateDuplicateWithCustomFields() {
638 $this->createCustomGroup();
639 $this->_ids
= $this->createCustomField();
641 $custom_params_1 = array(
642 "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship',
643 "custom_{$this->_ids[1]}" => 'Y',
644 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
645 "custom_{$this->_ids[3]}" => 'http://example.com',
648 $custom_params_2 = array(
649 "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
650 "custom_{$this->_ids[1]}" => 'Y',
651 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
652 "custom_{$this->_ids[3]}" => 'http://example.org',
656 'contact_id_a' => $this->_cId_a
,
657 'contact_id_b' => $this->_cId_b
,
658 'relationship_type_id' => $this->_relTypeID
,
659 'start_date' => '2008-12-20',
663 $params_1 = array_merge($params, $custom_params_1);
664 $params_2 = array_merge($params, $custom_params_2);
666 $result_1 = $this->callAPISuccess('relationship', 'create', $params_1);
667 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
669 $this->assertNotNull($result_2['id']);
670 $this->assertEquals(0, $result_2['is_error']);
672 $this->relationshipTypeDelete($this->_relTypeID
);
676 * CRM-13725 - Two relationships of same type with same start and end date
677 * should be OK if the custom field values differ. In this case, the
678 * existing relationship does not have custom values, but the new one
681 public function testRelationshipCreateDuplicateWithCustomFields2() {
682 $this->createCustomGroup();
683 $this->_ids
= $this->createCustomField();
685 $custom_params_2 = array(
686 "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
687 "custom_{$this->_ids[1]}" => 'Y',
688 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
689 "custom_{$this->_ids[3]}" => 'http://example.org',
693 'contact_id_a' => $this->_cId_a
,
694 'contact_id_b' => $this->_cId_b
,
695 'relationship_type_id' => $this->_relTypeID
,
696 'start_date' => '2008-12-20',
700 $params_2 = array_merge($params_1, $custom_params_2);
702 $this->callAPISuccess('relationship', 'create', $params_1);
703 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
705 $this->assertNotNull($result_2['id']);
706 $this->assertEquals(0, $result_2['is_error']);
708 $this->relationshipTypeDelete($this->_relTypeID
);
712 * CRM-13725 - Two relationships of same type with same start and end date
713 * should be OK if the custom field values differ. In this case, the
714 * existing relationship does have custom values, but the new one
717 public function testRelationshipCreateDuplicateWithCustomFields3() {
718 $this->createCustomGroup();
719 $this->_ids
= $this->createCustomField();
721 $custom_params_1 = array(
722 "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
723 "custom_{$this->_ids[1]}" => 'Y',
724 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
725 "custom_{$this->_ids[3]}" => 'http://example.org',
729 'contact_id_a' => $this->_cId_a
,
730 'contact_id_b' => $this->_cId_b
,
731 'relationship_type_id' => $this->_relTypeID
,
732 'start_date' => '2008-12-20',
736 $params_1 = array_merge($params_2, $custom_params_1);
738 $this->callAPISuccess('relationship', 'create', $params_1);
739 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
741 $this->assertNotNull($result_2['id']);
742 $this->assertEquals(0, $result_2['is_error']);
744 $this->relationshipTypeDelete($this->_relTypeID
);
748 * Check with valid params array.
750 public function testRelationshipsGet() {
752 'contact_id_a' => $this->_cId_a
,
753 'contact_id_b' => $this->_cId_b
,
754 'relationship_type_id' => $this->_relTypeID
,
755 'start_date' => '2011-01-01',
756 'end_date' => '2013-01-01',
760 $this->callAPISuccess('relationship', 'create', $relParams);
764 'contact_id' => $this->_cId_b
,
766 $result = $this->callAPISuccess('relationship', 'get', $params);
767 $this->assertEquals($result['count'], 1);
769 'contact_id_a' => $this->_cId_a
,
771 $result = $this->callAPISuccess('relationship', 'get', $params);
772 $this->assertEquals($result['count'], 1);
773 // contact_id_a is wrong so should be no matches
775 'contact_id_a' => $this->_cId_b
,
777 $result = $this->callAPISuccess('relationship', 'get', $params);
778 $this->assertEquals($result['count'], 0);
782 * Chain Relationship.get and to Contact.get.
784 public function testRelationshipGetWithChainedCall() {
785 // Create a relationship.
786 $createResult = $this->callAPISuccess('relationship', 'create', $this->_params
);
787 $id = $createResult['id'];
789 // Try to retrieve it using chaining.
791 'relationship_type_id' => $this->_relTypeID
,
793 'api.Contact.get' => array(
794 'id' => '$value.contact_id_b',
798 $result = $this->callAPISuccess('relationship', 'get', $params);
800 $this->assertEquals(1, $result['count']);
801 $relationship = CRM_Utils_Array
::first($result['values']);
802 $this->assertEquals(1, $relationship['api.Contact.get']['count']);
803 $contact = CRM_Utils_Array
::first($relationship['api.Contact.get']['values']);
804 $this->assertEquals($this->_cId_b
, $contact['id']);
808 * Chain Contact.get to Relationship.get and again to Contact.get.
810 public function testRelationshipGetInChainedCall() {
811 // Create a relationship.
812 $this->callAPISuccess('relationship', 'create', $this->_params
);
814 // Try to retrieve it using chaining.
816 'id' => $this->_cId_a
,
817 'api.Relationship.get' => array(
818 'relationship_type_id' => $this->_relTypeID
,
819 'contact_id_a' => '$value.id',
820 'api.Contact.get' => array(
821 'id' => '$value.contact_id_b',
826 $result = $this->callAPISuccess('contact', 'get', $params);
827 $this->assertEquals(1, $result['count']);
828 $contact = CRM_Utils_Array
::first($result['values']);
829 $this->assertEquals(1, $contact['api.Relationship.get']['count']);
830 $relationship = CRM_Utils_Array
::first($contact['api.Relationship.get']['values']);
831 $this->assertEquals(1, $relationship['api.Contact.get']['count']);
832 $contact = CRM_Utils_Array
::first($relationship['api.Contact.get']['values']);
833 $this->assertEquals($this->_cId_b
, $contact['id']);
837 * Check with valid params array.
838 * (The get function will behave differently without 'contact_id' passed
840 public function testRelationshipsGetGeneric() {
842 'contact_id_a' => $this->_cId_a
,
843 'contact_id_b' => $this->_cId_b
,
844 'relationship_type_id' => $this->_relTypeID
,
845 'start_date' => '2011-01-01',
846 'end_date' => '2013-01-01',
850 $this->callAPISuccess('relationship', 'create', $relParams);
854 'contact_id_b' => $this->_cId_b
,
856 $this->callAPISuccess('relationship', 'get', $params);
860 * Test retrieving only current relationships.
862 public function testGetIsCurrent() {
864 'contact_id_a' => $this->_cId_a
,
865 'contact_id_b' => $this->_cId_b2
,
866 'relationship_type_id' => $this->_relTypeID
,
867 'start_date' => '2008-12-20',
870 $this->callAPISuccess('relationship', 'create', $rel2Params);
871 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params
);
874 'filters' => array('is_current' => 1),
876 $description = "Demonstrates is_current filter.";
877 $subfile = 'filterIsCurrent';
878 //no relationship has been created
879 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
880 $this->assertEquals($result['count'], 1);
881 $this->AssertEquals($rel1['id'], $result['id']);
883 // now try not started
884 $rel2Params['is_active'] = 1;
885 $rel2Params['start_date'] = 'tomorrow';
886 $this->callAPISuccess('relationship', 'create', $rel2Params);
887 $result = $this->callAPISuccess('relationship', 'get', $getParams);
888 $this->assertEquals($result['count'], 1);
889 $this->AssertEquals($rel1['id'], $result['id']);
892 $rel2Params['is_active'] = 1;
893 $rel2Params['start_date'] = 'last week';
894 $rel2Params['end_date'] = 'yesterday';
895 $this->callAPISuccess('relationship', 'create', $rel2Params);
899 * Test using various operators.
901 public function testGetTypeOperators() {
902 $relTypeParams = array(
903 'name_a_b' => 'Relation 3 for delete',
904 'name_b_a' => 'Relation 6 for delete',
905 'description' => 'Testing relationship type 2',
906 'contact_type_a' => 'Individual',
907 'contact_type_b' => 'Organization',
911 $relationType2 = $this->relationshipTypeCreate($relTypeParams);
912 $relTypeParams = array(
913 'name_a_b' => 'Relation 8 for delete',
914 'name_b_a' => 'Relation 9 for delete',
915 'description' => 'Testing relationship type 7',
916 'contact_type_a' => 'Individual',
917 'contact_type_b' => 'Organization',
921 $relationType3 = $this->relationshipTypeCreate($relTypeParams);
923 $relTypeParams = array(
924 'name_a_b' => 'Relation 6 for delete',
925 'name_b_a' => 'Relation 88for delete',
926 'description' => 'Testing relationship type 00',
927 'contact_type_a' => 'Individual',
928 'contact_type_b' => 'Organization',
932 $relationType4 = $this->relationshipTypeCreate($relTypeParams);
934 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params
);
935 $rel2 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
936 array('relationship_type_id' => $relationType2)));
937 $rel3 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
938 array('relationship_type_id' => $relationType3)));
939 $rel4 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
940 array('relationship_type_id' => $relationType4)));
943 'relationship_type_id' => array('IN' => array($relationType2, $relationType3)),
946 $description = "Demonstrates use of IN filter.";
947 $subfile = 'INRelationshipType';
949 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
950 $this->assertEquals($result['count'], 2);
951 $this->AssertEquals(array($rel2['id'], $rel3['id']), array_keys($result['values']));
953 $description = "Demonstrates use of NOT IN filter.";
954 $subfile = 'NotInRelationshipType';
956 'relationship_type_id' => array('NOT IN' => array($relationType2, $relationType3)),
958 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
959 $this->assertEquals($result['count'], 2);
960 $this->AssertEquals(array($rel1['id'], $rel4['id']), array_keys($result['values']));
962 $description = "Demonstrates use of BETWEEN filter.";
963 $subfile = 'BetweenRelationshipType';
965 'relationship_type_id' => array('BETWEEN' => array($relationType2, $relationType4)),
967 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
968 $this->assertEquals($result['count'], 3);
969 $this->AssertEquals(array($rel2['id'], $rel3['id'], $rel4['id']), array_keys($result['values']));
971 $description = "Demonstrates use of Not BETWEEN filter.";
972 $subfile = 'NotBetweenRelationshipType';
974 'relationship_type_id' => array('NOT BETWEEN' => array($relationType2, $relationType4)),
976 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
977 $this->assertEquals($result['count'], 1);
978 $this->AssertEquals(array($rel1['id']), array_keys($result['values']));
983 * Check with invalid relationshipType Id.
985 public function testRelationshipTypeAddInvalidId() {
986 $relTypeParams = array(
988 'name_a_b' => 'Relation 1 for delete',
989 'name_b_a' => 'Relation 2 for delete',
990 'contact_type_a' => 'Individual',
991 'contact_type_b' => 'Organization',
993 $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
994 'id is not a valid integer');
998 * Check with valid data with contact_b.
1000 public function testGetRelationshipWithContactB() {
1002 'contact_id_a' => $this->_cId_a
,
1003 'contact_id_b' => $this->_cId_b
,
1004 'relationship_type_id' => $this->_relTypeID
,
1005 'start_date' => '2011-01-01',
1006 'end_date' => '2013-01-01',
1010 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
1013 'contact_id' => $this->_cId_a
,
1016 $result = $this->callAPISuccess('relationship', 'get', $contacts);
1017 $this->assertGreaterThan(0, $result['count']);
1019 'id' => $relationship['id'],
1021 $this->callAPISuccess('relationship', 'delete', $params);
1022 $this->relationshipTypeDelete($this->_relTypeID
);
1026 * Check with valid data with relationshipTypes.
1028 public function testGetRelationshipWithRelTypes() {
1030 'contact_id_a' => $this->_cId_a
,
1031 'contact_id_b' => $this->_cId_b
,
1032 'relationship_type_id' => $this->_relTypeID
,
1033 'start_date' => '2011-01-01',
1034 'end_date' => '2013-01-01',
1038 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
1041 'contact_id' => $this->_cId_a
,
1043 $this->callAPISuccess('relationship', 'get', $contact_a);
1046 'id' => $relationship['id'],
1048 $this->callAPISuccess('relationship', 'delete', $params);
1049 $this->relationshipTypeDelete($this->_relTypeID
);
1053 * Checks that passing in 'contact_id' + a relationship type
1054 * will filter by relationship type (relationships go in both directions)
1055 * as relationship api does a reciprocal check if contact_id provided
1057 * We should get 1 result without or with correct relationship type id & 0 with
1060 public function testGetRelationshipByTypeReciprocal() {
1061 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1062 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1063 'contact_id' => $this->_cId_a
,
1064 'relationship_type_id' => $this->_relTypeID
,
1066 $this->assertEquals(1, $result['count']);
1067 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1068 'contact_id' => $this->_cId_a
,
1069 'relationship_type_id' => $this->_relTypeID +
1,
1071 $this->assertEquals(0, $result['count']);
1072 $this->callAPISuccess($this->_entity
, 'delete', array('id' => $created['id']));
1076 * Checks that passing in 'contact_id_b' + a relationship type
1077 * will filter by relationship type for contact b
1079 * We should get 1 result without or with correct relationship type id & 0 with
1082 public function testGetRelationshipByTypeDAO() {
1083 $this->_ids
['relationship'] = $this->callAPISuccess($this->_entity
, 'create', array('format.only_id' => TRUE) +
1085 $this->callAPISuccess($this->_entity
, 'getcount', array(
1086 'contact_id_a' => $this->_cId_a
,
1089 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1090 'contact_id_a' => $this->_cId_a
,
1091 'relationship_type_id' => $this->_relTypeID
,
1093 $this->assertEquals(1, $result['count']);
1094 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1095 'contact_id_a' => $this->_cId_a
,
1096 'relationship_type_id' => $this->_relTypeID +
1,
1098 $this->assertEquals(0, $result['count']);
1102 * Checks that passing in 'contact_id_b' + a relationship type
1103 * will filter by relationship type for contact b
1105 * We should get 1 result without or with correct relationship type id & 0 with
1108 public function testGetRelationshipByTypeArrayDAO() {
1109 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1110 $org3 = $this->organizationCreate();
1111 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1112 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1115 $this->callAPISuccess($this->_entity
, 'create',
1116 array_merge($this->_params
, array(
1117 'relationship_type_id' => $relType2,
1118 'contact_id_b' => $this->_cId_b2
,
1123 $this->callAPISuccess($this->_entity
, 'create',
1124 array_merge($this->_params
, array(
1125 'relationship_type_id' => $relType3,
1126 'contact_id_b' => $org3,
1130 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1131 'contact_id_a' => $this->_cId_a
,
1132 'relationship_type_id' => array('IN' => array($this->_relTypeID
, $relType3)),
1135 $this->assertEquals(2, $result['count']);
1136 foreach ($result['values'] as $key => $value) {
1137 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID
, $relType3)));
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 testGetRelationshipByTypeArrayReciprocal() {
1149 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1150 $org3 = $this->organizationCreate();
1151 // lets just assume built in ones aren't being messed with!
1156 $this->callAPISuccess($this->_entity
, 'create',
1157 array_merge($this->_params
, array(
1158 'relationship_type_id' => $relType2,
1159 'contact_id_b' => $this->_cId_b2
,
1164 $this->callAPISuccess($this->_entity
, 'create',
1165 array_merge($this->_params
, array(
1166 'relationship_type_id' => $relType3,
1167 'contact_id_b' => $org3,
1171 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1172 'contact_id' => $this->_cId_a
,
1173 'relationship_type_id' => array('IN' => array($this->_relTypeID
, $relType3)),
1176 $this->assertEquals(2, $result['count']);
1177 foreach ($result['values'] as $key => $value) {
1178 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID
, $relType3)));
1183 * Test relationship get by membership type.
1185 * Checks that passing in 'contact_id_b' + a relationship type
1186 * will filter by relationship type for contact b
1188 * We should get 1 result without or with correct relationship type id & 0 with
1191 public function testGetRelationshipByMembershipTypeDAO() {
1192 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1193 $org3 = $this->organizationCreate();
1195 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1196 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1198 $memberType = $this->membershipTypeCreate(array(
1199 'relationship_type_id' => CRM_Core_DAO
::VALUE_SEPARATOR
. $relType1 . CRM_Core_DAO
::VALUE_SEPARATOR
. $relType3 . CRM_Core_DAO
::VALUE_SEPARATOR
,
1200 'relationship_direction' => CRM_Core_DAO
::VALUE_SEPARATOR
. 'a_b' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'b_a' . CRM_Core_DAO
::VALUE_SEPARATOR
,
1204 $this->callAPISuccess($this->_entity
, 'create',
1205 array_merge($this->_params
, array(
1206 'relationship_type_id' => $relType2,
1207 'contact_id_b' => $this->_cId_b2
,
1212 $this->callAPISuccess($this->_entity
, 'create',
1213 array_merge($this->_params
, array(
1214 'relationship_type_id' => $relType3,
1215 'contact_id_b' => $org3,
1219 // Relationship 4 with reversal.
1220 $this->callAPISuccess($this->_entity
, 'create',
1221 array_merge($this->_params
, array(
1222 'relationship_type_id' => $relType1,
1223 'contact_id_a' => $this->_cId_a
,
1224 'contact_id_b' => $this->_cId_a_2
,
1228 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1229 'contact_id_a' => $this->_cId_a
,
1230 'membership_type_id' => $memberType,
1232 // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1233 $this->assertEquals(1, $result['count']);
1234 foreach ($result['values'] as $key => $value) {
1235 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1)));
1240 * Checks that passing in 'contact_id_b' + a relationship type
1241 * will filter by relationship type for contact b
1243 * We should get 1 result without or with correct relationship type id & 0 with
1246 public function testGetRelationshipByMembershipTypeReciprocal() {
1247 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1248 $org3 = $this->organizationCreate();
1250 // Let's just assume built in ones aren't being messed with!
1254 $memberType = $this->membershipTypeCreate(array(
1255 'relationship_type_id' => CRM_Core_DAO
::VALUE_SEPARATOR
. $relType1 . CRM_Core_DAO
::VALUE_SEPARATOR
. $relType3 . CRM_Core_DAO
::VALUE_SEPARATOR
,
1256 'relationship_direction' => CRM_Core_DAO
::VALUE_SEPARATOR
. 'a_b' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'b_a' . CRM_Core_DAO
::VALUE_SEPARATOR
,
1260 $this->callAPISuccess($this->_entity
, 'create',
1261 array_merge($this->_params
, array(
1262 'relationship_type_id' => $relType2,
1263 'contact_id_b' => $this->_cId_b2
,
1268 $this->callAPISuccess($this->_entity
, 'create',
1269 array_merge($this->_params
, array(
1270 'relationship_type_id' => $relType3,
1271 'contact_id_b' => $org3,
1275 // Relationship 4 with reversal.
1276 $this->callAPISuccess($this->_entity
, 'create',
1277 array_merge($this->_params
, array(
1278 'relationship_type_id' => $relType1,
1279 'contact_id_a' => $this->_cId_a
,
1280 'contact_id_b' => $this->_cId_a_2
,
1284 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1285 'contact_id' => $this->_cId_a
,
1286 'membership_type_id' => $memberType,
1288 // Although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1289 $this->assertEquals(2, $result['count']);
1291 foreach ($result['values'] as $key => $value) {
1292 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1, $relType3)));
1297 * Check for e-notices on enable & disable as reported in CRM-14350
1299 public function testSetActive() {
1300 $relationship = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1301 $this->callAPISuccess($this->_entity
, 'create', array('id' => $relationship['id'], 'is_active' => 0));
1302 $this->callAPISuccess($this->_entity
, 'create', array('id' => $relationship['id'], 'is_active' => 1));
1306 * Test creating related memberships.
1308 public function testCreateRelatedMembership() {
1309 $relatedMembershipType = $this->callAPISuccess('MembershipType', 'create', array(
1310 'name' => 'Membership with Related',
1311 'member_of_contact_id' => 1,
1312 'financial_type_id' => 1,
1313 'minimum_fee' => 0.00,
1314 'duration_unit' => 'year',
1315 'duration_interval' => 1,
1316 'period_type' => 'rolling',
1317 'relationship_type_id' => $this->_relTypeID
,
1318 'relationship_direction' => 'b_a',
1319 'visibility' => 'Public',
1322 'domain_id' => CRM_Core_Config
::domainID(),
1324 $originalMembership = $this->callAPISuccess('Membership', 'create', array(
1325 'membership_type_id' => $relatedMembershipType['id'],
1326 'contact_id' => $this->_cId_b
,
1328 $this->callAPISuccess('Relationship', 'create', array(
1329 'relationship_type_id' => $this->_relTypeID
,
1330 'contact_id_a' => $this->_cId_a
,
1331 'contact_id_b' => $this->_cId_b
,
1333 $contactAMembership = $this->callAPISuccessGetSingle('membership', array('contact_id' => $this->_cId_a
));
1334 $this->assertEquals($originalMembership['id'], $contactAMembership['owner_membership_id']);
1336 // Adding a relationship with a future start date should NOT create a membership
1337 $this->callAPISuccess('Relationship', 'create', array(
1338 'relationship_type_id' => $this->_relTypeID
,
1339 'contact_id_a' => $this->_cId_a_2
,
1340 'contact_id_b' => $this->_cId_b
,
1341 'start_date' => 'now + 1 week',
1343 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_cId_a_2
), 0);
1345 // Deleting the organization should cause the related membership to be deleted.
1346 $this->callAPISuccess('contact', 'delete', array('id' => $this->_cId_b
));
1347 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_cId_a
), 0);