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 $customField = $this->createTextCustomField(['custom_group_id' => $customGroupID]);
117 $ids['text'] = $customField['id'];
119 if ((!empty($this->entity
) && $this->entity
!== 'Contribution') ||
empty($this->entity
)) {
120 $customField = $this->createSelectCustomField(['custom_group_id' => $customGroupID]);
121 $ids['select_string'] = $customField['id'];
124 $customField = $this->createDateCustomField(['custom_group_id' => $customGroupID]);
125 $ids['select_date'] = $customField['id'];
127 $customField = $this->createIntCustomField(['custom_group_id' => $customGroupID]);
128 $ids['int'] = $customField['id'];
131 'custom_group_id' => $customGroupID,
132 'name' => 'test_link',
133 'label' => 'test_link',
134 'html_type' => 'Link',
135 'data_type' => 'Link',
136 'default_value' => 'http://civicrm.org',
139 'is_searchable' => 0,
143 $customField = $this->callAPISuccess('custom_field', 'create', $params);
145 $ids['link'] = $customField['id'];
146 $fileField = $this->customFieldCreate([
147 'custom_group_id' => $customGroupID,
148 'data_type' => 'File',
149 'html_type' => 'File',
150 'default_value' => '',
153 $ids['file'] = $fileField['id'];
154 $ids['country'] = $this->customFieldCreate([
155 'custom_group_id' => $customGroupID,
156 'data_type' => 'Country',
157 'html_type' => 'Select Country',
158 'default_value' => '',
159 'label' => 'Country',
167 * Get the custom field name for the relevant key.
169 * e.g returns 'custom_5' where 5 is the id of the field using the key.
171 * Generally keys map to data types.
177 protected function getCustomFieldName($key) {
178 return 'custom_' . $this->getCustomFieldID($key);
182 * Get the custom field name for the relevant key.
184 * e.g returns 'custom_5' where 5 is the id of the field using the key.
186 * Generally keys map to data types.
192 protected function getCustomFieldID($key) {
193 return $this->ids
['CustomField'][$key];
197 * Create a custom text fields.
199 * @param array $params
200 * Parameter overrides, must include custom_group_id.
204 protected function createIntCustomField($params = []) {
205 $params = array_merge([
206 'label' => 'Enter integer here',
207 'html_type' => 'Text',
208 'data_type' => 'Int',
209 'default_value' => '4',
213 'is_searchable' => 1,
214 'is_search_range' => 1,
217 return $this->callAPISuccess('CustomField', 'create', $params)['values'][0];
221 * Create a custom text fields.
223 * @param array $params
224 * Parameter overrides, must include custom_group_id.
228 protected function createTextCustomField($params = []) {
229 $params = array_merge([
230 'label' => 'Enter text here',
231 'html_type' => 'Text',
232 'data_type' => 'String',
233 'default_value' => 'xyz',
237 'is_searchable' => 1,
238 'text_length' => 300,
241 return $this->callAPISuccess('CustomField', 'create', $params)['values'][0];
245 * Create custom select field.
247 * @param array $params
248 * Parameter overrides, must include custom_group_id.
252 protected function createSelectCustomField(array $params): array {
274 $params = array_merge([
275 'label' => 'Pick Color',
276 'html_type' => 'Select',
277 'data_type' => 'String',
280 'is_searchable' => 0,
282 'option_values' => $optionValue,
285 $customField = $this->callAPISuccess('custom_field', 'create', $params);
286 return $customField['values'][$customField['id']];
290 * Create a custom field of type date.
292 * @param array $params
296 protected function createDateCustomField($params): array {
297 $params = array_merge([
298 'name' => 'test_date',
299 'label' => 'Test Date',
300 'html_type' => 'Select Date',
301 'data_type' => 'Date',
302 'default_value' => '20090711',
304 'is_searchable' => 1,
305 'is_search_range' => 1,
309 $customField = $this->callAPISuccess('custom_field', 'create', $params);
310 return $customField['values'][$customField['id']];