Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06a1bc01 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06a1bc01 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 { | |
f6722559 | 39 | protected $_apiversion = 3; |
6a488035 TO |
40 | protected $_entity; |
41 | protected $_params; | |
b7c9bc4c | 42 | |
6a488035 TO |
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() { | |
6a488035 TO |
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, | |
6a488035 TO |
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 | |
f6722559 | 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) | |
6a488035 TO |
82 | */ |
83 | function testCustomGroupCreateNoParam() { | |
f6722559 | 84 | $customGroup = $this->callAPIFailure('custom_group', 'create', array(), |
85 | 'Mandatory key(s) missing from params array: title, extends' | |
6a488035 TO |
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, | |
6a488035 TO |
103 | ); |
104 | ||
f6722559 | 105 | $customGroup = $this->callAPIFailure('custom_group', 'create', $params); |
6a488035 | 106 | $this->assertEquals($customGroup['error_message'], 'Mandatory key(s) missing from params array: extends', 'In line ' . __LINE__); |
c0536a92 | 107 | $this->assertAPIFailure($customGroup, 'In line ' . __LINE__); |
6a488035 TO |
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(), | |
6a488035 TO |
125 | ); |
126 | ||
d0e1eff2 | 127 | $customGroup = $this->callAPIFailure('custom_group', 'create', $params); |
6a488035 | 128 | $this->assertEquals($customGroup['error_message'], 'Mandatory key(s) missing from params array: extends', 'In line ' . __LINE__); |
6a488035 TO |
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', | |
6a488035 TO |
146 | ); |
147 | ||
f6722559 | 148 | $customGroup = $this->callAPISuccess('custom_group', 'create', $params); |
6a488035 TO |
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, | |
6a488035 TO |
165 | ); |
166 | ||
f6722559 | 167 | $result = $this->callAPIAndDocument('custom_group', 'create', $params, __FUNCTION__, __FILE__); |
6a488035 TO |
168 | $this->assertNotNull($result['id'], 'In line ' . __LINE__); |
169 | $this->assertEquals($result['values'][$result['id']]['extends'], 'Individual', 'In line ' . __LINE__); | |
170 | } | |
aa7e7ff0 | 171 | |
6a488035 TO |
172 | /** |
173 | * check with valid array | |
174 | */ | |
175 | function testCustomGroupGetFields() { | |
176 | $params = array( | |
9775f926 | 177 | 'options' => array('get_options' => 'style'), |
6a488035 TO |
178 | ); |
179 | ||
92af9fcb CW |
180 | $result = $this->callAPISuccess('custom_group', 'getfields', $params); |
181 | $expected = array( | |
182 | 'Tab' => 'Tab', | |
183 | 'Inline' => 'Inline', | |
9775f926 | 184 | 'Tab with table' => 'Tab with table', |
92af9fcb CW |
185 | ); |
186 | $this->assertEquals($expected, $result['values']['style']['options']); | |
6a488035 TO |
187 | } |
188 | ||
6a488035 TO |
189 | /** |
190 | * check with extends array length greater than 1 | |
191 | */ | |
192 | function testCustomGroupExtendsMultipleCreate() { | |
193 | $params = array( | |
194 | 'title' => 'Test_Group_1', | |
195 | 'name' => 'test_group_1', | |
196 | 'extends' => array('Individual', 'Household'), | |
197 | 'weight' => 4, | |
198 | 'collapse_display' => 1, | |
199 | 'style' => 'Inline', | |
200 | 'help_pre' => 'This is Pre Help For Test Group 1', | |
201 | 'help_post' => 'This is Post Help For Test Group 1', | |
202 | 'is_active' => 1, | |
6a488035 TO |
203 | ); |
204 | ||
f6722559 | 205 | $result = $this->callAPIFailure('custom_group', 'create', $params, |
206 | 'implode(): Invalid arguments passed'); | |
6a488035 TO |
207 | } |
208 | ||
209 | /** | |
210 | * check with style missing from params array | |
211 | */ | |
212 | function testCustomGroupCreateNoStyle() { | |
213 | $params = array( | |
214 | 'title' => 'Test_Group_1', | |
215 | 'name' => 'test_group_1', | |
216 | 'extends' => array('Individual'), | |
217 | 'weight' => 4, | |
218 | 'collapse_display' => 1, | |
219 | 'help_pre' => 'This is Pre Help For Test Group 1', | |
220 | 'help_post' => 'This is Post Help For Test Group 1', | |
221 | 'is_active' => 1, | |
6a488035 TO |
222 | ); |
223 | ||
f6722559 | 224 | $customGroup = $this->callAPISuccess('custom_group', 'create', $params); |
6a488035 TO |
225 | $this->assertNotNull($customGroup['id'], 'In line ' . __LINE__); |
226 | $this->assertEquals($customGroup['values'][$customGroup['id']]['style'], 'Inline', 'In line ' . __LINE__); | |
227 | } | |
228 | ||
229 | /** | |
230 | * check with not array | |
231 | */ | |
232 | function testCustomGroupCreateNotArray() { | |
233 | $params = NULL; | |
d0e1eff2 | 234 | $customGroup = $this->callAPIFailure('custom_group', 'create', $params); |
6a488035 TO |
235 | $this->assertEquals($customGroup['error_message'], 'Input variable `params` is not an array', 'In line ' . __LINE__); |
236 | } | |
237 | ||
238 | /** | |
239 | * check without title | |
240 | */ | |
241 | function testCustomGroupCreateNoTitle() { | |
242 | $params = array('extends' => array('Contact'), | |
243 | 'weight' => 5, | |
244 | 'collapse_display' => 1, | |
245 | 'style' => 'Tab', | |
246 | 'help_pre' => 'This is Pre Help For Test Group 2', | |
247 | 'help_post' => 'This is Post Help For Test Group 2', | |
6a488035 TO |
248 | ); |
249 | ||
f6722559 | 250 | $customGroup = $this->callAPIFailure('custom_group', 'create', $params, |
251 | 'Mandatory key(s) missing from params array: title'); | |
6a488035 TO |
252 | } |
253 | ||
254 | /** | |
255 | * check for household without weight | |
256 | */ | |
257 | function testCustomGroupCreateHouseholdNoWeight() { | |
258 | $params = array( | |
259 | 'title' => 'Test_Group_3', | |
260 | 'name' => 'test_group_3', | |
261 | 'extends' => array('Household'), | |
262 | 'collapse_display' => 1, | |
263 | 'style' => 'Tab', | |
264 | 'help_pre' => 'This is Pre Help For Test Group 3', | |
265 | 'help_post' => 'This is Post Help For Test Group 3', | |
266 | 'is_active' => 1, | |
6a488035 TO |
267 | ); |
268 | ||
f6722559 | 269 | $customGroup = $this->callAPISuccess('custom_group', 'create', $params); |
6a488035 TO |
270 | $this->assertNotNull($customGroup['id'], 'In line ' . __LINE__); |
271 | $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Household', 'In line ' . __LINE__); | |
272 | $this->assertEquals($customGroup['values'][$customGroup['id']]['style'], 'Tab', 'In line ' . __LINE__); | |
273 | } | |
274 | ||
275 | /** | |
276 | * check for Contribution Donation | |
277 | */ | |
278 | function testCustomGroupCreateContributionDonation() { | |
279 | $params = array( | |
280 | 'title' => 'Test_Group_6', | |
281 | 'name' => 'test_group_6', | |
282 | 'extends' => array('Contribution', array(1)), | |
283 | 'weight' => 6, | |
284 | 'collapse_display' => 1, | |
285 | 'style' => 'Inline', | |
286 | 'help_pre' => 'This is Pre Help For Test Group 6', | |
287 | 'help_post' => 'This is Post Help For Test Group 6', | |
288 | 'is_active' => 1, | |
6a488035 TO |
289 | ); |
290 | ||
f6722559 | 291 | $customGroup = $this->callAPISuccess('custom_group', 'create', $params); |
6a488035 TO |
292 | $this->assertNotNull($customGroup['id'], 'In line ' . __LINE__); |
293 | $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Contribution', 'In line ' . __LINE__); | |
294 | } | |
295 | ||
296 | /** | |
297 | * check with valid array | |
298 | */ | |
299 | function testCustomGroupCreateGroup() { | |
300 | $params = array( | |
301 | 'domain_id' => 1, | |
302 | 'title' => 'Test_Group_8', | |
303 | 'name' => 'test_group_8', | |
304 | 'extends' => array('Group'), | |
305 | 'weight' => 7, | |
306 | 'collapse_display' => 1, | |
307 | 'is_active' => 1, | |
308 | 'style' => 'Inline', | |
309 | 'help_pre' => 'This is Pre Help For Test Group 8', | |
310 | 'help_post' => 'This is Post Help For Test Group 8', | |
6a488035 TO |
311 | ); |
312 | ||
f6722559 | 313 | $customGroup = $this->callAPISuccess('custom_group', 'create', $params); |
6a488035 TO |
314 | $this->assertNotNull($customGroup['id'], 'In line ' . __LINE__); |
315 | $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Group', 'In line ' . __LINE__); | |
316 | } | |
317 | ||
318 | /** | |
319 | * check with Activity - Meeting Type | |
320 | */ | |
321 | function testCustomGroupCreateActivityMeeting() { | |
322 | $params = array( | |
323 | 'title' => 'Test_Group_10', | |
324 | 'name' => 'test_group_10', | |
325 | 'extends' => array('Activity', array(1)), | |
326 | 'weight' => 8, | |
327 | 'collapse_display' => 1, | |
328 | 'style' => 'Inline', | |
329 | 'help_pre' => 'This is Pre Help For Test Group 10', | |
330 | 'help_post' => 'This is Post Help For Test Group 10', | |
6a488035 TO |
331 | ); |
332 | ||
f6722559 | 333 | $customGroup = $this->callAPISuccess('custom_group', 'create', $params); |
6a488035 TO |
334 | $this->assertNotNull($customGroup['id'], 'In line ' . __LINE__); |
335 | $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Activity', 'In line ' . __LINE__); | |
336 | } | |
337 | ||
338 | ///////////////// civicrm_custom_group_delete methods | |
339 | ||
340 | /** | |
341 | * check without GroupID | |
342 | */ | |
343 | function testCustomGroupDeleteWithoutGroupID() { | |
d0e1eff2 | 344 | $customGroup = $this->callAPIFailure('custom_group', 'delete', array()); |
6a488035 TO |
345 | $this->assertEquals($customGroup['error_message'], 'Mandatory key(s) missing from params array: id', 'In line ' . __LINE__); |
346 | } | |
347 | ||
348 | /** | |
349 | * check with no array | |
350 | */ | |
351 | function testCustomGroupDeleteNoArray() { | |
352 | $params = NULL; | |
d0e1eff2 | 353 | $customGroup = $this->callAPIFailure('custom_group', 'delete', $params); |
6a488035 TO |
354 | $this->assertEquals($customGroup['error_message'], 'Input variable `params` is not an array', 'In line ' . __LINE__); |
355 | } | |
356 | ||
357 | /** | |
358 | * check with valid custom group id | |
359 | */ | |
360 | function testCustomGroupDelete() { | |
f6722559 | 361 | $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group')); |
6a488035 TO |
362 | $params = array( |
363 | 'id' => $customGroup['id'], | |
6a488035 | 364 | ); |
f6722559 | 365 | $result = $this->callAPIAndDocument('custom_group', 'delete', $params, __FUNCTION__, __FILE__); |
c0536a92 | 366 | $this->assertAPISuccess($result, 'In line ' . __LINE__); |
6a488035 TO |
367 | } |
368 | /* | |
369 | * main success get function | |
370 | */ | |
371 | ||
372 | ||
373 | ||
374 | public function testGetCustomGroupSuccess() { | |
375 | ||
f6722559 | 376 | $this->callAPISuccess($this->_entity, 'create', $this->_params); |
377 | $params = array(); | |
378 | $result = $this->callAPIAndDocument($this->_entity, 'get', $params, __FUNCTION__, __FILE__); | |
6a488035 TO |
379 | $values = $result['values'][$result['id']]; |
380 | foreach ($this->_params as $key => $value) { | |
f6722559 | 381 | if ($key == 'weight') { |
6a488035 TO |
382 | continue; |
383 | } | |
384 | $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__); | |
385 | } | |
386 | } | |
387 | } | |
388 |