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