3a8c10e023a9cd0eefc159838e59caf17ea58074
3 * +--------------------------------------------------------------------+
4 * | CiviCRM version 4.7 |
5 * +--------------------------------------------------------------------+
6 * | Copyright CiviCRM LLC (c) 2004-2015 |
7 * +--------------------------------------------------------------------+
8 * | This file is a part of CiviCRM. |
10 * | CiviCRM is free software; you can copy, modify, and distribute it |
11 * | under the terms of the GNU Affero General Public License |
12 * | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 * | CiviCRM is distributed in the hope that it will be useful, but |
15 * | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 * | See the GNU Affero General Public License for more details. |
19 * | You should have received a copy of the GNU Affero General Public |
20 * | License and the CiviCRM Licensing Exception along |
21 * | with this program; if not, contact CiviCRM LLC |
22 * | at info[AT]civicrm[DOT]org. If you have questions about the |
23 * | GNU Affero General Public License or the licensing of CiviCRM, |
24 * | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 * +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
31 * Class contains api test cases for "civicrm_relationship"
33 class api_v3_RelationshipTest
extends CiviUnitTestCase
{
34 protected $_apiversion = 3;
43 * Second organization contact.
48 protected $_relTypeID;
49 protected $_ids = array();
50 protected $_customGroupId = NULL;
51 protected $_customFieldId = NULL;
59 public function setUp() {
61 $this->_cId_a
= $this->individualCreate();
62 $this->_cId_a_2
= $this->individualCreate(array(
65 'contact_type' => 'Individual',
67 $this->_cId_b
= $this->organizationCreate();
68 $this->_cId_b2
= $this->organizationCreate(array('organization_name' => ' Org 2'));
69 $this->_entity
= 'relationship';
70 //Create a relationship type.
71 $relTypeParams = array(
72 'name_a_b' => 'Relation 1 for delete',
73 'name_b_a' => 'Relation 2 for delete',
74 'description' => 'Testing relationship type',
75 'contact_type_a' => 'Individual',
76 'contact_type_b' => 'Organization',
81 $this->_relTypeID
= $this->relationshipTypeCreate($relTypeParams);
82 $this->_params
= array(
83 'contact_id_a' => $this->_cId_a
,
84 'contact_id_b' => $this->_cId_b
,
85 'relationship_type_id' => $this->_relTypeID
,
86 'start_date' => '2008-12-20',
97 public function tearDown() {
98 $this->contactDelete($this->_cId_a
);
99 $this->contactDelete($this->_cId_a_2
);
100 $this->contactDelete($this->_cId_b
);
101 $this->contactDelete($this->_cId_b2
);
102 $this->quickCleanup(array('civicrm_relationship'), TRUE);
103 $this->relationshipTypeDelete($this->_relTypeID
);
107 * Check with empty array.
109 public function testRelationshipCreateEmpty() {
110 $this->callAPIFailure('relationship', 'create', array());
114 * Check if required fields are not passed.
116 public function testRelationshipCreateWithoutRequired() {
118 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
119 'end_date' => array('d' => '10', 'M' => '1', 'Y' => '2009'),
123 $this->callAPIFailure('relationship', 'create', $params);
127 * Check with incorrect required fields.
129 public function testRelationshipCreateWithIncorrectData() {
132 'contact_id_a' => $this->_cId_a
,
133 'contact_id_b' => $this->_cId_b
,
134 'relationship_type_id' => 'Breaking Relationship',
137 $this->callAPIFailure('relationship', 'create', $params);
139 //contact id is not an integer
141 'contact_id_a' => 'invalid',
142 'contact_id_b' => $this->_cId_b
,
143 'relationship_type_id' => $this->_relTypeID
,
144 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
147 $this->callAPIFailure('relationship', 'create', $params);
149 // Contact id does not exist.
150 $params['contact_id_a'] = 999;
151 $this->callAPIFailure('relationship', 'create', $params);
154 $params['contact_id_a'] = $this->_cId_a
;
155 $params['start_date'] = array('d' => '1', 'M' => '1');
156 $this->callAPIFailure('relationship', 'create', $params);
160 * Check relationship creation with invalid Relationship.
162 public function testRelationshipCreateInvalidRelationship() {
163 // Both have the contact type Individual.
165 'contact_id_a' => $this->_cId_a
,
166 'contact_id_b' => $this->_cId_a
,
167 'relationship_type_id' => $this->_relTypeID
,
168 'start_date' => '2008-01-10',
172 $this->callAPIFailure('relationship', 'create', $params);
174 // both the contact of type Organization
176 'contact_id_a' => $this->_cId_b
,
177 'contact_id_b' => $this->_cId_b
,
178 'relationship_type_id' => $this->_relTypeID
,
179 'start_date' => '2008-01-10',
183 $this->callAPIFailure('relationship', 'create', $params);
187 * Check relationship already exists.
189 public function testRelationshipCreateAlreadyExists() {
191 'contact_id_a' => $this->_cId_a
,
192 'contact_id_b' => $this->_cId_b
,
193 'relationship_type_id' => $this->_relTypeID
,
194 'start_date' => '2008-12-20',
198 $relationship = $this->callAPISuccess('relationship', 'create', $params);
201 'contact_id_a' => $this->_cId_a
,
202 'contact_id_b' => $this->_cId_b
,
203 'relationship_type_id' => $this->_relTypeID
,
204 'start_date' => '2008-12-20',
207 $this->callAPIFailure('relationship', 'create', $params, 'Relationship already exists');
209 $params['id'] = $relationship['id'];
210 $this->callAPISuccess('relationship', 'delete', $params);
214 * Check relationship already exists.
216 public function testRelationshipCreateUpdateAlreadyExists() {
218 'contact_id_a' => $this->_cId_a
,
219 'contact_id_b' => $this->_cId_b
,
220 'relationship_type_id' => $this->_relTypeID
,
221 'start_date' => '2008-12-20',
226 $relationship = $this->callAPISuccess('relationship', 'create', $params);
228 'id' => $relationship['id'],
232 $this->callAPISuccess('relationship', 'create', $params);
233 $this->callAPISuccess('relationship', 'get', $params);
234 $params['id'] = $relationship['id'];
235 $this->callAPISuccess('relationship', 'delete', $params);
239 * Check update doesn't reset stuff badly - CRM-11789.
241 public function testRelationshipCreateUpdateDoesNotMangle() {
243 'contact_id_a' => $this->_cId_a
,
244 'contact_id_b' => $this->_cId_b
,
245 'relationship_type_id' => $this->_relTypeID
,
246 'start_date' => '2008-12-20',
248 'is_permission_a_b' => 1,
249 'description' => 'my desc',
251 $relationship = $this->callAPISuccess('relationship', 'create', $params);
253 $updateParams = array(
254 'id' => $relationship['id'],
255 'relationship_type_id' => $this->_relTypeID
,
257 $this->callAPISuccess('relationship', 'create', $updateParams);
259 //make sure the orig params didn't get changed
260 $this->getAndCheck($params, $relationship['id'], 'relationship');
266 * Check relationship creation.
268 public function testRelationshipCreate() {
270 'contact_id_a' => $this->_cId_a
,
271 'contact_id_b' => $this->_cId_b
,
272 'relationship_type_id' => $this->_relTypeID
,
273 'start_date' => '2010-10-30',
274 'end_date' => '2010-12-30',
279 $result = $this->callAPIAndDocument('relationship', 'create', $params, __FUNCTION__
, __FILE__
);
280 $this->assertNotNull($result['id']);
281 $relationParams = array(
282 'id' => $result['id'],
285 // assertDBState compares expected values in $result to actual values in the DB
286 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
287 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
288 $values = $result['values'][$result['id']];
289 foreach ($params as $key => $value) {
290 if ($key == 'note') {
293 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE));
295 $params['id'] = $result['id'];
296 $this->callAPISuccess('relationship', 'delete', $params);
300 * Ensure disabling works.
302 public function testRelationshipUpdate() {
303 $result = $this->callAPISuccess('relationship', 'create', $this->_params
);
304 $relID = $result['id'];
305 $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'description' => 'blah'));
306 $this->assertEquals($relID, $result['id']);
308 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
310 $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'is_permission_b_a' => 1));
311 $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
312 $result = $this->callAPISuccess('relationship', 'create', array('id' => $result['id'], 'is_active' => 0));
313 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
314 $this->assertEquals(0, $result['values'][$result['id']]['is_active']);
315 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
316 $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
320 * Check relationship creation.
322 public function testRelationshipCreateEmptyEndDate() {
324 'contact_id_a' => $this->_cId_a
,
325 'contact_id_b' => $this->_cId_b
,
326 'relationship_type_id' => $this->_relTypeID
,
327 'start_date' => '2010-10-30',
333 $result = $this->callAPISuccess('relationship', 'create', $params);
334 $this->assertNotNull($result['id']);
335 $relationParams = array(
336 'id' => $result['id'],
339 // assertDBState compares expected values in $result to actual values in the DB
340 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
341 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
342 $values = $result['values'][$result['id']];
343 foreach ($params as $key => $value) {
344 if ($key == 'note') {
347 if ($key == 'end_date') {
348 $this->assertTrue(empty($values[$key]));
351 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__
);
353 $params['id'] = $result['id'];
354 $this->callAPISuccess('relationship', 'delete', $params);
358 * Check relationship creation with custom data.
360 public function testRelationshipCreateWithCustomData() {
361 $this->createCustomGroup();
362 $this->_ids
= $this->createCustomField();
363 //few custom Values for comparing
364 $custom_params = array(
365 "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship',
366 "custom_{$this->_ids[1]}" => 'Y',
367 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
368 "custom_{$this->_ids[3]}" => 'http://example.com',
372 'contact_id_a' => $this->_cId_a
,
373 'contact_id_b' => $this->_cId_b
,
374 'relationship_type_id' => $this->_relTypeID
,
375 'start_date' => '2008-12-20',
378 $params = array_merge($params, $custom_params);
379 $result = $this->callAPISuccess('relationship', 'create', $params);
381 $relationParams = array(
382 'id' => $result['id'],
384 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
386 $params['id'] = $result['id'];
387 $this->callAPISuccess('relationship', 'delete', $params);
388 $this->relationshipTypeDelete($this->_relTypeID
);
392 * Check with complete array + custom field
393 * Note that the test is written on purpose without any
394 * variables specific to participant so it can be replicated into other entities
395 * and / or moved to the automated test suite
397 public function testGetWithCustom() {
398 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
400 $params = $this->_params
;
401 $params['custom_' . $ids['custom_field_id']] = "custom string";
403 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
404 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
406 $getParams = array('id' => $result['id']);
407 $check = $this->callAPIAndDocument($this->_entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
408 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
410 $this->customFieldDelete($ids['custom_field_id']);
411 $this->customGroupDelete($ids['custom_group_id']);
417 public function createCustomGroup() {
419 'title' => 'Custom Group',
420 'extends' => array('Relationship'),
426 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
427 $this->_customGroupId
= $customGroup['id'];
428 return $customGroup['id'];
434 public function createCustomField() {
437 'custom_group_id' => $this->_customGroupId
,
438 'label' => 'Enter text about relationship',
439 'html_type' => 'Text',
440 'data_type' => 'String',
441 'default_value' => 'xyz',
444 'is_searchable' => 0,
448 $this->callAPISuccess('CustomField', 'create', $params);
451 $ids[] = $customField['result']['customFieldId'];
453 $optionValue[] = array(
459 $optionValue[] = array(
465 $optionValue[] = array(
473 'label' => 'Pick Color',
474 'html_type' => 'Select',
475 'data_type' => 'String',
478 'is_searchable' => 0,
480 'option_values' => $optionValue,
481 'custom_group_id' => $this->_customGroupId
,
484 $customField = $this->callAPISuccess('custom_field', 'create', $params);
485 $ids[] = $customField['id'];
488 'custom_group_id' => $this->_customGroupId
,
489 'name' => 'test_date',
490 'label' => 'test_date',
491 'html_type' => 'Select Date',
492 'data_type' => 'Date',
493 'default_value' => '20090711',
496 'is_searchable' => 0,
500 $customField = $this->callAPISuccess('custom_field', 'create', $params);
502 $ids[] = $customField['id'];
504 'custom_group_id' => $this->_customGroupId
,
505 'name' => 'test_link',
506 'label' => 'test_link',
507 'html_type' => 'Link',
508 'data_type' => 'Link',
509 'default_value' => 'http://civicrm.org',
512 'is_searchable' => 0,
516 $customField = $this->callAPISuccess('custom_field', 'create', $params);
517 $ids[] = $customField['id'];
522 * Check with empty array.
524 public function testRelationshipDeleteEmpty() {
525 $this->callAPIFailure('relationship', 'delete', array(), 'Mandatory key(s) missing from params array: id');
529 * Check if required fields are not passed.
531 public function testRelationshipDeleteWithoutRequired() {
533 'start_date' => '2008-12-20',
534 'end_date' => '2009-12-20',
538 $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
542 * Check with incorrect required fields.
544 public function testRelationshipDeleteWithIncorrectData() {
546 'contact_id_a' => $this->_cId_a
,
547 'contact_id_b' => $this->_cId_b
,
548 'relationship_type_id' => 'Breaking Relationship',
551 $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
553 $params['id'] = "Invalid";
554 $this->callAPIFailure('relationship', 'delete', $params, 'Invalid value for relationship ID');
558 * Check relationship creation.
560 public function testRelationshipDelete() {
562 'contact_id_a' => $this->_cId_a
,
563 'contact_id_b' => $this->_cId_b
,
564 'relationship_type_id' => $this->_relTypeID
,
565 'start_date' => '2008-12-20',
569 $result = $this->callAPISuccess('relationship', 'create', $params);
570 $params = array('id' => $result['id']);
571 $this->callAPIAndDocument('relationship', 'delete', $params, __FUNCTION__
, __FILE__
);
572 $this->relationshipTypeDelete($this->_relTypeID
);
575 ///////////////// civicrm_relationship_update methods
578 * Check with empty array.
580 public function testRelationshipUpdateEmpty() {
581 $this->callAPIFailure('relationship', 'create', array(),
582 'Mandatory key(s) missing from params array: contact_id_a, contact_id_b, relationship_type_id');
586 * Check if required fields are not passed.
590 * Check relationship update.
592 public function testRelationshipCreateDuplicate() {
594 'contact_id_a' => $this->_cId_a
,
595 'contact_id_b' => $this->_cId_b
,
596 'relationship_type_id' => $this->_relTypeID
,
597 'start_date' => '20081214',
598 'end_date' => '20091214',
602 $result = $this->callAPISuccess('relationship', 'create', $relParams);
604 $this->assertNotNull($result['id']);
607 'contact_id_a' => $this->_cId_a
,
608 'contact_id_b' => $this->_cId_b
,
609 'relationship_type_id' => $this->_relTypeID
,
610 'start_date' => '20081214',
611 'end_date' => '20091214',
615 $this->callAPIFailure('relationship', 'create', $params, 'Relationship already exists');
617 $this->callAPISuccess('relationship', 'delete', array('id' => $result['id']));
618 $this->relationshipTypeDelete($this->_relTypeID
);
622 * Check with valid params array.
624 public function testRelationshipsGet() {
626 'contact_id_a' => $this->_cId_a
,
627 'contact_id_b' => $this->_cId_b
,
628 'relationship_type_id' => $this->_relTypeID
,
629 'start_date' => '2011-01-01',
630 'end_date' => '2013-01-01',
634 $this->callAPISuccess('relationship', 'create', $relParams);
638 'contact_id' => $this->_cId_b
,
640 $result = $this->callAPISuccess('relationship', 'get', $params);
641 $this->assertEquals($result['count'], 1);
643 'contact_id_a' => $this->_cId_a
,
645 $result = $this->callAPISuccess('relationship', 'get', $params);
646 $this->assertEquals($result['count'], 1);
647 // contact_id_a is wrong so should be no matches
649 'contact_id_a' => $this->_cId_b
,
651 $result = $this->callAPISuccess('relationship', 'get', $params);
652 $this->assertEquals($result['count'], 0);
656 * Check with valid params array.
657 * (The get function will behave differently without 'contact_id' passed
659 public function testRelationshipsGetGeneric() {
661 'contact_id_a' => $this->_cId_a
,
662 'contact_id_b' => $this->_cId_b
,
663 'relationship_type_id' => $this->_relTypeID
,
664 'start_date' => '2011-01-01',
665 'end_date' => '2013-01-01',
669 $this->callAPISuccess('relationship', 'create', $relParams);
673 'contact_id_b' => $this->_cId_b
,
675 $this->callAPISuccess('relationship', 'get', $params);
679 * Test retrieving only current relationships.
681 public function testGetIsCurrent() {
683 'contact_id_a' => $this->_cId_a
,
684 'contact_id_b' => $this->_cId_b2
,
685 'relationship_type_id' => $this->_relTypeID
,
686 'start_date' => '2008-12-20',
689 $this->callAPISuccess('relationship', 'create', $rel2Params);
690 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params
);
693 'filters' => array('is_current' => 1),
695 $description = "Demonstrates is_current filter.";
696 $subfile = 'filterIsCurrent';
697 //no relationship has been created
698 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
699 $this->assertEquals($result['count'], 1);
700 $this->AssertEquals($rel1['id'], $result['id']);
702 // now try not started
703 $rel2Params['is_active'] = 1;
704 $rel2Params['start_date'] = 'tomorrow';
705 $this->callAPISuccess('relationship', 'create', $rel2Params);
706 $result = $this->callAPISuccess('relationship', 'get', $getParams);
707 $this->assertEquals($result['count'], 1);
708 $this->AssertEquals($rel1['id'], $result['id']);
711 $rel2Params['is_active'] = 1;
712 $rel2Params['start_date'] = 'last week';
713 $rel2Params['end_date'] = 'yesterday';
714 $this->callAPISuccess('relationship', 'create', $rel2Params);
718 * Test using various operators.
720 public function testGetTypeOperators() {
721 $relTypeParams = array(
722 'name_a_b' => 'Relation 3 for delete',
723 'name_b_a' => 'Relation 6 for delete',
724 'description' => 'Testing relationship type 2',
725 'contact_type_a' => 'Individual',
726 'contact_type_b' => 'Organization',
730 $relationType2 = $this->relationshipTypeCreate($relTypeParams);
731 $relTypeParams = array(
732 'name_a_b' => 'Relation 8 for delete',
733 'name_b_a' => 'Relation 9 for delete',
734 'description' => 'Testing relationship type 7',
735 'contact_type_a' => 'Individual',
736 'contact_type_b' => 'Organization',
740 $relationType3 = $this->relationshipTypeCreate($relTypeParams);
742 $relTypeParams = array(
743 'name_a_b' => 'Relation 6 for delete',
744 'name_b_a' => 'Relation 88for delete',
745 'description' => 'Testing relationship type 00',
746 'contact_type_a' => 'Individual',
747 'contact_type_b' => 'Organization',
751 $relationType4 = $this->relationshipTypeCreate($relTypeParams);
753 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params
);
754 $rel2 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
755 array('relationship_type_id' => $relationType2)));
756 $rel3 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
757 array('relationship_type_id' => $relationType3)));
758 $rel4 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
759 array('relationship_type_id' => $relationType4)));
762 'relationship_type_id' => array('IN' => array($relationType2, $relationType3)),
765 $description = "Demonstrates use of IN filter.";
766 $subfile = 'INRelationshipType';
768 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
769 $this->assertEquals($result['count'], 2);
770 $this->AssertEquals(array($rel2['id'], $rel3['id']), array_keys($result['values']));
772 $description = "Demonstrates use of NOT IN filter.";
773 $subfile = 'NotInRelationshipType';
775 'relationship_type_id' => array('NOT IN' => array($relationType2, $relationType3)),
777 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
778 $this->assertEquals($result['count'], 2);
779 $this->AssertEquals(array($rel1['id'], $rel4['id']), array_keys($result['values']));
781 $description = "Demonstrates use of BETWEEN filter.";
782 $subfile = 'BetweenRelationshipType';
784 'relationship_type_id' => array('BETWEEN' => array($relationType2, $relationType4)),
786 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
787 $this->assertEquals($result['count'], 3);
788 $this->AssertEquals(array($rel2['id'], $rel3['id'], $rel4['id']), array_keys($result['values']));
790 $description = "Demonstrates use of Not BETWEEN filter.";
791 $subfile = 'NotBetweenRelationshipType';
793 'relationship_type_id' => array('NOT BETWEEN' => array($relationType2, $relationType4)),
795 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
796 $this->assertEquals($result['count'], 1);
797 $this->AssertEquals(array($rel1['id']), array_keys($result['values']));
802 * Check with invalid relationshipType Id.
804 public function testRelationshipTypeAddInvalidId() {
805 $relTypeParams = array(
807 'name_a_b' => 'Relation 1 for delete',
808 'name_b_a' => 'Relation 2 for delete',
809 'contact_type_a' => 'Individual',
810 'contact_type_b' => 'Organization',
812 $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
813 'id is not a valid integer');
817 * Check with valid data with contact_b.
819 public function testGetRelationshipWithContactB() {
821 'contact_id_a' => $this->_cId_a
,
822 'contact_id_b' => $this->_cId_b
,
823 'relationship_type_id' => $this->_relTypeID
,
824 'start_date' => '2011-01-01',
825 'end_date' => '2013-01-01',
829 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
832 'contact_id' => $this->_cId_a
,
835 $result = $this->callAPISuccess('relationship', 'get', $contacts);
836 $this->assertGreaterThan(0, $result['count']);
838 'id' => $relationship['id'],
840 $this->callAPISuccess('relationship', 'delete', $params);
841 $this->relationshipTypeDelete($this->_relTypeID
);
845 * Check with valid data with relationshipTypes.
847 public function testGetRelationshipWithRelTypes() {
849 'contact_id_a' => $this->_cId_a
,
850 'contact_id_b' => $this->_cId_b
,
851 'relationship_type_id' => $this->_relTypeID
,
852 'start_date' => '2011-01-01',
853 'end_date' => '2013-01-01',
857 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
860 'contact_id' => $this->_cId_a
,
862 $this->callAPISuccess('relationship', 'get', $contact_a);
865 'id' => $relationship['id'],
867 $this->callAPISuccess('relationship', 'delete', $params);
868 $this->relationshipTypeDelete($this->_relTypeID
);
872 * Checks that passing in 'contact_id' + a relationship type
873 * will filter by relationship type (relationships go in both directions)
874 * as relationship api does a reciprocal check if contact_id provided
876 * We should get 1 result without or with correct relationship type id & 0 with
879 public function testGetRelationshipByTypeReciprocal() {
880 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
881 $result = $this->callAPISuccess($this->_entity
, 'get', array(
882 'contact_id' => $this->_cId_a
,
883 'relationship_type_id' => $this->_relTypeID
,
885 $this->assertEquals(1, $result['count']);
886 $result = $this->callAPISuccess($this->_entity
, 'get', array(
887 'contact_id' => $this->_cId_a
,
888 'relationship_type_id' => $this->_relTypeID +
1,
890 $this->assertEquals(0, $result['count']);
891 $this->callAPISuccess($this->_entity
, 'delete', array('id' => $created['id']));
895 * Checks that passing in 'contact_id_b' + a relationship type
896 * will filter by relationship type for contact b
898 * We should get 1 result without or with correct relationship type id & 0 with
901 public function testGetRelationshipByTypeDAO() {
902 $this->_ids
['relationship'] = $this->callAPISuccess($this->_entity
, 'create', array('format.only_id' => TRUE) +
904 $this->callAPISuccess($this->_entity
, 'getcount', array(
905 'contact_id_a' => $this->_cId_a
,
908 $result = $this->callAPISuccess($this->_entity
, 'get', array(
909 'contact_id_a' => $this->_cId_a
,
910 'relationship_type_id' => $this->_relTypeID
,
912 $this->assertEquals(1, $result['count']);
913 $result = $this->callAPISuccess($this->_entity
, 'get', array(
914 'contact_id_a' => $this->_cId_a
,
915 'relationship_type_id' => $this->_relTypeID +
1,
917 $this->assertEquals(0, $result['count']);
921 * Checks that passing in 'contact_id_b' + a relationship type
922 * will filter by relationship type for contact b
924 * We should get 1 result without or with correct relationship type id & 0 with
927 public function testGetRelationshipByTypeArrayDAO() {
928 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
929 $org3 = $this->organizationCreate();
930 $relType2 = 5; // lets just assume built in ones aren't being messed with!
931 $relType3 = 6; // lets just assume built in ones aren't being messed with!
934 $this->callAPISuccess($this->_entity
, 'create',
935 array_merge($this->_params
, array(
936 'relationship_type_id' => $relType2,
937 'contact_id_b' => $this->_cId_b2
,
942 $this->callAPISuccess($this->_entity
, 'create',
943 array_merge($this->_params
, array(
944 'relationship_type_id' => $relType3,
945 '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 public function testGetRelationshipByTypeArrayReciprocal() {
968 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
969 $org3 = $this->organizationCreate();
970 // lets just assume built in ones aren't being messed with!
975 $this->callAPISuccess($this->_entity
, 'create',
976 array_merge($this->_params
, array(
977 'relationship_type_id' => $relType2,
978 'contact_id_b' => $this->_cId_b2
,
983 $this->callAPISuccess($this->_entity
, 'create',
984 array_merge($this->_params
, array(
985 'relationship_type_id' => $relType3,
986 'contact_id_b' => $org3,
990 $result = $this->callAPISuccess($this->_entity
, 'get', array(
991 'contact_id' => $this->_cId_a
,
992 'relationship_type_id' => array('IN' => array($this->_relTypeID
, $relType3)),
995 $this->assertEquals(2, $result['count']);
996 foreach ($result['values'] as $key => $value) {
997 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID
, $relType3)));
1002 * Test relationship get by membership type.
1004 * Checks that passing in 'contact_id_b' + a relationship type
1005 * will filter by relationship type for contact b
1007 * We should get 1 result without or with correct relationship type id & 0 with
1010 public function testGetRelationshipByMembershipTypeDAO() {
1011 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1012 $org3 = $this->organizationCreate();
1014 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1015 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1017 $memberType = $this->membershipTypeCreate(array(
1018 'relationship_type_id' => CRM_Core_DAO
::VALUE_SEPARATOR
. $relType1 . CRM_Core_DAO
::VALUE_SEPARATOR
. $relType3 . CRM_Core_DAO
::VALUE_SEPARATOR
,
1019 'relationship_direction' => CRM_Core_DAO
::VALUE_SEPARATOR
. 'a_b' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'b_a' . CRM_Core_DAO
::VALUE_SEPARATOR
,
1023 $this->callAPISuccess($this->_entity
, 'create',
1024 array_merge($this->_params
, array(
1025 'relationship_type_id' => $relType2,
1026 'contact_id_b' => $this->_cId_b2
,
1031 $this->callAPISuccess($this->_entity
, 'create',
1032 array_merge($this->_params
, array(
1033 'relationship_type_id' => $relType3,
1034 'contact_id_b' => $org3,
1038 // Relationship 4 with reversal.
1039 $this->callAPISuccess($this->_entity
, 'create',
1040 array_merge($this->_params
, array(
1041 'relationship_type_id' => $relType1,
1042 'contact_id_a' => $this->_cId_a
,
1043 'contact_id_b' => $this->_cId_a_2
,
1047 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1048 'contact_id_a' => $this->_cId_a
,
1049 'membership_type_id' => $memberType,
1051 // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1052 $this->assertEquals(1, $result['count']);
1053 foreach ($result['values'] as $key => $value) {
1054 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1)));
1059 * Checks that passing in 'contact_id_b' + a relationship type
1060 * will filter by relationship type for contact b
1062 * We should get 1 result without or with correct relationship type id & 0 with
1065 public function testGetRelationshipByMembershipTypeReciprocal() {
1066 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1067 $org3 = $this->organizationCreate();
1069 // Let's just assume built in ones aren't being messed with!
1073 $memberType = $this->membershipTypeCreate(array(
1074 'relationship_type_id' => CRM_Core_DAO
::VALUE_SEPARATOR
. $relType1 . CRM_Core_DAO
::VALUE_SEPARATOR
. $relType3 . CRM_Core_DAO
::VALUE_SEPARATOR
,
1075 'relationship_direction' => CRM_Core_DAO
::VALUE_SEPARATOR
. 'a_b' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'b_a' . CRM_Core_DAO
::VALUE_SEPARATOR
,
1079 $this->callAPISuccess($this->_entity
, 'create',
1080 array_merge($this->_params
, array(
1081 'relationship_type_id' => $relType2,
1082 'contact_id_b' => $this->_cId_b2
,
1087 $this->callAPISuccess($this->_entity
, 'create',
1088 array_merge($this->_params
, array(
1089 'relationship_type_id' => $relType3,
1090 'contact_id_b' => $org3,
1094 // Relationship 4 with reversal.
1095 $this->callAPISuccess($this->_entity
, 'create',
1096 array_merge($this->_params
, array(
1097 'relationship_type_id' => $relType1,
1098 'contact_id_a' => $this->_cId_a
,
1099 'contact_id_b' => $this->_cId_a_2
,
1103 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1104 'contact_id' => $this->_cId_a
,
1105 'membership_type_id' => $memberType,
1107 // Although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1108 $this->assertEquals(2, $result['count']);
1110 foreach ($result['values'] as $key => $value) {
1111 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1, $relType3)));
1116 * Check for e-notices on enable & disable as reported in CRM-14350
1118 public function testSetActive() {
1119 $relationship = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1120 $this->callAPISuccess($this->_entity
, 'create', array('id' => $relationship['id'], 'is_active' => 0));
1121 $this->callAPISuccess($this->_entity
, 'create', array('id' => $relationship['id'], 'is_active' => 1));
1125 * Test creating related memberships.
1127 public function testCreateRelatedMembership() {
1128 $relatedMembershipType = $this->callAPISuccess('MembershipType', 'create', array(
1129 'name' => 'Membership with Related',
1130 'member_of_contact_id' => 1,
1131 'financial_type_id' => 1,
1132 'minimum_fee' => 0.00,
1133 'duration_unit' => 'year',
1134 'duration_interval' => 1,
1135 'period_type' => 'rolling',
1136 'relationship_type_id' => $this->_relTypeID
,
1137 'relationship_direction' => 'b_a',
1138 'visibility' => 'Public',
1141 'domain_id' => CRM_Core_Config
::domainID(),
1143 $originalMembership = $this->callAPISuccess('Membership', 'create', array(
1144 'membership_type_id' => $relatedMembershipType['id'],
1145 'contact_id' => $this->_cId_b
,
1147 $this->callAPISuccess('Relationship', 'create', array(
1148 'relationship_type_id' => $this->_relTypeID
,
1149 'contact_id_a' => $this->_cId_a
,
1150 'contact_id_b' => $this->_cId_b
,
1152 $contactAMembership = $this->callAPISuccessGetSingle('membership', array('contact_id' => $this->_cId_a
));
1153 $this->assertEquals($originalMembership['id'], $contactAMembership['owner_membership_id']);
1154 // Deleting the organization should cause the related membership to be deleted.
1155 $this->callAPISuccess('contact', 'delete', array('id' => $this->_cId_b
));
1156 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_cId_a
), 0);