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 TO |
109 | |
110 | $n = 0; | |
111 | foreach ($dtype as $dkey => $dvalue) { | |
112 | foreach ($htype[$n] as $hkey => $hvalue) { | |
113 | //echo $dkey."][".$hvalue."\n"; | |
114 | $this->_loopingCustomFieldCreateTest($this->_buildParams($gid['id'], $hvalue, $dkey)); | |
115 | } | |
116 | $n++; | |
117 | } | |
118 | } | |
39b959db | 119 | |
6c6e6187 | 120 | /* |
e70a7fc0 TO |
121 | * Can't figure out the point of this? |
122 | */ | |
39b959db | 123 | |
4cbe18b8 | 124 | /** |
c490a46a | 125 | * @param array $params |
4cbe18b8 | 126 | */ |
00be9182 | 127 | public function _loopingCustomFieldCreateTest($params) { |
e830c6ae | 128 | $customField = $this->callAPISuccess('custom_field', 'create', $params); |
6a488035 TO |
129 | $this->assertNotNull($customField['id']); |
130 | $this->getAndCheck($params, $customField['id'], 'CustomField'); | |
131 | } | |
132 | ||
4cbe18b8 | 133 | /** |
100fef9d | 134 | * @param int $gid |
4cbe18b8 EM |
135 | * @param $htype |
136 | * @param $dtype | |
137 | * | |
138 | * @return array | |
139 | */ | |
00be9182 | 140 | public function _buildParams($gid, $htype, $dtype) { |
6a488035 TO |
141 | $params = $this->_buildBasicParams($gid, $htype, $dtype); |
142 | /* //Not Working for any type. Maybe redundant with testCustomFieldCreateWithOptionValues() | |
e70a7fc0 TO |
143 | if ($htype == 'Multi-Select') |
144 | $params = array_merge($params, array( | |
145 | 'option_label' => array( 'Label1','Label2'), | |
146 | 'option_value' => array( 'val1', 'val2' ), | |
147 | 'option_weight' => array( 1, 2), | |
148 | 'option_status' => array( 1, 1), | |
149 | )); | |
150 | */ | |
6a488035 TO |
151 | |
152 | return $params; | |
153 | } | |
154 | ||
4cbe18b8 | 155 | /** |
100fef9d | 156 | * @param int $gid |
4cbe18b8 EM |
157 | * @param $htype |
158 | * @param $dtype | |
159 | * | |
160 | * @return array | |
161 | */ | |
00be9182 | 162 | public function _buildBasicParams($gid, $htype, $dtype) { |
dba70c02 | 163 | return [ |
6a488035 TO |
164 | 'custom_group_id' => $gid, |
165 | 'label' => $dtype . $htype, | |
166 | 'html_type' => $htype, | |
167 | 'data_type' => $dtype, | |
168 | 'weight' => 4, | |
169 | 'is_required' => 0, | |
170 | 'is_searchable' => 0, | |
171 | 'is_active' => 1, | |
e830c6ae | 172 | |
dba70c02 | 173 | ]; |
6a488035 TO |
174 | } |
175 | ||
176 | /** | |
eceb18cc | 177 | * Test using example code. |
6a488035 TO |
178 | */ |
179 | /*function testCustomFieldCreateExample( ) | |
e70a7fc0 | 180 | { |
6a488035 | 181 | |
e70a7fc0 | 182 | $customGroup = $this->customGroupCreate('Individual','date_test_group',3); |
be44cfcb | 183 | require_once 'api/v3/examples/CustomField/Create.ex.php'; |
e70a7fc0 TO |
184 | $result = custom_field_create_example(); |
185 | $expectedResult = custom_field_create_expectedresult(); | |
186 | $this->assertEquals($result,$expectedResult); | |
187 | }*/ | |
6a488035 TO |
188 | |
189 | /** | |
100fef9d | 190 | * Check with data type - Options with option_values |
6a488035 | 191 | */ |
00be9182 | 192 | public function testCustomFieldCreateWithEmptyOptionGroup() { |
dba70c02 | 193 | $customGroup = $this->customGroupCreate(['extends' => 'Contact', 'title' => 'select_test_group']); |
194 | $params = [ | |
b958933f | 195 | 'custom_group_id' => $customGroup['id'], |
196 | 'label' => 'Country', | |
197 | 'html_type' => 'Select', | |
198 | 'data_type' => 'String', | |
199 | 'weight' => 4, | |
200 | 'is_required' => 1, | |
201 | 'is_searchable' => 0, | |
202 | 'is_active' => 1, | |
dba70c02 | 203 | ]; |
b958933f | 204 | |
e830c6ae | 205 | $customField = $this->callAPISuccess('custom_field', 'create', $params); |
b958933f | 206 | $this->assertNotNull($customField['id']); |
dba70c02 | 207 | $optionGroupID = $this->callAPISuccess('custom_field', 'getvalue', [ |
b958933f | 208 | 'id' => $customField['id'], |
209 | 'return' => 'option_group_id', | |
dba70c02 | 210 | ]); |
b958933f | 211 | |
212 | $this->assertTrue(is_numeric($optionGroupID) && ($optionGroupID > 0)); | |
dba70c02 | 213 | $optionGroup = $this->callAPISuccess('option_group', 'getsingle', [ |
ae5ffbb7 | 214 | 'id' => $optionGroupID, |
dba70c02 | 215 | ]); |
6c6e6187 | 216 | $this->assertEquals($optionGroup['title'], 'Country'); |
dba70c02 | 217 | $optionValueCount = $this->callAPISuccess('option_value', 'getcount', [ |
ae5ffbb7 | 218 | 'option_group_id' => $optionGroupID, |
dba70c02 | 219 | ]); |
b958933f | 220 | $this->assertEquals(0, $optionValueCount); |
221 | } | |
222 | ||
65c2fd15 JP |
223 | /** |
224 | * Check with non-ascii labels | |
225 | */ | |
226 | public function testCustomFieldCreateWithNonAsciiLabel() { | |
dba70c02 | 227 | $customGroup = $this->customGroupCreate(['extends' => 'Contact', 'title' => 'select_test_group']); |
228 | $params = [ | |
65c2fd15 JP |
229 | 'custom_group_id' => $customGroup['id'], |
230 | 'label' => 'ôôôô', | |
231 | 'html_type' => 'Select', | |
232 | 'data_type' => 'String', | |
233 | 'weight' => 4, | |
234 | 'is_required' => 1, | |
235 | 'is_searchable' => 0, | |
236 | 'is_active' => 1, | |
dba70c02 | 237 | ]; |
65c2fd15 JP |
238 | $customField = $this->callAPISuccess('custom_field', 'create', $params); |
239 | $this->assertNotNull($customField['id']); | |
240 | $params['label'] = 'Ã Ã Ã '; | |
241 | $customField = $this->callAPISuccess('custom_field', 'create', $params); | |
242 | $this->assertNotNull($customField['id']); | |
243 | } | |
244 | ||
eebb53df | 245 | /** |
eceb18cc | 246 | * Test custom field with existing option group. |
eebb53df | 247 | */ |
00be9182 | 248 | public function testCustomFieldExistingOptionGroup() { |
dba70c02 | 249 | $customGroup = $this->customGroupCreate(['extends' => 'Organization', 'title' => 'test_group']); |
250 | $params = [ | |
eebb53df JV |
251 | 'custom_group_id' => $customGroup['id'], |
252 | // Just to say something: | |
253 | 'label' => 'Organization Gender', | |
254 | 'html_type' => 'Select', | |
255 | 'data_type' => 'Int', | |
256 | 'weight' => 4, | |
257 | 'is_required' => 1, | |
258 | 'is_searchable' => 0, | |
259 | 'is_active' => 1, | |
260 | // Option group id 3: gender | |
261 | 'option_group_id' => 3, | |
dba70c02 | 262 | ]; |
eebb53df JV |
263 | |
264 | $customField = $this->callAPISuccess('custom_field', 'create', $params); | |
265 | $this->assertNotNull($customField['id']); | |
dba70c02 | 266 | $optionGroupID = $this->callAPISuccess('custom_field', 'getvalue', [ |
eebb53df JV |
267 | 'id' => $customField['id'], |
268 | 'return' => 'option_group_id', | |
dba70c02 | 269 | ]); |
eebb53df | 270 | |
6c6e6187 | 271 | $this->assertEquals($optionGroupID, 3); |
eebb53df JV |
272 | } |
273 | ||
aa4143bb | 274 | /** |
275 | * Test adding an optionGroup to an existing field doesn't cause a fatal error. | |
276 | * | |
277 | * (this was happening due to a check running despite no existing option_group_id) | |
278 | * | |
279 | * @throws \CiviCRM_API3_Exception | |
280 | */ | |
281 | public function testUpdateCustomFieldAddOptionGroup() { | |
282 | $customGroup = $this->customGroupCreate(['extends' => 'Organization', 'title' => 'test_group']); | |
283 | $params = [ | |
284 | 'custom_group_id' => $customGroup['id'], | |
285 | 'label' => 'Organization Gender', | |
286 | 'html_type' => 'Text', | |
287 | 'data_type' => 'Int', | |
288 | ]; | |
289 | ||
290 | $customField = $this->callAPISuccess('custom_field', 'create', $params); | |
291 | $this->callAPISuccess('CustomField', 'create', [ | |
292 | 'option_group_id' => civicrm_api3('OptionGroup', 'getvalue', ['options' => ['limit' => 1], 'return' => 'id']), | |
293 | 'id' => $customField['id'], | |
294 | 'html_type' => 'Select', | |
295 | ]); | |
296 | } | |
297 | ||
3c70d501 | 298 | /** |
299 | * Test custom field get works & return param works | |
300 | */ | |
9b873358 | 301 | public function testCustomFieldGetReturnOptions() { |
dba70c02 | 302 | $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'test_group']); |
303 | $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]); | |
3c70d501 | 304 | |
dba70c02 | 305 | $result = $this->callAPISuccess('custom_field', 'getsingle', [ |
3c70d501 | 306 | 'id' => $customField['id'], |
307 | 'return' => 'data_type', | |
dba70c02 | 308 | ]); |
3c70d501 | 309 | $this->assertTrue(array_key_exists('data_type', $result)); |
310 | $this->assertFalse(array_key_exists('custom_group_id', $result)); | |
311 | } | |
312 | ||
313 | /** | |
314 | * Test custom field get works & return param works | |
315 | */ | |
9b873358 | 316 | public function testCustomFieldGetReturnArray() { |
dba70c02 | 317 | $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'test_group']); |
318 | $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]); | |
3c70d501 | 319 | |
dba70c02 | 320 | $result = $this->callAPISuccess('custom_field', 'getsingle', [ |
92915c55 | 321 | 'id' => $customField['id'], |
dba70c02 | 322 | 'return' => ['data_type'], |
323 | ]); | |
3c70d501 | 324 | $this->assertTrue(array_key_exists('data_type', $result)); |
325 | $this->assertFalse(array_key_exists('custom_group_id', $result)); | |
326 | } | |
327 | ||
328 | /** | |
329 | * Test custom field get works & return param works | |
330 | */ | |
9b873358 | 331 | public function testCustomFieldGetReturnTwoOptions() { |
dba70c02 | 332 | $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'test_group']); |
333 | $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]); | |
3c70d501 | 334 | |
dba70c02 | 335 | $result = $this->callAPISuccess('custom_field', 'getsingle', [ |
92915c55 | 336 | 'id' => $customField['id'], |
3c70d501 | 337 | 'return' => 'data_type, custom_group_id', |
dba70c02 | 338 | ]); |
3c70d501 | 339 | $this->assertTrue(array_key_exists('data_type', $result)); |
340 | $this->assertTrue(array_key_exists('custom_group_id', $result)); | |
341 | $this->assertFalse(array_key_exists('label', $result)); | |
342 | } | |
343 | ||
00be9182 | 344 | public function testCustomFieldCreateWithOptionValues() { |
dba70c02 | 345 | $customGroup = $this->customGroupCreate(['extends' => 'Contact', 'title' => 'select_test_group']); |
6a488035 | 346 | |
dba70c02 | 347 | $option_values = [ |
348 | [ | |
92915c55 | 349 | 'weight' => 1, |
6a488035 TO |
350 | 'label' => 'Label1', |
351 | 'value' => 1, | |
352 | 'is_active' => 1, | |
dba70c02 | 353 | ], |
354 | [ | |
6a488035 TO |
355 | 'weight' => 2, |
356 | 'label' => 'Label2', | |
357 | 'value' => 2, | |
358 | 'is_active' => 1, | |
dba70c02 | 359 | ], |
360 | ]; | |
6a488035 | 361 | |
dba70c02 | 362 | $params = [ |
6a488035 | 363 | 'custom_group_id' => $customGroup['id'], |
7f6d3dd3 | 364 | 'label' => 'Our special field', |
6a488035 TO |
365 | 'html_type' => 'Select', |
366 | 'data_type' => 'String', | |
367 | 'weight' => 4, | |
368 | 'is_required' => 1, | |
369 | 'is_searchable' => 0, | |
370 | 'is_active' => 1, | |
371 | 'option_values' => $option_values, | |
e830c6ae | 372 | |
dba70c02 | 373 | ]; |
6a488035 | 374 | |
e830c6ae | 375 | $customField = $this->callAPISuccess('custom_field', 'create', $params); |
6a488035 | 376 | |
b958933f | 377 | $this->assertAPISuccess($customField); |
6a488035 | 378 | $this->assertNotNull($customField['id']); |
dba70c02 | 379 | $getFieldsParams = [ |
380 | 'options' => ['get_options' => 'custom_' . $customField['id']], | |
92915c55 | 381 | 'action' => 'create', |
dba70c02 | 382 | ]; |
5c49fee0 | 383 | $description = "Demonstrates retrieving metadata with custom field options."; |
6a488035 | 384 | $subfile = "GetFieldsOptions"; |
a828d7b8 | 385 | $fields = $this->callAPIAndDocument('contact', 'getfields', $getFieldsParams, __FUNCTION__, 'ContactTest.php', $description, $subfile); |
6a488035 TO |
386 | $this->assertArrayHasKey('options', $fields['values']['custom_' . $customField['id']]); |
387 | $this->assertEquals('Label1', $fields['values']['custom_' . $customField['id']]['options'][1]); | |
dba70c02 | 388 | $getOptionsArray = [ |
6a488035 | 389 | 'field' => 'custom_' . $customField['id'], |
dba70c02 | 390 | ]; |
5c49fee0 | 391 | $description = "Demonstrates retrieving options for a custom field."; |
6a488035 | 392 | $subfile = "GetOptions"; |
a828d7b8 | 393 | $result = $this->callAPIAndDocument('contact', 'getoptions', $getOptionsArray, __FUNCTION__, 'ContactTest.php', $description, ''); |
6a488035 | 394 | $this->assertEquals('Label1', $result['values'][1]); |
6a488035 TO |
395 | } |
396 | ||
397 | ///////////////// civicrm_custom_field_delete methods | |
398 | ||
6a488035 | 399 | /** |
eceb18cc | 400 | * Check without Field ID. |
6a488035 | 401 | */ |
00be9182 | 402 | public function testCustomFieldDeleteWithoutFieldID() { |
dba70c02 | 403 | $params = []; |
e830c6ae | 404 | $customField = $this->callAPIFailure('custom_field', 'delete', $params, |
405 | 'Mandatory key(s) missing from params array: id'); | |
6a488035 TO |
406 | } |
407 | ||
408 | /** | |
eceb18cc | 409 | * Check without valid array. |
6a488035 | 410 | */ |
00be9182 | 411 | public function testCustomFieldDelete() { |
dba70c02 | 412 | $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'test_group']); |
413 | $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]); | |
ba4a1892 | 414 | $this->assertNotNull($customField['id']); |
6a488035 | 415 | |
dba70c02 | 416 | $params = [ |
6a488035 | 417 | 'id' => $customField['id'], |
dba70c02 | 418 | ]; |
e830c6ae | 419 | $result = $this->callAPIAndDocument('custom_field', 'delete', $params, __FUNCTION__, __FILE__); |
6a488035 | 420 | |
a15773db | 421 | $this->assertAPISuccess($result); |
6a488035 TO |
422 | } |
423 | ||
424 | /** | |
eceb18cc | 425 | * Check for Option Value. |
6a488035 | 426 | */ |
00be9182 | 427 | public function testCustomFieldOptionValueDelete() { |
dba70c02 | 428 | $customGroup = $this->customGroupCreate(['extends' => 'Contact', 'title' => 'ABC']); |
6a488035 | 429 | $customOptionValueFields = $this->customFieldOptionValueCreate($customGroup, 'fieldABC'); |
dba70c02 | 430 | $params = [ |
6a488035 | 431 | 'id' => $customOptionValueFields, |
dba70c02 | 432 | ]; |
6a488035 | 433 | |
e830c6ae | 434 | $customField = $this->callAPISuccess('custom_field', 'delete', $customOptionValueFields); |
6a488035 | 435 | } |
6b59896e TO |
436 | |
437 | /** | |
438 | * If there's one custom group for "Contact" and one for "Activity", then "Contact.getfields" | |
439 | * and "Activity.getfields" should return only their respective fields (not the other's fields), | |
440 | * and unrelated entities should return no custom fields. | |
441 | */ | |
00be9182 | 442 | public function testGetfields_CrossEntityPollution() { |
dba70c02 | 443 | $auxEntities = ['Email', 'Address', 'LocBlock', 'Membership', 'ContributionPage', 'ReportInstance']; |
444 | $allEntities = array_merge(['Contact', 'Activity'], $auxEntities); | |
6b59896e TO |
445 | |
446 | // Baseline - getfields doesn't reporting any customfields for any entities | |
447 | foreach ($allEntities as $entity) { | |
448 | $this->assertEquals( | |
dba70c02 | 449 | [], |
450 | $this->getCustomFieldKeys($this->callAPISuccess($entity, 'getfields', [])), | |
6b59896e TO |
451 | "Baseline custom fields for $entity should be empty" |
452 | ); | |
453 | } | |
454 | ||
455 | // Add some fields | |
dba70c02 | 456 | $contactGroup = $this->customGroupCreate(['extends' => 'Contact', 'title' => 'test_group_c']); |
457 | $contactField = $this->customFieldCreate([ | |
39b959db SL |
458 | 'custom_group_id' => $contactGroup['id'], |
459 | 'label' => 'For Contacts', | |
dba70c02 | 460 | ]); |
461 | $indivGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'test_group_i']); | |
462 | $indivField = $this->customFieldCreate(['custom_group_id' => $indivGroup['id'], 'label' => 'For Individuals']); | |
463 | $activityGroup = $this->customGroupCreate(['extends' => 'Activity', 'title' => 'test_group_a']); | |
464 | $activityField = $this->customFieldCreate([ | |
39b959db SL |
465 | 'custom_group_id' => $activityGroup['id'], |
466 | 'label' => 'For Activities', | |
dba70c02 | 467 | ]); |
6b59896e | 468 | |
f8a3cd29 TO |
469 | // Check getfields |
470 | $this->assertEquals( | |
dba70c02 | 471 | ['custom_' . $contactField['id'], 'custom_' . $indivField['id']], |
472 | $this->getCustomFieldKeys($this->callAPISuccess('Contact', 'getfields', [])), | |
f8a3cd29 TO |
473 | 'Contact custom fields' |
474 | ); | |
475 | $this->assertEquals( | |
dba70c02 | 476 | ['custom_' . $contactField['id'], 'custom_' . $indivField['id']], |
477 | $this->getCustomFieldKeys($this->callAPISuccess('Individual', 'getfields', [])), | |
f8a3cd29 TO |
478 | 'Individual custom fields' |
479 | ); | |
480 | $this->assertEquals( | |
dba70c02 | 481 | ['custom_' . $contactField['id']], |
482 | $this->getCustomFieldKeys($this->callAPISuccess('Organization', 'getfields', [])), | |
f8a3cd29 TO |
483 | 'Organization custom fields' |
484 | ); | |
6b59896e | 485 | $this->assertEquals( |
dba70c02 | 486 | ['custom_' . $activityField['id']], |
487 | $this->getCustomFieldKeys($this->callAPISuccess('Activity', 'getfields', [])), | |
6b59896e TO |
488 | 'Activity custom fields' |
489 | ); | |
490 | foreach ($auxEntities as $entity) { | |
491 | $this->assertEquals( | |
dba70c02 | 492 | [], |
493 | $this->getCustomFieldKeys($this->callAPISuccess($entity, 'getfields', [])), | |
6b59896e TO |
494 | "Custom fields for $entity should be empty" |
495 | ); | |
496 | } | |
497 | } | |
498 | ||
b3c30fda CW |
499 | /** |
500 | * Test setting and getting a custom file field value. | |
501 | * | |
502 | * Uses the "attachment" api for setting value. | |
503 | */ | |
504 | public function testCustomFileField() { | |
dba70c02 | 505 | $customGroup = $this->customGroupCreate(['title' => 'attachment_test_group']); |
506 | $params = [ | |
b3c30fda CW |
507 | 'custom_group_id' => $customGroup['id'], |
508 | 'name' => 'test_file_attachment', | |
509 | 'label' => 'test_file_attachment', | |
510 | 'html_type' => 'File', | |
511 | 'data_type' => 'File', | |
512 | 'is_active' => 1, | |
dba70c02 | 513 | ]; |
b3c30fda CW |
514 | $customField = $this->callAPISuccess('custom_field', 'create', $params); |
515 | $cfId = 'custom_' . $customField['id']; | |
516 | ||
517 | $cid = $this->individualCreate(); | |
518 | ||
dba70c02 | 519 | $attachment = $this->callAPISuccess('attachment', 'create', [ |
b3c30fda CW |
520 | 'name' => CRM_Utils_String::createRandom(5, CRM_Utils_String::ALPHANUMERIC) . '_testCustomFileField.txt', |
521 | 'mime_type' => 'text/plain', | |
522 | 'content' => 'My test content', | |
523 | 'field_name' => $cfId, | |
524 | 'entity_id' => $cid, | |
dba70c02 | 525 | ]); |
b3c30fda CW |
526 | $this->assertAttachmentExistence(TRUE, $attachment); |
527 | ||
dba70c02 | 528 | $result = $this->callAPISuccess('contact', 'getsingle', [ |
b3c30fda CW |
529 | 'id' => $cid, |
530 | 'return' => $cfId, | |
dba70c02 | 531 | ]); |
b3c30fda CW |
532 | |
533 | $this->assertEquals($attachment['id'], $result[$cfId]); | |
534 | } | |
535 | ||
12b07b22 | 536 | public function testUpdateCustomField() { |
dba70c02 | 537 | $customGroup = $this->customGroupCreate(['extends' => 'Individual']); |
538 | $params = ['id' => $customGroup['id'], 'is_active' => 0]; | |
12b07b22 MD |
539 | $result = $this->callAPISuccess('CustomGroup', 'create', $params); |
540 | $result = array_shift($result['values']); | |
541 | ||
542 | $this->assertEquals(0, $result['is_active']); | |
543 | ||
544 | $this->customGroupDelete($customGroup['id']); | |
545 | } | |
546 | ||
f93514f8 | 547 | public function testCustomFieldCreateWithOptionGroupName() { |
dba70c02 | 548 | $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'test_custom_group']); |
549 | $params = [ | |
f93514f8 MM |
550 | 'custom_group_id' => $customGroup['id'], |
551 | 'name' => 'Activity type', | |
552 | 'label' => 'Activity type', | |
553 | 'data_type' => 'String', | |
554 | 'html_type' => 'Select', | |
555 | 'option_group_id' => 'activity_type', | |
dba70c02 | 556 | ]; |
f93514f8 MM |
557 | $result = $this->callAPISuccess('CustomField', 'create', $params); |
558 | } | |
559 | ||
4cbe18b8 EM |
560 | /** |
561 | * @param $getFieldsResult | |
562 | * | |
563 | * @return array | |
564 | */ | |
00be9182 | 565 | public function getCustomFieldKeys($getFieldsResult) { |
92915c55 | 566 | $isCustom = function ($key) { |
6b59896e TO |
567 | return preg_match('/^custom_/', $key); |
568 | }; | |
f8a3cd29 TO |
569 | $r = array_values(array_filter(array_keys($getFieldsResult['values']), $isCustom)); |
570 | sort($r); | |
571 | return $r; | |
6b59896e | 572 | } |
96025800 | 573 | |
7ab8180f | 574 | public function testMakeSearchableContactReferenceFieldUnsearchable() { |
dba70c02 | 575 | $customGroup = $this->customGroupCreate([ |
7ab8180f MM |
576 | 'name' => 'testCustomGroup', |
577 | 'title' => 'testCustomGroup', | |
578 | 'extends' => 'Individual', | |
dba70c02 | 579 | ]); |
580 | $params = [ | |
7ab8180f MM |
581 | 'name' => 'testCustomField', |
582 | 'label' => 'testCustomField', | |
583 | 'custom_group_id' => 'testCustomGroup', | |
584 | 'data_type' => 'ContactReference', | |
585 | 'html_type' => 'Autocomplete-Select', | |
586 | 'is_searchable' => '1', | |
dba70c02 | 587 | ]; |
7ab8180f MM |
588 | $result = $this->callAPISuccess('CustomField', 'create', $params); |
589 | $params = [ | |
590 | 'id' => $result['id'], | |
591 | 'is_searchable' => 0, | |
592 | ]; | |
593 | $result = $this->callAPISuccess('CustomField', 'create', $params); | |
594 | } | |
595 | ||
e5ce15c3 | 596 | /** |
597 | * Test disabling a searchable contact reference field. | |
598 | */ | |
f2ff6efa | 599 | public function testDisableSearchableContactReferenceField() { |
dba70c02 | 600 | $customGroup = $this->customGroupCreate([ |
f2ff6efa MM |
601 | 'name' => 'testCustomGroup', |
602 | 'title' => 'testCustomGroup', | |
603 | 'extends' => 'Individual', | |
dba70c02 | 604 | ]); |
605 | $params = [ | |
f2ff6efa MM |
606 | 'name' => 'testCustomField', |
607 | 'label' => 'testCustomField', | |
608 | 'custom_group_id' => 'testCustomGroup', | |
609 | 'data_type' => 'ContactReference', | |
610 | 'html_type' => 'Autocomplete-Select', | |
611 | 'is_searchable' => '1', | |
dba70c02 | 612 | ]; |
f2ff6efa MM |
613 | $result = $this->callAPISuccess('CustomField', 'create', $params); |
614 | $params = [ | |
615 | 'id' => $result['id'], | |
616 | 'is_active' => 0, | |
617 | ]; | |
e5ce15c3 | 618 | $this->callAPISuccess('CustomField', 'create', $params); |
f2ff6efa MM |
619 | } |
620 | ||
6a488035 | 621 | } |