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