Added xml metadata for group_id column in various tables. 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 pseudoconstant retrieval
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
52 // Create a custom field group for testing.
53 $custom_group_name = md5(microtime());
54 $api_params = array(
55 'version' => 3,
56 'title' => $custom_group_name,
57 'extends' => 'Individual',
58 'is_active' => TRUE,
59 );
60 $result = civicrm_api('customGroup', 'create', $api_params);
61
62 // Create a Group for testing.
63 $group_name = md5(microtime());
64 $api_params = array(
65 'version' => 3,
66 'title' => $group_name,
67 'is_active' => TRUE,
68 );
69 $result = civicrm_api('group', 'create', $api_params);
70
71 /**
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.
79 */
80 $fields = array(
81 'CRM_Contact_DAO_GroupContact' => array(
82 array(
83 'fieldName' => 'group_id',
84 'sample' => $group_name,
85 ),
86 ),
87 'CRM_Contact_DAO_GroupContactCache' => array(
88 array(
89 'fieldName' => 'group_id',
90 'sample' => $group_name,
91 ),
92 ),
93 'CRM_Contact_DAO_GroupOrganization' => array(
94 array(
95 'fieldName' => 'group_id',
96 'sample' => $group_name,
97 ),
98 ),
99 'CRM_Contact_DAO_SubscriptionHistory' => array(
100 array(
101 'fieldName' => 'group_id',
102 'sample' => $group_name,
103 ),
104 ),
105 'CRM_Core_DAO_ActionSchedule' => array(
106 array(
107 'fieldName' => 'group_id',
108 'sample' => $group_name,
109 ),
110 ),
111 'CRM_Mailing_Event_DAO_Subscribe' => array(
112 array(
113 'fieldName' => 'group_id',
114 'sample' => $group_name,
115 ),
116 ),
117 'CRM_Activity_DAO_Activity' => array(
118 array(
119 'fieldName' => 'activity_type_id',
120 'sample' => 'Text Message (SMS)',
121 'max' => 50,
122 ),
123 array(
124 'fieldName' => 'status_id',
125 'sample' => 'Scheduled',
126 ),
127 ),
128 'CRM_Campaign_DAO_Survey' => array(
129 array(
130 'fieldName' => 'activity_type_id',
131 'sample' => 'Text Message (SMS)',
132 'max' => 50,
133 ),
134 ),
135 'CRM_Event_DAO_ParticipantStatusType' => array(
136 array(
137 'fieldName' => 'visibility_id',
138 'sample' => 'Public',
139 ),
140 ),
141 'CRM_Member_DAO_MembershipType' => array(
142 array(
143 'fieldName' => 'visibility',
144 'sample' => 'Public',
145 ),
146 ),
147 'CRM_Price_DAO_Field' => array(
148 array(
149 'fieldName' => 'visibility_id',
150 'sample' => 'Public',
151 ),
152 ),
153 'CRM_Financial_DAO_EntityFinancialAccount' => array(
154 array(
155 'fieldName' => 'financial_account_id',
156 'sample' => 'Member Dues',
157 'max' => 15,
158 ),
159 array(
160 'fieldName' => 'account_relationship',
161 'sample' => 'Income Account is',
162 ),
163 ),
164 'CRM_Financial_DAO_FinancialItem' => array(
165 array(
166 'fieldName' => 'status_id',
167 'sample' => 'Partially paid',
168 ),
169 array(
170 'fieldName' => 'financial_account_id',
171 'sample' => 'Accounts Receivable',
172 'max' => 15,
173 ),
174 ),
175 'CRM_Financial_DAO_FinancialTrxn' => array(
176 array(
177 'fieldName' => 'from_financial_account_id',
178 'sample' => 'Accounts Receivable',
179 'max' => 15,
180 ),
181 array(
182 'fieldName' => 'to_financial_account_id',
183 'sample' => 'Accounts Receivable',
184 'max' => 15,
185 ),
186 ),
187 'CRM_Financial_DAO_FinancialAccount' => array(
188 array(
189 'fieldName' => 'financial_account_type_id',
190 'sample' => 'Cost of Sales',
191 ),
192 ),
193 'CRM_Event_DAO_Participant' => array(
194 array(
195 'fieldName' => 'fee_currency',
196 'sample' => '$',
197 'max' => 200,
198 ),
199 ),
200 'CRM_Core_DAO_UFField' => array(
201 array(
202 'fieldName' => 'uf_group_id',
203 'sample' => 'Name and Address',
204 'max' => 15,
205 ),
206 ),
207 'CRM_Core_DAO_UFJoin' => array(
208 array(
209 'fieldName' => 'uf_group_id',
210 'sample' => 'Name and Address',
211 'max' => 15,
212 ),
213 ),
214 'CRM_Contribute_DAO_ContributionSoft' => array(
215 array(
216 'fieldName' => 'currency',
217 'sample' => '$',
218 'max' => 200,
219 ),
220 ),
221 'CRM_Contribute_DAO_Contribution' => array(
222 array(
223 'fieldName' => 'currency',
224 'sample' => '$',
225 'max' => 200,
226 ),
227 ),
228 'CRM_Contribute_DAO_Product' => array(
229 array(
230 'fieldName' => 'currency',
231 'sample' => '$',
232 'max' => 200,
233 ),
234 ),
235 'CRM_Contribute_DAO_ContributionPage' => array(
236 array(
237 'fieldName' => 'currency',
238 'sample' => '$',
239 'max' => 200,
240 ),
241 ),
242 'CRM_Contribute_DAO_ContributionRecur' => array(
243 array(
244 'fieldName' => 'currency',
245 'sample' => '$',
246 'max' => 200,
247 ),
248 ),
249 'CRM_Event_DAO_Event' => array(
250 array(
251 'fieldName' => 'currency',
252 'sample' => '$',
253 'max' => 200,
254 ),
255 ),
256 'CRM_Financial_DAO_FinancialItem' => array(
257 array(
258 'fieldName' => 'currency',
259 'sample' => '$',
260 'max' => 200,
261 ),
262 ),
263 'CRM_Financial_DAO_OfficialReceipt' => array(
264 array(
265 'fieldName' => 'currency',
266 'sample' => '$',
267 'max' => 200,
268 ),
269 ),
270 'CRM_Financial_DAO_FinancialTrxn' => array(
271 array(
272 'fieldName' => 'currency',
273 'sample' => '$',
274 'max' => 200,
275 ),
276 ),
277 'CRM_Grant_DAO_Grant' => array(
278 array(
279 'fieldName' => 'currency',
280 'sample' => '$',
281 'max' => 200,
282 ),
283 ),
284 'CRM_Pledge_DAO_PledgePayment' => array(
285 array(
286 'fieldName' => 'currency',
287 'sample' => '$',
288 'max' => 200,
289 ),
290 ),
291 'CRM_Pledge_DAO_Pledge' => array(
292 array(
293 'fieldName' => 'currency',
294 'sample' => '$',
295 'max' => 200,
296 ),
297 ),
298 'CRM_PCP_DAO_PCP' => array(
299 array(
300 'fieldName' => 'currency',
301 'sample' => '$',
302 'max' => 200,
303 ),
304 ),
305 'CRM_Core_DAO_CustomField' => array(
306 array(
307 'fieldName' => 'custom_group_id',
308 'sample' => $custom_group_name,
309 ),
310 ),
311 'CRM_Core_DAO_EntityTag' => array(
312 array(
313 'fieldName' => 'tag_id',
314 'sample' => 'Government Entity',
315 ),
316 ),
317 'CRM_Core_DAO_OptionValue' => array(
318 array(
319 'fieldName' => 'component_id',
320 'sample' => 'CiviContribute',
321 ),
322 ),
323 'CRM_Project_DAO_Task' => array(
324 array(
325 'fieldName' => 'priority_id',
326 'sample' => 'Urgent',
327 ),
328 ),
329 'CRM_Activity_DAO_Activity' => array(
330 array(
331 'fieldName' => 'priority_id',
332 'sample' => 'Urgent',
333 ),
334 ),
335 'CRM_Core_DAO_MailSettings' => array(
336 array(
337 'fieldName' => 'protocol',
338 'sample' => 'Localdir',
339 ),
340 ),
341 'CRM_Core_DAO_Mapping' => array(
342 array(
343 'fieldName' => 'mapping_type_id',
344 'sample' => 'Search Builder',
345 'max' => 15,
346 ),
347 ),
348 'CRM_Pledge_DAO_Pledge' => array(
349 array(
350 'fieldName' => 'honor_type_id',
351 'sample' => 'In Honor of',
352 ),
353 ),
354 'CRM_Contribute_DAO_Contribution' => array(
355 array(
356 'fieldName' => 'honor_type_id',
357 'sample' => 'In Honor of',
358 ),
359 ),
360 'CRM_Core_DAO_Phone' => array(
361 array(
362 'fieldName' => 'phone_type_id',
363 'sample' => 'Phone',
364 ),
365 array(
366 'fieldName' => 'location_type_id',
367 'sample' => 'Home',
368 ),
369 ),
370 'CRM_Core_DAO_Email' => array(
371 array(
372 'fieldName' => 'location_type_id',
373 'sample' => 'Home',
374 ),
375 ),
376 'CRM_Core_DAO_Address' => array(
377 array(
378 'fieldName' => 'location_type_id',
379 'sample' => 'Home',
380 ),
381 ),
382 'CRM_Core_DAO_Website' => array(
383 array(
384 'fieldName' => 'website_type_id',
385 'sample' => 'Facebook',
386 ),
387 ),
388 'CRM_Core_DAO_MappingField' => array(
389 array(
390 'fieldName' => 'website_type_id',
391 'sample' => 'Facebook',
392 ),
393 array(
394 'fieldName' => 'im_provider_id',
395 'sample' => 'Yahoo',
396 ),
397 ),
398 'CRM_Contact_DAO_Contact' => array(
399 array(
400 'fieldName' => 'prefix_id',
401 'sample' => 'Mr.',
402 ),
403 array(
404 'fieldName' => 'suffix_id',
405 'sample' => 'Sr.',
406 ),
407 array(
408 'fieldName' => 'gender_id',
409 'sample' => 'Male',
410 ),
411 array(
412 'fieldName' => 'preferred_communication_method',
413 'sample' => 'Postal Mail',
414 ),
415 array(
416 'fieldName' => 'contact_type',
417 'sample' => 'Individual',
418 'exclude' => 'Team',
419 ),
420 array(
421 'fieldName' => 'contact_sub_type',
422 'sample' => 'Team',
423 'exclude' => 'Individual',
424 ),
425 ),
426 'CRM_Batch_DAO_Batch' => array(
427 array(
428 'fieldName' => 'type_id',
429 'sample' => 'Membership',
430 ),
431 array(
432 'fieldName' => 'status_id',
433 'sample' => 'Reopened',
434 ),
435 array(
436 'fieldName' => 'mode_id',
437 'sample' => 'Automatic Batch',
438 ),
439 ),
440 'CRM_Core_DAO_IM' => array(
441 array(
442 'fieldName' => 'provider_id',
443 'sample' => 'Yahoo',
444 ),
445 ),
446 'CRM_Event_DAO_Participant' => array(
447 array(
448 'fieldName' => 'status_id',
449 'sample' => 'Registered',
450 ),
451 array(
452 'fieldName' => 'role_id',
453 'sample' => 'Speaker',
454 ),
455 ),
456 'CRM_Event_DAO_Event' => array(
457 array(
458 'fieldName' => 'event_type_id',
459 'sample' => 'Fundraiser',
460 ),
461 ),
462 'CRM_PCP_DAO_PCP' => array(
463 array(
464 'fieldName' => 'status_id',
465 'sample' => 'Approved',
466 ),
467 ),
468 'CRM_Member_DAO_Membership' => array(
469 array(
470 'fieldName' => 'status_id',
471 'sample' => 'New',
472 ),
473 array(
474 'fieldName' => 'membership_type_id',
475 'sample' => 'Lifetime',
476 ),
477 ),
478 );
479
480 foreach ($fields as $daoName => $daoFields) {
481 foreach ($daoFields as $field) {
482 $message = "DAO name: '{$daoName}', field: '{$field['fieldName']}'";
483
484 $optionValues = CRM_Core_PseudoConstant::get($daoName, $field['fieldName']);
485 $this->assertNotEmpty($optionValues, $message);
486
487 // Ensure sample value is contained in the returned optionValues.
488 $this->assertContains($field['sample'], $optionValues, $message);
489
490 // Exclude test
491 if (!empty($field['exclude'])) {
492 $this->assertNotContains($field['exclude'], $optionValues, $message);
493 }
494
495 // Ensure count of optionValues is not extraordinarily high.
496 $max = CRM_Utils_Array::value('max', $field, 10);
497 $this->assertLessThanOrEqual($max, count($optionValues), $message);
498 }
499 }
500 }
501
502 function testContactTypes() {
503 $byName = array(
504 'Individual' => 'Individual',
505 'Household' => 'Household',
506 'Organization' => 'Organization',
507 );
508 $byId = array(
509 1 => 'Individual',
510 2 => 'Household',
511 3 => 'Organization',
512 );
513 // By default this should return an array keyed by name
514 $result = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'contact_type');
515 $this->assertEquals($byName, $result);
516 // But we can also fetch by ID
517 $result = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name'));
518 $this->assertEquals($byId, $result);
519 // Make sure flip param works
520 $result = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name', 'flip' => TRUE));
521 $this->assertEquals(array_flip($byId), $result);
522 }
523 }