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