61f3ef99b18fd63882ca531b9e0f9725fc382359
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' => '',
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
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]);
301 /////////////// test $this->callAPISuccess3_profile_set //////////////////
304 * check Without ProfileId
306 function testProfileSubmitWithoutProfileId() {
310 $result = $this->callAPIFailure('profile', 'submit', $params,
311 'Mandatory key(s) missing from params array: profile_id'
316 * check with no invalid profile Id
318 function testProfileSubmitInvalidProfileId() {
321 'profile_id' => 1000,
323 $result = $this->callAPIFailure('profile', 'submit', $params);
327 * check with missing required field in profile
329 function testProfileSubmitCheckProfileRequired() {
330 $pofileFieldValues = $this->_createIndividualContact();
331 current($pofileFieldValues);
332 $contactId = key($pofileFieldValues);
333 $updateParams = array(
334 'first_name' => 'abc2',
335 'last_name' => 'xyz2',
336 'phone-1-1' => '022 321 826',
337 'country-1' => '1013',
338 'state_province-1' => '1000',
341 $params = array_merge(array('profile_id' => 25, 'contact_id' => $contactId),
345 $result = $this->callAPIFailure('profile', 'submit', $params,
346 'Missing required parameters for profile id 25: email-Primary'
353 function testProfileSubmit() {
354 $pofileFieldValues = $this->_createIndividualContact();
355 current($pofileFieldValues);
356 $contactId = key($pofileFieldValues);
358 $updateParams = array(
359 'first_name' => 'abc2',
360 'last_name' => 'xyz2',
361 'email-Primary' => 'abc2.xyz2@gmail.com',
362 'phone-1-1' => '022 321 826',
363 'country-1' => '1013',
364 'state_province-1' => '1000',
367 $params = array_merge(array(
369 'contact_id' => $contactId,
372 $result = $this->callAPIAndDocument('profile', 'submit', $params, __FUNCTION__
, __FILE__
);
376 'contact_id' => $contactId,
378 $profileDetails = $this->callAPISuccess('profile', 'get', $getParams);
380 foreach ($updateParams as $profileField => $value) {
381 $this->assertEquals($value, CRM_Utils_Array
::value($profileField, $profileDetails['values']), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$profileField}"
387 * set is deprecated but we need to ensure it still works
389 function testLegacySet() {
390 $pofileFieldValues = $this->_createIndividualContact();
391 current($pofileFieldValues);
392 $contactId = key($pofileFieldValues);
394 $updateParams = array(
395 'first_name' => 'abc2',
396 'last_name' => 'xyz2',
397 'email-Primary' => 'abc2.xyz2@gmail.com',
398 'phone-1-1' => '022 321 826',
399 'country-1' => '1013',
400 'state_province-1' => '1000',
403 $params = array_merge(array(
405 'contact_id' => $contactId,
408 $result = $this->callAPISuccess('profile', 'set', $params);
409 $this->assertArrayKeyExists('values', $result);
412 'contact_id' => $contactId,
414 $profileDetails = $this->callAPISuccess('profile', 'get', $getParams);
416 foreach ($updateParams as $profileField => $value) {
417 $this->assertEquals($value, CRM_Utils_Array
::value($profileField, $profileDetails['values']), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$profileField}"
422 * check contact activity profile without activity id
424 function testContactActivitySubmitWithoutActivityId() {
425 list($params, $expected) = $this->_createContactWithActivity();
427 $params = array_merge($params, $expected);
428 unset($params['activity_id']);
429 $result = $this->callAPIFailure('profile', 'submit', $params);
430 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: activity_id');
434 * check contact activity profile wrong activity id
436 function testContactActivitySubmitWrongActivityId() {
437 list($params, $expected) = $this->_createContactWithActivity();
438 $params = array_merge($params, $expected);
439 $params['activity_id'] = 100001;
440 $result = $this->callAPIFailure('profile', 'submit', $params);
441 $this->assertEquals($result['error_message'], 'Invalid Activity Id (aid).');
445 * check contact activity profile with wrong activity type
447 function testContactActivitySubmitWrongActivityType() {
448 //flush cache by calling with reset
449 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
451 $sourceContactId = $this->householdCreate();
453 $activityparams = array(
454 'source_contact_id' => $sourceContactId,
455 'activity_type_id' => '2',
456 'subject' => 'Test activity',
457 'activity_date_time' => '20110316',
459 'location' => 'Pensulvania',
460 'details' => 'a test activity',
462 'priority_id' => '1',
465 $activity = $this->callAPISuccess('activity', 'create', $activityparams);
467 $activityValues = array_pop($activity['values']);
469 list($params, $expected) = $this->_createContactWithActivity();
471 $params = array_merge($params, $expected);
472 $params['activity_id'] = $activityValues['id'];
473 $result = $this->callAPIFailure('profile', 'submit', $params,
474 'This activity cannot be edited or viewed via this profile.');
478 * check contact activity profile with success
480 function testContactActivitySubmitSuccess() {
481 list($params, $expected) = $this->_createContactWithActivity();
483 $updateParams = array(
484 'first_name' => 'abc2',
485 'last_name' => 'xyz2',
486 'email-Primary' => 'abc2.xyz2@yahoo.com',
487 'activity_subject' => 'Test Meeting',
488 'activity_details' => 'a test activity details',
489 'activity_duration' => '100',
490 'activity_date_time' => '03/08/2010',
491 'activity_status_id' => '2',
493 $profileParams = array_merge($params, $updateParams);
494 $profile = $this->callAPISuccess('profile', 'submit', $profileParams);
495 $result = $this->callAPISuccess('profile', 'get', $params);
497 foreach ($updateParams as $profileField => $value) {
498 $this->assertEquals($value, CRM_Utils_Array
::value($profileField, $result['values']), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$profileField}"
504 * check profile apply Without ProfileId
506 function testProfileApplyWithoutProfileId() {
510 $result = $this->callAPIFailure('profile', 'apply', $params,
511 'Mandatory key(s) missing from params array: profile_id');
515 * check profile apply with no invalid profile Id
517 function testProfileApplyInvalidProfileId() {
520 'profile_id' => 1000,
522 $result = $this->callAPIFailure('profile', 'apply', $params);
528 function testProfileApply() {
529 $pofileFieldValues = $this->_createIndividualContact();
530 current($pofileFieldValues);
531 $contactId = key($pofileFieldValues);
535 'contact_id' => $contactId,
536 'first_name' => 'abc2',
537 'last_name' => 'xyz2',
538 'email-Primary' => 'abc2.xyz2@gmail.com',
539 'phone-1-1' => '022 321 826',
540 'country-1' => '1013',
541 'state_province-1' => '1000',
544 $result = $this->callAPIAndDocument('profile', 'apply', $params, __FUNCTION__
, __FILE__
);
546 // Expected field values
547 $expected['contact'] = array(
548 'contact_id' => $contactId,
549 'contact_type' => 'Individual',
550 'first_name' => 'abc2',
551 'last_name' => 'xyz2',
553 $expected['email'] = array(
554 'location_type_id' => 1,
556 'email' => 'abc2.xyz2@gmail.com',
559 $expected['phone'] = array(
560 'location_type_id' => 1,
562 'phone_type_id' => 1,
563 'phone' => '022 321 826',
565 $expected['address'] = array(
566 'location_type_id' => 1,
568 'country_id' => 1013,
569 'state_province_id' => 1000,
572 foreach ($expected['contact'] as $field => $value) {
573 $this->assertEquals($value, CRM_Utils_Array
::value($field, $result['values']), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$field}"
578 'email', 'phone', 'address') as $fieldType) {
579 $typeValues = array_pop($result['values'][$fieldType]);
580 foreach ($expected[$fieldType] as $field => $value) {
581 $this->assertEquals($value, CRM_Utils_Array
::value($field, $typeValues), "In line " . __LINE__
. " error message: " . "missing/mismatching value for {$field} ({$fieldType})"
588 * Helper function to create an Individual with address/email/phone info. Import UF Group and UF Fields
590 function _createIndividualContact($params = array()) {
591 $contactParams = array_merge(array(
592 'first_name' => 'abc1',
593 'last_name' => 'xyz1',
594 'email' => 'abc1.xyz1@yahoo.com',
595 'api.address.create' => array(
596 'location_type_id' => 1,
598 'street_address' => '5 Saint Helier St',
600 'country' => 'United States',
601 'state_province' => 'Michigan',
602 'supplemental_address_1' => 'Hallmark Ct',
603 'supplemental_address_2' => 'Jersey Village',
604 'postal_code' => '90210',
605 'city' => 'Gotham City',
608 'api.phone.create' => array(
609 'location_type_id' => '1',
610 'phone' => '021 512 755',
611 'phone_type_id' => '1',
617 $contactID = $this->individualCreate($contactParams);
618 $this->_createIndividualProfile();
619 // expected result of above created profile with contact Id $contactId
620 $profileData[$contactID] = array(
621 'first_name' => 'abc1',
622 'last_name' => 'xyz1',
623 'email-Primary' => 'abc1.xyz1@yahoo.com',
624 'phone-1-1' => '021 512 755',
625 'country-1' => '1228',
626 'state_province-1' => '1021',
632 function _createContactWithActivity() {
633 // @TODO: Create profile with custom fields
634 $op = new PHPUnit_Extensions_Database_Operation_Insert();
635 $op->execute($this->_dbconn
,
636 new PHPUnit_Extensions_Database_DataSet_FlatXMLDataSet(
637 dirname(__FILE__
) . '/dataset/uf_group_contact_activity_26.xml'
640 // hack: xml data set do not accept \ 1 (CRM_Core_DAO::VALUE_SEPARATOR)
641 CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_UFGroup', '26', 'group_type', 'Individual,Contact,Activity' . CRM_Core_DAO
::VALUE_SEPARATOR
. 'ActivityType:1');
643 $sourceContactId = $this->individualCreate();
644 $contactParams = array(
645 'first_name' => 'abc1',
646 'last_name' => 'xyz1',
647 'contact_type' => 'Individual',
648 'email' => 'abc1.xyz1@yahoo.com',
649 'api.address.create' => array(
650 'location_type_id' => 1,
652 'name' => 'Saint Helier St',
654 'country' => 'United States',
655 'state_province' => 'Michigan',
656 'supplemental_address_1' => 'Hallmark Ct',
657 'supplemental_address_2' => 'Jersey Village',
661 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
663 $keys = array_keys($contact['values']);
664 $contactId = array_pop($keys);
666 $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'])
669 $activityParams = array(
670 'source_contact_id' => $sourceContactId,
671 'assignee_contact_id' => $contactId,
672 'activity_type_id' => '1',
673 'subject' => 'Make-it-Happen Meeting',
674 'activity_date_time' => '20110316',
676 'location' => 'Pensulvania',
677 'details' => 'a test activity',
679 'priority_id' => '1',
681 $activity = $this->callAPISuccess('activity', 'create', $activityParams);
683 $activityValues = array_pop($activity['values']);
685 // valid parameters for above profile
686 $profileParams = array(
688 'contact_id' => $contactId,
689 'activity_id' => $activityValues['id'],
692 // expected result of above created profile
694 'first_name' => 'abc1',
695 'last_name' => 'xyz1',
696 'email-Primary' => 'abc1.xyz1@yahoo.com',
697 'activity_subject' => 'Make-it-Happen Meeting',
698 'activity_details' => 'a test activity',
699 'activity_duration' => '120',
700 'activity_date_time_time' => '12:00AM',
701 'activity_date_time' => '03/16/2011',
702 'activity_status_id' => '1',
705 return array($profileParams, $expected);
710 function _createIndividualProfile() {
711 // Create new profile having group_type: Contact,Individual
712 $op = new PHPUnit_Extensions_Database_Operation_Insert();
713 $op->execute($this->_dbconn
,
714 new PHPUnit_Extensions_Database_DataSet_XMLDataSet(
715 dirname(__FILE__
) . "/dataset/uf_group_25.xml"
718 // Create Contact + Idividual fields for profile
719 $op = new PHPUnit_Extensions_Database_Operation_Insert();
720 $op->execute($this->_dbconn
,
721 new PHPUnit_Extensions_Database_DataSet_XMLDataSet(
722 dirname(__FILE__
) . "/dataset/uf_field_uf_group_25.xml"
725 $this->_profileID
= 25;
728 function _addCustomFieldToProfile($profileID) {
729 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, '');
730 $this->uFFieldCreate(array('uf_group_id' => $profileID, 'field_name' => 'custom_' . $ids['custom_field_id'], 'contact_type' => 'Contact'));