4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.4 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
30 * Include class definitions
32 require_once 'tests/phpunit/CiviTest/CiviUnitTestCase.php';
35 * Test APIv3 civicrm_profile_* functions
39 class api_v3_ProfileTest
extends CiviUnitTestCase
{
40 protected $_apiversion;
41 protected $_profileID;
44 'name' => 'Profile Test',
45 'description' => 'Test all profile API methods.',
46 'group' => 'CiviCRM API Tests',
51 $this->_apiversion
= 3;
53 $config = CRM_Core_Config
::singleton();
54 $config->countryLimit
[1] = 1013;
55 $config->stateLimit
[1] = 1013;
60 $this->quickCleanup(array(
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 (25, 26)');
69 ////////////// test $this->callAPISuccess3_profile_get //////////////////
72 * check Without ProfileId
74 function testProfileGetWithoutProfileId() {
78 $result = $this->callAPIFailure('profile', 'get', $params,
79 'Mandatory key(s) missing from params array: profile_id'
84 * check with no invalid profile Id
86 function testProfileGetInvalidProfileId() {
91 $result = $this->callAPIFailure('profile', 'get', $params);
97 function testProfileGet() {
98 $pofileFieldValues = $this->_createIndividualContact();
99 $expected = current($pofileFieldValues);
100 $contactId = key($pofileFieldValues);
103 'contact_id' => $contactId,
105 $result = $this->callAPISuccess('profile', 'get', $params);
106 foreach ($expected as $profileField => $value) {
107 $this->assertEquals($value, CRM_Utils_Array
::value($profileField, $result['values']), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$profileField}"
112 function testProfileGetMultiple() {
113 $pofileFieldValues = $this->_createIndividualContact();
114 $expected = current($pofileFieldValues);
115 $contactId = key($pofileFieldValues);
117 'profile_id' => array(25, 1, 'Billing'),
118 'contact_id' => $contactId,
121 $result = $this->callAPIAndDocument('profile', 'get', $params, __FUNCTION__
, __FILE__
);
122 foreach ($expected as $profileField => $value) {
123 $this->assertEquals($value, CRM_Utils_Array
::value($profileField, $result['values'][25]), " error message: " . "missing/mismatching value for {$profileField}");
125 $this->assertEquals('abc1', $result['values'][1]['first_name'], " error message: " . "missing/mismatching value for {$profileField}");
126 $this->assertFalse(array_key_exists('email-Primary', $result['values'][1]), 'profile 1 doesn not include email');
127 $this->assertEquals($result['values']['Billing'], array(
128 'billing_first_name' => 'abc1',
129 'billing_middle_name' => 'J.',
130 'billing_last_name' => 'xyz1',
131 'billing_street_address-5' => '5 Saint Helier St',
132 'billing_city-5' => 'Gotham City',
133 'billing_state_province_id-5' => '1021',
134 'billing_country_id-5' => '1228',
135 'billing-email-5' => 'abc1.xyz1@yahoo.com',
136 'billing_postal_code-5' => '90210',
137 'billing-email-5' => 'abc1.xyz1@yahoo.com',
138 'email-5' => 'abc1.xyz1@yahoo.com',
142 function testProfileGetBillingUseIsBillingLocation() {
143 $individual = $this->_createIndividualContact();
144 $contactId = key($individual);
145 $this->callAPISuccess('address', 'create', array(
147 'street_address' => 'is billing st',
148 'location_type_id' => 2,
149 'contact_id' => $contactId,
152 $expected = current($individual);
155 'profile_id' => array(25, 1, 'Billing'),
156 'contact_id' => $contactId,
159 $result = $this->callAPISuccess('profile', 'get', $params);
160 $this->assertEquals('abc1', $result['values'][1]['first_name']);
161 $this->assertEquals(array(
162 'billing_first_name' => 'abc1',
163 'billing_middle_name' => 'J.',
164 'billing_last_name' => 'xyz1',
165 'billing_street_address-5' => 'is billing st',
166 'billing_city-5' => '',
167 'billing_state_province_id-5' => '',
168 'billing_country_id-5' => '',
169 'billing-email-5' => 'abc1.xyz1@yahoo.com',
170 'email-5' => 'abc1.xyz1@yahoo.com',
171 'billing_postal_code-5' => '',
172 ), $result['values']['Billing']);
175 function testProfileGetMultipleHasBillingLocation() {
176 $individual = $this->_createIndividualContact();
177 $contactId = key($individual);
178 $this->callAPISuccess('address', 'create', array('contact_id' => $contactId , 'street_address' => '25 Big Street', 'city' => 'big city', 'location_type_id' => 5));
179 $this->callAPISuccess('email', 'create', array('contact_id' => $contactId , 'email' => 'big@once.com', 'location_type_id' => 2, 'is_billing' => 1));
181 $expected = current($individual);
184 'profile_id' => array(25, 1, 'Billing'),
185 'contact_id' => $contactId,
188 $result = $this->callAPISuccess('profile', 'get', $params, __FUNCTION__
, __FILE__
);
189 $this->assertEquals('abc1', $result['values'][1]['first_name']);
190 $this->assertEquals($result['values']['Billing'], array(
191 'billing_first_name' => 'abc1',
192 'billing_middle_name' => 'J.',
193 'billing_last_name' => 'xyz1',
194 'billing_street_address-5' => '25 Big Street',
195 'billing_city-5' => 'big city',
196 'billing_state_province_id-5' => '',
197 'billing_country_id-5' => '',
198 'billing-email-5' => 'big@once.com',
199 'email-5' => 'big@once.com',
200 'billing_postal_code-5' => '',
206 * check contact activity profile without activity id
208 function testContactActivityGetWithoutActivityId() {
209 list($params, $expected) = $this->_createContactWithActivity();
211 unset($params['activity_id']);
212 $result = $this->callAPIFailure('profile', 'get', $params,
213 'Mandatory key(s) missing from params array: activity_id');
217 * check contact activity profile wrong activity id
219 function testContactActivityGetWrongActivityId() {
220 list($params, $expected) = $this->_createContactWithActivity();
222 $params['activity_id'] = 100001;
223 $result = $this->callAPIFailure('profile', 'get', $params,
224 'Invalid Activity Id (aid).');
228 * check contact activity profile with wrong activity type
230 function testContactActivityGetWrongActivityType() {
231 //flush cache by calling with reset
232 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
234 $sourceContactId = $this->householdCreate();
236 $activityparams = array(
237 'source_contact_id' => $sourceContactId,
238 'activity_type_id' => '2',
239 'subject' => 'Test activity',
240 'activity_date_time' => '20110316',
242 'location' => 'Pensulvania',
243 'details' => 'a test activity',
245 'priority_id' => '1',
248 $activity = $this->callAPISuccess('activity', 'create', $activityparams);
250 $activityValues = array_pop($activity['values']);
252 list($params, $expected) = $this->_createContactWithActivity();
254 $params['activity_id'] = $activityValues['id'];
255 $result = $this->callAPIFailure('profile', 'get', $params,
256 'This activity cannot be edited or viewed via this profile.'
261 * check contact activity profile with success
263 function testContactActivityGetSuccess() {
264 list($params, $expected) = $this->_createContactWithActivity();
266 $result = $this->callAPISuccess('profile', 'get', $params);
268 foreach ($expected as $profileField => $value) {
269 $this->assertEquals($value, CRM_Utils_Array
::value($profileField, $result['values']), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$profileField}"
275 * Check getfields works & gives us our fields
277 function testGetFields() {
278 $this->_createIndividualProfile();
279 $this->_addCustomFieldToProfile($this->_profileID
);
280 $result = $this->callAPIAndDocument('profile', 'getfields', array('action' => 'submit', 'profile_id' => 25), __FUNCTION__
, __FILE__
,
281 'demonstrates retrieving profile fields passing in an id');
282 $this->assertArrayKeyExists('first_name', $result['values']);
283 $this->assertEquals('2', $result['values']['first_name']['type']);
284 $this->assertEquals('Email', $result['values']['email']['title']);
285 $this->assertEquals('civicrm_state_province', $result['values']['state_province-1']['pseudoconstant']['table']);
286 $this->assertEquals('defaultValue', $result['values']['custom_1']['default_value']);
287 $this->assertFalse(array_key_exists('participant_status', $result['values']));
290 * Check getfields works & gives us our fields - partipant profile
292 function testGetFieldsParticipantProfile() {
293 $result = $this->callAPISuccess('profile', 'getfields', array(
294 'action' => 'submit',
295 'profile_id' => 'participant_status',
296 'get_options' => 'all')
298 $this->assertTrue(array_key_exists('participant_status', $result['values']));
299 $this->assertEquals('Attended', $result['values']['participant_status']['options'][2]);
303 * Check getfields works & gives us our fields - membership_batch_entry
304 * (getting to the end with no e-notices is pretty good evidence it's working)
306 function testGetFieldsMembershipBatchProfile() {
307 $result = $this->callAPISuccess('profile', 'getfields', array(
308 'action' => 'submit',
309 'profile_id' => 'membership_batch_entry',
310 'get_options' => 'all')
312 $this->assertTrue(array_key_exists('total_amount', $result['values']));
313 $this->assertEquals(12, $result['values']['receive_date']['type']);
317 * Check getfields works & gives us our fields - do them all
318 * (getting to the end with no e-notices is pretty good evidence it's working)
320 function testGetFieldsAllProfiles() {
321 $result = $this->callAPISuccess('uf_group', 'get', array('return' => 'id'));
322 $profileIDs = array_keys($result['values']);
323 foreach ($profileIDs as $profileID) {
324 $result = $this->callAPISuccess('profile', 'getfields', array(
325 'action' => 'submit',
326 'profile_id' => $profileID,
327 'get_options' => 'all')
331 /////////////// test $this->callAPISuccess3_profile_set //////////////////
334 * check Without ProfileId
336 function testProfileSubmitWithoutProfileId() {
340 $result = $this->callAPIFailure('profile', 'submit', $params,
341 'Mandatory key(s) missing from params array: profile_id'
346 * check with no invalid profile Id
348 function testProfileSubmitInvalidProfileId() {
351 'profile_id' => 1000,
353 $result = $this->callAPIFailure('profile', 'submit', $params);
357 * check with missing required field in profile
359 function testProfileSubmitCheckProfileRequired() {
360 $pofileFieldValues = $this->_createIndividualContact();
361 current($pofileFieldValues);
362 $contactId = key($pofileFieldValues);
363 $updateParams = array(
364 'first_name' => 'abc2',
365 'last_name' => 'xyz2',
366 'phone-1-1' => '022 321 826',
367 'country-1' => '1013',
368 'state_province-1' => '1000',
371 $params = array_merge(array('profile_id' => 25, 'contact_id' => $contactId),
375 $result = $this->callAPIFailure('profile', 'submit', $params,
376 'Missing required parameters for profile id 25: email-Primary'
383 function testProfileSubmit() {
384 $pofileFieldValues = $this->_createIndividualContact();
385 current($pofileFieldValues);
386 $contactId = key($pofileFieldValues);
388 $updateParams = array(
389 'first_name' => 'abc2',
390 'last_name' => 'xyz2',
391 'email-Primary' => 'abc2.xyz2@gmail.com',
392 'phone-1-1' => '022 321 826',
393 'country-1' => '1013',
394 'state_province-1' => '1000',
397 $params = array_merge(array(
399 'contact_id' => $contactId,
402 $result = $this->callAPIAndDocument('profile', 'submit', $params, __FUNCTION__
, __FILE__
);
406 'contact_id' => $contactId,
408 $profileDetails = $this->callAPISuccess('profile', 'get', $getParams);
410 foreach ($updateParams as $profileField => $value) {
411 $this->assertEquals($value, CRM_Utils_Array
::value($profileField, $profileDetails['values']), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$profileField}"
417 * set is deprecated but we need to ensure it still works
419 function testLegacySet() {
420 $pofileFieldValues = $this->_createIndividualContact();
421 current($pofileFieldValues);
422 $contactId = key($pofileFieldValues);
424 $updateParams = array(
425 'first_name' => 'abc2',
426 'last_name' => 'xyz2',
427 'email-Primary' => 'abc2.xyz2@gmail.com',
428 'phone-1-1' => '022 321 826',
429 'country-1' => '1013',
430 'state_province-1' => '1000',
433 $params = array_merge(array(
435 'contact_id' => $contactId,
438 $result = $this->callAPISuccess('profile', 'set', $params);
439 $this->assertArrayKeyExists('values', $result);
442 'contact_id' => $contactId,
444 $profileDetails = $this->callAPISuccess('profile', 'get', $getParams);
446 foreach ($updateParams as $profileField => $value) {
447 $this->assertEquals($value, CRM_Utils_Array
::value($profileField, $profileDetails['values']), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$profileField}"
452 * check contact activity profile without activity id
454 function testContactActivitySubmitWithoutActivityId() {
455 list($params, $expected) = $this->_createContactWithActivity();
457 $params = array_merge($params, $expected);
458 unset($params['activity_id']);
459 $result = $this->callAPIFailure('profile', 'submit', $params);
460 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: activity_id');
464 * check contact activity profile wrong activity id
466 function testContactActivitySubmitWrongActivityId() {
467 list($params, $expected) = $this->_createContactWithActivity();
468 $params = array_merge($params, $expected);
469 $params['activity_id'] = 100001;
470 $result = $this->callAPIFailure('profile', 'submit', $params);
471 $this->assertEquals($result['error_message'], 'Invalid Activity Id (aid).');
475 * check contact activity profile with wrong activity type
477 function testContactActivitySubmitWrongActivityType() {
478 //flush cache by calling with reset
479 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
481 $sourceContactId = $this->householdCreate();
483 $activityparams = array(
484 'source_contact_id' => $sourceContactId,
485 'activity_type_id' => '2',
486 'subject' => 'Test activity',
487 'activity_date_time' => '20110316',
489 'location' => 'Pensulvania',
490 'details' => 'a test activity',
492 'priority_id' => '1',
495 $activity = $this->callAPISuccess('activity', 'create', $activityparams);
497 $activityValues = array_pop($activity['values']);
499 list($params, $expected) = $this->_createContactWithActivity();
501 $params = array_merge($params, $expected);
502 $params['activity_id'] = $activityValues['id'];
503 $result = $this->callAPIFailure('profile', 'submit', $params,
504 'This activity cannot be edited or viewed via this profile.');
508 * check contact activity profile with success
510 function testContactActivitySubmitSuccess() {
511 list($params, $expected) = $this->_createContactWithActivity();
513 $updateParams = array(
514 'first_name' => 'abc2',
515 'last_name' => 'xyz2',
516 'email-Primary' => 'abc2.xyz2@yahoo.com',
517 'activity_subject' => 'Test Meeting',
518 'activity_details' => 'a test activity details',
519 'activity_duration' => '100',
520 'activity_date_time' => '03/08/2010',
521 'activity_status_id' => '2',
523 $profileParams = array_merge($params, $updateParams);
524 $profile = $this->callAPISuccess('profile', 'submit', $profileParams);
525 $result = $this->callAPISuccess('profile', 'get', $params);
527 foreach ($updateParams as $profileField => $value) {
528 $this->assertEquals($value, CRM_Utils_Array
::value($profileField, $result['values']), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$profileField}"
534 * check profile apply Without ProfileId
536 function testProfileApplyWithoutProfileId() {
540 $result = $this->callAPIFailure('profile', 'apply', $params,
541 'Mandatory key(s) missing from params array: profile_id');
545 * check profile apply with no invalid profile Id
547 function testProfileApplyInvalidProfileId() {
550 'profile_id' => 1000,
552 $result = $this->callAPIFailure('profile', 'apply', $params);
558 function testProfileApply() {
559 $pofileFieldValues = $this->_createIndividualContact();
560 current($pofileFieldValues);
561 $contactId = key($pofileFieldValues);
565 'contact_id' => $contactId,
566 'first_name' => 'abc2',
567 'last_name' => 'xyz2',
568 'email-Primary' => 'abc2.xyz2@gmail.com',
569 'phone-1-1' => '022 321 826',
570 'country-1' => '1013',
571 'state_province-1' => '1000',
574 $result = $this->callAPIAndDocument('profile', 'apply', $params, __FUNCTION__
, __FILE__
);
576 // Expected field values
577 $expected['contact'] = array(
578 'contact_id' => $contactId,
579 'contact_type' => 'Individual',
580 'first_name' => 'abc2',
581 'last_name' => 'xyz2',
583 $expected['email'] = array(
584 'location_type_id' => 1,
586 'email' => 'abc2.xyz2@gmail.com',
589 $expected['phone'] = array(
590 'location_type_id' => 1,
592 'phone_type_id' => 1,
593 'phone' => '022 321 826',
595 $expected['address'] = array(
596 'location_type_id' => 1,
598 'country_id' => 1013,
599 'state_province_id' => 1000,
602 foreach ($expected['contact'] as $field => $value) {
603 $this->assertEquals($value, CRM_Utils_Array
::value($field, $result['values']), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$field}"
608 'email', 'phone', 'address') as $fieldType) {
609 $typeValues = array_pop($result['values'][$fieldType]);
610 foreach ($expected[$fieldType] as $field => $value) {
611 $this->assertEquals($value, CRM_Utils_Array
::value($field, $typeValues), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$field} ({$fieldType})"
618 * Helper function to create an Individual with address/email/phone info. Import UF Group and UF Fields
620 function _createIndividualContact($params = array()) {
621 $contactParams = array_merge(array(
622 'first_name' => 'abc1',
623 'last_name' => 'xyz1',
624 'email' => 'abc1.xyz1@yahoo.com',
625 'api.address.create' => array(
626 'location_type_id' => 1,
628 'street_address' => '5 Saint Helier St',
630 'country' => 'United States',
631 'state_province' => 'Michigan',
632 'supplemental_address_1' => 'Hallmark Ct',
633 'supplemental_address_2' => 'Jersey Village',
634 'postal_code' => '90210',
635 'city' => 'Gotham City',
638 'api.phone.create' => array(
639 'location_type_id' => '1',
640 'phone' => '021 512 755',
641 'phone_type_id' => '1',
647 $contactID = $this->individualCreate($contactParams);
648 $this->_createIndividualProfile();
649 // expected result of above created profile with contact Id $contactId
650 $profileData[$contactID] = array(
651 'first_name' => 'abc1',
652 'last_name' => 'xyz1',
653 'email-Primary' => 'abc1.xyz1@yahoo.com',
654 'phone-1-1' => '021 512 755',
655 'country-1' => '1228',
656 'state_province-1' => '1021',
662 function _createContactWithActivity() {
663 // @TODO: Create profile with custom fields
664 $op = new PHPUnit_Extensions_Database_Operation_Insert();
665 $op->execute($this->_dbconn
,
666 new PHPUnit_Extensions_Database_DataSet_FlatXMLDataSet(
667 dirname(__FILE__
) . '/dataset/uf_group_contact_activity_26.xml'
670 // hack: xml data set do not accept \ 1 (CRM_Core_DAO::VALUE_SEPARATOR)
671 CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_UFGroup', '26', 'group_type', 'Individual,Contact,Activity' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'ActivityType:1');
673 $sourceContactId = $this->individualCreate();
674 $contactParams = array(
675 'first_name' => 'abc1',
676 'last_name' => 'xyz1',
677 'contact_type' => 'Individual',
678 'email' => 'abc1.xyz1@yahoo.com',
679 'api.address.create' => array(
680 'location_type_id' => 1,
682 'name' => 'Saint Helier St',
684 'country' => 'United States',
685 'state_province' => 'Michigan',
686 'supplemental_address_1' => 'Hallmark Ct',
687 'supplemental_address_2' => 'Jersey Village',
691 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
693 $keys = array_keys($contact['values']);
694 $contactId = array_pop($keys);
696 $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'])
699 $activityParams = array(
700 'source_contact_id' => $sourceContactId,
701 'assignee_contact_id' => $contactId,
702 'activity_type_id' => '1',
703 'subject' => 'Make-it-Happen Meeting',
704 'activity_date_time' => '20110316',
706 'location' => 'Pensulvania',
707 'details' => 'a test activity',
709 'priority_id' => '1',
711 $activity = $this->callAPISuccess('activity', 'create', $activityParams);
713 $activityValues = array_pop($activity['values']);
715 // valid parameters for above profile
716 $profileParams = array(
718 'contact_id' => $contactId,
719 'activity_id' => $activityValues['id'],
722 // expected result of above created profile
724 'first_name' => 'abc1',
725 'last_name' => 'xyz1',
726 'email-Primary' => 'abc1.xyz1@yahoo.com',
727 'activity_subject' => 'Make-it-Happen Meeting',
728 'activity_details' => 'a test activity',
729 'activity_duration' => '120',
730 'activity_date_time_time' => '12:00AM',
731 'activity_date_time' => '03/16/2011',
732 'activity_status_id' => '1',
735 return array($profileParams, $expected);
740 function _createIndividualProfile() {
741 // Create new profile having group_type: Contact,Individual
742 $op = new PHPUnit_Extensions_Database_Operation_Insert();
743 $op->execute($this->_dbconn
,
744 new PHPUnit_Extensions_Database_DataSet_XMLDataSet(
745 dirname(__FILE__
) . "/dataset/uf_group_25.xml"
748 // Create Contact + Idividual fields for profile
749 $op = new PHPUnit_Extensions_Database_Operation_Insert();
750 $op->execute($this->_dbconn
,
751 new PHPUnit_Extensions_Database_DataSet_XMLDataSet(
752 dirname(__FILE__
) . "/dataset/uf_field_uf_group_25.xml"
755 $this->_profileID
= 25;
758 function _addCustomFieldToProfile($profileID) {
759 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, '');
760 $this->uFFieldCreate(array('uf_group_id' => $profileID, 'field_name' => 'custom_' . $ids['custom_field_id'], 'contact_type' => 'Contact'));