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