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