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 +--------------------------------------------------------------------+
13 * Trait Custom Data trait.
15 * Trait for setting up custom data in tests.
17 trait CRMTraits_Custom_CustomDataTrait
{
20 * Create a custom group with fields of multiple types.
22 * @param array $groupParams
24 public function createCustomGroupWithFieldsOfAllTypes($groupParams = []) {
25 $this->createCustomGroup($groupParams);
26 $this->ids
['CustomField'] = $this->createCustomFieldsOfAllTypes();
30 * Create a custom group.
32 * @param array $params
36 public function createCustomGroup($params = []) {
37 $params = array_merge([
38 'title' => 'Custom Group',
39 'extends' => [$this->entity ??
'Contact'],
44 $identifier = $params['name'] ??
$params['title'];
45 $this->ids
['CustomGroup'][$identifier] = $this->callAPISuccess('CustomGroup', 'create', $params)['id'];
46 return $this->ids
['CustomGroup'][$identifier];
50 * Get the table_name for the specified custom group.
52 * @param string $identifier
56 public function getCustomGroupTable($identifier = 'Custom Group') {
57 return $this->callAPISuccessGetValue('CustomGroup', ['id' => $this->ids
['CustomGroup'][$identifier], 'return' => 'table_name']);
61 * Get the the column name for the identified custom field.
64 * Identifier - generally keys map to data type - eg. 'text', 'int' etc.
68 protected function getCustomFieldColumnName($key) {
69 return $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID($key), 'return' => 'column_name']);
73 * Create a custom group with a single field.
75 * @param array $groupParams
76 * @param string $customFieldType
78 * @param string $identifier
80 * @throws \CRM_Core_Exception
82 public function createCustomGroupWithFieldOfType($groupParams = [], $customFieldType = 'text', $identifier = NULL) {
83 $supported = ['text', 'select', 'date', 'int'];
84 if (!in_array($customFieldType, $supported, TRUE)) {
85 throw new CRM_Core_Exception('we have not yet extracted other custom field types from createCustomFieldsOfAllTypes, Use consistent syntax when you do');
87 $groupParams['title'] = empty($groupParams['title']) ?
$identifier . 'Group with field ' . $customFieldType : $groupParams['title'];
88 $groupParams['name'] = $identifier ??
'Custom Group';
89 $this->createCustomGroup($groupParams);
90 switch ($customFieldType) {
92 $customField = $this->createTextCustomField(['custom_group_id' => $this->ids
['CustomGroup'][$groupParams['name']]]);
96 $customField = $this->createSelectCustomField(['custom_group_id' => $this->ids
['CustomGroup'][$groupParams['name']]]);
100 $customField = $this->createIntCustomField(['custom_group_id' => $this->ids
['CustomGroup'][$groupParams['name']]]);
104 $customField = $this->createDateCustomField(['custom_group_id' => $this->ids
['CustomGroup'][$groupParams['name']]]);
107 $this->ids
['CustomField'][$identifier . $customFieldType] = $customField['id'];
113 public function createCustomFieldsOfAllTypes() {
114 $customGroupID = $this->ids
['CustomGroup']['Custom Group'];
116 $ids['text'] = (int) $this->createTextCustomField(['custom_group_id' => $customGroupID])['id'];
117 $ids['select_string'] = (int) $this->createSelectCustomField(['custom_group_id' => $customGroupID])['id'];
118 $ids['select_date'] = (int) $this->createDateCustomField(['custom_group_id' => $customGroupID])['id'];
119 $ids['int'] = (int) $this->createIntCustomField(['custom_group_id' => $customGroupID])['id'];
120 $ids['link'] = (int) $this->createLinkCustomField(['custom_group_id' => $customGroupID])['id'];
121 $ids['file'] = (int) $this->createFileCustomField(['custom_group_id' => $customGroupID])['id'];
122 $ids['country'] = (int) $this->createCountryCustomField(['custom_group_id' => $customGroupID])['id'];
123 $ids['contact_reference'] = $this->createContactReferenceCustomField(['custom_group_id' => $customGroupID])['id'];
128 * Get the custom field name for the relevant key.
130 * e.g returns 'custom_5' where 5 is the id of the field using the key.
132 * Generally keys map to data types.
138 protected function getCustomFieldName($key) {
139 return 'custom_' . $this->getCustomFieldID($key);
143 * Get the custom field name for the relevant key.
145 * e.g returns 'custom_5' where 5 is the id of the field using the key.
147 * Generally keys map to data types.
153 protected function getCustomFieldID($key) {
154 return $this->ids
['CustomField'][$key];
158 * Create a custom text fields.
160 * @param array $params
161 * Parameter overrides, must include custom_group_id.
165 protected function createIntCustomField($params = []) {
166 $params = array_merge($this->getFieldsValuesByType('Int'), $params);
167 return $this->callAPISuccess('CustomField', 'create', $params)['values'][0];
171 * Create a custom text fields.
173 * @param array $params
174 * Parameter overrides, must include custom_group_id.
178 protected function createContactReferenceCustomField($params = []) {
179 $params = array_merge($this->getFieldsValuesByType('ContactReference'), $params);
180 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
184 * Create a custom text fields.
186 * @param array $params
187 * Parameter overrides, must include custom_group_id.
191 protected function createTextCustomField($params = []) {
192 $params = array_merge($this->getFieldsValuesByType('String'), $params);
193 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
197 * Create a custom text fields.
199 * @param array $params
200 * Parameter overrides, must include custom_group_id.
204 protected function createLinkCustomField($params = []) {
205 $params = array_merge($this->getFieldsValuesByType('Link'), $params);
206 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
210 * Create a custom text fields.
212 * @param array $params
213 * Parameter overrides, must include custom_group_id.
217 protected function createCountryCustomField($params = []) {
218 $params = array_merge($this->getFieldsValuesByType('Country'), $params);
219 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
223 * Create a custom text fields.
225 * @param array $params
226 * Parameter overrides, must include custom_group_id.
230 protected function createFileCustomField($params = []) {
231 $params = array_merge($this->getFieldsValuesByType('File'), $params);
232 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
236 * Create custom select field.
238 * @param array $params
239 * Parameter overrides, must include custom_group_id.
243 protected function createSelectCustomField(array $params): array {
244 $params = array_merge($this->getFieldsValuesByType('String', 'Select'), $params);
245 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
249 * Create a custom field of type date.
251 * @param array $params
255 protected function createDateCustomField($params): array {
256 $params = array_merge($this->getFieldsValuesByType('Date'), $params);
257 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
261 * Get default field values for the type of field.
264 * @param string $htmlType
268 public function getFieldsValuesByType($dataType, $htmlType = 'default') {
269 $values = $this->getAvailableFieldCombinations()[$dataType];
271 'is_searchable' => 1,
273 'default_value' => '',
275 ], array_merge($values['default'], $values[$htmlType])
280 * Get data available for custom fields.
282 * The 'default' key holds general values. Where more than one html type is an option
283 * then the any values that differ to the defaults are keyed by html key.
285 * The order below is consistent with the UI.
289 protected function getAvailableFieldCombinations() {
293 'label' => 'Enter text here',
294 'html_type' => 'Text',
295 'data_type' => 'String',
296 'default_value' => 'xyz',
297 'text_length' => 300,
300 'label' => 'Pick Color',
301 'html_type' => 'Select',
302 'data_type' => 'String',
304 'default_value' => '',
329 'label' => 'Enter integer here',
330 'html_type' => 'Text',
331 'data_type' => 'Int',
332 'default_value' => '4',
333 'is_search_range' => 1,
338 'name' => 'test_date',
339 'label' => 'Test Date',
340 'html_type' => 'Select Date',
341 'data_type' => 'Date',
342 'default_value' => '20090711',
344 'is_search_range' => 1,
350 'data_type' => 'Country',
351 'html_type' => 'Select Country',
352 'label' => 'Country',
358 'label' => 'My file',
359 'data_type' => 'File',
360 'html_type' => 'File',
365 'name' => 'test_link',
366 'label' => 'test_link',
367 'html_type' => 'Link',
368 'data_type' => 'Link',
369 'default_value' => 'http://civicrm.org',
372 'ContactReference' => [
374 'label' => 'Contact reference field',
375 'html_type' => 'Autocomplete-Select',
376 'data_type' => 'ContactReference',