Commit | Line | Data |
---|---|---|
887a4028 A |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
29 | ||
30 | /** | |
31 | * Tests for linking to resource files | |
32 | */ | |
33 | class CRM_Core_PseudoConstantTest extends CiviUnitTestCase { | |
34 | function get_info() { | |
35 | return array( | |
36 | 'name' => 'PseudoConstant', | |
37 | 'description' => 'Tests for pseudoconstant option values', | |
38 | 'group' => 'Core', | |
39 | ); | |
40 | } | |
41 | ||
42 | function setUp() { | |
43 | parent::setUp(); | |
44 | } | |
45 | ||
cd43c5e3 AS |
46 | /** |
47 | * Assure CRM_Core_PseudoConstant::get() is working properly for a range of | |
48 | * DAO fields having a <pseudoconstant> tag in the XML schema. | |
49 | */ | |
398f49ab | 50 | function testOptionValues() { |
bd44e0df AS |
51 | |
52 | $custom_group_name = md5(microtime()); | |
cd43c5e3 | 53 | $api_params = array( |
bd44e0df AS |
54 | 'version' => 3, |
55 | 'title' => $custom_group_name, | |
56 | 'extends' => 'Individual', | |
57 | 'is_active' => TRUE, | |
cd43c5e3 | 58 | ); |
bd44e0df | 59 | $result = civicrm_api('customGroup', 'create', $api_params); |
cd43c5e3 AS |
60 | |
61 | /** | |
091fe2a8 | 62 | * daoName/field combinations to test |
cd43c5e3 AS |
63 | * Format: array[DAO Name] = $properties, where properties is an array whose |
64 | * named members can be: | |
091fe2a8 CW |
65 | * - fieldName: the SQL column name within the DAO table. |
66 | * - sample: Any one value which is expected in the list of option values. | |
67 | * - exclude: Any one value which should not be in the list. | |
68 | * - max: integer (default = 10) maximum number of option values expected. | |
69 | */ | |
398f49ab | 70 | $fields = array( |
bd44e0df AS |
71 | 'CRM_Event_DAO_Participant' => array( |
72 | array( | |
73 | 'fieldName' => 'fee_currency', | |
74 | 'sample' => '$', | |
75 | 'max' => 200, | |
76 | ), | |
77 | ), | |
78 | 'CRM_Core_DAO_UFField' => array( | |
79 | array( | |
80 | 'fieldName' => 'uf_group_id', | |
81 | 'sample' => 'Name and Address', | |
82 | 'max' => 15, | |
83 | ), | |
84 | ), | |
85 | 'CRM_Core_DAO_UFJoin' => array( | |
86 | array( | |
87 | 'fieldName' => 'uf_group_id', | |
88 | 'sample' => 'Name and Address', | |
89 | 'max' => 15, | |
90 | ), | |
91 | ), | |
92 | 'CRM_Contribute_DAO_ContributionSoft' => array( | |
93 | array( | |
94 | 'fieldName' => 'currency', | |
95 | 'sample' => '$', | |
96 | 'max' => 200, | |
97 | ), | |
98 | ), | |
99 | 'CRM_Contribute_DAO_Contribution' => array( | |
100 | array( | |
101 | 'fieldName' => 'currency', | |
102 | 'sample' => '$', | |
103 | 'max' => 200, | |
104 | ), | |
105 | ), | |
106 | 'CRM_Contribute_DAO_Product' => array( | |
107 | array( | |
108 | 'fieldName' => 'currency', | |
109 | 'sample' => '$', | |
110 | 'max' => 200, | |
111 | ), | |
112 | ), | |
113 | 'CRM_Contribute_DAO_ContributionPage' => array( | |
114 | array( | |
115 | 'fieldName' => 'currency', | |
116 | 'sample' => '$', | |
117 | 'max' => 200, | |
118 | ), | |
119 | ), | |
120 | 'CRM_Contribute_DAO_ContributionRecur' => array( | |
121 | array( | |
122 | 'fieldName' => 'currency', | |
123 | 'sample' => '$', | |
124 | 'max' => 200, | |
125 | ), | |
126 | ), | |
127 | 'CRM_Event_DAO_Event' => array( | |
128 | array( | |
129 | 'fieldName' => 'currency', | |
130 | 'sample' => '$', | |
131 | 'max' => 200, | |
132 | ), | |
133 | ), | |
134 | 'CRM_Financial_DAO_FinancialItem' => array( | |
135 | array( | |
136 | 'fieldName' => 'currency', | |
137 | 'sample' => '$', | |
138 | 'max' => 200, | |
139 | ), | |
140 | ), | |
141 | 'CRM_Financial_DAO_OfficialReceipt' => array( | |
142 | array( | |
143 | 'fieldName' => 'currency', | |
144 | 'sample' => '$', | |
145 | 'max' => 200, | |
146 | ), | |
147 | ), | |
148 | 'CRM_Financial_DAO_FinancialTrxn' => array( | |
149 | array( | |
150 | 'fieldName' => 'currency', | |
151 | 'sample' => '$', | |
152 | 'max' => 200, | |
153 | ), | |
154 | ), | |
155 | 'CRM_Grant_DAO_Grant' => array( | |
156 | array( | |
157 | 'fieldName' => 'currency', | |
158 | 'sample' => '$', | |
159 | 'max' => 200, | |
160 | ), | |
161 | ), | |
162 | 'CRM_Pledge_DAO_PledgePayment' => array( | |
163 | array( | |
164 | 'fieldName' => 'currency', | |
165 | 'sample' => '$', | |
166 | 'max' => 200, | |
167 | ), | |
168 | ), | |
169 | 'CRM_Pledge_DAO_Pledge' => array( | |
170 | array( | |
171 | 'fieldName' => 'currency', | |
172 | 'sample' => '$', | |
173 | 'max' => 200, | |
174 | ), | |
175 | ), | |
176 | 'CRM_PCP_DAO_PCP' => array( | |
177 | array( | |
178 | 'fieldName' => 'currency', | |
179 | 'sample' => '$', | |
180 | 'max' => 200, | |
181 | ), | |
182 | ), | |
cd43c5e3 AS |
183 | 'CRM_Core_DAO_CustomField' => array( |
184 | array( | |
185 | 'fieldName' => 'custom_group_id', | |
186 | 'sample' => $custom_group_name, | |
187 | ), | |
188 | ), | |
189 | 'CRM_Core_DAO_EntityTag' => array( | |
190 | array( | |
191 | 'fieldName' => 'tag_id', | |
192 | 'sample' => 'Government Entity', | |
193 | ), | |
194 | ), | |
8f785c9e AS |
195 | 'CRM_Core_DAO_OptionValue' => array( |
196 | array( | |
197 | 'fieldName' => 'component_id', | |
198 | 'sample' => 'CiviContribute', | |
199 | ), | |
200 | ), | |
cbf48754 AS |
201 | 'CRM_Project_DAO_Task' => array( |
202 | array( | |
203 | 'fieldName' => 'priority_id', | |
204 | 'sample' => 'Urgent', | |
205 | ), | |
206 | ), | |
207 | 'CRM_Activity_DAO_Activity' => array( | |
208 | array( | |
209 | 'fieldName' => 'priority_id', | |
210 | 'sample' => 'Urgent', | |
211 | ), | |
212 | ), | |
213 | 'CRM_Core_DAO_MailSettings' => array( | |
214 | array( | |
215 | 'fieldName' => 'protocol', | |
216 | 'sample' => 'Localdir', | |
217 | ), | |
218 | ), | |
219 | 'CRM_Core_DAO_Mapping' => array( | |
220 | array( | |
221 | 'fieldName' => 'mapping_type_id', | |
222 | 'sample' => 'Search Builder', | |
223 | 'max' => 15, | |
224 | ), | |
225 | ), | |
226 | 'CRM_Pledge_DAO_Pledge' => array( | |
227 | array( | |
228 | 'fieldName' => 'honor_type_id', | |
229 | 'sample' => 'In Honor of', | |
230 | ), | |
231 | ), | |
232 | 'CRM_Contribute_DAO_Contribution' => array( | |
233 | array( | |
234 | 'fieldName' => 'honor_type_id', | |
235 | 'sample' => 'In Honor of', | |
236 | ), | |
237 | ), | |
e7e657f0 AS |
238 | 'CRM_Core_DAO_Phone' => array( |
239 | array( | |
240 | 'fieldName' => 'phone_type_id', | |
241 | 'sample' => 'Phone', | |
242 | ), | |
243 | array( | |
244 | 'fieldName' => 'location_type_id', | |
245 | 'sample' => 'Home', | |
246 | ), | |
247 | ), | |
248 | 'CRM_Core_DAO_Email' => array( | |
249 | array( | |
250 | 'fieldName' => 'location_type_id', | |
251 | 'sample' => 'Home', | |
252 | ), | |
253 | ), | |
254 | 'CRM_Core_DAO_Address' => array( | |
255 | array( | |
256 | 'fieldName' => 'location_type_id', | |
257 | 'sample' => 'Home', | |
258 | ), | |
259 | ), | |
cbf48754 AS |
260 | 'CRM_Core_DAO_Website' => array( |
261 | array( | |
262 | 'fieldName' => 'website_type_id', | |
263 | 'sample' => 'Facebook', | |
264 | ), | |
265 | ), | |
266 | 'CRM_Core_DAO_MappingField' => array( | |
267 | array( | |
268 | 'fieldName' => 'website_type_id', | |
269 | 'sample' => 'Facebook', | |
270 | ), | |
e7e657f0 AS |
271 | array( |
272 | 'fieldName' => 'im_provider_id', | |
273 | 'sample' => 'Yahoo', | |
274 | ), | |
cbf48754 | 275 | ), |
398f49ab AS |
276 | 'CRM_Contact_DAO_Contact' => array( |
277 | array( | |
278 | 'fieldName' => 'prefix_id', | |
279 | 'sample' => 'Mr.', | |
280 | ), | |
281 | array( | |
282 | 'fieldName' => 'suffix_id', | |
283 | 'sample' => 'Sr.', | |
284 | ), | |
285 | array( | |
286 | 'fieldName' => 'gender_id', | |
287 | 'sample' => 'Male', | |
288 | ), | |
398f49ab | 289 | array( |
e7e657f0 AS |
290 | 'fieldName' => 'preferred_communication_method', |
291 | 'sample' => 'Postal Mail', | |
398f49ab | 292 | ), |
091fe2a8 CW |
293 | array( |
294 | 'fieldName' => 'contact_type', | |
295 | 'sample' => 'Individual', | |
296 | 'exclude' => 'Team', | |
297 | ), | |
298 | array( | |
299 | 'fieldName' => 'contact_sub_type', | |
300 | 'sample' => 'Team', | |
301 | 'exclude' => 'Individual', | |
302 | ), | |
398f49ab | 303 | ), |
e7e657f0 | 304 | 'CRM_Batch_DAO_Batch' => array( |
398f49ab | 305 | array( |
e7e657f0 AS |
306 | 'fieldName' => 'type_id', |
307 | 'sample' => 'Membership', | |
398f49ab | 308 | ), |
398f49ab | 309 | array( |
e7e657f0 AS |
310 | 'fieldName' => 'status_id', |
311 | 'sample' => 'Reopened', | |
398f49ab | 312 | ), |
cbf48754 | 313 | array( |
e7e657f0 AS |
314 | 'fieldName' => 'mode_id', |
315 | 'sample' => 'Automatic Batch', | |
cbf48754 AS |
316 | ), |
317 | ), | |
e7e657f0 | 318 | 'CRM_Core_DAO_IM' => array( |
cbf48754 | 319 | array( |
e7e657f0 AS |
320 | 'fieldName' => 'provider_id', |
321 | 'sample' => 'Yahoo', | |
cbf48754 AS |
322 | ), |
323 | ), | |
887a4028 | 324 | ); |
398f49ab AS |
325 | |
326 | foreach ($fields as $daoName => $daoFields) { | |
327 | foreach ($daoFields as $field) { | |
a42ef93c | 328 | $message = "DAO name: '{$daoName}', field: '{$field['fieldName']}'"; |
398f49ab | 329 | |
398f49ab | 330 | $optionValues = CRM_Core_PseudoConstant::get($daoName, $field['fieldName']); |
091fe2a8 CW |
331 | $this->assertNotEmpty($optionValues, $message); |
332 | ||
333 | // Ensure sample value is contained in the returned optionValues. | |
398f49ab AS |
334 | $this->assertContains($field['sample'], $optionValues, $message); |
335 | ||
091fe2a8 CW |
336 | // Exclude test |
337 | if (!empty($field['exclude'])) { | |
338 | $this->assertNotContains($field['exclude'], $optionValues, $message); | |
339 | } | |
340 | ||
398f49ab AS |
341 | // Ensure count of optionValues is not extraordinarily high. |
342 | $max = CRM_Utils_Array::value('max', $field, 10); | |
343 | $this->assertLessThanOrEqual($max, count($optionValues), $message); | |
344 | } | |
345 | } | |
887a4028 | 346 | } |
091fe2a8 CW |
347 | |
348 | function testContactTypes() { | |
349 | $byName = array( | |
350 | 'Individual' => 'Individual', | |
351 | 'Household' => 'Household', | |
352 | 'Organization' => 'Organization', | |
353 | ); | |
354 | $byId = array( | |
355 | 1 => 'Individual', | |
356 | 2 => 'Household', | |
357 | 3 => 'Organization', | |
358 | ); | |
a1ef51e2 | 359 | // By default this should return an array keyed by name |
091fe2a8 CW |
360 | $result = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'contact_type'); |
361 | $this->assertEquals($byName, $result); | |
362 | // But we can also fetch by ID | |
363 | $result = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name')); | |
364 | $this->assertEquals($byId, $result); | |
365 | // Make sure flip param works | |
366 | $result = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name', 'flip' => TRUE)); | |
367 | $this->assertEquals(array_flip($byId), $result); | |
368 | } | |
887a4028 | 369 | } |