3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
13 * Test APIv3 civicrm_custom_group* functions
15 * @package CiviCRM_APIv3
16 * @subpackage API_CustomGroup
19 class api_v3_CustomGroupTest
extends CiviUnitTestCase
{
20 protected $_apiversion = 3;
24 public $DBResetRequired = TRUE;
26 public function setUp() {
27 $this->_entity
= 'CustomGroup';
29 'title' => 'Test_Group_1',
30 'name' => 'test_group_1',
31 'extends' => 'Individual',
33 'collapse_display' => 1,
35 'help_pre' => 'This is Pre Help For Test Group 1',
36 'help_post' => 'This is Post Help For Test Group 1',
42 public function tearDown() {
43 $tablesToTruncate = ['civicrm_custom_group', 'civicrm_custom_field'];
44 // true tells quickCleanup to drop any tables that might have been created in the test
45 $this->quickCleanup($tablesToTruncate, TRUE);
48 ///////////////// civicrm_custom_group_create methods
51 * Check with empty array.
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)
56 public function testCustomGroupCreateNoParam() {
57 $customGroup = $this->callAPIFailure('custom_group', 'create', [],
58 'Mandatory key(s) missing from params array: title, extends'
63 * Check with empty array.
65 public function testCustomGroupCreateNoExtends() {
68 'title' => 'Test_Group_1',
69 'name' => 'test_group_1',
71 'collapse_display' => 1,
73 'help_pre' => 'This is Pre Help For Test Group 1',
74 'help_post' => 'This is Post Help For Test Group 1',
78 $customGroup = $this->callAPIFailure('custom_group', 'create', $params);
79 $this->assertEquals($customGroup['error_message'], 'Mandatory key(s) missing from params array: extends');
80 $this->assertAPIFailure($customGroup);
84 * Check with empty array.
86 public function testCustomGroupCreateInvalidExtends() {
89 'title' => 'Test_Group_1',
90 'name' => 'test_group_1',
92 'collapse_display' => 1,
94 'help_pre' => 'This is Pre Help For Test Group 1',
95 'help_post' => 'This is Post Help For Test Group 1',
100 $customGroup = $this->callAPIFailure('custom_group', 'create', $params);
101 $this->assertEquals($customGroup['error_message'], 'Mandatory key(s) missing from params array: extends');
105 * Check with a string instead of array for extends.
107 public function testCustomGroupCreateExtendsString() {
110 'title' => 'Test_Group_1',
111 'name' => 'test_group_1',
113 'collapse_display' => 1,
115 'help_pre' => 'This is Pre Help For Test Group 1',
116 'help_post' => 'This is Post Help For Test Group 1',
118 'extends' => 'Individual',
121 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
125 * Check with valid array.
127 public function testCustomGroupCreate() {
129 'title' => 'Test_Group_1',
130 'name' => 'test_group_1',
131 'extends' => ['Individual'],
133 'collapse_display' => 1,
135 'help_pre' => 'This is Pre Help For Test Group 1',
136 'help_post' => 'This is Post Help For Test Group 1',
140 $result = $this->callAPIAndDocument('custom_group', 'create', $params, __FUNCTION__
, __FILE__
);
141 $this->assertNotNull($result['id']);
142 $this->assertEquals($result['values'][$result['id']]['extends'], 'Individual');
146 * Check with valid array.
148 public function testCustomGroupGetFields() {
150 'options' => ['get_options' => 'style'],
153 $result = $this->callAPISuccess('custom_group', 'getfields', $params);
156 'Inline' => 'Inline',
157 'Tab with table' => 'Tab with table',
159 $this->assertEquals($expected, $result['values']['style']['options']);
163 * Check with extends array length greater than 1
165 public function testCustomGroupExtendsMultipleCreate() {
167 'title' => 'Test_Group_1',
168 'name' => 'test_group_1',
169 'extends' => ['Individual', 'Household'],
171 'collapse_display' => 1,
173 'help_pre' => 'This is Pre Help For Test Group 1',
174 'help_post' => 'This is Post Help For Test Group 1',
178 $result = $this->callAPIFailure('custom_group', 'create', $params,
179 'implode(): Invalid arguments passed');
183 * Check with style missing from params array.
185 public function testCustomGroupCreateNoStyle() {
187 'title' => 'Test_Group_1',
188 'name' => 'test_group_1',
189 'extends' => ['Individual'],
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',
197 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
198 $this->assertNotNull($customGroup['id']);
199 $this->assertEquals($customGroup['values'][$customGroup['id']]['style'], 'Inline');
203 * Check with not array.
205 public function testCustomGroupCreateNotArray() {
207 $customGroup = $this->callAPIFailure('custom_group', 'create', $params);
208 $this->assertEquals($customGroup['error_message'], 'Input variable `params` is not an array');
212 * Check without title.
214 public function testCustomGroupCreateNoTitle() {
216 'extends' => ['Contact'],
218 'collapse_display' => 1,
220 'help_pre' => 'This is Pre Help For Test Group 2',
221 'help_post' => 'This is Post Help For Test Group 2',
224 $customGroup = $this->callAPIFailure('custom_group', 'create', $params,
225 'Mandatory key(s) missing from params array: title');
229 * Check for household without weight.
231 public function testCustomGroupCreateHouseholdNoWeight() {
233 'title' => 'Test_Group_3',
234 'name' => 'test_group_3',
235 'extends' => ['Household'],
236 'collapse_display' => 1,
238 'help_pre' => 'This is Pre Help For Test Group 3',
239 'help_post' => 'This is Post Help For Test Group 3',
243 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
244 $this->assertNotNull($customGroup['id']);
245 $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Household');
246 $this->assertEquals($customGroup['values'][$customGroup['id']]['style'], 'Tab');
250 * Check for Contribution Donation.
252 public function testCustomGroupCreateContributionDonation() {
254 'title' => 'Test_Group_6',
255 'name' => 'test_group_6',
256 'extends' => ['Contribution', [1]],
258 'collapse_display' => 1,
260 'help_pre' => 'This is Pre Help For Test Group 6',
261 'help_post' => 'This is Post Help For Test Group 6',
265 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
266 $this->assertNotNull($customGroup['id']);
267 $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Contribution');
271 * Check with valid array.
273 public function testCustomGroupCreateGroup() {
276 'title' => 'Test_Group_8',
277 'name' => 'test_group_8',
278 'extends' => ['Group'],
280 'collapse_display' => 1,
283 'help_pre' => 'This is Pre Help For Test Group 8',
284 'help_post' => 'This is Post Help For Test Group 8',
287 $customGroup = $this->callAPISuccess('CustomGroup', 'create', $params);
288 $this->assertNotNull($customGroup['id']);
289 $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Group');
293 * Test an empty update does not trigger e-notices when is_multiple has been set.
295 public function testCustomGroupEmptyUpdate() {
296 $customGroup = $this->callAPISuccess('CustomGroup', 'create', array_merge($this->_params
, ['is_multiple' => 1]));
297 $this->callAPISuccess('CustomGroup', 'create', ['id' => $customGroup['id']]);
301 * Test an update when is_multiple is an emtpy string this can occur in form submissions for custom groups that extend activites.
304 public function testCustomGroupEmptyisMultipleUpdate() {
305 $customGroup = $this->callAPISuccess('CustomGroup', 'create', array_merge($this->_params
, ['is_multiple' => 0]));
306 $this->callAPISuccess('CustomGroup', 'create', ['id' => $customGroup['id'], 'is_multiple' => '']);
310 * Check with Activity - Meeting Type
312 public function testCustomGroupCreateActivityMeeting() {
314 'title' => 'Test_Group_10',
315 'name' => 'test_group_10',
316 'extends' => ['Activity', [1]],
318 'collapse_display' => 1,
320 'help_pre' => 'This is Pre Help For Test Group 10',
321 'help_post' => 'This is Post Help For Test Group 10',
324 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
325 $this->assertNotNull($customGroup['id']);
326 $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Activity');
329 ///////////////// civicrm_custom_group_delete methods
332 * Check without GroupID.
334 public function testCustomGroupDeleteWithoutGroupID() {
335 $customGroup = $this->callAPIFailure('custom_group', 'delete', []);
336 $this->assertEquals($customGroup['error_message'], 'Mandatory key(s) missing from params array: id');
340 * Check with no array.
342 public function testCustomGroupDeleteNoArray() {
344 $customGroup = $this->callAPIFailure('custom_group', 'delete', $params);
345 $this->assertEquals($customGroup['error_message'], 'Input variable `params` is not an array');
349 * Check with valid custom group id.
351 public function testCustomGroupDelete() {
352 $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'test_group']);
354 'id' => $customGroup['id'],
356 $result = $this->callAPIAndDocument('custom_group', 'delete', $params, __FUNCTION__
, __FILE__
);
357 $this->assertAPISuccess($result);
361 * Main success get function.
363 public function testGetCustomGroupSuccess() {
365 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
367 $result = $this->callAPIAndDocument($this->_entity
, 'get', $params, __FUNCTION__
, __FILE__
);
368 $values = $result['values'][$result['id']];
369 foreach ($this->_params
as $key => $value) {
370 if ($key == 'weight') {
373 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__
);
377 public function testUpdateCustomGroup() {
378 $customGroup = $this->customGroupCreate();
379 $customGroupId = $customGroup['id'];
382 $params = ['id' => $customGroupId, 'is_active' => 0];
383 $result = $this->callAPISuccess('CustomGroup', 'create', $params);
384 $result = array_shift($result['values']);
386 $this->assertEquals(0, $result['is_active']);
387 $this->customGroupDelete($customGroupId);