3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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;
49 public $_eNoticeCompliant = TRUE;
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);
293 * check relationship creation
295 function testRelationshipCreateEmptyEndDate() {
297 'contact_id_a' => $this->_cId_a
,
298 'contact_id_b' => $this->_cId_b
,
299 'relationship_type_id' => $this->_relTypeID
,
300 'start_date' => '2010-10-30',
306 $result = $this->callAPISuccess('relationship', 'create', $params);
307 $this->assertNotNull($result['id']);
308 $relationParams = array(
309 'id' => $result['id'],
312 // assertDBState compares expected values in $result to actual values in the DB
313 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
314 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
315 $values = $result['values'][$result['id']];
316 foreach ($params as $key => $value) {
317 if ($key == 'note') {
320 if($key == 'end_date'){
321 $this->assertTrue(empty($values[$key]));
324 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__
);
326 $params['id'] = $result['id'];
327 $this->callAPISuccess('relationship', 'delete', $params);
331 * check relationship creation with custom data
333 function testRelationshipCreateWithCustomData() {
334 $customGroup = $this->createCustomGroup();
335 $this->_ids
= $this->createCustomField();
336 //few custom Values for comparing
337 $custom_params = array(
338 "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship',
339 "custom_{$this->_ids[1]}" => 'Y',
340 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
341 "custom_{$this->_ids[3]}" => 'http://example.com',
345 'contact_id_a' => $this->_cId_a
,
346 'contact_id_b' => $this->_cId_b
,
347 'relationship_type_id' => $this->_relTypeID
,
348 'start_date' => '2008-12-20',
351 $params = array_merge($params, $custom_params);
352 $result = $this->callAPISuccess('relationship', 'create', $params);
354 $relationParams = array(
355 'id' => $result['id'],
357 // assertDBState compares expected values in $result to actual values in the DB
358 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
360 $params['id'] = $result['id'];
361 $result = $this->callAPISuccess('relationship', 'delete', $params);
362 $this->relationshipTypeDelete($this->_relTypeID
);
366 * check with complete array + custom field
367 * Note that the test is written on purpose without any
368 * variables specific to participant so it can be replicated into other entities
369 * and / or moved to the automated test suite
371 function testGetWithCustom() {
372 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
374 $params = $this->_params
;
375 $params['custom_' . $ids['custom_field_id']] = "custom string";
377 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
378 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
380 $getParams = array('id' => $result['id']);
381 $check = $this->callAPIAndDocument($this->_entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
382 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
384 $this->customFieldDelete($ids['custom_field_id']);
385 $this->customGroupDelete($ids['custom_group_id']);
388 function createCustomGroup() {
390 'title' => 'Custom Group',
391 'extends' => array('Relationship'),
397 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
398 $this->_customGroupId
= $customGroup['id'];
399 return $customGroup['id'];
402 function createCustomField() {
405 'custom_group_id' => $this->_customGroupId
,
406 'label' => 'Enter text about relationship',
407 'html_type' => 'Text',
408 'data_type' => 'String',
409 'default_value' => 'xyz',
412 'is_searchable' => 0,
417 $result = $this->callAPISuccess('CustomField', 'create', $params);
420 $ids[] = $customField['result']['customFieldId'];
422 $optionValue[] = array(
428 $optionValue[] = array(
434 $optionValue[] = array(
442 'label' => 'Pick Color',
443 'html_type' => 'Select',
444 'data_type' => 'String',
447 'is_searchable' => 0,
449 'option_values' => $optionValue,
450 'custom_group_id' => $this->_customGroupId
,
453 $customField = $this->callAPISuccess('custom_field', 'create', $params);
454 $ids[] = $customField['id'];
457 'custom_group_id' => $this->_customGroupId
,
458 'name' => 'test_date',
459 'label' => 'test_date',
460 'html_type' => 'Select Date',
461 'data_type' => 'Date',
462 'default_value' => '20090711',
465 'is_searchable' => 0,
469 $customField = $this->callAPISuccess('custom_field', 'create', $params);
471 $ids[] = $customField['id'];
473 'custom_group_id' => $this->_customGroupId
,
474 'name' => 'test_link',
475 'label' => 'test_link',
476 'html_type' => 'Link',
477 'data_type' => 'Link',
478 'default_value' => 'http://civicrm.org',
481 'is_searchable' => 0,
485 $customField = $this->callAPISuccess('custom_field', 'create', $params);
486 $ids[] = $customField['id'];
490 ///////////////// civicrm_relationship_delete methods
493 * check with empty array
495 function testRelationshipDeleteEmpty() {
497 $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
501 * check if required fields are not passed
503 function testRelationshipDeleteWithoutRequired() {
505 'start_date' => '2008-12-20',
506 'end_date' => '2009-12-20',
510 $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
514 * check with incorrect required fields
516 function testRelationshipDeleteWithIncorrectData() {
518 'contact_id_a' => $this->_cId_a
,
519 'contact_id_b' => $this->_cId_b
,
520 'relationship_type_id' => 'Breaking Relationship',
523 $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
525 $params['id'] = "Invalid";
526 $result = $this->callAPIFailure('relationship', 'delete', $params, 'Invalid value for relationship ID');
530 * check relationship creation
532 function testRelationshipDelete() {
534 'contact_id_a' => $this->_cId_a
,
535 'contact_id_b' => $this->_cId_b
,
536 'relationship_type_id' => $this->_relTypeID
,
537 'start_date' => '2008-12-20',
541 $result = $this->callAPISuccess('relationship', 'create', $params);
543 //Delete relationship
545 $params['id'] = $result['id'];
547 $result = $this->callAPIAndDocument('relationship', 'delete', $params, __FUNCTION__
, __FILE__
);
548 $this->relationshipTypeDelete($this->_relTypeID
);
551 ///////////////// civicrm_relationship_update methods
554 * check with empty array
556 function testRelationshipUpdateEmpty() {
557 $result = $this->callAPIFailure('relationship', 'create', array(),
558 'Mandatory key(s) missing from params array: contact_id_a, contact_id_b, relationship_type_id');
562 * check if required fields are not passed
566 * check relationship update
568 function testRelationshipCreateDuplicate() {
570 'contact_id_a' => $this->_cId_a
,
571 'contact_id_b' => $this->_cId_b
,
572 'relationship_type_id' => $this->_relTypeID
,
573 'start_date' => '20081214',
574 'end_date' => '20091214',
578 $result = $this->callAPISuccess('relationship', 'create', $relParams);
580 $this->assertNotNull($result['id']);
581 $this->_relationID
= $result['id'];
584 'contact_id_a' => $this->_cId_a
,
585 'contact_id_b' => $this->_cId_b
,
586 'relationship_type_id' => $this->_relTypeID
,
587 'start_date' => '20081214',
588 'end_date' => '20091214',
592 $result = $this->callAPIFailure('relationship', 'create', $params, 'Relationship already exists');
594 //delete created relationship
596 'id' => $this->_relationID
,
599 $result = $this->callAPISuccess('relationship', 'delete', $params);
601 //delete created relationship type
602 $this->relationshipTypeDelete($this->_relTypeID
);
606 * check with valid params array.
608 function testRelationshipsGet() {
610 'contact_id_a' => $this->_cId_a
,
611 'contact_id_b' => $this->_cId_b
,
612 'relationship_type_id' => $this->_relTypeID
,
613 'start_date' => '2011-01-01',
614 'end_date' => '2013-01-01',
618 $result = $this->callAPISuccess('relationship', 'create', $relParams);
622 'contact_id' => $this->_cId_b
,
624 $result = $this->callAPISuccess('relationship', 'get', $params);
625 $this->assertEquals($result['count'], 1);
627 'contact_id_a' => $this->_cId_a
,
629 $result = $this->callAPISuccess('relationship', 'get', $params);
630 $this->assertEquals($result['count'], 1);
631 // contact_id_a is wrong so should be no matches
633 'contact_id_a' => $this->_cId_b
,
635 $result = $this->callAPISuccess('relationship', 'get', $params);
636 $this->assertEquals($result['count'], 0);
640 * check with valid params array.
641 * (The get function will behave differently without 'contact_id' passed
643 function testRelationshipsGetGeneric() {
645 'contact_id_a' => $this->_cId_a
,
646 'contact_id_b' => $this->_cId_b
,
647 'relationship_type_id' => $this->_relTypeID
,
648 'start_date' => '2011-01-01',
649 'end_date' => '2013-01-01',
653 $result = $this->callAPISuccess('relationship', 'create', $relParams);
657 'contact_id_b' => $this->_cId_b
,
659 $result = $this->callAPISuccess('relationship', 'get', $params);
662 function testGetIsCurrent() {
664 'contact_id_a' => $this->_cId_a
,
665 'contact_id_b' => $this->_cId_b2
,
666 'relationship_type_id' => $this->_relTypeID
,
667 'start_date' => '2008-12-20',
670 $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params);
671 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params
);
674 'filters' => array('is_current' => 1)
676 $description = "demonstrates is_current filter";
677 $subfile = 'filterIsCurrent';
678 //no relationship has been created
679 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
680 $this->assertEquals($result['count'], 1);
681 $this->AssertEquals($rel1['id'], $result['id']);
683 // now try not started
684 $rel2Params['is_active'] =1;
685 $rel2Params['start_date'] ='tomorrow';
686 $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params);
687 $result = $this->callAPISuccess('relationship', 'get', $getParams);
688 $this->assertEquals($result['count'], 1);
689 $this->AssertEquals($rel1['id'], $result['id']);
692 $rel2Params['is_active'] =1;
693 $rel2Params['start_date'] ='last week';
694 $rel2Params['end_date'] ='yesterday';
695 $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params);
698 * Test using various operators
700 function testGetTypeOperators() {
701 $relTypeParams = array(
702 'name_a_b' => 'Relation 3 for delete',
703 'name_b_a' => 'Relation 6 for delete',
704 'description' => 'Testing relationship type 2',
705 'contact_type_a' => 'Individual',
706 'contact_type_b' => 'Organization',
710 $relationType2 = $this->relationshipTypeCreate($relTypeParams);
711 $relTypeParams = array(
712 'name_a_b' => 'Relation 8 for delete',
713 'name_b_a' => 'Relation 9 for delete',
714 'description' => 'Testing relationship type 7',
715 'contact_type_a' => 'Individual',
716 'contact_type_b' => 'Organization',
720 $relationType3 = $this->relationshipTypeCreate($relTypeParams);
722 $relTypeParams = array(
723 'name_a_b' => 'Relation 6 for delete',
724 'name_b_a' => 'Relation 88for delete',
725 'description' => 'Testing relationship type 00',
726 'contact_type_a' => 'Individual',
727 'contact_type_b' => 'Organization',
731 $relationType4 = $this->relationshipTypeCreate($relTypeParams);
733 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params
);
734 $rel2 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
735 array('relationship_type_id' => $relationType2,)));
736 $rel3 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
737 array('relationship_type_id' => $relationType3,)));
738 $rel4 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
739 array('relationship_type_id' => $relationType4,)));
742 'relationship_type_id' => array('IN' => array($relationType2, $relationType3))
745 $description = "demonstrates use of IN filter";
746 $subfile = 'INRelationshipType';
748 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
749 $this->assertEquals($result['count'], 2);
750 $this->AssertEquals(array($rel2['id'], $rel3['id']), array_keys($result['values']));
752 $description = "demonstrates use of NOT IN filter";
753 $subfile = 'NotInRelationshipType';
755 'relationship_type_id' => array('NOT IN' => array($relationType2, $relationType3))
757 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
758 $this->assertEquals($result['count'], 2);
759 $this->AssertEquals(array($rel1['id'], $rel4['id']), array_keys($result['values']));
761 $description = "demonstrates use of BETWEEN filter";
762 $subfile = 'BetweenRelationshipType';
764 'relationship_type_id' => array('BETWEEN' => array($relationType2, $relationType4))
766 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
767 $this->assertEquals($result['count'], 3);
768 $this->AssertEquals(array($rel2['id'], $rel3['id'], $rel4['id']), array_keys($result['values']));
770 $description = "demonstrates use of Not BETWEEN filter";
771 $subfile = 'NotBetweenRelationshipType';
773 'relationship_type_id' => array('NOT BETWEEN' => array($relationType2, $relationType4))
775 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
776 $this->assertEquals($result['count'], 1);
777 $this->AssertEquals(array($rel1['id'],), array_keys($result['values']));
781 * check with invalid relationshipType Id
783 function testRelationshipTypeAddInvalidId() {
784 $relTypeParams = array(
786 'name_a_b' => 'Relation 1 for delete',
787 'name_b_a' => 'Relation 2 for delete',
788 'contact_type_a' => 'Individual',
789 'contact_type_b' => 'Organization',
791 $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
792 'id is not a valid integer');
796 * check with valid data with contact_b
798 function testGetRelationshipWithContactB() {
800 'contact_id_a' => $this->_cId_a
,
801 'contact_id_b' => $this->_cId_b
,
802 'relationship_type_id' => $this->_relTypeID
,
803 'start_date' => '2011-01-01',
804 'end_date' => '2013-01-01',
808 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
811 'contact_id' => $this->_cId_a
,
814 $result = $this->callAPISuccess('relationship', 'get', $contacts);
815 $this->assertGreaterThan(0, $result['count']);
817 'id' => $relationship['id'],
819 $result = $this->callAPISuccess('relationship', 'delete', $params);
820 $this->relationshipTypeDelete($this->_relTypeID
);
824 * check with valid data with relationshipTypes
826 function testGetRelationshipWithRelTypes() {
828 'contact_id_a' => $this->_cId_a
,
829 'contact_id_b' => $this->_cId_b
,
830 'relationship_type_id' => $this->_relTypeID
,
831 'start_date' => '2011-01-01',
832 'end_date' => '2013-01-01',
836 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
839 'contact_id' => $this->_cId_a
,
841 $result = $this->callAPISuccess('relationship', 'get', $contact_a);
844 'id' => $relationship['id'],
846 $result = $this->callAPISuccess('relationship', 'delete', $params);
847 $this->relationshipTypeDelete($this->_relTypeID
);
851 * Checks that passing in 'contact_id' + a relationship type
852 * will filter by relationship type (relationships go in both directions)
853 * as relationship api does a reciprocal check if contact_id provided
855 * We should get 1 result without or with correct relationship type id & 0 with
858 function testGetRelationshipByTypeReciprocal() {
859 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
860 $result = $this->callAPISuccess($this->_entity
, 'get', array(
861 'contact_id' => $this->_cId_a
,
862 'relationship_type_id' => $this->_relTypeID
,
864 $this->assertEquals(1, $result['count']);
865 $result = $this->callAPISuccess($this->_entity
, 'get', array(
866 'contact_id' => $this->_cId_a
,
867 'relationship_type_id' => $this->_relTypeID +
1,
869 $this->assertEquals(0, $result['count']);
870 $this->callAPISuccess($this->_entity
, 'delete', array('id' => $created['id']));
874 * Checks that passing in 'contact_id_b' + a relationship type
875 * will filter by relationship type for contact b
877 * We should get 1 result without or with correct relationship type id & 0 with
880 function testGetRelationshipByTypeDAO() {
881 $this->ids
['relationship'] = $this->callAPISuccess($this->_entity
, 'create', array('format.only_id' => TRUE,) +
$this->_params
);
882 $result = $this->callAPISuccess($this->_entity
, 'getcount', array(
883 'contact_id_a' => $this->_cId_a
,),
885 $result = $this->callAPISuccess($this->_entity
, 'get', array(
886 'contact_id_a' => $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_a' => $this->_cId_a
,
892 'relationship_type_id' => $this->_relTypeID +
1,
894 $this->assertEquals(0, $result['count']);
898 * Checks that passing in 'contact_id_b' + a relationship type
899 * will filter by relationship type for contact b
901 * We should get 1 result without or with correct relationship type id & 0 with
904 function testGetRelationshipByTypeArrayDAO() {
905 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
906 $org3 = $this->organizationCreate();
907 $relType2 = 5; // lets just assume built in ones aren't being messed with!
908 $relType3 = 6; // lets just assume built in ones aren't being messed with!
911 $this->callAPISuccess($this->_entity
, 'create',
912 array_merge($this->_params
, array(
913 'relationship_type_id' => $relType2,
914 'contact_id_b' => $this->_cId_b2
))
918 $this->callAPISuccess($this->_entity
, 'create',
919 array_merge($this->_params
, array(
920 'relationship_type_id' => $relType3,
921 'contact_id_b' => $org3))
924 $result = $this->callAPISuccess($this->_entity
, 'get', array(
925 'contact_id_a' => $this->_cId_a
,
926 'relationship_type_id' => array('IN' => array($this->_relTypeID
, $relType3)),
929 $this->assertEquals(2, $result['count']);
930 foreach ($result['values'] as $key => $value) {
931 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID
, $relType3)));
936 * Checks that passing in 'contact_id_b' + a relationship type
937 * will filter by relationship type for contact b
939 * We should get 1 result without or with correct relationship type id & 0 with
942 function testGetRelationshipByTypeArrayReciprocal() {
943 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
944 $org3 = $this->organizationCreate();
945 $relType2 = 5; // lets just assume built in ones aren't being messed with!
946 $relType3 = 6; // lets just assume built in ones aren't being messed with!
949 $this->callAPISuccess($this->_entity
, 'create',
950 array_merge($this->_params
, array(
951 'relationship_type_id' => $relType2,
952 'contact_id_b' => $this->_cId_b2
))
956 $this->callAPISuccess($this->_entity
, 'create',
957 array_merge($this->_params
, array(
958 'relationship_type_id' => $relType3,
959 'contact_id_b' => $org3))
962 $result = $this->callAPISuccess($this->_entity
, 'get', array(
963 'contact_id' => $this->_cId_a
,
964 'relationship_type_id' => array('IN' => array($this->_relTypeID
, $relType3)),
967 $this->assertEquals(2, $result['count']);
968 foreach ($result['values'] as $key => $value) {
969 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID
, $relType3)));
974 * Checks that passing in 'contact_id_b' + a relationship type
975 * will filter by relationship type for contact b
977 * We should get 1 result without or with correct relationship type id & 0 with
980 function testGetRelationshipByMembershipTypeDAO() {
981 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
982 $org3 = $this->organizationCreate();
984 $relType2 = 5; // lets just assume built in ones aren't being messed with!
985 $relType3 = 6; // lets just assume built in ones aren't being messed with!
987 $memberType = $this->membershipTypeCreate(array(
988 'relationship_type_id' => CRM_Core_DAO
::VALUE_SEPARATOR
. $relType1 . CRM_Core_DAO
::VALUE_SEPARATOR
. $relType3 . CRM_Core_DAO
::VALUE_SEPARATOR
,
989 'relationship_direction' => CRM_Core_DAO
::VALUE_SEPARATOR
. 'a_b' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'b_a' . CRM_Core_DAO
::VALUE_SEPARATOR
,
993 $this->callAPISuccess($this->_entity
, 'create',
994 array_merge($this->_params
, array(
995 'relationship_type_id' => $relType2,
996 'contact_id_b' => $this->_cId_b2
))
1000 $this->callAPISuccess($this->_entity
, 'create',
1001 array_merge($this->_params
, array(
1002 'relationship_type_id' => $relType3,
1003 'contact_id_b' => $org3))
1006 //relationshp 4 with reveral
1007 $this->callAPISuccess($this->_entity
, 'create',
1008 array_merge($this->_params
, array(
1009 'relationship_type_id' => $relType1,
1010 'contact_id_a' => $this->_cId_a
,
1011 'contact_id_b' => $this->_cId_a_2
))
1014 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1015 'contact_id_a' => $this->_cId_a
,
1016 'membership_type_id' => $memberType,
1018 // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1019 $this->assertEquals(1, $result['count']);
1020 foreach ($result['values'] as $key => $value) {
1021 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1)));
1026 * Checks that passing in 'contact_id_b' + a relationship type
1027 * will filter by relationship type for contact b
1029 * We should get 1 result without or with correct relationship type id & 0 with
1032 function testGetRelationshipByMembershipTypeReciprocal() {
1033 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1034 $org3 = $this->organizationCreate();
1036 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1037 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1039 $memberType = $this->membershipTypeCreate(array(
1040 'relationship_type_id' => CRM_Core_DAO
::VALUE_SEPARATOR
. $relType1 . CRM_Core_DAO
::VALUE_SEPARATOR
. $relType3 . CRM_Core_DAO
::VALUE_SEPARATOR
,
1041 'relationship_direction' => CRM_Core_DAO
::VALUE_SEPARATOR
. 'a_b' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'b_a' . CRM_Core_DAO
::VALUE_SEPARATOR
,
1045 $this->callAPISuccess($this->_entity
, 'create',
1046 array_merge($this->_params
, array(
1047 'relationship_type_id' => $relType2,
1048 'contact_id_b' => $this->_cId_b2
))
1052 $this->callAPISuccess($this->_entity
, 'create',
1053 array_merge($this->_params
, array(
1054 'relationship_type_id' => $relType3,
1055 'contact_id_b' => $org3))
1058 //relationshp 4 with reveral
1059 $this->callAPISuccess($this->_entity
, 'create',
1060 array_merge($this->_params
, array(
1061 'relationship_type_id' => $relType1,
1062 'contact_id_a' => $this->_cId_a
,
1063 'contact_id_b' => $this->_cId_a_2
))
1066 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1067 'contact_id' => $this->_cId_a
,
1068 'membership_type_id' => $memberType,
1070 // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1071 $this->assertEquals(2, $result['count']);
1073 foreach ($result['values'] as $key => $value) {
1074 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1, $relType3)));