Fix civicrm_country template file
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / CustomFieldTest.php
... / ...
CommitLineData
1<?php
2
3/**
4 * Class CRM_Core_BAO_CustomFieldTest
5 * @group headless
6 */
7class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
8
9 public function setUp() {
10 parent::setUp();
11 }
12
13 public function testCreateCustomField() {
14 $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
15 $fields = array(
16 'label' => 'testFld',
17 'data_type' => 'String',
18 'html_type' => 'Text',
19 'custom_group_id' => $customGroup['id'],
20 );
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.'
24 );
25 $fields = array(
26 'id' => $customFieldID,
27 'label' => 'editTestFld',
28 'is_active' => 1,
29 'data_type' => 'String',
30 'html_type' => 'Text',
31 'custom_group_id' => $customGroup['id'],
32 );
33
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.');
37
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");
42
43 $this->customGroupDelete($customGroup['id']);
44 }
45
46 public function testCreateCustomFieldColumnName() {
47 $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
48 $fields = array(
49 'label' => 'testFld 2',
50 'column_name' => 'special_colname',
51 'data_type' => 'String',
52 'html_type' => 'Text',
53 'custom_group_id' => $customGroup['id'],
54 );
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.'
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,
61 "Column name set as specified");
62
63 $this->customGroupDelete($customGroup['id']);
64 }
65
66 public function testCreateCustomFieldName() {
67 $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
68 $fields = array(
69 'label' => 'testFld 2',
70 'name' => 'special_fldlname',
71 'data_type' => 'String',
72 'html_type' => 'Text',
73 'custom_group_id' => $customGroup['id'],
74 );
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.'
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
83 $this->customGroupDelete($customGroup['id']);
84 }
85
86 public function testGetFields() {
87 $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
88 $fields = array(
89 'label' => 'testFld1',
90 'data_type' => 'String',
91 'html_type' => 'Text',
92 'is_active' => 1,
93 'custom_group_id' => $customGroup['id'],
94 );
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.'
98 );
99 $fields = array(
100 'label' => 'testFld2',
101 'data_type' => 'String',
102 'html_type' => 'Text',
103 'is_active' => 1,
104 'custom_group_id' => $customGroup['id'],
105 );
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.'
109 );
110
111 $this->customGroupDelete($customGroup['id']);
112 }
113
114 public function testGetDisplayedValues() {
115 $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
116 $fieldsToCreate = array(
117 array(
118 'data_type' => 'Country',
119 'html_type' => 'Select Country',
120 'tests' => array(
121 'UNITED STATES' => 1228,
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 ),
171 );
172 foreach ($fieldsToCreate as $num => $field) {
173 $params = $field + array(
174 'label' => 'test field ' . $num,
175 'custom_group_id' => $customGroup['id'],
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 }
183
184 $this->customGroupDelete($customGroup['id']);
185 }
186
187 public function testDeleteCustomField() {
188 $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
189 $fields = array(
190 'custom_group_id' => $customGroup['id'],
191 'label' => 'Throwaway Field',
192 'dataType' => 'Memo',
193 'htmlType' => 'TextArea',
194 );
195
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.'
203 );
204 $this->customGroupDelete($customGroup['id']);
205 }
206
207 /**
208 * Move a custom field from $groupA to $groupB.
209 *
210 * Make sure that data records are correctly matched and created.
211 */
212 public function testMoveField() {
213 $countriesByName = array_flip(CRM_Core_PseudoConstant::country(FALSE, FALSE));
214 $this->assertTrue($countriesByName['ANDORRA'] > 0);
215 $groups = array(
216 'A' => $this->customGroupCreate(array(
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 )),
225 'B' => $this->customGroupCreate(array(
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 )),
234 );
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
259 $fields = array(
260 'countryA' => $countryA['values'][$countryA['id']],
261 'countryB' => $countryB['values'][$countryB['id']],
262 'countryC' => $countryC['values'][$countryC['id']],
263 );
264 $contacts = array(
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'],
270 )),
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'],
277 )),
278 'carol' => $this->individualCreate(array(
279 'first_name' => 'Carol',
280 'last_name' => 'Carolson',
281 'custom_' . $fields['countryC']['id'] => $countriesByName['CAMBODIA'],
282 )),
283 );
284
285 // Move!
286 CRM_Core_BAO_CustomField::moveField($fields['countryB']['id'], $groupB['id']);
287
288 // Group[A] no longer has fields[countryB]
289 $errorScope = CRM_Core_TemporaryErrorScope::useException();
290 try {
291 $this->assertDBQuery(1, "SELECT {$fields['countryB']['column_name']} FROM " . $groupA['table_name']);
292 $this->fail('Expected exception when querying column on wrong table');
293 }
294 catch (PEAR_Exception$e) {
295 }
296 $errorScope = NULL;
297
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']}
301 WHERE entity_id = %1
302 AND {$fields['countryB']['column_name']} = %3
303 AND {$fields['countryC']['column_name']} is null",
304 array(
305 1 => array($contacts['alice'], 'Integer'),
306 3 => array($countriesByName['BARBADOS'], 'Integer'),
307 )
308 );
309
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']}
313 WHERE entity_id = %1
314 AND {$fields['countryB']['column_name']} = %3
315 AND {$fields['countryC']['column_name']} = %4",
316 array(
317 1 => array($contacts['bob'], 'Integer'),
318 3 => array($countriesByName['BERMUDA'], 'Integer'),
319 4 => array($countriesByName['CHAD'], 'Integer'),
320 )
321 );
322
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']}
326 WHERE entity_id = %1
327 AND {$fields['countryB']['column_name']} is null
328 AND {$fields['countryC']['column_name']} = %4",
329 array(
330 1 => array($contacts['carol'], 'Integer'),
331 4 => array($countriesByName['CAMBODIA'], 'Integer'),
332 )
333 );
334
335 $this->customGroupDelete($groups['A']['id']);
336 $this->customGroupDelete($groupB['id']);
337 }
338
339}