3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 * Include class definitions
31 require_once 'tests/phpunit/CiviTest/CiviUnitTestCase.php';
35 * Test APIv3 civicrm_create_custom_group
39 class api_v3_CustomFieldTest
extends CiviUnitTestCase
{
40 protected $_apiversion;
47 'name' => 'Custom Field Create',
48 'description' => 'Test all Custom Field Create API methods.',
49 'group' => 'CiviCRM API Tests',
54 $this->_apiversion
= 3;
59 $tablesToTruncate = array(
60 'civicrm_custom_group', 'civicrm_custom_field',
62 // true tells quickCleanup to drop any tables that might have been created in the test
63 $this->quickCleanup($tablesToTruncate, TRUE);
69 function testCustomFieldCreateNoArray() {
72 $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams);
73 $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array');
79 function testCustomFieldCreateWithoutLabel() {
80 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'text_test_group'));
82 'custom_group_id' => $customGroup['id'],
83 'name' => 'test_textfield2',
84 'html_type' => 'Text',
85 'data_type' => 'String',
86 'default_value' => 'abc',
93 $customField = $this->callAPIFailure('custom_field', 'create', $params);
94 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: label');
100 function testCustomFieldCreateWithEdit() {
101 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'text_test_group'));
103 'custom_group_id' => $customGroup['id'],
104 'name' => 'test_textfield2',
106 'html_type' => 'Text',
107 'data_type' => 'String',
108 'default_value' => 'abc',
111 'is_searchable' => 0,
115 $customField = $this->callAPIAndDocument('custom_field', 'create', $params, __FUNCTION__
, __FILE__
);
116 $params['id'] = $customField['id'];
117 $customField = $this->callAPISuccess('custom_field', 'create', $params);
119 $this->assertNotNull($customField['id'], 'in line ' . __LINE__
);
123 * Check without groupId
125 function testCustomFieldCreateWithoutGroupID() {
126 $fieldParams = array(
127 'name' => 'test_textfield1',
129 'html_type' => 'Text',
130 'data_type' => 'String',
131 'default_value' => 'abc',
134 'is_searchable' => 0,
139 $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams);
140 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: custom_group_id');
144 * Check for Each data type: loop through available form input types
146 function testCustomFieldCreateAllAvailableFormInputs() {
147 $gid = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'testAllFormInputs'));
149 $dtype = CRM_Core_BAO_CustomField
::dataType();
150 $htype = CRM_Core_BAO_CustomField
::dataToHtml();
153 foreach ($dtype as $dkey => $dvalue) {
154 foreach ($htype[$n] as $hkey => $hvalue) {
155 //echo $dkey."][".$hvalue."\n";
156 $this->_loopingCustomFieldCreateTest($this->_buildParams($gid['id'], $hvalue, $dkey));
162 * Can't figure out the point of this?
165 * @param array $params
167 function _loopingCustomFieldCreateTest($params) {
168 $customField = $this->callAPISuccess('custom_field', 'create', $params);
169 $this->assertNotNull($customField['id']);
170 $this->getAndCheck($params, $customField['id'], 'CustomField');
180 function _buildParams($gid, $htype, $dtype) {
181 $params = $this->_buildBasicParams($gid, $htype, $dtype);
182 /* //Not Working for any type. Maybe redundant with testCustomFieldCreateWithOptionValues()
183 if ($htype == 'Multi-Select')
184 $params = array_merge($params, array(
185 'option_label' => array( 'Label1','Label2'),
186 'option_value' => array( 'val1', 'val2' ),
187 'option_weight' => array( 1, 2),
188 'option_status' => array( 1, 1),
204 function _buildBasicParams($gid, $htype, $dtype) {
206 'custom_group_id' => $gid,
207 'label' => $dtype . $htype,
208 'html_type' => $htype,
209 'data_type' => $dtype,
212 'is_searchable' => 0,
219 * Test using example code
221 /*function testCustomFieldCreateExample( )
225 $customGroup = $this->customGroupCreate('Individual','date_test_group',3);
226 require_once 'api/v3/examples/CustomField/Create.php';
227 $result = custom_field_create_example();
228 $expectedResult = custom_field_create_expectedresult();
229 $this->assertEquals($result,$expectedResult);
233 * Check with data type - Options with option_values
235 function testCustomFieldCreateWithEmptyOptionGroup() {
236 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'select_test_group'));
238 'custom_group_id' => $customGroup['id'],
239 'label' => 'Country',
240 'html_type' => 'Select',
241 'data_type' => 'String',
244 'is_searchable' => 0,
248 $customField = $this->callAPISuccess('custom_field', 'create', $params);
249 $this->assertNotNull($customField['id']);
250 $optionGroupID = $this->callAPISuccess('custom_field', 'getvalue', array(
251 'id' => $customField['id'],
252 'return' => 'option_group_id',
255 $this->assertTrue(is_numeric($optionGroupID) && ($optionGroupID > 0));
256 $optionGroup = $this->callAPISuccess('option_group', 'getsingle', array(
257 'id' => $optionGroupID));
258 $this->assertEquals($optionGroup['title'],'Country');
259 $optionValueCount = $this->callAPISuccess('option_value', 'getcount', array(
260 'option_group_id' => $optionGroupID));
261 $this->assertEquals(0, $optionValueCount);
265 * Test custom field with existing option group
267 function testCustomFieldExistingOptionGroup() {
268 $customGroup = $this->customGroupCreate(array('extends' => 'Organization', 'title' => 'test_group'));
270 'custom_group_id' => $customGroup['id'],
271 // Just to say something:
272 'label' => 'Organization Gender',
273 'html_type' => 'Select',
274 'data_type' => 'Int',
277 'is_searchable' => 0,
279 // Option group id 3: gender
280 'option_group_id' => 3,
283 $customField = $this->callAPISuccess('custom_field', 'create', $params);
284 $this->assertNotNull($customField['id']);
285 $optionGroupID = $this->callAPISuccess('custom_field', 'getvalue', array(
286 'id' => $customField['id'],
287 'return' => 'option_group_id',
290 $this->assertEquals($optionGroupID,3);
295 * Test custom field get works & return param works
297 function testCustomFieldGetReturnOptions(){
298 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
299 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
301 $result = $this->callAPISuccess('custom_field', 'getsingle', array(
302 'id' => $customField['id'],
303 'return' => 'data_type',
305 $this->assertTrue(array_key_exists('data_type', $result));
306 $this->assertFalse(array_key_exists('custom_group_id', $result));
310 * Test custom field get works & return param works
312 function testCustomFieldGetReturnArray(){
313 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
314 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
316 $result = $this->callAPISuccess('custom_field', 'getsingle', array(
317 'id' => $customField['id'],
318 'return' => array('data_type'),
320 $this->assertTrue(array_key_exists('data_type', $result));
321 $this->assertFalse(array_key_exists('custom_group_id', $result));
325 * Test custom field get works & return param works
327 function testCustomFieldGetReturnTwoOptions(){
328 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'test_group'));
329 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
331 $result = $this->callAPISuccess('custom_field', 'getsingle', array(
332 'id' => $customField['id'],
333 'return' => 'data_type, custom_group_id',
335 $this->assertTrue(array_key_exists('data_type', $result));
336 $this->assertTrue(array_key_exists('custom_group_id', $result));
337 $this->assertFalse(array_key_exists('label', $result));
340 function testCustomFieldCreateWithOptionValues() {
341 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'select_test_group'));
343 $option_values = array(
358 'custom_group_id' => $customGroup['id'],
359 'label' => 'Our special field',
360 'html_type' => 'Select',
361 'data_type' => 'String',
364 'is_searchable' => 0,
366 'option_values' => $option_values,
370 $customField = $this->callAPISuccess('custom_field', 'create', $params);
372 $this->assertAPISuccess($customField);
373 $this->assertNotNull($customField['id']);
374 $getFieldsParams = array(
375 'options' => array('get_options' => 'custom_' . $customField['id']),
376 'action' => 'create',
378 $description = "Demonstrate retrieving metadata with custom field options";
379 $subfile = "GetFieldsOptions";
380 $fields = $this->callAPIAndDocument('contact', 'getfields', $getFieldsParams, __FUNCTION__
, 'ContactTest.php', $description,$subfile,'GetFields');
381 $this->assertArrayHasKey('options', $fields['values']['custom_' . $customField['id']]);
382 $this->assertEquals('Label1', $fields['values']['custom_' . $customField['id']]['options'][1]);
383 $getOptionsArray = array(
384 'field' => 'custom_' . $customField['id'],
386 $description = "Demonstrates retrieving options for a custom field";
387 $subfile = "GetOptions";
388 $result = $this->callAPIAndDocument('contact', 'getoptions', $getOptionsArray, __FUNCTION__
, 'ContactTest.php', $description, '', 'getoptions');
389 $this->assertEquals('Label1', $result['values'][1]);
392 ///////////////// civicrm_custom_field_delete methods
395 * Check with no array
397 function testCustomFieldDeleteNoArray() {
399 $customField = $this->callAPIFailure('custom_field', 'delete', $params);
400 $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array');
404 * Check without Field ID
406 function testCustomFieldDeleteWithoutFieldID() {
408 $customField = $this->callAPIFailure('custom_field', 'delete', $params,
409 'Mandatory key(s) missing from params array: id');
413 * Check without valid array
415 function testCustomFieldDelete() {
416 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
417 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
418 $this->assertNotNull($customField['id'], 'in line ' . __LINE__
);
421 'id' => $customField['id'],
423 $result = $this->callAPIAndDocument('custom_field', 'delete', $params, __FUNCTION__
, __FILE__
);
425 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
429 * Check for Option Value
431 function testCustomFieldOptionValueDelete() {
432 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'ABC'));
433 $customOptionValueFields = $this->customFieldOptionValueCreate($customGroup, 'fieldABC');
435 'id' => $customOptionValueFields,
438 $customField = $this->callAPISuccess('custom_field', 'delete', $customOptionValueFields);
442 * If there's one custom group for "Contact" and one for "Activity", then "Contact.getfields"
443 * and "Activity.getfields" should return only their respective fields (not the other's fields),
444 * and unrelated entities should return no custom fields.
446 function testGetfields_CrossEntityPollution() {
447 $auxEntities = array('Email', 'Address', 'LocBlock', 'Membership', 'ContributionPage', 'ReportInstance');
448 $allEntities = array_merge(array('Contact', 'Activity'), $auxEntities);
450 // Baseline - getfields doesn't reporting any customfields for any entities
451 foreach ($allEntities as $entity) {
454 $this->getCustomFieldKeys($this->callAPISuccess($entity, 'getfields', array())),
455 "Baseline custom fields for $entity should be empty"
460 $contactGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'test_group_c'));
461 $contactField = $this->customFieldCreate(array('custom_group_id' => $contactGroup['id'], 'label' => 'For Contacts'));
462 $indivGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group_i'));
463 $indivField = $this->customFieldCreate(array('custom_group_id' => $indivGroup['id'], 'label' => 'For Individuals'));
464 $activityGroup = $this->customGroupCreate(array('extends' => 'Activity', 'title' => 'test_group_a'));
465 $activityField = $this->customFieldCreate(array('custom_group_id' => $activityGroup['id'], 'label' => 'For Activities'));
469 array('custom_' . $contactField['id'], 'custom_' . $indivField['id']),
470 $this->getCustomFieldKeys($this->callAPISuccess('Contact', 'getfields', array())),
471 'Contact custom fields'
474 array('custom_' . $contactField['id'], 'custom_' . $indivField['id']),
475 $this->getCustomFieldKeys($this->callAPISuccess('Individual', 'getfields', array())),
476 'Individual custom fields'
479 array('custom_' . $contactField['id']),
480 $this->getCustomFieldKeys($this->callAPISuccess('Organization', 'getfields', array())),
481 'Organization custom fields'
484 array('custom_' . $activityField['id']),
485 $this->getCustomFieldKeys($this->callAPISuccess('Activity', 'getfields', array())),
486 'Activity custom fields'
488 foreach ($auxEntities as $entity) {
491 $this->getCustomFieldKeys($this->callAPISuccess($entity, 'getfields', array())),
492 "Custom fields for $entity should be empty"
498 * @param $getFieldsResult
502 function getCustomFieldKeys($getFieldsResult) {
503 $isCustom = function($key) {
504 return preg_match('/^custom_/', $key);
506 $r = array_values(array_filter(array_keys($getFieldsResult['values']), $isCustom));