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 without title.
205 public function testCustomGroupCreateNoTitle() {
207 'extends' => ['Contact'],
209 'collapse_display' => 1,
211 'help_pre' => 'This is Pre Help For Test Group 2',
212 'help_post' => 'This is Post Help For Test Group 2',
215 $customGroup = $this->callAPIFailure('custom_group', 'create', $params,
216 'Mandatory key(s) missing from params array: title');
220 * Check for household without weight.
222 public function testCustomGroupCreateHouseholdNoWeight() {
224 'title' => 'Test_Group_3',
225 'name' => 'test_group_3',
226 'extends' => ['Household'],
227 'collapse_display' => 1,
229 'help_pre' => 'This is Pre Help For Test Group 3',
230 'help_post' => 'This is Post Help For Test Group 3',
234 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
235 $this->assertNotNull($customGroup['id']);
236 $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Household');
237 $this->assertEquals($customGroup['values'][$customGroup['id']]['style'], 'Tab');
241 * Check for Contribution Donation.
243 public function testCustomGroupCreateContributionDonation() {
245 'title' => 'Test_Group_6',
246 'name' => 'test_group_6',
247 'extends' => ['Contribution', [1]],
249 'collapse_display' => 1,
251 'help_pre' => 'This is Pre Help For Test Group 6',
252 'help_post' => 'This is Post Help For Test Group 6',
256 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
257 $this->assertNotNull($customGroup['id']);
258 $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Contribution');
262 * Check with valid array.
264 public function testCustomGroupCreateGroup() {
267 'title' => 'Test_Group_8',
268 'name' => 'test_group_8',
269 'extends' => ['Group'],
271 'collapse_display' => 1,
274 'help_pre' => 'This is Pre Help For Test Group 8',
275 'help_post' => 'This is Post Help For Test Group 8',
278 $customGroup = $this->callAPISuccess('CustomGroup', 'create', $params);
279 $this->assertNotNull($customGroup['id']);
280 $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Group');
284 * Test an empty update does not trigger e-notices when is_multiple has been set.
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']]);
292 * Test an update when is_multiple is an emtpy string this can occur in form submissions for custom groups that extend activites.
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' => '']);
301 * Check with Activity - Meeting Type
303 public function testCustomGroupCreateActivityMeeting() {
305 'title' => 'Test_Group_10',
306 'name' => 'test_group_10',
307 'extends' => ['Activity', [1]],
309 'collapse_display' => 1,
311 'help_pre' => 'This is Pre Help For Test Group 10',
312 'help_post' => 'This is Post Help For Test Group 10',
315 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
316 $this->assertNotNull($customGroup['id']);
317 $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Activity');
320 ///////////////// civicrm_custom_group_delete methods
323 * Check without GroupID.
325 public function testCustomGroupDeleteWithoutGroupID() {
326 $customGroup = $this->callAPIFailure('custom_group', 'delete', []);
327 $this->assertEquals($customGroup['error_message'], 'Mandatory key(s) missing from params array: id');
331 * Check with valid custom group id.
333 public function testCustomGroupDelete() {
334 $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'test_group']);
336 'id' => $customGroup['id'],
338 $result = $this->callAPIAndDocument('custom_group', 'delete', $params, __FUNCTION__
, __FILE__
);
339 $this->assertAPISuccess($result);
343 * Main success get function.
345 public function testGetCustomGroupSuccess() {
347 $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
349 $result = $this->callAPIAndDocument($this->_entity
, 'get', $params, __FUNCTION__
, __FILE__
);
350 $values = $result['values'][$result['id']];
351 foreach ($this->_params
as $key => $value) {
352 if ($key == 'weight') {
355 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__
);
359 public function testUpdateCustomGroup() {
360 $customGroup = $this->customGroupCreate();
361 $customGroupId = $customGroup['id'];
364 $params = ['id' => $customGroupId, 'is_active' => 0];
365 $result = $this->callAPISuccess('CustomGroup', 'create', $params);
366 $result = array_shift($result['values']);
368 $this->assertEquals(0, $result['is_active']);
369 $this->customGroupDelete($customGroupId);