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