Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
0eea664b | 2 | |
aba1cd8b EM |
3 | /** |
4 | * Class CRM_Core_BAO_CustomFieldTest | |
acb109b7 | 5 | * @group headless |
aba1cd8b | 6 | */ |
6a488035 | 7 | class 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 | ||
17369736 AS |
182 | public function testGetDisplayedValuesContactRef() { |
183 | $customGroup = $this->customGroupCreate(['extends' => 'Individual']); | |
184 | $params = [ | |
185 | 'data_type' => 'ContactReference', | |
186 | 'html_type' => 'Autocomplete-Select', | |
187 | 'label' => 'test ref', | |
188 | 'custom_group_id' => $customGroup['id'], | |
189 | ]; | |
190 | $createdField = $this->callAPISuccess('customField', 'create', $params); | |
191 | $contact1 = $this->individualCreate(); | |
192 | $contact2 = $this->individualCreate(['custom_' . $createdField['id'] => $contact1['id']]); | |
193 | ||
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'])); | |
196 | ||
197 | $this->contactDelete($contact2['id']); | |
198 | $this->contactDelete($contact1['id']); | |
199 | $this->customGroupDelete($customGroup['id']); | |
200 | } | |
201 | ||
08e37a2a | 202 | public function testDeleteCustomField() { |
203 | $customGroup = $this->customGroupCreate(array('extends' => 'Individual')); | |
6a488035 | 204 | $fields = array( |
08e37a2a | 205 | 'custom_group_id' => $customGroup['id'], |
8b3b9a2e | 206 | 'label' => 'Throwaway Field', |
d9136461 | 207 | 'dataType' => 'Memo', |
208 | 'htmlType' => 'TextArea', | |
6a488035 TO |
209 | ); |
210 | ||
08e37a2a | 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', | |
6a488035 TO |
217 | 'custom_group_id', 'Database check for deleted Custom Field.' |
218 | ); | |
08e37a2a | 219 | $this->customGroupDelete($customGroup['id']); |
6a488035 TO |
220 | } |
221 | ||
222 | /** | |
08e37a2a | 223 | * Move a custom field from $groupA to $groupB. |
224 | * | |
225 | * Make sure that data records are correctly matched and created. | |
6a488035 | 226 | */ |
00be9182 | 227 | public function testMoveField() { |
6a488035 | 228 | $countriesByName = array_flip(CRM_Core_PseudoConstant::country(FALSE, FALSE)); |
602b60e0 | 229 | $this->assertTrue($countriesByName['Andorra'] > 0); |
6a488035 | 230 | $groups = array( |
08e37a2a | 231 | 'A' => $this->customGroupCreate(array( |
92915c55 TO |
232 | 'title' => 'Test_Group A', |
233 | 'name' => 'test_group_a', | |
234 | 'extends' => array('Individual'), | |
235 | 'style' => 'Inline', | |
236 | 'is_multiple' => 0, | |
237 | 'is_active' => 1, | |
238 | 'version' => 3, | |
239 | )), | |
08e37a2a | 240 | 'B' => $this->customGroupCreate(array( |
92915c55 TO |
241 | 'title' => 'Test_Group B', |
242 | 'name' => 'test_group_b', | |
243 | 'extends' => array('Individual'), | |
244 | 'style' => 'Inline', | |
245 | 'is_multiple' => 0, | |
246 | 'is_active' => 1, | |
247 | 'version' => 3, | |
248 | )), | |
6a488035 | 249 | ); |
08e37a2a | 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, | |
258 | )); | |
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, | |
265 | )); | |
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, | |
272 | )); | |
273 | ||
6a488035 | 274 | $fields = array( |
08e37a2a | 275 | 'countryA' => $countryA['values'][$countryA['id']], |
276 | 'countryB' => $countryB['values'][$countryB['id']], | |
277 | 'countryC' => $countryC['values'][$countryC['id']], | |
6a488035 TO |
278 | ); |
279 | $contacts = array( | |
f2040bc6 | 280 | 'alice' => $this->individualCreate(array( |
92915c55 TO |
281 | 'first_name' => 'Alice', |
282 | 'last_name' => 'Albertson', | |
602b60e0 SL |
283 | 'custom_' . $fields['countryA']['id'] => $countriesByName['Andorra'], |
284 | 'custom_' . $fields['countryB']['id'] => $countriesByName['Barbados'], | |
92915c55 | 285 | )), |
f2040bc6 | 286 | 'bob' => $this->individualCreate(array( |
92915c55 TO |
287 | 'first_name' => 'Bob', |
288 | 'last_name' => 'Roberts', | |
602b60e0 SL |
289 | 'custom_' . $fields['countryA']['id'] => $countriesByName['Austria'], |
290 | 'custom_' . $fields['countryB']['id'] => $countriesByName['Bermuda'], | |
291 | 'custom_' . $fields['countryC']['id'] => $countriesByName['Chad'], | |
92915c55 | 292 | )), |
f2040bc6 | 293 | 'carol' => $this->individualCreate(array( |
92915c55 TO |
294 | 'first_name' => 'Carol', |
295 | 'last_name' => 'Carolson', | |
602b60e0 | 296 | 'custom_' . $fields['countryC']['id'] => $countriesByName['Cambodia'], |
92915c55 | 297 | )), |
6a488035 TO |
298 | ); |
299 | ||
300 | // Move! | |
08e37a2a | 301 | CRM_Core_BAO_CustomField::moveField($fields['countryB']['id'], $groupB['id']); |
6a488035 TO |
302 | |
303 | // Group[A] no longer has fields[countryB] | |
304 | $errorScope = CRM_Core_TemporaryErrorScope::useException(); | |
305 | try { | |
08e37a2a | 306 | $this->assertDBQuery(1, "SELECT {$fields['countryB']['column_name']} FROM " . $groupA['table_name']); |
6a488035 TO |
307 | $this->fail('Expected exception when querying column on wrong table'); |
308 | } | |
92915c55 TO |
309 | catch (PEAR_Exception$e) { |
310 | } | |
6a488035 TO |
311 | $errorScope = NULL; |
312 | ||
313 | // Alice: Group[B] has fields[countryB], but fields[countryC] did not exist before | |
314 | $this->assertDBQuery(1, | |
08e37a2a | 315 | "SELECT count(*) FROM {$groupB['table_name']} |
b2042573 | 316 | WHERE entity_id = %1 |
08e37a2a | 317 | AND {$fields['countryB']['column_name']} = %3 |
318 | AND {$fields['countryC']['column_name']} is null", | |
6a488035 TO |
319 | array( |
320 | 1 => array($contacts['alice'], 'Integer'), | |
602b60e0 | 321 | 3 => array($countriesByName['Barbados'], 'Integer'), |
6a488035 TO |
322 | ) |
323 | ); | |
324 | ||
325 | // Bob: Group[B] has merged fields[countryB] and fields[countryC] on the same record | |
326 | $this->assertDBQuery(1, | |
08e37a2a | 327 | "SELECT count(*) FROM {$groupB['table_name']} |
b2042573 | 328 | WHERE entity_id = %1 |
08e37a2a | 329 | AND {$fields['countryB']['column_name']} = %3 |
330 | AND {$fields['countryC']['column_name']} = %4", | |
6a488035 TO |
331 | array( |
332 | 1 => array($contacts['bob'], 'Integer'), | |
602b60e0 SL |
333 | 3 => array($countriesByName['Bermuda'], 'Integer'), |
334 | 4 => array($countriesByName['Chad'], 'Integer'), | |
6a488035 TO |
335 | ) |
336 | ); | |
337 | ||
338 | // Carol: Group[B] still has fields[countryC] but did not get fields[countryB] | |
339 | $this->assertDBQuery(1, | |
08e37a2a | 340 | "SELECT count(*) FROM {$groupB['table_name']} |
b2042573 | 341 | WHERE entity_id = %1 |
08e37a2a | 342 | AND {$fields['countryB']['column_name']} is null |
343 | AND {$fields['countryC']['column_name']} = %4", | |
6a488035 TO |
344 | array( |
345 | 1 => array($contacts['carol'], 'Integer'), | |
602b60e0 | 346 | 4 => array($countriesByName['Cambodia'], 'Integer'), |
6a488035 TO |
347 | ) |
348 | ); | |
349 | ||
08e37a2a | 350 | $this->customGroupDelete($groups['A']['id']); |
351 | $this->customGroupDelete($groupB['id']); | |
6a488035 | 352 | } |
96025800 | 353 | |
c6b559da | 354 | /** |
355 | * Test get custom field id function. | |
356 | */ | |
357 | public function testGetCustomFieldID() { | |
358 | $this->createCustomField(); | |
359 | $fieldID = CRM_Core_BAO_CustomField::getCustomFieldID('testFld'); | |
360 | $this->assertEquals($this->customFieldID, $fieldID); | |
361 | ||
362 | $fieldID = CRM_Core_BAO_CustomField::getCustomFieldID('testFld', 'new custom group'); | |
363 | $this->assertEquals($this->customFieldID, $fieldID); | |
30b6e002 PF |
364 | |
365 | $fieldID = CRM_Core_BAO_CustomField::getCustomFieldID('testFld', 'new custom group', TRUE); | |
366 | $this->assertEquals('custom_' . $this->customFieldID, $fieldID); | |
367 | ||
368 | // create field with same name in a different group | |
369 | $this->createCustomField('other custom group'); | |
370 | $otherFieldID = CRM_Core_BAO_CustomField::getCustomFieldID('testFld', 'other custom group'); | |
371 | // make sure it does not return the field ID of the first field | |
372 | $this->assertNotEquals($fieldID, $otherFieldID); | |
c6b559da | 373 | } |
374 | ||
375 | /** | |
30b6e002 PF |
376 | * Create a custom field |
377 | * | |
378 | * @param string $groupTitle | |
379 | * | |
c6b559da | 380 | * @return array |
381 | */ | |
30b6e002 PF |
382 | protected function createCustomField($groupTitle = 'new custom group') { |
383 | $customGroup = $this->customGroupCreate([ | |
384 | 'extends' => 'Individual', | |
385 | 'title' => $groupTitle, | |
386 | ]); | |
c6b559da | 387 | $fields = array( |
388 | 'label' => 'testFld', | |
389 | 'data_type' => 'String', | |
390 | 'html_type' => 'Text', | |
391 | 'custom_group_id' => $customGroup['id'], | |
392 | ); | |
393 | $field = CRM_Core_BAO_CustomField::create($fields); | |
394 | $this->customFieldID = $field->id; | |
395 | return $customGroup; | |
396 | } | |
397 | ||
6a488035 | 398 | } |