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;
42 public function setUp() {
43 $this->_apiversion
= 3;
47 public function tearDown() {
48 $tablesToTruncate = array(
49 'civicrm_custom_group', 'civicrm_custom_field',
51 // true tells quickCleanup to drop any tables that might have been created in the test
52 $this->quickCleanup($tablesToTruncate, TRUE);
58 public function testCustomFieldCreateNoArray() {
61 $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams);
62 $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array');
68 public function testCustomFieldCreateWithoutLabel() {
69 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'text_test_group'));
71 'custom_group_id' => $customGroup['id'],
72 'name' => 'test_textfield2',
73 'html_type' => 'Text',
74 'data_type' => 'String',
75 'default_value' => 'abc',
82 $customField = $this->callAPIFailure('custom_field', 'create', $params);
83 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: label');
89 public function testCustomFieldCreateWithEdit() {
90 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'text_test_group'));
92 'custom_group_id' => $customGroup['id'],
93 'name' => 'test_textfield2',
95 'html_type' => 'Text',
96 'data_type' => 'String',
97 'default_value' => 'abc',
100 'is_searchable' => 0,
104 $customField = $this->callAPIAndDocument('custom_field', 'create', $params, __FUNCTION__
, __FILE__
);
105 $params['id'] = $customField['id'];
106 $customField = $this->callAPISuccess('custom_field', 'create', $params);
108 $this->assertNotNull($customField['id'], 'in line ' . __LINE__
);
112 * Check without groupId
114 public function testCustomFieldCreateWithoutGroupID() {
115 $fieldParams = array(
116 'name' => 'test_textfield1',
118 'html_type' => 'Text',
119 'data_type' => 'String',
120 'default_value' => 'abc',
123 'is_searchable' => 0,
128 $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams);
129 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: custom_group_id');
133 * Check for Each data type: loop through available form input types
135 public function testCustomFieldCreateAllAvailableFormInputs() {
136 $gid = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'testAllFormInputs'));
138 $dtype = CRM_Core_BAO_CustomField
::dataType();
139 $htype = CRM_Core_BAO_CustomField
::dataToHtml();
142 foreach ($dtype as $dkey => $dvalue) {
143 foreach ($htype[$n] as $hkey => $hvalue) {
144 //echo $dkey."][".$hvalue."\n";
145 $this->_loopingCustomFieldCreateTest($this->_buildParams($gid['id'], $hvalue, $dkey));
151 * Can't figure out the point of this?
154 * @param array $params
156 public function _loopingCustomFieldCreateTest($params) {
157 $customField = $this->callAPISuccess('custom_field', 'create', $params);
158 $this->assertNotNull($customField['id']);
159 $this->getAndCheck($params, $customField['id'], 'CustomField');
169 public function _buildParams($gid, $htype, $dtype) {
170 $params = $this->_buildBasicParams($gid, $htype, $dtype);
171 /* //Not Working for any type. Maybe redundant with testCustomFieldCreateWithOptionValues()
172 if ($htype == 'Multi-Select')
173 $params = array_merge($params, array(
174 'option_label' => array( 'Label1','Label2'),
175 'option_value' => array( 'val1', 'val2' ),
176 'option_weight' => array( 1, 2),
177 'option_status' => array( 1, 1),
191 public function _buildBasicParams($gid, $htype, $dtype) {
193 'custom_group_id' => $gid,
194 'label' => $dtype . $htype,
195 'html_type' => $htype,
196 'data_type' => $dtype,
199 'is_searchable' => 0,
206 * Test using example code
208 /*function testCustomFieldCreateExample( )
212 $customGroup = $this->customGroupCreate('Individual','date_test_group',3);
213 require_once 'api/v3/examples/CustomField/Create.php';
214 $result = custom_field_create_example();
215 $expectedResult = custom_field_create_expectedresult();
216 $this->assertEquals($result,$expectedResult);
220 * Check with data type - Options with option_values
222 public function testCustomFieldCreateWithEmptyOptionGroup() {
223 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'select_test_group'));
225 'custom_group_id' => $customGroup['id'],
226 'label' => 'Country',
227 'html_type' => 'Select',
228 'data_type' => 'String',
231 'is_searchable' => 0,
235 $customField = $this->callAPISuccess('custom_field', 'create', $params);
236 $this->assertNotNull($customField['id']);
237 $optionGroupID = $this->callAPISuccess('custom_field', 'getvalue', array(
238 'id' => $customField['id'],
239 'return' => 'option_group_id',
242 $this->assertTrue(is_numeric($optionGroupID) && ($optionGroupID > 0));
243 $optionGroup = $this->callAPISuccess('option_group', 'getsingle', array(
244 'id' => $optionGroupID));
245 $this->assertEquals($optionGroup['title'], 'Country');
246 $optionValueCount = $this->callAPISuccess('option_value', 'getcount', array(
247 'option_group_id' => $optionGroupID));
248 $this->assertEquals(0, $optionValueCount);
252 * Test custom field with existing option group
254 public function testCustomFieldExistingOptionGroup() {
255 $customGroup = $this->customGroupCreate(array('extends' => 'Organization', 'title' => 'test_group'));
257 'custom_group_id' => $customGroup['id'],
258 // Just to say something:
259 'label' => 'Organization Gender',
260 'html_type' => 'Select',
261 'data_type' => 'Int',
264 'is_searchable' => 0,
266 // Option group id 3: gender
267 'option_group_id' => 3,
270 $customField = $this->callAPISuccess('custom_field', 'create', $params);
271 $this->assertNotNull($customField['id']);
272 $optionGroupID = $this->callAPISuccess('custom_field', 'getvalue', array(
273 'id' => $customField['id'],
274 'return' => 'option_group_id',
277 $this->assertEquals($optionGroupID, 3);
282 * Test custom field get works & return param works
284 public function testCustomFieldGetReturnOptions() {
285 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
286 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
288 $result = $this->callAPISuccess('custom_field', 'getsingle', array(
289 'id' => $customField['id'],
290 'return' => 'data_type',
292 $this->assertTrue(array_key_exists('data_type', $result));
293 $this->assertFalse(array_key_exists('custom_group_id', $result));
297 * Test custom field get works & return param works
299 public function testCustomFieldGetReturnArray() {
300 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
301 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
303 $result = $this->callAPISuccess('custom_field', 'getsingle', array(
304 'id' => $customField['id'],
305 'return' => array('data_type'),
307 $this->assertTrue(array_key_exists('data_type', $result));
308 $this->assertFalse(array_key_exists('custom_group_id', $result));
312 * Test custom field get works & return param works
314 public function testCustomFieldGetReturnTwoOptions() {
315 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'test_group'));
316 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
318 $result = $this->callAPISuccess('custom_field', 'getsingle', array(
319 'id' => $customField['id'],
320 'return' => 'data_type, custom_group_id',
322 $this->assertTrue(array_key_exists('data_type', $result));
323 $this->assertTrue(array_key_exists('custom_group_id', $result));
324 $this->assertFalse(array_key_exists('label', $result));
327 public function testCustomFieldCreateWithOptionValues() {
328 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'select_test_group'));
330 $option_values = array(
346 'custom_group_id' => $customGroup['id'],
347 'label' => 'Our special field',
348 'html_type' => 'Select',
349 'data_type' => 'String',
352 'is_searchable' => 0,
354 'option_values' => $option_values,
358 $customField = $this->callAPISuccess('custom_field', 'create', $params);
360 $this->assertAPISuccess($customField);
361 $this->assertNotNull($customField['id']);
362 $getFieldsParams = array(
363 'options' => array('get_options' => 'custom_' . $customField['id']),
364 'action' => 'create',
366 $description = "Demonstrate retrieving metadata with custom field options";
367 $subfile = "GetFieldsOptions";
368 $fields = $this->callAPIAndDocument('contact', 'getfields', $getFieldsParams, __FUNCTION__
, 'ContactTest.php', $description, $subfile, 'GetFields');
369 $this->assertArrayHasKey('options', $fields['values']['custom_' . $customField['id']]);
370 $this->assertEquals('Label1', $fields['values']['custom_' . $customField['id']]['options'][1]);
371 $getOptionsArray = array(
372 'field' => 'custom_' . $customField['id'],
374 $description = "Demonstrates retrieving options for a custom field";
375 $subfile = "GetOptions";
376 $result = $this->callAPIAndDocument('contact', 'getoptions', $getOptionsArray, __FUNCTION__
, 'ContactTest.php', $description, '', 'getoptions');
377 $this->assertEquals('Label1', $result['values'][1]);
380 ///////////////// civicrm_custom_field_delete methods
383 * Check with no array
385 public function testCustomFieldDeleteNoArray() {
387 $customField = $this->callAPIFailure('custom_field', 'delete', $params);
388 $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array');
392 * Check without Field ID
394 public function testCustomFieldDeleteWithoutFieldID() {
396 $customField = $this->callAPIFailure('custom_field', 'delete', $params,
397 'Mandatory key(s) missing from params array: id');
401 * Check without valid array
403 public function testCustomFieldDelete() {
404 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
405 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
406 $this->assertNotNull($customField['id'], 'in line ' . __LINE__
);
409 'id' => $customField['id'],
411 $result = $this->callAPIAndDocument('custom_field', 'delete', $params, __FUNCTION__
, __FILE__
);
413 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
417 * Check for Option Value
419 public function testCustomFieldOptionValueDelete() {
420 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'ABC'));
421 $customOptionValueFields = $this->customFieldOptionValueCreate($customGroup, 'fieldABC');
423 'id' => $customOptionValueFields,
426 $customField = $this->callAPISuccess('custom_field', 'delete', $customOptionValueFields);
430 * If there's one custom group for "Contact" and one for "Activity", then "Contact.getfields"
431 * and "Activity.getfields" should return only their respective fields (not the other's fields),
432 * and unrelated entities should return no custom fields.
434 public function testGetfields_CrossEntityPollution() {
435 $auxEntities = array('Email', 'Address', 'LocBlock', 'Membership', 'ContributionPage', 'ReportInstance');
436 $allEntities = array_merge(array('Contact', 'Activity'), $auxEntities);
438 // Baseline - getfields doesn't reporting any customfields for any entities
439 foreach ($allEntities as $entity) {
442 $this->getCustomFieldKeys($this->callAPISuccess($entity, 'getfields', array())),
443 "Baseline custom fields for $entity should be empty"
448 $contactGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'test_group_c'));
449 $contactField = $this->customFieldCreate(array('custom_group_id' => $contactGroup['id'], 'label' => 'For Contacts'));
450 $indivGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group_i'));
451 $indivField = $this->customFieldCreate(array('custom_group_id' => $indivGroup['id'], 'label' => 'For Individuals'));
452 $activityGroup = $this->customGroupCreate(array('extends' => 'Activity', 'title' => 'test_group_a'));
453 $activityField = $this->customFieldCreate(array('custom_group_id' => $activityGroup['id'], 'label' => 'For Activities'));
457 array('custom_' . $contactField['id'], 'custom_' . $indivField['id']),
458 $this->getCustomFieldKeys($this->callAPISuccess('Contact', 'getfields', array())),
459 'Contact custom fields'
462 array('custom_' . $contactField['id'], 'custom_' . $indivField['id']),
463 $this->getCustomFieldKeys($this->callAPISuccess('Individual', 'getfields', array())),
464 'Individual custom fields'
467 array('custom_' . $contactField['id']),
468 $this->getCustomFieldKeys($this->callAPISuccess('Organization', 'getfields', array())),
469 'Organization custom fields'
472 array('custom_' . $activityField['id']),
473 $this->getCustomFieldKeys($this->callAPISuccess('Activity', 'getfields', array())),
474 'Activity custom fields'
476 foreach ($auxEntities as $entity) {
479 $this->getCustomFieldKeys($this->callAPISuccess($entity, 'getfields', array())),
480 "Custom fields for $entity should be empty"
486 * @param $getFieldsResult
490 public function getCustomFieldKeys($getFieldsResult) {
491 $isCustom = function($key) {
492 return preg_match('/^custom_/', $key);
494 $r = array_values(array_filter(array_keys($getFieldsResult['values']), $isCustom));