4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
30 * Include class definitions
32 require_once 'tests/phpunit/CiviTest/CiviUnitTestCase.php';
36 * Test APIv3 civicrm_create_custom_group
40 class api_v3_CustomFieldTest
extends CiviUnitTestCase
{
41 protected $_apiversion;
42 public $_eNoticeCompliant = TRUE;
45 'name' => 'Custom Field Create',
46 'description' => 'Test all Custom Field Create API methods.',
47 'group' => 'CiviCRM API Tests',
52 $this->_apiversion
= 3;
57 $tablesToTruncate = array(
58 'civicrm_custom_group', 'civicrm_custom_field',
60 // true tells quickCleanup to drop any tables that might have been created in the test
61 $this->quickCleanup($tablesToTruncate, TRUE);
67 function testCustomFieldCreateNoArray() {
70 $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams);
71 $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array');
77 function testCustomFieldCreateWithoutLabel() {
78 $customGroup = $this->customGroupCreate('Individual', 'text_test_group', 3);
80 'custom_group_id' => $customGroup['id'],
81 'name' => 'test_textfield2',
82 'html_type' => 'Text',
83 'data_type' => 'String',
84 'default_value' => 'abc',
89 'version' => $this->_apiversion
,
92 $customField = $this->callAPIFailure('custom_field', 'create', $params);
93 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: label');
99 function testCustomFieldCreateWithEdit() {
100 $customGroup = $this->customGroupCreate('Individual', 'text_test_group', 3);
102 'custom_group_id' => $customGroup['id'],
103 'name' => 'test_textfield2',
105 'html_type' => 'Text',
106 'data_type' => 'String',
107 'default_value' => 'abc',
110 'is_searchable' => 0,
112 'version' => $this->_apiversion
,
115 $customField = civicrm_api('custom_field', 'create', $params);
116 $params['id'] = $customField['id'];
117 $customField = civicrm_api('custom_field', 'create', $params);
119 $this->assertEquals($customField['is_error'], 0, 'in line ' . __LINE__
);
120 $this->assertNotNull($customField['id'], 'in line ' . __LINE__
);
124 * check without groupId
126 function testCustomFieldCreateWithoutGroupID() {
127 $fieldParams = array(
128 'name' => 'test_textfield1',
130 'html_type' => 'Text',
131 'data_type' => 'String',
132 'default_value' => 'abc',
135 'is_searchable' => 0,
137 'version' => $this->_apiversion
,
140 $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams);
141 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: custom_group_id');
145 * Check for Each data type: loop through available form input types
147 function testCustomFieldCreateAllAvailableFormInputs() {
148 $gid = $this->customGroupCreate('Individual', 'testAllFormInputs');
150 $dtype = CRM_Core_BAO_CustomField
::dataType();
151 $htype = CRM_Core_BAO_CustomField
::dataToHtml();
154 foreach ($dtype as $dkey => $dvalue) {
155 foreach ($htype[$n] as $hkey => $hvalue) {
156 //echo $dkey."][".$hvalue."\n";
157 $this->_loopingCustomFieldCreateTest($this->_buildParams($gid['id'], $hvalue, $dkey));
163 * Can't figure out the point of this?
165 function _loopingCustomFieldCreateTest($params) {
166 $customField = civicrm_api('custom_field', 'create', $params);
167 $this->assertEquals(0, $customField['is_error'], var_export($customField, TRUE));
168 $this->assertNotNull($customField['id']);
169 $this->getAndCheck($params, $customField['id'], 'CustomField');
172 function _buildParams($gid, $htype, $dtype) {
173 $params = $this->_buildBasicParams($gid, $htype, $dtype);
174 /* //Not Working for any type. Maybe redundant with testCustomFieldCreateWithOptionValues()
175 if ($htype == 'Multi-Select')
176 $params = array_merge($params, array(
177 'option_label' => array( 'Label1','Label2'),
178 'option_value' => array( 'val1', 'val2' ),
179 'option_weight' => array( 1, 2),
180 'option_status' => array( 1, 1),
189 function _buildBasicParams($gid, $htype, $dtype) {
191 'custom_group_id' => $gid,
192 'label' => $dtype . $htype,
193 'html_type' => $htype,
194 'data_type' => $dtype,
197 'is_searchable' => 0,
199 'version' => $this->_apiversion
,
204 * Test using example code
206 /*function testCustomFieldCreateExample( )
210 $customGroup = $this->customGroupCreate('Individual','date_test_group',3);
211 require_once 'api/v3/examples/CustomFieldCreate.php';
212 $result = custom_field_create_example();
213 $expectedResult = custom_field_create_expectedresult();
214 $this->assertEquals($result,$expectedResult);
218 * check with data type - Options with option_values
220 function testCustomFieldCreateWithEmptyOptionGroup() {
221 $customGroup = $this->customGroupCreate('Contact', 'select_test_group', 3);
223 'custom_group_id' => $customGroup['id'],
224 'label' => 'Country',
225 'html_type' => 'Select',
226 'data_type' => 'String',
229 'is_searchable' => 0,
231 'version' => $this->_apiversion
,
234 $customField = civicrm_api('custom_field', 'create', $params);
235 $this->assertAPISuccess($customField);
236 $this->assertNotNull($customField['id']);
237 $optionGroupID = civicrm_api('custom_field', 'getvalue', array(
239 'id' => $customField['id'],
240 'return' => 'option_group_id',
243 $this->assertTrue(is_numeric($optionGroupID) && ($optionGroupID > 0));
244 $optionGroup = civicrm_api('option_group', 'getsingle', array(
245 'version' => 3, 'id' => $optionGroupID));
246 $this->assertEquals($optionGroup['title'],'Country');
247 $optionValueCount = civicrm_api('option_value', 'getcount', array(
248 'version' => 3, 'option_group_id' => $optionGroupID));
249 $this->assertEquals(0, $optionValueCount);
254 * Test custom field get works & return param works
256 function testCustomFieldGetReturnOptions(){
257 $customGroup = $this->customGroupCreate('Individual', 'test_group');
258 $customField = $this->customFieldCreate($customGroup['id'], 'test_name');
260 $result = civicrm_api('custom_field', 'getsingle', array(
262 'id' => $customField['id'],
263 'return' => 'data_type',
265 $this->assertTrue(array_key_exists('data_type', $result));
266 $this->assertFalse(array_key_exists('custom_group_id', $result));
270 * Test custom field get works & return param works
272 function testCustomFieldGetReturnArray(){
273 $customGroup = $this->customGroupCreate('Individual', 'test_group');
274 $customField = $this->customFieldCreate($customGroup['id'], 'test_name');
276 $result = civicrm_api('custom_field', 'getsingle', array(
278 'id' => $customField['id'],
279 'return' => array('data_type'),
281 $this->assertTrue(array_key_exists('data_type', $result));
282 $this->assertFalse(array_key_exists('custom_group_id', $result));
286 * Test custom field get works & return param works
288 function testCustomFieldGetReturnTwoOptions(){
289 $customGroup = $this->customGroupCreate('Individual', 'test_group');
290 $customField = $this->customFieldCreate($customGroup['id'], 'test_name');
292 $result = civicrm_api('custom_field', 'getsingle', array(
294 'id' => $customField['id'],
295 'return' => 'data_type, custom_group_id',
297 $this->assertTrue(array_key_exists('data_type', $result));
298 $this->assertTrue(array_key_exists('custom_group_id', $result));
299 $this->assertFalse(array_key_exists('label', $result));
302 function testCustomFieldCreateWithOptionValues() {
303 $customGroup = $this->customGroupCreate('Contact', 'select_test_group', 3);
305 $option_values = array(
320 'custom_group_id' => $customGroup['id'],
321 'label' => 'Our special field',
322 'html_type' => 'Select',
323 'data_type' => 'String',
326 'is_searchable' => 0,
328 'option_values' => $option_values,
329 'version' => $this->_apiversion
,
332 $customField = civicrm_api('custom_field', 'create', $params);
334 $this->assertAPISuccess($customField);
335 $this->assertNotNull($customField['id']);
336 $getFieldsParams = array(
337 'options' => array('get_options' => 'custom_' . $customField['id']),
339 'action' => 'create',
341 $description = "Demonstrate retrieving metadata with custom field options";
342 $subfile = "GetFieldsOptions";
343 $fields = civicrm_api('contact', 'getfields', $getFieldsParams);
344 $this->documentMe($getFieldsParams, $fields, __FUNCTION__
, 'ContactTest.php', $description,$subfile,'GetFields');
345 $this->assertArrayHasKey('options', $fields['values']['custom_' . $customField['id']]);
346 $this->assertEquals('Label1', $fields['values']['custom_' . $customField['id']]['options'][1]);
347 $getOptionsArray = array(
348 'field' => 'custom_' . $customField['id'],
351 $description = "Demonstrates retrieving options for a custom field";
352 $subfile = "GetOptions";
353 $result = civicrm_api('contact', 'getoptions', $getOptionsArray);
354 $this->assertEquals('Label1', $result['values'][1]);
355 $this->documentMe($getOptionsArray, $result, __FUNCTION__
, 'ContactTest.php', $description, '', 'getoptions');
358 ///////////////// civicrm_custom_field_delete methods
361 * check with no array
363 function testCustomFieldDeleteNoArray() {
365 $customField = $this->callAPIFailure('custom_field', 'delete', $params);
366 $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array');
370 * check without Field ID
372 function testCustomFieldDeleteWithoutFieldID() {
373 $params = array('version' => $this->_apiversion
);
374 $customField = $this->callAPIFailure('custom_field', 'delete', $params);
375 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: id');
379 * check without valid array
381 function testCustomFieldDelete() {
382 $customGroup = $this->customGroupCreate('Individual', 'test_group');
383 $customField = $this->customFieldCreate($customGroup['id'], 'test_name');
384 $this->assertNotNull($customField['id'], 'in line ' . __LINE__
);
387 'version' => $this->_apiversion
,
388 'id' => $customField['id'],
390 $result = civicrm_api('custom_field', 'delete', $params);
391 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
393 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
397 * check for Option Value
399 function testCustomFieldOptionValueDelete() {
400 $customGroup = $this->customGroupCreate('Contact', 'ABC');
401 $customOptionValueFields = $this->customFieldOptionValueCreate($customGroup, 'fieldABC');
402 $customOptionValueFields['version'] = $this->_apiversion
;
404 'version' => $this->_apiversion
,
405 'id' => $customOptionValueFields,
408 $customField = civicrm_api('custom_field', 'delete', $customOptionValueFields);
409 $this->assertAPISuccess($customField);