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