3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
33 class api_v3_CustomFieldTest
extends CiviUnitTestCase
{
34 protected $_apiversion;
36 public function setUp() {
37 $this->_apiversion
= 3;
41 public function tearDown() {
42 $tablesToTruncate = array(
45 'civicrm_entity_file',
47 // true tells quickCleanup to drop custom_value tables that might have been created in the test
48 $this->quickCleanup($tablesToTruncate, TRUE);
52 * Check with no array.
54 public function testCustomFieldCreateNoArray() {
57 $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams);
58 $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array');
62 * Check with no label.
64 public function testCustomFieldCreateWithoutLabel() {
65 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'text_test_group'));
67 'custom_group_id' => $customGroup['id'],
68 'name' => 'test_textfield2',
69 'html_type' => 'Text',
70 'data_type' => 'String',
71 'default_value' => 'abc',
78 $customField = $this->callAPIFailure('custom_field', 'create', $params);
79 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: label');
85 public function testCustomFieldCreateWithEdit() {
86 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'text_test_group'));
88 'custom_group_id' => $customGroup['id'],
89 'name' => 'test_textfield2',
91 'html_type' => 'Text',
92 'data_type' => 'String',
93 'default_value' => 'abc',
100 $customField = $this->callAPIAndDocument('custom_field', 'create', $params, __FUNCTION__
, __FILE__
);
101 $params['id'] = $customField['id'];
102 $customField = $this->callAPISuccess('custom_field', 'create', $params);
104 $this->assertNotNull($customField['id']);
108 * Check without groupId.
110 public function testCustomFieldCreateWithoutGroupID() {
111 $fieldParams = array(
112 'name' => 'test_textfield1',
114 'html_type' => 'Text',
115 'data_type' => 'String',
116 'default_value' => 'abc',
119 'is_searchable' => 0,
124 $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams);
125 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: custom_group_id');
129 * Check for Each data type: loop through available form input types
131 public function testCustomFieldCreateAllAvailableFormInputs() {
132 $gid = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'testAllFormInputs'));
134 $dtype = CRM_Core_BAO_CustomField
::dataType();
135 $htype = CRM_Core_BAO_CustomField
::dataToHtml();
138 foreach ($dtype as $dkey => $dvalue) {
139 foreach ($htype[$n] as $hkey => $hvalue) {
140 //echo $dkey."][".$hvalue."\n";
141 $this->_loopingCustomFieldCreateTest($this->_buildParams($gid['id'], $hvalue, $dkey));
147 * Can't figure out the point of this?
150 * @param array $params
152 public function _loopingCustomFieldCreateTest($params) {
153 $customField = $this->callAPISuccess('custom_field', 'create', $params);
154 $this->assertNotNull($customField['id']);
155 $this->getAndCheck($params, $customField['id'], 'CustomField');
165 public function _buildParams($gid, $htype, $dtype) {
166 $params = $this->_buildBasicParams($gid, $htype, $dtype);
167 /* //Not Working for any type. Maybe redundant with testCustomFieldCreateWithOptionValues()
168 if ($htype == 'Multi-Select')
169 $params = array_merge($params, array(
170 'option_label' => array( 'Label1','Label2'),
171 'option_value' => array( 'val1', 'val2' ),
172 'option_weight' => array( 1, 2),
173 'option_status' => array( 1, 1),
187 public function _buildBasicParams($gid, $htype, $dtype) {
189 'custom_group_id' => $gid,
190 'label' => $dtype . $htype,
191 'html_type' => $htype,
192 'data_type' => $dtype,
195 'is_searchable' => 0,
202 * Test using example code.
204 /*function testCustomFieldCreateExample( )
207 $customGroup = $this->customGroupCreate('Individual','date_test_group',3);
208 require_once 'api/v3/examples/CustomField/Create.php';
209 $result = custom_field_create_example();
210 $expectedResult = custom_field_create_expectedresult();
211 $this->assertEquals($result,$expectedResult);
215 * Check with data type - Options with option_values
217 public function testCustomFieldCreateWithEmptyOptionGroup() {
218 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'select_test_group'));
220 'custom_group_id' => $customGroup['id'],
221 'label' => 'Country',
222 'html_type' => 'Select',
223 'data_type' => 'String',
226 'is_searchable' => 0,
230 $customField = $this->callAPISuccess('custom_field', 'create', $params);
231 $this->assertNotNull($customField['id']);
232 $optionGroupID = $this->callAPISuccess('custom_field', 'getvalue', array(
233 'id' => $customField['id'],
234 'return' => 'option_group_id',
237 $this->assertTrue(is_numeric($optionGroupID) && ($optionGroupID > 0));
238 $optionGroup = $this->callAPISuccess('option_group', 'getsingle', array(
239 'id' => $optionGroupID,
241 $this->assertEquals($optionGroup['title'], 'Country');
242 $optionValueCount = $this->callAPISuccess('option_value', 'getcount', array(
243 'option_group_id' => $optionGroupID,
245 $this->assertEquals(0, $optionValueCount);
249 * Test custom field with existing option group.
251 public function testCustomFieldExistingOptionGroup() {
252 $customGroup = $this->customGroupCreate(array('extends' => 'Organization', 'title' => 'test_group'));
254 'custom_group_id' => $customGroup['id'],
255 // Just to say something:
256 'label' => 'Organization Gender',
257 'html_type' => 'Select',
258 'data_type' => 'Int',
261 'is_searchable' => 0,
263 // Option group id 3: gender
264 'option_group_id' => 3,
267 $customField = $this->callAPISuccess('custom_field', 'create', $params);
268 $this->assertNotNull($customField['id']);
269 $optionGroupID = $this->callAPISuccess('custom_field', 'getvalue', array(
270 'id' => $customField['id'],
271 'return' => 'option_group_id',
274 $this->assertEquals($optionGroupID, 3);
278 * Test custom field get works & return param works
280 public function testCustomFieldGetReturnOptions() {
281 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
282 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
284 $result = $this->callAPISuccess('custom_field', 'getsingle', array(
285 'id' => $customField['id'],
286 'return' => 'data_type',
288 $this->assertTrue(array_key_exists('data_type', $result));
289 $this->assertFalse(array_key_exists('custom_group_id', $result));
293 * Test custom field get works & return param works
295 public function testCustomFieldGetReturnArray() {
296 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
297 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
299 $result = $this->callAPISuccess('custom_field', 'getsingle', array(
300 'id' => $customField['id'],
301 'return' => array('data_type'),
303 $this->assertTrue(array_key_exists('data_type', $result));
304 $this->assertFalse(array_key_exists('custom_group_id', $result));
308 * Test custom field get works & return param works
310 public function testCustomFieldGetReturnTwoOptions() {
311 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'test_group'));
312 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
314 $result = $this->callAPISuccess('custom_field', 'getsingle', array(
315 'id' => $customField['id'],
316 'return' => 'data_type, custom_group_id',
318 $this->assertTrue(array_key_exists('data_type', $result));
319 $this->assertTrue(array_key_exists('custom_group_id', $result));
320 $this->assertFalse(array_key_exists('label', $result));
323 public function testCustomFieldCreateWithOptionValues() {
324 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'select_test_group'));
326 $option_values = array(
342 'custom_group_id' => $customGroup['id'],
343 'label' => 'Our special field',
344 'html_type' => 'Select',
345 'data_type' => 'String',
348 'is_searchable' => 0,
350 'option_values' => $option_values,
354 $customField = $this->callAPISuccess('custom_field', 'create', $params);
356 $this->assertAPISuccess($customField);
357 $this->assertNotNull($customField['id']);
358 $getFieldsParams = array(
359 'options' => array('get_options' => 'custom_' . $customField['id']),
360 'action' => 'create',
362 $description = "Demonstrates retrieving metadata with custom field options.";
363 $subfile = "GetFieldsOptions";
364 $fields = $this->callAPIAndDocument('contact', 'getfields', $getFieldsParams, __FUNCTION__
, 'ContactTest.php', $description, $subfile);
365 $this->assertArrayHasKey('options', $fields['values']['custom_' . $customField['id']]);
366 $this->assertEquals('Label1', $fields['values']['custom_' . $customField['id']]['options'][1]);
367 $getOptionsArray = array(
368 'field' => 'custom_' . $customField['id'],
370 $description = "Demonstrates retrieving options for a custom field.";
371 $subfile = "GetOptions";
372 $result = $this->callAPIAndDocument('contact', 'getoptions', $getOptionsArray, __FUNCTION__
, 'ContactTest.php', $description, '');
373 $this->assertEquals('Label1', $result['values'][1]);
376 ///////////////// civicrm_custom_field_delete methods
379 * Check with no array.
381 public function testCustomFieldDeleteNoArray() {
383 $customField = $this->callAPIFailure('custom_field', 'delete', $params);
384 $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array');
388 * Check without Field ID.
390 public function testCustomFieldDeleteWithoutFieldID() {
392 $customField = $this->callAPIFailure('custom_field', 'delete', $params,
393 'Mandatory key(s) missing from params array: id');
397 * Check without valid array.
399 public function testCustomFieldDelete() {
400 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
401 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
402 $this->assertNotNull($customField['id']);
405 'id' => $customField['id'],
407 $result = $this->callAPIAndDocument('custom_field', 'delete', $params, __FUNCTION__
, __FILE__
);
409 $this->assertAPISuccess($result);
413 * Check for Option Value.
415 public function testCustomFieldOptionValueDelete() {
416 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'ABC'));
417 $customOptionValueFields = $this->customFieldOptionValueCreate($customGroup, 'fieldABC');
419 'id' => $customOptionValueFields,
422 $customField = $this->callAPISuccess('custom_field', 'delete', $customOptionValueFields);
426 * If there's one custom group for "Contact" and one for "Activity", then "Contact.getfields"
427 * and "Activity.getfields" should return only their respective fields (not the other's fields),
428 * and unrelated entities should return no custom fields.
430 public function testGetfields_CrossEntityPollution() {
431 $auxEntities = array('Email', 'Address', 'LocBlock', 'Membership', 'ContributionPage', 'ReportInstance');
432 $allEntities = array_merge(array('Contact', 'Activity'), $auxEntities);
434 // Baseline - getfields doesn't reporting any customfields for any entities
435 foreach ($allEntities as $entity) {
438 $this->getCustomFieldKeys($this->callAPISuccess($entity, 'getfields', array())),
439 "Baseline custom fields for $entity should be empty"
444 $contactGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'test_group_c'));
445 $contactField = $this->customFieldCreate(array(
446 'custom_group_id' => $contactGroup['id'],
447 'label' => 'For Contacts',
449 $indivGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group_i'));
450 $indivField = $this->customFieldCreate(array('custom_group_id' => $indivGroup['id'], 'label' => 'For Individuals'));
451 $activityGroup = $this->customGroupCreate(array('extends' => 'Activity', 'title' => 'test_group_a'));
452 $activityField = $this->customFieldCreate(array(
453 'custom_group_id' => $activityGroup['id'],
454 'label' => 'For Activities',
459 array('custom_' . $contactField['id'], 'custom_' . $indivField['id']),
460 $this->getCustomFieldKeys($this->callAPISuccess('Contact', 'getfields', array())),
461 'Contact custom fields'
464 array('custom_' . $contactField['id'], 'custom_' . $indivField['id']),
465 $this->getCustomFieldKeys($this->callAPISuccess('Individual', 'getfields', array())),
466 'Individual custom fields'
469 array('custom_' . $contactField['id']),
470 $this->getCustomFieldKeys($this->callAPISuccess('Organization', 'getfields', array())),
471 'Organization custom fields'
474 array('custom_' . $activityField['id']),
475 $this->getCustomFieldKeys($this->callAPISuccess('Activity', 'getfields', array())),
476 'Activity custom fields'
478 foreach ($auxEntities as $entity) {
481 $this->getCustomFieldKeys($this->callAPISuccess($entity, 'getfields', array())),
482 "Custom fields for $entity should be empty"
488 * Test setting and getting a custom file field value.
490 * Uses the "attachment" api for setting value.
492 public function testCustomFileField() {
493 $customGroup = $this->customGroupCreate(array('title' => 'attachment_test_group'));
495 'custom_group_id' => $customGroup['id'],
496 'name' => 'test_file_attachment',
497 'label' => 'test_file_attachment',
498 'html_type' => 'File',
499 'data_type' => 'File',
502 $customField = $this->callAPISuccess('custom_field', 'create', $params);
503 $cfId = 'custom_' . $customField['id'];
505 $cid = $this->individualCreate();
507 $attachment = $this->callAPISuccess('attachment', 'create', array(
508 'name' => CRM_Utils_String
::createRandom(5, CRM_Utils_String
::ALPHANUMERIC
) . '_testCustomFileField.txt',
509 'mime_type' => 'text/plain',
510 'content' => 'My test content',
511 'field_name' => $cfId,
514 $this->assertAttachmentExistence(TRUE, $attachment);
516 $result = $this->callAPISuccess('contact', 'getsingle', array(
521 $this->assertEquals($attachment['id'], $result[$cfId]);
525 * @param $getFieldsResult
529 public function getCustomFieldKeys($getFieldsResult) {
530 $isCustom = function ($key) {
531 return preg_match('/^custom_/', $key);
533 $r = array_values(array_filter(array_keys($getFieldsResult['values']), $isCustom));