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;
57 'name' => 'Relationship Create',
58 'description' => 'Test all Relationship Create API methods.',
59 'group' => 'CiviCRM API Tests',
65 $this->_cId_a
= $this->individualCreate();
66 $this->_cId_a_2
= $this->individualCreate(array('last_name' => 'c2', 'email' => 'c@w.com', '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',
93 $this->contactDelete($this->_cId_a
);
94 $this->contactDelete($this->_cId_a_2
);
95 $this->contactDelete($this->_cId_b
);
96 $this->contactDelete($this->_cId_b2
);
97 $this->quickCleanup(array('civicrm_relationship'), TRUE);
98 $this->relationshipTypeDelete($this->_relTypeID
);
101 ///////////////// civicrm_relationship_create methods
104 * Check with empty array
106 function testRelationshipCreateEmpty() {
107 $this->callAPIFailure('relationship', 'create', array());
111 * Check if required fields are not passed
113 function testRelationshipCreateWithoutRequired() {
115 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
116 'end_date' => array('d' => '10', 'M' => '1', 'Y' => '2009'),
120 $this->callAPIFailure('relationship', 'create', $params);
124 * Check with incorrect required fields
126 function testRelationshipCreateWithIncorrectData() {
129 'contact_id_a' => $this->_cId_a
,
130 'contact_id_b' => $this->_cId_b
,
131 'relationship_type_id' => 'Breaking Relationship',
134 $this->callAPIFailure('relationship', 'create', $params);
136 //contact id is not an integer
138 'contact_id_a' => 'invalid',
139 'contact_id_b' => $this->_cId_b
,
140 'relationship_type_id' => $this->_relTypeID
,
141 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
144 $this->callAPIFailure('relationship', 'create', $params);
146 //contact id does not exists
147 $params['contact_id_a'] = 999;
148 $this->callAPIFailure('relationship', 'create', $params);
151 $params['contact_id_a'] = $this->_cId_a
;
152 $params['start_date'] = array('d' => '1', 'M' => '1');
153 $this->callAPIFailure('relationship', 'create', $params);
157 * Check relationship creation with invalid Relationship
159 function testRelationshipCreatInvalidRelationship() {
160 // both the contact of type Individual
162 'contact_id_a' => $this->_cId_a
,
163 'contact_id_b' => $this->_cId_a
,
164 'relationship_type_id' => $this->_relTypeID
,
165 'start_date' => '2008-01-10',
169 $this->callAPIFailure('relationship', 'create', $params);
171 // both the contact of type Organization
173 'contact_id_a' => $this->_cId_b
,
174 'contact_id_b' => $this->_cId_b
,
175 'relationship_type_id' => $this->_relTypeID
,
176 'start_date' => '2008-01-10',
180 $this->callAPIFailure('relationship', 'create', $params);
184 * Check relationship already exists
186 function testRelationshipCreateAlreadyExists() {
188 'contact_id_a' => $this->_cId_a
,
189 'contact_id_b' => $this->_cId_b
,
190 'relationship_type_id' => $this->_relTypeID
,
191 'start_date' => '2008-12-20', 'end_date' => NULL,
194 $relationship = $this->callAPISuccess('relationship', 'create', $params);
197 'contact_id_a' => $this->_cId_a
,
198 'contact_id_b' => $this->_cId_b
,
199 'relationship_type_id' => $this->_relTypeID
,
200 'start_date' => '2008-12-20',
203 $result = $this->callAPIFailure('relationship', 'create', $params, 'Relationship already exists');
205 $params['id'] = $relationship['id'];
206 $result = $this->callAPISuccess('relationship', 'delete', $params);
210 * Check relationship already exists
212 function testRelationshipCreateUpdateAlreadyExists() {
214 'contact_id_a' => $this->_cId_a
,
215 'contact_id_b' => $this->_cId_b
,
216 'relationship_type_id' => $this->_relTypeID
,
217 'start_date' => '2008-12-20',
222 $relationship = $this->callAPISuccess('relationship', 'create', $params);
224 'id' => $relationship['id'],
228 $result = $this->callAPISuccess('relationship', 'create', $params);
229 $result = $this->callAPISuccess('relationship', 'get', $params);
230 $params['id'] = $relationship['id'];
231 $result = $this->callAPISuccess('relationship', 'delete', $params);
235 * Checkupdate doesn't reset stuff badly - CRM-11789
237 function testRelationshipCreateUpdateDoesntMangle() {
239 'contact_id_a' => $this->_cId_a
,
240 'contact_id_b' => $this->_cId_b
,
241 'relationship_type_id' => $this->_relTypeID
,
242 'start_date' => '2008-12-20',
244 'is_permission_a_b' => 1,
245 'description' => 'my desc',
247 $relationship = $this->callAPISuccess('relationship', 'create', $params);
249 $updateparams = array(
250 'id' => $relationship['id'],
251 'relationship_type_id' => $this->_relTypeID
,
253 $result = $this->callAPISuccess('relationship', 'create', $updateparams);
255 //make sure the orig params didn't get changed
256 $this->getAndCheck($params, $relationship['id'], 'relationship');
263 * Check relationship creation
265 function testRelationshipCreate() {
267 'contact_id_a' => $this->_cId_a
,
268 'contact_id_b' => $this->_cId_b
,
269 'relationship_type_id' => $this->_relTypeID
,
270 'start_date' => '2010-10-30',
271 'end_date' => '2010-12-30',
276 $result = $this->callAPIAndDocument('relationship', 'create', $params, __FUNCTION__
, __FILE__
);
277 $this->assertNotNull($result['id']);
278 $relationParams = array(
279 'id' => $result['id'],
282 // assertDBState compares expected values in $result to actual values in the DB
283 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
284 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
285 $values = $result['values'][$result['id']];
286 foreach ($params as $key => $value) {
287 if ($key == 'note') {
290 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE));
292 $params['id'] = $result['id'];
293 $this->callAPISuccess('relationship', 'delete', $params);
296 * Ensure disabling works
298 function testRelationshipUpdate() {
299 $result = $this->callAPISuccess('relationship', 'create', $this->_params
);
300 $relID = $result['id'];
301 $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'description' => 'blah'));
302 $this->assertEquals($relID, $result['id']);
303 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
304 $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'is_permission_b_a' => 1));
305 $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
306 $result = $this->callAPISuccess('relationship', 'create', array('id' => $result['id'], 'is_active' => 0));
307 $this->assertEquals(0, $result['values'][$result['id']]['is_active']);
308 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
309 $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
312 * Check relationship creation
314 function testRelationshipCreateEmptyEndDate() {
316 'contact_id_a' => $this->_cId_a
,
317 'contact_id_b' => $this->_cId_b
,
318 'relationship_type_id' => $this->_relTypeID
,
319 'start_date' => '2010-10-30',
325 $result = $this->callAPISuccess('relationship', 'create', $params);
326 $this->assertNotNull($result['id']);
327 $relationParams = array(
328 'id' => $result['id'],
331 // assertDBState compares expected values in $result to actual values in the DB
332 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
333 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
334 $values = $result['values'][$result['id']];
335 foreach ($params as $key => $value) {
336 if ($key == 'note') {
339 if($key == 'end_date'){
340 $this->assertTrue(empty($values[$key]));
343 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__
);
345 $params['id'] = $result['id'];
346 $this->callAPISuccess('relationship', 'delete', $params);
350 * Check relationship creation with custom data
352 function testRelationshipCreateWithCustomData() {
353 $customGroup = $this->createCustomGroup();
354 $this->_ids
= $this->createCustomField();
355 //few custom Values for comparing
356 $custom_params = array(
357 "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship',
358 "custom_{$this->_ids[1]}" => 'Y',
359 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
360 "custom_{$this->_ids[3]}" => 'http://example.com',
364 'contact_id_a' => $this->_cId_a
,
365 'contact_id_b' => $this->_cId_b
,
366 'relationship_type_id' => $this->_relTypeID
,
367 'start_date' => '2008-12-20',
370 $params = array_merge($params, $custom_params);
371 $result = $this->callAPISuccess('relationship', 'create', $params);
373 $relationParams = array(
374 'id' => $result['id'],
376 // assertDBState compares expected values in $result to actual values in the DB
377 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
379 $params['id'] = $result['id'];
380 $result = $this->callAPISuccess('relationship', 'delete', $params);
381 $this->relationshipTypeDelete($this->_relTypeID
);
385 * Check with complete array + custom field
386 * Note that the test is written on purpose without any
387 * variables specific to participant so it can be replicated into other entities
388 * and / or moved to the automated test suite
390 function testGetWithCustom() {
391 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
393 $params = $this->_params
;
394 $params['custom_' . $ids['custom_field_id']] = "custom string";
396 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
397 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
399 $getParams = array('id' => $result['id']);
400 $check = $this->callAPIAndDocument($this->_entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
401 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
403 $this->customFieldDelete($ids['custom_field_id']);
404 $this->customGroupDelete($ids['custom_group_id']);
410 function createCustomGroup() {
412 'title' => 'Custom Group',
413 'extends' => array('Relationship'),
419 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
420 $this->_customGroupId
= $customGroup['id'];
421 return $customGroup['id'];
427 function createCustomField() {
430 'custom_group_id' => $this->_customGroupId
,
431 'label' => 'Enter text about relationship',
432 'html_type' => 'Text',
433 'data_type' => 'String',
434 'default_value' => 'xyz',
437 'is_searchable' => 0,
442 $result = $this->callAPISuccess('CustomField', 'create', $params);
445 $ids[] = $customField['result']['customFieldId'];
447 $optionValue[] = array(
453 $optionValue[] = array(
459 $optionValue[] = array(
467 'label' => 'Pick Color',
468 'html_type' => 'Select',
469 'data_type' => 'String',
472 'is_searchable' => 0,
474 'option_values' => $optionValue,
475 'custom_group_id' => $this->_customGroupId
,
478 $customField = $this->callAPISuccess('custom_field', 'create', $params);
479 $ids[] = $customField['id'];
482 'custom_group_id' => $this->_customGroupId
,
483 'name' => 'test_date',
484 'label' => 'test_date',
485 'html_type' => 'Select Date',
486 'data_type' => 'Date',
487 'default_value' => '20090711',
490 'is_searchable' => 0,
494 $customField = $this->callAPISuccess('custom_field', 'create', $params);
496 $ids[] = $customField['id'];
498 'custom_group_id' => $this->_customGroupId
,
499 'name' => 'test_link',
500 'label' => 'test_link',
501 'html_type' => 'Link',
502 'data_type' => 'Link',
503 'default_value' => 'http://civicrm.org',
506 'is_searchable' => 0,
510 $customField = $this->callAPISuccess('custom_field', 'create', $params);
511 $ids[] = $customField['id'];
515 ///////////////// civicrm_relationship_delete methods
518 * Check with empty array
520 function testRelationshipDeleteEmpty() {
522 $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
526 * Check if required fields are not passed
528 function testRelationshipDeleteWithoutRequired() {
530 'start_date' => '2008-12-20',
531 'end_date' => '2009-12-20',
535 $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
539 * Check with incorrect required fields
541 function testRelationshipDeleteWithIncorrectData() {
543 'contact_id_a' => $this->_cId_a
,
544 'contact_id_b' => $this->_cId_b
,
545 'relationship_type_id' => 'Breaking Relationship',
548 $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
550 $params['id'] = "Invalid";
551 $result = $this->callAPIFailure('relationship', 'delete', $params, 'Invalid value for relationship ID');
555 * Check relationship creation
557 function testRelationshipDelete() {
559 'contact_id_a' => $this->_cId_a
,
560 'contact_id_b' => $this->_cId_b
,
561 'relationship_type_id' => $this->_relTypeID
,
562 'start_date' => '2008-12-20',
566 $result = $this->callAPISuccess('relationship', 'create', $params);
568 //Delete relationship
570 $params['id'] = $result['id'];
572 $result = $this->callAPIAndDocument('relationship', 'delete', $params, __FUNCTION__
, __FILE__
);
573 $this->relationshipTypeDelete($this->_relTypeID
);
576 ///////////////// civicrm_relationship_update methods
579 * Check with empty array
581 function testRelationshipUpdateEmpty() {
582 $result = $this->callAPIFailure('relationship', 'create', array(),
583 'Mandatory key(s) missing from params array: contact_id_a, contact_id_b, relationship_type_id');
587 * Check if required fields are not passed
591 * Check relationship update
593 function testRelationshipCreateDuplicate() {
595 'contact_id_a' => $this->_cId_a
,
596 'contact_id_b' => $this->_cId_b
,
597 'relationship_type_id' => $this->_relTypeID
,
598 'start_date' => '20081214',
599 'end_date' => '20091214',
603 $result = $this->callAPISuccess('relationship', 'create', $relParams);
605 $this->assertNotNull($result['id']);
606 $this->_relationID
= $result['id'];
609 'contact_id_a' => $this->_cId_a
,
610 'contact_id_b' => $this->_cId_b
,
611 'relationship_type_id' => $this->_relTypeID
,
612 'start_date' => '20081214',
613 'end_date' => '20091214',
617 $result = $this->callAPIFailure('relationship', 'create', $params, 'Relationship already exists');
619 //delete created relationship
621 'id' => $this->_relationID
,
624 $result = $this->callAPISuccess('relationship', 'delete', $params);
626 //delete created relationship type
627 $this->relationshipTypeDelete($this->_relTypeID
);
631 * Check with valid params array.
633 function testRelationshipsGet() {
635 'contact_id_a' => $this->_cId_a
,
636 'contact_id_b' => $this->_cId_b
,
637 'relationship_type_id' => $this->_relTypeID
,
638 'start_date' => '2011-01-01',
639 'end_date' => '2013-01-01',
643 $result = $this->callAPISuccess('relationship', 'create', $relParams);
647 'contact_id' => $this->_cId_b
,
649 $result = $this->callAPISuccess('relationship', 'get', $params);
650 $this->assertEquals($result['count'], 1);
652 'contact_id_a' => $this->_cId_a
,
654 $result = $this->callAPISuccess('relationship', 'get', $params);
655 $this->assertEquals($result['count'], 1);
656 // contact_id_a is wrong so should be no matches
658 'contact_id_a' => $this->_cId_b
,
660 $result = $this->callAPISuccess('relationship', 'get', $params);
661 $this->assertEquals($result['count'], 0);
665 * Check with valid params array.
666 * (The get function will behave differently without 'contact_id' passed
668 function testRelationshipsGetGeneric() {
670 'contact_id_a' => $this->_cId_a
,
671 'contact_id_b' => $this->_cId_b
,
672 'relationship_type_id' => $this->_relTypeID
,
673 'start_date' => '2011-01-01',
674 'end_date' => '2013-01-01',
678 $result = $this->callAPISuccess('relationship', 'create', $relParams);
682 'contact_id_b' => $this->_cId_b
,
684 $result = $this->callAPISuccess('relationship', 'get', $params);
687 function testGetIsCurrent() {
689 'contact_id_a' => $this->_cId_a
,
690 'contact_id_b' => $this->_cId_b2
,
691 'relationship_type_id' => $this->_relTypeID
,
692 'start_date' => '2008-12-20',
695 $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params);
696 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params
);
699 'filters' => array('is_current' => 1)
701 $description = "demonstrates is_current filter";
702 $subfile = 'filterIsCurrent';
703 //no relationship has been created
704 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
705 $this->assertEquals($result['count'], 1);
706 $this->AssertEquals($rel1['id'], $result['id']);
708 // now try not started
709 $rel2Params['is_active'] =1;
710 $rel2Params['start_date'] ='tomorrow';
711 $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params);
712 $result = $this->callAPISuccess('relationship', 'get', $getParams);
713 $this->assertEquals($result['count'], 1);
714 $this->AssertEquals($rel1['id'], $result['id']);
717 $rel2Params['is_active'] =1;
718 $rel2Params['start_date'] ='last week';
719 $rel2Params['end_date'] ='yesterday';
720 $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params);
723 * Test using various operators
725 function testGetTypeOperators() {
726 $relTypeParams = array(
727 'name_a_b' => 'Relation 3 for delete',
728 'name_b_a' => 'Relation 6 for delete',
729 'description' => 'Testing relationship type 2',
730 'contact_type_a' => 'Individual',
731 'contact_type_b' => 'Organization',
735 $relationType2 = $this->relationshipTypeCreate($relTypeParams);
736 $relTypeParams = array(
737 'name_a_b' => 'Relation 8 for delete',
738 'name_b_a' => 'Relation 9 for delete',
739 'description' => 'Testing relationship type 7',
740 'contact_type_a' => 'Individual',
741 'contact_type_b' => 'Organization',
745 $relationType3 = $this->relationshipTypeCreate($relTypeParams);
747 $relTypeParams = array(
748 'name_a_b' => 'Relation 6 for delete',
749 'name_b_a' => 'Relation 88for delete',
750 'description' => 'Testing relationship type 00',
751 'contact_type_a' => 'Individual',
752 'contact_type_b' => 'Organization',
756 $relationType4 = $this->relationshipTypeCreate($relTypeParams);
758 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params
);
759 $rel2 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
760 array('relationship_type_id' => $relationType2,)));
761 $rel3 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
762 array('relationship_type_id' => $relationType3,)));
763 $rel4 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
764 array('relationship_type_id' => $relationType4,)));
767 'relationship_type_id' => array('IN' => array($relationType2, $relationType3))
770 $description = "demonstrates use of IN filter";
771 $subfile = 'INRelationshipType';
773 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
774 $this->assertEquals($result['count'], 2);
775 $this->AssertEquals(array($rel2['id'], $rel3['id']), array_keys($result['values']));
777 $description = "demonstrates use of NOT IN filter";
778 $subfile = 'NotInRelationshipType';
780 'relationship_type_id' => array('NOT IN' => array($relationType2, $relationType3))
782 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
783 $this->assertEquals($result['count'], 2);
784 $this->AssertEquals(array($rel1['id'], $rel4['id']), array_keys($result['values']));
786 $description = "demonstrates use of BETWEEN filter";
787 $subfile = 'BetweenRelationshipType';
789 'relationship_type_id' => array('BETWEEN' => array($relationType2, $relationType4))
791 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
792 $this->assertEquals($result['count'], 3);
793 $this->AssertEquals(array($rel2['id'], $rel3['id'], $rel4['id']), array_keys($result['values']));
795 $description = "demonstrates use of Not BETWEEN filter";
796 $subfile = 'NotBetweenRelationshipType';
798 'relationship_type_id' => array('NOT BETWEEN' => array($relationType2, $relationType4))
800 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
801 $this->assertEquals($result['count'], 1);
802 $this->AssertEquals(array($rel1['id'],), array_keys($result['values']));
806 * Check with invalid relationshipType Id
808 function testRelationshipTypeAddInvalidId() {
809 $relTypeParams = array(
811 'name_a_b' => 'Relation 1 for delete',
812 'name_b_a' => 'Relation 2 for delete',
813 'contact_type_a' => 'Individual',
814 'contact_type_b' => 'Organization',
816 $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
817 'id is not a valid integer');
821 * Check with valid data with contact_b
823 function testGetRelationshipWithContactB() {
825 'contact_id_a' => $this->_cId_a
,
826 'contact_id_b' => $this->_cId_b
,
827 'relationship_type_id' => $this->_relTypeID
,
828 'start_date' => '2011-01-01',
829 'end_date' => '2013-01-01',
833 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
836 'contact_id' => $this->_cId_a
,
839 $result = $this->callAPISuccess('relationship', 'get', $contacts);
840 $this->assertGreaterThan(0, $result['count']);
842 'id' => $relationship['id'],
844 $result = $this->callAPISuccess('relationship', 'delete', $params);
845 $this->relationshipTypeDelete($this->_relTypeID
);
849 * Check with valid data with relationshipTypes
851 function testGetRelationshipWithRelTypes() {
853 'contact_id_a' => $this->_cId_a
,
854 'contact_id_b' => $this->_cId_b
,
855 'relationship_type_id' => $this->_relTypeID
,
856 'start_date' => '2011-01-01',
857 'end_date' => '2013-01-01',
861 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
864 'contact_id' => $this->_cId_a
,
866 $result = $this->callAPISuccess('relationship', 'get', $contact_a);
869 'id' => $relationship['id'],
871 $result = $this->callAPISuccess('relationship', 'delete', $params);
872 $this->relationshipTypeDelete($this->_relTypeID
);
876 * Checks that passing in 'contact_id' + a relationship type
877 * will filter by relationship type (relationships go in both directions)
878 * as relationship api does a reciprocal check if contact_id provided
880 * We should get 1 result without or with correct relationship type id & 0 with
883 function testGetRelationshipByTypeReciprocal() {
884 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
885 $result = $this->callAPISuccess($this->_entity
, 'get', array(
886 'contact_id' => $this->_cId_a
,
887 'relationship_type_id' => $this->_relTypeID
,
889 $this->assertEquals(1, $result['count']);
890 $result = $this->callAPISuccess($this->_entity
, 'get', array(
891 'contact_id' => $this->_cId_a
,
892 'relationship_type_id' => $this->_relTypeID +
1,
894 $this->assertEquals(0, $result['count']);
895 $this->callAPISuccess($this->_entity
, 'delete', array('id' => $created['id']));
899 * Checks that passing in 'contact_id_b' + a relationship type
900 * will filter by relationship type for contact b
902 * We should get 1 result without or with correct relationship type id & 0 with
905 function testGetRelationshipByTypeDAO() {
906 $this->ids
['relationship'] = $this->callAPISuccess($this->_entity
, 'create', array('format.only_id' => TRUE,) +
$this->_params
);
907 $result = $this->callAPISuccess($this->_entity
, 'getcount', array(
908 'contact_id_a' => $this->_cId_a
,),
910 $result = $this->callAPISuccess($this->_entity
, 'get', array(
911 'contact_id_a' => $this->_cId_a
,
912 'relationship_type_id' => $this->_relTypeID
,
914 $this->assertEquals(1, $result['count']);
915 $result = $this->callAPISuccess($this->_entity
, 'get', array(
916 'contact_id_a' => $this->_cId_a
,
917 'relationship_type_id' => $this->_relTypeID +
1,
919 $this->assertEquals(0, $result['count']);
923 * Checks that passing in 'contact_id_b' + a relationship type
924 * will filter by relationship type for contact b
926 * We should get 1 result without or with correct relationship type id & 0 with
929 function testGetRelationshipByTypeArrayDAO() {
930 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
931 $org3 = $this->organizationCreate();
932 $relType2 = 5; // lets just assume built in ones aren't being messed with!
933 $relType3 = 6; // lets just assume built in ones aren't being messed with!
936 $this->callAPISuccess($this->_entity
, 'create',
937 array_merge($this->_params
, array(
938 'relationship_type_id' => $relType2,
939 'contact_id_b' => $this->_cId_b2
))
943 $this->callAPISuccess($this->_entity
, 'create',
944 array_merge($this->_params
, array(
945 'relationship_type_id' => $relType3,
946 'contact_id_b' => $org3))
949 $result = $this->callAPISuccess($this->_entity
, 'get', array(
950 'contact_id_a' => $this->_cId_a
,
951 'relationship_type_id' => array('IN' => array($this->_relTypeID
, $relType3)),
954 $this->assertEquals(2, $result['count']);
955 foreach ($result['values'] as $key => $value) {
956 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID
, $relType3)));
961 * Checks that passing in 'contact_id_b' + a relationship type
962 * will filter by relationship type for contact b
964 * We should get 1 result without or with correct relationship type id & 0 with
967 function testGetRelationshipByTypeArrayReciprocal() {
968 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
969 $org3 = $this->organizationCreate();
970 $relType2 = 5; // lets just assume built in ones aren't being messed with!
971 $relType3 = 6; // lets just assume built in ones aren't being messed with!
974 $this->callAPISuccess($this->_entity
, 'create',
975 array_merge($this->_params
, array(
976 'relationship_type_id' => $relType2,
977 'contact_id_b' => $this->_cId_b2
))
981 $this->callAPISuccess($this->_entity
, 'create',
982 array_merge($this->_params
, array(
983 'relationship_type_id' => $relType3,
984 'contact_id_b' => $org3))
987 $result = $this->callAPISuccess($this->_entity
, 'get', array(
988 'contact_id' => $this->_cId_a
,
989 'relationship_type_id' => array('IN' => array($this->_relTypeID
, $relType3)),
992 $this->assertEquals(2, $result['count']);
993 foreach ($result['values'] as $key => $value) {
994 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID
, $relType3)));
999 * Checks that passing in 'contact_id_b' + a relationship type
1000 * will filter by relationship type for contact b
1002 * We should get 1 result without or with correct relationship type id & 0 with
1005 function testGetRelationshipByMembershipTypeDAO() {
1006 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1007 $org3 = $this->organizationCreate();
1009 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1010 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1012 $memberType = $this->membershipTypeCreate(array(
1013 'relationship_type_id' => CRM_Core_DAO
::VALUE_SEPARATOR
. $relType1 . CRM_Core_DAO
::VALUE_SEPARATOR
. $relType3 . CRM_Core_DAO
::VALUE_SEPARATOR
,
1014 'relationship_direction' => CRM_Core_DAO
::VALUE_SEPARATOR
. 'a_b' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'b_a' . CRM_Core_DAO
::VALUE_SEPARATOR
,
1018 $this->callAPISuccess($this->_entity
, 'create',
1019 array_merge($this->_params
, array(
1020 'relationship_type_id' => $relType2,
1021 'contact_id_b' => $this->_cId_b2
))
1025 $this->callAPISuccess($this->_entity
, 'create',
1026 array_merge($this->_params
, array(
1027 'relationship_type_id' => $relType3,
1028 'contact_id_b' => $org3))
1031 //relationshp 4 with reveral
1032 $this->callAPISuccess($this->_entity
, 'create',
1033 array_merge($this->_params
, array(
1034 'relationship_type_id' => $relType1,
1035 'contact_id_a' => $this->_cId_a
,
1036 'contact_id_b' => $this->_cId_a_2
))
1039 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1040 'contact_id_a' => $this->_cId_a
,
1041 'membership_type_id' => $memberType,
1043 // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1044 $this->assertEquals(1, $result['count']);
1045 foreach ($result['values'] as $key => $value) {
1046 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1)));
1051 * Checks that passing in 'contact_id_b' + a relationship type
1052 * will filter by relationship type for contact b
1054 * We should get 1 result without or with correct relationship type id & 0 with
1057 function testGetRelationshipByMembershipTypeReciprocal() {
1058 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1059 $org3 = $this->organizationCreate();
1061 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1062 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1064 $memberType = $this->membershipTypeCreate(array(
1065 'relationship_type_id' => CRM_Core_DAO
::VALUE_SEPARATOR
. $relType1 . CRM_Core_DAO
::VALUE_SEPARATOR
. $relType3 . CRM_Core_DAO
::VALUE_SEPARATOR
,
1066 'relationship_direction' => CRM_Core_DAO
::VALUE_SEPARATOR
. 'a_b' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'b_a' . CRM_Core_DAO
::VALUE_SEPARATOR
,
1070 $this->callAPISuccess($this->_entity
, 'create',
1071 array_merge($this->_params
, array(
1072 'relationship_type_id' => $relType2,
1073 'contact_id_b' => $this->_cId_b2
))
1077 $this->callAPISuccess($this->_entity
, 'create',
1078 array_merge($this->_params
, array(
1079 'relationship_type_id' => $relType3,
1080 'contact_id_b' => $org3))
1083 //relationshp 4 with reveral
1084 $this->callAPISuccess($this->_entity
, 'create',
1085 array_merge($this->_params
, array(
1086 'relationship_type_id' => $relType1,
1087 'contact_id_a' => $this->_cId_a
,
1088 'contact_id_b' => $this->_cId_a_2
))
1091 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1092 'contact_id' => $this->_cId_a
,
1093 'membership_type_id' => $memberType,
1095 // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1096 $this->assertEquals(2, $result['count']);
1098 foreach ($result['values'] as $key => $value) {
1099 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1, $relType3)));
1104 * Check for enotices on enable & disable as reported in CRM-14350
1106 function testSetActive() {
1107 $relationship = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1108 $this->callAPISuccess($this->_entity
, 'create', array('id' => $relationship['id'], 'is_active' => 0));
1109 $this->callAPISuccess($this->_entity
, 'create', array('id' => $relationship['id'], 'is_active' => 1));