Merge pull request #16957 from pradpnayak/importMappings
[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 use Civi\Api4\CustomGroup;
13
14 /**
15 * Trait Custom Data trait.
16 *
17 * Trait for setting up custom data in tests.
18 */
19 trait CRMTraits_Custom_CustomDataTrait {
20
21 /**
22 * Create a custom group with fields of multiple types.
23 *
24 * @param array $groupParams
25 *
26 * @throws \API_Exception
27 * @throws \Civi\API\Exception\UnauthorizedException
28 */
29 public function createCustomGroupWithFieldsOfAllTypes($groupParams = []) {
30 $this->createCustomGroup($groupParams);
31 $this->ids['CustomField'] = $this->createCustomFieldsOfAllTypes();
32 }
33
34 /**
35 * Create a custom group.
36 *
37 * @param array $params
38 *
39 * @return int
40 *
41 * @throws \API_Exception
42 * @throws \Civi\API\Exception\UnauthorizedException
43 */
44 public function createCustomGroup($params = []) {
45 $params = array_merge([
46 'title' => 'Custom Group',
47 'extends' => [$this->entity ?? 'Contact'],
48 'weight' => 5,
49 'style' => 'Inline',
50 'max_multiple' => 0,
51 ], $params);
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];
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']);
78 }
79
80 /**
81 * Create a custom group with a single field.
82 *
83 * @param array $groupParams
84 * @param string $customFieldType
85 *
86 * @param string $identifier
87 *
88 * @throws \API_Exception
89 * @throws \CRM_Core_Exception
90 * @throws \Civi\API\Exception\UnauthorizedException
91 */
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');
96 }
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) {
101 case 'text':
102 $customField = $this->createTextCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['name']]]);
103 break;
104
105 case 'select':
106 $customField = $this->createSelectCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['name']]]);
107 break;
108
109 case 'int':
110 $customField = $this->createIntCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['name']]]);
111 break;
112
113 case 'date':
114 $customField = $this->createDateCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['name']]]);
115 break;
116 }
117 $this->ids['CustomField'][$identifier . $customFieldType] = $customField['id'];
118 }
119
120 /**
121 * @return array
122 */
123 public function createCustomFieldsOfAllTypes() {
124 $customGroupID = $this->ids['CustomGroup']['Custom Group'];
125 $ids = [];
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'];
134 return $ids;
135 }
136
137 /**
138 * Get the custom field name for the relevant key.
139 *
140 * e.g returns 'custom_5' where 5 is the id of the field using the key.
141 *
142 * Generally keys map to data types.
143 *
144 * @param string $key
145 *
146 * @return string
147 */
148 protected function getCustomFieldName($key) {
149 return 'custom_' . $this->getCustomFieldID($key);
150 }
151
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 getCustomFieldID($key) {
164 return $this->ids['CustomField'][$key];
165 }
166
167 /**
168 * Create a custom text fields.
169 *
170 * @param array $params
171 * Parameter overrides, must include custom_group_id.
172 *
173 * @return array
174 */
175 protected function createIntCustomField($params = []) {
176 $params = array_merge($this->getFieldsValuesByType('Int'), $params);
177 return $this->callAPISuccess('CustomField', 'create', $params)['values'][0];
178 }
179
180 /**
181 * Create a custom text fields.
182 *
183 * @param array $params
184 * Parameter overrides, must include custom_group_id.
185 *
186 * @return array
187 */
188 protected function createContactReferenceCustomField($params = []) {
189 $params = array_merge($this->getFieldsValuesByType('ContactReference'), $params);
190 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
191 }
192
193 /**
194 * Create a custom text fields.
195 *
196 * @param array $params
197 * Parameter overrides, must include custom_group_id.
198 *
199 * @return array
200 */
201 protected function createTextCustomField($params = []) {
202 $params = array_merge($this->getFieldsValuesByType('String'), $params);
203 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
204 }
205
206 /**
207 * Create a custom text fields.
208 *
209 * @param array $params
210 * Parameter overrides, must include custom_group_id.
211 *
212 * @return array
213 */
214 protected function createLinkCustomField($params = []) {
215 $params = array_merge($this->getFieldsValuesByType('Link'), $params);
216 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
217 }
218
219 /**
220 * Create a custom text fields.
221 *
222 * @param array $params
223 * Parameter overrides, must include custom_group_id.
224 *
225 * @return array
226 */
227 protected function createCountryCustomField($params = []) {
228 $params = array_merge($this->getFieldsValuesByType('Country'), $params);
229 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
230 }
231
232 /**
233 * Create a custom text fields.
234 *
235 * @param array $params
236 * Parameter overrides, must include custom_group_id.
237 *
238 * @return array
239 */
240 protected function createFileCustomField($params = []) {
241 $params = array_merge($this->getFieldsValuesByType('File'), $params);
242 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
243 }
244
245 /**
246 * Create custom select field.
247 *
248 * @param array $params
249 * Parameter overrides, must include custom_group_id.
250 *
251 * @return array
252 */
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];
256 }
257
258 /**
259 * Create a custom field of type date.
260 *
261 * @param array $params
262 *
263 * @return array
264 */
265 protected function createDateCustomField($params): array {
266 $params = array_merge($this->getFieldsValuesByType('Date'), $params);
267 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
268 }
269
270 /**
271 * Get default field values for the type of field.
272 *
273 * @param $dataType
274 * @param string $htmlType
275 *
276 * @return mixed
277 */
278 public function getFieldsValuesByType($dataType, $htmlType = 'default') {
279 $values = $this->getAvailableFieldCombinations()[$dataType];
280 return array_merge([
281 'is_searchable' => 1,
282 'sequential' => 1,
283 'default_value' => '',
284 'is_required' => 0,
285 ], array_merge($values['default'], $values[$htmlType])
286 );
287 }
288
289 /**
290 * Get data available for custom fields.
291 *
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.
294 *
295 * The order below is consistent with the UI.
296 *
297 * @return array
298 */
299 protected function getAvailableFieldCombinations() {
300 return [
301 'String' => [
302 'default' => [
303 'label' => 'Enter text here',
304 'html_type' => 'Text',
305 'data_type' => 'String',
306 'default_value' => 'xyz',
307 'text_length' => 300,
308 ],
309 'Select' => [
310 'label' => 'Pick Color',
311 'html_type' => 'Select',
312 'data_type' => 'String',
313 'text_length' => '',
314 'default_value' => '',
315 'option_values' => [
316 [
317 'label' => 'Red',
318 'value' => 'R',
319 'weight' => 1,
320 'is_active' => 1,
321 ],
322 [
323 'label' => 'Yellow',
324 'value' => 'Y',
325 'weight' => 2,
326 'is_active' => 1,
327 ],
328 [
329 'label' => 'Green',
330 'value' => 'G',
331 'weight' => 3,
332 'is_active' => 1,
333 ],
334 ],
335 ],
336 ],
337 'Int' => [
338 'default' => [
339 'label' => 'Enter integer here',
340 'html_type' => 'Text',
341 'data_type' => 'Int',
342 'default_value' => '4',
343 'is_search_range' => 1,
344 ],
345 ],
346 'Date' => [
347 'default' => [
348 'name' => 'test_date',
349 'label' => 'Test Date',
350 'html_type' => 'Select Date',
351 'data_type' => 'Date',
352 'default_value' => '20090711',
353 'weight' => 3,
354 'is_search_range' => 1,
355 'time_format' => 1,
356 ],
357 ],
358 'Country' => [
359 'default' => [
360 'data_type' => 'Country',
361 'html_type' => 'Select Country',
362 'label' => 'Country',
363 'option_type' => 0,
364 ],
365 ],
366 'File' => [
367 'default' => [
368 'label' => 'My file',
369 'data_type' => 'File',
370 'html_type' => 'File',
371 ],
372 ],
373 'Link' => [
374 'default' => [
375 'name' => 'test_link',
376 'label' => 'test_link',
377 'html_type' => 'Link',
378 'data_type' => 'Link',
379 'default_value' => 'http://civicrm.org',
380 ],
381 ],
382 'ContactReference' => [
383 'default' => [
384 'label' => 'Contact reference field',
385 'html_type' => 'Autocomplete-Select',
386 'data_type' => 'ContactReference',
387 ],
388 ],
389 ];
390 }
391
392 }