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