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',
50 'civicrm_custom_field',
52 // true tells quickCleanup to drop any tables that might have been created in the test
53 $this->quickCleanup($tablesToTruncate, TRUE);
59 public function testCustomFieldCreateNoArray() {
62 $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams);
63 $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array');
69 public function testCustomFieldCreateWithoutLabel() {
70 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'text_test_group'));
72 'custom_group_id' => $customGroup['id'],
73 'name' => 'test_textfield2',
74 'html_type' => 'Text',
75 'data_type' => 'String',
76 'default_value' => 'abc',
83 $customField = $this->callAPIFailure('custom_field', 'create', $params);
84 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: label');
90 public function testCustomFieldCreateWithEdit() {
91 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'text_test_group'));
93 'custom_group_id' => $customGroup['id'],
94 'name' => 'test_textfield2',
96 'html_type' => 'Text',
97 'data_type' => 'String',
98 'default_value' => 'abc',
101 'is_searchable' => 0,
105 $customField = $this->callAPIAndDocument('custom_field', 'create', $params, __FUNCTION__
, __FILE__
);
106 $params['id'] = $customField['id'];
107 $customField = $this->callAPISuccess('custom_field', 'create', $params);
109 $this->assertNotNull($customField['id'], 'in line ' . __LINE__
);
113 * Check without groupId
115 public function testCustomFieldCreateWithoutGroupID() {
116 $fieldParams = array(
117 'name' => 'test_textfield1',
119 'html_type' => 'Text',
120 'data_type' => 'String',
121 'default_value' => 'abc',
124 'is_searchable' => 0,
129 $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams);
130 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: custom_group_id');
134 * Check for Each data type: loop through available form input types
136 public function testCustomFieldCreateAllAvailableFormInputs() {
137 $gid = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'testAllFormInputs'));
139 $dtype = CRM_Core_BAO_CustomField
::dataType();
140 $htype = CRM_Core_BAO_CustomField
::dataToHtml();
143 foreach ($dtype as $dkey => $dvalue) {
144 foreach ($htype[$n] as $hkey => $hvalue) {
145 //echo $dkey."][".$hvalue."\n";
146 $this->_loopingCustomFieldCreateTest($this->_buildParams($gid['id'], $hvalue, $dkey));
152 * Can't figure out the point of this?
155 * @param array $params
157 public function _loopingCustomFieldCreateTest($params) {
158 $customField = $this->callAPISuccess('custom_field', 'create', $params);
159 $this->assertNotNull($customField['id']);
160 $this->getAndCheck($params, $customField['id'], 'CustomField');
170 public function _buildParams($gid, $htype, $dtype) {
171 $params = $this->_buildBasicParams($gid, $htype, $dtype);
172 /* //Not Working for any type. Maybe redundant with testCustomFieldCreateWithOptionValues()
173 if ($htype == 'Multi-Select')
174 $params = array_merge($params, array(
175 'option_label' => array( 'Label1','Label2'),
176 'option_value' => array( 'val1', 'val2' ),
177 'option_weight' => array( 1, 2),
178 'option_status' => array( 1, 1),
192 public 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,
207 * Test using example code
209 /*function testCustomFieldCreateExample( )
213 $customGroup = $this->customGroupCreate('Individual','date_test_group',3);
214 require_once 'api/v3/examples/CustomField/Create.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 public function testCustomFieldCreateWithEmptyOptionGroup() {
224 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'select_test_group'));
226 'custom_group_id' => $customGroup['id'],
227 'label' => 'Country',
228 'html_type' => 'Select',
229 'data_type' => 'String',
232 'is_searchable' => 0,
236 $customField = $this->callAPISuccess('custom_field', 'create', $params);
237 $this->assertNotNull($customField['id']);
238 $optionGroupID = $this->callAPISuccess('custom_field', 'getvalue', array(
239 'id' => $customField['id'],
240 'return' => 'option_group_id',
243 $this->assertTrue(is_numeric($optionGroupID) && ($optionGroupID > 0));
244 $optionGroup = $this->callAPISuccess('option_group', 'getsingle', array(
245 'id' => $optionGroupID
247 $this->assertEquals($optionGroup['title'], 'Country');
248 $optionValueCount = $this->callAPISuccess('option_value', 'getcount', array(
249 'option_group_id' => $optionGroupID
251 $this->assertEquals(0, $optionValueCount);
255 * Test custom field with existing option group
257 public function testCustomFieldExistingOptionGroup() {
258 $customGroup = $this->customGroupCreate(array('extends' => 'Organization', 'title' => 'test_group'));
260 'custom_group_id' => $customGroup['id'],
261 // Just to say something:
262 'label' => 'Organization Gender',
263 'html_type' => 'Select',
264 'data_type' => 'Int',
267 'is_searchable' => 0,
269 // Option group id 3: gender
270 'option_group_id' => 3,
273 $customField = $this->callAPISuccess('custom_field', 'create', $params);
274 $this->assertNotNull($customField['id']);
275 $optionGroupID = $this->callAPISuccess('custom_field', 'getvalue', array(
276 'id' => $customField['id'],
277 'return' => 'option_group_id',
280 $this->assertEquals($optionGroupID, 3);
285 * Test custom field get works & return param works
287 public function testCustomFieldGetReturnOptions() {
288 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
289 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
291 $result = $this->callAPISuccess('custom_field', 'getsingle', array(
292 'id' => $customField['id'],
293 'return' => 'data_type',
295 $this->assertTrue(array_key_exists('data_type', $result));
296 $this->assertFalse(array_key_exists('custom_group_id', $result));
300 * Test custom field get works & return param works
302 public function testCustomFieldGetReturnArray() {
303 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
304 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
306 $result = $this->callAPISuccess('custom_field', 'getsingle', array(
307 'id' => $customField['id'],
308 'return' => array('data_type'),
310 $this->assertTrue(array_key_exists('data_type', $result));
311 $this->assertFalse(array_key_exists('custom_group_id', $result));
315 * Test custom field get works & return param works
317 public function testCustomFieldGetReturnTwoOptions() {
318 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'test_group'));
319 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
321 $result = $this->callAPISuccess('custom_field', 'getsingle', array(
322 'id' => $customField['id'],
323 'return' => 'data_type, custom_group_id',
325 $this->assertTrue(array_key_exists('data_type', $result));
326 $this->assertTrue(array_key_exists('custom_group_id', $result));
327 $this->assertFalse(array_key_exists('label', $result));
330 public function testCustomFieldCreateWithOptionValues() {
331 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'select_test_group'));
333 $option_values = array(
349 'custom_group_id' => $customGroup['id'],
350 'label' => 'Our special field',
351 'html_type' => 'Select',
352 'data_type' => 'String',
355 'is_searchable' => 0,
357 'option_values' => $option_values,
361 $customField = $this->callAPISuccess('custom_field', 'create', $params);
363 $this->assertAPISuccess($customField);
364 $this->assertNotNull($customField['id']);
365 $getFieldsParams = array(
366 'options' => array('get_options' => 'custom_' . $customField['id']),
367 'action' => 'create',
369 $description = "Demonstrate retrieving metadata with custom field options";
370 $subfile = "GetFieldsOptions";
371 $fields = $this->callAPIAndDocument('contact', 'getfields', $getFieldsParams, __FUNCTION__
, 'ContactTest.php', $description, $subfile, 'GetFields');
372 $this->assertArrayHasKey('options', $fields['values']['custom_' . $customField['id']]);
373 $this->assertEquals('Label1', $fields['values']['custom_' . $customField['id']]['options'][1]);
374 $getOptionsArray = array(
375 'field' => 'custom_' . $customField['id'],
377 $description = "Demonstrates retrieving options for a custom field";
378 $subfile = "GetOptions";
379 $result = $this->callAPIAndDocument('contact', 'getoptions', $getOptionsArray, __FUNCTION__
, 'ContactTest.php', $description, '', 'getoptions');
380 $this->assertEquals('Label1', $result['values'][1]);
383 ///////////////// civicrm_custom_field_delete methods
386 * Check with no array
388 public function testCustomFieldDeleteNoArray() {
390 $customField = $this->callAPIFailure('custom_field', 'delete', $params);
391 $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array');
395 * Check without Field ID
397 public function testCustomFieldDeleteWithoutFieldID() {
399 $customField = $this->callAPIFailure('custom_field', 'delete', $params,
400 'Mandatory key(s) missing from params array: id');
404 * Check without valid array
406 public function testCustomFieldDelete() {
407 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
408 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
409 $this->assertNotNull($customField['id'], 'in line ' . __LINE__
);
412 'id' => $customField['id'],
414 $result = $this->callAPIAndDocument('custom_field', 'delete', $params, __FUNCTION__
, __FILE__
);
416 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
420 * Check for Option Value
422 public function testCustomFieldOptionValueDelete() {
423 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'ABC'));
424 $customOptionValueFields = $this->customFieldOptionValueCreate($customGroup, 'fieldABC');
426 'id' => $customOptionValueFields,
429 $customField = $this->callAPISuccess('custom_field', 'delete', $customOptionValueFields);
433 * If there's one custom group for "Contact" and one for "Activity", then "Contact.getfields"
434 * and "Activity.getfields" should return only their respective fields (not the other's fields),
435 * and unrelated entities should return no custom fields.
437 public function testGetfields_CrossEntityPollution() {
438 $auxEntities = array('Email', 'Address', 'LocBlock', 'Membership', 'ContributionPage', 'ReportInstance');
439 $allEntities = array_merge(array('Contact', 'Activity'), $auxEntities);
441 // Baseline - getfields doesn't reporting any customfields for any entities
442 foreach ($allEntities as $entity) {
445 $this->getCustomFieldKeys($this->callAPISuccess($entity, 'getfields', array())),
446 "Baseline custom fields for $entity should be empty"
451 $contactGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'test_group_c'));
452 $contactField = $this->customFieldCreate(array(
453 'custom_group_id' => $contactGroup['id'],
454 'label' => 'For Contacts'
456 $indivGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group_i'));
457 $indivField = $this->customFieldCreate(array('custom_group_id' => $indivGroup['id'], 'label' => 'For Individuals'));
458 $activityGroup = $this->customGroupCreate(array('extends' => 'Activity', 'title' => 'test_group_a'));
459 $activityField = $this->customFieldCreate(array(
460 'custom_group_id' => $activityGroup['id'],
461 'label' => 'For Activities'
466 array('custom_' . $contactField['id'], 'custom_' . $indivField['id']),
467 $this->getCustomFieldKeys($this->callAPISuccess('Contact', 'getfields', array())),
468 'Contact custom fields'
471 array('custom_' . $contactField['id'], 'custom_' . $indivField['id']),
472 $this->getCustomFieldKeys($this->callAPISuccess('Individual', 'getfields', array())),
473 'Individual custom fields'
476 array('custom_' . $contactField['id']),
477 $this->getCustomFieldKeys($this->callAPISuccess('Organization', 'getfields', array())),
478 'Organization custom fields'
481 array('custom_' . $activityField['id']),
482 $this->getCustomFieldKeys($this->callAPISuccess('Activity', 'getfields', array())),
483 'Activity custom fields'
485 foreach ($auxEntities as $entity) {
488 $this->getCustomFieldKeys($this->callAPISuccess($entity, 'getfields', array())),
489 "Custom fields for $entity should be empty"
495 * @param $getFieldsResult
499 public function getCustomFieldKeys($getFieldsResult) {
500 $isCustom = function ($key) {
501 return preg_match('/^custom_/', $key);
503 $r = array_values(array_filter(array_keys($getFieldsResult['values']), $isCustom));