3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Test class for CRM_Contact_BAO_Relationship
18 class CRM_Contact_BAO_RelationshipTest
extends CiviUnitTestCase
{
21 * Tears down the fixture, for example, closes a network connection.
23 * This method is called after a test is executed.
25 * @throws \CRM_Core_Exception
27 protected function tearDown(): void
{
29 'civicrm_relationship_type',
30 'civicrm_relationship',
38 * Test Relationship Type Options Will Return Specified Type
40 * @throws \CRM_Core_Exception
42 public function testRelationshipTypeOptionsWillReturnSpecifiedType() {
43 $orgToOrgType = 'A_B_relationship';
44 $orgToOrgReverseType = 'B_A_relationship';
45 $this->callAPISuccess('RelationshipType', 'create', [
46 'name_a_b' => $orgToOrgType,
47 'name_b_a' => $orgToOrgReverseType,
48 'contact_type_a' => 'Organization',
49 'contact_type_b' => 'Organization',
52 $result = civicrm_api3('Relationship', 'getoptions', [
53 'field' => 'relationship_type_id',
55 'contact_type' => 'Organization',
57 $this->assertContains($orgToOrgType, $result['values']);
58 $this->assertContains($orgToOrgReverseType, $result['values']);
60 $result = civicrm_api3('Relationship', 'getoptions', [
61 'field' => 'relationship_type_id',
63 'contact_type' => 'Individual',
66 $this->assertNotContains($orgToOrgType, $result['values']);
67 $this->assertNotContains($orgToOrgReverseType, $result['values']);
70 public function testContactIdAndRelationshipIdWillBeUsedInFilter() {
71 $individual = civicrm_api3('Contact', 'create', [
72 'display_name' => 'Individual A',
73 'contact_type' => 'Individual',
75 $organization = civicrm_api3('Contact', 'create', [
76 'organization_name' => 'Organization B',
77 'contact_type' => 'Organization',
80 $personToOrgType = 'A_B_relationship';
81 $orgToPersonType = 'B_A_relationship';
83 $orgToPersonTypeId = civicrm_api3('RelationshipType', 'create', [
84 'name_a_b' => $personToOrgType,
85 'name_b_a' => $orgToPersonType,
86 'contact_type_a' => 'Individual',
87 'contact_type_b' => 'Organization',
90 $personToPersonType = 'A_B_alt_relationship';
91 $personToPersonReverseType = 'B_A_alt_relationship';
93 civicrm_api3('RelationshipType', 'create', [
94 'name_a_b' => $personToPersonType,
95 'name_b_a' => $personToPersonReverseType,
96 'contact_type_a' => 'Individual',
97 'contact_type_b' => 'Individual',
100 // create a relationship individual => organization
101 $relationship = civicrm_api3('Relationship', 'create', [
102 'contact_id_a' => $individual['id'],
103 'contact_id_b' => $organization['id'],
104 'relationship_type_id' => $orgToPersonTypeId,
107 $options = civicrm_api3('Relationship', 'getoptions', [
108 'field' => 'relationship_type_id',
110 'relationship_id' => (string) $relationship['id'],
111 'contact_id' => $individual['id'],
114 // for this relationship only individual=>organization is possible
115 $this->assertContains($personToOrgType, $options['values']);
116 $this->assertNotContains($orgToPersonType, $options['values']);
118 // by passing relationship ID we know that the "B" side is an organization
119 $this->assertNotContains($personToPersonType, $options['values']);
120 $this->assertNotContains($personToPersonReverseType, $options['values']);
122 $options = civicrm_api3('Relationship', 'getoptions', [
123 'field' => 'relationship_type_id',
125 'contact_id' => $individual['id'],
128 // for this result we only know that "A" must be an individual
129 $this->assertContains($personToOrgType, $options['values']);
130 $this->assertNotContains($orgToPersonType, $options['values']);
132 // unlike when we pass relationship type ID there is no filter by "B" type
133 $this->assertContains($personToPersonType, $options['values']);
134 $this->assertContains($personToPersonReverseType, $options['values']);
138 * Test removeRelationshipTypeDuplicates method.
140 * @dataProvider getRelationshipTypeDuplicates
142 public function testRemoveRelationshipTypeDuplicates($relationshipTypeList, $suffix = NULL, $expected, $description) {
143 $result = CRM_Contact_BAO_Relationship
::removeRelationshipTypeDuplicates($relationshipTypeList, $suffix);
144 $this->assertEquals($expected, $result, "Failure on set '$description'");
147 public function getRelationshipTypeDuplicates() {
148 $relationshipTypeList = [
149 '1_a_b' => 'duplicate one',
150 '1_b_a' => 'duplicate one',
156 $relationshipTypeList,
159 '1_a_b' => 'duplicate one',
166 $relationshipTypeList,
169 '1_b_a' => 'duplicate one',
176 $relationshipTypeList,
179 '1_a_b' => 'duplicate one',
186 $relationshipTypeList,
189 '1_a_b' => 'duplicate one',
193 'With suffix "" (empty string)',
200 * Test that two similar memberships are not created for two relationships
202 * @throws \CRM_Core_Exception
204 public function testSingleMembershipForTwoRelationships() {
205 $individualID = $this->individualCreate(['display_name' => 'Individual A']);
206 $organisationID = $this->organizationCreate(['organization_name' => 'Organization B']);
207 $membershipOrganisationID = $this->organizationCreate(['organization_name' => 'Membership Organization']);
208 $orgToPersonTypeId1 = $this->relationshipTypeCreate(['name_a_b' => 'Inherited_Relationship_1_A_B', 'name_b_a' => 'Inherited_Relationship_1_B_A']);
209 $orgToPersonTypeId2 = $this->relationshipTypeCreate(['name_a_b' => 'Inherited_Relationship_2_A_B', 'name_b_a' => 'Inherited_Relationship_2_B_A']);
211 $membershipType = $this->callAPISuccess('MembershipType', 'create', [
212 'member_of_contact_id' => $membershipOrganisationID,
213 'financial_type_id' => 'Member Dues',
214 'duration_unit' => 'year',
215 'duration_interval' => 1,
216 'period_type' => 'rolling',
217 'name' => 'Inherited Membership',
218 'relationship_type_id' => [$orgToPersonTypeId1, $orgToPersonTypeId2],
219 'relationship_direction' => ['b_a', 'b_a'],
221 $membershipType = $this->callAPISuccessGetSingle('MembershipType', ['id' => $membershipType['id']]);
222 // Check the metadata worked....
223 $this->assertEquals([$orgToPersonTypeId1, $orgToPersonTypeId2], $membershipType['relationship_type_id']);
224 $this->assertEquals(['b_a', 'b_a'], $membershipType['relationship_direction']);
226 $startDate = date('Y-m') . '-19';
227 $joinDate = date('Y-m', strtotime('1 month ago')) . '-19';
228 $this->callAPISuccess('Membership', 'create', [
229 'membership_type_id' => $membershipType['id'],
230 'contact_id' => $organisationID,
231 'start_date' => $startDate,
232 'join_date' => $joinDate,
235 $relationshipOne = $this->callAPISuccess('Relationship', 'create', [
236 'contact_id_a' => $individualID,
237 'contact_id_b' => $organisationID,
238 'relationship_type_id' => $orgToPersonTypeId1,
240 $this->callAPISuccessGetCount('Membership', ['contact_id' => $individualID], 1);
241 $relationshipTwo = $this->callAPISuccess('Relationship', 'create', [
242 'contact_id_a' => $individualID,
243 'contact_id_b' => $organisationID,
244 'relationship_type_id' => $orgToPersonTypeId2,
246 $this->callAPISuccessGetCount('Membership', ['contact_id' => $individualID], 1);
248 $inheritedMembership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $individualID]);
249 $this->assertEquals($startDate, $inheritedMembership['start_date']);
250 $this->assertEquals($joinDate, $inheritedMembership['join_date']);
252 $this->callAPISuccessGetCount('Membership', ['contact_id' => $organisationID], 1);
253 // Disable the relationship & check the membership is not removed because the other relationship is still valid.
254 $relationshipOne['is_active'] = 0;
255 $this->callAPISuccess('Relationship', 'create', array_merge($relationshipOne, ['is_active' => 0]));
256 $this->callAPISuccessGetCount('Membership', ['contact_id' => $individualID], 1);
258 $relationshipTwo['is_active'] = 0;
259 $this->callAPISuccess('Relationship', 'create', $relationshipTwo);
260 $this->callAPISuccessGetCount('Membership', ['contact_id' => $individualID], 0);
262 $relationshipOne['is_active'] = 1;
263 $this->callAPISuccess('Relationship', 'create', $relationshipOne);
264 $this->callAPISuccessGetCount('Membership', ['contact_id' => $individualID], 1);
266 $relationshipTwo['is_active'] = 1;
267 $this->callAPISuccess('Relationship', 'create', $relationshipTwo);
268 $this->callAPISuccessGetCount('Membership', ['contact_id' => $individualID], 1);
269 $this->callAPISuccess('Relationship', 'delete', ['id' => $relationshipTwo['id']]);
270 $this->callAPISuccessGetCount('Membership', ['contact_id' => $individualID], 1);
271 $this->callAPISuccess('Relationship', 'delete', ['id' => $relationshipOne['id']]);
272 $this->callAPISuccessGetCount('Membership', ['contact_id' => $individualID], 0);
277 * Test CRM_Contact_BAO_Relationship::add() function directly.
279 * In general it's preferred to use the Relationship-create api since it does
280 * checks and such before calling add(). There are already some good tests
281 * for the api, but since it does some more business logic after too the
282 * tests might not be checking exactly the same thing.
284 public function testBAOAdd() {
286 $relationship_type_id_1 = $this->relationshipTypeCreate([
287 'name_a_b' => 'Food poison tester is',
288 'name_b_a' => 'Food poison tester for',
289 'contact_type_a' => 'Individual',
290 'contact_type_b' => 'Individual',
294 $contact_id_1 = $this->individualCreate();
295 $contact_id_2 = $this->individualCreate([], 1);
297 // create new relationship (using BAO)
299 'relationship_type_id' => $relationship_type_id_1,
300 'contact_id_a' => $contact_id_1,
301 'contact_id_b' => $contact_id_2,
303 $relationshipObj = CRM_Contact_BAO_Relationship
::add($params);
304 $this->assertEquals($relationshipObj->relationship_type_id
, $relationship_type_id_1);
305 $this->assertEquals($relationshipObj->contact_id_a
, $contact_id_1);
306 $this->assertEquals($relationshipObj->contact_id_b
, $contact_id_2);
307 $this->assertEquals($relationshipObj->is_active
, 1);
309 // demonstrate PR 15103 - should fail before the patch and pass after
310 $today = date('Ymd');
312 'id' => $relationshipObj->id
,
313 'end_date' => $today,
315 $relationshipObj = CRM_Contact_BAO_Relationship
::add($params);
316 $this->assertEquals($relationshipObj->relationship_type_id
, $relationship_type_id_1);
317 $this->assertEquals($relationshipObj->contact_id_a
, $contact_id_1);
318 $this->assertEquals($relationshipObj->contact_id_b
, $contact_id_2);
319 $this->assertEquals($relationshipObj->is_active
, 1);
320 $this->assertEquals($relationshipObj->end_date
, $today);