Merge pull request #21384 from mattwire/groupcontactcache
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / CustomValueTableTest.php
1 <?php
2
3 /**
4 * Class CRM_Core_BAO_CustomValueTableTest
5 * @group headless
6 */
7 class CRM_Core_BAO_CustomValueTableTest extends CiviUnitTestCase {
8
9 /**
10 * Test store function for country.
11 */
12 public function testStoreCountry() {
13 $params = [];
14 $contactID = $this->individualCreate();
15 $customGroup = $this->customGroupCreate();
16 $fields = [
17 'custom_group_id' => $customGroup['id'],
18 'data_type' => 'Country',
19 'html_type' => 'Select Country',
20 'default_value' => '',
21 ];
22
23 $customField = $this->customFieldCreate($fields);
24
25 $params[] = [
26 $customField['id'] => [
27 'value' => 1228,
28 'type' => 'Country',
29 'custom_field_id' => $customField['id'],
30 'custom_group_id' => $customGroup['id'],
31 'table_name' => $customGroup['values'][$customGroup['id']]['table_name'],
32 'column_name' => $customField['values'][$customField['id']]['column_name'],
33 'file_id' => '',
34 ],
35 ];
36
37 CRM_Core_BAO_CustomValueTable::store($params, 'civicrm_contact', $contactID);
38
39 $this->customFieldDelete($customField['id']);
40 $this->customGroupDelete($customGroup['id']);
41 $this->contactDelete($contactID);
42 }
43
44 /**
45 * Test store function for file.
46 */
47 public function testStoreFile() {
48 $contactID = $this->individualCreate();
49 $file = $this->callAPISuccess('File', 'create', ['uri' => 'dummy_data']);
50 $customGroup = $this->customGroupCreate();
51 $fields = [
52 'custom_group_id' => $customGroup['id'],
53 'data_type' => 'File',
54 'html_type' => 'File',
55 'default_value' => '',
56 ];
57
58 $customField = $this->customFieldCreate($fields);
59
60 $params[] = [
61 $customField['id'] => [
62 'value' => 'i/contact_house.png',
63 'type' => 'File',
64 'custom_field_id' => $customField['id'],
65 'custom_group_id' => $customGroup['id'],
66 'table_name' => $customGroup['values'][$customGroup['id']]['table_name'],
67 'column_name' => $customField['values'][$customField['id']]['column_name'],
68 'file_id' => $file['id'],
69 ],
70 ];
71
72 CRM_Core_BAO_CustomValueTable::store($params, 'civicrm_contact', $contactID);
73
74 $this->customFieldDelete($customField['id']);
75 $this->customGroupDelete($customGroup['id']);
76 $this->contactDelete($contactID);
77 }
78
79 /**
80 * Test store function for state province.
81 */
82 public function testStoreStateProvince() {
83 $contactID = $this->individualCreate();
84 $customGroup = $this->customGroupCreate();
85 $fields = [
86 'custom_group_id' => $customGroup['id'],
87 'data_type' => 'StateProvince',
88 'html_type' => 'Select',
89 'default_value' => '',
90 ];
91
92 $customField = $this->customFieldCreate($fields);
93
94 $params[] = [
95 $customField['id'] => [
96 'value' => 1029,
97 'type' => 'StateProvince',
98 'custom_field_id' => $customField['id'],
99 'custom_group_id' => $customGroup['id'],
100 'table_name' => $customGroup['values'][$customGroup['id']]['table_name'],
101 'column_name' => $customField['values'][$customField['id']]['column_name'],
102 'file_id' => 1,
103 ],
104 ];
105
106 CRM_Core_BAO_CustomValueTable::store($params, 'civicrm_contact', $contactID);
107
108 $this->customFieldDelete($customField['id']);
109 $this->customGroupDelete($customGroup['id']);
110 $this->contactDelete($contactID);
111 }
112
113 /**
114 * Test store function for date.
115 */
116 public function testStoreDate() {
117 $params = [];
118 $contactID = $this->individualCreate();
119 $customGroup = $this->customGroupCreate();
120 $fields = [
121 'custom_group_id' => $customGroup['id'],
122 'data_type' => 'Date',
123 'html_type' => 'Select Date',
124 'default_value' => '',
125 ];
126
127 $customField = $this->customFieldCreate($fields);
128
129 $params[] = [
130 $customField['id'] => [
131 'value' => '20080608000000',
132 'type' => 'Date',
133 'custom_field_id' => $customField['id'],
134 'custom_group_id' => $customGroup['id'],
135 'table_name' => $customGroup['values'][$customGroup['id']]['table_name'],
136 'column_name' => $customField['values'][$customField['id']]['column_name'],
137 'file_id' => '',
138 ],
139 ];
140
141 CRM_Core_BAO_CustomValueTable::store($params, 'civicrm_contact', $contactID);
142
143 $this->customFieldDelete($customField['id']);
144 $this->customGroupDelete($customGroup['id']);
145 $this->contactDelete($contactID);
146 }
147
148 /**
149 * Test store function for rich text editor.
150 */
151 public function testStoreRichTextEditor() {
152 $params = [];
153 $contactID = $this->individualCreate();
154 $customGroup = $this->customGroupCreate();
155 $fields = [
156 'custom_group_id' => $customGroup['id'],
157 'html_type' => 'RichTextEditor',
158 'data_type' => 'Memo',
159 ];
160
161 $customField = $this->customFieldCreate($fields);
162
163 $params[] = [
164 $customField['id'] => [
165 'value' => '<p><strong>This is a <u>test</u></p>',
166 'type' => 'Memo',
167 'custom_field_id' => $customField['id'],
168 'custom_group_id' => $customGroup['id'],
169 'table_name' => $customGroup['values'][$customGroup['id']]['table_name'],
170 'column_name' => $customField['values'][$customField['id']]['column_name'],
171 'file_id' => '',
172 ],
173 ];
174
175 CRM_Core_BAO_CustomValueTable::store($params, 'civicrm_contact', $contactID);
176
177 $this->customFieldDelete($customField['id']);
178 $this->customGroupDelete($customGroup['id']);
179 $this->contactDelete($contactID);
180 }
181
182 /**
183 * Test store function for multiselect int.
184 */
185 public function testStoreMultiSelectInt() {
186 $contactID = $this->individualCreate();
187 $customGroup = $this->customGroupCreate();
188 $fields = [
189 'custom_group_id' => $customGroup['id'],
190 'data_type' => 'Int',
191 'html_type' => 'Multi-Select',
192 'option_values' => [
193 1 => 'choice1',
194 2 => 'choice2',
195 ],
196 'default_value' => '',
197 ];
198
199 $customField = $this->customFieldCreate($fields);
200
201 $params = [
202 [
203 $customField['id'] => [
204 'value' => CRM_Core_DAO::VALUE_SEPARATOR . '1' . CRM_Core_DAO::VALUE_SEPARATOR . '2' . CRM_Core_DAO::VALUE_SEPARATOR,
205 'type' => 'Int',
206 'custom_field_id' => $customField['id'],
207 'custom_group_id' => $customGroup['id'],
208 'table_name' => $customGroup['values'][$customGroup['id']]['table_name'],
209 'column_name' => $customField['values'][$customField['id']]['column_name'],
210 'file_id' => '',
211 ],
212 ],
213 ];
214
215 CRM_Core_BAO_CustomValueTable::store($params, 'civicrm_contact', $contactID);
216
217 $customData = \Civi\Api4\Contact::get(FALSE)
218 ->addSelect('new_custom_group.Custom_Field')
219 ->addWhere('id', '=', $contactID)
220 ->execute()->first();
221 $this->assertEquals([1, 2], $customData['new_custom_group.Custom_Field']);
222
223 $this->customFieldDelete($customField['id']);
224 $this->customGroupDelete($customGroup['id']);
225 $this->contactDelete($contactID);
226 }
227
228 /**
229 * Test getEntityValues function for stored value.
230 */
231 public function testGetEntityValues() {
232
233 $params = [];
234 $contactID = $this->individualCreate();
235 $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
236 $fields = [
237 'custom_group_id' => $customGroup['id'],
238 'html_type' => 'RichTextEditor',
239 'data_type' => 'Memo',
240 ];
241
242 $customField = $this->customFieldCreate($fields);
243
244 $params[] = [
245 $customField['id'] => [
246 'value' => '<p><strong>This is a <u>test</u></p>',
247 'type' => 'Memo',
248 'custom_field_id' => $customField['id'],
249 'custom_group_id' => $customGroup['id'],
250 'table_name' => $customGroup['values'][$customGroup['id']]['table_name'],
251 'column_name' => $customField['values'][$customField['id']]['column_name'],
252 'file_id' => '',
253 ],
254 ];
255
256 CRM_Core_BAO_CustomValueTable::store($params, 'civicrm_contact', $contactID);
257
258 $entityValues = CRM_Core_BAO_CustomValueTable::getEntityValues($contactID, 'Individual');
259
260 $this->assertEquals($entityValues[$customField['id']], '<p><strong>This is a <u>test</u></p>',
261 'Checking same for returned value.'
262 );
263 $this->customFieldDelete($customField['id']);
264 $this->customGroupDelete($customGroup['id']);
265 $this->contactDelete($contactID);
266 }
267
268 public function testCustomGroupMultiple() {
269 $params = [];
270 $contactID = $this->individualCreate();
271 $customGroup = $this->customGroupCreate();
272
273 $fields = [
274 'custom_group_id' => $customGroup['id'],
275 'data_type' => 'String',
276 'html_type' => 'Text',
277 ];
278
279 $customField = $this->customFieldCreate($fields);
280
281 $params = [
282 'entityID' => $contactID,
283 'custom_' . $customField['id'] . '_-1' => 'First String',
284 ];
285 $error = CRM_Core_BAO_CustomValueTable::setValues($params);
286
287 $newParams = [
288 'entityID' => $contactID,
289 'custom_' . $customField['id'] => 1,
290 ];
291 $result = CRM_Core_BAO_CustomValueTable::getValues($newParams);
292
293 $this->assertEquals($params['custom_' . $customField['id'] . '_-1'], $result['custom_' . $customField['id']]);
294 $this->assertEquals($params['entityID'], $result['entityID']);
295
296 $this->customFieldDelete($customField['id']);
297 $this->customGroupDelete($customGroup['id']);
298 $this->contactDelete($contactID);
299 }
300
301 }