Added method fieldKeys() to DAOs. CRM-12464
[civicrm-core.git] / tests / phpunit / CRM / Core / PseudoConstantTest.php
CommitLineData
887a4028
A
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
28require_once 'CiviTest/CiviUnitTestCase.php';
29
30/**
31 * Tests for linking to resource files
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() {
cd43c5e3
AS
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 /**
091fe2a8 60 * daoName/field combinations to test
cd43c5e3
AS
61 * Format: array[DAO Name] = $properties, where properties is an array whose
62 * named members can be:
091fe2a8
CW
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 */
398f49ab 68 $fields = array(
cd43c5e3
AS
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 ),
8f785c9e
AS
81 'CRM_Core_DAO_OptionValue' => array(
82 array(
83 'fieldName' => 'component_id',
84 'sample' => 'CiviContribute',
85 ),
86 ),
cbf48754
AS
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 ),
e7e657f0
AS
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 ),
cbf48754
AS
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 ),
e7e657f0
AS
157 array(
158 'fieldName' => 'im_provider_id',
159 'sample' => 'Yahoo',
160 ),
cbf48754 161 ),
398f49ab
AS
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 ),
398f49ab 175 array(
e7e657f0
AS
176 'fieldName' => 'preferred_communication_method',
177 'sample' => 'Postal Mail',
398f49ab 178 ),
091fe2a8
CW
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 ),
398f49ab 189 ),
e7e657f0 190 'CRM_Batch_DAO_Batch' => array(
398f49ab 191 array(
e7e657f0
AS
192 'fieldName' => 'type_id',
193 'sample' => 'Membership',
398f49ab 194 ),
398f49ab 195 array(
e7e657f0
AS
196 'fieldName' => 'status_id',
197 'sample' => 'Reopened',
398f49ab 198 ),
cbf48754 199 array(
e7e657f0
AS
200 'fieldName' => 'mode_id',
201 'sample' => 'Automatic Batch',
cbf48754
AS
202 ),
203 ),
e7e657f0 204 'CRM_Core_DAO_IM' => array(
cbf48754 205 array(
e7e657f0
AS
206 'fieldName' => 'provider_id',
207 'sample' => 'Yahoo',
cbf48754
AS
208 ),
209 ),
887a4028 210 );
398f49ab
AS
211
212 foreach ($fields as $daoName => $daoFields) {
213 foreach ($daoFields as $field) {
a42ef93c 214 $message = "DAO name: '{$daoName}', field: '{$field['fieldName']}'";
398f49ab 215
398f49ab 216 $optionValues = CRM_Core_PseudoConstant::get($daoName, $field['fieldName']);
091fe2a8
CW
217 $this->assertNotEmpty($optionValues, $message);
218
219 // Ensure sample value is contained in the returned optionValues.
398f49ab
AS
220 $this->assertContains($field['sample'], $optionValues, $message);
221
091fe2a8
CW
222 // Exclude test
223 if (!empty($field['exclude'])) {
224 $this->assertNotContains($field['exclude'], $optionValues, $message);
225 }
226
398f49ab
AS
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 }
887a4028 232 }
091fe2a8
CW
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 );
a1ef51e2 245 // By default this should return an array keyed by name
091fe2a8
CW
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 }
887a4028 255}