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