Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
b6708aeb | 2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
06a1bc01 | 5 | | CiviCRM version 4.5 | |
b6708aeb | 6 | +--------------------------------------------------------------------+ |
06a1bc01 | 7 | | Copyright CiviCRM LLC (c) 2004-2014 | |
b6708aeb | 8 | +--------------------------------------------------------------------+ |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
11 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
12 | | under the terms of the GNU Affero General Public License | | |
13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
14 | | | | |
15 | | CiviCRM is distributed in the hope that it will be useful, but | | |
16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
18 | | See the GNU Affero General Public License for more details. | | |
19 | | | | |
20 | | You should have received a copy of the GNU Affero General Public | | |
21 | | License and the CiviCRM Licensing Exception along | | |
22 | | with this program; if not, contact CiviCRM LLC | | |
23 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
24 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
26 | +--------------------------------------------------------------------+ | |
27 | */ | |
6a488035 TO |
28 | |
29 | /** | |
30 | * Include class definitions | |
31 | */ | |
32 | require_once 'tests/phpunit/CiviTest/CiviUnitTestCase.php'; | |
33 | ||
34 | /** | |
35 | * Test APIv3 civicrm_profile_* functions | |
36 | * | |
37 | * @package CiviCRM | |
38 | */ | |
39 | class api_v3_ProfileTest extends CiviUnitTestCase { | |
40 | protected $_apiversion; | |
9da2e77c | 41 | protected $_profileID = 0; |
174dbdd5 E |
42 | protected $_membershipTypeID; |
43 | protected $_contactID; | |
4cbe18b8 EM |
44 | |
45 | /** | |
46 | * @return array | |
47 | */ | |
6a488035 TO |
48 | function get_info() { |
49 | return array( | |
50 | 'name' => 'Profile Test', | |
51 | 'description' => 'Test all profile API methods.', | |
52 | 'group' => 'CiviCRM API Tests', | |
53 | ); | |
54 | } | |
55 | ||
56 | function setUp() { | |
57 | $this->_apiversion = 3; | |
58 | parent::setUp(); | |
59 | $config = CRM_Core_Config::singleton(); | |
60 | $config->countryLimit[1] = 1013; | |
61 | $config->stateLimit[1] = 1013; | |
9da2e77c | 62 | $this->createLoggedInUser(); |
174dbdd5 | 63 | $this->_membershipTypeID = $this->membershipTypeCreate(); |
6a488035 TO |
64 | } |
65 | ||
66 | function tearDown() { | |
7fbb4198 | 67 | |
6a488035 | 68 | $this->quickCleanup(array( |
21170390 | 69 | 'civicrm_contact', |
70 | 'civicrm_phone', | |
71 | 'civicrm_address', | |
174dbdd5 E |
72 | 'civicrm_membership', |
73 | 'civicrm_contribution', | |
21170390 | 74 | ), TRUE); |
174dbdd5 | 75 | $this->callAPISuccess('membership_type', 'delete', array('id' => $this->_membershipTypeID)); |
f01ce56b | 76 | // ok can't be bothered wring an api to do this & truncating is crazy |
9da2e77c | 77 | CRM_Core_DAO::executeQuery(" DELETE FROM civicrm_uf_group WHERE id IN ($this->_profileID, 26)"); |
6a488035 TO |
78 | } |
79 | ||
7fbb4198 | 80 | ////////////// test $this->callAPISuccess3_profile_get ////////////////// |
6a488035 TO |
81 | |
82 | /** | |
83 | * check Without ProfileId | |
84 | */ | |
85 | function testProfileGetWithoutProfileId() { | |
86 | $params = array( | |
87 | 'contact_id' => 1, | |
6a488035 | 88 | ); |
7fbb4198 | 89 | $result = $this->callAPIFailure('profile', 'get', $params, |
90 | 'Mandatory key(s) missing from params array: profile_id' | |
91 | ); | |
6a488035 TO |
92 | } |
93 | ||
94 | /** | |
95 | * check with no invalid profile Id | |
96 | */ | |
97 | function testProfileGetInvalidProfileId() { | |
98 | $params = array( | |
99 | 'contact_id' => 1, | |
100 | 'profile_id' => 1000, | |
6a488035 | 101 | ); |
d0e1eff2 | 102 | $result = $this->callAPIFailure('profile', 'get', $params); |
6a488035 TO |
103 | } |
104 | ||
105 | /** | |
106 | * check with success | |
107 | */ | |
108 | function testProfileGet() { | |
109 | $pofileFieldValues = $this->_createIndividualContact(); | |
110 | $expected = current($pofileFieldValues); | |
111 | $contactId = key($pofileFieldValues); | |
112 | $params = array( | |
9da2e77c | 113 | 'profile_id' => $this->_profileID, |
6a488035 | 114 | 'contact_id' => $contactId, |
6a488035 | 115 | ); |
f01ce56b | 116 | $result = $this->callAPISuccess('profile', 'get', $params); |
6a488035 | 117 | foreach ($expected as $profileField => $value) { |
9da2e77c | 118 | $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result['values'])); |
6a488035 | 119 | } |
6a488035 TO |
120 | } |
121 | ||
f01ce56b | 122 | function testProfileGetMultiple() { |
123 | $pofileFieldValues = $this->_createIndividualContact(); | |
124 | $expected = current($pofileFieldValues); | |
125 | $contactId = key($pofileFieldValues); | |
126 | $params = array( | |
9da2e77c | 127 | 'profile_id' => array($this->_profileID, 1, 'Billing'), |
f01ce56b | 128 | 'contact_id' => $contactId, |
129 | ); | |
130 | ||
131 | $result = $this->callAPIAndDocument('profile', 'get', $params, __FUNCTION__, __FILE__); | |
132 | foreach ($expected as $profileField => $value) { | |
9da2e77c | 133 | $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result['values'][$this->_profileID]), " error message: " . "missing/mismatching value for {$profileField}"); |
f01ce56b | 134 | } |
135 | $this->assertEquals('abc1', $result['values'][1]['first_name'], " error message: " . "missing/mismatching value for {$profileField}"); | |
136 | $this->assertFalse(array_key_exists('email-Primary', $result['values'][1]), 'profile 1 doesn not include email'); | |
137 | $this->assertEquals($result['values']['Billing'], array( | |
138 | 'billing_first_name' => 'abc1', | |
e0efd2d0 | 139 | 'billing_middle_name' => 'J.', |
f01ce56b | 140 | 'billing_last_name' => 'xyz1', |
b29f74b6 | 141 | 'billing_street_address-5' => '5 Saint Helier St', |
142 | 'billing_city-5' => 'Gotham City', | |
e0efd2d0 | 143 | 'billing_state_province_id-5' => '1021', |
45c30250 | 144 | 'billing_country_id-5' => '1228', |
e0efd2d0 | 145 | 'billing_postal_code-5' => '90210', |
146 | 'billing-email-5' => 'abc1.xyz1@yahoo.com', | |
147 | 'email-5' => 'abc1.xyz1@yahoo.com', | |
f01ce56b | 148 | )); |
149 | } | |
150 | ||
b29f74b6 | 151 | function testProfileGetBillingUseIsBillingLocation() { |
152 | $individual = $this->_createIndividualContact(); | |
153 | $contactId = key($individual); | |
154 | $this->callAPISuccess('address', 'create', array( | |
155 | 'is_billing' => 1, | |
156 | 'street_address' => 'is billing st', | |
157 | 'location_type_id' => 2, | |
158 | 'contact_id' => $contactId, | |
21170390 | 159 | )); |
b29f74b6 | 160 | |
161 | $expected = current($individual); | |
162 | ||
163 | $params = array( | |
9da2e77c | 164 | 'profile_id' => array($this->_profileID, 1, 'Billing'), |
b29f74b6 | 165 | 'contact_id' => $contactId, |
166 | ); | |
167 | ||
168 | $result = $this->callAPISuccess('profile', 'get', $params); | |
169 | $this->assertEquals('abc1', $result['values'][1]['first_name']); | |
170 | $this->assertEquals(array( | |
171 | 'billing_first_name' => 'abc1', | |
172 | 'billing_middle_name' => 'J.', | |
173 | 'billing_last_name' => 'xyz1', | |
174 | 'billing_street_address-5' => 'is billing st', | |
175 | 'billing_city-5' => '', | |
176 | 'billing_state_province_id-5' => '', | |
177 | 'billing_country_id-5' => '', | |
178 | 'billing-email-5' => 'abc1.xyz1@yahoo.com', | |
179 | 'email-5' => 'abc1.xyz1@yahoo.com', | |
180 | 'billing_postal_code-5' => '', | |
181 | ), $result['values']['Billing']); | |
182 | } | |
183 | ||
f01ce56b | 184 | function testProfileGetMultipleHasBillingLocation() { |
185 | $individual = $this->_createIndividualContact(); | |
186 | $contactId = key($individual); | |
187 | $this->callAPISuccess('address', 'create', array('contact_id' => $contactId , 'street_address' => '25 Big Street', 'city' => 'big city', 'location_type_id' => 5)); | |
188 | $this->callAPISuccess('email', 'create', array('contact_id' => $contactId , 'email' => 'big@once.com', 'location_type_id' => 2, 'is_billing' => 1)); | |
189 | ||
190 | $expected = current($individual); | |
191 | ||
192 | $params = array( | |
9da2e77c | 193 | 'profile_id' => array($this->_profileID, 1, 'Billing'), |
f01ce56b | 194 | 'contact_id' => $contactId, |
195 | ); | |
196 | ||
197 | $result = $this->callAPISuccess('profile', 'get', $params, __FUNCTION__, __FILE__); | |
198 | $this->assertEquals('abc1', $result['values'][1]['first_name']); | |
199 | $this->assertEquals($result['values']['Billing'], array( | |
200 | 'billing_first_name' => 'abc1', | |
e0efd2d0 | 201 | 'billing_middle_name' => 'J.', |
f01ce56b | 202 | 'billing_last_name' => 'xyz1', |
203 | 'billing_street_address-5' => '25 Big Street', | |
204 | 'billing_city-5' => 'big city', | |
205 | 'billing_state_province_id-5' => '', | |
206 | 'billing_country_id-5' => '', | |
207 | 'billing-email-5' => 'big@once.com', | |
e0efd2d0 | 208 | 'email-5' => 'big@once.com', |
209 | 'billing_postal_code-5' => '', | |
f01ce56b | 210 | )); |
211 | } | |
b29f74b6 | 212 | |
5a9e1452 | 213 | /** |
214 | * get Billing empty contact - this will return generic defaults | |
215 | */ | |
216 | function testProfileGetBillingEmptyContact() { | |
217 | ||
218 | $params = array( | |
219 | 'profile_id' => array('Billing'), | |
220 | ); | |
221 | ||
222 | $result = $this->callAPISuccess('profile', 'get', $params); | |
223 | $this->assertEquals(array( | |
224 | 'billing_first_name' => '', | |
225 | 'billing_middle_name' => '', | |
226 | 'billing_last_name' => '', | |
227 | 'billing_street_address-5' => '', | |
228 | 'billing_city-5' => '', | |
229 | 'billing_state_province_id-5' => '', | |
230 | 'billing_country_id-5' => '1228', | |
231 | 'billing_email-5' => '', | |
232 | 'email-5' => '', | |
233 | 'billing_postal_code-5' => '', | |
234 | ), $result['values']['Billing']); | |
235 | } | |
b29f74b6 | 236 | |
7fbb4198 | 237 | /** |
238 | * check contact activity profile without activity id | |
239 | */ | |
6a488035 TO |
240 | function testContactActivityGetWithoutActivityId() { |
241 | list($params, $expected) = $this->_createContactWithActivity(); | |
242 | ||
243 | unset($params['activity_id']); | |
7fbb4198 | 244 | $result = $this->callAPIFailure('profile', 'get', $params, |
245 | 'Mandatory key(s) missing from params array: activity_id'); | |
6a488035 TO |
246 | } |
247 | ||
7fbb4198 | 248 | /** |
249 | * check contact activity profile wrong activity id | |
250 | */ | |
6a488035 TO |
251 | function testContactActivityGetWrongActivityId() { |
252 | list($params, $expected) = $this->_createContactWithActivity(); | |
253 | ||
254 | $params['activity_id'] = 100001; | |
7fbb4198 | 255 | $result = $this->callAPIFailure('profile', 'get', $params, |
21170390 | 256 | 'Invalid Activity Id (aid).'); |
6a488035 TO |
257 | } |
258 | ||
259 | /* | |
260 | * check contact activity profile with wrong activity type | |
261 | */ | |
262 | function testContactActivityGetWrongActivityType() { | |
263 | //flush cache by calling with reset | |
264 | $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name', TRUE); | |
265 | ||
266 | $sourceContactId = $this->householdCreate(); | |
267 | ||
268 | $activityparams = array( | |
269 | 'source_contact_id' => $sourceContactId, | |
270 | 'activity_type_id' => '2', | |
271 | 'subject' => 'Test activity', | |
272 | 'activity_date_time' => '20110316', | |
273 | 'duration' => '120', | |
274 | 'location' => 'Pensulvania', | |
275 | 'details' => 'a test activity', | |
276 | 'status_id' => '1', | |
6a488035 TO |
277 | 'priority_id' => '1', |
278 | ); | |
279 | ||
7fbb4198 | 280 | $activity = $this->callAPISuccess('activity', 'create', $activityparams); |
6a488035 TO |
281 | |
282 | $activityValues = array_pop($activity['values']); | |
283 | ||
284 | list($params, $expected) = $this->_createContactWithActivity(); | |
285 | ||
286 | $params['activity_id'] = $activityValues['id']; | |
7fbb4198 | 287 | $result = $this->callAPIFailure('profile', 'get', $params, |
288 | 'This activity cannot be edited or viewed via this profile.' | |
289 | ); | |
6a488035 TO |
290 | } |
291 | ||
292 | /* | |
293 | * check contact activity profile with success | |
294 | */ | |
295 | function testContactActivityGetSuccess() { | |
296 | list($params, $expected) = $this->_createContactWithActivity(); | |
297 | ||
7fbb4198 | 298 | $result = $this->callAPISuccess('profile', 'get', $params); |
6a488035 TO |
299 | |
300 | foreach ($expected as $profileField => $value) { | |
301 | $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$profileField}" | |
302 | ); | |
303 | } | |
6a488035 TO |
304 | } |
305 | ||
6a488035 | 306 | /** |
9dec4e61 | 307 | * Check getfields works & gives us our fields |
308 | */ | |
309 | function testGetFields() { | |
310 | $this->_createIndividualProfile(); | |
311 | $this->_addCustomFieldToProfile($this->_profileID); | |
9da2e77c | 312 | $result = $this->callAPIAndDocument('profile', 'getfields', array('action' => 'submit', 'profile_id' => $this->_profileID), __FUNCTION__, __FILE__, |
9dec4e61 | 313 | 'demonstrates retrieving profile fields passing in an id'); |
314 | $this->assertArrayKeyExists('first_name', $result['values']); | |
315 | $this->assertEquals('2', $result['values']['first_name']['type']); | |
9da2e77c | 316 | $this->assertEquals('Email', $result['values']['email-primary']['title']); |
9dec4e61 | 317 | $this->assertEquals('civicrm_state_province', $result['values']['state_province-1']['pseudoconstant']['table']); |
318 | $this->assertEquals('defaultValue', $result['values']['custom_1']['default_value']); | |
319 | $this->assertFalse(array_key_exists('participant_status', $result['values'])); | |
320 | } | |
321 | /** | |
b0b44427 | 322 | * Check getfields works & gives us our fields - partipant profile |
6a488035 | 323 | */ |
9dec4e61 | 324 | function testGetFieldsParticipantProfile() { |
325 | $result = $this->callAPISuccess('profile', 'getfields', array( | |
326 | 'action' => 'submit', | |
327 | 'profile_id' => 'participant_status', | |
328 | 'get_options' => 'all') | |
329 | ); | |
29fbb90a E |
330 | $this->assertTrue(array_key_exists('participant_status_id', $result['values'])); |
331 | $this->assertEquals('Attended', $result['values']['participant_status_id']['options'][2]); | |
332 | $this->assertEquals(array('participant_status'), $result['values']['participant_status_id']['api.aliases']); | |
6a488035 | 333 | } |
b0b44427 | 334 | |
335 | /** | |
336 | * Check getfields works & gives us our fields - membership_batch_entry | |
337 | * (getting to the end with no e-notices is pretty good evidence it's working) | |
338 | */ | |
339 | function testGetFieldsMembershipBatchProfile() { | |
340 | $result = $this->callAPISuccess('profile', 'getfields', array( | |
341 | 'action' => 'submit', | |
342 | 'profile_id' => 'membership_batch_entry', | |
343 | 'get_options' => 'all') | |
344 | ); | |
345 | $this->assertTrue(array_key_exists('total_amount', $result['values'])); | |
7c3f2c03 E |
346 | $this->assertTrue(array_key_exists('financial_type_id', $result['values'])); |
347 | $this->assertEquals(array('contribution_type_id', 'contribution_type', 'financial_type'), $result['values']['financial_type_id']['api.aliases']); | |
348 | $this->assertTrue(!array_key_exists('financial_type', $result['values'])); | |
b0b44427 | 349 | $this->assertEquals(12, $result['values']['receive_date']['type']); |
350 | } | |
351 | ||
352 | /** | |
353 | * Check getfields works & gives us our fields - do them all | |
354 | * (getting to the end with no e-notices is pretty good evidence it's working) | |
355 | */ | |
356 | function testGetFieldsAllProfiles() { | |
357 | $result = $this->callAPISuccess('uf_group', 'get', array('return' => 'id')); | |
358 | $profileIDs = array_keys($result['values']); | |
359 | foreach ($profileIDs as $profileID) { | |
21170390 | 360 | $result = $this->callAPISuccess('profile', 'getfields', array( |
361 | 'action' => 'submit', | |
362 | 'profile_id' => $profileID, | |
363 | 'get_options' => 'all') | |
364 | ); | |
b0b44427 | 365 | } |
366 | } | |
9dec4e61 | 367 | /////////////// test $this->callAPISuccess3_profile_set ////////////////// |
6a488035 TO |
368 | |
369 | /** | |
370 | * check Without ProfileId | |
371 | */ | |
9dec4e61 | 372 | function testProfileSubmitWithoutProfileId() { |
6a488035 TO |
373 | $params = array( |
374 | 'contact_id' => 1, | |
6a488035 | 375 | ); |
9dec4e61 | 376 | $result = $this->callAPIFailure('profile', 'submit', $params, |
7fbb4198 | 377 | 'Mandatory key(s) missing from params array: profile_id' |
378 | ); | |
6a488035 TO |
379 | } |
380 | ||
381 | /** | |
382 | * check with no invalid profile Id | |
383 | */ | |
9dec4e61 | 384 | function testProfileSubmitInvalidProfileId() { |
6a488035 TO |
385 | $params = array( |
386 | 'contact_id' => 1, | |
387 | 'profile_id' => 1000, | |
6a488035 | 388 | ); |
9dec4e61 | 389 | $result = $this->callAPIFailure('profile', 'submit', $params); |
6a488035 TO |
390 | } |
391 | ||
392 | /** | |
393 | * check with missing required field in profile | |
394 | */ | |
9dec4e61 | 395 | function testProfileSubmitCheckProfileRequired() { |
6a488035 TO |
396 | $pofileFieldValues = $this->_createIndividualContact(); |
397 | current($pofileFieldValues); | |
398 | $contactId = key($pofileFieldValues); | |
399 | $updateParams = array( | |
400 | 'first_name' => 'abc2', | |
401 | 'last_name' => 'xyz2', | |
402 | 'phone-1-1' => '022 321 826', | |
403 | 'country-1' => '1013', | |
404 | 'state_province-1' => '1000', | |
405 | ); | |
406 | ||
9da2e77c | 407 | $params = array_merge(array('profile_id' => $this->_profileID, 'contact_id' => $contactId), |
6a488035 TO |
408 | $updateParams |
409 | ); | |
410 | ||
9dec4e61 | 411 | $result = $this->callAPIFailure('profile', 'submit', $params, |
9da2e77c | 412 | "Missing required parameters for profile id $this->_profileID: email-Primary" |
7fbb4198 | 413 | ); |
6a488035 TO |
414 | } |
415 | ||
416 | /** | |
417 | * check with success | |
418 | */ | |
9dec4e61 | 419 | function testProfileSubmit() { |
6a488035 TO |
420 | $pofileFieldValues = $this->_createIndividualContact(); |
421 | current($pofileFieldValues); | |
422 | $contactId = key($pofileFieldValues); | |
423 | ||
424 | $updateParams = array( | |
425 | 'first_name' => 'abc2', | |
426 | 'last_name' => 'xyz2', | |
5aa090ea | 427 | 'email-primary' => 'abc2.xyz2@gmail.com', |
6a488035 TO |
428 | 'phone-1-1' => '022 321 826', |
429 | 'country-1' => '1013', | |
430 | 'state_province-1' => '1000', | |
431 | ); | |
432 | ||
433 | $params = array_merge(array( | |
9da2e77c | 434 | 'profile_id' => $this->_profileID, |
21170390 | 435 | 'contact_id' => $contactId, |
436 | ), $updateParams); | |
6a488035 | 437 | |
9dec4e61 | 438 | $result = $this->callAPIAndDocument('profile', 'submit', $params, __FUNCTION__, __FILE__); |
6a488035 TO |
439 | |
440 | $getParams = array( | |
9da2e77c | 441 | 'profile_id' => $this->_profileID, |
6a488035 | 442 | 'contact_id' => $contactId, |
6a488035 | 443 | ); |
7fbb4198 | 444 | $profileDetails = $this->callAPISuccess('profile', 'get', $getParams); |
6a488035 TO |
445 | |
446 | foreach ($updateParams as $profileField => $value) { | |
f5bf5e4f | 447 | $this->assertEquals($value, CRM_Utils_Array::value($profileField, $profileDetails['values']), "missing/mismatching value for {$profileField}" |
6a488035 TO |
448 | ); |
449 | } | |
f5bf5e4f E |
450 | unset($params['email-primary']); |
451 | $params['email-Primary'] = 'my@mail.com'; | |
702c5ce2 | 452 | $this->callAPISuccess('profile', 'submit', $params); |
5aa090ea | 453 | $profileDetails = $this->callAPISuccess('profile', 'get', $getParams); |
f5bf5e4f | 454 | $this->assertEquals('my@mail.com', $profileDetails['values']['email-Primary']); |
6a488035 TO |
455 | } |
456 | ||
7c3f2c03 E |
457 | /** |
458 | * Ensure caches are being cleared so we don't get into a debugging trap because of cached metadata | |
459 | * First we delete & create to increment the version & then check for caching probs | |
460 | */ | |
461 | function testProfileSubmitCheckCaching() { | |
462 | $this->callAPISuccess('membership_type', 'delete', array('id' => $this->_membershipTypeID)); | |
463 | $this->_membershipTypeID = $this->membershipTypeCreate(); | |
464 | ||
465 | $membershipTypes = $this->callAPISuccess('membership_type', 'get', array()); | |
466 | $profileFields = $this->callAPISuccess('profile', 'getfields', array('get_options' => 'all', 'action' => 'submit', 'profile_id' => 'membership_batch_entry')); | |
467 | $getoptions = $this->callAPISuccess('membership', 'getoptions', array('field' => 'membership_type', 'context' => 'validate')); | |
468 | $this->assertEquals(array_keys($membershipTypes['values']), array_keys($getoptions['values'])); | |
f5c68f3c | 469 | $this->assertEquals(array_keys($membershipTypes['values']), array_keys($profileFields['values']['membership_type_id']['options'])); |
7c3f2c03 E |
470 | |
471 | } | |
f5c68f3c E |
472 | |
473 | /** | |
474 | * Test that the fields are returned in the right order despite the faffing around that goes on | |
475 | */ | |
476 | function testMembershipGetFieldsOrder() { | |
477 | $result = $this->callAPISuccess('profile', 'getfields', array('action' => 'submit', 'profile_id' => 'membership_batch_entry')); | |
478 | $weight = 1; | |
479 | foreach($result['values'] as $fieldName => $field) { | |
480 | if($fieldName == 'profile_id') { | |
481 | continue; | |
482 | } | |
483 | $this->assertEquals($field['weight'], $weight); | |
484 | $weight++; | |
485 | } | |
486 | } | |
7c3f2c03 E |
487 | /** |
488 | * Check we can submit membership batch profiles (create mode) | |
489 | */ | |
490 | function testProfileSubmitMembershipBatch() { | |
491 | $this->_contactID = $this->individualCreate(); | |
492 | $this->callAPISuccess('profile', 'submit', array( | |
493 | 'profile_id' => 'membership_batch_entry', | |
494 | 'financial_type_id' => 1, | |
495 | 'membership_type' => $this->_membershipTypeID, | |
496 | 'join_date' => 'now', | |
497 | 'total_amount' => 10, | |
498 | 'contribution_status_id' => 1, | |
499 | 'receive_date' => 'now', | |
500 | 'contact_id' => $this->_contactID, | |
501 | )); | |
502 | } | |
158d3e03 | 503 | /** |
504 | * set is deprecated but we need to ensure it still works | |
505 | */ | |
506 | function testLegacySet() { | |
507 | $pofileFieldValues = $this->_createIndividualContact(); | |
508 | current($pofileFieldValues); | |
509 | $contactId = key($pofileFieldValues); | |
510 | ||
511 | $updateParams = array( | |
512 | 'first_name' => 'abc2', | |
513 | 'last_name' => 'xyz2', | |
514 | 'email-Primary' => 'abc2.xyz2@gmail.com', | |
515 | 'phone-1-1' => '022 321 826', | |
516 | 'country-1' => '1013', | |
517 | 'state_province-1' => '1000', | |
518 | ); | |
519 | ||
520 | $params = array_merge(array( | |
9da2e77c | 521 | 'profile_id' => $this->_profileID, |
158d3e03 | 522 | 'contact_id' => $contactId, |
523 | ), $updateParams); | |
524 | ||
525 | $result = $this->callAPISuccess('profile', 'set', $params); | |
526 | $this->assertArrayKeyExists('values', $result); | |
527 | $getParams = array( | |
9da2e77c | 528 | 'profile_id' => $this->_profileID, |
158d3e03 | 529 | 'contact_id' => $contactId, |
530 | ); | |
531 | $profileDetails = $this->callAPISuccess('profile', 'get', $getParams); | |
532 | ||
533 | foreach ($updateParams as $profileField => $value) { | |
534 | $this->assertEquals($value, CRM_Utils_Array::value($profileField, $profileDetails['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$profileField}" | |
535 | ); | |
536 | } | |
537 | } | |
6a488035 TO |
538 | /* |
539 | * check contact activity profile without activity id | |
540 | */ | |
9dec4e61 | 541 | function testContactActivitySubmitWithoutActivityId() { |
6a488035 TO |
542 | list($params, $expected) = $this->_createContactWithActivity(); |
543 | ||
544 | $params = array_merge($params, $expected); | |
545 | unset($params['activity_id']); | |
9dec4e61 | 546 | $result = $this->callAPIFailure('profile', 'submit', $params); |
6a488035 | 547 | $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: activity_id'); |
6a488035 TO |
548 | } |
549 | ||
550 | /* | |
551 | * check contact activity profile wrong activity id | |
552 | */ | |
9dec4e61 | 553 | function testContactActivitySubmitWrongActivityId() { |
6a488035 | 554 | list($params, $expected) = $this->_createContactWithActivity(); |
6a488035 TO |
555 | $params = array_merge($params, $expected); |
556 | $params['activity_id'] = 100001; | |
9dec4e61 | 557 | $result = $this->callAPIFailure('profile', 'submit', $params); |
6a488035 | 558 | $this->assertEquals($result['error_message'], 'Invalid Activity Id (aid).'); |
6a488035 TO |
559 | } |
560 | ||
561 | /* | |
562 | * check contact activity profile with wrong activity type | |
563 | */ | |
9dec4e61 | 564 | function testContactActivitySubmitWrongActivityType() { |
6a488035 TO |
565 | //flush cache by calling with reset |
566 | $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name', TRUE); | |
567 | ||
568 | $sourceContactId = $this->householdCreate(); | |
569 | ||
570 | $activityparams = array( | |
571 | 'source_contact_id' => $sourceContactId, | |
572 | 'activity_type_id' => '2', | |
573 | 'subject' => 'Test activity', | |
574 | 'activity_date_time' => '20110316', | |
575 | 'duration' => '120', | |
576 | 'location' => 'Pensulvania', | |
577 | 'details' => 'a test activity', | |
578 | 'status_id' => '1', | |
6a488035 TO |
579 | 'priority_id' => '1', |
580 | ); | |
581 | ||
7fbb4198 | 582 | $activity = $this->callAPISuccess('activity', 'create', $activityparams); |
6a488035 TO |
583 | |
584 | $activityValues = array_pop($activity['values']); | |
585 | ||
586 | list($params, $expected) = $this->_createContactWithActivity(); | |
587 | ||
588 | $params = array_merge($params, $expected); | |
589 | $params['activity_id'] = $activityValues['id']; | |
9dec4e61 | 590 | $result = $this->callAPIFailure('profile', 'submit', $params, |
7fbb4198 | 591 | 'This activity cannot be edited or viewed via this profile.'); |
6a488035 TO |
592 | } |
593 | ||
594 | /* | |
595 | * check contact activity profile with success | |
596 | */ | |
9dec4e61 | 597 | function testContactActivitySubmitSuccess() { |
6a488035 TO |
598 | list($params, $expected) = $this->_createContactWithActivity(); |
599 | ||
600 | $updateParams = array( | |
601 | 'first_name' => 'abc2', | |
602 | 'last_name' => 'xyz2', | |
603 | 'email-Primary' => 'abc2.xyz2@yahoo.com', | |
604 | 'activity_subject' => 'Test Meeting', | |
605 | 'activity_details' => 'a test activity details', | |
606 | 'activity_duration' => '100', | |
607 | 'activity_date_time' => '03/08/2010', | |
608 | 'activity_status_id' => '2', | |
609 | ); | |
610 | $profileParams = array_merge($params, $updateParams); | |
c3d3e837 E |
611 | $this->callAPISuccess('profile', 'submit', $profileParams); |
612 | $result = $this->callAPISuccess('profile', 'get', $params); | |
6a488035 TO |
613 | |
614 | foreach ($updateParams as $profileField => $value) { | |
615 | $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$profileField}" | |
616 | ); | |
617 | } | |
6a488035 TO |
618 | } |
619 | ||
6a488035 | 620 | /** |
7fbb4198 | 621 | * check profile apply Without ProfileId |
6a488035 TO |
622 | */ |
623 | function testProfileApplyWithoutProfileId() { | |
624 | $params = array( | |
625 | 'contact_id' => 1, | |
6a488035 | 626 | ); |
7fbb4198 | 627 | $result = $this->callAPIFailure('profile', 'apply', $params, |
628 | 'Mandatory key(s) missing from params array: profile_id'); | |
6a488035 TO |
629 | } |
630 | ||
631 | /** | |
7fbb4198 | 632 | * check profile apply with no invalid profile Id |
6a488035 TO |
633 | */ |
634 | function testProfileApplyInvalidProfileId() { | |
635 | $params = array( | |
636 | 'contact_id' => 1, | |
637 | 'profile_id' => 1000, | |
6a488035 | 638 | ); |
d0e1eff2 | 639 | $result = $this->callAPIFailure('profile', 'apply', $params); |
6a488035 TO |
640 | } |
641 | ||
642 | /** | |
643 | * check with success | |
644 | */ | |
645 | function testProfileApply() { | |
646 | $pofileFieldValues = $this->_createIndividualContact(); | |
647 | current($pofileFieldValues); | |
648 | $contactId = key($pofileFieldValues); | |
649 | ||
650 | $params = array( | |
9da2e77c | 651 | 'profile_id' => $this->_profileID, |
6a488035 | 652 | 'contact_id' => $contactId, |
6a488035 TO |
653 | 'first_name' => 'abc2', |
654 | 'last_name' => 'xyz2', | |
655 | 'email-Primary' => 'abc2.xyz2@gmail.com', | |
656 | 'phone-1-1' => '022 321 826', | |
657 | 'country-1' => '1013', | |
658 | 'state_province-1' => '1000', | |
659 | ); | |
660 | ||
7fbb4198 | 661 | $result = $this->callAPIAndDocument('profile', 'apply', $params, __FUNCTION__, __FILE__); |
6a488035 TO |
662 | |
663 | // Expected field values | |
664 | $expected['contact'] = array( | |
665 | 'contact_id' => $contactId, | |
666 | 'contact_type' => 'Individual', | |
667 | 'first_name' => 'abc2', | |
668 | 'last_name' => 'xyz2', | |
669 | ); | |
670 | $expected['email'] = array( | |
671 | 'location_type_id' => 1, | |
672 | 'is_primary' => 1, | |
673 | 'email' => 'abc2.xyz2@gmail.com', | |
674 | ); | |
675 | ||
676 | $expected['phone'] = array( | |
677 | 'location_type_id' => 1, | |
678 | 'is_primary' => 1, | |
679 | 'phone_type_id' => 1, | |
680 | 'phone' => '022 321 826', | |
681 | ); | |
682 | $expected['address'] = array( | |
683 | 'location_type_id' => 1, | |
684 | 'is_primary' => 1, | |
685 | 'country_id' => 1013, | |
686 | 'state_province_id' => 1000, | |
687 | ); | |
688 | ||
689 | foreach ($expected['contact'] as $field => $value) { | |
690 | $this->assertEquals($value, CRM_Utils_Array::value($field, $result['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$field}" | |
691 | ); | |
692 | } | |
693 | ||
694 | foreach (array( | |
695 | 'email', 'phone', 'address') as $fieldType) { | |
696 | $typeValues = array_pop($result['values'][$fieldType]); | |
697 | foreach ($expected[$fieldType] as $field => $value) { | |
698 | $this->assertEquals($value, CRM_Utils_Array::value($field, $typeValues), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$field} ({$fieldType})" | |
699 | ); | |
700 | } | |
701 | } | |
702 | } | |
703 | ||
704 | /* | |
705 | * Helper function to create an Individual with address/email/phone info. Import UF Group and UF Fields | |
706 | */ | |
4cbe18b8 EM |
707 | /** |
708 | * @param array $params | |
709 | * | |
710 | * @return mixed | |
711 | */ | |
e0efd2d0 | 712 | function _createIndividualContact($params = array()) { |
713 | $contactParams = array_merge(array( | |
6a488035 TO |
714 | 'first_name' => 'abc1', |
715 | 'last_name' => 'xyz1', | |
6a488035 | 716 | 'email' => 'abc1.xyz1@yahoo.com', |
6a488035 TO |
717 | 'api.address.create' => array( |
718 | 'location_type_id' => 1, | |
719 | 'is_primary' => 1, | |
b29f74b6 | 720 | 'street_address' => '5 Saint Helier St', |
6a488035 TO |
721 | 'county' => 'Marin', |
722 | 'country' => 'United States', | |
723 | 'state_province' => 'Michigan', | |
724 | 'supplemental_address_1' => 'Hallmark Ct', | |
725 | 'supplemental_address_2' => 'Jersey Village', | |
e0efd2d0 | 726 | 'postal_code' => '90210', |
b29f74b6 | 727 | 'city' => 'Gotham City', |
728 | 'is_billing' => 0, | |
6a488035 TO |
729 | ), |
730 | 'api.phone.create' => array( | |
731 | 'location_type_id' => '1', | |
732 | 'phone' => '021 512 755', | |
733 | 'phone_type_id' => '1', | |
734 | 'is_primary' => '1', | |
735 | ), | |
e0efd2d0 | 736 | ), $params |
6a488035 TO |
737 | ); |
738 | ||
174dbdd5 | 739 | $this->_contactID = $this->individualCreate($contactParams); |
9dec4e61 | 740 | $this->_createIndividualProfile(); |
6a488035 | 741 | // expected result of above created profile with contact Id $contactId |
174dbdd5 | 742 | $profileData[$this->_contactID] = array( |
6a488035 TO |
743 | 'first_name' => 'abc1', |
744 | 'last_name' => 'xyz1', | |
9da2e77c | 745 | 'email-primary' => 'abc1.xyz1@yahoo.com', |
6a488035 TO |
746 | 'phone-1-1' => '021 512 755', |
747 | 'country-1' => '1228', | |
748 | 'state_province-1' => '1021', | |
749 | ); | |
750 | ||
751 | return $profileData; | |
752 | } | |
753 | ||
4cbe18b8 EM |
754 | /** |
755 | * @return array | |
756 | */ | |
6a488035 TO |
757 | function _createContactWithActivity() { |
758 | // @TODO: Create profile with custom fields | |
759 | $op = new PHPUnit_Extensions_Database_Operation_Insert(); | |
760 | $op->execute($this->_dbconn, | |
761 | new PHPUnit_Extensions_Database_DataSet_FlatXMLDataSet( | |
762 | dirname(__FILE__) . '/dataset/uf_group_contact_activity_26.xml' | |
763 | ) | |
764 | ); | |
765 | // hack: xml data set do not accept \ 1 (CRM_Core_DAO::VALUE_SEPARATOR) | |
766 | CRM_Core_DAO::setFieldValue('CRM_Core_DAO_UFGroup', '26', 'group_type', 'Individual,Contact,Activity' . CRM_Core_DAO::VALUE_SEPARATOR . 'ActivityType:1'); | |
767 | ||
768 | $sourceContactId = $this->individualCreate(); | |
769 | $contactParams = array( | |
770 | 'first_name' => 'abc1', | |
771 | 'last_name' => 'xyz1', | |
772 | 'contact_type' => 'Individual', | |
773 | 'email' => 'abc1.xyz1@yahoo.com', | |
6a488035 TO |
774 | 'api.address.create' => array( |
775 | 'location_type_id' => 1, | |
776 | 'is_primary' => 1, | |
777 | 'name' => 'Saint Helier St', | |
778 | 'county' => 'Marin', | |
779 | 'country' => 'United States', | |
780 | 'state_province' => 'Michigan', | |
781 | 'supplemental_address_1' => 'Hallmark Ct', | |
782 | 'supplemental_address_2' => 'Jersey Village', | |
783 | ), | |
784 | ); | |
785 | ||
7fbb4198 | 786 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 TO |
787 | |
788 | $keys = array_keys($contact['values']); | |
789 | $contactId = array_pop($keys); | |
790 | ||
791 | $this->assertEquals(0, $contact['values'][$contactId]['api.address.create']['is_error'], "In line " . __LINE__ . " error message: " . CRM_Utils_Array::value('error_message', $contact['values'][$contactId]['api.address.create']) | |
792 | ); | |
793 | ||
794 | $activityParams = array( | |
795 | 'source_contact_id' => $sourceContactId, | |
796 | 'assignee_contact_id' => $contactId, | |
797 | 'activity_type_id' => '1', | |
798 | 'subject' => 'Make-it-Happen Meeting', | |
799 | 'activity_date_time' => '20110316', | |
800 | 'duration' => '120', | |
801 | 'location' => 'Pensulvania', | |
802 | 'details' => 'a test activity', | |
803 | 'status_id' => '1', | |
6a488035 TO |
804 | 'priority_id' => '1', |
805 | ); | |
7fbb4198 | 806 | $activity = $this->callAPISuccess('activity', 'create', $activityParams); |
6a488035 TO |
807 | |
808 | $activityValues = array_pop($activity['values']); | |
809 | ||
810 | // valid parameters for above profile | |
811 | $profileParams = array( | |
812 | 'profile_id' => 26, | |
813 | 'contact_id' => $contactId, | |
814 | 'activity_id' => $activityValues['id'], | |
21170390 | 815 | ); |
6a488035 TO |
816 | |
817 | // expected result of above created profile | |
818 | $expected = array( | |
819 | 'first_name' => 'abc1', | |
820 | 'last_name' => 'xyz1', | |
821 | 'email-Primary' => 'abc1.xyz1@yahoo.com', | |
822 | 'activity_subject' => 'Make-it-Happen Meeting', | |
823 | 'activity_details' => 'a test activity', | |
824 | 'activity_duration' => '120', | |
825 | 'activity_date_time_time' => '12:00AM', | |
826 | 'activity_date_time' => '03/16/2011', | |
827 | 'activity_status_id' => '1', | |
828 | ); | |
829 | ||
830 | return array($profileParams, $expected); | |
831 | } | |
9dec4e61 | 832 | /** |
833 | * Create a profile | |
834 | */ | |
835 | function _createIndividualProfile() { | |
9da2e77c E |
836 | |
837 | // creating these via the api as we want to utilise & test the flushing of caches when fields created | |
838 | // via the api | |
839 | ||
840 | $ufGroupParams = array( | |
841 | 'group_type' => 'Individual,Contact',// really we should remove this & test the ufField create sets it | |
842 | 'name' => 'test_individual_contact_profile', | |
843 | 'title' => 'Flat Coffee', | |
844 | 'api.uf_field.create' => array( | |
845 | array( | |
846 | 'field_name' => 'first_name', | |
847 | 'is_required' => 1, | |
848 | 'visibility' => 'Public Pages and Listings', | |
849 | 'field_type' => 'Individual', | |
91d8fc8a | 850 | 'label' => 'First Name', |
9da2e77c E |
851 | ), |
852 | array( | |
853 | 'field_name' => 'last_name', | |
854 | 'is_required' => 1, | |
855 | 'visibility' => 'Public Pages and Listings', | |
856 | 'field_type' => 'Individual', | |
91d8fc8a | 857 | 'label' => 'Last Name', |
9da2e77c E |
858 | ), |
859 | array( | |
860 | 'field_name' => 'email', | |
861 | 'is_required' => 1, | |
862 | 'visibility' => 'Public Pages and Listings', | |
863 | 'field_type' => 'Contact', | |
864 | 'label' => 'Email', | |
865 | ), | |
866 | array( | |
867 | 'field_name' => 'phone', | |
868 | 'is_required' => 1, | |
869 | 'visibility' => 'Public Pages and Listings', | |
870 | 'field_type' => 'Contact', | |
871 | 'location_type_id' => 1, | |
872 | 'phone_type_id' => 1, | |
873 | 'label' => 'Phone' | |
874 | ), | |
875 | array( | |
876 | 'field_name' => 'country', | |
877 | 'is_required' => 1, | |
878 | 'visibility' => 'Public Pages and Listings', | |
879 | 'field_type' => 'Contact', | |
880 | 'location_type_id' => 1, | |
881 | 'label' => 'Country' | |
882 | ), | |
883 | array( | |
884 | 'field_name' => 'state_province', | |
885 | 'is_required' => 1, | |
886 | 'visibility' => 'Public Pages and Listings', | |
887 | 'field_type' => 'Contact', | |
888 | 'location_type_id' => 1, | |
889 | 'label' => 'State Province' | |
890 | ), | |
891 | array( | |
892 | 'field_name' => 'postal_code', | |
893 | 'is_required' => 0, | |
894 | 'field_type' => 'Contact', | |
895 | 'location_type_id' => 1, | |
896 | 'label' => 'State Province' | |
897 | ), | |
898 | ), | |
9dec4e61 | 899 | ); |
9da2e77c E |
900 | $profile = $this->callAPISuccess('uf_group', 'create', $ufGroupParams); |
901 | $this->_profileID = $profile['id']; | |
9dec4e61 | 902 | } |
903 | ||
4cbe18b8 EM |
904 | /** |
905 | * @param $profileID | |
906 | */ | |
9dec4e61 | 907 | function _addCustomFieldToProfile($profileID) { |
908 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, ''); | |
909 | $this->uFFieldCreate(array('uf_group_id' => $profileID, 'field_name' => 'custom_' . $ids['custom_field_id'], 'contact_type' => 'Contact')); | |
910 | } | |
6a488035 TO |
911 | } |
912 |