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(): void
{
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 membership type for testing.
85 $membership_type = md5(microtime());
87 'name' => $membership_type,
89 'financial_type_id' => 1,
91 'member_of_contact_id' => 1,
92 'duration_unit' => 'day',
93 'duration_interval' => 1,
94 'period_type' => 'rolling',
96 $result = civicrm_api3('membership_type', 'create', $api_params);
98 // Create a contribution page for testing.
99 $contribution_page = md5(microtime());
101 'title' => $contribution_page,
103 'financial_type_id' => 1,
105 $result = civicrm_api3('contribution_page', 'create', $api_params);
108 * daoName/field combinations to test
109 * Format: array[DAO Name] = $properties, where properties is an array whose
110 * named members can be:
111 * - fieldName: the SQL column name within the DAO table.
112 * - sample: Any one value which is expected in the list of option values.
113 * - exclude: Any one value which should not be in the list.
114 * - max: integer (default = 20) maximum number of option values expected.
117 'CRM_ACL_DAO_ACL' => [
119 'fieldName' => 'operation',
123 'CRM_Contact_DAO_Group' => [
125 'fieldName' => 'visibility',
126 'sample' => 'Public Pages',
129 'CRM_Contact_DAO_GroupContact' => [
131 'fieldName' => 'group_id',
132 'sample' => $group_name,
135 'fieldName' => 'status',
139 'CRM_Contact_DAO_GroupContactCache' => [
141 'fieldName' => 'group_id',
142 'sample' => $group_name,
145 'CRM_Contact_DAO_GroupOrganization' => [
147 'fieldName' => 'group_id',
148 'sample' => $group_name,
151 'CRM_Contact_DAO_SubscriptionHistory' => [
153 'fieldName' => 'group_id',
154 'sample' => $group_name,
157 'fieldName' => 'method',
161 'fieldName' => 'status',
165 'CRM_Core_DAO_Cache' => [
167 'fieldName' => 'component_id',
168 'sample' => 'CiviMail',
171 'CRM_Contact_DAO_ACLContactCache' => [
173 'fieldName' => 'operation',
177 'CRM_Core_DAO_Setting' => [
179 'fieldName' => 'component_id',
180 'sample' => 'CiviMail',
183 'CRM_Core_DAO_ActionSchedule' => [
185 'fieldName' => 'group_id',
186 'sample' => $group_name,
189 'fieldName' => 'start_action_unit',
193 'fieldName' => 'repetition_frequency_unit',
197 'fieldName' => 'end_frequency_unit',
201 'fieldName' => 'mode',
205 'CRM_Dedupe_DAO_DedupeRuleGroup' => [
207 'fieldName' => 'contact_type',
208 'sample' => 'Individual',
211 'fieldName' => 'used',
212 'sample' => 'Unsupervised',
215 'CRM_Activity_DAO_Activity' => [
217 'fieldName' => 'activity_type_id',
222 'fieldName' => 'status_id',
223 'sample' => 'Scheduled',
226 'fieldName' => 'priority_id',
227 'sample' => 'Urgent',
230 'fieldName' => 'engagement_level',
234 'fieldName' => 'medium_id',
238 'CRM_Campaign_DAO_Campaign' => [
240 'fieldName' => 'campaign_type_id',
241 'sample' => 'Constituent Engagement',
245 'fieldName' => 'status_id',
246 'sample' => 'Completed',
250 'CRM_Campaign_DAO_Survey' => [
252 'fieldName' => 'activity_type_id',
253 'sample' => 'Phone Call',
257 'CRM_Campaign_DAO_CampaignGroup' => [
259 'fieldName' => 'group_type',
260 'sample' => 'Include',
263 'CRM_Contact_DAO_RelationshipType' => [
265 'fieldName' => 'contact_type_a',
266 'sample' => 'Individual',
269 'fieldName' => 'contact_type_b',
270 'sample' => 'Organization',
273 'CRM_Event_DAO_ParticipantStatusType' => [
275 'fieldName' => 'class',
276 'sample' => 'Waiting',
279 'fieldName' => 'visibility_id',
280 'sample' => 'Public',
283 'CRM_Price_DAO_LineItem' => [
285 'fieldName' => 'financial_type_id',
286 'sample' => 'Donation',
289 'CRM_Price_DAO_PriceField' => [
291 'fieldName' => 'html_type',
292 'sample' => 'Select',
295 'fieldName' => 'visibility_id',
296 'sample' => 'Public',
299 'CRM_Price_DAO_PriceFieldValue' => [
301 'fieldName' => 'financial_type_id',
302 'sample' => 'Donation',
305 'CRM_Price_DAO_PriceSet' => [
307 'fieldName' => 'domain_id',
308 'sample' => 'Default Domain Name',
311 'fieldName' => 'extends',
312 'sample' => 'CiviEvent',
315 'fieldName' => 'financial_type_id',
316 'sample' => 'Donation',
319 'CRM_Financial_DAO_EntityFinancialAccount' => [
321 'fieldName' => 'financial_account_id',
322 'sample' => 'Member Dues',
325 'fieldName' => 'account_relationship',
326 'sample' => 'Income Account is',
329 'CRM_Financial_DAO_FinancialItem' => [
331 'fieldName' => 'status_id',
332 'sample' => 'Partially paid',
335 'fieldName' => 'financial_account_id',
336 'sample' => 'Accounts Receivable',
339 'fieldName' => 'currency',
340 'sample' => ['USD' => 'US Dollar'],
344 'CRM_Financial_DAO_FinancialTrxn' => [
346 'fieldName' => 'from_financial_account_id',
347 'sample' => 'Accounts Receivable',
350 'fieldName' => 'to_financial_account_id',
351 'sample' => 'Accounts Receivable',
354 'fieldName' => 'currency',
355 'sample' => ['USD' => 'US Dollar'],
359 'fieldName' => 'payment_instrument_id',
363 'CRM_Financial_DAO_FinancialAccount' => [
365 'fieldName' => 'financial_account_type_id',
366 'sample' => 'Cost of Sales',
369 'CRM_Financial_DAO_PaymentProcessor' => [
371 'fieldName' => 'domain_id',
372 'sample' => 'Default Domain Name',
375 'CRM_Financial_BAO_PaymentProcessorType' => [
377 'fieldName' => 'billing_mode',
381 'CRM_Core_DAO_UFField' => [
383 'fieldName' => 'uf_group_id',
384 'sample' => 'Name and Address',
387 'fieldName' => 'visibility',
388 'sample' => 'Expose Publicly',
391 'CRM_Core_DAO_UFJoin' => [
393 'fieldName' => 'uf_group_id',
394 'sample' => 'Name and Address',
397 'CRM_Core_DAO_UFMatch' => [
399 'fieldName' => 'domain_id',
400 'sample' => 'Default Domain Name',
403 'CRM_Core_DAO_Job' => [
405 'fieldName' => 'domain_id',
406 'sample' => 'Default Domain Name',
409 'fieldName' => 'run_frequency',
413 'CRM_Core_DAO_JobLog' => [
415 'fieldName' => 'domain_id',
416 'sample' => 'Default Domain Name',
419 'CRM_Contribute_DAO_ContributionSoft' => [
421 'fieldName' => 'currency',
422 'sample' => ['USD' => 'US Dollar'],
426 'fieldName' => 'soft_credit_type_id',
427 'sample' => 'In Honor of',
430 'CRM_Contribute_DAO_Product' => [
432 'fieldName' => 'currency',
433 'sample' => ['USD' => 'US Dollar'],
437 'fieldName' => 'financial_type_id',
438 'sample' => 'Donation',
441 'fieldName' => 'period_type',
442 'sample' => 'Rolling',
445 'fieldName' => 'duration_unit',
449 'fieldName' => 'frequency_unit',
453 'CRM_Contribute_DAO_ContributionProduct' => [
455 'fieldName' => 'financial_type_id',
456 'sample' => 'Donation',
459 'CRM_Contribute_DAO_ContributionRecur' => [
461 'fieldName' => 'currency',
462 'sample' => ['USD' => 'US Dollar'],
466 'fieldName' => 'frequency_unit',
470 'fieldName' => 'contribution_status_id',
471 'sample' => 'Completed',
474 'fieldName' => 'financial_type_id',
475 'sample' => 'Donation',
478 'fieldName' => 'payment_instrument_id',
482 'CRM_Pledge_DAO_PledgePayment' => [
484 'fieldName' => 'currency',
485 'sample' => ['USD' => 'US Dollar'],
489 'CRM_Pledge_DAO_Pledge' => [
491 'fieldName' => 'currency',
492 'sample' => ['USD' => 'US Dollar'],
496 'fieldName' => 'financial_type_id',
497 'sample' => 'Donation',
500 'fieldName' => 'frequency_unit',
504 'CRM_PCP_DAO_PCP' => [
506 'fieldName' => 'currency',
507 'sample' => ['USD' => 'US Dollar'],
511 'fieldName' => 'status_id',
512 'sample' => 'Approved',
515 'CRM_Core_DAO_CustomField' => [
517 'fieldName' => 'custom_group_id',
518 'sample' => $custom_group_name,
521 'fieldName' => 'data_type',
522 'sample' => 'Alphanumeric',
525 'fieldName' => 'html_type',
526 'sample' => 'Select Date',
529 'CRM_Core_DAO_CustomGroup' => [
531 'fieldName' => 'style',
532 'sample' => 'Inline',
535 'CRM_Core_DAO_Dashboard' => [
537 'fieldName' => 'domain_id',
538 'sample' => 'Default Domain Name',
541 'CRM_Core_DAO_Tag' => [
543 'fieldName' => 'used_for',
544 'sample' => 'Contacts',
547 'CRM_Core_DAO_EntityTag' => [
549 'fieldName' => 'tag_id',
550 'sample' => 'Government Entity',
553 'CRM_Core_DAO_Extension' => [
555 'fieldName' => 'type',
556 'sample' => 'Module',
559 'CRM_Core_DAO_OptionValue' => [
561 'fieldName' => 'option_group_id',
562 'sample' => 'Gender',
566 'fieldName' => 'component_id',
567 'sample' => 'CiviContribute',
570 'fieldName' => 'domain_id',
571 'sample' => 'Default Domain Name',
574 'CRM_Core_DAO_MailSettings' => [
576 'fieldName' => 'domain_id',
577 'sample' => 'Default Domain Name',
580 'fieldName' => 'protocol',
581 'sample' => 'Localdir',
584 'CRM_Core_DAO_Managed' => [
586 'fieldName' => 'cleanup',
587 'sample' => 'Always',
590 'CRM_Core_DAO_Mapping' => [
592 'fieldName' => 'mapping_type_id',
593 'sample' => 'Search Builder',
596 'CRM_Core_DAO_Navigation' => [
598 'fieldName' => 'domain_id',
599 'sample' => 'Default Domain Name',
602 'CRM_Core_DAO_Phone' => [
604 'fieldName' => 'phone_type_id',
608 'fieldName' => 'location_type_id',
612 'CRM_Core_DAO_PrintLabel' => [
614 'fieldName' => 'label_format_name',
615 'sample' => 'Avery 5395',
618 'fieldName' => 'label_type_id',
619 'sample' => 'Event Badge',
622 'CRM_Core_DAO_Email' => [
624 'fieldName' => 'location_type_id',
628 'CRM_Core_DAO_Address' => [
630 'fieldName' => 'location_type_id',
634 'CRM_Core_DAO_Website' => [
636 'fieldName' => 'website_type_id',
637 'sample' => 'Facebook',
640 'CRM_Core_DAO_WordReplacement' => [
642 'fieldName' => 'match_type',
643 'sample' => 'Exact Match',
646 'fieldName' => 'domain_id',
647 'sample' => 'Default Domain Name',
650 'CRM_Core_DAO_MappingField' => [
652 'fieldName' => 'website_type_id',
653 'sample' => 'Facebook',
656 'fieldName' => 'im_provider_id',
660 'fieldName' => 'operator',
664 'CRM_Contact_DAO_Contact' => [
666 'fieldName' => 'prefix_id',
670 'fieldName' => 'suffix_id',
674 'fieldName' => 'gender_id',
678 'fieldName' => 'preferred_communication_method',
679 'sample' => 'Postal Mail',
682 'fieldName' => 'contact_type',
683 'sample' => 'Individual',
687 'fieldName' => 'contact_sub_type',
689 'exclude' => 'Individual',
692 'fieldName' => 'preferred_language',
693 'sample' => ['en_US' => 'English (United States)'],
697 'fieldName' => 'preferred_mail_format',
701 'fieldName' => 'communication_style_id',
702 'sample' => 'Formal',
705 'fieldName' => "custom_$customFieldId",
706 'sample' => ['foo' => 'Foo'],
710 'CRM_Batch_DAO_Batch' => [
712 'fieldName' => 'type_id',
713 'sample' => 'Membership',
716 'fieldName' => 'status_id',
717 'sample' => 'Reopened',
720 'fieldName' => 'mode_id',
721 'sample' => 'Automatic Batch',
724 'fieldName' => 'payment_instrument_id',
728 'CRM_Core_DAO_IM' => [
730 'fieldName' => 'provider_id',
734 'fieldName' => 'location_type_id',
738 'CRM_Event_DAO_Participant' => [
740 'fieldName' => 'status_id',
741 'sample' => 'Registered',
744 'fieldName' => 'role_id',
745 'sample' => 'Speaker',
748 'fieldName' => 'fee_currency',
749 'sample' => ['USD' => 'US Dollar'],
753 'CRM_Event_DAO_Event' => [
755 'fieldName' => 'event_type_id',
756 'sample' => 'Fundraiser',
759 'fieldName' => 'participant_listing_id',
760 'sample' => 'Name and Email',
763 'fieldName' => 'payment_processor',
764 'sample' => $pp_name,
767 'fieldName' => 'financial_type_id',
768 'sample' => 'Donation',
771 'fieldName' => 'default_role_id',
772 'sample' => 'Attendee',
775 'fieldName' => 'currency',
776 'sample' => ['USD' => 'US Dollar'],
780 'CRM_Core_DAO_Menu' => [
782 'fieldName' => 'domain_id',
783 'sample' => 'Default Domain Name',
786 'fieldName' => 'component_id',
787 'sample' => 'CiviMember',
790 'CRM_Member_DAO_Membership' => [
792 'fieldName' => 'membership_type_id',
793 'sample' => $membership_type,
796 'fieldName' => 'status_id',
800 'CRM_Member_DAO_MembershipStatus' => [
802 'fieldName' => 'start_event',
803 'sample' => 'start date',
806 'fieldName' => 'end_event',
807 'sample' => 'member since',
810 'fieldName' => 'start_event_adjust_unit',
814 'fieldName' => 'end_event_adjust_unit',
818 'CRM_Member_DAO_MembershipType' => [
820 'fieldName' => 'visibility',
821 'sample' => 'Public',
824 'fieldName' => 'domain_id',
825 'sample' => 'Default Domain Name',
828 'fieldName' => 'financial_type_id',
829 'sample' => 'Donation',
832 'fieldName' => 'duration_unit',
833 'sample' => 'lifetime',
836 'fieldName' => 'period_type',
837 'sample' => 'Rolling',
840 'CRM_Mailing_DAO_Mailing' => [
842 'fieldName' => 'approval_status_id',
843 'sample' => 'Approved',
846 'fieldName' => 'domain_id',
847 'sample' => 'Default Domain Name',
850 'fieldName' => 'visibility',
851 'sample' => 'Public Pages',
854 'CRM_Mailing_DAO_MailingComponent' => [
856 'fieldName' => 'component_type',
857 'sample' => 'Header',
860 'CRM_Mailing_DAO_MailingGroup' => [
862 'fieldName' => 'group_type',
863 'sample' => 'Include',
866 'CRM_Mailing_DAO_MailingJob' => [
868 'fieldName' => 'status',
869 'sample' => 'Scheduled',
872 'CRM_Mailing_Event_DAO_Bounce' => [
874 'fieldName' => 'bounce_type_id',
875 'sample' => 'Invalid',
878 'CRM_Mailing_Event_DAO_Subscribe' => [
880 'fieldName' => 'group_id',
881 'sample' => $group_name,
884 'CRM_Grant_DAO_Grant' => [
886 'fieldName' => 'status_id',
887 'sample' => 'Approved for Payment',
890 'fieldName' => 'grant_type_id',
891 'sample' => 'Emergency',
894 'fieldName' => 'currency',
895 'sample' => ['USD' => 'US Dollar'],
899 'fieldName' => 'financial_type_id',
900 'sample' => 'Donation',
903 'CRM_Contribute_DAO_Contribution' => [
905 'fieldName' => 'payment_instrument_id',
906 'sample' => 'Credit Card',
909 'fieldName' => 'financial_type_id',
910 'sample' => 'Donation',
913 'fieldName' => 'currency',
914 'sample' => ['USD' => 'US Dollar'],
918 'fieldName' => 'contribution_status_id',
919 'sample' => 'Completed',
922 'fieldName' => 'contribution_page_id',
923 'sample' => $contribution_page,
926 'CRM_Contribute_DAO_PremiumsProduct' => [
928 'fieldName' => 'financial_type_id',
929 'sample' => 'Donation',
932 'CRM_Contribute_DAO_ContributionPage' => [
934 'fieldName' => 'payment_processor',
935 'sample' => $pp_name,
938 'fieldName' => 'financial_type_id',
939 'sample' => 'Donation',
942 'fieldName' => 'currency',
943 'sample' => ['USD' => 'US Dollar'],
947 'CRM_Case_DAO_Case' => [
949 'fieldName' => 'status_id',
950 'sample' => 'Ongoing',
953 'fieldName' => 'case_type_id',
954 'sample' => 'Housing Support',
957 'CRM_Report_DAO_ReportInstance' => [
959 'fieldName' => 'domain_id',
960 'sample' => 'Default Domain Name',
965 foreach ($fields as $daoName => $daoFields) {
966 foreach ($daoFields as $field) {
967 $message = "DAO name: '{$daoName}', field: '{$field['fieldName']}'";
969 $optionValues = $daoName::buildOptions($field['fieldName']);
970 $this->assertNotEmpty($optionValues, $message);
972 // Ensure sample value is contained in the returned optionValues.
973 if (!is_array($field['sample'])) {
974 $this->assertContains($field['sample'], $optionValues, $message);
976 // If sample is an array, we check keys and values
978 foreach ($field['sample'] as $key => $value) {
979 $this->assertArrayHasKey($key, $optionValues, $message);
980 $this->assertEquals(CRM_Utils_Array
::value($key, $optionValues), $value, $message);
984 // Ensure exclude value is not contained in the optionValues
985 if (!empty($field['exclude'])) {
986 $this->assertNotContains($field['exclude'], $optionValues, $message);
989 // Ensure count of optionValues is not extraordinarily high.
990 $max = CRM_Utils_Array
::value('max', $field, 20);
991 $this->assertLessThanOrEqual($max, count($optionValues), $message);
996 public function testContactTypes() {
998 'Individual' => 'Individual',
999 'Household' => 'Household',
1000 'Organization' => 'Organization',
1005 3 => 'Organization',
1007 // By default this should return an array keyed by name
1008 $result = CRM_Contact_DAO_Contact
::buildOptions('contact_type');
1009 $this->assertEquals($byName, $result);
1010 // But we can also fetch by ID
1011 $result = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'contact_type', [
1012 'keyColumn' => 'id',
1013 'labelColumn' => 'name',
1015 $this->assertEquals($byId, $result);
1016 // Make sure flip param works
1017 $result = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'contact_type', [
1018 'keyColumn' => 'id',
1019 'labelColumn' => 'name',
1022 $this->assertEquals(array_flip($byId), $result);
1025 public function testGetTaxRates() {
1026 $contact = $this->createLoggedInUser();
1027 $financialType = $this->callAPISuccess('financial_type', 'create', [
1028 'name' => 'Test taxable financial Type',
1032 $financialAccount = $this->callAPISuccess('financial_account', 'create', [
1033 'name' => 'Test Tax financial account ',
1034 'contact_id' => $contact,
1035 'financial_account_type_id' => 2,
1042 $financialTypeId = $financialType['id'];
1043 $financialAccountId = $financialAccount['id'];
1044 $financialAccountParams = [
1045 'entity_table' => 'civicrm_financial_type',
1046 'entity_id' => $financialTypeId,
1047 'account_relationship' => 10,
1048 'financial_account_id' => $financialAccountId,
1050 CRM_Financial_BAO_FinancialTypeAccount
::add($financialAccountParams);
1051 $taxRates = CRM_Core_PseudoConstant
::getTaxRates();
1052 $this->assertEquals('5.00', round($taxRates[$financialType['id']], 2));