Merge branch 'master' into master-civimail-abtest
[civicrm-core.git] / tests / phpunit / api / v3 / CustomFieldTest.php
... / ...
CommitLineData
1<?php
2/*
3 +--------------------------------------------------------------------+
4| CiviCRM version 4.5 |
5+--------------------------------------------------------------------+
6| Copyright CiviCRM LLC (c) 2004-2014 |
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 */
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;
41
42 /**
43 * @return array
44 */
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 /**
67 * Check with no array
68 */
69 function testCustomFieldCreateNoArray() {
70 $fieldParams = NULL;
71
72 $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams);
73 $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array');
74 }
75
76 /**
77 * Check with no label
78 */
79 function testCustomFieldCreateWithoutLabel() {
80 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'text_test_group'));
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,
91 );
92
93 $customField = $this->callAPIFailure('custom_field', 'create', $params);
94 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: label');
95 }
96
97 /**
98 * Check with edit
99 */
100 function testCustomFieldCreateWithEdit() {
101 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'text_test_group'));
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,
113 );
114
115 $customField = $this->callAPIAndDocument('custom_field', 'create', $params, __FUNCTION__, __FILE__);
116 $params['id'] = $customField['id'];
117 $customField = $this->callAPISuccess('custom_field', 'create', $params);
118
119 $this->assertNotNull($customField['id'], 'in line ' . __LINE__);
120 }
121
122 /**
123 * Check without groupId
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,
136
137 );
138
139 $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams);
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() {
147 $gid = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'testAllFormInputs'));
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 */
164 /**
165 * @param array $params
166 */
167 function _loopingCustomFieldCreateTest($params) {
168 $customField = $this->callAPISuccess('custom_field', 'create', $params);
169 $this->assertNotNull($customField['id']);
170 $this->getAndCheck($params, $customField['id'], 'CustomField');
171 }
172
173 /**
174 * @param int $gid
175 * @param $htype
176 * @param $dtype
177 *
178 * @return array
179 */
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
197 /**
198 * @param int $gid
199 * @param $htype
200 * @param $dtype
201 *
202 * @return array
203 */
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,
214
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);
226 require_once 'api/v3/examples/CustomField/Create.php';
227 $result = custom_field_create_example();
228 $expectedResult = custom_field_create_expectedresult();
229 $this->assertEquals($result,$expectedResult);
230 }*/
231
232 /**
233 * Check with data type - Options with option_values
234 */
235 function testCustomFieldCreateWithEmptyOptionGroup() {
236 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'select_test_group'));
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,
246 );
247
248 $customField = $this->callAPISuccess('custom_field', 'create', $params);
249 $this->assertNotNull($customField['id']);
250 $optionGroupID = $this->callAPISuccess('custom_field', 'getvalue', array(
251 'id' => $customField['id'],
252 'return' => 'option_group_id',
253 ));
254
255 $this->assertTrue(is_numeric($optionGroupID) && ($optionGroupID > 0));
256 $optionGroup = $this->callAPISuccess('option_group', 'getsingle', array(
257 'id' => $optionGroupID));
258 $this->assertEquals($optionGroup['title'],'Country');
259 $optionValueCount = $this->callAPISuccess('option_value', 'getcount', array(
260 'option_group_id' => $optionGroupID));
261 $this->assertEquals(0, $optionValueCount);
262 }
263
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
293
294 /**
295 * Test custom field get works & return param works
296 */
297 function testCustomFieldGetReturnOptions(){
298 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
299 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
300
301 $result = $this->callAPISuccess('custom_field', 'getsingle', array(
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(){
313 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
314 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
315
316 $result = $this->callAPISuccess('custom_field', 'getsingle', array(
317 'id' => $customField['id'],
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(){
328 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'test_group'));
329 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
330
331 $result = $this->callAPISuccess('custom_field', 'getsingle', array(
332 'id' => $customField['id'],
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
340 function testCustomFieldCreateWithOptionValues() {
341 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'select_test_group'));
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'],
359 'label' => 'Our special field',
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,
367
368 );
369
370 $customField = $this->callAPISuccess('custom_field', 'create', $params);
371
372 $this->assertAPISuccess($customField);
373 $this->assertNotNull($customField['id']);
374 $getFieldsParams = array(
375 'options' => array('get_options' => 'custom_' . $customField['id']),
376 'action' => 'create',
377 );
378 $description = "Demonstrate retrieving metadata with custom field options";
379 $subfile = "GetFieldsOptions";
380 $fields = $this->callAPIAndDocument('contact', 'getfields', $getFieldsParams, __FUNCTION__, 'ContactTest.php', $description,$subfile,'GetFields');
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'],
385 );
386 $description = "Demonstrates retrieving options for a custom field";
387 $subfile = "GetOptions";
388 $result = $this->callAPIAndDocument('contact', 'getoptions', $getOptionsArray, __FUNCTION__, 'ContactTest.php', $description, '', 'getoptions');
389 $this->assertEquals('Label1', $result['values'][1]);
390 }
391
392 ///////////////// civicrm_custom_field_delete methods
393
394 /**
395 * Check with no array
396 */
397 function testCustomFieldDeleteNoArray() {
398 $params = NULL;
399 $customField = $this->callAPIFailure('custom_field', 'delete', $params);
400 $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array');
401 }
402
403 /**
404 * Check without Field ID
405 */
406 function testCustomFieldDeleteWithoutFieldID() {
407 $params = array();
408 $customField = $this->callAPIFailure('custom_field', 'delete', $params,
409 'Mandatory key(s) missing from params array: id');
410 }
411
412 /**
413 * Check without valid array
414 */
415 function testCustomFieldDelete() {
416 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
417 $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id']));
418 $this->assertNotNull($customField['id'], 'in line ' . __LINE__);
419
420 $params = array(
421 'id' => $customField['id'],
422 );
423 $result = $this->callAPIAndDocument('custom_field', 'delete', $params, __FUNCTION__, __FILE__);
424
425 $this->assertAPISuccess($result, 'in line ' . __LINE__);
426 }
427
428 /**
429 * Check for Option Value
430 */
431 function testCustomFieldOptionValueDelete() {
432 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'ABC'));
433 $customOptionValueFields = $this->customFieldOptionValueCreate($customGroup, 'fieldABC');
434 $params = array(
435 'id' => $customOptionValueFields,
436 );
437
438 $customField = $this->callAPISuccess('custom_field', 'delete', $customOptionValueFields);
439 }
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
460 $contactGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'test_group_c'));
461 $contactField = $this->customFieldCreate(array('custom_group_id' => $contactGroup['id'], 'label' => 'For Contacts'));
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'));
465 $activityField = $this->customFieldCreate(array('custom_group_id' => $activityGroup['id'], 'label' => 'For Activities'));
466
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 );
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
497 /**
498 * @param $getFieldsResult
499 *
500 * @return array
501 */
502 function getCustomFieldKeys($getFieldsResult) {
503 $isCustom = function($key) {
504 return preg_match('/^custom_/', $key);
505 };
506 $r = array_values(array_filter(array_keys($getFieldsResult['values']), $isCustom));
507 sort($r);
508 return $r;
509 }
510}
511