Add test to lock in obscure custom join handling
[civicrm-core.git] / tests / phpunit / CRMTraits / Custom / CustomDataTrait.php
CommitLineData
8ba5884d 1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28/**
29 * Trait Custom Data trait.
30 *
31 * Trait for setting up custom data in tests.
32 */
33trait CRMTraits_Custom_CustomDataTrait {
34
119664d6 35 /**
36 * Create a custom group with fields of multiple types.
37 *
38 * @param array $groupParams
39 */
40 public function createCustomGroupWithFieldsOfAllTypes($groupParams = []) {
41 $this->createCustomGroup($groupParams);
42 $this->ids['CustomField'] = $this->createCustomFieldsOfAllTypes();
43 }
44
8ba5884d 45 /**
46 * Create a custom group.
47 *
48 * @param array $params
49 *
50 * @return int
51 */
52 public function createCustomGroup($params = []) {
53 $params = array_merge([
54 'title' => 'Custom Group',
0e1544e7 55 'extends' => [$this->entity ?? 'Contact'],
8ba5884d 56 'weight' => 5,
57 'style' => 'Inline',
58 'max_multiple' => 0,
59 ], $params);
0e1544e7 60 $identifier = $params['name'] ?? $params['title'];
61 $this->ids['CustomGroup'][$identifier] = $this->callAPISuccess('CustomGroup', 'create', $params)['id'];
62 return $this->ids['CustomGroup'][$identifier];
63 }
64
65 /**
66 * Get the table_name for the specified custom group.
67 *
68 * @param string $identifier
69 *
70 * @return string
71 */
72 public function getCustomGroupTable($identifier = 'Custom Group') {
73 return $this->callAPISuccessGetValue('CustomGroup', ['id' => $this->ids['CustomGroup'][$identifier], 'return' => 'table_name']);
74 }
75
76 /**
77 * Get the the column name for the identified custom field.
78 *
79 * @param string $key
80 * Identifier - generally keys map to data type - eg. 'text', 'int' etc.
81 *
82 * @return string
83 */
84 protected function getCustomFieldColumnName($key) {
85 return $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID($key), 'return' => 'column_name']);
8ba5884d 86 }
87
712ee28f 88 /**
89 * Create a custom group with a single field.
90 *
91 * @param array $groupParams
92 * @param string $customFieldType
93 *
697aad03 94 * @param string $identifier
95 *
712ee28f 96 * @throws \CRM_Core_Exception
97 */
0e1544e7 98 public function createCustomGroupWithFieldOfType($groupParams = [], $customFieldType = 'text', $identifier = NULL) {
99 $supported = ['text', 'select', 'date', 'int'];
100 if (!in_array($customFieldType, $supported, TRUE)) {
712ee28f 101 throw new CRM_Core_Exception('we have not yet extracted other custom field types from createCustomFieldsOfAllTypes, Use consistent syntax when you do');
102 }
697aad03 103 $groupParams['title'] = empty($groupParams['title']) ? $identifier . 'Group with field ' . $customFieldType : $groupParams['title'];
0e1544e7 104 $groupParams['name'] = $identifier ?? 'Custom Group';
712ee28f 105 $this->createCustomGroup($groupParams);
2b81c27e 106 switch ($customFieldType) {
107 case 'text':
0e1544e7 108 $customField = $this->createTextCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['name']]]);
2b81c27e 109 break;
110
111 case 'select':
0e1544e7 112 $customField = $this->createSelectCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['name']]]);
113 break;
114
115 case 'int':
116 $customField = $this->createIntCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['name']]]);
2b81c27e 117 break;
35aca6d6 118
119 case 'date':
0e1544e7 120 $customField = $this->createDateCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['name']]]);
35aca6d6 121 break;
2b81c27e 122 }
697aad03 123 $this->ids['CustomField'][$identifier . $customFieldType] = $customField['id'];
712ee28f 124 }
125
8ba5884d 126 /**
127 * @return array
128 */
129 public function createCustomFieldsOfAllTypes() {
130 $customGroupID = $this->ids['CustomGroup']['Custom Group'];
131 $ids = [];
712ee28f 132 $customField = $this->createTextCustomField(['custom_group_id' => $customGroupID]);
119664d6 133 $ids['text'] = $customField['id'];
8ba5884d 134
2b81c27e 135 $customField = $this->createSelectCustomField(['custom_group_id' => $customGroupID]);
119664d6 136 $ids['select_string'] = $customField['id'];
8ba5884d 137
35aca6d6 138 $customField = $this->createDateCustomField(['custom_group_id' => $customGroupID]);
119664d6 139 $ids['select_date'] = $customField['id'];
0e1544e7 140
141 $customField = $this->createIntCustomField(['custom_group_id' => $customGroupID]);
142 $ids['int'] = $customField['id'];
143
8ba5884d 144 $params = [
145 'custom_group_id' => $customGroupID,
146 'name' => 'test_link',
147 'label' => 'test_link',
148 'html_type' => 'Link',
149 'data_type' => 'Link',
150 'default_value' => 'http://civicrm.org',
151 'weight' => 4,
152 'is_required' => 1,
153 'is_searchable' => 0,
154 'is_active' => 1,
155 ];
156
157 $customField = $this->callAPISuccess('custom_field', 'create', $params);
119664d6 158
159 $ids['link'] = $customField['id'];
160 $fileField = $this->customFieldCreate([
161 'custom_group_id' => $customGroupID,
162 'data_type' => 'File',
163 'html_type' => 'File',
164 'default_value' => '',
165 ]);
166
167 $ids['file'] = $fileField['id'];
168 $ids['country'] = $this->customFieldCreate([
169 'custom_group_id' => $customGroupID,
d989f2a4 170 'data_type' => 'Country',
119664d6 171 'html_type' => 'Select Country',
172 'default_value' => '',
173 'label' => 'Country',
174 'option_type' => 0,
175 ])['id'];
176
8ba5884d 177 return $ids;
178 }
179
119664d6 180 /**
181 * Get the custom field name for the relevant key.
182 *
183 * e.g returns 'custom_5' where 5 is the id of the field using the key.
184 *
185 * Generally keys map to data types.
186 *
187 * @param string $key
188 *
189 * @return string
190 */
191 protected function getCustomFieldName($key) {
0e1544e7 192 return 'custom_' . $this->getCustomFieldID($key);
8392a043 193 }
194
195 /**
196 * Get the custom field name for the relevant key.
197 *
198 * e.g returns 'custom_5' where 5 is the id of the field using the key.
199 *
200 * Generally keys map to data types.
201 *
202 * @param string $key
203 *
204 * @return string
205 */
206 protected function getCustomFieldID($key) {
0e1544e7 207 return $this->ids['CustomField'][$key];
208 }
209
210 /**
211 * Create a custom text fields.
212 *
213 * @param array $params
214 * Parameter overrides, must include custom_group_id.
215 *
216 * @return array
217 */
218 protected function createIntCustomField($params = []) {
219 $params = array_merge([
220 'label' => 'Enter integer here',
221 'html_type' => 'Text',
222 'data_type' => 'Int',
223 'default_value' => '4',
224 'weight' => 1,
225 'is_required' => 1,
226 'sequential' => 1,
227 'is_searchable' => 1,
228 'is_search_range' => 1,
229 ], $params);
230
231 return $this->callAPISuccess('CustomField', 'create', $params)['values'][0];
119664d6 232 }
233
712ee28f 234 /**
235 * Create a custom text fields.
236 *
237 * @param array $params
238 * Parameter overrides, must include custom_group_id.
239 *
240 * @return array
241 */
242 protected function createTextCustomField($params = []) {
243 $params = array_merge([
244 'label' => 'Enter text here',
245 'html_type' => 'Text',
246 'data_type' => 'String',
247 'default_value' => 'xyz',
248 'weight' => 1,
249 'is_required' => 1,
250 'sequential' => 1,
d7a896b4 251 'is_searchable' => 1,
b7d6b1fa 252 'text_length' => 300,
712ee28f 253 ], $params);
254
255 return $this->callAPISuccess('CustomField', 'create', $params)['values'][0];
256 }
257
2b81c27e 258 /**
259 * Create custom select field.
260 *
261 * @param array $params
262 * Parameter overrides, must include custom_group_id.
263 *
264 * @return array
265 */
266 protected function createSelectCustomField(array $params): array {
267 $optionValue = [
268 [
269 'label' => 'Red',
270 'value' => 'R',
271 'weight' => 1,
272 'is_active' => 1,
273 ],
274 [
275 'label' => 'Yellow',
276 'value' => 'Y',
277 'weight' => 2,
278 'is_active' => 1,
279 ],
280 [
281 'label' => 'Green',
282 'value' => 'G',
283 'weight' => 3,
284 'is_active' => 1,
285 ],
286 ];
287
288 $params = array_merge([
289 'label' => 'Pick Color',
290 'html_type' => 'Select',
291 'data_type' => 'String',
292 'weight' => 2,
293 'is_required' => 1,
294 'is_searchable' => 0,
295 'is_active' => 1,
296 'option_values' => $optionValue,
297 ], $params);
298
299 $customField = $this->callAPISuccess('custom_field', 'create', $params);
300 return $customField['values'][$customField['id']];
301 }
302
35aca6d6 303 /**
304 * Create a custom field of type date.
305 *
306 * @param array $params
307 *
308 * @return array
309 */
310 protected function createDateCustomField($params): array {
311 $params = array_merge([
312 'name' => 'test_date',
313 'label' => 'test_date',
314 'html_type' => 'Select Date',
315 'data_type' => 'Date',
316 'default_value' => '20090711',
317 'weight' => 3,
0e1544e7 318 'is_searchable' => 1,
319 'is_search_range' => 1,
35aca6d6 320 'time_format' => 1,
321 ], $params);
322
323 $customField = $this->callAPISuccess('custom_field', 'create', $params);
324 return $customField['values'][$customField['id']];
325 }
326
8ba5884d 327}