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