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