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