3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Tests for pseudoconstant retrieval
16 class CRM_Core_PseudoConstantTest
extends CiviUnitTestCase
{
18 public function setUp() {
21 $this->loadAllFixtures();
23 CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCase');
24 CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCampaign');
28 * Assure CRM_Core_PseudoConstant::get() is working properly for a range of
29 * DAO fields having a <pseudoconstant> tag in the XML schema.
31 public function testOptionValues() {
33 // Create a custom field group for testing.
34 $custom_group_name = md5(microtime());
36 'title' => $custom_group_name,
37 'extends' => 'Individual',
40 $result = civicrm_api3('customGroup', 'create', $api_params);
42 // Add a custom field to the above field group.
45 'custom_group_id' => $result['id'],
46 'label' => $custom_group_name,
47 'html_type' => 'Select',
48 'data_type' => 'String',
59 $result = civicrm_api3('custom_field', 'create', $api_params);
60 $customFieldId = $result['id'];
62 // Create a Contact Group for testing.
63 $group_name = md5(microtime());
65 'title' => $group_name,
68 $result = civicrm_api3('group', 'create', $api_params);
70 // Create a PaymentProcessor for testing.
71 $pp_name = md5(microtime());
74 'payment_processor_type_id' => 'Dummy',
76 'user_name' => $pp_name,
77 'class_name' => 'Payment_Dummy',
78 'url_site' => 'https://test.com/',
79 'url_recur' => 'https://test.com/',
82 $result = civicrm_api3('payment_processor', 'create', $api_params);
84 // Create a Campaign for testing.
85 $campaign_name = md5(microtime());
87 'title' => $campaign_name,
91 $result = civicrm_api3('campaign', 'create', $api_params);
93 // Create a membership type for testing.
94 $membership_type = md5(microtime());
96 'name' => $membership_type,
98 'financial_type_id' => 1,
100 'member_of_contact_id' => 1,
101 'duration_unit' => 'day',
102 'duration_interval' => 1,
103 'period_type' => 'rolling',
105 $result = civicrm_api3('membership_type', 'create', $api_params);
107 // Create a contribution page for testing.
108 $contribution_page = md5(microtime());
110 'title' => $contribution_page,
112 'financial_type_id' => 1,
114 $result = civicrm_api3('contribution_page', 'create', $api_params);
117 * daoName/field combinations to test
118 * Format: array[DAO Name] = $properties, where properties is an array whose
119 * named members can be:
120 * - fieldName: the SQL column name within the DAO table.
121 * - sample: Any one value which is expected in the list of option values.
122 * - exclude: Any one value which should not be in the list.
123 * - max: integer (default = 20) maximum number of option values expected.
126 'CRM_ACL_DAO_ACL' => [
128 'fieldName' => 'operation',
132 'CRM_Contact_DAO_Group' => [
134 'fieldName' => 'visibility',
135 'sample' => 'Public Pages',
138 'CRM_Contact_DAO_GroupContact' => [
140 'fieldName' => 'group_id',
141 'sample' => $group_name,
144 'fieldName' => 'status',
148 'CRM_Contact_DAO_GroupContactCache' => [
150 'fieldName' => 'group_id',
151 'sample' => $group_name,
154 'CRM_Contact_DAO_GroupOrganization' => [
156 'fieldName' => 'group_id',
157 'sample' => $group_name,
160 'CRM_Contact_DAO_SubscriptionHistory' => [
162 'fieldName' => 'group_id',
163 'sample' => $group_name,
166 'fieldName' => 'method',
170 'fieldName' => 'status',
174 'CRM_Core_DAO_Cache' => [
176 'fieldName' => 'component_id',
177 'sample' => 'CiviMail',
180 'CRM_Contact_DAO_ACLContactCache' => [
182 'fieldName' => 'operation',
186 'CRM_Core_DAO_Setting' => [
188 'fieldName' => 'component_id',
189 'sample' => 'CiviMail',
192 'CRM_Core_DAO_ActionSchedule' => [
194 'fieldName' => 'group_id',
195 'sample' => $group_name,
198 'fieldName' => 'start_action_unit',
202 'fieldName' => 'repetition_frequency_unit',
206 'fieldName' => 'end_frequency_unit',
210 'fieldName' => 'mode',
214 'CRM_Dedupe_DAO_RuleGroup' => [
216 'fieldName' => 'contact_type',
217 'sample' => 'Individual',
220 'fieldName' => 'used',
221 'sample' => 'Unsupervised',
224 'CRM_Activity_DAO_Activity' => [
226 'fieldName' => 'activity_type_id',
231 'fieldName' => 'status_id',
232 'sample' => 'Scheduled',
235 'fieldName' => 'priority_id',
236 'sample' => 'Urgent',
239 'fieldName' => 'engagement_level',
243 'fieldName' => 'medium_id',
247 'fieldName' => 'campaign_id',
248 'sample' => $campaign_name,
251 'CRM_Campaign_DAO_Campaign' => [
253 'fieldName' => 'campaign_type_id',
254 'sample' => 'Constituent Engagement',
258 'fieldName' => 'status_id',
259 'sample' => 'Completed',
263 'CRM_Campaign_DAO_Survey' => [
265 'fieldName' => 'campaign_id',
266 'sample' => $campaign_name,
269 'fieldName' => 'activity_type_id',
270 'sample' => 'Phone Call',
274 'CRM_Campaign_DAO_CampaignGroup' => [
276 'fieldName' => 'campaign_id',
277 'sample' => $campaign_name,
280 'fieldName' => 'group_type',
281 'sample' => 'Include',
284 'CRM_Contact_DAO_RelationshipType' => [
286 'fieldName' => 'contact_type_a',
287 'sample' => 'Individual',
290 'fieldName' => 'contact_type_b',
291 'sample' => 'Organization',
294 'CRM_Event_DAO_ParticipantStatusType' => [
296 'fieldName' => 'class',
297 'sample' => 'Waiting',
300 'fieldName' => 'visibility_id',
301 'sample' => 'Public',
304 'CRM_Price_DAO_LineItem' => [
306 'fieldName' => 'financial_type_id',
307 'sample' => 'Donation',
310 'CRM_Price_DAO_PriceField' => [
312 'fieldName' => 'html_type',
313 'sample' => 'Select',
316 'fieldName' => 'visibility_id',
317 'sample' => 'Public',
320 'CRM_Price_DAO_PriceFieldValue' => [
322 'fieldName' => 'financial_type_id',
323 'sample' => 'Donation',
326 'CRM_Price_DAO_PriceSet' => [
328 'fieldName' => 'domain_id',
329 'sample' => 'Default Domain Name',
332 'fieldName' => 'extends',
333 'sample' => 'CiviEvent',
336 'fieldName' => 'financial_type_id',
337 'sample' => 'Donation',
340 'CRM_Financial_DAO_EntityFinancialAccount' => [
342 'fieldName' => 'financial_account_id',
343 'sample' => 'Member Dues',
346 'fieldName' => 'account_relationship',
347 'sample' => 'Income Account is',
350 'CRM_Financial_DAO_FinancialItem' => [
352 'fieldName' => 'status_id',
353 'sample' => 'Partially paid',
356 'fieldName' => 'financial_account_id',
357 'sample' => 'Accounts Receivable',
360 'fieldName' => 'currency',
361 'sample' => ['USD' => 'US Dollar'],
365 'CRM_Financial_DAO_FinancialTrxn' => [
367 'fieldName' => 'from_financial_account_id',
368 'sample' => 'Accounts Receivable',
371 'fieldName' => 'to_financial_account_id',
372 'sample' => 'Accounts Receivable',
375 'fieldName' => 'currency',
376 'sample' => ['USD' => 'US Dollar'],
380 'fieldName' => 'payment_instrument_id',
384 'CRM_Financial_DAO_FinancialAccount' => [
386 'fieldName' => 'financial_account_type_id',
387 'sample' => 'Cost of Sales',
390 'CRM_Financial_DAO_PaymentProcessor' => [
392 'fieldName' => 'domain_id',
393 'sample' => 'Default Domain Name',
396 'CRM_Financial_BAO_PaymentProcessorType' => [
398 'fieldName' => 'billing_mode',
402 'CRM_Core_DAO_UFField' => [
404 'fieldName' => 'uf_group_id',
405 'sample' => 'Name and Address',
408 'fieldName' => 'visibility',
409 'sample' => 'Expose Publicly',
412 'CRM_Core_DAO_UFJoin' => [
414 'fieldName' => 'uf_group_id',
415 'sample' => 'Name and Address',
418 'CRM_Core_DAO_UFMatch' => [
420 'fieldName' => 'domain_id',
421 'sample' => 'Default Domain Name',
424 'CRM_Core_DAO_Job' => [
426 'fieldName' => 'domain_id',
427 'sample' => 'Default Domain Name',
430 'fieldName' => 'run_frequency',
434 'CRM_Core_DAO_JobLog' => [
436 'fieldName' => 'domain_id',
437 'sample' => 'Default Domain Name',
440 'CRM_Contribute_DAO_ContributionSoft' => [
442 'fieldName' => 'currency',
443 'sample' => ['USD' => 'US Dollar'],
447 'fieldName' => 'soft_credit_type_id',
448 'sample' => 'In Honor of',
451 'CRM_Contribute_DAO_Product' => [
453 'fieldName' => 'currency',
454 'sample' => ['USD' => 'US Dollar'],
458 'fieldName' => 'financial_type_id',
459 'sample' => 'Donation',
462 'fieldName' => 'period_type',
463 'sample' => 'Rolling',
466 'fieldName' => 'duration_unit',
470 'fieldName' => 'frequency_unit',
474 'CRM_Contribute_DAO_ContributionProduct' => [
476 'fieldName' => 'financial_type_id',
477 'sample' => 'Donation',
480 'CRM_Contribute_DAO_ContributionRecur' => [
482 'fieldName' => 'currency',
483 'sample' => ['USD' => 'US Dollar'],
487 'fieldName' => 'frequency_unit',
491 'fieldName' => 'contribution_status_id',
492 'sample' => 'Completed',
495 'fieldName' => 'financial_type_id',
496 'sample' => 'Donation',
499 'fieldName' => 'payment_instrument_id',
503 'fieldName' => 'campaign_id',
504 'sample' => $campaign_name,
507 'CRM_Pledge_DAO_PledgePayment' => [
509 'fieldName' => 'currency',
510 'sample' => ['USD' => 'US Dollar'],
514 'CRM_Pledge_DAO_Pledge' => [
516 'fieldName' => 'currency',
517 'sample' => ['USD' => 'US Dollar'],
521 'fieldName' => 'financial_type_id',
522 'sample' => 'Donation',
525 'fieldName' => 'frequency_unit',
529 'fieldName' => 'campaign_id',
530 'sample' => $campaign_name,
533 'CRM_PCP_DAO_PCP' => [
535 'fieldName' => 'currency',
536 'sample' => ['USD' => 'US Dollar'],
540 'fieldName' => 'status_id',
541 'sample' => 'Approved',
544 'CRM_Core_DAO_CustomField' => [
546 'fieldName' => 'custom_group_id',
547 'sample' => $custom_group_name,
550 'fieldName' => 'data_type',
551 'sample' => 'Alphanumeric',
554 'fieldName' => 'html_type',
555 'sample' => 'Select Date',
558 'CRM_Core_DAO_CustomGroup' => [
560 'fieldName' => 'style',
561 'sample' => 'Inline',
564 'CRM_Core_DAO_Dashboard' => [
566 'fieldName' => 'domain_id',
567 'sample' => 'Default Domain Name',
570 'CRM_Core_DAO_Tag' => [
572 'fieldName' => 'used_for',
573 'sample' => 'Contacts',
576 'CRM_Core_DAO_EntityTag' => [
578 'fieldName' => 'tag_id',
579 'sample' => 'Government Entity',
582 'CRM_Core_DAO_Extension' => [
584 'fieldName' => 'type',
585 'sample' => 'Module',
588 'CRM_Core_DAO_OptionValue' => [
590 'fieldName' => 'option_group_id',
591 'sample' => 'gender',
595 'fieldName' => 'component_id',
596 'sample' => 'CiviContribute',
599 'fieldName' => 'domain_id',
600 'sample' => 'Default Domain Name',
603 'CRM_Core_DAO_MailSettings' => [
605 'fieldName' => 'domain_id',
606 'sample' => 'Default Domain Name',
609 'fieldName' => 'protocol',
610 'sample' => 'Localdir',
613 'CRM_Core_DAO_Managed' => [
615 'fieldName' => 'cleanup',
616 'sample' => 'Always',
619 'CRM_Core_DAO_Mapping' => [
621 'fieldName' => 'mapping_type_id',
622 'sample' => 'Search Builder',
625 'CRM_Core_DAO_Navigation' => [
627 'fieldName' => 'domain_id',
628 'sample' => 'Default Domain Name',
631 'CRM_Core_DAO_Phone' => [
633 'fieldName' => 'phone_type_id',
637 'fieldName' => 'location_type_id',
641 'CRM_Core_DAO_PrintLabel' => [
643 'fieldName' => 'label_format_name',
644 'sample' => 'Avery 5395',
647 'fieldName' => 'label_type_id',
648 'sample' => 'Event Badge',
651 'CRM_Core_DAO_Email' => [
653 'fieldName' => 'location_type_id',
657 'CRM_Core_DAO_Address' => [
659 'fieldName' => 'location_type_id',
663 'CRM_Core_DAO_Website' => [
665 'fieldName' => 'website_type_id',
666 'sample' => 'Facebook',
669 'CRM_Core_DAO_WordReplacement' => [
671 'fieldName' => 'match_type',
672 'sample' => 'Exact Match',
675 'fieldName' => 'domain_id',
676 'sample' => 'Default Domain Name',
679 'CRM_Core_DAO_MappingField' => [
681 'fieldName' => 'website_type_id',
682 'sample' => 'Facebook',
685 'fieldName' => 'im_provider_id',
689 'fieldName' => 'operator',
693 'CRM_Contact_DAO_Contact' => [
695 'fieldName' => 'prefix_id',
699 'fieldName' => 'suffix_id',
703 'fieldName' => 'gender_id',
707 'fieldName' => 'preferred_communication_method',
708 'sample' => 'Postal Mail',
711 'fieldName' => 'contact_type',
712 'sample' => 'Individual',
716 'fieldName' => 'contact_sub_type',
718 'exclude' => 'Individual',
721 'fieldName' => 'preferred_language',
722 'sample' => ['en_US' => 'English (United States)'],
726 'fieldName' => 'preferred_mail_format',
730 'fieldName' => 'communication_style_id',
731 'sample' => 'Formal',
734 'fieldName' => "custom_$customFieldId",
735 'sample' => ['foo' => 'Foo'],
739 'CRM_Batch_DAO_Batch' => [
741 'fieldName' => 'type_id',
742 'sample' => 'Membership',
745 'fieldName' => 'status_id',
746 'sample' => 'Reopened',
749 'fieldName' => 'mode_id',
750 'sample' => 'Automatic Batch',
753 'fieldName' => 'payment_instrument_id',
757 'CRM_Core_DAO_IM' => [
759 'fieldName' => 'provider_id',
763 'fieldName' => 'location_type_id',
767 'CRM_Event_DAO_Participant' => [
769 'fieldName' => 'status_id',
770 'sample' => 'Registered',
773 'fieldName' => 'role_id',
774 'sample' => 'Speaker',
777 'fieldName' => 'fee_currency',
778 'sample' => ['USD' => 'US Dollar'],
782 'fieldName' => 'campaign_id',
783 'sample' => $campaign_name,
786 'CRM_Event_DAO_Event' => [
788 'fieldName' => 'event_type_id',
789 'sample' => 'Fundraiser',
792 'fieldName' => 'participant_listing_id',
793 'sample' => 'Name and Email',
796 'fieldName' => 'payment_processor',
797 'sample' => $pp_name,
800 'fieldName' => 'financial_type_id',
801 'sample' => 'Donation',
804 'fieldName' => 'default_role_id',
805 'sample' => 'Attendee',
808 'fieldName' => 'currency',
809 'sample' => ['USD' => 'US Dollar'],
813 'fieldName' => 'campaign_id',
814 'sample' => $campaign_name,
817 'CRM_Core_DAO_Menu' => [
819 'fieldName' => 'domain_id',
820 'sample' => 'Default Domain Name',
823 'fieldName' => 'component_id',
824 'sample' => 'CiviMember',
827 'CRM_Member_DAO_Membership' => [
829 'fieldName' => 'membership_type_id',
830 'sample' => $membership_type,
833 'fieldName' => 'status_id',
837 'fieldName' => 'campaign_id',
838 'sample' => $campaign_name,
841 'CRM_Member_DAO_MembershipStatus' => [
843 'fieldName' => 'start_event',
844 'sample' => 'start date',
847 'fieldName' => 'end_event',
848 'sample' => 'member since',
851 'fieldName' => 'start_event_adjust_unit',
855 'fieldName' => 'end_event_adjust_unit',
859 'CRM_Member_DAO_MembershipType' => [
861 'fieldName' => 'visibility',
862 'sample' => 'Public',
865 'fieldName' => 'domain_id',
866 'sample' => 'Default Domain Name',
869 'fieldName' => 'financial_type_id',
870 'sample' => 'Donation',
873 'fieldName' => 'duration_unit',
874 'sample' => 'lifetime',
877 'fieldName' => 'period_type',
878 'sample' => 'Rolling',
881 'CRM_Mailing_DAO_Mailing' => [
883 'fieldName' => 'approval_status_id',
884 'sample' => 'Approved',
887 'fieldName' => 'domain_id',
888 'sample' => 'Default Domain Name',
891 'fieldName' => 'visibility',
892 'sample' => 'Public Pages',
895 'fieldName' => 'campaign_id',
896 'sample' => $campaign_name,
899 'CRM_Mailing_DAO_MailingComponent' => [
901 'fieldName' => 'component_type',
902 'sample' => 'Header',
905 'CRM_Mailing_DAO_MailingGroup' => [
907 'fieldName' => 'group_type',
908 'sample' => 'Include',
911 'CRM_Mailing_DAO_MailingJob' => [
913 'fieldName' => 'status',
914 'sample' => 'Scheduled',
917 'CRM_Mailing_Event_DAO_Bounce' => [
919 'fieldName' => 'bounce_type_id',
920 'sample' => 'Invalid',
923 'CRM_Mailing_Event_DAO_Subscribe' => [
925 'fieldName' => 'group_id',
926 'sample' => $group_name,
929 'CRM_Grant_DAO_Grant' => [
931 'fieldName' => 'status_id',
932 'sample' => 'Approved for Payment',
935 'fieldName' => 'grant_type_id',
936 'sample' => 'Emergency',
939 'fieldName' => 'currency',
940 'sample' => ['USD' => 'US Dollar'],
944 'fieldName' => 'financial_type_id',
945 'sample' => 'Donation',
948 'CRM_Contribute_DAO_Contribution' => [
950 'fieldName' => 'payment_instrument_id',
951 'sample' => 'Credit Card',
954 'fieldName' => 'financial_type_id',
955 'sample' => 'Donation',
958 'fieldName' => 'currency',
959 'sample' => ['USD' => 'US Dollar'],
963 'fieldName' => 'contribution_status_id',
964 'sample' => 'Completed',
967 'fieldName' => 'contribution_page_id',
968 'sample' => $contribution_page,
971 'fieldName' => 'campaign_id',
972 'sample' => $campaign_name,
975 'CRM_Contribute_DAO_PremiumsProduct' => [
977 'fieldName' => 'financial_type_id',
978 'sample' => 'Donation',
981 'CRM_Contribute_DAO_ContributionPage' => [
983 'fieldName' => 'payment_processor',
984 'sample' => $pp_name,
987 'fieldName' => 'financial_type_id',
988 'sample' => 'Donation',
991 'fieldName' => 'currency',
992 'sample' => ['USD' => 'US Dollar'],
996 'fieldName' => 'campaign_id',
997 'sample' => $campaign_name,
1000 'CRM_Case_DAO_Case' => [
1002 'fieldName' => 'status_id',
1003 'sample' => 'Ongoing',
1006 'fieldName' => 'case_type_id',
1007 'sample' => 'Housing Support',
1010 'CRM_Report_DAO_ReportInstance' => [
1012 'fieldName' => 'domain_id',
1013 'sample' => 'Default Domain Name',
1018 foreach ($fields as $daoName => $daoFields) {
1019 foreach ($daoFields as $field) {
1020 $message = "DAO name: '{$daoName}', field: '{$field['fieldName']}'";
1022 $optionValues = $daoName::buildOptions($field['fieldName']);
1023 $this->assertNotEmpty($optionValues, $message);
1025 // Ensure sample value is contained in the returned optionValues.
1026 if (!is_array($field['sample'])) {
1027 $this->assertContains($field['sample'], $optionValues, $message);
1029 // If sample is an array, we check keys and values
1031 foreach ($field['sample'] as $key => $value) {
1032 $this->assertArrayHasKey($key, $optionValues, $message);
1033 $this->assertEquals(CRM_Utils_Array
::value($key, $optionValues), $value, $message);
1037 // Ensure exclude value is not contained in the optionValues
1038 if (!empty($field['exclude'])) {
1039 $this->assertNotContains($field['exclude'], $optionValues, $message);
1042 // Ensure count of optionValues is not extraordinarily high.
1043 $max = CRM_Utils_Array
::value('max', $field, 20);
1044 $this->assertLessThanOrEqual($max, count($optionValues), $message);
1049 public function testContactTypes() {
1051 'Individual' => 'Individual',
1052 'Household' => 'Household',
1053 'Organization' => 'Organization',
1058 3 => 'Organization',
1060 // By default this should return an array keyed by name
1061 $result = CRM_Contact_DAO_Contact
::buildOptions('contact_type');
1062 $this->assertEquals($byName, $result);
1063 // But we can also fetch by ID
1064 $result = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'contact_type', [
1065 'keyColumn' => 'id',
1066 'labelColumn' => 'name',
1068 $this->assertEquals($byId, $result);
1069 // Make sure flip param works
1070 $result = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'contact_type', [
1071 'keyColumn' => 'id',
1072 'labelColumn' => 'name',
1075 $this->assertEquals(array_flip($byId), $result);
1078 public function testGetTaxRates() {
1079 $contact = $this->createLoggedInUser();
1080 $financialType = $this->callAPISuccess('financial_type', 'create', [
1081 'name' => 'Test taxable financial Type',
1085 $financialAccount = $this->callAPISuccess('financial_account', 'create', [
1086 'name' => 'Test Tax financial account ',
1087 'contact_id' => $contact,
1088 'financial_account_type_id' => 2,
1095 $financialTypeId = $financialType['id'];
1096 $financialAccountId = $financialAccount['id'];
1097 $financialAccountParams = [
1098 'entity_table' => 'civicrm_financial_type',
1099 'entity_id' => $financialTypeId,
1100 'account_relationship' => 10,
1101 'financial_account_id' => $financialAccountId,
1103 CRM_Financial_BAO_FinancialTypeAccount
::add($financialAccountParams);
1104 $taxRates = CRM_Core_PseudoConstant
::getTaxRates();
1105 $this->assertEquals('5.00', $taxRates[$financialType['id']]);