CRM-12595 fix formatting in tests files
[civicrm-core.git] / tests / phpunit / api / v3 / CustomFieldTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 /**
30 * Include class definitions
31 */
32 require_once 'tests/phpunit/CiviTest/CiviUnitTestCase.php';
33
34
35 /**
36 * Test APIv3 civicrm_create_custom_group
37 *
38 * @package CiviCRM
39 */
40 class api_v3_CustomFieldTest extends CiviUnitTestCase {
41 protected $_apiversion;
42 public $_eNoticeCompliant = TRUE;
43 function get_info() {
44 return array(
45 'name' => 'Custom Field Create',
46 'description' => 'Test all Custom Field Create API methods.',
47 'group' => 'CiviCRM API Tests',
48 );
49 }
50
51 function setUp() {
52 $this->_apiversion = 3;
53 parent::setUp();
54 }
55
56 function tearDown() {
57 $tablesToTruncate = array(
58 'civicrm_custom_group', 'civicrm_custom_field',
59 );
60 // true tells quickCleanup to drop any tables that might have been created in the test
61 $this->quickCleanup($tablesToTruncate, TRUE);
62 }
63
64 /**
65 * check with no array
66 */
67 function testCustomFieldCreateNoArray() {
68 $fieldParams = NULL;
69
70 $customField = civicrm_api('custom_field', 'create', $fieldParams);
71 $this->assertEquals($customField['is_error'], 1);
72 $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array');
73 }
74
75 /**
76 * check with no label
77 */
78 function testCustomFieldCreateWithoutLabel() {
79 $customGroup = $this->customGroupCreate('Individual', 'text_test_group', 3);
80 $params = array(
81 'custom_group_id' => $customGroup['id'],
82 'name' => 'test_textfield2',
83 'html_type' => 'Text',
84 'data_type' => 'String',
85 'default_value' => 'abc',
86 'weight' => 4,
87 'is_required' => 1,
88 'is_searchable' => 0,
89 'is_active' => 1,
90 'version' => $this->_apiversion,
91 );
92
93 $customField = civicrm_api('custom_field', 'create', $params);
94 $this->assertEquals($customField['is_error'], 1);
95 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: label');
96 }
97
98 /**
99 * check with edit
100 */
101 function testCustomFieldCreateWithEdit() {
102 $customGroup = $this->customGroupCreate('Individual', 'text_test_group', 3);
103 $params = array(
104 'custom_group_id' => $customGroup['id'],
105 'name' => 'test_textfield2',
106 'label' => 'Name1',
107 'html_type' => 'Text',
108 'data_type' => 'String',
109 'default_value' => 'abc',
110 'weight' => 4,
111 'is_required' => 1,
112 'is_searchable' => 0,
113 'is_active' => 1,
114 'version' => $this->_apiversion,
115 );
116
117 $customField = civicrm_api('custom_field', 'create', $params);
118 $params['id'] = $customField['id'];
119 $customField = civicrm_api('custom_field', 'create', $params);
120
121 $this->assertEquals($customField['is_error'], 0, 'in line ' . __LINE__);
122 $this->assertNotNull($customField['id'], 'in line ' . __LINE__);
123 }
124
125 /**
126 * check without groupId
127 */
128 function testCustomFieldCreateWithoutGroupID() {
129 $fieldParams = array(
130 'name' => 'test_textfield1',
131 'label' => 'Name',
132 'html_type' => 'Text',
133 'data_type' => 'String',
134 'default_value' => 'abc',
135 'weight' => 4,
136 'is_required' => 1,
137 'is_searchable' => 0,
138 'is_active' => 1,
139 'version' => $this->_apiversion,
140 );
141
142 $customField = civicrm_api('custom_field', 'create', $fieldParams);
143 $this->assertEquals($customField['is_error'], 1);
144 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: custom_group_id');
145 }
146
147 /**
148 * Check for Each data type: loop through available form input types
149 **/
150 function testCustomFieldCreateAllAvailableFormInputs() {
151 $gid = $this->customGroupCreate('Individual', 'testAllFormInputs');
152
153 $dtype = CRM_Core_BAO_CustomField::dataType();
154 $htype = CRM_Core_BAO_CustomField::dataToHtml();
155
156 $n = 0;
157 foreach ($dtype as $dkey => $dvalue) {
158 foreach ($htype[$n] as $hkey => $hvalue) {
159 //echo $dkey."][".$hvalue."\n";
160 $this->_loopingCustomFieldCreateTest($this->_buildParams($gid['id'], $hvalue, $dkey));
161 }
162 $n++;
163 }
164 }
165 /*
166 * Can't figure out the point of this?
167 */
168 function _loopingCustomFieldCreateTest($params) {
169 $customField = civicrm_api('custom_field', 'create', $params);
170 $this->assertEquals(0, $customField['is_error'], var_export($customField, TRUE));
171 $this->assertNotNull($customField['id']);
172 $this->getAndCheck($params, $customField['id'], 'CustomField');
173 }
174
175 function _buildParams($gid, $htype, $dtype) {
176 $params = $this->_buildBasicParams($gid, $htype, $dtype);
177 /* //Not Working for any type. Maybe redundant with testCustomFieldCreateWithOptionValues()
178 if ($htype == 'Multi-Select')
179 $params = array_merge($params, array(
180 'option_label' => array( 'Label1','Label2'),
181 'option_value' => array( 'val1', 'val2' ),
182 'option_weight' => array( 1, 2),
183 'option_status' => array( 1, 1),
184 ));
185 */
186
187
188
189 return $params;
190 }
191
192 function _buildBasicParams($gid, $htype, $dtype) {
193 return array(
194 'custom_group_id' => $gid,
195 'label' => $dtype . $htype,
196 'html_type' => $htype,
197 'data_type' => $dtype,
198 'weight' => 4,
199 'is_required' => 0,
200 'is_searchable' => 0,
201 'is_active' => 1,
202 'version' => $this->_apiversion,
203 );
204 }
205
206 /**
207 * Test using example code
208 */
209 /*function testCustomFieldCreateExample( )
210 {
211
212
213 $customGroup = $this->customGroupCreate('Individual','date_test_group',3);
214 require_once 'api/v3/examples/CustomFieldCreate.php';
215 $result = custom_field_create_example();
216 $expectedResult = custom_field_create_expectedresult();
217 $this->assertEquals($result,$expectedResult);
218 }*/
219
220 /**
221 * check with data type - Options with option_values
222 */
223 function testCustomFieldCreateWithOptionValues() {
224 $customGroup = $this->customGroupCreate('Contact', 'select_test_group', 3);
225
226 $option_values = array(
227 array('weight' => 1,
228 'label' => 'Label1',
229 'value' => 1,
230 'is_active' => 1,
231 ),
232 array(
233 'weight' => 2,
234 'label' => 'Label2',
235 'value' => 2,
236 'is_active' => 1,
237 ),
238 );
239
240 $params = array(
241 'custom_group_id' => $customGroup['id'],
242 'label' => 'Country',
243 'html_type' => 'Select',
244 'data_type' => 'String',
245 'weight' => 4,
246 'is_required' => 1,
247 'is_searchable' => 0,
248 'is_active' => 1,
249 'option_values' => $option_values,
250 'version' => $this->_apiversion,
251 );
252
253 $customField = civicrm_api('custom_field', 'create', $params);
254
255 $this->assertEquals($customField['is_error'], 0);
256 $this->assertNotNull($customField['id']);
257 $getFieldsParams = array(
258 'options' => array('get_options' => 'custom_' . $customField['id']),
259 'version' => 3,
260 'action' => 'create',
261 );
262 $description = "Demonstrate retrieving custom field options";
263 $subfile = "GetFieldsOptions";
264 $fields = civicrm_api('contact', 'getfields', $getFieldsParams);
265 $this->documentMe($getFieldsParams, $fields, __FUNCTION__, 'ContactTest.php', $description,$subfile,'GetFields');
266 $this->assertArrayHasKey('options', $fields['values']['custom_' . $customField['id']]);
267 $this->assertEquals('Label1', $fields['values']['custom_' . $customField['id']]['options'][1]);
268 $getOptionsArray = array(
269 'field' => 'custom_' . $customField['id'],
270 'version' => 3,
271 );
272 $description = "Demonstrates retrieving options for a custom field";
273 $subfile = "GetOptions";
274 $result = civicrm_api('contact', 'getoptions', $getOptionsArray);
275 $this->assertEquals('Label1', $result['values'][1]);
276 $this->documentMe($getOptionsArray, $result, __FUNCTION__, 'ContactTest.php', $description, '', 'getoptions');
277 }
278
279 ///////////////// civicrm_custom_field_delete methods
280
281 /**
282 * check with no array
283 */
284 function testCustomFieldDeleteNoArray() {
285 $params = NULL;
286 $customField = civicrm_api('custom_field', 'delete', $params);
287 $this->assertEquals($customField['is_error'], 1);
288 $this->assertEquals($customField['error_message'], 'Input variable `params` is not an array');
289 }
290
291 /**
292 * check without Field ID
293 */
294 function testCustomFieldDeleteWithoutFieldID() {
295 $params = array('version' => $this->_apiversion);
296 $customField = civicrm_api('custom_field', 'delete', $params);
297 $this->assertEquals($customField['is_error'], 1);
298 $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: id');
299 }
300
301 /**
302 * check without valid array
303 */
304 function testCustomFieldDelete() {
305 $customGroup = $this->customGroupCreate('Individual', 'test_group');
306 $customField = $this->customFieldCreate($customGroup['id'], 'test_name');
307 $this->assertNotNull($customField['id'], 'in line ' . __LINE__);
308
309 $params = array(
310 'version' => $this->_apiversion,
311 'id' => $customField['id'],
312 );
313 $result = civicrm_api('custom_field', 'delete', $params);
314 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
315
316 $this->assertEquals($result['is_error'], 0, 'in line ' . __LINE__);
317 }
318
319 /**
320 * check for Option Value
321 */
322 function testCustomFieldOptionValueDelete() {
323 $customGroup = $this->customGroupCreate('Contact', 'ABC');
324 $customOptionValueFields = $this->customFieldOptionValueCreate($customGroup, 'fieldABC');
325 $customOptionValueFields['version'] = $this->_apiversion;
326 $params = array(
327 'version' => $this->_apiversion,
328 'id' => $customOptionValueFields,
329 );
330
331 $customField = civicrm_api('custom_field', 'delete', $customOptionValueFields);
332 $this->assertEquals($customField['is_error'], 0);
333 }
334 }
335