3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
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 +--------------------------------------------------------------------+
29 * Tests for pseudoconstant retrieval
32 class CRM_Core_PseudoConstantTest
extends CiviUnitTestCase
{
34 public function setUp() {
37 $this->loadAllFixtures();
39 CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCase');
40 CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCampaign');
44 * Assure CRM_Core_PseudoConstant::get() is working properly for a range of
45 * DAO fields having a <pseudoconstant> tag in the XML schema.
47 public function testOptionValues() {
49 // Create a custom field group for testing.
50 $custom_group_name = md5(microtime());
52 'title' => $custom_group_name,
53 'extends' => 'Individual',
56 $result = civicrm_api3('customGroup', 'create', $api_params);
58 // Add a custom field to the above field group.
61 'custom_group_id' => $result['id'],
62 'label' => $custom_group_name,
63 'html_type' => 'Select',
64 'data_type' => 'String',
75 $result = civicrm_api3('custom_field', 'create', $api_params);
76 $customFieldId = $result['id'];
78 // Create a Contact Group for testing.
79 $group_name = md5(microtime());
81 'title' => $group_name,
84 $result = civicrm_api3('group', 'create', $api_params);
86 // Create a PaymentProcessor for testing.
87 $pp_name = md5(microtime());
90 'payment_processor_type_id' => 'Dummy',
92 'user_name' => $pp_name,
93 'class_name' => 'Payment_Dummy',
94 'url_site' => 'https://test.com/',
95 'url_recur' => 'https://test.com/',
98 $result = civicrm_api3('payment_processor', 'create', $api_params);
100 // Create a Campaign for testing.
101 $campaign_name = md5(microtime());
103 'title' => $campaign_name,
107 $result = civicrm_api3('campaign', 'create', $api_params);
109 // Create a membership type for testing.
110 $membership_type = md5(microtime());
112 'name' => $membership_type,
114 'financial_type_id' => 1,
116 'member_of_contact_id' => 1,
117 'duration_unit' => 'day',
118 'duration_interval' => 1,
119 'period_type' => 'rolling',
121 $result = civicrm_api3('membership_type', 'create', $api_params);
123 // Create a contribution page for testing.
124 $contribution_page = md5(microtime());
126 'title' => $contribution_page,
128 'financial_type_id' => 1,
130 $result = civicrm_api3('contribution_page', 'create', $api_params);
133 * daoName/field combinations to test
134 * Format: array[DAO Name] = $properties, where properties is an array whose
135 * named members can be:
136 * - fieldName: the SQL column name within the DAO table.
137 * - sample: Any one value which is expected in the list of option values.
138 * - exclude: Any one value which should not be in the list.
139 * - max: integer (default = 20) maximum number of option values expected.
142 'CRM_ACL_DAO_ACL' => [
144 'fieldName' => 'operation',
148 'CRM_Contact_DAO_Group' => [
150 'fieldName' => 'visibility',
151 'sample' => 'Public Pages',
154 'CRM_Contact_DAO_GroupContact' => [
156 'fieldName' => 'group_id',
157 'sample' => $group_name,
160 'fieldName' => 'status',
164 'CRM_Contact_DAO_GroupContactCache' => [
166 'fieldName' => 'group_id',
167 'sample' => $group_name,
170 'CRM_Contact_DAO_GroupOrganization' => [
172 'fieldName' => 'group_id',
173 'sample' => $group_name,
176 'CRM_Contact_DAO_SubscriptionHistory' => [
178 'fieldName' => 'group_id',
179 'sample' => $group_name,
182 'fieldName' => 'method',
186 'fieldName' => 'status',
190 'CRM_Core_DAO_Cache' => [
192 'fieldName' => 'component_id',
193 'sample' => 'CiviMail',
196 'CRM_Contact_DAO_ACLContactCache' => [
198 'fieldName' => 'operation',
202 'CRM_Core_DAO_Setting' => [
204 'fieldName' => 'component_id',
205 'sample' => 'CiviMail',
208 'CRM_Core_DAO_ActionSchedule' => [
210 'fieldName' => 'group_id',
211 'sample' => $group_name,
214 'fieldName' => 'start_action_unit',
218 'fieldName' => 'repetition_frequency_unit',
222 'fieldName' => 'end_frequency_unit',
226 'fieldName' => 'mode',
230 'CRM_Dedupe_DAO_RuleGroup' => [
232 'fieldName' => 'contact_type',
233 'sample' => 'Individual',
236 'fieldName' => 'used',
237 'sample' => 'Unsupervised',
240 'CRM_Activity_DAO_Activity' => [
242 'fieldName' => 'activity_type_id',
247 'fieldName' => 'status_id',
248 'sample' => 'Scheduled',
251 'fieldName' => 'priority_id',
252 'sample' => 'Urgent',
255 'fieldName' => 'engagement_level',
259 'fieldName' => 'medium_id',
263 'fieldName' => 'campaign_id',
264 'sample' => $campaign_name,
267 'CRM_Campaign_DAO_Campaign' => [
269 'fieldName' => 'campaign_type_id',
270 'sample' => 'Constituent Engagement',
274 'fieldName' => 'status_id',
275 'sample' => 'Completed',
279 'CRM_Campaign_DAO_Survey' => [
281 'fieldName' => 'campaign_id',
282 'sample' => $campaign_name,
285 'fieldName' => 'activity_type_id',
286 'sample' => 'Phone Call',
290 'CRM_Campaign_DAO_CampaignGroup' => [
292 'fieldName' => 'campaign_id',
293 'sample' => $campaign_name,
296 'fieldName' => 'group_type',
297 'sample' => 'Include',
300 'CRM_Contact_DAO_RelationshipType' => [
302 'fieldName' => 'contact_type_a',
303 'sample' => 'Individual',
306 'fieldName' => 'contact_type_b',
307 'sample' => 'Organization',
310 'CRM_Event_DAO_ParticipantStatusType' => [
312 'fieldName' => 'class',
313 'sample' => 'Waiting',
316 'fieldName' => 'visibility_id',
317 'sample' => 'Public',
320 'CRM_Price_DAO_LineItem' => [
322 'fieldName' => 'financial_type_id',
323 'sample' => 'Donation',
326 'CRM_Price_DAO_PriceField' => [
328 'fieldName' => 'html_type',
329 'sample' => 'Select',
332 'fieldName' => 'visibility_id',
333 'sample' => 'Public',
336 'CRM_Price_DAO_PriceFieldValue' => [
338 'fieldName' => 'financial_type_id',
339 'sample' => 'Donation',
342 'CRM_Price_DAO_PriceSet' => [
344 'fieldName' => 'domain_id',
345 'sample' => 'Default Domain Name',
348 'fieldName' => 'extends',
349 'sample' => 'CiviEvent',
352 'fieldName' => 'financial_type_id',
353 'sample' => 'Donation',
356 'CRM_Financial_DAO_EntityFinancialAccount' => [
358 'fieldName' => 'financial_account_id',
359 'sample' => 'Member Dues',
362 'fieldName' => 'account_relationship',
363 'sample' => 'Income Account is',
366 'CRM_Financial_DAO_FinancialItem' => [
368 'fieldName' => 'status_id',
369 'sample' => 'Partially paid',
372 'fieldName' => 'financial_account_id',
373 'sample' => 'Accounts Receivable',
376 'fieldName' => 'currency',
377 'sample' => ['USD' => 'US Dollar'],
381 'CRM_Financial_DAO_FinancialTrxn' => [
383 'fieldName' => 'from_financial_account_id',
384 'sample' => 'Accounts Receivable',
387 'fieldName' => 'to_financial_account_id',
388 'sample' => 'Accounts Receivable',
391 'fieldName' => 'currency',
392 'sample' => ['USD' => 'US Dollar'],
396 'fieldName' => 'payment_instrument_id',
400 'CRM_Financial_DAO_FinancialAccount' => [
402 'fieldName' => 'financial_account_type_id',
403 'sample' => 'Cost of Sales',
406 'CRM_Financial_DAO_PaymentProcessor' => [
408 'fieldName' => 'domain_id',
409 'sample' => 'Default Domain Name',
412 'CRM_Financial_BAO_PaymentProcessorType' => [
414 'fieldName' => 'billing_mode',
418 'CRM_Core_DAO_UFField' => [
420 'fieldName' => 'uf_group_id',
421 'sample' => 'Name and Address',
424 'fieldName' => 'visibility',
425 'sample' => 'Expose Publicly',
428 'CRM_Core_DAO_UFJoin' => [
430 'fieldName' => 'uf_group_id',
431 'sample' => 'Name and Address',
434 'CRM_Core_DAO_UFMatch' => [
436 'fieldName' => 'domain_id',
437 'sample' => 'Default Domain Name',
440 'CRM_Core_DAO_Job' => [
442 'fieldName' => 'domain_id',
443 'sample' => 'Default Domain Name',
446 'fieldName' => 'run_frequency',
450 'CRM_Core_DAO_JobLog' => [
452 'fieldName' => 'domain_id',
453 'sample' => 'Default Domain Name',
456 'CRM_Contribute_DAO_ContributionSoft' => [
458 'fieldName' => 'currency',
459 'sample' => ['USD' => 'US Dollar'],
463 'fieldName' => 'soft_credit_type_id',
464 'sample' => 'In Honor of',
467 'CRM_Contribute_DAO_Product' => [
469 'fieldName' => 'currency',
470 'sample' => ['USD' => 'US Dollar'],
474 'fieldName' => 'financial_type_id',
475 'sample' => 'Donation',
478 'fieldName' => 'period_type',
479 'sample' => 'Rolling',
482 'fieldName' => 'duration_unit',
486 'fieldName' => 'frequency_unit',
490 'CRM_Contribute_DAO_ContributionProduct' => [
492 'fieldName' => 'financial_type_id',
493 'sample' => 'Donation',
496 'CRM_Contribute_DAO_ContributionRecur' => [
498 'fieldName' => 'currency',
499 'sample' => ['USD' => 'US Dollar'],
503 'fieldName' => 'frequency_unit',
507 'fieldName' => 'contribution_status_id',
508 'sample' => 'Completed',
511 'fieldName' => 'financial_type_id',
512 'sample' => 'Donation',
515 'fieldName' => 'payment_instrument_id',
519 'fieldName' => 'campaign_id',
520 'sample' => $campaign_name,
523 'CRM_Pledge_DAO_PledgePayment' => [
525 'fieldName' => 'currency',
526 'sample' => ['USD' => 'US Dollar'],
530 'CRM_Pledge_DAO_Pledge' => [
532 'fieldName' => 'currency',
533 'sample' => ['USD' => 'US Dollar'],
537 'fieldName' => 'financial_type_id',
538 'sample' => 'Donation',
541 'fieldName' => 'frequency_unit',
545 'fieldName' => 'campaign_id',
546 'sample' => $campaign_name,
549 'CRM_PCP_DAO_PCP' => [
551 'fieldName' => 'currency',
552 'sample' => ['USD' => 'US Dollar'],
556 'fieldName' => 'status_id',
557 'sample' => 'Approved',
560 'CRM_Core_DAO_CustomField' => [
562 'fieldName' => 'custom_group_id',
563 'sample' => $custom_group_name,
566 'fieldName' => 'data_type',
567 'sample' => 'Alphanumeric',
570 'fieldName' => 'html_type',
571 'sample' => 'Select Date',
574 'CRM_Core_DAO_CustomGroup' => [
576 'fieldName' => 'style',
577 'sample' => 'Inline',
580 'CRM_Core_DAO_Dashboard' => [
582 'fieldName' => 'domain_id',
583 'sample' => 'Default Domain Name',
586 'CRM_Core_DAO_Tag' => [
588 'fieldName' => 'used_for',
589 'sample' => 'Contacts',
592 'CRM_Core_DAO_EntityTag' => [
594 'fieldName' => 'tag_id',
595 'sample' => 'Government Entity',
598 'CRM_Core_DAO_Extension' => [
600 'fieldName' => 'type',
601 'sample' => 'Module',
604 'CRM_Core_DAO_OptionValue' => [
606 'fieldName' => 'option_group_id',
607 'sample' => 'gender',
611 'fieldName' => 'component_id',
612 'sample' => 'CiviContribute',
615 'fieldName' => 'domain_id',
616 'sample' => 'Default Domain Name',
619 'CRM_Core_DAO_MailSettings' => [
621 'fieldName' => 'domain_id',
622 'sample' => 'Default Domain Name',
625 'fieldName' => 'protocol',
626 'sample' => 'Localdir',
629 'CRM_Core_DAO_Managed' => [
631 'fieldName' => 'cleanup',
632 'sample' => 'Always',
635 'CRM_Core_DAO_Mapping' => [
637 'fieldName' => 'mapping_type_id',
638 'sample' => 'Search Builder',
641 'CRM_Core_DAO_Navigation' => [
643 'fieldName' => 'domain_id',
644 'sample' => 'Default Domain Name',
647 'CRM_Core_DAO_Phone' => [
649 'fieldName' => 'phone_type_id',
653 'fieldName' => 'location_type_id',
657 'CRM_Core_DAO_PrintLabel' => [
659 'fieldName' => 'label_format_name',
660 'sample' => 'Avery 5395',
663 'fieldName' => 'label_type_id',
664 'sample' => 'Event Badge',
667 'CRM_Core_DAO_Email' => [
669 'fieldName' => 'location_type_id',
673 'CRM_Core_DAO_Address' => [
675 'fieldName' => 'location_type_id',
679 'CRM_Core_DAO_Website' => [
681 'fieldName' => 'website_type_id',
682 'sample' => 'Facebook',
685 'CRM_Core_DAO_WordReplacement' => [
687 'fieldName' => 'match_type',
688 'sample' => 'Exact Match',
691 'fieldName' => 'domain_id',
692 'sample' => 'Default Domain Name',
695 'CRM_Core_DAO_MappingField' => [
697 'fieldName' => 'website_type_id',
698 'sample' => 'Facebook',
701 'fieldName' => 'im_provider_id',
705 'fieldName' => 'operator',
709 'CRM_Contact_DAO_Contact' => [
711 'fieldName' => 'prefix_id',
715 'fieldName' => 'suffix_id',
719 'fieldName' => 'gender_id',
723 'fieldName' => 'preferred_communication_method',
724 'sample' => 'Postal Mail',
727 'fieldName' => 'contact_type',
728 'sample' => 'Individual',
732 'fieldName' => 'contact_sub_type',
734 'exclude' => 'Individual',
737 'fieldName' => 'preferred_language',
738 'sample' => ['en_US' => 'English (United States)'],
742 'fieldName' => 'preferred_mail_format',
746 'fieldName' => 'communication_style_id',
747 'sample' => 'Formal',
750 'fieldName' => "custom_$customFieldId",
751 'sample' => ['foo' => 'Foo'],
755 'CRM_Batch_DAO_Batch' => [
757 'fieldName' => 'type_id',
758 'sample' => 'Membership',
761 'fieldName' => 'status_id',
762 'sample' => 'Reopened',
765 'fieldName' => 'mode_id',
766 'sample' => 'Automatic Batch',
769 'fieldName' => 'payment_instrument_id',
773 'CRM_Core_DAO_IM' => [
775 'fieldName' => 'provider_id',
779 'fieldName' => 'location_type_id',
783 'CRM_Event_DAO_Participant' => [
785 'fieldName' => 'status_id',
786 'sample' => 'Registered',
789 'fieldName' => 'role_id',
790 'sample' => 'Speaker',
793 'fieldName' => 'fee_currency',
794 'sample' => ['USD' => 'US Dollar'],
798 'fieldName' => 'campaign_id',
799 'sample' => $campaign_name,
802 'CRM_Event_DAO_Event' => [
804 'fieldName' => 'event_type_id',
805 'sample' => 'Fundraiser',
808 'fieldName' => 'participant_listing_id',
809 'sample' => 'Name and Email',
812 'fieldName' => 'payment_processor',
813 'sample' => $pp_name,
816 'fieldName' => 'financial_type_id',
817 'sample' => 'Donation',
820 'fieldName' => 'default_role_id',
821 'sample' => 'Attendee',
824 'fieldName' => 'currency',
825 'sample' => ['USD' => 'US Dollar'],
829 'fieldName' => 'campaign_id',
830 'sample' => $campaign_name,
833 'CRM_Core_DAO_Menu' => [
835 'fieldName' => 'domain_id',
836 'sample' => 'Default Domain Name',
839 'fieldName' => 'component_id',
840 'sample' => 'CiviMember',
843 'CRM_Member_DAO_Membership' => [
845 'fieldName' => 'membership_type_id',
846 'sample' => $membership_type,
849 'fieldName' => 'status_id',
853 'fieldName' => 'campaign_id',
854 'sample' => $campaign_name,
857 'CRM_Member_DAO_MembershipStatus' => [
859 'fieldName' => 'start_event',
860 'sample' => 'start date',
863 'fieldName' => 'end_event',
864 'sample' => 'member since',
867 'fieldName' => 'start_event_adjust_unit',
871 'fieldName' => 'end_event_adjust_unit',
875 'CRM_Member_DAO_MembershipType' => [
877 'fieldName' => 'visibility',
878 'sample' => 'Public',
881 'fieldName' => 'domain_id',
882 'sample' => 'Default Domain Name',
885 'fieldName' => 'financial_type_id',
886 'sample' => 'Donation',
889 'fieldName' => 'duration_unit',
890 'sample' => 'lifetime',
893 'fieldName' => 'period_type',
894 'sample' => 'Rolling',
897 'CRM_Mailing_DAO_Mailing' => [
899 'fieldName' => 'approval_status_id',
900 'sample' => 'Approved',
903 'fieldName' => 'domain_id',
904 'sample' => 'Default Domain Name',
907 'fieldName' => 'visibility',
908 'sample' => 'Public Pages',
911 'fieldName' => 'campaign_id',
912 'sample' => $campaign_name,
915 'CRM_Mailing_DAO_MailingComponent' => [
917 'fieldName' => 'component_type',
918 'sample' => 'Header',
921 'CRM_Mailing_DAO_MailingGroup' => [
923 'fieldName' => 'group_type',
924 'sample' => 'Include',
927 'CRM_Mailing_DAO_MailingJob' => [
929 'fieldName' => 'status',
930 'sample' => 'Scheduled',
933 'CRM_Mailing_Event_DAO_Bounce' => [
935 'fieldName' => 'bounce_type_id',
936 'sample' => 'Invalid',
939 'CRM_Mailing_Event_DAO_Subscribe' => [
941 'fieldName' => 'group_id',
942 'sample' => $group_name,
945 'CRM_Grant_DAO_Grant' => [
947 'fieldName' => 'status_id',
948 'sample' => 'Approved for Payment',
951 'fieldName' => 'grant_type_id',
952 'sample' => 'Emergency',
955 'fieldName' => 'currency',
956 'sample' => ['USD' => 'US Dollar'],
960 'fieldName' => 'financial_type_id',
961 'sample' => 'Donation',
964 'CRM_Contribute_DAO_Contribution' => [
966 'fieldName' => 'payment_instrument_id',
967 'sample' => 'Credit Card',
970 'fieldName' => 'financial_type_id',
971 'sample' => 'Donation',
974 'fieldName' => 'currency',
975 'sample' => ['USD' => 'US Dollar'],
979 'fieldName' => 'contribution_status_id',
980 'sample' => 'Completed',
983 'fieldName' => 'contribution_page_id',
984 'sample' => $contribution_page,
987 'fieldName' => 'campaign_id',
988 'sample' => $campaign_name,
991 'CRM_Contribute_DAO_PremiumsProduct' => [
993 'fieldName' => 'financial_type_id',
994 'sample' => 'Donation',
997 'CRM_Contribute_DAO_ContributionPage' => [
999 'fieldName' => 'payment_processor',
1000 'sample' => $pp_name,
1003 'fieldName' => 'financial_type_id',
1004 'sample' => 'Donation',
1007 'fieldName' => 'currency',
1008 'sample' => ['USD' => 'US Dollar'],
1012 'fieldName' => 'campaign_id',
1013 'sample' => $campaign_name,
1016 'CRM_Case_DAO_Case' => [
1018 'fieldName' => 'status_id',
1019 'sample' => 'Ongoing',
1022 'fieldName' => 'case_type_id',
1023 'sample' => 'Housing Support',
1026 'CRM_Report_DAO_ReportInstance' => [
1028 'fieldName' => 'domain_id',
1029 'sample' => 'Default Domain Name',
1034 foreach ($fields as $daoName => $daoFields) {
1035 foreach ($daoFields as $field) {
1036 $message = "DAO name: '{$daoName}', field: '{$field['fieldName']}'";
1038 $optionValues = $daoName::buildOptions($field['fieldName']);
1039 $this->assertNotEmpty($optionValues, $message);
1041 // Ensure sample value is contained in the returned optionValues.
1042 if (!is_array($field['sample'])) {
1043 $this->assertContains($field['sample'], $optionValues, $message);
1045 // If sample is an array, we check keys and values
1047 foreach ($field['sample'] as $key => $value) {
1048 $this->assertArrayHasKey($key, $optionValues, $message);
1049 $this->assertEquals(CRM_Utils_Array
::value($key, $optionValues), $value, $message);
1053 // Ensure exclude value is not contained in the optionValues
1054 if (!empty($field['exclude'])) {
1055 $this->assertNotContains($field['exclude'], $optionValues, $message);
1058 // Ensure count of optionValues is not extraordinarily high.
1059 $max = CRM_Utils_Array
::value('max', $field, 20);
1060 $this->assertLessThanOrEqual($max, count($optionValues), $message);
1065 public function testContactTypes() {
1067 'Individual' => 'Individual',
1068 'Household' => 'Household',
1069 'Organization' => 'Organization',
1074 3 => 'Organization',
1076 // By default this should return an array keyed by name
1077 $result = CRM_Contact_DAO_Contact
::buildOptions('contact_type');
1078 $this->assertEquals($byName, $result);
1079 // But we can also fetch by ID
1080 $result = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'contact_type', [
1081 'keyColumn' => 'id',
1082 'labelColumn' => 'name',
1084 $this->assertEquals($byId, $result);
1085 // Make sure flip param works
1086 $result = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'contact_type', [
1087 'keyColumn' => 'id',
1088 'labelColumn' => 'name',
1091 $this->assertEquals(array_flip($byId), $result);
1094 public function testGetTaxRates() {
1095 $contact = $this->createLoggedInUser();
1096 $financialType = $this->callAPISuccess('financial_type', 'create', [
1097 'name' => 'Test taxable financial Type',
1101 $financialAccount = $this->callAPISuccess('financial_account', 'create', [
1102 'name' => 'Test Tax financial account ',
1103 'contact_id' => $contact,
1104 'financial_account_type_id' => 2,
1111 $financialTypeId = $financialType['id'];
1112 $financialAccountId = $financialAccount['id'];
1113 $financialAccountParams = [
1114 'entity_table' => 'civicrm_financial_type',
1115 'entity_id' => $financialTypeId,
1116 'account_relationship' => 10,
1117 'financial_account_id' => $financialAccountId,
1119 CRM_Financial_BAO_FinancialTypeAccount
::add($financialAccountParams);
1120 $taxRates = CRM_Core_PseudoConstant
::getTaxRates();
1121 $this->assertEquals('5.00', $taxRates[$financialType['id']]);