3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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"
34 class api_v3_RelationshipTest
extends CiviUnitTestCase
{
35 protected $_apiversion = 3;
43 protected $_cId_b2;// second org
44 protected $_relTypeID;
45 protected $_ids = array();
46 protected $_customGroupId = NULL;
47 protected $_customFieldId = NULL;
53 'name' => 'Relationship Create',
54 'description' => 'Test all Relationship Create API methods.',
55 'group' => 'CiviCRM API Tests',
61 $this->_cId_a
= $this->individualCreate();
62 $this->_cId_a_2
= $this->individualCreate(array('last_name' => 'c2', 'email' => 'c@w.com', 'contact_type' => 'Individual'));
63 $this->_cId_b
= $this->organizationCreate();
64 $this->_cId_b2
= $this->organizationCreate(array('organization_name' => ' Org 2'));
65 $this->_entity
= 'relationship';
66 //Create a relationship type
67 $relTypeParams = array(
68 'name_a_b' => 'Relation 1 for delete',
69 'name_b_a' => 'Relation 2 for delete',
70 'description' => 'Testing relationship type',
71 'contact_type_a' => 'Individual',
72 'contact_type_b' => 'Organization',
77 $this->_relTypeID
= $this->relationshipTypeCreate($relTypeParams);
78 $this->_params
= array(
79 'contact_id_a' => $this->_cId_a
,
80 'contact_id_b' => $this->_cId_b
,
81 'relationship_type_id' => $this->_relTypeID
,
82 'start_date' => '2008-12-20',
89 $this->contactDelete($this->_cId_a
);
90 $this->contactDelete($this->_cId_a_2
);
91 $this->contactDelete($this->_cId_b
);
92 $this->contactDelete($this->_cId_b2
);
93 $this->quickCleanup(array('civicrm_relationship'), TRUE);
94 $this->relationshipTypeDelete($this->_relTypeID
);
97 ///////////////// civicrm_relationship_create methods
100 * check with empty array
102 function testRelationshipCreateEmpty() {
103 $this->callAPIFailure('relationship', 'create', array());
107 * check if required fields are not passed
109 function testRelationshipCreateWithoutRequired() {
111 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
112 'end_date' => array('d' => '10', 'M' => '1', 'Y' => '2009'),
116 $this->callAPIFailure('relationship', 'create', $params);
120 * check with incorrect required fields
122 function testRelationshipCreateWithIncorrectData() {
125 'contact_id_a' => $this->_cId_a
,
126 'contact_id_b' => $this->_cId_b
,
127 'relationship_type_id' => 'Breaking Relationship',
130 $this->callAPIFailure('relationship', 'create', $params);
132 //contact id is not an integer
134 'contact_id_a' => 'invalid',
135 'contact_id_b' => $this->_cId_b
,
136 'relationship_type_id' => $this->_relTypeID
,
137 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
140 $this->callAPIFailure('relationship', 'create', $params);
142 //contact id does not exists
143 $params['contact_id_a'] = 999;
144 $this->callAPIFailure('relationship', 'create', $params);
147 $params['contact_id_a'] = $this->_cId_a
;
148 $params['start_date'] = array('d' => '1', 'M' => '1');
149 $this->callAPIFailure('relationship', 'create', $params);
153 * check relationship creation with invalid Relationship
155 function testRelationshipCreatInvalidRelationship() {
156 // both the contact of type Individual
158 'contact_id_a' => $this->_cId_a
,
159 'contact_id_b' => $this->_cId_a
,
160 'relationship_type_id' => $this->_relTypeID
,
161 'start_date' => '2008-01-10',
165 $this->callAPIFailure('relationship', 'create', $params);
167 // both the contact of type Organization
169 'contact_id_a' => $this->_cId_b
,
170 'contact_id_b' => $this->_cId_b
,
171 'relationship_type_id' => $this->_relTypeID
,
172 'start_date' => '2008-01-10',
176 $this->callAPIFailure('relationship', 'create', $params);
180 * check relationship already exists
182 function testRelationshipCreateAlreadyExists() {
184 'contact_id_a' => $this->_cId_a
,
185 'contact_id_b' => $this->_cId_b
,
186 'relationship_type_id' => $this->_relTypeID
,
187 'start_date' => '2008-12-20', 'end_date' => NULL,
190 $relationship = $this->callAPISuccess('relationship', 'create', $params);
193 'contact_id_a' => $this->_cId_a
,
194 'contact_id_b' => $this->_cId_b
,
195 'relationship_type_id' => $this->_relTypeID
,
196 'start_date' => '2008-12-20',
199 $result = $this->callAPIFailure('relationship', 'create', $params, 'Relationship already exists');
201 $params['id'] = $relationship['id'];
202 $result = $this->callAPISuccess('relationship', 'delete', $params);
206 * check relationship already exists
208 function testRelationshipCreateUpdateAlreadyExists() {
210 'contact_id_a' => $this->_cId_a
,
211 'contact_id_b' => $this->_cId_b
,
212 'relationship_type_id' => $this->_relTypeID
,
213 'start_date' => '2008-12-20',
218 $relationship = $this->callAPISuccess('relationship', 'create', $params);
220 'id' => $relationship['id'],
224 $result = $this->callAPISuccess('relationship', 'create', $params);
225 $result = $this->callAPISuccess('relationship', 'get', $params);
226 $params['id'] = $relationship['id'];
227 $result = $this->callAPISuccess('relationship', 'delete', $params);
231 * checkupdate doesn't reset stuff badly - CRM-11789
233 function testRelationshipCreateUpdateDoesntMangle() {
235 'contact_id_a' => $this->_cId_a
,
236 'contact_id_b' => $this->_cId_b
,
237 'relationship_type_id' => $this->_relTypeID
,
238 'start_date' => '2008-12-20',
240 'is_permission_a_b' => 1,
241 'description' => 'my desc',
243 $relationship = $this->callAPISuccess('relationship', 'create', $params);
245 $updateparams = array(
246 'id' => $relationship['id'],
247 'relationship_type_id' => $this->_relTypeID
,
249 $result = $this->callAPISuccess('relationship', 'create', $updateparams);
251 //make sure the orig params didn't get changed
252 $this->getAndCheck($params, $relationship['id'], 'relationship');
259 * check relationship creation
261 function testRelationshipCreate() {
263 'contact_id_a' => $this->_cId_a
,
264 'contact_id_b' => $this->_cId_b
,
265 'relationship_type_id' => $this->_relTypeID
,
266 'start_date' => '2010-10-30',
267 'end_date' => '2010-12-30',
272 $result = $this->callAPIAndDocument('relationship', 'create', $params, __FUNCTION__
, __FILE__
);
273 $this->assertNotNull($result['id']);
274 $relationParams = array(
275 'id' => $result['id'],
278 // assertDBState compares expected values in $result to actual values in the DB
279 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
280 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
281 $values = $result['values'][$result['id']];
282 foreach ($params as $key => $value) {
283 if ($key == 'note') {
286 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE));
288 $params['id'] = $result['id'];
289 $this->callAPISuccess('relationship', 'delete', $params);
292 * ensure disabling works
294 function testRelationshipUpdate() {
295 $result = $this->callAPISuccess('relationship', 'create', $this->_params
);
296 $relID = $result['id'];
297 $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'description' => 'blah'));
298 $this->assertEquals($relID, $result['id']);
299 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
300 $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'is_permission_b_a' => 1));
301 $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
302 $result = $this->callAPISuccess('relationship', 'create', array('id' => $result['id'], 'is_active' => 0));
303 $this->assertEquals(0, $result['values'][$result['id']]['is_active']);
304 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
305 $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
308 * check relationship creation
310 function testRelationshipCreateEmptyEndDate() {
312 'contact_id_a' => $this->_cId_a
,
313 'contact_id_b' => $this->_cId_b
,
314 'relationship_type_id' => $this->_relTypeID
,
315 'start_date' => '2010-10-30',
321 $result = $this->callAPISuccess('relationship', 'create', $params);
322 $this->assertNotNull($result['id']);
323 $relationParams = array(
324 'id' => $result['id'],
327 // assertDBState compares expected values in $result to actual values in the DB
328 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
329 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
330 $values = $result['values'][$result['id']];
331 foreach ($params as $key => $value) {
332 if ($key == 'note') {
335 if($key == 'end_date'){
336 $this->assertTrue(empty($values[$key]));
339 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__
);
341 $params['id'] = $result['id'];
342 $this->callAPISuccess('relationship', 'delete', $params);
346 * check relationship creation with custom data
348 function testRelationshipCreateWithCustomData() {
349 $customGroup = $this->createCustomGroup();
350 $this->_ids
= $this->createCustomField();
351 //few custom Values for comparing
352 $custom_params = array(
353 "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship',
354 "custom_{$this->_ids[1]}" => 'Y',
355 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
356 "custom_{$this->_ids[3]}" => 'http://example.com',
360 'contact_id_a' => $this->_cId_a
,
361 'contact_id_b' => $this->_cId_b
,
362 'relationship_type_id' => $this->_relTypeID
,
363 'start_date' => '2008-12-20',
366 $params = array_merge($params, $custom_params);
367 $result = $this->callAPISuccess('relationship', 'create', $params);
369 $relationParams = array(
370 'id' => $result['id'],
372 // assertDBState compares expected values in $result to actual values in the DB
373 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
375 $params['id'] = $result['id'];
376 $result = $this->callAPISuccess('relationship', 'delete', $params);
377 $this->relationshipTypeDelete($this->_relTypeID
);
381 * check with complete array + custom field
382 * Note that the test is written on purpose without any
383 * variables specific to participant so it can be replicated into other entities
384 * and / or moved to the automated test suite
386 function testGetWithCustom() {
387 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
389 $params = $this->_params
;
390 $params['custom_' . $ids['custom_field_id']] = "custom string";
392 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
393 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
395 $getParams = array('id' => $result['id']);
396 $check = $this->callAPIAndDocument($this->_entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
397 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
399 $this->customFieldDelete($ids['custom_field_id']);
400 $this->customGroupDelete($ids['custom_group_id']);
403 function createCustomGroup() {
405 'title' => 'Custom Group',
406 'extends' => array('Relationship'),
412 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
413 $this->_customGroupId
= $customGroup['id'];
414 return $customGroup['id'];
417 function createCustomField() {
420 'custom_group_id' => $this->_customGroupId
,
421 'label' => 'Enter text about relationship',
422 'html_type' => 'Text',
423 'data_type' => 'String',
424 'default_value' => 'xyz',
427 'is_searchable' => 0,
432 $result = $this->callAPISuccess('CustomField', 'create', $params);
435 $ids[] = $customField['result']['customFieldId'];
437 $optionValue[] = array(
443 $optionValue[] = array(
449 $optionValue[] = array(
457 'label' => 'Pick Color',
458 'html_type' => 'Select',
459 'data_type' => 'String',
462 'is_searchable' => 0,
464 'option_values' => $optionValue,
465 'custom_group_id' => $this->_customGroupId
,
468 $customField = $this->callAPISuccess('custom_field', 'create', $params);
469 $ids[] = $customField['id'];
472 'custom_group_id' => $this->_customGroupId
,
473 'name' => 'test_date',
474 'label' => 'test_date',
475 'html_type' => 'Select Date',
476 'data_type' => 'Date',
477 'default_value' => '20090711',
480 'is_searchable' => 0,
484 $customField = $this->callAPISuccess('custom_field', 'create', $params);
486 $ids[] = $customField['id'];
488 'custom_group_id' => $this->_customGroupId
,
489 'name' => 'test_link',
490 'label' => 'test_link',
491 'html_type' => 'Link',
492 'data_type' => 'Link',
493 'default_value' => 'http://civicrm.org',
496 'is_searchable' => 0,
500 $customField = $this->callAPISuccess('custom_field', 'create', $params);
501 $ids[] = $customField['id'];
505 ///////////////// civicrm_relationship_delete methods
508 * check with empty array
510 function testRelationshipDeleteEmpty() {
512 $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
516 * check if required fields are not passed
518 function testRelationshipDeleteWithoutRequired() {
520 'start_date' => '2008-12-20',
521 'end_date' => '2009-12-20',
525 $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
529 * check with incorrect required fields
531 function testRelationshipDeleteWithIncorrectData() {
533 'contact_id_a' => $this->_cId_a
,
534 'contact_id_b' => $this->_cId_b
,
535 'relationship_type_id' => 'Breaking Relationship',
538 $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
540 $params['id'] = "Invalid";
541 $result = $this->callAPIFailure('relationship', 'delete', $params, 'Invalid value for relationship ID');
545 * check relationship creation
547 function testRelationshipDelete() {
549 'contact_id_a' => $this->_cId_a
,
550 'contact_id_b' => $this->_cId_b
,
551 'relationship_type_id' => $this->_relTypeID
,
552 'start_date' => '2008-12-20',
556 $result = $this->callAPISuccess('relationship', 'create', $params);
558 //Delete relationship
560 $params['id'] = $result['id'];
562 $result = $this->callAPIAndDocument('relationship', 'delete', $params, __FUNCTION__
, __FILE__
);
563 $this->relationshipTypeDelete($this->_relTypeID
);
566 ///////////////// civicrm_relationship_update methods
569 * check with empty array
571 function testRelationshipUpdateEmpty() {
572 $result = $this->callAPIFailure('relationship', 'create', array(),
573 'Mandatory key(s) missing from params array: contact_id_a, contact_id_b, relationship_type_id');
577 * check if required fields are not passed
581 * check relationship update
583 function testRelationshipCreateDuplicate() {
585 'contact_id_a' => $this->_cId_a
,
586 'contact_id_b' => $this->_cId_b
,
587 'relationship_type_id' => $this->_relTypeID
,
588 'start_date' => '20081214',
589 'end_date' => '20091214',
593 $result = $this->callAPISuccess('relationship', 'create', $relParams);
595 $this->assertNotNull($result['id']);
596 $this->_relationID
= $result['id'];
599 'contact_id_a' => $this->_cId_a
,
600 'contact_id_b' => $this->_cId_b
,
601 'relationship_type_id' => $this->_relTypeID
,
602 'start_date' => '20081214',
603 'end_date' => '20091214',
607 $result = $this->callAPIFailure('relationship', 'create', $params, 'Relationship already exists');
609 //delete created relationship
611 'id' => $this->_relationID
,
614 $result = $this->callAPISuccess('relationship', 'delete', $params);
616 //delete created relationship type
617 $this->relationshipTypeDelete($this->_relTypeID
);
621 * check with valid params array.
623 function testRelationshipsGet() {
625 'contact_id_a' => $this->_cId_a
,
626 'contact_id_b' => $this->_cId_b
,
627 'relationship_type_id' => $this->_relTypeID
,
628 'start_date' => '2011-01-01',
629 'end_date' => '2013-01-01',
633 $result = $this->callAPISuccess('relationship', 'create', $relParams);
637 'contact_id' => $this->_cId_b
,
639 $result = $this->callAPISuccess('relationship', 'get', $params);
640 $this->assertEquals($result['count'], 1);
642 'contact_id_a' => $this->_cId_a
,
644 $result = $this->callAPISuccess('relationship', 'get', $params);
645 $this->assertEquals($result['count'], 1);
646 // contact_id_a is wrong so should be no matches
648 'contact_id_a' => $this->_cId_b
,
650 $result = $this->callAPISuccess('relationship', 'get', $params);
651 $this->assertEquals($result['count'], 0);
655 * check with valid params array.
656 * (The get function will behave differently without 'contact_id' passed
658 function testRelationshipsGetGeneric() {
660 'contact_id_a' => $this->_cId_a
,
661 'contact_id_b' => $this->_cId_b
,
662 'relationship_type_id' => $this->_relTypeID
,
663 'start_date' => '2011-01-01',
664 'end_date' => '2013-01-01',
668 $result = $this->callAPISuccess('relationship', 'create', $relParams);
672 'contact_id_b' => $this->_cId_b
,
674 $result = $this->callAPISuccess('relationship', 'get', $params);
677 function testGetIsCurrent() {
679 'contact_id_a' => $this->_cId_a
,
680 'contact_id_b' => $this->_cId_b2
,
681 'relationship_type_id' => $this->_relTypeID
,
682 'start_date' => '2008-12-20',
685 $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params);
686 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params
);
689 'filters' => array('is_current' => 1)
691 $description = "demonstrates is_current filter";
692 $subfile = 'filterIsCurrent';
693 //no relationship has been created
694 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
695 $this->assertEquals($result['count'], 1);
696 $this->AssertEquals($rel1['id'], $result['id']);
698 // now try not started
699 $rel2Params['is_active'] =1;
700 $rel2Params['start_date'] ='tomorrow';
701 $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params);
702 $result = $this->callAPISuccess('relationship', 'get', $getParams);
703 $this->assertEquals($result['count'], 1);
704 $this->AssertEquals($rel1['id'], $result['id']);
707 $rel2Params['is_active'] =1;
708 $rel2Params['start_date'] ='last week';
709 $rel2Params['end_date'] ='yesterday';
710 $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params);
713 * Test using various operators
715 function testGetTypeOperators() {
716 $relTypeParams = array(
717 'name_a_b' => 'Relation 3 for delete',
718 'name_b_a' => 'Relation 6 for delete',
719 'description' => 'Testing relationship type 2',
720 'contact_type_a' => 'Individual',
721 'contact_type_b' => 'Organization',
725 $relationType2 = $this->relationshipTypeCreate($relTypeParams);
726 $relTypeParams = array(
727 'name_a_b' => 'Relation 8 for delete',
728 'name_b_a' => 'Relation 9 for delete',
729 'description' => 'Testing relationship type 7',
730 'contact_type_a' => 'Individual',
731 'contact_type_b' => 'Organization',
735 $relationType3 = $this->relationshipTypeCreate($relTypeParams);
737 $relTypeParams = array(
738 'name_a_b' => 'Relation 6 for delete',
739 'name_b_a' => 'Relation 88for delete',
740 'description' => 'Testing relationship type 00',
741 'contact_type_a' => 'Individual',
742 'contact_type_b' => 'Organization',
746 $relationType4 = $this->relationshipTypeCreate($relTypeParams);
748 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params
);
749 $rel2 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
750 array('relationship_type_id' => $relationType2,)));
751 $rel3 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
752 array('relationship_type_id' => $relationType3,)));
753 $rel4 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
754 array('relationship_type_id' => $relationType4,)));
757 'relationship_type_id' => array('IN' => array($relationType2, $relationType3))
760 $description = "demonstrates use of IN filter";
761 $subfile = 'INRelationshipType';
763 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
764 $this->assertEquals($result['count'], 2);
765 $this->AssertEquals(array($rel2['id'], $rel3['id']), array_keys($result['values']));
767 $description = "demonstrates use of NOT IN filter";
768 $subfile = 'NotInRelationshipType';
770 'relationship_type_id' => array('NOT IN' => array($relationType2, $relationType3))
772 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
773 $this->assertEquals($result['count'], 2);
774 $this->AssertEquals(array($rel1['id'], $rel4['id']), array_keys($result['values']));
776 $description = "demonstrates use of BETWEEN filter";
777 $subfile = 'BetweenRelationshipType';
779 'relationship_type_id' => array('BETWEEN' => array($relationType2, $relationType4))
781 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
782 $this->assertEquals($result['count'], 3);
783 $this->AssertEquals(array($rel2['id'], $rel3['id'], $rel4['id']), array_keys($result['values']));
785 $description = "demonstrates use of Not BETWEEN filter";
786 $subfile = 'NotBetweenRelationshipType';
788 'relationship_type_id' => array('NOT BETWEEN' => array($relationType2, $relationType4))
790 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
791 $this->assertEquals($result['count'], 1);
792 $this->AssertEquals(array($rel1['id'],), array_keys($result['values']));
796 * check with invalid relationshipType Id
798 function testRelationshipTypeAddInvalidId() {
799 $relTypeParams = array(
801 'name_a_b' => 'Relation 1 for delete',
802 'name_b_a' => 'Relation 2 for delete',
803 'contact_type_a' => 'Individual',
804 'contact_type_b' => 'Organization',
806 $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
807 'id is not a valid integer');
811 * check with valid data with contact_b
813 function testGetRelationshipWithContactB() {
815 'contact_id_a' => $this->_cId_a
,
816 'contact_id_b' => $this->_cId_b
,
817 'relationship_type_id' => $this->_relTypeID
,
818 'start_date' => '2011-01-01',
819 'end_date' => '2013-01-01',
823 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
826 'contact_id' => $this->_cId_a
,
829 $result = $this->callAPISuccess('relationship', 'get', $contacts);
830 $this->assertGreaterThan(0, $result['count']);
832 'id' => $relationship['id'],
834 $result = $this->callAPISuccess('relationship', 'delete', $params);
835 $this->relationshipTypeDelete($this->_relTypeID
);
839 * check with valid data with relationshipTypes
841 function testGetRelationshipWithRelTypes() {
843 'contact_id_a' => $this->_cId_a
,
844 'contact_id_b' => $this->_cId_b
,
845 'relationship_type_id' => $this->_relTypeID
,
846 'start_date' => '2011-01-01',
847 'end_date' => '2013-01-01',
851 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
854 'contact_id' => $this->_cId_a
,
856 $result = $this->callAPISuccess('relationship', 'get', $contact_a);
859 'id' => $relationship['id'],
861 $result = $this->callAPISuccess('relationship', 'delete', $params);
862 $this->relationshipTypeDelete($this->_relTypeID
);
866 * Checks that passing in 'contact_id' + a relationship type
867 * will filter by relationship type (relationships go in both directions)
868 * as relationship api does a reciprocal check if contact_id provided
870 * We should get 1 result without or with correct relationship type id & 0 with
873 function testGetRelationshipByTypeReciprocal() {
874 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
875 $result = $this->callAPISuccess($this->_entity
, 'get', array(
876 'contact_id' => $this->_cId_a
,
877 'relationship_type_id' => $this->_relTypeID
,
879 $this->assertEquals(1, $result['count']);
880 $result = $this->callAPISuccess($this->_entity
, 'get', array(
881 'contact_id' => $this->_cId_a
,
882 'relationship_type_id' => $this->_relTypeID +
1,
884 $this->assertEquals(0, $result['count']);
885 $this->callAPISuccess($this->_entity
, 'delete', array('id' => $created['id']));
889 * Checks that passing in 'contact_id_b' + a relationship type
890 * will filter by relationship type for contact b
892 * We should get 1 result without or with correct relationship type id & 0 with
895 function testGetRelationshipByTypeDAO() {
896 $this->ids
['relationship'] = $this->callAPISuccess($this->_entity
, 'create', array('format.only_id' => TRUE,) +
$this->_params
);
897 $result = $this->callAPISuccess($this->_entity
, 'getcount', array(
898 'contact_id_a' => $this->_cId_a
,),
900 $result = $this->callAPISuccess($this->_entity
, 'get', array(
901 'contact_id_a' => $this->_cId_a
,
902 'relationship_type_id' => $this->_relTypeID
,
904 $this->assertEquals(1, $result['count']);
905 $result = $this->callAPISuccess($this->_entity
, 'get', array(
906 'contact_id_a' => $this->_cId_a
,
907 'relationship_type_id' => $this->_relTypeID +
1,
909 $this->assertEquals(0, $result['count']);
913 * Checks that passing in 'contact_id_b' + a relationship type
914 * will filter by relationship type for contact b
916 * We should get 1 result without or with correct relationship type id & 0 with
919 function testGetRelationshipByTypeArrayDAO() {
920 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
921 $org3 = $this->organizationCreate();
922 $relType2 = 5; // lets just assume built in ones aren't being messed with!
923 $relType3 = 6; // lets just assume built in ones aren't being messed with!
926 $this->callAPISuccess($this->_entity
, 'create',
927 array_merge($this->_params
, array(
928 'relationship_type_id' => $relType2,
929 'contact_id_b' => $this->_cId_b2
))
933 $this->callAPISuccess($this->_entity
, 'create',
934 array_merge($this->_params
, array(
935 'relationship_type_id' => $relType3,
936 'contact_id_b' => $org3))
939 $result = $this->callAPISuccess($this->_entity
, 'get', array(
940 'contact_id_a' => $this->_cId_a
,
941 'relationship_type_id' => array('IN' => array($this->_relTypeID
, $relType3)),
944 $this->assertEquals(2, $result['count']);
945 foreach ($result['values'] as $key => $value) {
946 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID
, $relType3)));
951 * Checks that passing in 'contact_id_b' + a relationship type
952 * will filter by relationship type for contact b
954 * We should get 1 result without or with correct relationship type id & 0 with
957 function testGetRelationshipByTypeArrayReciprocal() {
958 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
959 $org3 = $this->organizationCreate();
960 $relType2 = 5; // lets just assume built in ones aren't being messed with!
961 $relType3 = 6; // lets just assume built in ones aren't being messed with!
964 $this->callAPISuccess($this->_entity
, 'create',
965 array_merge($this->_params
, array(
966 'relationship_type_id' => $relType2,
967 'contact_id_b' => $this->_cId_b2
))
971 $this->callAPISuccess($this->_entity
, 'create',
972 array_merge($this->_params
, array(
973 'relationship_type_id' => $relType3,
974 'contact_id_b' => $org3))
977 $result = $this->callAPISuccess($this->_entity
, 'get', array(
978 'contact_id' => $this->_cId_a
,
979 'relationship_type_id' => array('IN' => array($this->_relTypeID
, $relType3)),
982 $this->assertEquals(2, $result['count']);
983 foreach ($result['values'] as $key => $value) {
984 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID
, $relType3)));
989 * Checks that passing in 'contact_id_b' + a relationship type
990 * will filter by relationship type for contact b
992 * We should get 1 result without or with correct relationship type id & 0 with
995 function testGetRelationshipByMembershipTypeDAO() {
996 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
997 $org3 = $this->organizationCreate();
999 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1000 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1002 $memberType = $this->membershipTypeCreate(array(
1003 'relationship_type_id' => CRM_Core_DAO
::VALUE_SEPARATOR
. $relType1 . CRM_Core_DAO
::VALUE_SEPARATOR
. $relType3 . CRM_Core_DAO
::VALUE_SEPARATOR
,
1004 'relationship_direction' => CRM_Core_DAO
::VALUE_SEPARATOR
. 'a_b' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'b_a' . CRM_Core_DAO
::VALUE_SEPARATOR
,
1008 $this->callAPISuccess($this->_entity
, 'create',
1009 array_merge($this->_params
, array(
1010 'relationship_type_id' => $relType2,
1011 'contact_id_b' => $this->_cId_b2
))
1015 $this->callAPISuccess($this->_entity
, 'create',
1016 array_merge($this->_params
, array(
1017 'relationship_type_id' => $relType3,
1018 'contact_id_b' => $org3))
1021 //relationshp 4 with reveral
1022 $this->callAPISuccess($this->_entity
, 'create',
1023 array_merge($this->_params
, array(
1024 'relationship_type_id' => $relType1,
1025 'contact_id_a' => $this->_cId_a
,
1026 'contact_id_b' => $this->_cId_a_2
))
1029 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1030 'contact_id_a' => $this->_cId_a
,
1031 'membership_type_id' => $memberType,
1033 // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1034 $this->assertEquals(1, $result['count']);
1035 foreach ($result['values'] as $key => $value) {
1036 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1)));
1041 * Checks that passing in 'contact_id_b' + a relationship type
1042 * will filter by relationship type for contact b
1044 * We should get 1 result without or with correct relationship type id & 0 with
1047 function testGetRelationshipByMembershipTypeReciprocal() {
1048 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1049 $org3 = $this->organizationCreate();
1051 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1052 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1054 $memberType = $this->membershipTypeCreate(array(
1055 'relationship_type_id' => CRM_Core_DAO
::VALUE_SEPARATOR
. $relType1 . CRM_Core_DAO
::VALUE_SEPARATOR
. $relType3 . CRM_Core_DAO
::VALUE_SEPARATOR
,
1056 'relationship_direction' => CRM_Core_DAO
::VALUE_SEPARATOR
. 'a_b' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'b_a' . CRM_Core_DAO
::VALUE_SEPARATOR
,
1060 $this->callAPISuccess($this->_entity
, 'create',
1061 array_merge($this->_params
, array(
1062 'relationship_type_id' => $relType2,
1063 'contact_id_b' => $this->_cId_b2
))
1067 $this->callAPISuccess($this->_entity
, 'create',
1068 array_merge($this->_params
, array(
1069 'relationship_type_id' => $relType3,
1070 'contact_id_b' => $org3))
1073 //relationshp 4 with reveral
1074 $this->callAPISuccess($this->_entity
, 'create',
1075 array_merge($this->_params
, array(
1076 'relationship_type_id' => $relType1,
1077 'contact_id_a' => $this->_cId_a
,
1078 'contact_id_b' => $this->_cId_a_2
))
1081 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1082 'contact_id' => $this->_cId_a
,
1083 'membership_type_id' => $memberType,
1085 // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1086 $this->assertEquals(2, $result['count']);
1088 foreach ($result['values'] as $key => $value) {
1089 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1, $relType3)));