Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 | 27 | |
6a488035 TO |
28 | /** |
29 | * Test APIv3 civicrm_custom_group* functions | |
30 | * | |
6c6e6187 TO |
31 | * @package CiviCRM_APIv3 |
32 | * @subpackage API_CustomGroup | |
6a488035 | 33 | */ |
6a488035 | 34 | class api_v3_CustomGroupTest extends CiviUnitTestCase { |
f6722559 | 35 | protected $_apiversion = 3; |
6a488035 TO |
36 | protected $_entity; |
37 | protected $_params; | |
b7c9bc4c | 38 | |
6a488035 TO |
39 | public $DBResetRequired = TRUE; |
40 | ||
00be9182 | 41 | public function setUp() { |
92915c55 TO |
42 | $this->_entity = 'CustomGroup'; |
43 | $this->_params = array( | |
6a488035 TO |
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, | |
6a488035 TO |
53 | ); |
54 | parent::setUp(); | |
55 | } | |
56 | ||
00be9182 | 57 | public function tearDown() { |
6a488035 TO |
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 | /** | |
eceb18cc | 66 | * Check with empty array. |
f6722559 | 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) | |
6a488035 | 70 | */ |
00be9182 | 71 | public function testCustomGroupCreateNoParam() { |
f6722559 | 72 | $customGroup = $this->callAPIFailure('custom_group', 'create', array(), |
73 | 'Mandatory key(s) missing from params array: title, extends' | |
6a488035 TO |
74 | ); |
75 | } | |
76 | ||
77 | /** | |
eceb18cc | 78 | * Check with empty array. |
6a488035 | 79 | */ |
00be9182 | 80 | public function testCustomGroupCreateNoExtends() { |
6a488035 TO |
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, | |
6a488035 TO |
91 | ); |
92 | ||
f6722559 | 93 | $customGroup = $this->callAPIFailure('custom_group', 'create', $params); |
ba4a1892 | 94 | $this->assertEquals($customGroup['error_message'], 'Mandatory key(s) missing from params array: extends'); |
a15773db | 95 | $this->assertAPIFailure($customGroup); |
6a488035 TO |
96 | } |
97 | ||
98 | /** | |
eceb18cc | 99 | * Check with empty array. |
6a488035 | 100 | */ |
00be9182 | 101 | public function testCustomGroupCreateInvalidExtends() { |
6a488035 TO |
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(), | |
6a488035 TO |
113 | ); |
114 | ||
d0e1eff2 | 115 | $customGroup = $this->callAPIFailure('custom_group', 'create', $params); |
ba4a1892 | 116 | $this->assertEquals($customGroup['error_message'], 'Mandatory key(s) missing from params array: extends'); |
6a488035 TO |
117 | } |
118 | ||
119 | /** | |
eceb18cc | 120 | * Check with a string instead of array for extends. |
6a488035 | 121 | */ |
00be9182 | 122 | public function testCustomGroupCreateExtendsString() { |
6a488035 TO |
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', | |
6a488035 TO |
134 | ); |
135 | ||
f6722559 | 136 | $customGroup = $this->callAPISuccess('custom_group', 'create', $params); |
6a488035 TO |
137 | } |
138 | ||
139 | /** | |
eceb18cc | 140 | * Check with valid array. |
6a488035 | 141 | */ |
00be9182 | 142 | public function testCustomGroupCreate() { |
6a488035 TO |
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, | |
6a488035 TO |
153 | ); |
154 | ||
f6722559 | 155 | $result = $this->callAPIAndDocument('custom_group', 'create', $params, __FUNCTION__, __FILE__); |
ba4a1892 TM |
156 | $this->assertNotNull($result['id']); |
157 | $this->assertEquals($result['values'][$result['id']]['extends'], 'Individual'); | |
6a488035 | 158 | } |
aa7e7ff0 | 159 | |
6a488035 | 160 | /** |
eceb18cc | 161 | * Check with valid array. |
6a488035 | 162 | */ |
00be9182 | 163 | public function testCustomGroupGetFields() { |
6a488035 | 164 | $params = array( |
9775f926 | 165 | 'options' => array('get_options' => 'style'), |
6a488035 TO |
166 | ); |
167 | ||
92af9fcb CW |
168 | $result = $this->callAPISuccess('custom_group', 'getfields', $params); |
169 | $expected = array( | |
170 | 'Tab' => 'Tab', | |
171 | 'Inline' => 'Inline', | |
9775f926 | 172 | 'Tab with table' => 'Tab with table', |
92af9fcb CW |
173 | ); |
174 | $this->assertEquals($expected, $result['values']['style']['options']); | |
6a488035 TO |
175 | } |
176 | ||
6a488035 | 177 | /** |
100fef9d | 178 | * Check with extends array length greater than 1 |
6a488035 | 179 | */ |
00be9182 | 180 | public function testCustomGroupExtendsMultipleCreate() { |
6a488035 TO |
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, | |
6a488035 TO |
191 | ); |
192 | ||
f6722559 | 193 | $result = $this->callAPIFailure('custom_group', 'create', $params, |
92915c55 | 194 | 'implode(): Invalid arguments passed'); |
6a488035 TO |
195 | } |
196 | ||
197 | /** | |
eceb18cc | 198 | * Check with style missing from params array. |
6a488035 | 199 | */ |
00be9182 | 200 | public function testCustomGroupCreateNoStyle() { |
6a488035 TO |
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, | |
6a488035 TO |
210 | ); |
211 | ||
f6722559 | 212 | $customGroup = $this->callAPISuccess('custom_group', 'create', $params); |
ba4a1892 TM |
213 | $this->assertNotNull($customGroup['id']); |
214 | $this->assertEquals($customGroup['values'][$customGroup['id']]['style'], 'Inline'); | |
6a488035 TO |
215 | } |
216 | ||
217 | /** | |
eceb18cc | 218 | * Check with not array. |
6a488035 | 219 | */ |
00be9182 | 220 | public function testCustomGroupCreateNotArray() { |
6a488035 | 221 | $params = NULL; |
d0e1eff2 | 222 | $customGroup = $this->callAPIFailure('custom_group', 'create', $params); |
ba4a1892 | 223 | $this->assertEquals($customGroup['error_message'], 'Input variable `params` is not an array'); |
6a488035 TO |
224 | } |
225 | ||
226 | /** | |
eceb18cc | 227 | * Check without title. |
6a488035 | 228 | */ |
00be9182 | 229 | public function testCustomGroupCreateNoTitle() { |
6c6e6187 | 230 | $params = array( |
92915c55 | 231 | 'extends' => array('Contact'), |
6a488035 TO |
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', | |
6a488035 TO |
237 | ); |
238 | ||
f6722559 | 239 | $customGroup = $this->callAPIFailure('custom_group', 'create', $params, |
240 | 'Mandatory key(s) missing from params array: title'); | |
6a488035 TO |
241 | } |
242 | ||
243 | /** | |
eceb18cc | 244 | * Check for household without weight. |
6a488035 | 245 | */ |
00be9182 | 246 | public function testCustomGroupCreateHouseholdNoWeight() { |
6a488035 TO |
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, | |
6a488035 TO |
256 | ); |
257 | ||
f6722559 | 258 | $customGroup = $this->callAPISuccess('custom_group', 'create', $params); |
ba4a1892 TM |
259 | $this->assertNotNull($customGroup['id']); |
260 | $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Household'); | |
261 | $this->assertEquals($customGroup['values'][$customGroup['id']]['style'], 'Tab'); | |
6a488035 TO |
262 | } |
263 | ||
264 | /** | |
eceb18cc | 265 | * Check for Contribution Donation. |
6a488035 | 266 | */ |
00be9182 | 267 | public function testCustomGroupCreateContributionDonation() { |
6a488035 TO |
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, | |
6a488035 TO |
278 | ); |
279 | ||
f6722559 | 280 | $customGroup = $this->callAPISuccess('custom_group', 'create', $params); |
ba4a1892 TM |
281 | $this->assertNotNull($customGroup['id']); |
282 | $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Contribution'); | |
6a488035 TO |
283 | } |
284 | ||
285 | /** | |
eceb18cc | 286 | * Check with valid array. |
6a488035 | 287 | */ |
00be9182 | 288 | public function testCustomGroupCreateGroup() { |
6a488035 TO |
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', | |
6a488035 TO |
300 | ); |
301 | ||
f6722559 | 302 | $customGroup = $this->callAPISuccess('custom_group', 'create', $params); |
ba4a1892 TM |
303 | $this->assertNotNull($customGroup['id']); |
304 | $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Group'); | |
6a488035 TO |
305 | } |
306 | ||
307 | /** | |
100fef9d | 308 | * Check with Activity - Meeting Type |
6a488035 | 309 | */ |
00be9182 | 310 | public function testCustomGroupCreateActivityMeeting() { |
6a488035 TO |
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', | |
6a488035 TO |
320 | ); |
321 | ||
f6722559 | 322 | $customGroup = $this->callAPISuccess('custom_group', 'create', $params); |
ba4a1892 TM |
323 | $this->assertNotNull($customGroup['id']); |
324 | $this->assertEquals($customGroup['values'][$customGroup['id']]['extends'], 'Activity'); | |
6a488035 TO |
325 | } |
326 | ||
327 | ///////////////// civicrm_custom_group_delete methods | |
328 | ||
329 | /** | |
eceb18cc | 330 | * Check without GroupID. |
6a488035 | 331 | */ |
00be9182 | 332 | public function testCustomGroupDeleteWithoutGroupID() { |
d0e1eff2 | 333 | $customGroup = $this->callAPIFailure('custom_group', 'delete', array()); |
ba4a1892 | 334 | $this->assertEquals($customGroup['error_message'], 'Mandatory key(s) missing from params array: id'); |
6a488035 TO |
335 | } |
336 | ||
337 | /** | |
eceb18cc | 338 | * Check with no array. |
6a488035 | 339 | */ |
00be9182 | 340 | public function testCustomGroupDeleteNoArray() { |
6a488035 | 341 | $params = NULL; |
d0e1eff2 | 342 | $customGroup = $this->callAPIFailure('custom_group', 'delete', $params); |
ba4a1892 | 343 | $this->assertEquals($customGroup['error_message'], 'Input variable `params` is not an array'); |
6a488035 TO |
344 | } |
345 | ||
346 | /** | |
eceb18cc | 347 | * Check with valid custom group id. |
6a488035 | 348 | */ |
00be9182 | 349 | public function testCustomGroupDelete() { |
f6722559 | 350 | $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'test_group')); |
6a488035 TO |
351 | $params = array( |
352 | 'id' => $customGroup['id'], | |
6a488035 | 353 | ); |
f6722559 | 354 | $result = $this->callAPIAndDocument('custom_group', 'delete', $params, __FUNCTION__, __FILE__); |
a15773db | 355 | $this->assertAPISuccess($result); |
6a488035 | 356 | } |
6a488035 | 357 | |
c490a46a | 358 | /** |
eceb18cc | 359 | * Main success get function. |
c490a46a | 360 | */ |
6a488035 TO |
361 | public function testGetCustomGroupSuccess() { |
362 | ||
f6722559 | 363 | $this->callAPISuccess($this->_entity, 'create', $this->_params); |
364 | $params = array(); | |
365 | $result = $this->callAPIAndDocument($this->_entity, 'get', $params, __FUNCTION__, __FILE__); | |
6a488035 TO |
366 | $values = $result['values'][$result['id']]; |
367 | foreach ($this->_params as $key => $value) { | |
f6722559 | 368 | if ($key == 'weight') { |
6a488035 TO |
369 | continue; |
370 | } | |
371 | $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__); | |
372 | } | |
373 | } | |
96025800 | 374 | |
6a488035 | 375 | } |