Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
b6708aeb | 2 | /* |
3 | +--------------------------------------------------------------------+ | |
7d61e75f TO |
4 | | Copyright CiviCRM LLC. All rights reserved. | |
5 | | | | |
6 | | This work is published under the GNU AGPLv3 license with some | | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
9 | +--------------------------------------------------------------------+ | |
e70a7fc0 | 10 | */ |
6a488035 | 11 | |
6a488035 TO |
12 | /** |
13 | * Test APIv3 civicrm_create_custom_group | |
14 | * | |
6c6e6187 | 15 | * @package CiviCRM |
acb109b7 | 16 | * @group headless |
6a488035 TO |
17 | */ |
18 | class api_v3_CustomFieldTest extends CiviUnitTestCase { | |
dba70c02 | 19 | |
aa4143bb | 20 | /** |
21 | * Clean up after test. | |
22 | * | |
23 | * @throws \CRM_Core_Exception | |
24 | */ | |
00be9182 | 25 | public function tearDown() { |
aa4143bb | 26 | $this->quickCleanup([ |
b3c30fda CW |
27 | 'civicrm_contact', |
28 | 'civicrm_file', | |
29 | 'civicrm_entity_file', | |
aa4143bb | 30 | ], TRUE); |
31 | parent::tearDown(); | |
6a488035 TO |
32 | } |
33 | ||
6a488035 | 34 | /** |
eceb18cc | 35 | * Check with no label. |
6a488035 | 36 | */ |
00be9182 | 37 | public function testCustomFieldCreateWithoutLabel() { |
dba70c02 | 38 | $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'text_test_group']); |
39 | $params = [ | |
6a488035 TO |
40 | 'custom_group_id' => $customGroup['id'], |
41 | 'name' => 'test_textfield2', | |
42 | 'html_type' => 'Text', | |
43 | 'data_type' => 'String', | |
44 | 'default_value' => 'abc', | |
45 | 'weight' => 4, | |
46 | 'is_required' => 1, | |
47 | 'is_searchable' => 0, | |
48 | 'is_active' => 1, | |
dba70c02 | 49 | ]; |
6a488035 | 50 | |
d0e1eff2 | 51 | $customField = $this->callAPIFailure('custom_field', 'create', $params); |
6a488035 TO |
52 | $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: label'); |
53 | } | |
54 | ||
55 | /** | |
eceb18cc | 56 | * Check with edit. |
6a488035 | 57 | */ |
00be9182 | 58 | public function testCustomFieldCreateWithEdit() { |
dba70c02 | 59 | $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'text_test_group']); |
60 | $params = [ | |
6a488035 TO |
61 | 'custom_group_id' => $customGroup['id'], |
62 | 'name' => 'test_textfield2', | |
63 | 'label' => 'Name1', | |
64 | 'html_type' => 'Text', | |
65 | 'data_type' => 'String', | |
66 | 'default_value' => 'abc', | |
67 | 'weight' => 4, | |
68 | 'is_required' => 1, | |
69 | 'is_searchable' => 0, | |
70 | 'is_active' => 1, | |
dba70c02 | 71 | ]; |
6a488035 | 72 | |
92915c55 | 73 | $customField = $this->callAPIAndDocument('custom_field', 'create', $params, __FUNCTION__, __FILE__); |
6a488035 | 74 | $params['id'] = $customField['id']; |
92915c55 | 75 | $customField = $this->callAPISuccess('custom_field', 'create', $params); |
6a488035 | 76 | |
ba4a1892 | 77 | $this->assertNotNull($customField['id']); |
6a488035 TO |
78 | } |
79 | ||
80 | /** | |
eceb18cc | 81 | * Check without groupId. |
6a488035 | 82 | */ |
00be9182 | 83 | public function testCustomFieldCreateWithoutGroupID() { |
dba70c02 | 84 | $fieldParams = [ |
6a488035 TO |
85 | 'name' => 'test_textfield1', |
86 | 'label' => 'Name', | |
87 | 'html_type' => 'Text', | |
88 | 'data_type' => 'String', | |
89 | 'default_value' => 'abc', | |
90 | 'weight' => 4, | |
91 | 'is_required' => 1, | |
92 | 'is_searchable' => 0, | |
93 | 'is_active' => 1, | |
e830c6ae | 94 | |
dba70c02 | 95 | ]; |
6a488035 | 96 | |
d0e1eff2 | 97 | $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams); |
6a488035 TO |
98 | $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: custom_group_id'); |
99 | } | |
100 | ||
101 | /** | |
102 | * Check for Each data type: loop through available form input types | |
ae5ffbb7 | 103 | */ |
00be9182 | 104 | public function testCustomFieldCreateAllAvailableFormInputs() { |
dba70c02 | 105 | $gid = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'testAllFormInputs']); |
6a488035 TO |
106 | |
107 | $dtype = CRM_Core_BAO_CustomField::dataType(); | |
5d2f1ed6 | 108 | $htype = CRM_Custom_Form_Field::$_dataToHTML; |
6a488035 | 109 | |
a7bcb32d CW |
110 | // Legacy html types returned by v3 |
111 | foreach ($htype as &$item) { | |
112 | if (isset($item['StateProvince'])) { | |
113 | $item['StateProvince'] = 'Select State/Province'; | |
114 | } | |
115 | if (isset($item['Country'])) { | |
116 | $item['Country'] = 'Select Country'; | |
117 | } | |
118 | } | |
119 | ||
6a488035 TO |
120 | $n = 0; |
121 | foreach ($dtype as $dkey => $dvalue) { | |
122 | foreach ($htype[$n] as $hkey => $hvalue) { | |
123 | //echo $dkey."][".$hvalue."\n"; | |
124 | $this->_loopingCustomFieldCreateTest($this->_buildParams($gid['id'], $hvalue, $dkey)); | |
125 | } | |
126 | $n++; | |
127 | } | |
128 | } | |
39b959db | 129 | |
6c6e6187 | 130 | /* |
e70a7fc0 TO |
131 | * Can't figure out the point of this? |
132 | */ | |
39b959db | 133 | |
4cbe18b8 | 134 | /** |
c490a46a | 135 | * @param array $params |
4cbe18b8 | 136 | */ |
00be9182 | 137 | public function _loopingCustomFieldCreateTest($params) { |
e830c6ae | 138 | $customField = $this->callAPISuccess('custom_field', 'create', $params); |
6a488035 TO |
139 | $this->assertNotNull($customField['id']); |
140 | $this->getAndCheck($params, $customField['id'], 'CustomField'); | |
141 | } | |
142 | ||
4cbe18b8 | 143 | /** |
100fef9d | 144 | * @param int $gid |
4cbe18b8 EM |
145 | * @param $htype |
146 | * @param $dtype | |
147 | * | |
148 | * @return array | |
149 | */ | |
00be9182 | 150 | public function _buildParams($gid, $htype, $dtype) { |
6a488035 TO |
151 | $params = $this->_buildBasicParams($gid, $htype, $dtype); |
152 | /* //Not Working for any type. Maybe redundant with testCustomFieldCreateWithOptionValues() | |
e70a7fc0 TO |
153 | if ($htype == 'Multi-Select') |
154 | $params = array_merge($params, array( | |
155 | 'option_label' => array( 'Label1','Label2'), | |
156 | 'option_value' => array( 'val1', 'val2' ), | |
157 | 'option_weight' => array( 1, 2), | |
158 | 'option_status' => array( 1, 1), | |
159 | )); | |
160 | */ | |
6a488035 TO |
161 | |
162 | return $params; | |
163 | } | |
164 | ||
4cbe18b8 | 165 | /** |
100fef9d | 166 | * @param int $gid |
4cbe18b8 EM |
167 | * @param $htype |
168 | * @param $dtype | |
169 | * | |
170 | * @return array | |
171 | */ | |
00be9182 | 172 | public function _buildBasicParams($gid, $htype, $dtype) { |
dba70c02 | 173 | return [ |
6a488035 TO |
174 | 'custom_group_id' => $gid, |
175 | 'label' => $dtype . $htype, | |
176 | 'html_type' => $htype, | |
177 | 'data_type' => $dtype, | |
178 | 'weight' => 4, | |
179 | 'is_required' => 0, | |
180 | 'is_searchable' => 0, | |
181 | 'is_active' => 1, | |
e830c6ae | 182 | |
dba70c02 | 183 | ]; |
6a488035 TO |
184 | } |
185 | ||
186 | /** | |
eceb18cc | 187 | * Test using example code. |
6a488035 TO |
188 | */ |
189 | /*function testCustomFieldCreateExample( ) | |
e70a7fc0 | 190 | { |
6a488035 | 191 | |
e70a7fc0 | 192 | $customGroup = $this->customGroupCreate('Individual','date_test_group',3); |
be44cfcb | 193 | require_once 'api/v3/examples/CustomField/Create.ex.php'; |
e70a7fc0 TO |
194 | $result = custom_field_create_example(); |
195 | $expectedResult = custom_field_create_expectedresult(); | |
196 | $this->assertEquals($result,$expectedResult); | |
197 | }*/ | |
6a488035 TO |
198 | |
199 | /** | |
100fef9d | 200 | * Check with data type - Options with option_values |
6a488035 | 201 | */ |
00be9182 | 202 | public function testCustomFieldCreateWithEmptyOptionGroup() { |
dba70c02 | 203 | $customGroup = $this->customGroupCreate(['extends' => 'Contact', 'title' => 'select_test_group']); |
204 | $params = [ | |
b958933f | 205 | 'custom_group_id' => $customGroup['id'], |
206 | 'label' => 'Country', | |
207 | 'html_type' => 'Select', | |
208 | 'data_type' => 'String', | |
209 | 'weight' => 4, | |
210 | 'is_required' => 1, | |
211 | 'is_searchable' => 0, | |
212 | 'is_active' => 1, | |
dba70c02 | 213 | ]; |
b958933f | 214 | |
e830c6ae | 215 | $customField = $this->callAPISuccess('custom_field', 'create', $params); |
b958933f | 216 | $this->assertNotNull($customField['id']); |
dba70c02 | 217 | $optionGroupID = $this->callAPISuccess('custom_field', 'getvalue', [ |
b958933f | 218 | 'id' => $customField['id'], |
219 | 'return' => 'option_group_id', | |
dba70c02 | 220 | ]); |
b958933f | 221 | |
222 | $this->assertTrue(is_numeric($optionGroupID) && ($optionGroupID > 0)); | |
dba70c02 | 223 | $optionGroup = $this->callAPISuccess('option_group', 'getsingle', [ |
ae5ffbb7 | 224 | 'id' => $optionGroupID, |
dba70c02 | 225 | ]); |
6c6e6187 | 226 | $this->assertEquals($optionGroup['title'], 'Country'); |
dba70c02 | 227 | $optionValueCount = $this->callAPISuccess('option_value', 'getcount', [ |
ae5ffbb7 | 228 | 'option_group_id' => $optionGroupID, |
dba70c02 | 229 | ]); |
b958933f | 230 | $this->assertEquals(0, $optionValueCount); |
231 | } | |
232 | ||
65c2fd15 JP |
233 | /** |
234 | * Check with non-ascii labels | |
235 | */ | |
236 | public function testCustomFieldCreateWithNonAsciiLabel() { | |
dba70c02 | 237 | $customGroup = $this->customGroupCreate(['extends' => 'Contact', 'title' => 'select_test_group']); |
238 | $params = [ | |
65c2fd15 JP |
239 | 'custom_group_id' => $customGroup['id'], |
240 | 'label' => 'ôôôô', | |
241 | 'html_type' => 'Select', | |
242 | 'data_type' => 'String', | |
243 | 'weight' => 4, | |
244 | 'is_required' => 1, | |
245 | 'is_searchable' => 0, | |
246 | 'is_active' => 1, | |
dba70c02 | 247 | ]; |
65c2fd15 JP |
248 | $customField = $this->callAPISuccess('custom_field', 'create', $params); |
249 | $this->assertNotNull($customField['id']); | |
250 | $params['label'] = 'Ã Ã Ã '; | |
251 | $customField = $this->callAPISuccess('custom_field', 'create', $params); | |
252 | $this->assertNotNull($customField['id']); | |
253 | } | |
254 | ||
eebb53df | 255 | /** |
eceb18cc | 256 | * Test custom field with existing option group. |
eebb53df | 257 | */ |
00be9182 | 258 | public function testCustomFieldExistingOptionGroup() { |
dba70c02 | 259 | $customGroup = $this->customGroupCreate(['extends' => 'Organization', 'title' => 'test_group']); |
260 | $params = [ | |
eebb53df JV |
261 | 'custom_group_id' => $customGroup['id'], |
262 | // Just to say something: | |
263 | 'label' => 'Organization Gender', | |
264 | 'html_type' => 'Select', | |
265 | 'data_type' => 'Int', | |
266 | 'weight' => 4, | |
267 | 'is_required' => 1, | |
268 | 'is_searchable' => 0, | |
269 | 'is_active' => 1, | |
270 | // Option group id 3: gender | |
271 | 'option_group_id' => 3, | |
dba70c02 | 272 | ]; |
eebb53df JV |
273 | |
274 | $customField = $this->callAPISuccess('custom_field', 'create', $params); | |
275 | $this->assertNotNull($customField['id']); | |
dba70c02 | 276 | $optionGroupID = $this->callAPISuccess('custom_field', 'getvalue', [ |
eebb53df JV |
277 | 'id' => $customField['id'], |
278 | 'return' => 'option_group_id', | |
dba70c02 | 279 | ]); |
eebb53df | 280 | |
6c6e6187 | 281 | $this->assertEquals($optionGroupID, 3); |
eebb53df JV |
282 | } |
283 | ||
aa4143bb | 284 | /** |
285 | * Test adding an optionGroup to an existing field doesn't cause a fatal error. | |
286 | * | |
287 | * (this was happening due to a check running despite no existing option_group_id) | |
288 | * | |
289 | * @throws \CiviCRM_API3_Exception | |
290 | */ | |
291 | public function testUpdateCustomFieldAddOptionGroup() { | |
292 | $customGroup = $this->customGroupCreate(['extends' => 'Organization', 'title' => 'test_group']); | |
293 | $params = [ | |
294 | 'custom_group_id' => $customGroup['id'], | |
295 | 'label' => 'Organization Gender', | |
296 | 'html_type' => 'Text', | |
297 | 'data_type' => 'Int', | |
298 | ]; | |
299 | ||
300 | $customField = $this->callAPISuccess('custom_field', 'create', $params); | |
301 | $this->callAPISuccess('CustomField', 'create', [ | |
302 | 'option_group_id' => civicrm_api3('OptionGroup', 'getvalue', ['options' => ['limit' => 1], 'return' => 'id']), | |
303 | 'id' => $customField['id'], | |
304 | 'html_type' => 'Select', | |
305 | ]); | |
306 | } | |
307 | ||
3c70d501 | 308 | /** |
309 | * Test custom field get works & return param works | |
310 | */ | |
9b873358 | 311 | public function testCustomFieldGetReturnOptions() { |
dba70c02 | 312 | $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'test_group']); |
313 | $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]); | |
3c70d501 | 314 | |
dba70c02 | 315 | $result = $this->callAPISuccess('custom_field', 'getsingle', [ |
3c70d501 | 316 | 'id' => $customField['id'], |
317 | 'return' => 'data_type', | |
dba70c02 | 318 | ]); |
3c70d501 | 319 | $this->assertTrue(array_key_exists('data_type', $result)); |
320 | $this->assertFalse(array_key_exists('custom_group_id', $result)); | |
321 | } | |
322 | ||
323 | /** | |
324 | * Test custom field get works & return param works | |
325 | */ | |
9b873358 | 326 | public function testCustomFieldGetReturnArray() { |
dba70c02 | 327 | $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'test_group']); |
328 | $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]); | |
3c70d501 | 329 | |
dba70c02 | 330 | $result = $this->callAPISuccess('custom_field', 'getsingle', [ |
92915c55 | 331 | 'id' => $customField['id'], |
dba70c02 | 332 | 'return' => ['data_type'], |
333 | ]); | |
3c70d501 | 334 | $this->assertTrue(array_key_exists('data_type', $result)); |
335 | $this->assertFalse(array_key_exists('custom_group_id', $result)); | |
336 | } | |
337 | ||
338 | /** | |
339 | * Test custom field get works & return param works | |
340 | */ | |
9b873358 | 341 | public function testCustomFieldGetReturnTwoOptions() { |
dba70c02 | 342 | $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'test_group']); |
343 | $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]); | |
3c70d501 | 344 | |
dba70c02 | 345 | $result = $this->callAPISuccess('custom_field', 'getsingle', [ |
92915c55 | 346 | 'id' => $customField['id'], |
3c70d501 | 347 | 'return' => 'data_type, custom_group_id', |
dba70c02 | 348 | ]); |
3c70d501 | 349 | $this->assertTrue(array_key_exists('data_type', $result)); |
350 | $this->assertTrue(array_key_exists('custom_group_id', $result)); | |
351 | $this->assertFalse(array_key_exists('label', $result)); | |
352 | } | |
353 | ||
00be9182 | 354 | public function testCustomFieldCreateWithOptionValues() { |
dba70c02 | 355 | $customGroup = $this->customGroupCreate(['extends' => 'Contact', 'title' => 'select_test_group']); |
6a488035 | 356 | |
dba70c02 | 357 | $option_values = [ |
358 | [ | |
92915c55 | 359 | 'weight' => 1, |
6a488035 TO |
360 | 'label' => 'Label1', |
361 | 'value' => 1, | |
362 | 'is_active' => 1, | |
dba70c02 | 363 | ], |
364 | [ | |
6a488035 TO |
365 | 'weight' => 2, |
366 | 'label' => 'Label2', | |
367 | 'value' => 2, | |
368 | 'is_active' => 1, | |
dba70c02 | 369 | ], |
370 | ]; | |
6a488035 | 371 | |
dba70c02 | 372 | $params = [ |
6a488035 | 373 | 'custom_group_id' => $customGroup['id'], |
7f6d3dd3 | 374 | 'label' => 'Our special field', |
6a488035 TO |
375 | 'html_type' => 'Select', |
376 | 'data_type' => 'String', | |
377 | 'weight' => 4, | |
378 | 'is_required' => 1, | |
379 | 'is_searchable' => 0, | |
380 | 'is_active' => 1, | |
381 | 'option_values' => $option_values, | |
e830c6ae | 382 | |
dba70c02 | 383 | ]; |
6a488035 | 384 | |
e830c6ae | 385 | $customField = $this->callAPISuccess('custom_field', 'create', $params); |
6a488035 | 386 | |
b958933f | 387 | $this->assertAPISuccess($customField); |
6a488035 | 388 | $this->assertNotNull($customField['id']); |
dba70c02 | 389 | $getFieldsParams = [ |
390 | 'options' => ['get_options' => 'custom_' . $customField['id']], | |
92915c55 | 391 | 'action' => 'create', |
dba70c02 | 392 | ]; |
5c49fee0 | 393 | $description = "Demonstrates retrieving metadata with custom field options."; |
6a488035 | 394 | $subfile = "GetFieldsOptions"; |
a828d7b8 | 395 | $fields = $this->callAPIAndDocument('contact', 'getfields', $getFieldsParams, __FUNCTION__, 'ContactTest.php', $description, $subfile); |
6a488035 TO |
396 | $this->assertArrayHasKey('options', $fields['values']['custom_' . $customField['id']]); |
397 | $this->assertEquals('Label1', $fields['values']['custom_' . $customField['id']]['options'][1]); | |
dba70c02 | 398 | $getOptionsArray = [ |
6a488035 | 399 | 'field' => 'custom_' . $customField['id'], |
dba70c02 | 400 | ]; |
5c49fee0 | 401 | $description = "Demonstrates retrieving options for a custom field."; |
6a488035 | 402 | $subfile = "GetOptions"; |
a828d7b8 | 403 | $result = $this->callAPIAndDocument('contact', 'getoptions', $getOptionsArray, __FUNCTION__, 'ContactTest.php', $description, ''); |
6a488035 | 404 | $this->assertEquals('Label1', $result['values'][1]); |
6a488035 TO |
405 | } |
406 | ||
407 | ///////////////// civicrm_custom_field_delete methods | |
408 | ||
6a488035 | 409 | /** |
eceb18cc | 410 | * Check without Field ID. |
6a488035 | 411 | */ |
00be9182 | 412 | public function testCustomFieldDeleteWithoutFieldID() { |
dba70c02 | 413 | $params = []; |
e830c6ae | 414 | $customField = $this->callAPIFailure('custom_field', 'delete', $params, |
415 | 'Mandatory key(s) missing from params array: id'); | |
6a488035 TO |
416 | } |
417 | ||
418 | /** | |
eceb18cc | 419 | * Check without valid array. |
6a488035 | 420 | */ |
00be9182 | 421 | public function testCustomFieldDelete() { |
dba70c02 | 422 | $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'test_group']); |
423 | $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]); | |
ba4a1892 | 424 | $this->assertNotNull($customField['id']); |
6a488035 | 425 | |
dba70c02 | 426 | $params = [ |
6a488035 | 427 | 'id' => $customField['id'], |
dba70c02 | 428 | ]; |
e830c6ae | 429 | $result = $this->callAPIAndDocument('custom_field', 'delete', $params, __FUNCTION__, __FILE__); |
6a488035 | 430 | |
a15773db | 431 | $this->assertAPISuccess($result); |
6a488035 TO |
432 | } |
433 | ||
434 | /** | |
eceb18cc | 435 | * Check for Option Value. |
6a488035 | 436 | */ |
00be9182 | 437 | public function testCustomFieldOptionValueDelete() { |
dba70c02 | 438 | $customGroup = $this->customGroupCreate(['extends' => 'Contact', 'title' => 'ABC']); |
6a488035 | 439 | $customOptionValueFields = $this->customFieldOptionValueCreate($customGroup, 'fieldABC'); |
dba70c02 | 440 | $params = [ |
6a488035 | 441 | 'id' => $customOptionValueFields, |
dba70c02 | 442 | ]; |
6a488035 | 443 | |
e830c6ae | 444 | $customField = $this->callAPISuccess('custom_field', 'delete', $customOptionValueFields); |
6a488035 | 445 | } |
6b59896e TO |
446 | |
447 | /** | |
448 | * If there's one custom group for "Contact" and one for "Activity", then "Contact.getfields" | |
449 | * and "Activity.getfields" should return only their respective fields (not the other's fields), | |
450 | * and unrelated entities should return no custom fields. | |
451 | */ | |
00be9182 | 452 | public function testGetfields_CrossEntityPollution() { |
dba70c02 | 453 | $auxEntities = ['Email', 'Address', 'LocBlock', 'Membership', 'ContributionPage', 'ReportInstance']; |
454 | $allEntities = array_merge(['Contact', 'Activity'], $auxEntities); | |
6b59896e TO |
455 | |
456 | // Baseline - getfields doesn't reporting any customfields for any entities | |
457 | foreach ($allEntities as $entity) { | |
458 | $this->assertEquals( | |
dba70c02 | 459 | [], |
460 | $this->getCustomFieldKeys($this->callAPISuccess($entity, 'getfields', [])), | |
6b59896e TO |
461 | "Baseline custom fields for $entity should be empty" |
462 | ); | |
463 | } | |
464 | ||
465 | // Add some fields | |
dba70c02 | 466 | $contactGroup = $this->customGroupCreate(['extends' => 'Contact', 'title' => 'test_group_c']); |
467 | $contactField = $this->customFieldCreate([ | |
39b959db SL |
468 | 'custom_group_id' => $contactGroup['id'], |
469 | 'label' => 'For Contacts', | |
dba70c02 | 470 | ]); |
471 | $indivGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'test_group_i']); | |
472 | $indivField = $this->customFieldCreate(['custom_group_id' => $indivGroup['id'], 'label' => 'For Individuals']); | |
473 | $activityGroup = $this->customGroupCreate(['extends' => 'Activity', 'title' => 'test_group_a']); | |
474 | $activityField = $this->customFieldCreate([ | |
39b959db SL |
475 | 'custom_group_id' => $activityGroup['id'], |
476 | 'label' => 'For Activities', | |
dba70c02 | 477 | ]); |
6b59896e | 478 | |
f8a3cd29 TO |
479 | // Check getfields |
480 | $this->assertEquals( | |
dba70c02 | 481 | ['custom_' . $contactField['id'], 'custom_' . $indivField['id']], |
482 | $this->getCustomFieldKeys($this->callAPISuccess('Contact', 'getfields', [])), | |
f8a3cd29 TO |
483 | 'Contact custom fields' |
484 | ); | |
485 | $this->assertEquals( | |
dba70c02 | 486 | ['custom_' . $contactField['id'], 'custom_' . $indivField['id']], |
487 | $this->getCustomFieldKeys($this->callAPISuccess('Individual', 'getfields', [])), | |
f8a3cd29 TO |
488 | 'Individual custom fields' |
489 | ); | |
490 | $this->assertEquals( | |
dba70c02 | 491 | ['custom_' . $contactField['id']], |
492 | $this->getCustomFieldKeys($this->callAPISuccess('Organization', 'getfields', [])), | |
f8a3cd29 TO |
493 | 'Organization custom fields' |
494 | ); | |
6b59896e | 495 | $this->assertEquals( |
dba70c02 | 496 | ['custom_' . $activityField['id']], |
497 | $this->getCustomFieldKeys($this->callAPISuccess('Activity', 'getfields', [])), | |
6b59896e TO |
498 | 'Activity custom fields' |
499 | ); | |
500 | foreach ($auxEntities as $entity) { | |
501 | $this->assertEquals( | |
dba70c02 | 502 | [], |
503 | $this->getCustomFieldKeys($this->callAPISuccess($entity, 'getfields', [])), | |
6b59896e TO |
504 | "Custom fields for $entity should be empty" |
505 | ); | |
506 | } | |
507 | } | |
508 | ||
b3c30fda CW |
509 | /** |
510 | * Test setting and getting a custom file field value. | |
511 | * | |
512 | * Uses the "attachment" api for setting value. | |
513 | */ | |
514 | public function testCustomFileField() { | |
dba70c02 | 515 | $customGroup = $this->customGroupCreate(['title' => 'attachment_test_group']); |
516 | $params = [ | |
b3c30fda CW |
517 | 'custom_group_id' => $customGroup['id'], |
518 | 'name' => 'test_file_attachment', | |
519 | 'label' => 'test_file_attachment', | |
520 | 'html_type' => 'File', | |
521 | 'data_type' => 'File', | |
522 | 'is_active' => 1, | |
dba70c02 | 523 | ]; |
b3c30fda CW |
524 | $customField = $this->callAPISuccess('custom_field', 'create', $params); |
525 | $cfId = 'custom_' . $customField['id']; | |
526 | ||
527 | $cid = $this->individualCreate(); | |
528 | ||
dba70c02 | 529 | $attachment = $this->callAPISuccess('attachment', 'create', [ |
b3c30fda CW |
530 | 'name' => CRM_Utils_String::createRandom(5, CRM_Utils_String::ALPHANUMERIC) . '_testCustomFileField.txt', |
531 | 'mime_type' => 'text/plain', | |
532 | 'content' => 'My test content', | |
533 | 'field_name' => $cfId, | |
534 | 'entity_id' => $cid, | |
dba70c02 | 535 | ]); |
b3c30fda CW |
536 | $this->assertAttachmentExistence(TRUE, $attachment); |
537 | ||
dba70c02 | 538 | $result = $this->callAPISuccess('contact', 'getsingle', [ |
b3c30fda CW |
539 | 'id' => $cid, |
540 | 'return' => $cfId, | |
dba70c02 | 541 | ]); |
b3c30fda CW |
542 | |
543 | $this->assertEquals($attachment['id'], $result[$cfId]); | |
544 | } | |
545 | ||
12b07b22 | 546 | public function testUpdateCustomField() { |
dba70c02 | 547 | $customGroup = $this->customGroupCreate(['extends' => 'Individual']); |
548 | $params = ['id' => $customGroup['id'], 'is_active' => 0]; | |
12b07b22 MD |
549 | $result = $this->callAPISuccess('CustomGroup', 'create', $params); |
550 | $result = array_shift($result['values']); | |
551 | ||
552 | $this->assertEquals(0, $result['is_active']); | |
553 | ||
554 | $this->customGroupDelete($customGroup['id']); | |
555 | } | |
556 | ||
f93514f8 | 557 | public function testCustomFieldCreateWithOptionGroupName() { |
dba70c02 | 558 | $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'test_custom_group']); |
559 | $params = [ | |
f93514f8 MM |
560 | 'custom_group_id' => $customGroup['id'], |
561 | 'name' => 'Activity type', | |
562 | 'label' => 'Activity type', | |
563 | 'data_type' => 'String', | |
564 | 'html_type' => 'Select', | |
565 | 'option_group_id' => 'activity_type', | |
dba70c02 | 566 | ]; |
f93514f8 MM |
567 | $result = $this->callAPISuccess('CustomField', 'create', $params); |
568 | } | |
569 | ||
4cbe18b8 EM |
570 | /** |
571 | * @param $getFieldsResult | |
572 | * | |
573 | * @return array | |
574 | */ | |
00be9182 | 575 | public function getCustomFieldKeys($getFieldsResult) { |
92915c55 | 576 | $isCustom = function ($key) { |
6b59896e TO |
577 | return preg_match('/^custom_/', $key); |
578 | }; | |
f8a3cd29 TO |
579 | $r = array_values(array_filter(array_keys($getFieldsResult['values']), $isCustom)); |
580 | sort($r); | |
581 | return $r; | |
6b59896e | 582 | } |
96025800 | 583 | |
7ab8180f | 584 | public function testMakeSearchableContactReferenceFieldUnsearchable() { |
dba70c02 | 585 | $customGroup = $this->customGroupCreate([ |
7ab8180f MM |
586 | 'name' => 'testCustomGroup', |
587 | 'title' => 'testCustomGroup', | |
588 | 'extends' => 'Individual', | |
dba70c02 | 589 | ]); |
590 | $params = [ | |
7ab8180f MM |
591 | 'name' => 'testCustomField', |
592 | 'label' => 'testCustomField', | |
593 | 'custom_group_id' => 'testCustomGroup', | |
594 | 'data_type' => 'ContactReference', | |
595 | 'html_type' => 'Autocomplete-Select', | |
596 | 'is_searchable' => '1', | |
dba70c02 | 597 | ]; |
7ab8180f MM |
598 | $result = $this->callAPISuccess('CustomField', 'create', $params); |
599 | $params = [ | |
600 | 'id' => $result['id'], | |
601 | 'is_searchable' => 0, | |
602 | ]; | |
603 | $result = $this->callAPISuccess('CustomField', 'create', $params); | |
604 | } | |
605 | ||
e5ce15c3 | 606 | /** |
607 | * Test disabling a searchable contact reference field. | |
608 | */ | |
f2ff6efa | 609 | public function testDisableSearchableContactReferenceField() { |
dba70c02 | 610 | $customGroup = $this->customGroupCreate([ |
f2ff6efa MM |
611 | 'name' => 'testCustomGroup', |
612 | 'title' => 'testCustomGroup', | |
613 | 'extends' => 'Individual', | |
dba70c02 | 614 | ]); |
615 | $params = [ | |
f2ff6efa MM |
616 | 'name' => 'testCustomField', |
617 | 'label' => 'testCustomField', | |
618 | 'custom_group_id' => 'testCustomGroup', | |
619 | 'data_type' => 'ContactReference', | |
620 | 'html_type' => 'Autocomplete-Select', | |
621 | 'is_searchable' => '1', | |
dba70c02 | 622 | ]; |
f2ff6efa MM |
623 | $result = $this->callAPISuccess('CustomField', 'create', $params); |
624 | $params = [ | |
625 | 'id' => $result['id'], | |
626 | 'is_active' => 0, | |
627 | ]; | |
e5ce15c3 | 628 | $this->callAPISuccess('CustomField', 'create', $params); |
f2ff6efa MM |
629 | } |
630 | ||
49df88a8 CW |
631 | public function testLegacyHtmlType() { |
632 | $customGroup = $this->customGroupCreate([ | |
633 | 'name' => 'testCustomGroup', | |
634 | 'title' => 'testCustomGroup', | |
635 | 'extends' => 'Individual', | |
636 | ]); | |
637 | $f1 = $this->callAPISuccess('CustomField', 'create', [ | |
638 | 'label' => 'SingleSelect', | |
639 | 'custom_group_id' => 'testCustomGroup', | |
640 | 'data_type' => 'String', | |
641 | 'html_type' => 'Select', | |
642 | 'option_values' => [1 => 'One', 2 => 'Two'], | |
643 | ]); | |
644 | $f2 = $this->callAPISuccess('CustomField', 'create', [ | |
645 | 'label' => 'CheckBoxes', | |
646 | 'custom_group_id' => 'testCustomGroup', | |
647 | 'data_type' => 'String', | |
648 | 'html_type' => 'CheckBox', | |
649 | 'option_values' => [1 => 'One', 2 => 'Two'], | |
650 | ]); | |
651 | $f3 = $this->callAPISuccess('CustomField', 'create', [ | |
652 | 'label' => 'MultiSelect', | |
653 | 'custom_group_id' => 'testCustomGroup', | |
654 | 'data_type' => 'String', | |
655 | 'html_type' => 'Multi-Select', | |
656 | 'option_values' => [1 => 'One', 2 => 'Two'], | |
657 | ]); | |
658 | ||
659 | $result = $this->callAPISuccess('CustomField', 'get', [ | |
660 | 'custom_group_id' => 'testCustomGroup', | |
661 | 'html_type' => 'Multi-Select', | |
662 | 'sequential' => 1, | |
663 | ]); | |
664 | $this->assertCount(1, $result['values']); | |
665 | $this->assertEquals('MultiSelect', $result['values'][0]['label']); | |
666 | ||
667 | $result = $this->callAPISuccess('CustomField', 'get', [ | |
668 | 'custom_group_id' => 'testCustomGroup', | |
669 | 'html_type' => ['IN' => ['Multi-Select', 'CheckBox']], | |
670 | 'sequential' => 1, | |
671 | ]); | |
672 | $this->assertCount(2, $result['values']); | |
673 | ||
674 | $result = $this->callAPISuccess('CustomField', 'get', [ | |
675 | 'custom_group_id' => 'testCustomGroup', | |
676 | 'html_type' => 'Select', | |
677 | 'sequential' => 1, | |
678 | ]); | |
679 | $this->assertCount(1, $result['values']); | |
680 | $this->assertEquals('SingleSelect', $result['values'][0]['label']); | |
681 | ||
682 | $result = $this->callAPISuccess('CustomField', 'get', [ | |
683 | 'custom_group_id' => 'testCustomGroup', | |
684 | 'html_type' => ['IN' => ['Select']], | |
685 | 'sequential' => 1, | |
686 | ]); | |
687 | $this->assertCount(1, $result['values']); | |
688 | $this->assertEquals('SingleSelect', $result['values'][0]['label']); | |
689 | } | |
690 | ||
a7bcb32d CW |
691 | public function testLegacyStateCountryTypes() { |
692 | $customGroup = $this->customGroupCreate([ | |
693 | 'name' => 'testCustomGroup', | |
694 | 'title' => 'testCustomGroup', | |
695 | 'extends' => 'Individual', | |
696 | ]); | |
697 | $f1 = $this->callAPISuccess('CustomField', 'create', [ | |
698 | 'label' => 'CountrySelect', | |
699 | 'custom_group_id' => 'testCustomGroup', | |
700 | 'data_type' => 'Country', | |
701 | 'html_type' => 'Select Country', | |
702 | ]); | |
703 | $f2 = $this->callAPISuccess('CustomField', 'create', [ | |
704 | 'label' => 'StateSelect', | |
705 | 'custom_group_id' => 'testCustomGroup', | |
706 | 'data_type' => 'StateProvince', | |
707 | 'html_type' => 'Select State/Province', | |
708 | ]); | |
709 | $f3 = $this->callAPISuccess('CustomField', 'create', [ | |
710 | 'label' => 'MultiSelectSP', | |
711 | 'custom_group_id' => 'testCustomGroup', | |
712 | 'data_type' => 'StateProvince', | |
713 | 'html_type' => 'Multi-Select State/Province', | |
714 | ]); | |
715 | $f4 = $this->callAPISuccess('CustomField', 'create', [ | |
716 | 'label' => 'MultiSelectCountry', | |
717 | 'custom_group_id' => 'testCustomGroup', | |
718 | 'data_type' => 'Country', | |
719 | 'html_type' => 'Select Country', | |
720 | 'serialize' => 1, | |
721 | ]); | |
722 | ||
723 | $result = $this->callAPISuccess('CustomField', 'get', [ | |
724 | 'custom_group_id' => 'testCustomGroup', | |
725 | 'html_type' => 'Multi-Select State/Province', | |
726 | 'sequential' => 1, | |
727 | ]); | |
728 | $this->assertCount(1, $result['values']); | |
729 | $this->assertEquals('MultiSelectSP', $result['values'][0]['label']); | |
730 | $this->assertEquals('Multi-Select State/Province', $result['values'][0]['html_type']); | |
731 | $this->assertEquals('1', $result['values'][0]['serialize']); | |
732 | ||
733 | $result = $this->callAPISuccess('CustomField', 'get', [ | |
734 | 'custom_group_id' => 'testCustomGroup', | |
735 | 'html_type' => 'Multi-Select Country', | |
736 | 'sequential' => 1, | |
737 | ]); | |
738 | $this->assertCount(1, $result['values']); | |
739 | $this->assertEquals('MultiSelectCountry', $result['values'][0]['label']); | |
740 | $this->assertEquals('Multi-Select Country', $result['values'][0]['html_type']); | |
741 | $this->assertEquals('1', $result['values'][0]['serialize']); | |
742 | ||
743 | $result = $this->callAPISuccess('CustomField', 'get', [ | |
744 | 'custom_group_id' => 'testCustomGroup', | |
745 | 'html_type' => 'Select Country', | |
746 | 'sequential' => 1, | |
747 | ]); | |
748 | $this->assertCount(1, $result['values']); | |
749 | $this->assertEquals('CountrySelect', $result['values'][0]['label']); | |
750 | $this->assertEquals('Select Country', $result['values'][0]['html_type']); | |
751 | $this->assertEquals('0', $result['values'][0]['serialize']); | |
752 | ||
753 | $result = $this->callAPISuccess('CustomField', 'get', [ | |
754 | 'custom_group_id' => 'testCustomGroup', | |
755 | 'html_type' => 'Select State/Province', | |
756 | 'sequential' => 1, | |
757 | ]); | |
758 | $this->assertCount(1, $result['values']); | |
759 | $this->assertEquals('StateSelect', $result['values'][0]['label']); | |
760 | $this->assertEquals('Select State/Province', $result['values'][0]['html_type']); | |
761 | $this->assertEquals('0', $result['values'][0]['serialize']); | |
762 | } | |
763 | ||
6a488035 | 764 | } |