3 * +--------------------------------------------------------------------+
4 * | CiviCRM version 4.7 |
5 * +--------------------------------------------------------------------+
6 * | Copyright CiviCRM LLC (c) 2004-2017 |
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 * +--------------------------------------------------------------------+
29 * Class contains api test cases for "civicrm_relationship"
32 class api_v3_RelationshipTest
extends CiviUnitTestCase
{
33 protected $_apiversion = 3;
42 * Second organization contact.
47 protected $_relTypeID;
48 protected $_ids = array();
49 protected $_customGroupId = NULL;
50 protected $_customFieldId = NULL;
58 public function setUp() {
60 $this->_cId_a
= $this->individualCreate();
61 $this->_cId_a_2
= $this->individualCreate(array(
64 'contact_type' => 'Individual',
66 $this->_cId_b
= $this->organizationCreate();
67 $this->_cId_b2
= $this->organizationCreate(array('organization_name' => ' Org 2'));
68 $this->_entity
= 'relationship';
69 //Create a relationship type.
70 $relTypeParams = array(
71 'name_a_b' => 'Relation 1 for delete',
72 'name_b_a' => 'Relation 2 for delete',
73 'description' => 'Testing relationship type',
74 'contact_type_a' => 'Individual',
75 'contact_type_b' => 'Organization',
80 $this->_relTypeID
= $this->relationshipTypeCreate($relTypeParams);
81 $this->_params
= array(
82 'contact_id_a' => $this->_cId_a
,
83 'contact_id_b' => $this->_cId_b
,
84 'relationship_type_id' => $this->_relTypeID
,
85 'start_date' => '2008-12-20',
96 public function tearDown() {
97 $this->contactDelete($this->_cId_a
);
98 $this->contactDelete($this->_cId_a_2
);
99 $this->contactDelete($this->_cId_b
);
100 $this->contactDelete($this->_cId_b2
);
101 $this->quickCleanup(array('civicrm_relationship'), TRUE);
102 $this->relationshipTypeDelete($this->_relTypeID
);
106 * Check with empty array.
108 public function testRelationshipCreateEmpty() {
109 $this->callAPIFailure('relationship', 'create', array());
113 * Check if required fields are not passed.
115 public function testRelationshipCreateWithoutRequired() {
117 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
118 'end_date' => array('d' => '10', 'M' => '1', 'Y' => '2009'),
122 $this->callAPIFailure('relationship', 'create', $params);
126 * Check with incorrect required fields.
128 public function testRelationshipCreateWithIncorrectData() {
131 'contact_id_a' => $this->_cId_a
,
132 'contact_id_b' => $this->_cId_b
,
133 'relationship_type_id' => 'Breaking Relationship',
136 $this->callAPIFailure('relationship', 'create', $params);
138 //contact id is not an integer
140 'contact_id_a' => 'invalid',
141 'contact_id_b' => $this->_cId_b
,
142 'relationship_type_id' => $this->_relTypeID
,
143 'start_date' => array('d' => '10', 'M' => '1', 'Y' => '2008'),
146 $this->callAPIFailure('relationship', 'create', $params);
148 // Contact id does not exist.
149 $params['contact_id_a'] = 999;
150 $this->callAPIFailure('relationship', 'create', $params);
153 $params['contact_id_a'] = $this->_cId_a
;
154 $params['start_date'] = array('d' => '1', 'M' => '1');
155 $this->callAPIFailure('relationship', 'create', $params);
159 * Check relationship creation with invalid Relationship.
161 public function testRelationshipCreateInvalidRelationship() {
162 // Both have the contact type Individual.
164 'contact_id_a' => $this->_cId_a
,
165 'contact_id_b' => $this->_cId_a
,
166 'relationship_type_id' => $this->_relTypeID
,
167 'start_date' => '2008-01-10',
171 $this->callAPIFailure('relationship', 'create', $params);
173 // both the contact of type Organization
175 'contact_id_a' => $this->_cId_b
,
176 'contact_id_b' => $this->_cId_b
,
177 'relationship_type_id' => $this->_relTypeID
,
178 'start_date' => '2008-01-10',
182 $this->callAPIFailure('relationship', 'create', $params);
186 * Check relationship already exists.
188 public function testRelationshipCreateAlreadyExists() {
190 'contact_id_a' => $this->_cId_a
,
191 'contact_id_b' => $this->_cId_b
,
192 'relationship_type_id' => $this->_relTypeID
,
193 'start_date' => '2008-12-20',
197 $relationship = $this->callAPISuccess('relationship', 'create', $params);
200 'contact_id_a' => $this->_cId_a
,
201 'contact_id_b' => $this->_cId_b
,
202 'relationship_type_id' => $this->_relTypeID
,
203 'start_date' => '2008-12-20',
206 $this->callAPIFailure('relationship', 'create', $params, 'Duplicate Relationship');
208 $params['id'] = $relationship['id'];
209 $this->callAPISuccess('relationship', 'delete', $params);
213 * Check relationship already exists.
215 public function testRelationshipCreateUpdateAlreadyExists() {
217 'contact_id_a' => $this->_cId_a
,
218 'contact_id_b' => $this->_cId_b
,
219 'relationship_type_id' => $this->_relTypeID
,
220 'start_date' => '2008-12-20',
225 $relationship = $this->callAPISuccess('relationship', 'create', $params);
227 'id' => $relationship['id'],
231 $this->callAPISuccess('relationship', 'create', $params);
232 $this->callAPISuccess('relationship', 'get', $params);
233 $params['id'] = $relationship['id'];
234 $this->callAPISuccess('relationship', 'delete', $params);
238 * Check update doesn't reset stuff badly - CRM-11789.
240 public function testRelationshipCreateUpdateDoesNotMangle() {
242 'contact_id_a' => $this->_cId_a
,
243 'contact_id_b' => $this->_cId_b
,
244 'relationship_type_id' => $this->_relTypeID
,
245 'start_date' => '2008-12-20',
247 'is_permission_a_b' => 1,
248 'description' => 'my desc',
250 $relationship = $this->callAPISuccess('relationship', 'create', $params);
252 $updateParams = array(
253 'id' => $relationship['id'],
254 'relationship_type_id' => $this->_relTypeID
,
256 $this->callAPISuccess('relationship', 'create', $updateParams);
258 //make sure the orig params didn't get changed
259 $this->getAndCheck($params, $relationship['id'], 'relationship');
265 * Check relationship creation.
267 public function testRelationshipCreate() {
269 'contact_id_a' => $this->_cId_a
,
270 'contact_id_b' => $this->_cId_b
,
271 'relationship_type_id' => $this->_relTypeID
,
272 'start_date' => '2010-10-30',
273 'end_date' => '2010-12-30',
278 $result = $this->callAPIAndDocument('relationship', 'create', $params, __FUNCTION__
, __FILE__
);
279 $this->assertNotNull($result['id']);
280 $relationParams = array(
281 'id' => $result['id'],
284 // assertDBState compares expected values in $result to actual values in the DB
285 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
286 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
287 $values = $result['values'][$result['id']];
288 foreach ($params as $key => $value) {
289 if ($key == 'note') {
292 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE));
294 $params['id'] = $result['id'];
295 $this->callAPISuccess('relationship', 'delete', $params);
299 * Ensure disabling works.
301 public function testRelationshipUpdate() {
302 $result = $this->callAPISuccess('relationship', 'create', $this->_params
);
303 $relID = $result['id'];
304 $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'description' => 'blah'));
305 $this->assertEquals($relID, $result['id']);
307 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
309 $result = $this->callAPISuccess('relationship', 'create', array('id' => $relID, 'is_permission_b_a' => 1));
310 $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
311 $result = $this->callAPISuccess('relationship', 'create', array('id' => $result['id'], 'is_active' => 0));
312 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
313 $this->assertEquals(0, $result['values'][$result['id']]['is_active']);
314 $this->assertEquals('blah', $result['values'][$result['id']]['description']);
315 $this->assertEquals(1, $result['values'][$result['id']]['is_permission_b_a']);
319 * Check relationship creation.
321 public function testRelationshipCreateEmptyEndDate() {
323 'contact_id_a' => $this->_cId_a
,
324 'contact_id_b' => $this->_cId_b
,
325 'relationship_type_id' => $this->_relTypeID
,
326 'start_date' => '2010-10-30',
332 $result = $this->callAPISuccess('relationship', 'create', $params);
333 $this->assertNotNull($result['id']);
334 $relationParams = array(
335 'id' => $result['id'],
338 // assertDBState compares expected values in $result to actual values in the DB
339 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
340 $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id']));
341 $values = $result['values'][$result['id']];
342 foreach ($params as $key => $value) {
343 if ($key == 'note') {
346 if ($key == 'end_date') {
347 $this->assertTrue(empty($values[$key]));
350 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__
);
352 $params['id'] = $result['id'];
353 $this->callAPISuccess('relationship', 'delete', $params);
357 * Check relationship creation with custom data.
359 public function testRelationshipCreateWithCustomData() {
360 $this->createCustomGroup();
361 $this->_ids
= $this->createCustomField();
362 //few custom Values for comparing
363 $custom_params = array(
364 "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship',
365 "custom_{$this->_ids[1]}" => 'Y',
366 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
367 "custom_{$this->_ids[3]}" => 'http://example.com',
371 'contact_id_a' => $this->_cId_a
,
372 'contact_id_b' => $this->_cId_b
,
373 'relationship_type_id' => $this->_relTypeID
,
374 'start_date' => '2008-12-20',
377 $params = array_merge($params, $custom_params);
378 $result = $this->callAPISuccess('relationship', 'create', $params);
380 $relationParams = array(
381 'id' => $result['id'],
383 $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams);
385 $params['id'] = $result['id'];
386 $this->callAPISuccess('relationship', 'delete', $params);
387 $this->relationshipTypeDelete($this->_relTypeID
);
391 * Check with complete array + custom field
392 * Note that the test is written on purpose without any
393 * variables specific to participant so it can be replicated into other entities
394 * and / or moved to the automated test suite
396 public function testGetWithCustom() {
397 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
399 $params = $this->_params
;
400 $params['custom_' . $ids['custom_field_id']] = "custom string";
402 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
403 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
405 $getParams = array('id' => $result['id']);
406 $check = $this->callAPIAndDocument($this->_entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
407 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
409 $this->customFieldDelete($ids['custom_field_id']);
410 $this->customGroupDelete($ids['custom_group_id']);
416 public function createCustomGroup() {
418 'title' => 'Custom Group',
419 'extends' => array('Relationship'),
425 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
426 $this->_customGroupId
= $customGroup['id'];
427 return $customGroup['id'];
433 public function createCustomField() {
436 'custom_group_id' => $this->_customGroupId
,
437 'label' => 'Enter text about relationship',
438 'html_type' => 'Text',
439 'data_type' => 'String',
440 'default_value' => 'xyz',
443 'is_searchable' => 0,
447 $this->callAPISuccess('CustomField', 'create', $params);
450 $ids[] = $customField['result']['customFieldId'];
452 $optionValue[] = array(
458 $optionValue[] = array(
464 $optionValue[] = array(
472 'label' => 'Pick Color',
473 'html_type' => 'Select',
474 'data_type' => 'String',
477 'is_searchable' => 0,
479 'option_values' => $optionValue,
480 'custom_group_id' => $this->_customGroupId
,
483 $customField = $this->callAPISuccess('custom_field', 'create', $params);
484 $ids[] = $customField['id'];
487 'custom_group_id' => $this->_customGroupId
,
488 'name' => 'test_date',
489 'label' => 'test_date',
490 'html_type' => 'Select Date',
491 'data_type' => 'Date',
492 'default_value' => '20090711',
495 'is_searchable' => 0,
499 $customField = $this->callAPISuccess('custom_field', 'create', $params);
501 $ids[] = $customField['id'];
503 'custom_group_id' => $this->_customGroupId
,
504 'name' => 'test_link',
505 'label' => 'test_link',
506 'html_type' => 'Link',
507 'data_type' => 'Link',
508 'default_value' => 'http://civicrm.org',
511 'is_searchable' => 0,
515 $customField = $this->callAPISuccess('custom_field', 'create', $params);
516 $ids[] = $customField['id'];
521 * Check with empty array.
523 public function testRelationshipDeleteEmpty() {
524 $this->callAPIFailure('relationship', 'delete', array(), 'Mandatory key(s) missing from params array: id');
528 * Check if required fields are not passed.
530 public function testRelationshipDeleteWithoutRequired() {
532 'start_date' => '2008-12-20',
533 'end_date' => '2009-12-20',
537 $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
541 * Check with incorrect required fields.
543 public function testRelationshipDeleteWithIncorrectData() {
545 'contact_id_a' => $this->_cId_a
,
546 'contact_id_b' => $this->_cId_b
,
547 'relationship_type_id' => 'Breaking Relationship',
550 $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
552 $params['id'] = "Invalid";
553 $this->callAPIFailure('relationship', 'delete', $params, 'id is not a valid integer');
557 * Check relationship creation.
559 public function testRelationshipDelete() {
561 'contact_id_a' => $this->_cId_a
,
562 'contact_id_b' => $this->_cId_b
,
563 'relationship_type_id' => $this->_relTypeID
,
564 'start_date' => '2008-12-20',
568 $result = $this->callAPISuccess('relationship', 'create', $params);
569 $params = array('id' => $result['id']);
570 $this->callAPIAndDocument('relationship', 'delete', $params, __FUNCTION__
, __FILE__
);
571 $this->relationshipTypeDelete($this->_relTypeID
);
574 ///////////////// civicrm_relationship_update methods
577 * Check with empty array.
579 public function testRelationshipUpdateEmpty() {
580 $this->callAPIFailure('relationship', 'create', array(),
581 'Mandatory key(s) missing from params array: contact_id_a, contact_id_b, relationship_type_id');
585 * Check if required fields are not passed.
589 * Check relationship update.
591 public function testRelationshipCreateDuplicate() {
593 'contact_id_a' => $this->_cId_a
,
594 'contact_id_b' => $this->_cId_b
,
595 'relationship_type_id' => $this->_relTypeID
,
596 'start_date' => '20081214',
597 'end_date' => '20091214',
601 $result = $this->callAPISuccess('relationship', 'create', $relParams);
603 $this->assertNotNull($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 $this->callAPIFailure('relationship', 'create', $params, 'Duplicate Relationship');
616 $this->callAPISuccess('relationship', 'delete', array('id' => $result['id']));
617 $this->relationshipTypeDelete($this->_relTypeID
);
621 * CRM-13725 - Two relationships of same type with same start and end date
622 * should be OK if the custom field values differ.
624 public function testRelationshipCreateDuplicateWithCustomFields() {
625 $this->createCustomGroup();
626 $this->_ids
= $this->createCustomField();
628 $custom_params_1 = array(
629 "custom_{$this->_ids[0]}" => 'Hello! this is custom data for relationship',
630 "custom_{$this->_ids[1]}" => 'Y',
631 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
632 "custom_{$this->_ids[3]}" => 'http://example.com',
635 $custom_params_2 = array(
636 "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
637 "custom_{$this->_ids[1]}" => 'Y',
638 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
639 "custom_{$this->_ids[3]}" => 'http://example.org',
643 'contact_id_a' => $this->_cId_a
,
644 'contact_id_b' => $this->_cId_b
,
645 'relationship_type_id' => $this->_relTypeID
,
646 'start_date' => '2008-12-20',
650 $params_1 = array_merge($params, $custom_params_1);
651 $params_2 = array_merge($params, $custom_params_2);
653 $result_1 = $this->callAPISuccess('relationship', 'create', $params_1);
654 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
656 $this->assertNotNull($result_2['id']);
657 $this->assertEquals(0, $result_2['is_error']);
659 $this->relationshipTypeDelete($this->_relTypeID
);
663 * CRM-13725 - Two relationships of same type with same start and end date
664 * should be OK if the custom field values differ. In this case, the
665 * existing relationship does not have custom values, but the new one
668 public function testRelationshipCreateDuplicateWithCustomFields2() {
669 $this->createCustomGroup();
670 $this->_ids
= $this->createCustomField();
672 $custom_params_2 = array(
673 "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
674 "custom_{$this->_ids[1]}" => 'Y',
675 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
676 "custom_{$this->_ids[3]}" => 'http://example.org',
680 'contact_id_a' => $this->_cId_a
,
681 'contact_id_b' => $this->_cId_b
,
682 'relationship_type_id' => $this->_relTypeID
,
683 'start_date' => '2008-12-20',
687 $params_2 = array_merge($params_1, $custom_params_2);
689 $this->callAPISuccess('relationship', 'create', $params_1);
690 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
692 $this->assertNotNull($result_2['id']);
693 $this->assertEquals(0, $result_2['is_error']);
695 $this->relationshipTypeDelete($this->_relTypeID
);
699 * CRM-13725 - Two relationships of same type with same start and end date
700 * should be OK if the custom field values differ. In this case, the
701 * existing relationship does have custom values, but the new one
704 public function testRelationshipCreateDuplicateWithCustomFields3() {
705 $this->createCustomGroup();
706 $this->_ids
= $this->createCustomField();
708 $custom_params_1 = array(
709 "custom_{$this->_ids[0]}" => 'Hello! this is other custom data',
710 "custom_{$this->_ids[1]}" => 'Y',
711 "custom_{$this->_ids[2]}" => '2009-07-11 00:00:00',
712 "custom_{$this->_ids[3]}" => 'http://example.org',
716 'contact_id_a' => $this->_cId_a
,
717 'contact_id_b' => $this->_cId_b
,
718 'relationship_type_id' => $this->_relTypeID
,
719 'start_date' => '2008-12-20',
723 $params_1 = array_merge($params_2, $custom_params_1);
725 $this->callAPISuccess('relationship', 'create', $params_1);
726 $result_2 = $this->callAPISuccess('relationship', 'create', $params_2);
728 $this->assertNotNull($result_2['id']);
729 $this->assertEquals(0, $result_2['is_error']);
731 $this->relationshipTypeDelete($this->_relTypeID
);
735 * Check with valid params array.
737 public function testRelationshipsGet() {
739 'contact_id_a' => $this->_cId_a
,
740 'contact_id_b' => $this->_cId_b
,
741 'relationship_type_id' => $this->_relTypeID
,
742 'start_date' => '2011-01-01',
743 'end_date' => '2013-01-01',
747 $this->callAPISuccess('relationship', 'create', $relParams);
751 'contact_id' => $this->_cId_b
,
753 $result = $this->callAPISuccess('relationship', 'get', $params);
754 $this->assertEquals($result['count'], 1);
756 'contact_id_a' => $this->_cId_a
,
758 $result = $this->callAPISuccess('relationship', 'get', $params);
759 $this->assertEquals($result['count'], 1);
760 // contact_id_a is wrong so should be no matches
762 'contact_id_a' => $this->_cId_b
,
764 $result = $this->callAPISuccess('relationship', 'get', $params);
765 $this->assertEquals($result['count'], 0);
769 * Check with valid params array.
770 * (The get function will behave differently without 'contact_id' passed
772 public function testRelationshipsGetGeneric() {
774 'contact_id_a' => $this->_cId_a
,
775 'contact_id_b' => $this->_cId_b
,
776 'relationship_type_id' => $this->_relTypeID
,
777 'start_date' => '2011-01-01',
778 'end_date' => '2013-01-01',
782 $this->callAPISuccess('relationship', 'create', $relParams);
786 'contact_id_b' => $this->_cId_b
,
788 $this->callAPISuccess('relationship', 'get', $params);
792 * Test retrieving only current relationships.
794 public function testGetIsCurrent() {
796 'contact_id_a' => $this->_cId_a
,
797 'contact_id_b' => $this->_cId_b2
,
798 'relationship_type_id' => $this->_relTypeID
,
799 'start_date' => '2008-12-20',
802 $this->callAPISuccess('relationship', 'create', $rel2Params);
803 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params
);
806 'filters' => array('is_current' => 1),
808 $description = "Demonstrates is_current filter.";
809 $subfile = 'filterIsCurrent';
810 //no relationship has been created
811 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
812 $this->assertEquals($result['count'], 1);
813 $this->AssertEquals($rel1['id'], $result['id']);
815 // now try not started
816 $rel2Params['is_active'] = 1;
817 $rel2Params['start_date'] = 'tomorrow';
818 $this->callAPISuccess('relationship', 'create', $rel2Params);
819 $result = $this->callAPISuccess('relationship', 'get', $getParams);
820 $this->assertEquals($result['count'], 1);
821 $this->AssertEquals($rel1['id'], $result['id']);
824 $rel2Params['is_active'] = 1;
825 $rel2Params['start_date'] = 'last week';
826 $rel2Params['end_date'] = 'yesterday';
827 $this->callAPISuccess('relationship', 'create', $rel2Params);
831 * Test using various operators.
833 public function testGetTypeOperators() {
834 $relTypeParams = array(
835 'name_a_b' => 'Relation 3 for delete',
836 'name_b_a' => 'Relation 6 for delete',
837 'description' => 'Testing relationship type 2',
838 'contact_type_a' => 'Individual',
839 'contact_type_b' => 'Organization',
843 $relationType2 = $this->relationshipTypeCreate($relTypeParams);
844 $relTypeParams = array(
845 'name_a_b' => 'Relation 8 for delete',
846 'name_b_a' => 'Relation 9 for delete',
847 'description' => 'Testing relationship type 7',
848 'contact_type_a' => 'Individual',
849 'contact_type_b' => 'Organization',
853 $relationType3 = $this->relationshipTypeCreate($relTypeParams);
855 $relTypeParams = array(
856 'name_a_b' => 'Relation 6 for delete',
857 'name_b_a' => 'Relation 88for delete',
858 'description' => 'Testing relationship type 00',
859 'contact_type_a' => 'Individual',
860 'contact_type_b' => 'Organization',
864 $relationType4 = $this->relationshipTypeCreate($relTypeParams);
866 $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params
);
867 $rel2 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
868 array('relationship_type_id' => $relationType2)));
869 $rel3 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
870 array('relationship_type_id' => $relationType3)));
871 $rel4 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params
,
872 array('relationship_type_id' => $relationType4)));
875 'relationship_type_id' => array('IN' => array($relationType2, $relationType3)),
878 $description = "Demonstrates use of IN filter.";
879 $subfile = 'INRelationshipType';
881 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
882 $this->assertEquals($result['count'], 2);
883 $this->AssertEquals(array($rel2['id'], $rel3['id']), array_keys($result['values']));
885 $description = "Demonstrates use of NOT IN filter.";
886 $subfile = 'NotInRelationshipType';
888 'relationship_type_id' => array('NOT IN' => array($relationType2, $relationType3)),
890 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
891 $this->assertEquals($result['count'], 2);
892 $this->AssertEquals(array($rel1['id'], $rel4['id']), array_keys($result['values']));
894 $description = "Demonstrates use of BETWEEN filter.";
895 $subfile = 'BetweenRelationshipType';
897 'relationship_type_id' => array('BETWEEN' => array($relationType2, $relationType4)),
899 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
900 $this->assertEquals($result['count'], 3);
901 $this->AssertEquals(array($rel2['id'], $rel3['id'], $rel4['id']), array_keys($result['values']));
903 $description = "Demonstrates use of Not BETWEEN filter.";
904 $subfile = 'NotBetweenRelationshipType';
906 'relationship_type_id' => array('NOT BETWEEN' => array($relationType2, $relationType4)),
908 $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__
, __FILE__
, $description, $subfile);
909 $this->assertEquals($result['count'], 1);
910 $this->AssertEquals(array($rel1['id']), array_keys($result['values']));
915 * Check with invalid relationshipType Id.
917 public function testRelationshipTypeAddInvalidId() {
918 $relTypeParams = array(
920 'name_a_b' => 'Relation 1 for delete',
921 'name_b_a' => 'Relation 2 for delete',
922 'contact_type_a' => 'Individual',
923 'contact_type_b' => 'Organization',
925 $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
926 'id is not a valid integer');
930 * Check with valid data with contact_b.
932 public function testGetRelationshipWithContactB() {
934 'contact_id_a' => $this->_cId_a
,
935 'contact_id_b' => $this->_cId_b
,
936 'relationship_type_id' => $this->_relTypeID
,
937 'start_date' => '2011-01-01',
938 'end_date' => '2013-01-01',
942 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
945 'contact_id' => $this->_cId_a
,
948 $result = $this->callAPISuccess('relationship', 'get', $contacts);
949 $this->assertGreaterThan(0, $result['count']);
951 'id' => $relationship['id'],
953 $this->callAPISuccess('relationship', 'delete', $params);
954 $this->relationshipTypeDelete($this->_relTypeID
);
958 * Check with valid data with relationshipTypes.
960 public function testGetRelationshipWithRelTypes() {
962 'contact_id_a' => $this->_cId_a
,
963 'contact_id_b' => $this->_cId_b
,
964 'relationship_type_id' => $this->_relTypeID
,
965 'start_date' => '2011-01-01',
966 'end_date' => '2013-01-01',
970 $relationship = $this->callAPISuccess('relationship', 'create', $relParams);
973 'contact_id' => $this->_cId_a
,
975 $this->callAPISuccess('relationship', 'get', $contact_a);
978 'id' => $relationship['id'],
980 $this->callAPISuccess('relationship', 'delete', $params);
981 $this->relationshipTypeDelete($this->_relTypeID
);
985 * Checks that passing in 'contact_id' + a relationship type
986 * will filter by relationship type (relationships go in both directions)
987 * as relationship api does a reciprocal check if contact_id provided
989 * We should get 1 result without or with correct relationship type id & 0 with
992 public function testGetRelationshipByTypeReciprocal() {
993 $created = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
994 $result = $this->callAPISuccess($this->_entity
, 'get', array(
995 'contact_id' => $this->_cId_a
,
996 'relationship_type_id' => $this->_relTypeID
,
998 $this->assertEquals(1, $result['count']);
999 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1000 'contact_id' => $this->_cId_a
,
1001 'relationship_type_id' => $this->_relTypeID +
1,
1003 $this->assertEquals(0, $result['count']);
1004 $this->callAPISuccess($this->_entity
, 'delete', array('id' => $created['id']));
1008 * Checks that passing in 'contact_id_b' + a relationship type
1009 * will filter by relationship type for contact b
1011 * We should get 1 result without or with correct relationship type id & 0 with
1014 public function testGetRelationshipByTypeDAO() {
1015 $this->_ids
['relationship'] = $this->callAPISuccess($this->_entity
, 'create', array('format.only_id' => TRUE) +
1017 $this->callAPISuccess($this->_entity
, 'getcount', array(
1018 'contact_id_a' => $this->_cId_a
,
1021 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1022 'contact_id_a' => $this->_cId_a
,
1023 'relationship_type_id' => $this->_relTypeID
,
1025 $this->assertEquals(1, $result['count']);
1026 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1027 'contact_id_a' => $this->_cId_a
,
1028 'relationship_type_id' => $this->_relTypeID +
1,
1030 $this->assertEquals(0, $result['count']);
1034 * Checks that passing in 'contact_id_b' + a relationship type
1035 * will filter by relationship type for contact b
1037 * We should get 1 result without or with correct relationship type id & 0 with
1040 public function testGetRelationshipByTypeArrayDAO() {
1041 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1042 $org3 = $this->organizationCreate();
1043 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1044 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1047 $this->callAPISuccess($this->_entity
, 'create',
1048 array_merge($this->_params
, array(
1049 'relationship_type_id' => $relType2,
1050 'contact_id_b' => $this->_cId_b2
,
1055 $this->callAPISuccess($this->_entity
, 'create',
1056 array_merge($this->_params
, array(
1057 'relationship_type_id' => $relType3,
1058 'contact_id_b' => $org3,
1062 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1063 'contact_id_a' => $this->_cId_a
,
1064 'relationship_type_id' => array('IN' => array($this->_relTypeID
, $relType3)),
1067 $this->assertEquals(2, $result['count']);
1068 foreach ($result['values'] as $key => $value) {
1069 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID
, $relType3)));
1074 * Checks that passing in 'contact_id_b' + a relationship type
1075 * will filter by relationship type for contact b
1077 * We should get 1 result without or with correct relationship type id & 0 with
1080 public function testGetRelationshipByTypeArrayReciprocal() {
1081 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1082 $org3 = $this->organizationCreate();
1083 // lets just assume built in ones aren't being messed with!
1088 $this->callAPISuccess($this->_entity
, 'create',
1089 array_merge($this->_params
, array(
1090 'relationship_type_id' => $relType2,
1091 'contact_id_b' => $this->_cId_b2
,
1096 $this->callAPISuccess($this->_entity
, 'create',
1097 array_merge($this->_params
, array(
1098 'relationship_type_id' => $relType3,
1099 'contact_id_b' => $org3,
1103 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1104 'contact_id' => $this->_cId_a
,
1105 'relationship_type_id' => array('IN' => array($this->_relTypeID
, $relType3)),
1108 $this->assertEquals(2, $result['count']);
1109 foreach ($result['values'] as $key => $value) {
1110 $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID
, $relType3)));
1115 * Test relationship get by membership type.
1117 * Checks that passing in 'contact_id_b' + a relationship type
1118 * will filter by relationship type for contact b
1120 * We should get 1 result without or with correct relationship type id & 0 with
1123 public function testGetRelationshipByMembershipTypeDAO() {
1124 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1125 $org3 = $this->organizationCreate();
1127 $relType2 = 5; // lets just assume built in ones aren't being messed with!
1128 $relType3 = 6; // lets just assume built in ones aren't being messed with!
1130 $memberType = $this->membershipTypeCreate(array(
1131 'relationship_type_id' => CRM_Core_DAO
::VALUE_SEPARATOR
. $relType1 . CRM_Core_DAO
::VALUE_SEPARATOR
. $relType3 . CRM_Core_DAO
::VALUE_SEPARATOR
,
1132 'relationship_direction' => CRM_Core_DAO
::VALUE_SEPARATOR
. 'a_b' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'b_a' . CRM_Core_DAO
::VALUE_SEPARATOR
,
1136 $this->callAPISuccess($this->_entity
, 'create',
1137 array_merge($this->_params
, array(
1138 'relationship_type_id' => $relType2,
1139 'contact_id_b' => $this->_cId_b2
,
1144 $this->callAPISuccess($this->_entity
, 'create',
1145 array_merge($this->_params
, array(
1146 'relationship_type_id' => $relType3,
1147 'contact_id_b' => $org3,
1151 // Relationship 4 with reversal.
1152 $this->callAPISuccess($this->_entity
, 'create',
1153 array_merge($this->_params
, array(
1154 'relationship_type_id' => $relType1,
1155 'contact_id_a' => $this->_cId_a
,
1156 'contact_id_b' => $this->_cId_a_2
,
1160 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1161 'contact_id_a' => $this->_cId_a
,
1162 'membership_type_id' => $memberType,
1164 // although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1165 $this->assertEquals(1, $result['count']);
1166 foreach ($result['values'] as $key => $value) {
1167 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1)));
1172 * Checks that passing in 'contact_id_b' + a relationship type
1173 * will filter by relationship type for contact b
1175 * We should get 1 result without or with correct relationship type id & 0 with
1178 public function testGetRelationshipByMembershipTypeReciprocal() {
1179 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1180 $org3 = $this->organizationCreate();
1182 // Let's just assume built in ones aren't being messed with!
1186 $memberType = $this->membershipTypeCreate(array(
1187 'relationship_type_id' => CRM_Core_DAO
::VALUE_SEPARATOR
. $relType1 . CRM_Core_DAO
::VALUE_SEPARATOR
. $relType3 . CRM_Core_DAO
::VALUE_SEPARATOR
,
1188 'relationship_direction' => CRM_Core_DAO
::VALUE_SEPARATOR
. 'a_b' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'b_a' . CRM_Core_DAO
::VALUE_SEPARATOR
,
1192 $this->callAPISuccess($this->_entity
, 'create',
1193 array_merge($this->_params
, array(
1194 'relationship_type_id' => $relType2,
1195 'contact_id_b' => $this->_cId_b2
,
1200 $this->callAPISuccess($this->_entity
, 'create',
1201 array_merge($this->_params
, array(
1202 'relationship_type_id' => $relType3,
1203 'contact_id_b' => $org3,
1207 // Relationship 4 with reversal.
1208 $this->callAPISuccess($this->_entity
, 'create',
1209 array_merge($this->_params
, array(
1210 'relationship_type_id' => $relType1,
1211 'contact_id_a' => $this->_cId_a
,
1212 'contact_id_b' => $this->_cId_a_2
,
1216 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1217 'contact_id' => $this->_cId_a
,
1218 'membership_type_id' => $memberType,
1220 // Although our contact has more than one relationship we have passed them in as contact_id_a & can't get reciprocal
1221 $this->assertEquals(2, $result['count']);
1223 foreach ($result['values'] as $key => $value) {
1224 $this->assertTrue(in_array($value['relationship_type_id'], array($relType1, $relType3)));
1229 * Check for e-notices on enable & disable as reported in CRM-14350
1231 public function testSetActive() {
1232 $relationship = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
1233 $this->callAPISuccess($this->_entity
, 'create', array('id' => $relationship['id'], 'is_active' => 0));
1234 $this->callAPISuccess($this->_entity
, 'create', array('id' => $relationship['id'], 'is_active' => 1));
1238 * Test creating related memberships.
1240 public function testCreateRelatedMembership() {
1241 $relatedMembershipType = $this->callAPISuccess('MembershipType', 'create', array(
1242 'name' => 'Membership with Related',
1243 'member_of_contact_id' => 1,
1244 'financial_type_id' => 1,
1245 'minimum_fee' => 0.00,
1246 'duration_unit' => 'year',
1247 'duration_interval' => 1,
1248 'period_type' => 'rolling',
1249 'relationship_type_id' => $this->_relTypeID
,
1250 'relationship_direction' => 'b_a',
1251 'visibility' => 'Public',
1254 'domain_id' => CRM_Core_Config
::domainID(),
1256 $originalMembership = $this->callAPISuccess('Membership', 'create', array(
1257 'membership_type_id' => $relatedMembershipType['id'],
1258 'contact_id' => $this->_cId_b
,
1260 $this->callAPISuccess('Relationship', 'create', array(
1261 'relationship_type_id' => $this->_relTypeID
,
1262 'contact_id_a' => $this->_cId_a
,
1263 'contact_id_b' => $this->_cId_b
,
1265 $contactAMembership = $this->callAPISuccessGetSingle('membership', array('contact_id' => $this->_cId_a
));
1266 $this->assertEquals($originalMembership['id'], $contactAMembership['owner_membership_id']);
1268 // Adding a relationship with a future start date should NOT create a membership
1269 $this->callAPISuccess('Relationship', 'create', array(
1270 'relationship_type_id' => $this->_relTypeID
,
1271 'contact_id_a' => $this->_cId_a_2
,
1272 'contact_id_b' => $this->_cId_b
,
1273 'start_date' => 'now + 1 week',
1275 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_cId_a_2
), 0);
1277 // Deleting the organization should cause the related membership to be deleted.
1278 $this->callAPISuccess('contact', 'delete', array('id' => $this->_cId_b
));
1279 $this->callAPISuccessGetCount('membership', array('contact_id' => $this->_cId_a
), 0);