[REF] Cleanup function for retrieving contact types.
[civicrm-core.git] / tests / phpunit / CRM / Contact / BAO / ContactType / ContactTypeTest.php
1 <?php
2 use Civi\Api4\ContactType;
3
4 /**
5 * Class CRM_Contact_BAO_ContactType_ContactTypeTest
6 * @group headless
7 */
8 class CRM_Contact_BAO_ContactType_ContactTypeTest extends CiviUnitTestCase {
9
10 public function setUp() {
11 parent::setUp();
12 $params = [
13 'label' => 'sub1_individual',
14 'name' => 'sub1_individual',
15 'parent_id:name' => 'Individual',
16 'is_active' => 1,
17 ];
18
19 $this->ids['ContactType'][] = ContactType::create()->setValues($params)->execute()->first()['id'];
20
21 $params = [
22 'label' => 'sub2_individual',
23 'name' => 'sub2_individual',
24 'parent_id:name' => 'Individual',
25 'is_active' => 1,
26 ];
27
28 $this->ids['ContactType'][] = ContactType::create()->setValues($params)->execute()->first()['id'];
29
30 $params = [
31 'label' => 'sub_organization',
32 'name' => 'sub_organization',
33 'parent_id:name' => 'Organization',
34 'is_active' => 1,
35 ];
36
37 $this->ids['ContactType'][] = ContactType::create()->setValues($params)->execute()->first()['id'];
38
39 $params = [
40 'label' => 'sub_household',
41 'name' => 'sub_household',
42 'parent_id:name' => 'Household',
43 'is_active' => 1,
44 ];
45 $this->ids['ContactType'][] = (int) ContactType::create()->setValues($params)->execute()->first()['id'];
46 }
47
48 /**
49 * Cleanup contact types.
50 *
51 * @throws \API_Exception
52 * @throws \Civi\API\Exception\UnauthorizedException
53 */
54 public function tearDown() {
55 parent::tearDown();
56 ContactType::delete()->addWhere('id', 'IN', $this->ids['ContactType'])->execute();
57 }
58
59 /**
60 * Test contactTypes() and subTypes() methods return correct contact types.
61 */
62 public function testGetMethods() {
63 $result = CRM_Contact_BAO_ContactType::contactTypes(TRUE);
64 $this->assertEquals(array_keys($this->getExpectedContactTypes()), $result);
65
66 // check for type:Individual
67 $result = CRM_Contact_BAO_ContactType::subTypes('Individual');
68 $this->assertEquals(array_keys($this->getExpectedContactSubTypes('Individual')), $result);
69
70 // check for type:Organization
71 $result = CRM_Contact_BAO_ContactType::subTypes('Organization');
72 $this->assertEquals(array_keys($this->getExpectedContactSubTypes('Organization')), $result);
73
74 // check for type:Household
75 $result = CRM_Contact_BAO_ContactType::subTypes('Household');
76 $this->assertEquals(array_keys($this->getExpectedContactSubTypes('Household')), $result);
77
78 // check for all contact types
79 $result = CRM_Contact_BAO_ContactType::subTypes();
80 $this->assertEquals(array_keys($this->getExpectedAllSubtypes()), $result);
81 }
82
83 /**
84 * Test subTypes() methods with invalid data
85 */
86 public function testGetMethodsInvalid() {
87
88 $params = 'invalid';
89 $result = CRM_Contact_BAO_ContactType::subTypes($params);
90 $this->assertEquals(empty($result), TRUE);
91
92 $params = ['invalid'];
93 $result = CRM_Contact_BAO_ContactType::subTypes($params);
94 $this->assertEquals(empty($result), TRUE);
95 }
96
97 /**
98 * Test function for getting contact types.
99 *
100 * @throws \API_Exception
101 */
102 public function testContactTypeInfo() {
103 $blahType = ['is_active' => 0, 'name' => 'blah', 'label' => 'blah blah', 'parent_id:name' => 'Individual'];
104 $createdType = ContactType::create()->setValues($blahType)->execute()->first();
105 $activeTypes = CRM_Contact_BAO_ContactType::contactTypeInfo();
106 $expected = $this->getExpectedContactTypes();
107 $this->assertEquals($expected, $activeTypes);
108 $allTypes = CRM_Contact_BAO_ContactType::contactTypeInfo(TRUE);
109 $expected['blah'] = [
110 'is_active' => FALSE,
111 'name' => 'blah',
112 'label' => 'blah blah',
113 'id' => $createdType['id'],
114 'parent_id' => 1,
115 'is_reserved' => FALSE,
116 'parent' => 'Individual',
117 'parent_label' => 'Individual',
118 'description' => NULL,
119 'image_URL' => NULL,
120 ];
121 $this->assertEquals($expected, $allTypes);
122 }
123
124 /**
125 * Get all expected types.
126 *
127 * @return array
128 */
129 public function getExpectedContactTypes() {
130 return [
131 'Individual' =>
132 [
133 'id' => '1',
134 'name' => 'Individual',
135 'label' => 'Individual',
136 'is_active' => TRUE,
137 'is_reserved' => TRUE,
138 'description' => NULL,
139 'parent_id' => NULL,
140 'parent' => NULL,
141 'parent_label' => NULL,
142 'image_URL' => NULL,
143 ],
144 'Household' =>
145 [
146 'id' => '2',
147 'name' => 'Household',
148 'label' => 'Household',
149 'is_active' => TRUE,
150 'is_reserved' => TRUE,
151 'description' => NULL,
152 'parent_id' => NULL,
153 'parent' => NULL,
154 'parent_label' => NULL,
155 'image_URL' => NULL,
156 ],
157 'Organization' =>
158 [
159 'id' => '3',
160 'name' => 'Organization',
161 'label' => 'Organization',
162 'is_active' => TRUE,
163 'is_reserved' => TRUE,
164 'description' => NULL,
165 'parent_id' => NULL,
166 'parent' => NULL,
167 'parent_label' => NULL,
168 'image_URL' => NULL,
169 ],
170 'Student' =>
171 [
172 'id' => 4,
173 'name' => 'Student',
174 'label' => 'Student',
175 'parent_id' => 1,
176 'is_active' => '1',
177 'is_reserved' => FALSE,
178 'description' => NULL,
179 'parent' => 'Individual',
180 'parent_label' => 'Individual',
181 'image_URL' => NULL,
182 ],
183 'Parent' =>
184 [
185 'id' => 5,
186 'name' => 'Parent',
187 'label' => 'Parent',
188 'parent_id' => 1,
189 'is_active' => TRUE,
190 'is_reserved' => FALSE,
191 'description' => NULL,
192 'parent' => 'Individual',
193 'parent_label' => 'Individual',
194 'image_URL' => NULL,
195 ],
196 'Staff' =>
197 [
198 'id' => 6,
199 'name' => 'Staff',
200 'label' => 'Staff',
201 'parent_id' => 1,
202 'is_active' => TRUE,
203 'is_reserved' => FALSE,
204 'description' => NULL,
205 'parent' => 'Individual',
206 'parent_label' => 'Individual',
207 'image_URL' => NULL,
208 ],
209 'Team' =>
210 [
211 'id' => 7,
212 'name' => 'Team',
213 'label' => 'Team',
214 'parent_id' => 3,
215 'is_active' => TRUE,
216 'is_reserved' => FALSE,
217 'description' => NULL,
218 'parent' => 'Organization',
219 'parent_label' => 'Organization',
220 'image_URL' => NULL,
221 ],
222 'Sponsor' =>
223 [
224 'id' => 8,
225 'name' => 'Sponsor',
226 'label' => 'Sponsor',
227 'parent_id' => 3,
228 'is_active' => TRUE,
229 'is_reserved' => FALSE,
230 'description' => NULL,
231 'parent' => 'Organization',
232 'parent_label' => 'Organization',
233 'image_URL' => NULL,
234 ],
235 'sub1_individual' =>
236 [
237 'id' => $this->ids['ContactType'][0],
238 'name' => 'sub1_individual',
239 'label' => 'sub1_individual',
240 'parent_id' => 1,
241 'is_active' => TRUE,
242 'is_reserved' => FALSE,
243 'description' => NULL,
244 'parent' => 'Individual',
245 'parent_label' => 'Individual',
246 'image_URL' => NULL,
247 ],
248 'sub2_individual' =>
249 [
250 'id' => $this->ids['ContactType'][1],
251 'name' => 'sub2_individual',
252 'label' => 'sub2_individual',
253 'parent_id' => 1,
254 'is_active' => TRUE,
255 'is_reserved' => FALSE,
256 'description' => NULL,
257 'parent' => 'Individual',
258 'parent_label' => 'Individual',
259 'image_URL' => NULL,
260 ],
261 'sub_organization' =>
262 [
263 'id' => $this->ids['ContactType'][2],
264 'name' => 'sub_organization',
265 'label' => 'sub_organization',
266 'parent_id' => 3,
267 'is_active' => TRUE,
268 'is_reserved' => FALSE,
269 'description' => NULL,
270 'parent' => 'Organization',
271 'parent_label' => 'Organization',
272 'image_URL' => NULL,
273 ],
274 'sub_household' =>
275 [
276 'id' => $this->ids['ContactType'][3],
277 'name' => 'sub_household',
278 'label' => 'sub_household',
279 'parent_id' => 2,
280 'is_active' => TRUE,
281 'is_reserved' => FALSE,
282 'description' => NULL,
283 'parent' => 'Household',
284 'parent_label' => 'Household',
285 'image_URL' => NULL,
286 ],
287 ];
288 }
289
290 /**
291 * Get subtypes for all main types.
292 *
293 * @return array
294 */
295 public function getExpectedAllSubtypes() {
296 return array_merge(
297 $this->getExpectedContactSubTypes('Individual'),
298 $this->getExpectedContactSubTypes('Household'),
299 $this->getExpectedContactSubTypes('Organization')
300 );
301 }
302
303 /**
304 * Get the expected subtypes of the given contact type.
305 *
306 * @param string $parentType
307 *
308 * @return array
309 */
310 public function getExpectedContactSubTypes($parentType) {
311 $expected = $this->getExpectedContactTypes();
312 foreach ($expected as $index => $values) {
313 if (($values['parent_label'] ?? '') !== $parentType) {
314 unset($expected[$index]);
315 }
316 }
317 return $expected;
318 }
319
320 /**
321 * Test add() methods with valid data
322 * success expected
323 */
324 public function testAdd() {
325
326 $params = [
327 'label' => 'indiviSubType',
328 'name' => 'indiviSubType',
329 'parent_id' => 1,
330 'is_active' => 1,
331 ];
332 $result = CRM_Contact_BAO_ContactType::add($params);
333 $this->assertEquals($result->label, $params['label']);
334 $this->assertEquals($result->name, $params['name']);
335 $this->assertEquals($result->parent_id, $params['parent_id']);
336 $this->assertEquals($result->is_active, $params['is_active']);
337 CRM_Contact_BAO_ContactType::del($result->id);
338
339 $params = [
340 'label' => 'householdSubType',
341 'name' => 'householdSubType',
342 'parent_id' => 2,
343 'is_active' => 0,
344 ];
345 $result = CRM_Contact_BAO_ContactType::add($params);
346 $this->assertEquals($result->label, $params['label']);
347 $this->assertEquals($result->name, $params['name']);
348 $this->assertEquals($result->parent_id, $params['parent_id']);
349 $this->assertEquals($result->is_active, $params['is_active']);
350 CRM_Contact_BAO_ContactType::del($result->id);
351 }
352
353 /**
354 * Test add() with invalid data
355 */
356 public function testAddInvalid1() {
357
358 // parent id does not exist in db
359 $params = [
360 'label' => 'subType',
361 'name' => 'subType',
362 // non existent
363 'parent_id' => 100,
364 'is_active' => 1,
365 ];
366 $result = CRM_Contact_BAO_ContactType::add($params);
367 $this->assertEquals($result, NULL);
368 }
369
370 public function testAddInvalid2() {
371
372 // params does not have name and label keys
373 $params = [
374 'parent_id' => 1,
375 'is_active' => 1,
376 ];
377 $result = CRM_Contact_BAO_ContactType::add($params);
378 $this->assertEquals($result, NULL);
379 }
380
381 public function testAddInvalid3() {
382
383 // params does not have parent_id
384 $params = [
385 'label' => 'subType',
386 'name' => 'subType',
387 'is_active' => 1,
388 ];
389 $result = CRM_Contact_BAO_ContactType::add($params);
390 $this->assertEquals($result, NULL);
391 }
392
393 /**
394 * Test del() with valid data.
395 */
396 public function testDel() {
397
398 $params = [
399 'label' => 'indiviSubType',
400 'name' => 'indiviSubType',
401 'parent_id' => 1,
402 'is_active' => 1,
403 ];
404 $subtype = CRM_Contact_BAO_ContactType::add($params);
405 $result = CRM_Contact_BAO_ContactType::subTypes();
406 $this->assertEquals(TRUE, in_array($subtype->name, $result, TRUE));
407 $this->callAPISuccess('ContactType', 'delete', ['id' => $subtype->id]);
408
409 $result = CRM_Contact_BAO_ContactType::subTypes();
410 $this->assertEquals(FALSE, in_array($subtype->name, $result, TRUE));
411 }
412
413 /**
414 * Test del() with invalid data
415 */
416 public function testDelInvalid() {
417 $del = CRM_Contact_BAO_ContactType::del(NULL);
418 $this->assertEquals($del, FALSE);
419 }
420
421 }