CRM Test Fixes
[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 ),
d9136461 280 array(
281 'fieldName' => 'soft_credit_type_id',
282 'sample' => 'In Honor of',
283 ),
bd44e0df 284 ),
bd44e0df
AS
285 'CRM_Contribute_DAO_Product' => array(
286 array(
287 'fieldName' => 'currency',
a38a89fc 288 'sample' => array('USD' => 'US Dollar'),
bd44e0df
AS
289 'max' => 200,
290 ),
291 ),
bd44e0df
AS
292 'CRM_Contribute_DAO_ContributionRecur' => array(
293 array(
294 'fieldName' => 'currency',
a38a89fc 295 'sample' => array('USD' => 'US Dollar'),
bd44e0df
AS
296 'max' => 200,
297 ),
298 ),
5ccc09d8 299 'CRM_Pledge_DAO_PledgePayment' => array(
bd44e0df
AS
300 array(
301 'fieldName' => 'currency',
a38a89fc 302 'sample' => array('USD' => 'US Dollar'),
bd44e0df
AS
303 'max' => 200,
304 ),
305 ),
306 'CRM_Pledge_DAO_Pledge' => array(
307 array(
308 'fieldName' => 'currency',
a38a89fc 309 'sample' => array('USD' => 'US Dollar'),
bd44e0df
AS
310 'max' => 200,
311 ),
312 ),
313 'CRM_PCP_DAO_PCP' => array(
314 array(
315 'fieldName' => 'currency',
a38a89fc 316 'sample' => array('USD' => 'US Dollar'),
bd44e0df
AS
317 'max' => 200,
318 ),
6d68a4cb
CW
319 array(
320 'fieldName' => 'status_id',
321 'sample' => 'Approved',
322 ),
bd44e0df 323 ),
cd43c5e3
AS
324 'CRM_Core_DAO_CustomField' => array(
325 array(
326 'fieldName' => 'custom_group_id',
327 'sample' => $custom_group_name,
328 ),
329 ),
330 'CRM_Core_DAO_EntityTag' => array(
331 array(
332 'fieldName' => 'tag_id',
333 'sample' => 'Government Entity',
334 ),
335 ),
8f785c9e
AS
336 'CRM_Core_DAO_OptionValue' => array(
337 array(
338 'fieldName' => 'component_id',
339 'sample' => 'CiviContribute',
340 ),
341 ),
cbf48754
AS
342 'CRM_Core_DAO_MailSettings' => array(
343 array(
344 'fieldName' => 'protocol',
345 'sample' => 'Localdir',
346 ),
347 ),
348 'CRM_Core_DAO_Mapping' => array(
349 array(
350 'fieldName' => 'mapping_type_id',
351 'sample' => 'Search Builder',
352 'max' => 15,
353 ),
354 ),
e7e657f0
AS
355 'CRM_Core_DAO_Phone' => array(
356 array(
357 'fieldName' => 'phone_type_id',
358 'sample' => 'Phone',
359 ),
360 array(
361 'fieldName' => 'location_type_id',
362 'sample' => 'Home',
363 ),
364 ),
365 'CRM_Core_DAO_Email' => array(
366 array(
367 'fieldName' => 'location_type_id',
368 'sample' => 'Home',
369 ),
370 ),
371 'CRM_Core_DAO_Address' => array(
372 array(
373 'fieldName' => 'location_type_id',
374 'sample' => 'Home',
375 ),
376 ),
cbf48754
AS
377 'CRM_Core_DAO_Website' => array(
378 array(
379 'fieldName' => 'website_type_id',
380 'sample' => 'Facebook',
381 ),
382 ),
383 'CRM_Core_DAO_MappingField' => array(
384 array(
385 'fieldName' => 'website_type_id',
386 'sample' => 'Facebook',
387 ),
e7e657f0
AS
388 array(
389 'fieldName' => 'im_provider_id',
390 'sample' => 'Yahoo',
391 ),
cbf48754 392 ),
398f49ab
AS
393 'CRM_Contact_DAO_Contact' => array(
394 array(
395 'fieldName' => 'prefix_id',
396 'sample' => 'Mr.',
397 ),
398 array(
399 'fieldName' => 'suffix_id',
400 'sample' => 'Sr.',
401 ),
402 array(
403 'fieldName' => 'gender_id',
404 'sample' => 'Male',
405 ),
398f49ab 406 array(
e7e657f0
AS
407 'fieldName' => 'preferred_communication_method',
408 'sample' => 'Postal Mail',
398f49ab 409 ),
091fe2a8
CW
410 array(
411 'fieldName' => 'contact_type',
412 'sample' => 'Individual',
413 'exclude' => 'Team',
414 ),
415 array(
416 'fieldName' => 'contact_sub_type',
417 'sample' => 'Team',
418 'exclude' => 'Individual',
419 ),
c0c9cd82
CW
420 array(
421 'fieldName' => 'preferred_language',
422 'sample' => array('en_US' => 'English (United States)'),
423 'max' => 250,
424 ),
deceed83
CW
425 array(
426 'fieldName' => "custom_$customFieldId",
427 'sample' => array('foo' => 'Foo'),
428 'max' => 1,
429 ),
398f49ab 430 ),
e7e657f0 431 'CRM_Batch_DAO_Batch' => array(
398f49ab 432 array(
e7e657f0
AS
433 'fieldName' => 'type_id',
434 'sample' => 'Membership',
398f49ab 435 ),
398f49ab 436 array(
e7e657f0
AS
437 'fieldName' => 'status_id',
438 'sample' => 'Reopened',
398f49ab 439 ),
cbf48754 440 array(
e7e657f0
AS
441 'fieldName' => 'mode_id',
442 'sample' => 'Automatic Batch',
cbf48754
AS
443 ),
444 ),
e7e657f0 445 'CRM_Core_DAO_IM' => array(
cbf48754 446 array(
e7e657f0
AS
447 'fieldName' => 'provider_id',
448 'sample' => 'Yahoo',
cbf48754
AS
449 ),
450 ),
2158332a
CW
451 'CRM_Event_DAO_Participant' => array(
452 array(
453 'fieldName' => 'status_id',
454 'sample' => 'Registered',
455 ),
456 array(
457 'fieldName' => 'role_id',
458 'sample' => 'Speaker',
459 ),
6d68a4cb
CW
460 array(
461 'fieldName' => 'fee_currency',
a38a89fc 462 'sample' => array('USD' => 'US Dollar'),
6d68a4cb
CW
463 'max' => 200,
464 ),
2158332a
CW
465 ),
466 'CRM_Event_DAO_Event' => array(
467 array(
468 'fieldName' => 'event_type_id',
469 'sample' => 'Fundraiser',
470 ),
2158332a 471 array(
6d68a4cb
CW
472 'fieldName' => 'payment_processor',
473 'sample' => $pp_name,
474 ),
475 array(
476 'fieldName' => 'financial_type_id',
477 'sample' => 'Donation',
478 ),
479 array(
480 'fieldName' => 'currency',
a38a89fc 481 'sample' => array('USD' => 'US Dollar'),
6d68a4cb 482 'max' => 200,
2158332a
CW
483 ),
484 ),
f899561e
CW
485 'CRM_Member_DAO_Membership' => array(
486 array(
487 'fieldName' => 'status_id',
488 'sample' => 'New',
489 ),
6ce01b02
CW
490 ),
491 'CRM_Mailing_DAO_Mailing' => array(
f899561e 492 array(
6ce01b02
CW
493 'fieldName' => 'approval_status_id',
494 'sample' => 'Approved',
f899561e
CW
495 ),
496 ),
fb1fd730
CW
497 'CRM_Grant_DAO_Grant' => array(
498 array(
499 'fieldName' => 'status_id',
500 'sample' => 'Approved',
501 ),
502 array(
503 'fieldName' => 'grant_type_id',
504 'sample' => 'Emergency',
505 ),
6d68a4cb
CW
506 array(
507 'fieldName' => 'currency',
a38a89fc 508 'sample' => array('USD' => 'US Dollar'),
6d68a4cb
CW
509 'max' => 200,
510 ),
511 ),
512 'CRM_Contribute_DAO_Contribution' => array(
513 array(
514 'fieldName' => 'payment_instrument_id',
515 'sample' => 'Credit Card',
516 ),
517 array(
518 'fieldName' => 'financial_type_id',
519 'sample' => 'Donation',
520 ),
521 array(
522 'fieldName' => 'currency',
a38a89fc 523 'sample' => array('USD' => 'US Dollar'),
6d68a4cb
CW
524 'max' => 200,
525 ),
526 array(
527 'fieldName' => 'contribution_status_id',
528 'sample' => 'Completed',
529 ),
6d68a4cb
CW
530 ),
531 'CRM_Contribute_DAO_ContributionPage' => array(
532 array(
533 'fieldName' => 'payment_processor',
534 'sample' => $pp_name,
535 ),
536 array(
537 'fieldName' => 'financial_type_id',
538 'sample' => 'Donation',
539 ),
540 array(
541 'fieldName' => 'currency',
a38a89fc 542 'sample' => array('USD' => 'US Dollar'),
6d68a4cb
CW
543 'max' => 200,
544 ),
fb1fd730 545 ),
3f3a3ba0
CW
546 'CRM_Case_DAO_Case' => array(
547 array(
548 'fieldName' => 'status_id',
549 'sample' => 'Ongoing',
550 ),
551 ),
887a4028 552 );
398f49ab
AS
553
554 foreach ($fields as $daoName => $daoFields) {
555 foreach ($daoFields as $field) {
a42ef93c 556 $message = "DAO name: '{$daoName}', field: '{$field['fieldName']}'";
398f49ab 557
6d68a4cb 558 $optionValues = $daoName::buildOptions($field['fieldName']);
091fe2a8
CW
559 $this->assertNotEmpty($optionValues, $message);
560
561 // Ensure sample value is contained in the returned optionValues.
c0c9cd82
CW
562 if (!is_array($field['sample'])) {
563 $this->assertContains($field['sample'], $optionValues, $message);
564 }
565 // If sample is an array, we check keys and values
566 else {
567 foreach ($field['sample'] as $key => $value) {
568 $this->assertArrayHasKey($key, $optionValues, $message);
569 $this->assertEquals(CRM_Utils_Array::value($key, $optionValues), $value, $message);
570 }
571 }
398f49ab 572
6ce01b02 573 // Ensure exclude value is not contained in the optionValues
091fe2a8
CW
574 if (!empty($field['exclude'])) {
575 $this->assertNotContains($field['exclude'], $optionValues, $message);
576 }
577
398f49ab
AS
578 // Ensure count of optionValues is not extraordinarily high.
579 $max = CRM_Utils_Array::value('max', $field, 10);
580 $this->assertLessThanOrEqual($max, count($optionValues), $message);
581 }
582 }
887a4028 583 }
091fe2a8
CW
584
585 function testContactTypes() {
586 $byName = array(
587 'Individual' => 'Individual',
588 'Household' => 'Household',
589 'Organization' => 'Organization',
590 );
591 $byId = array(
592 1 => 'Individual',
593 2 => 'Household',
594 3 => 'Organization',
595 );
a1ef51e2 596 // By default this should return an array keyed by name
c0c9cd82 597 $result = CRM_Contact_DAO_Contact::buildOptions('contact_type');
091fe2a8
CW
598 $this->assertEquals($byName, $result);
599 // But we can also fetch by ID
600 $result = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name'));
601 $this->assertEquals($byId, $result);
602 // Make sure flip param works
603 $result = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name', 'flip' => TRUE));
604 $this->assertEquals(array_flip($byId), $result);
605 }
887a4028 606}