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