phpcs - Fix error, "CONST keyword must be lowercase; expected const but found CONST"
[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';
aba1cd8b
EM
5
6/**
7 * Class CRM_Core_BAO_CustomFieldTest
8 */
6a488035 9class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase {
6a488035
TO
10
11 function setUp() {
12 parent::setUp();
13 }
14
15 function testCreateCustomfield() {
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,
43 "Column name ends in ID");
44
45 Custom::deleteGroup($customGroup);
46 }
b2042573 47
8b3b9a2e
ARW
48 function testCreateCustomfieldColumnName() {
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,
63 "Column name set as specified");
64
6a488035
TO
65 Custom::deleteGroup($customGroup);
66 }
67
43ff3196
TO
68 function testCreateCustomfieldName() {
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
88
6a488035
TO
89 function testGetFields() {
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
118
119 Custom::deleteGroup($customGroup);
120 }
121
122 function testGetDisplayedValues() {
123 $customGroup = Custom::createGroup(array(), 'Individual');
124 $fields = array(
125 'label' => 'testCountryFld1',
126 'data_type' => 'Country',
127 'html_type' => 'Select Country',
128 'is_active' => 1,
129 'default_value' => 1228,
130 'custom_group_id' => $customGroup->id,
131 );
132 $customField1 = CRM_Core_BAO_CustomField::create($fields);
133 $customFieldID1 = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id', 'custom_group_id',
134 'Database check for created CustomField.'
135 );
136 $options = array();
137 $options[$customFieldID1]['attributes'] = array(
138 'label' => 'testCountryFld1',
139 'data_type' => 'Country',
140 'html_type' => 'Select Country',
141 );
142 $display = CRM_Core_BAO_CustomField::getDisplayValue($fields['default_value'], $customFieldID1, $options);
143
144 $this->assertEquals('United States', $display, 'Confirm Country display Name');
145
146 Custom::deleteGroup($customGroup);
147 }
148
149 function testDeleteCustomfield() {
150 $customGroup = Custom::createGroup(array(), 'Individual');
151 $fields = array(
e39729e1 152 'groupId' => $customGroup->id,
8b3b9a2e 153 'label' => 'Throwaway Field',
d9136461 154 'dataType' => 'Memo',
155 'htmlType' => 'TextArea',
6a488035
TO
156 );
157
158 $customField = Custom::createField(array(), $fields);
159 $this->assertNotNull($customField);
160 CRM_Core_BAO_CustomField::deleteField($customField);
161 $this->assertDBNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id',
162 'custom_group_id', 'Database check for deleted Custom Field.'
163 );
164 Custom::deleteGroup($customGroup);
165 }
166
167 /**
168 * Move a custom field from $groupA to $groupB. Make sure that data records are
169 * correctly matched and created.
170 */
171 function testMoveField() {
172 $countriesByName = array_flip(CRM_Core_PseudoConstant::country(FALSE, FALSE));
173 $this->assertTrue($countriesByName['Andorra'] > 0);
174 $groups = array(
175 'A' => Custom::createGroup(array(
176 'title' => 'Test_Group A',
177 'name' => 'test_group_a',
178 'extends' => array('Individual'),
179 'style' => 'Inline',
180 'is_multiple' => 0,
181 'is_active' => 1,
182 'version' => 3,
183 )),
184 'B' => Custom::createGroup(array(
185 'title' => 'Test_Group B',
186 'name' => 'test_group_b',
187 'extends' => array('Individual'),
188 'style' => 'Inline',
189 'is_multiple' => 0,
190 'is_active' => 1,
191 'version' => 3,
192 )),
193 );
194 $fields = array(
195 'countryA' => Custom::createField(array(
196 ), array(
197 'groupId' => $groups['A']->id,
198 'label' => 'Country A',
199 'dataType' => 'Country',
200 'htmlType' => 'Select Country',
201 )),
202 'countryB' => Custom::createField(array(
203 ), array(
204 'groupId' => $groups['A']->id,
205 'label' => 'Country B',
206 'dataType' => 'Country',
207 'htmlType' => 'Select Country',
208 )),
209 'countryC' => Custom::createField(array(
210 ), array(
211 'groupId' => $groups['B']->id,
212 'label' => 'Country C',
213 'dataType' => 'Country',
214 'htmlType' => 'Select Country',
215 )),
216 );
217 $contacts = array(
218 'alice' => Contact::createIndividual(array(
219 'first_name' => 'Alice',
220 'last_name' => 'Albertson',
221 'custom_' . $fields['countryA']->id => $countriesByName['Andorra'],
222 'custom_' . $fields['countryB']->id => $countriesByName['Barbados'],
223 )),
224 'bob' => Contact::createIndividual(array(
225 'first_name' => 'Bob',
226 'last_name' => 'Roberts',
227 'custom_' . $fields['countryA']->id => $countriesByName['Austria'],
228 'custom_' . $fields['countryB']->id => $countriesByName['Bermuda'],
229 'custom_' . $fields['countryC']->id => $countriesByName['Chad'],
230 )),
231 'carol' => Contact::createIndividual(array(
232 'first_name' => 'Carol',
233 'last_name' => 'Carolson',
234 'custom_' . $fields['countryC']->id => $countriesByName['Cambodia'],
235 )),
236 );
237
238 // Move!
239 CRM_Core_BAO_CustomField::moveField($fields['countryB']->id, $groups['B']->id);
240
241 // Group[A] no longer has fields[countryB]
242 $errorScope = CRM_Core_TemporaryErrorScope::useException();
243 try {
244 $this->assertDBQuery(1, "SELECT {$fields['countryB']->column_name} FROM {$groups['A']->table_name}");
245 $this->fail('Expected exception when querying column on wrong table');
246 }
247 catch(PEAR_Exception$e) {}
248 $errorScope = NULL;
249
250 // Alice: Group[B] has fields[countryB], but fields[countryC] did not exist before
251 $this->assertDBQuery(1,
b2042573 252 "SELECT count(*) FROM {$groups['B']->table_name}
253 WHERE entity_id = %1
6a488035
TO
254 AND {$fields['countryB']->column_name} = %3
255 AND {$fields['countryC']->column_name} is null",
256 array(
257 1 => array($contacts['alice'], 'Integer'),
258 3 => array($countriesByName['Barbados'], 'Integer'),
259 )
260 );
261
262 // Bob: Group[B] has merged fields[countryB] and fields[countryC] on the same record
263 $this->assertDBQuery(1,
b2042573 264 "SELECT count(*) FROM {$groups['B']->table_name}
265 WHERE entity_id = %1
6a488035
TO
266 AND {$fields['countryB']->column_name} = %3
267 AND {$fields['countryC']->column_name} = %4",
268 array(
269 1 => array($contacts['bob'], 'Integer'),
270 3 => array($countriesByName['Bermuda'], 'Integer'),
271 4 => array($countriesByName['Chad'], 'Integer'),
272 )
273 );
274
275 // Carol: Group[B] still has fields[countryC] but did not get fields[countryB]
276 $this->assertDBQuery(1,
b2042573 277 "SELECT count(*) FROM {$groups['B']->table_name}
278 WHERE entity_id = %1
6a488035
TO
279 AND {$fields['countryB']->column_name} is null
280 AND {$fields['countryC']->column_name} = %4",
281 array(
282 1 => array($contacts['carol'], 'Integer'),
283 4 => array($countriesByName['Cambodia'], 'Integer'),
284 )
285 );
286
287 Custom::deleteGroup($groups['A']);
288 Custom::deleteGroup($groups['B']);
289 }
290}
291
292