Fix bug if string value is passed to CRM_Core_BAO_CustomField::displayValue() for...
[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
acb109b7 5 * @group headless
aba1cd8b 6 */
6a488035 7class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
6a488035 8
c6b559da 9 protected $customFieldID;
10
00be9182 11 public function setUp() {
6a488035
TO
12 parent::setUp();
13 }
14
08e37a2a 15 public function testCreateCustomField() {
c6b559da 16 $customGroup = $this->createCustomField();
08e37a2a 17 $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
6a488035
TO
18 'Database check for created CustomField.'
19 );
20 $fields = array(
21 'id' => $customFieldID,
22 'label' => 'editTestFld',
23 'is_active' => 1,
24 'data_type' => 'String',
25 'html_type' => 'Text',
08e37a2a 26 'custom_group_id' => $customGroup['id'],
6a488035
TO
27 );
28
08e37a2a 29 CRM_Core_BAO_CustomField::create($fields);
6a488035
TO
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.');
32
8b3b9a2e
ARW
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,
92915c55 36 "Column name ends in ID");
8b3b9a2e 37
08e37a2a 38 $this->customGroupDelete($customGroup['id']);
8b3b9a2e 39 }
b2042573 40
08e37a2a 41 public function testCreateCustomFieldColumnName() {
42 $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
8b3b9a2e
ARW
43 $fields = array(
44 'label' => 'testFld 2',
45 'column_name' => 'special_colname',
46 'data_type' => 'String',
47 'html_type' => 'Text',
08e37a2a 48 'custom_group_id' => $customGroup['id'],
8b3b9a2e 49 );
08e37a2a 50 CRM_Core_BAO_CustomField::create($fields);
51 $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
8b3b9a2e
ARW
52 'Database check for created CustomField.'
53 );
54 $dbColumnName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'column_name', 'id', 'Database check for edited CustomField.');
55 $this->assertEquals($fields['column_name'], $dbColumnName,
92915c55 56 "Column name set as specified");
8b3b9a2e 57
08e37a2a 58 $this->customGroupDelete($customGroup['id']);
6a488035
TO
59 }
60
08e37a2a 61 public function testCreateCustomFieldName() {
62 $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
43ff3196
TO
63 $fields = array(
64 'label' => 'testFld 2',
65 'name' => 'special_fldlname',
66 'data_type' => 'String',
67 'html_type' => 'Text',
08e37a2a 68 'custom_group_id' => $customGroup['id'],
43ff3196 69 );
08e37a2a 70 CRM_Core_BAO_CustomField::create($fields);
71 $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
43ff3196
TO
72 'Database check for created CustomField.'
73 );
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");
77
08e37a2a 78 $this->customGroupDelete($customGroup['id']);
43ff3196
TO
79 }
80
00be9182 81 public function testGetFields() {
08e37a2a 82 $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
6a488035
TO
83 $fields = array(
84 'label' => 'testFld1',
85 'data_type' => 'String',
86 'html_type' => 'Text',
87 'is_active' => 1,
08e37a2a 88 'custom_group_id' => $customGroup['id'],
6a488035 89 );
08e37a2a 90 CRM_Core_BAO_CustomField::create($fields);
91 $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
6a488035
TO
92 'Database check for created CustomField.'
93 );
94 $fields = array(
95 'label' => 'testFld2',
96 'data_type' => 'String',
97 'html_type' => 'Text',
98 'is_active' => 1,
08e37a2a 99 'custom_group_id' => $customGroup['id'],
6a488035 100 );
08e37a2a 101 CRM_Core_BAO_CustomField::create($fields);
102 $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id',
6a488035
TO
103 'Database check for created CustomField.'
104 );
6a488035 105
08e37a2a 106 $this->customGroupDelete($customGroup['id']);
6a488035
TO
107 }
108
00be9182 109 public function testGetDisplayedValues() {
08e37a2a 110 $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
d5c86914
CW
111 $fieldsToCreate = array(
112 array(
113 'data_type' => 'Country',
114 'html_type' => 'Select Country',
115 'tests' => array(
602b60e0 116 'United States' => 1228,
d5c86914
CW
117 '' => NULL,
118 ),
119 ),
120 array(
121 'data_type' => 'StateProvince',
122 'html_type' => 'Multi-Select State/Province',
123 'tests' => array(
124 '' => 0,
125 'Alabama' => 1000,
126 'Alabama, Alaska' => array(1000, 1001),
127 ),
128 ),
129 array(
130 'data_type' => 'String',
131 'html_type' => 'Radio',
132 'option_values' => array(
133 'key' => 'KeyLabel',
134 ),
135 'tests' => array(
136 'KeyLabel' => 'key',
137 ),
138 ),
139 array(
140 'data_type' => 'String',
141 'html_type' => 'CheckBox',
142 'option_values' => array(
143 'key1' => 'Label1',
144 'key2' => 'Label2',
145 'key3' => 'Label3',
146 'key4' => 'Label4',
147 ),
148 'tests' => array(
149 'Label1' => array('key1'),
150 'Label2' => 'key2',
151 'Label2, Label3' => array('key2', 'key3'),
152 'Label3, Label4' => CRM_Utils_Array::implodePadded(array('key3', 'key4')),
153 'Label1, Label4' => array('key1' => 1, 'key4' => 1),
154 ),
155 ),
156 array(
157 'data_type' => 'Date',
158 'html_type' => 'Select Date',
159 'date_format' => 'd M yy',
160 'time_format' => 1,
161 'tests' => array(
162 '1 Jun 1999 1:30PM' => '1999-06-01 13:30',
163 '' => '',
164 ),
165 ),
6a488035 166 );
d5c86914
CW
167 foreach ($fieldsToCreate as $num => $field) {
168 $params = $field + array(
169 'label' => 'test field ' . $num,
08e37a2a 170 'custom_group_id' => $customGroup['id'],
d5c86914
CW
171 );
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']));
176 }
177 }
6a488035 178
08e37a2a 179 $this->customGroupDelete($customGroup['id']);
6a488035
TO
180 }
181
08e37a2a 182 public function testDeleteCustomField() {
183 $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
6a488035 184 $fields = array(
08e37a2a 185 'custom_group_id' => $customGroup['id'],
8b3b9a2e 186 'label' => 'Throwaway Field',
d9136461 187 'dataType' => 'Memo',
188 'htmlType' => 'TextArea',
6a488035
TO
189 );
190
08e37a2a 191 $customField = $this->customFieldCreate($fields);
192 $fieldObject = new CRM_Core_BAO_CustomField();
193 $fieldObject->id = $customField['id'];
194 $fieldObject->find(TRUE);
195 CRM_Core_BAO_CustomField::deleteField($fieldObject);
196 $this->assertDBNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id',
6a488035
TO
197 'custom_group_id', 'Database check for deleted Custom Field.'
198 );
08e37a2a 199 $this->customGroupDelete($customGroup['id']);
6a488035
TO
200 }
201
202 /**
08e37a2a 203 * Move a custom field from $groupA to $groupB.
204 *
205 * Make sure that data records are correctly matched and created.
6a488035 206 */
00be9182 207 public function testMoveField() {
6a488035 208 $countriesByName = array_flip(CRM_Core_PseudoConstant::country(FALSE, FALSE));
602b60e0 209 $this->assertTrue($countriesByName['Andorra'] > 0);
6a488035 210 $groups = array(
08e37a2a 211 'A' => $this->customGroupCreate(array(
92915c55
TO
212 'title' => 'Test_Group A',
213 'name' => 'test_group_a',
214 'extends' => array('Individual'),
215 'style' => 'Inline',
216 'is_multiple' => 0,
217 'is_active' => 1,
218 'version' => 3,
219 )),
08e37a2a 220 'B' => $this->customGroupCreate(array(
92915c55
TO
221 'title' => 'Test_Group B',
222 'name' => 'test_group_b',
223 'extends' => array('Individual'),
224 'style' => 'Inline',
225 'is_multiple' => 0,
226 'is_active' => 1,
227 'version' => 3,
228 )),
6a488035 229 );
08e37a2a 230 $groupA = $groups['A']['values'][$groups['A']['id']];
231 $groupB = $groups['B']['values'][$groups['B']['id']];
232 $countryA = $this->customFieldCreate(array(
233 'custom_group_id' => $groups['A']['id'],
234 'label' => 'Country A',
235 'dataType' => 'Country',
236 'htmlType' => 'Select Country',
237 'default_value' => NULL,
238 ));
239 $countryB = $this->customFieldCreate(array(
240 'custom_group_id' => $groups['A']['id'],
241 'label' => 'Country B',
242 'dataType' => 'Country',
243 'htmlType' => 'Select Country',
244 'default_value' => NULL,
245 ));
246 $countryC = $this->customFieldCreate(array(
247 'custom_group_id' => $groups['B']['id'],
248 'label' => 'Country C',
249 'dataType' => 'Country',
250 'htmlType' => 'Select Country',
251 'default_value' => NULL,
252 ));
253
6a488035 254 $fields = array(
08e37a2a 255 'countryA' => $countryA['values'][$countryA['id']],
256 'countryB' => $countryB['values'][$countryB['id']],
257 'countryC' => $countryC['values'][$countryC['id']],
6a488035
TO
258 );
259 $contacts = array(
f2040bc6 260 'alice' => $this->individualCreate(array(
92915c55
TO
261 'first_name' => 'Alice',
262 'last_name' => 'Albertson',
602b60e0
SL
263 'custom_' . $fields['countryA']['id'] => $countriesByName['Andorra'],
264 'custom_' . $fields['countryB']['id'] => $countriesByName['Barbados'],
92915c55 265 )),
f2040bc6 266 'bob' => $this->individualCreate(array(
92915c55
TO
267 'first_name' => 'Bob',
268 'last_name' => 'Roberts',
602b60e0
SL
269 'custom_' . $fields['countryA']['id'] => $countriesByName['Austria'],
270 'custom_' . $fields['countryB']['id'] => $countriesByName['Bermuda'],
271 'custom_' . $fields['countryC']['id'] => $countriesByName['Chad'],
92915c55 272 )),
f2040bc6 273 'carol' => $this->individualCreate(array(
92915c55
TO
274 'first_name' => 'Carol',
275 'last_name' => 'Carolson',
602b60e0 276 'custom_' . $fields['countryC']['id'] => $countriesByName['Cambodia'],
92915c55 277 )),
6a488035
TO
278 );
279
280 // Move!
08e37a2a 281 CRM_Core_BAO_CustomField::moveField($fields['countryB']['id'], $groupB['id']);
6a488035
TO
282
283 // Group[A] no longer has fields[countryB]
284 $errorScope = CRM_Core_TemporaryErrorScope::useException();
285 try {
08e37a2a 286 $this->assertDBQuery(1, "SELECT {$fields['countryB']['column_name']} FROM " . $groupA['table_name']);
6a488035
TO
287 $this->fail('Expected exception when querying column on wrong table');
288 }
92915c55
TO
289 catch (PEAR_Exception$e) {
290 }
6a488035
TO
291 $errorScope = NULL;
292
293 // Alice: Group[B] has fields[countryB], but fields[countryC] did not exist before
294 $this->assertDBQuery(1,
08e37a2a 295 "SELECT count(*) FROM {$groupB['table_name']}
b2042573 296 WHERE entity_id = %1
08e37a2a 297 AND {$fields['countryB']['column_name']} = %3
298 AND {$fields['countryC']['column_name']} is null",
6a488035
TO
299 array(
300 1 => array($contacts['alice'], 'Integer'),
602b60e0 301 3 => array($countriesByName['Barbados'], 'Integer'),
6a488035
TO
302 )
303 );
304
305 // Bob: Group[B] has merged fields[countryB] and fields[countryC] on the same record
306 $this->assertDBQuery(1,
08e37a2a 307 "SELECT count(*) FROM {$groupB['table_name']}
b2042573 308 WHERE entity_id = %1
08e37a2a 309 AND {$fields['countryB']['column_name']} = %3
310 AND {$fields['countryC']['column_name']} = %4",
6a488035
TO
311 array(
312 1 => array($contacts['bob'], 'Integer'),
602b60e0
SL
313 3 => array($countriesByName['Bermuda'], 'Integer'),
314 4 => array($countriesByName['Chad'], 'Integer'),
6a488035
TO
315 )
316 );
317
318 // Carol: Group[B] still has fields[countryC] but did not get fields[countryB]
319 $this->assertDBQuery(1,
08e37a2a 320 "SELECT count(*) FROM {$groupB['table_name']}
b2042573 321 WHERE entity_id = %1
08e37a2a 322 AND {$fields['countryB']['column_name']} is null
323 AND {$fields['countryC']['column_name']} = %4",
6a488035
TO
324 array(
325 1 => array($contacts['carol'], 'Integer'),
602b60e0 326 4 => array($countriesByName['Cambodia'], 'Integer'),
6a488035
TO
327 )
328 );
329
08e37a2a 330 $this->customGroupDelete($groups['A']['id']);
331 $this->customGroupDelete($groupB['id']);
6a488035 332 }
96025800 333
c6b559da 334 /**
335 * Test get custom field id function.
336 */
337 public function testGetCustomFieldID() {
338 $this->createCustomField();
339 $fieldID = CRM_Core_BAO_CustomField::getCustomFieldID('testFld');
340 $this->assertEquals($this->customFieldID, $fieldID);
341
342 $fieldID = CRM_Core_BAO_CustomField::getCustomFieldID('testFld', 'new custom group');
343 $this->assertEquals($this->customFieldID, $fieldID);
344 }
345
346 /**
347 * @return array
348 */
349 protected function createCustomField() {
350 $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
351 $fields = array(
352 'label' => 'testFld',
353 'data_type' => 'String',
354 'html_type' => 'Text',
355 'custom_group_id' => $customGroup['id'],
356 );
357 $field = CRM_Core_BAO_CustomField::create($fields);
358 $this->customFieldID = $field->id;
359 return $customGroup;
360 }
361
6a488035 362}