3 * +--------------------------------------------------------------------+
4 * | CiviCRM version 4.7 |
5 * +--------------------------------------------------------------------+
6 * | Copyright CiviCRM LLC (c) 2004-2015 |
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 * +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
31 * Class contains api test cases for "civicrm_relationship"
33 class api_v3_RelationshipTest
extends CiviUnitTestCase
{
34 protected $_apiversion = 3;
43 * Second organization contact.
48 protected $_relTypeID;
49 protected $_ids = array();
50 protected $_customGroupId = NULL;
51 protected $_customFieldId = NULL;
59 public function setUp() {
61 $this->_cId_a
= $this->individualCreate();
62 $this->_cId_a_2
= $this->individualCreate(array(
65 'contact_type' => 'Individual',
67 $this->_cId_b
= $this->organizationCreate();
68 $this->_cId_b2
= $this->organizationCreate(array('organization_name' => ' Org 2'));
69 $this->_entity
= 'relationship';
70 //Create a relationship type.
71 $relTypeParams = array(
72 'name_a_b' => 'Relation 1 for delete',
73 'name_b_a' => 'Relation 2 for delete',
74 'description' => 'Testing relationship type',
75 'contact_type_a' => 'Individual',
76 'contact_type_b' => 'Organization',
81 $this->_relTypeID
= $this->relationshipTypeCreate($relTypeParams);
82 $this->_params
= array(
83 'contact_id_a' => $this->_cId_a
,
84 'contact_id_b' => $this->_cId_b
,
85 'relationship_type_id' => $this->_relTypeID
,
86 'start_date' => '2008-12-20',
97 public function tearDown() {
98 $this->contactDelete($this->_cId_a
);
99 $this->contactDelete($this->_cId_a_2
);
100 $this->contactDelete($this->_cId_b
);
101 $this->contactDelete($this->_cId_b2
);
102 $this->quickCleanup(array('civicrm_relationship'), TRUE);
103 $this->relationshipTypeDelete($this->_relTypeID
);
107 * Check with empty array.
109 public function testRelationshipCreateEmpty() {
110 $this->callAPIFailure('relationship', 'create', array());
114 * Check if required fields are not passed.
116 public function testRelationshipCreateWithoutRequired() {
118 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
119 'end_date' => array('d' => '10', 'M' => '1', 'Y' => '2009'),
123 $this->callAPIFailure('relationship', 'create', $params);
127 * Check with incorrect required fields.
129 public function testRelationshipCreateWithIncorrectData() {
132 'contact_id_a' => $this->_cId_a
,
133 'contact_id_b' => $this->_cId_b
,
134 'relationship_type_id' => 'Breaking Relationship',
137 $this->callAPIFailure('relationship', 'create', $params);
139 //contact id is not an integer
141 'contact_id_a' => 'invalid',
142 'contact_id_b' => $this->_cId_b
,
143 'relationship_type_id' => $this->_relTypeID
,
144 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
147 $this->callAPIFailure('relationship', 'create', $params);
149 // Contact id does not exist.
150 $params['contact_id_a'] = 999;
151 $this->callAPIFailure('relationship', 'create', $params);
154 $params['contact_id_a'] = $this->_cId_a
;
155 $params['start_date'] = array('d' => '1', 'M' => '1');
156 $this->callAPIFailure('relationship', 'create', $params);
160 * Check relationship creation with invalid Relationship.
162 public function testRelationshipCreateInvalidRelationship() {
163 // Both have the contact type Individual.
165 'contact_id_a' => $this->_cId_a
,
166 'contact_id_b' => $this->_cId_a
,
167 'relationship_type_id' => $this->_relTypeID
,
168 'start_date' => '2008-01-10',
172 $this->callAPIFailure('relationship', 'create', $params);
174 // both the contact of type Organization
176 'contact_id_a' => $this->_cId_b
,
177 'contact_id_b' => $this->_cId_b
,
178 'relationship_type_id' => $this->_relTypeID
,
179 'start_date' => '2008-01-10',
183 $this->callAPIFailure('relationship', 'create', $params);
187 * Check relationship already exists.
189 public function testRelationshipCreateAlreadyExists() {
191 'contact_id_a' => $this->_cId_a
,
192 'contact_id_b' => $this->_cId_b
,
193 'relationship_type_id' => $this->_relTypeID
,
194 'start_date' => '2008-12-20',
198 $relationship = $this->callAPISuccess('relationship', 'create', $params);
201 'contact_id_a' => $this->_cId_a
,
202 'contact_id_b' => $this->_cId_b
,
203 'relationship_type_id' => $this->_relTypeID
,
204 'start_date' => '2008-12-20',
207 $this->callAPIFailure('relationship', 'create', $params, 'Relationship already exists');
209 $params['id'] = $relationship['id'];
210 $this->callAPISuccess('relationship', 'delete', $params);
214 * Check relationship already exists.
216 public function testRelationshipCreateUpdateAlreadyExists() {
218 'contact_id_a' => $this->_cId_a
,
219 'contact_id_b' => $this->_cId_b
,
220 'relationship_type_id' => $this->_relTypeID
,
221 'start_date' => '2008-12-20',
226 $relationship = $this->callAPISuccess('relationship', 'create', $params);
228 'id' => $relationship['id'],
232 $this->callAPISuccess('relationship', 'create', $params);
233 $this->callAPISuccess('relationship', 'get', $params);
234 $params['id'] = $relationship['id'];
235 $this->callAPISuccess('relationship', 'delete', $params);
239 * Check update doesn't reset stuff badly - CRM-11789.
241 public function testRelationshipCreateUpdateDoesNotMangle() {
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',
248 'is_permission_a_b' => 1,
249 'description' => 'my desc',
251 $relationship = $this->callAPISuccess('relationship', 'create', $params);
253 $updateParams = array(
254 'id' => $relationship['id'],
255 'relationship_type_id' => $this->_relTypeID
,
257 $this->callAPISuccess('relationship', 'create', $updateParams);
259 //make sure the orig params didn't get changed
260 $this->getAndCheck($params, $relationship['id'], 'relationship');
266 * Check relationship creation.
268 public function testRelationshipCreate() {
270 'contact_id_a' => $this->_cId_a
,
271 'contact_id_b' => $this->_cId_b
,
272 'relationship_type_id' => $this->_relTypeID
,
273 'start_date' => '2010-10-30',
274 'end_date' => '2010-12-30',
279 $result = $this->callAPIAndDocument('relationship', 'create', $params, __FUNCTION__
, __FILE__
);
280 $this->assertNotNull($result['id']);
281 $relationParams = array(
282 'id' => $result['id'],
285 // assertDBState compares expected values in $result to actual values in the DB
286 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
287 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
288 $values = $result['values'][$result['id']];
289 foreach ($params as $key => $value) {
290 if ($key == 'note') {
293 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE));
295 $params['id'] = $result['id'];
296 $this->callAPISuccess('relationship', 'delete', $params);
300 * Ensure disabling works.
302 public function testRelationshipUpdate() {
303 $result = $this->callAPISuccess('relationship', 'create', $this->_params
);
304 $relID = $result['id'];
305 $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'description' => 'blah'));
306 $this->assertEquals($relID, $result['id']);
308 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
310 $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'is_permission_b_a' => 1));
311 $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
312 $result = $this->callAPISuccess('relationship', 'create', array('id' => $result['id'], 'is_active' => 0));
313 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
314 $this->assertEquals(0, $result['values'][$result['id']]['is_active']);
315 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
316 $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
320 * Check relationship creation.
322 public function testRelationshipCreateEmptyEndDate() {
324 'contact_id_a' => $this->_cId_a
,
325 'contact_id_b' => $this->_cId_b
,
326 'relationship_type_id' => $this->_relTypeID
,
327 'start_date' => '2010-10-30',
333 $result = $this->callAPISuccess('relationship', 'create', $params);
334 $this->assertNotNull($result['id']);
335 $relationParams = array(
336 'id' => $result['id'],
339 // assertDBState compares expected values in $result to actual values in the DB
340 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
341 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
342 $values = $result['values'][$result['id']];
343 foreach ($params as $key => $value) {
344 if ($key == 'note') {
347 if ($key == 'end_date') {
348 $this->assertTrue(empty($values[$key]));
351 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__
);
353 $params['id'] = $result['id'];
354 $this->callAPISuccess('relationship', 'delete', $params);
358 * Check relationship creation with custom data.
360 public function testRelationshipCreateWithCustomData() {
361 $this->createCustomGroup();
362 $this->_ids
= $this->createCustomField();
363 //few custom Values for comparing
364 $custom_params = array(
365 "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship',
366 "custom_{$this->_ids[1]}" => 'Y',
367 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
368 "custom_{$this->_ids[3]}" => 'http://example.com',
372 'contact_id_a' => $this->_cId_a
,
373 'contact_id_b' => $this->_cId_b
,
374 'relationship_type_id' => $this->_relTypeID
,
375 'start_date' => '2008-12-20',
378 $params = array_merge($params, $custom_params);
379 $result = $this->callAPISuccess('relationship', 'create', $params);
381 $relationParams = array(
382 'id' => $result['id'],
384 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
386 $params['id'] = $result['id'];
387 $this->callAPISuccess('relationship', 'delete', $params);
388 $this->relationshipTypeDelete($this->_relTypeID
);
392 * Check with complete array + custom field
393 * Note that the test is written on purpose without any
394 * variables specific to participant so it can be replicated into other entities
395 * and / or moved to the automated test suite
397 public function testGetWithCustom() {
398 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
400 $params = $this->_params
;
401 $params['custom_' . $ids['custom_field_id']] = "custom string";
403 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
404 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
406 $getParams = array('id' => $result['id']);
407 $check = $this->callAPIAndDocument($this->_entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
408 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
410 $this->customFieldDelete($ids['custom_field_id']);
411 $this->customGroupDelete($ids['custom_group_id']);
417 public function createCustomGroup() {
419 'title' => 'Custom Group',
420 'extends' => array('Relationship'),
426 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
427 $this->_customGroupId
= $customGroup['id'];
428 return $customGroup['id'];
434 public function createCustomField() {
437 'custom_group_id' => $this->_customGroupId
,
438 'label' => 'Enter text about relationship',
439 'html_type' => 'Text',
440 'data_type' => 'String',
441 'default_value' => 'xyz',
444 'is_searchable' => 0,
448 $this->callAPISuccess('CustomField', 'create', $params);
451 $ids[] = $customField['result']['customFieldId'];
453 $optionValue[] = array(
459 $optionValue[] = array(
465 $optionValue[] = array(
473 'label' => 'Pick Color',
474 'html_type' => 'Select',
475 'data_type' => 'String',
478 'is_searchable' => 0,
480 'option_values' => $optionValue,
481 'custom_group_id' => $this->_customGroupId
,
484 $customField = $this->callAPISuccess('custom_field', 'create', $params);
485 $ids[] = $customField['id'];
488 'custom_group_id' => $this->_customGroupId
,
489 'name' => 'test_date',
490 'label' => 'test_date',
491 'html_type' => 'Select Date',
492 'data_type' => 'Date',
493 'default_value' => '20090711',
496 'is_searchable' => 0,
500 $customField = $this->callAPISuccess('custom_field', 'create', $params);
502 $ids[] = $customField['id'];
504 'custom_group_id' => $this->_customGroupId
,
505 'name' => 'test_link',
506 'label' => 'test_link',
507 'html_type' => 'Link',
508 'data_type' => 'Link',
509 'default_value' => 'http://civicrm.org',
512 'is_searchable' => 0,
516 $customField = $this->callAPISuccess('custom_field', 'create', $params);
517 $ids[] = $customField['id'];
522 * Check with empty array.
524 public function testRelationshipDeleteEmpty() {
525 $this->callAPIFailure('relationship', 'delete', array(), 'Mandatory key(s) missing from params array: id');
529 * Check if required fields are not passed.
531 public function testRelationshipDeleteWithoutRequired() {
533 'start_date' => '2008-12-20',
534 'end_date' => '2009-12-20',
538 $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
542 * Check with incorrect required fields.
544 public function testRelationshipDeleteWithIncorrectData() {
546 'contact_id_a' => $this->_cId_a
,
547 'contact_id_b' => $this->_cId_b
,
548 'relationship_type_id' => 'Breaking Relationship',
551 $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
553 $params['id'] = "Invalid";
554 $this->callAPIFailure('relationship', 'delete', $params, 'Invalid value for relationship ID');
558 * Check relationship creation.
560 public function testRelationshipDelete() {
562 'contact_id_a' => $this->_cId_a
,
563 'contact_id_b' => $this->_cId_b
,
564 'relationship_type_id' => $this->_relTypeID
,
565 'start_date' => '2008-12-20',
569 $result = $this->callAPISuccess('relationship', 'create', $params);
570 $params = array('id' => $result['id']);
571 $this->callAPIAndDocument('relationship', 'delete', $params, __FUNCTION__
, __FILE__
);
572 $this->relationshipTypeDelete($this->_relTypeID
);
575 ///////////////// civicrm_relationship_update methods
578 * Check with empty array.
580 public function testRelationshipUpdateEmpty() {
581 $this->callAPIFailure('relationship', 'create', array(),
582 'Mandatory key(s) missing from params array: contact_id_a, contact_id_b, relationship_type_id');
586 * Check if required fields are not passed.
590 * Check relationship update.
592 public function testRelationshipCreateDuplicate() {
594 'contact_id_a' => $this->_cId_a
,
595 'contact_id_b' => $this->_cId_b
,
596 'relationship_type_id' => $this->_relTypeID
,
597 'start_date' => '20081214',
598 'end_date' => '20091214',
602 $result = $this->callAPISuccess('relationship', 'create', $relParams);
604 $this->assertNotNull($result['id']);
607 'contact_id_a' => $this->_cId_a
,
608 'contact_id_b' => $this->_cId_b
,
609 'relationship_type_id' => $this->_relTypeID
,
610 'start_date' => '20081214',
611 'end_date' => '20091214',
615 $this->callAPIFailure('relationship', 'create', $params, 'Relationship already exists');
617 $this->callAPISuccess('relationship', 'delete', array('id' => $result['id']));
618 $this->relationshipTypeDelete($this->_relTypeID
);
622 * CRM-13725 - Two relationships of same type with same start and end date
623 * should be OK if the custom field values differ.
625 public function testRelationshipCreateDuplicateWithCustomFields() {
626 $this->createCustomGroup();
627 $this->_ids
= $this->createCustomField();
629 $custom_params_1 = array(
630 "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship',
631 "custom_{$this->_ids[1]}" => 'Y',
632 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
633 "custom_{$this->_ids[3]}" => 'http://example.com',
636 $custom_params_2 = array(
637 "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
638 "custom_{$this->_ids[1]}" => 'Y',
639 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
640 "custom_{$this->_ids[3]}" => 'http://example.org',
644 'contact_id_a' => $this->_cId_a
,
645 'contact_id_b' => $this->_cId_b
,
646 'relationship_type_id' => $this->_relTypeID
,
647 'start_date' => '2008-12-20',
651 $params_1 = array_merge($params, $custom_params_1);
652 $params_2 = array_merge($params, $custom_params_2);
654 $result_1 = $this->callAPISuccess('relationship', 'create', $params_1);
655 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
657 $this->assertNotNull($result_2['id']);
658 $this->assertEquals(0, $result_2['is_error']);
660 $this->relationshipTypeDelete($this->_relTypeID
);
664 * CRM-13725 - Two relationships of same type with same start and end date
665 * should be OK if the custom field values differ. In this case, the
666 * existing relationship does not have custom values, but the new one
669 public function testRelationshipCreateDuplicateWithCustomFields2() {
670 $this->createCustomGroup();
671 $this->_ids
= $this->createCustomField();
673 $custom_params_2 = array(
674 "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
675 "custom_{$this->_ids[1]}" => 'Y',
676 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
677 "custom_{$this->_ids[3]}" => 'http://example.org',
681 'contact_id_a' => $this->_cId_a
,
682 'contact_id_b' => $this->_cId_b
,
683 'relationship_type_id' => $this->_relTypeID
,
684 'start_date' => '2008-12-20',
688 $params_2 = array_merge($params_1, $custom_params_2);
690 $this->callAPISuccess('relationship', 'create', $params_1);
691 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
693 $this->assertNotNull($result_2['id']);
694 $this->assertEquals(0, $result_2['is_error']);
696 $this->relationshipTypeDelete($this->_relTypeID
);
700 * CRM-13725 - Two relationships of same type with same start and end date
701 * should be OK if the custom field values differ. In this case, the
702 * existing relationship does have custom values, but the new one
705 public function testRelationshipCreateDuplicateWithCustomFields3() {
706 $this->createCustomGroup();
707 $this->_ids
= $this->createCustomField();
709 $custom_params_1 = array(
710 "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
711 "custom_{$this->_ids[1]}" => 'Y',
712 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
713 "custom_{$this->_ids[3]}" => 'http://example.org',
717 'contact_id_a' => $this->_cId_a
,
718 'contact_id_b' => $this->_cId_b
,
719 'relationship_type_id' => $this->_relTypeID
,
720 'start_date' => '2008-12-20',
724 $params_1 = array_merge($params_2, $custom_params_1);
726 $this->callAPISuccess('relationship', 'create', $params_1);
727 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
729 $this->assertNotNull($result_2['id']);
730 $this->assertEquals(0, $result_2['is_error']);
732 $this->relationshipTypeDelete($this->_relTypeID
);
736 * Check with valid params array.
738 public function testRelationshipsGet() {
740 'contact_id_a' => $this->_cId_a
,
741 'contact_id_b' => $this->_cId_b
,
742 'relationship_type_id' => $this->_relTypeID
,
743 'start_date' => '2011-01-01',
744 'end_date' => '2013-01-01',
748 $this->callAPISuccess('relationship', 'create', $relParams);
752 'contact_id' => $this->_cId_b
,
754 $result = $this->callAPISuccess('relationship', 'get', $params);
755 $this->assertEquals($result['count'], 1);
757 'contact_id_a' => $this->_cId_a
,
759 $result = $this->callAPISuccess('relationship', 'get', $params);
760 $this->assertEquals($result['count'], 1);
761 // contact_id_a is wrong so should be no matches
763 'contact_id_a' => $this->_cId_b
,
765 $result = $this->callAPISuccess('relationship', 'get', $params);
766 $this->assertEquals($result['count'], 0);
770 * Check with valid params array.
771 * (The get function will behave differently without 'contact_id' passed
773 public function testRelationshipsGetGeneric() {
775 'contact_id_a' => $this->_cId_a
,
776 'contact_id_b' => $this->_cId_b
,
777 'relationship_type_id' => $this->_relTypeID
,
778 'start_date' => '2011-01-01',
779 'end_date' => '2013-01-01',
783 $this->callAPISuccess('relationship', 'create', $relParams);
787 'contact_id_b' => $this->_cId_b
,
789 $this->callAPISuccess('relationship', 'get', $params);
793 * Test retrieving only current relationships.
795 public function testGetIsCurrent() {
797 'contact_id_a' => $this->_cId_a
,
798 'contact_id_b' => $this->_cId_b2
,
799 'relationship_type_id' => $this->_relTypeID
,
800 'start_date' => '2008-12-20',
803 $this->callAPISuccess('relationship', 'create', $rel2Params);
804 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params
);
807 'filters' => array('is_current' => 1),
809 $description = "Demonstrates is_current filter.";
810 $subfile = 'filterIsCurrent';
811 //no relationship has been created
812 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
813 $this->assertEquals($result['count'], 1);
814 $this->AssertEquals($rel1['id'], $result['id']);
816 // now try not started
817 $rel2Params['is_active'] = 1;
818 $rel2Params['start_date'] = 'tomorrow';
819 $this->callAPISuccess('relationship', 'create', $rel2Params);
820 $result = $this->callAPISuccess('relationship', 'get', $getParams);
821 $this->assertEquals($result['count'], 1);
822 $this->AssertEquals($rel1['id'], $result['id']);
825 $rel2Params['is_active'] = 1;
826 $rel2Params['start_date'] = 'last week';
827 $rel2Params['end_date'] = 'yesterday';
828 $this->callAPISuccess('relationship', 'create', $rel2Params);
832 * Test using various operators.
834 public function testGetTypeOperators() {
835 $relTypeParams = array(
836 'name_a_b' => 'Relation 3 for delete',
837 'name_b_a' => 'Relation 6 for delete',
838 'description' => 'Testing relationship type 2',
839 'contact_type_a' => 'Individual',
840 'contact_type_b' => 'Organization',
844 $relationType2 = $this->relationshipTypeCreate($relTypeParams);
845 $relTypeParams = array(
846 'name_a_b' => 'Relation 8 for delete',
847 'name_b_a' => 'Relation 9 for delete',
848 'description' => 'Testing relationship type 7',
849 'contact_type_a' => 'Individual',
850 'contact_type_b' => 'Organization',
854 $relationType3 = $this->relationshipTypeCreate($relTypeParams);
856 $relTypeParams = array(
857 'name_a_b' => 'Relation 6 for delete',
858 'name_b_a' => 'Relation 88for delete',
859 'description' => 'Testing relationship type 00',
860 'contact_type_a' => 'Individual',
861 'contact_type_b' => 'Organization',
865 $relationType4 = $this->relationshipTypeCreate($relTypeParams);
867 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params
);
868 $rel2 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
869 array('relationship_type_id' => $relationType2)));
870 $rel3 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
871 array('relationship_type_id' => $relationType3)));
872 $rel4 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
873 array('relationship_type_id' => $relationType4)));
876 'relationship_type_id' => array('IN' => array($relationType2, $relationType3)),
879 $description = "Demonstrates use of IN filter.";
880 $subfile = 'INRelationshipType';
882 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
883 $this->assertEquals($result['count'], 2);
884 $this->AssertEquals(array($rel2['id'], $rel3['id']), array_keys($result['values']));
886 $description = "Demonstrates use of NOT IN filter.";
887 $subfile = 'NotInRelationshipType';
889 'relationship_type_id' => array('NOT IN' => array($relationType2, $relationType3)),
891 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
892 $this->assertEquals($result['count'], 2);
893 $this->AssertEquals(array($rel1['id'], $rel4['id']), array_keys($result['values']));
895 $description = "Demonstrates use of BETWEEN filter.";
896 $subfile = 'BetweenRelationshipType';
898 'relationship_type_id' => array('BETWEEN' => array($relationType2, $relationType4)),
900 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
901 $this->assertEquals($result['count'], 3);
902 $this->AssertEquals(array($rel2['id'], $rel3['id'], $rel4['id']), array_keys($result['values']));
904 $description = "Demonstrates use of Not BETWEEN filter.";
905 $subfile = 'NotBetweenRelationshipType';
907 'relationship_type_id' => array('NOT BETWEEN' => array($relationType2, $relationType4)),
909 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
910 $this->assertEquals($result['count'], 1);
911 $this->AssertEquals(array($rel1['id']), array_keys($result['values']));
916 * Check with invalid relationshipType Id.
918 public function testRelationshipTypeAddInvalidId() {
919 $relTypeParams = array(
921 'name_a_b' => 'Relation 1 for delete',
922 'name_b_a' => 'Relation 2 for delete',
923 'contact_type_a' => 'Individual',
924 'contact_type_b' => 'Organization',
926 $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
927 'id is not a valid integer');
931 * Check with valid data with contact_b.
933 public function testGetRelationshipWithContactB() {
935 'contact_id_a' => $this->_cId_a
,
936 'contact_id_b' => $this->_cId_b
,
937 'relationship_type_id' => $this->_relTypeID
,
938 'start_date' => '2011-01-01',
939 'end_date' => '2013-01-01',
943 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
946 'contact_id' => $this->_cId_a
,
949 $result = $this->callAPISuccess('relationship', 'get', $contacts);
950 $this->assertGreaterThan(0, $result['count']);
952 'id' => $relationship['id'],
954 $this->callAPISuccess('relationship', 'delete', $params);
955 $this->relationshipTypeDelete($this->_relTypeID
);
959 * Check with valid data with relationshipTypes.
961 public function testGetRelationshipWithRelTypes() {
963 'contact_id_a' => $this->_cId_a
,
964 'contact_id_b' => $this->_cId_b
,
965 'relationship_type_id' => $this->_relTypeID
,
966 'start_date' => '2011-01-01',
967 'end_date' => '2013-01-01',
971 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
974 'contact_id' => $this->_cId_a
,
976 $this->callAPISuccess('relationship', 'get', $contact_a);
979 'id' => $relationship['id'],
981 $this->callAPISuccess('relationship', 'delete', $params);
982 $this->relationshipTypeDelete($this->_relTypeID
);
986 * Checks that passing in 'contact_id' + a relationship type
987 * will filter by relationship type (relationships go in both directions)
988 * as relationship api does a reciprocal check if contact_id provided
990 * We should get 1 result without or with correct relationship type id & 0 with
993 public function testGetRelationshipByTypeReciprocal() {
994 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
995 $result = $this->callAPISuccess($this->_entity
, 'get', array(
996 'contact_id' => $this->_cId_a
,
997 'relationship_type_id' => $this->_relTypeID
,
999 $this->assertEquals(1, $result['count']);
1000 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1001 'contact_id' => $this->_cId_a
,
1002 'relationship_type_id' => $this->_relTypeID +
1,
1004 $this->assertEquals(0, $result['count']);
1005 $this->callAPISuccess($this->_entity
, 'delete', array('id' => $created['id']));
1009 * Checks that passing in 'contact_id_b' + a relationship type
1010 * will filter by relationship type for contact b
1012 * We should get 1 result without or with correct relationship type id & 0 with
1015 public function testGetRelationshipByTypeDAO() {
1016 $this->_ids
['relationship'] = $this->callAPISuccess($this->_entity
, 'create', array('format.only_id' => TRUE) +
1018 $this->callAPISuccess($this->_entity
, 'getcount', array(
1019 'contact_id_a' => $this->_cId_a
,
1022 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1023 'contact_id_a' => $this->_cId_a
,
1024 'relationship_type_id' => $this->_relTypeID
,
1026 $this->assertEquals(1, $result['count']);
1027 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1028 'contact_id_a' => $this->_cId_a
,
1029 'relationship_type_id' => $this->_relTypeID +
1,
1031 $this->assertEquals(0, $result['count']);
1035 * Checks that passing in 'contact_id_b' + a relationship type
1036 * will filter by relationship type for contact b
1038 * We should get 1 result without or with correct relationship type id & 0 with
1041 public function testGetRelationshipByTypeArrayDAO() {
1042 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1043 $org3 = $this->organizationCreate();
1044 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1045 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1048 $this->callAPISuccess($this->_entity
, 'create',
1049 array_merge($this->_params
, array(
1050 'relationship_type_id' => $relType2,
1051 'contact_id_b' => $this->_cId_b2
,
1056 $this->callAPISuccess($this->_entity
, 'create',
1057 array_merge($this->_params
, array(
1058 'relationship_type_id' => $relType3,
1059 'contact_id_b' => $org3,
1063 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1064 'contact_id_a' => $this->_cId_a
,
1065 'relationship_type_id' => array('IN' => array($this->_relTypeID
, $relType3)),
1068 $this->assertEquals(2, $result['count']);
1069 foreach ($result['values'] as $key => $value) {
1070 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID
, $relType3)));
1075 * Checks that passing in 'contact_id_b' + a relationship type
1076 * will filter by relationship type for contact b
1078 * We should get 1 result without or with correct relationship type id & 0 with
1081 public function testGetRelationshipByTypeArrayReciprocal() {
1082 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1083 $org3 = $this->organizationCreate();
1084 // lets just assume built in ones aren't being messed with!
1089 $this->callAPISuccess($this->_entity
, 'create',
1090 array_merge($this->_params
, array(
1091 'relationship_type_id' => $relType2,
1092 'contact_id_b' => $this->_cId_b2
,
1097 $this->callAPISuccess($this->_entity
, 'create',
1098 array_merge($this->_params
, array(
1099 'relationship_type_id' => $relType3,
1100 'contact_id_b' => $org3,
1104 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1105 'contact_id' => $this->_cId_a
,
1106 'relationship_type_id' => array('IN' => array($this->_relTypeID
, $relType3)),
1109 $this->assertEquals(2, $result['count']);
1110 foreach ($result['values'] as $key => $value) {
1111 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID
, $relType3)));
1116 * Test relationship get by membership type.
1118 * Checks that passing in 'contact_id_b' + a relationship type
1119 * will filter by relationship type for contact b
1121 * We should get 1 result without or with correct relationship type id & 0 with
1124 public function testGetRelationshipByMembershipTypeDAO() {
1125 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1126 $org3 = $this->organizationCreate();
1128 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1129 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1131 $memberType = $this->membershipTypeCreate(array(
1132 'relationship_type_id' => CRM_Core_DAO
::VALUE_SEPARATOR
. $relType1 . CRM_Core_DAO
::VALUE_SEPARATOR
. $relType3 . CRM_Core_DAO
::VALUE_SEPARATOR
,
1133 'relationship_direction' => CRM_Core_DAO
::VALUE_SEPARATOR
. 'a_b' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'b_a' . CRM_Core_DAO
::VALUE_SEPARATOR
,
1137 $this->callAPISuccess($this->_entity
, 'create',
1138 array_merge($this->_params
, array(
1139 'relationship_type_id' => $relType2,
1140 'contact_id_b' => $this->_cId_b2
,
1145 $this->callAPISuccess($this->_entity
, 'create',
1146 array_merge($this->_params
, array(
1147 'relationship_type_id' => $relType3,
1148 'contact_id_b' => $org3,
1152 // Relationship 4 with reversal.
1153 $this->callAPISuccess($this->_entity
, 'create',
1154 array_merge($this->_params
, array(
1155 'relationship_type_id' => $relType1,
1156 'contact_id_a' => $this->_cId_a
,
1157 'contact_id_b' => $this->_cId_a_2
,
1161 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1162 'contact_id_a' => $this->_cId_a
,
1163 'membership_type_id' => $memberType,
1165 // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1166 $this->assertEquals(1, $result['count']);
1167 foreach ($result['values'] as $key => $value) {
1168 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1)));
1173 * Checks that passing in 'contact_id_b' + a relationship type
1174 * will filter by relationship type for contact b
1176 * We should get 1 result without or with correct relationship type id & 0 with
1179 public function testGetRelationshipByMembershipTypeReciprocal() {
1180 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1181 $org3 = $this->organizationCreate();
1183 // Let's just assume built in ones aren't being messed with!
1187 $memberType = $this->membershipTypeCreate(array(
1188 'relationship_type_id' => CRM_Core_DAO
::VALUE_SEPARATOR
. $relType1 . CRM_Core_DAO
::VALUE_SEPARATOR
. $relType3 . CRM_Core_DAO
::VALUE_SEPARATOR
,
1189 'relationship_direction' => CRM_Core_DAO
::VALUE_SEPARATOR
. 'a_b' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'b_a' . CRM_Core_DAO
::VALUE_SEPARATOR
,
1193 $this->callAPISuccess($this->_entity
, 'create',
1194 array_merge($this->_params
, array(
1195 'relationship_type_id' => $relType2,
1196 'contact_id_b' => $this->_cId_b2
,
1201 $this->callAPISuccess($this->_entity
, 'create',
1202 array_merge($this->_params
, array(
1203 'relationship_type_id' => $relType3,
1204 'contact_id_b' => $org3,
1208 // Relationship 4 with reversal.
1209 $this->callAPISuccess($this->_entity
, 'create',
1210 array_merge($this->_params
, array(
1211 'relationship_type_id' => $relType1,
1212 'contact_id_a' => $this->_cId_a
,
1213 'contact_id_b' => $this->_cId_a_2
,
1217 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1218 'contact_id' => $this->_cId_a
,
1219 'membership_type_id' => $memberType,
1221 // Although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1222 $this->assertEquals(2, $result['count']);
1224 foreach ($result['values'] as $key => $value) {
1225 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1, $relType3)));
1230 * Check for e-notices on enable & disable as reported in CRM-14350
1232 public function testSetActive() {
1233 $relationship = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1234 $this->callAPISuccess($this->_entity
, 'create', array('id' => $relationship['id'], 'is_active' => 0));
1235 $this->callAPISuccess($this->_entity
, 'create', array('id' => $relationship['id'], 'is_active' => 1));
1239 * Test creating related memberships.
1241 public function testCreateRelatedMembership() {
1242 $relatedMembershipType = $this->callAPISuccess('MembershipType', 'create', array(
1243 'name' => 'Membership with Related',
1244 'member_of_contact_id' => 1,
1245 'financial_type_id' => 1,
1246 'minimum_fee' => 0.00,
1247 'duration_unit' => 'year',
1248 'duration_interval' => 1,
1249 'period_type' => 'rolling',
1250 'relationship_type_id' => $this->_relTypeID
,
1251 'relationship_direction' => 'b_a',
1252 'visibility' => 'Public',
1255 'domain_id' => CRM_Core_Config
::domainID(),
1257 $originalMembership = $this->callAPISuccess('Membership', 'create', array(
1258 'membership_type_id' => $relatedMembershipType['id'],
1259 'contact_id' => $this->_cId_b
,
1261 $this->callAPISuccess('Relationship', 'create', array(
1262 'relationship_type_id' => $this->_relTypeID
,
1263 'contact_id_a' => $this->_cId_a
,
1264 'contact_id_b' => $this->_cId_b
,
1266 $contactAMembership = $this->callAPISuccessGetSingle('membership', array('contact_id' => $this->_cId_a
));
1267 $this->assertEquals($originalMembership['id'], $contactAMembership['owner_membership_id']);
1269 // Adding a relationship with a future start date should NOT create a membership
1270 $this->callAPISuccess('Relationship', 'create', array(
1271 'relationship_type_id' => $this->_relTypeID
,
1272 'contact_id_a' => $this->_cId_a_2
,
1273 'contact_id_b' => $this->_cId_b
,
1274 'start_date' => 'now + 1 week',
1276 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_cId_a_2
), 0);
1278 // Deleting the organization should cause the related membership to be deleted.
1279 $this->callAPISuccess('contact', 'delete', array('id' => $this->_cId_b
));
1280 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_cId_a
), 0);