commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / 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 public function testRelationshipTypeAddIndiviParent() {
97 //check Individual to Parent RelationshipType
98 $params = array(
99 'name_a_b' => 'indivToparent',
100 'name_b_a' => 'parentToindiv',
101 'contact_type_a' => 'Individual',
102 'contact_type_b' => 'Individual',
103 'contact_sub_type_b' => $this->parent,
104 );
105 $ids = array();
106 $result = CRM_Contact_BAO_RelationshipType::add($params, $ids);
107 $this->assertEquals($result->name_a_b, 'indivToparent');
108 $this->assertEquals($result->contact_type_a, 'Individual');
109 $this->assertEquals($result->contact_type_b, 'Individual');
110 $this->assertEquals($result->contact_sub_type_b, $this->parent);
111 $this->relationshipTypeDelete($result->id);
112 }
113
114 public function testRelationshipTypeAddSponcorIndivi() {
115 //check Sponcor to Individual RelationshipType
116 $params = array(
117 'name_a_b' => 'SponsorToIndiv',
118 'name_b_a' => 'IndivToSponsor',
119 'contact_type_a' => 'Organization',
120 'contact_sub_type_a' => $this->sponsor,
121 'contact_type_b' => 'Individual',
122 );
123 $ids = array();
124 $result = CRM_Contact_BAO_RelationshipType::add($params, $ids);
125 $this->assertEquals($result->name_a_b, 'SponsorToIndiv');
126 $this->assertEquals($result->contact_type_a, 'Organization');
127 $this->assertEquals($result->contact_sub_type_a, $this->sponsor);
128 $this->assertEquals($result->contact_type_b, 'Individual');
129 $this->relationshipTypeDelete($result->id);
130 }
131
132 public function testRelationshipTypeAddStudentSponcor() {
133 //check Student to Sponcer RelationshipType
134 $params = array(
135 'name_a_b' => 'StudentToSponser',
136 'name_b_a' => 'SponsorToStudent',
137 'contact_type_a' => 'Individual',
138 'contact_sub_type_a' => $this->student,
139 'contact_type_b' => 'Organization',
140 'contact_sub_type_b' => $this->sponsor,
141 );
142 $ids = array();
143 $result = CRM_Contact_BAO_RelationshipType::add($params, $ids);
144 $this->assertEquals($result->name_a_b, 'StudentToSponser');
145 $this->assertEquals($result->contact_type_a, 'Individual');
146 $this->assertEquals($result->contact_sub_type_a, $this->student);
147 $this->assertEquals($result->contact_type_b, 'Organization');
148 $this->assertEquals($result->contact_sub_type_b, $this->sponsor);
149 $this->relationshipTypeDelete($result->id);
150 }
151
152 /**
153 * Methods create relationshipe within same contact type with invalid Relationships.
154 */
155 public function testRelationshipCreateInvalidWithinSameType() {
156 //check for Individual to Parent
157 $relTypeParams = array(
158 'name_a_b' => 'indivToparent',
159 'name_b_a' => 'parentToindiv',
160 'contact_type_a' => 'Individual',
161 'contact_type_b' => 'Individual',
162 'contact_sub_type_b' => $this->parent,
163 );
164 $relTypeIds = array();
165 $relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams, $relTypeIds);
166 $params = array(
167 'relationship_type_id' => $relType->id . '_a_b',
168 'contact_check' => array($this->indivi_student => 1),
169 );
170 $ids = array('contact' => $this->individual);
171
172 list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::legacyCreateMultiple($params, $ids);
173
174 $this->assertEquals($invalid, 1, 'In line ' . __LINE__);
175 $this->assertEquals(empty($relationshipIds), TRUE, 'In line ' . __LINE__);
176 $this->relationshipTypeDelete($relType->id);
177 }
178
179 /**
180 * Methods create relationshipe within diff contact type with invalid Relationships.
181 */
182 public function testRelCreateInvalidWithinDiffTypeSpocorIndivi() {
183 //check for Sponcer to Individual
184 $relTypeParams = array(
185 'name_a_b' => 'SponsorToIndiv',
186 'name_b_a' => 'IndivToSponsor',
187 'contact_type_a' => 'Organization',
188 'contact_sub_type_a' => $this->sponsor,
189 'contact_type_b' => 'Individual',
190 );
191 $relTypeIds = array();
192 $relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams, $relTypeIds);
193 $params = array(
194 'relationship_type_id' => $relType->id . '_a_b',
195 'contact_check' => array($this->individual => 1),
196 );
197 $ids = array('contact' => $this->indivi_parent);
198
199 list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::legacyCreateMultiple($params, $ids);
200
201 $this->assertEquals($invalid, 1, 'In line ' . __LINE__);
202 $this->assertEquals(empty($relationshipIds), TRUE, 'In line ' . __LINE__);
203 $this->relationshipTypeDelete($relType->id);
204 }
205
206 public function testRelCreateInvalidWithinDiffTypeStudentSponcor() {
207 //check for Student to Sponcer
208 $relTypeParams = array(
209 'name_a_b' => 'StudentToSponser',
210 'name_b_a' => 'SponsorToStudent',
211 'contact_type_a' => 'Individual',
212 'contact_sub_type_a' => $this->student,
213 'contact_type_b' => 'Organization',
214 'contact_sub_type_b' => 'Sponser',
215 );
216 $relTypeIds = array();
217 $relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams, $relTypeIds);
218 $params = array(
219 'relationship_type_id' => $relType->id . '_a_b',
220 'contact_check' => array($this->individual => 1),
221 );
222 $ids = array('contact' => $this->indivi_parent);
223
224 list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::legacyCreateMultiple($params, $ids);
225
226 $this->assertEquals($invalid, 1, 'In line ' . __LINE__);
227 $this->assertEquals(empty($relationshipIds), TRUE, 'In line ' . __LINE__);
228 $this->relationshipTypeDelete($relType->id);
229 }
230
231 /**
232 * Methods create relationshipe within same contact type with valid data.
233 * success expected
234 */
235 public function testRelationshipCreateWithinSameType() {
236 //check for Individual to Parent
237 $relTypeParams = array(
238 'name_a_b' => 'indivToparent',
239 'name_b_a' => 'parentToindiv',
240 'contact_type_a' => 'Individual',
241 'contact_type_b' => 'Individual',
242 'contact_sub_type_b' => $this->parent,
243 );
244 $relTypeIds = array();
245 $relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams, $relTypeIds);
246 $params = array(
247 'relationship_type_id' => $relType->id . '_a_b',
248 'is_active' => 1,
249 'contact_check' => array($this->indivi_parent => $this->indivi_parent),
250 );
251 $ids = array('contact' => $this->individual);
252 list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::legacyCreateMultiple($params, $ids);
253
254 $this->assertEquals($valid, 1, 'In line ' . __LINE__);
255 $this->assertEquals(empty($relationshipIds), FALSE, 'In line ' . __LINE__);
256 $this->relationshipTypeDelete($relType->id);
257 }
258
259 /**
260 * Methods create relationshipe within different contact type with valid data.
261 * success expected
262 */
263 public function testRelCreateWithinDiffTypeSponsorIndivi() {
264 //check for Sponcer to Individual
265 $relTypeParams = array(
266 'name_a_b' => 'SponsorToIndiv',
267 'name_b_a' => 'IndivToSponsor',
268 'contact_type_a' => 'Organization',
269 'contact_sub_type_a' => $this->sponsor,
270 'contact_type_b' => 'Individual',
271 );
272 $relTypeIds = array();
273 $relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams, $relTypeIds);
274 $params = array(
275 'relationship_type_id' => $relType->id . '_a_b',
276 'is_active' => 1,
277 'contact_check' => array($this->indivi_student => 1),
278 );
279 $ids = array('contact' => $this->organization_sponsor);
280 list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::legacyCreateMultiple($params, $ids);
281
282 $this->assertEquals($valid, 1, 'In line ' . __LINE__);
283 $this->assertEquals(empty($relationshipIds), FALSE, 'In line ' . __LINE__);
284 $this->relationshipTypeDelete($relType->id);
285 }
286
287 public function testRelCreateWithinDiffTypeStudentSponsor() {
288 //check for Student to Sponcer
289 $relTypeParams = array(
290 'name_a_b' => 'StudentToSponsor',
291 'name_b_a' => 'SponsorToStudent',
292 'contact_type_a' => 'Individual',
293 'contact_sub_type_a' => $this->student,
294 'contact_type_b' => 'Organization',
295 'contact_sub_type_b' => $this->sponsor,
296 );
297 $relTypeIds = array();
298 $relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams, $relTypeIds);
299 $params = array(
300 'relationship_type_id' => $relType->id . '_a_b',
301 'is_active' => 1,
302 'contact_check' => array($this->organization_sponsor => 1),
303 );
304 $ids = array('contact' => $this->indivi_student);
305 list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::legacyCreateMultiple($params, $ids);
306
307 $this->assertEquals($valid, 1, 'In line ' . __LINE__);
308 $this->assertEquals(empty($relationshipIds), FALSE, 'In line ' . __LINE__);
309 $this->relationshipTypeDelete($relType->id);
310 }
311
312 }