3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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;
41 public $_eNoticeCompliant = TRUE;
44 'name' => 'Custom Field Create',
45 'description' => 'Test all Custom Field Create API methods.',
46 'group' => 'CiviCRM API Tests',
51 $this->_apiversion
= 3;
56 $tablesToTruncate = array(
57 'civicrm_custom_group', 'civicrm_custom_field',
59 // true tells quickCleanup to drop any tables that might have been created in the test
60 $this->quickCleanup($tablesToTruncate, TRUE);
66 function testCustomFieldCreateNoArray() {
69 $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams);
70 $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array');
76 function testCustomFieldCreateWithoutLabel() {
77 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'text_test_group'));
79 'custom_group_id' => $customGroup['id'],
80 'name' => 'test_textfield2',
81 'html_type' => 'Text',
82 'data_type' => 'String',
83 'default_value' => 'abc',
90 $customField = $this->callAPIFailure('custom_field', 'create', $params);
91 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: label');
97 function testCustomFieldCreateWithEdit() {
98 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'text_test_group'));
100 'custom_group_id' => $customGroup['id'],
101 'name' => 'test_textfield2',
103 'html_type' => 'Text',
104 'data_type' => 'String',
105 'default_value' => 'abc',
108 'is_searchable' => 0,
112 $customField = $this->callAPIAndDocument('custom_field', 'create', $params, __FUNCTION__
, __FILE__
);
113 $params['id'] = $customField['id'];
114 $customField = $this->callAPISuccess('custom_field', 'create', $params);
116 $this->assertNotNull($customField['id'], 'in line ' . __LINE__
);
120 * check without groupId
122 function testCustomFieldCreateWithoutGroupID() {
123 $fieldParams = array(
124 'name' => 'test_textfield1',
126 'html_type' => 'Text',
127 'data_type' => 'String',
128 'default_value' => 'abc',
131 'is_searchable' => 0,
136 $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams);
137 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: custom_group_id');
141 * Check for Each data type: loop through available form input types
143 function testCustomFieldCreateAllAvailableFormInputs() {
144 $gid = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'testAllFormInputs'));
146 $dtype = CRM_Core_BAO_CustomField
::dataType();
147 $htype = CRM_Core_BAO_CustomField
::dataToHtml();
150 foreach ($dtype as $dkey => $dvalue) {
151 foreach ($htype[$n] as $hkey => $hvalue) {
152 //echo $dkey."][".$hvalue."\n";
153 $this->_loopingCustomFieldCreateTest($this->_buildParams($gid['id'], $hvalue, $dkey));
159 * Can't figure out the point of this?
161 function _loopingCustomFieldCreateTest($params) {
162 $customField = $this->callAPISuccess('custom_field', 'create', $params);
163 $this->assertNotNull($customField['id']);
164 $this->getAndCheck($params, $customField['id'], 'CustomField');
167 function _buildParams($gid, $htype, $dtype) {
168 $params = $this->_buildBasicParams($gid, $htype, $dtype);
169 /* //Not Working for any type. Maybe redundant with testCustomFieldCreateWithOptionValues()
170 if ($htype == 'Multi-Select')
171 $params = array_merge($params, array(
172 'option_label' => array( 'Label1','Label2'),
173 'option_value' => array( 'val1', 'val2' ),
174 'option_weight' => array( 1, 2),
175 'option_status' => array( 1, 1),
184 function _buildBasicParams($gid, $htype, $dtype) {
186 'custom_group_id' => $gid,
187 'label' => $dtype . $htype,
188 'html_type' => $htype,
189 'data_type' => $dtype,
192 'is_searchable' => 0,
199 * Test using example code
201 /*function testCustomFieldCreateExample( )
205 $customGroup = $this->customGroupCreate('Individual','date_test_group',3);
206 require_once 'api/v3/examples/CustomFieldCreate.php';
207 $result = custom_field_create_example();
208 $expectedResult = custom_field_create_expectedresult();
209 $this->assertEquals($result,$expectedResult);
213 * check with data type - Options with option_values
215 function testCustomFieldCreateWithEmptyOptionGroup() {
216 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'select_test_group'));
218 'custom_group_id' => $customGroup['id'],
219 'label' => 'Country',
220 'html_type' => 'Select',
221 'data_type' => 'String',
224 'is_searchable' => 0,
228 $customField = $this->callAPISuccess('custom_field', 'create', $params);
229 $this->assertNotNull($customField['id']);
230 $optionGroupID = $this->callAPISuccess('custom_field', 'getvalue', array(
231 'id' => $customField['id'],
232 'return' => 'option_group_id',
235 $this->assertTrue(is_numeric($optionGroupID) && ($optionGroupID > 0));
236 $optionGroup = $this->callAPISuccess('option_group', 'getsingle', array(
237 'id' => $optionGroupID));
238 $this->assertEquals($optionGroup['title'],'Country');
239 $optionValueCount = $this->callAPISuccess('option_value', 'getcount', array(
240 'option_group_id' => $optionGroupID));
241 $this->assertEquals(0, $optionValueCount);
246 * Test custom field get works & return param works
248 function testCustomFieldGetReturnOptions(){
249 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
250 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
252 $result = $this->callAPISuccess('custom_field', 'getsingle', array(
253 'id' => $customField['id'],
254 'return' => 'data_type',
256 $this->assertTrue(array_key_exists('data_type', $result));
257 $this->assertFalse(array_key_exists('custom_group_id', $result));
261 * Test custom field get works & return param works
263 function testCustomFieldGetReturnArray(){
264 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
265 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
267 $result = $this->callAPISuccess('custom_field', 'getsingle', array(
268 'id' => $customField['id'],
269 'return' => array('data_type'),
271 $this->assertTrue(array_key_exists('data_type', $result));
272 $this->assertFalse(array_key_exists('custom_group_id', $result));
276 * Test custom field get works & return param works
278 function testCustomFieldGetReturnTwoOptions(){
279 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'test_group'));
280 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
282 $result = $this->callAPISuccess('custom_field', 'getsingle', array(
283 'id' => $customField['id'],
284 'return' => 'data_type, custom_group_id',
286 $this->assertTrue(array_key_exists('data_type', $result));
287 $this->assertTrue(array_key_exists('custom_group_id', $result));
288 $this->assertFalse(array_key_exists('label', $result));
291 function testCustomFieldCreateWithOptionValues() {
292 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'select_test_group'));
294 $option_values = array(
309 'custom_group_id' => $customGroup['id'],
310 'label' => 'Our special field',
311 'html_type' => 'Select',
312 'data_type' => 'String',
315 'is_searchable' => 0,
317 'option_values' => $option_values,
321 $customField = $this->callAPISuccess('custom_field', 'create', $params);
323 $this->assertAPISuccess($customField);
324 $this->assertNotNull($customField['id']);
325 $getFieldsParams = array(
326 'options' => array('get_options' => 'custom_' . $customField['id']),
327 'action' => 'create',
329 $description = "Demonstrate retrieving metadata with custom field options";
330 $subfile = "GetFieldsOptions";
331 $fields = $this->callAPIAndDocument('contact', 'getfields', $getFieldsParams, __FUNCTION__
, 'ContactTest.php', $description,$subfile,'GetFields');
332 $this->assertArrayHasKey('options', $fields['values']['custom_' . $customField['id']]);
333 $this->assertEquals('Label1', $fields['values']['custom_' . $customField['id']]['options'][1]);
334 $getOptionsArray = array(
335 'field' => 'custom_' . $customField['id'],
337 $description = "Demonstrates retrieving options for a custom field";
338 $subfile = "GetOptions";
339 $result = $this->callAPIAndDocument('contact', 'getoptions', $getOptionsArray, __FUNCTION__
, 'ContactTest.php', $description, '', 'getoptions');
340 $this->assertEquals('Label1', $result['values'][1]);
343 ///////////////// civicrm_custom_field_delete methods
346 * check with no array
348 function testCustomFieldDeleteNoArray() {
350 $customField = $this->callAPIFailure('custom_field', 'delete', $params);
351 $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array');
355 * check without Field ID
357 function testCustomFieldDeleteWithoutFieldID() {
359 $customField = $this->callAPIFailure('custom_field', 'delete', $params,
360 'Mandatory key(s) missing from params array: id');
364 * check without valid array
366 function testCustomFieldDelete() {
367 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
368 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
369 $this->assertNotNull($customField['id'], 'in line ' . __LINE__
);
372 'id' => $customField['id'],
374 $result = $this->callAPIAndDocument('custom_field', 'delete', $params, __FUNCTION__
, __FILE__
);
376 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
380 * check for Option Value
382 function testCustomFieldOptionValueDelete() {
383 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'ABC'));
384 $customOptionValueFields = $this->customFieldOptionValueCreate($customGroup, 'fieldABC');
386 'id' => $customOptionValueFields,
389 $customField = $this->callAPISuccess('custom_field', 'delete', $customOptionValueFields);
393 * If there's one custom group for "Contact" and one for "Activity", then "Contact.getfields"
394 * and "Activity.getfields" should return only their respective fields (not the other's fields),
395 * and unrelated entities should return no custom fields.
397 function testGetfields_CrossEntityPollution() {
398 $auxEntities = array('Email', 'Address', 'LocBlock', 'Membership', 'ContributionPage', 'ReportInstance');
399 $allEntities = array_merge(array('Contact', 'Activity'), $auxEntities);
401 // Baseline - getfields doesn't reporting any customfields for any entities
402 foreach ($allEntities as $entity) {
405 $this->getCustomFieldKeys($this->callAPISuccess($entity, 'getfields', array())),
406 "Baseline custom fields for $entity should be empty"
411 $contactGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'test_group'));
412 $contactField = $this->customFieldCreate(array('custom_group_id' => $contactGroup['id'], 'label' => 'For Contacts'));
413 $activityGroup = $this->customGroupCreate(array('extends' => 'Activity', 'title' => 'test_group'));
414 $activityField = $this->customFieldCreate(array('custom_group_id' => $activityGroup['id'], 'label' => 'For Activities'));
416 // getfields reports exactly one custom field for each entity
417 foreach (array('Contact', 'Individual', 'Organization') as $entity) {
419 array('custom_' . $contactField['id']),
420 $this->getCustomFieldKeys($this->callAPISuccess($entity, 'getfields', array())),
421 'Contact custom fields'
425 array('custom_' . $activityField['id']),
426 $this->getCustomFieldKeys($this->callAPISuccess('Activity', 'getfields', array())),
427 'Activity custom fields'
429 foreach ($auxEntities as $entity) {
432 $this->getCustomFieldKeys($this->callAPISuccess($entity, 'getfields', array())),
433 "Custom fields for $entity should be empty"
438 function getCustomFieldKeys($getFieldsResult) {
439 $isCustom = function($key) {
440 return preg_match('/^custom_/', $key);
442 return array_values(array_filter(array_keys($getFieldsResult['values']), $isCustom));