Merge pull request #1031 from deepak-srivastava/hr
[civicrm-core.git] / tests / phpunit / api / v3 / CustomFieldTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 /**
30 * Include class definitions
31 */
32 require_once 'tests/phpunit/CiviTest/CiviUnitTestCase.php';
33
34
35 /**
36 * Test APIv3 civicrm_create_custom_group
37 *
38 * @package CiviCRM
39 */
40 class api_v3_CustomFieldTest extends CiviUnitTestCase {
41 protected $_apiversion;
42 public $_eNoticeCompliant = TRUE;
43 function get_info() {
44 return array(
45 'name' => 'Custom Field Create',
46 'description' => 'Test all Custom Field Create API methods.',
47 'group' => 'CiviCRM API Tests',
48 );
49 }
50
51 function setUp() {
52 $this->_apiversion = 3;
53 parent::setUp();
54 }
55
56 function tearDown() {
57 $tablesToTruncate = array(
58 'civicrm_custom_group', 'civicrm_custom_field',
59 );
60 // true tells quickCleanup to drop any tables that might have been created in the test
61 $this->quickCleanup($tablesToTruncate, TRUE);
62 }
63
64 /**
65 * check with no array
66 */
67 function testCustomFieldCreateNoArray() {
68 $fieldParams = NULL;
69
70 $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams);
71 $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array');
72 }
73
74 /**
75 * check with no label
76 */
77 function testCustomFieldCreateWithoutLabel() {
78 $customGroup = $this->customGroupCreate('Individual', 'text_test_group', 3);
79 $params = array(
80 'custom_group_id' => $customGroup['id'],
81 'name' => 'test_textfield2',
82 'html_type' => 'Text',
83 'data_type' => 'String',
84 'default_value' => 'abc',
85 'weight' => 4,
86 'is_required' => 1,
87 'is_searchable' => 0,
88 'is_active' => 1,
89 'version' => $this->_apiversion,
90 );
91
92 $customField = $this->callAPIFailure('custom_field', 'create', $params);
93 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: label');
94 }
95
96 /**
97 * check with edit
98 */
99 function testCustomFieldCreateWithEdit() {
100 $customGroup = $this->customGroupCreate('Individual', 'text_test_group', 3);
101 $params = array(
102 'custom_group_id' => $customGroup['id'],
103 'name' => 'test_textfield2',
104 'label' => 'Name1',
105 'html_type' => 'Text',
106 'data_type' => 'String',
107 'default_value' => 'abc',
108 'weight' => 4,
109 'is_required' => 1,
110 'is_searchable' => 0,
111 'is_active' => 1,
112 'version' => $this->_apiversion,
113 );
114
115 $customField = civicrm_api('custom_field', 'create', $params);
116 $params['id'] = $customField['id'];
117 $customField = civicrm_api('custom_field', 'create', $params);
118
119 $this->assertEquals($customField['is_error'], 0, 'in line ' . __LINE__);
120 $this->assertNotNull($customField['id'], 'in line ' . __LINE__);
121 }
122
123 /**
124 * check without groupId
125 */
126 function testCustomFieldCreateWithoutGroupID() {
127 $fieldParams = array(
128 'name' => 'test_textfield1',
129 'label' => 'Name',
130 'html_type' => 'Text',
131 'data_type' => 'String',
132 'default_value' => 'abc',
133 'weight' => 4,
134 'is_required' => 1,
135 'is_searchable' => 0,
136 'is_active' => 1,
137 'version' => $this->_apiversion,
138 );
139
140 $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams);
141 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: custom_group_id');
142 }
143
144 /**
145 * Check for Each data type: loop through available form input types
146 **/
147 function testCustomFieldCreateAllAvailableFormInputs() {
148 $gid = $this->customGroupCreate('Individual', 'testAllFormInputs');
149
150 $dtype = CRM_Core_BAO_CustomField::dataType();
151 $htype = CRM_Core_BAO_CustomField::dataToHtml();
152
153 $n = 0;
154 foreach ($dtype as $dkey => $dvalue) {
155 foreach ($htype[$n] as $hkey => $hvalue) {
156 //echo $dkey."][".$hvalue."\n";
157 $this->_loopingCustomFieldCreateTest($this->_buildParams($gid['id'], $hvalue, $dkey));
158 }
159 $n++;
160 }
161 }
162 /*
163 * Can't figure out the point of this?
164 */
165 function _loopingCustomFieldCreateTest($params) {
166 $customField = civicrm_api('custom_field', 'create', $params);
167 $this->assertEquals(0, $customField['is_error'], var_export($customField, TRUE));
168 $this->assertNotNull($customField['id']);
169 $this->getAndCheck($params, $customField['id'], 'CustomField');
170 }
171
172 function _buildParams($gid, $htype, $dtype) {
173 $params = $this->_buildBasicParams($gid, $htype, $dtype);
174 /* //Not Working for any type. Maybe redundant with testCustomFieldCreateWithOptionValues()
175 if ($htype == 'Multi-Select')
176 $params = array_merge($params, array(
177 'option_label' => array( 'Label1','Label2'),
178 'option_value' => array( 'val1', 'val2' ),
179 'option_weight' => array( 1, 2),
180 'option_status' => array( 1, 1),
181 ));
182 */
183
184
185
186 return $params;
187 }
188
189 function _buildBasicParams($gid, $htype, $dtype) {
190 return array(
191 'custom_group_id' => $gid,
192 'label' => $dtype . $htype,
193 'html_type' => $htype,
194 'data_type' => $dtype,
195 'weight' => 4,
196 'is_required' => 0,
197 'is_searchable' => 0,
198 'is_active' => 1,
199 'version' => $this->_apiversion,
200 );
201 }
202
203 /**
204 * Test using example code
205 */
206 /*function testCustomFieldCreateExample( )
207 {
208
209
210 $customGroup = $this->customGroupCreate('Individual','date_test_group',3);
211 require_once 'api/v3/examples/CustomFieldCreate.php';
212 $result = custom_field_create_example();
213 $expectedResult = custom_field_create_expectedresult();
214 $this->assertEquals($result,$expectedResult);
215 }*/
216
217 /**
218 * check with data type - Options with option_values
219 */
220 function testCustomFieldCreateWithEmptyOptionGroup() {
221 $customGroup = $this->customGroupCreate('Contact', 'select_test_group', 3);
222 $params = array(
223 'custom_group_id' => $customGroup['id'],
224 'label' => 'Country',
225 'html_type' => 'Select',
226 'data_type' => 'String',
227 'weight' => 4,
228 'is_required' => 1,
229 'is_searchable' => 0,
230 'is_active' => 1,
231 'version' => $this->_apiversion,
232 );
233
234 $customField = civicrm_api('custom_field', 'create', $params);
235 $this->assertAPISuccess($customField);
236 $this->assertNotNull($customField['id']);
237 $optionGroupID = civicrm_api('custom_field', 'getvalue', array(
238 'version' => 3,
239 'id' => $customField['id'],
240 'return' => 'option_group_id',
241 ));
242
243 $this->assertTrue(is_numeric($optionGroupID) && ($optionGroupID > 0));
244 $optionGroup = civicrm_api('option_group', 'getsingle', array(
245 'version' => 3, 'id' => $optionGroupID));
246 $this->assertEquals($optionGroup['title'],'Country');
247 $optionValueCount = civicrm_api('option_value', 'getcount', array(
248 'version' => 3, 'option_group_id' => $optionGroupID));
249 $this->assertEquals(0, $optionValueCount);
250 }
251
252
253 /**
254 * Test custom field get works & return param works
255 */
256 function testCustomFieldGetReturnOptions(){
257 $customGroup = $this->customGroupCreate('Individual', 'test_group');
258 $customField = $this->customFieldCreate($customGroup['id'], 'test_name');
259
260 $result = civicrm_api('custom_field', 'getsingle', array(
261 'version' => 3,
262 'id' => $customField['id'],
263 'return' => 'data_type',
264 ));
265 $this->assertTrue(array_key_exists('data_type', $result));
266 $this->assertFalse(array_key_exists('custom_group_id', $result));
267 }
268
269 /**
270 * Test custom field get works & return param works
271 */
272 function testCustomFieldGetReturnArray(){
273 $customGroup = $this->customGroupCreate('Individual', 'test_group');
274 $customField = $this->customFieldCreate($customGroup['id'], 'test_name');
275
276 $result = civicrm_api('custom_field', 'getsingle', array(
277 'version' => 3,
278 'id' => $customField['id'],
279 'return' => array('data_type'),
280 ));
281 $this->assertTrue(array_key_exists('data_type', $result));
282 $this->assertFalse(array_key_exists('custom_group_id', $result));
283 }
284
285 /**
286 * Test custom field get works & return param works
287 */
288 function testCustomFieldGetReturnTwoOptions(){
289 $customGroup = $this->customGroupCreate('Individual', 'test_group');
290 $customField = $this->customFieldCreate($customGroup['id'], 'test_name');
291
292 $result = civicrm_api('custom_field', 'getsingle', array(
293 'version' => 3,
294 'id' => $customField['id'],
295 'return' => 'data_type, custom_group_id',
296 ));
297 $this->assertTrue(array_key_exists('data_type', $result));
298 $this->assertTrue(array_key_exists('custom_group_id', $result));
299 $this->assertFalse(array_key_exists('label', $result));
300 }
301
302 function testCustomFieldCreateWithOptionValues() {
303 $customGroup = $this->customGroupCreate('Contact', 'select_test_group', 3);
304
305 $option_values = array(
306 array('weight' => 1,
307 'label' => 'Label1',
308 'value' => 1,
309 'is_active' => 1,
310 ),
311 array(
312 'weight' => 2,
313 'label' => 'Label2',
314 'value' => 2,
315 'is_active' => 1,
316 ),
317 );
318
319 $params = array(
320 'custom_group_id' => $customGroup['id'],
321 'label' => 'Our special field',
322 'html_type' => 'Select',
323 'data_type' => 'String',
324 'weight' => 4,
325 'is_required' => 1,
326 'is_searchable' => 0,
327 'is_active' => 1,
328 'option_values' => $option_values,
329 'version' => $this->_apiversion,
330 );
331
332 $customField = civicrm_api('custom_field', 'create', $params);
333
334 $this->assertAPISuccess($customField);
335 $this->assertNotNull($customField['id']);
336 $getFieldsParams = array(
337 'options' => array('get_options' => 'custom_' . $customField['id']),
338 'version' => 3,
339 'action' => 'create',
340 );
341 $description = "Demonstrate retrieving metadata with custom field options";
342 $subfile = "GetFieldsOptions";
343 $fields = civicrm_api('contact', 'getfields', $getFieldsParams);
344 $this->documentMe($getFieldsParams, $fields, __FUNCTION__, 'ContactTest.php', $description,$subfile,'GetFields');
345 $this->assertArrayHasKey('options', $fields['values']['custom_' . $customField['id']]);
346 $this->assertEquals('Label1', $fields['values']['custom_' . $customField['id']]['options'][1]);
347 $getOptionsArray = array(
348 'field' => 'custom_' . $customField['id'],
349 'version' => 3,
350 );
351 $description = "Demonstrates retrieving options for a custom field";
352 $subfile = "GetOptions";
353 $result = civicrm_api('contact', 'getoptions', $getOptionsArray);
354 $this->assertEquals('Label1', $result['values'][1]);
355 $this->documentMe($getOptionsArray, $result, __FUNCTION__, 'ContactTest.php', $description, '', 'getoptions');
356 }
357
358 ///////////////// civicrm_custom_field_delete methods
359
360 /**
361 * check with no array
362 */
363 function testCustomFieldDeleteNoArray() {
364 $params = NULL;
365 $customField = $this->callAPIFailure('custom_field', 'delete', $params);
366 $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array');
367 }
368
369 /**
370 * check without Field ID
371 */
372 function testCustomFieldDeleteWithoutFieldID() {
373 $params = array('version' => $this->_apiversion);
374 $customField = $this->callAPIFailure('custom_field', 'delete', $params);
375 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: id');
376 }
377
378 /**
379 * check without valid array
380 */
381 function testCustomFieldDelete() {
382 $customGroup = $this->customGroupCreate('Individual', 'test_group');
383 $customField = $this->customFieldCreate($customGroup['id'], 'test_name');
384 $this->assertNotNull($customField['id'], 'in line ' . __LINE__);
385
386 $params = array(
387 'version' => $this->_apiversion,
388 'id' => $customField['id'],
389 );
390 $result = civicrm_api('custom_field', 'delete', $params);
391 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
392
393 $this->assertAPISuccess($result, 'in line ' . __LINE__);
394 }
395
396 /**
397 * check for Option Value
398 */
399 function testCustomFieldOptionValueDelete() {
400 $customGroup = $this->customGroupCreate('Contact', 'ABC');
401 $customOptionValueFields = $this->customFieldOptionValueCreate($customGroup, 'fieldABC');
402 $customOptionValueFields['version'] = $this->_apiversion;
403 $params = array(
404 'version' => $this->_apiversion,
405 'id' => $customOptionValueFields,
406 );
407
408 $customField = civicrm_api('custom_field', 'delete', $customOptionValueFields);
409 $this->assertAPISuccess($customField);
410 }
411 }
412