Add support for orderColumn CRM-12464
[civicrm-core.git] / tests / phpunit / CRM / Core / PseudoConstantTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 require_once 'CiviTest/CiviUnitTestCase.php';
29
30 /**
31 * Tests for linking to resource files
32 */
33 class CRM_Core_PseudoConstantTest extends CiviUnitTestCase {
34 function get_info() {
35 return array(
36 'name' => 'PseudoConstant',
37 'description' => 'Tests for pseudoconstant option values',
38 'group' => 'Core',
39 );
40 }
41
42 function setUp() {
43 parent::setUp();
44 }
45
46 /**
47 * Assure CRM_Core_PseudoConstant::get() is working properly for a range of
48 * DAO fields having a <pseudoconstant> tag in the XML schema.
49 */
50 function testOptionValues() {
51 $custom_group_name = 'Test custom group';
52 $api_params = array(
53 'version' => 3,
54 'title' => $custom_group_name,
55 'extends' => 'Individual',
56 );
57 civicrm_api('customGroup', 'create', $api_params);
58
59 /**
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.
67 */
68 $fields = array(
69 'CRM_Core_DAO_CustomField' => array(
70 array(
71 'fieldName' => 'custom_group_id',
72 'sample' => $custom_group_name,
73 ),
74 ),
75 'CRM_Core_DAO_EntityTag' => array(
76 array(
77 'fieldName' => 'tag_id',
78 'sample' => 'Government Entity',
79 ),
80 ),
81 'CRM_Core_DAO_OptionValue' => array(
82 array(
83 'fieldName' => 'component_id',
84 'sample' => 'CiviContribute',
85 ),
86 ),
87 'CRM_Project_DAO_Task' => array(
88 array(
89 'fieldName' => 'priority_id',
90 'sample' => 'Urgent',
91 ),
92 ),
93 'CRM_Activity_DAO_Activity' => array(
94 array(
95 'fieldName' => 'priority_id',
96 'sample' => 'Urgent',
97 ),
98 ),
99 'CRM_Core_DAO_MailSettings' => array(
100 array(
101 'fieldName' => 'protocol',
102 'sample' => 'Localdir',
103 ),
104 ),
105 'CRM_Core_DAO_Mapping' => array(
106 array(
107 'fieldName' => 'mapping_type_id',
108 'sample' => 'Search Builder',
109 'max' => 15,
110 ),
111 ),
112 'CRM_Pledge_DAO_Pledge' => array(
113 array(
114 'fieldName' => 'honor_type_id',
115 'sample' => 'In Honor of',
116 ),
117 ),
118 'CRM_Contribute_DAO_Contribution' => array(
119 array(
120 'fieldName' => 'honor_type_id',
121 'sample' => 'In Honor of',
122 ),
123 ),
124 'CRM_Core_DAO_Phone' => array(
125 array(
126 'fieldName' => 'phone_type_id',
127 'sample' => 'Phone',
128 ),
129 array(
130 'fieldName' => 'location_type_id',
131 'sample' => 'Home',
132 ),
133 ),
134 'CRM_Core_DAO_Email' => array(
135 array(
136 'fieldName' => 'location_type_id',
137 'sample' => 'Home',
138 ),
139 ),
140 'CRM_Core_DAO_Address' => array(
141 array(
142 'fieldName' => 'location_type_id',
143 'sample' => 'Home',
144 ),
145 ),
146 'CRM_Core_DAO_Website' => array(
147 array(
148 'fieldName' => 'website_type_id',
149 'sample' => 'Facebook',
150 ),
151 ),
152 'CRM_Core_DAO_MappingField' => array(
153 array(
154 'fieldName' => 'website_type_id',
155 'sample' => 'Facebook',
156 ),
157 array(
158 'fieldName' => 'im_provider_id',
159 'sample' => 'Yahoo',
160 ),
161 ),
162 'CRM_Contact_DAO_Contact' => array(
163 array(
164 'fieldName' => 'prefix_id',
165 'sample' => 'Mr.',
166 ),
167 array(
168 'fieldName' => 'suffix_id',
169 'sample' => 'Sr.',
170 ),
171 array(
172 'fieldName' => 'gender_id',
173 'sample' => 'Male',
174 ),
175 array(
176 'fieldName' => 'preferred_communication_method',
177 'sample' => 'Postal Mail',
178 ),
179 array(
180 'fieldName' => 'contact_type',
181 'sample' => 'Individual',
182 'exclude' => 'Team',
183 ),
184 array(
185 'fieldName' => 'contact_sub_type',
186 'sample' => 'Team',
187 'exclude' => 'Individual',
188 ),
189 ),
190 'CRM_Batch_DAO_Batch' => array(
191 array(
192 'fieldName' => 'type_id',
193 'sample' => 'Membership',
194 ),
195 array(
196 'fieldName' => 'status_id',
197 'sample' => 'Reopened',
198 ),
199 array(
200 'fieldName' => 'mode_id',
201 'sample' => 'Automatic Batch',
202 ),
203 ),
204 'CRM_Core_DAO_IM' => array(
205 array(
206 'fieldName' => 'provider_id',
207 'sample' => 'Yahoo',
208 ),
209 ),
210 );
211
212 foreach ($fields as $daoName => $daoFields) {
213 foreach ($daoFields as $field) {
214 $message = "DAO name: '{$daoName}', field: '{$field['fieldName']}'";
215
216 $optionValues = CRM_Core_PseudoConstant::get($daoName, $field['fieldName']);
217 $this->assertNotEmpty($optionValues, $message);
218
219 // Ensure sample value is contained in the returned optionValues.
220 $this->assertContains($field['sample'], $optionValues, $message);
221
222 // Exclude test
223 if (!empty($field['exclude'])) {
224 $this->assertNotContains($field['exclude'], $optionValues, $message);
225 }
226
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);
230 }
231 }
232 }
233
234 function testContactTypes() {
235 $byName = array(
236 'Individual' => 'Individual',
237 'Household' => 'Household',
238 'Organization' => 'Organization',
239 );
240 $byId = array(
241 1 => 'Individual',
242 2 => 'Household',
243 3 => 'Organization',
244 );
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);
254 }
255 }