Merge pull request #16888 from mattwire/addvarsanyregion
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / CustomFieldTest.php
1 <?php
2
3 /**
4 * Class CRM_Core_BAO_CustomFieldTest
5 *
6 * @group headless
7 */
8 class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
9
10 use CRMTraits_Custom_CustomDataTrait;
11
12 protected $customFieldID;
13
14 /**
15 * Clean up after test.
16 *
17 * @throws \Exception
18 */
19 public function tearDown() {
20 $this->quickCleanup([], TRUE);
21 parent::tearDown();
22 }
23
24 /**
25 * Test creating a custom field.
26 */
27 public function testCreateCustomField() {
28 $customGroup = $this->createCustomField();
29 $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
30 'Database check for created CustomField.'
31 );
32 $fields = [
33 'id' => $customFieldID,
34 'label' => 'editTestFld',
35 'is_active' => 1,
36 'data_type' => 'String',
37 'html_type' => 'Text',
38 'custom_group_id' => $customGroup['id'],
39 ];
40
41 CRM_Core_BAO_CustomField::create($fields);
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
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,
48 "Column name ends in ID");
49
50 $this->customGroupDelete($customGroup['id']);
51 }
52
53 /**
54 * Test custom field create accepts passed column name.
55 */
56 public function testCreateCustomFieldColumnName() {
57 $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
58 $fields = [
59 'label' => 'testFld 2',
60 'column_name' => 'special_colname',
61 'data_type' => 'String',
62 'html_type' => 'Text',
63 'custom_group_id' => $customGroup['id'],
64 ];
65 CRM_Core_BAO_CustomField::create($fields);
66 $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
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,
71 "Column name set as specified");
72
73 $this->customGroupDelete($customGroup['id']);
74 }
75
76 /**
77 * Test that name is used for the column.
78 */
79 public function testCreateCustomFieldName() {
80 $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
81 $fields = [
82 'label' => 'testFld 2',
83 'name' => 'special_fldlname',
84 'data_type' => 'String',
85 'html_type' => 'Text',
86 'custom_group_id' => $customGroup['id'],
87 ];
88 CRM_Core_BAO_CustomField::create($fields);
89 $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
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
96 $this->customGroupDelete($customGroup['id']);
97 }
98
99 /**
100 * Test get fields function.
101 */
102 public function testGetFields() {
103 $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
104 $fields = [
105 'label' => 'testFld1',
106 'data_type' => 'String',
107 'html_type' => 'Text',
108 'is_active' => 1,
109 'custom_group_id' => $customGroup['id'],
110 ];
111 CRM_Core_BAO_CustomField::create($fields);
112 $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
113 'Database check for created CustomField.'
114 );
115 $fields = [
116 'label' => 'testFld2',
117 'data_type' => 'String',
118 'html_type' => 'Text',
119 'is_active' => 1,
120 'custom_group_id' => $customGroup['id'],
121 ];
122 CRM_Core_BAO_CustomField::create($fields);
123 $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
124 'Database check for created CustomField.'
125 );
126
127 $this->customGroupDelete($customGroup['id']);
128 }
129
130 /**
131 * @throws \Exception
132 */
133 public function testGetDisplayedValues() {
134 $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
135 $fieldsToCreate = [
136 [
137 'data_type' => 'Country',
138 'html_type' => 'Select Country',
139 'tests' => [
140 'United States' => 1228,
141 '' => NULL,
142 ],
143 ],
144 [
145 'data_type' => 'StateProvince',
146 'html_type' => 'Multi-Select State/Province',
147 'tests' => [
148 '' => 0,
149 'Alabama' => 1000,
150 'Alabama, Alaska' => [1000, 1001],
151 ],
152 ],
153 [
154 'data_type' => 'String',
155 'html_type' => 'Radio',
156 'option_values' => [
157 'key' => 'KeyLabel',
158 ],
159 'tests' => [
160 'KeyLabel' => 'key',
161 ],
162 ],
163 [
164 'data_type' => 'String',
165 'html_type' => 'CheckBox',
166 'option_values' => [
167 'key1' => 'Label1',
168 'key2' => 'Label2',
169 'key3' => 'Label3',
170 'key4' => 'Label4',
171 ],
172 'tests' => [
173 'Label1' => ['key1'],
174 'Label2' => 'key2',
175 'Label2, Label3' => ['key2', 'key3'],
176 'Label3, Label4' => CRM_Utils_Array::implodePadded(['key3', 'key4']),
177 'Label1, Label4' => ['key1' => 1, 'key4' => 1],
178 ],
179 ],
180 [
181 'data_type' => 'Date',
182 'html_type' => 'Select Date',
183 'date_format' => 'd M yy',
184 'time_format' => 1,
185 'tests' => [
186 '1 Jun 1999 1:30PM' => '1999-06-01 13:30',
187 '' => '',
188 ],
189 ],
190 ];
191 foreach ($fieldsToCreate as $num => $field) {
192 $params = $field + ['label' => 'test field ' . $num, 'custom_group_id' => $customGroup['id']];
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 }
199
200 $this->customGroupDelete($customGroup['id']);
201 }
202
203 /**
204 * Test CRM_Core_BAO_CustomField::displayValue.
205 *
206 * @throws \CRM_Core_Exception
207 * @throws \Exception
208 */
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
229 public function testDeleteCustomField() {
230 $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
231 $fields = [
232 'custom_group_id' => $customGroup['id'],
233 'label' => 'Throwaway Field',
234 'dataType' => 'Memo',
235 'htmlType' => 'TextArea',
236 ];
237
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',
244 'custom_group_id', 'Database check for deleted Custom Field.'
245 );
246 $this->customGroupDelete($customGroup['id']);
247 }
248
249 /**
250 * Move a custom field from $groupA to $groupB.
251 *
252 * Make sure that data records are correctly matched and created.
253 *
254 * @throws \CRM_Core_Exception
255 */
256 public function testMoveField() {
257 $countriesByName = array_flip(CRM_Core_PseudoConstant::country(FALSE, FALSE));
258 $this->assertTrue($countriesByName['Andorra'] > 0);
259 $groups = [
260 'A' => $this->customGroupCreate([
261 'title' => 'Test_Group A',
262 'name' => 'test_group_a',
263 'extends' => ['Individual'],
264 'style' => 'Inline',
265 'is_multiple' => 0,
266 'is_active' => 1,
267 'version' => 3,
268 ]),
269 'B' => $this->customGroupCreate([
270 'title' => 'Test_Group B',
271 'name' => 'test_group_b',
272 'extends' => ['Individual'],
273 'style' => 'Inline',
274 'is_multiple' => 0,
275 'is_active' => 1,
276 'version' => 3,
277 ]),
278 ];
279 $groupA = $groups['A']['values'][$groups['A']['id']];
280 $groupB = $groups['B']['values'][$groups['B']['id']];
281 $countryA = $this->customFieldCreate([
282 'custom_group_id' => $groups['A']['id'],
283 'label' => 'Country A',
284 'dataType' => 'Country',
285 'htmlType' => 'Select Country',
286 'default_value' => NULL,
287 ]);
288 $countryB = $this->customFieldCreate([
289 'custom_group_id' => $groups['A']['id'],
290 'label' => 'Country B',
291 'dataType' => 'Country',
292 'htmlType' => 'Select Country',
293 'default_value' => NULL,
294 ]);
295 $countryC = $this->customFieldCreate([
296 'custom_group_id' => $groups['B']['id'],
297 'label' => 'Country C',
298 'dataType' => 'Country',
299 'htmlType' => 'Select Country',
300 'default_value' => NULL,
301 ]);
302
303 $fields = [
304 'countryA' => $countryA['values'][$countryA['id']],
305 'countryB' => $countryB['values'][$countryB['id']],
306 'countryC' => $countryC['values'][$countryC['id']],
307 ];
308 $contacts = [
309 'alice' => $this->individualCreate([
310 'first_name' => 'Alice',
311 'last_name' => 'Albertson',
312 'custom_' . $fields['countryA']['id'] => $countriesByName['Andorra'],
313 'custom_' . $fields['countryB']['id'] => $countriesByName['Barbados'],
314 ]),
315 'bob' => $this->individualCreate([
316 'first_name' => 'Bob',
317 'last_name' => 'Roberts',
318 'custom_' . $fields['countryA']['id'] => $countriesByName['Austria'],
319 'custom_' . $fields['countryB']['id'] => $countriesByName['Bermuda'],
320 'custom_' . $fields['countryC']['id'] => $countriesByName['Chad'],
321 ]),
322 'carol' => $this->individualCreate([
323 'first_name' => 'Carol',
324 'last_name' => 'Carolson',
325 'custom_' . $fields['countryC']['id'] => $countriesByName['Cambodia'],
326 ]),
327 ];
328
329 // Move!
330 CRM_Core_BAO_CustomField::moveField($fields['countryB']['id'], $groupB['id']);
331
332 // Group[A] no longer has fields[countryB]
333 $errorScope = CRM_Core_TemporaryErrorScope::useException();
334 try {
335 $this->assertDBQuery(1, "SELECT {$fields['countryB']['column_name']} FROM " . $groupA['table_name']);
336 $this->fail('Expected exception when querying column on wrong table');
337 }
338 catch (PEAR_Exception$e) {
339 }
340 $errorScope = NULL;
341
342 // Alice: Group[B] has fields[countryB], but fields[countryC] did not exist before
343 $this->assertDBQuery(1,
344 "SELECT count(*) FROM {$groupB['table_name']}
345 WHERE entity_id = %1
346 AND {$fields['countryB']['column_name']} = %3
347 AND {$fields['countryC']['column_name']} is null",
348 [
349 1 => [$contacts['alice'], 'Integer'],
350 3 => [$countriesByName['Barbados'], 'Integer'],
351 ]
352 );
353
354 // Bob: Group[B] has merged fields[countryB] and fields[countryC] on the same record
355 $this->assertDBQuery(1,
356 "SELECT count(*) FROM {$groupB['table_name']}
357 WHERE entity_id = %1
358 AND {$fields['countryB']['column_name']} = %3
359 AND {$fields['countryC']['column_name']} = %4",
360 [
361 1 => [$contacts['bob'], 'Integer'],
362 3 => [$countriesByName['Bermuda'], 'Integer'],
363 4 => [$countriesByName['Chad'], 'Integer'],
364 ]
365 );
366
367 // Carol: Group[B] still has fields[countryC] but did not get fields[countryB]
368 $this->assertDBQuery(1,
369 "SELECT count(*) FROM {$groupB['table_name']}
370 WHERE entity_id = %1
371 AND {$fields['countryB']['column_name']} is null
372 AND {$fields['countryC']['column_name']} = %4",
373 [
374 1 => [$contacts['carol'], 'Integer'],
375 4 => [$countriesByName['Cambodia'], 'Integer'],
376 ]
377 );
378
379 $this->customGroupDelete($groups['A']['id']);
380 $this->customGroupDelete($groupB['id']);
381 }
382
383 /**
384 * Test get custom field id function.
385 *
386 * @throws \CiviCRM_API3_Exception
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);
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);
404 }
405
406 /**
407 * Create a custom field
408 *
409 * @param string $groupTitle
410 *
411 * @return array
412 */
413 protected function createCustomField($groupTitle = 'new custom group') {
414 $customGroup = $this->customGroupCreate([
415 'extends' => 'Individual',
416 'title' => $groupTitle,
417 ]);
418 $fields = [
419 'label' => 'testFld',
420 'data_type' => 'String',
421 'html_type' => 'Text',
422 'custom_group_id' => $customGroup['id'],
423 ];
424 $field = CRM_Core_BAO_CustomField::create($fields);
425 $this->customFieldID = $field->id;
426 return $customGroup;
427 }
428
429 /**
430 * Test the getFieldsForImport function.
431 *
432 * @throws \Exception
433 */
434 public function testGetFieldsForImport() {
435 $this->entity = 'Contact';
436 $this->createCustomGroupWithFieldsOfAllTypes();
437 $customGroupID = $this->ids['CustomGroup']['Custom Group'];
438 $expected = [
439 $this->getCustomFieldName('country') => [
440 'name' => $this->getCustomFieldName('country'),
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,
448 'data_type' => 'Country',
449 'html_type' => 'Select Country',
450 'is_search_range' => '0',
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,
464 'is_required' => 0,
465 'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
466 'column_name' => 'country_' . $this->getCustomFieldID('country'),
467 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.country_' . $this->getCustomFieldID('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('file') => [
478 'name' => $this->getCustomFieldName('file'),
479 'type' => 2,
480 'title' => 'My file',
481 'headerPattern' => '//',
482 'import' => 1,
483 'custom_field_id' => $this->getCustomFieldID('file'),
484 'options_per_line' => NULL,
485 'text_length' => NULL,
486 'data_type' => 'File',
487 'html_type' => 'File',
488 'is_search_range' => '0',
489 'id' => $this->getCustomFieldID('file'),
490 'label' => 'My file',
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' => 'my_file_' . $this->getCustomFieldID('file'),
505 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.my_file_' . $this->getCustomFieldID('file'),
506 'extends_table' => 'civicrm_contact',
507 'search_table' => 'contact_a',
508 ],
509 $this->getCustomFieldName('text') => [
510 'name' => $this->getCustomFieldName('text'),
511 'type' => 2,
512 'title' => 'Enter text here',
513 'headerPattern' => '//',
514 'import' => 1,
515 'custom_field_id' => $this->getCustomFieldID('text'),
516 'options_per_line' => NULL,
517 'text_length' => 300,
518 'data_type' => 'String',
519 'html_type' => 'Text',
520 'is_search_range' => '0',
521 'id' => $this->getCustomFieldID('text'),
522 'label' => 'Enter text here',
523 'groupTitle' => 'Custom Group',
524 'default_value' => 'xyz',
525 'custom_group_id' => '1',
526 'extends' => 'Contact',
527 'extends_entity_column_value' => NULL,
528 'extends_entity_column_id' => NULL,
529 'is_view' => '0',
530 'is_multiple' => '0',
531 'option_group_id' => NULL,
532 'date_format' => NULL,
533 'time_format' => NULL,
534 'is_required' => 0,
535 'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
536 'column_name' => 'enter_text_here_' . $this->getCustomFieldID('text'),
537 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.enter_text_here_' . $this->getCustomFieldID('text'),
538 'extends_table' => 'civicrm_contact',
539 'search_table' => 'contact_a',
540 'maxlength' => 300,
541 ],
542 $this->getCustomFieldName('select_string') => [
543 'name' => $this->getCustomFieldName('select_string'),
544 'type' => 2,
545 'title' => 'Pick Color',
546 'headerPattern' => '//',
547 'import' => 1,
548 'custom_field_id' => $this->getCustomFieldID('select_string'),
549 'options_per_line' => NULL,
550 'text_length' => NULL,
551 'data_type' => 'String',
552 'html_type' => 'Select',
553 'is_search_range' => '0',
554 'id' => $this->getCustomFieldID('select_string'),
555 'label' => 'Pick Color',
556 'groupTitle' => 'Custom Group',
557 'default_value' => NULL,
558 'custom_group_id' => $customGroupID,
559 'extends' => 'Contact',
560 'extends_entity_column_value' => NULL,
561 'extends_entity_column_id' => NULL,
562 'is_view' => '0',
563 'is_multiple' => '0',
564 'option_group_id' => $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID('select_string'), 'return' => 'option_group_id']),
565 'date_format' => NULL,
566 'time_format' => NULL,
567 'is_required' => 0,
568 'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
569 'column_name' => 'pick_color_' . $this->getCustomFieldID('select_string'),
570 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.pick_color_' . $this->getCustomFieldID('select_string'),
571 'extends_table' => 'civicrm_contact',
572 'search_table' => 'contact_a',
573 'pseudoconstant' => [
574 'optionGroupName' => $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID('select_string'), 'return' => 'option_group_id.name']),
575 'optionEditPath' => 'civicrm/admin/options/' . $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID('select_string'), 'return' => 'option_group_id.name']),
576 ],
577 ],
578 $this->getCustomFieldName('select_date') => [
579 'name' => $this->getCustomFieldName('select_date'),
580 'type' => 4,
581 'title' => 'Test Date',
582 'headerPattern' => '//',
583 'import' => 1,
584 'custom_field_id' => $this->getCustomFieldID('select_date'),
585 'options_per_line' => NULL,
586 'text_length' => NULL,
587 'data_type' => 'Date',
588 'html_type' => 'Select Date',
589 'is_search_range' => '1',
590 'date_format' => 'mm/dd/yy',
591 'time_format' => '1',
592 'id' => $this->getCustomFieldID('select_date'),
593 'label' => 'Test Date',
594 'groupTitle' => 'Custom Group',
595 'default_value' => '20090711',
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' => NULL,
603 'is_required' => '0',
604 'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
605 'column_name' => 'test_date_' . $this->getCustomFieldID('select_date'),
606 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.test_date_' . $this->getCustomFieldID('select_date'),
607 'extends_table' => 'civicrm_contact',
608 'search_table' => 'contact_a',
609 ],
610 $this->getCustomFieldName('link') => [
611 'name' => $this->getCustomFieldName('link'),
612 'type' => 2,
613 'title' => 'test_link',
614 'headerPattern' => '//',
615 'import' => 1,
616 'custom_field_id' => $this->getCustomFieldID('link'),
617 'options_per_line' => NULL,
618 'text_length' => NULL,
619 'data_type' => 'Link',
620 'html_type' => 'Link',
621 'is_search_range' => '0',
622 'id' => $this->getCustomFieldID('link'),
623 'label' => 'test_link',
624 'groupTitle' => 'Custom Group',
625 'default_value' => 'http://civicrm.org',
626 'custom_group_id' => $customGroupID,
627 'extends' => 'Contact',
628 'extends_entity_column_value' => NULL,
629 'extends_entity_column_id' => NULL,
630 'is_view' => '0',
631 'is_multiple' => '0',
632 'option_group_id' => NULL,
633 'date_format' => NULL,
634 'time_format' => NULL,
635 'is_required' => 0,
636 'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
637 'column_name' => 'test_link_' . $this->getCustomFieldID('link'),
638 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.test_link_' . $this->getCustomFieldID('link'),
639 'extends_table' => 'civicrm_contact',
640 'search_table' => 'contact_a',
641 ],
642 $this->getCustomFieldName('int') => [
643 'name' => $this->getCustomFieldName('int'),
644 'type' => CRM_Utils_Type::T_INT,
645 'title' => 'Enter integer here',
646 'headerPattern' => '//',
647 'import' => 1,
648 'custom_field_id' => $this->getCustomFieldID('int'),
649 'options_per_line' => NULL,
650 'text_length' => NULL,
651 'data_type' => 'Int',
652 'html_type' => 'Text',
653 'is_search_range' => '1',
654 'id' => $this->getCustomFieldID('int'),
655 'label' => 'Enter integer here',
656 'groupTitle' => 'Custom Group',
657 'default_value' => '4',
658 'custom_group_id' => $customGroupID,
659 'extends' => 'Contact',
660 'extends_entity_column_value' => NULL,
661 'extends_entity_column_id' => NULL,
662 'is_view' => '0',
663 'is_multiple' => '0',
664 'option_group_id' => NULL,
665 'date_format' => NULL,
666 'time_format' => NULL,
667 'is_required' => 0,
668 'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
669 'column_name' => $this->getCustomFieldColumnName('int'),
670 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.' . $this->getCustomFieldColumnName('int'),
671 'extends_table' => 'civicrm_contact',
672 'search_table' => 'contact_a',
673 ],
674 $this->getCustomFieldName('contact_reference') => [
675 'name' => $this->getCustomFieldName('contact_reference'),
676 'type' => CRM_Utils_Type::T_INT,
677 'title' => 'Contact reference field',
678 'headerPattern' => '//',
679 'import' => 1,
680 'custom_field_id' => $this->getCustomFieldID('contact_reference'),
681 'options_per_line' => NULL,
682 'text_length' => NULL,
683 'data_type' => 'ContactReference',
684 'html_type' => 'Autocomplete-Select',
685 'is_search_range' => '0',
686 'id' => $this->getCustomFieldID('contact_reference'),
687 'label' => 'Contact reference field',
688 'groupTitle' => 'Custom Group',
689 'default_value' => NULL,
690 'custom_group_id' => $customGroupID,
691 'extends' => 'Contact',
692 'extends_entity_column_value' => NULL,
693 'extends_entity_column_id' => NULL,
694 'is_view' => '0',
695 'is_multiple' => '0',
696 'option_group_id' => NULL,
697 'date_format' => NULL,
698 'time_format' => NULL,
699 'is_required' => 0,
700 'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
701 'column_name' => $this->getCustomFieldColumnName('contact_reference'),
702 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.' . $this->getCustomFieldColumnName('contact_reference'),
703 'extends_table' => 'civicrm_contact',
704 'search_table' => 'contact_a',
705 ],
706 ];
707 $this->assertEquals($expected, CRM_Core_BAO_CustomField::getFieldsForImport());
708 }
709
710 /**
711 * Test the bulk create function works.
712 */
713 public function testBulkCreate() {
714 $customGroup = $this->customGroupCreate([
715 'extends' => 'Individual',
716 'title' => 'my bulk group',
717 ]);
718 CRM_Core_BAO_CustomField::bulkSave([
719 [
720 'label' => 'Test',
721 'data_type' => 'String',
722 'html_type' => 'Text',
723 'column_name' => 'my_text',
724 ],
725 [
726 'label' => 'test_link',
727 'data_type' => 'Link',
728 'html_type' => 'Link',
729 'is_search_range' => '0',
730 ],
731 ],
732 [
733 'custom_group_id' => $customGroup['id'],
734 'is_active' => 1,
735 'is_searchable' => 1,
736 ]);
737 $dao = CRM_Core_DAO::executeQuery(('SHOW CREATE TABLE ' . $customGroup['values'][$customGroup['id']]['table_name']));
738 $dao->fetch();
739 $this->assertContains('`test_link_2` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL', $dao->Create_Table);
740 $this->assertContains('KEY `INDEX_my_text` (`my_text`)', $dao->Create_Table);
741 }
742
743 /**
744 * Check that outputting the display value for a file field with No description doesn't generate error
745 */
746 public function testFileDisplayValueNoDescription() {
747 $customGroup = $this->customGroupCreate([
748 'extends' => 'Individual',
749 'title' => 'Test Contact File Custom Group',
750 ]);
751 $fileField = $this->customFieldCreate([
752 'custom_group_id' => $customGroup['id'],
753 'data_type' => 'File',
754 'html_type' => 'File',
755 'default_value' => '',
756 ]);
757 $filePath = Civi::paths()->getPath('[civicrm.files]/custom/test_file.txt');
758 $file = $this->callAPISuccess('File', 'create', [
759 'uri' => $filePath,
760 ]);
761 $individual = $this->individualCreate(['custom_' . $fileField['id'] => $file['id']]);
762 $expectedDisplayValue = CRM_Core_BAO_File::paperIconAttachment('*', $file['id'])[$file['id']];
763 $this->assertEquals($expectedDisplayValue, CRM_Core_BAO_CustomField::displayValue($file['id'], $fileField['id']));
764 }
765
766 /**
767 * Test for hook_civicrm_alterCustomFieldDisplayValue().
768 */
769 public function testAlterCustomFieldDisplayValueHook() {
770 CRM_Utils_Hook_UnitTests::singleton()->setHook('civicrm_alterCustomFieldDisplayValue', [$this, 'alterCustomFieldDisplayValue']);
771 $customGroupId = $this->customGroupCreate([
772 'extends' => 'Individual',
773 'title' => 'Test Contactcustom Group',
774 ])['id'];
775 $fieldId = $this->customFieldCreate([
776 'custom_group_id' => $customGroupId,
777 'name' => 'alter_cf_field',
778 'label' => 'Alter CF Field',
779 ])['id'];
780 $contactId = $this->individualCreate(['custom_' . $fieldId => 'Test']);
781
782 $this->assertEquals('Test', $this->callAPISuccessGetValue('Contact',
783 ['id' => $contactId, 'return' => "custom_{$fieldId}"]
784 ));
785
786 $values = [];
787 $fields = [
788 'custom_' . $fieldId => $this->callAPISuccess('Contact', 'getfield', [
789 'name' => 'custom_' . $fieldId,
790 'action' => 'get',
791 ])['values'],
792 ];
793
794 // CRM_Core_BAO_UFGroup::getValues() invokes CRM_Core_BAO_CustomField::displayValue() function.
795 CRM_Core_BAO_UFGroup::getValues($contactId, $fields, $values);
796 $this->assertEquals('New value', $values['Alter CF Field']);
797 }
798
799 /**
800 * @param string $displayValue
801 * @param mixed $value
802 * @param int $entityId
803 * @param array $fieldInfo
804 *
805 */
806 public function alterCustomFieldDisplayValue(&$displayValue, $value, $entityId, $fieldInfo) {
807 if ($fieldInfo['name'] == 'alter_cf_field') {
808 $displayValue = 'New value';
809 }
810 }
811
812 }