3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
{
36 'name' => 'PseudoConstant',
37 'description' => 'Tests for pseudoconstant option values',
47 * Assure CRM_Core_PseudoConstant::get() is working properly for a range of
48 * DAO fields having a <pseudoconstant> tag in the XML schema.
50 function testOptionValues() {
52 // Create a custom field group for testing.
53 $custom_group_name = md5(microtime());
56 'title' => $custom_group_name,
57 'extends' => 'Individual',
60 $result = civicrm_api('customGroup', 'create', $api_params);
61 $this->assertAPISuccess($result);
63 // Add a custom field to the above field group.
67 'custom_group_id' => $result['id'],
68 'label' => $custom_group_name,
69 'html_type' => 'Select',
70 'data_type' => 'String',
72 'option_values' => array(array(
79 $result = civicrm_api('custom_field', 'create', $api_params);
80 $this->assertAPISuccess($result);
81 $customFieldId = $result['id'];
83 // Create a Contact Group for testing.
84 $group_name = md5(microtime());
87 'title' => $group_name,
90 $result = civicrm_api('group', 'create', $api_params);
91 $this->assertAPISuccess($result);
93 // Create a PaymentProcessor for testing.
94 $pp_name = md5(microtime());
98 'payment_processor_type_id' => 10,
100 'user_name' => $pp_name,
101 'class_name' => 'Payment_Dummy',
102 'url_site' => 'https://test.com/',
103 'url_recur' => 'https://test.com/',
106 $result = civicrm_api('payment_processor', 'create', $api_params);
107 $this->assertAPISuccess($result);
110 * daoName/field combinations to test
111 * Format: array[DAO Name] = $properties, where properties is an array whose
112 * named members can be:
113 * - fieldName: the SQL column name within the DAO table.
114 * - sample: Any one value which is expected in the list of option values.
115 * - exclude: Any one value which should not be in the list.
116 * - max: integer (default = 10) maximum number of option values expected.
119 'CRM_Contact_DAO_GroupContact' => array(
121 'fieldName' => 'group_id',
122 'sample' => $group_name,
125 'CRM_Contact_DAO_GroupContactCache' => array(
127 'fieldName' => 'group_id',
128 'sample' => $group_name,
131 'CRM_Contact_DAO_GroupOrganization' => array(
133 'fieldName' => 'group_id',
134 'sample' => $group_name,
137 'CRM_Contact_DAO_SubscriptionHistory' => array(
139 'fieldName' => 'group_id',
140 'sample' => $group_name,
143 'CRM_Core_DAO_ActionSchedule' => array(
145 'fieldName' => 'group_id',
146 'sample' => $group_name,
149 'CRM_Mailing_Event_DAO_Subscribe' => array(
151 'fieldName' => 'group_id',
152 'sample' => $group_name,
155 'CRM_Activity_DAO_Activity' => array(
157 'fieldName' => 'activity_type_id',
162 'fieldName' => 'status_id',
163 'sample' => 'Scheduled',
166 'fieldName' => 'priority_id',
167 'sample' => 'Urgent',
170 'fieldName' => 'engagement_level',
174 'fieldName' => 'medium_id',
178 'CRM_Campaign_DAO_Campaign' => array(
180 'fieldName' => 'status_id',
181 'sample' => 'Completed',
185 'CRM_Campaign_DAO_Survey' => array(
187 'fieldName' => 'activity_type_id',
188 'sample' => 'Phone Call',
192 'CRM_Event_DAO_ParticipantStatusType' => array(
194 'fieldName' => 'visibility_id',
195 'sample' => 'Public',
198 'CRM_Member_DAO_MembershipType' => array(
200 'fieldName' => 'visibility',
201 'sample' => 'Public',
204 'CRM_Price_DAO_PriceField' => array(
206 'fieldName' => 'visibility_id',
207 'sample' => 'Public',
210 'CRM_Financial_DAO_EntityFinancialAccount' => array(
212 'fieldName' => 'financial_account_id',
213 'sample' => 'Member Dues',
217 'fieldName' => 'account_relationship',
218 'sample' => 'Income Account is',
221 'CRM_Financial_DAO_FinancialItem' => array(
223 'fieldName' => 'status_id',
224 'sample' => 'Partially paid',
227 'fieldName' => 'financial_account_id',
228 'sample' => 'Accounts Receivable',
232 'fieldName' => 'currency',
233 'sample' => array('USD' => 'US Dollar'),
237 'CRM_Financial_DAO_FinancialTrxn' => array(
239 'fieldName' => 'from_financial_account_id',
240 'sample' => 'Accounts Receivable',
244 'fieldName' => 'to_financial_account_id',
245 'sample' => 'Accounts Receivable',
249 'fieldName' => 'currency',
250 'sample' => array('USD' => 'US Dollar'),
254 'CRM_Financial_DAO_FinancialAccount' => array(
256 'fieldName' => 'financial_account_type_id',
257 'sample' => 'Cost of Sales',
260 'CRM_Core_DAO_UFField' => array(
262 'fieldName' => 'uf_group_id',
263 'sample' => 'Name and Address',
267 'CRM_Core_DAO_UFJoin' => array(
269 'fieldName' => 'uf_group_id',
270 'sample' => 'Name and Address',
274 'CRM_Contribute_DAO_ContributionSoft' => array(
276 'fieldName' => 'currency',
277 'sample' => array('USD' => 'US Dollar'),
281 'CRM_Contribute_DAO_Product' => array(
283 'fieldName' => 'currency',
284 'sample' => array('USD' => 'US Dollar'),
288 'CRM_Contribute_DAO_ContributionRecur' => array(
290 'fieldName' => 'currency',
291 'sample' => array('USD' => 'US Dollar'),
295 'CRM_Pledge_DAO_PledgePayment' => array(
297 'fieldName' => 'currency',
298 'sample' => array('USD' => 'US Dollar'),
302 'CRM_Pledge_DAO_Pledge' => array(
304 'fieldName' => 'currency',
305 'sample' => array('USD' => 'US Dollar'),
309 'CRM_PCP_DAO_PCP' => array(
311 'fieldName' => 'currency',
312 'sample' => array('USD' => 'US Dollar'),
316 'fieldName' => 'status_id',
317 'sample' => 'Approved',
320 'CRM_Core_DAO_CustomField' => array(
322 'fieldName' => 'custom_group_id',
323 'sample' => $custom_group_name,
326 'CRM_Core_DAO_EntityTag' => array(
328 'fieldName' => 'tag_id',
329 'sample' => 'Government Entity',
332 'CRM_Core_DAO_OptionValue' => array(
334 'fieldName' => 'component_id',
335 'sample' => 'CiviContribute',
338 'CRM_Core_DAO_MailSettings' => array(
340 'fieldName' => 'protocol',
341 'sample' => 'Localdir',
344 'CRM_Core_DAO_Mapping' => array(
346 'fieldName' => 'mapping_type_id',
347 'sample' => 'Search Builder',
351 'CRM_Pledge_DAO_Pledge' => array(
353 'fieldName' => 'honor_type_id',
354 'sample' => 'In Honor of',
357 'CRM_Core_DAO_Phone' => array(
359 'fieldName' => 'phone_type_id',
363 'fieldName' => 'location_type_id',
367 'CRM_Core_DAO_Email' => array(
369 'fieldName' => 'location_type_id',
373 'CRM_Core_DAO_Address' => array(
375 'fieldName' => 'location_type_id',
379 'CRM_Core_DAO_Website' => array(
381 'fieldName' => 'website_type_id',
382 'sample' => 'Facebook',
385 'CRM_Core_DAO_MappingField' => array(
387 'fieldName' => 'website_type_id',
388 'sample' => 'Facebook',
391 'fieldName' => 'im_provider_id',
395 'CRM_Contact_DAO_Contact' => array(
397 'fieldName' => 'prefix_id',
401 'fieldName' => 'suffix_id',
405 'fieldName' => 'gender_id',
409 'fieldName' => 'preferred_communication_method',
410 'sample' => 'Postal Mail',
413 'fieldName' => 'contact_type',
414 'sample' => 'Individual',
418 'fieldName' => 'contact_sub_type',
420 'exclude' => 'Individual',
423 'fieldName' => 'preferred_language',
424 'sample' => array('en_US' => 'English (United States)'),
428 'fieldName' => "custom_$customFieldId",
429 'sample' => array('foo' => 'Foo'),
433 'CRM_Batch_DAO_Batch' => array(
435 'fieldName' => 'type_id',
436 'sample' => 'Membership',
439 'fieldName' => 'status_id',
440 'sample' => 'Reopened',
443 'fieldName' => 'mode_id',
444 'sample' => 'Automatic Batch',
447 'CRM_Core_DAO_IM' => array(
449 'fieldName' => 'provider_id',
453 'CRM_Event_DAO_Participant' => array(
455 'fieldName' => 'status_id',
456 'sample' => 'Registered',
459 'fieldName' => 'role_id',
460 'sample' => 'Speaker',
463 'fieldName' => 'fee_currency',
464 'sample' => array('USD' => 'US Dollar'),
468 'CRM_Event_DAO_Event' => array(
470 'fieldName' => 'event_type_id',
471 'sample' => 'Fundraiser',
474 'fieldName' => 'payment_processor',
475 'sample' => $pp_name,
478 'fieldName' => 'financial_type_id',
479 'sample' => 'Donation',
482 'fieldName' => 'currency',
483 'sample' => array('USD' => 'US Dollar'),
487 'CRM_Member_DAO_Membership' => array(
489 'fieldName' => 'status_id',
493 'CRM_Mailing_DAO_Mailing' => array(
495 'fieldName' => 'approval_status_id',
496 'sample' => 'Approved',
499 'CRM_Grant_DAO_Grant' => array(
501 'fieldName' => 'status_id',
502 'sample' => 'Approved',
505 'fieldName' => 'grant_type_id',
506 'sample' => 'Emergency',
509 'fieldName' => 'currency',
510 'sample' => array('USD' => 'US Dollar'),
514 'CRM_Contribute_DAO_Contribution' => array(
516 'fieldName' => 'payment_instrument_id',
517 'sample' => 'Credit Card',
520 'fieldName' => 'financial_type_id',
521 'sample' => 'Donation',
524 'fieldName' => 'currency',
525 'sample' => array('USD' => 'US Dollar'),
529 'fieldName' => 'contribution_status_id',
530 'sample' => 'Completed',
533 'fieldName' => 'honor_type_id',
534 'sample' => 'In Honor of',
537 'CRM_Contribute_DAO_ContributionPage' => array(
539 'fieldName' => 'payment_processor',
540 'sample' => $pp_name,
543 'fieldName' => 'financial_type_id',
544 'sample' => 'Donation',
547 'fieldName' => 'currency',
548 'sample' => array('USD' => 'US Dollar'),
552 'CRM_Case_DAO_Case' => array(
554 'fieldName' => 'status_id',
555 'sample' => 'Ongoing',
560 foreach ($fields as $daoName => $daoFields) {
561 foreach ($daoFields as $field) {
562 $message = "DAO name: '{$daoName}', field: '{$field['fieldName']}'";
564 $optionValues = $daoName::buildOptions($field['fieldName']);
565 $this->assertNotEmpty($optionValues, $message);
567 // Ensure sample value is contained in the returned optionValues.
568 if (!is_array($field['sample'])) {
569 $this->assertContains($field['sample'], $optionValues, $message);
571 // If sample is an array, we check keys and values
573 foreach ($field['sample'] as $key => $value) {
574 $this->assertArrayHasKey($key, $optionValues, $message);
575 $this->assertEquals(CRM_Utils_Array
::value($key, $optionValues), $value, $message);
579 // Ensure exclude value is not contained in the optionValues
580 if (!empty($field['exclude'])) {
581 $this->assertNotContains($field['exclude'], $optionValues, $message);
584 // Ensure count of optionValues is not extraordinarily high.
585 $max = CRM_Utils_Array
::value('max', $field, 10);
586 $this->assertLessThanOrEqual($max, count($optionValues), $message);
591 function testContactTypes() {
593 'Individual' => 'Individual',
594 'Household' => 'Household',
595 'Organization' => 'Organization',
602 // By default this should return an array keyed by name
603 $result = CRM_Contact_DAO_Contact
::buildOptions('contact_type');
604 $this->assertEquals($byName, $result);
605 // But we can also fetch by ID
606 $result = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name'));
607 $this->assertEquals($byId, $result);
608 // Make sure flip param works
609 $result = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name', 'flip' => TRUE));
610 $this->assertEquals(array_flip($byId), $result);