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