Merge pull request #14252 from jitendrapurohit/core-961
[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'];
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 $this->ids['CustomField'][$identifier . $customFieldType] = $customField['id'];
91 }
92
93 /**
94 * @return array
95 */
96 public function createCustomFieldsOfAllTypes() {
97 $customGroupID = $this->ids['CustomGroup']['Custom Group'];
98 $ids = [];
99 $customField = $this->createTextCustomField(['custom_group_id' => $customGroupID]);
100 $ids['text'] = $customField['id'];
101
102 $customField = $this->createSelectCustomField(['custom_group_id' => $customGroupID]);
103 $ids['select_string'] = $customField['id'];
104
105 $params = [
106 'custom_group_id' => $customGroupID,
107 'name' => 'test_date',
108 'label' => 'test_date',
109 'html_type' => 'Select Date',
110 'data_type' => 'Date',
111 'default_value' => '20090711',
112 'weight' => 3,
113 'time_format' => 1,
114 ];
115
116 $customField = $this->callAPISuccess('custom_field', 'create', $params);
117
118 $ids['select_date'] = $customField['id'];
119 $params = [
120 'custom_group_id' => $customGroupID,
121 'name' => 'test_link',
122 'label' => 'test_link',
123 'html_type' => 'Link',
124 'data_type' => 'Link',
125 'default_value' => 'http://civicrm.org',
126 'weight' => 4,
127 'is_required' => 1,
128 'is_searchable' => 0,
129 'is_active' => 1,
130 ];
131
132 $customField = $this->callAPISuccess('custom_field', 'create', $params);
133
134 $ids['link'] = $customField['id'];
135 $fileField = $this->customFieldCreate([
136 'custom_group_id' => $customGroupID,
137 'data_type' => 'File',
138 'html_type' => 'File',
139 'default_value' => '',
140 ]);
141
142 $ids['file'] = $fileField['id'];
143 $ids['country'] = $this->customFieldCreate([
144 'custom_group_id' => $customGroupID,
145 'data_type' => 'Int',
146 'html_type' => 'Select Country',
147 'default_value' => '',
148 'label' => 'Country',
149 'option_type' => 0,
150 ])['id'];
151
152 return $ids;
153 }
154
155 /**
156 * Get the custom field name for the relevant key.
157 *
158 * e.g returns 'custom_5' where 5 is the id of the field using the key.
159 *
160 * Generally keys map to data types.
161 *
162 * @param string $key
163 *
164 * @return string
165 */
166 protected function getCustomFieldName($key) {
167 $linkField = 'custom_' . $this->getCustomFieldID($key);
168 return $linkField;
169 }
170
171 /**
172 * Get the custom field name for the relevant key.
173 *
174 * e.g returns 'custom_5' where 5 is the id of the field using the key.
175 *
176 * Generally keys map to data types.
177 *
178 * @param string $key
179 *
180 * @return string
181 */
182 protected function getCustomFieldID($key) {
183 $linkField = $this->ids['CustomField'][$key];
184 return $linkField;
185 }
186
187 /**
188 * Create a custom text fields.
189 *
190 * @param array $params
191 * Parameter overrides, must include custom_group_id.
192 *
193 * @return array
194 */
195 protected function createTextCustomField($params = []) {
196 $params = array_merge([
197 'label' => 'Enter text here',
198 'html_type' => 'Text',
199 'data_type' => 'String',
200 'default_value' => 'xyz',
201 'weight' => 1,
202 'is_required' => 1,
203 'sequential' => 1,
204 'is_searchable' => 1,
205 ], $params);
206
207 return $this->callAPISuccess('CustomField', 'create', $params)['values'][0];
208 }
209
210 /**
211 * Create custom select field.
212 *
213 * @param array $params
214 * Parameter overrides, must include custom_group_id.
215 *
216 * @return array
217 */
218 protected function createSelectCustomField(array $params): array {
219 $optionValue = [
220 [
221 'label' => 'Red',
222 'value' => 'R',
223 'weight' => 1,
224 'is_active' => 1,
225 ],
226 [
227 'label' => 'Yellow',
228 'value' => 'Y',
229 'weight' => 2,
230 'is_active' => 1,
231 ],
232 [
233 'label' => 'Green',
234 'value' => 'G',
235 'weight' => 3,
236 'is_active' => 1,
237 ],
238 ];
239
240 $params = array_merge([
241 'label' => 'Pick Color',
242 'html_type' => 'Select',
243 'data_type' => 'String',
244 'weight' => 2,
245 'is_required' => 1,
246 'is_searchable' => 0,
247 'is_active' => 1,
248 'option_values' => $optionValue,
249 ], $params);
250
251 $customField = $this->callAPISuccess('custom_field', 'create', $params);
252 return $customField['values'][$customField['id']];
253 }
254
255 }