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