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