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(
76 $result = civicrm_api3('custom_field', 'create', $api_params);
77 $customFieldId = $result['id'];
79 // Create a Contact Group for testing.
80 $group_name = md5(microtime());
82 'title' => $group_name,
85 $result = civicrm_api3('group', 'create', $api_params);
87 // Create a PaymentProcessor for testing.
88 $pp_name = md5(microtime());
91 'payment_processor_type_id' => 10,
93 'user_name' => $pp_name,
94 'class_name' => 'Payment_Dummy',
95 'url_site' => 'https://test.com/',
96 'url_recur' => 'https://test.com/',
99 $result = civicrm_api3('payment_processor', 'create', $api_params);
101 // Create a Campaign for testing.
102 $campaign_name = md5(microtime());
104 'title' => $campaign_name,
108 $result = civicrm_api3('campaign', 'create', $api_params);
110 // Create a membership type for testing.
111 $membership_type = md5(microtime());
113 'name' => $membership_type,
115 'financial_type_id' => 1,
117 'member_of_contact_id' => 1,
118 'duration_unit' => 'day',
119 'duration_interval' => 1,
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' => array(
144 'fieldName' => 'operation',
148 'CRM_Contact_DAO_Group' => array(
150 'fieldName' => 'visibility',
151 'sample' => 'Public Pages',
154 'CRM_Contact_DAO_GroupContact' => array(
156 'fieldName' => 'group_id',
157 'sample' => $group_name,
160 'fieldName' => 'status',
164 'CRM_Contact_DAO_GroupContactCache' => array(
166 'fieldName' => 'group_id',
167 'sample' => $group_name,
170 'CRM_Contact_DAO_GroupOrganization' => array(
172 'fieldName' => 'group_id',
173 'sample' => $group_name,
176 'CRM_Contact_DAO_SubscriptionHistory' => array(
178 'fieldName' => 'group_id',
179 'sample' => $group_name,
182 'fieldName' => 'method',
186 'fieldName' => 'status',
190 'CRM_Core_DAO_Cache' => array(
192 'fieldName' => 'component_id',
193 'sample' => 'CiviMail',
196 'CRM_Contact_DAO_ACLContactCache' => array(
198 'fieldName' => 'operation',
202 'CRM_Core_DAO_Setting' => array(
204 'fieldName' => 'component_id',
205 'sample' => 'CiviMail',
208 'CRM_Core_DAO_ActionSchedule' => array(
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' => array(
232 'fieldName' => 'contact_type',
233 'sample' => 'Individual',
236 'fieldName' => 'used',
237 'sample' => 'Unsupervised',
240 'CRM_Activity_DAO_Activity' => array(
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' => array(
269 'fieldName' => 'campaign_type_id',
270 'sample' => 'Constituent Engagement',
274 'fieldName' => 'status_id',
275 'sample' => 'Completed',
279 'CRM_Campaign_DAO_Survey' => array(
281 'fieldName' => 'campaign_id',
282 'sample' => $campaign_name,
285 'fieldName' => 'activity_type_id',
286 'sample' => 'Phone Call',
290 'CRM_Campaign_DAO_CampaignGroup' => array(
292 'fieldName' => 'campaign_id',
293 'sample' => $campaign_name,
296 'fieldName' => 'group_type',
297 'sample' => 'Include',
300 'CRM_Contact_DAO_RelationshipType' => array(
302 'fieldName' => 'contact_type_a',
303 'sample' => 'Individual',
306 'fieldName' => 'contact_type_b',
307 'sample' => 'Organization',
310 'CRM_Event_DAO_ParticipantStatusType' => array(
312 'fieldName' => 'class',
313 'sample' => 'Waiting',
316 'fieldName' => 'visibility_id',
317 'sample' => 'Public',
320 'CRM_Price_DAO_LineItem' => array(
322 'fieldName' => 'financial_type_id',
323 'sample' => 'Donation',
326 'CRM_Price_DAO_PriceField' => array(
328 'fieldName' => 'html_type',
329 'sample' => 'Select',
332 'fieldName' => 'visibility_id',
333 'sample' => 'Public',
336 'CRM_Price_DAO_PriceFieldValue' => array(
338 'fieldName' => 'financial_type_id',
339 'sample' => 'Donation',
342 'CRM_Price_DAO_PriceSet' => array(
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' => array(
358 'fieldName' => 'financial_account_id',
359 'sample' => 'Member Dues',
362 'fieldName' => 'account_relationship',
363 'sample' => 'Income Account is',
366 'CRM_Financial_DAO_FinancialItem' => array(
368 'fieldName' => 'status_id',
369 'sample' => 'Partially paid',
372 'fieldName' => 'financial_account_id',
373 'sample' => 'Accounts Receivable',
376 'fieldName' => 'currency',
377 'sample' => array('USD' => 'US Dollar'),
381 'CRM_Financial_DAO_FinancialTrxn' => array(
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' => array('USD' => 'US Dollar'),
396 'fieldName' => 'payment_instrument_id',
400 'CRM_Financial_DAO_FinancialAccount' => array(
402 'fieldName' => 'financial_account_type_id',
403 'sample' => 'Cost of Sales',
406 'CRM_Financial_DAO_PaymentProcessor' => array(
408 'fieldName' => 'domain_id',
409 'sample' => 'Default Domain Name',
412 'CRM_Financial_BAO_PaymentProcessorType' => array(
414 'fieldName' => 'billing_mode',
418 'CRM_Core_DAO_UFField' => array(
420 'fieldName' => 'uf_group_id',
421 'sample' => 'Name and Address',
424 'fieldName' => 'visibility',
425 'sample' => 'Public Pages',
428 'CRM_Core_DAO_UFJoin' => array(
430 'fieldName' => 'uf_group_id',
431 'sample' => 'Name and Address',
434 'CRM_Core_DAO_UFMatch' => array(
436 'fieldName' => 'domain_id',
437 'sample' => 'Default Domain Name',
440 'CRM_Core_DAO_Job' => array(
442 'fieldName' => 'domain_id',
443 'sample' => 'Default Domain Name',
446 'fieldName' => 'run_frequency',
450 'CRM_Core_DAO_JobLog' => array(
452 'fieldName' => 'domain_id',
453 'sample' => 'Default Domain Name',
456 'CRM_Contribute_DAO_ContributionSoft' => array(
458 'fieldName' => 'currency',
459 'sample' => array('USD' => 'US Dollar'),
463 'fieldName' => 'soft_credit_type_id',
464 'sample' => 'In Honor of',
467 'CRM_Contribute_DAO_Product' => array(
469 'fieldName' => 'currency',
470 'sample' => array('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' => array(
492 'fieldName' => 'financial_type_id',
493 'sample' => 'Donation',
496 'CRM_Contribute_DAO_ContributionRecur' => array(
498 'fieldName' => 'currency',
499 'sample' => array('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' => array(
525 'fieldName' => 'currency',
526 'sample' => array('USD' => 'US Dollar'),
530 'CRM_Pledge_DAO_Pledge' => array(
532 'fieldName' => 'currency',
533 'sample' => array('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' => array(
551 'fieldName' => 'currency',
552 'sample' => array('USD' => 'US Dollar'),
556 'fieldName' => 'status_id',
557 'sample' => 'Approved',
560 'CRM_Core_DAO_CustomField' => array(
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' => array(
576 'fieldName' => 'style',
577 'sample' => 'Inline',
580 'CRM_Core_DAO_Dashboard' => array(
582 'fieldName' => 'domain_id',
583 'sample' => 'Default Domain Name',
586 'CRM_Core_DAO_Tag' => array(
588 'fieldName' => 'used_for',
589 'sample' => 'Contacts',
592 'CRM_Core_DAO_EntityTag' => array(
594 'fieldName' => 'tag_id',
595 'sample' => 'Government Entity',
598 'CRM_Core_DAO_Extension' => array(
600 'fieldName' => 'type',
601 'sample' => 'Module',
604 'CRM_Core_DAO_OptionValue' => array(
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' => array(
621 'fieldName' => 'domain_id',
622 'sample' => 'Default Domain Name',
625 'fieldName' => 'protocol',
626 'sample' => 'Localdir',
629 'CRM_Core_DAO_Managed' => array(
631 'fieldName' => 'cleanup',
632 'sample' => 'Always',
635 'CRM_Core_DAO_Mapping' => array(
637 'fieldName' => 'mapping_type_id',
638 'sample' => 'Search Builder',
641 'CRM_Core_DAO_Navigation' => array(
643 'fieldName' => 'domain_id',
644 'sample' => 'Default Domain Name',
647 'CRM_Core_DAO_Phone' => array(
649 'fieldName' => 'phone_type_id',
653 'fieldName' => 'location_type_id',
657 'CRM_Core_DAO_PrintLabel' => array(
659 'fieldName' => 'label_format_name',
660 'sample' => 'Avery 5395',
663 'fieldName' => 'label_type_id',
664 'sample' => 'Event Badge',
667 'CRM_Core_DAO_Email' => array(
669 'fieldName' => 'location_type_id',
673 'CRM_Core_DAO_Address' => array(
675 'fieldName' => 'location_type_id',
679 'CRM_Core_DAO_Website' => array(
681 'fieldName' => 'website_type_id',
682 'sample' => 'Facebook',
685 'CRM_Core_DAO_WordReplacement' => array(
687 'fieldName' => 'match_type',
688 'sample' => 'Exact Match',
691 'fieldName' => 'domain_id',
692 'sample' => 'Default Domain Name',
695 'CRM_Core_DAO_MappingField' => array(
697 'fieldName' => 'contact_type',
698 'sample' => 'Individual',
701 'fieldName' => 'website_type_id',
702 'sample' => 'Facebook',
705 'fieldName' => 'im_provider_id',
709 'fieldName' => 'operator',
713 'CRM_Contact_DAO_Contact' => array(
715 'fieldName' => 'prefix_id',
719 'fieldName' => 'suffix_id',
723 'fieldName' => 'gender_id',
727 'fieldName' => 'preferred_communication_method',
728 'sample' => 'Postal Mail',
731 'fieldName' => 'contact_type',
732 'sample' => 'Individual',
736 'fieldName' => 'contact_sub_type',
738 'exclude' => 'Individual',
741 'fieldName' => 'preferred_language',
742 'sample' => array('en_US' => 'English (United States)'),
746 'fieldName' => 'preferred_mail_format',
750 'fieldName' => 'communication_style_id',
751 'sample' => 'Formal',
754 'fieldName' => "custom_$customFieldId",
755 'sample' => array('foo' => 'Foo'),
759 'CRM_Batch_DAO_Batch' => array(
761 'fieldName' => 'type_id',
762 'sample' => 'Membership',
765 'fieldName' => 'status_id',
766 'sample' => 'Reopened',
769 'fieldName' => 'mode_id',
770 'sample' => 'Automatic Batch',
773 'fieldName' => 'payment_instrument_id',
777 'CRM_Core_DAO_IM' => array(
779 'fieldName' => 'provider_id',
783 'fieldName' => 'location_type_id',
787 'CRM_Event_DAO_Participant' => array(
789 'fieldName' => 'status_id',
790 'sample' => 'Registered',
793 'fieldName' => 'role_id',
794 'sample' => 'Speaker',
797 'fieldName' => 'fee_currency',
798 'sample' => array('USD' => 'US Dollar'),
802 'fieldName' => 'campaign_id',
803 'sample' => $campaign_name,
806 'CRM_Event_DAO_Event' => array(
808 'fieldName' => 'event_type_id',
809 'sample' => 'Fundraiser',
812 'fieldName' => 'participant_listing_id',
813 'sample' => 'Name and Email',
816 'fieldName' => 'payment_processor',
817 'sample' => $pp_name,
820 'fieldName' => 'financial_type_id',
821 'sample' => 'Donation',
824 'fieldName' => 'default_role_id',
825 'sample' => 'Attendee',
828 'fieldName' => 'currency',
829 'sample' => array('USD' => 'US Dollar'),
833 'fieldName' => 'campaign_id',
834 'sample' => $campaign_name,
837 'CRM_Core_DAO_Menu' => array(
839 'fieldName' => 'domain_id',
840 'sample' => 'Default Domain Name',
843 'fieldName' => 'component_id',
844 'sample' => 'CiviMember',
847 'CRM_Member_DAO_Membership' => array(
849 'fieldName' => 'membership_type_id',
850 'sample' => $membership_type,
853 'fieldName' => 'status_id',
857 'fieldName' => 'campaign_id',
858 'sample' => $campaign_name,
861 'CRM_Member_DAO_MembershipStatus' => array(
863 'fieldName' => 'start_event',
864 'sample' => 'start date',
867 'fieldName' => 'end_event',
868 'sample' => 'member since',
871 'fieldName' => 'start_event_adjust_unit',
875 'fieldName' => 'end_event_adjust_unit',
879 'CRM_Member_DAO_MembershipType' => array(
881 'fieldName' => 'visibility',
882 'sample' => 'Public',
885 'fieldName' => 'domain_id',
886 'sample' => 'Default Domain Name',
889 'fieldName' => 'financial_type_id',
890 'sample' => 'Donation',
893 'fieldName' => 'duration_unit',
894 'sample' => 'lifetime',
897 'fieldName' => 'period_type',
898 'sample' => 'Rolling',
901 'CRM_Mailing_DAO_Mailing' => array(
903 'fieldName' => 'approval_status_id',
904 'sample' => 'Approved',
907 'fieldName' => 'domain_id',
908 'sample' => 'Default Domain Name',
911 'fieldName' => 'visibility',
912 'sample' => 'Public Pages',
915 'fieldName' => 'campaign_id',
916 'sample' => $campaign_name,
919 'CRM_Mailing_DAO_Component' => array(
921 'fieldName' => 'component_type',
922 'sample' => 'Header',
925 'CRM_Mailing_DAO_MailingGroup' => array(
927 'fieldName' => 'group_type',
928 'sample' => 'Include',
931 'CRM_Mailing_DAO_MailingJob' => array(
933 'fieldName' => 'status',
934 'sample' => 'Scheduled',
937 'CRM_Mailing_Event_DAO_Bounce' => array(
939 'fieldName' => 'bounce_type_id',
940 'sample' => 'Invalid',
943 'CRM_Mailing_Event_DAO_Subscribe' => array(
945 'fieldName' => 'group_id',
946 'sample' => $group_name,
949 'CRM_Grant_DAO_Grant' => array(
951 'fieldName' => 'status_id',
952 'sample' => 'Approved for Payment',
955 'fieldName' => 'grant_type_id',
956 'sample' => 'Emergency',
959 'fieldName' => 'currency',
960 'sample' => array('USD' => 'US Dollar'),
964 'fieldName' => 'financial_type_id',
965 'sample' => 'Donation',
968 'CRM_Contribute_DAO_Contribution' => array(
970 'fieldName' => 'payment_instrument_id',
971 'sample' => 'Credit Card',
974 'fieldName' => 'financial_type_id',
975 'sample' => 'Donation',
978 'fieldName' => 'currency',
979 'sample' => array('USD' => 'US Dollar'),
983 'fieldName' => 'contribution_status_id',
984 'sample' => 'Completed',
987 'fieldName' => 'contribution_page_id',
988 'sample' => $contribution_page,
991 'fieldName' => 'campaign_id',
992 'sample' => $campaign_name,
995 'CRM_Contribute_DAO_PremiumsProduct' => array(
997 'fieldName' => 'financial_type_id',
998 'sample' => 'Donation',
1001 'CRM_Contribute_DAO_ContributionPage' => array(
1003 'fieldName' => 'payment_processor',
1004 'sample' => $pp_name,
1007 'fieldName' => 'financial_type_id',
1008 'sample' => 'Donation',
1011 'fieldName' => 'currency',
1012 'sample' => array('USD' => 'US Dollar'),
1016 'fieldName' => 'campaign_id',
1017 'sample' => $campaign_name,
1020 'CRM_Case_DAO_Case' => array(
1022 'fieldName' => 'status_id',
1023 'sample' => 'Ongoing',
1026 'fieldName' => 'case_type_id',
1027 'sample' => 'Housing Support',
1030 'CRM_Report_DAO_ReportInstance' => array(
1032 'fieldName' => 'domain_id',
1033 'sample' => 'Default Domain Name',
1038 foreach ($fields as $daoName => $daoFields) {
1039 foreach ($daoFields as $field) {
1040 $message = "DAO name: '{$daoName}', field: '{$field['fieldName']}'";
1042 $optionValues = $daoName::buildOptions($field['fieldName']);
1043 $this->assertNotEmpty($optionValues, $message);
1045 // Ensure sample value is contained in the returned optionValues.
1046 if (!is_array($field['sample'])) {
1047 $this->assertContains($field['sample'], $optionValues, $message);
1049 // If sample is an array, we check keys and values
1051 foreach ($field['sample'] as $key => $value) {
1052 $this->assertArrayHasKey($key, $optionValues, $message);
1053 $this->assertEquals(CRM_Utils_Array
::value($key, $optionValues), $value, $message);
1057 // Ensure exclude value is not contained in the optionValues
1058 if (!empty($field['exclude'])) {
1059 $this->assertNotContains($field['exclude'], $optionValues, $message);
1062 // Ensure count of optionValues is not extraordinarily high.
1063 $max = CRM_Utils_Array
::value('max', $field, 20);
1064 $this->assertLessThanOrEqual($max, count($optionValues), $message);
1069 public function testContactTypes() {
1071 'Individual' => 'Individual',
1072 'Household' => 'Household',
1073 'Organization' => 'Organization',
1078 3 => 'Organization',
1080 // By default this should return an array keyed by name
1081 $result = CRM_Contact_DAO_Contact
::buildOptions('contact_type');
1082 $this->assertEquals($byName, $result);
1083 // But we can also fetch by ID
1084 $result = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'contact_type', array(
1085 'keyColumn' => 'id',
1086 'labelColumn' => 'name',
1088 $this->assertEquals($byId, $result);
1089 // Make sure flip param works
1090 $result = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'contact_type', array(
1091 'keyColumn' => 'id',
1092 'labelColumn' => 'name',
1095 $this->assertEquals(array_flip($byId), $result);