Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
b6708aeb | 2 | /* |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
b6708aeb | 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 | */ | |
6a488035 TO |
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 | ||
d0e1eff2 | 69 | $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams); |
6a488035 TO |
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() { | |
e830c6ae | 77 | $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'text_test_group')); |
6a488035 TO |
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, | |
6a488035 TO |
88 | ); |
89 | ||
d0e1eff2 | 90 | $customField = $this->callAPIFailure('custom_field', 'create', $params); |
6a488035 TO |
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() { | |
e830c6ae | 98 | $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'text_test_group')); |
6a488035 TO |
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, | |
6a488035 TO |
110 | ); |
111 | ||
fb32de45 | 112 | $customField = $this->callAPIAndDocument('custom_field', 'create', $params, __FUNCTION__, __FILE__); |
6a488035 | 113 | $params['id'] = $customField['id']; |
e830c6ae | 114 | $customField = $this->callAPISuccess('custom_field', 'create', $params); |
6a488035 | 115 | |
6a488035 TO |
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, | |
e830c6ae | 133 | |
6a488035 TO |
134 | ); |
135 | ||
d0e1eff2 | 136 | $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams); |
6a488035 TO |
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() { | |
e830c6ae | 144 | $gid = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'testAllFormInputs')); |
6a488035 TO |
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) { | |
e830c6ae | 162 | $customField = $this->callAPISuccess('custom_field', 'create', $params); |
6a488035 TO |
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, | |
e830c6ae | 194 | |
6a488035 TO |
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 | */ | |
b958933f | 215 | function testCustomFieldCreateWithEmptyOptionGroup() { |
e830c6ae | 216 | $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'select_test_group')); |
b958933f | 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, | |
b958933f | 226 | ); |
227 | ||
e830c6ae | 228 | $customField = $this->callAPISuccess('custom_field', 'create', $params); |
b958933f | 229 | $this->assertNotNull($customField['id']); |
e830c6ae | 230 | $optionGroupID = $this->callAPISuccess('custom_field', 'getvalue', array( |
b958933f | 231 | 'id' => $customField['id'], |
232 | 'return' => 'option_group_id', | |
233 | )); | |
234 | ||
235 | $this->assertTrue(is_numeric($optionGroupID) && ($optionGroupID > 0)); | |
e830c6ae | 236 | $optionGroup = $this->callAPISuccess('option_group', 'getsingle', array( |
237 | 'id' => $optionGroupID)); | |
b958933f | 238 | $this->assertEquals($optionGroup['title'],'Country'); |
e830c6ae | 239 | $optionValueCount = $this->callAPISuccess('option_value', 'getcount', array( |
240 | 'option_group_id' => $optionGroupID)); | |
b958933f | 241 | $this->assertEquals(0, $optionValueCount); |
242 | } | |
243 | ||
244 | ||
3c70d501 | 245 | /** |
246 | * Test custom field get works & return param works | |
247 | */ | |
248 | function testCustomFieldGetReturnOptions(){ | |
e830c6ae | 249 | $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group')); |
3c70d501 | 250 | $customField = $this->customFieldCreate($customGroup['id'], 'test_name'); |
251 | ||
e830c6ae | 252 | $result = $this->callAPISuccess('custom_field', 'getsingle', array( |
3c70d501 | 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(){ | |
e830c6ae | 264 | $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group')); |
3c70d501 | 265 | $customField = $this->customFieldCreate($customGroup['id'], 'test_name'); |
266 | ||
e830c6ae | 267 | $result = $this->callAPISuccess('custom_field', 'getsingle', array( |
268 | 'id' => $customField['id'], | |
3c70d501 | 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(){ | |
e830c6ae | 279 | $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'test_group')); |
3c70d501 | 280 | $customField = $this->customFieldCreate($customGroup['id'], 'test_name'); |
281 | ||
e830c6ae | 282 | $result = $this->callAPISuccess('custom_field', 'getsingle', array( |
283 | 'id' => $customField['id'], | |
3c70d501 | 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 | ||
6a488035 | 291 | function testCustomFieldCreateWithOptionValues() { |
e830c6ae | 292 | $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'select_test_group')); |
6a488035 TO |
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'], | |
7f6d3dd3 | 310 | 'label' => 'Our special field', |
6a488035 TO |
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, | |
e830c6ae | 318 | |
6a488035 TO |
319 | ); |
320 | ||
e830c6ae | 321 | $customField = $this->callAPISuccess('custom_field', 'create', $params); |
6a488035 | 322 | |
b958933f | 323 | $this->assertAPISuccess($customField); |
6a488035 TO |
324 | $this->assertNotNull($customField['id']); |
325 | $getFieldsParams = array( | |
326 | 'options' => array('get_options' => 'custom_' . $customField['id']), | |
e830c6ae | 327 | 'action' => 'create', |
6a488035 | 328 | ); |
a4c5e9a3 | 329 | $description = "Demonstrate retrieving metadata with custom field options"; |
6a488035 | 330 | $subfile = "GetFieldsOptions"; |
e830c6ae | 331 | $fields = $this->callAPIAndDocument('contact', 'getfields', $getFieldsParams, __FUNCTION__, 'ContactTest.php', $description,$subfile,'GetFields'); |
6a488035 TO |
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'], | |
e830c6ae | 336 | ); |
6a488035 TO |
337 | $description = "Demonstrates retrieving options for a custom field"; |
338 | $subfile = "GetOptions"; | |
e830c6ae | 339 | $result = $this->callAPIAndDocument('contact', 'getoptions', $getOptionsArray, __FUNCTION__, 'ContactTest.php', $description, '', 'getoptions'); |
6a488035 | 340 | $this->assertEquals('Label1', $result['values'][1]); |
6a488035 TO |
341 | } |
342 | ||
343 | ///////////////// civicrm_custom_field_delete methods | |
344 | ||
345 | /** | |
346 | * check with no array | |
347 | */ | |
348 | function testCustomFieldDeleteNoArray() { | |
349 | $params = NULL; | |
d0e1eff2 | 350 | $customField = $this->callAPIFailure('custom_field', 'delete', $params); |
6a488035 TO |
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() { | |
e830c6ae | 358 | $params = array(); |
359 | $customField = $this->callAPIFailure('custom_field', 'delete', $params, | |
360 | 'Mandatory key(s) missing from params array: id'); | |
6a488035 TO |
361 | } |
362 | ||
363 | /** | |
364 | * check without valid array | |
365 | */ | |
366 | function testCustomFieldDelete() { | |
e830c6ae | 367 | $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group')); |
6a488035 TO |
368 | $customField = $this->customFieldCreate($customGroup['id'], 'test_name'); |
369 | $this->assertNotNull($customField['id'], 'in line ' . __LINE__); | |
370 | ||
371 | $params = array( | |
6a488035 TO |
372 | 'id' => $customField['id'], |
373 | ); | |
e830c6ae | 374 | $result = $this->callAPIAndDocument('custom_field', 'delete', $params, __FUNCTION__, __FILE__); |
6a488035 | 375 | |
791c263c | 376 | $this->assertAPISuccess($result, 'in line ' . __LINE__); |
6a488035 TO |
377 | } |
378 | ||
379 | /** | |
380 | * check for Option Value | |
381 | */ | |
382 | function testCustomFieldOptionValueDelete() { | |
e830c6ae | 383 | $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'ABC')); |
6a488035 | 384 | $customOptionValueFields = $this->customFieldOptionValueCreate($customGroup, 'fieldABC'); |
6a488035 | 385 | $params = array( |
6a488035 TO |
386 | 'id' => $customOptionValueFields, |
387 | ); | |
388 | ||
e830c6ae | 389 | $customField = $this->callAPISuccess('custom_field', 'delete', $customOptionValueFields); |
6a488035 TO |
390 | } |
391 | } | |
392 |