3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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);
62 // Create a Group for testing.
63 $group_name = md5(microtime());
66 'title' => $group_name,
69 $result = civicrm_api('group', 'create', $api_params);
72 * daoName/field combinations to test
73 * Format: array[DAO Name] = $properties, where properties is an array whose
74 * named members can be:
75 * - fieldName: the SQL column name within the DAO table.
76 * - sample: Any one value which is expected in the list of option values.
77 * - exclude: Any one value which should not be in the list.
78 * - max: integer (default = 10) maximum number of option values expected.
81 'CRM_Contact_DAO_GroupContact' => array(
83 'fieldName' => 'group_id',
84 'sample' => $group_name,
87 'CRM_Contact_DAO_GroupContactCache' => array(
89 'fieldName' => 'group_id',
90 'sample' => $group_name,
93 'CRM_Contact_DAO_GroupOrganization' => array(
95 'fieldName' => 'group_id',
96 'sample' => $group_name,
99 'CRM_Contact_DAO_SubscriptionHistory' => array(
101 'fieldName' => 'group_id',
102 'sample' => $group_name,
105 'CRM_Core_DAO_ActionSchedule' => array(
107 'fieldName' => 'group_id',
108 'sample' => $group_name,
111 'CRM_Mailing_Event_DAO_Subscribe' => array(
113 'fieldName' => 'group_id',
114 'sample' => $group_name,
117 'CRM_Activity_DAO_Activity' => array(
119 'fieldName' => 'activity_type_id',
120 'sample' => 'Text Message (SMS)',
124 'fieldName' => 'status_id',
125 'sample' => 'Scheduled',
128 'CRM_Campaign_DAO_Survey' => array(
130 'fieldName' => 'activity_type_id',
131 'sample' => 'Text Message (SMS)',
135 'CRM_Event_DAO_ParticipantStatusType' => array(
137 'fieldName' => 'visibility_id',
138 'sample' => 'Public',
141 'CRM_Member_DAO_MembershipType' => array(
143 'fieldName' => 'visibility',
144 'sample' => 'Public',
147 'CRM_Price_DAO_Field' => array(
149 'fieldName' => 'visibility_id',
150 'sample' => 'Public',
153 'CRM_Financial_DAO_EntityFinancialAccount' => array(
155 'fieldName' => 'financial_account_id',
156 'sample' => 'Member Dues',
160 'fieldName' => 'account_relationship',
161 'sample' => 'Income Account is',
164 'CRM_Financial_DAO_FinancialItem' => array(
166 'fieldName' => 'status_id',
167 'sample' => 'Partially paid',
170 'fieldName' => 'financial_account_id',
171 'sample' => 'Accounts Receivable',
175 'CRM_Financial_DAO_FinancialTrxn' => array(
177 'fieldName' => 'from_financial_account_id',
178 'sample' => 'Accounts Receivable',
182 'fieldName' => 'to_financial_account_id',
183 'sample' => 'Accounts Receivable',
187 'CRM_Financial_DAO_FinancialAccount' => array(
189 'fieldName' => 'financial_account_type_id',
190 'sample' => 'Cost of Sales',
193 'CRM_Event_DAO_Participant' => array(
195 'fieldName' => 'fee_currency',
200 'CRM_Core_DAO_UFField' => array(
202 'fieldName' => 'uf_group_id',
203 'sample' => 'Name and Address',
207 'CRM_Core_DAO_UFJoin' => array(
209 'fieldName' => 'uf_group_id',
210 'sample' => 'Name and Address',
214 'CRM_Contribute_DAO_ContributionSoft' => array(
216 'fieldName' => 'currency',
221 'CRM_Contribute_DAO_Contribution' => array(
223 'fieldName' => 'currency',
228 'CRM_Contribute_DAO_Product' => array(
230 'fieldName' => 'currency',
235 'CRM_Contribute_DAO_ContributionPage' => array(
237 'fieldName' => 'currency',
242 'CRM_Contribute_DAO_ContributionRecur' => array(
244 'fieldName' => 'currency',
249 'CRM_Event_DAO_Event' => array(
251 'fieldName' => 'currency',
256 'CRM_Financial_DAO_FinancialItem' => array(
258 'fieldName' => 'currency',
263 'CRM_Financial_DAO_OfficialReceipt' => array(
265 'fieldName' => 'currency',
270 'CRM_Financial_DAO_FinancialTrxn' => array(
272 'fieldName' => 'currency',
277 'CRM_Grant_DAO_Grant' => array(
279 'fieldName' => 'currency',
284 'CRM_Pledge_DAO_PledgePayment' => array(
286 'fieldName' => 'currency',
291 'CRM_Pledge_DAO_Pledge' => array(
293 'fieldName' => 'currency',
298 'CRM_PCP_DAO_PCP' => array(
300 'fieldName' => 'currency',
305 'CRM_Core_DAO_CustomField' => array(
307 'fieldName' => 'custom_group_id',
308 'sample' => $custom_group_name,
311 'CRM_Core_DAO_EntityTag' => array(
313 'fieldName' => 'tag_id',
314 'sample' => 'Government Entity',
317 'CRM_Core_DAO_OptionValue' => array(
319 'fieldName' => 'component_id',
320 'sample' => 'CiviContribute',
323 'CRM_Project_DAO_Task' => array(
325 'fieldName' => 'priority_id',
326 'sample' => 'Urgent',
329 'CRM_Activity_DAO_Activity' => array(
331 'fieldName' => 'priority_id',
332 'sample' => 'Urgent',
335 'CRM_Core_DAO_MailSettings' => array(
337 'fieldName' => 'protocol',
338 'sample' => 'Localdir',
341 'CRM_Core_DAO_Mapping' => array(
343 'fieldName' => 'mapping_type_id',
344 'sample' => 'Search Builder',
348 'CRM_Pledge_DAO_Pledge' => array(
350 'fieldName' => 'honor_type_id',
351 'sample' => 'In Honor of',
354 'CRM_Contribute_DAO_Contribution' => array(
356 'fieldName' => 'honor_type_id',
357 'sample' => 'In Honor of',
360 'CRM_Core_DAO_Phone' => array(
362 'fieldName' => 'phone_type_id',
366 'fieldName' => 'location_type_id',
370 'CRM_Core_DAO_Email' => array(
372 'fieldName' => 'location_type_id',
376 'CRM_Core_DAO_Address' => array(
378 'fieldName' => 'location_type_id',
382 'CRM_Core_DAO_Website' => array(
384 'fieldName' => 'website_type_id',
385 'sample' => 'Facebook',
388 'CRM_Core_DAO_MappingField' => array(
390 'fieldName' => 'website_type_id',
391 'sample' => 'Facebook',
394 'fieldName' => 'im_provider_id',
398 'CRM_Contact_DAO_Contact' => array(
400 'fieldName' => 'prefix_id',
404 'fieldName' => 'suffix_id',
408 'fieldName' => 'gender_id',
412 'fieldName' => 'preferred_communication_method',
413 'sample' => 'Postal Mail',
416 'fieldName' => 'contact_type',
417 'sample' => 'Individual',
421 'fieldName' => 'contact_sub_type',
423 'exclude' => 'Individual',
426 'CRM_Batch_DAO_Batch' => array(
428 'fieldName' => 'type_id',
429 'sample' => 'Membership',
432 'fieldName' => 'status_id',
433 'sample' => 'Reopened',
436 'fieldName' => 'mode_id',
437 'sample' => 'Automatic Batch',
440 'CRM_Core_DAO_IM' => array(
442 'fieldName' => 'provider_id',
446 'CRM_Event_DAO_Participant' => array(
448 'fieldName' => 'status_id',
449 'sample' => 'Registered',
452 'fieldName' => 'role_id',
453 'sample' => 'Speaker',
456 'CRM_Event_DAO_Event' => array(
458 'fieldName' => 'event_type_id',
459 'sample' => 'Fundraiser',
462 'CRM_PCP_DAO_PCP' => array(
464 'fieldName' => 'status_id',
465 'sample' => 'Approved',
468 'CRM_Member_DAO_Membership' => array(
470 'fieldName' => 'status_id',
474 'CRM_Mailing_DAO_Mailing' => array(
476 'fieldName' => 'approval_status_id',
477 'sample' => 'Approved',
480 'CRM_Grant_DAO_Grant' => array(
482 'fieldName' => 'status_id',
483 'sample' => 'Approved',
486 'fieldName' => 'grant_type_id',
487 'sample' => 'Emergency',
492 foreach ($fields as $daoName => $daoFields) {
493 foreach ($daoFields as $field) {
494 $message = "DAO name: '{$daoName}', field: '{$field['fieldName']}'";
496 $optionValues = CRM_Core_PseudoConstant
::get($daoName, $field['fieldName']);
497 $this->assertNotEmpty($optionValues, $message);
499 // Ensure sample value is contained in the returned optionValues.
500 $this->assertContains($field['sample'], $optionValues, $message);
502 // Ensure exclude value is not contained in the optionValues
503 if (!empty($field['exclude'])) {
504 $this->assertNotContains($field['exclude'], $optionValues, $message);
507 // Ensure count of optionValues is not extraordinarily high.
508 $max = CRM_Utils_Array
::value('max', $field, 10);
509 $this->assertLessThanOrEqual($max, count($optionValues), $message);
514 function testContactTypes() {
516 'Individual' => 'Individual',
517 'Household' => 'Household',
518 'Organization' => 'Organization',
525 // By default this should return an array keyed by name
526 $result = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'contact_type');
527 $this->assertEquals($byName, $result);
528 // But we can also fetch by ID
529 $result = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name'));
530 $this->assertEquals($byId, $result);
531 // Make sure flip param works
532 $result = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name', 'flip' => TRUE));
533 $this->assertEquals(array_flip($byId), $result);