Cleanup references to custom field html_type
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / CustomFieldTest.php
CommitLineData
6a488035 1<?php
0eea664b 2
aba1cd8b
EM
3/**
4 * Class CRM_Core_BAO_CustomFieldTest
8392a043 5 *
acb109b7 6 * @group headless
aba1cd8b 7 */
6a488035 8class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
6a488035 9
8392a043 10 use CRMTraits_Custom_CustomDataTrait;
11
c6b559da 12 protected $customFieldID;
13
cef2e96c 14 /**
15 * Clean up after test.
16 *
17 * @throws \Exception
18 */
19 public function tearDown() {
20 $this->quickCleanup([], TRUE);
21 parent::tearDown();
22 }
23
c4817841 24 /**
25 * Test creating a custom field.
26 */
08e37a2a 27 public function testCreateCustomField() {
c6b559da 28 $customGroup = $this->createCustomField();
08e37a2a 29 $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
6a488035
TO
30 'Database check for created CustomField.'
31 );
c4817841 32 $fields = [
6a488035
TO
33 'id' => $customFieldID,
34 'label' => 'editTestFld',
35 'is_active' => 1,
36 'data_type' => 'String',
37 'html_type' => 'Text',
08e37a2a 38 'custom_group_id' => $customGroup['id'],
c4817841 39 ];
6a488035 40
08e37a2a 41 CRM_Core_BAO_CustomField::create($fields);
6a488035
TO
42 $this->assertDBNotNull('CRM_Core_DAO_CustomField', 1, 'id', 'is_active', 'Database check for edited CustomField.');
43 $this->assertDBNotNull('CRM_Core_DAO_CustomField', $fields['label'], 'id', 'label', 'Database check for edited CustomField.');
44
8b3b9a2e
ARW
45 $dbFieldName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'name', 'id', 'Database check for edited CustomField.');
46 $dbColumnName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'column_name', 'id', 'Database check for edited CustomField.');
47 $this->assertEquals(strtolower("{$dbFieldName}_{$customFieldID}"), $dbColumnName,
92915c55 48 "Column name ends in ID");
8b3b9a2e 49
08e37a2a 50 $this->customGroupDelete($customGroup['id']);
8b3b9a2e 51 }
b2042573 52
c4817841 53 /**
54 * Test custom field create accepts passed column name.
55 */
08e37a2a 56 public function testCreateCustomFieldColumnName() {
c4817841 57 $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
58 $fields = [
8b3b9a2e
ARW
59 'label' => 'testFld 2',
60 'column_name' => 'special_colname',
61 'data_type' => 'String',
62 'html_type' => 'Text',
08e37a2a 63 'custom_group_id' => $customGroup['id'],
c4817841 64 ];
08e37a2a 65 CRM_Core_BAO_CustomField::create($fields);
66 $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
8b3b9a2e
ARW
67 'Database check for created CustomField.'
68 );
69 $dbColumnName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'column_name', 'id', 'Database check for edited CustomField.');
70 $this->assertEquals($fields['column_name'], $dbColumnName,
92915c55 71 "Column name set as specified");
8b3b9a2e 72
08e37a2a 73 $this->customGroupDelete($customGroup['id']);
6a488035
TO
74 }
75
c4817841 76 /**
77 * Test that name is used for the column.
78 */
08e37a2a 79 public function testCreateCustomFieldName() {
c4817841 80 $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
81 $fields = [
43ff3196
TO
82 'label' => 'testFld 2',
83 'name' => 'special_fldlname',
84 'data_type' => 'String',
85 'html_type' => 'Text',
08e37a2a 86 'custom_group_id' => $customGroup['id'],
c4817841 87 ];
08e37a2a 88 CRM_Core_BAO_CustomField::create($fields);
89 $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
43ff3196
TO
90 'Database check for created CustomField.'
91 );
92 $dbFieldName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'name', 'id', 'Database check for edited CustomField.');
93 $this->assertEquals($fields['name'], $dbFieldName,
94 "Column name set as specified");
95
08e37a2a 96 $this->customGroupDelete($customGroup['id']);
43ff3196
TO
97 }
98
c4817841 99 /**
100 * Test get fields function.
101 */
00be9182 102 public function testGetFields() {
c4817841 103 $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
104 $fields = [
6a488035
TO
105 'label' => 'testFld1',
106 'data_type' => 'String',
107 'html_type' => 'Text',
108 'is_active' => 1,
08e37a2a 109 'custom_group_id' => $customGroup['id'],
c4817841 110 ];
08e37a2a 111 CRM_Core_BAO_CustomField::create($fields);
112 $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
6a488035
TO
113 'Database check for created CustomField.'
114 );
c4817841 115 $fields = [
6a488035
TO
116 'label' => 'testFld2',
117 'data_type' => 'String',
118 'html_type' => 'Text',
119 'is_active' => 1,
08e37a2a 120 'custom_group_id' => $customGroup['id'],
c4817841 121 ];
08e37a2a 122 CRM_Core_BAO_CustomField::create($fields);
123 $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
6a488035
TO
124 'Database check for created CustomField.'
125 );
6a488035 126
08e37a2a 127 $this->customGroupDelete($customGroup['id']);
6a488035
TO
128 }
129
c4817841 130 /**
131 * @throws \Exception
132 */
00be9182 133 public function testGetDisplayedValues() {
c4817841 134 $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
135 $fieldsToCreate = [
136 [
d5c86914
CW
137 'data_type' => 'Country',
138 'html_type' => 'Select Country',
c4817841 139 'tests' => [
602b60e0 140 'United States' => 1228,
d5c86914 141 '' => NULL,
c4817841 142 ],
143 ],
144 [
d5c86914
CW
145 'data_type' => 'StateProvince',
146 'html_type' => 'Multi-Select State/Province',
c4817841 147 'tests' => [
d5c86914
CW
148 '' => 0,
149 'Alabama' => 1000,
c4817841 150 'Alabama, Alaska' => [1000, 1001],
151 ],
152 ],
153 [
d5c86914
CW
154 'data_type' => 'String',
155 'html_type' => 'Radio',
c4817841 156 'option_values' => [
d5c86914 157 'key' => 'KeyLabel',
c4817841 158 ],
159 'tests' => [
d5c86914 160 'KeyLabel' => 'key',
c4817841 161 ],
162 ],
163 [
d5c86914
CW
164 'data_type' => 'String',
165 'html_type' => 'CheckBox',
c4817841 166 'option_values' => [
d5c86914
CW
167 'key1' => 'Label1',
168 'key2' => 'Label2',
169 'key3' => 'Label3',
170 'key4' => 'Label4',
c4817841 171 ],
172 'tests' => [
173 'Label1' => ['key1'],
d5c86914 174 'Label2' => 'key2',
c4817841 175 'Label2, Label3' => ['key2', 'key3'],
176 'Label3, Label4' => CRM_Utils_Array::implodePadded(['key3', 'key4']),
177 'Label1, Label4' => ['key1' => 1, 'key4' => 1],
178 ],
179 ],
180 [
d5c86914
CW
181 'data_type' => 'Date',
182 'html_type' => 'Select Date',
183 'date_format' => 'd M yy',
184 'time_format' => 1,
c4817841 185 'tests' => [
d5c86914
CW
186 '1 Jun 1999 1:30PM' => '1999-06-01 13:30',
187 '' => '',
c4817841 188 ],
189 ],
190 ];
d5c86914 191 foreach ($fieldsToCreate as $num => $field) {
c4817841 192 $params = $field + ['label' => 'test field ' . $num, 'custom_group_id' => $customGroup['id']];
d5c86914
CW
193 unset($params['tests']);
194 $createdField = $this->callAPISuccess('customField', 'create', $params);
195 foreach ($field['tests'] as $expected => $input) {
196 $this->assertEquals($expected, CRM_Core_BAO_CustomField::displayValue($input, $createdField['id']));
197 }
198 }
6a488035 199
08e37a2a 200 $this->customGroupDelete($customGroup['id']);
6a488035
TO
201 }
202
c4817841 203 /**
204 * Test CRM_Core_BAO_CustomField::displayValue.
205 *
206 * @throws \CRM_Core_Exception
207 * @throws \Exception
208 */
17369736
AS
209 public function testGetDisplayedValuesContactRef() {
210 $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
211 $params = [
212 'data_type' => 'ContactReference',
213 'html_type' => 'Autocomplete-Select',
214 'label' => 'test ref',
215 'custom_group_id' => $customGroup['id'],
216 ];
217 $createdField = $this->callAPISuccess('customField', 'create', $params);
218 $contact1 = $this->individualCreate();
219 $contact2 = $this->individualCreate(['custom_' . $createdField['id'] => $contact1['id']]);
220
221 $this->assertEquals($contact1['display_name'], CRM_Core_BAO_CustomField::displayValue($contact2['id'], $createdField['id']));
222 $this->assertEquals("Bob", CRM_Core_BAO_CustomField::displayValue("Bob", $createdField['id']));
223
224 $this->contactDelete($contact2['id']);
225 $this->contactDelete($contact1['id']);
226 $this->customGroupDelete($customGroup['id']);
227 }
228
08e37a2a 229 public function testDeleteCustomField() {
c4817841 230 $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
231 $fields = [
08e37a2a 232 'custom_group_id' => $customGroup['id'],
8b3b9a2e 233 'label' => 'Throwaway Field',
d9136461 234 'dataType' => 'Memo',
235 'htmlType' => 'TextArea',
c4817841 236 ];
6a488035 237
08e37a2a 238 $customField = $this->customFieldCreate($fields);
239 $fieldObject = new CRM_Core_BAO_CustomField();
240 $fieldObject->id = $customField['id'];
241 $fieldObject->find(TRUE);
242 CRM_Core_BAO_CustomField::deleteField($fieldObject);
243 $this->assertDBNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id',
6a488035
TO
244 'custom_group_id', 'Database check for deleted Custom Field.'
245 );
08e37a2a 246 $this->customGroupDelete($customGroup['id']);
6a488035
TO
247 }
248
249 /**
08e37a2a 250 * Move a custom field from $groupA to $groupB.
251 *
252 * Make sure that data records are correctly matched and created.
c4817841 253 *
254 * @throws \CRM_Core_Exception
6a488035 255 */
00be9182 256 public function testMoveField() {
6a488035 257 $countriesByName = array_flip(CRM_Core_PseudoConstant::country(FALSE, FALSE));
602b60e0 258 $this->assertTrue($countriesByName['Andorra'] > 0);
c4817841 259 $groups = [
260 'A' => $this->customGroupCreate([
92915c55
TO
261 'title' => 'Test_Group A',
262 'name' => 'test_group_a',
c4817841 263 'extends' => ['Individual'],
92915c55
TO
264 'style' => 'Inline',
265 'is_multiple' => 0,
266 'is_active' => 1,
267 'version' => 3,
c4817841 268 ]),
269 'B' => $this->customGroupCreate([
92915c55
TO
270 'title' => 'Test_Group B',
271 'name' => 'test_group_b',
c4817841 272 'extends' => ['Individual'],
92915c55
TO
273 'style' => 'Inline',
274 'is_multiple' => 0,
275 'is_active' => 1,
276 'version' => 3,
c4817841 277 ]),
278 ];
08e37a2a 279 $groupA = $groups['A']['values'][$groups['A']['id']];
280 $groupB = $groups['B']['values'][$groups['B']['id']];
c4817841 281 $countryA = $this->customFieldCreate([
08e37a2a 282 'custom_group_id' => $groups['A']['id'],
283 'label' => 'Country A',
284 'dataType' => 'Country',
285 'htmlType' => 'Select Country',
286 'default_value' => NULL,
c4817841 287 ]);
288 $countryB = $this->customFieldCreate([
08e37a2a 289 'custom_group_id' => $groups['A']['id'],
290 'label' => 'Country B',
291 'dataType' => 'Country',
292 'htmlType' => 'Select Country',
293 'default_value' => NULL,
c4817841 294 ]);
295 $countryC = $this->customFieldCreate([
08e37a2a 296 'custom_group_id' => $groups['B']['id'],
297 'label' => 'Country C',
298 'dataType' => 'Country',
299 'htmlType' => 'Select Country',
300 'default_value' => NULL,
c4817841 301 ]);
08e37a2a 302
c4817841 303 $fields = [
08e37a2a 304 'countryA' => $countryA['values'][$countryA['id']],
305 'countryB' => $countryB['values'][$countryB['id']],
306 'countryC' => $countryC['values'][$countryC['id']],
c4817841 307 ];
308 $contacts = [
309 'alice' => $this->individualCreate([
92915c55
TO
310 'first_name' => 'Alice',
311 'last_name' => 'Albertson',
602b60e0
SL
312 'custom_' . $fields['countryA']['id'] => $countriesByName['Andorra'],
313 'custom_' . $fields['countryB']['id'] => $countriesByName['Barbados'],
c4817841 314 ]),
315 'bob' => $this->individualCreate([
92915c55
TO
316 'first_name' => 'Bob',
317 'last_name' => 'Roberts',
602b60e0
SL
318 'custom_' . $fields['countryA']['id'] => $countriesByName['Austria'],
319 'custom_' . $fields['countryB']['id'] => $countriesByName['Bermuda'],
320 'custom_' . $fields['countryC']['id'] => $countriesByName['Chad'],
c4817841 321 ]),
322 'carol' => $this->individualCreate([
92915c55
TO
323 'first_name' => 'Carol',
324 'last_name' => 'Carolson',
602b60e0 325 'custom_' . $fields['countryC']['id'] => $countriesByName['Cambodia'],
c4817841 326 ]),
327 ];
6a488035
TO
328
329 // Move!
08e37a2a 330 CRM_Core_BAO_CustomField::moveField($fields['countryB']['id'], $groupB['id']);
6a488035
TO
331
332 // Group[A] no longer has fields[countryB]
333 $errorScope = CRM_Core_TemporaryErrorScope::useException();
334 try {
08e37a2a 335 $this->assertDBQuery(1, "SELECT {$fields['countryB']['column_name']} FROM " . $groupA['table_name']);
6a488035
TO
336 $this->fail('Expected exception when querying column on wrong table');
337 }
92915c55
TO
338 catch (PEAR_Exception$e) {
339 }
6a488035
TO
340 $errorScope = NULL;
341
342 // Alice: Group[B] has fields[countryB], but fields[countryC] did not exist before
343 $this->assertDBQuery(1,
08e37a2a 344 "SELECT count(*) FROM {$groupB['table_name']}
b2042573 345 WHERE entity_id = %1
08e37a2a 346 AND {$fields['countryB']['column_name']} = %3
347 AND {$fields['countryC']['column_name']} is null",
c4817841 348 [
349 1 => [$contacts['alice'], 'Integer'],
350 3 => [$countriesByName['Barbados'], 'Integer'],
351 ]
6a488035
TO
352 );
353
354 // Bob: Group[B] has merged fields[countryB] and fields[countryC] on the same record
355 $this->assertDBQuery(1,
08e37a2a 356 "SELECT count(*) FROM {$groupB['table_name']}
b2042573 357 WHERE entity_id = %1
08e37a2a 358 AND {$fields['countryB']['column_name']} = %3
359 AND {$fields['countryC']['column_name']} = %4",
c4817841 360 [
361 1 => [$contacts['bob'], 'Integer'],
362 3 => [$countriesByName['Bermuda'], 'Integer'],
363 4 => [$countriesByName['Chad'], 'Integer'],
364 ]
6a488035
TO
365 );
366
367 // Carol: Group[B] still has fields[countryC] but did not get fields[countryB]
368 $this->assertDBQuery(1,
08e37a2a 369 "SELECT count(*) FROM {$groupB['table_name']}
b2042573 370 WHERE entity_id = %1
08e37a2a 371 AND {$fields['countryB']['column_name']} is null
372 AND {$fields['countryC']['column_name']} = %4",
c4817841 373 [
374 1 => [$contacts['carol'], 'Integer'],
375 4 => [$countriesByName['Cambodia'], 'Integer'],
376 ]
6a488035
TO
377 );
378
08e37a2a 379 $this->customGroupDelete($groups['A']['id']);
380 $this->customGroupDelete($groupB['id']);
6a488035 381 }
96025800 382
c6b559da 383 /**
384 * Test get custom field id function.
cef2e96c 385 *
386 * @throws \CiviCRM_API3_Exception
c6b559da 387 */
388 public function testGetCustomFieldID() {
389 $this->createCustomField();
390 $fieldID = CRM_Core_BAO_CustomField::getCustomFieldID('testFld');
391 $this->assertEquals($this->customFieldID, $fieldID);
392
393 $fieldID = CRM_Core_BAO_CustomField::getCustomFieldID('testFld', 'new custom group');
394 $this->assertEquals($this->customFieldID, $fieldID);
30b6e002
PF
395
396 $fieldID = CRM_Core_BAO_CustomField::getCustomFieldID('testFld', 'new custom group', TRUE);
397 $this->assertEquals('custom_' . $this->customFieldID, $fieldID);
398
399 // create field with same name in a different group
400 $this->createCustomField('other custom group');
401 $otherFieldID = CRM_Core_BAO_CustomField::getCustomFieldID('testFld', 'other custom group');
402 // make sure it does not return the field ID of the first field
403 $this->assertNotEquals($fieldID, $otherFieldID);
c6b559da 404 }
405
406 /**
30b6e002
PF
407 * Create a custom field
408 *
409 * @param string $groupTitle
410 *
c6b559da 411 * @return array
412 */
30b6e002
PF
413 protected function createCustomField($groupTitle = 'new custom group') {
414 $customGroup = $this->customGroupCreate([
415 'extends' => 'Individual',
8392a043 416 'title' => $groupTitle,
30b6e002 417 ]);
8392a043 418 $fields = [
c6b559da 419 'label' => 'testFld',
420 'data_type' => 'String',
421 'html_type' => 'Text',
422 'custom_group_id' => $customGroup['id'],
8392a043 423 ];
c6b559da 424 $field = CRM_Core_BAO_CustomField::create($fields);
425 $this->customFieldID = $field->id;
426 return $customGroup;
427 }
428
8392a043 429 /**
cef2e96c 430 * Test the getFieldsForImport function.
431 *
432 * @throws \Exception
8392a043 433 */
434 public function testGetFieldsForImport() {
435 $this->entity = 'Contact';
436 $this->createCustomGroupWithFieldsOfAllTypes();
cef2e96c 437 $customGroupID = $this->ids['CustomGroup']['Custom Group'];
8392a043 438 $expected = [
439 $this->getCustomFieldName('country') => [
cef2e96c 440 'name' => $this->getCustomFieldName('country'),
8392a043 441 'type' => 1,
442 'title' => 'Country',
443 'headerPattern' => '//',
444 'import' => 1,
445 'custom_field_id' => $this->getCustomFieldID('country'),
446 'options_per_line' => NULL,
447 'text_length' => NULL,
d989f2a4 448 'data_type' => 'Country',
8392a043 449 'html_type' => 'Select Country',
450 'is_search_range' => '0',
cef2e96c 451 'id' => $this->getCustomFieldID('country'),
452 'label' => 'Country',
453 'groupTitle' => 'Custom Group',
454 'default_value' => NULL,
455 'custom_group_id' => $customGroupID,
456 'extends' => 'Contact',
457 'extends_entity_column_value' => NULL,
458 'extends_entity_column_id' => NULL,
459 'is_view' => '0',
460 'is_multiple' => '0',
461 'option_group_id' => NULL,
462 'date_format' => NULL,
463 'time_format' => NULL,
79c9d4c2 464 'is_required' => 0,
cef2e96c 465 'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
ca64c337 466 'column_name' => $this->getCustomFieldColumnName('country'),
467 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.' . $this->getCustomFieldColumnName('country'),
468 'extends_table' => 'civicrm_contact',
469 'search_table' => 'contact_a',
470 'pseudoconstant' => [
471 'table' => 'civicrm_country',
472 'keyColumn' => 'id',
473 'labelColumn' => 'name',
474 'nameColumn' => 'iso_code',
475 ],
476 ],
477 $this->getCustomFieldName('multi_country') => [
478 'name' => $this->getCustomFieldName('multi_country'),
479 'type' => 1,
480 'title' => 'Country-multi',
481 'headerPattern' => '//',
482 'import' => 1,
483 'custom_field_id' => $this->getCustomFieldID('multi_country'),
484 'options_per_line' => NULL,
485 'text_length' => NULL,
486 'data_type' => 'Country',
487 'html_type' => 'Multi-Select Country',
488 'is_search_range' => '0',
489 'id' => $this->getCustomFieldID('multi_country'),
490 'label' => 'Country-multi',
491 'groupTitle' => 'Custom Group',
492 'default_value' => NULL,
493 'custom_group_id' => $customGroupID,
494 'extends' => 'Contact',
495 'extends_entity_column_value' => NULL,
496 'extends_entity_column_id' => NULL,
497 'is_view' => '0',
498 'is_multiple' => '0',
499 'option_group_id' => NULL,
500 'date_format' => NULL,
501 'time_format' => NULL,
502 'is_required' => 0,
503 'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
504 'column_name' => $this->getCustomFieldColumnName('multi_country'),
505 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.' . $this->getCustomFieldColumnName('multi_country'),
cef2e96c 506 'extends_table' => 'civicrm_contact',
507 'search_table' => 'contact_a',
d989f2a4 508 'pseudoconstant' => [
509 'table' => 'civicrm_country',
510 'keyColumn' => 'id',
511 'labelColumn' => 'name',
512 'nameColumn' => 'iso_code',
513 ],
8392a043 514 ],
cef2e96c 515 $this->getCustomFieldName('file') => [
8392a043 516 'name' => $this->getCustomFieldName('file'),
517 'type' => 2,
79c9d4c2 518 'title' => 'My file',
8392a043 519 'headerPattern' => '//',
520 'import' => 1,
521 'custom_field_id' => $this->getCustomFieldID('file'),
522 'options_per_line' => NULL,
523 'text_length' => NULL,
524 'data_type' => 'File',
525 'html_type' => 'File',
526 'is_search_range' => '0',
cef2e96c 527 'id' => $this->getCustomFieldID('file'),
79c9d4c2 528 'label' => 'My file',
cef2e96c 529 'groupTitle' => 'Custom Group',
530 'default_value' => NULL,
531 'custom_group_id' => $customGroupID,
532 'extends' => 'Contact',
533 'extends_entity_column_value' => NULL,
534 'extends_entity_column_id' => NULL,
535 'is_view' => '0',
536 'is_multiple' => '0',
537 'option_group_id' => NULL,
538 'date_format' => NULL,
539 'time_format' => NULL,
79c9d4c2 540 'is_required' => 0,
cef2e96c 541 'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
79c9d4c2 542 'column_name' => 'my_file_' . $this->getCustomFieldID('file'),
543 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.my_file_' . $this->getCustomFieldID('file'),
cef2e96c 544 'extends_table' => 'civicrm_contact',
545 'search_table' => 'contact_a',
8392a043 546 ],
547 $this->getCustomFieldName('text') => [
548 'name' => $this->getCustomFieldName('text'),
549 'type' => 2,
550 'title' => 'Enter text here',
551 'headerPattern' => '//',
552 'import' => 1,
553 'custom_field_id' => $this->getCustomFieldID('text'),
554 'options_per_line' => NULL,
b7d6b1fa 555 'text_length' => 300,
8392a043 556 'data_type' => 'String',
557 'html_type' => 'Text',
558 'is_search_range' => '0',
cef2e96c 559 'id' => $this->getCustomFieldID('text'),
560 'label' => 'Enter text here',
561 'groupTitle' => 'Custom Group',
562 'default_value' => 'xyz',
563 'custom_group_id' => '1',
564 'extends' => 'Contact',
565 'extends_entity_column_value' => NULL,
566 'extends_entity_column_id' => NULL,
567 'is_view' => '0',
568 'is_multiple' => '0',
569 'option_group_id' => NULL,
570 'date_format' => NULL,
571 'time_format' => NULL,
79c9d4c2 572 'is_required' => 0,
cef2e96c 573 'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
574 'column_name' => 'enter_text_here_' . $this->getCustomFieldID('text'),
575 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.enter_text_here_' . $this->getCustomFieldID('text'),
576 'extends_table' => 'civicrm_contact',
577 'search_table' => 'contact_a',
b7d6b1fa 578 'maxlength' => 300,
8392a043 579 ],
580 $this->getCustomFieldName('select_string') => [
cef2e96c 581 'name' => $this->getCustomFieldName('select_string'),
8392a043 582 'type' => 2,
583 'title' => 'Pick Color',
584 'headerPattern' => '//',
585 'import' => 1,
586 'custom_field_id' => $this->getCustomFieldID('select_string'),
587 'options_per_line' => NULL,
588 'text_length' => NULL,
589 'data_type' => 'String',
590 'html_type' => 'Select',
591 'is_search_range' => '0',
cef2e96c 592 'id' => $this->getCustomFieldID('select_string'),
593 'label' => 'Pick Color',
594 'groupTitle' => 'Custom Group',
595 'default_value' => NULL,
596 'custom_group_id' => $customGroupID,
597 'extends' => 'Contact',
598 'extends_entity_column_value' => NULL,
599 'extends_entity_column_id' => NULL,
600 'is_view' => '0',
601 'is_multiple' => '0',
602 'option_group_id' => $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID('select_string'), 'return' => 'option_group_id']),
603 'date_format' => NULL,
604 'time_format' => NULL,
79c9d4c2 605 'is_required' => 0,
cef2e96c 606 'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
607 'column_name' => 'pick_color_' . $this->getCustomFieldID('select_string'),
608 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.pick_color_' . $this->getCustomFieldID('select_string'),
609 'extends_table' => 'civicrm_contact',
610 'search_table' => 'contact_a',
611 'pseudoconstant' => [
612 'optionGroupName' => $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID('select_string'), 'return' => 'option_group_id.name']),
613 'optionEditPath' => 'civicrm/admin/options/' . $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID('select_string'), 'return' => 'option_group_id.name']),
614 ],
8392a043 615 ],
616 $this->getCustomFieldName('select_date') => [
617 'name' => $this->getCustomFieldName('select_date'),
618 'type' => 4,
56d1630d 619 'title' => 'Test Date',
8392a043 620 'headerPattern' => '//',
621 'import' => 1,
622 'custom_field_id' => $this->getCustomFieldID('select_date'),
623 'options_per_line' => NULL,
624 'text_length' => NULL,
625 'data_type' => 'Date',
626 'html_type' => 'Select Date',
0e1544e7 627 'is_search_range' => '1',
8392a043 628 'date_format' => 'mm/dd/yy',
629 'time_format' => '1',
cef2e96c 630 'id' => $this->getCustomFieldID('select_date'),
56d1630d 631 'label' => 'Test Date',
cef2e96c 632 'groupTitle' => 'Custom Group',
633 'default_value' => '20090711',
634 'custom_group_id' => $customGroupID,
635 'extends' => 'Contact',
636 'extends_entity_column_value' => NULL,
637 'extends_entity_column_id' => NULL,
638 'is_view' => '0',
639 'is_multiple' => '0',
640 'option_group_id' => NULL,
641 'is_required' => '0',
642 'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
643 'column_name' => 'test_date_' . $this->getCustomFieldID('select_date'),
644 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.test_date_' . $this->getCustomFieldID('select_date'),
645 'extends_table' => 'civicrm_contact',
646 'search_table' => 'contact_a',
8392a043 647 ],
648 $this->getCustomFieldName('link') => [
649 'name' => $this->getCustomFieldName('link'),
650 'type' => 2,
651 'title' => 'test_link',
652 'headerPattern' => '//',
653 'import' => 1,
654 'custom_field_id' => $this->getCustomFieldID('link'),
655 'options_per_line' => NULL,
656 'text_length' => NULL,
657 'data_type' => 'Link',
658 'html_type' => 'Link',
659 'is_search_range' => '0',
cef2e96c 660 'id' => $this->getCustomFieldID('link'),
661 'label' => 'test_link',
662 'groupTitle' => 'Custom Group',
663 'default_value' => 'http://civicrm.org',
664 'custom_group_id' => $customGroupID,
665 'extends' => 'Contact',
666 'extends_entity_column_value' => NULL,
667 'extends_entity_column_id' => NULL,
668 'is_view' => '0',
669 'is_multiple' => '0',
670 'option_group_id' => NULL,
671 'date_format' => NULL,
672 'time_format' => NULL,
79c9d4c2 673 'is_required' => 0,
cef2e96c 674 'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
675 'column_name' => 'test_link_' . $this->getCustomFieldID('link'),
676 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.test_link_' . $this->getCustomFieldID('link'),
677 'extends_table' => 'civicrm_contact',
678 'search_table' => 'contact_a',
8392a043 679 ],
0e1544e7 680 $this->getCustomFieldName('int') => [
681 'name' => $this->getCustomFieldName('int'),
682 'type' => CRM_Utils_Type::T_INT,
683 'title' => 'Enter integer here',
684 'headerPattern' => '//',
685 'import' => 1,
686 'custom_field_id' => $this->getCustomFieldID('int'),
687 'options_per_line' => NULL,
688 'text_length' => NULL,
689 'data_type' => 'Int',
690 'html_type' => 'Text',
691 'is_search_range' => '1',
692 'id' => $this->getCustomFieldID('int'),
693 'label' => 'Enter integer here',
694 'groupTitle' => 'Custom Group',
695 'default_value' => '4',
696 'custom_group_id' => $customGroupID,
697 'extends' => 'Contact',
698 'extends_entity_column_value' => NULL,
699 'extends_entity_column_id' => NULL,
700 'is_view' => '0',
701 'is_multiple' => '0',
702 'option_group_id' => NULL,
703 'date_format' => NULL,
704 'time_format' => NULL,
79c9d4c2 705 'is_required' => 0,
0e1544e7 706 'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
707 'column_name' => $this->getCustomFieldColumnName('int'),
708 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.' . $this->getCustomFieldColumnName('int'),
709 'extends_table' => 'civicrm_contact',
710 'search_table' => 'contact_a',
711 ],
79c9d4c2 712 $this->getCustomFieldName('contact_reference') => [
713 'name' => $this->getCustomFieldName('contact_reference'),
714 'type' => CRM_Utils_Type::T_INT,
715 'title' => 'Contact reference field',
716 'headerPattern' => '//',
717 'import' => 1,
718 'custom_field_id' => $this->getCustomFieldID('contact_reference'),
719 'options_per_line' => NULL,
720 'text_length' => NULL,
721 'data_type' => 'ContactReference',
722 'html_type' => 'Autocomplete-Select',
723 'is_search_range' => '0',
724 'id' => $this->getCustomFieldID('contact_reference'),
725 'label' => 'Contact reference field',
726 'groupTitle' => 'Custom Group',
727 'default_value' => NULL,
728 'custom_group_id' => $customGroupID,
729 'extends' => 'Contact',
730 'extends_entity_column_value' => NULL,
731 'extends_entity_column_id' => NULL,
732 'is_view' => '0',
733 'is_multiple' => '0',
734 'option_group_id' => NULL,
735 'date_format' => NULL,
736 'time_format' => NULL,
737 'is_required' => 0,
738 'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
739 'column_name' => $this->getCustomFieldColumnName('contact_reference'),
740 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.' . $this->getCustomFieldColumnName('contact_reference'),
741 'extends_table' => 'civicrm_contact',
742 'search_table' => 'contact_a',
743 ],
ca64c337 744 $this->getCustomFieldName('state') => [
745 'name' => $this->getCustomFieldName('state'),
746 'id' => $this->getCustomFieldID('state'),
747 'label' => 'State',
748 'headerPattern' => '//',
749 'title' => 'State',
750 'custom_field_id' => $this->getCustomFieldID('state'),
751 'groupTitle' => 'Custom Group',
752 'default_value' => NULL,
753 'custom_group_id' => $customGroupID,
754 'extends' => 'Contact',
755 'extends_entity_column_value' => NULL,
756 'extends_entity_column_id' => NULL,
757 'is_view' => '0',
758 'is_multiple' => '0',
759 'option_group_id' => NULL,
760 'date_format' => NULL,
761 'time_format' => NULL,
762 'is_required' => 0,
763 'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
764 'column_name' => $this->getCustomFieldColumnName('state'),
765 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.' . $this->getCustomFieldColumnName('state'),
766 'extends_table' => 'civicrm_contact',
767 'search_table' => 'contact_a',
768 'pseudoconstant' => [
769 'table' => 'civicrm_state_province',
770 'keyColumn' => 'id',
771 'labelColumn' => 'name',
772 ],
773 'import' => 1,
774 'data_type' => 'StateProvince',
775 'type' => 1,
776 'html_type' => 'Select State/Province',
777 'text_length' => NULL,
778 'options_per_line' => NULL,
779 'is_search_range' => '0',
780 ],
781 $this->getCustomFieldName('multi_state') => [
782 'id' => $this->getCustomFieldID('multi_state'),
783 'label' => 'State-multi',
784 'headerPattern' => '//',
785 'title' => 'State-multi',
786 'custom_field_id' => $this->getCustomFieldID('multi_state'),
787 'groupTitle' => 'Custom Group',
788 'default_value' => NULL,
789 'custom_group_id' => $customGroupID,
790 'extends' => 'Contact',
791 'extends_entity_column_value' => NULL,
792 'extends_entity_column_id' => NULL,
793 'is_view' => '0',
794 'is_multiple' => '0',
795 'option_group_id' => NULL,
796 'date_format' => NULL,
797 'time_format' => NULL,
798 'is_required' => 0,
799 'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
800 'column_name' => $this->getCustomFieldColumnName('multi_state'),
801 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.' . $this->getCustomFieldColumnName('multi_state'),
802 'extends_table' => 'civicrm_contact',
803 'search_table' => 'contact_a',
804 'pseudoconstant' => [
805 'table' => 'civicrm_state_province',
806 'keyColumn' => 'id',
807 'labelColumn' => 'name',
808 ],
809 'import' => 1,
810 'data_type' => 'StateProvince',
811 'name' => $this->getCustomFieldName('multi_state'),
812 'type' => 1,
813 'html_type' => 'Multi-Select State/Province',
814 'text_length' => NULL,
815 'options_per_line' => NULL,
816 'is_search_range' => '0',
817 ],
818 $this->getCustomFieldName('boolean') => [
819 'id' => $this->getCustomFieldID('boolean'),
820 'label' => 'Yes No',
821 'headerPattern' => '//',
822 'title' => 'Yes No',
823 'custom_field_id' => $this->getCustomFieldID('boolean'),
824 'groupTitle' => 'Custom Group',
825 'default_value' => NULL,
826 'custom_group_id' => $customGroupID,
827 'extends' => 'Contact',
828 'extends_entity_column_value' => NULL,
829 'extends_entity_column_id' => NULL,
830 'is_view' => '0',
831 'is_multiple' => '0',
832 'option_group_id' => NULL,
833 'date_format' => NULL,
834 'time_format' => NULL,
835 'is_required' => 0,
836 'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
837 'column_name' => $this->getCustomFieldColumnName('boolean'),
838 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.' . $this->getCustomFieldColumnName('boolean'),
839 'extends_table' => 'civicrm_contact',
840 'search_table' => 'contact_a',
841 'import' => 1,
842 'data_type' => 'Boolean',
843 'name' => $this->getCustomFieldName('boolean'),
844 'type' => 16,
845 'html_type' => 'Radio',
846 'text_length' => NULL,
847 'options_per_line' => NULL,
848 'is_search_range' => '0',
849 'pseudoconstant' => [
850 'callback' => 'CRM_Core_SelectValues::boolean',
851 ],
852 ],
8392a043 853 ];
854 $this->assertEquals($expected, CRM_Core_BAO_CustomField::getFieldsForImport());
855 }
856
e5ce15c3 857 /**
858 * Test the bulk create function works.
859 */
860 public function testBulkCreate() {
861 $customGroup = $this->customGroupCreate([
862 'extends' => 'Individual',
863 'title' => 'my bulk group',
864 ]);
865 CRM_Core_BAO_CustomField::bulkSave([
866 [
867 'label' => 'Test',
868 'data_type' => 'String',
869 'html_type' => 'Text',
870 'column_name' => 'my_text',
871 ],
872 [
873 'label' => 'test_link',
874 'data_type' => 'Link',
875 'html_type' => 'Link',
876 'is_search_range' => '0',
877 ],
878 ],
879 [
880 'custom_group_id' => $customGroup['id'],
881 'is_active' => 1,
882 'is_searchable' => 1,
883 ]);
884 $dao = CRM_Core_DAO::executeQuery(('SHOW CREATE TABLE ' . $customGroup['values'][$customGroup['id']]['table_name']));
885 $dao->fetch();
886 $this->assertContains('`test_link_2` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL', $dao->Create_Table);
887 $this->assertContains('KEY `INDEX_my_text` (`my_text`)', $dao->Create_Table);
888 }
889
1de1c868
SL
890 /**
891 * Check that outputting the display value for a file field with No description doesn't generate error
892 */
893 public function testFileDisplayValueNoDescription() {
894 $customGroup = $this->customGroupCreate([
895 'extends' => 'Individual',
896 'title' => 'Test Contact File Custom Group',
897 ]);
898 $fileField = $this->customFieldCreate([
899 'custom_group_id' => $customGroup['id'],
900 'data_type' => 'File',
901 'html_type' => 'File',
902 'default_value' => '',
903 ]);
904 $filePath = Civi::paths()->getPath('[civicrm.files]/custom/test_file.txt');
905 $file = $this->callAPISuccess('File', 'create', [
906 'uri' => $filePath,
907 ]);
908 $individual = $this->individualCreate(['custom_' . $fileField['id'] => $file['id']]);
909 $expectedDisplayValue = CRM_Core_BAO_File::paperIconAttachment('*', $file['id'])[$file['id']];
910 $this->assertEquals($expectedDisplayValue, CRM_Core_BAO_CustomField::displayValue($file['id'], $fileField['id']));
911 }
912
3566563f
PN
913 /**
914 * Test for hook_civicrm_alterCustomFieldDisplayValue().
915 */
916 public function testAlterCustomFieldDisplayValueHook() {
917 CRM_Utils_Hook_UnitTests::singleton()->setHook('civicrm_alterCustomFieldDisplayValue', [$this, 'alterCustomFieldDisplayValue']);
918 $customGroupId = $this->customGroupCreate([
919 'extends' => 'Individual',
920 'title' => 'Test Contactcustom Group',
921 ])['id'];
922 $fieldId = $this->customFieldCreate([
923 'custom_group_id' => $customGroupId,
924 'name' => 'alter_cf_field',
925 'label' => 'Alter CF Field',
926 ])['id'];
927 $contactId = $this->individualCreate(['custom_' . $fieldId => 'Test']);
928
929 $this->assertEquals('Test', $this->callAPISuccessGetValue('Contact',
930 ['id' => $contactId, 'return' => "custom_{$fieldId}"]
931 ));
932
933 $values = [];
934 $fields = [
935 'custom_' . $fieldId => $this->callAPISuccess('Contact', 'getfield', [
936 'name' => 'custom_' . $fieldId,
937 'action' => 'get',
938 ])['values'],
939 ];
940
941 // CRM_Core_BAO_UFGroup::getValues() invokes CRM_Core_BAO_CustomField::displayValue() function.
942 CRM_Core_BAO_UFGroup::getValues($contactId, $fields, $values);
943 $this->assertEquals('New value', $values['Alter CF Field']);
944 }
945
946 /**
947 * @param string $displayValue
948 * @param mixed $value
949 * @param int $entityId
950 * @param array $fieldInfo
951 *
952 */
953 public function alterCustomFieldDisplayValue(&$displayValue, $value, $entityId, $fieldInfo) {
954 if ($fieldInfo['name'] == 'alter_cf_field') {
955 $displayValue = 'New value';
956 }
957 }
958
6a488035 959}