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