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