Merge pull request #17602 from mattwire/supportsrecurring
[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 $subtypes = array_keys($this->getExpectedAllSubtypes());
81 $this->assertEquals(sort($subtypes), sort($result));
82 }
83
84 /**
85 * Test subTypes() methods with invalid data
86 */
87 public function testGetMethodsInvalid() {
88
89 $params = 'invalid';
90 $result = CRM_Contact_BAO_ContactType::subTypes($params);
91 $this->assertEquals(empty($result), TRUE);
92
93 $params = ['invalid'];
94 $result = CRM_Contact_BAO_ContactType::subTypes($params);
95 $this->assertEquals(empty($result), TRUE);
96 }
97
98 /**
99 * Test function for getting contact types.
100 *
101 * @throws \API_Exception
102 */
103 public function testContactTypeInfo() {
104 $blahType = ['is_active' => 0, 'name' => 'blah', 'label' => 'blah blah', 'parent_id:name' => 'Individual'];
105 $createdType = ContactType::create()->setValues($blahType)->execute()->first();
106 $activeTypes = CRM_Contact_BAO_ContactType::contactTypeInfo();
107 $expected = $this->getExpectedContactTypes();
108 $this->assertEquals($expected, $activeTypes);
109 $allTypes = CRM_Contact_BAO_ContactType::contactTypeInfo(TRUE);
110 $expected['blah'] = [
111 'is_active' => FALSE,
112 'name' => 'blah',
113 'label' => 'blah blah',
114 'id' => $createdType['id'],
115 'parent_id' => 1,
116 'is_reserved' => FALSE,
117 'parent' => 'Individual',
118 'parent_label' => 'Individual',
119 'description' => NULL,
120 'image_URL' => NULL,
121 ];
122 $this->assertEquals($expected, $allTypes);
123 }
124
125 /**
126 * Get all expected types.
127 *
128 * @return array
129 */
130 public function getExpectedContactTypes() {
131 return [
132 'Individual' =>
133 [
134 'id' => '1',
135 'name' => 'Individual',
136 'label' => 'Individual',
137 'is_active' => TRUE,
138 'is_reserved' => TRUE,
139 'description' => NULL,
140 'parent_id' => NULL,
141 'parent' => NULL,
142 'parent_label' => NULL,
143 'image_URL' => NULL,
144 ],
145 'Household' =>
146 [
147 'id' => '2',
148 'name' => 'Household',
149 'label' => 'Household',
150 'is_active' => TRUE,
151 'is_reserved' => TRUE,
152 'description' => NULL,
153 'parent_id' => NULL,
154 'parent' => NULL,
155 'parent_label' => NULL,
156 'image_URL' => NULL,
157 ],
158 'Organization' =>
159 [
160 'id' => '3',
161 'name' => 'Organization',
162 'label' => 'Organization',
163 'is_active' => TRUE,
164 'is_reserved' => TRUE,
165 'description' => NULL,
166 'parent_id' => NULL,
167 'parent' => NULL,
168 'parent_label' => NULL,
169 'image_URL' => NULL,
170 ],
171 'Student' =>
172 [
173 'id' => 4,
174 'name' => 'Student',
175 'label' => 'Student',
176 'parent_id' => 1,
177 'is_active' => '1',
178 'is_reserved' => FALSE,
179 'description' => NULL,
180 'parent' => 'Individual',
181 'parent_label' => 'Individual',
182 'image_URL' => NULL,
183 ],
184 'Parent' =>
185 [
186 'id' => 5,
187 'name' => 'Parent',
188 'label' => 'Parent',
189 'parent_id' => 1,
190 'is_active' => TRUE,
191 'is_reserved' => FALSE,
192 'description' => NULL,
193 'parent' => 'Individual',
194 'parent_label' => 'Individual',
195 'image_URL' => NULL,
196 ],
197 'Staff' =>
198 [
199 'id' => 6,
200 'name' => 'Staff',
201 'label' => 'Staff',
202 'parent_id' => 1,
203 'is_active' => TRUE,
204 'is_reserved' => FALSE,
205 'description' => NULL,
206 'parent' => 'Individual',
207 'parent_label' => 'Individual',
208 'image_URL' => NULL,
209 ],
210 'Team' =>
211 [
212 'id' => 7,
213 'name' => 'Team',
214 'label' => 'Team',
215 'parent_id' => 3,
216 'is_active' => TRUE,
217 'is_reserved' => FALSE,
218 'description' => NULL,
219 'parent' => 'Organization',
220 'parent_label' => 'Organization',
221 'image_URL' => NULL,
222 ],
223 'Sponsor' =>
224 [
225 'id' => 8,
226 'name' => 'Sponsor',
227 'label' => 'Sponsor',
228 'parent_id' => 3,
229 'is_active' => TRUE,
230 'is_reserved' => FALSE,
231 'description' => NULL,
232 'parent' => 'Organization',
233 'parent_label' => 'Organization',
234 'image_URL' => NULL,
235 ],
236 'sub1_individual' =>
237 [
238 'id' => $this->ids['ContactType'][0],
239 'name' => 'sub1_individual',
240 'label' => 'sub1_individual',
241 'parent_id' => 1,
242 'is_active' => TRUE,
243 'is_reserved' => FALSE,
244 'description' => NULL,
245 'parent' => 'Individual',
246 'parent_label' => 'Individual',
247 'image_URL' => NULL,
248 ],
249 'sub2_individual' =>
250 [
251 'id' => $this->ids['ContactType'][1],
252 'name' => 'sub2_individual',
253 'label' => 'sub2_individual',
254 'parent_id' => 1,
255 'is_active' => TRUE,
256 'is_reserved' => FALSE,
257 'description' => NULL,
258 'parent' => 'Individual',
259 'parent_label' => 'Individual',
260 'image_URL' => NULL,
261 ],
262 'sub_organization' =>
263 [
264 'id' => $this->ids['ContactType'][2],
265 'name' => 'sub_organization',
266 'label' => 'sub_organization',
267 'parent_id' => 3,
268 'is_active' => TRUE,
269 'is_reserved' => FALSE,
270 'description' => NULL,
271 'parent' => 'Organization',
272 'parent_label' => 'Organization',
273 'image_URL' => NULL,
274 ],
275 'sub_household' =>
276 [
277 'id' => $this->ids['ContactType'][3],
278 'name' => 'sub_household',
279 'label' => 'sub_household',
280 'parent_id' => 2,
281 'is_active' => TRUE,
282 'is_reserved' => FALSE,
283 'description' => NULL,
284 'parent' => 'Household',
285 'parent_label' => 'Household',
286 'image_URL' => NULL,
287 ],
288 ];
289 }
290
291 /**
292 * Get subtypes for all main types.
293 *
294 * @return array
295 */
296 public function getExpectedAllSubtypes() {
297 return array_merge(
298 $this->getExpectedContactSubTypes('Individual'),
299 $this->getExpectedContactSubTypes('Household'),
300 $this->getExpectedContactSubTypes('Organization')
301 );
302 }
303
304 /**
305 * Get the expected subtypes of the given contact type.
306 *
307 * @param string $parentType
308 *
309 * @return array
310 */
311 public function getExpectedContactSubTypes($parentType) {
312 $expected = $this->getExpectedContactTypes();
313 foreach ($expected as $index => $values) {
314 if (($values['parent_label'] ?? '') !== $parentType) {
315 unset($expected[$index]);
316 }
317 }
318 return $expected;
319 }
320
321 /**
322 * Test add() methods with valid data
323 * success expected
324 */
325 public function testAdd() {
326
327 $params = [
328 'label' => 'indiviSubType',
329 'name' => 'indiviSubType',
330 'parent_id' => 1,
331 'is_active' => 1,
332 ];
333 $result = CRM_Contact_BAO_ContactType::add($params);
334 $this->assertEquals($result->label, $params['label']);
335 $this->assertEquals($result->name, $params['name']);
336 $this->assertEquals($result->parent_id, $params['parent_id']);
337 $this->assertEquals($result->is_active, $params['is_active']);
338 CRM_Contact_BAO_ContactType::del($result->id);
339
340 $params = [
341 'label' => 'householdSubType',
342 'name' => 'householdSubType',
343 'parent_id' => 2,
344 'is_active' => 0,
345 ];
346 $result = CRM_Contact_BAO_ContactType::add($params);
347 $this->assertEquals($result->label, $params['label']);
348 $this->assertEquals($result->name, $params['name']);
349 $this->assertEquals($result->parent_id, $params['parent_id']);
350 $this->assertEquals($result->is_active, $params['is_active']);
351 CRM_Contact_BAO_ContactType::del($result->id);
352 }
353
354 /**
355 * Test add() with invalid data
356 */
357 public function testAddInvalid1() {
358
359 // parent id does not exist in db
360 $params = [
361 'label' => 'subType',
362 'name' => 'subType',
363 // non existent
364 'parent_id' => 100,
365 'is_active' => 1,
366 ];
367 $result = CRM_Contact_BAO_ContactType::add($params);
368 $this->assertEquals($result, NULL);
369 }
370
371 public function testAddInvalid2() {
372
373 // params does not have name and label keys
374 $params = [
375 'parent_id' => 1,
376 'is_active' => 1,
377 ];
378 $result = CRM_Contact_BAO_ContactType::add($params);
379 $this->assertEquals($result, NULL);
380 }
381
382 public function testAddInvalid3() {
383
384 // params does not have parent_id
385 $params = [
386 'label' => 'subType',
387 'name' => 'subType',
388 'is_active' => 1,
389 ];
390 $result = CRM_Contact_BAO_ContactType::add($params);
391 $this->assertEquals($result, NULL);
392 }
393
394 /**
395 * Test del() with valid data.
396 */
397 public function testDel() {
398
399 $params = [
400 'label' => 'indiviSubType',
401 'name' => 'indiviSubType',
402 'parent_id' => 1,
403 'is_active' => 1,
404 ];
405 $subtype = CRM_Contact_BAO_ContactType::add($params);
406 $result = CRM_Contact_BAO_ContactType::subTypes();
407 $this->assertEquals(TRUE, in_array($subtype->name, $result, TRUE));
408 $this->callAPISuccess('ContactType', 'delete', ['id' => $subtype->id]);
409
410 $result = CRM_Contact_BAO_ContactType::subTypes();
411 $this->assertEquals(FALSE, in_array($subtype->name, $result, TRUE));
412 }
413
414 /**
415 * Test del() with invalid data
416 */
417 public function testDelInvalid() {
418 $del = CRM_Contact_BAO_ContactType::del(NULL);
419 $this->assertEquals($del, FALSE);
420 }
421
422 }