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 = civicrm_api('custom_field', 'create', $fieldParams);
71 $this->assertEquals($customField['is_error'], 1);
72 $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array');
78 function testCustomFieldCreateWithoutLabel() {
79 $customGroup = $this->customGroupCreate('Individual', 'text_test_group', 3);
81 'custom_group_id' => $customGroup['id'],
82 'name' => 'test_textfield2',
83 'html_type' => 'Text',
84 'data_type' => 'String',
85 'default_value' => 'abc',
90 'version' => $this->_apiversion
,
93 $customField = civicrm_api('custom_field', 'create', $params);
94 $this->assertEquals($customField['is_error'], 1);
95 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: label');
101 function testCustomFieldCreateWithEdit() {
102 $customGroup = $this->customGroupCreate('Individual', 'text_test_group', 3);
104 'custom_group_id' => $customGroup['id'],
105 'name' => 'test_textfield2',
107 'html_type' => 'Text',
108 'data_type' => 'String',
109 'default_value' => 'abc',
112 'is_searchable' => 0,
114 'version' => $this->_apiversion
,
117 $customField = civicrm_api('custom_field', 'create', $params);
118 $params['id'] = $customField['id'];
119 $customField = civicrm_api('custom_field', 'create', $params);
121 $this->assertEquals($customField['is_error'], 0, 'in line ' . __LINE__
);
122 $this->assertNotNull($customField['id'], 'in line ' . __LINE__
);
126 * check without groupId
128 function testCustomFieldCreateWithoutGroupID() {
129 $fieldParams = array(
130 'name' => 'test_textfield1',
132 'html_type' => 'Text',
133 'data_type' => 'String',
134 'default_value' => 'abc',
137 'is_searchable' => 0,
139 'version' => $this->_apiversion
,
142 $customField = civicrm_api('custom_field', 'create', $fieldParams);
143 $this->assertEquals($customField['is_error'], 1);
144 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: custom_group_id');
148 * Check for Each data type: loop through available form input types
150 function testCustomFieldCreateAllAvailableFormInputs() {
151 $gid = $this->customGroupCreate('Individual', 'testAllFormInputs');
153 $dtype = CRM_Core_BAO_CustomField
::dataType();
154 $htype = CRM_Core_BAO_CustomField
::dataToHtml();
157 foreach ($dtype as $dkey => $dvalue) {
158 foreach ($htype[$n] as $hkey => $hvalue) {
159 //echo $dkey."][".$hvalue."\n";
160 $this->_loopingCustomFieldCreateTest($this->_buildParams($gid['id'], $hvalue, $dkey));
166 * Can't figure out the point of this?
168 function _loopingCustomFieldCreateTest($params) {
169 $customField = civicrm_api('custom_field', 'create', $params);
170 $this->assertEquals(0, $customField['is_error'], var_export($customField, TRUE));
171 $this->assertNotNull($customField['id']);
172 $this->getAndCheck($params, $customField['id'], 'CustomField');
175 function _buildParams($gid, $htype, $dtype) {
176 $params = $this->_buildBasicParams($gid, $htype, $dtype);
177 /* //Not Working for any type. Maybe redundant with testCustomFieldCreateWithOptionValues()
178 if ($htype == 'Multi-Select')
179 $params = array_merge($params, array(
180 'option_label' => array( 'Label1','Label2'),
181 'option_value' => array( 'val1', 'val2' ),
182 'option_weight' => array( 1, 2),
183 'option_status' => array( 1, 1),
192 function _buildBasicParams($gid, $htype, $dtype) {
194 'custom_group_id' => $gid,
195 'label' => $dtype . $htype,
196 'html_type' => $htype,
197 'data_type' => $dtype,
200 'is_searchable' => 0,
202 'version' => $this->_apiversion
,
207 * Test using example code
209 /*function testCustomFieldCreateExample( )
213 $customGroup = $this->customGroupCreate('Individual','date_test_group',3);
214 require_once 'api/v3/examples/CustomFieldCreate.php';
215 $result = custom_field_create_example();
216 $expectedResult = custom_field_create_expectedresult();
217 $this->assertEquals($result,$expectedResult);
221 * check with data type - Options with option_values
223 function testCustomFieldCreateWithEmptyOptionGroup() {
224 $customGroup = $this->customGroupCreate('Contact', 'select_test_group', 3);
226 'custom_group_id' => $customGroup['id'],
227 'label' => 'Country',
228 'html_type' => 'Select',
229 'data_type' => 'String',
232 'is_searchable' => 0,
234 'version' => $this->_apiversion
,
237 $customField = civicrm_api('custom_field', 'create', $params);
238 $this->assertAPISuccess($customField);
239 $this->assertNotNull($customField['id']);
240 $optionGroupID = civicrm_api('custom_field', 'getvalue', array(
242 'id' => $customField['id'],
243 'return' => 'option_group_id',
246 $this->assertTrue(is_numeric($optionGroupID) && ($optionGroupID > 0));
247 $optionGroup = civicrm_api('option_group', 'getsingle', array(
248 'version' => 3, 'id' => $optionGroupID));
249 $this->assertEquals($optionGroup['title'],'Country');
250 $optionValueCount = civicrm_api('option_value', 'getcount', array(
251 'version' => 3, 'option_group_id' => $optionGroupID));
252 $this->assertEquals(0, $optionValueCount);
257 * Test custom field get works & return param works
259 function testCustomFieldGetReturnOptions(){
260 $customGroup = $this->customGroupCreate('Individual', 'test_group');
261 $customField = $this->customFieldCreate($customGroup['id'], 'test_name');
263 $result = civicrm_api('custom_field', 'getsingle', array(
265 'id' => $customField['id'],
266 'return' => 'data_type',
268 $this->assertTrue(array_key_exists('data_type', $result));
269 $this->assertFalse(array_key_exists('custom_group_id', $result));
273 * Test custom field get works & return param works
275 function testCustomFieldGetReturnArray(){
276 $customGroup = $this->customGroupCreate('Individual', 'test_group');
277 $customField = $this->customFieldCreate($customGroup['id'], 'test_name');
279 $result = civicrm_api('custom_field', 'getsingle', array(
281 'id' => $customField['id'],
282 'return' => array('data_type'),
284 $this->assertTrue(array_key_exists('data_type', $result));
285 $this->assertFalse(array_key_exists('custom_group_id', $result));
289 * Test custom field get works & return param works
291 function testCustomFieldGetReturnTwoOptions(){
292 $customGroup = $this->customGroupCreate('Individual', 'test_group');
293 $customField = $this->customFieldCreate($customGroup['id'], 'test_name');
295 $result = civicrm_api('custom_field', 'getsingle', array(
297 'id' => $customField['id'],
298 'return' => 'data_type, custom_group_id',
300 $this->assertTrue(array_key_exists('data_type', $result));
301 $this->assertTrue(array_key_exists('custom_group_id', $result));
302 $this->assertFalse(array_key_exists('label', $result));
305 function testCustomFieldCreateWithOptionValues() {
306 $customGroup = $this->customGroupCreate('Contact', 'select_test_group', 3);
308 $option_values = array(
323 'custom_group_id' => $customGroup['id'],
324 'label' => 'Our special field',
325 'html_type' => 'Select',
326 'data_type' => 'String',
329 'is_searchable' => 0,
331 'option_values' => $option_values,
332 'version' => $this->_apiversion
,
335 $customField = civicrm_api('custom_field', 'create', $params);
337 $this->assertAPISuccess($customField);
338 $this->assertNotNull($customField['id']);
339 $getFieldsParams = array(
340 'options' => array('get_options' => 'custom_' . $customField['id']),
342 'action' => 'create',
344 $description = "Demonstrate retrieving custom field options";
345 $subfile = "GetFieldsOptions";
346 $fields = civicrm_api('contact', 'getfields', $getFieldsParams);
347 $this->documentMe($getFieldsParams, $fields, __FUNCTION__
, 'ContactTest.php', $description,$subfile,'GetFields');
348 $this->assertArrayHasKey('options', $fields['values']['custom_' . $customField['id']]);
349 $this->assertEquals('Label1', $fields['values']['custom_' . $customField['id']]['options'][1]);
350 $getOptionsArray = array(
351 'field' => 'custom_' . $customField['id'],
354 $description = "Demonstrates retrieving options for a custom field";
355 $subfile = "GetOptions";
356 $result = civicrm_api('contact', 'getoptions', $getOptionsArray);
357 $this->assertEquals('Label1', $result['values'][1]);
358 $this->documentMe($getOptionsArray, $result, __FUNCTION__
, 'ContactTest.php', $description, '', 'getoptions');
361 ///////////////// civicrm_custom_field_delete methods
364 * check with no array
366 function testCustomFieldDeleteNoArray() {
368 $customField = civicrm_api('custom_field', 'delete', $params);
369 $this->assertEquals($customField['is_error'], 1);
370 $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array');
374 * check without Field ID
376 function testCustomFieldDeleteWithoutFieldID() {
377 $params = array('version' => $this->_apiversion
);
378 $customField = civicrm_api('custom_field', 'delete', $params);
379 $this->assertEquals($customField['is_error'], 1);
380 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: id');
384 * check without valid array
386 function testCustomFieldDelete() {
387 $customGroup = $this->customGroupCreate('Individual', 'test_group');
388 $customField = $this->customFieldCreate($customGroup['id'], 'test_name');
389 $this->assertNotNull($customField['id'], 'in line ' . __LINE__
);
392 'version' => $this->_apiversion
,
393 'id' => $customField['id'],
395 $result = civicrm_api('custom_field', 'delete', $params);
396 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
398 $this->assertEquals($result['is_error'], 0, 'in line ' . __LINE__
);
402 * check for Option Value
404 function testCustomFieldOptionValueDelete() {
405 $customGroup = $this->customGroupCreate('Contact', 'ABC');
406 $customOptionValueFields = $this->customFieldOptionValueCreate($customGroup, 'fieldABC');
407 $customOptionValueFields['version'] = $this->_apiversion
;
409 'version' => $this->_apiversion
,
410 'id' => $customOptionValueFields,
413 $customField = civicrm_api('custom_field', 'delete', $customOptionValueFields);
414 $this->assertAPISuccess($customField);