Merge pull request #19921 from eileenmcnaughton/tear
[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 getEntityValues function for stored value.
184 */
185 public function testGetEntityValues() {
186
187 $params = [];
188 $contactID = $this->individualCreate();
189 $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
190 $fields = [
191 'custom_group_id' => $customGroup['id'],
192 'html_type' => 'RichTextEditor',
193 'data_type' => 'Memo',
194 ];
195
196 $customField = $this->customFieldCreate($fields);
197
198 $params[] = [
199 $customField['id'] => [
200 'value' => '<p><strong>This is a <u>test</u></p>',
201 'type' => 'Memo',
202 'custom_field_id' => $customField['id'],
203 'custom_group_id' => $customGroup['id'],
204 'table_name' => $customGroup['values'][$customGroup['id']]['table_name'],
205 'column_name' => $customField['values'][$customField['id']]['column_name'],
206 'file_id' => '',
207 ],
208 ];
209
210 CRM_Core_BAO_CustomValueTable::store($params, 'civicrm_contact', $contactID);
211
212 $entityValues = CRM_Core_BAO_CustomValueTable::getEntityValues($contactID, 'Individual');
213
214 $this->assertEquals($entityValues[$customField['id']], '<p><strong>This is a <u>test</u></p>',
215 'Checking same for returned value.'
216 );
217 $this->customFieldDelete($customField['id']);
218 $this->customGroupDelete($customGroup['id']);
219 $this->contactDelete($contactID);
220 }
221
222 public function testCustomGroupMultiple() {
223 $params = [];
224 $contactID = $this->individualCreate();
225 $customGroup = $this->customGroupCreate();
226
227 $fields = [
228 'custom_group_id' => $customGroup['id'],
229 'data_type' => 'String',
230 'html_type' => 'Text',
231 ];
232
233 $customField = $this->customFieldCreate($fields);
234
235 $params = [
236 'entityID' => $contactID,
237 'custom_' . $customField['id'] . '_-1' => 'First String',
238 ];
239 $error = CRM_Core_BAO_CustomValueTable::setValues($params);
240
241 $newParams = [
242 'entityID' => $contactID,
243 'custom_' . $customField['id'] => 1,
244 ];
245 $result = CRM_Core_BAO_CustomValueTable::getValues($newParams);
246
247 $this->assertEquals($params['custom_' . $customField['id'] . '_-1'], $result['custom_' . $customField['id']]);
248 $this->assertEquals($params['entityID'], $result['entityID']);
249
250 $this->customFieldDelete($customField['id']);
251 $this->customGroupDelete($customGroup['id']);
252 $this->contactDelete($contactID);
253 }
254
255 }