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