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 linking to resource files
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() {
51 $custom_group_name = 'Test custom group';
54 'title' => $custom_group_name,
55 'extends' => 'Individual',
57 civicrm_api('customGroup', 'create', $api_params);
60 * daoName/field combinations to test
61 * Format: array[DAO Name] = $properties, where properties is an array whose
62 * named members can be:
63 * - fieldName: the SQL column name within the DAO table.
64 * - sample: Any one value which is expected in the list of option values.
65 * - exclude: Any one value which should not be in the list.
66 * - max: integer (default = 10) maximum number of option values expected.
69 'CRM_Core_DAO_CustomField' => array(
71 'fieldName' => 'custom_group_id',
72 'sample' => $custom_group_name,
75 'CRM_Core_DAO_EntityTag' => array(
77 'fieldName' => 'tag_id',
78 'sample' => 'Government Entity',
81 'CRM_Core_DAO_OptionValue' => array(
83 'fieldName' => 'component_id',
84 'sample' => 'CiviContribute',
87 'CRM_Project_DAO_Task' => array(
89 'fieldName' => 'priority_id',
93 'CRM_Activity_DAO_Activity' => array(
95 'fieldName' => 'priority_id',
99 'CRM_Core_DAO_MailSettings' => array(
101 'fieldName' => 'protocol',
102 'sample' => 'Localdir',
105 'CRM_Core_DAO_Mapping' => array(
107 'fieldName' => 'mapping_type_id',
108 'sample' => 'Search Builder',
112 'CRM_Pledge_DAO_Pledge' => array(
114 'fieldName' => 'honor_type_id',
115 'sample' => 'In Honor of',
118 'CRM_Contribute_DAO_Contribution' => array(
120 'fieldName' => 'honor_type_id',
121 'sample' => 'In Honor of',
124 'CRM_Core_DAO_Phone' => array(
126 'fieldName' => 'phone_type_id',
130 'fieldName' => 'location_type_id',
134 'CRM_Core_DAO_Email' => array(
136 'fieldName' => 'location_type_id',
140 'CRM_Core_DAO_Address' => array(
142 'fieldName' => 'location_type_id',
146 'CRM_Core_DAO_Website' => array(
148 'fieldName' => 'website_type_id',
149 'sample' => 'Facebook',
152 'CRM_Core_DAO_MappingField' => array(
154 'fieldName' => 'website_type_id',
155 'sample' => 'Facebook',
158 'fieldName' => 'im_provider_id',
162 'CRM_Contact_DAO_Contact' => array(
164 'fieldName' => 'prefix_id',
168 'fieldName' => 'suffix_id',
172 'fieldName' => 'gender_id',
176 'fieldName' => 'preferred_communication_method',
177 'sample' => 'Postal Mail',
180 'fieldName' => 'contact_type',
181 'sample' => 'Individual',
185 'fieldName' => 'contact_sub_type',
187 'exclude' => 'Individual',
190 'CRM_Batch_DAO_Batch' => array(
192 'fieldName' => 'type_id',
193 'sample' => 'Membership',
196 'fieldName' => 'status_id',
197 'sample' => 'Reopened',
200 'fieldName' => 'mode_id',
201 'sample' => 'Automatic Batch',
204 'CRM_Core_DAO_IM' => array(
206 'fieldName' => 'provider_id',
212 foreach ($fields as $daoName => $daoFields) {
213 foreach ($daoFields as $field) {
214 $message = "DAO name: '{$daoName}', field: '{$field['fieldName']}'";
216 $optionValues = CRM_Core_PseudoConstant
::get($daoName, $field['fieldName']);
217 $this->assertNotEmpty($optionValues, $message);
219 // Ensure sample value is contained in the returned optionValues.
220 $this->assertContains($field['sample'], $optionValues, $message);
223 if (!empty($field['exclude'])) {
224 $this->assertNotContains($field['exclude'], $optionValues, $message);
227 // Ensure count of optionValues is not extraordinarily high.
228 $max = CRM_Utils_Array
::value('max', $field, 10);
229 $this->assertLessThanOrEqual($max, count($optionValues), $message);
234 function testContactTypes() {
236 'Individual' => 'Individual',
237 'Household' => 'Household',
238 'Organization' => 'Organization',
245 // By default this should return an array keyed by name
246 $result = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'contact_type');
247 $this->assertEquals($byName, $result);
248 // But we can also fetch by ID
249 $result = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name'));
250 $this->assertEquals($byId, $result);
251 // Make sure flip param works
252 $result = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name', 'flip' => TRUE));
253 $this->assertEquals(array_flip($byId), $result);