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