3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * Test APIv3 civicrm_create_custom_group
34 class api_v3_CustomFieldTest
extends CiviUnitTestCase
{
35 protected $_apiversion;
37 public function setUp() {
38 $this->_apiversion
= 3;
42 public function tearDown() {
43 $tablesToTruncate = array(
46 'civicrm_entity_file',
48 // true tells quickCleanup to drop custom_value tables that might have been created in the test
49 $this->quickCleanup($tablesToTruncate, TRUE);
53 * Check with no array.
55 public function testCustomFieldCreateNoArray() {
58 $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams);
59 $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array');
63 * Check with no label.
65 public function testCustomFieldCreateWithoutLabel() {
66 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'text_test_group'));
68 'custom_group_id' => $customGroup['id'],
69 'name' => 'test_textfield2',
70 'html_type' => 'Text',
71 'data_type' => 'String',
72 'default_value' => 'abc',
79 $customField = $this->callAPIFailure('custom_field', 'create', $params);
80 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: label');
86 public function testCustomFieldCreateWithEdit() {
87 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'text_test_group'));
89 'custom_group_id' => $customGroup['id'],
90 'name' => 'test_textfield2',
92 'html_type' => 'Text',
93 'data_type' => 'String',
94 'default_value' => 'abc',
101 $customField = $this->callAPIAndDocument('custom_field', 'create', $params, __FUNCTION__
, __FILE__
);
102 $params['id'] = $customField['id'];
103 $customField = $this->callAPISuccess('custom_field', 'create', $params);
105 $this->assertNotNull($customField['id']);
109 * Check without groupId.
111 public function testCustomFieldCreateWithoutGroupID() {
112 $fieldParams = array(
113 'name' => 'test_textfield1',
115 'html_type' => 'Text',
116 'data_type' => 'String',
117 'default_value' => 'abc',
120 'is_searchable' => 0,
125 $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams);
126 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: custom_group_id');
130 * Check for Each data type: loop through available form input types
132 public function testCustomFieldCreateAllAvailableFormInputs() {
133 $gid = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'testAllFormInputs'));
135 $dtype = CRM_Core_BAO_CustomField
::dataType();
136 $htype = CRM_Core_BAO_CustomField
::dataToHtml();
139 foreach ($dtype as $dkey => $dvalue) {
140 foreach ($htype[$n] as $hkey => $hvalue) {
141 //echo $dkey."][".$hvalue."\n";
142 $this->_loopingCustomFieldCreateTest($this->_buildParams($gid['id'], $hvalue, $dkey));
148 * Can't figure out the point of this?
151 * @param array $params
153 public function _loopingCustomFieldCreateTest($params) {
154 $customField = $this->callAPISuccess('custom_field', 'create', $params);
155 $this->assertNotNull($customField['id']);
156 $this->getAndCheck($params, $customField['id'], 'CustomField');
166 public function _buildParams($gid, $htype, $dtype) {
167 $params = $this->_buildBasicParams($gid, $htype, $dtype);
168 /* //Not Working for any type. Maybe redundant with testCustomFieldCreateWithOptionValues()
169 if ($htype == 'Multi-Select')
170 $params = array_merge($params, array(
171 'option_label' => array( 'Label1','Label2'),
172 'option_value' => array( 'val1', 'val2' ),
173 'option_weight' => array( 1, 2),
174 'option_status' => array( 1, 1),
188 public function _buildBasicParams($gid, $htype, $dtype) {
190 'custom_group_id' => $gid,
191 'label' => $dtype . $htype,
192 'html_type' => $htype,
193 'data_type' => $dtype,
196 'is_searchable' => 0,
203 * Test using example code.
205 /*function testCustomFieldCreateExample( )
208 $customGroup = $this->customGroupCreate('Individual','date_test_group',3);
209 require_once 'api/v3/examples/CustomField/Create.php';
210 $result = custom_field_create_example();
211 $expectedResult = custom_field_create_expectedresult();
212 $this->assertEquals($result,$expectedResult);
216 * Check with data type - Options with option_values
218 public function testCustomFieldCreateWithEmptyOptionGroup() {
219 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'select_test_group'));
221 'custom_group_id' => $customGroup['id'],
222 'label' => 'Country',
223 'html_type' => 'Select',
224 'data_type' => 'String',
227 'is_searchable' => 0,
231 $customField = $this->callAPISuccess('custom_field', 'create', $params);
232 $this->assertNotNull($customField['id']);
233 $optionGroupID = $this->callAPISuccess('custom_field', 'getvalue', array(
234 'id' => $customField['id'],
235 'return' => 'option_group_id',
238 $this->assertTrue(is_numeric($optionGroupID) && ($optionGroupID > 0));
239 $optionGroup = $this->callAPISuccess('option_group', 'getsingle', array(
240 'id' => $optionGroupID,
242 $this->assertEquals($optionGroup['title'], 'Country');
243 $optionValueCount = $this->callAPISuccess('option_value', 'getcount', array(
244 'option_group_id' => $optionGroupID,
246 $this->assertEquals(0, $optionValueCount);
250 * Check with non-ascii labels
252 public function testCustomFieldCreateWithNonAsciiLabel() {
253 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'select_test_group'));
255 'custom_group_id' => $customGroup['id'],
256 'label' => 'ôôôô',
257 'html_type' => 'Select',
258 'data_type' => 'String',
261 'is_searchable' => 0,
264 $customField = $this->callAPISuccess('custom_field', 'create', $params);
265 $this->assertNotNull($customField['id']);
266 $params['label'] = 'Ã Ã Ã ';
267 $customField = $this->callAPISuccess('custom_field', 'create', $params);
268 $this->assertNotNull($customField['id']);
272 * Test custom field with existing option group.
274 public function testCustomFieldExistingOptionGroup() {
275 $customGroup = $this->customGroupCreate(array('extends' => 'Organization', 'title' => 'test_group'));
277 'custom_group_id' => $customGroup['id'],
278 // Just to say something:
279 'label' => 'Organization Gender',
280 'html_type' => 'Select',
281 'data_type' => 'Int',
284 'is_searchable' => 0,
286 // Option group id 3: gender
287 'option_group_id' => 3,
290 $customField = $this->callAPISuccess('custom_field', 'create', $params);
291 $this->assertNotNull($customField['id']);
292 $optionGroupID = $this->callAPISuccess('custom_field', 'getvalue', array(
293 'id' => $customField['id'],
294 'return' => 'option_group_id',
297 $this->assertEquals($optionGroupID, 3);
301 * Test custom field get works & return param works
303 public function testCustomFieldGetReturnOptions() {
304 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
305 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
307 $result = $this->callAPISuccess('custom_field', 'getsingle', array(
308 'id' => $customField['id'],
309 'return' => 'data_type',
311 $this->assertTrue(array_key_exists('data_type', $result));
312 $this->assertFalse(array_key_exists('custom_group_id', $result));
316 * Test custom field get works & return param works
318 public function testCustomFieldGetReturnArray() {
319 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
320 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
322 $result = $this->callAPISuccess('custom_field', 'getsingle', array(
323 'id' => $customField['id'],
324 'return' => array('data_type'),
326 $this->assertTrue(array_key_exists('data_type', $result));
327 $this->assertFalse(array_key_exists('custom_group_id', $result));
331 * Test custom field get works & return param works
333 public function testCustomFieldGetReturnTwoOptions() {
334 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'test_group'));
335 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
337 $result = $this->callAPISuccess('custom_field', 'getsingle', array(
338 'id' => $customField['id'],
339 'return' => 'data_type, custom_group_id',
341 $this->assertTrue(array_key_exists('data_type', $result));
342 $this->assertTrue(array_key_exists('custom_group_id', $result));
343 $this->assertFalse(array_key_exists('label', $result));
346 public function testCustomFieldCreateWithOptionValues() {
347 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'select_test_group'));
349 $option_values = array(
365 'custom_group_id' => $customGroup['id'],
366 'label' => 'Our special field',
367 'html_type' => 'Select',
368 'data_type' => 'String',
371 'is_searchable' => 0,
373 'option_values' => $option_values,
377 $customField = $this->callAPISuccess('custom_field', 'create', $params);
379 $this->assertAPISuccess($customField);
380 $this->assertNotNull($customField['id']);
381 $getFieldsParams = array(
382 'options' => array('get_options' => 'custom_' . $customField['id']),
383 'action' => 'create',
385 $description = "Demonstrates retrieving metadata with custom field options.";
386 $subfile = "GetFieldsOptions";
387 $fields = $this->callAPIAndDocument('contact', 'getfields', $getFieldsParams, __FUNCTION__
, 'ContactTest.php', $description, $subfile);
388 $this->assertArrayHasKey('options', $fields['values']['custom_' . $customField['id']]);
389 $this->assertEquals('Label1', $fields['values']['custom_' . $customField['id']]['options'][1]);
390 $getOptionsArray = array(
391 'field' => 'custom_' . $customField['id'],
393 $description = "Demonstrates retrieving options for a custom field.";
394 $subfile = "GetOptions";
395 $result = $this->callAPIAndDocument('contact', 'getoptions', $getOptionsArray, __FUNCTION__
, 'ContactTest.php', $description, '');
396 $this->assertEquals('Label1', $result['values'][1]);
399 ///////////////// civicrm_custom_field_delete methods
402 * Check with no array.
404 public function testCustomFieldDeleteNoArray() {
406 $customField = $this->callAPIFailure('custom_field', 'delete', $params);
407 $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array');
411 * Check without Field ID.
413 public function testCustomFieldDeleteWithoutFieldID() {
415 $customField = $this->callAPIFailure('custom_field', 'delete', $params,
416 'Mandatory key(s) missing from params array: id');
420 * Check without valid array.
422 public function testCustomFieldDelete() {
423 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
424 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
425 $this->assertNotNull($customField['id']);
428 'id' => $customField['id'],
430 $result = $this->callAPIAndDocument('custom_field', 'delete', $params, __FUNCTION__
, __FILE__
);
432 $this->assertAPISuccess($result);
436 * Check for Option Value.
438 public function testCustomFieldOptionValueDelete() {
439 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'ABC'));
440 $customOptionValueFields = $this->customFieldOptionValueCreate($customGroup, 'fieldABC');
442 'id' => $customOptionValueFields,
445 $customField = $this->callAPISuccess('custom_field', 'delete', $customOptionValueFields);
449 * If there's one custom group for "Contact" and one for "Activity", then "Contact.getfields"
450 * and "Activity.getfields" should return only their respective fields (not the other's fields),
451 * and unrelated entities should return no custom fields.
453 public function testGetfields_CrossEntityPollution() {
454 $auxEntities = array('Email', 'Address', 'LocBlock', 'Membership', 'ContributionPage', 'ReportInstance');
455 $allEntities = array_merge(array('Contact', 'Activity'), $auxEntities);
457 // Baseline - getfields doesn't reporting any customfields for any entities
458 foreach ($allEntities as $entity) {
461 $this->getCustomFieldKeys($this->callAPISuccess($entity, 'getfields', array())),
462 "Baseline custom fields for $entity should be empty"
467 $contactGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'test_group_c'));
468 $contactField = $this->customFieldCreate(array(
469 'custom_group_id' => $contactGroup['id'],
470 'label' => 'For Contacts',
472 $indivGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group_i'));
473 $indivField = $this->customFieldCreate(array('custom_group_id' => $indivGroup['id'], 'label' => 'For Individuals'));
474 $activityGroup = $this->customGroupCreate(array('extends' => 'Activity', 'title' => 'test_group_a'));
475 $activityField = $this->customFieldCreate(array(
476 'custom_group_id' => $activityGroup['id'],
477 'label' => 'For Activities',
482 array('custom_' . $contactField['id'], 'custom_' . $indivField['id']),
483 $this->getCustomFieldKeys($this->callAPISuccess('Contact', 'getfields', array())),
484 'Contact custom fields'
487 array('custom_' . $contactField['id'], 'custom_' . $indivField['id']),
488 $this->getCustomFieldKeys($this->callAPISuccess('Individual', 'getfields', array())),
489 'Individual custom fields'
492 array('custom_' . $contactField['id']),
493 $this->getCustomFieldKeys($this->callAPISuccess('Organization', 'getfields', array())),
494 'Organization custom fields'
497 array('custom_' . $activityField['id']),
498 $this->getCustomFieldKeys($this->callAPISuccess('Activity', 'getfields', array())),
499 'Activity custom fields'
501 foreach ($auxEntities as $entity) {
504 $this->getCustomFieldKeys($this->callAPISuccess($entity, 'getfields', array())),
505 "Custom fields for $entity should be empty"
511 * Test setting and getting a custom file field value.
513 * Uses the "attachment" api for setting value.
515 public function testCustomFileField() {
516 $customGroup = $this->customGroupCreate(array('title' => 'attachment_test_group'));
518 'custom_group_id' => $customGroup['id'],
519 'name' => 'test_file_attachment',
520 'label' => 'test_file_attachment',
521 'html_type' => 'File',
522 'data_type' => 'File',
525 $customField = $this->callAPISuccess('custom_field', 'create', $params);
526 $cfId = 'custom_' . $customField['id'];
528 $cid = $this->individualCreate();
530 $attachment = $this->callAPISuccess('attachment', 'create', array(
531 'name' => CRM_Utils_String
::createRandom(5, CRM_Utils_String
::ALPHANUMERIC
) . '_testCustomFileField.txt',
532 'mime_type' => 'text/plain',
533 'content' => 'My test content',
534 'field_name' => $cfId,
537 $this->assertAttachmentExistence(TRUE, $attachment);
539 $result = $this->callAPISuccess('contact', 'getsingle', array(
544 $this->assertEquals($attachment['id'], $result[$cfId]);
547 public function testUpdateCustomField() {
548 $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
549 $params = array('id' => $customGroup['id'], 'is_active' => 0);
550 $result = $this->callAPISuccess('CustomGroup', 'create', $params);
551 $result = array_shift($result['values']);
553 $this->assertEquals(0, $result['is_active']);
555 $this->customGroupDelete($customGroup['id']);
558 public function testCustomFieldCreateWithOptionGroupName() {
559 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_custom_group'));
561 'custom_group_id' => $customGroup['id'],
562 'name' => 'Activity type',
563 'label' => 'Activity type',
564 'data_type' => 'String',
565 'html_type' => 'Select',
566 'option_group_id' => 'activity_type',
568 $result = $this->callAPISuccess('CustomField', 'create', $params);
573 * @param $getFieldsResult
577 public function getCustomFieldKeys($getFieldsResult) {
578 $isCustom = function ($key) {
579 return preg_match('/^custom_/', $key);
581 $r = array_values(array_filter(array_keys($getFieldsResult['values']), $isCustom));
586 public function testMakeSearchableContactReferenceFieldUnsearchable() {
587 $customGroup = $this->customGroupCreate(array(
588 'name' => 'testCustomGroup',
589 'title' => 'testCustomGroup',
590 'extends' => 'Individual',
593 'name' => 'testCustomField',
594 'label' => 'testCustomField',
595 'custom_group_id' => 'testCustomGroup',
596 'data_type' => 'ContactReference',
597 'html_type' => 'Autocomplete-Select',
598 'is_searchable' => '1',
600 $result = $this->callAPISuccess('CustomField', 'create', $params);
602 'id' => $result['id'],
603 'is_searchable' => 0,
605 $result = $this->callAPISuccess('CustomField', 'create', $params);
608 public function testDisableSearchableContactReferenceField() {
609 $customGroup = $this->customGroupCreate(array(
610 'name' => 'testCustomGroup',
611 'title' => 'testCustomGroup',
612 'extends' => 'Individual',
615 'name' => 'testCustomField',
616 'label' => 'testCustomField',
617 'custom_group_id' => 'testCustomGroup',
618 'data_type' => 'ContactReference',
619 'html_type' => 'Autocomplete-Select',
620 'is_searchable' => '1',
622 $result = $this->callAPISuccess('CustomField', 'create', $params);
624 'id' => $result['id'],
627 $result = $this->callAPISuccess('CustomField', 'create', $params);