3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * Test APIv3 civicrm_profile_* functions
34 class api_v3_ProfileTest
extends CiviUnitTestCase
{
36 protected $_profileID = 0;
38 protected $_membershipTypeID;
40 protected $_contactID;
45 public function setUp() {
47 $config = CRM_Core_Config
::singleton();
48 $countryLimit = $config->countryLimit
;
49 $countryLimit[1] = 1013;
50 $config->countryLimit
= $countryLimit;
52 $this->createLoggedInUser();
53 $this->_membershipTypeID
= $this->membershipTypeCreate();
61 public function tearDown() {
68 'civicrm_contribution',
71 $this->callAPISuccess('membership_type', 'delete', ['id' => $this->_membershipTypeID
]);
72 CRM_Core_DAO
::executeQuery(" DELETE FROM civicrm_uf_group WHERE id = $this->_profileID OR name = 'test_contact_activity_profile'");
77 * Check Without ProfileId.
79 public function testProfileGetWithoutProfileId() {
80 $this->callAPIFailure('profile', 'get', ['contact_id' => 1],
81 'Mandatory key(s) missing from params array: profile_id'
86 * Check with no invalid profile Id.
88 public function testProfileGetInvalidProfileId() {
89 $this->callAPIFailure('profile', 'get', [
98 public function testProfileGet() {
99 $profileFieldValues = $this->_createIndividualContact();
100 $expected = reset($profileFieldValues);
101 $contactId = key($profileFieldValues);
103 'profile_id' => $this->_profileID
,
104 'contact_id' => $contactId,
106 $result = $this->callAPISuccess('profile', 'get', $params)['values'];
107 foreach ($expected as $profileField => $value) {
108 $this->assertEquals($value, CRM_Utils_Array
::value($profileField, $result));
113 * Test getting multiple profiles.
115 public function testProfileGetMultiple() {
116 $profileFieldValues = $this->_createIndividualContact();
117 $expected = reset($profileFieldValues);
118 $contactId = key($profileFieldValues);
120 'profile_id' => [$this->_profileID
, 1, 'Billing'],
121 'contact_id' => $contactId,
124 $result = $this->callAPIAndDocument('profile', 'get', $params, __FUNCTION__
, __FILE__
)['values'];
125 foreach ($expected as $profileField => $value) {
126 $this->assertEquals($value, CRM_Utils_Array
::value($profileField, $result[$this->_profileID
]), " error message: " . "missing/mismatching value for {$profileField}");
128 $this->assertEquals('abc1', $result[1]['first_name'], " error message: " . "missing/mismatching value for first name");
129 $this->assertFalse(array_key_exists('email-Primary', $result[1]), 'profile 1 does not include email');
130 $this->assertEquals($result['Billing'], [
131 'billing_first_name' => 'abc1',
132 'billing_middle_name' => 'J.',
133 'billing_last_name' => 'xyz1',
134 'billing_street_address-5' => '5 Saint Helier St',
135 'billing_city-5' => 'Gotham City',
136 'billing_state_province_id-5' => '1021',
137 'billing_country_id-5' => '1228',
138 'billing_postal_code-5' => '90210',
139 'billing-email-5' => 'abc1.xyz1@yahoo.com',
140 'email-5' => 'abc1.xyz1@yahoo.com',
145 * Test getting billing profile filled using is_billing.
147 public function testProfileGetBillingUseIsBillingLocation() {
148 $individual = $this->_createIndividualContact();
149 $contactId = key($individual);
150 $this->callAPISuccess('address', 'create', [
152 'street_address' => 'is billing st',
153 'location_type_id' => 2,
154 'contact_id' => $contactId,
158 'profile_id' => [$this->_profileID
, 1, 'Billing'],
159 'contact_id' => $contactId,
162 $result = $this->callAPISuccess('profile', 'get', $params)['values'];
163 $this->assertEquals('abc1', $result[1]['first_name']);
164 $this->assertEquals([
165 'billing_first_name' => 'abc1',
166 'billing_middle_name' => 'J.',
167 'billing_last_name' => 'xyz1',
168 'billing_street_address-5' => 'is billing st',
169 'billing_city-5' => '',
170 'billing_state_province_id-5' => '',
171 'billing_country_id-5' => '',
172 'billing-email-5' => 'abc1.xyz1@yahoo.com',
173 'email-5' => 'abc1.xyz1@yahoo.com',
174 'billing_postal_code-5' => '',
175 ], $result['Billing']);
179 * Test getting multiple profiles, including billing.
181 public function testProfileGetMultipleHasBillingLocation() {
182 $individual = $this->_createIndividualContact();
183 $contactId = key($individual);
184 $this->callAPISuccess('address', 'create', [
185 'contact_id' => $contactId,
186 'street_address' => '25 Big Street',
187 'city' => 'big city',
188 'location_type_id' => 5,
190 $this->callAPISuccess('email', 'create', [
191 'contact_id' => $contactId,
192 'email' => 'big@once.com',
193 'location_type_id' => 2,
198 'profile_id' => [$this->_profileID
, 1, 'Billing'],
199 'contact_id' => $contactId,
202 $result = $this->callAPISuccess('profile', 'get', $params);
203 $this->assertEquals('abc1', $result['values'][1]['first_name']);
204 $this->assertEquals($result['values']['Billing'], [
205 'billing_first_name' => 'abc1',
206 'billing_middle_name' => 'J.',
207 'billing_last_name' => 'xyz1',
208 'billing_street_address-5' => '25 Big Street',
209 'billing_city-5' => 'big city',
210 'billing_state_province_id-5' => '',
211 'billing_country_id-5' => '',
212 'billing-email-5' => 'big@once.com',
213 'email-5' => 'big@once.com',
214 'billing_postal_code-5' => '',
219 * Get Billing empty contact - this will return generic defaults.
221 public function testProfileGetBillingEmptyContact() {
222 $this->callAPISuccess('Setting', 'create', ['defaultContactCountry' => 1228]);
224 'profile_id' => ['Billing'],
227 $result = $this->callAPISuccess('profile', 'get', $params)['values'];
228 $this->assertEquals([
229 'billing_first_name' => '',
230 'billing_middle_name' => '',
231 'billing_last_name' => '',
232 'billing_street_address-5' => '',
233 'billing_city-5' => '',
234 'billing_state_province_id-5' => '',
235 'billing_country_id-5' => '1228',
236 'billing_email-5' => '',
238 'billing_postal_code-5' => '',
239 ], $result['Billing']);
243 * Check contact activity profile without activity id.
245 public function testContactActivityGetWithoutActivityId() {
246 list($params) = $this->_createContactWithActivity();
248 unset($params['activity_id']);
249 $this->callAPIFailure('profile', 'get', $params, 'Mandatory key(s) missing from params array: activity_id');
253 * Check contact activity profile wrong activity id.
255 public function testContactActivityGetWrongActivityId() {
256 list($params) = $this->_createContactWithActivity();
257 $params['activity_id'] = 100001;
258 $this->callAPIFailure('profile', 'get', $params, 'Invalid Activity Id (aid).');
262 * Check contact activity profile with wrong activity type.
266 public function testContactActivityGetWrongActivityType() {
267 $activity = $this->callAPISuccess('activity', 'create', [
268 'source_contact_id' => $this->householdCreate(),
269 'activity_type_id' => '2',
270 'subject' => 'Test activity',
271 'activity_date_time' => '20110316',
273 'location' => 'Pennsylvania',
274 'details' => 'a test activity',
276 'priority_id' => '1',
279 $activityValues = array_pop($activity);
281 list($params) = $this->_createContactWithActivity();
283 $params['activity_id'] = $activityValues['id'];
284 $this->callAPIFailure('profile', 'get', $params, 'This activity cannot be edited or viewed via this profile.');
288 * Check contact activity profile with success.
290 public function testContactActivityGetSuccess() {
291 list($params, $expected) = $this->_createContactWithActivity();
293 $result = $this->callAPISuccess('profile', 'get', $params);
295 foreach ($expected as $profileField => $value) {
296 $this->assertEquals($value, CRM_Utils_Array
::value($profileField, $result['values']), " error message: " . "missing/mismatching value for {$profileField}"
302 * Check getfields works & gives us our fields
304 public function testGetFields() {
305 $this->_createIndividualProfile();
306 $this->_addCustomFieldToProfile($this->_profileID
);
307 $result = $this->callAPIAndDocument('profile', 'getfields', [
308 'action' => 'submit',
309 'profile_id' => $this->_profileID
,
310 ], __FUNCTION__
, __FILE__
,
311 'demonstrates retrieving profile fields passing in an id');
312 $this->assertArrayKeyExists('first_name', $result['values']);
313 $this->assertEquals('2', $result['values']['first_name']['type']);
314 $this->assertEquals('Email', $result['values']['email-primary']['title']);
315 $this->assertEquals('civicrm_state_province', $result['values']['state_province-1']['pseudoconstant']['table']);
316 $this->assertEquals('defaultValue', $result['values']['custom_1']['default_value']);
317 $this->assertFalse(array_key_exists('participant_status', $result['values']));
321 * Check getfields works & gives us our fields - participant profile
323 public function testGetFieldsParticipantProfile() {
324 $result = $this->callAPISuccess('profile', 'getfields', [
325 'action' => 'submit',
326 'profile_id' => 'participant_status',
327 'get_options' => 'all',
329 $this->assertTrue(array_key_exists('participant_status_id', $result['values']));
330 $this->assertEquals('Attended', $result['values']['participant_status_id']['options'][2]);
331 $this->assertEquals(['participant_status'], $result['values']['participant_status_id']['api.aliases']);
335 * Check getfields works & gives us our fields - membership_batch_entry
336 * (getting to the end with no e-notices is pretty good evidence it's working)
338 public function testGetFieldsMembershipBatchProfile() {
339 $result = $this->callAPISuccess('profile', 'getfields', [
340 'action' => 'submit',
341 'profile_id' => 'membership_batch_entry',
342 'get_options' => 'all',
344 $this->assertTrue(array_key_exists('total_amount', $result['values']));
345 $this->assertTrue(array_key_exists('financial_type_id', $result['values']));
346 $this->assertEquals([
347 'contribution_type_id',
350 ], $result['values']['financial_type_id']['api.aliases']);
351 $this->assertTrue(!array_key_exists('financial_type', $result['values']));
352 $this->assertEquals(12, $result['values']['receive_date']['type']);
356 * Check getfields works & gives us our fields - do them all
357 * (getting to the end with no e-notices is pretty good evidence it's working)
359 public function testGetFieldsAllProfiles() {
360 $result = $this->callAPISuccess('uf_group', 'get', ['return' => 'id'])['values'];
361 $profileIDs = array_keys($result);
362 foreach ($profileIDs as $profileID) {
363 $this->callAPISuccess('profile', 'getfields', [
364 'action' => 'submit',
365 'profile_id' => $profileID,
366 'get_options' => 'all',
372 * Check Without ProfileId.
374 public function testProfileSubmitWithoutProfileId() {
378 $this->callAPIFailure('profile', 'submit', $params,
379 'Mandatory key(s) missing from params array: profile_id'
384 * Check with no invalid profile Id.
386 public function testProfileSubmitInvalidProfileId() {
389 'profile_id' => 1000,
391 $this->callAPIFailure('profile', 'submit', $params);
395 * Check with missing required field in profile.
397 public function testProfileSubmitCheckProfileRequired() {
398 $profileFieldValues = $this->_createIndividualContact();
399 $contactId = key($profileFieldValues);
401 'first_name' => 'abc2',
402 'last_name' => 'xyz2',
403 'phone-1-1' => '022 321 826',
404 'country-1' => '1013',
405 'state_province-1' => '1000',
408 $params = array_merge([
409 'profile_id' => $this->_profileID
,
410 'contact_id' => $contactId,
415 $this->callAPIFailure('profile', 'submit', $params,
416 "Mandatory key(s) missing from params array: email-primary"
421 * Check with success.
423 public function testProfileSubmit() {
424 $profileFieldValues = $this->_createIndividualContact();
425 $contactId = key($profileFieldValues);
428 'first_name' => 'abc2',
429 'last_name' => 'xyz2',
430 'email-primary' => 'abc2.xyz2@gmail.com',
431 'phone-1-1' => '022 321 826',
432 'country-1' => '1013',
433 'state_province-1' => '1000',
436 $params = array_merge([
437 'profile_id' => $this->_profileID
,
438 'contact_id' => $contactId,
441 $this->callAPIAndDocument('profile', 'submit', $params, __FUNCTION__
, __FILE__
);
444 'profile_id' => $this->_profileID
,
445 'contact_id' => $contactId,
447 $profileDetails = $this->callAPISuccess('profile', 'get', $getParams);
449 foreach ($updateParams as $profileField => $value) {
450 $this->assertEquals($value, CRM_Utils_Array
::value($profileField, $profileDetails['values']), "missing/mismatching value for {$profileField}"
453 unset($params['email-primary']);
454 $params['email-Primary'] = 'my@mail.com';
455 $this->callAPISuccess('profile', 'submit', $params);
456 $profileDetails = $this->callAPISuccess('profile', 'get', $getParams);
457 $this->assertEquals('my@mail.com', $profileDetails['values']['email-Primary']);
461 * Ensure caches are being cleared so we don't get into a debugging trap because of cached metadata
462 * First we delete & create to increment the version & then check for caching problems.
464 public function testProfileSubmitCheckCaching() {
465 $this->callAPISuccess('membership_type', 'delete', ['id' => $this->_membershipTypeID
]);
466 $this->_membershipTypeID
= $this->membershipTypeCreate();
468 $membershipTypes = $this->callAPISuccess('membership_type', 'get', []);
469 $profileFields = $this->callAPISuccess('profile', 'getfields', [
470 'get_options' => 'all',
471 'action' => 'submit',
472 'profile_id' => 'membership_batch_entry',
474 $getoptions = $this->callAPISuccess('membership', 'getoptions', [
475 'field' => 'membership_type',
476 'context' => 'validate',
478 $this->assertEquals(array_keys($membershipTypes['values']), array_keys($getoptions['values']));
479 $this->assertEquals(array_keys($membershipTypes['values']), array_keys($profileFields['values']['membership_type_id']['options']));
484 * Test that the fields are returned in the right order despite the faffing around that goes on.
486 public function testMembershipGetFieldsOrder() {
487 $result = $this->callAPISuccess('profile', 'getfields', [
488 'action' => 'submit',
489 'profile_id' => 'membership_batch_entry',
492 foreach ($result as $fieldName => $field) {
493 if ($fieldName == 'profile_id') {
496 $this->assertEquals($field['weight'], $weight);
502 * Check we can submit membership batch profiles (create mode)
504 public function testProfileSubmitMembershipBatch() {
505 $this->_contactID
= $this->individualCreate();
506 $this->callAPISuccess('profile', 'submit', [
507 'profile_id' => 'membership_batch_entry',
508 'financial_type_id' => 1,
509 'membership_type' => $this->_membershipTypeID
,
510 'join_date' => 'now',
511 'total_amount' => 10,
512 'contribution_status_id' => 1,
513 'receive_date' => 'now',
514 'contact_id' => $this->_contactID
,
519 * Set is deprecated but we need to ensure it still works.
521 public function testLegacySet() {
522 $profileFieldValues = $this->_createIndividualContact();
523 $contactId = key($profileFieldValues);
526 'first_name' => 'abc2',
527 'last_name' => 'xyz2',
528 'email-Primary' => 'abc2.xyz2@gmail.com',
529 'phone-1-1' => '022 321 826',
530 'country-1' => '1013',
531 'state_province-1' => '1000',
534 $params = array_merge([
535 'profile_id' => $this->_profileID
,
536 'contact_id' => $contactId,
539 $result = $this->callAPISuccess('profile', 'set', $params);
540 $this->assertArrayKeyExists('values', $result);
542 'profile_id' => $this->_profileID
,
543 'contact_id' => $contactId,
545 $profileDetails = $this->callAPISuccess('profile', 'get', $getParams);
547 foreach ($updateParams as $profileField => $value) {
548 $this->assertEquals($value, CRM_Utils_Array
::value($profileField, $profileDetails['values']), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$profileField}"
554 * Check contact activity profile without activity id.
556 public function testContactActivitySubmitWithoutActivityId() {
557 list($params, $expected) = $this->_createContactWithActivity();
559 $params = array_merge($params, $expected);
560 unset($params['activity_id']);
561 $this->callAPIFailure('profile', 'submit', $params, 'Mandatory key(s) missing from params array: activity_id');
565 * Check contact activity profile wrong activity id.
567 public function testContactActivitySubmitWrongActivityId() {
568 list($params, $expected) = $this->_createContactWithActivity();
569 $params = array_merge($params, $expected);
570 $params['activity_id'] = 100001;
571 $this->callAPIFailure('profile', 'submit', $params, 'Invalid Activity Id (aid).');
575 * Check contact activity profile with wrong activity type.
579 public function testContactActivitySubmitWrongActivityType() {
581 $sourceContactId = $this->householdCreate();
584 'source_contact_id' => $sourceContactId,
585 'activity_type_id' => '2',
586 'subject' => 'Test activity',
587 'activity_date_time' => '20110316',
589 'location' => 'Pennsylvania',
590 'details' => 'a test activity',
592 'priority_id' => '1',
595 $activity = $this->callAPISuccess('activity', 'create', $activityParams);
597 $activityValues = array_pop($activity['values']);
599 list($params, $expected) = $this->_createContactWithActivity();
601 $params = array_merge($params, $expected);
602 $params['activity_id'] = $activityValues['id'];
603 $this->callAPIFailure('profile', 'submit', $params,
604 'This activity cannot be edited or viewed via this profile.');
608 * Check contact activity profile with success.
610 public function testContactActivitySubmitSuccess() {
611 list($params) = $this->_createContactWithActivity();
614 'first_name' => 'abc2',
615 'last_name' => 'xyz2',
616 'email-Primary' => 'abc2.xyz2@yahoo.com',
617 'activity_subject' => 'Test Meeting',
618 'activity_details' => 'a test activity details',
619 'activity_duration' => '100',
620 'activity_date_time' => '2010-03-08 00:00:00',
621 'activity_status_id' => '2',
623 $profileParams = array_merge($params, $updateParams);
624 $this->callAPISuccess('profile', 'submit', $profileParams);
625 $result = $this->callAPISuccess('profile', 'get', $params)['values'];
627 foreach ($updateParams as $profileField => $value) {
628 $this->assertEquals($value, CRM_Utils_Array
::value($profileField, $result), " error message: " . "missing/mismatching value for {$profileField}"
634 * Check profile apply Without ProfileId.
636 public function testProfileApplyWithoutProfileId() {
640 $this->callAPIFailure('profile', 'apply', $params,
641 'Mandatory key(s) missing from params array: profile_id');
645 * Check profile apply with no invalid profile Id.
647 public function testProfileApplyInvalidProfileId() {
650 'profile_id' => 1000,
652 $this->callAPIFailure('profile', 'apply', $params);
656 * Check with success.
658 public function testProfileApply() {
659 $profileFieldValues = $this->_createIndividualContact();
660 current($profileFieldValues);
661 $contactId = key($profileFieldValues);
664 'profile_id' => $this->_profileID
,
665 'contact_id' => $contactId,
666 'first_name' => 'abc2',
667 'last_name' => 'xyz2',
668 'email-Primary' => 'abc2.xyz2@gmail.com',
669 'phone-1-1' => '022 321 826',
670 'country-1' => '1013',
671 'state_province-1' => '1000',
674 $result = $this->callAPIAndDocument('profile', 'apply', $params, __FUNCTION__
, __FILE__
);
676 // Expected field values
677 $expected['contact'] = [
678 'contact_id' => $contactId,
679 'contact_type' => 'Individual',
680 'first_name' => 'abc2',
681 'last_name' => 'xyz2',
683 $expected['email'] = [
684 'location_type_id' => 1,
686 'email' => 'abc2.xyz2@gmail.com',
689 $expected['phone'] = [
690 'location_type_id' => 1,
692 'phone_type_id' => 1,
693 'phone' => '022 321 826',
695 $expected['address'] = [
696 'location_type_id' => 1,
698 'country_id' => 1013,
699 'state_province_id' => 1000,
702 foreach ($expected['contact'] as $field => $value) {
703 $this->assertEquals($value, CRM_Utils_Array
::value($field, $result['values']), "missing/mismatching value for {$field}"
707 foreach (['email', 'phone', 'address'] as $fieldType) {
708 $typeValues = array_pop($result['values'][$fieldType]);
709 foreach ($expected[$fieldType] as $field => $value) {
710 $this->assertEquals($value, CRM_Utils_Array
::value($field, $typeValues), "missing/mismatching value for {$field} ({$fieldType})"
717 * Check success with tags.
719 public function testSubmitWithTags() {
720 $profileFieldValues = $this->_createIndividualContact();
721 $params = reset($profileFieldValues);
722 $contactId = key($profileFieldValues);
723 $params['profile_id'] = $this->_profileID
;
724 $params['contact_id'] = $contactId;
726 $this->callAPISuccess('ufField', 'create', [
727 'uf_group_id' => $this->_profileID
,
728 'field_name' => 'tag',
729 'visibility' => 'Public Pages and Listings',
730 'field_type' => 'Contact',
734 $tag_1 = $this->callAPISuccess('tag', 'create', ['name' => 'abc'])['id'];
735 $tag_2 = $this->callAPISuccess('tag', 'create', ['name' => 'def'])['id'];
737 $params['tag'] = "$tag_1,$tag_2";
738 $this->callAPISuccess('profile', 'submit', $params);
740 $tags = $this->callAPISuccess('entityTag', 'get', ['entity_id' => $contactId]);
741 $this->assertEquals(2, $tags['count']);
743 $params['tag'] = [$tag_1];
744 $this->callAPISuccess('profile', 'submit', $params);
746 $tags = $this->callAPISuccess('entityTag', 'get', ['entity_id' => $contactId]);
747 $this->assertEquals(1, $tags['count']);
750 $this->callAPISuccess('profile', 'submit', $params);
752 $tags = $this->callAPISuccess('entityTag', 'get', ['entity_id' => $contactId]);
753 $this->assertEquals(0, $tags['count']);
758 * Check success with a note.
762 public function testSubmitWithNote() {
763 $profileFieldValues = $this->_createIndividualContact();
764 $params = reset($profileFieldValues);
765 $contactId = key($profileFieldValues);
766 $params['profile_id'] = $this->_profileID
;
767 $params['contact_id'] = $contactId;
769 $this->callAPISuccess('ufField', 'create', [
770 'uf_group_id' => $this->_profileID
,
771 'field_name' => 'note',
772 'visibility' => 'Public Pages and Listings',
773 'field_type' => 'Contact',
777 $params['note'] = "Hello 123";
778 $this->callAPISuccess('profile', 'submit', $params);
780 $note = $this->callAPISuccessGetSingle('note', ['entity_id' => $contactId]);
781 $this->assertEquals("Hello 123", $note['note']);
785 * Check handling a custom greeting.
787 * @throws \CiviCRM_API3_Exception
789 public function testSubmitGreetingFields() {
790 $profileFieldValues = $this->_createIndividualContact();
791 $params = reset($profileFieldValues);
792 $contactId = key($profileFieldValues);
793 $params['profile_id'] = $this->_profileID
;
794 $params['contact_id'] = $contactId;
796 $this->callAPISuccess('ufField', 'create', [
797 'uf_group_id' => $this->_profileID
,
798 'field_name' => 'email_greeting',
799 'visibility' => 'Public Pages and Listings',
800 'field_type' => 'Contact',
801 'label' => 'Email Greeting',
804 $emailGreetings = array_column(civicrm_api3('OptionValue', 'get', ['option_group_id' => "email_greeting"])['values'], NULL, 'name');
806 $params['email_greeting'] = $emailGreetings['Customized']['value'];
807 // Custom greeting should be required
808 $this->callAPIFailure('profile', 'submit', $params);
810 $params['email_greeting_custom'] = 'Hello fool!';
811 $this->callAPISuccess('profile', 'submit', $params);
813 // Api3 will not return custom greeting field so resorting to this
814 $greeting = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'email_greeting_custom');
816 $this->assertEquals("Hello fool!", $greeting);
820 * Helper function to create an Individual with address/email/phone info. Import UF Group and UF Fields
822 * @param array $params
826 public function _createIndividualContact($params = []) {
827 $contactParams = array_merge([
828 'first_name' => 'abc1',
829 'last_name' => 'xyz1',
830 'email' => 'abc1.xyz1@yahoo.com',
831 'api.address.create' => [
832 'location_type_id' => 1,
834 'street_address' => '5 Saint Helier St',
836 'country' => 'UNITED STATES',
837 'state_province' => 'Michigan',
838 'supplemental_address_1' => 'Hallmark Ct',
839 'supplemental_address_2' => 'Jersey Village',
840 'supplemental_address_3' => 'My Town',
841 'postal_code' => '90210',
842 'city' => 'Gotham City',
845 'api.phone.create' => [
846 'location_type_id' => '1',
847 'phone' => '021 512 755',
848 'phone_type_id' => '1',
853 $this->_contactID
= $this->individualCreate($contactParams);
854 $this->_createIndividualProfile();
855 // expected result of above created profile with contact Id $contactId
856 $profileData[$this->_contactID
] = [
857 'first_name' => 'abc1',
858 'last_name' => 'xyz1',
859 'email-primary' => 'abc1.xyz1@yahoo.com',
860 'phone-1-1' => '021 512 755',
861 'country-1' => '1228',
862 'state_province-1' => '1021',
871 public function _createContactWithActivity() {
872 $ufGroupID = $this->callAPISuccess('UFGroup', 'create', [
873 'group_type' => 'Individual,Contact,Activity',
874 'title' => 'Test Contact-Activity Profile',
875 'name' => 'test_contact_activity_profile',
877 $this->callAPISuccess('UFField', 'create', [
878 'uf_group_id' => $ufGroupID,
879 'field_name' => 'first_name',
880 'is_required' => TRUE,
881 'visibility' => 'Public Pages and Listings',
882 'label' => 'First Name',
883 'field_type' => 'Individual',
885 $this->callAPISuccess('UFField', 'create', [
886 'uf_group_id' => $ufGroupID,
887 'field_name' => 'last_name',
888 'is_required' => TRUE,
889 'visibility' => 'Public Pages and Listings',
890 'label' => 'Last Name',
891 'field_type' => 'Individual',
893 $this->callAPISuccess('UFField', 'create', [
894 'uf_group_id' => $ufGroupID,
895 'field_name' => 'email',
896 'is_required' => TRUE,
897 'visibility' => 'Public Pages and Listings',
899 'field_type' => 'Contact',
901 $this->callAPISuccess('UFField', 'create', [
902 'uf_group_id' => $ufGroupID,
903 'field_name' => 'activity_subject',
904 'is_required' => TRUE,
905 'visibility' => 'Public Pages and Listings',
906 'label' => 'Activity Subject',
907 'is_searchable' => TRUE,
908 'field_type' => 'Activity',
910 $this->callAPISuccess('UFField', 'create', [
911 'uf_group_id' => $ufGroupID,
912 'field_name' => 'activity_details',
913 'is_required' => TRUE,
914 'visibility' => 'Public Pages and Listings',
915 'label' => 'Activity Details',
916 'is_searchable' => TRUE,
917 'field_type' => 'Activity',
919 $this->callAPISuccess('UFField', 'create', [
920 'uf_group_id' => $ufGroupID,
921 'field_name' => 'activity_duration',
922 'is_required' => TRUE,
923 'visibility' => 'Public Pages and Listings',
924 'label' => 'Activity Duration',
925 'is_searchable' => TRUE,
926 'field_type' => 'Activity',
928 $this->callAPISuccess('UFField', 'create', [
929 'uf_group_id' => $ufGroupID,
930 'field_name' => 'activity_date_time',
931 'is_required' => TRUE,
932 'visibility' => 'Public Pages and Listings',
933 'label' => 'Activity Date',
934 'is_searchable' => TRUE,
935 'field_type' => 'Activity',
937 $this->callAPISuccess('UFField', 'create', [
938 'uf_group_id' => $ufGroupID,
939 'field_name' => 'activity_status_id',
940 'is_required' => TRUE,
941 'visibility' => 'Public Pages and Listings',
942 'label' => 'Activity Status',
943 'is_searchable' => TRUE,
944 'field_type' => 'Activity',
947 // hack: xml data set did not accept \ 1 (CRM_Core_DAO::VALUE_SEPARATOR) - should be possible
948 // to unhack now we use the api.
949 CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_UFGroup', $ufGroupID, 'group_type', 'Individual,Contact,Activity' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'ActivityType:1');
951 $sourceContactId = $this->individualCreate();
953 'first_name' => 'abc1',
954 'last_name' => 'xyz1',
955 'contact_type' => 'Individual',
956 'email' => 'abc1.xyz1@yahoo.com',
957 'api.address.create' => [
958 'location_type_id' => 1,
960 'name' => 'Saint Helier St',
962 'country' => 'UNITED STATES',
963 'state_province' => 'Michigan',
964 'supplemental_address_1' => 'Hallmark Ct',
965 'supplemental_address_2' => 'Jersey Village',
966 'supplemental_address_3' => 'My Town',
970 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
972 $keys = array_keys($contact['values']);
973 $contactId = array_pop($keys);
975 $this->assertEquals(0, $contact['values'][$contactId]['api.address.create']['is_error'], " error message: " . CRM_Utils_Array
::value('error_message', $contact['values'][$contactId]['api.address.create'])
979 'source_contact_id' => $sourceContactId,
980 'assignee_contact_id' => $contactId,
981 'activity_type_id' => '1',
982 'subject' => 'Make-it-Happen Meeting',
983 'activity_date_time' => '2011-03-16 00:00:00',
985 'location' => 'Pennsylvania',
986 'details' => 'a test activity',
988 'priority_id' => '1',
990 $activity = $this->callAPISuccess('activity', 'create', $activityParams);
992 $activityValues = array_pop($activity['values']);
994 // valid parameters for above profile
996 'profile_id' => $ufGroupID,
997 'contact_id' => $contactId,
998 'activity_id' => $activityValues['id'],
1001 // expected result of above created profile
1003 'first_name' => 'abc1',
1004 'last_name' => 'xyz1',
1005 'email-Primary' => 'abc1.xyz1@yahoo.com',
1006 'activity_subject' => 'Make-it-Happen Meeting',
1007 'activity_details' => 'a test activity',
1008 'activity_duration' => '120',
1009 'activity_date_time' => '2011-03-16 00:00:00',
1010 'activity_status_id' => '1',
1013 return [$profileParams, $expected];
1019 public function _createIndividualProfile() {
1021 'group_type' => 'Individual,Contact',
1022 // really we should remove this & test the ufField create sets it
1023 'name' => 'test_individual_contact_profile',
1024 'title' => 'Flat Coffee',
1025 'api.uf_field.create' => [
1027 'field_name' => 'first_name',
1029 'visibility' => 'Public Pages and Listings',
1030 'field_type' => 'Individual',
1031 'label' => 'First Name',
1034 'field_name' => 'last_name',
1036 'visibility' => 'Public Pages and Listings',
1037 'field_type' => 'Individual',
1038 'label' => 'Last Name',
1041 'field_name' => 'email',
1043 'visibility' => 'Public Pages and Listings',
1044 'field_type' => 'Contact',
1048 'field_name' => 'phone',
1050 'visibility' => 'Public Pages and Listings',
1051 'field_type' => 'Contact',
1052 'location_type_id' => 1,
1053 'phone_type_id' => 1,
1057 'field_name' => 'country',
1059 'visibility' => 'Public Pages and Listings',
1060 'field_type' => 'Contact',
1061 'location_type_id' => 1,
1062 'label' => 'Country',
1065 'field_name' => 'state_province',
1067 'visibility' => 'Public Pages and Listings',
1068 'field_type' => 'Contact',
1069 'location_type_id' => 1,
1070 'label' => 'State Province',
1073 'field_name' => 'postal_code',
1075 'field_type' => 'Contact',
1076 'location_type_id' => 1,
1077 'label' => 'State Province',
1081 $profile = $this->callAPISuccess('uf_group', 'create', $ufGroupParams);
1082 $this->_profileID
= $profile['id'];
1086 * @param int $profileID
1088 public function _addCustomFieldToProfile($profileID) {
1089 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, '');
1090 $this->uFFieldCreate([
1091 'uf_group_id' => $profileID,
1092 'field_name' => 'custom_' . $ids['custom_field_id'],
1093 'contact_type' => 'Contact',