4 * Class CRM_Core_BAO_CustomFieldTest
7 class CRM_Core_BAO_CustomFieldTest
extends CiviUnitTestCase
{
9 public function setUp() {
13 public function testCreateCustomField() {
14 $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
17 'data_type' => 'String',
18 'html_type' => 'Text',
19 'custom_group_id' => $customGroup['id'],
21 CRM_Core_BAO_CustomField
::create($fields);
22 $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
23 'Database check for created CustomField.'
26 'id' => $customFieldID,
27 'label' => 'editTestFld',
29 'data_type' => 'String',
30 'html_type' => 'Text',
31 'custom_group_id' => $customGroup['id'],
34 CRM_Core_BAO_CustomField
::create($fields);
35 $this->assertDBNotNull('CRM_Core_DAO_CustomField', 1, 'id', 'is_active', 'Database check for edited CustomField.');
36 $this->assertDBNotNull('CRM_Core_DAO_CustomField', $fields['label'], 'id', 'label', 'Database check for edited CustomField.');
38 $dbFieldName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'name', 'id', 'Database check for edited CustomField.');
39 $dbColumnName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'column_name', 'id', 'Database check for edited CustomField.');
40 $this->assertEquals(strtolower("{$dbFieldName}_{$customFieldID}"), $dbColumnName,
41 "Column name ends in ID");
43 $this->customGroupDelete($customGroup['id']);
46 public function testCreateCustomFieldColumnName() {
47 $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
49 'label' => 'testFld 2',
50 'column_name' => 'special_colname',
51 'data_type' => 'String',
52 'html_type' => 'Text',
53 'custom_group_id' => $customGroup['id'],
55 CRM_Core_BAO_CustomField
::create($fields);
56 $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
57 'Database check for created CustomField.'
59 $dbColumnName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'column_name', 'id', 'Database check for edited CustomField.');
60 $this->assertEquals($fields['column_name'], $dbColumnName,
61 "Column name set as specified");
63 $this->customGroupDelete($customGroup['id']);
66 public function testCreateCustomFieldName() {
67 $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
69 'label' => 'testFld 2',
70 'name' => 'special_fldlname',
71 'data_type' => 'String',
72 'html_type' => 'Text',
73 'custom_group_id' => $customGroup['id'],
75 CRM_Core_BAO_CustomField
::create($fields);
76 $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
77 'Database check for created CustomField.'
79 $dbFieldName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'name', 'id', 'Database check for edited CustomField.');
80 $this->assertEquals($fields['name'], $dbFieldName,
81 "Column name set as specified");
83 $this->customGroupDelete($customGroup['id']);
86 public function testGetFields() {
87 $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
89 'label' => 'testFld1',
90 'data_type' => 'String',
91 'html_type' => 'Text',
93 'custom_group_id' => $customGroup['id'],
95 CRM_Core_BAO_CustomField
::create($fields);
96 $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
97 'Database check for created CustomField.'
100 'label' => 'testFld2',
101 'data_type' => 'String',
102 'html_type' => 'Text',
104 'custom_group_id' => $customGroup['id'],
106 CRM_Core_BAO_CustomField
::create($fields);
107 $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
108 'Database check for created CustomField.'
111 $this->customGroupDelete($customGroup['id']);
114 public function testGetDisplayedValues() {
115 $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
116 $fieldsToCreate = array(
118 'data_type' => 'Country',
119 'html_type' => 'Select Country',
121 'United States' => 1228,
126 'data_type' => 'StateProvince',
127 'html_type' => 'Multi-Select State/Province',
131 'Alabama, Alaska' => array(1000, 1001),
135 'data_type' => 'String',
136 'html_type' => 'Radio',
137 'option_values' => array(
145 'data_type' => 'String',
146 'html_type' => 'CheckBox',
147 'option_values' => array(
154 'Label1' => array('key1'),
156 'Label2, Label3' => array('key2', 'key3'),
157 'Label3, Label4' => CRM_Utils_Array
::implodePadded(array('key3', 'key4')),
158 'Label1, Label4' => array('key1' => 1, 'key4' => 1),
162 'data_type' => 'Date',
163 'html_type' => 'Select Date',
164 'date_format' => 'd M yy',
167 '1 Jun 1999 1:30PM' => '1999-06-01 13:30',
172 foreach ($fieldsToCreate as $num => $field) {
173 $params = $field +
array(
174 'label' => 'test field ' . $num,
175 'custom_group_id' => $customGroup['id'],
177 unset($params['tests']);
178 $createdField = $this->callAPISuccess('customField', 'create', $params);
179 foreach ($field['tests'] as $expected => $input) {
180 $this->assertEquals($expected, CRM_Core_BAO_CustomField
::displayValue($input, $createdField['id']));
184 $this->customGroupDelete($customGroup['id']);
187 public function testDeleteCustomField() {
188 $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
190 'custom_group_id' => $customGroup['id'],
191 'label' => 'Throwaway Field',
192 'dataType' => 'Memo',
193 'htmlType' => 'TextArea',
196 $customField = $this->customFieldCreate($fields);
197 $fieldObject = new CRM_Core_BAO_CustomField();
198 $fieldObject->id
= $customField['id'];
199 $fieldObject->find(TRUE);
200 CRM_Core_BAO_CustomField
::deleteField($fieldObject);
201 $this->assertDBNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id',
202 'custom_group_id', 'Database check for deleted Custom Field.'
204 $this->customGroupDelete($customGroup['id']);
208 * Move a custom field from $groupA to $groupB.
210 * Make sure that data records are correctly matched and created.
212 public function testMoveField() {
213 $countriesByName = array_flip(CRM_Core_PseudoConstant
::country(FALSE, FALSE));
214 $this->assertTrue($countriesByName['Andorra'] > 0);
216 'A' => $this->customGroupCreate(array(
217 'title' => 'Test_Group A',
218 'name' => 'test_group_a',
219 'extends' => array('Individual'),
225 'B' => $this->customGroupCreate(array(
226 'title' => 'Test_Group B',
227 'name' => 'test_group_b',
228 'extends' => array('Individual'),
235 $groupA = $groups['A']['values'][$groups['A']['id']];
236 $groupB = $groups['B']['values'][$groups['B']['id']];
237 $countryA = $this->customFieldCreate(array(
238 'custom_group_id' => $groups['A']['id'],
239 'label' => 'Country A',
240 'dataType' => 'Country',
241 'htmlType' => 'Select Country',
242 'default_value' => NULL,
244 $countryB = $this->customFieldCreate(array(
245 'custom_group_id' => $groups['A']['id'],
246 'label' => 'Country B',
247 'dataType' => 'Country',
248 'htmlType' => 'Select Country',
249 'default_value' => NULL,
251 $countryC = $this->customFieldCreate(array(
252 'custom_group_id' => $groups['B']['id'],
253 'label' => 'Country C',
254 'dataType' => 'Country',
255 'htmlType' => 'Select Country',
256 'default_value' => NULL,
260 'countryA' => $countryA['values'][$countryA['id']],
261 'countryB' => $countryB['values'][$countryB['id']],
262 'countryC' => $countryC['values'][$countryC['id']],
265 'alice' => $this->individualCreate(array(
266 'first_name' => 'Alice',
267 'last_name' => 'Albertson',
268 'custom_' . $fields['countryA']['id'] => $countriesByName['Andorra'],
269 'custom_' . $fields['countryB']['id'] => $countriesByName['Barbados'],
271 'bob' => $this->individualCreate(array(
272 'first_name' => 'Bob',
273 'last_name' => 'Roberts',
274 'custom_' . $fields['countryA']['id'] => $countriesByName['Austria'],
275 'custom_' . $fields['countryB']['id'] => $countriesByName['Bermuda'],
276 'custom_' . $fields['countryC']['id'] => $countriesByName['Chad'],
278 'carol' => $this->individualCreate(array(
279 'first_name' => 'Carol',
280 'last_name' => 'Carolson',
281 'custom_' . $fields['countryC']['id'] => $countriesByName['Cambodia'],
286 CRM_Core_BAO_CustomField
::moveField($fields['countryB']['id'], $groupB['id']);
288 // Group[A] no longer has fields[countryB]
289 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
291 $this->assertDBQuery(1, "SELECT {$fields['countryB']['column_name']} FROM " . $groupA['table_name']);
292 $this->fail('Expected exception when querying column on wrong table');
294 catch (PEAR_Exception
$e) {
298 // Alice: Group[B] has fields[countryB], but fields[countryC] did not exist before
299 $this->assertDBQuery(1,
300 "SELECT count(*) FROM {$groupB['table_name']}
302 AND {$fields['countryB']['column_name']} = %3
303 AND {$fields['countryC']['column_name']} is null",
305 1 => array($contacts['alice'], 'Integer'),
306 3 => array($countriesByName['Barbados'], 'Integer'),
310 // Bob: Group[B] has merged fields[countryB] and fields[countryC] on the same record
311 $this->assertDBQuery(1,
312 "SELECT count(*) FROM {$groupB['table_name']}
314 AND {$fields['countryB']['column_name']} = %3
315 AND {$fields['countryC']['column_name']} = %4",
317 1 => array($contacts['bob'], 'Integer'),
318 3 => array($countriesByName['Bermuda'], 'Integer'),
319 4 => array($countriesByName['Chad'], 'Integer'),
323 // Carol: Group[B] still has fields[countryC] but did not get fields[countryB]
324 $this->assertDBQuery(1,
325 "SELECT count(*) FROM {$groupB['table_name']}
327 AND {$fields['countryB']['column_name']} is null
328 AND {$fields['countryC']['column_name']} = %4",
330 1 => array($contacts['carol'], 'Integer'),
331 4 => array($countriesByName['Cambodia'], 'Integer'),
335 $this->customGroupDelete($groups['A']['id']);
336 $this->customGroupDelete($groupB['id']);