Merge pull request #15322 from alifrumin/removePrintIcon
[civicrm-core.git] / tests / phpunit / CRMTraits / Custom / CustomDataTrait.php
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 */
33 trait CRMTraits_Custom_CustomDataTrait {
34
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
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',
55 'extends' => [$this->entity],
56 'weight' => 5,
57 'style' => 'Inline',
58 'max_multiple' => 0,
59 ], $params);
60 $this->ids['CustomGroup'][$params['title']] = $this->callAPISuccess('CustomGroup', 'create', $params)['id'];
61 return $this->ids['CustomGroup'][$params['title']];
62 }
63
64 /**
65 * Create a custom group with a single field.
66 *
67 * @param array $groupParams
68 * @param string $customFieldType
69 *
70 * @param string $identifier
71 *
72 * @throws \CRM_Core_Exception
73 */
74 public function createCustomGroupWithFieldOfType($groupParams = [], $customFieldType = 'text', $identifier = '') {
75 $supported = ['text', 'select', 'date'];
76 if (!in_array($customFieldType, $supported)) {
77 throw new CRM_Core_Exception('we have not yet extracted other custom field types from createCustomFieldsOfAllTypes, Use consistent syntax when you do');
78 }
79 $groupParams['title'] = empty($groupParams['title']) ? $identifier . 'Group with field ' . $customFieldType : $groupParams['title'];
80 $this->createCustomGroup($groupParams);
81 switch ($customFieldType) {
82 case 'text':
83 $customField = $this->createTextCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['title']]]);
84 break;
85
86 case 'select':
87 $customField = $this->createSelectCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['title']]]);
88 break;
89
90 case 'date':
91 $customField = $this->createDateCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['title']]]);
92 break;
93 }
94 $this->ids['CustomField'][$identifier . $customFieldType] = $customField['id'];
95 }
96
97 /**
98 * @return array
99 */
100 public function createCustomFieldsOfAllTypes() {
101 $customGroupID = $this->ids['CustomGroup']['Custom Group'];
102 $ids = [];
103 $customField = $this->createTextCustomField(['custom_group_id' => $customGroupID]);
104 $ids['text'] = $customField['id'];
105
106 $customField = $this->createSelectCustomField(['custom_group_id' => $customGroupID]);
107 $ids['select_string'] = $customField['id'];
108
109 $customField = $this->createDateCustomField(['custom_group_id' => $customGroupID]);
110
111 $ids['select_date'] = $customField['id'];
112 $params = [
113 'custom_group_id' => $customGroupID,
114 'name' => 'test_link',
115 'label' => 'test_link',
116 'html_type' => 'Link',
117 'data_type' => 'Link',
118 'default_value' => 'http://civicrm.org',
119 'weight' => 4,
120 'is_required' => 1,
121 'is_searchable' => 0,
122 'is_active' => 1,
123 ];
124
125 $customField = $this->callAPISuccess('custom_field', 'create', $params);
126
127 $ids['link'] = $customField['id'];
128 $fileField = $this->customFieldCreate([
129 'custom_group_id' => $customGroupID,
130 'data_type' => 'File',
131 'html_type' => 'File',
132 'default_value' => '',
133 ]);
134
135 $ids['file'] = $fileField['id'];
136 $ids['country'] = $this->customFieldCreate([
137 'custom_group_id' => $customGroupID,
138 'data_type' => 'Country',
139 'html_type' => 'Select Country',
140 'default_value' => '',
141 'label' => 'Country',
142 'option_type' => 0,
143 ])['id'];
144
145 return $ids;
146 }
147
148 /**
149 * Get the custom field name for the relevant key.
150 *
151 * e.g returns 'custom_5' where 5 is the id of the field using the key.
152 *
153 * Generally keys map to data types.
154 *
155 * @param string $key
156 *
157 * @return string
158 */
159 protected function getCustomFieldName($key) {
160 $linkField = 'custom_' . $this->getCustomFieldID($key);
161 return $linkField;
162 }
163
164 /**
165 * Get the custom field name for the relevant key.
166 *
167 * e.g returns 'custom_5' where 5 is the id of the field using the key.
168 *
169 * Generally keys map to data types.
170 *
171 * @param string $key
172 *
173 * @return string
174 */
175 protected function getCustomFieldID($key) {
176 $linkField = $this->ids['CustomField'][$key];
177 return $linkField;
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 createTextCustomField($params = []) {
189 $params = array_merge([
190 'label' => 'Enter text here',
191 'html_type' => 'Text',
192 'data_type' => 'String',
193 'default_value' => 'xyz',
194 'weight' => 1,
195 'is_required' => 1,
196 'sequential' => 1,
197 'is_searchable' => 1,
198 'text_length' => 300,
199 ], $params);
200
201 return $this->callAPISuccess('CustomField', 'create', $params)['values'][0];
202 }
203
204 /**
205 * Create custom select field.
206 *
207 * @param array $params
208 * Parameter overrides, must include custom_group_id.
209 *
210 * @return array
211 */
212 protected function createSelectCustomField(array $params): array {
213 $optionValue = [
214 [
215 'label' => 'Red',
216 'value' => 'R',
217 'weight' => 1,
218 'is_active' => 1,
219 ],
220 [
221 'label' => 'Yellow',
222 'value' => 'Y',
223 'weight' => 2,
224 'is_active' => 1,
225 ],
226 [
227 'label' => 'Green',
228 'value' => 'G',
229 'weight' => 3,
230 'is_active' => 1,
231 ],
232 ];
233
234 $params = array_merge([
235 'label' => 'Pick Color',
236 'html_type' => 'Select',
237 'data_type' => 'String',
238 'weight' => 2,
239 'is_required' => 1,
240 'is_searchable' => 0,
241 'is_active' => 1,
242 'option_values' => $optionValue,
243 ], $params);
244
245 $customField = $this->callAPISuccess('custom_field', 'create', $params);
246 return $customField['values'][$customField['id']];
247 }
248
249 /**
250 * Create a custom field of type date.
251 *
252 * @param array $params
253 *
254 * @return array
255 */
256 protected function createDateCustomField($params): array {
257 $params = array_merge([
258 'name' => 'test_date',
259 'label' => 'test_date',
260 'html_type' => 'Select Date',
261 'data_type' => 'Date',
262 'default_value' => '20090711',
263 'weight' => 3,
264 'time_format' => 1,
265 ], $params);
266
267 $customField = $this->callAPISuccess('custom_field', 'create', $params);
268 return $customField['values'][$customField['id']];
269 }
270
271 }