[NFC][Test] cleanup on customDataTrait for tests
[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 $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'];
124 return $ids;
125 }
126
127 /**
128 * Get the custom field name for the relevant key.
129 *
130 * e.g returns 'custom_5' where 5 is the id of the field using the key.
131 *
132 * Generally keys map to data types.
133 *
134 * @param string $key
135 *
136 * @return string
137 */
138 protected function getCustomFieldName($key) {
139 return 'custom_' . $this->getCustomFieldID($key);
140 }
141
142 /**
143 * Get the custom field name for the relevant key.
144 *
145 * e.g returns 'custom_5' where 5 is the id of the field using the key.
146 *
147 * Generally keys map to data types.
148 *
149 * @param string $key
150 *
151 * @return string
152 */
153 protected function getCustomFieldID($key) {
154 return $this->ids['CustomField'][$key];
155 }
156
157 /**
158 * Create a custom text fields.
159 *
160 * @param array $params
161 * Parameter overrides, must include custom_group_id.
162 *
163 * @return array
164 */
165 protected function createIntCustomField($params = []) {
166 $params = array_merge($this->getFieldsValuesByType('Int'), $params);
167 return $this->callAPISuccess('CustomField', 'create', $params)['values'][0];
168 }
169
170 /**
171 * Create a custom text fields.
172 *
173 * @param array $params
174 * Parameter overrides, must include custom_group_id.
175 *
176 * @return array
177 */
178 protected function createContactReferenceCustomField($params = []) {
179 $params = array_merge($this->getFieldsValuesByType('ContactReference'), $params);
180 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
181 }
182
183 /**
184 * Create a custom text fields.
185 *
186 * @param array $params
187 * Parameter overrides, must include custom_group_id.
188 *
189 * @return array
190 */
191 protected function createTextCustomField($params = []) {
192 $params = array_merge($this->getFieldsValuesByType('String'), $params);
193 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
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 createLinkCustomField($params = []) {
205 $params = array_merge($this->getFieldsValuesByType('Link'), $params);
206 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
207 }
208
209 /**
210 * Create a custom text fields.
211 *
212 * @param array $params
213 * Parameter overrides, must include custom_group_id.
214 *
215 * @return array
216 */
217 protected function createCountryCustomField($params = []) {
218 $params = array_merge($this->getFieldsValuesByType('Country'), $params);
219 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
220 }
221
222 /**
223 * Create a custom text fields.
224 *
225 * @param array $params
226 * Parameter overrides, must include custom_group_id.
227 *
228 * @return array
229 */
230 protected function createFileCustomField($params = []) {
231 $params = array_merge($this->getFieldsValuesByType('File'), $params);
232 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
233 }
234
235 /**
236 * Create custom select field.
237 *
238 * @param array $params
239 * Parameter overrides, must include custom_group_id.
240 *
241 * @return array
242 */
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];
246 }
247
248 /**
249 * Create a custom field of type date.
250 *
251 * @param array $params
252 *
253 * @return array
254 */
255 protected function createDateCustomField($params): array {
256 $params = array_merge($this->getFieldsValuesByType('Date'), $params);
257 return $this->callAPISuccess('custom_field', 'create', $params)['values'][0];
258 }
259
260 /**
261 * Get default field values for the type of field.
262 *
263 * @param $dataType
264 * @param string $htmlType
265 *
266 * @return mixed
267 */
268 public function getFieldsValuesByType($dataType, $htmlType = 'default') {
269 $values = $this->getAvailableFieldCombinations()[$dataType];
270 return array_merge([
271 'is_searchable' => 1,
272 'sequential' => 1,
273 'default_value' => '',
274 'is_required' => 0,
275 ], array_merge($values['default'], $values[$htmlType])
276 );
277 }
278
279 /**
280 * Get data available for custom fields.
281 *
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.
284 *
285 * The order below is consistent with the UI.
286 *
287 * @return array
288 */
289 protected function getAvailableFieldCombinations() {
290 return [
291 'String' => [
292 'default' => [
293 'label' => 'Enter text here',
294 'html_type' => 'Text',
295 'data_type' => 'String',
296 'default_value' => 'xyz',
297 'text_length' => 300,
298 ],
299 'Select' => [
300 'label' => 'Pick Color',
301 'html_type' => 'Select',
302 'data_type' => 'String',
303 'text_length' => '',
304 'default_value' => '',
305 'option_values' => [
306 [
307 'label' => 'Red',
308 'value' => 'R',
309 'weight' => 1,
310 'is_active' => 1,
311 ],
312 [
313 'label' => 'Yellow',
314 'value' => 'Y',
315 'weight' => 2,
316 'is_active' => 1,
317 ],
318 [
319 'label' => 'Green',
320 'value' => 'G',
321 'weight' => 3,
322 'is_active' => 1,
323 ],
324 ],
325 ],
326 ],
327 'Int' => [
328 'default' => [
329 'label' => 'Enter integer here',
330 'html_type' => 'Text',
331 'data_type' => 'Int',
332 'default_value' => '4',
333 'is_search_range' => 1,
334 ],
335 ],
336 'Date' => [
337 'default' => [
338 'name' => 'test_date',
339 'label' => 'Test Date',
340 'html_type' => 'Select Date',
341 'data_type' => 'Date',
342 'default_value' => '20090711',
343 'weight' => 3,
344 'is_search_range' => 1,
345 'time_format' => 1,
346 ],
347 ],
348 'Country' => [
349 'default' => [
350 'data_type' => 'Country',
351 'html_type' => 'Select Country',
352 'label' => 'Country',
353 'option_type' => 0,
354 ],
355 ],
356 'File' => [
357 'default' => [
358 'label' => 'My file',
359 'data_type' => 'File',
360 'html_type' => 'File',
361 ],
362 ],
363 'Link' => [
364 'default' => [
365 'name' => 'test_link',
366 'label' => 'test_link',
367 'html_type' => 'Link',
368 'data_type' => 'Link',
369 'default_value' => 'http://civicrm.org',
370 ],
371 ],
372 'ContactReference' => [
373 'default' => [
374 'label' => 'Contact reference field',
375 'html_type' => 'Autocomplete-Select',
376 'data_type' => 'ContactReference',
377 ],
378 ],
379 ];
380 }
381
382 }