Merge pull request #15884 from kainuk/issue-lab-1365
[civicrm-core.git] / tests / phpunit / CRMTraits / Custom / CustomDataTrait.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Trait Custom Data trait.
14 *
15 * Trait for setting up custom data in tests.
16 */
17 trait CRMTraits_Custom_CustomDataTrait {
18
19 /**
20 * Create a custom group with fields of multiple types.
21 *
22 * @param array $groupParams
23 */
24 public function createCustomGroupWithFieldsOfAllTypes($groupParams = []) {
25 $this->createCustomGroup($groupParams);
26 $this->ids['CustomField'] = $this->createCustomFieldsOfAllTypes();
27 }
28
29 /**
30 * Create a custom group.
31 *
32 * @param array $params
33 *
34 * @return int
35 */
36 public function createCustomGroup($params = []) {
37 $params = array_merge([
38 'title' => 'Custom Group',
39 'extends' => [$this->entity ?? 'Contact'],
40 'weight' => 5,
41 'style' => 'Inline',
42 'max_multiple' => 0,
43 ], $params);
44 $identifier = $params['name'] ?? $params['title'];
45 $this->ids['CustomGroup'][$identifier] = $this->callAPISuccess('CustomGroup', 'create', $params)['id'];
46 return $this->ids['CustomGroup'][$identifier];
47 }
48
49 /**
50 * Get the table_name for the specified custom group.
51 *
52 * @param string $identifier
53 *
54 * @return string
55 */
56 public function getCustomGroupTable($identifier = 'Custom Group') {
57 return $this->callAPISuccessGetValue('CustomGroup', ['id' => $this->ids['CustomGroup'][$identifier], 'return' => 'table_name']);
58 }
59
60 /**
61 * Get the the column name for the identified custom field.
62 *
63 * @param string $key
64 * Identifier - generally keys map to data type - eg. 'text', 'int' etc.
65 *
66 * @return string
67 */
68 protected function getCustomFieldColumnName($key) {
69 return $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID($key), 'return' => 'column_name']);
70 }
71
72 /**
73 * Create a custom group with a single field.
74 *
75 * @param array $groupParams
76 * @param string $customFieldType
77 *
78 * @param string $identifier
79 *
80 * @throws \CRM_Core_Exception
81 */
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');
86 }
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) {
91 case 'text':
92 $customField = $this->createTextCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['name']]]);
93 break;
94
95 case 'select':
96 $customField = $this->createSelectCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['name']]]);
97 break;
98
99 case 'int':
100 $customField = $this->createIntCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['name']]]);
101 break;
102
103 case 'date':
104 $customField = $this->createDateCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['name']]]);
105 break;
106 }
107 $this->ids['CustomField'][$identifier . $customFieldType] = $customField['id'];
108 }
109
110 /**
111 * @return array
112 */
113 public function createCustomFieldsOfAllTypes() {
114 $customGroupID = $this->ids['CustomGroup']['Custom Group'];
115 $ids = [];
116 $customField = $this->createTextCustomField(['custom_group_id' => $customGroupID]);
117 $ids['text'] = $customField['id'];
118
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'];
122 }
123
124 $customField = $this->createDateCustomField(['custom_group_id' => $customGroupID]);
125 $ids['select_date'] = $customField['id'];
126
127 $customField = $this->createIntCustomField(['custom_group_id' => $customGroupID]);
128 $ids['int'] = $customField['id'];
129
130 $params = [
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',
137 'weight' => 4,
138 'is_required' => 1,
139 'is_searchable' => 0,
140 'is_active' => 1,
141 ];
142
143 $customField = $this->callAPISuccess('custom_field', 'create', $params);
144
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' => '',
151 ]);
152
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',
160 'option_type' => 0,
161 ])['id'];
162
163 return $ids;
164 }
165
166 /**
167 * Get the custom field name for the relevant key.
168 *
169 * e.g returns 'custom_5' where 5 is the id of the field using the key.
170 *
171 * Generally keys map to data types.
172 *
173 * @param string $key
174 *
175 * @return string
176 */
177 protected function getCustomFieldName($key) {
178 return 'custom_' . $this->getCustomFieldID($key);
179 }
180
181 /**
182 * Get the custom field name for the relevant key.
183 *
184 * e.g returns 'custom_5' where 5 is the id of the field using the key.
185 *
186 * Generally keys map to data types.
187 *
188 * @param string $key
189 *
190 * @return string
191 */
192 protected function getCustomFieldID($key) {
193 return $this->ids['CustomField'][$key];
194 }
195
196 /**
197 * Create a custom text fields.
198 *
199 * @param array $params
200 * Parameter overrides, must include custom_group_id.
201 *
202 * @return array
203 */
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',
210 'weight' => 1,
211 'is_required' => 1,
212 'sequential' => 1,
213 'is_searchable' => 1,
214 'is_search_range' => 1,
215 ], $params);
216
217 return $this->callAPISuccess('CustomField', 'create', $params)['values'][0];
218 }
219
220 /**
221 * Create a custom text fields.
222 *
223 * @param array $params
224 * Parameter overrides, must include custom_group_id.
225 *
226 * @return array
227 */
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',
234 'weight' => 1,
235 'is_required' => 1,
236 'sequential' => 1,
237 'is_searchable' => 1,
238 'text_length' => 300,
239 ], $params);
240
241 return $this->callAPISuccess('CustomField', 'create', $params)['values'][0];
242 }
243
244 /**
245 * Create custom select field.
246 *
247 * @param array $params
248 * Parameter overrides, must include custom_group_id.
249 *
250 * @return array
251 */
252 protected function createSelectCustomField(array $params): array {
253 $optionValue = [
254 [
255 'label' => 'Red',
256 'value' => 'R',
257 'weight' => 1,
258 'is_active' => 1,
259 ],
260 [
261 'label' => 'Yellow',
262 'value' => 'Y',
263 'weight' => 2,
264 'is_active' => 1,
265 ],
266 [
267 'label' => 'Green',
268 'value' => 'G',
269 'weight' => 3,
270 'is_active' => 1,
271 ],
272 ];
273
274 $params = array_merge([
275 'label' => 'Pick Color',
276 'html_type' => 'Select',
277 'data_type' => 'String',
278 'weight' => 2,
279 'is_required' => 1,
280 'is_searchable' => 0,
281 'is_active' => 1,
282 'option_values' => $optionValue,
283 ], $params);
284
285 $customField = $this->callAPISuccess('custom_field', 'create', $params);
286 return $customField['values'][$customField['id']];
287 }
288
289 /**
290 * Create a custom field of type date.
291 *
292 * @param array $params
293 *
294 * @return array
295 */
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',
303 'weight' => 3,
304 'is_searchable' => 1,
305 'is_search_range' => 1,
306 'time_format' => 1,
307 ], $params);
308
309 $customField = $this->callAPISuccess('custom_field', 'create', $params);
310 return $customField['values'][$customField['id']];
311 }
312
313 }