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 | ||
43 | Custom::deleteGroup($customGroup); | |
44 | } | |
45 | ||
46 | function testGetFields() { | |
47 | $customGroup = Custom::createGroup(array(), 'Individual'); | |
48 | $fields = array( | |
49 | 'label' => 'testFld1', | |
50 | 'data_type' => 'String', | |
51 | 'html_type' => 'Text', | |
52 | 'is_active' => 1, | |
53 | 'custom_group_id' => $customGroup->id, | |
54 | ); | |
55 | $customField1 = CRM_Core_BAO_CustomField::create($fields); | |
56 | $customFieldID1 = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id', 'custom_group_id', | |
57 | 'Database check for created CustomField.' | |
58 | ); | |
59 | $fields = array( | |
60 | 'label' => 'testFld2', | |
61 | 'data_type' => 'String', | |
62 | 'html_type' => 'Text', | |
63 | 'is_active' => 1, | |
64 | 'custom_group_id' => $customGroup->id, | |
65 | ); | |
66 | $customField2 = CRM_Core_BAO_CustomField::create($fields); | |
67 | $customFieldID2 = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id', 'custom_group_id', | |
68 | 'Database check for created CustomField.' | |
69 | ); | |
70 | $getCustomFields = array(); | |
71 | $getCustomFields = CRM_Core_BAO_CustomField::getFields('Individual', TRUE, TRUE); | |
72 | //CRM_Core_Error::debug('fdf',$getCustomFields); | |
73 | //$this->assertEquals( 'testFld1', $getCustomFields[$customFieldID1][0], 'Confirm First Custom field label' ); | |
74 | //$this->assertEquals( 'testFld2', $getCustomFields[$customFieldID2][0], 'Confirm Second Custom field label' ); | |
75 | ||
76 | ||
77 | Custom::deleteGroup($customGroup); | |
78 | } | |
79 | ||
80 | function testGetDisplayedValues() { | |
81 | $customGroup = Custom::createGroup(array(), 'Individual'); | |
82 | $fields = array( | |
83 | 'label' => 'testCountryFld1', | |
84 | 'data_type' => 'Country', | |
85 | 'html_type' => 'Select Country', | |
86 | 'is_active' => 1, | |
87 | 'default_value' => 1228, | |
88 | 'custom_group_id' => $customGroup->id, | |
89 | ); | |
90 | $customField1 = CRM_Core_BAO_CustomField::create($fields); | |
91 | $customFieldID1 = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id', 'custom_group_id', | |
92 | 'Database check for created CustomField.' | |
93 | ); | |
94 | $options = array(); | |
95 | $options[$customFieldID1]['attributes'] = array( | |
96 | 'label' => 'testCountryFld1', | |
97 | 'data_type' => 'Country', | |
98 | 'html_type' => 'Select Country', | |
99 | ); | |
100 | $display = CRM_Core_BAO_CustomField::getDisplayValue($fields['default_value'], $customFieldID1, $options); | |
101 | ||
102 | $this->assertEquals('United States', $display, 'Confirm Country display Name'); | |
103 | ||
104 | Custom::deleteGroup($customGroup); | |
105 | } | |
106 | ||
107 | function testDeleteCustomfield() { | |
108 | $customGroup = Custom::createGroup(array(), 'Individual'); | |
109 | $fields = array( | |
110 | 'groupId' => $customGroup->id, | |
111 | 'dataType' => 'Memo', | |
112 | 'htmlType' => 'TextArea', | |
113 | ); | |
114 | ||
115 | $customField = Custom::createField(array(), $fields); | |
116 | $this->assertNotNull($customField); | |
117 | CRM_Core_BAO_CustomField::deleteField($customField); | |
118 | $this->assertDBNull('CRM_Core_DAO_CustomField', $customGroup->id, 'id', | |
119 | 'custom_group_id', 'Database check for deleted Custom Field.' | |
120 | ); | |
121 | Custom::deleteGroup($customGroup); | |
122 | } | |
123 | ||
124 | /** | |
125 | * Move a custom field from $groupA to $groupB. Make sure that data records are | |
126 | * correctly matched and created. | |
127 | */ | |
128 | function testMoveField() { | |
129 | $countriesByName = array_flip(CRM_Core_PseudoConstant::country(FALSE, FALSE)); | |
130 | $this->assertTrue($countriesByName['Andorra'] > 0); | |
131 | $groups = array( | |
132 | 'A' => Custom::createGroup(array( | |
133 | 'title' => 'Test_Group A', | |
134 | 'name' => 'test_group_a', | |
135 | 'extends' => array('Individual'), | |
136 | 'style' => 'Inline', | |
137 | 'is_multiple' => 0, | |
138 | 'is_active' => 1, | |
139 | 'version' => 3, | |
140 | )), | |
141 | 'B' => Custom::createGroup(array( | |
142 | 'title' => 'Test_Group B', | |
143 | 'name' => 'test_group_b', | |
144 | 'extends' => array('Individual'), | |
145 | 'style' => 'Inline', | |
146 | 'is_multiple' => 0, | |
147 | 'is_active' => 1, | |
148 | 'version' => 3, | |
149 | )), | |
150 | ); | |
151 | $fields = array( | |
152 | 'countryA' => Custom::createField(array( | |
153 | ), array( | |
154 | 'groupId' => $groups['A']->id, | |
155 | 'label' => 'Country A', | |
156 | 'dataType' => 'Country', | |
157 | 'htmlType' => 'Select Country', | |
158 | )), | |
159 | 'countryB' => Custom::createField(array( | |
160 | ), array( | |
161 | 'groupId' => $groups['A']->id, | |
162 | 'label' => 'Country B', | |
163 | 'dataType' => 'Country', | |
164 | 'htmlType' => 'Select Country', | |
165 | )), | |
166 | 'countryC' => Custom::createField(array( | |
167 | ), array( | |
168 | 'groupId' => $groups['B']->id, | |
169 | 'label' => 'Country C', | |
170 | 'dataType' => 'Country', | |
171 | 'htmlType' => 'Select Country', | |
172 | )), | |
173 | ); | |
174 | $contacts = array( | |
175 | 'alice' => Contact::createIndividual(array( | |
176 | 'first_name' => 'Alice', | |
177 | 'last_name' => 'Albertson', | |
178 | 'custom_' . $fields['countryA']->id => $countriesByName['Andorra'], | |
179 | 'custom_' . $fields['countryB']->id => $countriesByName['Barbados'], | |
180 | )), | |
181 | 'bob' => Contact::createIndividual(array( | |
182 | 'first_name' => 'Bob', | |
183 | 'last_name' => 'Roberts', | |
184 | 'custom_' . $fields['countryA']->id => $countriesByName['Austria'], | |
185 | 'custom_' . $fields['countryB']->id => $countriesByName['Bermuda'], | |
186 | 'custom_' . $fields['countryC']->id => $countriesByName['Chad'], | |
187 | )), | |
188 | 'carol' => Contact::createIndividual(array( | |
189 | 'first_name' => 'Carol', | |
190 | 'last_name' => 'Carolson', | |
191 | 'custom_' . $fields['countryC']->id => $countriesByName['Cambodia'], | |
192 | )), | |
193 | ); | |
194 | ||
195 | // Move! | |
196 | CRM_Core_BAO_CustomField::moveField($fields['countryB']->id, $groups['B']->id); | |
197 | ||
198 | // Group[A] no longer has fields[countryB] | |
199 | $errorScope = CRM_Core_TemporaryErrorScope::useException(); | |
200 | try { | |
201 | $this->assertDBQuery(1, "SELECT {$fields['countryB']->column_name} FROM {$groups['A']->table_name}"); | |
202 | $this->fail('Expected exception when querying column on wrong table'); | |
203 | } | |
204 | catch(PEAR_Exception$e) {} | |
205 | $errorScope = NULL; | |
206 | ||
207 | // Alice: Group[B] has fields[countryB], but fields[countryC] did not exist before | |
208 | $this->assertDBQuery(1, | |
209 | "SELECT count(*) FROM {$groups['B']->table_name} | |
210 | WHERE entity_id = %1 | |
211 | AND {$fields['countryB']->column_name} = %3 | |
212 | AND {$fields['countryC']->column_name} is null", | |
213 | array( | |
214 | 1 => array($contacts['alice'], 'Integer'), | |
215 | 3 => array($countriesByName['Barbados'], 'Integer'), | |
216 | ) | |
217 | ); | |
218 | ||
219 | // Bob: Group[B] has merged fields[countryB] and fields[countryC] on the same record | |
220 | $this->assertDBQuery(1, | |
221 | "SELECT count(*) FROM {$groups['B']->table_name} | |
222 | WHERE entity_id = %1 | |
223 | AND {$fields['countryB']->column_name} = %3 | |
224 | AND {$fields['countryC']->column_name} = %4", | |
225 | array( | |
226 | 1 => array($contacts['bob'], 'Integer'), | |
227 | 3 => array($countriesByName['Bermuda'], 'Integer'), | |
228 | 4 => array($countriesByName['Chad'], 'Integer'), | |
229 | ) | |
230 | ); | |
231 | ||
232 | // Carol: Group[B] still has fields[countryC] but did not get fields[countryB] | |
233 | $this->assertDBQuery(1, | |
234 | "SELECT count(*) FROM {$groups['B']->table_name} | |
235 | WHERE entity_id = %1 | |
236 | AND {$fields['countryB']->column_name} is null | |
237 | AND {$fields['countryC']->column_name} = %4", | |
238 | array( | |
239 | 1 => array($contacts['carol'], 'Integer'), | |
240 | 4 => array($countriesByName['Cambodia'], 'Integer'), | |
241 | ) | |
242 | ); | |
243 | ||
244 | Custom::deleteGroup($groups['A']); | |
245 | Custom::deleteGroup($groups['B']); | |
246 | } | |
247 | } | |
248 | ||
249 |