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