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