3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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
{
39 'name' => 'PseudoConstant',
40 'description' => 'Tests for pseudoconstant option values',
50 * Assure CRM_Core_PseudoConstant::get() is working properly for a range of
51 * DAO fields having a <pseudoconstant> tag in the XML schema.
53 function testOptionValues() {
55 // Create a custom field group for testing.
56 $custom_group_name = md5(microtime());
59 'title' => $custom_group_name,
60 'extends' => 'Individual',
63 $result = civicrm_api('customGroup', 'create', $api_params);
64 $this->assertAPISuccess($result);
66 // Add a custom field to the above field group.
70 'custom_group_id' => $result['id'],
71 'label' => $custom_group_name,
72 'html_type' => 'Select',
73 'data_type' => 'String',
75 'option_values' => array(array(
82 $result = civicrm_api('custom_field', 'create', $api_params);
83 $this->assertAPISuccess($result);
84 $customFieldId = $result['id'];
86 // Create a Contact Group for testing.
87 $group_name = md5(microtime());
90 'title' => $group_name,
93 $result = civicrm_api('group', 'create', $api_params);
94 $this->assertAPISuccess($result);
96 // Create a PaymentProcessor for testing.
97 $pp_name = md5(microtime());
101 'payment_processor_type_id' => 10,
103 'user_name' => $pp_name,
104 'class_name' => 'Payment_Dummy',
105 'url_site' => 'https://test.com/',
106 'url_recur' => 'https://test.com/',
109 $result = civicrm_api('payment_processor', 'create', $api_params);
110 $this->assertAPISuccess($result);
113 * daoName/field combinations to test
114 * Format: array[DAO Name] = $properties, where properties is an array whose
115 * named members can be:
116 * - fieldName: the SQL column name within the DAO table.
117 * - sample: Any one value which is expected in the list of option values.
118 * - exclude: Any one value which should not be in the list.
119 * - max: integer (default = 10) maximum number of option values expected.
122 'CRM_Contact_DAO_GroupContact' => array(
124 'fieldName' => 'group_id',
125 'sample' => $group_name,
128 'CRM_Contact_DAO_GroupContactCache' => array(
130 'fieldName' => 'group_id',
131 'sample' => $group_name,
134 'CRM_Contact_DAO_GroupOrganization' => array(
136 'fieldName' => 'group_id',
137 'sample' => $group_name,
140 'CRM_Contact_DAO_SubscriptionHistory' => array(
142 'fieldName' => 'group_id',
143 'sample' => $group_name,
146 'CRM_Core_DAO_ActionSchedule' => array(
148 'fieldName' => 'group_id',
149 'sample' => $group_name,
152 'CRM_Mailing_Event_DAO_Subscribe' => array(
154 'fieldName' => 'group_id',
155 'sample' => $group_name,
158 'CRM_Activity_DAO_Activity' => array(
160 'fieldName' => 'activity_type_id',
165 'fieldName' => 'status_id',
166 'sample' => 'Scheduled',
169 'fieldName' => 'priority_id',
170 'sample' => 'Urgent',
173 'fieldName' => 'engagement_level',
177 'fieldName' => 'medium_id',
181 'CRM_Campaign_DAO_Campaign' => array(
183 'fieldName' => 'status_id',
184 'sample' => 'Completed',
188 'CRM_Campaign_DAO_Survey' => array(
190 'fieldName' => 'activity_type_id',
191 'sample' => 'Phone Call',
195 'CRM_Event_DAO_ParticipantStatusType' => array(
197 'fieldName' => 'visibility_id',
198 'sample' => 'Public',
201 'CRM_Member_DAO_MembershipType' => array(
203 'fieldName' => 'visibility',
204 'sample' => 'Public',
207 'CRM_Price_DAO_PriceField' => array(
209 'fieldName' => 'visibility_id',
210 'sample' => 'Public',
213 'CRM_Financial_DAO_EntityFinancialAccount' => array(
215 'fieldName' => 'financial_account_id',
216 'sample' => 'Member Dues',
220 'fieldName' => 'account_relationship',
221 'sample' => 'Income Account is',
224 'CRM_Financial_DAO_FinancialItem' => array(
226 'fieldName' => 'status_id',
227 'sample' => 'Partially paid',
230 'fieldName' => 'financial_account_id',
231 'sample' => 'Accounts Receivable',
235 'fieldName' => 'currency',
236 'sample' => array('USD' => 'US Dollar'),
240 'CRM_Financial_DAO_FinancialTrxn' => array(
242 'fieldName' => 'from_financial_account_id',
243 'sample' => 'Accounts Receivable',
247 'fieldName' => 'to_financial_account_id',
248 'sample' => 'Accounts Receivable',
252 'fieldName' => 'currency',
253 'sample' => array('USD' => 'US Dollar'),
257 'CRM_Financial_DAO_FinancialAccount' => array(
259 'fieldName' => 'financial_account_type_id',
260 'sample' => 'Cost of Sales',
263 'CRM_Core_DAO_UFField' => array(
265 'fieldName' => 'uf_group_id',
266 'sample' => 'Name and Address',
270 'CRM_Core_DAO_UFJoin' => array(
272 'fieldName' => 'uf_group_id',
273 'sample' => 'Name and Address',
277 'CRM_Contribute_DAO_ContributionSoft' => array(
279 'fieldName' => 'currency',
280 'sample' => array('USD' => 'US Dollar'),
284 'fieldName' => 'soft_credit_type_id',
285 'sample' => 'In Honor of',
288 'CRM_Contribute_DAO_Product' => array(
290 'fieldName' => 'currency',
291 'sample' => array('USD' => 'US Dollar'),
295 'CRM_Contribute_DAO_ContributionRecur' => array(
297 'fieldName' => 'currency',
298 'sample' => array('USD' => 'US Dollar'),
302 'CRM_Pledge_DAO_PledgePayment' => array(
304 'fieldName' => 'currency',
305 'sample' => array('USD' => 'US Dollar'),
309 'CRM_Pledge_DAO_Pledge' => array(
311 'fieldName' => 'currency',
312 'sample' => array('USD' => 'US Dollar'),
316 'CRM_PCP_DAO_PCP' => array(
318 'fieldName' => 'currency',
319 'sample' => array('USD' => 'US Dollar'),
323 'fieldName' => 'status_id',
324 'sample' => 'Approved',
327 'CRM_Core_DAO_CustomField' => array(
329 'fieldName' => 'custom_group_id',
330 'sample' => $custom_group_name,
333 'CRM_Core_DAO_EntityTag' => array(
335 'fieldName' => 'tag_id',
336 'sample' => 'Government Entity',
339 'CRM_Core_DAO_OptionValue' => array(
341 'fieldName' => 'component_id',
342 'sample' => 'CiviContribute',
345 'CRM_Core_DAO_MailSettings' => array(
347 'fieldName' => 'protocol',
348 'sample' => 'Localdir',
351 'CRM_Core_DAO_Mapping' => array(
353 'fieldName' => 'mapping_type_id',
354 'sample' => 'Search Builder',
358 'CRM_Core_DAO_Phone' => array(
360 'fieldName' => 'phone_type_id',
364 'fieldName' => 'location_type_id',
368 'CRM_Core_DAO_Email' => array(
370 'fieldName' => 'location_type_id',
374 'CRM_Core_DAO_Address' => array(
376 'fieldName' => 'location_type_id',
380 'CRM_Core_DAO_Website' => array(
382 'fieldName' => 'website_type_id',
383 'sample' => 'Facebook',
386 'CRM_Core_DAO_MappingField' => array(
388 'fieldName' => 'website_type_id',
389 'sample' => 'Facebook',
392 'fieldName' => 'im_provider_id',
396 'CRM_Contact_DAO_Contact' => array(
398 'fieldName' => 'prefix_id',
402 'fieldName' => 'suffix_id',
406 'fieldName' => 'gender_id',
410 'fieldName' => 'preferred_communication_method',
411 'sample' => 'Postal Mail',
414 'fieldName' => 'contact_type',
415 'sample' => 'Individual',
419 'fieldName' => 'contact_sub_type',
421 'exclude' => 'Individual',
424 'fieldName' => 'preferred_language',
425 'sample' => array('en_US' => 'English (United States)'),
429 'fieldName' => "custom_$customFieldId",
430 'sample' => array('foo' => 'Foo'),
434 'CRM_Batch_DAO_Batch' => array(
436 'fieldName' => 'type_id',
437 'sample' => 'Membership',
440 'fieldName' => 'status_id',
441 'sample' => 'Reopened',
444 'fieldName' => 'mode_id',
445 'sample' => 'Automatic Batch',
448 'CRM_Core_DAO_IM' => array(
450 'fieldName' => 'provider_id',
454 'CRM_Event_DAO_Participant' => array(
456 'fieldName' => 'status_id',
457 'sample' => 'Registered',
460 'fieldName' => 'role_id',
461 'sample' => 'Speaker',
464 'fieldName' => 'fee_currency',
465 'sample' => array('USD' => 'US Dollar'),
469 'CRM_Event_DAO_Event' => array(
471 'fieldName' => 'event_type_id',
472 'sample' => 'Fundraiser',
475 'fieldName' => 'payment_processor',
476 'sample' => $pp_name,
479 'fieldName' => 'financial_type_id',
480 'sample' => 'Donation',
483 'fieldName' => 'currency',
484 'sample' => array('USD' => 'US Dollar'),
488 'CRM_Member_DAO_Membership' => array(
490 'fieldName' => 'status_id',
494 'CRM_Mailing_DAO_Mailing' => array(
496 'fieldName' => 'approval_status_id',
497 'sample' => 'Approved',
500 'CRM_Grant_DAO_Grant' => array(
502 'fieldName' => 'status_id',
503 'sample' => 'Approved for Payment',
506 'fieldName' => 'grant_type_id',
507 'sample' => 'Emergency',
510 'fieldName' => 'currency',
511 'sample' => array('USD' => 'US Dollar'),
515 'CRM_Contribute_DAO_Contribution' => array(
517 'fieldName' => 'payment_instrument_id',
518 'sample' => 'Credit Card',
521 'fieldName' => 'financial_type_id',
522 'sample' => 'Donation',
525 'fieldName' => 'currency',
526 'sample' => array('USD' => 'US Dollar'),
530 'fieldName' => 'contribution_status_id',
531 'sample' => 'Completed',
534 'CRM_Contribute_DAO_ContributionPage' => array(
536 'fieldName' => 'payment_processor',
537 'sample' => $pp_name,
540 'fieldName' => 'financial_type_id',
541 'sample' => 'Donation',
544 'fieldName' => 'currency',
545 'sample' => array('USD' => 'US Dollar'),
549 'CRM_Case_DAO_Case' => array(
551 'fieldName' => 'status_id',
552 'sample' => 'Ongoing',
557 foreach ($fields as $daoName => $daoFields) {
558 foreach ($daoFields as $field) {
559 $message = "DAO name: '{$daoName}', field: '{$field['fieldName']}'";
561 $optionValues = $daoName::buildOptions($field['fieldName']);
562 $this->assertNotEmpty($optionValues, $message);
564 // Ensure sample value is contained in the returned optionValues.
565 if (!is_array($field['sample'])) {
566 $this->assertContains($field['sample'], $optionValues, $message);
568 // If sample is an array, we check keys and values
570 foreach ($field['sample'] as $key => $value) {
571 $this->assertArrayHasKey($key, $optionValues, $message);
572 $this->assertEquals(CRM_Utils_Array
::value($key, $optionValues), $value, $message);
576 // Ensure exclude value is not contained in the optionValues
577 if (!empty($field['exclude'])) {
578 $this->assertNotContains($field['exclude'], $optionValues, $message);
581 // Ensure count of optionValues is not extraordinarily high.
582 $max = CRM_Utils_Array
::value('max', $field, 12);
583 $this->assertLessThanOrEqual($max, count($optionValues), $message);
588 function testContactTypes() {
590 'Individual' => 'Individual',
591 'Household' => 'Household',
592 'Organization' => 'Organization',
599 // By default this should return an array keyed by name
600 $result = CRM_Contact_DAO_Contact
::buildOptions('contact_type');
601 $this->assertEquals($byName, $result);
602 // But we can also fetch by ID
603 $result = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name'));
604 $this->assertEquals($byId, $result);
605 // Make sure flip param works
606 $result = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name', 'flip' => TRUE));
607 $this->assertEquals(array_flip($byId), $result);