3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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',
66 'option_values' => array(
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,
120 $result = civicrm_api3('membership_type', 'create', $api_params);
122 // Create a contribution page for testing.
123 $contribution_page = md5(microtime());
125 'title' => $contribution_page,
127 'financial_type_id' => 1,
129 $result = civicrm_api3('contribution_page', 'create', $api_params);
132 * daoName/field combinations to test
133 * Format: array[DAO Name] = $properties, where properties is an array whose
134 * named members can be:
135 * - fieldName: the SQL column name within the DAO table.
136 * - sample: Any one value which is expected in the list of option values.
137 * - exclude: Any one value which should not be in the list.
138 * - max: integer (default = 20) maximum number of option values expected.
141 'CRM_ACL_DAO_ACL' => array(
143 'fieldName' => 'operation',
147 'CRM_Contact_DAO_Group' => array(
149 'fieldName' => 'visibility',
150 'sample' => 'Public Pages',
153 'CRM_Contact_DAO_GroupContact' => array(
155 'fieldName' => 'group_id',
156 'sample' => $group_name,
159 'fieldName' => 'status',
163 'CRM_Contact_DAO_GroupContactCache' => array(
165 'fieldName' => 'group_id',
166 'sample' => $group_name,
169 'CRM_Contact_DAO_GroupOrganization' => array(
171 'fieldName' => 'group_id',
172 'sample' => $group_name,
175 'CRM_Contact_DAO_SubscriptionHistory' => array(
177 'fieldName' => 'group_id',
178 'sample' => $group_name,
181 'fieldName' => 'method',
185 'fieldName' => 'status',
189 'CRM_Core_DAO_Cache' => array(
191 'fieldName' => 'component_id',
192 'sample' => 'CiviMail',
195 'CRM_Contact_DAO_ACLContactCache' => array(
197 'fieldName' => 'operation',
201 'CRM_Core_DAO_Setting' => array(
203 'fieldName' => 'component_id',
204 'sample' => 'CiviMail',
207 'CRM_Core_DAO_ActionSchedule' => array(
209 'fieldName' => 'group_id',
210 'sample' => $group_name,
213 'fieldName' => 'start_action_unit',
217 'fieldName' => 'repetition_frequency_unit',
221 'fieldName' => 'end_frequency_unit',
225 'fieldName' => 'mode',
229 'CRM_Dedupe_DAO_RuleGroup' => array(
231 'fieldName' => 'contact_type',
232 'sample' => 'Individual',
235 'fieldName' => 'used',
236 'sample' => 'Unsupervised',
239 'CRM_Activity_DAO_Activity' => array(
241 'fieldName' => 'activity_type_id',
246 'fieldName' => 'status_id',
247 'sample' => 'Scheduled',
250 'fieldName' => 'priority_id',
251 'sample' => 'Urgent',
254 'fieldName' => 'engagement_level',
258 'fieldName' => 'medium_id',
262 'fieldName' => 'campaign_id',
263 'sample' => $campaign_name,
266 'CRM_Campaign_DAO_Campaign' => array(
268 'fieldName' => 'campaign_type_id',
269 'sample' => 'Constituent Engagement',
273 'fieldName' => 'status_id',
274 'sample' => 'Completed',
278 'CRM_Campaign_DAO_Survey' => array(
280 'fieldName' => 'campaign_id',
281 'sample' => $campaign_name,
284 'fieldName' => 'activity_type_id',
285 'sample' => 'Phone Call',
289 'CRM_Campaign_DAO_CampaignGroup' => array(
291 'fieldName' => 'campaign_id',
292 'sample' => $campaign_name,
295 'fieldName' => 'group_type',
296 'sample' => 'Include',
299 'CRM_Contact_DAO_RelationshipType' => array(
301 'fieldName' => 'contact_type_a',
302 'sample' => 'Individual',
305 'fieldName' => 'contact_type_b',
306 'sample' => 'Organization',
309 'CRM_Event_DAO_ParticipantStatusType' => array(
311 'fieldName' => 'class',
312 'sample' => 'Waiting',
315 'fieldName' => 'visibility_id',
316 'sample' => 'Public',
319 'CRM_Price_DAO_LineItem' => array(
321 'fieldName' => 'financial_type_id',
322 'sample' => 'Donation',
325 'CRM_Price_DAO_PriceField' => array(
327 'fieldName' => 'html_type',
328 'sample' => 'Select',
331 'fieldName' => 'visibility_id',
332 'sample' => 'Public',
335 'CRM_Price_DAO_PriceFieldValue' => array(
337 'fieldName' => 'financial_type_id',
338 'sample' => 'Donation',
341 'CRM_Price_DAO_PriceSet' => array(
343 'fieldName' => 'domain_id',
344 'sample' => 'Default Domain Name',
347 'fieldName' => 'extends',
348 'sample' => 'CiviEvent',
351 'fieldName' => 'financial_type_id',
352 'sample' => 'Donation',
355 'CRM_Financial_DAO_EntityFinancialAccount' => array(
357 'fieldName' => 'financial_account_id',
358 'sample' => 'Member Dues',
361 'fieldName' => 'account_relationship',
362 'sample' => 'Income Account is',
365 'CRM_Financial_DAO_FinancialItem' => array(
367 'fieldName' => 'status_id',
368 'sample' => 'Partially paid',
371 'fieldName' => 'financial_account_id',
372 'sample' => 'Accounts Receivable',
375 'fieldName' => 'currency',
376 'sample' => array('USD' => 'US Dollar'),
380 'CRM_Financial_DAO_FinancialTrxn' => array(
382 'fieldName' => 'from_financial_account_id',
383 'sample' => 'Accounts Receivable',
386 'fieldName' => 'to_financial_account_id',
387 'sample' => 'Accounts Receivable',
390 'fieldName' => 'currency',
391 'sample' => array('USD' => 'US Dollar'),
395 'fieldName' => 'payment_instrument_id',
399 'CRM_Financial_DAO_FinancialAccount' => array(
401 'fieldName' => 'financial_account_type_id',
402 'sample' => 'Cost of Sales',
405 'CRM_Financial_DAO_PaymentProcessor' => array(
407 'fieldName' => 'domain_id',
408 'sample' => 'Default Domain Name',
411 'CRM_Financial_BAO_PaymentProcessorType' => array(
413 'fieldName' => 'billing_mode',
417 'CRM_Core_DAO_UFField' => array(
419 'fieldName' => 'uf_group_id',
420 'sample' => 'Name and Address',
423 'fieldName' => 'visibility',
424 'sample' => 'Expose Publicly',
427 'CRM_Core_DAO_UFJoin' => array(
429 'fieldName' => 'uf_group_id',
430 'sample' => 'Name and Address',
433 'CRM_Core_DAO_UFMatch' => array(
435 'fieldName' => 'domain_id',
436 'sample' => 'Default Domain Name',
439 'CRM_Core_DAO_Job' => array(
441 'fieldName' => 'domain_id',
442 'sample' => 'Default Domain Name',
445 'fieldName' => 'run_frequency',
449 'CRM_Core_DAO_JobLog' => array(
451 'fieldName' => 'domain_id',
452 'sample' => 'Default Domain Name',
455 'CRM_Contribute_DAO_ContributionSoft' => array(
457 'fieldName' => 'currency',
458 'sample' => array('USD' => 'US Dollar'),
462 'fieldName' => 'soft_credit_type_id',
463 'sample' => 'In Honor of',
466 'CRM_Contribute_DAO_Product' => array(
468 'fieldName' => 'currency',
469 'sample' => array('USD' => 'US Dollar'),
473 'fieldName' => 'financial_type_id',
474 'sample' => 'Donation',
477 'fieldName' => 'period_type',
478 'sample' => 'Rolling',
481 'fieldName' => 'duration_unit',
485 'fieldName' => 'frequency_unit',
489 'CRM_Contribute_DAO_ContributionProduct' => array(
491 'fieldName' => 'financial_type_id',
492 'sample' => 'Donation',
495 'CRM_Contribute_DAO_ContributionRecur' => array(
497 'fieldName' => 'currency',
498 'sample' => array('USD' => 'US Dollar'),
502 'fieldName' => 'frequency_unit',
506 'fieldName' => 'contribution_status_id',
507 'sample' => 'Completed',
510 'fieldName' => 'financial_type_id',
511 'sample' => 'Donation',
514 'fieldName' => 'payment_instrument_id',
518 'fieldName' => 'campaign_id',
519 'sample' => $campaign_name,
522 'CRM_Pledge_DAO_PledgePayment' => array(
524 'fieldName' => 'currency',
525 'sample' => array('USD' => 'US Dollar'),
529 'CRM_Pledge_DAO_Pledge' => array(
531 'fieldName' => 'currency',
532 'sample' => array('USD' => 'US Dollar'),
536 'fieldName' => 'financial_type_id',
537 'sample' => 'Donation',
540 'fieldName' => 'frequency_unit',
544 'fieldName' => 'campaign_id',
545 'sample' => $campaign_name,
548 'CRM_PCP_DAO_PCP' => array(
550 'fieldName' => 'currency',
551 'sample' => array('USD' => 'US Dollar'),
555 'fieldName' => 'status_id',
556 'sample' => 'Approved',
559 'CRM_Core_DAO_CustomField' => array(
561 'fieldName' => 'custom_group_id',
562 'sample' => $custom_group_name,
565 'fieldName' => 'data_type',
566 'sample' => 'Alphanumeric',
569 'fieldName' => 'html_type',
570 'sample' => 'Select Date',
573 'CRM_Core_DAO_CustomGroup' => array(
575 'fieldName' => 'style',
576 'sample' => 'Inline',
579 'CRM_Core_DAO_Dashboard' => array(
581 'fieldName' => 'domain_id',
582 'sample' => 'Default Domain Name',
585 'CRM_Core_DAO_Tag' => array(
587 'fieldName' => 'used_for',
588 'sample' => 'Contacts',
591 'CRM_Core_DAO_EntityTag' => array(
593 'fieldName' => 'tag_id',
594 'sample' => 'Government Entity',
597 'CRM_Core_DAO_Extension' => array(
599 'fieldName' => 'type',
600 'sample' => 'Module',
603 'CRM_Core_DAO_OptionValue' => array(
605 'fieldName' => 'option_group_id',
606 'sample' => 'gender',
610 'fieldName' => 'component_id',
611 'sample' => 'CiviContribute',
614 'fieldName' => 'domain_id',
615 'sample' => 'Default Domain Name',
618 'CRM_Core_DAO_MailSettings' => array(
620 'fieldName' => 'domain_id',
621 'sample' => 'Default Domain Name',
624 'fieldName' => 'protocol',
625 'sample' => 'Localdir',
628 'CRM_Core_DAO_Managed' => array(
630 'fieldName' => 'cleanup',
631 'sample' => 'Always',
634 'CRM_Core_DAO_Mapping' => array(
636 'fieldName' => 'mapping_type_id',
637 'sample' => 'Search Builder',
640 'CRM_Core_DAO_Navigation' => array(
642 'fieldName' => 'domain_id',
643 'sample' => 'Default Domain Name',
646 'CRM_Core_DAO_Phone' => array(
648 'fieldName' => 'phone_type_id',
652 'fieldName' => 'location_type_id',
656 'CRM_Core_DAO_PrintLabel' => array(
658 'fieldName' => 'label_format_name',
659 'sample' => 'Avery 5395',
662 'fieldName' => 'label_type_id',
663 'sample' => 'Event Badge',
666 'CRM_Core_DAO_Email' => array(
668 'fieldName' => 'location_type_id',
672 'CRM_Core_DAO_Address' => array(
674 'fieldName' => 'location_type_id',
678 'CRM_Core_DAO_Website' => array(
680 'fieldName' => 'website_type_id',
681 'sample' => 'Facebook',
684 'CRM_Core_DAO_WordReplacement' => array(
686 'fieldName' => 'match_type',
687 'sample' => 'Exact Match',
690 'fieldName' => 'domain_id',
691 'sample' => 'Default Domain Name',
694 'CRM_Core_DAO_MappingField' => array(
696 'fieldName' => 'website_type_id',
697 'sample' => 'Facebook',
700 'fieldName' => 'im_provider_id',
704 'fieldName' => 'operator',
708 'CRM_Contact_DAO_Contact' => array(
710 'fieldName' => 'prefix_id',
714 'fieldName' => 'suffix_id',
718 'fieldName' => 'gender_id',
722 'fieldName' => 'preferred_communication_method',
723 'sample' => 'Postal Mail',
726 'fieldName' => 'contact_type',
727 'sample' => 'Individual',
731 'fieldName' => 'contact_sub_type',
733 'exclude' => 'Individual',
736 'fieldName' => 'preferred_language',
737 'sample' => array('en_US' => 'English (United States)'),
741 'fieldName' => 'preferred_mail_format',
745 'fieldName' => 'communication_style_id',
746 'sample' => 'Formal',
749 'fieldName' => "custom_$customFieldId",
750 'sample' => array('foo' => 'Foo'),
754 'CRM_Batch_DAO_Batch' => array(
756 'fieldName' => 'type_id',
757 'sample' => 'Membership',
760 'fieldName' => 'status_id',
761 'sample' => 'Reopened',
764 'fieldName' => 'mode_id',
765 'sample' => 'Automatic Batch',
768 'fieldName' => 'payment_instrument_id',
772 'CRM_Core_DAO_IM' => array(
774 'fieldName' => 'provider_id',
778 'fieldName' => 'location_type_id',
782 'CRM_Event_DAO_Participant' => array(
784 'fieldName' => 'status_id',
785 'sample' => 'Registered',
788 'fieldName' => 'role_id',
789 'sample' => 'Speaker',
792 'fieldName' => 'fee_currency',
793 'sample' => array('USD' => 'US Dollar'),
797 'fieldName' => 'campaign_id',
798 'sample' => $campaign_name,
801 'CRM_Event_DAO_Event' => array(
803 'fieldName' => 'event_type_id',
804 'sample' => 'Fundraiser',
807 'fieldName' => 'participant_listing_id',
808 'sample' => 'Name and Email',
811 'fieldName' => 'payment_processor',
812 'sample' => $pp_name,
815 'fieldName' => 'financial_type_id',
816 'sample' => 'Donation',
819 'fieldName' => 'default_role_id',
820 'sample' => 'Attendee',
823 'fieldName' => 'currency',
824 'sample' => array('USD' => 'US Dollar'),
828 'fieldName' => 'campaign_id',
829 'sample' => $campaign_name,
832 'CRM_Core_DAO_Menu' => array(
834 'fieldName' => 'domain_id',
835 'sample' => 'Default Domain Name',
838 'fieldName' => 'component_id',
839 'sample' => 'CiviMember',
842 'CRM_Member_DAO_Membership' => array(
844 'fieldName' => 'membership_type_id',
845 'sample' => $membership_type,
848 'fieldName' => 'status_id',
852 'fieldName' => 'campaign_id',
853 'sample' => $campaign_name,
856 'CRM_Member_DAO_MembershipStatus' => array(
858 'fieldName' => 'start_event',
859 'sample' => 'start date',
862 'fieldName' => 'end_event',
863 'sample' => 'member since',
866 'fieldName' => 'start_event_adjust_unit',
870 'fieldName' => 'end_event_adjust_unit',
874 'CRM_Member_DAO_MembershipType' => array(
876 'fieldName' => 'visibility',
877 'sample' => 'Public',
880 'fieldName' => 'domain_id',
881 'sample' => 'Default Domain Name',
884 'fieldName' => 'financial_type_id',
885 'sample' => 'Donation',
888 'fieldName' => 'duration_unit',
889 'sample' => 'lifetime',
892 'fieldName' => 'period_type',
893 'sample' => 'Rolling',
896 'CRM_Mailing_DAO_Mailing' => array(
898 'fieldName' => 'approval_status_id',
899 'sample' => 'Approved',
902 'fieldName' => 'domain_id',
903 'sample' => 'Default Domain Name',
906 'fieldName' => 'visibility',
907 'sample' => 'Public Pages',
910 'fieldName' => 'campaign_id',
911 'sample' => $campaign_name,
914 'CRM_Mailing_DAO_Component' => array(
916 'fieldName' => 'component_type',
917 'sample' => 'Header',
920 'CRM_Mailing_DAO_MailingGroup' => array(
922 'fieldName' => 'group_type',
923 'sample' => 'Include',
926 'CRM_Mailing_DAO_MailingJob' => array(
928 'fieldName' => 'status',
929 'sample' => 'Scheduled',
932 'CRM_Mailing_Event_DAO_Bounce' => array(
934 'fieldName' => 'bounce_type_id',
935 'sample' => 'Invalid',
938 'CRM_Mailing_Event_DAO_Subscribe' => array(
940 'fieldName' => 'group_id',
941 'sample' => $group_name,
944 'CRM_Grant_DAO_Grant' => array(
946 'fieldName' => 'status_id',
947 'sample' => 'Approved for Payment',
950 'fieldName' => 'grant_type_id',
951 'sample' => 'Emergency',
954 'fieldName' => 'currency',
955 'sample' => array('USD' => 'US Dollar'),
959 'fieldName' => 'financial_type_id',
960 'sample' => 'Donation',
963 'CRM_Contribute_DAO_Contribution' => array(
965 'fieldName' => 'payment_instrument_id',
966 'sample' => 'Credit Card',
969 'fieldName' => 'financial_type_id',
970 'sample' => 'Donation',
973 'fieldName' => 'currency',
974 'sample' => array('USD' => 'US Dollar'),
978 'fieldName' => 'contribution_status_id',
979 'sample' => 'Completed',
982 'fieldName' => 'contribution_page_id',
983 'sample' => $contribution_page,
986 'fieldName' => 'campaign_id',
987 'sample' => $campaign_name,
990 'CRM_Contribute_DAO_PremiumsProduct' => array(
992 'fieldName' => 'financial_type_id',
993 'sample' => 'Donation',
996 'CRM_Contribute_DAO_ContributionPage' => array(
998 'fieldName' => 'payment_processor',
999 'sample' => $pp_name,
1002 'fieldName' => 'financial_type_id',
1003 'sample' => 'Donation',
1006 'fieldName' => 'currency',
1007 'sample' => array('USD' => 'US Dollar'),
1011 'fieldName' => 'campaign_id',
1012 'sample' => $campaign_name,
1015 'CRM_Case_DAO_Case' => array(
1017 'fieldName' => 'status_id',
1018 'sample' => 'Ongoing',
1021 'fieldName' => 'case_type_id',
1022 'sample' => 'Housing Support',
1025 'CRM_Report_DAO_ReportInstance' => array(
1027 'fieldName' => 'domain_id',
1028 'sample' => 'Default Domain Name',
1033 foreach ($fields as $daoName => $daoFields) {
1034 foreach ($daoFields as $field) {
1035 $message = "DAO name: '{$daoName}', field: '{$field['fieldName']}'";
1037 $optionValues = $daoName::buildOptions($field['fieldName']);
1038 $this->assertNotEmpty($optionValues, $message);
1040 // Ensure sample value is contained in the returned optionValues.
1041 if (!is_array($field['sample'])) {
1042 $this->assertContains($field['sample'], $optionValues, $message);
1044 // If sample is an array, we check keys and values
1046 foreach ($field['sample'] as $key => $value) {
1047 $this->assertArrayHasKey($key, $optionValues, $message);
1048 $this->assertEquals(CRM_Utils_Array
::value($key, $optionValues), $value, $message);
1052 // Ensure exclude value is not contained in the optionValues
1053 if (!empty($field['exclude'])) {
1054 $this->assertNotContains($field['exclude'], $optionValues, $message);
1057 // Ensure count of optionValues is not extraordinarily high.
1058 $max = CRM_Utils_Array
::value('max', $field, 20);
1059 $this->assertLessThanOrEqual($max, count($optionValues), $message);
1064 public function testContactTypes() {
1066 'Individual' => 'Individual',
1067 'Household' => 'Household',
1068 'Organization' => 'Organization',
1073 3 => 'Organization',
1075 // By default this should return an array keyed by name
1076 $result = CRM_Contact_DAO_Contact
::buildOptions('contact_type');
1077 $this->assertEquals($byName, $result);
1078 // But we can also fetch by ID
1079 $result = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'contact_type', array(
1080 'keyColumn' => 'id',
1081 'labelColumn' => 'name',
1083 $this->assertEquals($byId, $result);
1084 // Make sure flip param works
1085 $result = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'contact_type', array(
1086 'keyColumn' => 'id',
1087 'labelColumn' => 'name',
1090 $this->assertEquals(array_flip($byId), $result);