Merge remote-tracking branch 'upstream/4.4' into 4.4-4.5-2014-09-01-22-48-29
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / CustomFieldTest.php
1 <?php
2 require_once 'CiviTest/CiviUnitTestCase.php';
3 require_once 'CiviTest/Contact.php';
4 require_once 'CiviTest/Custom.php';
5
6 /**
7 * Class CRM_Core_BAO_CustomFieldTest
8 */
9 class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
10 /**
11 * @return array
12 */
13 function get_info() {
14 return array(
15 'name' => 'Custom Field BAOs',
16 'description' => 'Test all Core_BAO_CustomField methods.',
17 'group' => 'CiviCRM BAO Tests',
18 );
19 }
20
21 function setUp() {
22 parent::setUp();
23 }
24
25 function testCreateCustomfield() {
26 $customGroup = Custom::createGroup(array(), 'Individual');
27 $fields = array(
28 'label' => 'testFld',
29 'data_type' => 'String',
30 'html_type' => 'Text',
31 'custom_group_id' => $customGroup->id,
32 );
33 $customField = CRM_Core_BAO_CustomField::create($fields);
34 $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id', 'custom_group_id',
35 'Database check for created CustomField.'
36 );
37 $fields = array(
38 'id' => $customFieldID,
39 'label' => 'editTestFld',
40 'is_active' => 1,
41 'data_type' => 'String',
42 'html_type' => 'Text',
43 'custom_group_id' => $customGroup->id,
44 );
45
46 $customField = CRM_Core_BAO_CustomField::create($fields);
47 $this->assertDBNotNull('CRM_Core_DAO_CustomField', 1, 'id', 'is_active', 'Database check for edited CustomField.');
48 $this->assertDBNotNull('CRM_Core_DAO_CustomField', $fields['label'], 'id', 'label', 'Database check for edited CustomField.');
49
50 $dbFieldName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'name', 'id', 'Database check for edited CustomField.');
51 $dbColumnName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'column_name', 'id', 'Database check for edited CustomField.');
52 $this->assertEquals(strtolower("{$dbFieldName}_{$customFieldID}"), $dbColumnName,
53 "Column name ends in ID");
54
55 Custom::deleteGroup($customGroup);
56 }
57
58 function testCreateCustomfieldColumnName() {
59 $customGroup = Custom::createGroup(array(), 'Individual');
60 $fields = array(
61 'label' => 'testFld 2',
62 'column_name' => 'special_colname',
63 'data_type' => 'String',
64 'html_type' => 'Text',
65 'custom_group_id' => $customGroup->id,
66 );
67 $customField = CRM_Core_BAO_CustomField::create($fields);
68 $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id', 'custom_group_id',
69 'Database check for created CustomField.'
70 );
71 $dbColumnName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'column_name', 'id', 'Database check for edited CustomField.');
72 $this->assertEquals($fields['column_name'], $dbColumnName,
73 "Column name set as specified");
74
75 Custom::deleteGroup($customGroup);
76 }
77
78 function testCreateCustomfieldName() {
79 $customGroup = Custom::createGroup(array(), 'Individual');
80 $fields = array(
81 'label' => 'testFld 2',
82 'name' => 'special_fldlname',
83 'data_type' => 'String',
84 'html_type' => 'Text',
85 'custom_group_id' => $customGroup->id,
86 );
87 $customField = CRM_Core_BAO_CustomField::create($fields);
88 $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id', 'custom_group_id',
89 'Database check for created CustomField.'
90 );
91 $dbFieldName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'name', 'id', 'Database check for edited CustomField.');
92 $this->assertEquals($fields['name'], $dbFieldName,
93 "Column name set as specified");
94
95 Custom::deleteGroup($customGroup);
96 }
97
98
99 function testGetFields() {
100 $customGroup = Custom::createGroup(array(), 'Individual');
101 $fields = array(
102 'label' => 'testFld1',
103 'data_type' => 'String',
104 'html_type' => 'Text',
105 'is_active' => 1,
106 'custom_group_id' => $customGroup->id,
107 );
108 $customField1 = CRM_Core_BAO_CustomField::create($fields);
109 $customFieldID1 = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id', 'custom_group_id',
110 'Database check for created CustomField.'
111 );
112 $fields = array(
113 'label' => 'testFld2',
114 'data_type' => 'String',
115 'html_type' => 'Text',
116 'is_active' => 1,
117 'custom_group_id' => $customGroup->id,
118 );
119 $customField2 = CRM_Core_BAO_CustomField::create($fields);
120 $customFieldID2 = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id', 'custom_group_id',
121 'Database check for created CustomField.'
122 );
123 $getCustomFields = array();
124 $getCustomFields = CRM_Core_BAO_CustomField::getFields('Individual', TRUE, TRUE);
125 //CRM_Core_Error::debug('fdf',$getCustomFields);
126 //$this->assertEquals( 'testFld1', $getCustomFields[$customFieldID1][0], 'Confirm First Custom field label' );
127 //$this->assertEquals( 'testFld2', $getCustomFields[$customFieldID2][0], 'Confirm Second Custom field label' );
128
129
130 Custom::deleteGroup($customGroup);
131 }
132
133 function testGetDisplayedValues() {
134 $customGroup = Custom::createGroup(array(), 'Individual');
135 $fields = array(
136 'label' => 'testCountryFld1',
137 'data_type' => 'Country',
138 'html_type' => 'Select Country',
139 'is_active' => 1,
140 'default_value' => 1228,
141 'custom_group_id' => $customGroup->id,
142 );
143 $customField1 = CRM_Core_BAO_CustomField::create($fields);
144 $customFieldID1 = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id', 'custom_group_id',
145 'Database check for created CustomField.'
146 );
147 $options = array();
148 $options[$customFieldID1]['attributes'] = array(
149 'label' => 'testCountryFld1',
150 'data_type' => 'Country',
151 'html_type' => 'Select Country',
152 );
153 $display = CRM_Core_BAO_CustomField::getDisplayValue($fields['default_value'], $customFieldID1, $options);
154
155 $this->assertEquals('United States', $display, 'Confirm Country display Name');
156
157 Custom::deleteGroup($customGroup);
158 }
159
160 function testDeleteCustomfield() {
161 $customGroup = Custom::createGroup(array(), 'Individual');
162 $fields = array(
163 'groupId' => $customGroup->id,
164 'label' => 'Throwaway Field',
165 'dataType' => 'Memo',
166 'htmlType' => 'TextArea',
167 );
168
169 $customField = Custom::createField(array(), $fields);
170 $this->assertNotNull($customField);
171 CRM_Core_BAO_CustomField::deleteField($customField);
172 $this->assertDBNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id',
173 'custom_group_id', 'Database check for deleted Custom Field.'
174 );
175 Custom::deleteGroup($customGroup);
176 }
177
178 /**
179 * Move a custom field from $groupA to $groupB. Make sure that data records are
180 * correctly matched and created.
181 */
182 function testMoveField() {
183 $countriesByName = array_flip(CRM_Core_PseudoConstant::country(FALSE, FALSE));
184 $this->assertTrue($countriesByName['Andorra'] > 0);
185 $groups = array(
186 'A' => Custom::createGroup(array(
187 'title' => 'Test_Group A',
188 'name' => 'test_group_a',
189 'extends' => array('Individual'),
190 'style' => 'Inline',
191 'is_multiple' => 0,
192 'is_active' => 1,
193 'version' => 3,
194 )),
195 'B' => Custom::createGroup(array(
196 'title' => 'Test_Group B',
197 'name' => 'test_group_b',
198 'extends' => array('Individual'),
199 'style' => 'Inline',
200 'is_multiple' => 0,
201 'is_active' => 1,
202 'version' => 3,
203 )),
204 );
205 $fields = array(
206 'countryA' => Custom::createField(array(
207 ), array(
208 'groupId' => $groups['A']->id,
209 'label' => 'Country A',
210 'dataType' => 'Country',
211 'htmlType' => 'Select Country',
212 )),
213 'countryB' => Custom::createField(array(
214 ), array(
215 'groupId' => $groups['A']->id,
216 'label' => 'Country B',
217 'dataType' => 'Country',
218 'htmlType' => 'Select Country',
219 )),
220 'countryC' => Custom::createField(array(
221 ), array(
222 'groupId' => $groups['B']->id,
223 'label' => 'Country C',
224 'dataType' => 'Country',
225 'htmlType' => 'Select Country',
226 )),
227 );
228 $contacts = array(
229 'alice' => Contact::createIndividual(array(
230 'first_name' => 'Alice',
231 'last_name' => 'Albertson',
232 'custom_' . $fields['countryA']->id => $countriesByName['Andorra'],
233 'custom_' . $fields['countryB']->id => $countriesByName['Barbados'],
234 )),
235 'bob' => Contact::createIndividual(array(
236 'first_name' => 'Bob',
237 'last_name' => 'Roberts',
238 'custom_' . $fields['countryA']->id => $countriesByName['Austria'],
239 'custom_' . $fields['countryB']->id => $countriesByName['Bermuda'],
240 'custom_' . $fields['countryC']->id => $countriesByName['Chad'],
241 )),
242 'carol' => Contact::createIndividual(array(
243 'first_name' => 'Carol',
244 'last_name' => 'Carolson',
245 'custom_' . $fields['countryC']->id => $countriesByName['Cambodia'],
246 )),
247 );
248
249 // Move!
250 CRM_Core_BAO_CustomField::moveField($fields['countryB']->id, $groups['B']->id);
251
252 // Group[A] no longer has fields[countryB]
253 $errorScope = CRM_Core_TemporaryErrorScope::useException();
254 try {
255 $this->assertDBQuery(1, "SELECT {$fields['countryB']->column_name} FROM {$groups['A']->table_name}");
256 $this->fail('Expected exception when querying column on wrong table');
257 }
258 catch(PEAR_Exception$e) {}
259 $errorScope = NULL;
260
261 // Alice: Group[B] has fields[countryB], but fields[countryC] did not exist before
262 $this->assertDBQuery(1,
263 "SELECT count(*) FROM {$groups['B']->table_name}
264 WHERE entity_id = %1
265 AND {$fields['countryB']->column_name} = %3
266 AND {$fields['countryC']->column_name} is null",
267 array(
268 1 => array($contacts['alice'], 'Integer'),
269 3 => array($countriesByName['Barbados'], 'Integer'),
270 )
271 );
272
273 // Bob: Group[B] has merged fields[countryB] and fields[countryC] on the same record
274 $this->assertDBQuery(1,
275 "SELECT count(*) FROM {$groups['B']->table_name}
276 WHERE entity_id = %1
277 AND {$fields['countryB']->column_name} = %3
278 AND {$fields['countryC']->column_name} = %4",
279 array(
280 1 => array($contacts['bob'], 'Integer'),
281 3 => array($countriesByName['Bermuda'], 'Integer'),
282 4 => array($countriesByName['Chad'], 'Integer'),
283 )
284 );
285
286 // Carol: Group[B] still has fields[countryC] but did not get fields[countryB]
287 $this->assertDBQuery(1,
288 "SELECT count(*) FROM {$groups['B']->table_name}
289 WHERE entity_id = %1
290 AND {$fields['countryB']->column_name} is null
291 AND {$fields['countryC']->column_name} = %4",
292 array(
293 1 => array($contacts['carol'], 'Integer'),
294 4 => array($countriesByName['Cambodia'], 'Integer'),
295 )
296 );
297
298 Custom::deleteGroup($groups['A']);
299 Custom::deleteGroup($groups['B']);
300 }
301 }
302
303