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