Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
b6708aeb | 2 | /* |
3 | +--------------------------------------------------------------------+ | |
2fe49090 | 4 | | CiviCRM version 5 | |
b6708aeb | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
b6708aeb | 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 | +--------------------------------------------------------------------+ | |
e70a7fc0 | 26 | */ |
6a488035 | 27 | |
6a488035 TO |
28 | /** |
29 | * Test APIv3 civicrm_create_custom_group | |
30 | * | |
6c6e6187 | 31 | * @package CiviCRM |
acb109b7 | 32 | * @group headless |
6a488035 TO |
33 | */ |
34 | class api_v3_CustomFieldTest extends CiviUnitTestCase { | |
35 | protected $_apiversion; | |
b7c9bc4c | 36 | |
00be9182 | 37 | public function setUp() { |
6a488035 TO |
38 | $this->_apiversion = 3; |
39 | parent::setUp(); | |
40 | } | |
41 | ||
00be9182 | 42 | public function tearDown() { |
6a488035 | 43 | $tablesToTruncate = array( |
b3c30fda CW |
44 | 'civicrm_contact', |
45 | 'civicrm_file', | |
46 | 'civicrm_entity_file', | |
6a488035 | 47 | ); |
b3c30fda | 48 | // true tells quickCleanup to drop custom_value tables that might have been created in the test |
6a488035 TO |
49 | $this->quickCleanup($tablesToTruncate, TRUE); |
50 | } | |
51 | ||
52 | /** | |
eceb18cc | 53 | * Check with no array. |
6a488035 | 54 | */ |
00be9182 | 55 | public function testCustomFieldCreateNoArray() { |
6a488035 TO |
56 | $fieldParams = NULL; |
57 | ||
d0e1eff2 | 58 | $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams); |
6a488035 TO |
59 | $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array'); |
60 | } | |
61 | ||
62 | /** | |
eceb18cc | 63 | * Check with no label. |
6a488035 | 64 | */ |
00be9182 | 65 | public function testCustomFieldCreateWithoutLabel() { |
e830c6ae | 66 | $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'text_test_group')); |
6a488035 TO |
67 | $params = array( |
68 | 'custom_group_id' => $customGroup['id'], | |
69 | 'name' => 'test_textfield2', | |
70 | 'html_type' => 'Text', | |
71 | 'data_type' => 'String', | |
72 | 'default_value' => 'abc', | |
73 | 'weight' => 4, | |
74 | 'is_required' => 1, | |
75 | 'is_searchable' => 0, | |
76 | 'is_active' => 1, | |
6a488035 TO |
77 | ); |
78 | ||
d0e1eff2 | 79 | $customField = $this->callAPIFailure('custom_field', 'create', $params); |
6a488035 TO |
80 | $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: label'); |
81 | } | |
82 | ||
83 | /** | |
eceb18cc | 84 | * Check with edit. |
6a488035 | 85 | */ |
00be9182 | 86 | public function testCustomFieldCreateWithEdit() { |
e830c6ae | 87 | $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'text_test_group')); |
6a488035 TO |
88 | $params = array( |
89 | 'custom_group_id' => $customGroup['id'], | |
90 | 'name' => 'test_textfield2', | |
91 | 'label' => 'Name1', | |
92 | 'html_type' => 'Text', | |
93 | 'data_type' => 'String', | |
94 | 'default_value' => 'abc', | |
95 | 'weight' => 4, | |
96 | 'is_required' => 1, | |
97 | 'is_searchable' => 0, | |
98 | 'is_active' => 1, | |
6a488035 TO |
99 | ); |
100 | ||
92915c55 | 101 | $customField = $this->callAPIAndDocument('custom_field', 'create', $params, __FUNCTION__, __FILE__); |
6a488035 | 102 | $params['id'] = $customField['id']; |
92915c55 | 103 | $customField = $this->callAPISuccess('custom_field', 'create', $params); |
6a488035 | 104 | |
ba4a1892 | 105 | $this->assertNotNull($customField['id']); |
6a488035 TO |
106 | } |
107 | ||
108 | /** | |
eceb18cc | 109 | * Check without groupId. |
6a488035 | 110 | */ |
00be9182 | 111 | public function testCustomFieldCreateWithoutGroupID() { |
6a488035 TO |
112 | $fieldParams = array( |
113 | 'name' => 'test_textfield1', | |
114 | 'label' => 'Name', | |
115 | 'html_type' => 'Text', | |
116 | 'data_type' => 'String', | |
117 | 'default_value' => 'abc', | |
118 | 'weight' => 4, | |
119 | 'is_required' => 1, | |
120 | 'is_searchable' => 0, | |
121 | 'is_active' => 1, | |
e830c6ae | 122 | |
6a488035 TO |
123 | ); |
124 | ||
d0e1eff2 | 125 | $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams); |
6a488035 TO |
126 | $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: custom_group_id'); |
127 | } | |
128 | ||
129 | /** | |
130 | * Check for Each data type: loop through available form input types | |
ae5ffbb7 | 131 | */ |
00be9182 | 132 | public function testCustomFieldCreateAllAvailableFormInputs() { |
e830c6ae | 133 | $gid = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'testAllFormInputs')); |
6a488035 TO |
134 | |
135 | $dtype = CRM_Core_BAO_CustomField::dataType(); | |
136 | $htype = CRM_Core_BAO_CustomField::dataToHtml(); | |
137 | ||
138 | $n = 0; | |
139 | foreach ($dtype as $dkey => $dvalue) { | |
140 | foreach ($htype[$n] as $hkey => $hvalue) { | |
141 | //echo $dkey."][".$hvalue."\n"; | |
142 | $this->_loopingCustomFieldCreateTest($this->_buildParams($gid['id'], $hvalue, $dkey)); | |
143 | } | |
144 | $n++; | |
145 | } | |
146 | } | |
39b959db | 147 | |
6c6e6187 | 148 | /* |
e70a7fc0 TO |
149 | * Can't figure out the point of this? |
150 | */ | |
39b959db | 151 | |
4cbe18b8 | 152 | /** |
c490a46a | 153 | * @param array $params |
4cbe18b8 | 154 | */ |
00be9182 | 155 | public function _loopingCustomFieldCreateTest($params) { |
e830c6ae | 156 | $customField = $this->callAPISuccess('custom_field', 'create', $params); |
6a488035 TO |
157 | $this->assertNotNull($customField['id']); |
158 | $this->getAndCheck($params, $customField['id'], 'CustomField'); | |
159 | } | |
160 | ||
4cbe18b8 | 161 | /** |
100fef9d | 162 | * @param int $gid |
4cbe18b8 EM |
163 | * @param $htype |
164 | * @param $dtype | |
165 | * | |
166 | * @return array | |
167 | */ | |
00be9182 | 168 | public function _buildParams($gid, $htype, $dtype) { |
6a488035 TO |
169 | $params = $this->_buildBasicParams($gid, $htype, $dtype); |
170 | /* //Not Working for any type. Maybe redundant with testCustomFieldCreateWithOptionValues() | |
e70a7fc0 TO |
171 | if ($htype == 'Multi-Select') |
172 | $params = array_merge($params, array( | |
173 | 'option_label' => array( 'Label1','Label2'), | |
174 | 'option_value' => array( 'val1', 'val2' ), | |
175 | 'option_weight' => array( 1, 2), | |
176 | 'option_status' => array( 1, 1), | |
177 | )); | |
178 | */ | |
6a488035 TO |
179 | |
180 | return $params; | |
181 | } | |
182 | ||
4cbe18b8 | 183 | /** |
100fef9d | 184 | * @param int $gid |
4cbe18b8 EM |
185 | * @param $htype |
186 | * @param $dtype | |
187 | * | |
188 | * @return array | |
189 | */ | |
00be9182 | 190 | public function _buildBasicParams($gid, $htype, $dtype) { |
6a488035 TO |
191 | return array( |
192 | 'custom_group_id' => $gid, | |
193 | 'label' => $dtype . $htype, | |
194 | 'html_type' => $htype, | |
195 | 'data_type' => $dtype, | |
196 | 'weight' => 4, | |
197 | 'is_required' => 0, | |
198 | 'is_searchable' => 0, | |
199 | 'is_active' => 1, | |
e830c6ae | 200 | |
6a488035 TO |
201 | ); |
202 | } | |
203 | ||
204 | /** | |
eceb18cc | 205 | * Test using example code. |
6a488035 TO |
206 | */ |
207 | /*function testCustomFieldCreateExample( ) | |
e70a7fc0 | 208 | { |
6a488035 | 209 | |
e70a7fc0 TO |
210 | $customGroup = $this->customGroupCreate('Individual','date_test_group',3); |
211 | require_once 'api/v3/examples/CustomField/Create.php'; | |
212 | $result = custom_field_create_example(); | |
213 | $expectedResult = custom_field_create_expectedresult(); | |
214 | $this->assertEquals($result,$expectedResult); | |
215 | }*/ | |
6a488035 TO |
216 | |
217 | /** | |
100fef9d | 218 | * Check with data type - Options with option_values |
6a488035 | 219 | */ |
00be9182 | 220 | public function testCustomFieldCreateWithEmptyOptionGroup() { |
e830c6ae | 221 | $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'select_test_group')); |
b958933f | 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, | |
b958933f | 231 | ); |
232 | ||
e830c6ae | 233 | $customField = $this->callAPISuccess('custom_field', 'create', $params); |
b958933f | 234 | $this->assertNotNull($customField['id']); |
e830c6ae | 235 | $optionGroupID = $this->callAPISuccess('custom_field', 'getvalue', array( |
b958933f | 236 | 'id' => $customField['id'], |
237 | 'return' => 'option_group_id', | |
238 | )); | |
239 | ||
240 | $this->assertTrue(is_numeric($optionGroupID) && ($optionGroupID > 0)); | |
e830c6ae | 241 | $optionGroup = $this->callAPISuccess('option_group', 'getsingle', array( |
ae5ffbb7 | 242 | 'id' => $optionGroupID, |
92915c55 | 243 | )); |
6c6e6187 | 244 | $this->assertEquals($optionGroup['title'], 'Country'); |
e830c6ae | 245 | $optionValueCount = $this->callAPISuccess('option_value', 'getcount', array( |
ae5ffbb7 | 246 | 'option_group_id' => $optionGroupID, |
92915c55 | 247 | )); |
b958933f | 248 | $this->assertEquals(0, $optionValueCount); |
249 | } | |
250 | ||
65c2fd15 JP |
251 | /** |
252 | * Check with non-ascii labels | |
253 | */ | |
254 | public function testCustomFieldCreateWithNonAsciiLabel() { | |
255 | $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'select_test_group')); | |
256 | $params = array( | |
257 | 'custom_group_id' => $customGroup['id'], | |
258 | 'label' => 'ôôôô', | |
259 | 'html_type' => 'Select', | |
260 | 'data_type' => 'String', | |
261 | 'weight' => 4, | |
262 | 'is_required' => 1, | |
263 | 'is_searchable' => 0, | |
264 | 'is_active' => 1, | |
265 | ); | |
266 | $customField = $this->callAPISuccess('custom_field', 'create', $params); | |
267 | $this->assertNotNull($customField['id']); | |
268 | $params['label'] = 'Ã Ã Ã '; | |
269 | $customField = $this->callAPISuccess('custom_field', 'create', $params); | |
270 | $this->assertNotNull($customField['id']); | |
271 | } | |
272 | ||
eebb53df | 273 | /** |
eceb18cc | 274 | * Test custom field with existing option group. |
eebb53df | 275 | */ |
00be9182 | 276 | public function testCustomFieldExistingOptionGroup() { |
eebb53df JV |
277 | $customGroup = $this->customGroupCreate(array('extends' => 'Organization', 'title' => 'test_group')); |
278 | $params = array( | |
279 | 'custom_group_id' => $customGroup['id'], | |
280 | // Just to say something: | |
281 | 'label' => 'Organization Gender', | |
282 | 'html_type' => 'Select', | |
283 | 'data_type' => 'Int', | |
284 | 'weight' => 4, | |
285 | 'is_required' => 1, | |
286 | 'is_searchable' => 0, | |
287 | 'is_active' => 1, | |
288 | // Option group id 3: gender | |
289 | 'option_group_id' => 3, | |
290 | ); | |
291 | ||
292 | $customField = $this->callAPISuccess('custom_field', 'create', $params); | |
293 | $this->assertNotNull($customField['id']); | |
294 | $optionGroupID = $this->callAPISuccess('custom_field', 'getvalue', array( | |
295 | 'id' => $customField['id'], | |
296 | 'return' => 'option_group_id', | |
297 | )); | |
298 | ||
6c6e6187 | 299 | $this->assertEquals($optionGroupID, 3); |
eebb53df JV |
300 | } |
301 | ||
3c70d501 | 302 | /** |
303 | * Test custom field get works & return param works | |
304 | */ | |
9b873358 | 305 | public function testCustomFieldGetReturnOptions() { |
e830c6ae | 306 | $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group')); |
b422b715 | 307 | $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id'])); |
3c70d501 | 308 | |
e830c6ae | 309 | $result = $this->callAPISuccess('custom_field', 'getsingle', array( |
3c70d501 | 310 | 'id' => $customField['id'], |
311 | 'return' => 'data_type', | |
312 | )); | |
313 | $this->assertTrue(array_key_exists('data_type', $result)); | |
314 | $this->assertFalse(array_key_exists('custom_group_id', $result)); | |
315 | } | |
316 | ||
317 | /** | |
318 | * Test custom field get works & return param works | |
319 | */ | |
9b873358 | 320 | public function testCustomFieldGetReturnArray() { |
e830c6ae | 321 | $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group')); |
b422b715 | 322 | $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id'])); |
3c70d501 | 323 | |
e830c6ae | 324 | $result = $this->callAPISuccess('custom_field', 'getsingle', array( |
92915c55 | 325 | 'id' => $customField['id'], |
3c70d501 | 326 | 'return' => array('data_type'), |
327 | )); | |
328 | $this->assertTrue(array_key_exists('data_type', $result)); | |
329 | $this->assertFalse(array_key_exists('custom_group_id', $result)); | |
330 | } | |
331 | ||
332 | /** | |
333 | * Test custom field get works & return param works | |
334 | */ | |
9b873358 | 335 | public function testCustomFieldGetReturnTwoOptions() { |
e830c6ae | 336 | $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'test_group')); |
b422b715 | 337 | $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id'])); |
3c70d501 | 338 | |
e830c6ae | 339 | $result = $this->callAPISuccess('custom_field', 'getsingle', array( |
92915c55 | 340 | 'id' => $customField['id'], |
3c70d501 | 341 | 'return' => 'data_type, custom_group_id', |
342 | )); | |
343 | $this->assertTrue(array_key_exists('data_type', $result)); | |
344 | $this->assertTrue(array_key_exists('custom_group_id', $result)); | |
345 | $this->assertFalse(array_key_exists('label', $result)); | |
346 | } | |
347 | ||
00be9182 | 348 | public function testCustomFieldCreateWithOptionValues() { |
e830c6ae | 349 | $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'select_test_group')); |
6a488035 TO |
350 | |
351 | $option_values = array( | |
6c6e6187 | 352 | array( |
92915c55 | 353 | 'weight' => 1, |
6a488035 TO |
354 | 'label' => 'Label1', |
355 | 'value' => 1, | |
356 | 'is_active' => 1, | |
357 | ), | |
358 | array( | |
359 | 'weight' => 2, | |
360 | 'label' => 'Label2', | |
361 | 'value' => 2, | |
362 | 'is_active' => 1, | |
363 | ), | |
364 | ); | |
365 | ||
366 | $params = array( | |
367 | 'custom_group_id' => $customGroup['id'], | |
7f6d3dd3 | 368 | 'label' => 'Our special field', |
6a488035 TO |
369 | 'html_type' => 'Select', |
370 | 'data_type' => 'String', | |
371 | 'weight' => 4, | |
372 | 'is_required' => 1, | |
373 | 'is_searchable' => 0, | |
374 | 'is_active' => 1, | |
375 | 'option_values' => $option_values, | |
e830c6ae | 376 | |
6a488035 TO |
377 | ); |
378 | ||
e830c6ae | 379 | $customField = $this->callAPISuccess('custom_field', 'create', $params); |
6a488035 | 380 | |
b958933f | 381 | $this->assertAPISuccess($customField); |
6a488035 TO |
382 | $this->assertNotNull($customField['id']); |
383 | $getFieldsParams = array( | |
384 | 'options' => array('get_options' => 'custom_' . $customField['id']), | |
92915c55 | 385 | 'action' => 'create', |
6a488035 | 386 | ); |
5c49fee0 | 387 | $description = "Demonstrates retrieving metadata with custom field options."; |
6a488035 | 388 | $subfile = "GetFieldsOptions"; |
a828d7b8 | 389 | $fields = $this->callAPIAndDocument('contact', 'getfields', $getFieldsParams, __FUNCTION__, 'ContactTest.php', $description, $subfile); |
6a488035 TO |
390 | $this->assertArrayHasKey('options', $fields['values']['custom_' . $customField['id']]); |
391 | $this->assertEquals('Label1', $fields['values']['custom_' . $customField['id']]['options'][1]); | |
392 | $getOptionsArray = array( | |
393 | 'field' => 'custom_' . $customField['id'], | |
92915c55 | 394 | ); |
5c49fee0 | 395 | $description = "Demonstrates retrieving options for a custom field."; |
6a488035 | 396 | $subfile = "GetOptions"; |
a828d7b8 | 397 | $result = $this->callAPIAndDocument('contact', 'getoptions', $getOptionsArray, __FUNCTION__, 'ContactTest.php', $description, ''); |
6a488035 | 398 | $this->assertEquals('Label1', $result['values'][1]); |
6a488035 TO |
399 | } |
400 | ||
401 | ///////////////// civicrm_custom_field_delete methods | |
402 | ||
403 | /** | |
eceb18cc | 404 | * Check with no array. |
6a488035 | 405 | */ |
00be9182 | 406 | public function testCustomFieldDeleteNoArray() { |
6a488035 | 407 | $params = NULL; |
d0e1eff2 | 408 | $customField = $this->callAPIFailure('custom_field', 'delete', $params); |
6a488035 TO |
409 | $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array'); |
410 | } | |
411 | ||
412 | /** | |
eceb18cc | 413 | * Check without Field ID. |
6a488035 | 414 | */ |
00be9182 | 415 | public function testCustomFieldDeleteWithoutFieldID() { |
e830c6ae | 416 | $params = array(); |
417 | $customField = $this->callAPIFailure('custom_field', 'delete', $params, | |
418 | 'Mandatory key(s) missing from params array: id'); | |
6a488035 TO |
419 | } |
420 | ||
421 | /** | |
eceb18cc | 422 | * Check without valid array. |
6a488035 | 423 | */ |
00be9182 | 424 | public function testCustomFieldDelete() { |
e830c6ae | 425 | $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group')); |
b422b715 | 426 | $customField = $this->customFieldCreate(array('custom_group_id' => $customGroup['id'])); |
ba4a1892 | 427 | $this->assertNotNull($customField['id']); |
6a488035 TO |
428 | |
429 | $params = array( | |
6a488035 TO |
430 | 'id' => $customField['id'], |
431 | ); | |
e830c6ae | 432 | $result = $this->callAPIAndDocument('custom_field', 'delete', $params, __FUNCTION__, __FILE__); |
6a488035 | 433 | |
a15773db | 434 | $this->assertAPISuccess($result); |
6a488035 TO |
435 | } |
436 | ||
437 | /** | |
eceb18cc | 438 | * Check for Option Value. |
6a488035 | 439 | */ |
00be9182 | 440 | public function testCustomFieldOptionValueDelete() { |
e830c6ae | 441 | $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'ABC')); |
6a488035 | 442 | $customOptionValueFields = $this->customFieldOptionValueCreate($customGroup, 'fieldABC'); |
6a488035 | 443 | $params = array( |
6a488035 TO |
444 | 'id' => $customOptionValueFields, |
445 | ); | |
446 | ||
e830c6ae | 447 | $customField = $this->callAPISuccess('custom_field', 'delete', $customOptionValueFields); |
6a488035 | 448 | } |
6b59896e TO |
449 | |
450 | /** | |
451 | * If there's one custom group for "Contact" and one for "Activity", then "Contact.getfields" | |
452 | * and "Activity.getfields" should return only their respective fields (not the other's fields), | |
453 | * and unrelated entities should return no custom fields. | |
454 | */ | |
00be9182 | 455 | public function testGetfields_CrossEntityPollution() { |
6b59896e TO |
456 | $auxEntities = array('Email', 'Address', 'LocBlock', 'Membership', 'ContributionPage', 'ReportInstance'); |
457 | $allEntities = array_merge(array('Contact', 'Activity'), $auxEntities); | |
458 | ||
459 | // Baseline - getfields doesn't reporting any customfields for any entities | |
460 | foreach ($allEntities as $entity) { | |
461 | $this->assertEquals( | |
462 | array(), | |
463 | $this->getCustomFieldKeys($this->callAPISuccess($entity, 'getfields', array())), | |
464 | "Baseline custom fields for $entity should be empty" | |
465 | ); | |
466 | } | |
467 | ||
468 | // Add some fields | |
f8a3cd29 | 469 | $contactGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'test_group_c')); |
92915c55 | 470 | $contactField = $this->customFieldCreate(array( |
39b959db SL |
471 | 'custom_group_id' => $contactGroup['id'], |
472 | 'label' => 'For Contacts', | |
473 | )); | |
f8a3cd29 TO |
474 | $indivGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group_i')); |
475 | $indivField = $this->customFieldCreate(array('custom_group_id' => $indivGroup['id'], 'label' => 'For Individuals')); | |
476 | $activityGroup = $this->customGroupCreate(array('extends' => 'Activity', 'title' => 'test_group_a')); | |
92915c55 | 477 | $activityField = $this->customFieldCreate(array( |
39b959db SL |
478 | 'custom_group_id' => $activityGroup['id'], |
479 | 'label' => 'For Activities', | |
480 | )); | |
6b59896e | 481 | |
f8a3cd29 TO |
482 | // Check getfields |
483 | $this->assertEquals( | |
484 | array('custom_' . $contactField['id'], 'custom_' . $indivField['id']), | |
485 | $this->getCustomFieldKeys($this->callAPISuccess('Contact', 'getfields', array())), | |
486 | 'Contact custom fields' | |
487 | ); | |
488 | $this->assertEquals( | |
489 | array('custom_' . $contactField['id'], 'custom_' . $indivField['id']), | |
490 | $this->getCustomFieldKeys($this->callAPISuccess('Individual', 'getfields', array())), | |
491 | 'Individual custom fields' | |
492 | ); | |
493 | $this->assertEquals( | |
494 | array('custom_' . $contactField['id']), | |
495 | $this->getCustomFieldKeys($this->callAPISuccess('Organization', 'getfields', array())), | |
496 | 'Organization custom fields' | |
497 | ); | |
6b59896e TO |
498 | $this->assertEquals( |
499 | array('custom_' . $activityField['id']), | |
500 | $this->getCustomFieldKeys($this->callAPISuccess('Activity', 'getfields', array())), | |
501 | 'Activity custom fields' | |
502 | ); | |
503 | foreach ($auxEntities as $entity) { | |
504 | $this->assertEquals( | |
505 | array(), | |
506 | $this->getCustomFieldKeys($this->callAPISuccess($entity, 'getfields', array())), | |
507 | "Custom fields for $entity should be empty" | |
508 | ); | |
509 | } | |
510 | } | |
511 | ||
b3c30fda CW |
512 | /** |
513 | * Test setting and getting a custom file field value. | |
514 | * | |
515 | * Uses the "attachment" api for setting value. | |
516 | */ | |
517 | public function testCustomFileField() { | |
518 | $customGroup = $this->customGroupCreate(array('title' => 'attachment_test_group')); | |
519 | $params = array( | |
520 | 'custom_group_id' => $customGroup['id'], | |
521 | 'name' => 'test_file_attachment', | |
522 | 'label' => 'test_file_attachment', | |
523 | 'html_type' => 'File', | |
524 | 'data_type' => 'File', | |
525 | 'is_active' => 1, | |
526 | ); | |
527 | $customField = $this->callAPISuccess('custom_field', 'create', $params); | |
528 | $cfId = 'custom_' . $customField['id']; | |
529 | ||
530 | $cid = $this->individualCreate(); | |
531 | ||
532 | $attachment = $this->callAPISuccess('attachment', 'create', array( | |
533 | 'name' => CRM_Utils_String::createRandom(5, CRM_Utils_String::ALPHANUMERIC) . '_testCustomFileField.txt', | |
534 | 'mime_type' => 'text/plain', | |
535 | 'content' => 'My test content', | |
536 | 'field_name' => $cfId, | |
537 | 'entity_id' => $cid, | |
538 | )); | |
539 | $this->assertAttachmentExistence(TRUE, $attachment); | |
540 | ||
541 | $result = $this->callAPISuccess('contact', 'getsingle', array( | |
542 | 'id' => $cid, | |
543 | 'return' => $cfId, | |
544 | )); | |
545 | ||
546 | $this->assertEquals($attachment['id'], $result[$cfId]); | |
547 | } | |
548 | ||
12b07b22 MD |
549 | public function testUpdateCustomField() { |
550 | $customGroup = $this->customGroupCreate(array('extends' => 'Individual')); | |
551 | $params = array('id' => $customGroup['id'], 'is_active' => 0); | |
552 | $result = $this->callAPISuccess('CustomGroup', 'create', $params); | |
553 | $result = array_shift($result['values']); | |
554 | ||
555 | $this->assertEquals(0, $result['is_active']); | |
556 | ||
557 | $this->customGroupDelete($customGroup['id']); | |
558 | } | |
559 | ||
f93514f8 MM |
560 | public function testCustomFieldCreateWithOptionGroupName() { |
561 | $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_custom_group')); | |
562 | $params = array( | |
563 | 'custom_group_id' => $customGroup['id'], | |
564 | 'name' => 'Activity type', | |
565 | 'label' => 'Activity type', | |
566 | 'data_type' => 'String', | |
567 | 'html_type' => 'Select', | |
568 | 'option_group_id' => 'activity_type', | |
569 | ); | |
570 | $result = $this->callAPISuccess('CustomField', 'create', $params); | |
571 | } | |
572 | ||
4cbe18b8 EM |
573 | /** |
574 | * @param $getFieldsResult | |
575 | * | |
576 | * @return array | |
577 | */ | |
00be9182 | 578 | public function getCustomFieldKeys($getFieldsResult) { |
92915c55 | 579 | $isCustom = function ($key) { |
6b59896e TO |
580 | return preg_match('/^custom_/', $key); |
581 | }; | |
f8a3cd29 TO |
582 | $r = array_values(array_filter(array_keys($getFieldsResult['values']), $isCustom)); |
583 | sort($r); | |
584 | return $r; | |
6b59896e | 585 | } |
96025800 | 586 | |
7ab8180f MM |
587 | public function testMakeSearchableContactReferenceFieldUnsearchable() { |
588 | $customGroup = $this->customGroupCreate(array( | |
589 | 'name' => 'testCustomGroup', | |
590 | 'title' => 'testCustomGroup', | |
591 | 'extends' => 'Individual', | |
592 | )); | |
593 | $params = array( | |
594 | 'name' => 'testCustomField', | |
595 | 'label' => 'testCustomField', | |
596 | 'custom_group_id' => 'testCustomGroup', | |
597 | 'data_type' => 'ContactReference', | |
598 | 'html_type' => 'Autocomplete-Select', | |
599 | 'is_searchable' => '1', | |
600 | ); | |
601 | $result = $this->callAPISuccess('CustomField', 'create', $params); | |
602 | $params = [ | |
603 | 'id' => $result['id'], | |
604 | 'is_searchable' => 0, | |
605 | ]; | |
606 | $result = $this->callAPISuccess('CustomField', 'create', $params); | |
607 | } | |
608 | ||
f2ff6efa MM |
609 | public function testDisableSearchableContactReferenceField() { |
610 | $customGroup = $this->customGroupCreate(array( | |
611 | 'name' => 'testCustomGroup', | |
612 | 'title' => 'testCustomGroup', | |
613 | 'extends' => 'Individual', | |
614 | )); | |
615 | $params = array( | |
616 | 'name' => 'testCustomField', | |
617 | 'label' => 'testCustomField', | |
618 | 'custom_group_id' => 'testCustomGroup', | |
619 | 'data_type' => 'ContactReference', | |
620 | 'html_type' => 'Autocomplete-Select', | |
621 | 'is_searchable' => '1', | |
622 | ); | |
623 | $result = $this->callAPISuccess('CustomField', 'create', $params); | |
624 | $params = [ | |
625 | 'id' => $result['id'], | |
626 | 'is_active' => 0, | |
627 | ]; | |
628 | $result = $this->callAPISuccess('CustomField', 'create', $params); | |
629 | } | |
630 | ||
6a488035 | 631 | } |