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