3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Trait Custom Data trait.
31 * Trait for setting up custom data in tests.
33 trait CRMTraits_Custom_CustomDataTrait
{
36 * Create a custom group with fields of multiple types.
38 * @param array $groupParams
40 public function createCustomGroupWithFieldsOfAllTypes($groupParams = []) {
41 $this->createCustomGroup($groupParams);
42 $this->ids
['CustomField'] = $this->createCustomFieldsOfAllTypes();
46 * Create a custom group.
48 * @param array $params
52 public function createCustomGroup($params = []) {
53 $params = array_merge([
54 'title' => 'Custom Group',
55 'extends' => [$this->entity
],
60 $this->ids
['CustomGroup'][$params['title']] = $this->callAPISuccess('CustomGroup', 'create', $params)['id'];
61 return $this->ids
['CustomGroup'][$params['title']];
65 * Create a custom group with a single field.
67 * @param array $groupParams
68 * @param string $customFieldType
70 * @param string $identifier
72 * @throws \CRM_Core_Exception
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');
79 $groupParams['title'] = empty($groupParams['title']) ?
$identifier . 'Group with field ' . $customFieldType : $groupParams['title'];
80 $this->createCustomGroup($groupParams);
81 switch ($customFieldType) {
83 $customField = $this->createTextCustomField(['custom_group_id' => $this->ids
['CustomGroup'][$groupParams['title']]]);
87 $customField = $this->createSelectCustomField(['custom_group_id' => $this->ids
['CustomGroup'][$groupParams['title']]]);
91 $customField = $this->createDateCustomField(['custom_group_id' => $this->ids
['CustomGroup'][$groupParams['title']]]);
94 $this->ids
['CustomField'][$identifier . $customFieldType] = $customField['id'];
100 public function createCustomFieldsOfAllTypes() {
101 $customGroupID = $this->ids
['CustomGroup']['Custom Group'];
103 $customField = $this->createTextCustomField(['custom_group_id' => $customGroupID]);
104 $ids['text'] = $customField['id'];
106 $customField = $this->createSelectCustomField(['custom_group_id' => $customGroupID]);
107 $ids['select_string'] = $customField['id'];
109 $customField = $this->createDateCustomField(['custom_group_id' => $customGroupID]);
111 $ids['select_date'] = $customField['id'];
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',
121 'is_searchable' => 0,
125 $customField = $this->callAPISuccess('custom_field', 'create', $params);
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' => '',
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',
149 * Get the custom field name for the relevant key.
151 * e.g returns 'custom_5' where 5 is the id of the field using the key.
153 * Generally keys map to data types.
159 protected function getCustomFieldName($key) {
160 $linkField = 'custom_' . $this->getCustomFieldID($key);
165 * Get the custom field name for the relevant key.
167 * e.g returns 'custom_5' where 5 is the id of the field using the key.
169 * Generally keys map to data types.
175 protected function getCustomFieldID($key) {
176 $linkField = $this->ids
['CustomField'][$key];
181 * Create a custom text fields.
183 * @param array $params
184 * Parameter overrides, must include custom_group_id.
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',
197 'is_searchable' => 1,
200 return $this->callAPISuccess('CustomField', 'create', $params)['values'][0];
204 * Create custom select field.
206 * @param array $params
207 * Parameter overrides, must include custom_group_id.
211 protected function createSelectCustomField(array $params): array {
233 $params = array_merge([
234 'label' => 'Pick Color',
235 'html_type' => 'Select',
236 'data_type' => 'String',
239 'is_searchable' => 0,
241 'option_values' => $optionValue,
244 $customField = $this->callAPISuccess('custom_field', 'create', $params);
245 return $customField['values'][$customField['id']];
249 * Create a custom field of type date.
251 * @param array $params
255 protected function createDateCustomField($params): array {
256 $params = array_merge([
257 'name' => 'test_date',
258 'label' => 'test_date',
259 'html_type' => 'Select Date',
260 'data_type' => 'Date',
261 'default_value' => '20090711',
266 $customField = $this->callAPISuccess('custom_field', 'create', $params);
267 return $customField['values'][$customField['id']];