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