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