Merge pull request #9910 from seamuslee001/CRM-19835
[civicrm-core.git] / tests / phpunit / api / v3 / ProfileTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 = current($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 = current($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 $pofileFieldValues = $this->_createIndividualContact();
386 current($pofileFieldValues);
387 $contactId = key($pofileFieldValues);
388 $updateParams = array(
389 'first_name' => 'abc2',
390 'last_name' => 'xyz2',
391 'phone-1-1' => '022 321 826',
392 'country-1' => '1013',
393 'state_province-1' => '1000',
394 );
395
396 $params = array_merge(array('profile_id' => $this->_profileID, 'contact_id' => $contactId),
397 $updateParams
398 );
399
400 $this->callAPIFailure('profile', 'submit', $params,
401 "Mandatory key(s) missing from params array: email-primary"
402 );
403 }
404
405 /**
406 * Check with success.
407 */
408 public function testProfileSubmit() {
409 $pofileFieldValues = $this->_createIndividualContact();
410 current($pofileFieldValues);
411 $contactId = key($pofileFieldValues);
412
413 $updateParams = array(
414 'first_name' => 'abc2',
415 'last_name' => 'xyz2',
416 'email-primary' => 'abc2.xyz2@gmail.com',
417 'phone-1-1' => '022 321 826',
418 'country-1' => '1013',
419 'state_province-1' => '1000',
420 );
421
422 $params = array_merge(array(
423 'profile_id' => $this->_profileID,
424 'contact_id' => $contactId,
425 ), $updateParams);
426
427 $this->callAPIAndDocument('profile', 'submit', $params, __FUNCTION__, __FILE__);
428
429 $getParams = array(
430 'profile_id' => $this->_profileID,
431 'contact_id' => $contactId,
432 );
433 $profileDetails = $this->callAPISuccess('profile', 'get', $getParams);
434
435 foreach ($updateParams as $profileField => $value) {
436 $this->assertEquals($value, CRM_Utils_Array::value($profileField, $profileDetails['values']), "missing/mismatching value for {$profileField}"
437 );
438 }
439 unset($params['email-primary']);
440 $params['email-Primary'] = 'my@mail.com';
441 $this->callAPISuccess('profile', 'submit', $params);
442 $profileDetails = $this->callAPISuccess('profile', 'get', $getParams);
443 $this->assertEquals('my@mail.com', $profileDetails['values']['email-Primary']);
444 }
445
446 /**
447 * Ensure caches are being cleared so we don't get into a debugging trap because of cached metadata
448 * First we delete & create to increment the version & then check for caching probs
449 */
450 public function testProfileSubmitCheckCaching() {
451 $this->callAPISuccess('membership_type', 'delete', array('id' => $this->_membershipTypeID));
452 $this->_membershipTypeID = $this->membershipTypeCreate();
453
454 $membershipTypes = $this->callAPISuccess('membership_type', 'get', array());
455 $profileFields = $this->callAPISuccess('profile', 'getfields', array(
456 'get_options' => 'all',
457 'action' => 'submit',
458 'profile_id' => 'membership_batch_entry',
459 ));
460 $getoptions = $this->callAPISuccess('membership', 'getoptions', array(
461 'field' => 'membership_type',
462 'context' => 'validate',
463 ));
464 $this->assertEquals(array_keys($membershipTypes['values']), array_keys($getoptions['values']));
465 $this->assertEquals(array_keys($membershipTypes['values']), array_keys($profileFields['values']['membership_type_id']['options']));
466
467 }
468
469 /**
470 * Test that the fields are returned in the right order despite the faffing around that goes on.
471 */
472 public function testMembershipGetFieldsOrder() {
473 $result = $this->callAPISuccess('profile', 'getfields', array(
474 'action' => 'submit',
475 'profile_id' => 'membership_batch_entry',
476 ));
477 $weight = 1;
478 foreach ($result['values'] as $fieldName => $field) {
479 if ($fieldName == 'profile_id') {
480 continue;
481 }
482 $this->assertEquals($field['weight'], $weight);
483 $weight++;
484 }
485 }
486
487 /**
488 * Check we can submit membership batch profiles (create mode)
489 */
490 public function testProfileSubmitMembershipBatch() {
491 $this->_contactID = $this->individualCreate();
492 $this->callAPISuccess('profile', 'submit', array(
493 'profile_id' => 'membership_batch_entry',
494 'financial_type_id' => 1,
495 'membership_type' => $this->_membershipTypeID,
496 'join_date' => 'now',
497 'total_amount' => 10,
498 'contribution_status_id' => 1,
499 'receive_date' => 'now',
500 'contact_id' => $this->_contactID,
501 ));
502 }
503
504 /**
505 * Set is deprecated but we need to ensure it still works.
506 */
507 public function testLegacySet() {
508 $pofileFieldValues = $this->_createIndividualContact();
509 current($pofileFieldValues);
510 $contactId = key($pofileFieldValues);
511
512 $updateParams = array(
513 'first_name' => 'abc2',
514 'last_name' => 'xyz2',
515 'email-Primary' => 'abc2.xyz2@gmail.com',
516 'phone-1-1' => '022 321 826',
517 'country-1' => '1013',
518 'state_province-1' => '1000',
519 );
520
521 $params = array_merge(array(
522 'profile_id' => $this->_profileID,
523 'contact_id' => $contactId,
524 ), $updateParams);
525
526 $result = $this->callAPISuccess('profile', 'set', $params);
527 $this->assertArrayKeyExists('values', $result);
528 $getParams = array(
529 'profile_id' => $this->_profileID,
530 'contact_id' => $contactId,
531 );
532 $profileDetails = $this->callAPISuccess('profile', 'get', $getParams);
533
534 foreach ($updateParams as $profileField => $value) {
535 $this->assertEquals($value, CRM_Utils_Array::value($profileField, $profileDetails['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$profileField}"
536 );
537 }
538 }
539
540 /**
541 * Check contact activity profile without activity id.
542 */
543 public function testContactActivitySubmitWithoutActivityId() {
544 list($params, $expected) = $this->_createContactWithActivity();
545
546 $params = array_merge($params, $expected);
547 unset($params['activity_id']);
548 $result = $this->callAPIFailure('profile', 'submit', $params);
549 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: activity_id');
550 }
551
552 /**
553 * Check contact activity profile wrong activity id.
554 */
555 public function testContactActivitySubmitWrongActivityId() {
556 list($params, $expected) = $this->_createContactWithActivity();
557 $params = array_merge($params, $expected);
558 $params['activity_id'] = 100001;
559 $result = $this->callAPIFailure('profile', 'submit', $params);
560 $this->assertEquals($result['error_message'], 'Invalid Activity Id (aid).');
561 }
562
563 /**
564 * Check contact activity profile with wrong activity type.
565 */
566 public function testContactActivitySubmitWrongActivityType() {
567 //flush cache by calling with reset
568 CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
569
570 $sourceContactId = $this->householdCreate();
571
572 $activityparams = array(
573 'source_contact_id' => $sourceContactId,
574 'activity_type_id' => '2',
575 'subject' => 'Test activity',
576 'activity_date_time' => '20110316',
577 'duration' => '120',
578 'location' => 'Pensulvania',
579 'details' => 'a test activity',
580 'status_id' => '1',
581 'priority_id' => '1',
582 );
583
584 $activity = $this->callAPISuccess('activity', 'create', $activityparams);
585
586 $activityValues = array_pop($activity['values']);
587
588 list($params, $expected) = $this->_createContactWithActivity();
589
590 $params = array_merge($params, $expected);
591 $params['activity_id'] = $activityValues['id'];
592 $this->callAPIFailure('profile', 'submit', $params,
593 'This activity cannot be edited or viewed via this profile.');
594 }
595
596 /**
597 * Check contact activity profile with success.
598 */
599 public function testContactActivitySubmitSuccess() {
600 list($params) = $this->_createContactWithActivity();
601
602 $updateParams = array(
603 'first_name' => 'abc2',
604 'last_name' => 'xyz2',
605 'email-Primary' => 'abc2.xyz2@yahoo.com',
606 'activity_subject' => 'Test Meeting',
607 'activity_details' => 'a test activity details',
608 'activity_duration' => '100',
609 'activity_date_time' => '2010-03-08 00:00:00',
610 'activity_status_id' => '2',
611 );
612 $profileParams = array_merge($params, $updateParams);
613 $this->callAPISuccess('profile', 'submit', $profileParams);
614 $result = $this->callAPISuccess('profile', 'get', $params);
615
616 foreach ($updateParams as $profileField => $value) {
617 $this->assertEquals($value, CRM_Utils_Array::value($profileField, $result['values']), " error message: " . "missing/mismatching value for {$profileField}"
618 );
619 }
620 }
621
622 /**
623 * Check profile apply Without ProfileId.
624 */
625 public function testProfileApplyWithoutProfileId() {
626 $params = array(
627 'contact_id' => 1,
628 );
629 $this->callAPIFailure('profile', 'apply', $params,
630 'Mandatory key(s) missing from params array: profile_id');
631 }
632
633 /**
634 * Check profile apply with no invalid profile Id.
635 */
636 public function testProfileApplyInvalidProfileId() {
637 $params = array(
638 'contact_id' => 1,
639 'profile_id' => 1000,
640 );
641 $this->callAPIFailure('profile', 'apply', $params);
642 }
643
644 /**
645 * Check with success.
646 */
647 public function testProfileApply() {
648 $profileFieldValues = $this->_createIndividualContact();
649 current($profileFieldValues);
650 $contactId = key($profileFieldValues);
651
652 $params = array(
653 'profile_id' => $this->_profileID,
654 'contact_id' => $contactId,
655 'first_name' => 'abc2',
656 'last_name' => 'xyz2',
657 'email-Primary' => 'abc2.xyz2@gmail.com',
658 'phone-1-1' => '022 321 826',
659 'country-1' => '1013',
660 'state_province-1' => '1000',
661 );
662
663 $result = $this->callAPIAndDocument('profile', 'apply', $params, __FUNCTION__, __FILE__);
664
665 // Expected field values
666 $expected['contact'] = array(
667 'contact_id' => $contactId,
668 'contact_type' => 'Individual',
669 'first_name' => 'abc2',
670 'last_name' => 'xyz2',
671 );
672 $expected['email'] = array(
673 'location_type_id' => 1,
674 'is_primary' => 1,
675 'email' => 'abc2.xyz2@gmail.com',
676 );
677
678 $expected['phone'] = array(
679 'location_type_id' => 1,
680 'is_primary' => 1,
681 'phone_type_id' => 1,
682 'phone' => '022 321 826',
683 );
684 $expected['address'] = array(
685 'location_type_id' => 1,
686 'is_primary' => 1,
687 'country_id' => 1013,
688 'state_province_id' => 1000,
689 );
690
691 foreach ($expected['contact'] as $field => $value) {
692 $this->assertEquals($value, CRM_Utils_Array::value($field, $result['values']), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$field}"
693 );
694 }
695
696 foreach (array(
697 'email',
698 'phone',
699 'address',
700 ) as $fieldType) {
701 $typeValues = array_pop($result['values'][$fieldType]);
702 foreach ($expected[$fieldType] as $field => $value) {
703 $this->assertEquals($value, CRM_Utils_Array::value($field, $typeValues), "In line " . __LINE__ . " error message: " . "missing/mismatching value for {$field} ({$fieldType})"
704 );
705 }
706 }
707 }
708
709 /**
710 * Helper function to create an Individual with address/email/phone info. Import UF Group and UF Fields
711 * @param array $params
712 *
713 * @return mixed
714 */
715 public function _createIndividualContact($params = array()) {
716 $contactParams = array_merge(array(
717 'first_name' => 'abc1',
718 'last_name' => 'xyz1',
719 'email' => 'abc1.xyz1@yahoo.com',
720 'api.address.create' => array(
721 'location_type_id' => 1,
722 'is_primary' => 1,
723 'street_address' => '5 Saint Helier St',
724 'county' => 'Marin',
725 'country' => 'UNITED STATES',
726 'state_province' => 'Michigan',
727 'supplemental_address_1' => 'Hallmark Ct',
728 'supplemental_address_2' => 'Jersey Village',
729 'postal_code' => '90210',
730 'city' => 'Gotham City',
731 'is_billing' => 0,
732 ),
733 'api.phone.create' => array(
734 'location_type_id' => '1',
735 'phone' => '021 512 755',
736 'phone_type_id' => '1',
737 'is_primary' => '1',
738 ),
739 ), $params
740 );
741
742 $this->_contactID = $this->individualCreate($contactParams);
743 $this->_createIndividualProfile();
744 // expected result of above created profile with contact Id $contactId
745 $profileData[$this->_contactID] = array(
746 'first_name' => 'abc1',
747 'last_name' => 'xyz1',
748 'email-primary' => 'abc1.xyz1@yahoo.com',
749 'phone-1-1' => '021 512 755',
750 'country-1' => '1228',
751 'state_province-1' => '1021',
752 );
753
754 return $profileData;
755 }
756
757 /**
758 * @return array
759 */
760 public function _createContactWithActivity() {
761 // @TODO: Create profile with custom fields
762 $op = new PHPUnit_Extensions_Database_Operation_Insert();
763 $op->execute($this->_dbconn,
764 $this->createFlatXMLDataSet(
765 dirname(__FILE__) . '/dataset/uf_group_contact_activity_26.xml'
766 )
767 );
768 // hack: xml data set do not accept \ 1 (CRM_Core_DAO::VALUE_SEPARATOR)
769 CRM_Core_DAO::setFieldValue('CRM_Core_DAO_UFGroup', '26', 'group_type', 'Individual,Contact,Activity' . CRM_Core_DAO::VALUE_SEPARATOR . 'ActivityType:1');
770
771 $sourceContactId = $this->individualCreate();
772 $contactParams = array(
773 'first_name' => 'abc1',
774 'last_name' => 'xyz1',
775 'contact_type' => 'Individual',
776 'email' => 'abc1.xyz1@yahoo.com',
777 'api.address.create' => array(
778 'location_type_id' => 1,
779 'is_primary' => 1,
780 'name' => 'Saint Helier St',
781 'county' => 'Marin',
782 'country' => 'UNITED STATES',
783 'state_province' => 'Michigan',
784 'supplemental_address_1' => 'Hallmark Ct',
785 'supplemental_address_2' => 'Jersey Village',
786 ),
787 );
788
789 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
790
791 $keys = array_keys($contact['values']);
792 $contactId = array_pop($keys);
793
794 $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'])
795 );
796
797 $activityParams = array(
798 'source_contact_id' => $sourceContactId,
799 'assignee_contact_id' => $contactId,
800 'activity_type_id' => '1',
801 'subject' => 'Make-it-Happen Meeting',
802 'activity_date_time' => '2011-03-16 00:00:00',
803 'duration' => '120',
804 'location' => 'Pensulvania',
805 'details' => 'a test activity',
806 'status_id' => '1',
807 'priority_id' => '1',
808 );
809 $activity = $this->callAPISuccess('activity', 'create', $activityParams);
810
811 $activityValues = array_pop($activity['values']);
812
813 // valid parameters for above profile
814 $profileParams = array(
815 'profile_id' => 26,
816 'contact_id' => $contactId,
817 'activity_id' => $activityValues['id'],
818 );
819
820 // expected result of above created profile
821 $expected = array(
822 'first_name' => 'abc1',
823 'last_name' => 'xyz1',
824 'email-Primary' => 'abc1.xyz1@yahoo.com',
825 'activity_subject' => 'Make-it-Happen Meeting',
826 'activity_details' => 'a test activity',
827 'activity_duration' => '120',
828 'activity_date_time' => '2011-03-16 00:00:00',
829 'activity_status_id' => '1',
830 );
831
832 return array($profileParams, $expected);
833 }
834
835 /**
836 * Create a profile.
837 */
838 public function _createIndividualProfile() {
839 $ufGroupParams = array(
840 'group_type' => 'Individual,Contact',
841 // really we should remove this & test the ufField create sets it
842 'name' => 'test_individual_contact_profile',
843 'title' => 'Flat Coffee',
844 'api.uf_field.create' => array(
845 array(
846 'field_name' => 'first_name',
847 'is_required' => 1,
848 'visibility' => 'Public Pages and Listings',
849 'field_type' => 'Individual',
850 'label' => 'First Name',
851 ),
852 array(
853 'field_name' => 'last_name',
854 'is_required' => 1,
855 'visibility' => 'Public Pages and Listings',
856 'field_type' => 'Individual',
857 'label' => 'Last Name',
858 ),
859 array(
860 'field_name' => 'email',
861 'is_required' => 1,
862 'visibility' => 'Public Pages and Listings',
863 'field_type' => 'Contact',
864 'label' => 'Email',
865 ),
866 array(
867 'field_name' => 'phone',
868 'is_required' => 1,
869 'visibility' => 'Public Pages and Listings',
870 'field_type' => 'Contact',
871 'location_type_id' => 1,
872 'phone_type_id' => 1,
873 'label' => 'Phone',
874 ),
875 array(
876 'field_name' => 'country',
877 'is_required' => 1,
878 'visibility' => 'Public Pages and Listings',
879 'field_type' => 'Contact',
880 'location_type_id' => 1,
881 'label' => 'Country',
882 ),
883 array(
884 'field_name' => 'state_province',
885 'is_required' => 1,
886 'visibility' => 'Public Pages and Listings',
887 'field_type' => 'Contact',
888 'location_type_id' => 1,
889 'label' => 'State Province',
890 ),
891 array(
892 'field_name' => 'postal_code',
893 'is_required' => 0,
894 'field_type' => 'Contact',
895 'location_type_id' => 1,
896 'label' => 'State Province',
897 ),
898 ),
899 );
900 $profile = $this->callAPISuccess('uf_group', 'create', $ufGroupParams);
901 $this->_profileID = $profile['id'];
902 }
903
904 /**
905 * @param int $profileID
906 */
907 public function _addCustomFieldToProfile($profileID) {
908 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, '');
909 $this->uFFieldCreate(array(
910 'uf_group_id' => $profileID,
911 'field_name' => 'custom_' . $ids['custom_field_id'],
912 'contact_type' => 'Contact',
913 ));
914 }
915
916 }