tests/ - Remove get_info()
[civicrm-core.git] / tests / phpunit / api / v3 / CustomGroupTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 setUp() {
46 $this->_entity = 'CustomGroup';
47 $this->_params = array(
48 'title' => 'Test_Group_1',
49 'name' => 'test_group_1',
50 'extends' => 'Individual',
51 'weight' => 4,
52 'collapse_display' => 1,
53 'style' => 'Inline',
54 'help_pre' => 'This is Pre Help For Test Group 1',
55 'help_post' => 'This is Post Help For Test Group 1',
56 'is_active' => 1,
57 );
58 parent::setUp();
59 }
60
61 function tearDown() {
62 $tablesToTruncate = array('civicrm_custom_group', 'civicrm_custom_field');
63 // true tells quickCleanup to drop any tables that might have been created in the test
64 $this->quickCleanup($tablesToTruncate, TRUE);
65 }
66
67 ///////////////// civicrm_custom_group_create methods
68
69 /**
70 * Check with empty array
71 * note that these tests are of marginal value so should not be included in copy & paste
72 * code. The SyntaxConformance is capable of testing this for all entities on create
73 * & delete (& it would be easy to add if not there)
74 */
75 function testCustomGroupCreateNoParam() {
76 $customGroup = $this->callAPIFailure('custom_group', 'create', array(),
77 'Mandatory key(s) missing from params array: title, extends'
78 );
79 }
80
81 /**
82 * Check with empty array
83 */
84 function testCustomGroupCreateNoExtends() {
85 $params = array(
86 'domain_id' => 1,
87 'title' => 'Test_Group_1',
88 'name' => 'test_group_1',
89 'weight' => 4,
90 'collapse_display' => 1,
91 'style' => 'Tab',
92 'help_pre' => 'This is Pre Help For Test Group 1',
93 'help_post' => 'This is Post Help For Test Group 1',
94 'is_active' => 1,
95 );
96
97 $customGroup = $this->callAPIFailure('custom_group', 'create', $params);
98 $this->assertEquals($customGroup['error_message'], 'Mandatory key(s) missing from params array: extends', 'In line ' . __LINE__);
99 $this->assertAPIFailure($customGroup, 'In line ' . __LINE__);
100 }
101
102 /**
103 * Check with empty array
104 */
105 function testCustomGroupCreateInvalidExtends() {
106 $params = array(
107 'domain_id' => 1,
108 'title' => 'Test_Group_1',
109 'name' => 'test_group_1',
110 'weight' => 4,
111 'collapse_display' => 1,
112 'style' => 'Tab',
113 'help_pre' => 'This is Pre Help For Test Group 1',
114 'help_post' => 'This is Post Help For Test Group 1',
115 'is_active' => 1,
116 'extends' => array(),
117 );
118
119 $customGroup = $this->callAPIFailure('custom_group', 'create', $params);
120 $this->assertEquals($customGroup['error_message'], 'Mandatory key(s) missing from params array: extends', 'In line ' . __LINE__);
121 }
122
123 /**
124 * Check with a string instead of array for extends
125 */
126 function testCustomGroupCreateExtendsString() {
127 $params = array(
128 'domain_id' => 1,
129 'title' => 'Test_Group_1',
130 'name' => 'test_group_1',
131 'weight' => 4,
132 'collapse_display' => 1,
133 'style' => 'Tab',
134 'help_pre' => 'This is Pre Help For Test Group 1',
135 'help_post' => 'This is Post Help For Test Group 1',
136 'is_active' => 1,
137 'extends' => 'Individual',
138 );
139
140 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
141 }
142
143 /**
144 * Check with valid array
145 */
146 function testCustomGroupCreate() {
147 $params = array(
148 'title' => 'Test_Group_1',
149 'name' => 'test_group_1',
150 'extends' => array('Individual'),
151 'weight' => 4,
152 'collapse_display' => 1,
153 'style' => 'Inline',
154 'help_pre' => 'This is Pre Help For Test Group 1',
155 'help_post' => 'This is Post Help For Test Group 1',
156 'is_active' => 1,
157 );
158
159 $result = $this->callAPIAndDocument('custom_group', 'create', $params, __FUNCTION__, __FILE__);
160 $this->assertNotNull($result['id'], 'In line ' . __LINE__);
161 $this->assertEquals($result['values'][$result['id']]['extends'], 'Individual', 'In line ' . __LINE__);
162 }
163
164 /**
165 * Check with valid array
166 */
167 function testCustomGroupGetFields() {
168 $params = array(
169 'options' => array('get_options' => 'style'),
170 );
171
172 $result = $this->callAPISuccess('custom_group', 'getfields', $params);
173 $expected = array(
174 'Tab' => 'Tab',
175 'Inline' => 'Inline',
176 'Tab with table' => 'Tab with table',
177 );
178 $this->assertEquals($expected, $result['values']['style']['options']);
179 }
180
181 /**
182 * Check with extends array length greater than 1
183 */
184 function testCustomGroupExtendsMultipleCreate() {
185 $params = array(
186 'title' => 'Test_Group_1',
187 'name' => 'test_group_1',
188 'extends' => array('Individual', 'Household'),
189 'weight' => 4,
190 'collapse_display' => 1,
191 'style' => 'Inline',
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,
195 );
196
197 $result = $this->callAPIFailure('custom_group', 'create', $params,
198 'implode(): Invalid arguments passed');
199 }
200
201 /**
202 * Check with style missing from params array
203 */
204 function testCustomGroupCreateNoStyle() {
205 $params = array(
206 'title' => 'Test_Group_1',
207 'name' => 'test_group_1',
208 'extends' => array('Individual'),
209 'weight' => 4,
210 'collapse_display' => 1,
211 'help_pre' => 'This is Pre Help For Test Group 1',
212 'help_post' => 'This is Post Help For Test Group 1',
213 'is_active' => 1,
214 );
215
216 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
217 $this->assertNotNull($customGroup['id'], 'In line ' . __LINE__);
218 $this->assertEquals($customGroup['values'][$customGroup['id']]['style'], 'Inline', 'In line ' . __LINE__);
219 }
220
221 /**
222 * Check with not array
223 */
224 function testCustomGroupCreateNotArray() {
225 $params = NULL;
226 $customGroup = $this->callAPIFailure('custom_group', 'create', $params);
227 $this->assertEquals($customGroup['error_message'], 'Input variable `params` is not an array', 'In line ' . __LINE__);
228 }
229
230 /**
231 * Check without title
232 */
233 function testCustomGroupCreateNoTitle() {
234 $params = array('extends' => array('Contact'),
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',
240 );
241
242 $customGroup = $this->callAPIFailure('custom_group', 'create', $params,
243 'Mandatory key(s) missing from params array: title');
244 }
245
246 /**
247 * Check for household without weight
248 */
249 function testCustomGroupCreateHouseholdNoWeight() {
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,
259 );
260
261 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
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 /**
268 * Check for Contribution Donation
269 */
270 function testCustomGroupCreateContributionDonation() {
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,
281 );
282
283 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
284 $this->assertNotNull($customGroup['id'], 'In line ' . __LINE__);
285 $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Contribution', 'In line ' . __LINE__);
286 }
287
288 /**
289 * Check with valid array
290 */
291 function testCustomGroupCreateGroup() {
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',
303 );
304
305 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
306 $this->assertNotNull($customGroup['id'], 'In line ' . __LINE__);
307 $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Group', 'In line ' . __LINE__);
308 }
309
310 /**
311 * Check with Activity - Meeting Type
312 */
313 function testCustomGroupCreateActivityMeeting() {
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',
323 );
324
325 $customGroup = $this->callAPISuccess('custom_group', 'create', $params);
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 /**
333 * Check without GroupID
334 */
335 function testCustomGroupDeleteWithoutGroupID() {
336 $customGroup = $this->callAPIFailure('custom_group', 'delete', array());
337 $this->assertEquals($customGroup['error_message'], 'Mandatory key(s) missing from params array: id', 'In line ' . __LINE__);
338 }
339
340 /**
341 * Check with no array
342 */
343 function testCustomGroupDeleteNoArray() {
344 $params = NULL;
345 $customGroup = $this->callAPIFailure('custom_group', 'delete', $params);
346 $this->assertEquals($customGroup['error_message'], 'Input variable `params` is not an array', 'In line ' . __LINE__);
347 }
348
349 /**
350 * Check with valid custom group id
351 */
352 function testCustomGroupDelete() {
353 $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group'));
354 $params = array(
355 'id' => $customGroup['id'],
356 );
357 $result = $this->callAPIAndDocument('custom_group', 'delete', $params, __FUNCTION__, __FILE__);
358 $this->assertAPISuccess($result, 'In line ' . __LINE__);
359 }
360
361 /**
362 * Main success get function
363 */
364 public function testGetCustomGroupSuccess() {
365
366 $this->callAPISuccess($this->_entity, 'create', $this->_params);
367 $params = array();
368 $result = $this->callAPIAndDocument($this->_entity, 'get', $params, __FUNCTION__, __FILE__);
369 $values = $result['values'][$result['id']];
370 foreach ($this->_params as $key => $value) {
371 if ($key == 'weight') {
372 continue;
373 }
374 $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__);
375 }
376 }
377 }
378