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('last_name' => 'c2', 'email' => 'c@w.com', 'contact_type' => 'Individual'));
56 $this->_cId_b
= $this->organizationCreate();
57 $this->_cId_b2
= $this->organizationCreate(array('organization_name' => ' Org 2'));
58 $this->_entity
= 'relationship';
59 //Create a relationship type
60 $relTypeParams = array(
61 'name_a_b' => 'Relation 1 for delete',
62 'name_b_a' => 'Relation 2 for delete',
63 'description' => 'Testing relationship type',
64 'contact_type_a' => 'Individual',
65 'contact_type_b' => 'Organization',
70 $this->_relTypeID
= $this->relationshipTypeCreate($relTypeParams);
71 $this->_params
= array(
72 'contact_id_a' => $this->_cId_a
,
73 'contact_id_b' => $this->_cId_b
,
74 'relationship_type_id' => $this->_relTypeID
,
75 'start_date' => '2008-12-20',
81 public function tearDown() {
82 $this->contactDelete($this->_cId_a
);
83 $this->contactDelete($this->_cId_a_2
);
84 $this->contactDelete($this->_cId_b
);
85 $this->contactDelete($this->_cId_b2
);
86 $this->quickCleanup(array('civicrm_relationship'), TRUE);
87 $this->relationshipTypeDelete($this->_relTypeID
);
90 ///////////////// civicrm_relationship_create methods
93 * Check with empty array
95 public function testRelationshipCreateEmpty() {
96 $this->callAPIFailure('relationship', 'create', array());
100 * Check if required fields are not passed
102 public function testRelationshipCreateWithoutRequired() {
104 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
105 'end_date' => array('d' => '10', 'M' => '1', 'Y' => '2009'),
109 $this->callAPIFailure('relationship', 'create', $params);
113 * Check with incorrect required fields
115 public function testRelationshipCreateWithIncorrectData() {
118 'contact_id_a' => $this->_cId_a
,
119 'contact_id_b' => $this->_cId_b
,
120 'relationship_type_id' => 'Breaking Relationship',
123 $this->callAPIFailure('relationship', 'create', $params);
125 //contact id is not an integer
127 'contact_id_a' => 'invalid',
128 'contact_id_b' => $this->_cId_b
,
129 'relationship_type_id' => $this->_relTypeID
,
130 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
133 $this->callAPIFailure('relationship', 'create', $params);
135 //contact id does not exists
136 $params['contact_id_a'] = 999;
137 $this->callAPIFailure('relationship', 'create', $params);
140 $params['contact_id_a'] = $this->_cId_a
;
141 $params['start_date'] = array('d' => '1', 'M' => '1');
142 $this->callAPIFailure('relationship', 'create', $params);
146 * Check relationship creation with invalid Relationship
148 public function testRelationshipCreatInvalidRelationship() {
149 // both the contact of type Individual
151 'contact_id_a' => $this->_cId_a
,
152 'contact_id_b' => $this->_cId_a
,
153 'relationship_type_id' => $this->_relTypeID
,
154 'start_date' => '2008-01-10',
158 $this->callAPIFailure('relationship', 'create', $params);
160 // both the contact of type Organization
162 'contact_id_a' => $this->_cId_b
,
163 'contact_id_b' => $this->_cId_b
,
164 'relationship_type_id' => $this->_relTypeID
,
165 'start_date' => '2008-01-10',
169 $this->callAPIFailure('relationship', 'create', $params);
173 * Check relationship already exists
175 public function testRelationshipCreateAlreadyExists() {
177 'contact_id_a' => $this->_cId_a
,
178 'contact_id_b' => $this->_cId_b
,
179 'relationship_type_id' => $this->_relTypeID
,
180 'start_date' => '2008-12-20', 'end_date' => NULL,
183 $relationship = $this->callAPISuccess('relationship', 'create', $params);
186 'contact_id_a' => $this->_cId_a
,
187 'contact_id_b' => $this->_cId_b
,
188 'relationship_type_id' => $this->_relTypeID
,
189 'start_date' => '2008-12-20',
192 $result = $this->callAPIFailure('relationship', 'create', $params, 'Relationship already exists');
194 $params['id'] = $relationship['id'];
195 $result = $this->callAPISuccess('relationship', 'delete', $params);
199 * Check relationship already exists
201 public function testRelationshipCreateUpdateAlreadyExists() {
203 'contact_id_a' => $this->_cId_a
,
204 'contact_id_b' => $this->_cId_b
,
205 'relationship_type_id' => $this->_relTypeID
,
206 'start_date' => '2008-12-20',
211 $relationship = $this->callAPISuccess('relationship', 'create', $params);
213 'id' => $relationship['id'],
217 $result = $this->callAPISuccess('relationship', 'create', $params);
218 $result = $this->callAPISuccess('relationship', 'get', $params);
219 $params['id'] = $relationship['id'];
220 $result = $this->callAPISuccess('relationship', 'delete', $params);
224 * Checkupdate doesn't reset stuff badly - CRM-11789
226 public function testRelationshipCreateUpdateDoesntMangle() {
228 'contact_id_a' => $this->_cId_a
,
229 'contact_id_b' => $this->_cId_b
,
230 'relationship_type_id' => $this->_relTypeID
,
231 'start_date' => '2008-12-20',
233 'is_permission_a_b' => 1,
234 'description' => 'my desc',
236 $relationship = $this->callAPISuccess('relationship', 'create', $params);
238 $updateparams = array(
239 'id' => $relationship['id'],
240 'relationship_type_id' => $this->_relTypeID
,
242 $result = $this->callAPISuccess('relationship', 'create', $updateparams);
244 //make sure the orig params didn't get changed
245 $this->getAndCheck($params, $relationship['id'], 'relationship');
252 * Check relationship creation
254 public function testRelationshipCreate() {
256 'contact_id_a' => $this->_cId_a
,
257 'contact_id_b' => $this->_cId_b
,
258 'relationship_type_id' => $this->_relTypeID
,
259 'start_date' => '2010-10-30',
260 'end_date' => '2010-12-30',
265 $result = $this->callAPIAndDocument('relationship', 'create', $params, __FUNCTION__
, __FILE__
);
266 $this->assertNotNull($result['id']);
267 $relationParams = array(
268 'id' => $result['id'],
271 // assertDBState compares expected values in $result to actual values in the DB
272 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
273 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
274 $values = $result['values'][$result['id']];
275 foreach ($params as $key => $value) {
276 if ($key == 'note') {
279 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE));
281 $params['id'] = $result['id'];
282 $this->callAPISuccess('relationship', 'delete', $params);
285 * Ensure disabling works
287 public function testRelationshipUpdate() {
288 $result = $this->callAPISuccess('relationship', 'create', $this->_params
);
289 $relID = $result['id'];
290 $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'description' => 'blah'));
291 $this->assertEquals($relID, $result['id']);
293 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
295 $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'is_permission_b_a' => 1));
296 $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
297 $result = $this->callAPISuccess('relationship', 'create', array('id' => $result['id'], 'is_active' => 0));
298 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
299 $this->assertEquals(0, $result['values'][$result['id']]['is_active']);
300 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
301 $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
304 * Check relationship creation
306 public function testRelationshipCreateEmptyEndDate() {
308 'contact_id_a' => $this->_cId_a
,
309 'contact_id_b' => $this->_cId_b
,
310 'relationship_type_id' => $this->_relTypeID
,
311 'start_date' => '2010-10-30',
317 $result = $this->callAPISuccess('relationship', 'create', $params);
318 $this->assertNotNull($result['id']);
319 $relationParams = array(
320 'id' => $result['id'],
323 // assertDBState compares expected values in $result to actual values in the DB
324 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
325 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
326 $values = $result['values'][$result['id']];
327 foreach ($params as $key => $value) {
328 if ($key == 'note') {
331 if($key == 'end_date'){
332 $this->assertTrue(empty($values[$key]));
335 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__
);
337 $params['id'] = $result['id'];
338 $this->callAPISuccess('relationship', 'delete', $params);
342 * Check relationship creation with custom data
344 public function testRelationshipCreateWithCustomData() {
345 $customGroup = $this->createCustomGroup();
346 $this->_ids
= $this->createCustomField();
347 //few custom Values for comparing
348 $custom_params = array(
349 "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship',
350 "custom_{$this->_ids[1]}" => 'Y',
351 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
352 "custom_{$this->_ids[3]}" => 'http://example.com',
356 'contact_id_a' => $this->_cId_a
,
357 'contact_id_b' => $this->_cId_b
,
358 'relationship_type_id' => $this->_relTypeID
,
359 'start_date' => '2008-12-20',
362 $params = array_merge($params, $custom_params);
363 $result = $this->callAPISuccess('relationship', 'create', $params);
365 $relationParams = array(
366 'id' => $result['id'],
368 // assertDBState compares expected values in $result to actual values in the DB
369 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
371 $params['id'] = $result['id'];
372 $result = $this->callAPISuccess('relationship', 'delete', $params);
373 $this->relationshipTypeDelete($this->_relTypeID
);
377 * Check with complete array + custom field
378 * Note that the test is written on purpose without any
379 * variables specific to participant so it can be replicated into other entities
380 * and / or moved to the automated test suite
382 public function testGetWithCustom() {
383 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
385 $params = $this->_params
;
386 $params['custom_' . $ids['custom_field_id']] = "custom string";
388 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
389 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
391 $getParams = array('id' => $result['id']);
392 $check = $this->callAPIAndDocument($this->_entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
393 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
395 $this->customFieldDelete($ids['custom_field_id']);
396 $this->customGroupDelete($ids['custom_group_id']);
402 public function createCustomGroup() {
404 'title' => 'Custom Group',
405 'extends' => array('Relationship'),
411 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
412 $this->_customGroupId
= $customGroup['id'];
413 return $customGroup['id'];
419 public function createCustomField() {
422 'custom_group_id' => $this->_customGroupId
,
423 'label' => 'Enter text about relationship',
424 'html_type' => 'Text',
425 'data_type' => 'String',
426 'default_value' => 'xyz',
429 'is_searchable' => 0,
434 $result = $this->callAPISuccess('CustomField', 'create', $params);
437 $ids[] = $customField['result']['customFieldId'];
439 $optionValue[] = array(
445 $optionValue[] = array(
451 $optionValue[] = array(
459 'label' => 'Pick Color',
460 'html_type' => 'Select',
461 'data_type' => 'String',
464 'is_searchable' => 0,
466 'option_values' => $optionValue,
467 'custom_group_id' => $this->_customGroupId
,
470 $customField = $this->callAPISuccess('custom_field', 'create', $params);
471 $ids[] = $customField['id'];
474 'custom_group_id' => $this->_customGroupId
,
475 'name' => 'test_date',
476 'label' => 'test_date',
477 'html_type' => 'Select Date',
478 'data_type' => 'Date',
479 'default_value' => '20090711',
482 'is_searchable' => 0,
486 $customField = $this->callAPISuccess('custom_field', 'create', $params);
488 $ids[] = $customField['id'];
490 'custom_group_id' => $this->_customGroupId
,
491 'name' => 'test_link',
492 'label' => 'test_link',
493 'html_type' => 'Link',
494 'data_type' => 'Link',
495 'default_value' => 'http://civicrm.org',
498 'is_searchable' => 0,
502 $customField = $this->callAPISuccess('custom_field', 'create', $params);
503 $ids[] = $customField['id'];
507 ///////////////// civicrm_relationship_delete methods
510 * Check with empty array
512 public function testRelationshipDeleteEmpty() {
514 $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
518 * Check if required fields are not passed
520 public function testRelationshipDeleteWithoutRequired() {
522 'start_date' => '2008-12-20',
523 'end_date' => '2009-12-20',
527 $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
531 * Check with incorrect required fields
533 public function testRelationshipDeleteWithIncorrectData() {
535 'contact_id_a' => $this->_cId_a
,
536 'contact_id_b' => $this->_cId_b
,
537 'relationship_type_id' => 'Breaking Relationship',
540 $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
542 $params['id'] = "Invalid";
543 $result = $this->callAPIFailure('relationship', 'delete', $params, 'Invalid value for relationship ID');
547 * Check relationship creation
549 public function testRelationshipDelete() {
551 'contact_id_a' => $this->_cId_a
,
552 'contact_id_b' => $this->_cId_b
,
553 'relationship_type_id' => $this->_relTypeID
,
554 'start_date' => '2008-12-20',
558 $result = $this->callAPISuccess('relationship', 'create', $params);
560 //Delete relationship
562 $params['id'] = $result['id'];
564 $result = $this->callAPIAndDocument('relationship', 'delete', $params, __FUNCTION__
, __FILE__
);
565 $this->relationshipTypeDelete($this->_relTypeID
);
568 ///////////////// civicrm_relationship_update methods
571 * Check with empty array
573 public function testRelationshipUpdateEmpty() {
574 $result = $this->callAPIFailure('relationship', 'create', array(),
575 'Mandatory key(s) missing from params array: contact_id_a, contact_id_b, relationship_type_id');
579 * Check if required fields are not passed
583 * Check relationship update
585 public function testRelationshipCreateDuplicate() {
587 'contact_id_a' => $this->_cId_a
,
588 'contact_id_b' => $this->_cId_b
,
589 'relationship_type_id' => $this->_relTypeID
,
590 'start_date' => '20081214',
591 'end_date' => '20091214',
595 $result = $this->callAPISuccess('relationship', 'create', $relParams);
597 $this->assertNotNull($result['id']);
598 $this->_relationID
= $result['id'];
601 'contact_id_a' => $this->_cId_a
,
602 'contact_id_b' => $this->_cId_b
,
603 'relationship_type_id' => $this->_relTypeID
,
604 'start_date' => '20081214',
605 'end_date' => '20091214',
609 $result = $this->callAPIFailure('relationship', 'create', $params, 'Relationship already exists');
611 //delete created relationship
613 'id' => $this->_relationID
,
616 $result = $this->callAPISuccess('relationship', 'delete', $params);
618 //delete created relationship type
619 $this->relationshipTypeDelete($this->_relTypeID
);
623 * Check with valid params array.
625 public function testRelationshipsGet() {
627 'contact_id_a' => $this->_cId_a
,
628 'contact_id_b' => $this->_cId_b
,
629 'relationship_type_id' => $this->_relTypeID
,
630 'start_date' => '2011-01-01',
631 'end_date' => '2013-01-01',
635 $result = $this->callAPISuccess('relationship', 'create', $relParams);
639 'contact_id' => $this->_cId_b
,
641 $result = $this->callAPISuccess('relationship', 'get', $params);
642 $this->assertEquals($result['count'], 1);
644 'contact_id_a' => $this->_cId_a
,
646 $result = $this->callAPISuccess('relationship', 'get', $params);
647 $this->assertEquals($result['count'], 1);
648 // contact_id_a is wrong so should be no matches
650 'contact_id_a' => $this->_cId_b
,
652 $result = $this->callAPISuccess('relationship', 'get', $params);
653 $this->assertEquals($result['count'], 0);
657 * Check with valid params array.
658 * (The get function will behave differently without 'contact_id' passed
660 public function testRelationshipsGetGeneric() {
662 'contact_id_a' => $this->_cId_a
,
663 'contact_id_b' => $this->_cId_b
,
664 'relationship_type_id' => $this->_relTypeID
,
665 'start_date' => '2011-01-01',
666 'end_date' => '2013-01-01',
670 $result = $this->callAPISuccess('relationship', 'create', $relParams);
674 'contact_id_b' => $this->_cId_b
,
676 $result = $this->callAPISuccess('relationship', 'get', $params);
679 public function testGetIsCurrent() {
681 'contact_id_a' => $this->_cId_a
,
682 'contact_id_b' => $this->_cId_b2
,
683 'relationship_type_id' => $this->_relTypeID
,
684 'start_date' => '2008-12-20',
687 $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params);
688 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params
);
691 'filters' => array('is_current' => 1)
693 $description = "demonstrates is_current filter";
694 $subfile = 'filterIsCurrent';
695 //no relationship has been created
696 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
697 $this->assertEquals($result['count'], 1);
698 $this->AssertEquals($rel1['id'], $result['id']);
700 // now try not started
701 $rel2Params['is_active'] =1;
702 $rel2Params['start_date'] ='tomorrow';
703 $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params);
704 $result = $this->callAPISuccess('relationship', 'get', $getParams);
705 $this->assertEquals($result['count'], 1);
706 $this->AssertEquals($rel1['id'], $result['id']);
709 $rel2Params['is_active'] =1;
710 $rel2Params['start_date'] ='last week';
711 $rel2Params['end_date'] ='yesterday';
712 $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params);
715 * Test using various operators
717 public function testGetTypeOperators() {
718 $relTypeParams = array(
719 'name_a_b' => 'Relation 3 for delete',
720 'name_b_a' => 'Relation 6 for delete',
721 'description' => 'Testing relationship type 2',
722 'contact_type_a' => 'Individual',
723 'contact_type_b' => 'Organization',
727 $relationType2 = $this->relationshipTypeCreate($relTypeParams);
728 $relTypeParams = array(
729 'name_a_b' => 'Relation 8 for delete',
730 'name_b_a' => 'Relation 9 for delete',
731 'description' => 'Testing relationship type 7',
732 'contact_type_a' => 'Individual',
733 'contact_type_b' => 'Organization',
737 $relationType3 = $this->relationshipTypeCreate($relTypeParams);
739 $relTypeParams = array(
740 'name_a_b' => 'Relation 6 for delete',
741 'name_b_a' => 'Relation 88for delete',
742 'description' => 'Testing relationship type 00',
743 'contact_type_a' => 'Individual',
744 'contact_type_b' => 'Organization',
748 $relationType4 = $this->relationshipTypeCreate($relTypeParams);
750 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params
);
751 $rel2 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
752 array('relationship_type_id' => $relationType2,)));
753 $rel3 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
754 array('relationship_type_id' => $relationType3,)));
755 $rel4 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
756 array('relationship_type_id' => $relationType4,)));
759 'relationship_type_id' => array('IN' => array($relationType2, $relationType3))
762 $description = "demonstrates use of IN filter";
763 $subfile = 'INRelationshipType';
765 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
766 $this->assertEquals($result['count'], 2);
767 $this->AssertEquals(array($rel2['id'], $rel3['id']), array_keys($result['values']));
769 $description = "demonstrates use of NOT IN filter";
770 $subfile = 'NotInRelationshipType';
772 'relationship_type_id' => array('NOT IN' => array($relationType2, $relationType3))
774 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
775 $this->assertEquals($result['count'], 2);
776 $this->AssertEquals(array($rel1['id'], $rel4['id']), array_keys($result['values']));
778 $description = "demonstrates use of BETWEEN filter";
779 $subfile = 'BetweenRelationshipType';
781 'relationship_type_id' => array('BETWEEN' => array($relationType2, $relationType4))
783 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
784 $this->assertEquals($result['count'], 3);
785 $this->AssertEquals(array($rel2['id'], $rel3['id'], $rel4['id']), array_keys($result['values']));
787 $description = "demonstrates use of Not BETWEEN filter";
788 $subfile = 'NotBetweenRelationshipType';
790 'relationship_type_id' => array('NOT BETWEEN' => array($relationType2, $relationType4))
792 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
793 $this->assertEquals($result['count'], 1);
794 $this->AssertEquals(array($rel1['id'],), array_keys($result['values']));
798 * Check with invalid relationshipType Id
800 public function testRelationshipTypeAddInvalidId() {
801 $relTypeParams = array(
803 'name_a_b' => 'Relation 1 for delete',
804 'name_b_a' => 'Relation 2 for delete',
805 'contact_type_a' => 'Individual',
806 'contact_type_b' => 'Organization',
808 $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
809 'id is not a valid integer');
813 * Check with valid data with contact_b
815 public function testGetRelationshipWithContactB() {
817 'contact_id_a' => $this->_cId_a
,
818 'contact_id_b' => $this->_cId_b
,
819 'relationship_type_id' => $this->_relTypeID
,
820 'start_date' => '2011-01-01',
821 'end_date' => '2013-01-01',
825 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
828 'contact_id' => $this->_cId_a
,
831 $result = $this->callAPISuccess('relationship', 'get', $contacts);
832 $this->assertGreaterThan(0, $result['count']);
834 'id' => $relationship['id'],
836 $result = $this->callAPISuccess('relationship', 'delete', $params);
837 $this->relationshipTypeDelete($this->_relTypeID
);
841 * Check with valid data with relationshipTypes
843 public function testGetRelationshipWithRelTypes() {
845 'contact_id_a' => $this->_cId_a
,
846 'contact_id_b' => $this->_cId_b
,
847 'relationship_type_id' => $this->_relTypeID
,
848 'start_date' => '2011-01-01',
849 'end_date' => '2013-01-01',
853 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
856 'contact_id' => $this->_cId_a
,
858 $result = $this->callAPISuccess('relationship', 'get', $contact_a);
861 'id' => $relationship['id'],
863 $result = $this->callAPISuccess('relationship', 'delete', $params);
864 $this->relationshipTypeDelete($this->_relTypeID
);
868 * Checks that passing in 'contact_id' + a relationship type
869 * will filter by relationship type (relationships go in both directions)
870 * as relationship api does a reciprocal check if contact_id provided
872 * We should get 1 result without or with correct relationship type id & 0 with
875 public function testGetRelationshipByTypeReciprocal() {
876 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
877 $result = $this->callAPISuccess($this->_entity
, 'get', array(
878 'contact_id' => $this->_cId_a
,
879 'relationship_type_id' => $this->_relTypeID
,
881 $this->assertEquals(1, $result['count']);
882 $result = $this->callAPISuccess($this->_entity
, 'get', array(
883 'contact_id' => $this->_cId_a
,
884 'relationship_type_id' => $this->_relTypeID +
1,
886 $this->assertEquals(0, $result['count']);
887 $this->callAPISuccess($this->_entity
, 'delete', array('id' => $created['id']));
891 * Checks that passing in 'contact_id_b' + a relationship type
892 * will filter by relationship type for contact b
894 * We should get 1 result without or with correct relationship type id & 0 with
897 public function testGetRelationshipByTypeDAO() {
898 $this->ids
['relationship'] = $this->callAPISuccess($this->_entity
, 'create', array('format.only_id' => TRUE,) +
$this->_params
);
899 $result = $this->callAPISuccess($this->_entity
, 'getcount', array(
900 'contact_id_a' => $this->_cId_a
,),
902 $result = $this->callAPISuccess($this->_entity
, 'get', array(
903 'contact_id_a' => $this->_cId_a
,
904 'relationship_type_id' => $this->_relTypeID
,
906 $this->assertEquals(1, $result['count']);
907 $result = $this->callAPISuccess($this->_entity
, 'get', array(
908 'contact_id_a' => $this->_cId_a
,
909 'relationship_type_id' => $this->_relTypeID +
1,
911 $this->assertEquals(0, $result['count']);
915 * Checks that passing in 'contact_id_b' + a relationship type
916 * will filter by relationship type for contact b
918 * We should get 1 result without or with correct relationship type id & 0 with
921 public function testGetRelationshipByTypeArrayDAO() {
922 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
923 $org3 = $this->organizationCreate();
924 $relType2 = 5; // lets just assume built in ones aren't being messed with!
925 $relType3 = 6; // lets just assume built in ones aren't being messed with!
928 $this->callAPISuccess($this->_entity
, 'create',
929 array_merge($this->_params
, array(
930 'relationship_type_id' => $relType2,
931 'contact_id_b' => $this->_cId_b2
))
935 $this->callAPISuccess($this->_entity
, 'create',
936 array_merge($this->_params
, array(
937 'relationship_type_id' => $relType3,
938 'contact_id_b' => $org3))
941 $result = $this->callAPISuccess($this->_entity
, 'get', array(
942 'contact_id_a' => $this->_cId_a
,
943 'relationship_type_id' => array('IN' => array($this->_relTypeID
, $relType3)),
946 $this->assertEquals(2, $result['count']);
947 foreach ($result['values'] as $key => $value) {
948 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID
, $relType3)));
953 * Checks that passing in 'contact_id_b' + a relationship type
954 * will filter by relationship type for contact b
956 * We should get 1 result without or with correct relationship type id & 0 with
959 public function testGetRelationshipByTypeArrayReciprocal() {
960 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
961 $org3 = $this->organizationCreate();
962 $relType2 = 5; // lets just assume built in ones aren't being messed with!
963 $relType3 = 6; // lets just assume built in ones aren't being messed with!
966 $this->callAPISuccess($this->_entity
, 'create',
967 array_merge($this->_params
, array(
968 'relationship_type_id' => $relType2,
969 'contact_id_b' => $this->_cId_b2
))
973 $this->callAPISuccess($this->_entity
, 'create',
974 array_merge($this->_params
, array(
975 'relationship_type_id' => $relType3,
976 'contact_id_b' => $org3))
979 $result = $this->callAPISuccess($this->_entity
, 'get', array(
980 'contact_id' => $this->_cId_a
,
981 'relationship_type_id' => array('IN' => array($this->_relTypeID
, $relType3)),
984 $this->assertEquals(2, $result['count']);
985 foreach ($result['values'] as $key => $value) {
986 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID
, $relType3)));
991 * Checks that passing in 'contact_id_b' + a relationship type
992 * will filter by relationship type for contact b
994 * We should get 1 result without or with correct relationship type id & 0 with
997 public function testGetRelationshipByMembershipTypeDAO() {
998 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
999 $org3 = $this->organizationCreate();
1001 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1002 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1004 $memberType = $this->membershipTypeCreate(array(
1005 'relationship_type_id' => CRM_Core_DAO
::VALUE_SEPARATOR
. $relType1 . CRM_Core_DAO
::VALUE_SEPARATOR
. $relType3 . CRM_Core_DAO
::VALUE_SEPARATOR
,
1006 'relationship_direction' => CRM_Core_DAO
::VALUE_SEPARATOR
. 'a_b' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'b_a' . CRM_Core_DAO
::VALUE_SEPARATOR
,
1010 $this->callAPISuccess($this->_entity
, 'create',
1011 array_merge($this->_params
, array(
1012 'relationship_type_id' => $relType2,
1013 'contact_id_b' => $this->_cId_b2
))
1017 $this->callAPISuccess($this->_entity
, 'create',
1018 array_merge($this->_params
, array(
1019 'relationship_type_id' => $relType3,
1020 'contact_id_b' => $org3))
1023 //relationshp 4 with reveral
1024 $this->callAPISuccess($this->_entity
, 'create',
1025 array_merge($this->_params
, array(
1026 'relationship_type_id' => $relType1,
1027 'contact_id_a' => $this->_cId_a
,
1028 'contact_id_b' => $this->_cId_a_2
))
1031 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1032 'contact_id_a' => $this->_cId_a
,
1033 'membership_type_id' => $memberType,
1035 // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1036 $this->assertEquals(1, $result['count']);
1037 foreach ($result['values'] as $key => $value) {
1038 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1)));
1043 * Checks that passing in 'contact_id_b' + a relationship type
1044 * will filter by relationship type for contact b
1046 * We should get 1 result without or with correct relationship type id & 0 with
1049 public function testGetRelationshipByMembershipTypeReciprocal() {
1050 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1051 $org3 = $this->organizationCreate();
1053 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1054 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1056 $memberType = $this->membershipTypeCreate(array(
1057 'relationship_type_id' => CRM_Core_DAO
::VALUE_SEPARATOR
. $relType1 . CRM_Core_DAO
::VALUE_SEPARATOR
. $relType3 . CRM_Core_DAO
::VALUE_SEPARATOR
,
1058 'relationship_direction' => CRM_Core_DAO
::VALUE_SEPARATOR
. 'a_b' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'b_a' . CRM_Core_DAO
::VALUE_SEPARATOR
,
1062 $this->callAPISuccess($this->_entity
, 'create',
1063 array_merge($this->_params
, array(
1064 'relationship_type_id' => $relType2,
1065 'contact_id_b' => $this->_cId_b2
))
1069 $this->callAPISuccess($this->_entity
, 'create',
1070 array_merge($this->_params
, array(
1071 'relationship_type_id' => $relType3,
1072 'contact_id_b' => $org3))
1075 //relationshp 4 with reveral
1076 $this->callAPISuccess($this->_entity
, 'create',
1077 array_merge($this->_params
, array(
1078 'relationship_type_id' => $relType1,
1079 'contact_id_a' => $this->_cId_a
,
1080 'contact_id_b' => $this->_cId_a_2
))
1083 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1084 'contact_id' => $this->_cId_a
,
1085 'membership_type_id' => $memberType,
1087 // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1088 $this->assertEquals(2, $result['count']);
1090 foreach ($result['values'] as $key => $value) {
1091 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1, $relType3)));
1096 * Check for enotices on enable & disable as reported in CRM-14350
1098 public function testSetActive() {
1099 $relationship = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1100 $this->callAPISuccess($this->_entity
, 'create', array('id' => $relationship['id'], 'is_active' => 0));
1101 $this->callAPISuccess($this->_entity
, 'create', array('id' => $relationship['id'], 'is_active' => 1));