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