fix test regression related to name clash not tested action
[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 = civicrm_api('custom_field', 'create', $fieldParams);
71 $this->assertEquals($customField['is_error'], 1);
72 $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array');
73 }
74
75 /**
76 * check with no label
77 */
78 function testCustomFieldCreateWithoutLabel() {
79 $customGroup = $this->customGroupCreate('Individual', 'text_test_group', 3);
80 $params = array(
81 'custom_group_id' => $customGroup['id'],
82 'name' => 'test_textfield2',
83 'html_type' => 'Text',
84 'data_type' => 'String',
85 'default_value' => 'abc',
86 'weight' => 4,
87 'is_required' => 1,
88 'is_searchable' => 0,
89 'is_active' => 1,
90 'version' => $this->_apiversion,
91 );
92
93 $customField = civicrm_api('custom_field', 'create', $params);
94 $this->assertEquals($customField['is_error'], 1);
95 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: label');
96 }
97
98 /**
99 * check with edit
100 */
101 function testCustomFieldCreateWithEdit() {
102 $customGroup = $this->customGroupCreate('Individual', 'text_test_group', 3);
103 $params = array(
104 'custom_group_id' => $customGroup['id'],
105 'name' => 'test_textfield2',
106 'label' => 'Name1',
107 'html_type' => 'Text',
108 'data_type' => 'String',
109 'default_value' => 'abc',
110 'weight' => 4,
111 'is_required' => 1,
112 'is_searchable' => 0,
113 'is_active' => 1,
114 'version' => $this->_apiversion,
115 );
116
117 $customField = civicrm_api('custom_field', 'create', $params);
118 $params['id'] = $customField['id'];
119 $customField = civicrm_api('custom_field', 'create', $params);
120
121 $this->assertEquals($customField['is_error'], 0, 'in line ' . __LINE__);
122 $this->assertNotNull($customField['id'], 'in line ' . __LINE__);
123 }
124
125 /**
126 * check without groupId
127 */
128 function testCustomFieldCreateWithoutGroupID() {
129 $fieldParams = array(
130 'name' => 'test_textfield1',
131 'label' => 'Name',
132 'html_type' => 'Text',
133 'data_type' => 'String',
134 'default_value' => 'abc',
135 'weight' => 4,
136 'is_required' => 1,
137 'is_searchable' => 0,
138 'is_active' => 1,
139 'version' => $this->_apiversion,
140 );
141
142 $customField = civicrm_api('custom_field', 'create', $fieldParams);
143 $this->assertEquals($customField['is_error'], 1);
144 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: custom_group_id');
145 }
146
147 /**
148 * Check for Each data type: loop through available form input types
149 **/
150 function testCustomFieldCreateAllAvailableFormInputs() {
151 $gid = $this->customGroupCreate('Individual', 'testAllFormInputs');
152
153 $dtype = CRM_Core_BAO_CustomField::dataType();
154 $htype = CRM_Core_BAO_CustomField::dataToHtml();
155
156 $n = 0;
157 foreach ($dtype as $dkey => $dvalue) {
158 foreach ($htype[$n] as $hkey => $hvalue) {
159 //echo $dkey."][".$hvalue."\n";
160 $this->_loopingCustomFieldCreateTest($this->_buildParams($gid['id'], $hvalue, $dkey));
161 }
162 $n++;
163 }
164 }
165 /*
166 * Can't figure out the point of this?
167 */
168 function _loopingCustomFieldCreateTest($params) {
169 $customField = civicrm_api('custom_field', 'create', $params);
170 $this->assertEquals(0, $customField['is_error'], var_export($customField, TRUE));
171 $this->assertNotNull($customField['id']);
172 $this->getAndCheck($params, $customField['id'], 'CustomField');
173 }
174
175 function _buildParams($gid, $htype, $dtype) {
176 $params = $this->_buildBasicParams($gid, $htype, $dtype);
177 /* //Not Working for any type. Maybe redundant with testCustomFieldCreateWithOptionValues()
178 if ($htype == 'Multi-Select')
179 $params = array_merge($params, array(
180 'option_label' => array( 'Label1','Label2'),
181 'option_value' => array( 'val1', 'val2' ),
182 'option_weight' => array( 1, 2),
183 'option_status' => array( 1, 1),
184 ));
185 */
186
187
188
189 return $params;
190 }
191
192 function _buildBasicParams($gid, $htype, $dtype) {
193 return array(
194 'custom_group_id' => $gid,
195 'label' => $dtype . $htype,
196 'html_type' => $htype,
197 'data_type' => $dtype,
198 'weight' => 4,
199 'is_required' => 0,
200 'is_searchable' => 0,
201 'is_active' => 1,
202 'version' => $this->_apiversion,
203 );
204 }
205
206 /**
207 * Test using example code
208 */
209 /*function testCustomFieldCreateExample( )
210 {
211
212
213 $customGroup = $this->customGroupCreate('Individual','date_test_group',3);
214 require_once 'api/v3/examples/CustomFieldCreate.php';
215 $result = custom_field_create_example();
216 $expectedResult = custom_field_create_expectedresult();
217 $this->assertEquals($result,$expectedResult);
218 }*/
219
220 /**
221 * check with data type - Options with option_values
222 */
223 function testCustomFieldCreateWithEmptyOptionGroup() {
224 $customGroup = $this->customGroupCreate('Contact', 'select_test_group', 3);
225 $params = array(
226 'custom_group_id' => $customGroup['id'],
227 'label' => 'Country',
228 'html_type' => 'Select',
229 'data_type' => 'String',
230 'weight' => 4,
231 'is_required' => 1,
232 'is_searchable' => 0,
233 'is_active' => 1,
234 'version' => $this->_apiversion,
235 );
236
237 $customField = civicrm_api('custom_field', 'create', $params);
238 $this->assertAPISuccess($customField);
239 $this->assertNotNull($customField['id']);
240 $optionGroupID = civicrm_api('custom_field', 'getvalue', array(
241 'version' => 3,
242 'id' => $customField['id'],
243 'return' => 'option_group_id',
244 ));
245
246 $this->assertTrue(is_numeric($optionGroupID) && ($optionGroupID > 0));
247 $optionGroup = civicrm_api('option_group', 'getsingle', array(
248 'version' => 3, 'id' => $optionGroupID));
249 $this->assertEquals($optionGroup['title'],'Country');
250 $optionValueCount = civicrm_api('option_value', 'getcount', array(
251 'version' => 3, 'option_group_id' => $optionGroupID));
252 $this->assertEquals(0, $optionValueCount);
253 }
254
255
256 /**
257 * Test custom field get works & return param works
258 */
259 function testCustomFieldGetReturnOptions(){
260 $customGroup = $this->customGroupCreate('Individual', 'test_group');
261 $customField = $this->customFieldCreate($customGroup['id'], 'test_name');
262
263 $result = civicrm_api('custom_field', 'getsingle', array(
264 'version' => 3,
265 'id' => $customField['id'],
266 'return' => 'data_type',
267 ));
268 $this->assertTrue(array_key_exists('data_type', $result));
269 $this->assertFalse(array_key_exists('custom_group_id', $result));
270 }
271
272 /**
273 * Test custom field get works & return param works
274 */
275 function testCustomFieldGetReturnArray(){
276 $customGroup = $this->customGroupCreate('Individual', 'test_group');
277 $customField = $this->customFieldCreate($customGroup['id'], 'test_name');
278
279 $result = civicrm_api('custom_field', 'getsingle', array(
280 'version' => 3,
281 'id' => $customField['id'],
282 'return' => array('data_type'),
283 ));
284 $this->assertTrue(array_key_exists('data_type', $result));
285 $this->assertFalse(array_key_exists('custom_group_id', $result));
286 }
287
288 /**
289 * Test custom field get works & return param works
290 */
291 function testCustomFieldGetReturnTwoOptions(){
292 $customGroup = $this->customGroupCreate('Individual', 'test_group');
293 $customField = $this->customFieldCreate($customGroup['id'], 'test_name');
294
295 $result = civicrm_api('custom_field', 'getsingle', array(
296 'version' => 3,
297 'id' => $customField['id'],
298 'return' => 'data_type, custom_group_id',
299 ));
300 $this->assertTrue(array_key_exists('data_type', $result));
301 $this->assertTrue(array_key_exists('custom_group_id', $result));
302 $this->assertFalse(array_key_exists('label', $result));
303 }
304
305 function testCustomFieldCreateWithOptionValues() {
306 $customGroup = $this->customGroupCreate('Contact', 'select_test_group', 3);
307
308 $option_values = array(
309 array('weight' => 1,
310 'label' => 'Label1',
311 'value' => 1,
312 'is_active' => 1,
313 ),
314 array(
315 'weight' => 2,
316 'label' => 'Label2',
317 'value' => 2,
318 'is_active' => 1,
319 ),
320 );
321
322 $params = array(
323 'custom_group_id' => $customGroup['id'],
324 'label' => 'Our special field',
325 'html_type' => 'Select',
326 'data_type' => 'String',
327 'weight' => 4,
328 'is_required' => 1,
329 'is_searchable' => 0,
330 'is_active' => 1,
331 'option_values' => $option_values,
332 'version' => $this->_apiversion,
333 );
334
335 $customField = civicrm_api('custom_field', 'create', $params);
336
337 $this->assertAPISuccess($customField);
338 $this->assertNotNull($customField['id']);
339 $getFieldsParams = array(
340 'options' => array('get_options' => 'custom_' . $customField['id']),
341 'version' => 3,
342 'action' => 'create',
343 );
344 $description = "Demonstrate retrieving custom field options";
345 $subfile = "GetFieldsOptions";
346 $fields = civicrm_api('contact', 'getfields', $getFieldsParams);
347 $this->documentMe($getFieldsParams, $fields, __FUNCTION__, 'ContactTest.php', $description,$subfile,'GetFields');
348 $this->assertArrayHasKey('options', $fields['values']['custom_' . $customField['id']]);
349 $this->assertEquals('Label1', $fields['values']['custom_' . $customField['id']]['options'][1]);
350 $getOptionsArray = array(
351 'field' => 'custom_' . $customField['id'],
352 'version' => 3,
353 );
354 $description = "Demonstrates retrieving options for a custom field";
355 $subfile = "GetOptions";
356 $result = civicrm_api('contact', 'getoptions', $getOptionsArray);
357 $this->assertEquals('Label1', $result['values'][1]);
358 $this->documentMe($getOptionsArray, $result, __FUNCTION__, 'ContactTest.php', $description, '', 'getoptions');
359 }
360
361 ///////////////// civicrm_custom_field_delete methods
362
363 /**
364 * check with no array
365 */
366 function testCustomFieldDeleteNoArray() {
367 $params = NULL;
368 $customField = civicrm_api('custom_field', 'delete', $params);
369 $this->assertEquals($customField['is_error'], 1);
370 $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array');
371 }
372
373 /**
374 * check without Field ID
375 */
376 function testCustomFieldDeleteWithoutFieldID() {
377 $params = array('version' => $this->_apiversion);
378 $customField = civicrm_api('custom_field', 'delete', $params);
379 $this->assertEquals($customField['is_error'], 1);
380 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: id');
381 }
382
383 /**
384 * check without valid array
385 */
386 function testCustomFieldDelete() {
387 $customGroup = $this->customGroupCreate('Individual', 'test_group');
388 $customField = $this->customFieldCreate($customGroup['id'], 'test_name');
389 $this->assertNotNull($customField['id'], 'in line ' . __LINE__);
390
391 $params = array(
392 'version' => $this->_apiversion,
393 'id' => $customField['id'],
394 );
395 $result = civicrm_api('custom_field', 'delete', $params);
396 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
397
398 $this->assertEquals($result['is_error'], 0, 'in line ' . __LINE__);
399 }
400
401 /**
402 * check for Option Value
403 */
404 function testCustomFieldOptionValueDelete() {
405 $customGroup = $this->customGroupCreate('Contact', 'ABC');
406 $customOptionValueFields = $this->customFieldOptionValueCreate($customGroup, 'fieldABC');
407 $customOptionValueFields['version'] = $this->_apiversion;
408 $params = array(
409 'version' => $this->_apiversion,
410 'id' => $customOptionValueFields,
411 );
412
413 $customField = civicrm_api('custom_field', 'delete', $customOptionValueFields);
414 $this->assertAPISuccess($customField);
415 }
416 }
417