Merge pull request #1014 from colemanw/importParser
[civicrm-core.git] / tests / phpunit / api / v3 / CustomGroupTest.php
CommitLineData
6a488035
TO
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/**
32 * Test APIv3 civicrm_custom_group* functions
33 *
34 * @package CiviCRM_APIv3
35 * @subpackage API_CustomGroup
36 */
37
38class api_v3_CustomGroupTest extends CiviUnitTestCase {
39 protected $_apiversion;
40 protected $_entity;
41 protected $_params;
42 public $_eNoticeCompliant = TRUE;
43 public $DBResetRequired = TRUE;
44
45 function get_info() {
46 return array(
47 'name' => 'Custom Group Create',
48 'description' => 'Test all Custom Group Create API methods.',
49 'group' => 'CiviCRM API Tests',
50 );
51 }
52
53 function setUp() {
54 $this->_apiversion = 3;
55 $this->_entity = 'CustomGroup';
56 $this->_params = array(
57 'title' => 'Test_Group_1',
58 'name' => 'test_group_1',
59 'extends' => 'Individual',
60 'weight' => 4,
61 'collapse_display' => 1,
62 'style' => 'Inline',
63 'help_pre' => 'This is Pre Help For Test Group 1',
64 'help_post' => 'This is Post Help For Test Group 1',
65 'is_active' => 1,
66 'version' => $this->_apiversion,
67 );
68 parent::setUp();
69 }
70
71 function tearDown() {
72 $tablesToTruncate = array('civicrm_custom_group', 'civicrm_custom_field');
73 // true tells quickCleanup to drop any tables that might have been created in the test
74 $this->quickCleanup($tablesToTruncate, TRUE);
75 }
76
77 ///////////////// civicrm_custom_group_create methods
78
79 /**
80 * check with empty array
81 */
82 function testCustomGroupCreateNoParam() {
83 $params = array(
84 'version' => $this->_apiversion,
85 );
d0e1eff2 86 $customGroup = $this->callAPIFailure('custom_group', 'create', $params);
6a488035
TO
87 $this->assertEquals($customGroup['error_message'],
88 'Mandatory key(s) missing from params array: title, extends', 'In line ' . __LINE__
89 );
90 }
91
92 /**
93 * check with empty array
94 */
95 function testCustomGroupCreateNoExtends() {
96 $params = array(
97 'domain_id' => 1,
98 'title' => 'Test_Group_1',
99 'name' => 'test_group_1',
100 'weight' => 4,
101 'collapse_display' => 1,
102 'style' => 'Tab',
103 'help_pre' => 'This is Pre Help For Test Group 1',
104 'help_post' => 'This is Post Help For Test Group 1',
105 'is_active' => 1,
106 'version' => $this->_apiversion,
107 );
108
109 $customGroup = civicrm_api('custom_group', 'create', $params);
110 $this->assertEquals($customGroup['error_message'], 'Mandatory key(s) missing from params array: extends', 'In line ' . __LINE__);
c0536a92 111 $this->assertAPIFailure($customGroup, 'In line ' . __LINE__);
6a488035
TO
112 }
113
114 /**
115 * check with empty array
116 */
117 function testCustomGroupCreateInvalidExtends() {
118 $params = array(
119 'domain_id' => 1,
120 'title' => 'Test_Group_1',
121 'name' => 'test_group_1',
122 'weight' => 4,
123 'collapse_display' => 1,
124 'style' => 'Tab',
125 'help_pre' => 'This is Pre Help For Test Group 1',
126 'help_post' => 'This is Post Help For Test Group 1',
127 'is_active' => 1,
128 'extends' => array(),
129 'version' => $this->_apiversion,
130 );
131
d0e1eff2 132 $customGroup = $this->callAPIFailure('custom_group', 'create', $params);
6a488035 133 $this->assertEquals($customGroup['error_message'], 'Mandatory key(s) missing from params array: extends', 'In line ' . __LINE__);
6a488035
TO
134 }
135
136 /**
137 * check with a string instead of array for extends
138 */
139 function testCustomGroupCreateExtendsString() {
140 $params = array(
141 'domain_id' => 1,
142 'title' => 'Test_Group_1',
143 'name' => 'test_group_1',
144 'weight' => 4,
145 'collapse_display' => 1,
146 'style' => 'Tab',
147 'help_pre' => 'This is Pre Help For Test Group 1',
148 'help_post' => 'This is Post Help For Test Group 1',
149 'is_active' => 1,
150 'extends' => 'Individual',
151 'version' => $this->_apiversion,
152 );
153
154 $customGroup = civicrm_api('custom_group', 'create', $params);
155 $this->assertApiSuccess($customGroup);
156 }
157
158 /**
159 * check with valid array
160 */
161 function testCustomGroupCreate() {
162 $params = array(
163 'title' => 'Test_Group_1',
164 'name' => 'test_group_1',
165 'extends' => array('Individual'),
166 'weight' => 4,
167 'collapse_display' => 1,
168 'style' => 'Inline',
169 'help_pre' => 'This is Pre Help For Test Group 1',
170 'help_post' => 'This is Post Help For Test Group 1',
171 'is_active' => 1,
172 'version' => $this->_apiversion,
173 );
174
175 $result = civicrm_api('custom_group', 'create', $params);
176 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
177 $this->assertAPISuccess($result);
178 $this->assertNotNull($result['id'], 'In line ' . __LINE__);
179 $this->assertEquals($result['values'][$result['id']]['extends'], 'Individual', 'In line ' . __LINE__);
180 }
aa7e7ff0 181
6a488035
TO
182 /**
183 * check with valid array
184 */
185 function testCustomGroupGetFields() {
186 $params = array(
aa7e7ff0
CW
187 'version' => $this->_apiversion,
188 'options' => array('get_options' => 'style'),
6a488035
TO
189 );
190
191 $result = civicrm_api('custom_group', 'getfields', $params);
c0536a92 192 $this->assertAPISuccess($result);
6a488035
TO
193 $this->assertEquals('Tab', $result['values']['style']['options'][0]);
194 $this->assertEquals('Inline', $result['values']['style']['options'][1]);
6a488035
TO
195 }
196
6a488035
TO
197 /**
198 * check with extends array length greater than 1
199 */
200 function testCustomGroupExtendsMultipleCreate() {
201 $params = array(
202 'title' => 'Test_Group_1',
203 'name' => 'test_group_1',
204 'extends' => array('Individual', 'Household'),
205 'weight' => 4,
206 'collapse_display' => 1,
207 'style' => 'Inline',
208 'help_pre' => 'This is Pre Help For Test Group 1',
209 'help_post' => 'This is Post Help For Test Group 1',
210 'is_active' => 1,
211 'version' => $this->_apiversion,
212 );
213
d0e1eff2 214 $result = $this->callAPIFailure('custom_group', 'create', $params);
6a488035
TO
215 $this->assertEquals($result['error_message'], 'implode(): Invalid arguments passed', 'In line ' . __LINE__);
216 }
217
218 /**
219 * check with style missing from params array
220 */
221 function testCustomGroupCreateNoStyle() {
222 $params = array(
223 'title' => 'Test_Group_1',
224 'name' => 'test_group_1',
225 'extends' => array('Individual'),
226 'weight' => 4,
227 'collapse_display' => 1,
228 'help_pre' => 'This is Pre Help For Test Group 1',
229 'help_post' => 'This is Post Help For Test Group 1',
230 'is_active' => 1,
231 'version' => $this->_apiversion,
232 );
233
234 $customGroup = civicrm_api('custom_group', 'create', $params);
c0536a92 235 $this->assertAPISuccess($customGroup, 'In line ' . __LINE__);
6a488035
TO
236 $this->assertNotNull($customGroup['id'], 'In line ' . __LINE__);
237 $this->assertEquals($customGroup['values'][$customGroup['id']]['style'], 'Inline', 'In line ' . __LINE__);
238 }
239
240 /**
241 * check with not array
242 */
243 function testCustomGroupCreateNotArray() {
244 $params = NULL;
d0e1eff2 245 $customGroup = $this->callAPIFailure('custom_group', 'create', $params);
6a488035
TO
246 $this->assertEquals($customGroup['error_message'], 'Input variable `params` is not an array', 'In line ' . __LINE__);
247 }
248
249 /**
250 * check without title
251 */
252 function testCustomGroupCreateNoTitle() {
253 $params = array('extends' => array('Contact'),
254 'weight' => 5,
255 'collapse_display' => 1,
256 'style' => 'Tab',
257 'help_pre' => 'This is Pre Help For Test Group 2',
258 'help_post' => 'This is Post Help For Test Group 2',
259 'version' => $this->_apiversion,
260 );
261
262 $customGroup = civicrm_api('custom_group', 'create', $params);
263 $this->assertEquals($customGroup['error_message'], 'Mandatory key(s) missing from params array: title', 'In line ' . __LINE__);
c0536a92 264 $this->assertAPIFailure($customGroup, 'In line ' . __LINE__);
6a488035
TO
265 }
266
267 /**
268 * check for household without weight
269 */
270 function testCustomGroupCreateHouseholdNoWeight() {
271 $params = array(
272 'title' => 'Test_Group_3',
273 'name' => 'test_group_3',
274 'extends' => array('Household'),
275 'collapse_display' => 1,
276 'style' => 'Tab',
277 'help_pre' => 'This is Pre Help For Test Group 3',
278 'help_post' => 'This is Post Help For Test Group 3',
279 'is_active' => 1,
280 'version' => $this->_apiversion,
281 );
282
283 $customGroup = civicrm_api('custom_group', 'create', $params);
c0536a92 284 $this->assertAPISuccess($customGroup, 'In line ' . __LINE__);
6a488035
TO
285 $this->assertNotNull($customGroup['id'], 'In line ' . __LINE__);
286 $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Household', 'In line ' . __LINE__);
287 $this->assertEquals($customGroup['values'][$customGroup['id']]['style'], 'Tab', 'In line ' . __LINE__);
288 }
289
290 /**
291 * check for Contribution Donation
292 */
293 function testCustomGroupCreateContributionDonation() {
294 $params = array(
295 'title' => 'Test_Group_6',
296 'name' => 'test_group_6',
297 'extends' => array('Contribution', array(1)),
298 'weight' => 6,
299 'collapse_display' => 1,
300 'style' => 'Inline',
301 'help_pre' => 'This is Pre Help For Test Group 6',
302 'help_post' => 'This is Post Help For Test Group 6',
303 'is_active' => 1,
304 'version' => $this->_apiversion,
305 );
306
307 $customGroup = civicrm_api('custom_group', 'create', $params);
c0536a92 308 $this->assertAPISuccess($customGroup, 'In line ' . __LINE__);
6a488035
TO
309 $this->assertNotNull($customGroup['id'], 'In line ' . __LINE__);
310 $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Contribution', 'In line ' . __LINE__);
311 }
312
313 /**
314 * check with valid array
315 */
316 function testCustomGroupCreateGroup() {
317 $params = array(
318 'domain_id' => 1,
319 'title' => 'Test_Group_8',
320 'name' => 'test_group_8',
321 'extends' => array('Group'),
322 'weight' => 7,
323 'collapse_display' => 1,
324 'is_active' => 1,
325 'style' => 'Inline',
326 'help_pre' => 'This is Pre Help For Test Group 8',
327 'help_post' => 'This is Post Help For Test Group 8',
328 'version' => $this->_apiversion,
329 );
330
331 $customGroup = civicrm_api('custom_group', 'create', $params);
c0536a92 332 $this->assertAPISuccess($customGroup, 'In line ' . __LINE__);
6a488035
TO
333 $this->assertNotNull($customGroup['id'], 'In line ' . __LINE__);
334 $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Group', 'In line ' . __LINE__);
335 }
336
337 /**
338 * check with Activity - Meeting Type
339 */
340 function testCustomGroupCreateActivityMeeting() {
341 $params = array(
342 'title' => 'Test_Group_10',
343 'name' => 'test_group_10',
344 'extends' => array('Activity', array(1)),
345 'weight' => 8,
346 'collapse_display' => 1,
347 'style' => 'Inline',
348 'help_pre' => 'This is Pre Help For Test Group 10',
349 'help_post' => 'This is Post Help For Test Group 10',
350 'version' => $this->_apiversion,
351 );
352
353 $customGroup = civicrm_api('custom_group', 'create', $params);
c0536a92 354 $this->assertAPISuccess($customGroup, 'In line ' . __LINE__);
6a488035
TO
355 $this->assertNotNull($customGroup['id'], 'In line ' . __LINE__);
356 $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Activity', 'In line ' . __LINE__);
357 }
358
359 ///////////////// civicrm_custom_group_delete methods
360
361 /**
362 * check without GroupID
363 */
364 function testCustomGroupDeleteWithoutGroupID() {
d0e1eff2 365 $customGroup = $this->callAPIFailure('custom_group', 'delete', array());
6a488035
TO
366 $this->assertEquals($customGroup['error_message'], 'Mandatory key(s) missing from params array: id', 'In line ' . __LINE__);
367 }
368
369 /**
370 * check with no array
371 */
372 function testCustomGroupDeleteNoArray() {
373 $params = NULL;
d0e1eff2 374 $customGroup = $this->callAPIFailure('custom_group', 'delete', $params);
6a488035
TO
375 $this->assertEquals($customGroup['error_message'], 'Input variable `params` is not an array', 'In line ' . __LINE__);
376 }
377
378 /**
379 * check with valid custom group id
380 */
381 function testCustomGroupDelete() {
382 $customGroup = $this->customGroupCreate('Individual', 'test_group');
383 $params = array(
384 'id' => $customGroup['id'],
385 'version' => $this->_apiversion,
386 );
387 $result = civicrm_api('custom_group', 'delete', $params);
388 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
c0536a92 389 $this->assertAPISuccess($result, 'In line ' . __LINE__);
6a488035
TO
390 }
391 /*
392 * main success get function
393 */
394
395
396
397 public function testGetCustomGroupSuccess() {
398
399 civicrm_api($this->_entity, 'create', $this->_params);
400 $params = array('version' => 3);
401 $result = civicrm_api($this->_entity, 'get', $params);
402 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
c0536a92 403 $this->assertAPISuccess($result, 'In line ' . __LINE__);
6a488035
TO
404 $values = $result['values'][$result['id']];
405 foreach ($this->_params as $key => $value) {
406 if ($key == 'version' || $key == 'weight') {
407 continue;
408 }
409 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__);
410 }
411 }
412}
413