Merge pull request #17396 from seamuslee001/use_util_mail_events
[civicrm-core.git] / tests / phpunit / CRMTraits / Custom / CustomDataTrait.php
CommitLineData
8ba5884d 1<?php
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
8ba5884d 5 | |
7d61e75f
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
8ba5884d 9 +--------------------------------------------------------------------+
10 */
11
cf66fb20 12use Civi\Api4\CustomGroup;
13
8ba5884d 14/**
15 * Trait Custom Data trait.
16 *
17 * Trait for setting up custom data in tests.
18 */
19trait CRMTraits_Custom_CustomDataTrait {
20
119664d6 21 /**
22 * Create a custom group with fields of multiple types.
23 *
24 * @param array $groupParams
cf66fb20 25 *
26 * @throws \API_Exception
27 * @throws \Civi\API\Exception\UnauthorizedException
119664d6 28 */
29 public function createCustomGroupWithFieldsOfAllTypes($groupParams = []) {
30 $this->createCustomGroup($groupParams);
31 $this->ids['CustomField'] = $this->createCustomFieldsOfAllTypes();
32 }
33
8ba5884d 34 /**
35 * Create a custom group.
36 *
37 * @param array $params
38 *
39 * @return int
cf66fb20 40 *
41 * @throws \API_Exception
42 * @throws \Civi\API\Exception\UnauthorizedException
8ba5884d 43 */
44 public function createCustomGroup($params = []) {
45 $params = array_merge([
46 'title' => 'Custom Group',
0e1544e7 47 'extends' => [$this->entity ?? 'Contact'],
8ba5884d 48 'weight' => 5,
49 'style' => 'Inline',
50 'max_multiple' => 0,
51 ], $params);
0e1544e7 52 $identifier = $params['name'] ?? $params['title'];
fe806431 53 $this->ids['CustomGroup'][$identifier] = CustomGroup::create(FALSE)->setValues($params)->execute()->first()['id'];
0e1544e7 54 return $this->ids['CustomGroup'][$identifier];
55 }
56
57 /**
58 * Get the table_name for the specified custom group.
59 *
60 * @param string $identifier
61 *
62 * @return string
63 */
64 public function getCustomGroupTable($identifier = 'Custom Group') {
65 return $this->callAPISuccessGetValue('CustomGroup', ['id' => $this->ids['CustomGroup'][$identifier], 'return' => 'table_name']);
66 }
67
68 /**
69 * Get the the column name for the identified custom field.
70 *
71 * @param string $key
72 * Identifier - generally keys map to data type - eg. 'text', 'int' etc.
73 *
74 * @return string
75 */
76 protected function getCustomFieldColumnName($key) {
77 return $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID($key), 'return' => 'column_name']);
8ba5884d 78 }
79
712ee28f 80 /**
81 * Create a custom group with a single field.
82 *
83 * @param array $groupParams
ca64c337 84 * Params for the group to be created.
712ee28f 85 * @param string $customFieldType
86 *
697aad03 87 * @param string $identifier
88 *
cf66fb20 89 * @throws \API_Exception
712ee28f 90 * @throws \CRM_Core_Exception
cf66fb20 91 * @throws \Civi\API\Exception\UnauthorizedException
712ee28f 92 */
0e1544e7 93 public function createCustomGroupWithFieldOfType($groupParams = [], $customFieldType = 'text', $identifier = NULL) {
c971eccf 94 $supported = ['text', 'select', 'date', 'int', 'contact_reference', 'radio'];
0e1544e7 95 if (!in_array($customFieldType, $supported, TRUE)) {
712ee28f 96 throw new CRM_Core_Exception('we have not yet extracted other custom field types from createCustomFieldsOfAllTypes, Use consistent syntax when you do');
97 }
697aad03 98 $groupParams['title'] = empty($groupParams['title']) ? $identifier . 'Group with field ' . $customFieldType : $groupParams['title'];
0e1544e7 99 $groupParams['name'] = $identifier ?? 'Custom Group';
712ee28f 100 $this->createCustomGroup($groupParams);
ca64c337 101 $reference = &$this->ids['CustomField'][$identifier . $customFieldType];
102 $fieldParams = ['custom_group_id' => $this->ids['CustomGroup'][$groupParams['name']]];
2b81c27e 103 switch ($customFieldType) {
104 case 'text':
ca64c337 105 $reference = $this->createTextCustomField($fieldParams)['id'];
106 return;
2b81c27e 107
108 case 'select':
ca64c337 109 $reference = $this->createSelectCustomField($fieldParams)['id'];
110 return;
0e1544e7 111
112 case 'int':
ca64c337 113 $reference = $this->createIntCustomField($fieldParams)['id'];
114 return;
35aca6d6 115
116 case 'date':
ca64c337 117 $reference = $this->createDateCustomField($fieldParams)['id'];
118 return;
119
120 case 'contact_reference':
121 $reference = $this->createContactReferenceCustomField($fieldParams)['id'];
122 return;
c971eccf
CW
123
124 case 'radio':
125 $reference = $this->createIntegerRadioCustomField($fieldParams)['id'];
126 return;
127
2b81c27e 128 }
712ee28f 129 }
130
8ba5884d 131 /**
132 * @return array
133 */
134 public function createCustomFieldsOfAllTypes() {
135 $customGroupID = $this->ids['CustomGroup']['Custom Group'];
136 $ids = [];
79c9d4c2 137 $ids['text'] = (int) $this->createTextCustomField(['custom_group_id' => $customGroupID])['id'];
138 $ids['select_string'] = (int) $this->createSelectCustomField(['custom_group_id' => $customGroupID])['id'];
139 $ids['select_date'] = (int) $this->createDateCustomField(['custom_group_id' => $customGroupID])['id'];
140 $ids['int'] = (int) $this->createIntCustomField(['custom_group_id' => $customGroupID])['id'];
141 $ids['link'] = (int) $this->createLinkCustomField(['custom_group_id' => $customGroupID])['id'];
142 $ids['file'] = (int) $this->createFileCustomField(['custom_group_id' => $customGroupID])['id'];
143 $ids['country'] = (int) $this->createCountryCustomField(['custom_group_id' => $customGroupID])['id'];
ca64c337 144 $ids['multi_country'] = (int) $this->createMultiCountryCustomField(['custom_group_id' => $customGroupID])['id'];
79c9d4c2 145 $ids['contact_reference'] = $this->createContactReferenceCustomField(['custom_group_id' => $customGroupID])['id'];
ca64c337 146 $ids['state'] = (int) $this->createStateCustomField(['custom_group_id' => $customGroupID])['id'];
147 $ids['multi_state'] = (int) $this->createMultiStateCustomField(['custom_group_id' => $customGroupID])['id'];
148 $ids['boolean'] = (int) $this->createBooleanCustomField(['custom_group_id' => $customGroupID])['id'];
8ba5884d 149 return $ids;
150 }
151
119664d6 152 /**
153 * Get the custom field name for the relevant key.
154 *
155 * e.g returns 'custom_5' where 5 is the id of the field using the key.
156 *
157 * Generally keys map to data types.
158 *
159 * @param string $key
160 *
161 * @return string
162 */
163 protected function getCustomFieldName($key) {
0e1544e7 164 return 'custom_' . $this->getCustomFieldID($key);
8392a043 165 }
166
167 /**
168 * Get the custom field name for the relevant key.
169 *
170 * e.g returns 'custom_5' where 5 is the id of the field using the key.
171 *
172 * Generally keys map to data types.
173 *
174 * @param string $key
175 *
176 * @return string
177 */
178 protected function getCustomFieldID($key) {
0e1544e7 179 return $this->ids['CustomField'][$key];
180 }
181
182 /**
183 * Create a custom text fields.
184 *
185 * @param array $params
186 * Parameter overrides, must include custom_group_id.
187 *
188 * @return array
189 */
190 protected function createIntCustomField($params = []) {
79c9d4c2 191 $params = array_merge($this->getFieldsValuesByType('Int'), $params);
0e1544e7 192 return $this->callAPISuccess('CustomField', 'create', $params)['values'][0];
119664d6 193 }
194
ca64c337 195 /**
196 * Create a custom text fields.
197 *
198 * @param array $params
199 * Parameter overrides, must include custom_group_id.
200 *
201 * @return array
202 */
203 protected function createBooleanCustomField($params = []) {
204 $params = array_merge($this->getFieldsValuesByType('Boolean'), $params);
205 return $this->callAPISuccess('CustomField', 'create', $params)['values'][0];
206 }
207
79c9d4c2 208 /**
209 * Create a custom text fields.
210 *
211 * @param array $params
212 * Parameter overrides, must include custom_group_id.
213 *
214 * @return array
215 */
216 protected function createContactReferenceCustomField($params = []) {
217 $params = array_merge($this->getFieldsValuesByType('ContactReference'), $params);
218 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
219 }
220
712ee28f 221 /**
222 * Create a custom text fields.
223 *
224 * @param array $params
225 * Parameter overrides, must include custom_group_id.
226 *
227 * @return array
228 */
229 protected function createTextCustomField($params = []) {
79c9d4c2 230 $params = array_merge($this->getFieldsValuesByType('String'), $params);
231 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
232 }
712ee28f 233
79c9d4c2 234 /**
235 * Create a custom text fields.
236 *
237 * @param array $params
238 * Parameter overrides, must include custom_group_id.
239 *
240 * @return array
241 */
242 protected function createLinkCustomField($params = []) {
243 $params = array_merge($this->getFieldsValuesByType('Link'), $params);
244 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
712ee28f 245 }
246
2b81c27e 247 /**
ca64c337 248 * Create a custom country fields.
2b81c27e 249 *
250 * @param array $params
251 * Parameter overrides, must include custom_group_id.
252 *
253 * @return array
254 */
79c9d4c2 255 protected function createCountryCustomField($params = []) {
256 $params = array_merge($this->getFieldsValuesByType('Country'), $params);
257 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
258 }
2b81c27e 259
ca64c337 260 /**
261 * Create a custom multi select country fields.
262 *
263 * @param array $params
264 * Parameter overrides, must include custom_group_id.
265 *
266 * @return array
267 */
268 protected function createMultiCountryCustomField($params = []) {
269 $params = array_merge($this->getFieldsValuesByType('Country', 'Multi-Select Country'), $params);
270 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
271 }
272
273 /**
274 * Create a custom state fields.
275 *
276 * @param array $params
277 * Parameter overrides, must include custom_group_id.
278 *
279 * @return array
280 */
281 protected function createStateCustomField($params = []) {
282 $params = array_merge($this->getFieldsValuesByType('StateProvince'), $params);
283 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
284 }
285
286 /**
287 * Create a custom multi select state fields.
288 *
289 * @param array $params
290 * Parameter overrides, must include custom_group_id.
291 *
292 * @return array
293 */
294 protected function createMultiStateCustomField($params = []) {
295 $params = array_merge($this->getFieldsValuesByType('StateProvince', 'Multi-Select State/Province'), $params);
296 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
297 }
298
79c9d4c2 299 /**
300 * Create a custom text fields.
301 *
302 * @param array $params
303 * Parameter overrides, must include custom_group_id.
304 *
305 * @return array
306 */
307 protected function createFileCustomField($params = []) {
308 $params = array_merge($this->getFieldsValuesByType('File'), $params);
309 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
310 }
2b81c27e 311
79c9d4c2 312 /**
313 * Create custom select field.
314 *
315 * @param array $params
316 * Parameter overrides, must include custom_group_id.
317 *
318 * @return array
319 */
320 protected function createSelectCustomField(array $params): array {
321 $params = array_merge($this->getFieldsValuesByType('String', 'Select'), $params);
322 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
2b81c27e 323 }
324
35aca6d6 325 /**
326 * Create a custom field of type date.
327 *
328 * @param array $params
329 *
330 * @return array
331 */
332 protected function createDateCustomField($params): array {
79c9d4c2 333 $params = array_merge($this->getFieldsValuesByType('Date'), $params);
334 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
335 }
336
c971eccf
CW
337 /**
338 * Create a custom field of type radio with integer values.
339 *
340 * @param array $params
341 *
342 * @return array
343 */
344 protected function createIntegerRadioCustomField($params): array {
345 $params = array_merge($this->getFieldsValuesByType('Int', 'Radio'), $params);
346 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
347 }
348
79c9d4c2 349 /**
350 * Get default field values for the type of field.
351 *
352 * @param $dataType
353 * @param string $htmlType
354 *
355 * @return mixed
356 */
357 public function getFieldsValuesByType($dataType, $htmlType = 'default') {
358 $values = $this->getAvailableFieldCombinations()[$dataType];
359 return array_merge([
0e1544e7 360 'is_searchable' => 1,
79c9d4c2 361 'sequential' => 1,
362 'default_value' => '',
363 'is_required' => 0,
364 ], array_merge($values['default'], $values[$htmlType])
365 );
366 }
35aca6d6 367
79c9d4c2 368 /**
369 * Get data available for custom fields.
370 *
371 * The 'default' key holds general values. Where more than one html type is an option
372 * then the any values that differ to the defaults are keyed by html key.
373 *
374 * The order below is consistent with the UI.
375 *
376 * @return array
377 */
378 protected function getAvailableFieldCombinations() {
379 return [
380 'String' => [
381 'default' => [
382 'label' => 'Enter text here',
383 'html_type' => 'Text',
384 'data_type' => 'String',
385 'default_value' => 'xyz',
386 'text_length' => 300,
387 ],
388 'Select' => [
389 'label' => 'Pick Color',
390 'html_type' => 'Select',
391 'data_type' => 'String',
392 'text_length' => '',
393 'default_value' => '',
394 'option_values' => [
395 [
396 'label' => 'Red',
397 'value' => 'R',
398 'weight' => 1,
399 'is_active' => 1,
400 ],
401 [
402 'label' => 'Yellow',
403 'value' => 'Y',
404 'weight' => 2,
405 'is_active' => 1,
406 ],
407 [
408 'label' => 'Green',
409 'value' => 'G',
410 'weight' => 3,
411 'is_active' => 1,
412 ],
413 ],
414 ],
ca64c337 415 'Radio' => [
416 'label' => 'Pick Color',
417 'html_type' => 'Radio',
418 'data_type' => 'String',
419 'text_length' => '',
420 'default_value' => '',
421 'option_values' => [
422 [
423 'label' => 'Red',
424 'value' => 'R',
425 'weight' => 1,
426 'is_active' => 1,
427 ],
428 [
429 'label' => 'Yellow',
430 'value' => 'Y',
431 'weight' => 2,
432 'is_active' => 1,
433 ],
434 [
435 'label' => 'Green',
436 'value' => 'G',
437 'weight' => 3,
438 'is_active' => 1,
439 ],
440 ],
441 ],
442 'CheckBox' => [
443 'label' => 'Pick Color',
c971eccf 444 'html_type' => 'CheckBox',
ca64c337 445 'data_type' => 'String',
446 'text_length' => '',
447 'default_value' => '',
448 'option_values' => [
449 [
450 'label' => 'Red',
451 'value' => 'R',
452 'weight' => 1,
453 'is_active' => 1,
454 ],
455 [
456 'label' => 'Yellow',
457 'value' => 'Y',
458 'weight' => 2,
459 'is_active' => 1,
460 ],
461 [
462 'label' => 'Green',
463 'value' => 'G',
464 'weight' => 3,
465 'is_active' => 1,
466 ],
467 ],
468 ],
469 'Multi-Select' => [
470 'label' => 'Pick Color',
471 'html_type' => 'Multi-Select',
472 'data_type' => 'String',
473 'text_length' => '',
474 'default_value' => '',
475 'option_values' => [
476 [
477 'label' => 'Red',
478 'value' => 'R',
479 'weight' => 1,
480 'is_active' => 1,
481 ],
482 [
483 'label' => 'Yellow',
484 'value' => 'Y',
485 'weight' => 2,
486 'is_active' => 1,
487 ],
488 [
489 'label' => 'Green',
490 'value' => 'G',
491 'weight' => 3,
492 'is_active' => 1,
493 ],
494 ],
495 ],
496 'Autocomplete-Select' => [
497 'label' => 'Pick Color',
498 'html_type' => 'Autocomplete-Select',
499 'data_type' => 'String',
500 'text_length' => '',
501 'default_value' => '',
502 'option_values' => [
503 [
504 'label' => 'Red',
505 'value' => 'R',
506 'weight' => 1,
507 'is_active' => 1,
508 ],
509 [
510 'label' => 'Yellow',
511 'value' => 'Y',
512 'weight' => 2,
513 'is_active' => 1,
514 ],
515 [
516 'label' => 'Green',
517 'value' => 'G',
518 'weight' => 3,
519 'is_active' => 1,
520 ],
521 ],
522 ],
79c9d4c2 523 ],
524 'Int' => [
525 'default' => [
526 'label' => 'Enter integer here',
527 'html_type' => 'Text',
528 'data_type' => 'Int',
529 'default_value' => '4',
530 'is_search_range' => 1,
531 ],
ca64c337 532 'Select' => [
533 'label' => 'Integer select',
534 'html_type' => 'Select',
535 'option_values' => [
536 [
537 'label' => '50',
538 'value' => 3,
539 'weight' => 1,
540 'is_active' => 1,
541 ],
542 [
543 'label' => '100',
544 'value' => 4,
545 'weight' => 2,
546 'is_active' => 1,
547 ],
548 ],
549 ],
550 'Radio' => [
551 'label' => 'Integer radio',
552 'html_type' => 'Radio',
553 'option_values' => [
554 [
555 'label' => '50',
556 'value' => 3,
557 'weight' => 1,
558 'is_active' => 1,
559 ],
560 [
561 'label' => '100',
562 'value' => 4,
563 'weight' => 2,
564 'is_active' => 1,
565 ],
c971eccf
CW
566 [
567 'label' => 'Red Testing',
568 'value' => 5,
569 'weight' => 3,
570 'is_active' => 1,
571 ],
ca64c337 572 ],
573 ],
79c9d4c2 574 ],
575 'Date' => [
576 'default' => [
577 'name' => 'test_date',
578 'label' => 'Test Date',
579 'html_type' => 'Select Date',
580 'data_type' => 'Date',
581 'default_value' => '20090711',
582 'weight' => 3,
583 'is_search_range' => 1,
584 'time_format' => 1,
585 ],
586 ],
ca64c337 587 'Float' => [
588 'default' => [
589 'label' => 'Number',
590 'html_type' => 'Text',
591 'data_type' => 'Float',
592 ],
593 'Select' => [
594 'label' => 'Number select',
595 'html_type' => 'Select',
596 'option_values' => [
597 [
598 'label' => '50',
599 'value' => 3,
600 'weight' => 1,
601 'is_active' => 1,
602 ],
603 [
604 'label' => '100',
605 'value' => 4,
606 'weight' => 2,
607 'is_active' => 1,
608 ],
609 ],
610 ],
611 'Radio' => [
612 'label' => 'Number radio',
613 'html_type' => 'Radio',
614 'option_values' => [
615 [
616 'label' => '50',
617 'value' => 3,
618 'weight' => 1,
619 'is_active' => 1,
620 ],
621 [
622 'label' => '100',
623 'value' => 4,
624 'weight' => 2,
625 'is_active' => 1,
626 ],
627 ],
628 ],
629 ],
630 'Money' => [
631 'default' => [
632 'label' => 'Money',
633 'html_type' => 'Text',
634 'data_type' => 'Money',
635 ],
636 'Select' => [
637 'label' => 'Money select',
638 'html_type' => 'Select',
639 'option_values' => [
640 [
641 'label' => '50',
642 'value' => 3,
643 'weight' => 1,
644 'is_active' => 1,
645 ],
646 [
647 'label' => '100',
648 'value' => 4,
649 'weight' => 2,
650 'is_active' => 1,
651 ],
652 ],
653 ],
654 'Radio' => [
655 'label' => 'Money radio',
656 'html_type' => 'Radio',
657 'option_values' => [
658 [
659 'label' => '50',
660 'value' => 3,
661 'weight' => 1,
662 'is_active' => 1,
663 ],
664 [
665 'label' => '100',
666 'value' => 4,
667 'weight' => 2,
668 'is_active' => 1,
669 ],
670 ],
671 ],
672 ],
673 'Memo' => [
674 'default' => [
675 'label' => 'Memo',
676 'html_type' => 'TextArea',
677 'data_type' => 'Memo',
678 'attributes' => 'rows=4, cols=60',
679 ],
680 'RichTextEditor' => [
681 'label' => 'Memo Rich Text Editor',
682 'html_type' => 'Memo',
683 ],
684 ],
685 'Boolean' => [
686 'default' => [
687 'data_type' => 'Boolean',
688 'html_type' => 'Radio',
689 'label' => 'Yes No',
690 ],
691 ],
692 'StateProvince' => [
693 'default' => [
694 'data_type' => 'StateProvince',
695 'html_type' => 'Select State/Province',
696 'label' => 'State',
697 'option_type' => 0,
698 ],
699 'Multi-Select State/Province' => [
700 'html_type' => 'Multi-Select State/Province',
701 'label' => 'State-multi',
702 ],
703 ],
79c9d4c2 704 'Country' => [
705 'default' => [
706 'data_type' => 'Country',
707 'html_type' => 'Select Country',
708 'label' => 'Country',
709 'option_type' => 0,
710 ],
ca64c337 711 'Multi-Select Country' => [
712 'html_type' => 'Multi-Select Country',
713 'label' => 'Country-multi',
714 'option_type' => 0,
715 ],
79c9d4c2 716 ],
717 'File' => [
718 'default' => [
719 'label' => 'My file',
720 'data_type' => 'File',
721 'html_type' => 'File',
722 ],
723 ],
724 'Link' => [
725 'default' => [
726 'name' => 'test_link',
727 'label' => 'test_link',
728 'html_type' => 'Link',
729 'data_type' => 'Link',
730 'default_value' => 'http://civicrm.org',
731 ],
732 ],
733 'ContactReference' => [
734 'default' => [
735 'label' => 'Contact reference field',
736 'html_type' => 'Autocomplete-Select',
737 'data_type' => 'ContactReference',
738 ],
739 ],
740 ];
35aca6d6 741 }
742
8ba5884d 743}