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