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