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'];
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']]]);
90 $this->ids
['CustomField'][$identifier . $customFieldType] = $customField['id'];
96 public function createCustomFieldsOfAllTypes() {
97 $customGroupID = $this->ids
['CustomGroup']['Custom Group'];
99 $customField = $this->createTextCustomField(['custom_group_id' => $customGroupID]);
100 $ids['text'] = $customField['id'];
102 $customField = $this->createSelectCustomField(['custom_group_id' => $customGroupID]);
103 $ids['select_string'] = $customField['id'];
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',
116 $customField = $this->callAPISuccess('custom_field', 'create', $params);
118 $ids['select_date'] = $customField['id'];
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',
128 'is_searchable' => 0,
132 $customField = $this->callAPISuccess('custom_field', 'create', $params);
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' => '',
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',
156 * Get the custom field name for the relevant key.
158 * e.g returns 'custom_5' where 5 is the id of the field using the key.
160 * Generally keys map to data types.
166 protected function getCustomFieldName($key) {
167 $linkField = 'custom_' . $this->getCustomFieldID($key);
172 * Get the custom field name for the relevant key.
174 * e.g returns 'custom_5' where 5 is the id of the field using the key.
176 * Generally keys map to data types.
182 protected function getCustomFieldID($key) {
183 $linkField = $this->ids
['CustomField'][$key];
188 * Create a custom text fields.
190 * @param array $params
191 * Parameter overrides, must include custom_group_id.
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',
204 'is_searchable' => 1,
207 return $this->callAPISuccess('CustomField', 'create', $params)['values'][0];
211 * Create custom select field.
213 * @param array $params
214 * Parameter overrides, must include custom_group_id.
218 protected function createSelectCustomField(array $params): array {
240 $params = array_merge([
241 'label' => 'Pick Color',
242 'html_type' => 'Select',
243 'data_type' => 'String',
246 'is_searchable' => 0,
248 'option_values' => $optionValue,
251 $customField = $this->callAPISuccess('custom_field', 'create', $params);
252 return $customField['values'][$customField['id']];