Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
0eea664b | 2 | |
6a488035 TO |
3 | require_once 'CiviTest/Contact.php'; |
4 | require_once 'CiviTest/Custom.php'; | |
aba1cd8b EM |
5 | |
6 | /** | |
7 | * Class CRM_Core_BAO_CustomFieldTest | |
acb109b7 | 8 | * @group headless |
aba1cd8b | 9 | */ |
6a488035 | 10 | class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { |
6a488035 | 11 | |
00be9182 | 12 | public function setUp() { |
6a488035 TO |
13 | parent::setUp(); |
14 | } | |
15 | ||
00be9182 | 16 | public function testCreateCustomfield() { |
6a488035 TO |
17 | $customGroup = Custom::createGroup(array(), 'Individual'); |
18 | $fields = array( | |
19 | 'label' => 'testFld', | |
20 | 'data_type' => 'String', | |
21 | 'html_type' => 'Text', | |
22 | 'custom_group_id' => $customGroup->id, | |
23 | ); | |
24 | $customField = CRM_Core_BAO_CustomField::create($fields); | |
25 | $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id', 'custom_group_id', | |
26 | 'Database check for created CustomField.' | |
27 | ); | |
28 | $fields = array( | |
29 | 'id' => $customFieldID, | |
30 | 'label' => 'editTestFld', | |
31 | 'is_active' => 1, | |
32 | 'data_type' => 'String', | |
33 | 'html_type' => 'Text', | |
34 | 'custom_group_id' => $customGroup->id, | |
35 | ); | |
36 | ||
37 | $customField = CRM_Core_BAO_CustomField::create($fields); | |
38 | $this->assertDBNotNull('CRM_Core_DAO_CustomField', 1, 'id', 'is_active', 'Database check for edited CustomField.'); | |
39 | $this->assertDBNotNull('CRM_Core_DAO_CustomField', $fields['label'], 'id', 'label', 'Database check for edited CustomField.'); | |
40 | ||
8b3b9a2e ARW |
41 | $dbFieldName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'name', 'id', 'Database check for edited CustomField.'); |
42 | $dbColumnName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'column_name', 'id', 'Database check for edited CustomField.'); | |
43 | $this->assertEquals(strtolower("{$dbFieldName}_{$customFieldID}"), $dbColumnName, | |
92915c55 | 44 | "Column name ends in ID"); |
8b3b9a2e ARW |
45 | |
46 | Custom::deleteGroup($customGroup); | |
47 | } | |
b2042573 | 48 | |
00be9182 | 49 | public function testCreateCustomfieldColumnName() { |
8b3b9a2e ARW |
50 | $customGroup = Custom::createGroup(array(), 'Individual'); |
51 | $fields = array( | |
52 | 'label' => 'testFld 2', | |
53 | 'column_name' => 'special_colname', | |
54 | 'data_type' => 'String', | |
55 | 'html_type' => 'Text', | |
56 | 'custom_group_id' => $customGroup->id, | |
57 | ); | |
58 | $customField = CRM_Core_BAO_CustomField::create($fields); | |
59 | $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id', 'custom_group_id', | |
60 | 'Database check for created CustomField.' | |
61 | ); | |
62 | $dbColumnName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'column_name', 'id', 'Database check for edited CustomField.'); | |
63 | $this->assertEquals($fields['column_name'], $dbColumnName, | |
92915c55 | 64 | "Column name set as specified"); |
8b3b9a2e | 65 | |
6a488035 TO |
66 | Custom::deleteGroup($customGroup); |
67 | } | |
68 | ||
00be9182 | 69 | public function testCreateCustomfieldName() { |
43ff3196 TO |
70 | $customGroup = Custom::createGroup(array(), 'Individual'); |
71 | $fields = array( | |
72 | 'label' => 'testFld 2', | |
73 | 'name' => 'special_fldlname', | |
74 | 'data_type' => 'String', | |
75 | 'html_type' => 'Text', | |
76 | 'custom_group_id' => $customGroup->id, | |
77 | ); | |
78 | $customField = CRM_Core_BAO_CustomField::create($fields); | |
79 | $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id', 'custom_group_id', | |
80 | 'Database check for created CustomField.' | |
81 | ); | |
82 | $dbFieldName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'name', 'id', 'Database check for edited CustomField.'); | |
83 | $this->assertEquals($fields['name'], $dbFieldName, | |
84 | "Column name set as specified"); | |
85 | ||
86 | Custom::deleteGroup($customGroup); | |
87 | } | |
88 | ||
00be9182 | 89 | public function testGetFields() { |
6a488035 TO |
90 | $customGroup = Custom::createGroup(array(), 'Individual'); |
91 | $fields = array( | |
92 | 'label' => 'testFld1', | |
93 | 'data_type' => 'String', | |
94 | 'html_type' => 'Text', | |
95 | 'is_active' => 1, | |
96 | 'custom_group_id' => $customGroup->id, | |
97 | ); | |
98 | $customField1 = CRM_Core_BAO_CustomField::create($fields); | |
99 | $customFieldID1 = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id', 'custom_group_id', | |
100 | 'Database check for created CustomField.' | |
101 | ); | |
102 | $fields = array( | |
103 | 'label' => 'testFld2', | |
104 | 'data_type' => 'String', | |
105 | 'html_type' => 'Text', | |
106 | 'is_active' => 1, | |
107 | 'custom_group_id' => $customGroup->id, | |
108 | ); | |
109 | $customField2 = CRM_Core_BAO_CustomField::create($fields); | |
110 | $customFieldID2 = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id', 'custom_group_id', | |
111 | 'Database check for created CustomField.' | |
112 | ); | |
113 | $getCustomFields = array(); | |
114 | $getCustomFields = CRM_Core_BAO_CustomField::getFields('Individual', TRUE, TRUE); | |
6a488035 TO |
115 | //$this->assertEquals( 'testFld1', $getCustomFields[$customFieldID1][0], 'Confirm First Custom field label' ); |
116 | //$this->assertEquals( 'testFld2', $getCustomFields[$customFieldID2][0], 'Confirm Second Custom field label' ); | |
117 | ||
6a488035 TO |
118 | Custom::deleteGroup($customGroup); |
119 | } | |
120 | ||
00be9182 | 121 | public function testGetDisplayedValues() { |
6a488035 | 122 | $customGroup = Custom::createGroup(array(), 'Individual'); |
d5c86914 CW |
123 | $fieldsToCreate = array( |
124 | array( | |
125 | 'data_type' => 'Country', | |
126 | 'html_type' => 'Select Country', | |
127 | 'tests' => array( | |
128 | 'UNITED STATES' => 1228, | |
129 | '' => NULL, | |
130 | ), | |
131 | ), | |
132 | array( | |
133 | 'data_type' => 'StateProvince', | |
134 | 'html_type' => 'Multi-Select State/Province', | |
135 | 'tests' => array( | |
136 | '' => 0, | |
137 | 'Alabama' => 1000, | |
138 | 'Alabama, Alaska' => array(1000, 1001), | |
139 | ), | |
140 | ), | |
141 | array( | |
142 | 'data_type' => 'String', | |
143 | 'html_type' => 'Radio', | |
144 | 'option_values' => array( | |
145 | 'key' => 'KeyLabel', | |
146 | ), | |
147 | 'tests' => array( | |
148 | 'KeyLabel' => 'key', | |
149 | ), | |
150 | ), | |
151 | array( | |
152 | 'data_type' => 'String', | |
153 | 'html_type' => 'CheckBox', | |
154 | 'option_values' => array( | |
155 | 'key1' => 'Label1', | |
156 | 'key2' => 'Label2', | |
157 | 'key3' => 'Label3', | |
158 | 'key4' => 'Label4', | |
159 | ), | |
160 | 'tests' => array( | |
161 | 'Label1' => array('key1'), | |
162 | 'Label2' => 'key2', | |
163 | 'Label2, Label3' => array('key2', 'key3'), | |
164 | 'Label3, Label4' => CRM_Utils_Array::implodePadded(array('key3', 'key4')), | |
165 | 'Label1, Label4' => array('key1' => 1, 'key4' => 1), | |
166 | ), | |
167 | ), | |
168 | array( | |
169 | 'data_type' => 'Date', | |
170 | 'html_type' => 'Select Date', | |
171 | 'date_format' => 'd M yy', | |
172 | 'time_format' => 1, | |
173 | 'tests' => array( | |
174 | '1 Jun 1999 1:30PM' => '1999-06-01 13:30', | |
175 | '' => '', | |
176 | ), | |
177 | ), | |
6a488035 | 178 | ); |
d5c86914 CW |
179 | foreach ($fieldsToCreate as $num => $field) { |
180 | $params = $field + array( | |
181 | 'label' => 'test field ' . $num, | |
182 | 'custom_group_id' => $customGroup->id, | |
183 | ); | |
184 | unset($params['tests']); | |
185 | $createdField = $this->callAPISuccess('customField', 'create', $params); | |
186 | foreach ($field['tests'] as $expected => $input) { | |
187 | $this->assertEquals($expected, CRM_Core_BAO_CustomField::displayValue($input, $createdField['id'])); | |
188 | } | |
189 | } | |
6a488035 TO |
190 | |
191 | Custom::deleteGroup($customGroup); | |
192 | } | |
193 | ||
00be9182 | 194 | public function testDeleteCustomfield() { |
6a488035 TO |
195 | $customGroup = Custom::createGroup(array(), 'Individual'); |
196 | $fields = array( | |
e39729e1 | 197 | 'groupId' => $customGroup->id, |
8b3b9a2e | 198 | 'label' => 'Throwaway Field', |
d9136461 | 199 | 'dataType' => 'Memo', |
200 | 'htmlType' => 'TextArea', | |
6a488035 TO |
201 | ); |
202 | ||
203 | $customField = Custom::createField(array(), $fields); | |
204 | $this->assertNotNull($customField); | |
205 | CRM_Core_BAO_CustomField::deleteField($customField); | |
206 | $this->assertDBNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id', | |
207 | 'custom_group_id', 'Database check for deleted Custom Field.' | |
208 | ); | |
209 | Custom::deleteGroup($customGroup); | |
210 | } | |
211 | ||
212 | /** | |
213 | * Move a custom field from $groupA to $groupB. Make sure that data records are | |
214 | * correctly matched and created. | |
215 | */ | |
00be9182 | 216 | public function testMoveField() { |
6a488035 | 217 | $countriesByName = array_flip(CRM_Core_PseudoConstant::country(FALSE, FALSE)); |
86797006 | 218 | $this->assertTrue($countriesByName['ANDORRA'] > 0); |
6a488035 TO |
219 | $groups = array( |
220 | 'A' => Custom::createGroup(array( | |
92915c55 TO |
221 | 'title' => 'Test_Group A', |
222 | 'name' => 'test_group_a', | |
223 | 'extends' => array('Individual'), | |
224 | 'style' => 'Inline', | |
225 | 'is_multiple' => 0, | |
226 | 'is_active' => 1, | |
227 | 'version' => 3, | |
228 | )), | |
6a488035 | 229 | 'B' => Custom::createGroup(array( |
92915c55 TO |
230 | 'title' => 'Test_Group B', |
231 | 'name' => 'test_group_b', | |
232 | 'extends' => array('Individual'), | |
233 | 'style' => 'Inline', | |
234 | 'is_multiple' => 0, | |
235 | 'is_active' => 1, | |
236 | 'version' => 3, | |
237 | )), | |
6a488035 TO |
238 | ); |
239 | $fields = array( | |
6c6e6187 | 240 | 'countryA' => Custom::createField(array(), array( |
92915c55 TO |
241 | 'groupId' => $groups['A']->id, |
242 | 'label' => 'Country A', | |
243 | 'dataType' => 'Country', | |
244 | 'htmlType' => 'Select Country', | |
245 | )), | |
6c6e6187 | 246 | 'countryB' => Custom::createField(array(), array( |
92915c55 TO |
247 | 'groupId' => $groups['A']->id, |
248 | 'label' => 'Country B', | |
249 | 'dataType' => 'Country', | |
250 | 'htmlType' => 'Select Country', | |
251 | )), | |
6c6e6187 | 252 | 'countryC' => Custom::createField(array(), array( |
92915c55 TO |
253 | 'groupId' => $groups['B']->id, |
254 | 'label' => 'Country C', | |
255 | 'dataType' => 'Country', | |
256 | 'htmlType' => 'Select Country', | |
257 | )), | |
6a488035 TO |
258 | ); |
259 | $contacts = array( | |
260 | 'alice' => Contact::createIndividual(array( | |
92915c55 TO |
261 | 'first_name' => 'Alice', |
262 | 'last_name' => 'Albertson', | |
86797006 | 263 | 'custom_' . $fields['countryA']->id => $countriesByName['ANDORRA'], |
264 | 'custom_' . $fields['countryB']->id => $countriesByName['BARBADOS'], | |
92915c55 | 265 | )), |
6a488035 | 266 | 'bob' => Contact::createIndividual(array( |
92915c55 TO |
267 | 'first_name' => 'Bob', |
268 | 'last_name' => 'Roberts', | |
86797006 | 269 | 'custom_' . $fields['countryA']->id => $countriesByName['AUSTRIA'], |
270 | 'custom_' . $fields['countryB']->id => $countriesByName['BERMUDA'], | |
271 | 'custom_' . $fields['countryC']->id => $countriesByName['CHAD'], | |
92915c55 | 272 | )), |
6a488035 | 273 | 'carol' => Contact::createIndividual(array( |
92915c55 TO |
274 | 'first_name' => 'Carol', |
275 | 'last_name' => 'Carolson', | |
86797006 | 276 | 'custom_' . $fields['countryC']->id => $countriesByName['CAMBODIA'], |
92915c55 | 277 | )), |
6a488035 TO |
278 | ); |
279 | ||
280 | // Move! | |
281 | CRM_Core_BAO_CustomField::moveField($fields['countryB']->id, $groups['B']->id); | |
282 | ||
283 | // Group[A] no longer has fields[countryB] | |
284 | $errorScope = CRM_Core_TemporaryErrorScope::useException(); | |
285 | try { | |
286 | $this->assertDBQuery(1, "SELECT {$fields['countryB']->column_name} FROM {$groups['A']->table_name}"); | |
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, | |
b2042573 | 295 | "SELECT count(*) FROM {$groups['B']->table_name} |
296 | WHERE entity_id = %1 | |
6a488035 TO |
297 | AND {$fields['countryB']->column_name} = %3 |
298 | AND {$fields['countryC']->column_name} is null", | |
299 | array( | |
300 | 1 => array($contacts['alice'], 'Integer'), | |
86797006 | 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, | |
b2042573 | 307 | "SELECT count(*) FROM {$groups['B']->table_name} |
308 | WHERE entity_id = %1 | |
6a488035 TO |
309 | AND {$fields['countryB']->column_name} = %3 |
310 | AND {$fields['countryC']->column_name} = %4", | |
311 | array( | |
312 | 1 => array($contacts['bob'], 'Integer'), | |
86797006 | 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, | |
b2042573 | 320 | "SELECT count(*) FROM {$groups['B']->table_name} |
321 | WHERE entity_id = %1 | |
6a488035 TO |
322 | AND {$fields['countryB']->column_name} is null |
323 | AND {$fields['countryC']->column_name} = %4", | |
324 | array( | |
325 | 1 => array($contacts['carol'], 'Integer'), | |
86797006 | 326 | 4 => array($countriesByName['CAMBODIA'], 'Integer'), |
6a488035 TO |
327 | ) |
328 | ); | |
329 | ||
330 | Custom::deleteGroup($groups['A']); | |
331 | Custom::deleteGroup($groups['B']); | |
332 | } | |
96025800 | 333 | |
6a488035 | 334 | } |