Merge pull request #4629 from systopia/CRM-15665
[civicrm-core.git] / tests / phpunit / CRM / Contact / BAO / ContactType / RelationshipTest.php
1 <?php
2 require_once 'CiviTest/CiviUnitTestCase.php';
3 require_once 'CiviTest/Contact.php';
4
5 /**
6 * Class CRM_Contact_BAO_ContactType_RelationshipTest
7 */
8 class CRM_Contact_BAO_ContactType_RelationshipTest extends CiviUnitTestCase {
9 /**
10 * @return array
11 */
12 function get_info() {
13 return array(
14 'name' => 'Relationship Subtype',
15 'description' => 'Test Relattionship for subtype.',
16 'group' => 'CiviCRM BAO Tests',
17 );
18 }
19
20 function setUp() {
21 parent::setUp();
22
23 //create contact subtypes
24 $params = array(
25 'label' => 'indivi_student',
26 'name' => 'indivi_student',
27 // Individual
28 'parent_id' => 1,
29 'is_active' => 1,
30 );
31 $result = CRM_Contact_BAO_ContactType::add($params);
32 $this->student = $params['name'];
33
34 $params = array(
35 'label' => 'indivi_parent',
36 'name' => 'indivi_parent',
37 // Individual
38 'parent_id' => 1,
39 'is_active' => 1,
40 );
41 $result = CRM_Contact_BAO_ContactType::add($params);
42 $this->parent = $params['name'];
43
44 $params = array(
45 'label' => 'org_sponsor',
46 'name' => 'org_sponsor',
47 // Organization
48 'parent_id' => 3,
49 'is_active' => 1,
50 );
51 $result = CRM_Contact_BAO_ContactType::add($params);
52 $this->sponsor = $params['name'];
53
54 //create contacts
55 $params = array(
56 'first_name' => 'Anne',
57 'last_name' => 'Grant',
58 'contact_type' => 'Individual',
59 );
60 $this->individual = Contact::create($params);
61
62 $params = array(
63 'first_name' => 'Bill',
64 'last_name' => 'Adams',
65 'contact_type' => 'Individual',
66 'contact_sub_type' => $this->student,
67 );
68 $this->indivi_student = Contact::create($params);
69
70 $params = array(
71 'first_name' => 'Alen',
72 'last_name' => 'Adams',
73 'contact_type' => 'Individual',
74 'contact_sub_type' => $this->parent,
75 );
76 $this->indivi_parent = Contact::create($params);
77
78 $params = array(
79 'organization_name' => 'Compumentor',
80 'contact_type' => 'Organization',
81 );
82 $this->organization = Contact::create($params);
83
84 $params = array(
85 'organization_name' => 'Conservation Corp',
86 'contact_type' => 'Organization',
87 'contact_sub_type' => $this->sponsor,
88 );
89 $this->organization_sponsor = Contact::create($params);
90 }
91
92 function tearDown() {
93 $this->quickCleanup(array('civicrm_contact'));
94
95 $query = "
96 DELETE FROM civicrm_contact_type
97 WHERE name IN ('{$this->student}','{$this->parent}','{$this->sponsor}');
98 ";
99 CRM_Core_DAO::executeQuery($query);
100 }
101
102 /**
103 * Methods create relationshipType with valid data
104 * success expected
105 *
106 */
107 function testRelationshipTypeAddIndiviParent() {
108 //check Individual to Parent RelationshipType
109 $params = array(
110 'name_a_b' => 'indivToparent',
111 'name_b_a' => 'parentToindiv',
112 'contact_type_a' => 'Individual',
113 'contact_type_b' => 'Individual',
114 'contact_sub_type_b' => $this->parent,
115 );
116 $ids = array();
117 $result = CRM_Contact_BAO_RelationshipType::add($params, $ids);
118 $this->assertEquals($result->name_a_b, 'indivToparent');
119 $this->assertEquals($result->contact_type_a, 'Individual');
120 $this->assertEquals($result->contact_type_b, 'Individual');
121 $this->assertEquals($result->contact_sub_type_b, $this->parent);
122 $this->relationshipTypeDelete($result->id);
123 }
124
125 function testRelationshipTypeAddSponcorIndivi() {
126 //check Sponcor to Individual RelationshipType
127 $params = array(
128 'name_a_b' => 'SponsorToIndiv',
129 'name_b_a' => 'IndivToSponsor',
130 'contact_type_a' => 'Organization',
131 'contact_sub_type_a' => $this->sponsor,
132 'contact_type_b' => 'Individual',
133 );
134 $ids = array();
135 $result = CRM_Contact_BAO_RelationshipType::add($params, $ids);
136 $this->assertEquals($result->name_a_b, 'SponsorToIndiv');
137 $this->assertEquals($result->contact_type_a, 'Organization');
138 $this->assertEquals($result->contact_sub_type_a, $this->sponsor);
139 $this->assertEquals($result->contact_type_b, 'Individual');
140 $this->relationshipTypeDelete($result->id);
141 }
142
143 function testRelationshipTypeAddStudentSponcor() {
144 //check Student to Sponcer RelationshipType
145 $params = array(
146 'name_a_b' => 'StudentToSponser',
147 'name_b_a' => 'SponsorToStudent',
148 'contact_type_a' => 'Individual',
149 'contact_sub_type_a' => $this->student,
150 'contact_type_b' => 'Organization',
151 'contact_sub_type_b' => $this->sponsor,
152 );
153 $ids = array();
154 $result = CRM_Contact_BAO_RelationshipType::add($params, $ids);
155 $this->assertEquals($result->name_a_b, 'StudentToSponser');
156 $this->assertEquals($result->contact_type_a, 'Individual');
157 $this->assertEquals($result->contact_sub_type_a, $this->student);
158 $this->assertEquals($result->contact_type_b, 'Organization');
159 $this->assertEquals($result->contact_sub_type_b, $this->sponsor);
160 $this->relationshipTypeDelete($result->id);
161 }
162
163 /**
164 * Methods create relationshipe within same contact type with invalid Relationships
165 *
166 */
167 function testRelationshipCreateInvalidWithinSameType() {
168 //check for Individual to Parent
169 $relTypeParams = array(
170 'name_a_b' => 'indivToparent',
171 'name_b_a' => 'parentToindiv',
172 'contact_type_a' => 'Individual',
173 'contact_type_b' => 'Individual',
174 'contact_sub_type_b' => $this->parent,
175 );
176 $relTypeIds = array();
177 $relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams, $relTypeIds);
178 $params = array(
179 'relationship_type_id' => $relType->id . '_a_b',
180 'contact_check' => array($this->indivi_student => 1),
181 );
182 $ids = array('contact' => $this->individual);
183
184 list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::create($params, $ids);
185
186 $this->assertEquals($invalid, 1, 'In line ' . __LINE__);
187 $this->assertEquals(empty($relationshipIds), TRUE, 'In line ' . __LINE__);
188 $this->relationshipTypeDelete($relType->id);
189 }
190
191 /**
192 * Methods create relationshipe within diff contact type with invalid Relationships
193 *
194 */
195 function testRelCreateInvalidWithinDiffTypeSpocorIndivi() {
196 //check for Sponcer to Individual
197 $relTypeParams = array(
198 'name_a_b' => 'SponsorToIndiv',
199 'name_b_a' => 'IndivToSponsor',
200 'contact_type_a' => 'Organization',
201 'contact_sub_type_a' => $this->sponsor,
202 'contact_type_b' => 'Individual',
203 );
204 $relTypeIds = array();
205 $relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams, $relTypeIds);
206 $params = array(
207 'relationship_type_id' => $relType->id . '_a_b',
208 'contact_check' => array($this->individual => 1),
209 );
210 $ids = array('contact' => $this->indivi_parent);
211
212 list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::create($params, $ids);
213
214 $this->assertEquals($invalid, 1, 'In line ' . __LINE__);
215 $this->assertEquals(empty($relationshipIds), TRUE, 'In line ' . __LINE__);
216 $this->relationshipTypeDelete($relType->id);
217 }
218
219 function testRelCreateInvalidWithinDiffTypeStudentSponcor() {
220 //check for Student to Sponcer
221 $relTypeParams = array(
222 'name_a_b' => 'StudentToSponser',
223 'name_b_a' => 'SponsorToStudent',
224 'contact_type_a' => 'Individual',
225 'contact_sub_type_a' => $this->student,
226 'contact_type_b' => 'Organization',
227 'contact_sub_type_b' => 'Sponser',
228 );
229 $relTypeIds = array();
230 $relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams, $relTypeIds);
231 $params = array(
232 'relationship_type_id' => $relType->id . '_a_b',
233 'contact_check' => array($this->individual => 1),
234 );
235 $ids = array('contact' => $this->indivi_parent);
236
237 list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::create($params, $ids);
238
239 $this->assertEquals($invalid, 1, 'In line ' . __LINE__);
240 $this->assertEquals(empty($relationshipIds), TRUE, 'In line ' . __LINE__);
241 $this->relationshipTypeDelete($relType->id);
242 }
243
244 /**
245 * Methods create relationshipe within same contact type with valid data
246 * success expected
247 *
248 */
249 function testRelationshipCreateWithinSameType() {
250 //check for Individual to Parent
251 $relTypeParams = array(
252 'name_a_b' => 'indivToparent',
253 'name_b_a' => 'parentToindiv',
254 'contact_type_a' => 'Individual',
255 'contact_type_b' => 'Individual',
256 'contact_sub_type_b' => $this->parent,
257 );
258 $relTypeIds = array();
259 $relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams, $relTypeIds);
260 $params = array(
261 'relationship_type_id' => $relType->id . '_a_b',
262 'is_active' => 1,
263 'contact_check' => array($this->indivi_parent => $this->indivi_parent),
264 );
265 $ids = array('contact' => $this->individual);
266 list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::create($params, $ids);
267
268 $this->assertEquals($valid, 1, 'In line ' . __LINE__);
269 $this->assertEquals(empty($relationshipIds), FALSE, 'In line ' . __LINE__);
270 $this->relationshipTypeDelete($relType->id);
271 }
272
273 /**
274 * Methods create relationshipe within different contact type with valid data
275 * success expected
276 *
277 */
278 function testRelCreateWithinDiffTypeSponsorIndivi() {
279 //check for Sponcer to Individual
280 $relTypeParams = array(
281 'name_a_b' => 'SponsorToIndiv',
282 'name_b_a' => 'IndivToSponsor',
283 'contact_type_a' => 'Organization',
284 'contact_sub_type_a' => $this->sponsor,
285 'contact_type_b' => 'Individual',
286 );
287 $relTypeIds = array();
288 $relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams, $relTypeIds);
289 $params = array(
290 'relationship_type_id' => $relType->id . '_a_b',
291 'is_active' => 1,
292 'contact_check' => array($this->indivi_student => 1),
293 );
294 $ids = array('contact' => $this->organization_sponsor);
295 list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::create($params, $ids);
296
297 $this->assertEquals($valid, 1, 'In line ' . __LINE__);
298 $this->assertEquals(empty($relationshipIds), FALSE, 'In line ' . __LINE__);
299 $this->relationshipTypeDelete($relType->id);
300 }
301
302 function testRelCreateWithinDiffTypeStudentSponsor() {
303 //check for Student to Sponcer
304 $relTypeParams = array(
305 'name_a_b' => 'StudentToSponsor',
306 'name_b_a' => 'SponsorToStudent',
307 'contact_type_a' => 'Individual',
308 'contact_sub_type_a' => $this->student,
309 'contact_type_b' => 'Organization',
310 'contact_sub_type_b' => $this->sponsor,
311 );
312 $relTypeIds = array();
313 $relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams, $relTypeIds);
314 $params = array(
315 'relationship_type_id' => $relType->id . '_a_b',
316 'is_active' => 1,
317 'contact_check' => array($this->organization_sponsor => 1),
318 );
319 $ids = array('contact' => $this->indivi_student);
320 list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::create($params, $ids);
321
322 $this->assertEquals($valid, 1, 'In line ' . __LINE__);
323 $this->assertEquals(empty($relationshipIds), FALSE, 'In line ' . __LINE__);
324 $this->relationshipTypeDelete($relType->id);
325 }
326 }
327
328
329