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