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