CRM-13890 - Test fixes (unit tests were coded against the buggy behavior where real...
[civicrm-core.git] / tests / phpunit / CRM / Core / PseudoConstantTest.php
CommitLineData
887a4028
A
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
887a4028
A
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
28require_once 'CiviTest/CiviUnitTestCase.php';
29
30/**
2158332a 31 * Tests for pseudoconstant retrieval
887a4028
A
32 */
33class 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
cd43c5e3
AS
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 */
398f49ab 50 function testOptionValues() {
bd44e0df 51
29494eef 52 // Create a custom field group for testing.
bd44e0df 53 $custom_group_name = md5(microtime());
cd43c5e3 54 $api_params = array(
bd44e0df
AS
55 'version' => 3,
56 'title' => $custom_group_name,
57 'extends' => 'Individual',
58 'is_active' => TRUE,
cd43c5e3 59 );
bd44e0df 60 $result = civicrm_api('customGroup', 'create', $api_params);
6d68a4cb 61 $this->assertAPISuccess($result);
cd43c5e3 62
deceed83
CW
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.
29494eef
AS
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);
6d68a4cb
CW
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);
29494eef 108
cd43c5e3 109 /**
091fe2a8 110 * daoName/field combinations to test
cd43c5e3
AS
111 * Format: array[DAO Name] = $properties, where properties is an array whose
112 * named members can be:
091fe2a8
CW
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 */
398f49ab 118 $fields = array(
29494eef
AS
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 ),
4080e473
AS
155 'CRM_Activity_DAO_Activity' => array(
156 array(
157 'fieldName' => 'activity_type_id',
a38a89fc 158 'sample' => 'Email',
4080e473
AS
159 'max' => 50,
160 ),
161 array(
162 'fieldName' => 'status_id',
163 'sample' => 'Scheduled',
164 ),
6d68a4cb
CW
165 array(
166 'fieldName' => 'priority_id',
167 'sample' => 'Urgent',
168 ),
3f3a3ba0
CW
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 ),
4080e473
AS
184 ),
185 'CRM_Campaign_DAO_Survey' => array(
186 array(
187 'fieldName' => 'activity_type_id',
a38a89fc 188 'sample' => 'Phone Call',
4080e473
AS
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 ),
9da8dc8c 204 'CRM_Price_DAO_PriceField' => array(
4080e473
AS
205 array(
206 'fieldName' => 'visibility_id',
207 'sample' => 'Public',
208 ),
209 ),
7611ae71
AS
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 ),
6d68a4cb
CW
231 array(
232 'fieldName' => 'currency',
a38a89fc 233 'sample' => array('USD' => 'US Dollar'),
6d68a4cb
CW
234 'max' => 200,
235 ),
7611ae71
AS
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 ),
6d68a4cb
CW
248 array(
249 'fieldName' => 'currency',
a38a89fc 250 'sample' => array('USD' => 'US Dollar'),
6d68a4cb
CW
251 'max' => 200,
252 ),
7611ae71
AS
253 ),
254 'CRM_Financial_DAO_FinancialAccount' => array(
255 array(
256 'fieldName' => 'financial_account_type_id',
257 'sample' => 'Cost of Sales',
258 ),
259 ),
bd44e0df
AS
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',
a38a89fc 277 'sample' => array('USD' => 'US Dollar'),
bd44e0df
AS
278 'max' => 200,
279 ),
280 ),
bd44e0df
AS
281 'CRM_Contribute_DAO_Product' => array(
282 array(
283 'fieldName' => 'currency',
a38a89fc 284 'sample' => array('USD' => 'US Dollar'),
bd44e0df
AS
285 'max' => 200,
286 ),
287 ),
bd44e0df
AS
288 'CRM_Contribute_DAO_ContributionRecur' => array(
289 array(
290 'fieldName' => 'currency',
a38a89fc 291 'sample' => array('USD' => 'US Dollar'),
bd44e0df
AS
292 'max' => 200,
293 ),
294 ),
5ccc09d8 295 'CRM_Pledge_DAO_PledgePayment' => array(
bd44e0df
AS
296 array(
297 'fieldName' => 'currency',
a38a89fc 298 'sample' => array('USD' => 'US Dollar'),
bd44e0df
AS
299 'max' => 200,
300 ),
301 ),
302 'CRM_Pledge_DAO_Pledge' => array(
303 array(
304 'fieldName' => 'currency',
a38a89fc 305 'sample' => array('USD' => 'US Dollar'),
bd44e0df
AS
306 'max' => 200,
307 ),
308 ),
309 'CRM_PCP_DAO_PCP' => array(
310 array(
311 'fieldName' => 'currency',
a38a89fc 312 'sample' => array('USD' => 'US Dollar'),
bd44e0df
AS
313 'max' => 200,
314 ),
6d68a4cb
CW
315 array(
316 'fieldName' => 'status_id',
317 'sample' => 'Approved',
318 ),
bd44e0df 319 ),
cd43c5e3
AS
320 'CRM_Core_DAO_CustomField' => array(
321 array(
322 'fieldName' => 'custom_group_id',
323 'sample' => $custom_group_name,
324 ),
325 ),
326 'CRM_Core_DAO_EntityTag' => array(
327 array(
328 'fieldName' => 'tag_id',
329 'sample' => 'Government Entity',
330 ),
331 ),
8f785c9e
AS
332 'CRM_Core_DAO_OptionValue' => array(
333 array(
334 'fieldName' => 'component_id',
335 'sample' => 'CiviContribute',
336 ),
337 ),
cbf48754
AS
338 'CRM_Core_DAO_MailSettings' => array(
339 array(
340 'fieldName' => 'protocol',
341 'sample' => 'Localdir',
342 ),
343 ),
344 'CRM_Core_DAO_Mapping' => array(
345 array(
346 'fieldName' => 'mapping_type_id',
347 'sample' => 'Search Builder',
348 'max' => 15,
349 ),
350 ),
351 'CRM_Pledge_DAO_Pledge' => array(
352 array(
353 'fieldName' => 'honor_type_id',
354 'sample' => 'In Honor of',
355 ),
356 ),
e7e657f0
AS
357 'CRM_Core_DAO_Phone' => array(
358 array(
359 'fieldName' => 'phone_type_id',
360 'sample' => 'Phone',
361 ),
362 array(
363 'fieldName' => 'location_type_id',
364 'sample' => 'Home',
365 ),
366 ),
367 'CRM_Core_DAO_Email' => array(
368 array(
369 'fieldName' => 'location_type_id',
370 'sample' => 'Home',
371 ),
372 ),
373 'CRM_Core_DAO_Address' => array(
374 array(
375 'fieldName' => 'location_type_id',
376 'sample' => 'Home',
377 ),
378 ),
cbf48754
AS
379 'CRM_Core_DAO_Website' => array(
380 array(
381 'fieldName' => 'website_type_id',
382 'sample' => 'Facebook',
383 ),
384 ),
385 'CRM_Core_DAO_MappingField' => array(
386 array(
387 'fieldName' => 'website_type_id',
388 'sample' => 'Facebook',
389 ),
e7e657f0
AS
390 array(
391 'fieldName' => 'im_provider_id',
392 'sample' => 'Yahoo',
393 ),
cbf48754 394 ),
398f49ab
AS
395 'CRM_Contact_DAO_Contact' => array(
396 array(
397 'fieldName' => 'prefix_id',
398 'sample' => 'Mr.',
399 ),
400 array(
401 'fieldName' => 'suffix_id',
402 'sample' => 'Sr.',
403 ),
404 array(
405 'fieldName' => 'gender_id',
406 'sample' => 'Male',
407 ),
398f49ab 408 array(
e7e657f0
AS
409 'fieldName' => 'preferred_communication_method',
410 'sample' => 'Postal Mail',
398f49ab 411 ),
091fe2a8
CW
412 array(
413 'fieldName' => 'contact_type',
414 'sample' => 'Individual',
415 'exclude' => 'Team',
416 ),
417 array(
418 'fieldName' => 'contact_sub_type',
419 'sample' => 'Team',
420 'exclude' => 'Individual',
421 ),
c0c9cd82
CW
422 array(
423 'fieldName' => 'preferred_language',
424 'sample' => array('en_US' => 'English (United States)'),
425 'max' => 250,
426 ),
deceed83
CW
427 array(
428 'fieldName' => "custom_$customFieldId",
429 'sample' => array('foo' => 'Foo'),
430 'max' => 1,
431 ),
398f49ab 432 ),
e7e657f0 433 'CRM_Batch_DAO_Batch' => array(
398f49ab 434 array(
e7e657f0
AS
435 'fieldName' => 'type_id',
436 'sample' => 'Membership',
398f49ab 437 ),
398f49ab 438 array(
e7e657f0
AS
439 'fieldName' => 'status_id',
440 'sample' => 'Reopened',
398f49ab 441 ),
cbf48754 442 array(
e7e657f0
AS
443 'fieldName' => 'mode_id',
444 'sample' => 'Automatic Batch',
cbf48754
AS
445 ),
446 ),
e7e657f0 447 'CRM_Core_DAO_IM' => array(
cbf48754 448 array(
e7e657f0
AS
449 'fieldName' => 'provider_id',
450 'sample' => 'Yahoo',
cbf48754
AS
451 ),
452 ),
2158332a
CW
453 'CRM_Event_DAO_Participant' => array(
454 array(
455 'fieldName' => 'status_id',
456 'sample' => 'Registered',
457 ),
458 array(
459 'fieldName' => 'role_id',
460 'sample' => 'Speaker',
461 ),
6d68a4cb
CW
462 array(
463 'fieldName' => 'fee_currency',
a38a89fc 464 'sample' => array('USD' => 'US Dollar'),
6d68a4cb
CW
465 'max' => 200,
466 ),
2158332a
CW
467 ),
468 'CRM_Event_DAO_Event' => array(
469 array(
470 'fieldName' => 'event_type_id',
471 'sample' => 'Fundraiser',
472 ),
2158332a 473 array(
6d68a4cb
CW
474 'fieldName' => 'payment_processor',
475 'sample' => $pp_name,
476 ),
477 array(
478 'fieldName' => 'financial_type_id',
479 'sample' => 'Donation',
480 ),
481 array(
482 'fieldName' => 'currency',
a38a89fc 483 'sample' => array('USD' => 'US Dollar'),
6d68a4cb 484 'max' => 200,
2158332a
CW
485 ),
486 ),
f899561e
CW
487 'CRM_Member_DAO_Membership' => array(
488 array(
489 'fieldName' => 'status_id',
490 'sample' => 'New',
491 ),
6ce01b02
CW
492 ),
493 'CRM_Mailing_DAO_Mailing' => array(
f899561e 494 array(
6ce01b02
CW
495 'fieldName' => 'approval_status_id',
496 'sample' => 'Approved',
f899561e
CW
497 ),
498 ),
fb1fd730
CW
499 'CRM_Grant_DAO_Grant' => array(
500 array(
501 'fieldName' => 'status_id',
502 'sample' => 'Approved',
503 ),
504 array(
505 'fieldName' => 'grant_type_id',
506 'sample' => 'Emergency',
507 ),
6d68a4cb
CW
508 array(
509 'fieldName' => 'currency',
a38a89fc 510 'sample' => array('USD' => 'US Dollar'),
6d68a4cb
CW
511 'max' => 200,
512 ),
513 ),
514 'CRM_Contribute_DAO_Contribution' => array(
515 array(
516 'fieldName' => 'payment_instrument_id',
517 'sample' => 'Credit Card',
518 ),
519 array(
520 'fieldName' => 'financial_type_id',
521 'sample' => 'Donation',
522 ),
523 array(
524 'fieldName' => 'currency',
a38a89fc 525 'sample' => array('USD' => 'US Dollar'),
6d68a4cb
CW
526 'max' => 200,
527 ),
528 array(
529 'fieldName' => 'contribution_status_id',
530 'sample' => 'Completed',
531 ),
532 array(
533 'fieldName' => 'honor_type_id',
534 'sample' => 'In Honor of',
535 ),
536 ),
537 'CRM_Contribute_DAO_ContributionPage' => array(
538 array(
539 'fieldName' => 'payment_processor',
540 'sample' => $pp_name,
541 ),
542 array(
543 'fieldName' => 'financial_type_id',
544 'sample' => 'Donation',
545 ),
546 array(
547 'fieldName' => 'currency',
a38a89fc 548 'sample' => array('USD' => 'US Dollar'),
6d68a4cb
CW
549 'max' => 200,
550 ),
fb1fd730 551 ),
3f3a3ba0
CW
552 'CRM_Case_DAO_Case' => array(
553 array(
554 'fieldName' => 'status_id',
555 'sample' => 'Ongoing',
556 ),
557 ),
887a4028 558 );
398f49ab
AS
559
560 foreach ($fields as $daoName => $daoFields) {
561 foreach ($daoFields as $field) {
a42ef93c 562 $message = "DAO name: '{$daoName}', field: '{$field['fieldName']}'";
398f49ab 563
6d68a4cb 564 $optionValues = $daoName::buildOptions($field['fieldName']);
091fe2a8
CW
565 $this->assertNotEmpty($optionValues, $message);
566
567 // Ensure sample value is contained in the returned optionValues.
c0c9cd82
CW
568 if (!is_array($field['sample'])) {
569 $this->assertContains($field['sample'], $optionValues, $message);
570 }
571 // If sample is an array, we check keys and values
572 else {
573 foreach ($field['sample'] as $key => $value) {
574 $this->assertArrayHasKey($key, $optionValues, $message);
575 $this->assertEquals(CRM_Utils_Array::value($key, $optionValues), $value, $message);
576 }
577 }
398f49ab 578
6ce01b02 579 // Ensure exclude value is not contained in the optionValues
091fe2a8
CW
580 if (!empty($field['exclude'])) {
581 $this->assertNotContains($field['exclude'], $optionValues, $message);
582 }
583
398f49ab
AS
584 // Ensure count of optionValues is not extraordinarily high.
585 $max = CRM_Utils_Array::value('max', $field, 10);
586 $this->assertLessThanOrEqual($max, count($optionValues), $message);
587 }
588 }
887a4028 589 }
091fe2a8
CW
590
591 function testContactTypes() {
592 $byName = array(
593 'Individual' => 'Individual',
594 'Household' => 'Household',
595 'Organization' => 'Organization',
596 );
597 $byId = array(
598 1 => 'Individual',
599 2 => 'Household',
600 3 => 'Organization',
601 );
a1ef51e2 602 // By default this should return an array keyed by name
c0c9cd82 603 $result = CRM_Contact_DAO_Contact::buildOptions('contact_type');
091fe2a8
CW
604 $this->assertEquals($byName, $result);
605 // But we can also fetch by ID
606 $result = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name'));
607 $this->assertEquals($byId, $result);
608 // Make sure flip param works
609 $result = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name', 'flip' => TRUE));
610 $this->assertEquals(array_flip($byId), $result);
611 }
887a4028 612}