Ian province abbreviation patch - issue 724
[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 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
88
00be9182 89 public function testGetFields() {
6a488035
TO
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
6a488035
TO
118 Custom::deleteGroup($customGroup);
119 }
120
00be9182 121 public function testGetDisplayedValues() {
6a488035
TO
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
c39cffbd 143 $this->assertEquals('UNITED STATES', $display, 'Confirm Country display Name');
6a488035
TO
144
145 Custom::deleteGroup($customGroup);
146 }
147
00be9182 148 public function testDeleteCustomfield() {
6a488035
TO
149 $customGroup = Custom::createGroup(array(), 'Individual');
150 $fields = array(
e39729e1 151 'groupId' => $customGroup->id,
8b3b9a2e 152 'label' => 'Throwaway Field',
d9136461 153 'dataType' => 'Memo',
154 'htmlType' => 'TextArea',
6a488035
TO
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 */
00be9182 170 public function testMoveField() {
6a488035 171 $countriesByName = array_flip(CRM_Core_PseudoConstant::country(FALSE, FALSE));
86797006 172 $this->assertTrue($countriesByName['ANDORRA'] > 0);
6a488035
TO
173 $groups = array(
174 'A' => Custom::createGroup(array(
92915c55
TO
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 )),
6a488035 183 'B' => Custom::createGroup(array(
92915c55
TO
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 )),
6a488035
TO
192 );
193 $fields = array(
6c6e6187 194 'countryA' => Custom::createField(array(), array(
92915c55
TO
195 'groupId' => $groups['A']->id,
196 'label' => 'Country A',
197 'dataType' => 'Country',
198 'htmlType' => 'Select Country',
199 )),
6c6e6187 200 'countryB' => Custom::createField(array(), array(
92915c55
TO
201 'groupId' => $groups['A']->id,
202 'label' => 'Country B',
203 'dataType' => 'Country',
204 'htmlType' => 'Select Country',
205 )),
6c6e6187 206 'countryC' => Custom::createField(array(), array(
92915c55
TO
207 'groupId' => $groups['B']->id,
208 'label' => 'Country C',
209 'dataType' => 'Country',
210 'htmlType' => 'Select Country',
211 )),
6a488035
TO
212 );
213 $contacts = array(
214 'alice' => Contact::createIndividual(array(
92915c55
TO
215 'first_name' => 'Alice',
216 'last_name' => 'Albertson',
86797006 217 'custom_' . $fields['countryA']->id => $countriesByName['ANDORRA'],
218 'custom_' . $fields['countryB']->id => $countriesByName['BARBADOS'],
92915c55 219 )),
6a488035 220 'bob' => Contact::createIndividual(array(
92915c55
TO
221 'first_name' => 'Bob',
222 'last_name' => 'Roberts',
86797006 223 'custom_' . $fields['countryA']->id => $countriesByName['AUSTRIA'],
224 'custom_' . $fields['countryB']->id => $countriesByName['BERMUDA'],
225 'custom_' . $fields['countryC']->id => $countriesByName['CHAD'],
92915c55 226 )),
6a488035 227 'carol' => Contact::createIndividual(array(
92915c55
TO
228 'first_name' => 'Carol',
229 'last_name' => 'Carolson',
86797006 230 'custom_' . $fields['countryC']->id => $countriesByName['CAMBODIA'],
92915c55 231 )),
6a488035
TO
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 }
92915c55
TO
243 catch (PEAR_Exception$e) {
244 }
6a488035
TO
245 $errorScope = NULL;
246
247 // Alice: Group[B] has fields[countryB], but fields[countryC] did not exist before
248 $this->assertDBQuery(1,
b2042573 249 "SELECT count(*) FROM {$groups['B']->table_name}
250 WHERE entity_id = %1
6a488035
TO
251 AND {$fields['countryB']->column_name} = %3
252 AND {$fields['countryC']->column_name} is null",
253 array(
254 1 => array($contacts['alice'], 'Integer'),
86797006 255 3 => array($countriesByName['BARBADOS'], 'Integer'),
6a488035
TO
256 )
257 );
258
259 // Bob: Group[B] has merged fields[countryB] and fields[countryC] on the same record
260 $this->assertDBQuery(1,
b2042573 261 "SELECT count(*) FROM {$groups['B']->table_name}
262 WHERE entity_id = %1
6a488035
TO
263 AND {$fields['countryB']->column_name} = %3
264 AND {$fields['countryC']->column_name} = %4",
265 array(
266 1 => array($contacts['bob'], 'Integer'),
86797006 267 3 => array($countriesByName['BERMUDA'], 'Integer'),
268 4 => array($countriesByName['CHAD'], 'Integer'),
6a488035
TO
269 )
270 );
271
272 // Carol: Group[B] still has fields[countryC] but did not get fields[countryB]
273 $this->assertDBQuery(1,
b2042573 274 "SELECT count(*) FROM {$groups['B']->table_name}
275 WHERE entity_id = %1
6a488035
TO
276 AND {$fields['countryB']->column_name} is null
277 AND {$fields['countryC']->column_name} = %4",
278 array(
279 1 => array($contacts['carol'], 'Integer'),
86797006 280 4 => array($countriesByName['CAMBODIA'], 'Integer'),
6a488035
TO
281 )
282 );
283
284 Custom::deleteGroup($groups['A']);
285 Custom::deleteGroup($groups['B']);
286 }
96025800 287
6a488035 288}