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