3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
31 * Tests for pseudoconstant retrieval
33 class CRM_Core_PseudoConstantTest
extends CiviUnitTestCase
{
35 public function setUp() {
38 $this->loadAllFixtures();
40 CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCase');
41 CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCampaign');
45 * Assure CRM_Core_PseudoConstant::get() is working properly for a range of
46 * DAO fields having a <pseudoconstant> tag in the XML schema.
48 public function testOptionValues() {
50 // Create a custom field group for testing.
51 $custom_group_name = md5(microtime());
53 'title' => $custom_group_name,
54 'extends' => 'Individual',
57 $result = civicrm_api3('customGroup', 'create', $api_params);
59 // Add a custom field to the above field group.
62 'custom_group_id' => $result['id'],
63 'label' => $custom_group_name,
64 'html_type' => 'Select',
65 'data_type' => 'String',
67 'option_values' => array(array(
74 $result = civicrm_api3('custom_field', 'create', $api_params);
75 $customFieldId = $result['id'];
77 // Create a Contact Group for testing.
78 $group_name = md5(microtime());
80 'title' => $group_name,
83 $result = civicrm_api3('group', 'create', $api_params);
85 // Create a PaymentProcessor for testing.
86 $pp_name = md5(microtime());
89 'payment_processor_type_id' => 10,
91 'user_name' => $pp_name,
92 'class_name' => 'Payment_Dummy',
93 'url_site' => 'https://test.com/',
94 'url_recur' => 'https://test.com/',
97 $result = civicrm_api3('payment_processor', 'create', $api_params);
99 // Create a Campaign for testing.
100 $campaign_name = md5(microtime());
102 'title' => $campaign_name,
106 $result = civicrm_api3('campaign', 'create', $api_params);
108 // Create a membership type for testing.
109 $membership_type = md5(microtime());
111 'name' => $membership_type,
113 'financial_type_id' => 1,
115 'member_of_contact_id' => 1,
116 'duration_unit' => 'day',
117 'duration_interval' => 1,
119 $result = civicrm_api3('membership_type', 'create', $api_params);
121 // Create a contribution page for testing.
122 $contribution_page = md5(microtime());
124 'title' => $contribution_page,
126 'financial_type_id' => 1,
128 $result = civicrm_api3('contribution_page', 'create', $api_params);
131 * daoName/field combinations to test
132 * Format: array[DAO Name] = $properties, where properties is an array whose
133 * named members can be:
134 * - fieldName: the SQL column name within the DAO table.
135 * - sample: Any one value which is expected in the list of option values.
136 * - exclude: Any one value which should not be in the list.
137 * - max: integer (default = 20) maximum number of option values expected.
140 'CRM_ACL_DAO_ACL' => array(
142 'fieldName' => 'operation',
146 'CRM_Contact_DAO_Group' => array(
148 'fieldName' => 'visibility',
149 'sample' => 'Public Pages',
152 'CRM_Contact_DAO_GroupContact' => array(
154 'fieldName' => 'group_id',
155 'sample' => $group_name,
158 'fieldName' => 'status',
162 'CRM_Contact_DAO_GroupContactCache' => array(
164 'fieldName' => 'group_id',
165 'sample' => $group_name,
168 'CRM_Contact_DAO_GroupOrganization' => array(
170 'fieldName' => 'group_id',
171 'sample' => $group_name,
174 'CRM_Contact_DAO_SubscriptionHistory' => array(
176 'fieldName' => 'group_id',
177 'sample' => $group_name,
180 'fieldName' => 'method',
184 'fieldName' => 'status',
188 'CRM_Core_DAO_Cache' => array(
190 'fieldName' => 'component_id',
191 'sample' => 'CiviMail',
194 'CRM_Contact_DAO_ACLContactCache' => array(
196 'fieldName' => 'operation',
200 'CRM_Core_DAO_Setting' => array(
202 'fieldName' => 'component_id',
203 'sample' => 'CiviMail',
206 'CRM_Core_DAO_ActionSchedule' => array(
208 'fieldName' => 'group_id',
209 'sample' => $group_name,
212 'fieldName' => 'start_action_unit',
216 'fieldName' => 'repetition_frequency_unit',
220 'fieldName' => 'end_frequency_unit',
224 'fieldName' => 'mode',
228 'CRM_Dedupe_DAO_RuleGroup' => array(
230 'fieldName' => 'contact_type',
231 'sample' => 'Individual',
234 'fieldName' => 'used',
235 'sample' => 'Unsupervised',
238 'CRM_Mailing_Event_DAO_Subscribe' => array(
240 'fieldName' => 'group_id',
241 'sample' => $group_name,
244 'CRM_Activity_DAO_Activity' => array(
246 'fieldName' => 'activity_type_id',
251 'fieldName' => 'status_id',
252 'sample' => 'Scheduled',
255 'fieldName' => 'priority_id',
256 'sample' => 'Urgent',
259 'fieldName' => 'engagement_level',
263 'fieldName' => 'medium_id',
267 'fieldName' => 'campaign_id',
268 'sample' => $campaign_name,
271 'CRM_Campaign_DAO_Campaign' => array(
273 'fieldName' => 'campaign_type_id',
274 'sample' => 'Constituent Engagement',
278 'fieldName' => 'status_id',
279 'sample' => 'Completed',
283 'CRM_Campaign_DAO_Survey' => array(
285 'fieldName' => 'campaign_id',
286 'sample' => $campaign_name,
289 'fieldName' => 'activity_type_id',
290 'sample' => 'Phone Call',
294 'CRM_Campaign_DAO_CampaignGroup' => array(
296 'fieldName' => 'campaign_id',
297 'sample' => $campaign_name,
300 'fieldName' => 'group_type',
301 'sample' => 'Include',
304 'CRM_Contact_DAO_RelationshipType' => array(
306 'fieldName' => 'contact_type_a',
307 'sample' => 'Individual',
310 'fieldName' => 'contact_type_b',
311 'sample' => 'Organization',
314 'CRM_Event_DAO_ParticipantStatusType' => array(
316 'fieldName' => 'class',
317 'sample' => 'Waiting',
320 'fieldName' => 'visibility_id',
321 'sample' => 'Public',
324 'CRM_Price_DAO_LineItem' => array(
326 'fieldName' => 'financial_type_id',
327 'sample' => 'Donation',
330 'CRM_Price_DAO_PriceField' => array(
332 'fieldName' => 'html_type',
333 'sample' => 'Select',
336 'fieldName' => 'visibility_id',
337 'sample' => 'Public',
340 'CRM_Price_DAO_PriceFieldValue' => array(
342 'fieldName' => 'financial_type_id',
343 'sample' => 'Donation',
346 'CRM_Price_DAO_PriceSet' => array(
348 'fieldName' => 'domain_id',
349 'sample' => 'Default Domain Name',
352 'fieldName' => 'extends',
353 'sample' => 'CiviEvent',
356 'fieldName' => 'financial_type_id',
357 'sample' => 'Donation',
360 'CRM_Financial_DAO_EntityFinancialAccount' => array(
362 'fieldName' => 'financial_account_id',
363 'sample' => 'Member Dues',
366 'fieldName' => 'account_relationship',
367 'sample' => 'Income Account is',
370 'CRM_Financial_DAO_FinancialItem' => array(
372 'fieldName' => 'status_id',
373 'sample' => 'Partially paid',
376 'fieldName' => 'financial_account_id',
377 'sample' => 'Accounts Receivable',
380 'fieldName' => 'currency',
381 'sample' => array('USD' => 'US Dollar'),
385 'CRM_Financial_DAO_FinancialTrxn' => array(
387 'fieldName' => 'from_financial_account_id',
388 'sample' => 'Accounts Receivable',
391 'fieldName' => 'to_financial_account_id',
392 'sample' => 'Accounts Receivable',
395 'fieldName' => 'currency',
396 'sample' => array('USD' => 'US Dollar'),
400 'fieldName' => 'payment_instrument_id',
404 'CRM_Financial_DAO_FinancialAccount' => array(
406 'fieldName' => 'financial_account_type_id',
407 'sample' => 'Cost of Sales',
410 'CRM_Financial_DAO_PaymentProcessor' => array(
412 'fieldName' => 'domain_id',
413 'sample' => 'Default Domain Name',
416 'CRM_Financial_BAO_PaymentProcessorType' => array(
418 'fieldName' => 'billing_mode',
422 'CRM_Core_DAO_UFField' => array(
424 'fieldName' => 'uf_group_id',
425 'sample' => 'Name and Address',
428 'fieldName' => 'visibility',
429 'sample' => 'Public Pages',
432 'CRM_Core_DAO_UFJoin' => array(
434 'fieldName' => 'uf_group_id',
435 'sample' => 'Name and Address',
438 'CRM_Core_DAO_UFMatch' => array(
440 'fieldName' => 'domain_id',
441 'sample' => 'Default Domain Name',
444 'CRM_Core_DAO_Job' => array(
446 'fieldName' => 'domain_id',
447 'sample' => 'Default Domain Name',
450 'fieldName' => 'run_frequency',
454 'CRM_Core_DAO_JobLog' => array(
456 'fieldName' => 'domain_id',
457 'sample' => 'Default Domain Name',
460 'CRM_Contribute_DAO_ContributionSoft' => array(
462 'fieldName' => 'currency',
463 'sample' => array('USD' => 'US Dollar'),
467 'fieldName' => 'soft_credit_type_id',
468 'sample' => 'In Honor of',
471 'CRM_Contribute_DAO_Product' => array(
473 'fieldName' => 'currency',
474 'sample' => array('USD' => 'US Dollar'),
478 'fieldName' => 'financial_type_id',
479 'sample' => 'Donation',
482 'fieldName' => 'period_type',
483 'sample' => 'Rolling',
486 'fieldName' => 'duration_unit',
490 'fieldName' => 'frequency_unit',
494 'CRM_Contribute_DAO_ContributionProduct' => array(
496 'fieldName' => 'financial_type_id',
497 'sample' => 'Donation',
500 'CRM_Contribute_DAO_ContributionRecur' => array(
502 'fieldName' => 'currency',
503 'sample' => array('USD' => 'US Dollar'),
507 'fieldName' => 'frequency_unit',
511 'fieldName' => 'contribution_status_id',
512 'sample' => 'Completed',
515 'fieldName' => 'financial_type_id',
516 'sample' => 'Donation',
519 'fieldName' => 'payment_instrument_id',
523 'fieldName' => 'campaign_id',
524 'sample' => $campaign_name,
527 'CRM_Pledge_DAO_PledgePayment' => array(
529 'fieldName' => 'currency',
530 'sample' => array('USD' => 'US Dollar'),
534 'CRM_Pledge_DAO_Pledge' => array(
536 'fieldName' => 'currency',
537 'sample' => array('USD' => 'US Dollar'),
541 'fieldName' => 'financial_type_id',
542 'sample' => 'Donation',
545 'fieldName' => 'frequency_unit',
549 'fieldName' => 'campaign_id',
550 'sample' => $campaign_name,
553 'CRM_PCP_DAO_PCP' => array(
555 'fieldName' => 'currency',
556 'sample' => array('USD' => 'US Dollar'),
560 'fieldName' => 'status_id',
561 'sample' => 'Approved',
564 'CRM_Core_DAO_CustomField' => array(
566 'fieldName' => 'custom_group_id',
567 'sample' => $custom_group_name,
570 'fieldName' => 'data_type',
571 'sample' => 'Alphanumeric',
574 'fieldName' => 'html_type',
575 'sample' => 'Select Date',
578 'CRM_Core_DAO_CustomGroup' => array(
580 'fieldName' => 'style',
581 'sample' => 'Inline',
584 'CRM_Core_DAO_Dashboard' => array(
586 'fieldName' => 'domain_id',
587 'sample' => 'Default Domain Name',
590 'CRM_Core_DAO_Tag' => array(
592 'fieldName' => 'used_for',
593 'sample' => 'Contacts',
596 'CRM_Core_DAO_EntityTag' => array(
598 'fieldName' => 'tag_id',
599 'sample' => 'Government Entity',
602 'CRM_Core_DAO_Extension' => array(
604 'fieldName' => 'type',
605 'sample' => 'Module',
608 'CRM_Core_DAO_OptionValue' => array(
610 'fieldName' => 'option_group_id',
611 'sample' => 'gender',
615 'fieldName' => 'component_id',
616 'sample' => 'CiviContribute',
619 'fieldName' => 'domain_id',
620 'sample' => 'Default Domain Name',
623 'CRM_Core_DAO_MailSettings' => array(
625 'fieldName' => 'domain_id',
626 'sample' => 'Default Domain Name',
629 'fieldName' => 'protocol',
630 'sample' => 'Localdir',
633 'CRM_Core_DAO_Managed' => array(
635 'fieldName' => 'cleanup',
636 'sample' => 'Always',
639 'CRM_Core_DAO_Mapping' => array(
641 'fieldName' => 'mapping_type_id',
642 'sample' => 'Search Builder',
645 'CRM_Core_DAO_Navigation' => array(
647 'fieldName' => 'domain_id',
648 'sample' => 'Default Domain Name',
651 'CRM_Core_DAO_Phone' => array(
653 'fieldName' => 'phone_type_id',
657 'fieldName' => 'location_type_id',
661 'CRM_Core_DAO_PrintLabel' => array(
663 'fieldName' => 'label_format_name',
664 'sample' => 'Avery 5395',
667 'fieldName' => 'label_type_id',
668 'sample' => 'Event Badge',
671 'CRM_Core_DAO_Email' => array(
673 'fieldName' => 'location_type_id',
677 'CRM_Core_DAO_Address' => array(
679 'fieldName' => 'location_type_id',
683 'CRM_Core_DAO_Website' => array(
685 'fieldName' => 'website_type_id',
686 'sample' => 'Facebook',
689 'CRM_Core_DAO_WordReplacement' => array(
691 'fieldName' => 'match_type',
692 'sample' => 'Exact Match',
695 'fieldName' => 'domain_id',
696 'sample' => 'Default Domain Name',
699 'CRM_Core_DAO_MappingField' => array(
701 'fieldName' => 'contact_type',
702 'sample' => 'Individual',
705 'fieldName' => 'website_type_id',
706 'sample' => 'Facebook',
709 'fieldName' => 'im_provider_id',
713 'fieldName' => 'operator',
717 'CRM_Contact_DAO_Contact' => array(
719 'fieldName' => 'prefix_id',
723 'fieldName' => 'suffix_id',
727 'fieldName' => 'gender_id',
731 'fieldName' => 'preferred_communication_method',
732 'sample' => 'Postal Mail',
735 'fieldName' => 'contact_type',
736 'sample' => 'Individual',
740 'fieldName' => 'contact_sub_type',
742 'exclude' => 'Individual',
745 'fieldName' => 'preferred_language',
746 'sample' => array('en_US' => 'English (United States)'),
750 'fieldName' => 'preferred_mail_format',
754 'fieldName' => 'communication_style_id',
755 'sample' => 'Formal',
758 'fieldName' => "custom_$customFieldId",
759 'sample' => array('foo' => 'Foo'),
763 'CRM_Batch_DAO_Batch' => array(
765 'fieldName' => 'type_id',
766 'sample' => 'Membership',
769 'fieldName' => 'status_id',
770 'sample' => 'Reopened',
773 'fieldName' => 'mode_id',
774 'sample' => 'Automatic Batch',
777 'fieldName' => 'payment_instrument_id',
781 'CRM_Core_DAO_IM' => array(
783 'fieldName' => 'provider_id',
787 'fieldName' => 'location_type_id',
791 'CRM_Event_DAO_Participant' => array(
793 'fieldName' => 'status_id',
794 'sample' => 'Registered',
797 'fieldName' => 'role_id',
798 'sample' => 'Speaker',
801 'fieldName' => 'fee_currency',
802 'sample' => array('USD' => 'US Dollar'),
806 'fieldName' => 'campaign_id',
807 'sample' => $campaign_name,
810 'CRM_Event_DAO_Event' => array(
812 'fieldName' => 'event_type_id',
813 'sample' => 'Fundraiser',
816 'fieldName' => 'participant_listing_id',
817 'sample' => 'Name and Email',
820 'fieldName' => 'payment_processor',
821 'sample' => $pp_name,
824 'fieldName' => 'financial_type_id',
825 'sample' => 'Donation',
828 'fieldName' => 'default_role_id',
829 'sample' => 'Attendee',
832 'fieldName' => 'currency',
833 'sample' => array('USD' => 'US Dollar'),
837 'fieldName' => 'campaign_id',
838 'sample' => $campaign_name,
841 'CRM_Core_DAO_Menu' => array(
843 'fieldName' => 'domain_id',
844 'sample' => 'Default Domain Name',
847 'fieldName' => 'component_id',
848 'sample' => 'CiviMember',
851 'CRM_Member_DAO_Membership' => array(
853 'fieldName' => 'membership_type_id',
854 'sample' => $membership_type,
857 'fieldName' => 'status_id',
861 'fieldName' => 'campaign_id',
862 'sample' => $campaign_name,
865 'CRM_Member_DAO_MembershipStatus' => array(
867 'fieldName' => 'start_event',
868 'sample' => 'start date',
871 'fieldName' => 'end_event',
872 'sample' => 'member since',
875 'fieldName' => 'start_event_adjust_unit',
879 'fieldName' => 'end_event_adjust_unit',
883 'CRM_Member_DAO_MembershipType' => array(
885 'fieldName' => 'visibility',
886 'sample' => 'Public',
889 'fieldName' => 'domain_id',
890 'sample' => 'Default Domain Name',
893 'fieldName' => 'financial_type_id',
894 'sample' => 'Donation',
897 'fieldName' => 'duration_unit',
898 'sample' => 'lifetime',
901 'fieldName' => 'period_type',
902 'sample' => 'Rolling',
905 'CRM_Mailing_DAO_Mailing' => array(
907 'fieldName' => 'approval_status_id',
908 'sample' => 'Approved',
911 'fieldName' => 'domain_id',
912 'sample' => 'Default Domain Name',
915 'fieldName' => 'visibility',
916 'sample' => 'Public Pages',
919 'fieldName' => 'campaign_id',
920 'sample' => $campaign_name,
923 'CRM_Mailing_DAO_Component' => array(
925 'fieldName' => 'component_type',
926 'sample' => 'Header',
929 'CRM_Mailing_DAO_MailingGroup' => array(
931 'fieldName' => 'group_type',
932 'sample' => 'Include',
935 'CRM_Mailing_DAO_MailingJob' => array(
937 'fieldName' => 'status',
938 'sample' => 'Scheduled',
941 'CRM_Mailing_Event_DAO_Bounce' => array(
943 'fieldName' => 'bounce_type_id',
944 'sample' => 'Invalid',
947 'CRM_Mailing_Event_DAO_Subscribe' => array(
949 'fieldName' => 'group_id',
950 'sample' => $group_name,
953 'CRM_Grant_DAO_Grant' => array(
955 'fieldName' => 'status_id',
956 'sample' => 'Approved for Payment',
959 'fieldName' => 'grant_type_id',
960 'sample' => 'Emergency',
963 'fieldName' => 'currency',
964 'sample' => array('USD' => 'US Dollar'),
968 'fieldName' => 'financial_type_id',
969 'sample' => 'Donation',
972 'CRM_Contribute_DAO_Contribution' => array(
974 'fieldName' => 'payment_instrument_id',
975 'sample' => 'Credit Card',
978 'fieldName' => 'financial_type_id',
979 'sample' => 'Donation',
982 'fieldName' => 'currency',
983 'sample' => array('USD' => 'US Dollar'),
987 'fieldName' => 'contribution_status_id',
988 'sample' => 'Completed',
991 'fieldName' => 'contribution_page_id',
992 'sample' => $contribution_page,
995 'fieldName' => 'campaign_id',
996 'sample' => $campaign_name,
999 'CRM_Contribute_DAO_PremiumsProduct' => array(
1001 'fieldName' => 'financial_type_id',
1002 'sample' => 'Donation',
1005 'CRM_Contribute_DAO_ContributionPage' => array(
1007 'fieldName' => 'payment_processor',
1008 'sample' => $pp_name,
1011 'fieldName' => 'financial_type_id',
1012 'sample' => 'Donation',
1015 'fieldName' => 'currency',
1016 'sample' => array('USD' => 'US Dollar'),
1020 'fieldName' => 'campaign_id',
1021 'sample' => $campaign_name,
1024 'CRM_Case_DAO_Case' => array(
1026 'fieldName' => 'status_id',
1027 'sample' => 'Ongoing',
1030 'fieldName' => 'case_type_id',
1031 'sample' => 'Housing Support',
1034 'CRM_Report_DAO_ReportInstance' => array(
1036 'fieldName' => 'domain_id',
1037 'sample' => 'Default Domain Name',
1042 foreach ($fields as $daoName => $daoFields) {
1043 foreach ($daoFields as $field) {
1044 $message = "DAO name: '{$daoName}', field: '{$field['fieldName']}'";
1046 $optionValues = $daoName::buildOptions($field['fieldName']);
1047 $this->assertNotEmpty($optionValues, $message);
1049 // Ensure sample value is contained in the returned optionValues.
1050 if (!is_array($field['sample'])) {
1051 $this->assertContains($field['sample'], $optionValues, $message);
1053 // If sample is an array, we check keys and values
1055 foreach ($field['sample'] as $key => $value) {
1056 $this->assertArrayHasKey($key, $optionValues, $message);
1057 $this->assertEquals(CRM_Utils_Array
::value($key, $optionValues), $value, $message);
1061 // Ensure exclude value is not contained in the optionValues
1062 if (!empty($field['exclude'])) {
1063 $this->assertNotContains($field['exclude'], $optionValues, $message);
1066 // Ensure count of optionValues is not extraordinarily high.
1067 $max = CRM_Utils_Array
::value('max', $field, 20);
1068 $this->assertLessThanOrEqual($max, count($optionValues), $message);
1073 public function testContactTypes() {
1075 'Individual' => 'Individual',
1076 'Household' => 'Household',
1077 'Organization' => 'Organization',
1082 3 => 'Organization',
1084 // By default this should return an array keyed by name
1085 $result = CRM_Contact_DAO_Contact
::buildOptions('contact_type');
1086 $this->assertEquals($byName, $result);
1087 // But we can also fetch by ID
1088 $result = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name'));
1089 $this->assertEquals($byId, $result);
1090 // Make sure flip param works
1091 $result = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name', 'flip' => TRUE));
1092 $this->assertEquals(array_flip($byId), $result);