Merge pull request #14283 from eileenmcnaughton/db_test3
[civicrm-core.git] / tests / phpunit / CRM / Contact / BAO / RelationshipTest.php
CommitLineData
02e028a0
AS
1<?php
2/*
3 +--------------------------------------------------------------------+
2fe49090 4 | CiviCRM version 5 |
02e028a0 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
02e028a0
AS
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28/**
29 * Test class for CRM_Contact_BAO_Relationship
30 *
31 * @package CiviCRM
32 * @group headless
33 */
34class CRM_Contact_BAO_RelationshipTest extends CiviUnitTestCase {
35
36 /**
37 * Sets up the fixture, for example, opens a network connection.
38 *
39 * This method is called before a test is executed.
40 */
41 protected function setUp() {
42 parent::setUp();
43 }
44
45 /**
46 * Tears down the fixture, for example, closes a network connection.
47 *
48 * This method is called after a test is executed.
49 */
50 protected function tearDown() {
89e45979
MD
51 $this->quickCleanup([
52 'civicrm_relationship_type',
53 'civicrm_relationship',
39b959db 54 'civicrm_contact',
89e45979
MD
55 ]);
56
02e028a0
AS
57 parent::tearDown();
58 }
59
89e45979
MD
60 public function testRelationshipTypeOptionsWillReturnSpecifiedType() {
61 $orgToOrgType = 'A_B_relationship';
62 $orgToOrgReverseType = 'B_A_relationship';
63 civicrm_api3('RelationshipType', 'create', [
64 'name_a_b' => $orgToOrgType,
65 'name_b_a' => $orgToOrgReverseType,
66 'contact_type_a' => 'Organization',
67 'contact_type_b' => 'Organization',
68 ]);
69
70 $result = CRM_Contact_BAO_Relationship::buildRelationshipTypeOptions(
71 ['contact_type' => 'Organization']
72 );
73 $this->assertContains($orgToOrgType, $result);
74 $this->assertContains($orgToOrgReverseType, $result);
75
76 $result = CRM_Contact_BAO_Relationship::buildRelationshipTypeOptions(
77 ['contact_type' => 'Individual']
78 );
79
80 $this->assertNotContains($orgToOrgType, $result);
81 $this->assertNotContains($orgToOrgReverseType, $result);
82 }
83
84 public function testContactIdAndRelationshipIdWillBeUsedInFilter() {
85 $individual = civicrm_api3('Contact', 'create', [
86 'display_name' => 'Individual A',
87 'contact_type' => 'Individual',
88 ]);
89 $organization = civicrm_api3('Contact', 'create', [
90 'organization_name' => 'Organization B',
91 'contact_type' => 'Organization',
92 ]);
93
94 $personToOrgType = 'A_B_relationship';
95 $orgToPersonType = 'B_A_relationship';
96
97 $orgToPersonTypeId = civicrm_api3('RelationshipType', 'create', [
98 'name_a_b' => $personToOrgType,
99 'name_b_a' => $orgToPersonType,
100 'contact_type_a' => 'Individual',
101 'contact_type_b' => 'Organization',
102 ])['id'];
103
104 $personToPersonType = 'A_B_alt_relationship';
105 $personToPersonReverseType = 'B_A_alt_relationship';
106
107 civicrm_api3('RelationshipType', 'create', [
108 'name_a_b' => $personToPersonType,
109 'name_b_a' => $personToPersonReverseType,
110 'contact_type_a' => 'Individual',
111 'contact_type_b' => 'Individual',
112 ]);
113
114 // create a relationship individual => organization
115 $relationship = civicrm_api3('Relationship', 'create', [
116 'contact_id_a' => $individual['id'],
117 'contact_id_b' => $organization['id'],
118 'relationship_type_id' => $orgToPersonTypeId,
119 ]);
120
121 $options = CRM_Contact_BAO_Relationship::buildRelationshipTypeOptions([
122 'relationship_id' => (string) $relationship['id'],
39b959db 123 'contact_id' => $individual['id'],
89e45979
MD
124 ]);
125
126 // for this relationship only individual=>organization is possible
127 $this->assertContains($personToOrgType, $options);
128 $this->assertNotContains($orgToPersonType, $options);
129
130 // by passing relationship ID we know that the "B" side is an organization
131 $this->assertNotContains($personToPersonType, $options);
132 $this->assertNotContains($personToPersonReverseType, $options);
133
134 $options = CRM_Contact_BAO_Relationship::buildRelationshipTypeOptions([
39b959db 135 'contact_id' => $individual['id'],
89e45979
MD
136 ]);
137
138 // for this result we only know that "A" must be an individual
139 $this->assertContains($personToOrgType, $options);
140 $this->assertNotContains($orgToPersonType, $options);
141
142 // unlike when we pass relationship type ID there is no filter by "B" type
143 $this->assertContains($personToPersonType, $options);
144 $this->assertContains($personToPersonReverseType, $options);
145 }
146
02e028a0
AS
147 /**
148 * Test removeRelationshipTypeDuplicates method.
149 *
150 * @dataProvider getRelationshipTypeDuplicates
151 */
152 public function testRemoveRelationshipTypeDuplicates($relationshipTypeList, $suffix = NULL, $expected, $description) {
153 $result = CRM_Contact_BAO_Relationship::removeRelationshipTypeDuplicates($relationshipTypeList, $suffix);
154 $this->assertEquals($expected, $result, "Failure on set '$description'");
155 }
156
157 public function getRelationshipTypeDuplicates() {
158 $relationshipTypeList = array(
159 '1_a_b' => 'duplicate one',
160 '1_b_a' => 'duplicate one',
161 '2_a_b' => 'two a',
162 '2_b_a' => 'two b',
163 );
164 $data = array(
165 array(
166 $relationshipTypeList,
167 'a_b',
168 array(
169 '1_a_b' => 'duplicate one',
170 '2_a_b' => 'two a',
171 '2_b_a' => 'two b',
172 ),
173 'With suffix a_b',
174 ),
175 array(
176 $relationshipTypeList,
177 'b_a',
178 array(
179 '1_b_a' => 'duplicate one',
180 '2_a_b' => 'two a',
181 '2_b_a' => 'two b',
182 ),
183 'With suffix b_a',
184 ),
185 array(
186 $relationshipTypeList,
187 NULL,
188 array(
189 '1_a_b' => 'duplicate one',
190 '2_a_b' => 'two a',
191 '2_b_a' => 'two b',
192 ),
193 'With suffix NULL',
194 ),
195 array(
196 $relationshipTypeList,
197 NULL,
198 array(
199 '1_a_b' => 'duplicate one',
200 '2_a_b' => 'two a',
201 '2_b_a' => 'two b',
202 ),
203 'With suffix "" (empty string)',
204 ),
205 );
206 return $data;
207 }
208
209}