Refactor language pseudoconstant CRM-12464
[civicrm-core.git] / tests / phpunit / CRM / Core / PseudoConstantTest.php
CommitLineData
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
28require_once 'CiviTest/CiviUnitTestCase.php';
29
30/**
2158332a 31 * Tests for pseudoconstant retrieval
887a4028
A
32 */
33class 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 51
29494eef 52 // Create a custom field group for testing.
bd44e0df 53 $custom_group_name = md5(microtime());
cd43c5e3 54 $api_params = array(
bd44e0df
AS
55 'version' => 3,
56 'title' => $custom_group_name,
57 'extends' => 'Individual',
58 'is_active' => TRUE,
cd43c5e3 59 );
bd44e0df 60 $result = civicrm_api('customGroup', 'create', $api_params);
cd43c5e3 61
29494eef
AS
62 // Create a Group for testing.
63 $group_name = md5(microtime());
64 $api_params = array(
65 'version' => 3,
66 'title' => $group_name,
67 'is_active' => TRUE,
68 );
69 $result = civicrm_api('group', 'create', $api_params);
70
cd43c5e3 71 /**
091fe2a8 72 * daoName/field combinations to test
cd43c5e3
AS
73 * Format: array[DAO Name] = $properties, where properties is an array whose
74 * named members can be:
091fe2a8
CW
75 * - fieldName: the SQL column name within the DAO table.
76 * - sample: Any one value which is expected in the list of option values.
77 * - exclude: Any one value which should not be in the list.
78 * - max: integer (default = 10) maximum number of option values expected.
79 */
398f49ab 80 $fields = array(
29494eef
AS
81 'CRM_Contact_DAO_GroupContact' => array(
82 array(
83 'fieldName' => 'group_id',
84 'sample' => $group_name,
85 ),
86 ),
87 'CRM_Contact_DAO_GroupContactCache' => array(
88 array(
89 'fieldName' => 'group_id',
90 'sample' => $group_name,
91 ),
92 ),
93 'CRM_Contact_DAO_GroupOrganization' => array(
94 array(
95 'fieldName' => 'group_id',
96 'sample' => $group_name,
97 ),
98 ),
99 'CRM_Contact_DAO_SubscriptionHistory' => array(
100 array(
101 'fieldName' => 'group_id',
102 'sample' => $group_name,
103 ),
104 ),
105 'CRM_Core_DAO_ActionSchedule' => array(
106 array(
107 'fieldName' => 'group_id',
108 'sample' => $group_name,
109 ),
110 ),
111 'CRM_Mailing_Event_DAO_Subscribe' => array(
112 array(
113 'fieldName' => 'group_id',
114 'sample' => $group_name,
115 ),
116 ),
4080e473
AS
117 'CRM_Activity_DAO_Activity' => array(
118 array(
119 'fieldName' => 'activity_type_id',
120 'sample' => 'Text Message (SMS)',
121 'max' => 50,
122 ),
123 array(
124 'fieldName' => 'status_id',
125 'sample' => 'Scheduled',
126 ),
127 ),
128 'CRM_Campaign_DAO_Survey' => array(
129 array(
130 'fieldName' => 'activity_type_id',
131 'sample' => 'Text Message (SMS)',
132 'max' => 50,
133 ),
134 ),
135 'CRM_Event_DAO_ParticipantStatusType' => array(
136 array(
137 'fieldName' => 'visibility_id',
138 'sample' => 'Public',
139 ),
140 ),
141 'CRM_Member_DAO_MembershipType' => array(
142 array(
143 'fieldName' => 'visibility',
144 'sample' => 'Public',
145 ),
146 ),
147 'CRM_Price_DAO_Field' => array(
148 array(
149 'fieldName' => 'visibility_id',
150 'sample' => 'Public',
151 ),
152 ),
7611ae71
AS
153 'CRM_Financial_DAO_EntityFinancialAccount' => array(
154 array(
155 'fieldName' => 'financial_account_id',
156 'sample' => 'Member Dues',
157 'max' => 15,
158 ),
159 array(
160 'fieldName' => 'account_relationship',
161 'sample' => 'Income Account is',
162 ),
163 ),
164 'CRM_Financial_DAO_FinancialItem' => array(
165 array(
166 'fieldName' => 'status_id',
167 'sample' => 'Partially paid',
168 ),
169 array(
170 'fieldName' => 'financial_account_id',
171 'sample' => 'Accounts Receivable',
172 'max' => 15,
173 ),
174 ),
175 'CRM_Financial_DAO_FinancialTrxn' => array(
176 array(
177 'fieldName' => 'from_financial_account_id',
178 'sample' => 'Accounts Receivable',
179 'max' => 15,
180 ),
181 array(
182 'fieldName' => 'to_financial_account_id',
183 'sample' => 'Accounts Receivable',
184 'max' => 15,
185 ),
186 ),
187 'CRM_Financial_DAO_FinancialAccount' => array(
188 array(
189 'fieldName' => 'financial_account_type_id',
190 'sample' => 'Cost of Sales',
191 ),
192 ),
bd44e0df
AS
193 'CRM_Event_DAO_Participant' => array(
194 array(
195 'fieldName' => 'fee_currency',
196 'sample' => '$',
197 'max' => 200,
198 ),
199 ),
200 'CRM_Core_DAO_UFField' => array(
201 array(
202 'fieldName' => 'uf_group_id',
203 'sample' => 'Name and Address',
204 'max' => 15,
205 ),
206 ),
207 'CRM_Core_DAO_UFJoin' => array(
208 array(
209 'fieldName' => 'uf_group_id',
210 'sample' => 'Name and Address',
211 'max' => 15,
212 ),
213 ),
214 'CRM_Contribute_DAO_ContributionSoft' => array(
215 array(
216 'fieldName' => 'currency',
217 'sample' => '$',
218 'max' => 200,
219 ),
220 ),
221 'CRM_Contribute_DAO_Contribution' => array(
222 array(
223 'fieldName' => 'currency',
224 'sample' => '$',
225 'max' => 200,
226 ),
227 ),
228 'CRM_Contribute_DAO_Product' => array(
229 array(
230 'fieldName' => 'currency',
231 'sample' => '$',
232 'max' => 200,
233 ),
234 ),
235 'CRM_Contribute_DAO_ContributionPage' => array(
236 array(
237 'fieldName' => 'currency',
238 'sample' => '$',
239 'max' => 200,
240 ),
241 ),
242 'CRM_Contribute_DAO_ContributionRecur' => array(
243 array(
244 'fieldName' => 'currency',
245 'sample' => '$',
246 'max' => 200,
247 ),
248 ),
249 'CRM_Event_DAO_Event' => array(
250 array(
251 'fieldName' => 'currency',
252 'sample' => '$',
253 'max' => 200,
254 ),
255 ),
256 'CRM_Financial_DAO_FinancialItem' => array(
257 array(
258 'fieldName' => 'currency',
259 'sample' => '$',
260 'max' => 200,
261 ),
262 ),
263 'CRM_Financial_DAO_OfficialReceipt' => array(
264 array(
265 'fieldName' => 'currency',
266 'sample' => '$',
267 'max' => 200,
268 ),
269 ),
270 'CRM_Financial_DAO_FinancialTrxn' => array(
271 array(
272 'fieldName' => 'currency',
273 'sample' => '$',
274 'max' => 200,
275 ),
276 ),
277 'CRM_Grant_DAO_Grant' => array(
278 array(
279 'fieldName' => 'currency',
280 'sample' => '$',
281 'max' => 200,
282 ),
283 ),
284 'CRM_Pledge_DAO_PledgePayment' => array(
285 array(
286 'fieldName' => 'currency',
287 'sample' => '$',
288 'max' => 200,
289 ),
290 ),
291 'CRM_Pledge_DAO_Pledge' => array(
292 array(
293 'fieldName' => 'currency',
294 'sample' => '$',
295 'max' => 200,
296 ),
297 ),
298 'CRM_PCP_DAO_PCP' => array(
299 array(
300 'fieldName' => 'currency',
301 'sample' => '$',
302 'max' => 200,
303 ),
304 ),
cd43c5e3
AS
305 'CRM_Core_DAO_CustomField' => array(
306 array(
307 'fieldName' => 'custom_group_id',
308 'sample' => $custom_group_name,
309 ),
310 ),
311 'CRM_Core_DAO_EntityTag' => array(
312 array(
313 'fieldName' => 'tag_id',
314 'sample' => 'Government Entity',
315 ),
316 ),
8f785c9e
AS
317 'CRM_Core_DAO_OptionValue' => array(
318 array(
319 'fieldName' => 'component_id',
320 'sample' => 'CiviContribute',
321 ),
322 ),
cbf48754
AS
323 'CRM_Project_DAO_Task' => array(
324 array(
325 'fieldName' => 'priority_id',
326 'sample' => 'Urgent',
327 ),
328 ),
329 'CRM_Activity_DAO_Activity' => array(
330 array(
331 'fieldName' => 'priority_id',
332 'sample' => 'Urgent',
333 ),
334 ),
335 'CRM_Core_DAO_MailSettings' => array(
336 array(
337 'fieldName' => 'protocol',
338 'sample' => 'Localdir',
339 ),
340 ),
341 'CRM_Core_DAO_Mapping' => array(
342 array(
343 'fieldName' => 'mapping_type_id',
344 'sample' => 'Search Builder',
345 'max' => 15,
346 ),
347 ),
348 'CRM_Pledge_DAO_Pledge' => array(
349 array(
350 'fieldName' => 'honor_type_id',
351 'sample' => 'In Honor of',
352 ),
353 ),
354 'CRM_Contribute_DAO_Contribution' => array(
355 array(
356 'fieldName' => 'honor_type_id',
357 'sample' => 'In Honor of',
358 ),
359 ),
e7e657f0
AS
360 'CRM_Core_DAO_Phone' => array(
361 array(
362 'fieldName' => 'phone_type_id',
363 'sample' => 'Phone',
364 ),
365 array(
366 'fieldName' => 'location_type_id',
367 'sample' => 'Home',
368 ),
369 ),
370 'CRM_Core_DAO_Email' => array(
371 array(
372 'fieldName' => 'location_type_id',
373 'sample' => 'Home',
374 ),
375 ),
376 'CRM_Core_DAO_Address' => array(
377 array(
378 'fieldName' => 'location_type_id',
379 'sample' => 'Home',
380 ),
381 ),
cbf48754
AS
382 'CRM_Core_DAO_Website' => array(
383 array(
384 'fieldName' => 'website_type_id',
385 'sample' => 'Facebook',
386 ),
387 ),
388 'CRM_Core_DAO_MappingField' => array(
389 array(
390 'fieldName' => 'website_type_id',
391 'sample' => 'Facebook',
392 ),
e7e657f0
AS
393 array(
394 'fieldName' => 'im_provider_id',
395 'sample' => 'Yahoo',
396 ),
cbf48754 397 ),
398f49ab
AS
398 'CRM_Contact_DAO_Contact' => array(
399 array(
400 'fieldName' => 'prefix_id',
401 'sample' => 'Mr.',
402 ),
403 array(
404 'fieldName' => 'suffix_id',
405 'sample' => 'Sr.',
406 ),
407 array(
408 'fieldName' => 'gender_id',
409 'sample' => 'Male',
410 ),
398f49ab 411 array(
e7e657f0
AS
412 'fieldName' => 'preferred_communication_method',
413 'sample' => 'Postal Mail',
398f49ab 414 ),
091fe2a8
CW
415 array(
416 'fieldName' => 'contact_type',
417 'sample' => 'Individual',
418 'exclude' => 'Team',
419 ),
420 array(
421 'fieldName' => 'contact_sub_type',
422 'sample' => 'Team',
423 'exclude' => 'Individual',
424 ),
c0c9cd82
CW
425 array(
426 'fieldName' => 'preferred_language',
427 'sample' => array('en_US' => 'English (United States)'),
428 'max' => 250,
429 ),
398f49ab 430 ),
e7e657f0 431 'CRM_Batch_DAO_Batch' => array(
398f49ab 432 array(
e7e657f0
AS
433 'fieldName' => 'type_id',
434 'sample' => 'Membership',
398f49ab 435 ),
398f49ab 436 array(
e7e657f0
AS
437 'fieldName' => 'status_id',
438 'sample' => 'Reopened',
398f49ab 439 ),
cbf48754 440 array(
e7e657f0
AS
441 'fieldName' => 'mode_id',
442 'sample' => 'Automatic Batch',
cbf48754
AS
443 ),
444 ),
e7e657f0 445 'CRM_Core_DAO_IM' => array(
cbf48754 446 array(
e7e657f0
AS
447 'fieldName' => 'provider_id',
448 'sample' => 'Yahoo',
cbf48754
AS
449 ),
450 ),
2158332a
CW
451 'CRM_Event_DAO_Participant' => array(
452 array(
453 'fieldName' => 'status_id',
454 'sample' => 'Registered',
455 ),
456 array(
457 'fieldName' => 'role_id',
458 'sample' => 'Speaker',
459 ),
460 ),
461 'CRM_Event_DAO_Event' => array(
462 array(
463 'fieldName' => 'event_type_id',
464 'sample' => 'Fundraiser',
465 ),
466 ),
467 'CRM_PCP_DAO_PCP' => array(
468 array(
469 'fieldName' => 'status_id',
470 'sample' => 'Approved',
471 ),
472 ),
f899561e
CW
473 'CRM_Member_DAO_Membership' => array(
474 array(
475 'fieldName' => 'status_id',
476 'sample' => 'New',
477 ),
6ce01b02
CW
478 ),
479 'CRM_Mailing_DAO_Mailing' => array(
f899561e 480 array(
6ce01b02
CW
481 'fieldName' => 'approval_status_id',
482 'sample' => 'Approved',
f899561e
CW
483 ),
484 ),
fb1fd730
CW
485 'CRM_Grant_DAO_Grant' => array(
486 array(
487 'fieldName' => 'status_id',
488 'sample' => 'Approved',
489 ),
490 array(
491 'fieldName' => 'grant_type_id',
492 'sample' => 'Emergency',
493 ),
494 ),
887a4028 495 );
398f49ab
AS
496
497 foreach ($fields as $daoName => $daoFields) {
498 foreach ($daoFields as $field) {
a42ef93c 499 $message = "DAO name: '{$daoName}', field: '{$field['fieldName']}'";
398f49ab 500
398f49ab 501 $optionValues = CRM_Core_PseudoConstant::get($daoName, $field['fieldName']);
091fe2a8
CW
502 $this->assertNotEmpty($optionValues, $message);
503
504 // Ensure sample value is contained in the returned optionValues.
c0c9cd82
CW
505 if (!is_array($field['sample'])) {
506 $this->assertContains($field['sample'], $optionValues, $message);
507 }
508 // If sample is an array, we check keys and values
509 else {
510 foreach ($field['sample'] as $key => $value) {
511 $this->assertArrayHasKey($key, $optionValues, $message);
512 $this->assertEquals(CRM_Utils_Array::value($key, $optionValues), $value, $message);
513 }
514 }
398f49ab 515
6ce01b02 516 // Ensure exclude value is not contained in the optionValues
091fe2a8
CW
517 if (!empty($field['exclude'])) {
518 $this->assertNotContains($field['exclude'], $optionValues, $message);
519 }
520
398f49ab
AS
521 // Ensure count of optionValues is not extraordinarily high.
522 $max = CRM_Utils_Array::value('max', $field, 10);
523 $this->assertLessThanOrEqual($max, count($optionValues), $message);
524 }
525 }
887a4028 526 }
091fe2a8
CW
527
528 function testContactTypes() {
529 $byName = array(
530 'Individual' => 'Individual',
531 'Household' => 'Household',
532 'Organization' => 'Organization',
533 );
534 $byId = array(
535 1 => 'Individual',
536 2 => 'Household',
537 3 => 'Organization',
538 );
a1ef51e2 539 // By default this should return an array keyed by name
c0c9cd82 540 $result = CRM_Contact_DAO_Contact::buildOptions('contact_type');
091fe2a8
CW
541 $this->assertEquals($byName, $result);
542 // But we can also fetch by ID
543 $result = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name'));
544 $this->assertEquals($byId, $result);
545 // Make sure flip param works
546 $result = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name', 'flip' => TRUE));
547 $this->assertEquals(array_flip($byId), $result);
548 }
887a4028 549}