3 * +--------------------------------------------------------------------+
4 * | CiviCRM version 4.6 |
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;
52 public function setUp() {
54 $this->_cId_a
= $this->individualCreate();
55 $this->_cId_a_2
= $this->individualCreate(array(
58 'contact_type' => 'Individual',
60 $this->_cId_b
= $this->organizationCreate();
61 $this->_cId_b2
= $this->organizationCreate(array('organization_name' => ' Org 2'));
62 $this->_entity
= 'relationship';
63 //Create a relationship type
64 $relTypeParams = array(
65 'name_a_b' => 'Relation 1 for delete',
66 'name_b_a' => 'Relation 2 for delete',
67 'description' => 'Testing relationship type',
68 'contact_type_a' => 'Individual',
69 'contact_type_b' => 'Organization',
74 $this->_relTypeID
= $this->relationshipTypeCreate($relTypeParams);
75 $this->_params
= array(
76 'contact_id_a' => $this->_cId_a
,
77 'contact_id_b' => $this->_cId_b
,
78 'relationship_type_id' => $this->_relTypeID
,
79 'start_date' => '2008-12-20',
85 public function tearDown() {
86 $this->contactDelete($this->_cId_a
);
87 $this->contactDelete($this->_cId_a_2
);
88 $this->contactDelete($this->_cId_b
);
89 $this->contactDelete($this->_cId_b2
);
90 $this->quickCleanup(array('civicrm_relationship'), TRUE);
91 $this->relationshipTypeDelete($this->_relTypeID
);
94 ///////////////// civicrm_relationship_create methods
97 * Check with empty array
99 public function testRelationshipCreateEmpty() {
100 $this->callAPIFailure('relationship', 'create', array());
104 * Check if required fields are not passed
106 public function testRelationshipCreateWithoutRequired() {
108 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
109 'end_date' => array('d' => '10', 'M' => '1', 'Y' => '2009'),
113 $this->callAPIFailure('relationship', 'create', $params);
117 * Check with incorrect required fields
119 public function testRelationshipCreateWithIncorrectData() {
122 'contact_id_a' => $this->_cId_a
,
123 'contact_id_b' => $this->_cId_b
,
124 'relationship_type_id' => 'Breaking Relationship',
127 $this->callAPIFailure('relationship', 'create', $params);
129 //contact id is not an integer
131 'contact_id_a' => 'invalid',
132 'contact_id_b' => $this->_cId_b
,
133 'relationship_type_id' => $this->_relTypeID
,
134 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
137 $this->callAPIFailure('relationship', 'create', $params);
139 //contact id does not exists
140 $params['contact_id_a'] = 999;
141 $this->callAPIFailure('relationship', 'create', $params);
144 $params['contact_id_a'] = $this->_cId_a
;
145 $params['start_date'] = array('d' => '1', 'M' => '1');
146 $this->callAPIFailure('relationship', 'create', $params);
150 * Check relationship creation with invalid Relationship
152 public function testRelationshipCreatInvalidRelationship() {
153 // both the contact of type Individual
155 'contact_id_a' => $this->_cId_a
,
156 'contact_id_b' => $this->_cId_a
,
157 'relationship_type_id' => $this->_relTypeID
,
158 'start_date' => '2008-01-10',
162 $this->callAPIFailure('relationship', 'create', $params);
164 // both the contact of type Organization
166 'contact_id_a' => $this->_cId_b
,
167 'contact_id_b' => $this->_cId_b
,
168 'relationship_type_id' => $this->_relTypeID
,
169 'start_date' => '2008-01-10',
173 $this->callAPIFailure('relationship', 'create', $params);
177 * Check relationship already exists
179 public function testRelationshipCreateAlreadyExists() {
181 'contact_id_a' => $this->_cId_a
,
182 'contact_id_b' => $this->_cId_b
,
183 'relationship_type_id' => $this->_relTypeID
,
184 'start_date' => '2008-12-20',
188 $relationship = $this->callAPISuccess('relationship', 'create', $params);
191 'contact_id_a' => $this->_cId_a
,
192 'contact_id_b' => $this->_cId_b
,
193 'relationship_type_id' => $this->_relTypeID
,
194 'start_date' => '2008-12-20',
197 $result = $this->callAPIFailure('relationship', 'create', $params, 'Relationship already exists');
199 $params['id'] = $relationship['id'];
200 $result = $this->callAPISuccess('relationship', 'delete', $params);
204 * Check relationship already exists
206 public function testRelationshipCreateUpdateAlreadyExists() {
208 'contact_id_a' => $this->_cId_a
,
209 'contact_id_b' => $this->_cId_b
,
210 'relationship_type_id' => $this->_relTypeID
,
211 'start_date' => '2008-12-20',
216 $relationship = $this->callAPISuccess('relationship', 'create', $params);
218 'id' => $relationship['id'],
222 $result = $this->callAPISuccess('relationship', 'create', $params);
223 $result = $this->callAPISuccess('relationship', 'get', $params);
224 $params['id'] = $relationship['id'];
225 $result = $this->callAPISuccess('relationship', 'delete', $params);
229 * Checkupdate doesn't reset stuff badly - CRM-11789
231 public function testRelationshipCreateUpdateDoesntMangle() {
233 'contact_id_a' => $this->_cId_a
,
234 'contact_id_b' => $this->_cId_b
,
235 'relationship_type_id' => $this->_relTypeID
,
236 'start_date' => '2008-12-20',
238 'is_permission_a_b' => 1,
239 'description' => 'my desc',
241 $relationship = $this->callAPISuccess('relationship', 'create', $params);
243 $updateparams = array(
244 'id' => $relationship['id'],
245 'relationship_type_id' => $this->_relTypeID
,
247 $result = $this->callAPISuccess('relationship', 'create', $updateparams);
249 //make sure the orig params didn't get changed
250 $this->getAndCheck($params, $relationship['id'], 'relationship');
256 * Check relationship creation
258 public function testRelationshipCreate() {
260 'contact_id_a' => $this->_cId_a
,
261 'contact_id_b' => $this->_cId_b
,
262 'relationship_type_id' => $this->_relTypeID
,
263 'start_date' => '2010-10-30',
264 'end_date' => '2010-12-30',
269 $result = $this->callAPIAndDocument('relationship', 'create', $params, __FUNCTION__
, __FILE__
);
270 $this->assertNotNull($result['id']);
271 $relationParams = array(
272 'id' => $result['id'],
275 // assertDBState compares expected values in $result to actual values in the DB
276 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
277 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
278 $values = $result['values'][$result['id']];
279 foreach ($params as $key => $value) {
280 if ($key == 'note') {
283 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE));
285 $params['id'] = $result['id'];
286 $this->callAPISuccess('relationship', 'delete', $params);
290 * Ensure disabling works
292 public function testRelationshipUpdate() {
293 $result = $this->callAPISuccess('relationship', 'create', $this->_params
);
294 $relID = $result['id'];
295 $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'description' => 'blah'));
296 $this->assertEquals($relID, $result['id']);
298 $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 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
304 $this->assertEquals(0, $result['values'][$result['id']]['is_active']);
305 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
306 $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
310 * Check relationship creation
312 public function testRelationshipCreateEmptyEndDate() {
314 'contact_id_a' => $this->_cId_a
,
315 'contact_id_b' => $this->_cId_b
,
316 'relationship_type_id' => $this->_relTypeID
,
317 'start_date' => '2010-10-30',
323 $result = $this->callAPISuccess('relationship', 'create', $params);
324 $this->assertNotNull($result['id']);
325 $relationParams = array(
326 'id' => $result['id'],
329 // assertDBState compares expected values in $result to actual values in the DB
330 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
331 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
332 $values = $result['values'][$result['id']];
333 foreach ($params as $key => $value) {
334 if ($key == 'note') {
337 if ($key == 'end_date') {
338 $this->assertTrue(empty($values[$key]));
341 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__
);
343 $params['id'] = $result['id'];
344 $this->callAPISuccess('relationship', 'delete', $params);
348 * Check relationship creation with custom data
350 public function testRelationshipCreateWithCustomData() {
351 $customGroup = $this->createCustomGroup();
352 $this->_ids
= $this->createCustomField();
353 //few custom Values for comparing
354 $custom_params = array(
355 "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship',
356 "custom_{$this->_ids[1]}" => 'Y',
357 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
358 "custom_{$this->_ids[3]}" => 'http://example.com',
362 'contact_id_a' => $this->_cId_a
,
363 'contact_id_b' => $this->_cId_b
,
364 'relationship_type_id' => $this->_relTypeID
,
365 'start_date' => '2008-12-20',
368 $params = array_merge($params, $custom_params);
369 $result = $this->callAPISuccess('relationship', 'create', $params);
371 $relationParams = array(
372 'id' => $result['id'],
374 // assertDBState compares expected values in $result to actual values in the DB
375 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
377 $params['id'] = $result['id'];
378 $result = $this->callAPISuccess('relationship', 'delete', $params);
379 $this->relationshipTypeDelete($this->_relTypeID
);
383 * Check with complete array + custom field
384 * Note that the test is written on purpose without any
385 * variables specific to participant so it can be replicated into other entities
386 * and / or moved to the automated test suite
388 public function testGetWithCustom() {
389 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
391 $params = $this->_params
;
392 $params['custom_' . $ids['custom_field_id']] = "custom string";
394 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
395 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
397 $getParams = array('id' => $result['id']);
398 $check = $this->callAPIAndDocument($this->_entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
399 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
401 $this->customFieldDelete($ids['custom_field_id']);
402 $this->customGroupDelete($ids['custom_group_id']);
408 public function createCustomGroup() {
410 'title' => 'Custom Group',
411 'extends' => array('Relationship'),
417 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
418 $this->_customGroupId
= $customGroup['id'];
419 return $customGroup['id'];
425 public function createCustomField() {
428 'custom_group_id' => $this->_customGroupId
,
429 'label' => 'Enter text about relationship',
430 'html_type' => 'Text',
431 'data_type' => 'String',
432 'default_value' => 'xyz',
435 'is_searchable' => 0,
439 $result = $this->callAPISuccess('CustomField', 'create', $params);
442 $ids[] = $customField['result']['customFieldId'];
444 $optionValue[] = array(
450 $optionValue[] = array(
456 $optionValue[] = array(
464 'label' => 'Pick Color',
465 'html_type' => 'Select',
466 'data_type' => 'String',
469 'is_searchable' => 0,
471 'option_values' => $optionValue,
472 'custom_group_id' => $this->_customGroupId
,
475 $customField = $this->callAPISuccess('custom_field', 'create', $params);
476 $ids[] = $customField['id'];
479 'custom_group_id' => $this->_customGroupId
,
480 'name' => 'test_date',
481 'label' => 'test_date',
482 'html_type' => 'Select Date',
483 'data_type' => 'Date',
484 'default_value' => '20090711',
487 'is_searchable' => 0,
491 $customField = $this->callAPISuccess('custom_field', 'create', $params);
493 $ids[] = $customField['id'];
495 'custom_group_id' => $this->_customGroupId
,
496 'name' => 'test_link',
497 'label' => 'test_link',
498 'html_type' => 'Link',
499 'data_type' => 'Link',
500 'default_value' => 'http://civicrm.org',
503 'is_searchable' => 0,
507 $customField = $this->callAPISuccess('custom_field', 'create', $params);
508 $ids[] = $customField['id'];
512 ///////////////// civicrm_relationship_delete methods
515 * Check with empty array
517 public function testRelationshipDeleteEmpty() {
519 $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
523 * Check if required fields are not passed
525 public function testRelationshipDeleteWithoutRequired() {
527 'start_date' => '2008-12-20',
528 'end_date' => '2009-12-20',
532 $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
536 * Check with incorrect required fields
538 public function testRelationshipDeleteWithIncorrectData() {
540 'contact_id_a' => $this->_cId_a
,
541 'contact_id_b' => $this->_cId_b
,
542 'relationship_type_id' => 'Breaking Relationship',
545 $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
547 $params['id'] = "Invalid";
548 $result = $this->callAPIFailure('relationship', 'delete', $params, 'Invalid value for relationship ID');
552 * Check relationship creation
554 public function testRelationshipDelete() {
556 'contact_id_a' => $this->_cId_a
,
557 'contact_id_b' => $this->_cId_b
,
558 'relationship_type_id' => $this->_relTypeID
,
559 'start_date' => '2008-12-20',
563 $result = $this->callAPISuccess('relationship', 'create', $params);
565 //Delete relationship
567 $params['id'] = $result['id'];
569 $result = $this->callAPIAndDocument('relationship', 'delete', $params, __FUNCTION__
, __FILE__
);
570 $this->relationshipTypeDelete($this->_relTypeID
);
573 ///////////////// civicrm_relationship_update methods
576 * Check with empty array
578 public function testRelationshipUpdateEmpty() {
579 $result = $this->callAPIFailure('relationship', 'create', array(),
580 'Mandatory key(s) missing from params array: contact_id_a, contact_id_b, relationship_type_id');
584 * Check if required fields are not passed
588 * Check relationship update
590 public function testRelationshipCreateDuplicate() {
592 'contact_id_a' => $this->_cId_a
,
593 'contact_id_b' => $this->_cId_b
,
594 'relationship_type_id' => $this->_relTypeID
,
595 'start_date' => '20081214',
596 'end_date' => '20091214',
600 $result = $this->callAPISuccess('relationship', 'create', $relParams);
602 $this->assertNotNull($result['id']);
603 $this->_relationID
= $result['id'];
606 'contact_id_a' => $this->_cId_a
,
607 'contact_id_b' => $this->_cId_b
,
608 'relationship_type_id' => $this->_relTypeID
,
609 'start_date' => '20081214',
610 'end_date' => '20091214',
614 $result = $this->callAPIFailure('relationship', 'create', $params, 'Relationship already exists');
616 //delete created relationship
618 'id' => $this->_relationID
,
621 $result = $this->callAPISuccess('relationship', 'delete', $params);
623 //delete created relationship type
624 $this->relationshipTypeDelete($this->_relTypeID
);
628 * Check with valid params array.
630 public function testRelationshipsGet() {
632 'contact_id_a' => $this->_cId_a
,
633 'contact_id_b' => $this->_cId_b
,
634 'relationship_type_id' => $this->_relTypeID
,
635 'start_date' => '2011-01-01',
636 'end_date' => '2013-01-01',
640 $result = $this->callAPISuccess('relationship', 'create', $relParams);
644 'contact_id' => $this->_cId_b
,
646 $result = $this->callAPISuccess('relationship', 'get', $params);
647 $this->assertEquals($result['count'], 1);
649 'contact_id_a' => $this->_cId_a
,
651 $result = $this->callAPISuccess('relationship', 'get', $params);
652 $this->assertEquals($result['count'], 1);
653 // contact_id_a is wrong so should be no matches
655 'contact_id_a' => $this->_cId_b
,
657 $result = $this->callAPISuccess('relationship', 'get', $params);
658 $this->assertEquals($result['count'], 0);
662 * Check with valid params array.
663 * (The get function will behave differently without 'contact_id' passed
665 public function testRelationshipsGetGeneric() {
667 'contact_id_a' => $this->_cId_a
,
668 'contact_id_b' => $this->_cId_b
,
669 'relationship_type_id' => $this->_relTypeID
,
670 'start_date' => '2011-01-01',
671 'end_date' => '2013-01-01',
675 $result = $this->callAPISuccess('relationship', 'create', $relParams);
679 'contact_id_b' => $this->_cId_b
,
681 $result = $this->callAPISuccess('relationship', 'get', $params);
684 public function testGetIsCurrent() {
686 'contact_id_a' => $this->_cId_a
,
687 'contact_id_b' => $this->_cId_b2
,
688 'relationship_type_id' => $this->_relTypeID
,
689 'start_date' => '2008-12-20',
692 $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params);
693 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params
);
696 'filters' => array('is_current' => 1),
698 $description = "demonstrates is_current filter";
699 $subfile = 'filterIsCurrent';
700 //no relationship has been created
701 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
702 $this->assertEquals($result['count'], 1);
703 $this->AssertEquals($rel1['id'], $result['id']);
705 // now try not started
706 $rel2Params['is_active'] = 1;
707 $rel2Params['start_date'] = 'tomorrow';
708 $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params);
709 $result = $this->callAPISuccess('relationship', 'get', $getParams);
710 $this->assertEquals($result['count'], 1);
711 $this->AssertEquals($rel1['id'], $result['id']);
714 $rel2Params['is_active'] = 1;
715 $rel2Params['start_date'] = 'last week';
716 $rel2Params['end_date'] = 'yesterday';
717 $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params);
721 * Test using various operators
723 public function testGetTypeOperators() {
724 $relTypeParams = array(
725 'name_a_b' => 'Relation 3 for delete',
726 'name_b_a' => 'Relation 6 for delete',
727 'description' => 'Testing relationship type 2',
728 'contact_type_a' => 'Individual',
729 'contact_type_b' => 'Organization',
733 $relationType2 = $this->relationshipTypeCreate($relTypeParams);
734 $relTypeParams = array(
735 'name_a_b' => 'Relation 8 for delete',
736 'name_b_a' => 'Relation 9 for delete',
737 'description' => 'Testing relationship type 7',
738 'contact_type_a' => 'Individual',
739 'contact_type_b' => 'Organization',
743 $relationType3 = $this->relationshipTypeCreate($relTypeParams);
745 $relTypeParams = array(
746 'name_a_b' => 'Relation 6 for delete',
747 'name_b_a' => 'Relation 88for delete',
748 'description' => 'Testing relationship type 00',
749 'contact_type_a' => 'Individual',
750 'contact_type_b' => 'Organization',
754 $relationType4 = $this->relationshipTypeCreate($relTypeParams);
756 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params
);
757 $rel2 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
758 array('relationship_type_id' => $relationType2)));
759 $rel3 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
760 array('relationship_type_id' => $relationType3)));
761 $rel4 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
762 array('relationship_type_id' => $relationType4)));
765 'relationship_type_id' => array('IN' => array($relationType2, $relationType3)),
768 $description = "demonstrates use of IN filter";
769 $subfile = 'INRelationshipType';
771 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
772 $this->assertEquals($result['count'], 2);
773 $this->AssertEquals(array($rel2['id'], $rel3['id']), array_keys($result['values']));
775 $description = "demonstrates use of NOT IN filter";
776 $subfile = 'NotInRelationshipType';
778 'relationship_type_id' => array('NOT IN' => array($relationType2, $relationType3)),
780 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
781 $this->assertEquals($result['count'], 2);
782 $this->AssertEquals(array($rel1['id'], $rel4['id']), array_keys($result['values']));
784 $description = "demonstrates use of BETWEEN filter";
785 $subfile = 'BetweenRelationshipType';
787 'relationship_type_id' => array('BETWEEN' => array($relationType2, $relationType4)),
789 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
790 $this->assertEquals($result['count'], 3);
791 $this->AssertEquals(array($rel2['id'], $rel3['id'], $rel4['id']), array_keys($result['values']));
793 $description = "demonstrates use of Not BETWEEN filter";
794 $subfile = 'NotBetweenRelationshipType';
796 'relationship_type_id' => array('NOT BETWEEN' => array($relationType2, $relationType4)),
798 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
799 $this->assertEquals($result['count'], 1);
800 $this->AssertEquals(array($rel1['id']), array_keys($result['values']));
805 * Check with invalid relationshipType Id
807 public function testRelationshipTypeAddInvalidId() {
808 $relTypeParams = array(
810 'name_a_b' => 'Relation 1 for delete',
811 'name_b_a' => 'Relation 2 for delete',
812 'contact_type_a' => 'Individual',
813 'contact_type_b' => 'Organization',
815 $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
816 'id is not a valid integer');
820 * Check with valid data with contact_b
822 public function testGetRelationshipWithContactB() {
824 'contact_id_a' => $this->_cId_a
,
825 'contact_id_b' => $this->_cId_b
,
826 'relationship_type_id' => $this->_relTypeID
,
827 'start_date' => '2011-01-01',
828 'end_date' => '2013-01-01',
832 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
835 'contact_id' => $this->_cId_a
,
838 $result = $this->callAPISuccess('relationship', 'get', $contacts);
839 $this->assertGreaterThan(0, $result['count']);
841 'id' => $relationship['id'],
843 $result = $this->callAPISuccess('relationship', 'delete', $params);
844 $this->relationshipTypeDelete($this->_relTypeID
);
848 * Check with valid data with relationshipTypes
850 public function testGetRelationshipWithRelTypes() {
852 'contact_id_a' => $this->_cId_a
,
853 'contact_id_b' => $this->_cId_b
,
854 'relationship_type_id' => $this->_relTypeID
,
855 'start_date' => '2011-01-01',
856 'end_date' => '2013-01-01',
860 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
863 'contact_id' => $this->_cId_a
,
865 $result = $this->callAPISuccess('relationship', 'get', $contact_a);
868 'id' => $relationship['id'],
870 $result = $this->callAPISuccess('relationship', 'delete', $params);
871 $this->relationshipTypeDelete($this->_relTypeID
);
875 * Checks that passing in 'contact_id' + a relationship type
876 * will filter by relationship type (relationships go in both directions)
877 * as relationship api does a reciprocal check if contact_id provided
879 * We should get 1 result without or with correct relationship type id & 0 with
882 public function testGetRelationshipByTypeReciprocal() {
883 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
884 $result = $this->callAPISuccess($this->_entity
, 'get', array(
885 'contact_id' => $this->_cId_a
,
886 'relationship_type_id' => $this->_relTypeID
,
888 $this->assertEquals(1, $result['count']);
889 $result = $this->callAPISuccess($this->_entity
, 'get', array(
890 'contact_id' => $this->_cId_a
,
891 'relationship_type_id' => $this->_relTypeID +
1,
893 $this->assertEquals(0, $result['count']);
894 $this->callAPISuccess($this->_entity
, 'delete', array('id' => $created['id']));
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 public function testGetRelationshipByTypeDAO() {
905 $this->ids
['relationship'] = $this->callAPISuccess($this->_entity
, 'create', array('format.only_id' => TRUE) +
$this->_params
);
906 $result = $this->callAPISuccess($this->_entity
, 'getcount', array(
907 '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 public 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
,
944 $this->callAPISuccess($this->_entity
, 'create',
945 array_merge($this->_params
, array(
946 'relationship_type_id' => $relType3,
947 'contact_id_b' => $org3,
951 $result = $this->callAPISuccess($this->_entity
, 'get', array(
952 'contact_id_a' => $this->_cId_a
,
953 'relationship_type_id' => array('IN' => array($this->_relTypeID
, $relType3)),
956 $this->assertEquals(2, $result['count']);
957 foreach ($result['values'] as $key => $value) {
958 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID
, $relType3)));
963 * Checks that passing in 'contact_id_b' + a relationship type
964 * will filter by relationship type for contact b
966 * We should get 1 result without or with correct relationship type id & 0 with
969 public function testGetRelationshipByTypeArrayReciprocal() {
970 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
971 $org3 = $this->organizationCreate();
972 $relType2 = 5; // lets just assume built in ones aren't being messed with!
973 $relType3 = 6; // lets just assume built in ones aren't being messed with!
976 $this->callAPISuccess($this->_entity
, 'create',
977 array_merge($this->_params
, array(
978 'relationship_type_id' => $relType2,
979 'contact_id_b' => $this->_cId_b2
,
984 $this->callAPISuccess($this->_entity
, 'create',
985 array_merge($this->_params
, array(
986 'relationship_type_id' => $relType3,
987 'contact_id_b' => $org3,
991 $result = $this->callAPISuccess($this->_entity
, 'get', array(
992 'contact_id' => $this->_cId_a
,
993 'relationship_type_id' => array('IN' => array($this->_relTypeID
, $relType3)),
996 $this->assertEquals(2, $result['count']);
997 foreach ($result['values'] as $key => $value) {
998 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID
, $relType3)));
1003 * Checks that passing in 'contact_id_b' + a relationship type
1004 * will filter by relationship type for contact b
1006 * We should get 1 result without or with correct relationship type id & 0 with
1009 public function testGetRelationshipByMembershipTypeDAO() {
1010 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1011 $org3 = $this->organizationCreate();
1013 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1014 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1016 $memberType = $this->membershipTypeCreate(array(
1017 'relationship_type_id' => CRM_Core_DAO
::VALUE_SEPARATOR
. $relType1 . CRM_Core_DAO
::VALUE_SEPARATOR
. $relType3 . CRM_Core_DAO
::VALUE_SEPARATOR
,
1018 'relationship_direction' => CRM_Core_DAO
::VALUE_SEPARATOR
. 'a_b' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'b_a' . CRM_Core_DAO
::VALUE_SEPARATOR
,
1022 $this->callAPISuccess($this->_entity
, 'create',
1023 array_merge($this->_params
, array(
1024 'relationship_type_id' => $relType2,
1025 'contact_id_b' => $this->_cId_b2
,
1030 $this->callAPISuccess($this->_entity
, 'create',
1031 array_merge($this->_params
, array(
1032 'relationship_type_id' => $relType3,
1033 'contact_id_b' => $org3,
1037 //relationshp 4 with reveral
1038 $this->callAPISuccess($this->_entity
, 'create',
1039 array_merge($this->_params
, array(
1040 'relationship_type_id' => $relType1,
1041 'contact_id_a' => $this->_cId_a
,
1042 'contact_id_b' => $this->_cId_a_2
,
1046 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1047 'contact_id_a' => $this->_cId_a
,
1048 'membership_type_id' => $memberType,
1050 // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1051 $this->assertEquals(1, $result['count']);
1052 foreach ($result['values'] as $key => $value) {
1053 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1)));
1058 * Checks that passing in 'contact_id_b' + a relationship type
1059 * will filter by relationship type for contact b
1061 * We should get 1 result without or with correct relationship type id & 0 with
1064 public function testGetRelationshipByMembershipTypeReciprocal() {
1065 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1066 $org3 = $this->organizationCreate();
1068 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1069 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1071 $memberType = $this->membershipTypeCreate(array(
1072 'relationship_type_id' => CRM_Core_DAO
::VALUE_SEPARATOR
. $relType1 . CRM_Core_DAO
::VALUE_SEPARATOR
. $relType3 . CRM_Core_DAO
::VALUE_SEPARATOR
,
1073 'relationship_direction' => CRM_Core_DAO
::VALUE_SEPARATOR
. 'a_b' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'b_a' . CRM_Core_DAO
::VALUE_SEPARATOR
,
1077 $this->callAPISuccess($this->_entity
, 'create',
1078 array_merge($this->_params
, array(
1079 'relationship_type_id' => $relType2,
1080 'contact_id_b' => $this->_cId_b2
,
1085 $this->callAPISuccess($this->_entity
, 'create',
1086 array_merge($this->_params
, array(
1087 'relationship_type_id' => $relType3,
1088 'contact_id_b' => $org3,
1092 //relationshp 4 with reveral
1093 $this->callAPISuccess($this->_entity
, 'create',
1094 array_merge($this->_params
, array(
1095 'relationship_type_id' => $relType1,
1096 'contact_id_a' => $this->_cId_a
,
1097 'contact_id_b' => $this->_cId_a_2
,
1101 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1102 'contact_id' => $this->_cId_a
,
1103 'membership_type_id' => $memberType,
1105 // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1106 $this->assertEquals(2, $result['count']);
1108 foreach ($result['values'] as $key => $value) {
1109 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1, $relType3)));
1114 * Check for enotices on enable & disable as reported in CRM-14350
1116 public function testSetActive() {
1117 $relationship = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1118 $this->callAPISuccess($this->_entity
, 'create', array('id' => $relationship['id'], 'is_active' => 0));
1119 $this->callAPISuccess($this->_entity
, 'create', array('id' => $relationship['id'], 'is_active' => 1));