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