Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
0eea664b | 2 | |
6a488035 | 3 | require_once 'CiviTest/Contact.php'; |
aba1cd8b EM |
4 | |
5 | /** | |
6 | * Class CRM_Contact_BAO_ContactType_RelationshipTest | |
acb109b7 | 7 | * @group headless |
aba1cd8b | 8 | */ |
6a488035 | 9 | class CRM_Contact_BAO_ContactType_RelationshipTest extends CiviUnitTestCase { |
6a488035 | 10 | |
00be9182 | 11 | public function setUp() { |
6a488035 TO |
12 | parent::setUp(); |
13 | ||
14 | //create contact subtypes | |
15 | $params = array( | |
16 | 'label' => 'indivi_student', | |
17 | 'name' => 'indivi_student', | |
18 | // Individual | |
19 | 'parent_id' => 1, | |
20 | 'is_active' => 1, | |
21 | ); | |
22 | $result = CRM_Contact_BAO_ContactType::add($params); | |
23 | $this->student = $params['name']; | |
24 | ||
25 | $params = array( | |
26 | 'label' => 'indivi_parent', | |
27 | 'name' => 'indivi_parent', | |
28 | // Individual | |
29 | 'parent_id' => 1, | |
30 | 'is_active' => 1, | |
31 | ); | |
32 | $result = CRM_Contact_BAO_ContactType::add($params); | |
33 | $this->parent = $params['name']; | |
34 | ||
35 | $params = array( | |
36 | 'label' => 'org_sponsor', | |
37 | 'name' => 'org_sponsor', | |
38 | // Organization | |
39 | 'parent_id' => 3, | |
40 | 'is_active' => 1, | |
41 | ); | |
42 | $result = CRM_Contact_BAO_ContactType::add($params); | |
43 | $this->sponsor = $params['name']; | |
44 | ||
45 | //create contacts | |
46 | $params = array( | |
47 | 'first_name' => 'Anne', | |
48 | 'last_name' => 'Grant', | |
49 | 'contact_type' => 'Individual', | |
50 | ); | |
f2040bc6 | 51 | $this->individual = $this->individualCreate($params); |
6a488035 TO |
52 | |
53 | $params = array( | |
54 | 'first_name' => 'Bill', | |
55 | 'last_name' => 'Adams', | |
56 | 'contact_type' => 'Individual', | |
57 | 'contact_sub_type' => $this->student, | |
58 | ); | |
f2040bc6 | 59 | $this->indivi_student = $this->individualCreate($params); |
6a488035 TO |
60 | |
61 | $params = array( | |
62 | 'first_name' => 'Alen', | |
63 | 'last_name' => 'Adams', | |
64 | 'contact_type' => 'Individual', | |
65 | 'contact_sub_type' => $this->parent, | |
66 | ); | |
f2040bc6 | 67 | $this->indivi_parent = $this->individualCreate($params); |
6a488035 TO |
68 | |
69 | $params = array( | |
70 | 'organization_name' => 'Compumentor', | |
71 | 'contact_type' => 'Organization', | |
72 | ); | |
f2040bc6 | 73 | $this->organization = $this->organizationCreate($params); |
6a488035 TO |
74 | |
75 | $params = array( | |
76 | 'organization_name' => 'Conservation Corp', | |
77 | 'contact_type' => 'Organization', | |
78 | 'contact_sub_type' => $this->sponsor, | |
79 | ); | |
f2040bc6 | 80 | $this->organization_sponsor = $this->organizationCreate($params); |
6a488035 TO |
81 | } |
82 | ||
00be9182 | 83 | public function tearDown() { |
6a488035 TO |
84 | $this->quickCleanup(array('civicrm_contact')); |
85 | ||
86 | $query = " | |
87 | DELETE FROM civicrm_contact_type | |
88 | WHERE name IN ('{$this->student}','{$this->parent}','{$this->sponsor}'); | |
89 | "; | |
90 | CRM_Core_DAO::executeQuery($query); | |
91 | } | |
92 | ||
93 | /** | |
eceb18cc | 94 | * Methods create relationshipType with valid data. |
6a488035 | 95 | * success expected |
6a488035 | 96 | */ |
00be9182 | 97 | public function testRelationshipTypeAddIndiviParent() { |
6a488035 TO |
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 | ||
00be9182 | 115 | public function testRelationshipTypeAddSponcorIndivi() { |
6a488035 TO |
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 | ||
00be9182 | 133 | public function testRelationshipTypeAddStudentSponcor() { |
6a488035 TO |
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 | /** | |
eceb18cc | 154 | * Methods create relationshipe within same contact type with invalid Relationships. |
6a488035 | 155 | */ |
00be9182 | 156 | public function testRelationshipCreateInvalidWithinSameType() { |
6a488035 TO |
157 | //check for Individual to Parent |
158 | $relTypeParams = array( | |
159 | 'name_a_b' => 'indivToparent', | |
160 | 'name_b_a' => 'parentToindiv', | |
161 | 'contact_type_a' => 'Individual', | |
162 | 'contact_type_b' => 'Individual', | |
163 | 'contact_sub_type_b' => $this->parent, | |
164 | ); | |
165 | $relTypeIds = array(); | |
92915c55 TO |
166 | $relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams, $relTypeIds); |
167 | $params = array( | |
6a488035 TO |
168 | 'relationship_type_id' => $relType->id . '_a_b', |
169 | 'contact_check' => array($this->indivi_student => 1), | |
170 | ); | |
171 | $ids = array('contact' => $this->individual); | |
172 | ||
2da59b29 | 173 | list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::legacyCreateMultiple($params, $ids); |
6a488035 | 174 | |
a15773db TM |
175 | $this->assertEquals($invalid, 1); |
176 | $this->assertEquals(empty($relationshipIds), TRUE); | |
6a488035 TO |
177 | $this->relationshipTypeDelete($relType->id); |
178 | } | |
179 | ||
180 | /** | |
eceb18cc | 181 | * Methods create relationshipe within diff contact type with invalid Relationships. |
6a488035 | 182 | */ |
00be9182 | 183 | public function testRelCreateInvalidWithinDiffTypeSpocorIndivi() { |
6a488035 TO |
184 | //check for Sponcer to Individual |
185 | $relTypeParams = array( | |
186 | 'name_a_b' => 'SponsorToIndiv', | |
187 | 'name_b_a' => 'IndivToSponsor', | |
188 | 'contact_type_a' => 'Organization', | |
189 | 'contact_sub_type_a' => $this->sponsor, | |
190 | 'contact_type_b' => 'Individual', | |
191 | ); | |
192 | $relTypeIds = array(); | |
92915c55 TO |
193 | $relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams, $relTypeIds); |
194 | $params = array( | |
6a488035 TO |
195 | 'relationship_type_id' => $relType->id . '_a_b', |
196 | 'contact_check' => array($this->individual => 1), | |
197 | ); | |
198 | $ids = array('contact' => $this->indivi_parent); | |
199 | ||
2da59b29 | 200 | list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::legacyCreateMultiple($params, $ids); |
6a488035 | 201 | |
a15773db TM |
202 | $this->assertEquals($invalid, 1); |
203 | $this->assertEquals(empty($relationshipIds), TRUE); | |
6a488035 TO |
204 | $this->relationshipTypeDelete($relType->id); |
205 | } | |
206 | ||
00be9182 | 207 | public function testRelCreateInvalidWithinDiffTypeStudentSponcor() { |
6a488035 TO |
208 | //check for Student to Sponcer |
209 | $relTypeParams = array( | |
210 | 'name_a_b' => 'StudentToSponser', | |
211 | 'name_b_a' => 'SponsorToStudent', | |
212 | 'contact_type_a' => 'Individual', | |
213 | 'contact_sub_type_a' => $this->student, | |
214 | 'contact_type_b' => 'Organization', | |
215 | 'contact_sub_type_b' => 'Sponser', | |
216 | ); | |
217 | $relTypeIds = array(); | |
92915c55 TO |
218 | $relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams, $relTypeIds); |
219 | $params = array( | |
6a488035 TO |
220 | 'relationship_type_id' => $relType->id . '_a_b', |
221 | 'contact_check' => array($this->individual => 1), | |
222 | ); | |
223 | $ids = array('contact' => $this->indivi_parent); | |
224 | ||
2da59b29 | 225 | list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::legacyCreateMultiple($params, $ids); |
6a488035 | 226 | |
a15773db TM |
227 | $this->assertEquals($invalid, 1); |
228 | $this->assertEquals(empty($relationshipIds), TRUE); | |
6a488035 TO |
229 | $this->relationshipTypeDelete($relType->id); |
230 | } | |
231 | ||
232 | /** | |
eceb18cc | 233 | * Methods create relationshipe within same contact type with valid data. |
6a488035 | 234 | * success expected |
6a488035 | 235 | */ |
00be9182 | 236 | public function testRelationshipCreateWithinSameType() { |
6a488035 TO |
237 | //check for Individual to Parent |
238 | $relTypeParams = array( | |
239 | 'name_a_b' => 'indivToparent', | |
240 | 'name_b_a' => 'parentToindiv', | |
241 | 'contact_type_a' => 'Individual', | |
242 | 'contact_type_b' => 'Individual', | |
243 | 'contact_sub_type_b' => $this->parent, | |
244 | ); | |
245 | $relTypeIds = array(); | |
92915c55 TO |
246 | $relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams, $relTypeIds); |
247 | $params = array( | |
6a488035 TO |
248 | 'relationship_type_id' => $relType->id . '_a_b', |
249 | 'is_active' => 1, | |
250 | 'contact_check' => array($this->indivi_parent => $this->indivi_parent), | |
251 | ); | |
252 | $ids = array('contact' => $this->individual); | |
2da59b29 | 253 | list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::legacyCreateMultiple($params, $ids); |
6a488035 | 254 | |
a15773db TM |
255 | $this->assertEquals($valid, 1); |
256 | $this->assertEquals(empty($relationshipIds), FALSE); | |
6a488035 TO |
257 | $this->relationshipTypeDelete($relType->id); |
258 | } | |
259 | ||
260 | /** | |
eceb18cc | 261 | * Methods create relationshipe within different contact type with valid data. |
6a488035 | 262 | * success expected |
6a488035 | 263 | */ |
00be9182 | 264 | public function testRelCreateWithinDiffTypeSponsorIndivi() { |
6a488035 TO |
265 | //check for Sponcer to Individual |
266 | $relTypeParams = array( | |
267 | 'name_a_b' => 'SponsorToIndiv', | |
268 | 'name_b_a' => 'IndivToSponsor', | |
269 | 'contact_type_a' => 'Organization', | |
270 | 'contact_sub_type_a' => $this->sponsor, | |
271 | 'contact_type_b' => 'Individual', | |
272 | ); | |
273 | $relTypeIds = array(); | |
92915c55 TO |
274 | $relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams, $relTypeIds); |
275 | $params = array( | |
6a488035 TO |
276 | 'relationship_type_id' => $relType->id . '_a_b', |
277 | 'is_active' => 1, | |
278 | 'contact_check' => array($this->indivi_student => 1), | |
279 | ); | |
280 | $ids = array('contact' => $this->organization_sponsor); | |
2da59b29 | 281 | list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::legacyCreateMultiple($params, $ids); |
6a488035 | 282 | |
a15773db TM |
283 | $this->assertEquals($valid, 1); |
284 | $this->assertEquals(empty($relationshipIds), FALSE); | |
6a488035 TO |
285 | $this->relationshipTypeDelete($relType->id); |
286 | } | |
287 | ||
00be9182 | 288 | public function testRelCreateWithinDiffTypeStudentSponsor() { |
6a488035 TO |
289 | //check for Student to Sponcer |
290 | $relTypeParams = array( | |
291 | 'name_a_b' => 'StudentToSponsor', | |
292 | 'name_b_a' => 'SponsorToStudent', | |
293 | 'contact_type_a' => 'Individual', | |
294 | 'contact_sub_type_a' => $this->student, | |
295 | 'contact_type_b' => 'Organization', | |
296 | 'contact_sub_type_b' => $this->sponsor, | |
297 | ); | |
298 | $relTypeIds = array(); | |
92915c55 TO |
299 | $relType = CRM_Contact_BAO_RelationshipType::add($relTypeParams, $relTypeIds); |
300 | $params = array( | |
6a488035 TO |
301 | 'relationship_type_id' => $relType->id . '_a_b', |
302 | 'is_active' => 1, | |
303 | 'contact_check' => array($this->organization_sponsor => 1), | |
304 | ); | |
305 | $ids = array('contact' => $this->indivi_student); | |
2da59b29 | 306 | list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::legacyCreateMultiple($params, $ids); |
6a488035 | 307 | |
a15773db TM |
308 | $this->assertEquals($valid, 1); |
309 | $this->assertEquals(empty($relationshipIds), FALSE); | |
6a488035 TO |
310 | $this->relationshipTypeDelete($relType->id); |
311 | } | |
96025800 | 312 | |
6a488035 | 313 | } |