Merge branch 'master' into master-civimail-abtest
[civicrm-core.git] / tests / phpunit / api / v3 / CustomFieldTest.php
CommitLineData
6a488035 1<?php
b6708aeb 2/*
3 +--------------------------------------------------------------------+
06a1bc01 4| CiviCRM version 4.5 |
b6708aeb 5+--------------------------------------------------------------------+
06a1bc01 6| Copyright CiviCRM LLC (c) 2004-2014 |
b6708aeb 7+--------------------------------------------------------------------+
8| This file is a part of CiviCRM. |
9| |
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. |
13| |
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. |
18| |
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+--------------------------------------------------------------------+
26*/
6a488035
TO
27
28/**
29 * Include class definitions
30 */
31require_once 'tests/phpunit/CiviTest/CiviUnitTestCase.php';
32
33
34/**
35 * Test APIv3 civicrm_create_custom_group
36 *
37 * @package CiviCRM
38 */
39class api_v3_CustomFieldTest extends CiviUnitTestCase {
40 protected $_apiversion;
b7c9bc4c 41
4cbe18b8
EM
42 /**
43 * @return array
44 */
6a488035
TO
45 function get_info() {
46 return array(
47 'name' => 'Custom Field Create',
48 'description' => 'Test all Custom Field Create API methods.',
49 'group' => 'CiviCRM API Tests',
50 );
51 }
52
53 function setUp() {
54 $this->_apiversion = 3;
55 parent::setUp();
56 }
57
58 function tearDown() {
59 $tablesToTruncate = array(
60 'civicrm_custom_group', 'civicrm_custom_field',
61 );
62 // true tells quickCleanup to drop any tables that might have been created in the test
63 $this->quickCleanup($tablesToTruncate, TRUE);
64 }
65
66 /**
100fef9d 67 * Check with no array
6a488035
TO
68 */
69 function testCustomFieldCreateNoArray() {
70 $fieldParams = NULL;
71
d0e1eff2 72 $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams);
6a488035
TO
73 $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array');
74 }
75
76 /**
100fef9d 77 * Check with no label
6a488035
TO
78 */
79 function testCustomFieldCreateWithoutLabel() {
e830c6ae 80 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'text_test_group'));
6a488035
TO
81 $params = array(
82 'custom_group_id' => $customGroup['id'],
83 'name' => 'test_textfield2',
84 'html_type' => 'Text',
85 'data_type' => 'String',
86 'default_value' => 'abc',
87 'weight' => 4,
88 'is_required' => 1,
89 'is_searchable' => 0,
90 'is_active' => 1,
6a488035
TO
91 );
92
d0e1eff2 93 $customField = $this->callAPIFailure('custom_field', 'create', $params);
6a488035
TO
94 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: label');
95 }
96
97 /**
100fef9d 98 * Check with edit
6a488035
TO
99 */
100 function testCustomFieldCreateWithEdit() {
e830c6ae 101 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'text_test_group'));
6a488035
TO
102 $params = array(
103 'custom_group_id' => $customGroup['id'],
104 'name' => 'test_textfield2',
105 'label' => 'Name1',
106 'html_type' => 'Text',
107 'data_type' => 'String',
108 'default_value' => 'abc',
109 'weight' => 4,
110 'is_required' => 1,
111 'is_searchable' => 0,
112 'is_active' => 1,
6a488035
TO
113 );
114
fb32de45 115 $customField = $this->callAPIAndDocument('custom_field', 'create', $params, __FUNCTION__, __FILE__);
6a488035 116 $params['id'] = $customField['id'];
e830c6ae 117 $customField = $this->callAPISuccess('custom_field', 'create', $params);
6a488035 118
6a488035
TO
119 $this->assertNotNull($customField['id'], 'in line ' . __LINE__);
120 }
121
122 /**
100fef9d 123 * Check without groupId
6a488035
TO
124 */
125 function testCustomFieldCreateWithoutGroupID() {
126 $fieldParams = array(
127 'name' => 'test_textfield1',
128 'label' => 'Name',
129 'html_type' => 'Text',
130 'data_type' => 'String',
131 'default_value' => 'abc',
132 'weight' => 4,
133 'is_required' => 1,
134 'is_searchable' => 0,
135 'is_active' => 1,
e830c6ae 136
6a488035
TO
137 );
138
d0e1eff2 139 $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams);
6a488035
TO
140 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: custom_group_id');
141 }
142
143 /**
144 * Check for Each data type: loop through available form input types
145 **/
146 function testCustomFieldCreateAllAvailableFormInputs() {
e830c6ae 147 $gid = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'testAllFormInputs'));
6a488035
TO
148
149 $dtype = CRM_Core_BAO_CustomField::dataType();
150 $htype = CRM_Core_BAO_CustomField::dataToHtml();
151
152 $n = 0;
153 foreach ($dtype as $dkey => $dvalue) {
154 foreach ($htype[$n] as $hkey => $hvalue) {
155 //echo $dkey."][".$hvalue."\n";
156 $this->_loopingCustomFieldCreateTest($this->_buildParams($gid['id'], $hvalue, $dkey));
157 }
158 $n++;
159 }
160 }
161/*
162 * Can't figure out the point of this?
163 */
4cbe18b8 164 /**
c490a46a 165 * @param array $params
4cbe18b8 166 */
6a488035 167 function _loopingCustomFieldCreateTest($params) {
e830c6ae 168 $customField = $this->callAPISuccess('custom_field', 'create', $params);
6a488035
TO
169 $this->assertNotNull($customField['id']);
170 $this->getAndCheck($params, $customField['id'], 'CustomField');
171 }
172
4cbe18b8 173 /**
100fef9d 174 * @param int $gid
4cbe18b8
EM
175 * @param $htype
176 * @param $dtype
177 *
178 * @return array
179 */
6a488035
TO
180 function _buildParams($gid, $htype, $dtype) {
181 $params = $this->_buildBasicParams($gid, $htype, $dtype);
182 /* //Not Working for any type. Maybe redundant with testCustomFieldCreateWithOptionValues()
183 if ($htype == 'Multi-Select')
184 $params = array_merge($params, array(
185 'option_label' => array( 'Label1','Label2'),
186 'option_value' => array( 'val1', 'val2' ),
187 'option_weight' => array( 1, 2),
188 'option_status' => array( 1, 1),
189 ));
190*/
191
192
193
194 return $params;
195 }
196
4cbe18b8 197 /**
100fef9d 198 * @param int $gid
4cbe18b8
EM
199 * @param $htype
200 * @param $dtype
201 *
202 * @return array
203 */
6a488035
TO
204 function _buildBasicParams($gid, $htype, $dtype) {
205 return array(
206 'custom_group_id' => $gid,
207 'label' => $dtype . $htype,
208 'html_type' => $htype,
209 'data_type' => $dtype,
210 'weight' => 4,
211 'is_required' => 0,
212 'is_searchable' => 0,
213 'is_active' => 1,
e830c6ae 214
6a488035
TO
215 );
216 }
217
218 /**
219 * Test using example code
220 */
221 /*function testCustomFieldCreateExample( )
222 {
223
224
225 $customGroup = $this->customGroupCreate('Individual','date_test_group',3);
3ec6e38d 226 require_once 'api/v3/examples/CustomField/Create.php';
6a488035
TO
227 $result = custom_field_create_example();
228 $expectedResult = custom_field_create_expectedresult();
229 $this->assertEquals($result,$expectedResult);
230 }*/
231
232 /**
100fef9d 233 * Check with data type - Options with option_values
6a488035 234 */
b958933f 235 function testCustomFieldCreateWithEmptyOptionGroup() {
e830c6ae 236 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'select_test_group'));
b958933f 237 $params = array(
238 'custom_group_id' => $customGroup['id'],
239 'label' => 'Country',
240 'html_type' => 'Select',
241 'data_type' => 'String',
242 'weight' => 4,
243 'is_required' => 1,
244 'is_searchable' => 0,
245 'is_active' => 1,
b958933f 246 );
247
e830c6ae 248 $customField = $this->callAPISuccess('custom_field', 'create', $params);
b958933f 249 $this->assertNotNull($customField['id']);
e830c6ae 250 $optionGroupID = $this->callAPISuccess('custom_field', 'getvalue', array(
b958933f 251 'id' => $customField['id'],
252 'return' => 'option_group_id',
253 ));
254
255 $this->assertTrue(is_numeric($optionGroupID) && ($optionGroupID > 0));
e830c6ae 256 $optionGroup = $this->callAPISuccess('option_group', 'getsingle', array(
257 'id' => $optionGroupID));
b958933f 258 $this->assertEquals($optionGroup['title'],'Country');
e830c6ae 259 $optionValueCount = $this->callAPISuccess('option_value', 'getcount', array(
260 'option_group_id' => $optionGroupID));
b958933f 261 $this->assertEquals(0, $optionValueCount);
262 }
263
eebb53df
JV
264 /**
265 * Test custom field with existing option group
266 */
267 function testCustomFieldExistingOptionGroup() {
268 $customGroup = $this->customGroupCreate(array('extends' => 'Organization', 'title' => 'test_group'));
269 $params = array(
270 'custom_group_id' => $customGroup['id'],
271 // Just to say something:
272 'label' => 'Organization Gender',
273 'html_type' => 'Select',
274 'data_type' => 'Int',
275 'weight' => 4,
276 'is_required' => 1,
277 'is_searchable' => 0,
278 'is_active' => 1,
279 // Option group id 3: gender
280 'option_group_id' => 3,
281 );
282
283 $customField = $this->callAPISuccess('custom_field', 'create', $params);
284 $this->assertNotNull($customField['id']);
285 $optionGroupID = $this->callAPISuccess('custom_field', 'getvalue', array(
286 'id' => $customField['id'],
287 'return' => 'option_group_id',
288 ));
289
290 $this->assertEquals($optionGroupID,3);
291 }
292
b958933f 293
3c70d501 294 /**
295 * Test custom field get works & return param works
296 */
297 function testCustomFieldGetReturnOptions(){
e830c6ae 298 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
b422b715 299 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
3c70d501 300
e830c6ae 301 $result = $this->callAPISuccess('custom_field', 'getsingle', array(
3c70d501 302 'id' => $customField['id'],
303 'return' => 'data_type',
304 ));
305 $this->assertTrue(array_key_exists('data_type', $result));
306 $this->assertFalse(array_key_exists('custom_group_id', $result));
307 }
308
309 /**
310 * Test custom field get works & return param works
311 */
312 function testCustomFieldGetReturnArray(){
e830c6ae 313 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
b422b715 314 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
3c70d501 315
e830c6ae 316 $result = $this->callAPISuccess('custom_field', 'getsingle', array(
317 'id' => $customField['id'],
3c70d501 318 'return' => array('data_type'),
319 ));
320 $this->assertTrue(array_key_exists('data_type', $result));
321 $this->assertFalse(array_key_exists('custom_group_id', $result));
322 }
323
324 /**
325 * Test custom field get works & return param works
326 */
327 function testCustomFieldGetReturnTwoOptions(){
e830c6ae 328 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'test_group'));
b422b715 329 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
3c70d501 330
e830c6ae 331 $result = $this->callAPISuccess('custom_field', 'getsingle', array(
332 'id' => $customField['id'],
3c70d501 333 'return' => 'data_type, custom_group_id',
334 ));
335 $this->assertTrue(array_key_exists('data_type', $result));
336 $this->assertTrue(array_key_exists('custom_group_id', $result));
337 $this->assertFalse(array_key_exists('label', $result));
338 }
339
6a488035 340 function testCustomFieldCreateWithOptionValues() {
e830c6ae 341 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'select_test_group'));
6a488035
TO
342
343 $option_values = array(
344 array('weight' => 1,
345 'label' => 'Label1',
346 'value' => 1,
347 'is_active' => 1,
348 ),
349 array(
350 'weight' => 2,
351 'label' => 'Label2',
352 'value' => 2,
353 'is_active' => 1,
354 ),
355 );
356
357 $params = array(
358 'custom_group_id' => $customGroup['id'],
7f6d3dd3 359 'label' => 'Our special field',
6a488035
TO
360 'html_type' => 'Select',
361 'data_type' => 'String',
362 'weight' => 4,
363 'is_required' => 1,
364 'is_searchable' => 0,
365 'is_active' => 1,
366 'option_values' => $option_values,
e830c6ae 367
6a488035
TO
368 );
369
e830c6ae 370 $customField = $this->callAPISuccess('custom_field', 'create', $params);
6a488035 371
b958933f 372 $this->assertAPISuccess($customField);
6a488035
TO
373 $this->assertNotNull($customField['id']);
374 $getFieldsParams = array(
375 'options' => array('get_options' => 'custom_' . $customField['id']),
e830c6ae 376 'action' => 'create',
6a488035 377 );
a4c5e9a3 378 $description = "Demonstrate retrieving metadata with custom field options";
6a488035 379 $subfile = "GetFieldsOptions";
e830c6ae 380 $fields = $this->callAPIAndDocument('contact', 'getfields', $getFieldsParams, __FUNCTION__, 'ContactTest.php', $description,$subfile,'GetFields');
6a488035
TO
381 $this->assertArrayHasKey('options', $fields['values']['custom_' . $customField['id']]);
382 $this->assertEquals('Label1', $fields['values']['custom_' . $customField['id']]['options'][1]);
383 $getOptionsArray = array(
384 'field' => 'custom_' . $customField['id'],
e830c6ae 385 );
6a488035
TO
386 $description = "Demonstrates retrieving options for a custom field";
387 $subfile = "GetOptions";
e830c6ae 388 $result = $this->callAPIAndDocument('contact', 'getoptions', $getOptionsArray, __FUNCTION__, 'ContactTest.php', $description, '', 'getoptions');
6a488035 389 $this->assertEquals('Label1', $result['values'][1]);
6a488035
TO
390 }
391
392 ///////////////// civicrm_custom_field_delete methods
393
394 /**
100fef9d 395 * Check with no array
6a488035
TO
396 */
397 function testCustomFieldDeleteNoArray() {
398 $params = NULL;
d0e1eff2 399 $customField = $this->callAPIFailure('custom_field', 'delete', $params);
6a488035
TO
400 $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array');
401 }
402
403 /**
100fef9d 404 * Check without Field ID
6a488035
TO
405 */
406 function testCustomFieldDeleteWithoutFieldID() {
e830c6ae 407 $params = array();
408 $customField = $this->callAPIFailure('custom_field', 'delete', $params,
409 'Mandatory key(s) missing from params array: id');
6a488035
TO
410 }
411
412 /**
100fef9d 413 * Check without valid array
6a488035
TO
414 */
415 function testCustomFieldDelete() {
e830c6ae 416 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
b422b715 417 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
6a488035
TO
418 $this->assertNotNull($customField['id'], 'in line ' . __LINE__);
419
420 $params = array(
6a488035
TO
421 'id' => $customField['id'],
422 );
e830c6ae 423 $result = $this->callAPIAndDocument('custom_field', 'delete', $params, __FUNCTION__, __FILE__);
6a488035 424
791c263c 425 $this->assertAPISuccess($result, 'in line ' . __LINE__);
6a488035
TO
426 }
427
428 /**
100fef9d 429 * Check for Option Value
6a488035
TO
430 */
431 function testCustomFieldOptionValueDelete() {
e830c6ae 432 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'ABC'));
6a488035 433 $customOptionValueFields = $this->customFieldOptionValueCreate($customGroup, 'fieldABC');
6a488035 434 $params = array(
6a488035
TO
435 'id' => $customOptionValueFields,
436 );
437
e830c6ae 438 $customField = $this->callAPISuccess('custom_field', 'delete', $customOptionValueFields);
6a488035 439 }
6b59896e
TO
440
441 /**
442 * If there's one custom group for "Contact" and one for "Activity", then "Contact.getfields"
443 * and "Activity.getfields" should return only their respective fields (not the other's fields),
444 * and unrelated entities should return no custom fields.
445 */
446 function testGetfields_CrossEntityPollution() {
447 $auxEntities = array('Email', 'Address', 'LocBlock', 'Membership', 'ContributionPage', 'ReportInstance');
448 $allEntities = array_merge(array('Contact', 'Activity'), $auxEntities);
449
450 // Baseline - getfields doesn't reporting any customfields for any entities
451 foreach ($allEntities as $entity) {
452 $this->assertEquals(
453 array(),
454 $this->getCustomFieldKeys($this->callAPISuccess($entity, 'getfields', array())),
455 "Baseline custom fields for $entity should be empty"
456 );
457 }
458
459 // Add some fields
f8a3cd29 460 $contactGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'test_group_c'));
6b59896e 461 $contactField = $this->customFieldCreate(array('custom_group_id' => $contactGroup['id'], 'label' => 'For Contacts'));
f8a3cd29
TO
462 $indivGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group_i'));
463 $indivField = $this->customFieldCreate(array('custom_group_id' => $indivGroup['id'], 'label' => 'For Individuals'));
464 $activityGroup = $this->customGroupCreate(array('extends' => 'Activity', 'title' => 'test_group_a'));
6b59896e
TO
465 $activityField = $this->customFieldCreate(array('custom_group_id' => $activityGroup['id'], 'label' => 'For Activities'));
466
f8a3cd29
TO
467 // Check getfields
468 $this->assertEquals(
469 array('custom_' . $contactField['id'], 'custom_' . $indivField['id']),
470 $this->getCustomFieldKeys($this->callAPISuccess('Contact', 'getfields', array())),
471 'Contact custom fields'
472 );
473 $this->assertEquals(
474 array('custom_' . $contactField['id'], 'custom_' . $indivField['id']),
475 $this->getCustomFieldKeys($this->callAPISuccess('Individual', 'getfields', array())),
476 'Individual custom fields'
477 );
478 $this->assertEquals(
479 array('custom_' . $contactField['id']),
480 $this->getCustomFieldKeys($this->callAPISuccess('Organization', 'getfields', array())),
481 'Organization custom fields'
482 );
6b59896e
TO
483 $this->assertEquals(
484 array('custom_' . $activityField['id']),
485 $this->getCustomFieldKeys($this->callAPISuccess('Activity', 'getfields', array())),
486 'Activity custom fields'
487 );
488 foreach ($auxEntities as $entity) {
489 $this->assertEquals(
490 array(),
491 $this->getCustomFieldKeys($this->callAPISuccess($entity, 'getfields', array())),
492 "Custom fields for $entity should be empty"
493 );
494 }
495 }
496
4cbe18b8
EM
497 /**
498 * @param $getFieldsResult
499 *
500 * @return array
501 */
6b59896e
TO
502 function getCustomFieldKeys($getFieldsResult) {
503 $isCustom = function($key) {
504 return preg_match('/^custom_/', $key);
505 };
f8a3cd29
TO
506 $r = array_values(array_filter(array_keys($getFieldsResult['values']), $isCustom));
507 sort($r);
508 return $r;
6b59896e 509 }
6a488035
TO
510}
511