Merge pull request #814 from eileenmcnaughton/CRM-12642
[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 $this->assertAPISuccess($result);
62
63 // Add a custom field to the above field group.
64 $api_params = array(
65 'version' => 3,
66 'debug' => 1,
67 'custom_group_id' => $result['id'],
68 'label' => $custom_group_name,
69 'html_type' => 'Select',
70 'data_type' => 'String',
71 'is_active' => TRUE,
72 'option_values' => array(array(
73 'label' => 'Foo',
74 'value' => 'foo',
75 'is_active' => 1,
76 'weight' => 0,
77 )),
78 );
79 $result = civicrm_api('custom_field', 'create', $api_params);
80 $this->assertAPISuccess($result);
81 $customFieldId = $result['id'];
82
83 // Create a Contact Group for testing.
84 $group_name = md5(microtime());
85 $api_params = array(
86 'version' => 3,
87 'title' => $group_name,
88 'is_active' => TRUE,
89 );
90 $result = civicrm_api('group', 'create', $api_params);
91 $this->assertAPISuccess($result);
92
93 // Create a PaymentProcessor for testing.
94 $pp_name = md5(microtime());
95 $api_params = array(
96 'version' => 3,
97 'domain_id' => 1,
98 'payment_processor_type_id' => 10,
99 'name' => $pp_name,
100 'user_name' => $pp_name,
101 'class_name' => 'Payment_Dummy',
102 'url_site' => 'https://test.com/',
103 'url_recur' => 'https://test.com/',
104 'is_active' => 1,
105 );
106 $result = civicrm_api('payment_processor', 'create', $api_params);
107 $this->assertAPISuccess($result);
108
109 /**
110 * daoName/field combinations to test
111 * Format: array[DAO Name] = $properties, where properties is an array whose
112 * named members can be:
113 * - fieldName: the SQL column name within the DAO table.
114 * - sample: Any one value which is expected in the list of option values.
115 * - exclude: Any one value which should not be in the list.
116 * - max: integer (default = 10) maximum number of option values expected.
117 */
118 $fields = array(
119 'CRM_Contact_DAO_GroupContact' => array(
120 array(
121 'fieldName' => 'group_id',
122 'sample' => $group_name,
123 ),
124 ),
125 'CRM_Contact_DAO_GroupContactCache' => array(
126 array(
127 'fieldName' => 'group_id',
128 'sample' => $group_name,
129 ),
130 ),
131 'CRM_Contact_DAO_GroupOrganization' => array(
132 array(
133 'fieldName' => 'group_id',
134 'sample' => $group_name,
135 ),
136 ),
137 'CRM_Contact_DAO_SubscriptionHistory' => array(
138 array(
139 'fieldName' => 'group_id',
140 'sample' => $group_name,
141 ),
142 ),
143 'CRM_Core_DAO_ActionSchedule' => array(
144 array(
145 'fieldName' => 'group_id',
146 'sample' => $group_name,
147 ),
148 ),
149 'CRM_Mailing_Event_DAO_Subscribe' => array(
150 array(
151 'fieldName' => 'group_id',
152 'sample' => $group_name,
153 ),
154 ),
155 'CRM_Activity_DAO_Activity' => array(
156 array(
157 'fieldName' => 'activity_type_id',
158 'sample' => 'Text Message (SMS)',
159 'max' => 50,
160 ),
161 array(
162 'fieldName' => 'status_id',
163 'sample' => 'Scheduled',
164 ),
165 array(
166 'fieldName' => 'priority_id',
167 'sample' => 'Urgent',
168 ),
169 array(
170 'fieldName' => 'engagement_level',
171 'sample' => '1',
172 ),
173 array(
174 'fieldName' => 'medium_id',
175 'sample' => 'Phone',
176 ),
177 ),
178 'CRM_Campaign_DAO_Campaign' => array(
179 array(
180 'fieldName' => 'status_id',
181 'sample' => 'Completed',
182 'max' => 50,
183 ),
184 ),
185 'CRM_Campaign_DAO_Survey' => array(
186 array(
187 'fieldName' => 'activity_type_id',
188 'sample' => 'Text Message (SMS)',
189 'max' => 50,
190 ),
191 ),
192 'CRM_Event_DAO_ParticipantStatusType' => array(
193 array(
194 'fieldName' => 'visibility_id',
195 'sample' => 'Public',
196 ),
197 ),
198 'CRM_Member_DAO_MembershipType' => array(
199 array(
200 'fieldName' => 'visibility',
201 'sample' => 'Public',
202 ),
203 ),
204 'CRM_Price_DAO_Field' => array(
205 array(
206 'fieldName' => 'visibility_id',
207 'sample' => 'Public',
208 ),
209 ),
210 'CRM_Financial_DAO_EntityFinancialAccount' => array(
211 array(
212 'fieldName' => 'financial_account_id',
213 'sample' => 'Member Dues',
214 'max' => 15,
215 ),
216 array(
217 'fieldName' => 'account_relationship',
218 'sample' => 'Income Account is',
219 ),
220 ),
221 'CRM_Financial_DAO_FinancialItem' => array(
222 array(
223 'fieldName' => 'status_id',
224 'sample' => 'Partially paid',
225 ),
226 array(
227 'fieldName' => 'financial_account_id',
228 'sample' => 'Accounts Receivable',
229 'max' => 15,
230 ),
231 array(
232 'fieldName' => 'currency',
233 'sample' => '$',
234 'max' => 200,
235 ),
236 ),
237 'CRM_Financial_DAO_FinancialTrxn' => array(
238 array(
239 'fieldName' => 'from_financial_account_id',
240 'sample' => 'Accounts Receivable',
241 'max' => 15,
242 ),
243 array(
244 'fieldName' => 'to_financial_account_id',
245 'sample' => 'Accounts Receivable',
246 'max' => 15,
247 ),
248 array(
249 'fieldName' => 'currency',
250 'sample' => '$',
251 'max' => 200,
252 ),
253 ),
254 'CRM_Financial_DAO_FinancialAccount' => array(
255 array(
256 'fieldName' => 'financial_account_type_id',
257 'sample' => 'Cost of Sales',
258 ),
259 ),
260 'CRM_Core_DAO_UFField' => array(
261 array(
262 'fieldName' => 'uf_group_id',
263 'sample' => 'Name and Address',
264 'max' => 15,
265 ),
266 ),
267 'CRM_Core_DAO_UFJoin' => array(
268 array(
269 'fieldName' => 'uf_group_id',
270 'sample' => 'Name and Address',
271 'max' => 15,
272 ),
273 ),
274 'CRM_Contribute_DAO_ContributionSoft' => array(
275 array(
276 'fieldName' => 'currency',
277 'sample' => '$',
278 'max' => 200,
279 ),
280 ),
281 'CRM_Contribute_DAO_Product' => array(
282 array(
283 'fieldName' => 'currency',
284 'sample' => '$',
285 'max' => 200,
286 ),
287 ),
288 'CRM_Contribute_DAO_ContributionRecur' => array(
289 array(
290 'fieldName' => 'currency',
291 'sample' => '$',
292 'max' => 200,
293 ),
294 ),
295 'CRM_Financial_DAO_OfficialReceipt' => array(
296 array(
297 'fieldName' => 'currency',
298 'sample' => '$',
299 'max' => 200,
300 ),
301 ),
302 'CRM_Pledge_DAO_PledgePayment' => array(
303 array(
304 'fieldName' => 'currency',
305 'sample' => '$',
306 'max' => 200,
307 ),
308 ),
309 'CRM_Pledge_DAO_Pledge' => array(
310 array(
311 'fieldName' => 'currency',
312 'sample' => '$',
313 'max' => 200,
314 ),
315 ),
316 'CRM_PCP_DAO_PCP' => array(
317 array(
318 'fieldName' => 'currency',
319 'sample' => '$',
320 'max' => 200,
321 ),
322 array(
323 'fieldName' => 'status_id',
324 'sample' => 'Approved',
325 ),
326 ),
327 'CRM_Core_DAO_CustomField' => array(
328 array(
329 'fieldName' => 'custom_group_id',
330 'sample' => $custom_group_name,
331 ),
332 ),
333 'CRM_Core_DAO_EntityTag' => array(
334 array(
335 'fieldName' => 'tag_id',
336 'sample' => 'Government Entity',
337 ),
338 ),
339 'CRM_Core_DAO_OptionValue' => array(
340 array(
341 'fieldName' => 'component_id',
342 'sample' => 'CiviContribute',
343 ),
344 ),
345 'CRM_Core_DAO_MailSettings' => array(
346 array(
347 'fieldName' => 'protocol',
348 'sample' => 'Localdir',
349 ),
350 ),
351 'CRM_Core_DAO_Mapping' => array(
352 array(
353 'fieldName' => 'mapping_type_id',
354 'sample' => 'Search Builder',
355 'max' => 15,
356 ),
357 ),
358 'CRM_Pledge_DAO_Pledge' => array(
359 array(
360 'fieldName' => 'honor_type_id',
361 'sample' => 'In Honor of',
362 ),
363 ),
364 'CRM_Core_DAO_Phone' => array(
365 array(
366 'fieldName' => 'phone_type_id',
367 'sample' => 'Phone',
368 ),
369 array(
370 'fieldName' => 'location_type_id',
371 'sample' => 'Home',
372 ),
373 ),
374 'CRM_Core_DAO_Email' => array(
375 array(
376 'fieldName' => 'location_type_id',
377 'sample' => 'Home',
378 ),
379 ),
380 'CRM_Core_DAO_Address' => array(
381 array(
382 'fieldName' => 'location_type_id',
383 'sample' => 'Home',
384 ),
385 ),
386 'CRM_Core_DAO_Website' => array(
387 array(
388 'fieldName' => 'website_type_id',
389 'sample' => 'Facebook',
390 ),
391 ),
392 'CRM_Core_DAO_MappingField' => array(
393 array(
394 'fieldName' => 'website_type_id',
395 'sample' => 'Facebook',
396 ),
397 array(
398 'fieldName' => 'im_provider_id',
399 'sample' => 'Yahoo',
400 ),
401 ),
402 'CRM_Contact_DAO_Contact' => array(
403 array(
404 'fieldName' => 'prefix_id',
405 'sample' => 'Mr.',
406 ),
407 array(
408 'fieldName' => 'suffix_id',
409 'sample' => 'Sr.',
410 ),
411 array(
412 'fieldName' => 'gender_id',
413 'sample' => 'Male',
414 ),
415 array(
416 'fieldName' => 'preferred_communication_method',
417 'sample' => 'Postal Mail',
418 ),
419 array(
420 'fieldName' => 'contact_type',
421 'sample' => 'Individual',
422 'exclude' => 'Team',
423 ),
424 array(
425 'fieldName' => 'contact_sub_type',
426 'sample' => 'Team',
427 'exclude' => 'Individual',
428 ),
429 array(
430 'fieldName' => 'preferred_language',
431 'sample' => array('en_US' => 'English (United States)'),
432 'max' => 250,
433 ),
434 array(
435 'fieldName' => "custom_$customFieldId",
436 'sample' => array('foo' => 'Foo'),
437 'max' => 1,
438 ),
439 ),
440 'CRM_Batch_DAO_Batch' => array(
441 array(
442 'fieldName' => 'type_id',
443 'sample' => 'Membership',
444 ),
445 array(
446 'fieldName' => 'status_id',
447 'sample' => 'Reopened',
448 ),
449 array(
450 'fieldName' => 'mode_id',
451 'sample' => 'Automatic Batch',
452 ),
453 ),
454 'CRM_Core_DAO_IM' => array(
455 array(
456 'fieldName' => 'provider_id',
457 'sample' => 'Yahoo',
458 ),
459 ),
460 'CRM_Event_DAO_Participant' => array(
461 array(
462 'fieldName' => 'status_id',
463 'sample' => 'Registered',
464 ),
465 array(
466 'fieldName' => 'role_id',
467 'sample' => 'Speaker',
468 ),
469 array(
470 'fieldName' => 'fee_currency',
471 'sample' => '$',
472 'max' => 200,
473 ),
474 ),
475 'CRM_Event_DAO_Event' => array(
476 array(
477 'fieldName' => 'event_type_id',
478 'sample' => 'Fundraiser',
479 ),
480 array(
481 'fieldName' => 'payment_processor',
482 'sample' => $pp_name,
483 ),
484 array(
485 'fieldName' => 'financial_type_id',
486 'sample' => 'Donation',
487 ),
488 array(
489 'fieldName' => 'currency',
490 'sample' => '$',
491 'max' => 200,
492 ),
493 ),
494 'CRM_Member_DAO_Membership' => array(
495 array(
496 'fieldName' => 'status_id',
497 'sample' => 'New',
498 ),
499 ),
500 'CRM_Mailing_DAO_Mailing' => array(
501 array(
502 'fieldName' => 'approval_status_id',
503 'sample' => 'Approved',
504 ),
505 ),
506 'CRM_Grant_DAO_Grant' => array(
507 array(
508 'fieldName' => 'status_id',
509 'sample' => 'Approved',
510 ),
511 array(
512 'fieldName' => 'grant_type_id',
513 'sample' => 'Emergency',
514 ),
515 array(
516 'fieldName' => 'currency',
517 'sample' => '$',
518 'max' => 200,
519 ),
520 ),
521 'CRM_Contribute_DAO_Contribution' => array(
522 array(
523 'fieldName' => 'payment_instrument_id',
524 'sample' => 'Credit Card',
525 ),
526 array(
527 'fieldName' => 'financial_type_id',
528 'sample' => 'Donation',
529 ),
530 array(
531 'fieldName' => 'currency',
532 'sample' => '$',
533 'max' => 200,
534 ),
535 array(
536 'fieldName' => 'contribution_status_id',
537 'sample' => 'Completed',
538 ),
539 array(
540 'fieldName' => 'honor_type_id',
541 'sample' => 'In Honor of',
542 ),
543 ),
544 'CRM_Contribute_DAO_ContributionPage' => array(
545 array(
546 'fieldName' => 'payment_processor',
547 'sample' => $pp_name,
548 ),
549 array(
550 'fieldName' => 'financial_type_id',
551 'sample' => 'Donation',
552 ),
553 array(
554 'fieldName' => 'currency',
555 'sample' => '$',
556 'max' => 200,
557 ),
558 ),
559 'CRM_Case_DAO_Case' => array(
560 array(
561 'fieldName' => 'status_id',
562 'sample' => 'Ongoing',
563 ),
564 ),
565 );
566
567 foreach ($fields as $daoName => $daoFields) {
568 foreach ($daoFields as $field) {
569 $message = "DAO name: '{$daoName}', field: '{$field['fieldName']}'";
570
571 $optionValues = $daoName::buildOptions($field['fieldName']);
572 $this->assertNotEmpty($optionValues, $message);
573
574 // Ensure sample value is contained in the returned optionValues.
575 if (!is_array($field['sample'])) {
576 $this->assertContains($field['sample'], $optionValues, $message);
577 }
578 // If sample is an array, we check keys and values
579 else {
580 foreach ($field['sample'] as $key => $value) {
581 $this->assertArrayHasKey($key, $optionValues, $message);
582 $this->assertEquals(CRM_Utils_Array::value($key, $optionValues), $value, $message);
583 }
584 }
585
586 // Ensure exclude value is not contained in the optionValues
587 if (!empty($field['exclude'])) {
588 $this->assertNotContains($field['exclude'], $optionValues, $message);
589 }
590
591 // Ensure count of optionValues is not extraordinarily high.
592 $max = CRM_Utils_Array::value('max', $field, 10);
593 $this->assertLessThanOrEqual($max, count($optionValues), $message);
594 }
595 }
596 }
597
598 function testContactTypes() {
599 $byName = array(
600 'Individual' => 'Individual',
601 'Household' => 'Household',
602 'Organization' => 'Organization',
603 );
604 $byId = array(
605 1 => 'Individual',
606 2 => 'Household',
607 3 => 'Organization',
608 );
609 // By default this should return an array keyed by name
610 $result = CRM_Contact_DAO_Contact::buildOptions('contact_type');
611 $this->assertEquals($byName, $result);
612 // But we can also fetch by ID
613 $result = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name'));
614 $this->assertEquals($byId, $result);
615 // Make sure flip param works
616 $result = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name', 'flip' => TRUE));
617 $this->assertEquals(array_flip($byId), $result);
618 }
619 }