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