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