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