4 * Class CRM_Core_BAO_CustomFieldTest
7 class CRM_Core_BAO_CustomFieldTest
extends CiviUnitTestCase
{
9 protected $customFieldID;
11 public function setUp() {
15 public function testCreateCustomField() {
16 $customGroup = $this->createCustomField();
17 $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
18 'Database check for created CustomField.'
21 'id' => $customFieldID,
22 'label' => 'editTestFld',
24 'data_type' => 'String',
25 'html_type' => 'Text',
26 'custom_group_id' => $customGroup['id'],
29 CRM_Core_BAO_CustomField
::create($fields);
30 $this->assertDBNotNull('CRM_Core_DAO_CustomField', 1, 'id', 'is_active', 'Database check for edited CustomField.');
31 $this->assertDBNotNull('CRM_Core_DAO_CustomField', $fields['label'], 'id', 'label', 'Database check for edited CustomField.');
33 $dbFieldName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'name', 'id', 'Database check for edited CustomField.');
34 $dbColumnName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'column_name', 'id', 'Database check for edited CustomField.');
35 $this->assertEquals(strtolower("{$dbFieldName}_{$customFieldID}"), $dbColumnName,
36 "Column name ends in ID");
38 $this->customGroupDelete($customGroup['id']);
41 public function testCreateCustomFieldColumnName() {
42 $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
44 'label' => 'testFld 2',
45 'column_name' => 'special_colname',
46 'data_type' => 'String',
47 'html_type' => 'Text',
48 'custom_group_id' => $customGroup['id'],
50 CRM_Core_BAO_CustomField
::create($fields);
51 $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
52 'Database check for created CustomField.'
54 $dbColumnName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'column_name', 'id', 'Database check for edited CustomField.');
55 $this->assertEquals($fields['column_name'], $dbColumnName,
56 "Column name set as specified");
58 $this->customGroupDelete($customGroup['id']);
61 public function testCreateCustomFieldName() {
62 $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
64 'label' => 'testFld 2',
65 'name' => 'special_fldlname',
66 'data_type' => 'String',
67 'html_type' => 'Text',
68 'custom_group_id' => $customGroup['id'],
70 CRM_Core_BAO_CustomField
::create($fields);
71 $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
72 'Database check for created CustomField.'
74 $dbFieldName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'name', 'id', 'Database check for edited CustomField.');
75 $this->assertEquals($fields['name'], $dbFieldName,
76 "Column name set as specified");
78 $this->customGroupDelete($customGroup['id']);
81 public function testGetFields() {
82 $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
84 'label' => 'testFld1',
85 'data_type' => 'String',
86 'html_type' => 'Text',
88 'custom_group_id' => $customGroup['id'],
90 CRM_Core_BAO_CustomField
::create($fields);
91 $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
92 'Database check for created CustomField.'
95 'label' => 'testFld2',
96 'data_type' => 'String',
97 'html_type' => 'Text',
99 'custom_group_id' => $customGroup['id'],
101 CRM_Core_BAO_CustomField
::create($fields);
102 $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
103 'Database check for created CustomField.'
106 $this->customGroupDelete($customGroup['id']);
109 public function testGetDisplayedValues() {
110 $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
111 $fieldsToCreate = array(
113 'data_type' => 'Country',
114 'html_type' => 'Select Country',
116 'United States' => 1228,
121 'data_type' => 'StateProvince',
122 'html_type' => 'Multi-Select State/Province',
126 'Alabama, Alaska' => array(1000, 1001),
130 'data_type' => 'String',
131 'html_type' => 'Radio',
132 'option_values' => array(
140 'data_type' => 'String',
141 'html_type' => 'CheckBox',
142 'option_values' => array(
149 'Label1' => array('key1'),
151 'Label2, Label3' => array('key2', 'key3'),
152 'Label3, Label4' => CRM_Utils_Array
::implodePadded(array('key3', 'key4')),
153 'Label1, Label4' => array('key1' => 1, 'key4' => 1),
157 'data_type' => 'Date',
158 'html_type' => 'Select Date',
159 'date_format' => 'd M yy',
162 '1 Jun 1999 1:30PM' => '1999-06-01 13:30',
167 foreach ($fieldsToCreate as $num => $field) {
168 $params = $field +
array(
169 'label' => 'test field ' . $num,
170 'custom_group_id' => $customGroup['id'],
172 unset($params['tests']);
173 $createdField = $this->callAPISuccess('customField', 'create', $params);
174 foreach ($field['tests'] as $expected => $input) {
175 $this->assertEquals($expected, CRM_Core_BAO_CustomField
::displayValue($input, $createdField['id']));
179 $this->customGroupDelete($customGroup['id']);
182 public function testGetDisplayedValuesContactRef() {
183 $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
185 'data_type' => 'ContactReference',
186 'html_type' => 'Autocomplete-Select',
187 'label' => 'test ref',
188 'custom_group_id' => $customGroup['id'],
190 $createdField = $this->callAPISuccess('customField', 'create', $params);
191 $contact1 = $this->individualCreate();
192 $contact2 = $this->individualCreate(['custom_' . $createdField['id'] => $contact1['id']]);
194 $this->assertEquals($contact1['display_name'], CRM_Core_BAO_CustomField
::displayValue($contact2['id'], $createdField['id']));
195 $this->assertEquals("Bob", CRM_Core_BAO_CustomField
::displayValue("Bob", $createdField['id']));
197 $this->contactDelete($contact2['id']);
198 $this->contactDelete($contact1['id']);
199 $this->customGroupDelete($customGroup['id']);
202 public function testDeleteCustomField() {
203 $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
205 'custom_group_id' => $customGroup['id'],
206 'label' => 'Throwaway Field',
207 'dataType' => 'Memo',
208 'htmlType' => 'TextArea',
211 $customField = $this->customFieldCreate($fields);
212 $fieldObject = new CRM_Core_BAO_CustomField();
213 $fieldObject->id
= $customField['id'];
214 $fieldObject->find(TRUE);
215 CRM_Core_BAO_CustomField
::deleteField($fieldObject);
216 $this->assertDBNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id',
217 'custom_group_id', 'Database check for deleted Custom Field.'
219 $this->customGroupDelete($customGroup['id']);
223 * Move a custom field from $groupA to $groupB.
225 * Make sure that data records are correctly matched and created.
227 public function testMoveField() {
228 $countriesByName = array_flip(CRM_Core_PseudoConstant
::country(FALSE, FALSE));
229 $this->assertTrue($countriesByName['Andorra'] > 0);
231 'A' => $this->customGroupCreate(array(
232 'title' => 'Test_Group A',
233 'name' => 'test_group_a',
234 'extends' => array('Individual'),
240 'B' => $this->customGroupCreate(array(
241 'title' => 'Test_Group B',
242 'name' => 'test_group_b',
243 'extends' => array('Individual'),
250 $groupA = $groups['A']['values'][$groups['A']['id']];
251 $groupB = $groups['B']['values'][$groups['B']['id']];
252 $countryA = $this->customFieldCreate(array(
253 'custom_group_id' => $groups['A']['id'],
254 'label' => 'Country A',
255 'dataType' => 'Country',
256 'htmlType' => 'Select Country',
257 'default_value' => NULL,
259 $countryB = $this->customFieldCreate(array(
260 'custom_group_id' => $groups['A']['id'],
261 'label' => 'Country B',
262 'dataType' => 'Country',
263 'htmlType' => 'Select Country',
264 'default_value' => NULL,
266 $countryC = $this->customFieldCreate(array(
267 'custom_group_id' => $groups['B']['id'],
268 'label' => 'Country C',
269 'dataType' => 'Country',
270 'htmlType' => 'Select Country',
271 'default_value' => NULL,
275 'countryA' => $countryA['values'][$countryA['id']],
276 'countryB' => $countryB['values'][$countryB['id']],
277 'countryC' => $countryC['values'][$countryC['id']],
280 'alice' => $this->individualCreate(array(
281 'first_name' => 'Alice',
282 'last_name' => 'Albertson',
283 'custom_' . $fields['countryA']['id'] => $countriesByName['Andorra'],
284 'custom_' . $fields['countryB']['id'] => $countriesByName['Barbados'],
286 'bob' => $this->individualCreate(array(
287 'first_name' => 'Bob',
288 'last_name' => 'Roberts',
289 'custom_' . $fields['countryA']['id'] => $countriesByName['Austria'],
290 'custom_' . $fields['countryB']['id'] => $countriesByName['Bermuda'],
291 'custom_' . $fields['countryC']['id'] => $countriesByName['Chad'],
293 'carol' => $this->individualCreate(array(
294 'first_name' => 'Carol',
295 'last_name' => 'Carolson',
296 'custom_' . $fields['countryC']['id'] => $countriesByName['Cambodia'],
301 CRM_Core_BAO_CustomField
::moveField($fields['countryB']['id'], $groupB['id']);
303 // Group[A] no longer has fields[countryB]
304 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
306 $this->assertDBQuery(1, "SELECT {$fields['countryB']['column_name']} FROM " . $groupA['table_name']);
307 $this->fail('Expected exception when querying column on wrong table');
309 catch (PEAR_Exception
$e) {
313 // Alice: Group[B] has fields[countryB], but fields[countryC] did not exist before
314 $this->assertDBQuery(1,
315 "SELECT count(*) FROM {$groupB['table_name']}
317 AND {$fields['countryB']['column_name']} = %3
318 AND {$fields['countryC']['column_name']} is null",
320 1 => array($contacts['alice'], 'Integer'),
321 3 => array($countriesByName['Barbados'], 'Integer'),
325 // Bob: Group[B] has merged fields[countryB] and fields[countryC] on the same record
326 $this->assertDBQuery(1,
327 "SELECT count(*) FROM {$groupB['table_name']}
329 AND {$fields['countryB']['column_name']} = %3
330 AND {$fields['countryC']['column_name']} = %4",
332 1 => array($contacts['bob'], 'Integer'),
333 3 => array($countriesByName['Bermuda'], 'Integer'),
334 4 => array($countriesByName['Chad'], 'Integer'),
338 // Carol: Group[B] still has fields[countryC] but did not get fields[countryB]
339 $this->assertDBQuery(1,
340 "SELECT count(*) FROM {$groupB['table_name']}
342 AND {$fields['countryB']['column_name']} is null
343 AND {$fields['countryC']['column_name']} = %4",
345 1 => array($contacts['carol'], 'Integer'),
346 4 => array($countriesByName['Cambodia'], 'Integer'),
350 $this->customGroupDelete($groups['A']['id']);
351 $this->customGroupDelete($groupB['id']);
355 * Test get custom field id function.
357 public function testGetCustomFieldID() {
358 $this->createCustomField();
359 $fieldID = CRM_Core_BAO_CustomField
::getCustomFieldID('testFld');
360 $this->assertEquals($this->customFieldID
, $fieldID);
362 $fieldID = CRM_Core_BAO_CustomField
::getCustomFieldID('testFld', 'new custom group');
363 $this->assertEquals($this->customFieldID
, $fieldID);
369 protected function createCustomField() {
370 $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
372 'label' => 'testFld',
373 'data_type' => 'String',
374 'html_type' => 'Text',
375 'custom_group_id' => $customGroup['id'],
377 $field = CRM_Core_BAO_CustomField
::create($fields);
378 $this->customFieldID
= $field->id
;