3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
12 use Civi\Api4\CustomGroup
;
15 * Trait Custom Data trait.
17 * Trait for setting up custom data in tests.
19 trait CRMTraits_Custom_CustomDataTrait
{
22 * Create a custom group with fields of multiple types.
24 * @param array $groupParams
26 * @throws \API_Exception
27 * @throws \Civi\API\Exception\UnauthorizedException
29 public function createCustomGroupWithFieldsOfAllTypes($groupParams = []) {
30 $this->createCustomGroup($groupParams);
31 $this->ids
['CustomField'] = $this->createCustomFieldsOfAllTypes();
35 * Create a custom group.
37 * @param array $params
41 * @throws \API_Exception
42 * @throws \Civi\API\Exception\UnauthorizedException
44 public function createCustomGroup($params = []) {
45 $params = array_merge([
46 'title' => 'Custom Group',
47 'extends' => [$this->entity ??
'Contact'],
52 $identifier = $params['name'] ??
$params['title'];
53 $this->ids
['CustomGroup'][$identifier] = CustomGroup
::create()->setCheckPermissions(FALSE)->setValues($params)->execute()->first()['id'];
54 return $this->ids
['CustomGroup'][$identifier];
58 * Get the table_name for the specified custom group.
60 * @param string $identifier
64 public function getCustomGroupTable($identifier = 'Custom Group') {
65 return $this->callAPISuccessGetValue('CustomGroup', ['id' => $this->ids
['CustomGroup'][$identifier], 'return' => 'table_name']);
69 * Get the the column name for the identified custom field.
72 * Identifier - generally keys map to data type - eg. 'text', 'int' etc.
76 protected function getCustomFieldColumnName($key) {
77 return $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID($key), 'return' => 'column_name']);
81 * Create a custom group with a single field.
83 * @param array $groupParams
84 * @param string $customFieldType
86 * @param string $identifier
88 * @throws \API_Exception
89 * @throws \CRM_Core_Exception
90 * @throws \Civi\API\Exception\UnauthorizedException
92 public function createCustomGroupWithFieldOfType($groupParams = [], $customFieldType = 'text', $identifier = NULL) {
93 $supported = ['text', 'select', 'date', 'int'];
94 if (!in_array($customFieldType, $supported, TRUE)) {
95 throw new CRM_Core_Exception('we have not yet extracted other custom field types from createCustomFieldsOfAllTypes, Use consistent syntax when you do');
97 $groupParams['title'] = empty($groupParams['title']) ?
$identifier . 'Group with field ' . $customFieldType : $groupParams['title'];
98 $groupParams['name'] = $identifier ??
'Custom Group';
99 $this->createCustomGroup($groupParams);
100 switch ($customFieldType) {
102 $customField = $this->createTextCustomField(['custom_group_id' => $this->ids
['CustomGroup'][$groupParams['name']]]);
106 $customField = $this->createSelectCustomField(['custom_group_id' => $this->ids
['CustomGroup'][$groupParams['name']]]);
110 $customField = $this->createIntCustomField(['custom_group_id' => $this->ids
['CustomGroup'][$groupParams['name']]]);
114 $customField = $this->createDateCustomField(['custom_group_id' => $this->ids
['CustomGroup'][$groupParams['name']]]);
117 $this->ids
['CustomField'][$identifier . $customFieldType] = $customField['id'];
123 public function createCustomFieldsOfAllTypes() {
124 $customGroupID = $this->ids
['CustomGroup']['Custom Group'];
126 $ids['text'] = (int) $this->createTextCustomField(['custom_group_id' => $customGroupID])['id'];
127 $ids['select_string'] = (int) $this->createSelectCustomField(['custom_group_id' => $customGroupID])['id'];
128 $ids['select_date'] = (int) $this->createDateCustomField(['custom_group_id' => $customGroupID])['id'];
129 $ids['int'] = (int) $this->createIntCustomField(['custom_group_id' => $customGroupID])['id'];
130 $ids['link'] = (int) $this->createLinkCustomField(['custom_group_id' => $customGroupID])['id'];
131 $ids['file'] = (int) $this->createFileCustomField(['custom_group_id' => $customGroupID])['id'];
132 $ids['country'] = (int) $this->createCountryCustomField(['custom_group_id' => $customGroupID])['id'];
133 $ids['contact_reference'] = $this->createContactReferenceCustomField(['custom_group_id' => $customGroupID])['id'];
138 * Get the custom field name for the relevant key.
140 * e.g returns 'custom_5' where 5 is the id of the field using the key.
142 * Generally keys map to data types.
148 protected function getCustomFieldName($key) {
149 return 'custom_' . $this->getCustomFieldID($key);
153 * Get the custom field name for the relevant key.
155 * e.g returns 'custom_5' where 5 is the id of the field using the key.
157 * Generally keys map to data types.
163 protected function getCustomFieldID($key) {
164 return $this->ids
['CustomField'][$key];
168 * Create a custom text fields.
170 * @param array $params
171 * Parameter overrides, must include custom_group_id.
175 protected function createIntCustomField($params = []) {
176 $params = array_merge($this->getFieldsValuesByType('Int'), $params);
177 return $this->callAPISuccess('CustomField', 'create', $params)['values'][0];
181 * Create a custom text fields.
183 * @param array $params
184 * Parameter overrides, must include custom_group_id.
188 protected function createContactReferenceCustomField($params = []) {
189 $params = array_merge($this->getFieldsValuesByType('ContactReference'), $params);
190 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
194 * Create a custom text fields.
196 * @param array $params
197 * Parameter overrides, must include custom_group_id.
201 protected function createTextCustomField($params = []) {
202 $params = array_merge($this->getFieldsValuesByType('String'), $params);
203 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
207 * Create a custom text fields.
209 * @param array $params
210 * Parameter overrides, must include custom_group_id.
214 protected function createLinkCustomField($params = []) {
215 $params = array_merge($this->getFieldsValuesByType('Link'), $params);
216 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
220 * Create a custom text fields.
222 * @param array $params
223 * Parameter overrides, must include custom_group_id.
227 protected function createCountryCustomField($params = []) {
228 $params = array_merge($this->getFieldsValuesByType('Country'), $params);
229 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
233 * Create a custom text fields.
235 * @param array $params
236 * Parameter overrides, must include custom_group_id.
240 protected function createFileCustomField($params = []) {
241 $params = array_merge($this->getFieldsValuesByType('File'), $params);
242 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
246 * Create custom select field.
248 * @param array $params
249 * Parameter overrides, must include custom_group_id.
253 protected function createSelectCustomField(array $params): array {
254 $params = array_merge($this->getFieldsValuesByType('String', 'Select'), $params);
255 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
259 * Create a custom field of type date.
261 * @param array $params
265 protected function createDateCustomField($params): array {
266 $params = array_merge($this->getFieldsValuesByType('Date'), $params);
267 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
271 * Get default field values for the type of field.
274 * @param string $htmlType
278 public function getFieldsValuesByType($dataType, $htmlType = 'default') {
279 $values = $this->getAvailableFieldCombinations()[$dataType];
281 'is_searchable' => 1,
283 'default_value' => '',
285 ], array_merge($values['default'], $values[$htmlType])
290 * Get data available for custom fields.
292 * The 'default' key holds general values. Where more than one html type is an option
293 * then the any values that differ to the defaults are keyed by html key.
295 * The order below is consistent with the UI.
299 protected function getAvailableFieldCombinations() {
303 'label' => 'Enter text here',
304 'html_type' => 'Text',
305 'data_type' => 'String',
306 'default_value' => 'xyz',
307 'text_length' => 300,
310 'label' => 'Pick Color',
311 'html_type' => 'Select',
312 'data_type' => 'String',
314 'default_value' => '',
339 'label' => 'Enter integer here',
340 'html_type' => 'Text',
341 'data_type' => 'Int',
342 'default_value' => '4',
343 'is_search_range' => 1,
348 'name' => 'test_date',
349 'label' => 'Test Date',
350 'html_type' => 'Select Date',
351 'data_type' => 'Date',
352 'default_value' => '20090711',
354 'is_search_range' => 1,
360 'data_type' => 'Country',
361 'html_type' => 'Select Country',
362 'label' => 'Country',
368 'label' => 'My file',
369 'data_type' => 'File',
370 'html_type' => 'File',
375 'name' => 'test_link',
376 'label' => 'test_link',
377 'html_type' => 'Link',
378 'data_type' => 'Link',
379 'default_value' => 'http://civicrm.org',
382 'ContactReference' => [
384 'label' => 'Contact reference field',
385 'html_type' => 'Autocomplete-Select',
386 'data_type' => 'ContactReference',